This repository was archived by the owner on Dec 15, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
360 lines (308 loc) · 10.7 KB
/
Copy pathexample.py
File metadata and controls
360 lines (308 loc) · 10.7 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""Deploy Certificates to VMs from customer-managed Key Vault in Python.
"""
import logging
import os
import time
from haikunator import Haikunator
from six.moves import input
from azure.identity import ClientSecretCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.keyvault import KeyVaultManagementClient
from azure.keyvault.certificates import LifetimeAction, CertificateClient, CertificatePolicy, CertificatePolicyAction
from azure.keyvault.secrets import SecretClient
HAIKUNATOR = Haikunator()
# Activate this if you want to see detailed log
# logging.basicConfig(level=logging.DEBUG)
# Resource
LOCATION = 'westus2'
GROUP_NAME = 'azure-kv-vm-certificate-sample-group'
# KeyVault
# Random name to avoid collision executing this sample
KV_NAME = HAIKUNATOR.haikunate()
# This default Certificate creation policy. This is the same policy that:
# - Is pre-configured in the Portal when you choose "Generate" in the Certificates tab
# - You get when you use the CLI 2.0: az keyvault certificate get-default-policy
DEFAULT_POLICY = CertificatePolicy(
'Self',
exportable=True,
key_type='RSA',
key_size=2048,
reuse_key=True,
content_type='application/x-pkcs12',
subject='CN=CLIGetDefaultPolicy',
validity_in_months=12,
key_usage=[
"cRLSign",
"dataEncipherment",
"digitalSignature",
"keyEncipherment",
"keyAgreement",
"keyCertSign"
],
lifetime_actions=[
LifetimeAction(action=CertificatePolicyAction.auto_renew, days_before_expiry=90)
]
)
# Network
VNET_NAME = 'azure-sample-vnet'
SUBNET_NAME = 'azure-sample-subnet'
PUBLIC_IP_NAME = 'azure-sample-pip'
NIC_NAME = 'azure-sample-nic'
IP_CONFIG_NAME = 'azure-sample-ip-config'
# VM
VM_NAME = 'azuretestmsi'
ADMIN_LOGIN = 'Foo12'
ADMIN_PASSWORD = 'BaR@123' + GROUP_NAME
# Create a Linux VM with Key Vault certificates installed at creation.
#
# This script expects that the following environment vars are set:
#
# AZURE_TENANT_ID: with your Azure Active Directory tenant id or domain
# AZURE_CLIENT_ID: with your Azure Active Directory Application Client ID
# AZURE_CLIENT_OBJECT_ID: with your Azure Active Directory Application Object ID
# AZURE_CLIENT_SECRET: with your Azure Active Directory Application Secret
# AZURE_SUBSCRIPTION_ID: with your Azure Subscription Id
#
def run_example():
"""Resource Group management example."""
#
# Create the Resource Manager Client with an Application (service principal) token provider
#
subscription_id = os.environ.get(
'AZURE_SUBSCRIPTION_ID',
'11111111-1111-1111-1111-111111111111') # your Azure Subscription Id
credential = ClientSecretCredential(
client_id=os.environ['AZURE_CLIENT_ID'],
client_secret=os.environ['AZURE_CLIENT_SECRET'],
tenant_id=os.environ['AZURE_TENANT_ID']
)
resource_client = ResourceManagementClient(credential, subscription_id)
compute_client = ComputeManagementClient(credential, subscription_id)
network_client = NetworkManagementClient(credential, subscription_id)
kv_mgmt_client = KeyVaultManagementClient(credential, subscription_id)
cert_client = CertificateClient(
"https://{}.vault.azure.net".format(KV_NAME),
credential
)
# Create Resource group
print('\nCreate Resource Group')
resource_group = resource_client.resource_groups.create_or_update(
GROUP_NAME,
{'location': LOCATION}
)
print_item(resource_group)
# Resolve the client_id as object_id for KeyVault access policy.
# If you already know your object_id, you can skip this part
sp_object_id = os.environ.get(
'AZURE_CLIENT_OBJECT_ID',
'11111111-1111-1111-1111-111111111111') # your service principal's object id
# Create Key Vault account
print('\nCreate Key Vault account')
async_vault_poller = kv_mgmt_client.vaults.begin_create_or_update(
GROUP_NAME,
KV_NAME,
{
'location': LOCATION,
'properties': {
'sku': {
'name': 'standard'
},
'tenant_id': os.environ['AZURE_TENANT_ID'],
'access_policies': [{
'tenant_id': os.environ['AZURE_TENANT_ID'],
'object_id': sp_object_id,
'permissions': {
# Only "certificates" and "secrets" are needed for this sample
'certificates': ['all'],
'secrets': ['all']
}
}],
# Critical to allow the VM to download certificates later
'enabled_for_deployment': True
}
}
)
vault = async_vault_poller.result()
print_item(vault)
# # KeyVault recommentation is to wait 20 seconds after account creation for DNS update
time.sleep(20)
# Ask KeyVault to create a Certificate
certificate_name = "cert1"
print('\nCreate Key Vault Certificate')
certificate_poller = cert_client.begin_create_certificate(
certificate_name,
policy=DEFAULT_POLICY
)
certificate_poller.wait()
while True:
check = cert_client.get_certificate_operation(certificate_name)
if check.status != 'inProgress':
break
try:
print("Waiting for certificate creation to finish")
time.sleep(10)
except KeyboardInterrupt:
print("Certificate creation wait cancelled.")
raise
print_item(check)
print('\nGet Key Vault created certificate as a secret')
secret_client = SecretClient(
"https://{}.vault.azure.net".format(KV_NAME),
credential
)
certificate_as_secret = secret_client.get_secret(
certificate_name,
"" # Latest version
)
print_item(certificate_as_secret)
print("\nCreate Network")
# Create Network components of the VM
# This is not related to the main topic of this sample and is just required to create the VM
subnet = create_virtual_network(network_client)
public_ip = create_public_ip(network_client)
nic = create_network_interface(network_client, subnet, public_ip)
print_item(nic)
# Create a VM with some Key Vault certificates
params_create = {
'location': LOCATION,
'hardware_profile': get_hardware_profile(),
'network_profile': get_network_profile(nic.id),
'storage_profile': get_storage_profile(),
'os_profile': {
'admin_username': ADMIN_LOGIN,
'admin_password': ADMIN_PASSWORD,
'computer_name': 'testkvcertificates',
# This is the Key Vault critical part
'secrets': [{
'source_vault': {
'id': vault.id,
},
'vault_certificates': [{
'certificate_url': certificate_as_secret.id
}]
}]
}
}
print("\nCreate VM")
vm_poller = compute_client.virtual_machines.begin_create_or_update(
GROUP_NAME,
VM_NAME,
params_create,
)
vm_result = vm_poller.result()
print_item(vm_result)
# Get the PublicIP after VM creation, since assignment is dynamic
public_ip = network_client.public_ip_addresses.get(
GROUP_NAME,
PUBLIC_IP_NAME
)
print("You can connect to the VM using:")
print("ssh {}@{}".format(
ADMIN_LOGIN,
public_ip.ip_address,
))
print("And password: {}\n".format(ADMIN_PASSWORD))
print("Your certificate is available in this folder: /var/lib/waagent")
print("You must be root to see it (sudo su)\n")
input("Press enter to delete this Resource Group.")
# Delete Resource group and everything in it
print('Delete Resource Group')
delete_async_operation = resource_client.resource_groups.begin_delete(GROUP_NAME)
delete_async_operation.wait()
print("\nDeleted: {}".format(GROUP_NAME))
def print_item(group):
"""Print a ResourceGroup instance."""
if hasattr(group, 'name'):
print("\tName: {}".format(group.name))
print("\tId: {}".format(group.id))
if hasattr(group, 'location'):
print("\tLocation: {}".format(group.location))
print_properties(getattr(group, 'properties', None))
def print_properties(props):
"""Print a ResourceGroup propertyies instance."""
if props and hasattr(props, 'provisioning_state'):
print("\tProperties:")
print("\t\tProvisioning State: {}".format(props.provisioning_state))
print("\n\n")
###### Network creation, not specific to MSI scenario ######
def create_virtual_network(network_client):
"""Usual VNet creation.
"""
params_create = {
'location': LOCATION,
'address_space': {
'address_prefixes': ['10.0.0.0/16'],
},
'subnets': [{
'name': SUBNET_NAME,
'address_prefix': '10.0.0.0/24',
}],
}
vnet_poller = network_client.virtual_networks.begin_create_or_update(
GROUP_NAME,
VNET_NAME,
params_create,
)
vnet_poller.wait()
return network_client.subnets.get(
GROUP_NAME,
VNET_NAME,
SUBNET_NAME,
)
def create_public_ip(network_client):
"""Usual PublicIP creation.
"""
params_create = {
'location': LOCATION,
'public_ip_allocation_method': 'dynamic',
}
pip_poller = network_client.public_ip_addresses.begin_create_or_update(
GROUP_NAME,
PUBLIC_IP_NAME,
params_create,
)
return pip_poller.result()
def create_network_interface(network_client, subnet, public_ip):
"""Usual create NIC.
"""
params_create = {
'location': LOCATION,
'ip_configurations': [{
'name': IP_CONFIG_NAME,
'private_ip_allocation_method': "Dynamic",
'subnet': subnet,
'public_ip_address': {
'id': public_ip.id
}
}]
}
nic_poller = network_client.network_interfaces.begin_create_or_update(
GROUP_NAME,
NIC_NAME,
params_create,
)
return nic_poller.result()
###### VM creation, not specific to this scenario ######
def get_hardware_profile():
return {
'vm_size': 'standard_a0'
}
def get_network_profile(network_interface_id):
return {
'network_interfaces': [{
'id': network_interface_id,
}],
}
def get_storage_profile():
return {
'image_reference': {
'publisher': 'Canonical',
'offer': 'UbuntuServer',
'sku': '16.04.0-LTS',
'version': 'latest'
}
}
if __name__ == "__main__":
run_example()