Skip to content

Commit ff734d4

Browse files
Merge pull request #225921 from vmagelo/passwordless-work
Updates for passwordless.
2 parents 1bd39bb + 817af7f commit ff734d4

File tree

4 files changed

+135
-52
lines changed

4 files changed

+135
-52
lines changed

articles/key-vault/certificates/quick-create-python.md

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,36 @@ title: Quickstart – Azure Key Vault Python client library – manage certifica
33
description: Learn how to create, retrieve, and delete certificates from an Azure key vault using the Python client library
44
author: msmbaldwin
55
ms.author: mbaldwin
6-
ms.date: 01/22/2022
6+
ms.date: 02/03/2023
77
ms.service: key-vault
88
ms.subservice: certificates
99
ms.topic: quickstart
1010
ms.devlang: python
11-
ms.custom: devx-track-python, devx-track-azurecli, mode-api
11+
ms.custom: devx-track-python, devx-track-azurecli, mode-api, passwordless-python
1212
---
1313

1414
# Quickstart: Azure Key Vault certificate client library for Python
1515

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.
1717

1818
[API reference documentation](/python/api/overview/azure/keyvault-certificates-readme) | [Library source code](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/keyvault/azure-keyvault-certificates) | [Package (Python Package Index)](https://pypi.org/project/azure-keyvault-certificates)
1919

2020
## Prerequisites
2121

2222
- An Azure subscription - [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
23-
- [Python 2.7+ or 3.6+](/azure/developer/python/configure-local-development-environment)
23+
- [Python 3.7+](/azure/developer/python/configure-local-development-environment)
2424
- [Azure CLI](/cli/azure/install-azure-cli)
2525

26-
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.
2727

2828
## Set up your local environment
2929

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).
3131

3232
### Sign in to Azure
3333

34+
### [Azure CLI](#tab/azure-cli)
35+
3436
1. Run the `login` command.
3537

3638
```azurecli-interactive
@@ -44,6 +46,23 @@ This quickstart is using Azure Identity library with Azure CLI to authenticate u
4446
4547
2. Sign in with your account credentials in the browser.
4648
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+
4766
### Install the packages
4867
4968
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
7392
7493
Create an access policy for your key vault that grants certificate permission to your user account
7594
95+
### [Azure CLI](#tab/azure-cli)
96+
7697
```azurecli
7798
az keyvault set-policy --name <your-unique-keyvault-name> --upn [email protected] --certificate-permissions delete get list create
7899
```
79100

101+
### [Azure PowerShell](#tab/azure-powershell)
102+
103+
```azurepowershell
104+
Set-AzKeyVaultAccessPolicy -VaultName "<your-unique-keyvault-name>" -UserPrincipalName "[email protected]" -PermissionsToCertificates delete,get,list,create
105+
```
106+
107+
---
108+
80109
## Create the sample code
81110

82111
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.
85114

86115
```python
87116
import os
88-
from azure.keyvault.certificates import CertificateClient, CertificatePolicy,CertificateContentType, WellKnownIssuerNames
117+
from azure.keyvault.certificates import CertificateClient, CertificatePolicy
89118
from azure.identity import DefaultAzureCredential
90119

91120
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
125154
python kv_certificates.py
126155
```
127156

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.
130159

131160
## Code details
132161

133162
### Authenticate and create a client
134163

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).
136167

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`.
138169

139170
```python
140171
credential = DefaultAzureCredential()
@@ -143,34 +174,34 @@ client = CertificateClient(vault_url=KVUri, credential=credential)
143174

144175
### Save a certificate
145176

146-
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:
147178

148179
```python
149180
policy = CertificatePolicy.get_default()
150181
poller = client.begin_create_certificate(certificate_name=certificateName, policy=policy)
151182
certificate = poller.result()
152183
```
153184

154-
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.
155186

156187
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.
157188

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.
159190

160191

161192
### Retrieve a certificate
162193

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:
164195

165196
```python
166197
retrieved_certificate = client.get_certificate(certificateName)
167198
```
168199

169-
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)
170201

171202
### Delete a certificate
172203

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:
174205

175206
```python
176207
poller = client.begin_delete_certificate(certificateName)
@@ -179,7 +210,7 @@ deleted_certificate = poller.result()
179210

180211
The `begin_delete_certificate` method is asynchronous and returns a poller object. Calling the poller's `result` method waits for its completion.
181212

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).
183214

184215
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.
185216

@@ -189,10 +220,20 @@ If you want to also experiment with [secrets](../secrets/quick-create-python.md)
189220

190221
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:
191222

223+
### [Azure CLI](#tab/azure-cli)
224+
192225
```azurecli
193226
az group delete --resource-group myResourceGroup
194227
```
195228

229+
### [Azure PowerShell](#tab/azure-powershell)
230+
231+
```azurepowershell
232+
Remove-AzResourceGroup -Name myResourceGroup
233+
```
234+
235+
---
236+
196237
## Next steps
197238

198239
- [Overview of Azure Key Vault](../general/overview.md)

0 commit comments

Comments
 (0)