com.azure.security.keyvault.administration.KeyVaultSettingsClient 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.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
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.implementation.models.SettingsListResult;
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 java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* The {@link KeyVaultSettingsClient} provides synchronous 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 KeyVaultSettingsClient} 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 Synchronous Backup Client
*
* The following code sample demonstrates the creation of a {@link KeyVaultSettingsClient}, using the
* {@link KeyVaultSettingsClientBuilder} to configure it.
*
*
*
* KeyVaultSettingsClient keyVaultSettingsClient = new KeyVaultSettingsClientBuilder()
* .vaultUrl("<your-managed-hsm-url>")
* .credential(new DefaultAzureCredentialBuilder().build())
* .buildClient();
*
*
*
*
*
*
*
* Get All Settings
* The {@link KeyVaultSettingsClient} can be used to list all the settings for an Azure Key Vault account.
*
* Code Sample:
* The following code sample demonstrates how to synchronously back up an entire collection of keys using, using the
* {@link KeyVaultSettingsClient#getSettings()} API.
*
*
*
* KeyVaultGetSettingsResult getSettingsResult = keyVaultSettingsClient.getSettings();
* List<KeyVaultSetting> settings = getSettingsResult.getSettings();
*
* settings.forEach(setting ->
* System.out.printf("Retrieved setting with name '%s' and value %s'.%n", setting.getName(),
* setting.asBoolean()));
*
*
*
* Note: For the asynchronous sample, refer to {@link KeyVaultSettingsAsyncClient}.
*
*
*
*
*
* Retrieve a Specific Setting
* The {@link KeyVaultSettingsClient} can be used to retrieve a specific setting.
*
* Code Sample:
* The following code sample demonstrates how to synchronously restore an entire collection of keys from a backup,
* using the {@link KeyVaultSettingsClient#getSetting(String)} (String, String)} API.
*
*
*
* KeyVaultSetting setting = keyVaultSettingsClient.getSetting(settingName);
*
* System.out.printf("Retrieved setting '%s' with value '%s'.%n", setting.getName(), setting.asBoolean());
*
*
*
* Note: For the asynchronous sample, refer to {@link KeyVaultSettingsAsyncClient}.
*
*
*
*
*
* Update a Specific Setting
* The {@link KeyVaultSettingsClient} can be used to restore a specific key from a backup.
*
* Code Sample:
* The following code sample demonstrates how to synchronously restore a specific key from a backup, using
* the {@link KeyVaultSettingsClient#updateSetting(KeyVaultSetting)} API.
*
*
*
* KeyVaultSetting settingToUpdate = new KeyVaultSetting(settingName, true);
* KeyVaultSetting updatedSetting = keyVaultSettingsClient.updateSetting(settingToUpdate);
*
* System.out.printf("Updated setting '%s' to '%s'.%n", updatedSetting.getName(), updatedSetting.asBoolean());
*
*
*
* Note: For the asynchronous sample, refer to {@link KeyVaultSettingsAsyncClient}.
*
*
*
*
*
* @see com.azure.security.keyvault.administration
* @see KeyVaultSettingsClientBuilder
*/
@ServiceClient(builder = KeyVaultSettingsClientBuilder.class)
public final class KeyVaultSettingsClient {
private static final ClientLogger LOGGER = new ClientLogger(KeyVaultSettingsClient.class);
private final String vaultUrl;
private final KeyVaultSettingsClientImpl implClient;
/**
* Initializes an instance of {@link KeyVaultSettingsClient} class.
*
* @param vaultUrl The URL of the key vault this client will act on.
* @param implClient The implementation client used to service requests.
*/
KeyVaultSettingsClient(String vaultUrl, KeyVaultSettingsClientImpl implClient) {
this.vaultUrl = vaultUrl;
this.implClient = implClient;
}
/**
* 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);
* KeyVaultSetting updatedSetting = keyVaultSettingsClient.updateSetting(settingToUpdate);
*
* System.out.printf("Updated setting '%s' to '%s'.%n", updatedSetting.getName(), updatedSetting.asBoolean());
*
*
*
* @param setting The {@link KeyVaultSetting account setting} to update.
*
* @return 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 KeyVaultSetting 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 KeyVaultSettingsAsyncClient.transformToKeyVaultSetting(
implClient.updateSetting(vaultUrl, setting.getName(), settingValue));
} catch (RuntimeException e) {
throw LOGGER.logExceptionAsError(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);
* Response<KeyVaultSetting> response =
* keyVaultSettingsClient.updateSettingWithResponse(mySettingToUpdate, new Context("key1", "value1"));
*
* 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.
* @param context Additional {@link Context} that is passed through the HTTP pipeline during the service call.
*
* @return 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 Response updateSettingWithResponse(KeyVaultSetting setting, Context context) {
Objects.requireNonNull(setting, String.format(KeyVaultErrorCodeStrings.PARAMETER_REQUIRED, "'setting'"));
try {
String settingValue = null;
if (setting.getType() == KeyVaultSettingType.BOOLEAN) {
settingValue = Boolean.toString(setting.asBoolean());
}
Response response =
implClient.updateSettingWithResponse(vaultUrl, setting.getName(), settingValue, context);
return new SimpleResponse<>(response,
KeyVaultSettingsAsyncClient.transformToKeyVaultSetting(response.getValue()));
} catch (RuntimeException e) {
throw LOGGER.logExceptionAsError(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}.
*
*
* KeyVaultSetting setting = keyVaultSettingsClient.getSetting(settingName);
*
* 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 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 KeyVaultSetting getSetting(String name) {
try {
return KeyVaultSettingsAsyncClient.transformToKeyVaultSetting(this.implClient.getSetting(vaultUrl, name));
} catch (RuntimeException e) {
throw LOGGER.logExceptionAsError(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}.
*
*
* Response<KeyVaultSetting> response =
* keyVaultSettingsClient.getSettingWithResponse(settingName, new Context("key1", "value1"));
*
* 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.
* @param context Additional {@link Context} that is passed through the HTTP pipeline during the service call.
*
* @return 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 Response getSettingWithResponse(String name, Context context) {
try {
Response response = implClient.getSettingWithResponse(vaultUrl, name, context);
return new SimpleResponse<>(response,
KeyVaultSettingsAsyncClient.transformToKeyVaultSetting(response.getValue()));
} catch (RuntimeException e) {
throw LOGGER.logExceptionAsError(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}.
*
*
* KeyVaultGetSettingsResult getSettingsResult = keyVaultSettingsClient.getSettings();
* 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 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 KeyVaultGetSettingsResult getSettings() {
List keyVaultSettings = new ArrayList<>();
try {
implClient.getSettings(vaultUrl).getSettings()
.forEach(setting -> keyVaultSettings.add(KeyVaultSettingsAsyncClient.transformToKeyVaultSetting(setting)));
return new KeyVaultGetSettingsResult(keyVaultSettings);
} catch (RuntimeException e) {
throw LOGGER.logExceptionAsError(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}.
*
*
* Response<KeyVaultGetSettingsResult> response =
* keyVaultSettingsClient.getSettingsWithResponse(new Context("key1", "value1"));
*
* System.out.printf("Response successful with status code: %d.", response.getStatusCode());
*
* KeyVaultGetSettingsResult myGetSettingsResult = response.getValue();
* List<KeyVaultSetting> mySettings = myGetSettingsResult.getSettings();
*
* mySettings.forEach(setting ->
* System.out.printf("Retrieved setting with name '%s' and value %s'.%n", setting.getName(),
* setting.asBoolean()));
*
*
*
* @param context Additional {@link Context} that is passed through the HTTP pipeline during the service call.
*
* @return 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 Response getSettingsWithResponse(Context context) {
try {
Response response = implClient.getSettingsWithResponse(vaultUrl, context);
List keyVaultSettings = new ArrayList<>();
response.getValue().getSettings()
.forEach(setting ->
keyVaultSettings.add(KeyVaultSettingsAsyncClient.transformToKeyVaultSetting(setting)));
return new SimpleResponse<>(response, new KeyVaultGetSettingsResult(keyVaultSettings));
} catch (RuntimeException e) {
throw LOGGER.logExceptionAsError(e);
}
}
}