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")
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.. Deleting Secret...") client.begin_delete_secret(secret.name)