Search in sources :

Example 1 with SecretItem

use of com.microsoft.azure.keyvault.models.SecretItem in project azure-sdk-for-java by Azure.

the class SecretOperationsTest method listSecretVersions.

@Test
public void listSecretVersions() throws Exception {
    HashSet<String> secrets = new HashSet<String>();
    for (int i = 0; i < MAX_SECRETS; ++i) {
        int failureCount = 0;
        for (; ; ) {
            try {
                SecretBundle secret = keyVaultClient.setSecret(new SetSecretRequest.Builder(getVaultUri(), SECRET_NAME, SECRET_VALUE).build());
                secrets.add(secret.id());
                break;
            } catch (KeyVaultErrorException e) {
                ++failureCount;
                if (e.body().error().code().equals("Throttled")) {
                    System.out.println("Throttled!");
                    Thread.sleep(failureCount * 1500);
                    continue;
                }
                throw e;
            }
        }
    }
    PagedList<SecretItem> listResult = keyVaultClient.listSecretVersions(getVaultUri(), SECRET_NAME, PAGELIST_MAX_SECRETS);
    Assert.assertTrue(PAGELIST_MAX_SECRETS >= listResult.currentPage().items().size());
    listResult = keyVaultClient.listSecretVersions(getVaultUri(), SECRET_NAME);
    for (SecretItem item : listResult) {
        if (item != null) {
            secrets.remove(item.id());
        }
    }
    Assert.assertEquals(0, secrets.size());
    keyVaultClient.deleteSecret(getVaultUri(), SECRET_NAME);
}
Also used : SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) SecretItem(com.microsoft.azure.keyvault.models.SecretItem) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with SecretItem

use of com.microsoft.azure.keyvault.models.SecretItem in project azure-sdk-for-java by Azure.

the class AsyncOperationsTest method secretAsync.

@Test
public void secretAsync() throws Exception {
    String vault = getVaultUri();
    String secretname = "mySecret";
    String password = "password";
    SetSecretRequest setSecretRequest = new SetSecretRequest.Builder(vault, secretname, password).build();
    SecretBundle secretBundle = keyVaultClient.setSecretAsync(setSecretRequest, null).get();
    Assert.assertNotNull(secretBundle);
    UpdateSecretRequest updateSecretRequest = new UpdateSecretRequest.Builder(secretBundle.id()).build();
    secretBundle = keyVaultClient.updateSecretAsync(updateSecretRequest, null).get();
    Assert.assertNotNull(secretBundle);
    secretBundle = keyVaultClient.getSecretAsync(secretBundle.id(), null).get();
    Assert.assertNotNull(secretBundle);
    List<SecretItem> secretItems = keyVaultClient.listSecretsAsync(vault, 2, null).get();
    Assert.assertNotNull(secretItems);
    List<SecretItem> secretVersionItems = keyVaultClient.listSecretVersionsAsync(vault, secretname, 2, null).get();
    Assert.assertNotNull(secretVersionItems);
    secretBundle = keyVaultClient.deleteSecretAsync(vault, secretname, null).get();
    Assert.assertNotNull(secretBundle);
    try {
        keyVaultClient.deleteSecretAsync(vault, secretname, null).get();
    } catch (ExecutionException ex) {
        Throwable t = ex.getCause();
        if (t instanceof KeyVaultErrorException) {
            Assert.assertEquals("SecretNotFound", ((KeyVaultErrorException) t).body().error().code());
        } else
            throw ex;
    }
}
Also used : SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) SecretItem(com.microsoft.azure.keyvault.models.SecretItem) SetSecretRequest(com.microsoft.azure.keyvault.requests.SetSecretRequest) ExecutionException(java.util.concurrent.ExecutionException) UpdateSecretRequest(com.microsoft.azure.keyvault.requests.UpdateSecretRequest) Test(org.junit.Test)

Example 3 with SecretItem

use of com.microsoft.azure.keyvault.models.SecretItem in project azure-sdk-for-java by Azure.

the class SecretOperationsTest method listSecrets.

@Test
public void listSecrets() throws Exception {
    HashSet<String> secrets = new HashSet<String>();
    for (int i = 0; i < MAX_SECRETS; ++i) {
        int failureCount = 0;
        for (; ; ) {
            try {
                SecretBundle secret = keyVaultClient.setSecret(new SetSecretRequest.Builder(getVaultUri(), SECRET_NAME + i, SECRET_VALUE).build());
                SecretIdentifier id = new SecretIdentifier(secret.id());
                secrets.add(id.baseIdentifier());
                break;
            } catch (KeyVaultErrorException e) {
                ++failureCount;
                if (e.body().error().code().equals("Throttled")) {
                    System.out.println("Waiting to avoid throttling");
                    Thread.sleep(failureCount * 1500);
                    continue;
                }
                throw e;
            }
        }
    }
    PagedList<SecretItem> listResult = keyVaultClient.listSecrets(getVaultUri(), PAGELIST_MAX_SECRETS);
    Assert.assertTrue(PAGELIST_MAX_SECRETS >= listResult.currentPage().items().size());
    HashSet<String> toDelete = new HashSet<String>();
    for (SecretItem item : listResult) {
        if (item != null) {
            SecretIdentifier id = new SecretIdentifier(item.id());
            toDelete.add(id.name());
            secrets.remove(item.id());
        }
    }
    Assert.assertEquals(0, secrets.size());
    for (String secretName : toDelete) {
        try {
            keyVaultClient.deleteSecret(getVaultUri(), secretName);
        } catch (KeyVaultErrorException e) {
            // Ignore forbidden exception for certificate secrets that cannot be deleted
            if (!e.body().error().code().equals("Forbidden"))
                throw e;
        }
    }
}
Also used : SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) SecretIdentifier(com.microsoft.azure.keyvault.SecretIdentifier) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) SecretItem(com.microsoft.azure.keyvault.models.SecretItem) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

KeyVaultErrorException (com.microsoft.azure.keyvault.models.KeyVaultErrorException)3 SecretBundle (com.microsoft.azure.keyvault.models.SecretBundle)3 SecretItem (com.microsoft.azure.keyvault.models.SecretItem)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 SecretIdentifier (com.microsoft.azure.keyvault.SecretIdentifier)1 SetSecretRequest (com.microsoft.azure.keyvault.requests.SetSecretRequest)1 UpdateSecretRequest (com.microsoft.azure.keyvault.requests.UpdateSecretRequest)1 ExecutionException (java.util.concurrent.ExecutionException)1