|
| 1 | +# Azure KeyVault Administration client module for Go |
| 2 | + |
| 3 | +>**Note:** The Administration module only works with [Managed HSM][managed_hsm] – functions targeting a Key Vault will fail. |
| 4 | +
|
| 5 | +* Vault administration (this module) - role-based access control (RBAC), settings, and vault-level backup and restore options |
| 6 | +* Certificate management ([azcertificates](https://aka.ms/azsdk/go/keyvault-certificates/docs)) - create, manage, and deploy public and private SSL/TLS certificates |
| 7 | +* Cryptographic key management ([azkeys](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/keyvault/azkeys)) - create, store, and control access to the keys used to encrypt your data |
| 8 | +* Secrets management ([azsecrets](https://aka.ms/azsdk/go/keyvault-secrets/docs)) - securely store and control access to tokens, passwords, certificates, API keys, and other secrets |
| 9 | + |
| 10 | +Azure Key Vault Managed HSM is a fully-managed, highly-available, single-tenant, standards-compliant cloud service that enables you to safeguard |
| 11 | +cryptographic keys for your cloud applications using FIPS 140-2 Level 3 validated HSMs. |
| 12 | + |
| 13 | +The Azure Key Vault administration library clients support administrative tasks such as full backup / restore, key-level role-based access control (RBAC), and settings management. |
| 14 | + |
| 15 | +Source code | Package (pkg.go.dev)| [Product documentation][managed_hsm_docs] | Samples |
| 16 | + |
| 17 | +## Getting started |
| 18 | + |
| 19 | +### Install the package |
| 20 | + |
| 21 | +Install `azadmin` and `azidentity` with `go get`: |
| 22 | +``` |
| 23 | +go get github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azadmin |
| 24 | +go get github.com/Azure/azure-sdk-for-go/sdk/azidentity |
| 25 | +``` |
| 26 | +[azidentity][azure_identity] is used for Azure Active Directory authentication during client contruction. |
| 27 | + |
| 28 | + |
| 29 | +### Prerequisites |
| 30 | + |
| 31 | +* An [Azure subscription][azure_sub]. |
| 32 | +* A supported Go version (the Azure SDK supports the two most recent Go releases) |
| 33 | +* An existing [Key Vault Managed HSM][managed_hsm]. If you need to create one, you can do so using the Azure CLI by following the steps in [this document][create_managed_hsm]. |
| 34 | + |
| 35 | +### Authentication |
| 36 | + |
| 37 | +This document demonstrates using [azidentity.NewDefaultAzureCredential][default_cred_ref] to authenticate. This credential type works in both local development and production environments. We recommend using a [managed identity][managed_identity] in production. |
| 38 | + |
| 39 | +The clients accept any [azidentity][azure_identity] credential. See the [azidentity][azure_identity] documentation for more information about other credential types. |
| 40 | + |
| 41 | +#### Create a client |
| 42 | + |
| 43 | +Constructing the client also requires your Managed HSM's URL, which you can get from the Azure CLI or the Azure Portal. |
| 44 | + |
| 45 | +## Key concepts |
| 46 | + |
| 47 | +### RoleDefinition |
| 48 | + |
| 49 | +A `RoleDefinition` is a collection of permissions. A role definition defines the operations that can be performed, such as read, write, |
| 50 | +and delete. It can also define the operations that are excluded from allowed operations. |
| 51 | + |
| 52 | +RoleDefinitions can be listed and specified as part of a `RoleAssignment`. |
| 53 | + |
| 54 | +### RoleAssignment |
| 55 | + |
| 56 | +A `RoleAssignment` is the association of a RoleDefinition to a service principal. They can be created, listed, fetched individually, and deleted. |
| 57 | + |
| 58 | +### rbac.Client |
| 59 | + |
| 60 | +An `rbac.Client` allows for management of `RoleDefinition` and `RoleAssignment` types. |
| 61 | + |
| 62 | +### backup.Client |
| 63 | + |
| 64 | +A `backup.Client` allows for performing full key backups, full key restores, and selective key restores. |
| 65 | + |
| 66 | +### settings.Client |
| 67 | + |
| 68 | +A `settings.Client` provides methods to update, get, and list settings for a Managed HSM. |
| 69 | + |
| 70 | +## Examples |
| 71 | + |
| 72 | +Get started with our examples. |
| 73 | + |
| 74 | +## Troubleshooting |
| 75 | + |
| 76 | +### Error Handling |
| 77 | + |
| 78 | +All methods which send HTTP requests return `*azcore.ResponseError` when these requests fail. `ResponseError` has error details and the raw response from Key Vault. |
| 79 | + |
| 80 | +```go |
| 81 | +import "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
| 82 | + |
| 83 | +settings, err := client.GetSettings(context.Background(), nil) |
| 84 | +if err != nil { |
| 85 | + var httpErr *azcore.ResponseError |
| 86 | + if errors.As(err, &httpErr) { |
| 87 | + // TODO: investigate httpErr |
| 88 | + } else { |
| 89 | + // TODO: not an HTTP error |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### Logging |
| 95 | + |
| 96 | +This module uses the logging implementation in `azcore`. To turn on logging for all Azure SDK modules, set `AZURE_SDK_GO_LOGGING` to `all`. By default the logger writes to stderr. Use the `azcore/log` package to control log output. For example, logging only HTTP request and response events, and printing them to stdout: |
| 97 | + |
| 98 | +```go |
| 99 | +import azlog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log" |
| 100 | + |
| 101 | +// Print log events to stdout |
| 102 | +azlog.SetListener(func(cls azlog.Event, msg string) { |
| 103 | + fmt.Println(msg) |
| 104 | +}) |
| 105 | + |
| 106 | +// Includes only requests and responses in credential logs |
| 107 | +azlog.SetEvents(azlog.EventRequest, azlog.EventResponse) |
| 108 | +``` |
| 109 | + |
| 110 | +### Accessing `http.Response` |
| 111 | + |
| 112 | +You can access the raw `*http.Response` returned by Key Vault using the `runtime.WithCaptureResponse` method and a context passed to any client method. |
| 113 | + |
| 114 | +```go |
| 115 | +import "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" |
| 116 | + |
| 117 | +var response *http.Response |
| 118 | +ctx := runtime.WithCaptureResponse(context.TODO(), &response) |
| 119 | +_, err = client.GetSettings(context.Background(), nil) |
| 120 | +if err != nil { |
| 121 | + // TODO: handle error |
| 122 | +} |
| 123 | +// TODO: do something with response |
| 124 | +``` |
| 125 | + |
| 126 | +## Contributing |
| 127 | + |
| 128 | +This project welcomes contributions and suggestions. Most contributions require |
| 129 | +you to agree to a Contributor License Agreement (CLA) declaring that you have |
| 130 | +the right to, and actually do, grant us the rights to use your contribution. For |
| 131 | +details, visit <https://cla.microsoft.com>. |
| 132 | + |
| 133 | +This project has adopted the [Microsoft Open Source Code of Conduct][code_of_conduct]. |
| 134 | +For more information see the [Code of Conduct FAQ][coc_faq] |
| 135 | +or contact [email protected] with any |
| 136 | +additional questions or comments. |
| 137 | + |
| 138 | +<!-- LINKS --> |
| 139 | +[azure_identity]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity |
| 140 | +[azure_sub]: https://azure.microsoft.com/free |
| 141 | +[create_managed_hsm]: https://learn.microsoft.com/azure/key-vault/managed-hsm/quick-create-cli |
| 142 | +[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/ |
| 143 | +[default_cred_ref]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/azidentity#defaultazurecredential |
| 144 | +[managed_hsm]: https://docs.microsoft.com/azure/key-vault/managed-hsm/overview |
| 145 | +[managed_hsm_docs]: https://learn.microsoft.com/azure/key-vault/managed-hsm/ |
| 146 | +[managed_identity]: https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/overview |
| 147 | +[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ |
| 148 | + |
0 commit comments