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());
}
}
}
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;
}
}
}
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);
}
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());
}
}
}
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;
}
}
}
Aggregations