-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Key Vault: reworked certificates samples, all passing (#7225)
* reworked certificates samples, all passing * automatically run sync samples instead of within a try catch block
- Loading branch information
1 parent
58e3bf1
commit 1caeded
Showing
12 changed files
with
433 additions
and
542 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,19 +8,13 @@ | |
from azure.core.exceptions import HttpResponseError | ||
|
||
# ---------------------------------------------------------------------------------------------------------- | ||
# Prerequistes - | ||
# Prerequisites: | ||
# 1. An Azure Key Vault (https://docs.microsoft.com/en-us/azure/key-vault/quick-create-cli) | ||
# | ||
# 1. An Azure Key Vault- | ||
# https://docs.microsoft.com/en-us/azure/key-vault/quick-create-cli | ||
# 2. azure-keyvault-certificates and azure-identity packages (pip install these) | ||
# | ||
# 2. Microsoft Azure Key Vault PyPI package - | ||
# https://pypi.python.org/pypi/azure-keyvault-certificates/ | ||
# | ||
# 3. Microsoft Azure Identity package - | ||
# https://pypi.python.org/pypi/azure-identity/ | ||
# | ||
# 4. Set Environment variables AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET, VAULT_URL. | ||
# How to do this - https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/keyvault/azure-keyvault-certificates#createget-credentials) | ||
# 3. Set Environment variables AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET, VAULT_URL | ||
# (See https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/keyvault/azure-keyvault-keys#authenticate-the-client) | ||
# | ||
# ---------------------------------------------------------------------------------------------------------- | ||
# Sample - demonstrates basic CRUD operations for the certificate contacts for a key vault. | ||
|
@@ -32,48 +26,39 @@ | |
# 3. Delete contacts (delete_contacts) | ||
# ---------------------------------------------------------------------------------------------------------- | ||
|
||
def run_sample(): | ||
# Instantiate a certificate client that will be used to call the service. | ||
# Notice that the client is using default Azure credentials. | ||
# To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID', | ||
# 'AZURE_CLIENT_SECRET' and 'AZURE_TENANT_ID' are set with the service principal credentials. | ||
VAULT_URL = os.environ["VAULT_URL"] | ||
credential = DefaultAzureCredential() | ||
client = CertificateClient(vault_url=VAULT_URL, credential=credential) | ||
try: | ||
# First we create a list of Contacts that we would like to make the certificate contacts for this key vault. | ||
contact_list = [ | ||
Contact(email='[email protected]', | ||
name='John Doe', | ||
phone='1111111111'), | ||
Contact(email='[email protected]', | ||
name='John Doe2', | ||
phone='2222222222') | ||
] | ||
|
||
# Creates and sets the certificate contacts for this key vault. | ||
client.create_contacts(contacts=contact_list) | ||
|
||
# Gets the certificate contacts for this key vault. | ||
contacts = client.get_contacts() | ||
for contact in contacts: | ||
print(contact.name) | ||
print(contact.email) | ||
print(contact.phone) | ||
|
||
# Deletes all of the certificate contacts for this key vault. | ||
client.delete_contacts() | ||
# Instantiate a certificate client that will be used to call the service. | ||
# Notice that the client is using default Azure credentials. | ||
# To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID', | ||
# 'AZURE_CLIENT_SECRET' and 'AZURE_TENANT_ID' are set with the service principal credentials. | ||
VAULT_URL = os.environ["VAULT_URL"] | ||
credential = DefaultAzureCredential() | ||
client = CertificateClient(vault_url=VAULT_URL, credential=credential) | ||
try: | ||
# First we create a list of Contacts that we would like to make the certificate contacts for this key vault. | ||
contact_list = [ | ||
Contact(email='[email protected]', | ||
name='John Doe', | ||
phone='1111111111'), | ||
Contact(email='[email protected]', | ||
name='John Doe2', | ||
phone='2222222222') | ||
] | ||
|
||
except HttpResponseError as e: | ||
print("\nrun_sample has caught an error. {0}".format(e.message)) | ||
# Creates and sets the certificate contacts for this key vault. | ||
client.create_contacts(contacts=contact_list) | ||
|
||
finally: | ||
print("\nrun_sample done") | ||
# Gets the certificate contacts for this key vault. | ||
contacts = client.get_contacts() | ||
for contact in contacts: | ||
print(contact.name) | ||
print(contact.email) | ||
print(contact.phone) | ||
|
||
# Deletes all of the certificate contacts for this key vault. | ||
client.delete_contacts() | ||
|
||
if __name__ == "__main__": | ||
try: | ||
run_sample() | ||
except HttpResponseError as e: | ||
print("\nrun_sample has caught an error. {0}".format(e.message)) | ||
|
||
except Exception as e: | ||
print("Top level Error: {0}".format(str(e))) | ||
finally: | ||
print("\nrun_sample done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.