Search in sources :

Example 16 with KeyVaultErrorException

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

the class KeyOperationsTest method crudOperations.

@Test
public void crudOperations() throws Exception {
    KeyBundle createdBundle;
    {
        // Create key
        createdBundle = keyVaultClient.createKey(new CreateKeyRequest.Builder(getVaultUri(), KEY_NAME, JsonWebKeyType.RSA).build());
        validateRsaKeyBundle(createdBundle, getVaultUri(), KEY_NAME, JsonWebKeyType.RSA, null, null);
    }
    // Key identifier.
    KeyIdentifier keyId = new KeyIdentifier(createdBundle.key().kid());
    {
        // Get key using kid WO version
        KeyBundle readBundle = keyVaultClient.getKey(keyId.baseIdentifier());
        compareKeyBundles(createdBundle, readBundle);
    }
    {
        // Get key using full kid as defined in the bundle
        KeyBundle readBundle = keyVaultClient.getKey(createdBundle.key().kid());
        compareKeyBundles(createdBundle, readBundle);
    }
    {
        // Get key using vault and key name.
        KeyBundle readBundle = keyVaultClient.getKey(getVaultUri(), KEY_NAME);
        compareKeyBundles(createdBundle, readBundle);
    }
    {
        // Get key using vault, key name and version.
        KeyBundle readBundle = keyVaultClient.getKey(getVaultUri(), KEY_NAME, keyId.version());
        compareKeyBundles(createdBundle, readBundle);
    }
    {
        // Get key using vault, key name and a null version.
        KeyBundle readBundle = keyVaultClient.getKey(getVaultUri(), KEY_NAME);
        compareKeyBundles(createdBundle, readBundle);
    }
    {
        // Update key using the kid as defined in the bundle
        // First we create a bundle with the modified attributes.
        createdBundle.attributes().withExpires(new DateTime().withMonthOfYear(2).withDayOfMonth(1).withYear(2050));
        List<JsonWebKeyOperation> key_ops = Arrays.asList(JsonWebKeyOperation.ENCRYPT, JsonWebKeyOperation.DECRYPT);
        Map<String, String> tags = new HashMap<String, String>();
        tags.put("foo", "baz");
        createdBundle.key().withKeyOps(key_ops);
        createdBundle.withTags(tags);
        // Perform the operation.
        KeyBundle updatedBundle = keyVaultClient.updateKey(new UpdateKeyRequest.Builder(createdBundle.key().kid()).withKeyOperations(key_ops).withAttributes(createdBundle.attributes()).withTags(createdBundle.tags()).build());
        compareKeyBundles(createdBundle, updatedBundle);
        // Subsequent operations must use the updated bundle for comparison.
        createdBundle = updatedBundle;
    }
    {
        // Update key using vault and key name.
        // First we create a bundle with the modified attributes.
        createdBundle.attributes().withNotBefore(new DateTime().withMonthOfYear(2).withDayOfMonth(1).withYear(2000));
        List<JsonWebKeyOperation> key_ops = Arrays.asList(JsonWebKeyOperation.SIGN, JsonWebKeyOperation.VERIFY);
        createdBundle.key().withKeyOps(key_ops);
        Map<String, String> tags = new HashMap<String, String>();
        tags.put("foo", "baz");
        createdBundle.withTags(tags);
        // Perform the operation.
        KeyBundle updatedBundle = keyVaultClient.updateKey(new UpdateKeyRequest.Builder(getVaultUri(), KEY_NAME).withKeyOperations(key_ops).withAttributes(createdBundle.attributes()).withTags(createdBundle.tags()).build());
        compareKeyBundles(createdBundle, updatedBundle);
    }
    {
        // Delete key
        KeyBundle deleteBundle = keyVaultClient.deleteKey(getVaultUri(), KEY_NAME);
        compareKeyBundles(createdBundle, deleteBundle);
    }
    {
        // Expects a key not found
        try {
            keyVaultClient.getKey(keyId.baseIdentifier());
        } catch (KeyVaultErrorException e) {
            Assert.assertNotNull(e.body().error());
            Assert.assertEquals("KeyNotFound", e.body().error().code());
        }
    }
}
Also used : KeyIdentifier(com.microsoft.azure.keyvault.KeyIdentifier) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) PagedList(com.microsoft.azure.PagedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 17 with KeyVaultErrorException

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

the class KeyOperationsTest method listKeys.

@Test
public void listKeys() throws Exception {
    HashSet<String> keys = new HashSet<String>();
    for (int i = 0; i < MAX_KEYS; ++i) {
        int failureCount = 0;
        for (; ; ) {
            try {
                KeyBundle createdBundle = keyVaultClient.createKey(new CreateKeyRequest.Builder(getVaultUri(), KEY_NAME + i, JsonWebKeyType.RSA).build());
                KeyIdentifier kid = new KeyIdentifier(createdBundle.key().kid());
                keys.add(kid.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<KeyItem> listResult = keyVaultClient.listKeys(getVaultUri(), PAGELIST_MAX_KEYS);
    Assert.assertTrue(PAGELIST_MAX_KEYS >= listResult.currentPage().items().size());
    HashSet<String> toDelete = new HashSet<String>();
    for (KeyItem item : listResult) {
        if (item != null) {
            KeyIdentifier id = new KeyIdentifier(item.kid());
            toDelete.add(id.name());
            keys.remove(item.kid());
        }
    }
    Assert.assertEquals(0, keys.size());
    for (String name : toDelete) {
        try {
            keyVaultClient.deleteKey(getVaultUri(), name);
        } catch (KeyVaultErrorException e) {
            // Ignore forbidden exception for certificate keys that cannot be deleted
            if (!e.body().error().code().equals("Forbidden"))
                throw e;
        }
    }
}
Also used : KeyIdentifier(com.microsoft.azure.keyvault.KeyIdentifier) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) KeyItem(com.microsoft.azure.keyvault.models.KeyItem) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with KeyVaultErrorException

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

the class SecretOperationsTest method disabledSecretGet.

@Test
public // verifies the inner error on disabled secret
void disabledSecretGet() throws Exception {
    String secretName = "disabledsecret";
    SecretBundle secret = keyVaultClient.setSecret(new SetSecretRequest.Builder(getVaultUri(), secretName, SECRET_VALUE).withAttributes(new SecretAttributes().withEnabled(false)).build());
    try {
        keyVaultClient.getSecret(secret.id());
        Assert.fail("Should throw exception for disabled secret.");
    } catch (KeyVaultErrorException e) {
        Assert.assertEquals(e.body().error().code(), "Forbidden");
        Assert.assertNotNull(e.body().error().message());
        Assert.assertNotNull(e.body().error().innerError());
        Assert.assertEquals(e.body().error().innerError().code(), "SecretDisabled");
    } catch (Exception e) {
        Assert.fail("Should throw KeyVaultErrorException for disabled secret.");
    }
    keyVaultClient.deleteSecret(getVaultUri(), secretName);
}
Also used : SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) SecretAttributes(com.microsoft.azure.keyvault.models.SecretAttributes) SetSecretRequest(com.microsoft.azure.keyvault.requests.SetSecretRequest) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) Test(org.junit.Test)

Example 19 with KeyVaultErrorException

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

the class SecretOperationsTest method crudOperations.

@Test
public void crudOperations() throws Exception {
    SecretBundle secret;
    {
        // Create secret
        secret = keyVaultClient.setSecret(new SetSecretRequest.Builder(getVaultUri(), SECRET_NAME, SECRET_VALUE).build());
        validateSecret(secret, getVaultUri(), SECRET_NAME, SECRET_VALUE, null, null);
    }
    // Secret identifier.
    SecretIdentifier secretId = new SecretIdentifier(secret.id());
    {
        // Get secret using kid WO version
        SecretBundle readBundle = keyVaultClient.getSecret(secretId.baseIdentifier());
        compareSecrets(secret, readBundle);
    }
    {
        // Get secret using full kid as defined in the bundle
        SecretBundle readBundle = keyVaultClient.getSecret(secret.id());
        compareSecrets(secret, readBundle);
    }
    {
        // Get secret using vault and secret name.
        SecretBundle readBundle = keyVaultClient.getSecret(getVaultUri(), SECRET_NAME);
        compareSecrets(secret, readBundle);
    }
    {
        // Get secret using vault, secret name and version.
        SecretBundle readBundle = keyVaultClient.getSecret(getVaultUri(), SECRET_NAME, secretId.version());
        compareSecrets(secret, readBundle);
    }
    {
        secret.attributes().withExpires(new DateTime().withMonthOfYear(2).withDayOfMonth(1).withYear(2050));
        Map<String, String> tags = new HashMap<String, String>();
        tags.put("foo", "baz");
        secret.withTags(tags).withContentType("application/html").withValue(// The value doesn't get updated
        null);
        // Update secret using the kid as defined in the bundle
        SecretBundle updatedSecret = keyVaultClient.updateSecret(new UpdateSecretRequest.Builder(secret.id()).withContentType(secret.contentType()).withAttributes(secret.attributes()).withTags(secret.tags()).build());
        compareSecrets(secret, updatedSecret);
        // Subsequent operations must use the updated bundle for comparison.
        secret = updatedSecret;
    }
    {
        // Update secret using vault and secret name.
        secret.attributes().withNotBefore(new DateTime().withMonthOfYear(2).withDayOfMonth(1).withYear(2000));
        Map<String, String> tags = new HashMap<String, String>();
        tags.put("rex", "woof");
        secret.withTags(tags).withContentType("application/html");
        // Perform the operation.
        SecretBundle updatedSecret = keyVaultClient.updateSecret(new UpdateSecretRequest.Builder(getVaultUri(), SECRET_NAME).withVersion(secret.secretIdentifier().version()).withContentType(secret.contentType()).withAttributes(secret.attributes()).withTags(secret.tags()).build());
        compareSecrets(secret, updatedSecret);
        validateSecret(updatedSecret, secret.secretIdentifier().vault(), secret.secretIdentifier().name(), null, secret.contentType(), secret.attributes());
    }
    {
        // Delete secret
        SecretBundle deleteBundle = keyVaultClient.deleteSecret(getVaultUri(), SECRET_NAME);
        compareSecrets(secret, deleteBundle);
    }
    {
        // Expects a secret not found
        try {
            keyVaultClient.getSecret(secretId.baseIdentifier());
        } catch (KeyVaultErrorException e) {
            Assert.assertNotNull(e.body().error().code());
            Assert.assertEquals("SecretNotFound", e.body().error().code());
        }
    }
}
Also used : SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) SecretIdentifier(com.microsoft.azure.keyvault.SecretIdentifier) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) HashMap(java.util.HashMap) Map(java.util.Map) DateTime(org.joda.time.DateTime) UpdateSecretRequest(com.microsoft.azure.keyvault.requests.UpdateSecretRequest) Test(org.junit.Test)

Example 20 with KeyVaultErrorException

use of com.microsoft.azure.keyvault.models.KeyVaultErrorException 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)20 Test (org.junit.Test)20 CertificateBundle (com.microsoft.azure.keyvault.models.CertificateBundle)9 CertificatePolicy (com.microsoft.azure.keyvault.models.CertificatePolicy)9 SecretProperties (com.microsoft.azure.keyvault.models.SecretProperties)9 SecretBundle (com.microsoft.azure.keyvault.models.SecretBundle)8 CertificateOperation (com.microsoft.azure.keyvault.models.CertificateOperation)6 IssuerParameters (com.microsoft.azure.keyvault.models.IssuerParameters)6 X509CertificateProperties (com.microsoft.azure.keyvault.models.X509CertificateProperties)6 CreateCertificateRequest (com.microsoft.azure.keyvault.requests.CreateCertificateRequest)6 HashSet (java.util.HashSet)6 SecretIdentifier (com.microsoft.azure.keyvault.SecretIdentifier)5 KeyBundle (com.microsoft.azure.keyvault.models.KeyBundle)5 ArrayList (java.util.ArrayList)4 KeyIdentifier (com.microsoft.azure.keyvault.KeyIdentifier)3 AdministratorDetails (com.microsoft.azure.keyvault.models.AdministratorDetails)3 CertificateItem (com.microsoft.azure.keyvault.models.CertificateItem)3 IssuerBundle (com.microsoft.azure.keyvault.models.IssuerBundle)3 IssuerCredentials (com.microsoft.azure.keyvault.models.IssuerCredentials)3 KeyItem (com.microsoft.azure.keyvault.models.KeyItem)3