com.azure.security.keyvault.administration.KeyVaultSettingsAsyncClient Maven / Gradle / Ivy
Show all versions of azure-security-keyvault-administration Show documentation
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.security.keyvault.administration;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.logging.ClientLogger;
import com.azure.security.keyvault.administration.implementation.KeyVaultAdministrationUtils;
import com.azure.security.keyvault.administration.implementation.KeyVaultErrorCodeStrings;
import com.azure.security.keyvault.administration.implementation.KeyVaultSettingsClientImpl;
import com.azure.security.keyvault.administration.implementation.models.KeyVaultErrorException;
import com.azure.security.keyvault.administration.implementation.models.Setting;
import com.azure.security.keyvault.administration.models.KeyVaultGetSettingsResult;
import com.azure.security.keyvault.administration.models.KeyVaultRoleDefinition;
import com.azure.security.keyvault.administration.models.KeyVaultSetting;
import com.azure.security.keyvault.administration.models.KeyVaultSettingType;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static com.azure.core.util.FluxUtil.monoError;
/**
* The {@link KeyVaultSettingsAsyncClient} provides asynchronous methods to create, update, get and list
* {@link KeyVaultSetting settings} for an Azure Key Vault account.
*
* Getting Started
*
* In order to interact with the Azure Key Vault service, you will need to create an instance of the
* {@link KeyVaultSettingsAsyncClient} 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 Backup Client
*
* The following code sample demonstrates the creation of a {@link KeyVaultSettingsAsyncClient}, using the
* {@link KeyVaultSettingsClientBuilder} to configure it.
*
*
*
* KeyVaultSettingsAsyncClient keyVaultSettingsAsyncClient = new KeyVaultSettingsClientBuilder()
* .vaultUrl("<your-managed-hsm-url>")
* .credential(new DefaultAzureCredentialBuilder().build())
* .buildAsyncClient();
*
*
*
*
*
*
*
* Get All Settings
* The {@link KeyVaultSettingsAsyncClient} can be used to list all the settings for an Azure Key Vault account.
*
* Code Sample:
* The following code sample demonstrates how to asynchronously back up an entire collection of keys using, using the
* {@link KeyVaultSettingsAsyncClient#getSettings()} API.
*
*
*
* keyVaultSettingsAsyncClient.getSettings().subscribe(getSettingsResult ->
* getSettingsResult.getSettings().forEach(setting ->
* System.out.printf("Retrieved setting with name '%s' and value %s'.%n", setting.getName(),
* setting.asBoolean())));
*
*
*
* Note: For the synchronous sample, refer to {@link KeyVaultSettingsClient}.
*
*
*
*
*
* Retrieve a Specific Setting
* The {@link KeyVaultSettingsClient} can be used to retrieve a specific setting.
*
* Code Sample:
* The following code sample demonstrates how to asynchronously restore an entire collection of keys from a backup,
* using the {@link KeyVaultSettingsClient#getSetting(String)} (String, String)} API.
*
*
*
* keyVaultSettingsAsyncClient.getSetting(settingName)
* .subscribe(setting ->
* System.out.printf("Retrieved setting '%s' with value '%s'.%n", setting.getName(), setting.asBoolean()));
*
*
*
* Note: For the synchronous sample, refer to {@link KeyVaultSettingsClient}.
*
*
*
*
*
* Update a Specific Setting
* The {@link KeyVaultSettingsAsyncClient} can be used to restore a specific key from a backup.
*
* Code Sample:
* The following code sample demonstrates how to asynchronously restore a specific key from a backup, using
* the {@link KeyVaultSettingsAsyncClient#updateSetting(KeyVaultSetting)} API.
*
*
*
* KeyVaultSetting settingToUpdate = new KeyVaultSetting(settingName, true);
*
* keyVaultSettingsAsyncClient.updateSetting(settingToUpdate)
* .subscribe(updatedSetting ->
* System.out.printf("Updated setting '%s' to '%s'.%n", updatedSetting.getName(),
* updatedSetting.asBoolean()));
*
*
*
* Note: For the synchronous sample, refer to {@link KeyVaultSettingsClient}.
*
*
*
*
*
* @see com.azure.security.keyvault.administration
* @see KeyVaultSettingsClientBuilder
*/
@ServiceClient(builder = KeyVaultSettingsClientBuilder.class, isAsync = true, serviceInterfaces =
KeyVaultSettingsClientImpl.KeyVaultSettingsClientService.class)
public final class KeyVaultSettingsAsyncClient {
private static final ClientLogger LOGGER = new ClientLogger(KeyVaultSettingsAsyncClient.class);
private final String vaultUrl;
private final KeyVaultSettingsClientImpl implClient;
/**
* Creates a {@link KeyVaultSettingsAsyncClient} that uses a {@link KeyVaultSettingsClientImpl} to service requests.
*
* @param vaultUrl The URL of the key vault this client will act on.
* @param implClient The implementation client used to service requests.
*/
KeyVaultSettingsAsyncClient(String vaultUrl, KeyVaultSettingsClientImpl implClient) {
this.vaultUrl = vaultUrl;
this.implClient = implClient;
}
/**
* Gets the {@link HttpPipeline} powering this client.
*
* @return The pipeline.
*/
HttpPipeline getHttpPipeline() {
return this.implClient.getHttpPipeline();
}
/**
* Updates a given {@link KeyVaultSetting account setting}.
*
* Code Samples
* Updates a given {@link KeyVaultSetting setting}. Prints out the details of the updated
* {@link KeyVaultRoleDefinition setting}.
*
*
* KeyVaultSetting settingToUpdate = new KeyVaultSetting(settingName, true);
*
* keyVaultSettingsAsyncClient.updateSetting(settingToUpdate)
* .subscribe(updatedSetting ->
* System.out.printf("Updated setting '%s' to '%s'.%n", updatedSetting.getName(),
* updatedSetting.asBoolean()));
*
*
*
* @param setting The {@link KeyVaultSetting account setting} to update.
*
* @return A {@link Mono} containing the updated {@link KeyVaultSetting account setting}.
*
* @throws NullPointerException if {@code setting} is {@code null}.
* @throws KeyVaultErrorException thrown if the request is rejected by the server.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono updateSetting(KeyVaultSetting setting) {
Objects.requireNonNull(setting, String.format(KeyVaultErrorCodeStrings.PARAMETER_REQUIRED, "'setting'"));
try {
String settingValue = null;
if (setting.getType() == KeyVaultSettingType.BOOLEAN) {
settingValue = Boolean.toString(setting.asBoolean());
}
return implClient.updateSettingAsync(vaultUrl, setting.getName(), settingValue)
.doOnRequest(ignored -> LOGGER.verbose("Updating account setting - {}", setting.getName()))
.doOnSuccess(response -> LOGGER.verbose("Updated account setting - {}", setting.getName()))
.doOnError(error -> LOGGER.warning("Failed updating account setting - {}", setting.getName(), error))
.onErrorMap(KeyVaultAdministrationUtils::mapThrowableToKeyVaultAdministrationException)
.map(KeyVaultSettingsAsyncClient::transformToKeyVaultSetting);
} catch (RuntimeException e) {
return monoError(LOGGER, e);
}
}
/**
* Updates a given {@link KeyVaultSetting account setting}.
*
* Code Samples
* Updates a given {@link KeyVaultSetting setting}. Prints out the details of the {@link Response HTTP response}
* and the updated {@link KeyVaultSetting setting}.
*
*
* KeyVaultSetting mySettingToUpdate = new KeyVaultSetting(settingName, true);
*
* keyVaultSettingsAsyncClient.updateSettingWithResponse(mySettingToUpdate)
* .subscribe(response ->
* System.out.printf("Response successful with status code: %d. Updated setting '%s' to '%s'.%n",
* response.getStatusCode(), response.getValue().getName(), response.getValue().asBoolean()));
*
*
*
* @param setting The {@link KeyVaultSetting account setting} to update.
*
* @return A {@link Mono} containing a {@link Response} whose {@link Response#getValue() value} contains the updated
* {@link KeyVaultSetting account setting}.
*
* @throws NullPointerException if {@code setting} is {@code null}.
* @throws KeyVaultErrorException thrown if the request is rejected by the server.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> updateSettingWithResponse(KeyVaultSetting setting) {
Objects.requireNonNull(setting, String.format(KeyVaultErrorCodeStrings.PARAMETER_REQUIRED, "'setting'"));
try {
String settingValue = null;
if (setting.getType() == KeyVaultSettingType.BOOLEAN) {
settingValue = Boolean.toString(setting.asBoolean());
}
return implClient.updateSettingWithResponseAsync(vaultUrl, setting.getName(), settingValue)
.doOnRequest(ignored -> LOGGER.verbose("Updating account setting - {}", setting.getName()))
.doOnSuccess(response -> LOGGER.verbose("Updated account setting - {}", setting.getName()))
.doOnError(error -> LOGGER.warning("Failed updating account setting - {}", setting.getName(), error))
.onErrorMap(KeyVaultAdministrationUtils::mapThrowableToKeyVaultAdministrationException)
.map(response -> new SimpleResponse<>(response, transformToKeyVaultSetting(response.getValue())));
} catch (RuntimeException e) {
return monoError(LOGGER, e);
}
}
/**
* Get the value of a specific account setting.
*
* Code Samples
* Retrieves a specific {@link KeyVaultSetting setting}. Prints out the details of the retrieved
* {@link KeyVaultRoleDefinition setting}.
*
*
* keyVaultSettingsAsyncClient.getSetting(settingName)
* .subscribe(setting ->
* System.out.printf("Retrieved setting '%s' with value '%s'.%n", setting.getName(), setting.asBoolean()));
*
*
*
* @param name The name of setting to retrieve the value of.
*
* @return A {@link Mono} containing the {@link KeyVaultSetting account setting}.
*
* @throws IllegalArgumentException thrown if the setting type is not supported.
* @throws KeyVaultErrorException thrown if the request is rejected by the server.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono getSetting(String name) {
try {
return implClient.getSettingAsync(vaultUrl, name)
.doOnRequest(ignored -> LOGGER.verbose("Retrieving account setting - {}", name))
.doOnSuccess(response -> LOGGER.verbose("Retrieved account setting - {}", name))
.doOnError(error -> LOGGER.warning("Failed retrieving account setting - {}", name, error))
.onErrorMap(KeyVaultAdministrationUtils::mapThrowableToKeyVaultAdministrationException)
.map(KeyVaultSettingsAsyncClient::transformToKeyVaultSetting);
} catch (RuntimeException e) {
return monoError(LOGGER, e);
}
}
/**
* Get the value of a specific account setting.
*
* Code Samples
* Retrieves a specific {@link KeyVaultSetting setting}. Prints out the details of the
* {@link Response HTTP response} and the retrieved {@link KeyVaultSetting setting}.
*
*
* keyVaultSettingsAsyncClient.getSettingWithResponse(settingName)
* .subscribe(response ->
* System.out.printf("Response successful with status code: %d. Retrieved setting '%s' with value '%s'.%n",
* response.getStatusCode(), response.getValue().getName(), response.getValue().asBoolean()));
*
*
*
* @param name The name of setting to retrieve the value of.
*
* @return A {@link Mono} containing a {@link Response} whose {@link Response#getValue() value} contains the
* {@link KeyVaultSetting account setting}.
*
* @throws IllegalArgumentException thrown if the setting type is not supported.
* @throws KeyVaultErrorException thrown if the request is rejected by the server.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> getSettingWithResponse(String name) {
try {
return implClient.getSettingWithResponseAsync(vaultUrl, name)
.doOnRequest(ignored -> LOGGER.verbose("Retrieving account setting - {}", name))
.doOnSuccess(response -> LOGGER.verbose("Retrieved account setting - {}", name))
.doOnError(error -> LOGGER.warning("Failed retrieving account setting - {}", name, error))
.onErrorMap(KeyVaultAdministrationUtils::mapThrowableToKeyVaultAdministrationException)
.map(response -> new SimpleResponse<>(response, transformToKeyVaultSetting(response.getValue())));
} catch (RuntimeException e) {
return monoError(LOGGER, e);
}
}
/**
* Get the account's settings.
*
* Code Samples
* Retrieves all the {@link KeyVaultSetting settings} for an account. Prints out the details of the retrieved
* {@link KeyVaultRoleDefinition settings}.
*
*
* keyVaultSettingsAsyncClient.getSettings().subscribe(getSettingsResult ->
* getSettingsResult.getSettings().forEach(setting ->
* System.out.printf("Retrieved setting with name '%s' and value %s'.%n", setting.getName(),
* setting.asBoolean())));
*
*
*
* @return A {@link Mono} containing a {@link KeyVaultGetSettingsResult result object} wrapping the list of
* {@link KeyVaultSetting account settings}.
*
* @throws IllegalArgumentException thrown if a setting type in the list is not supported.
* @throws KeyVaultErrorException thrown if the request is rejected by the server.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono getSettings() {
try {
return implClient.getSettingsAsync(vaultUrl)
.doOnRequest(ignored -> LOGGER.verbose("Listing account settings"))
.doOnSuccess(response -> LOGGER.verbose("Listed account settings successfully"))
.doOnError(error -> LOGGER.warning("Failed retrieving account settings", error))
.onErrorMap(KeyVaultAdministrationUtils::mapThrowableToKeyVaultAdministrationException)
.map(settingsListResult -> {
List keyVaultSettings = new ArrayList<>();
settingsListResult.getSettings().forEach(setting ->
keyVaultSettings.add(transformToKeyVaultSetting(setting)));
return new KeyVaultGetSettingsResult(keyVaultSettings);
});
} catch (RuntimeException e) {
return monoError(LOGGER, e);
}
}
/**
* Get the account's settings.
*
* Code Samples
* Retrieves all {@link KeyVaultSetting settings for an account}. Prints out the details of the
* {@link Response HTTP response} and the retrieved {@link KeyVaultSetting settings}.
*
*
* keyVaultSettingsAsyncClient.getSettingsWithResponse()
* .subscribe(response -> {
* System.out.printf("Response successful with status code: %d.", response.getStatusCode());
*
* KeyVaultGetSettingsResult getSettingsResult = response.getValue();
* List<KeyVaultSetting> settings = getSettingsResult.getSettings();
*
* settings.forEach(setting ->
* System.out.printf("Retrieved setting with name '%s' and value %s'.%n", setting.getName(),
* setting.asBoolean()));
* });
*
*
*
* @return A {@link Mono} containing a {@link Response} whose {@link Response#getValue() value} contains a
* {@link KeyVaultGetSettingsResult result object} wrapping the list of {@link KeyVaultSetting account settings}.
*
* @throws IllegalArgumentException thrown if a setting type in the list is not supported.
* @throws KeyVaultErrorException thrown if the request is rejected by the server.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono> getSettingsWithResponse() {
try {
return implClient.getSettingsWithResponseAsync(vaultUrl)
.doOnRequest(ignored -> LOGGER.verbose("Listing account settings"))
.doOnSuccess(response -> LOGGER.verbose("Listed account settings successfully"))
.doOnError(error -> LOGGER.warning("Failed retrieving account settings", error))
.onErrorMap(KeyVaultAdministrationUtils::mapThrowableToKeyVaultAdministrationException)
.map(response -> {
List keyVaultSettings = new ArrayList<>();
response.getValue().getSettings().forEach(setting ->
keyVaultSettings.add(transformToKeyVaultSetting(setting)));
return new SimpleResponse<>(response, new KeyVaultGetSettingsResult(keyVaultSettings));
});
} catch (RuntimeException e) {
return monoError(LOGGER, e);
}
}
static KeyVaultSetting transformToKeyVaultSetting(Setting setting) {
if (KeyVaultSettingType.BOOLEAN.toString().equalsIgnoreCase(setting.getType().toString())) {
return new KeyVaultSetting(setting.getName(), Boolean.parseBoolean(setting.getValue()));
} else {
throw new IllegalArgumentException(
String.format("Could not deserialize setting with name '%s'. Type '%s' is not supported.",
setting.getName(), setting.getType()));
}
}
}