use of com.microsoft.azure.keyvault.requests.CreateKeyRequest 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;
}
}
use of com.microsoft.azure.keyvault.requests.CreateKeyRequest 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.requests.CreateKeyRequest 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());
}
}
Aggregations