|
3 | 3 | # Copyright (c) Microsoft Corporation. |
4 | 4 | # Licensed under the MIT License. |
5 | 5 | # ------------------------------------ |
| 6 | +import os |
| 7 | +import base64 |
| 8 | +import asyncio |
| 9 | + |
| 10 | +from azure.identity.aio import DefaultAzureCredential |
| 11 | +from azure.keyvault.administration import KeyVaultEkmConnection |
| 12 | +from azure.keyvault.administration.aio import KeyVaultEkmClient |
6 | 13 |
|
7 | 14 | # ---------------------------------------------------------------------------------------------------------- |
8 | 15 | # Prerequisites: |
|
37 | 44 |
|
38 | 45 | # Instantiate an EKM client that will be used to call the service. |
39 | 46 | # Here we use the DefaultAzureCredential, but any azure-identity credential can be used. |
40 | | -# [START create_a_ekm_client] |
41 | | -import os |
42 | | -import base64 |
43 | | -import asyncio |
44 | | -from azure.identity import DefaultAzureCredential |
45 | | -from azure.keyvault.administration import KeyVaultEkmConnection |
46 | | -from azure.keyvault.administration.aio import KeyVaultEkmClient |
47 | 47 |
|
48 | 48 |
|
49 | 49 | async def run_sample(): |
50 | 50 | MANAGED_HSM_URL = os.environ["MANAGED_HSM_URL"] |
51 | | - EKM_PROXY_HOST = os.environ["EKM_PROXY_HOST"] |
52 | | - CA_CERTIFICATE = os.environ["CA_CERTIFICATE"] |
53 | | - CA_CERTIFICATES = [CA_CERTIFICATE] |
54 | 51 | credential = DefaultAzureCredential() |
55 | 52 | client = KeyVaultEkmClient(vault_url=MANAGED_HSM_URL, credential=credential) |
56 | | - # [END create_a_ekm_client] |
57 | 53 |
|
58 | 54 | # First, let's create an EKM connection |
59 | 55 | print("\n.. Create EKM connection") |
60 | | - # [START create_ekm_connection] |
| 56 | + EKM_PROXY_HOST = os.environ["EKM_PROXY_HOST"] |
| 57 | + CA_CERTIFICATE = os.environ["CA_CERTIFICATE"] |
| 58 | + CA_CERTIFICATES = [CA_CERTIFICATE] |
61 | 59 | ekm_connection = KeyVaultEkmConnection( |
62 | 60 | host=EKM_PROXY_HOST, |
63 | 61 | server_ca_certificates=[base64.b64decode(cert) for cert in CA_CERTIFICATES], |
64 | 62 | path_prefix="/api/v1", |
65 | 63 | ) |
66 | | - created_ekm_connection = await client.create_ekm_connection( |
67 | | - connection=ekm_connection |
68 | | - ) |
| 64 | + created_ekm_connection = await client.create_ekm_connection(connection=ekm_connection) |
69 | 65 | print(f"EKM connection created with host: {created_ekm_connection.host}") |
70 | | - # [END create_ekm_connection] |
71 | 66 |
|
72 | 67 | # Let's get the EKM connection we just created |
73 | 68 | print("\n.. Get EKM connection") |
74 | | - # [START get_ekm_connection] |
75 | 69 | retrieved_ekm_connection = await client.get_ekm_connection() |
76 | 70 | print("Retrieved EKM connection with:") |
77 | 71 | print(f"\tHost: {retrieved_ekm_connection.host}") |
78 | 72 | print(f"\tPath prefix: {retrieved_ekm_connection.path_prefix}") |
79 | | - print( |
80 | | - f"\tServer subject common name: {retrieved_ekm_connection.server_subject_common_name}" |
81 | | - ) |
82 | | - # [END get_ekm_connection] |
| 73 | + print(f"\tServer subject common name: {retrieved_ekm_connection.server_subject_common_name}") |
83 | 74 |
|
84 | 75 | # Get the EKM certificate |
85 | 76 | print("\n.. Get EKM certificate") |
86 | | - # [START get_ekm_certificate] |
87 | 77 | ekm_certificate = await client.get_ekm_certificate() |
88 | | - print( |
89 | | - f"EKM certificate retrieved with subject: {ekm_certificate.subject_common_name}" |
90 | | - ) |
91 | | - # [END get_ekm_certificate] |
| 78 | + print(f"EKM certificate retrieved with subject: {ekm_certificate.subject_common_name}") |
92 | 79 |
|
93 | 80 | # Check the EKM connection status |
94 | 81 | print("\n.. Check EKM connection") |
95 | | - # [START check_ekm_connection] |
96 | 82 | connection_status = await client.check_ekm_connection() |
97 | 83 | print("EKM connection status:") |
98 | 84 | print(f"\tAPI Version: {connection_status.api_version}") |
99 | 85 | print(f"\tProxy Vendor: {connection_status.proxy_vendor}") |
100 | 86 | print(f"\tProxy Name: {connection_status.proxy_name}") |
101 | 87 | print(f"\tEKM Vendor: {connection_status.ekm_vendor}") |
102 | 88 | print(f"\tEKM Product: {connection_status.ekm_product}") |
103 | | - # [END check_ekm_connection] |
104 | 89 |
|
105 | 90 | # Update the EKM connection |
106 | 91 | print("\n.. Update EKM connection") |
107 | | - # [START update_ekm_connection] |
108 | 92 | updated_ekm_connection = KeyVaultEkmConnection( |
109 | 93 | host="ekm-proxy-updated.contoso.com", |
110 | 94 | server_ca_certificates=[base64.b64decode(cert) for cert in CA_CERTIFICATES], |
111 | 95 | path_prefix="/api/v2", |
112 | 96 | ) |
113 | 97 | result = await client.update_ekm_connection(connection=updated_ekm_connection) |
114 | 98 | print(f"EKM connection updated with host: {result.host}") |
115 | | - # [END update_ekm_connection] |
116 | 99 |
|
117 | 100 | # Finally, let's delete the EKM connection |
118 | 101 | print("\n.. Delete EKM connection") |
119 | | - # [START delete_ekm_connection] |
120 | 102 | deleted_ekm_connection = await client.delete_ekm_connection() |
121 | 103 | print("EKM connection deleted successfully") |
122 | | - # [END delete_ekm_connection] |
123 | 104 |
|
124 | 105 | await client.close() |
125 | 106 | await credential.close() |
|
0 commit comments