def run_sample(): # Instantiate a secret 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 = SecretClient(vault_url=VAULT_URL, credential=credential) try: # Let's create secrets holding storage and bank accounts credentials. If the secret # already exists in the Key Vault, then a new version of the secret is created. print("\n1. Create Secret") bank_secret = client.set_secret("recoverPurgeBankSecretName", "recoverPurgeSecretValue1") storage_secret = client.set_secret("recoverPurgeStorageSecretName", "recoverPurgeSecretValue2") print("Secret with name '{0}' was created.".format(bank_secret.name)) print("Secret with name '{0}' was created.".format( storage_secret.name)) # The storage account was closed, need to delete its credentials from the Key Vault. print("\n2. Delete a Secret") secret = client.delete_secret(bank_secret.name) time.sleep(20) print("Secret with name '{0}' was deleted on date {1}.".format( secret.name, secret.deleted_date)) # We accidentally deleted the bank account secret. Let's recover it. # A deleted secret can only be recovered if the Key Vault is soft-delete enabled. print("\n3. Recover Deleted Secret") recovered_secret = client.recover_deleted_secret(bank_secret.name) print("Recovered Secret with name '{0}'.".format( recovered_secret.name)) # Let's delete storage account now. # If the keyvault is soft-delete enabled, then for permanent deletion deleted secret needs to be purged. client.delete_secret(storage_secret.name) # To ensure secret is deleted on the server side. print("\nDeleting Storage Secret...") time.sleep(20) # To ensure permanent deletion, we might need to purge the secret. print("\n4. Purge Deleted Secret") client.purge_deleted_secret(storage_secret.name) print("Secret has been permanently deleted.") except HttpResponseError as e: if "(NotSupported)" in e.message: print( "\n{0} Please enable soft delete on Key Vault to perform this operation." .format(e.message)) else: print("\nrun_sample has caught an error. {0}".format(e.message)) finally: print("\nrun_sample done")
def run_sample(): # Instantiate a secret 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_ENDPOINT = os.environ["VAULT_ENDPOINT"] credential = DefaultAzureCredential() client = SecretClient(vault_endpoint=VAULT_ENDPOINT, credential=credential) try: # Let's create a secret holding bank account credentials valid for 1 year. # if the secret already exists in the Key Vault, then a new version of the secret is created. print("\n.. Create Secret") expires = datetime.datetime.utcnow() + datetime.timedelta(days=365) secret = client.set_secret("helloWorldSecretName", "helloWorldSecretValue", expires_on=expires) print("Secret with name '{0}' created with value '{1}'".format( secret.name, secret.value)) print("Secret with name '{0}' expires on '{1}'".format( secret.name, secret.properties.expires_on)) # Let's get the bank secret using its name print("\n.. Get a Secret by name") bank_secret = client.get_secret(secret.name) print("Secret with name '{0}' was found with value '{1}'.".format( bank_secret.name, bank_secret.value)) # After one year, the bank account is still active, we need to update the expiry time of the secret. # The update method can be used to update the expiry attribute of the secret. It cannot be used to update # the value of the secret. print("\n.. Update a Secret by name") expires = bank_secret.properties.expires_on + datetime.timedelta( days=365) updated_secret_properties = client.update_secret_properties( secret.name, expires_on=expires) print("Secret with name '{0}' was updated on date '{1}'".format( secret.name, updated_secret_properties.updated_on)) print("Secret with name '{0}' was updated to expire on '{1}'".format( secret.name, updated_secret_properties.expires_on)) # Bank forced a password update for security purposes. Let's change the value of the secret in the Key Vault. # To achieve this, we need to create a new version of the secret in the Key Vault. The update operation cannot # change the value of the secret. secret = client.set_secret(secret.name, "newSecretValue") print("Secret with name '{0}' created with value '{1}'".format( secret.name, secret.value)) # The bank account was closed, need to delete its credentials from the Key Vault. print("\n.. Delete Secret") deleted_secret = client.delete_secret(secret.name) print("Deleting Secret..") print("Secret with name '{0}' was deleted.".format( deleted_secret.name)) except HttpResponseError as e: print("\nrun_sample has caught an error. {0}".format(e.message)) finally: print("\nrun_sample done")
def run_sample(): # Instantiate a secret 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 = SecretClient(vault_url=VAULT_URL, credential=credential) try: # Let's create a secret holding storage account credentials. # if the secret already exists in the Key Vault, then a new version of the secret is created. print("\n1. Create Secret") secret = client.set_secret("backupRestoreSecretName", "backupRestoreSecretValue") print("Secret with name '{0}' created with value '{1}'".format( secret.name, secret.value)) # Backups are good to have, if in case secrets gets deleted accidentally. # For long term storage, it is ideal to write the backup to a file. print("\n2. Create a backup for an existing Secret") secret_backup = client.backup_secret(secret.name) print("Backup created for secret with name '{0}'.".format(secret.name)) # The storage account secret is no longer in use, so you delete it. client.delete_secret(secret.name) # To ensure secret is deleted on the server side. print("\nDeleting secret...") time.sleep(20) print("Deleted Secret with name '{0}'".format(secret.name)) # In future, if the secret is required again, we can use the backup value to restore it in the Key Vault. print("\n3. Restore the secret using the backed up secret bytes") secret = client.restore_secret(secret_backup) print("Restored Secret with name '{0}'".format(secret.name)) except HttpResponseError as e: print("\nrun_sample has caught an error. {0}".format(e.message)) finally: print("\nrun_sample done")
class KeyVaultSecrets: def __init__(self): # DefaultAzureCredential() expects the following environment variables: # * AZURE_CLIENT_ID # * AZURE_CLIENT_SECRET # * AZURE_TENANT_ID credential = DefaultAzureCredential() self.secret_client = SecretClient( vault_url=os.environ["AZURE_PROJECT_URL"], credential=credential) self.secret_name = "secret-name-" + uuid.uuid1().hex self.secret_Value = "secret-value" def set_secret(self): print("Setting a secret...") self.secret_client.set_secret(self.secret_name, self.secret_Value) print("\tdone") def get_secret(self): print("Getting a secret...") secret = self.secret_client.get_secret(self.secret_name) print("\tdone, secret: (" + secret.name + "," + secret.value + ").") def delete_secret(self): print("Deleting a secret...") deleted_secret = self.secret_client.delete_secret(self.secret_name) print("\tdone: " + deleted_secret.name) def run(self): print("") print("------------------------") print("Key Vault - Secrets\nIdentity - Credential") print("------------------------") print("1) Set a secret") print("2) Get that secret") print("3) Delete that secret (Clean up the resource)") print("") try: self.set_secret() self.get_secret() finally: self.delete_secret()
class KeyVault: def __init__(self): # DefaultAzureCredential() expects the following environment variables: # * AZURE_CLIENT_ID # * AZURE_CLIENT_SECRET # * AZURE_TENANT_ID credential = DefaultAzureCredential() self.secret_client = SecretClient( vault_url=os.environ["AZURE_PROJECT_URL"], credential=credential) def SetSecret(self): print("Setting a secret...") self.secret_client.set_secret("secret-name", "secret-value") print("\tdone") def GetSecret(self): print("Getting a secret...") secret = self.secret_client.get_secret("secret-name") print("\tdone: " + secret.name) def DeleteSecret(self): print("Deleting a secret...") deleted_secret = self.secret_client.delete_secret("secret-name") print("\tdone: " + deleted_secret.name) def Run(self): print() print("------------------------") print("Key Vault - Secrets\nIdentity - Credential") print("------------------------") print("1) Set a secret") print("2) Get that secret") print("3) Delete that secret (Clean up the resource)") print() try: self.SetSecret() self.GetSecret() finally: self.DeleteSecret()
credential = DefaultAzureCredential() client = SecretClient(vault_endpoint=VAULT_ENDPOINT, credential=credential) try: # Let's create secrets holding storage and bank accounts credentials. If the secret # already exists in the Key Vault, then a new version of the secret is created. print("\n.. Create Secret") bank_secret = client.set_secret("recoverPurgeBankSecretName", "recoverPurgeSecretValue1") storage_secret = client.set_secret("recoverPurgeStorageSecretName", "recoverPurgeSecretValue2") print("Secret with name '{0}' was created.".format(bank_secret.name)) print("Secret with name '{0}' was created.".format(storage_secret.name)) # The storage account was closed, need to delete its credentials from the Key Vault. print("\n.. Delete a Secret") secret = client.delete_secret(bank_secret.name) time.sleep(20) print("Secret with name '{0}' was deleted on date {1}.".format( secret.name, secret.deleted_date)) # We accidentally deleted the bank account secret. Let's recover it. # A deleted secret can only be recovered if the Key Vault is soft-delete enabled. print("\n.. Recover Deleted Secret") recovered_secret = client.recover_deleted_secret(bank_secret.name) print("Recovered Secret with name '{0}'.".format(recovered_secret.name)) # Let's delete storage account now. # If the keyvault is soft-delete enabled, then for permanent deletion deleted secret needs to be purged. client.delete_secret(storage_secret.name) # To ensure secret is deleted on the server side.
def run_sample(): # Instantiate a secret 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 = SecretClient(vault_url=VAULT_URL, credential=credential) try: # Let's create secrets holding storage and bank accounts credentials. If the secret # already exists in the Key Vault, then a new version of the secret is created. print("\n1. Create Secret") bank_secret = client.set_secret("listOpsBankSecretName", "listOpsSecretValue1") storage_secret = client.set_secret("listOpsStorageSecretName", "listOpsSecretValue2") print("Secret with name '{0}' was created.".format(bank_secret.name)) print("Secret with name '{0}' was created.".format(storage_secret.name)) # You need to check if any of the secrets are sharing same values. # Let's list the secrets and print their values. # List operations don 't return the secrets with value information. # So, for each returned secret we call get_secret to get the secret with its value information. print("\n2. List secrets from the Key Vault") secrets = client.list_secrets() for secret in secrets: retrieved_secret = client.get_secret(secret.name) print( "Secret with name '{0}' and value {1} was found.".format(retrieved_secret.name, retrieved_secret.name) ) # The bank account password got updated, so you want to update the secret in Key Vault to ensure it reflects the # new password. Calling set_secret on an existing secret creates a new version of the secret in the Key Vault # with the new value. updated_secret = client.set_secret(bank_secret.name, "newSecretValue") print( "Secret with name '{0}' was updated with new value '{1}'".format(updated_secret.name, updated_secret.value) ) # You need to check all the different values your bank account password secret had previously. Lets print all # the versions of this secret. print("\n3. List versions of the secret using its name") secret_versions = client.list_secret_versions(bank_secret.name) for secret_version in secret_versions: print("Bank Secret with name '{0}' has version: '{1}'.".format(secret_version.name, secret_version.version)) # The bank account and storage accounts got closed. Let's delete bank and storage accounts secrets. client.delete_secret(bank_secret.name) client.delete_secret(storage_secret.name) # To ensure secret is deleted on the server side. print("Deleting secrets...") time.sleep(30) # You can list all the deleted and non-purged secrets, assuming Key Vault is soft-delete enabled. print("\n3. List deleted secrets from the Key Vault") deleted_secrets = client.list_deleted_secrets() for deleted_secret in deleted_secrets: print( "Secret with name '{0}' has recovery id '{1}'".format(deleted_secret.name, deleted_secret.recovery_id) ) except HttpResponseError as e: if "(NotSupported)" in e.message: print("\n{0} Please enable soft delete on Key Vault to perform this operation.".format(e.message)) else: print("\nrun_sample has caught an error. {0}".format(e.message)) finally: print("\nrun_sample done")
client = SecretClient(vault_endpoint=VAULT_ENDPOINT, credential=credential) try: # Let's create a secret holding storage account credentials. # if the secret already exists in the Key Vault, then a new version of the secret is created. print("\n.. Create Secret") secret = client.set_secret("backupRestoreSecretName", "backupRestoreSecretValue") print("Secret with name '{0}' created with value '{1}'".format( secret.name, secret.value)) # Backups are good to have, if in case secrets gets deleted accidentally. # For long term storage, it is ideal to write the backup to a file. print("\n.. Create a backup for an existing Secret") secret_backup = client.backup_secret(secret.name) print("Backup created for secret with name '{0}'.".format(secret.name)) # The storage account secret is no longer in use, so you delete it. client.delete_secret(secret.name) print("Deleted Secret with name '{0}'".format(secret.name)) # In future, if the secret is required again, we can use the backup value to restore it in the Key Vault. print("\n.. Restore the secret using the backed up secret bytes") secret = client.restore_secret_backup(secret_backup) print("Restored Secret with name '{0}'".format(secret.name)) except HttpResponseError as e: print("\nrun_sample has caught an error. {0}".format(e.message)) finally: print("\nrun_sample done")