-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
issuers_async.py
96 lines (79 loc) · 3.68 KB
/
issuers_async.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import os
import asyncio
from azure.identity.aio import DefaultAzureCredential
from azure.keyvault.certificates.aio import CertificateClient
from azure.keyvault.certificates import AdministratorContact
# ----------------------------------------------------------------------------------------------------------
# Prerequisites:
# 1. An Azure Key Vault (https://docs.microsoft.com/azure/key-vault/quick-create-cli)
#
# 2. azure-keyvault-certificates and azure-identity packages (pip install these)
#
# 3. Set up your environment to use azure-identity's DefaultAzureCredential. For more information about how to configure
# the DefaultAzureCredential, refer to https://aka.ms/azsdk/python/identity/docs#azure.identity.DefaultAzureCredential
#
# ----------------------------------------------------------------------------------------------------------
# Sample - demonstrates basic CRUD operations for certificate issuers.
#
# 1. Create an issuer (create_issuer)
#
# 2. Get an issuer (get_issuer)
#
# 3. List issuers for the key vault (list_properties_of_issuers)
#
# 4. Update an issuer (update_issuer)
#
# 5. Delete an issuer (delete_issuer)
# ----------------------------------------------------------------------------------------------------------
async def run_sample():
# Instantiate a certificate client that will be used to call the service.
# Here we use the DefaultAzureCredential, but any azure-identity credential can be used.
VAULT_URL = os.environ["VAULT_URL"]
credential = DefaultAzureCredential()
client = CertificateClient(vault_url=VAULT_URL, credential=credential)
# First we specify the AdministratorContact for our issuers.
admin_contacts = [
AdministratorContact(first_name="John", last_name="Doe", email="admin@microsoft.com", phone="4255555555")
]
# Next we create an issuer with these administrator details
# The name field refers to the name you would like to get the issuer. There are also pre-set names, such as 'Self' and 'Unknown'
await client.create_issuer(
issuer_name="issuer1", provider="Test", account_id="keyvaultuser", admin_contacts=admin_contacts, enabled=True
)
# Now we get this issuer by name
issuer1 = await client.get_issuer("issuer1")
print(issuer1.name)
print(issuer1.provider)
print(issuer1.account_id)
for contact in issuer1.admin_contacts:
print(contact.first_name)
print(contact.last_name)
print(contact.email)
print(contact.phone)
# Now we update the administrator contact for this issuer
admin_contacts = [
AdministratorContact(first_name="Jane", last_name="Doe", email="admin@microsoft.com", phone="4255555555")
]
issuer1 = await client.update_issuer(issuer_name="issuer1", admin_contacts=admin_contacts)
for contact in issuer1.admin_contacts:
print(contact.first_name)
print(contact.last_name)
print(contact.email)
print(contact.phone)
# Now we will list all of the certificate issuers for this key vault. To better demonstrate this, we will first create another issuer.
await client.create_issuer(issuer_name="issuer2", provider="Test", account_id="keyvaultuser", enabled=True)
issuers = client.list_properties_of_issuers()
async for issuer in issuers:
print(issuer.name)
print(issuer.provider)
# Finally, we delete our first issuer by name.
await client.delete_issuer("issuer1")
print("\nrun_sample done")
await credential.close()
await client.close()
if __name__ == "__main__":
asyncio.run(run_sample())