Azure KeyVault Administration client library for .NET - version 4.5.0
Azure Key Vault Managed HSM is a fully-managed, highly-available, single-tenant, standards-compliant cloud service that enables you to safeguard cryptographic keys for your cloud applications using FIPS 140-2 Level 3 validated HSMs.
The Azure Key Vault administration library clients support administrative tasks such as full backup / restore and key-level role-based access control (RBAC).
Source code | Package (NuGet) | Product documentation | Samples
Getting started
Install the package
Install the Azure Key Vault administration client library for .NET with NuGet:
dotnet add package Azure.Security.KeyVault.Administration
Prerequisites
- An Azure subscription.
- An existing Azure Key Vault. If you need to create an Azure Key Vault, you can use the Azure CLI.
- Authorization to an existing Azure Key Vault using either RBAC (recommended) or access control.
To create a Managed HSM resource, run the following CLI command:
az keyvault create --hsm-name <your-key-vault-name> --resource-group <your-resource-group-name> --administrators <your-user-object-id> --location <your-azure-location>
To get <your-user-object-id>
you can run the following CLI command:
az ad user show --id <your-user-principal> --query id
Authenticate the client
In order to interact with the Azure Key Vault service, you'll need to create an instance of the client classes below. You need a vault url, which you may see as "DNS Name" in the portal, and credentials to instantiate a client object.
The examples shown below use a DefaultAzureCredential
, 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.
To use the DefaultAzureCredential
provider shown below,
or other credential providers provided with the Azure SDK, you must first install the Azure.Identity package:
dotnet add package Azure.Identity
Activate your managed HSM
All data plane commands are disabled until the HSM is activated. You will not be able to create keys or assign roles. Only the designated administrators that were assigned during the create command can activate the HSM. To activate the HSM you must download the security domain.
To activate your HSM you need:
- A minimum of 3 RSA key-pairs (maximum 10)
- Specify the minimum number of keys required to decrypt the security domain (quorum)
To activate the HSM you send at least 3 (maximum 10) RSA public keys to the HSM. The HSM encrypts the security domain with these keys and sends it back. Once this security domain is successfully downloaded, your HSM is ready to use. You also need to specify quorum, which is the minimum number of private keys required to decrypt the security domain.
The example below shows how to use openssl to generate 3 self-signed certificates.
openssl req -newkey rsa:2048 -nodes -keyout cert_0.key -x509 -days 365 -out cert_0.cer
openssl req -newkey rsa:2048 -nodes -keyout cert_1.key -x509 -days 365 -out cert_1.cer
openssl req -newkey rsa:2048 -nodes -keyout cert_2.key -x509 -days 365 -out cert_2.cer
Use the az keyvault security-domain download
command to download the security domain and activate your managed HSM.
The example below uses 3 RSA key pairs (only public keys are needed for this command) and sets the quorum to 2.
az keyvault security-domain download --hsm-name <your-managed-hsm-name> --sd-wrapping-keys ./certs/cert_0.cer ./certs/cert_1.cer ./certs/cert_2.cer --sd-quorum 2 --security-domain-file ContosoMHSM-SD.json
Controlling access to your managed HSM
The designated administrators assigned during creation are automatically added to the "Managed HSM Administrators" built-in role, who are able to download a security domain and manage roles for data plane access, among other limited permissions.
To perform other actions on keys, you need to assign principals to other roles such as "Managed HSM Crypto User", which can perform non-destructive key operations:
az keyvault role assignment create --hsm-name <your-managed-hsm-name> --role "Managed HSM Crypto User" --scope / --assignee-object-id <principal-or-user-object-ID> --assignee-principal-type <principal-type>
Please read best practices for properly securing your managed HSM.
Create KeyVaultAccessControlClient
Instantiate a DefaultAzureCredential
to pass to the KeyVaultAccessControlClient
.
The same instance of a token credential can be used with multiple clients if they will be authenticating with the same identity.
KeyVaultAccessControlClient client = new KeyVaultAccessControlClient(new Uri(managedHsmUrl), new DefaultAzureCredential());
Create KeyVaultBackupClient
Instantiate a DefaultAzureCredential
to pass to the KeyVaultBackupClient
.
The same instance of a token credential can be used with multiple clients if they will be authenticating with the same identity.
KeyVaultBackupClient client = new KeyVaultBackupClient(new Uri(managedHsmUrl), new DefaultAzureCredential());
Create KeyVaultSettingClient
Instantiate a DefaultAzureCredential
to pass to the KeyVaultSettingsClient
.
The same instance of a token credential can be used with multiple clients if they will be authenticating with the same identity.
KeyVaultSettingsClient client = new KeyVaultSettingsClient(new Uri(managedHsmUrl), new DefaultAzureCredential());
Key concepts
KeyVaultRoleDefinition
A KeyVaultRoleDefinition
is a collection of permissions. A role definition defines the operations that can be performed, such as read, write,
and delete. It can also define the operations that are excluded from allowed operations.
KeyVaultRoleDefinitions can be listed and specified as part of a KeyVaultRoleAssignment
.
KeyVaultRoleAssignment
A KeyVaultRoleAssignment
is the association of a KeyVaultRoleDefinition to a service principal. They can be created, listed, fetched individually, and deleted.
KeyVaultAccessControlClient
A KeyVaultAccessControlClient
provides both synchronous and asynchronous operations allowing for management of KeyVaultRoleDefinition
and KeyVaultRoleAssignment
objects.
KeyVaultBackupClient
A KeyVaultBackupClient
provides both synchronous and asynchronous operations for performing full key backups, full key restores, and selective key restores.
BackupOperation
A BackupOperation
represents a long running operation for a full key backup.
RestoreOperation
A RestoreOperation
represents a long running operation for both a full key and selective key restore.
Thread safety
We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.
Additional concepts
Client options | Accessing the response | Long-running operations | Handling failures | Diagnostics | Mocking | Client lifetime
Examples
The Azure.Security.KeyVault.Administration package supports synchronous and asynchronous APIs.
The following section provides several code snippets using the client
created above for either access control or backup clients, covering some of the most common Azure Key Vault access control related tasks:
Sync examples
- Access control
- Backup and restore
Async examples
- Access control
- Backup and restore
Troubleshooting
See our troubleshooting guide for details on how to diagnose various failure scenarios.
General
When you interact with the Azure Key Vault Administration library using the .NET SDK, errors returned by the service correspond to the same HTTP status codes returned for REST API requests.
For example, if you try to retrieve a role assignment that doesn't exist in your Azure Key Vault, a 404
error is returned, indicating "Not Found".
try
{
KeyVaultRoleAssignment roleAssignment = client.GetRoleAssignment(KeyVaultRoleScope.Global, "example-name");
}
catch (RequestFailedException ex)
{
Console.WriteLine(ex.ToString());
}
Azure.RequestFailedException: Service request failed.
Status: 404 (Not Found)
Content:
{"error":{"code":"RoleAssignmentNotFound","message":"Requested role assignment not found (Activity ID: a67f09f4-b68e-11ea-bd6d-0242ac120006)"}}
Headers:
X-Content-Type-Options: REDACTED
x-ms-request-id: a67f09f4-b68e-11ea-bd6d-0242ac120006
Content-Length: 143
Content-Type: application/json
Setting up console logging
The simplest way to see the logs is to enable the console logging.
To create an Azure SDK log listener that outputs messages to console, use the AzureEventSourceListener.CreateConsoleLogger
method.
// Setup a listener to monitor logged events.
using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();
To learn more about other logging mechanisms see here.
Next steps
Get started with our samples.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Azure SDK for .NET