Class KeyAsyncClient
KeyAsyncClient
provides asynchronous methods to manage keys
in the Azure Key Vault.
The client supports creating, retrieving, updating, deleting, purging, backing up, restoring, listing, releasing
and rotating the keys
. The client also supports listing deleted keys
for a
soft-delete enabled key vault.
Getting Started
In order to interact with the Azure Key Vault service, you will need to create an instance of the
KeyAsyncClient
class, a vault url and a credential object.
The examples shown in this document use a credential object named DefaultAzureCredential for authentication, which is appropriate for most scenarios, including local development and production environments. Additionally, we recommend using a managed identity for authentication in production environments. You can find more information on different ways of authenticating and their corresponding credential types in the Azure Identity documentation".
Sample: Construct Asynchronous Key Client
The following code sample demonstrates the creation of a KeyAsyncClient
, using the
KeyClientBuilder
to configure it.
KeyAsyncClient keyAsyncClient = new KeyClientBuilder() .vaultUrl("<your-key-vault-url>") .credential(new DefaultAzureCredentialBuilder().build()) .buildAsyncClient();
Create a Cryptographic Key
TheKeyAsyncClient
can be used to create a key in the key vault.
Code Sample:
The following code sample demonstrates how to asynchronously create a cryptographic key in the key vault,
using the createKey(String, KeyType)
API.
keyAsyncClient.createKey("keyName", KeyType.EC) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(key -> System.out.printf("Created key with name: %s and id: %s %n", key.getName(), key.getId()));
Note: For the synchronous sample, refer to KeyClient
.
Get a Cryptographic Key
TheKeyAsyncClient
can be used to retrieve a key from the key vault.
Code Sample:
The following code sample demonstrates how to asynchronously retrieve a key from the key vault, using
the getKey(String)
API.
keyAsyncClient.getKey("keyName") .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(key -> System.out.printf("Created key with name: %s and: id %s%n", key.getName(), key.getId()));
Note: For the synchronous sample, refer to KeyClient
.
Delete a Cryptographic Key
TheKeyAsyncClient
can be used to delete a key from the key vault.
Code Sample:
The following code sample demonstrates how to asynchronously delete a key from the
key vault, using the beginDeleteKey(String)
API.
keyAsyncClient.beginDeleteKey("keyName") .subscribe(pollResponse -> { System.out.printf("Deletion status: %s%n", pollResponse.getStatus()); System.out.printf("Key name: %s%n", pollResponse.getValue().getName()); System.out.printf("Key delete date: %s%n", pollResponse.getValue().getDeletedOn()); });
Note: For the synchronous sample, refer to KeyClient
.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionMono
<byte[]> Requests a backup of the specifiedkey
be downloaded to the client.Mono
<com.azure.core.http.rest.Response<byte[]>> backupKeyWithResponse
(String name) Requests a backup of the specifiedkey
be downloaded to the client.com.azure.core.util.polling.PollerFlux
<DeletedKey, Void> beginDeleteKey
(String name) Deletes akey
of any type from the key vault.com.azure.core.util.polling.PollerFlux
<KeyVaultKey, Void> beginRecoverDeletedKey
(String name) Recovers thedeleted key
in the key vault to its latest version and can only be performed on a soft-delete enabled vault.createEcKey
(CreateEcKeyOptions createEcKeyOptions) Creates a newEC key
and stores it in the key vault.Mono
<com.azure.core.http.rest.Response<KeyVaultKey>> createEcKeyWithResponse
(CreateEcKeyOptions createEcKeyOptions) Creates a newEC key
and stores it in the key vault.createKey
(CreateKeyOptions createKeyOptions) Creates a newkey
and stores it in the key vault.Creates a newkey
and stores it in the key vault.Mono
<com.azure.core.http.rest.Response<KeyVaultKey>> createKeyWithResponse
(CreateKeyOptions createKeyOptions) Creates a newkey
and stores it in the key vault.createOctKey
(CreateOctKeyOptions createOctKeyOptions) Creates and stores a newsymmetric key
in the key vault.Mono
<com.azure.core.http.rest.Response<KeyVaultKey>> createOctKeyWithResponse
(CreateOctKeyOptions createOctKeyOptions) Creates and stores a newsymmetric key
in the key vault.createRsaKey
(CreateRsaKeyOptions createRsaKeyOptions) /** Creates a newRSA key
and stores it in the key vault.Mono
<com.azure.core.http.rest.Response<KeyVaultKey>> createRsaKeyWithResponse
(CreateRsaKeyOptions createRsaKeyOptions) Creates a newRSA key
and stores it in the key vault.getCryptographyAsyncClient
(String keyName) Creates aCryptographyAsyncClient
for the latest version of a given key.getCryptographyAsyncClient
(String keyName, String keyVersion) Creates aCryptographyAsyncClient
for a given key version.getDeletedKey
(String name) Gets the public part of adeleted key
.Mono
<com.azure.core.http.rest.Response<DeletedKey>> Gets the public part of adeleted key
.Gets the public part of the specifiedkey
and key version.Gets the public part of the specifiedkey
and key version.getKeyRotationPolicy
(String keyName) Gets theKeyRotationPolicy
for thekey
with the provided name.Mono
<com.azure.core.http.rest.Response<KeyRotationPolicy>> getKeyRotationPolicyWithResponse
(String keyName) Gets theKeyRotationPolicy
for thekey
with the provided name.Mono
<com.azure.core.http.rest.Response<KeyVaultKey>> getKeyWithResponse
(String name, String version) Gets the public part of the specifiedkey
and key version.Mono
<byte[]> getRandomBytes
(int count) Get the requested number of bytes containing random values from a managed HSM.Mono
<com.azure.core.http.rest.Response<byte[]>> getRandomBytesWithResponse
(int count) Get the requested number of bytes containing random values from a managed HSM.Get the vault endpoint url to which service requests are sent to.importKey
(ImportKeyOptions importKeyOptions) Imports an externally createdkey
and stores it in the key vault.importKey
(String name, JsonWebKey keyMaterial) Imports an externally createdkey
and stores it in the key vault.Mono
<com.azure.core.http.rest.Response<KeyVaultKey>> importKeyWithResponse
(ImportKeyOptions importKeyOptions) Imports an externally createdkey
and stores it in the key vault.com.azure.core.http.rest.PagedFlux
<DeletedKey> Listsdeleted keys
of the key vault.com.azure.core.http.rest.PagedFlux
<KeyProperties> Listkeys
in the key vault.com.azure.core.http.rest.PagedFlux
<KeyProperties> List all versions of the specifiedkeys
.purgeDeletedKey
(String name) Permanently deletes the specifiedkey
without the possibility of recovery.Permanently deletes the specifiedkey
without the possibility of recovery.releaseKey
(String name, String targetAttestationToken) Releases the latest version of akey
.releaseKey
(String name, String version, String targetAttestationToken) Releases a key.Mono
<com.azure.core.http.rest.Response<ReleaseKeyResult>> releaseKeyWithResponse
(String name, String version, String targetAttestationToken, ReleaseKeyOptions releaseKeyOptions) Releases a key.restoreKeyBackup
(byte[] backup) Restores a backed upkey
to a vault.Mono
<com.azure.core.http.rest.Response<KeyVaultKey>> restoreKeyBackupWithResponse
(byte[] backup) Restores a backed upkey
to a vault.Rotates akey
.Mono
<com.azure.core.http.rest.Response<KeyVaultKey>> rotateKeyWithResponse
(String name) Rotates akey
.updateKeyProperties
(KeyProperties keyProperties, KeyOperation... keyOperations) Updates theattributes
andkey operations
associated with the specifiedkey
, but not the cryptographic key material of the specifiedkey
in the key vault.Mono
<com.azure.core.http.rest.Response<KeyVaultKey>> updateKeyPropertiesWithResponse
(KeyProperties keyProperties, KeyOperation... keyOperations) Updates theattributes
andkey operations
associated with the specifiedkey
, but not the cryptographic key material of the specifiedkey
in the key vault.updateKeyRotationPolicy
(String keyName, KeyRotationPolicy keyRotationPolicy) Updates theKeyRotationPolicy
of the key with the provided name.Mono
<com.azure.core.http.rest.Response<KeyRotationPolicy>> updateKeyRotationPolicyWithResponse
(String keyName, KeyRotationPolicy keyRotationPolicy) Updates theKeyRotationPolicy
of the key with the provided name.
-
Method Details
-
getVaultUrl
Get the vault endpoint url to which service requests are sent to.- Returns:
- The vault endpoint url
-
getCryptographyAsyncClient
Creates aCryptographyAsyncClient
for the latest version of a given key.To ensure correct behavior when performing operations such as
Decrypt
,Unwrap
andVerify
, it is recommended to use aCryptographyAsyncClient
created for the specific key version that was used for the corresponding inverse operation:Encrypt
,Wrap
, orSign
, respectively.You can provide a key version either via
getCryptographyAsyncClient(String, String)
or by ensuring it is included in thekeyIdentifier
passed toCryptographyClientBuilder.keyIdentifier(String)
before building a client.- Parameters:
keyName
- The name of the key.- Returns:
- An instance of
CryptographyAsyncClient
associated with the latest version of a key with the provided name. - Throws:
IllegalArgumentException
- IfkeyName
isnull
or empty.
-
getCryptographyAsyncClient
Creates aCryptographyAsyncClient
for a given key version.- Parameters:
keyName
- The name of the key.keyVersion
- The key version.- Returns:
- An instance of
CryptographyAsyncClient
associated with a key with the provided name and version. IfkeyVersion
isnull
or empty, the client will use the latest version of the key. - Throws:
IllegalArgumentException
- IfkeyName
isnull
or empty.
-
createKey
Creates a newkey
and stores it in the key vault. The create key operation can be used to create anykeyType
in Azure Key Vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of thekey
. It requires thekeys/create
permission.The
keyType
indicates the type ofkey
to create. Possible values include:EC
,EC-HSM
,RSA
,RSA-HSM
,OCT
, andOCT-HSM
.Code Samples
Creates a new
EC key
. Subscribes to the call asynchronously and prints out the newlycreated key
details when a response has been received.keyAsyncClient.createKey("keyName", KeyType.EC) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(key -> System.out.printf("Created key with name: %s and id: %s %n", key.getName(), key.getId()));
- Parameters:
name
- The name of thekey
being created.keyType
- The type ofkey
to create. For valid values, seeKeyType
.- Returns:
- A
Mono
containing thecreated key
. - Throws:
com.azure.core.exception.HttpResponseException
- Ifname
is an empty string.NullPointerException
- Ifname
orkeyType
arenull
.
-
createKeyWithResponse
public Mono<com.azure.core.http.rest.Response<KeyVaultKey>> createKeyWithResponse(CreateKeyOptions createKeyOptions) Creates a newkey
and stores it in the key vault. The create key operation can be used to create anykeyType
in Azure Key Vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of thekey
. It requires thekeys/create
permission.The
keyType
indicates the type ofkey
to create. Possible values include:EC
,EC-HSM
,RSA
,RSA-HSM
,OCT
, andOCT-HSM
.Code Samples
Creates a new
EC key
. Subscribes to the call asynchronously and prints out the newlycreated key
details when a response has been received.CreateKeyOptions createKeyOptions = new CreateKeyOptions("keyName", KeyType.RSA) .setNotBefore(OffsetDateTime.now().plusDays(1)) .setExpiresOn(OffsetDateTime.now().plusYears(1)); keyAsyncClient.createKeyWithResponse(createKeyOptions) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(createKeyResponse -> System.out.printf("Created key with name: %s and: id %s%n", createKeyResponse.getValue().getName(), createKeyResponse.getValue().getId()));
- Parameters:
createKeyOptions
- Theoptions object
containing information about thekey
being created.- Returns:
- A
Mono
containing aResponse
whosevalue
contains thecreated key
. - Throws:
com.azure.core.exception.HttpResponseException
- IfCreateKeyOptions.getName()
is an empty string.NullPointerException
- IfcreateKeyOptions
is null.com.azure.core.exception.ResourceModifiedException
- IfcreateKeyOptions
is malformed.
-
createKey
Creates a newkey
and stores it in the key vault. The create key operation can be used to create anykeyType
in Azure Key Vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of thekey
. It requires thekeys/create
permission.The
CreateKeyOptions
parameter is required. Theexpires
andnotBefore
values are optional. Theenabled
field is set totrue
by Azure Key Vault, if not specified.The
keyType
indicates the type ofkey
to create. Possible values include:EC
,EC-HSM
,RSA
,RSA-HSM
,OCT
, andOCT-HSM
.Code Samples
Creates a new
RSA key
which activates in one day and expires in one year. Subscribes to the call asynchronously and prints out the newlycreated key
details when a response has been received.CreateKeyOptions createKeyOptions = new CreateKeyOptions("keyName", KeyType.RSA) .setNotBefore(OffsetDateTime.now().plusDays(1)) .setExpiresOn(OffsetDateTime.now().plusYears(1)); keyAsyncClient.createKey(createKeyOptions) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(key -> System.out.printf("Created key with name: %s and id: %s %n", key.getName(), key.getId()));
- Parameters:
createKeyOptions
- Theoptions object
containing information about thekey
being created.- Returns:
- A
Mono
containing thecreated key
. - Throws:
com.azure.core.exception.HttpResponseException
- IfCreateKeyOptions.getName()
is an empty string.NullPointerException
- IfcreateKeyOptions
isnull
.com.azure.core.exception.ResourceModifiedException
- IfcreateKeyOptions
is malformed.
-
createRsaKey
/** Creates a newRSA key
and stores it in the key vault. The create RSA key operation can be used to create any RSA key type in Azure Key Vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of thekey
. It requires thekeys/create
permission.The
CreateRsaKeyOptions
parameter is required. ThekeySize
can be optionally specified. Theexpires
andnotBefore
values are optional. Theenabled
field is set totrue
by Azure Key Vault, if not specified.The
keyType
indicates the type ofkey
to create. Possible values include:RSA
andRSA-HSM
.Code Samples
Creates a new
RSA key
with size 2048 which activates in one day and expires in one year. Subscribes to the call asynchronously and prints out the newlycreated key
details when a response has been received.CreateRsaKeyOptions createRsaKeyOptions = new CreateRsaKeyOptions("keyName") .setKeySize(2048) .setNotBefore(OffsetDateTime.now().plusDays(1)) .setExpiresOn(OffsetDateTime.now().plusYears(1)); keyAsyncClient.createRsaKey(createRsaKeyOptions) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(rsaKey -> System.out.printf("Created key with name: %s and id: %s %n", rsaKey.getName(), rsaKey.getId()));
- Parameters:
createRsaKeyOptions
- Theoptions object
containing information about theRSA key
being created.- Returns:
- A
Mono
containing thecreated key
. - Throws:
com.azure.core.exception.HttpResponseException
- IfCreateKeyOptions.getName()
is an empty string.NullPointerException
- IfcreateRsaKeyOptions
isnull
.com.azure.core.exception.ResourceModifiedException
- IfcreateRsaKeyOptions
is malformed.
-
createRsaKeyWithResponse
public Mono<com.azure.core.http.rest.Response<KeyVaultKey>> createRsaKeyWithResponse(CreateRsaKeyOptions createRsaKeyOptions) Creates a newRSA key
and stores it in the key vault. The create RSA key operation can be used to create any RSA key type in Azure Key Vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of thekey
. It requires thekeys/create
permission.The
CreateRsaKeyOptions
parameter is required. ThekeySize
can be optionally specified. Theexpires
andnotBefore
values are optional. Theenabled
field is set totrue
by Azure Key Vault, if not specified.The
keyType
indicates the type ofkey
to create. Possible values include:RSA
andRSA-HSM
.Code Samples
Creates a new
RSA key
with size 2048 which activates in one day and expires in one year. Subscribes to the call asynchronously and prints out the newlycreated key
details when a response has been received.CreateRsaKeyOptions createRsaKeyOptions = new CreateRsaKeyOptions("keyName") .setKeySize(2048) .setNotBefore(OffsetDateTime.now().plusDays(1)) .setExpiresOn(OffsetDateTime.now().plusYears(1)); keyAsyncClient.createRsaKeyWithResponse(createRsaKeyOptions) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(createRsaKeyResponse -> System.out.printf("Created key with name: %s and: id %s%n", createRsaKeyResponse.getValue().getName(), createRsaKeyResponse.getValue().getId()));
- Parameters:
createRsaKeyOptions
- Theoptions object
containing information about theRSA key
being created.- Returns:
- A
Mono
containing aResponse
whosevalue
contains thecreated key
. - Throws:
com.azure.core.exception.HttpResponseException
- IfCreateKeyOptions.getName()
is an empty string.NullPointerException
- IfcreateRsaKeyOptions
isnull
.com.azure.core.exception.ResourceModifiedException
- IfcreateRsaKeyOptions
is malformed.
-
createEcKey
Creates a newEC key
and stores it in the key vault. The create EC key operation can be used to create any ECkey type
in Azure Key Vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of thekey
. It requires thekeys/create
permission.The
CreateEcKeyOptions
parameter is required. Thekey curve
can be optionally specified. If not specified, the default valueP-256
is used by Azure Key Vault. Theexpires
andnotBefore
values are optional. Theenabled
field is set totrue
by Azure Key Vault, if not specified.The
keyType
indicates the type ofKeyVaultKey
key to create. Possible values include:EC
andEC-HSM
.Code Samples
Creates a new
EC key
with aP-384
web key curve. The key activates in one day and expires in one year. Subscribes to the call asynchronously and prints out the newlycreated key
details when a response has been received.CreateEcKeyOptions createEcKeyOptions = new CreateEcKeyOptions("keyName") .setCurveName(KeyCurveName.P_384) .setNotBefore(OffsetDateTime.now().plusDays(1)) .setExpiresOn(OffsetDateTime.now().plusYears(1)); keyAsyncClient.createEcKey(createEcKeyOptions) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(ecKey -> System.out.printf("Created key with name: %s and id: %s %n", ecKey.getName(), ecKey.getId()));
- Parameters:
createEcKeyOptions
- Theoptions object
containing information about theEC key
being created.- Returns:
- A
Mono
containing thecreated key
. - Throws:
com.azure.core.exception.HttpResponseException
- IfCreateKeyOptions.getName()
is an empty string.NullPointerException
- IfecKeyCreateOptions
isnull
.com.azure.core.exception.ResourceModifiedException
- IfecKeyCreateOptions
is malformed.
-
createEcKeyWithResponse
public Mono<com.azure.core.http.rest.Response<KeyVaultKey>> createEcKeyWithResponse(CreateEcKeyOptions createEcKeyOptions) Creates a newEC key
and stores it in the key vault. The create EC key operation can be used to create any ECkey type
in Azure Key Vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of thekey
. It requires thekeys/create
permission.The
CreateEcKeyOptions
parameter is required. Thekey curve
can be optionally specified. If not specified, the default valueP-256
is used by Azure Key Vault. Theexpires
andnotBefore
values are optional. Theenabled
field is set totrue
by Azure Key Vault, if not specified.The
keyType
indicates the type ofKeyVaultKey
key to create. Possible values include:EC
andEC-HSM
.Code Samples
Creates a new
EC key
with aP-384
web key curve. The key activates in one day and expires in one year. Subscribes to the call asynchronously and prints out the newlycreated key
details when a response has been received.CreateEcKeyOptions createEcKeyOptions = new CreateEcKeyOptions("keyName") .setCurveName(KeyCurveName.P_384) .setNotBefore(OffsetDateTime.now().plusDays(1)) .setExpiresOn(OffsetDateTime.now().plusYears(1)); keyAsyncClient.createEcKeyWithResponse(createEcKeyOptions) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(createEcKeyResponse -> System.out.printf("Created key with name: %s and: id %s%n", createEcKeyResponse.getValue().getName(), createEcKeyResponse.getValue().getId()));
- Parameters:
createEcKeyOptions
- Theoptions object
containing information about theEC key
being created.- Returns:
- A
Mono
containing aResponse
whosevalue
contains thecreated key
. - Throws:
com.azure.core.exception.HttpResponseException
- IfCreateKeyOptions.getName()
is an empty string.NullPointerException
- IfecKeyCreateOptions
isnull
.com.azure.core.exception.ResourceModifiedException
- IfecKeyCreateOptions
is malformed.
-
createOctKey
Creates and stores a newsymmetric key
in the key vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of the key. This operation requires thekeys/create
permission.The
CreateOctKeyOptions
parameter is required. Theexpires
andnotBefore
values are optional. Theenabled
field is set totrue
by Azure Key Vault, if not specified.The
keyType
indicates the type ofKeyVaultKey
key to create. Possible values include:OCT
andOCT-HSM
.Code Samples
Creates a new
symmetric key
. Thekey
activates in one day and expires in one year. Subscribes to the call asynchronously and prints out the details of the newlycreated key
when a response has been received.CreateOctKeyOptions createOctKeyOptions = new CreateOctKeyOptions("keyName") .setNotBefore(OffsetDateTime.now().plusDays(1)) .setExpiresOn(OffsetDateTime.now().plusYears(1)); keyAsyncClient.createOctKey(createOctKeyOptions) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(octKey -> System.out.printf("Created key with name: %s and id: %s %n", octKey.getName(), octKey.getId()));
- Parameters:
createOctKeyOptions
- Theoptions object
containing information about thesymmetric key
being created.- Returns:
- A
Mono
containing thecreated key
. - Throws:
com.azure.core.exception.HttpResponseException
- IfCreateKeyOptions.getName()
is an empty string.NullPointerException
- IfecKeyCreateOptions
isnull
.com.azure.core.exception.ResourceModifiedException
- IfecKeyCreateOptions
is malformed.
-
createOctKeyWithResponse
public Mono<com.azure.core.http.rest.Response<KeyVaultKey>> createOctKeyWithResponse(CreateOctKeyOptions createOctKeyOptions) Creates and stores a newsymmetric key
in the key vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of the key. This operation requires thekeys/create
permission.The
CreateOctKeyOptions
parameter is required. Theexpires
andnotBefore
values are optional. Theenabled
field is set totrue
by Azure Key Vault, if not specified.The
keyType
indicates the type ofKeyVaultKey
key to create. Possible values include:OCT
andOCT-HSM
.Code Samples
Creates a new
symmetric key
. Thekey
activates in one day and expires in one year. Subscribes to the call asynchronously and prints out the details of the newlycreated key
when a response has been received.CreateOctKeyOptions createOctKeyOptions = new CreateOctKeyOptions("keyName") .setNotBefore(OffsetDateTime.now().plusDays(1)) .setExpiresOn(OffsetDateTime.now().plusYears(1)); keyAsyncClient.createOctKeyWithResponse(createOctKeyOptions) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(createOctKeyResponse -> System.out.printf("Created key with name: %s and: id %s%n", createOctKeyResponse.getValue().getName(), createOctKeyResponse.getValue().getId()));
- Parameters:
createOctKeyOptions
- Theoptions object
containing information about thesymmetric key
being created.- Returns:
- A
Mono
containing aResponse
whosevalue
contains thecreated key
. - Throws:
com.azure.core.exception.HttpResponseException
- IfCreateKeyOptions.getName()
is an empty string.NullPointerException
- IfcreateOctKeyOptions
isnull
.com.azure.core.exception.ResourceModifiedException
- IfcreateOctKeyOptions
is malformed.
-
importKey
Imports an externally createdkey
and stores it in the key vault. The import key operation may be used to import anykey type
into Azure Key Vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of thekey
. This operation requires thekeys/import
permission.Code Samples
Imports a new
key
into key vault. Subscribes to the call asynchronously and prints out the newlyimported key
details when a response has been received.keyAsyncClient.importKey("keyName", jsonWebKeyToImport) .subscribe(keyVaultKey -> System.out.printf("Imported key with name: %s and id: %s%n", keyVaultKey.getName(), keyVaultKey.getId()));
- Parameters:
name
- The name for the imported key.keyMaterial
- The Json web key being imported.- Returns:
- A
Mono
containing aResponse
whosevalue
contains theimported key
. - Throws:
com.azure.core.exception.HttpResponseException
- Ifname
is an empty string.
-
importKey
Imports an externally createdkey
and stores it in the key vault. The import key operation may be used to import anykey type
into Azure Key Vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of thekey
. This operation requires thekeys/import
permission.ImportKeyOptions
is required and its fieldsname
andkey material
cannot benull
. Theexpires
andnotBefore
values inkeyImportOptions
are optional. If not specified, no values are set for the fields. Theenabled
field is set totrue
and thehsm
field is set tofalse
by Azure Key Vault, if they are not specified.Code Samples
Imports a new
key
into key vault. Subscribes to the call asynchronously and prints out the newlyimported key
details when a response has been received.ImportKeyOptions options = new ImportKeyOptions("keyName", jsonWebKeyToImport) .setHardwareProtected(false); keyAsyncClient.importKey(options).subscribe(keyVaultKey -> System.out.printf("Imported key with name: %s and id: %s%n", keyVaultKey.getName(), keyVaultKey.getId()));
- Parameters:
importKeyOptions
- Theoptions object
containing information about theJsonWebKey
being imported.- Returns:
- A
Mono
containing theimported key
. - Throws:
com.azure.core.exception.HttpResponseException
- IfKeyProperties.getName()
is an empty string.NullPointerException
- IfimportKeyOptions
isnull
.
-
importKeyWithResponse
public Mono<com.azure.core.http.rest.Response<KeyVaultKey>> importKeyWithResponse(ImportKeyOptions importKeyOptions) Imports an externally createdkey
and stores it in the key vault. The import key operation may be used to import anykey type
into Azure Key Vault. If akey
with the provided name already exists, Azure Key Vault creates a new version of thekey
. This operation requires thekeys/import
permission.ImportKeyOptions
is required and its fieldsname
andkey material
cannot benull
. Theexpires
andnotBefore
values inkeyImportOptions
are optional. If not specified, no values are set for the fields. Theenabled
field is set totrue
and thehsm
field is set tofalse
by Azure Key Vault, if they are not specified.Code Samples
Imports a new
key
into key vault. Subscribes to the call asynchronously and prints out the newlyimported key
details when a response has been received.ImportKeyOptions importKeyOptions = new ImportKeyOptions("keyName", jsonWebKeyToImport) .setHardwareProtected(false); keyAsyncClient.importKeyWithResponse(importKeyOptions).subscribe(response -> System.out.printf("Imported key with name: %s and id: %s%n", response.getValue().getName(), response.getValue().getId()));
- Parameters:
importKeyOptions
- Theoptions object
containing information about theJsonWebKey
being imported.- Returns:
- A
Mono
containing aResponse
whosevalue
contains theimported key
. - Throws:
com.azure.core.exception.HttpResponseException
- IfKeyProperties.getName()
is an empty string.NullPointerException
- IfimportKeyOptions
isnull
.
-
getKey
Gets the public part of the specifiedkey
and key version. The get key operation is applicable to allkey types
and it requires thekeys/get
permission.Code Samples
Gets a specific version of the
key
in the key vault. Subscribes to the call asynchronously and prints out theretrieved key
details when a response has been received.String keyVersion = "6A385B124DEF4096AF1361A85B16C204"; keyAsyncClient.getKey("keyName", keyVersion) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(key -> System.out.printf("Created key with name: %s and: id %s%n", key.getName(), key.getId()));
- Parameters:
name
- The name of thekey
, cannot benull
.version
- The version of the key to retrieve. If this is an empty String or null, this call is equivalent to callinggetKey(String)
, with the latest version being retrieved.- Returns:
- A
Mono
containing the requestedkey
. The content of the key isnull
if bothname
andversion
arenull
or empty. - Throws:
com.azure.core.exception.HttpResponseException
- If a validname
and a non null/emptyversion
is specified.com.azure.core.exception.ResourceNotFoundException
- When akey
with the providedname
doesn't exist in the key vault or an empty/null
name
and a non-null/emptyversion
is provided.
-
getKeyWithResponse
public Mono<com.azure.core.http.rest.Response<KeyVaultKey>> getKeyWithResponse(String name, String version) Gets the public part of the specifiedkey
and key version. The get key operation is applicable to allkey types
and it requires thekeys/get
permission.Code Samples
Gets a specific version of the
key
in the key vault. Subscribes to the call asynchronously and prints out theretrieved key
details when a response has been received.String keyVersion = "6A385B124DEF4096AF1361A85B16C204"; keyAsyncClient.getKeyWithResponse("keyName", keyVersion) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(getKeyResponse -> System.out.printf("Created key with name: %s and: id %s%n", getKeyResponse.getValue().getName(), getKeyResponse.getValue().getId()));
- Parameters:
name
- The name of thekey
, cannot benull
.version
- The version of the key to retrieve. If this is an empty String or null, this call is equivalent to callinggetKey(String)
, with the latest version being retrieved.- Returns:
- A
Mono
containing aResponse
whosevalue
contains the requestedkey
. The content of the key isnull
if bothname
andversion
arenull
or empty. - Throws:
com.azure.core.exception.HttpResponseException
- If a validname
and a non-null/emptyversion
is specified.com.azure.core.exception.ResourceNotFoundException
- When akey
with the providedname
doesn't exist in the key vault or an empty/null
name
and a non-null/emptyversion
is provided.
-
getKey
Gets the public part of the specifiedkey
and key version. The get key operation is applicable to allkey types
and it requires thekeys/get
permission.Code Samples
Gets a specific version of the
key
in the key vault. Subscribes to the call asynchronously and prints out theretrieved key
details when a response has been received.keyAsyncClient.getKey("keyName") .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(key -> System.out.printf("Created key with name: %s and: id %s%n", key.getName(), key.getId()));
- Parameters:
name
- The name of thekey
, cannot benull
.- Returns:
- A
Mono
containing the requestedkey
. The content of the key isnull
ifname
isnull
or empty. - Throws:
com.azure.core.exception.HttpResponseException
- If a validname
and a non-null/emptyversion
is specified.com.azure.core.exception.ResourceNotFoundException
- When akey
with the providedname
doesn't exist in the key vault or an empty/null
name
and a non-null/emptyversion
is provided.
-
updateKeyPropertiesWithResponse
public Mono<com.azure.core.http.rest.Response<KeyVaultKey>> updateKeyPropertiesWithResponse(KeyProperties keyProperties, KeyOperation... keyOperations) Updates theattributes
andkey operations
associated with the specifiedkey
, but not the cryptographic key material of the specifiedkey
in the key vault. The update operation changes specifiedattributes
of an existing storedkey
andattributes
that are not specified in the request are left unchanged. The cryptographic key material of akey
itself cannot be changed. This operation requires thekeys/set
permission.Code Samples
Gets latest version of the
key
, changes its notBefore time and then updates it in the Azure Key Vault. Subscribes to the call asynchronously and prints out thereturned key
details when a response has been received.keyAsyncClient.getKey("keyName") .subscribe(getKeyResponse -> { //Update the not before time of the key. getKeyResponse.getProperties().setNotBefore(OffsetDateTime.now().plusDays(50)); keyAsyncClient.updateKeyPropertiesWithResponse(getKeyResponse.getProperties(), KeyOperation.ENCRYPT, KeyOperation.DECRYPT) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(updateKeyResponse -> System.out.printf("Updated key's \"not before time\": %s%n", updateKeyResponse.getValue().getProperties().getNotBefore().toString())); });
- Parameters:
keyProperties
- Thekey properties
object with updated properties.keyOperations
- The updatedkey operations
to associate with the key.- Returns:
- A
Mono
containing aResponse
whosevalue
contains theupdated key
. - Throws:
com.azure.core.exception.HttpResponseException
- Ifname
orversion
is an empty string.NullPointerException
- IfkeyProperties
is null.com.azure.core.exception.ResourceNotFoundException
- When a key withname
andversion
doesn't exist in the key vault.
-
updateKeyProperties
public Mono<KeyVaultKey> updateKeyProperties(KeyProperties keyProperties, KeyOperation... keyOperations) Updates theattributes
andkey operations
associated with the specifiedkey
, but not the cryptographic key material of the specifiedkey
in the key vault. The update operation changes specifiedattributes
of an existing storedkey
andattributes
that are not specified in the request are left unchanged. The cryptographic key material of akey
itself cannot be changed. This operation requires thekeys/set
permission.Code Samples
Gets latest version of the
key
, changes its notBefore time and then updates it in the Azure Key Vault. Subscribes to the call asynchronously and prints out thereturned key
details when a response has been received.keyAsyncClient.getKey("keyName") .subscribe(key -> { //Update the not before time of the key. key.getProperties().setNotBefore(OffsetDateTime.now().plusDays(50)); keyAsyncClient.updateKeyProperties(key.getProperties(), KeyOperation.ENCRYPT, KeyOperation.DECRYPT) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(updatedKey -> System.out.printf("Updated key's \"not before time\": %s%n", updatedKey.getProperties().getNotBefore().toString())); });
- Parameters:
keyProperties
- Thekey properties
object with updated properties.keyOperations
- The updatedkey operations
to associate with the key.- Returns:
- A
Mono
containing theupdated key
. - Throws:
com.azure.core.exception.HttpResponseException
- Ifname
orversion
is an empty string.NullPointerException
- Ifkey
isnull
.com.azure.core.exception.ResourceNotFoundException
- When a key withname
andversion
doesn't exist in the key vault.
-
beginDeleteKey
Deletes akey
of any type from the key vault. If soft-delete is enabled on the key vault then thekey
is placed in the deleted state and requires to be purged for permanent deletion else thekey
is permanently deleted. The delete operation applies to anykey
stored in Azure Key Vault but it cannot be applied to an individual version of akey
. This operation removes the cryptographic material associated with thekey
, which means thekey
is not usable forSign/Verify
,Wrap/Unwrap
orEncrypt/Decrypt
operations. This operation requires thekeys/delete
permission.Code Samples
Deletes the
key
in the Azure Key Vault. Subscribes to the call asynchronously and prints out thedeleted key
details when a response has been received.keyAsyncClient.beginDeleteKey("keyName") .subscribe(pollResponse -> { System.out.printf("Deletion status: %s%n", pollResponse.getStatus()); System.out.printf("Key name: %s%n", pollResponse.getValue().getName()); System.out.printf("Key delete date: %s%n", pollResponse.getValue().getDeletedOn()); });
- Parameters:
name
- The name of thekey
to be deleted.- Returns:
- A
PollerFlux
to poll on thedeleted key
status. - Throws:
com.azure.core.exception.HttpResponseException
- When a key withname
is an empty string.com.azure.core.exception.ResourceNotFoundException
- When a key withname
doesn't exist in the key vault.
-
getDeletedKey
Gets the public part of adeleted key
. The get deleted Key operation is applicable for soft-delete enabled vaults. This operation requires thekeys/get
permission.Code Samples
Gets the
deleted key
from the key vault enabled for soft-delete. Subscribes to the call asynchronously and prints out thedeleted key
details when a response has been received.keyAsyncClient.getDeletedKey("keyName") .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(deletedKey -> System.out.printf("Deleted key's recovery id:%s%n", deletedKey.getRecoveryId()));
- Parameters:
name
- The name of the deletedkey
.- Returns:
- A
Mono
containing thedeleted key
. - Throws:
com.azure.core.exception.HttpResponseException
- When a key withname
is an empty string.com.azure.core.exception.ResourceNotFoundException
- When a key withname
doesn't exist in the key vault.
-
getDeletedKeyWithResponse
Gets the public part of adeleted key
. The get deleted Key operation is applicable for soft-delete enabled vaults. This operation requires thekeys/get
permission.Code Samples
Gets the
deleted key
from the key vault enabled for soft-delete. Subscribes to the call asynchronously and prints out thedeleted key
details when a response has been received.keyAsyncClient.getDeletedKeyWithResponse("keyName") .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(getDeletedKeyResponse -> System.out.printf("Deleted key's recovery id: %s%n", getDeletedKeyResponse.getValue().getRecoveryId()));
- Parameters:
name
- The name of the deletedkey
.- Returns:
- A
Mono
containing aResponse
whosevalue
contains thedeleted key
. - Throws:
com.azure.core.exception.HttpResponseException
- When a key withname
is an empty string.com.azure.core.exception.ResourceNotFoundException
- When a key withname
doesn't exist in the key vault.
-
purgeDeletedKey
Permanently deletes the specifiedkey
without the possibility of recovery. The purge deleted key operation is applicable for soft-delete enabled vaults. This operation requires thekeys/purge
permission.Code Samples
Purges the
deleted key
from the key vault enabled for soft-delete. Subscribes to the call asynchronously and prints out the status code from the server response when a response has been received.keyAsyncClient.purgeDeletedKey("deletedKeyName") .subscribe(ignored -> System.out.println("Successfully purged deleted key"));
- Parameters:
name
- The name of thedeleted key
.- Returns:
- An empty
Mono
. - Throws:
com.azure.core.exception.HttpResponseException
- When a key withname
is an empty string.com.azure.core.exception.ResourceNotFoundException
- When a key withname
doesn't exist in the key vault.
-
purgeDeletedKeyWithResponse
Permanently deletes the specifiedkey
without the possibility of recovery. The purge deleted key operation is applicable for soft-delete enabled vaults. This operation requires thekeys/purge
permission.Code Samples
Purges the
deleted key
from the key vault enabled for soft-delete. Subscribes to the call asynchronously and prints out the status code from the server response when a response has been received.keyAsyncClient.purgeDeletedKeyWithResponse("deletedKeyName") .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(purgeDeletedKeyResponse -> System.out.printf("Purge response status code: %d%n", purgeDeletedKeyResponse.getStatusCode()));
- Parameters:
name
- The name of thedeleted key
.- Returns:
- A
Mono
containing a Response containing status code and HTTP headers. - Throws:
com.azure.core.exception.HttpResponseException
- When a key withname
is an empty string.com.azure.core.exception.ResourceNotFoundException
- When a key withname
doesn't exist in the key vault.
-
beginRecoverDeletedKey
Recovers thedeleted key
in the key vault to its latest version and can only be performed on a soft-delete enabled vault. An attempt to recover annon-deleted key
will return an error. Consider this the inverse of the delete operation on soft-delete enabled vaults. This operation requires thekeys/recover
permission.Code Samples
Recovers the
deleted key
from the key vault enabled for soft-delete. Subscribes to the call asynchronously and prints out the recovered key details when a response has been received.keyAsyncClient.beginRecoverDeletedKey("deletedKeyName") .subscribe(pollResponse -> { System.out.printf("Recovery status: %s%n", pollResponse.getStatus()); System.out.printf("Key name: %s%n", pollResponse.getValue().getName()); System.out.printf("Key type: %s%n", pollResponse.getValue().getKeyType()); });
- Parameters:
name
- The name of thedeleted key
to be recovered.- Returns:
- A
PollerFlux
to poll on therecovered key
status. - Throws:
com.azure.core.exception.HttpResponseException
- When a key withname
is an empty string.com.azure.core.exception.ResourceNotFoundException
- When a key withname
doesn't exist in the key vault.
-
backupKey
Requests a backup of the specifiedkey
be downloaded to the client. The key backup operation exports akey
from Azure Key Vault in a protected form. Note that this operation does not return key material in a form that can be used outside the Azure Key Vault system, the returned key material is either protected to a Azure Key Vault HSM or to Azure Key Vault itself. The intent of this operation is to allow a client to generate akey
in one Azure Key Vault instance, backup thekey
, and then restore it into another Azure Key Vault instance. The backup operation may be used to export, in protected form, anykey type
from Azure Key Vault. Individual versions of akey
cannot be backed up.Backup/Restore
can be performed within geographical boundaries only; meaning that a backup from one geographical area cannot be restored to another geographical area. For example, a backup from the US geographical area cannot be restored in an EU geographical area. This operation requires thekey/backup
permission.Code Samples
Backs up the
key
from the key vault. Subscribes to the call asynchronously and prints out the length of the key's backup byte array returned in the response.keyAsyncClient.backupKey("keyName") .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(bytes -> System.out.printf("Key backup byte array length: %s%n", bytes.length));
-
backupKeyWithResponse
Requests a backup of the specifiedkey
be downloaded to the client. The key backup operation exports akey
from Azure Key Vault in a protected form. Note that this operation does not return key material in a form that can be used outside the Azure Key Vault system, the returned key material is either protected to a Azure Key Vault HSM or to Azure Key Vault itself. The intent of this operation is to allow a client to generate akey
in one Azure Key Vault instance, backup thekey
, and then restore it into another Azure Key Vault instance. The backup operation may be used to export, in protected form, anykey type
from Azure Key Vault. Individual versions of akey
cannot be backed up.Backup/Restore
can be performed within geographical boundaries only; meaning that a backup from one geographical area cannot be restored to another geographical area. For example, a backup from the US geographical area cannot be restored in an EU geographical area. This operation requires thekey/backup
permission.Code Samples
Backs up the
key
from the key vault. Subscribes to the call asynchronously and prints out the length of the key's backup byte array returned in the response.keyAsyncClient.backupKeyWithResponse("keyName") .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(backupKeyResponse -> System.out.printf("Key backup byte array length: %s%n", backupKeyResponse.getValue().length));
- Parameters:
name
- The name of thekey
.- Returns:
- A
Mono
containing aResponse
whosevalue
contains the backed up key blob. - Throws:
com.azure.core.exception.ResourceNotFoundException
- When a key withname
doesn't exist in the key vault.com.azure.core.exception.HttpResponseException
- When a key withname
is an empty string.
-
restoreKeyBackup
Restores a backed upkey
to a vault. Imports a previously backed upkey
into Azure Key Vault, restoring thekey
, its key identifier, attributes and access control policies. The restore operation may be used to import a previously backed upkey
. Individual versions of akey
cannot be restored. Thekey
is restored in its entirety with the same key name as it had when it was backed up. If the key name is not available in the target key vault, the restore operation will be rejected. While the key name is retained during restore, the final key identifier will change if thekey
is restored to a different vault. Restore will restore all versions and preserve version identifiers. The restore operation is subject to security constraints: The target key vault must be owned by the same Microsoft Azure Subscription as the source key vault. The user must have therestore
permission in the target key vault. This operation requires thekeys/restore
permission.Code Samples
Restores the
//Pass the Key Backup Byte array to the restore operation.key
in the key vault from its backup. Subscribes to the call asynchronously and prints out the restored key details when a response has been received.keyAsyncClient.restoreKeyBackup(keyBackupByteArray) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(restoreKeyResponse -> System.out.printf("Restored key with name: %s and: id %s%n", restoreKeyResponse.getName(), restoreKeyResponse.getId()));
- Parameters:
backup
- The backup blob associated with thekey
.- Returns:
- A
Mono
containing therestored key
. - Throws:
com.azure.core.exception.ResourceModifiedException
- Whenbackup
blob is malformed.
-
restoreKeyBackupWithResponse
public Mono<com.azure.core.http.rest.Response<KeyVaultKey>> restoreKeyBackupWithResponse(byte[] backup) Restores a backed upkey
to a vault. Imports a previously backed upkey
into Azure Key Vault, restoring thekey
, its key identifier, attributes and access control policies. The restore operation may be used to import a previously backed upkey
. Individual versions of akey
cannot be restored. Thekey
is restored in its entirety with the same key name as it had when it was backed up. If the key name is not available in the target key vault, the restore operation will be rejected. While the key name is retained during restore, the final key identifier will change if thekey
is restored to a different vault. Restore will restore all versions and preserve version identifiers. The restore operation is subject to security constraints: The target key vault must be owned by the same Microsoft Azure Subscription as the source key vault. The user must have therestore
permission in the target key vault. This operation requires thekeys/restore
permission.Code Samples
Restores the
//Pass the Key Backup Byte array to the restore operation.key
in the key vault from its backup. Subscribes to the call asynchronously and prints out the restored key details when a response has been received.keyAsyncClient.restoreKeyBackupWithResponse(keyBackupByteArray) .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(restoreKeyBackupResponse -> System.out.printf("Restored key with name: %s and: id %s%n", restoreKeyBackupResponse.getValue().getName(), restoreKeyBackupResponse.getValue().getId()));
- Parameters:
backup
- The backup blob associated with thekey
.- Returns:
- A
Mono
containing aResponse
whosevalue
contains therestored key
. - Throws:
com.azure.core.exception.ResourceModifiedException
- Whenbackup
blob is malformed.
-
listPropertiesOfKeys
Listkeys
in the key vault. Retrieves a list of thekeys
in the key vault asJsonWebKey
structures that contain the public part of a storedkey
. The list operation is applicable to allkey types
and the individualkey
response in the list is represented byKeyProperties
as only the key identifier, attributes and tags are provided in the response. The key material and individual key versions are not listed in the response. This operation requires thekeys/list
permission.Code Samples
It is possible to get
full keys
with key material from this information. Convert theFlux
containingkey properties
toFlux
containingkey
usinggetKey(String, String)
withinFlux.flatMap(Function)
.keyAsyncClient.listPropertiesOfKeys() .contextWrite(Context.of("key1", "value1", "key2", "value2")) .flatMap(keyProperties -> keyAsyncClient.getKey(keyProperties.getName(), keyProperties.getVersion())) .subscribe(key -> System.out.printf("Retrieved key with name: %s and type: %s%n", key.getName(), key.getKeyType()));
- Returns:
- A
PagedFlux
containingkey
of all the keys in the vault.
-
listDeletedKeys
Listsdeleted keys
of the key vault. Thedeleted keys
are retrieved asJsonWebKey
structures that contain the public part of adeleted key
. The get deleted keys operation is applicable for vaults enabled for soft-delete. This operation requires thekeys/list
permission.Code Samples
Lists the
deleted keys
in the key vault. Subscribes to the call asynchronously and prints out the recovery id of eachdeleted key
when a response has been received.keyAsyncClient.listDeletedKeys() .contextWrite(Context.of("key1", "value1", "key2", "value2")) .subscribe(deletedKey -> System.out.printf("Deleted key's recovery id:%s%n", deletedKey.getRecoveryId()));
- Returns:
- A
PagedFlux
containing all of thedeleted keys
in the vault.
-
listPropertiesOfKeyVersions
List all versions of the specifiedkeys
. The individual key response in the flux is represented byKeyProperties
as only the key identifier, attributes and tags are provided in the response. The key material values are not provided in the response. This operation requires thekeys/list
permission.Code Samples
It is possible to get the keys with key material of all the versions from this information. Convert the
Flux
containingkey properties
toFlux
containingkey
usinggetKey(String, String)
withinFlux.flatMap(Function)
.keyAsyncClient.listPropertiesOfKeyVersions("keyName") .contextWrite(Context.of("key1", "value1", "key2", "value2")) .flatMap(keyProperties -> keyAsyncClient.getKey(keyProperties.getName(), keyProperties.getVersion())) .subscribe(key -> System.out.printf("Retrieved key version: %s with name: %s and type: %s%n", key.getProperties().getVersion(), key.getName(), key.getKeyType()));
- Parameters:
name
- The name of thekey
.- Returns:
- A
PagedFlux
containingKeyProperties
of all the versions of the specifiedkeys
in the vault.Flux
is empty if key withname
does not exist in the key vault. - Throws:
com.azure.core.exception.ResourceNotFoundException
- When a given keyname
isnull
or an empty string.
-
getRandomBytes
Get the requested number of bytes containing random values from a managed HSM.Code Samples
Gets a number of bytes containing random values from a Managed HSM. Prints out the retrieved bytes in base64Url format.
int amount = 16; keyAsyncClient.getRandomBytes(amount) .subscribe(randomBytes -> System.out.printf("Retrieved %d random bytes: %s%n", amount, Arrays.toString(randomBytes)));
- Parameters:
count
- The requested number of random bytes.- Returns:
- A
Mono
containing the requested number of bytes containing random values from a managed HSM.
-
getRandomBytesWithResponse
Get the requested number of bytes containing random values from a managed HSM.Code Samples
Gets a number of bytes containing random values from a Managed HSM. Prints out the
HTTP Response
details and the retrieved bytes in base64Url format.int amountOfBytes = 16; keyAsyncClient.getRandomBytesWithResponse(amountOfBytes).subscribe(response -> System.out.printf("Response received successfully with status code: %d. Retrieved %d random bytes: %s%n", response.getStatusCode(), amountOfBytes, Arrays.toString(response.getValue())));
- Parameters:
count
- The requested number of random bytes.- Returns:
- A
Mono
containing theHTTP response
for this operation and the requested number of bytes containing random values from a managed HSM.
-
releaseKey
Releases the latest version of akey
.The
key
must be exportable. This operation requires thekeys/release
permission.Code Samples
Releases a
key
. Subscribes to the call asynchronously and prints out the signed object that contains thereleased key
when a response has been received.String targetAttestationToken = "someAttestationToken"; ReleaseKeyResult releaseKeyResult = keyClient.releaseKey("keyName", targetAttestationToken); System.out.printf("Signed object containing released key: %s%n", releaseKeyResult);
- Parameters:
name
- The name of thekey
to release.targetAttestationToken
- The attestation assertion for the target of thekey
release.- Returns:
- A
Mono
containing theReleaseKeyResult
containing the released key. - Throws:
IllegalArgumentException
- Ifname
ortargetAttestationToken
arenull
or empty.com.azure.core.exception.ResourceNotFoundException
- If thekey
for the providedname
does not exist.
-
releaseKey
public Mono<ReleaseKeyResult> releaseKey(String name, String version, String targetAttestationToken) Releases a key.The key must be exportable. This operation requires the 'keys/release' permission.
Code Samples
Releases a
key
. Subscribes to the call asynchronously and prints out the signed object that contains thereleased key
when a response has been received.String myKeyVersion = "6A385B124DEF4096AF1361A85B16C204"; String myTargetAttestationToken = "someAttestationToken"; keyAsyncClient.releaseKey("keyName", myKeyVersion, myTargetAttestationToken) .subscribe(releaseKeyResult -> System.out.printf("Signed object containing released key: %s%n", releaseKeyResult.getValue()));
- Parameters:
name
- The name of thekey
to release.version
- The version of the key to retrieve. If this is empty ornull
, this call is equivalent to callingreleaseKey(String, String)
, with the latest key version being released.targetAttestationToken
- The attestation assertion for the target of the key release.- Returns:
- A
Mono
containing theReleaseKeyResult
containing the released key. - Throws:
IllegalArgumentException
- Ifname
ortargetAttestationToken
arenull
or empty.com.azure.core.exception.ResourceNotFoundException
- If thekey
for the providedname
does not exist.
-
releaseKeyWithResponse
public Mono<com.azure.core.http.rest.Response<ReleaseKeyResult>> releaseKeyWithResponse(String name, String version, String targetAttestationToken, ReleaseKeyOptions releaseKeyOptions) Releases a key.The key must be exportable. This operation requires the 'keys/release' permission.
Code Samples
Releases a
key
. Subscribes to the call asynchronously and prints out theHTTP Response
details and the signed object that contains thereleased key
when a response has been received.String releaseKeyVersion = "6A385B124DEF4096AF1361A85B16C204"; String someTargetAttestationToken = "someAttestationToken"; ReleaseKeyOptions releaseKeyOptions = new ReleaseKeyOptions() .setAlgorithm(KeyExportEncryptionAlgorithm.RSA_AES_KEY_WRAP_256) .setNonce("someNonce"); keyAsyncClient.releaseKeyWithResponse("keyName", releaseKeyVersion, someTargetAttestationToken, releaseKeyOptions) .subscribe(releaseKeyResponse -> System.out.printf("Response received successfully with status code: %d. Signed object containing" + "released key: %s%n", releaseKeyResponse.getStatusCode(), releaseKeyResponse.getValue().getValue()));
- Parameters:
name
- The name of the key to release.version
- The version of the key to retrieve. If this is empty ornull
, this call is equivalent to callingreleaseKey(String, String)
, with the latest key version being released.targetAttestationToken
- The attestation assertion for the target of the key release.releaseKeyOptions
- Additionaloptions
for releasing akey
.- Returns:
- A
Mono
containing theHTTP response
for this operation and theReleaseKeyResult
containing the released key. - Throws:
IllegalArgumentException
- Ifname
ortargetAttestationToken
arenull
or empty.com.azure.core.exception.ResourceNotFoundException
- If thekey
for the providedname
does not exist.
-
rotateKey
Rotates akey
. The rotate key operation will do so based onkey's rotation policy
. This operation requires thekeys/rotate
permission.Code Samples
Rotates a
key
. Prints outrotated key
details.keyAsyncClient.rotateKey("keyName") .subscribe(key -> System.out.printf("Rotated key with name: %s and version:%s%n", key.getName(), key.getProperties().getVersion()));
- Parameters:
name
- The name ofkey
to be rotated. The system will generate a new version in the specifiedkey
.- Returns:
- The new version of the rotated
key
. - Throws:
IllegalArgumentException
- Ifname
isnull
or empty.com.azure.core.exception.ResourceNotFoundException
- If thekey
for the providedname
does not exist.
-
rotateKeyWithResponse
Rotates akey
. The rotate key operation will do so based onkey's rotation policy
. This operation requires thekeys/rotate
permission.Code Samples
Rotates a
key
. Subscribes to the call asynchronously and prints out theHTTP Response
androtated key
details when a response has been received.keyAsyncClient.rotateKeyWithResponse("keyName") .subscribe(rotateKeyResponse -> System.out.printf("Response received successfully with status code: %d. Rotated key with name: %s and" + "version: %s%n", rotateKeyResponse.getStatusCode(), rotateKeyResponse.getValue().getName(), rotateKeyResponse.getValue().getProperties().getVersion()));
- Parameters:
name
- The name ofkey
to be rotated. The system will generate a new version in the specifiedkey
.- Returns:
- A
Mono
containing theHTTP response
for this operation and the new version of the rotatedkey
. - Throws:
IllegalArgumentException
- Ifname
isnull
or empty.com.azure.core.exception.ResourceNotFoundException
- If thekey
for the providedname
does not exist.
-
getKeyRotationPolicy
Gets theKeyRotationPolicy
for thekey
with the provided name. This operation requires thekeys/get
permission.Code Samples
Retrieves the
rotation policy
of a givenkey
. Subscribes to the call asynchronously and prints out therotation policy key
details when a response has been received.keyAsyncClient.getKeyRotationPolicy("keyName") .subscribe(keyRotationPolicy -> System.out.printf("Retrieved key rotation policy with id: %s%n", keyRotationPolicy.getId()));
- Parameters:
keyName
- The name of thekey
.- Returns:
- A
Mono
containing theKeyRotationPolicy
for the key. - Throws:
IllegalArgumentException
- Ifname
isnull
or empty.com.azure.core.exception.ResourceNotFoundException
- If thekey
for the providedname
does not exist.
-
getKeyRotationPolicyWithResponse
public Mono<com.azure.core.http.rest.Response<KeyRotationPolicy>> getKeyRotationPolicyWithResponse(String keyName) Gets theKeyRotationPolicy
for thekey
with the provided name. This operation requires thekeys/get
permission.Code Samples
Retrieves the
rotation policy
of a givenkey
. Subscribes to the call asynchronously and prints out theHTTP Response
androtation policy key
details when a response has been received.keyAsyncClient.getKeyRotationPolicyWithResponse("keyName") .subscribe(getKeyRotationPolicyResponse -> System.out.printf("Response received successfully with status code: %d. Retrieved key rotation policy" + "with id: %s%n", getKeyRotationPolicyResponse.getStatusCode(), getKeyRotationPolicyResponse.getValue().getId()));
- Parameters:
keyName
- The name of thekey
.- Returns:
- A
Mono
containing theHTTP response
for this operation and theKeyRotationPolicy
for the key. - Throws:
IllegalArgumentException
- Ifname
isnull
or empty.com.azure.core.exception.ResourceNotFoundException
- If thekey
for the providedname
does not exist.
-
updateKeyRotationPolicy
public Mono<KeyRotationPolicy> updateKeyRotationPolicy(String keyName, KeyRotationPolicy keyRotationPolicy) Updates theKeyRotationPolicy
of the key with the provided name. This operation requires thekeys/update
permission.Code Samples
Updates the
rotation policy
of a givenkey
. Subscribes to the call asynchronously and prints out therotation policy key
details when a response has been received.List<KeyRotationLifetimeAction> lifetimeActions = new ArrayList<>(); KeyRotationLifetimeAction rotateLifetimeAction = new KeyRotationLifetimeAction(KeyRotationPolicyAction.ROTATE) .setTimeAfterCreate("P90D"); KeyRotationLifetimeAction notifyLifetimeAction = new KeyRotationLifetimeAction(KeyRotationPolicyAction.NOTIFY) .setTimeBeforeExpiry("P45D"); lifetimeActions.add(rotateLifetimeAction); lifetimeActions.add(notifyLifetimeAction); KeyRotationPolicy keyRotationPolicy = new KeyRotationPolicy() .setLifetimeActions(lifetimeActions) .setExpiresIn("P6M"); keyAsyncClient.updateKeyRotationPolicy("keyName", keyRotationPolicy) .subscribe(updatedPolicy -> System.out.printf("Updated key rotation policy with id: %s%n", updatedPolicy.getId()));
- Parameters:
keyName
- The name of thekey
.keyRotationPolicy
- TheKeyRotationPolicy
for the key.- Returns:
- A
Mono
containing theKeyRotationPolicy
for the key. - Throws:
IllegalArgumentException
- Ifname
isnull
or empty.com.azure.core.exception.ResourceNotFoundException
- If thekey
for the providedname
does not exist.
-
updateKeyRotationPolicyWithResponse
public Mono<com.azure.core.http.rest.Response<KeyRotationPolicy>> updateKeyRotationPolicyWithResponse(String keyName, KeyRotationPolicy keyRotationPolicy) Updates theKeyRotationPolicy
of the key with the provided name. This operation requires thekeys/update
permission.Code Samples
Updates the
rotation policy
of a givenkey
. Subscribes to the call asynchronously and prints out theHTTP Response
androtation policy key
details when a response has been received.List<KeyRotationLifetimeAction> myLifetimeActions = new ArrayList<>(); KeyRotationLifetimeAction myRotateLifetimeAction = new KeyRotationLifetimeAction(KeyRotationPolicyAction.ROTATE) .setTimeAfterCreate("P90D"); KeyRotationLifetimeAction myNotifyLifetimeAction = new KeyRotationLifetimeAction(KeyRotationPolicyAction.NOTIFY) .setTimeBeforeExpiry("P45D"); myLifetimeActions.add(myRotateLifetimeAction); myLifetimeActions.add(myNotifyLifetimeAction); KeyRotationPolicy myKeyRotationPolicy = new KeyRotationPolicy() .setLifetimeActions(myLifetimeActions) .setExpiresIn("P6M"); keyAsyncClient.updateKeyRotationPolicyWithResponse("keyName", myKeyRotationPolicy) .subscribe(myUpdatedPolicyResponse -> System.out.printf("Response received successfully with status code: %d. Updated key rotation policy" + "with id: %s%n", myUpdatedPolicyResponse.getStatusCode(), myUpdatedPolicyResponse.getValue().getId()));
- Parameters:
keyName
- The name of thekey
.keyRotationPolicy
- TheKeyRotationPolicy
for the key.- Returns:
- A
Mono
containing theHTTP response
for this operation and theKeyRotationPolicy
for the key. - Throws:
IllegalArgumentException
- Ifname
isnull
or empty.com.azure.core.exception.ResourceNotFoundException
- If thekey
for the providedname
does not exist.
-