Skip to content

Commit 8552d80

Browse files
authored
[keyvault] azadmin module (Azure#20296)
1 parent 8aa9682 commit 8552d80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+5102
-0
lines changed

eng/config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
{
8585
"Name": "resourcemanager",
8686
"CoverageGoal": 0.0
87+
},
88+
{
89+
"Name": "security/keyvault/azadmin",
90+
"CoverageGoal": 0.80
8791
}
8892
]
8993
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## 0.1.0 (2023-03-13)
2+
* This is the initial release of the `azadmin` library
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Microsoft Corporation. All rights reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"AssetsRepo": "Azure/azure-sdk-assets",
3+
"AssetsRepoPrefixPath": "go",
4+
"TagPrefix": "go/security/keyvault/azadmin",
5+
"Tag": "go/security/keyvault/azadmin_6b57e62028"
6+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
## Go
2+
3+
```yaml
4+
clear-output-folder: false
5+
export-clients: true
6+
go: true
7+
input-file:
8+
- https://github.com/Azure/azure-rest-api-specs/blob/551275acb80e1f8b39036b79dfc35a8f63b601a7/specification/keyvault/data-plane/Microsoft.KeyVault/stable/7.4/backuprestore.json
9+
license-header: MICROSOFT_MIT_NO_VERSION
10+
openapi-type: "data-plane"
11+
output-folder: ../backup
12+
override-client-name: Client
13+
security: "AADToken"
14+
security-scopes: "https://vault.azure.net/.default"
15+
use: "@autorest/[email protected]"
16+
version: "^3.0.0"
17+
18+
directive:
19+
20+
# make vault URL a parameter of the client constructor
21+
- from: swagger-document
22+
where: $["x-ms-parameterized-host"]
23+
transform: $.parameters[0]["x-ms-parameter-location"] = "client"
24+
25+
# rename restore operation from BeginFullRestoreOperation to BeginFullRestore
26+
- rename-operation:
27+
from: FullRestoreOperation
28+
to: FullRestore
29+
- rename-operation:
30+
from: SelectiveKeyRestoreOperation
31+
to: SelectiveKeyRestore
32+
33+
# make SASToken parameter required
34+
- from: swagger-document
35+
where: $.paths["/backup"].post.parameters[0]
36+
transform: $["required"] = true
37+
38+
# delete backup and restore status methods
39+
- from: swagger-document
40+
where: $["paths"]
41+
transform: >
42+
delete $["/backup/{jobId}/pending"];
43+
- from: swagger-document
44+
where: $["paths"]
45+
transform: >
46+
delete $["/restore/{jobId}/pending"];
47+
48+
# delete generated client constructor
49+
- from: client.go
50+
where: $
51+
transform: return $.replace(/(?:\/\/.*\s)+func NewClient.+\{\s(?:.+\s)+\}\s/, "");
52+
53+
# delete unused error models
54+
- from: models.go
55+
where: $
56+
transform: return $.replace(/(?:\/\/.*\s)+type (?:Error|KeyVaultError).+\{(?:\s.+\s)+\}\s/g, "");
57+
- from: models_serde.go
58+
where: $
59+
transform: return $.replace(/(?:\/\/.*\s)+func \(\w \*?(?:Error|KeyVaultError)\).*\{\s(?:.+\s)+\}\s/g, "");
60+
- from: models.go
61+
where: $
62+
transform: return $.replace(/Error \*Error/g, "Error *ServerError");
63+
64+
# modify Restore to use implementation with custom poller handler
65+
- from: client.go
66+
where: $
67+
transform: return $.replace(/\[ClientFullRestoreResponse\], error\) \{\s(?:.+\s)+\}/, "[ClientFullRestoreResponse], error) {return client.beginFullRestore(ctx, restoreBlobDetails, options)}");
68+
- from: client.go
69+
where: $
70+
transform: return $.replace(/\[ClientSelectiveKeyRestoreResponse\], error\) \{\s(?:.+\s)+\}/, "[ClientSelectiveKeyRestoreResponse], error) {return client.beginSelectiveKeyRestore(ctx, keyName, restoreBlobDetails, options)}");
71+
72+
# delete client name prefix from method options and response types
73+
- from:
74+
- client.go
75+
- models.go
76+
- response_types.go
77+
where: $
78+
transform: return $.replace(/Client(\w+)((?:Options|Response))/g, "$1$2");
79+
80+
# add doc comments for models with missing descriptions
81+
- from: swagger-document
82+
where: $.definitions.SASTokenParameter
83+
transform: $["description"] = "Contains the information required to access blob storage."
84+
- from: swagger-document
85+
where: $.definitions.RestoreOperationParameters
86+
transform: $["description"] = "Parameters for the restore operation"
87+
- from: swagger-document
88+
where: $.definitions.SelectiveKeyRestoreOperationParameters
89+
transform: $["description"] = "Parameters for the selective restore operation"
90+
```

0 commit comments

Comments
 (0)