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