Search in sources :

Example 16 with KeyBundle

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

the class KeyOperationsTest method importKeyOperation.

@Test
public void importKeyOperation() throws Exception {
    KeyBundle keyBundle = new KeyBundle();
    JsonWebKey key = JsonWebKey.fromRSA(getTestKeyMaterial());
    key.withKeyOps(Arrays.asList(JsonWebKeyOperation.ENCRYPT, JsonWebKeyOperation.DECRYPT));
    keyBundle.withKey(key);
    checkImportOperation(keyBundle, false);
    checkImportOperation(keyBundle, true);
}
Also used : KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) Test(org.junit.Test)

Example 17 with KeyBundle

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

the class KeyOperationsTest method importTestKey.

private static JsonWebKey importTestKey() throws Exception {
    KeyBundle keyBundle = new KeyBundle();
    JsonWebKey key = JsonWebKey.fromRSA(getTestKeyMaterial());
    key.withKty(JsonWebKeyType.RSA);
    key.withKeyOps(Arrays.asList(JsonWebKeyOperation.ENCRYPT, JsonWebKeyOperation.DECRYPT, JsonWebKeyOperation.SIGN, JsonWebKeyOperation.VERIFY, JsonWebKeyOperation.WRAP_KEY, JsonWebKeyOperation.UNWRAP_KEY));
    keyBundle = keyVaultClient.importKey(new ImportKeyRequest.Builder(getVaultUri(), KEY_NAME, key).withHsm(false).build());
    validateRsaKeyBundle(keyBundle, getVaultUri(), KEY_NAME, JsonWebKeyType.RSA, null, null);
    return keyBundle.key();
}
Also used : KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey)

Example 18 with KeyBundle

use of com.microsoft.azure.keyvault.models.KeyBundle 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)18 Test (org.junit.Test)9 ServiceResponse (com.microsoft.rest.ServiceResponse)7 ResponseBody (okhttp3.ResponseBody)7 Response (retrofit2.Response)7 Observable (rx.Observable)7 KeyVaultErrorException (com.microsoft.azure.keyvault.models.KeyVaultErrorException)5 KeyAttributes (com.microsoft.azure.keyvault.models.KeyAttributes)4 KeyIdentifier (com.microsoft.azure.keyvault.KeyIdentifier)3 KeyItem (com.microsoft.azure.keyvault.models.KeyItem)3 CreateKeyRequest (com.microsoft.azure.keyvault.requests.CreateKeyRequest)3 JsonWebKey (com.microsoft.azure.keyvault.webkey.JsonWebKey)3 ExecutionException (java.util.concurrent.ExecutionException)3 DateTime (org.joda.time.DateTime)3 IKey (com.microsoft.azure.keyvault.core.IKey)2 KeyVaultKeyResolver (com.microsoft.azure.keyvault.extensions.KeyVaultKeyResolver)2 Attributes (com.microsoft.azure.keyvault.models.Attributes)2 KeyCreateParameters (com.microsoft.azure.keyvault.models.KeyCreateParameters)2 KeyImportParameters (com.microsoft.azure.keyvault.models.KeyImportParameters)2 KeyUpdateParameters (com.microsoft.azure.keyvault.models.KeyUpdateParameters)2