com.microsoft.azure.keyvault.requests.CreateKeyRequest Java Examples

The following examples show how to use com.microsoft.azure.keyvault.requests.CreateKeyRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KeyVaultKeyResolverDefaultProviderTest.java    From azure-keyvault-java with MIT License 5 votes vote down vote up
@Test
public void KeyVault_KeyVaultKeyResolver_Key_KeyVaultKeyResolverDefaultProviderTest() throws InterruptedException, ExecutionException
{
	String TEST_KEY_NAME = KEY_NAME + "1";
    try {
        // Create a key on a vault.
        CreateKeyRequest  request   = new CreateKeyRequest.Builder(getVaultUri(), TEST_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(), TEST_KEY_NAME );
            pollOnKeyDeletion( getVaultUri(), TEST_KEY_NAME );
            keyVaultClient.purgeDeletedKey( getVaultUri(), TEST_KEY_NAME );
        }
    } catch(Exception ex) {
        fail(ex.getMessage());
    }
}
 
Example #2
Source File: KeyVaultKeyResolverBCProviderTest.java    From azure-keyvault-java with MIT License 5 votes vote down vote up
@Test
public void KeyVault_KeyVaultKeyResolver_Key_KeyVaultKeyResolverBCProviderTest() throws InterruptedException, ExecutionException
{
	String TEST_KEY_NAME = KEY_NAME + "1";
    try {
        // Create a key on a vault.
        CreateKeyRequest           request  = new CreateKeyRequest.Builder(getVaultUri(), TEST_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(), TEST_KEY_NAME );
                pollOnKeyDeletion( getVaultUri(), TEST_KEY_NAME );
                keyVaultClient.purgeDeletedKey( getVaultUri(), TEST_KEY_NAME);

            }
        }
    }
    catch ( Exception ex )
    {
        Assert.fail(ex.getMessage());
    }
}
 
Example #3
Source File: KeyOperationsTest.java    From azure-keyvault-java with MIT License 5 votes vote down vote up
@Test
public void backupRestoreForKeyOperationsTest() 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();
        if (isRecordMode()) {
        	Thread.sleep(20000);
        }
    }

    // Deletes the key.
    {
        keyVaultClient.deleteKey(getVaultUri(), KEY_NAME);
        pollOnKeyDeletion(getVaultUri(), KEY_NAME);
    }
    
    keyVaultClient.purgeDeletedKey(getVaultUri(), KEY_NAME);
    if (isRecordMode()) {
    	Thread.sleep(40000);
    }

    // Restores the key.
    {
        KeyBundle restoredBundle = keyVaultClient.restoreKey(getVaultUri(), keyBackup);
        compareKeyBundles(createdBundle, restoredBundle);
    }
    
}
 
Example #4
Source File: KeyImpl.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public KeyImpl withKeyTypeToCreate(JsonWebKeyType keyType) {
    createKeyRequest = new CreateKeyRequest.Builder(vault.vaultUri(), name(), keyType);
    return this;
}
 
Example #5
Source File: KeyOperationsTest.java    From azure-keyvault-java with MIT License 4 votes vote down vote up
@Test
public void listKeyVersionsForKeyOperationsTest() 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");
                    if (isRecordMode()) {
                    	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);
    pollOnKeyDeletion(getVaultUri(), KEY_NAME);
    
    keyVaultClient.purgeDeletedKey(getVaultUri(), KEY_NAME);
    if (isRecordMode()) {
    	Thread.sleep(40000);        
    }
}
 
Example #6
Source File: AsyncOperationsTest.java    From azure-keyvault-java with MIT License 4 votes vote down vote up
@Test
public void keyAsyncForAsyncOperationsTest() 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(vault, 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();
	pollOnKeyDeletion(keyBundle.keyIdentifier().vault(), keyBundle.keyIdentifier().name());
	keyVaultClient.purgeDeletedKey(keyBundle.keyIdentifier().vault(), keyBundle.keyIdentifier().name());
	if (isRecordMode()) {
		Thread.sleep(20000);
	}

	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);
	pollOnKeyDeletion(keyBundle.keyIdentifier().vault(), keyBundle.keyIdentifier().name());
	keyVaultClient.purgeDeletedKey(keyBundle.keyIdentifier().vault(), keyBundle.keyIdentifier().name());
	if (isRecordMode()) {
		Thread.sleep(20000);
	}
	// Get the unavailable key to throw exception -> it gets stuck

	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;
	}

}
 
Example #7
Source File: KeyVaultClientCustomImpl.java    From azure-keyvault-java with MIT License 3 votes vote down vote up
/**
 * Creates a new key, stores it, then returns key parameters and attributes to
 * the client. The create key operation can be used to create any key type in
 * Azure Key Vault. If the named key already exists, Azure Key Vault creates a
 * new version of the key. Authorization: Requires the keys/create permission.
 *
 * @param createKeyRequest
 *            the grouped properties for creating a key request
 * @return the KeyBundle if successful.
 */
public KeyBundle createKey(CreateKeyRequest createKeyRequest) {

    return createKey(createKeyRequest.vaultBaseUrl(), createKeyRequest.keyName(), createKeyRequest.keyType(),
            createKeyRequest.keySize(), createKeyRequest.keyOperations(), createKeyRequest.keyAttributes(),
            createKeyRequest.tags(), createKeyRequest.curve());
}
 
Example #8
Source File: KeyVaultClientCustomImpl.java    From azure-keyvault-java with MIT License 3 votes vote down vote up
/**
 * Creates a new key, stores it, then returns key parameters and attributes to
 * the client. The create key operation can be used to create any key type in
 * Azure Key Vault. If the named key already exists, Azure Key Vault creates a
 * new version of the key. Authorization: Requires the keys/create permission.
 *
 * @param createKeyRequest
 *            the grouped properties for creating a key request
 * @param serviceCallback
 *            the async ServiceCallback to handle successful and failed
 *            responses.
 * @return the {@link ServiceFuture} object
 */
public ServiceFuture<KeyBundle> createKeyAsync(CreateKeyRequest createKeyRequest,
        ServiceCallback<KeyBundle> serviceCallback) {
    createKeyRequest.vaultBaseUrl();
    return createKeyAsync(createKeyRequest.vaultBaseUrl(), createKeyRequest.keyName(), createKeyRequest.keyType(),
            createKeyRequest.keySize(), createKeyRequest.keyOperations(), createKeyRequest.keyAttributes(),
            createKeyRequest.tags(), createKeyRequest.curve(), serviceCallback);
}
 
Example #9
Source File: KeyVaultClientCustom.java    From azure-keyvault-java with MIT License 2 votes vote down vote up
/**
 * Creates a new key, stores it, then returns key parameters and attributes to
 * the client. The create key operation can be used to create any key type in
 * Azure Key Vault. If the named key already exists, Azure Key Vault creates a
 * new version of the key. Authorization: Requires the keys/create permission.
 *
 * @param createKeyRequest
 *            the grouped properties for creating a key request
 * @return the KeyBundle if successful.
 */
KeyBundle createKey(CreateKeyRequest createKeyRequest);
 
Example #10
Source File: KeyVaultClientCustom.java    From azure-keyvault-java with MIT License 2 votes vote down vote up
/**
 * Creates a new key, stores it, then returns key parameters and attributes to
 * the client. The create key operation can be used to create any key type in
 * Azure Key Vault. If the named key already exists, Azure Key Vault creates a
 * new version of the key. Authorization: Requires the keys/create permission.
 *
 * @param createKeyRequest
 *            the grouped properties for creating a key request
 *
 * @param serviceCallback
 *            the async ServiceCallback to handle successful and failed
 *            responses.
 * @return the {@link ServiceFuture} object
 */
ServiceFuture<KeyBundle> createKeyAsync(CreateKeyRequest createKeyRequest,
        ServiceCallback<KeyBundle> serviceCallback);