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