Search in sources :

Example 11 with KeyBundle

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

the class KeyVaultKeyResolverBCProviderTest method KeyVault_KeyVaultKeyResolver_Key.

@Test
public void KeyVault_KeyVaultKeyResolver_Key() throws InterruptedException, ExecutionException {
    try {
        // Create a key on a vault.
        CreateKeyRequest request = new CreateKeyRequest.Builder(getVaultUri(), KEY_NAME, JsonWebKeyType.RSA).build();
        KeyBundle bundle = keyVaultClient.createKey(request);
        if (bundle != null) {
            try {
                // ctor with client
                KeyVaultKeyResolver resolver = new KeyVaultKeyResolver(keyVaultClient, _provider);
                Future<IKey> baseKeyFuture = resolver.resolveKeyAsync(bundle.keyIdentifier().baseIdentifier());
                Future<IKey> versionKeyFuture = resolver.resolveKeyAsync(bundle.keyIdentifier().identifier());
                IKey baseKey = baseKeyFuture.get();
                IKey versionKey = versionKeyFuture.get();
                Assert.assertEquals(baseKey.getKid(), versionKey.getKid());
            } finally {
                // Delete the key
                keyVaultClient.deleteKey(getVaultUri(), KEY_NAME);
            }
        }
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : IKey(com.microsoft.azure.keyvault.core.IKey) CreateKeyRequest(com.microsoft.azure.keyvault.requests.CreateKeyRequest) KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) ExecutionException(java.util.concurrent.ExecutionException) KeyVaultKeyResolver(com.microsoft.azure.keyvault.extensions.KeyVaultKeyResolver) Test(org.junit.Test)

Example 12 with KeyBundle

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

the class KeyVaultKeyResolverDefaultProviderTest method KeyVault_KeyVaultKeyResolver_Key.

@Test
public void KeyVault_KeyVaultKeyResolver_Key() throws InterruptedException, ExecutionException {
    try {
        // Create a key on a vault.
        CreateKeyRequest request = new CreateKeyRequest.Builder(getVaultUri(), KEY_NAME, JsonWebKeyType.RSA).build();
        KeyBundle keyBundle = keyVaultClient.createKey(request);
        try {
            // ctor with client
            final KeyVaultKeyResolver resolver = new KeyVaultKeyResolver(keyVaultClient);
            IKey baseKey = resolver.resolveKeyAsync(keyBundle.keyIdentifier().baseIdentifier()).get();
            IKey versionKey = resolver.resolveKeyAsync(keyBundle.keyIdentifier().identifier()).get();
            Assert.assertEquals(baseKey.getKid(), versionKey.getKid());
        } finally {
            // Delete the key
            keyVaultClient.deleteKey(getVaultUri(), KEY_NAME);
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}
Also used : IKey(com.microsoft.azure.keyvault.core.IKey) CreateKeyRequest(com.microsoft.azure.keyvault.requests.CreateKeyRequest) KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) ExecutionException(java.util.concurrent.ExecutionException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) KeyVaultKeyResolver(com.microsoft.azure.keyvault.extensions.KeyVaultKeyResolver) Test(org.junit.Test)

Example 13 with KeyBundle

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

the class KeyOperationsTest method checkImportOperation.

private void checkImportOperation(KeyBundle keyBundle, boolean importToHardware) throws Exception {
    Attributes attribute = new KeyAttributes().withEnabled(true).withExpires(new DateTime().withYear(2050).withMonthOfYear(1)).withNotBefore(new DateTime().withYear(2000).withMonthOfYear(1));
    Map<String, String> tags = new HashMap<String, String>();
    tags.put("foo", "baz");
    JsonWebKey importedJwk = keyBundle.key();
    KeyBundle importResultBundle = keyVaultClient.importKey(new ImportKeyRequest.Builder(getVaultUri(), KEY_NAME, keyBundle.key()).withHsm(importToHardware).withAttributes(attribute).withTags(tags).build());
    validateRsaKeyBundle(importResultBundle, getVaultUri(), KEY_NAME, importToHardware ? JsonWebKeyType.RSA_HSM : JsonWebKeyType.RSA, importedJwk.keyOps(), attribute);
    checkEncryptDecryptSequence(importedJwk, importResultBundle);
    Assert.assertTrue(importResultBundle.key().isValid());
}
Also used : HashMap(java.util.HashMap) KeyAttributes(com.microsoft.azure.keyvault.models.KeyAttributes) ImportKeyRequest(com.microsoft.azure.keyvault.requests.ImportKeyRequest) Attributes(com.microsoft.azure.keyvault.models.Attributes) KeyAttributes(com.microsoft.azure.keyvault.models.KeyAttributes) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) DateTime(org.joda.time.DateTime)

Example 14 with KeyBundle

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

the class KeyOperationsTest method backupRestore.

@Test
public void backupRestore() throws Exception {
    KeyBundle createdBundle;
    // Creates a key
    {
        createdBundle = keyVaultClient.createKey(new CreateKeyRequest.Builder(getVaultUri(), KEY_NAME, JsonWebKeyType.RSA).build());
        validateRsaKeyBundle(createdBundle, getVaultUri(), KEY_NAME, JsonWebKeyType.RSA, null, null);
    }
    // Creates a backup of key.
    byte[] keyBackup;
    {
        keyBackup = keyVaultClient.backupKey(getVaultUri(), KEY_NAME).value();
    }
    // Deletes the key.
    {
        keyVaultClient.deleteKey(getVaultUri(), KEY_NAME);
    }
    // Restores the key.
    {
        KeyBundle restoredBundle = keyVaultClient.restoreKey(getVaultUri(), keyBackup);
        compareKeyBundles(createdBundle, restoredBundle);
    }
}
Also used : KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) Test(org.junit.Test)

Example 15 with KeyBundle

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

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