Skip to content

Commit 8f55308

Browse files
committed
add backup and restore snippets
1 parent fcd20f2 commit 8f55308

File tree

1 file changed

+106
-7
lines changed
  • sdk/keyvault/azure-keyvault-administration

1 file changed

+106
-7
lines changed

sdk/keyvault/azure-keyvault-administration/README.md

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ Once the **AZURE_CLIENT_ID**, **AZURE_CLIENT_SECRET** and
9090
[DefaultAzureCredential][default_cred_ref] will be able to authenticate the
9191
[KeyVaultAccessControlClient][rbac_client_docs].
9292
93-
Constructing the client also requires your vault's URL, which you can
93+
There are two clients available in this package, below are snippets demonstrating how to construct
94+
each one of these clients. Constructing a client also requires your vault's URL, which you can
9495
get from the Azure CLI or the Azure Portal. In the Azure Portal, this URL is
9596
the vault's "DNS Name".
9697
98+
##### Create a KeyVaultAccessControlClient
9799
```python
98100
from azure.identity import DefaultAzureCredential
99101
from azure.keyvault.administration import KeyVaultAccessControlClient
@@ -102,6 +104,17 @@ credential = DefaultAzureCredential()
102104
103105
client = KeyVaultAccessControlClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
104106
```
107+
108+
##### Create a KeyVaultBackupClient
109+
```python
110+
from azure.identity import DefaultAzureCredential
111+
from azure.keyvault.administration import KeyVaultBackupClient
112+
113+
credential = DefaultAzureCredential()
114+
115+
client = KeyVaultBackupClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
116+
```
117+
105118
## Key concepts
106119
107120
### Role Definition
@@ -115,12 +128,20 @@ A role assignment is the association of a role definition to a service principal
115128
### KeyVaultAccessControlClient
116129
A `KeyVaultAccessControlClient` manages role definitions and role assignments.
117130
131+
### KeyVaultBackupClient
132+
A `KeyVaultBackupClient` performs full key backups, full key restores, and selective key restores.
133+
118134
## Examples
119135
This section conntains code snippets covering common tasks:
120-
* [List the role definitions](#list-the-role-definitions "List the role definitions")
121-
* [Create, Get, and Delete a role assignment](#create-get-and-delete-a-role-assignment "Create, Get, and Delete a role assignment")
122-
123-
### List the role definitions
136+
* Access Control
137+
* [List all role definitions](#list-all-role-definitions "List all role definitions")
138+
* [List all role assignments](#list-all-role-assignments "List all role assignments")
139+
* [Create, Get, and Delete a role assignment](#create-get-and-delete-a-role-assignment "Create, Get, and Delete a role assignment")
140+
* Backup and Restore
141+
* [Perform a full key backup](#perform-a-full-key-backup "Perform a full key backup")
142+
* [Perform a full key restore](#perform-a-full-key-restore "Perform a full key restore")
143+
144+
### List all role definitions
124145
List the role definitions available for assignment.
125146
126147
```python
@@ -132,16 +153,36 @@ credential = DefaultAzureCredential()
132153
client = KeyVaultAccessControlClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
133154
134155
# this is the global scope. This will list all role definitions available for assignment
135-
role_definitions = client.list_role_definitions(role_scope="/")
156+
role_definitions = client.list_role_definitions(role_scope=KeyVaultRoleScope.global_value)
136157
137158
for role_definition in role_definitions:
138159
print(role_definition.id)
139160
print(role_definition.role_name)
140161
print(role_definition.description)
141162
```
142163
164+
### List all role assignments
165+
Before creating a new role assignment in the [next snippet](#create-get-and-delete-a-role-assignment), list all of the current role assignments
166+
167+
```python
168+
from azure.identity import DefaultAzureCredential
169+
from azure.keyvault.administration import KeyVaultAccessControlClient
170+
171+
credential = DefaultAzureCredential()
172+
173+
client = KeyVaultAccessControlClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
174+
175+
# this is the global scope. This will list all role assignments available for assignment
176+
role_assignments = client.list_role_assignments(role_scope=KeyVaultRoleScope.global_value)
177+
178+
for role_assignment in role_assignments:
179+
print(role_assignment.name)
180+
print(role_assignment.principal_id)
181+
print(role_assignment.role_definition_id)
182+
```
183+
143184
### Create, Get, and Delete a role assignment
144-
Assign a role to a service principal. This will require a role definition id from the list retrieved in the [above snippet](#list-the-role-definitions) and the principal object id retrieved in the [Create and Get credentials](#create-and-get-credentials)
185+
Assign a role to a service principal. This will require a role definition id from the list retrieved in the [above snippet](#list-all-role-definitions) and the principal object id retrieved in the [Create and Get credentials](#create-and-get-credentials)
145186
146187
```python
147188
import uuid
@@ -174,10 +215,68 @@ role_assignment = client.delete_role_assignment(role_scope, role_assignment.name
174215
print(role_assignment.name)
175216
print(role_assignment.principal_id)
176217
print(role_assignment.role_definition_id)
218+
```
219+
220+
### Perform a full key backup
221+
Back up your entire collection of keys. The backing store for full key backups is a blob storage container using Shared Access Signature authentication.
177222
223+
For more details on creating a SAS token using the `BlobServiceClient`, see the sample [here](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/storage/azure-storage-blob/samples/blob_samples_authentication.py#L105).
224+
Alternatively, it is possible to [generate a SAS token in Storage Explorer](https://docs.microsoft.com/en-us/azure/vs-azure-tools-storage-manage-with-storage-explorer?tabs=windows#generate-a-shared-access-signature-in-storage-explorer)
225+
226+
```python
227+
from azure.identity import DefaultAzureCredential
228+
from azure.keyvault.administration import KeyVaultBackupClient
229+
from azure.core.exceptions import ResourceNotFoundError
230+
231+
credential = DefaultAzureCredential()
232+
client = KeyVaultBackupClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
233+
234+
blob_storage_uri = "<your-blob-storage-uri>" # the URI to your storage account. Should contain the name of the specific container
235+
sas_token = "<your-sas-token>" # replace with the sas token to your storage account. See this snippet's description on help to retrieve
236+
237+
# performing a full key backup is a long-running operation. Calling `result()` on the poller will wait
238+
# until the backup is completed, then return an object representing the backup operation.
239+
backup_operation = client.begin_full_backup(blob_storage_uri, sas_token).result()
240+
241+
# this is the URI of the Azure blob storage container which contains the backup
242+
azure_storage_blob_container_uri = backup_operation.azure_storage_blob_container_uri
243+
244+
print(backup_operation.status)
245+
print(backup_operation.job_id)
246+
print(azure_storage_blob_container_uri)
178247
```
179248
180249
250+
### Perform a full key restore
251+
Restore your entire collection of keys from a backup. The data source for a full key restore is a storage blob accessed using Shared Access Signature authentication.
252+
You will also need the `azure_storage_blob_container_uri` from the [above snippet](#perform-a-full-key-backup).
253+
254+
For more details on creating a SAS token using the `BlobServiceClient`, see the sample [here](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/storage/azure-storage-blob/samples/blob_samples_authentication.py#L105).
255+
Alternatively, it is possible to [generate a SAS token in Storage Explorer](https://docs.microsoft.com/en-us/azure/vs-azure-tools-storage-manage-with-storage-explorer?tabs=windows#generate-a-shared-access-signature-in-storage-explorer)
256+
257+
```python
258+
from azure.identity import DefaultAzureCredential
259+
from azure.keyvault.administration import KeyVaultBackupClient
260+
from azure.core.exceptions import ResourceNotFoundError
261+
262+
credential = DefaultAzureCredential()
263+
client = KeyVaultBackupClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
264+
265+
blob_storage_uri = "<your-blob-storage-uri>" # the URI to your storage account. Should contain the name of the specific container
266+
sas_token = "<your-sas-token>" # replace with the sas token to your storage account. See this snippet's description on help to retrieve
267+
268+
# Replace <azure-storage-blob-container-uri> with the blob storage container returned in the previous example
269+
azure_storage_blob_container_uri = "<azure-storage-blob-container-uri>"
270+
folder_name = azure_storage_blob_container_uri.split("/")[-1]
271+
272+
# performing a full key restore is a long-running operation. Calling `result()` on the poller will wait
273+
# until the restore is completed, then return an object representing the restore operation.
274+
restore_operation = client.begin_full_restore(blob_storage_uri, sas_token, folder_name).result()
275+
276+
print(restore_operation.status)
277+
print(restore_operation.job_id)
278+
```
279+
181280
## Troubleshooting
182281
### General
183282
Key Vault clients raise exceptions defined in [azure-core][azure_core_exceptions].

0 commit comments

Comments
 (0)