Пример #1
0
    def backup_restore_secret(self):
        """
        backs up a key vault secret and restores it to another key vault
        """
        # create a key vault
        first_vault = self.create_vault()

        # create a secret client
        credential = DefaultAzureCredential()
        first_secret_client = SecretClient(
            vault_url=first_vault.properties.vault_uri, credential=credential)

        # add a secret to the vault
        secret_name = get_name('secret')
        secret_value = 'this is a secret value to be migrated from one vault to another'

        secret = first_secret_client.set_secret(secret_name, secret_value)
        print('created secret {}'.format(secret.name))

        # list the secrets in the vault
        secret_properties = first_secret_client.list_properties_of_secrets()
        print("all of the secrets in the client's vault:")
        for secret_property in secret_properties:
            print(secret_property.name)

        # backup the secret
        backup = first_secret_client.backup_secret(secret_name)
        print('backed up secret {}'.format(secret_name))

        # create a second vault
        second_vault = self.create_vault()

        # create a secret client
        second_secret_client = SecretClient(
            vault_url=second_vault.properties.vault_uri, credential=credential)

        # restore the secret to the new vault
        restored = second_secret_client.restore_secret_backup(backup)
        print('restored secret {}'.format(restored.name))

        # list the secrets in the new vault
        secret_properties = second_secret_client.list_properties_of_secrets()
        print("all of the secrets in the new vault:")
        for secret_property in secret_properties:
            print(secret_property.name)
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.
    print("\n.. Deleting secret...")
    deleted_secret = client.begin_delete_secret(secret.name).result()
    print("Deleted Secret with name '{0}'".format(deleted_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")