Search in sources :

Example 1 with KeyItem

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

the class KeyOperationsTest method listKeyVersions.

@Test
public void listKeyVersions() 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, JsonWebKeyType.RSA).build());
                keys.add(createdBundle.key().kid());
                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.listKeyVersions(getVaultUri(), KEY_NAME, MAX_KEYS);
    //TODO bug: Assert.assertTrue(PAGELIST_MAX_KEYS >= listResult.currentPage().getItems().size());
    listResult = keyVaultClient.listKeyVersions(getVaultUri(), KEY_NAME);
    for (KeyItem item : listResult) {
        if (item != null) {
            keys.remove(item.kid());
        }
    }
    Assert.assertEquals(0, keys.size());
    keyVaultClient.deleteKey(getVaultUri(), KEY_NAME);
}
Also used : 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 2 with KeyItem

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

the class AsyncOperationsTest method keyAsync.

@Test
public void keyAsync() throws Exception {
    String vault = getVaultUri();
    String keyname = "mykey";
    CreateKeyRequest createKeyRequest = new CreateKeyRequest.Builder(vault, keyname, JsonWebKeyType.RSA).build();
    KeyBundle keyBundle = keyVaultClient.createKeyAsync(createKeyRequest, null).get();
    Assert.assertNotNull(keyBundle);
    UpdateKeyRequest updateKeyRequest = new UpdateKeyRequest.Builder(keyBundle.key().kid()).build();
    keyBundle = keyVaultClient.updateKeyAsync(updateKeyRequest, null).get();
    Assert.assertNotNull(keyBundle);
    keyBundle = keyVaultClient.getKeyAsync(keyBundle.key().kid(), null).get();
    Assert.assertNotNull(keyBundle);
    List<KeyItem> keyItems = keyVaultClient.listKeysAsync(vault, 2, null).get();
    Assert.assertNotNull(keyItems);
    List<KeyItem> keyVersionItems = keyVaultClient.listKeyVersionsAsync(getVaultUri(), keyname, 2, null).get();
    Assert.assertNotNull(keyVersionItems);
    BackupKeyResult backupResult = keyVaultClient.backupKeyAsync(vault, keyname, null).get();
    Assert.assertNotNull(backupResult);
    keyVaultClient.deleteKeyAsync(keyBundle.keyIdentifier().vault(), keyBundle.keyIdentifier().name(), null).get();
    KeyBundle restoreResult = keyVaultClient.restoreKeyAsync(vault, backupResult.value(), null).get();
    Assert.assertNotNull(restoreResult);
    KeyOperationResult encryptResult = keyVaultClient.encryptAsync(keyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, new byte[100], null).get();
    Assert.assertNotNull(encryptResult);
    KeyOperationResult decryptResult = keyVaultClient.decryptAsync(keyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, encryptResult.result(), null).get();
    Assert.assertNotNull(decryptResult);
    KeyOperationResult wrapResult = keyVaultClient.wrapKeyAsync(keyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, new byte[100], null).get();
    Assert.assertNotNull(wrapResult);
    KeyOperationResult unwrapResult = keyVaultClient.unwrapKeyAsync(keyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, wrapResult.result(), null).get();
    Assert.assertNotNull(unwrapResult);
    byte[] plainText = new byte[100];
    new Random(0x1234567L).nextBytes(plainText);
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(plainText);
    byte[] digest = md.digest();
    KeyOperationResult signResult = keyVaultClient.signAsync(keyBundle.key().kid(), JsonWebKeySignatureAlgorithm.RS256, digest, null).get();
    Assert.assertNotNull(signResult);
    KeyVerifyResult verifypResult = keyVaultClient.verifyAsync(keyBundle.key().kid(), JsonWebKeySignatureAlgorithm.RS256, digest, signResult.result(), null).get();
    Assert.assertTrue(verifypResult.value());
    keyBundle = keyVaultClient.deleteKeyAsync(keyBundle.keyIdentifier().vault(), keyBundle.keyIdentifier().name(), null).get();
    Assert.assertNotNull(keyBundle);
    try {
        keyVaultClient.deleteKeyAsync(keyBundle.keyIdentifier().vault(), keyBundle.keyIdentifier().name(), null).get();
    } catch (ExecutionException ex) {
        Throwable t = ex.getCause();
        if (t instanceof KeyVaultErrorException) {
            Assert.assertEquals("KeyNotFound", ((KeyVaultErrorException) t).body().error().code());
        } else
            throw ex;
    }
}
Also used : KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) KeyItem(com.microsoft.azure.keyvault.models.KeyItem) BackupKeyResult(com.microsoft.azure.keyvault.models.BackupKeyResult) UpdateKeyRequest(com.microsoft.azure.keyvault.requests.UpdateKeyRequest) Random(java.util.Random) KeyVerifyResult(com.microsoft.azure.keyvault.models.KeyVerifyResult) CreateKeyRequest(com.microsoft.azure.keyvault.requests.CreateKeyRequest) KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) MessageDigest(java.security.MessageDigest) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with KeyItem

use of com.microsoft.azure.keyvault.models.KeyItem 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)

Aggregations

KeyBundle (com.microsoft.azure.keyvault.models.KeyBundle)3 KeyItem (com.microsoft.azure.keyvault.models.KeyItem)3 KeyVaultErrorException (com.microsoft.azure.keyvault.models.KeyVaultErrorException)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 KeyIdentifier (com.microsoft.azure.keyvault.KeyIdentifier)1 BackupKeyResult (com.microsoft.azure.keyvault.models.BackupKeyResult)1 KeyOperationResult (com.microsoft.azure.keyvault.models.KeyOperationResult)1 KeyVerifyResult (com.microsoft.azure.keyvault.models.KeyVerifyResult)1 CreateKeyRequest (com.microsoft.azure.keyvault.requests.CreateKeyRequest)1 UpdateKeyRequest (com.microsoft.azure.keyvault.requests.UpdateKeyRequest)1 MessageDigest (java.security.MessageDigest)1 Random (java.util.Random)1 ExecutionException (java.util.concurrent.ExecutionException)1