Search in sources :

Example 6 with SecretIdentifier

use of com.microsoft.azure.keyvault.SecretIdentifier 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

SecretIdentifier (com.microsoft.azure.keyvault.SecretIdentifier)6 SecretBundle (com.microsoft.azure.keyvault.models.SecretBundle)6 KeyVaultErrorException (com.microsoft.azure.keyvault.models.KeyVaultErrorException)5 Test (org.junit.Test)5 X509Certificate (java.security.cert.X509Certificate)4 CertificateBundle (com.microsoft.azure.keyvault.models.CertificateBundle)3 CertificatePolicy (com.microsoft.azure.keyvault.models.CertificatePolicy)3 SecretProperties (com.microsoft.azure.keyvault.models.SecretProperties)3 KeyStore (java.security.KeyStore)3 Attributes (com.microsoft.azure.keyvault.models.Attributes)2 CertificateAttributes (com.microsoft.azure.keyvault.models.CertificateAttributes)2 CertificateOperation (com.microsoft.azure.keyvault.models.CertificateOperation)2 IssuerParameters (com.microsoft.azure.keyvault.models.IssuerParameters)2 X509CertificateProperties (com.microsoft.azure.keyvault.models.X509CertificateProperties)2 CreateCertificateRequest (com.microsoft.azure.keyvault.requests.CreateCertificateRequest)2 DateTime (org.joda.time.DateTime)2 KeyIdentifier (com.microsoft.azure.keyvault.KeyIdentifier)1 AdministratorDetails (com.microsoft.azure.keyvault.models.AdministratorDetails)1 IssuerBundle (com.microsoft.azure.keyvault.models.IssuerBundle)1 IssuerCredentials (com.microsoft.azure.keyvault.models.IssuerCredentials)1