Class CryptographyClientBuilder
- All Implemented Interfaces:
com.azure.core.client.traits.ConfigurationTrait<CryptographyClientBuilder>, com.azure.core.client.traits.HttpTrait<CryptographyClientBuilder>, com.azure.core.client.traits.TokenCredentialTrait<CryptographyClientBuilder>
CryptographyAsyncClient and CryptographyClient, by calling
buildAsyncClient() and buildClient() respectively
It constructs an instance of the desired client.
The minimal configuration options required by cryptographyClientBuilder to build
a CryptographyAsyncClient or a CryptographyClient are a credential and either
a JSON Web Key or a Azure Key Vault key identifier.
CryptographyAsyncClient cryptographyAsyncClient = new CryptographyClientBuilder()
.keyIdentifier("<your-key-id>")
.credential(new DefaultAzureCredentialBuilder().build())
.buildAsyncClient();
JsonWebKey jsonWebKey = new JsonWebKey().setId("SampleJsonWebKey");
CryptographyAsyncClient cryptographyAsyncClient = new CryptographyClientBuilder()
.jsonWebKey(jsonWebKey)
.buildAsyncClient();
When a CryptographyAsyncClient or CryptographyClient gets created using a
Azure Key Vault key identifier, the first time a cryptographic operation is attempted, the client will
attempt to retrieve the key material from the service, cache it, and perform all future cryptographic operations
locally, deferring to the service when that's not possible. If key retrieval and caching fails because of a
non-retryable error, the client will not make any further attempts and will fall back to performing all cryptographic
operations on the service side. Conversely, when a CryptographyAsyncClient or CryptographyClient gets
created using a JSON Web Key, all cryptographic operations will be performed locally.
To ensure correct behavior when performing operations such as Decrypt, Unwrap and
Verify, it is recommended to use a CryptographyAsyncClient or CryptographyClient created
for the specific key version that was used for the corresponding inverse operation: Encrypt,
Wrap, or Sign, respectively.
The log detail level, multiple custom policies and a custom
http client can be optionally configured in the CryptographyClientBuilder.
CryptographyAsyncClient cryptographyAsyncClient = new CryptographyClientBuilder()
.keyIdentifier("<your-key-id>")
.credential(new DefaultAzureCredentialBuilder().build())
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.httpClient(HttpClient.createDefault())
.buildAsyncClient();
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddPolicy(com.azure.core.http.policy.HttpPipelinePolicy policy) Adds apipeline policyto apply on each request sent.Creates aCryptographyAsyncClientbased on options set in the builder.Creates aCryptographyClientbased on options set in the builder.clientOptions(com.azure.core.util.ClientOptions clientOptions) Allows for setting common properties such as application ID, headers, proxy configuration, etc.configuration(com.azure.core.util.Configuration configuration) Sets the configuration store that is used during construction of the service client.credential(com.azure.core.credential.TokenCredential credential) Sets theTokenCredentialused to authorize requests sent to the service.Disables verifying if the authentication challenge resource matches the Key Vault or Managed HSM domain.Disables local key caching and defers all cryptographic operations to the service.httpClient(com.azure.core.http.HttpClient client) Sets theHttpClientto use for sending and receiving requests to and from the service.httpLogOptions(com.azure.core.http.policy.HttpLogOptions logOptions) Sets thelogging configurationto use when sending and receiving requests to and from the service.jsonWebKey(JsonWebKey jsonWebKey) Sets theJsonWebKeyto be used for local cryptography operations.keyIdentifier(String keyId) Sets the Azure Key Vault key identifier of the JSON Web Key to be used for cryptography operations.pipeline(com.azure.core.http.HttpPipeline pipeline) Sets theHttpPipelineto use for the service client.retryOptions(com.azure.core.http.policy.RetryOptions retryOptions) Sets theRetryOptionsfor all the requests made through the client.retryPolicy(com.azure.core.http.policy.RetryPolicy retryPolicy) Sets theRetryPolicythat is used when each request is sent.serviceVersion(CryptographyServiceVersion version) Sets theCryptographyServiceVersionthat is used when making API requests.
-
Constructor Details
-
CryptographyClientBuilder
public CryptographyClientBuilder()The constructor with defaults.
-
-
Method Details
-
buildClient
Creates aCryptographyClientbased on options set in the builder. Every timebuildClient()is called, a new instance ofCryptographyClientis created.If
jsonWebKeyis set, then all other builder settings are ignored.If
pipelineis set, then thepipelineandjsonWebKey identifierare used to create theclient. All other builder settings are ignored. Ifpipelineis not set, then anAzure Key Vault credentialandJSON Web Key identifierare required to build theclient.- Returns:
- A
CryptographyClientwith the options set from the builder. - Throws:
IllegalStateException- Ifcredential(TokenCredential)isnullorkeyIdentifier(String)is empty ornull.IllegalStateException- If bothretryOptions(RetryOptions)andretryPolicy(RetryPolicy)have been set.
-
buildAsyncClient
Creates aCryptographyAsyncClientbased on options set in the builder. Every timebuildAsyncClient()is called, a new instance ofCryptographyAsyncClientis created.If
jsonWebKeyis set, then all other builder settings are ignored.If
pipelineis set, then thepipelineandjsonWebKey identifier) are used to create theasync client. All other builder settings are ignored. Ifpipelineis not set, then anAzure Key Vault credentialandJSON Web Key identifierare required to build theasync client.- Returns:
- A
CryptographyAsyncClientwith the options set from the builder. - Throws:
IllegalStateException- Ifcredential(TokenCredential)isnullorkeyIdentifier(String)is empty ornull.IllegalStateException- If bothretryOptions(RetryOptions)andretryPolicy(RetryPolicy)have been set.
-
keyIdentifier
Sets the Azure Key Vault key identifier of the JSON Web Key to be used for cryptography operations. You should validate that this URL references a valid Key Vault or Managed HSM resource. Refer to the following documentation for details.To ensure correct behavior when performing operations such as
Decrypt,UnwrapandVerify, it is recommended to use aCryptographyAsyncClientorCryptographyClientcreated for the specific key version that was used for the corresponding inverse operation:EncryptWrap, orSign, respectively.- Parameters:
keyId- The Azure Key Vault key identifier of the JSON Web Key stored in the key vault.- Returns:
- The updated
CryptographyClientBuilderobject. - Throws:
NullPointerException- IfkeyIdisnull.
-
credential
Sets theTokenCredentialused to authorize requests sent to the service. Refer to the Azure SDK for Java identity and authentication documentation for more details on proper usage of theTokenCredentialtype.- Specified by:
credentialin interfacecom.azure.core.client.traits.TokenCredentialTrait<CryptographyClientBuilder>- Parameters:
credential-TokenCredentialused to authorize requests sent to the service.- Returns:
- The updated
CryptographyClientBuilderobject. - Throws:
NullPointerException- Ifcredentialisnull.
-
jsonWebKey
Sets theJsonWebKeyto be used for local cryptography operations.If
jsonWebKeyis provided, then all other builder settings are ignored.- Parameters:
jsonWebKey- The JSON Web Key to be used for local cryptography operations.- Returns:
- The updated
CryptographyClientBuilderobject. - Throws:
NullPointerException- IfjsonWebKeyisnull.
-
httpLogOptions
public CryptographyClientBuilder httpLogOptions(com.azure.core.http.policy.HttpLogOptions logOptions) Sets thelogging configurationto use when sending and receiving requests to and from the service. If alogLevelis not provided, default value ofHttpLogDetailLevel.NONEis set.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.- Specified by:
httpLogOptionsin interfacecom.azure.core.client.traits.HttpTrait<CryptographyClientBuilder>- Parameters:
logOptions- Thelogging configurationto use when sending and receiving requests to and from the service.- Returns:
- The updated
CryptographyClientBuilderobject.
-
addPolicy
Adds apipeline policyto apply on each request sent.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.- Specified by:
addPolicyin interfacecom.azure.core.client.traits.HttpTrait<CryptographyClientBuilder>- Parameters:
policy- Apipeline policy.- Returns:
- The updated
CryptographyClientBuilderobject. - Throws:
NullPointerException- Ifpolicyisnull.
-
httpClient
Sets theHttpClientto use for sending and receiving requests to and from the service.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.- Specified by:
httpClientin interfacecom.azure.core.client.traits.HttpTrait<CryptographyClientBuilder>- Parameters:
client- TheHttpClientto use for requests.- Returns:
- The updated
CryptographyClientBuilderobject.
-
pipeline
Sets theHttpPipelineto use for the service client.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.The
JSON Web Key identifieris not ignored whenpipelineis set.- Specified by:
pipelinein interfacecom.azure.core.client.traits.HttpTrait<CryptographyClientBuilder>- Parameters:
pipeline-HttpPipelineto use for sending service requests and receiving responses.- Returns:
- The updated
CryptographyClientBuilderobject.
-
configuration
Sets the configuration store that is used during construction of the service client. The default configuration store is a clone of theglobal configuration store, useConfiguration.NONEto bypass using configuration settings during construction.- Specified by:
configurationin interfacecom.azure.core.client.traits.ConfigurationTrait<CryptographyClientBuilder>- Parameters:
configuration- The configuration store used to get configuration details.- Returns:
- The updated
CryptographyClientBuilderobject.
-
serviceVersion
Sets theCryptographyServiceVersionthat is used when making API requests.If a service version is not provided, the service version that will be used will be the latest known service version based on the version of the client library being used. If no service version is specified, updating to a newer version the client library will have the result of potentially moving to a newer service version.
- Parameters:
version-CryptographyServiceVersionof the service to be used when making requests.- Returns:
- The updated
CryptographyClientBuilderobject.
-
retryPolicy
Sets theRetryPolicythat is used when each request is sent. The default retry policy will be used in the pipeline, if not provided. Setting this is mutually exclusive with usingretryOptions(RetryOptions).- Parameters:
retryPolicy- User'sRetryPolicyapplied to each request.- Returns:
- The updated
CryptographyClientBuilderobject.
-
retryOptions
Sets theRetryOptionsfor all the requests made through the client.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.Setting this is mutually exclusive with using
retryPolicy(RetryPolicy).- Specified by:
retryOptionsin interfacecom.azure.core.client.traits.HttpTrait<CryptographyClientBuilder>- Parameters:
retryOptions- TheRetryOptionsto use for all the requests made through the client.- Returns:
- The updated
CryptographyClientBuilderobject.
-
clientOptions
Allows for setting common properties such as application ID, headers, proxy configuration, etc. Note that it is recommended that this method be called with an instance of theHttpClientOptionsclass (a subclass of theClientOptionsbase class). The HttpClientOptions subclass provides more configuration options suitable for HTTP clients, which is applicable for any class that implements this HttpTrait interface.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.- Specified by:
clientOptionsin interfacecom.azure.core.client.traits.HttpTrait<CryptographyClientBuilder>- Parameters:
clientOptions- A configured instance ofHttpClientOptions.- Returns:
- The updated
CryptographyClientBuilderobject. - See Also:
-
disableChallengeResourceVerification
Disables verifying if the authentication challenge resource matches the Key Vault or Managed HSM domain. This verification is performed by default.- Returns:
- The updated
CryptographyClientBuilderobject.
-
disableKeyCaching
Disables local key caching and defers all cryptographic operations to the service.This method will have no effect if used in conjunction with the
jsonWebKey(JsonWebKey)method.- Returns:
- The updated
CryptographyClientBuilderobject.
-