Azure.Security.KeyVault.Administration
4.3.0
Prefix Reserved
See the version list below for details.
dotnet add package Azure.Security.KeyVault.Administration --version 4.3.0
NuGet\Install-Package Azure.Security.KeyVault.Administration -Version 4.3.0
<PackageReference Include="Azure.Security.KeyVault.Administration" Version="4.3.0" />
paket add Azure.Security.KeyVault.Administration --version 4.3.0
#r "nuget: Azure.Security.KeyVault.Administration, 4.3.0"
// Install Azure.Security.KeyVault.Administration as a Cake Addin #addin nuget:?package=Azure.Security.KeyVault.Administration&version=4.3.0 // Install Azure.Security.KeyVault.Administration as a Cake Tool #tool nuget:?package=Azure.Security.KeyVault.Administration&version=4.3.0
Azure KeyVault Administration client library for .NET
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Azure.Core (>= 1.30.0)
- System.Text.Json (>= 4.7.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Azure.Security.KeyVault.Administration:
Repository | Stars |
---|---|
Azure/azure-powershell
Microsoft Azure PowerShell
|
Version | Downloads | Last updated |
---|---|---|
4.5.0 | 1,578 | 10/15/2024 |
4.4.0 | 53,314 | 2/15/2024 |
4.4.0-beta.2 | 722 | 11/13/2023 |
4.4.0-beta.1 | 19,678 | 11/10/2023 |
4.3.0 | 108,840 | 3/14/2023 |
4.3.0-beta.1 | 964 | 11/9/2022 |
4.2.0 | 15,643 | 9/20/2022 |
4.1.0 | 29,928 | 3/26/2022 |
4.1.0-beta.3 | 974 | 2/9/2022 |
4.1.0-beta.2 | 409 | 10/14/2021 |
4.1.0-beta.1 | 292 | 8/11/2021 |
4.0.0 | 74,465 | 6/16/2021 |
4.0.0-beta.5 | 279 | 5/12/2021 |
4.0.0-beta.4 | 944 | 2/11/2021 |
4.0.0-beta.3 | 423 | 11/13/2020 |
4.0.0-beta.2 | 36,139 | 10/7/2020 |
4.0.0-beta.1 | 375 | 9/9/2020 |