You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Quickstart: Azure Key Vault certificate client library for Python
15
15
16
-
Get started with the Azure Key Vault certificate client library for Python. Follow the steps below to install the package and try out example code for basic tasks. By using Key Vault to store certificates, you avoid storing certificates in your code, which increases the security of your app.
16
+
Get started with the Azure Key Vault certificate client library for Python. Follow these steps to install the package and try out example code for basic tasks. By using Key Vault to store certificates, you avoid storing certificates in your code, which increases the security of your app.
This quickstart assumes you are running [Azure CLI](/cli/azure/install-azure-cli) in a Linux terminal window.
26
+
This quickstart assumes you're running [Azure CLI](/cli/azure/install-azure-cli) or [Azure PowerShell](/powershell/azure/install-az-ps) in a Linux terminal window.
27
27
28
28
## Set up your local environment
29
29
30
-
This quickstart is using Azure Identity library with Azure CLI to authenticate user to Azure Services. Developers can also use Visual Studio or Visual Studio Code to authenticate their calls, for more information, see [Authenticate the client with Azure Identity client library](/python/api/overview/azure/identity-readme)
30
+
This quickstart uses the Azure Identity library with Azure CLI or Azure PowerShell to authenticate the user to Azure services. Developers can also use Visual Studio or Visual Studio Code to authenticate their calls. For more information, see [Authenticate the client with Azure Identity client library](/python/api/overview/azure/identity-readme).
31
31
32
32
### Sign in to Azure
33
33
34
+
### [Azure CLI](#tab/azure-cli)
35
+
34
36
1. Run the `login` command.
35
37
36
38
```azurecli-interactive
@@ -44,6 +46,23 @@ This quickstart is using Azure Identity library with Azure CLI to authenticate u
44
46
45
47
2. Sign in with your account credentials in the browser.
46
48
49
+
### [Azure PowerShell](#tab/azure-powershell)
50
+
51
+
1. Run the `Connect-AzAccount` command.
52
+
53
+
```azurepowershell
54
+
Connect-AzAccount
55
+
```
56
+
57
+
If PowerShell can open your default browser, it will do so and load an Azure sign-in page.
58
+
59
+
Otherwise, open a browser page at [https://aka.ms/devicelogin](https://aka.ms/devicelogin) and enter the
60
+
authorization code displayed in your terminal.
61
+
62
+
2. Sign in with your account credentials in the browser.
63
+
64
+
---
65
+
47
66
### Install the packages
48
67
49
68
1. In a terminal or command prompt, create a suitable project folder, and then create and activate a Python virtual environment as described on [Use Python virtual environments](/azure/developer/python/configure-local-development-environment?tabs=cmd#use-python-virtual-environments)
@@ -73,10 +92,20 @@ This quickstart is using Azure Identity library with Azure CLI to authenticate u
73
92
74
93
Create an access policy for your key vault that grants certificate permission to your user account
75
94
95
+
### [Azure CLI](#tab/azure-cli)
96
+
76
97
```azurecli
77
98
az keyvault set-policy --name <your-unique-keyvault-name> --upn [email protected] --certificate-permissions delete get list create
The Azure Key Vault certificate client library for Python allows you to manage certificates. The following code sample demonstrates how to create a client, set a certificate, retrieve a certificate, and delete a certificate.
@@ -85,7 +114,7 @@ Create a file named *kv_certificates.py* that contains this code.
85
114
86
115
```python
87
116
import os
88
-
from azure.keyvault.certificates import CertificateClient, CertificatePolicy,CertificateContentType, WellKnownIssuerNames
117
+
from azure.keyvault.certificates import CertificateClient, CertificatePolicy
89
118
from azure.identity import DefaultAzureCredential
90
119
91
120
keyVaultName = os.environ["KEY_VAULT_NAME"]
@@ -125,16 +154,18 @@ Make sure the code in the previous section is in a file named *kv_certificates.p
125
154
python kv_certificates.py
126
155
```
127
156
128
-
- If you encounter permissions errors, make sure you ran the [`az keyvault set-policy` command](#grant-access-to-your-key-vault).
129
-
-Re-running the code with the same key name may produce the error, "(Conflict) Certificate \<name\> is currently in a deleted but recoverable state." Use a different key name.
157
+
- If you encounter permissions errors, make sure you ran the [`az keyvault set-policy`or `Set-AzKeyVaultAccessPolicy`command](#grant-access-to-your-key-vault).
158
+
-Rerunning the code with the same key name may produce the error, "(Conflict) Certificate \<name\> is currently in a deleted but recoverable state." Use a different key name.
130
159
131
160
## Code details
132
161
133
162
### Authenticate and create a client
134
163
135
-
In this quickstart, logged in user is used to authenticate to key vault, which is preferred method for local development. For applications deployed to Azure, managed identity should be assigned to App Service or Virtual Machine, for more information, see [Managed Identity Overview](../../active-directory/managed-identities-azure-resources/overview.md).
164
+
Application requests to most Azure services must be authorized. Using the [DefaultAzureCredential](/python/api/azure-identity/azure.identity.defaultazurecredential) class provided by the [Azure Identity client library](/python/api/overview/azure/identity-readme) is the recommended approach for implementing passwordless connections to Azure services in your code. `DefaultAzureCredential` supports multiple authentication methods and determines which method should be used at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code.
165
+
166
+
In this quickstart, `DefaultAzureCredential` authenticates to key vault using the credentials of the local development user logged into the Azure CLI. When the application is deployed to Azure, the same `DefaultAzureCredential` code can automatically discover and use a managed identity that is assigned to an App Service, Virtual Machine, or other services. For more information, see [Managed Identity Overview](/azure/active-directory/managed-identities-azure-resources/overview).
136
167
137
-
In below example, the name of your key vault is expanded to the key vault URI, in the format `https://\<your-key-vault-name\>.vault.azure.net`. This example is using ['DefaultAzureCredential()'](/python/api/azure-identity/azure.identity.defaultazurecredential) class, which allows to use the same code across different environments with different options to provide identity. For more information, see [Default Azure Credential Authentication](/python/api/overview/azure/identity-readme).
168
+
In the example code, the name of your key vault is expanded to the key vault URI, in the format `https://\<your-key-vault-name>.vault.azure.net`.
Once you've obtained the client object for the key vault, you can create a certificate using the [begin_create_certificate](/python/api/azure-keyvault-certificates/azure.keyvault.certificates.certificateclient?#begin-create-certificate-certificate-name--policy----kwargs-) method:
177
+
Once you've obtained the client object for the key vault, you can create a certificate using the [begin_create_certificate](/python/api/azure-keyvault-certificates/azure.keyvault.certificates.certificateclient#azure-keyvault-certificates-certificateclient-begin-create-certificate) method:
Here, the certificate requires a policy obtained with the [CertificatePolicy.get_default](/python/api/azure-keyvault-certificates/azure.keyvault.certificates.certificatepolicy?#get-default--) method.
185
+
Here, the certificate requires a policy obtained with the [CertificatePolicy.get_default](/python/api/azure-keyvault-certificates/azure.keyvault.certificates.certificatepolicy#azure-keyvault-certificates-certificatepolicy-get-default) method.
155
186
156
187
Calling a `begin_create_certificate` method generates an asynchronous call to the Azure REST API for the key vault. The asynchronous call returns a poller object. To wait for the result of the operation, call the poller's `result` method.
157
188
158
-
When handling the request, Azure authenticates the caller's identity (the service principal) using the credential object you provided to the client.
189
+
When Azure handles the request, it authenticates the caller's identity (the service principal) using the credential object you provided to the client.
159
190
160
191
161
192
### Retrieve a certificate
162
193
163
-
To read a certificate from Key Vault, use the [get_certificate](/python/api/azure-keyvault-certificates/azure.keyvault.certificates.certificateclient?#get-certificate-certificate-name----kwargs-) method:
194
+
To read a certificate from Key Vault, use the [get_certificate](/python/api/azure-keyvault-certificates/azure.keyvault.certificates.certificateclient#azure-keyvault-certificates-certificateclient-get-certificate) method:
You can also verify that the certificate has been set with the Azure CLI command [az keyvault certificate show](/cli/azure/keyvault/certificate?#az-keyvault-certificate-show).
200
+
You can also verify that the certificate has been set with the Azure CLI command [az keyvault certificate show](/cli/azure/keyvault/certificate?#az-keyvault-certificate-show) or the Azure PowerShell cmdlet [Get-AzKeyVaultCertificate](/powershell/module/az.keyvault/get-azkeyvaultcertificate)
170
201
171
202
### Delete a certificate
172
203
173
-
To delete a certificate, use the [begin_delete_certificate](/python/api/azure-keyvault-certificates/azure.keyvault.certificates.certificateclient?#begin-delete-certificate-certificate-name----kwargs-) method:
204
+
To delete a certificate, use the [begin_delete_certificate](/python/api/azure-keyvault-certificates/azure.keyvault.certificates.certificateclient#azure-keyvault-certificates-certificateclient-begin-delete-certificate) method:
The `begin_delete_certificate` method is asynchronous and returns a poller object. Calling the poller's `result` method waits for its completion.
181
212
182
-
You can verify that the certificate is deleted with the Azure CLI command [az keyvault certificate show](/cli/azure/keyvault/certificate?#az-keyvault-certificate-show).
213
+
You can verify that the certificate is deleted with the Azure CLI command [az keyvault certificate show](/cli/azure/keyvault/certificate#az-keyvault-certificate-show) or the Azure PowerShell cmdlet [Get-AzKeyVaultCertificate](/powershell/module/az.keyvault/get-azkeyvaultcertificate).
183
214
184
215
Once deleted, a certificate remains in a deleted but recoverable state for a time. If you run the code again, use a different certificate name.
185
216
@@ -189,10 +220,20 @@ If you want to also experiment with [secrets](../secrets/quick-create-python.md)
189
220
190
221
Otherwise, when you're finished with the resources created in this article, use the following command to delete the resource group and all its contained resources:
191
222
223
+
### [Azure CLI](#tab/azure-cli)
224
+
192
225
```azurecli
193
226
az group delete --resource-group myResourceGroup
194
227
```
195
228
229
+
### [Azure PowerShell](#tab/azure-powershell)
230
+
231
+
```azurepowershell
232
+
Remove-AzResourceGroup -Name myResourceGroup
233
+
```
234
+
235
+
---
236
+
196
237
## Next steps
197
238
198
239
-[Overview of Azure Key Vault](../general/overview.md)
0 commit comments