Skip to content

Instantly share code, notes, and snippets.

@drodrz
Created July 8, 2019 20:45
Show Gist options
  • Select an option

  • Save drodrz/2aa3c83abfdfd710a7ac155cd046c216 to your computer and use it in GitHub Desktop.

Select an option

Save drodrz/2aa3c83abfdfd710a7ac155cd046c216 to your computer and use it in GitHub Desktop.
import json
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.keyvault import KeyVaultManagementClient
from azure.mgmt.resource.resources import ResourceManagementClient
from haikunator import Haikunator
REGION = 'eastus'
GROUP_NAME = 'azure-group-name'
KV_NAME = 'vault-name'
OBJECT_ID = '00000000-0000-0000-0000-000000000000'
subscription_id = os.environ['AZURE_SUBSCRIPTION_ID']
credentials = ServicePrincipalCredentials(
client_id=os.environ['AZURE_CLIENT_ID'],
secret=os.environ['AZURE_CLIENT_SECRET'],
tenant=os.environ['AZURE_TENANT_ID']
)
kv_client = KeyVaultManagementClient(credentials, subscription_id)
resource_client = ResourceManagementClient(credentials, subscription_id)
# You MIGHT need to add KeyVault as a valid provider for these credentials
# If so, this operation has to be done only once for each credentials
resource_client.providers.register('Microsoft.KeyVault')
# Create Resource group
resource_group_params = {'location': REGION}
print_item(resource_client.resource_groups.create_or_update(
GROUP_NAME, resource_group_params))
# Create a vault
print('\nCreate a vault')
vault = kv_client.vaults.create_or_update(
GROUP_NAME,
KV_NAME,
{
'location': REGION,
'properties': {
'sku': {
'name': 'standard'
},
'tenant_id': os.environ['AZURE_TENANT_ID'],
'access_policies': [{
'tenant_id': os.environ['AZURE_TENANT_ID'],
'object_id': OBJECT_ID,
'permissions': {
'keys': ['all'],
'secrets': ['all']
}
}]
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment