Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Key Vault] Fix backup/restore samples #17648

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#
# 3. Delete a certificate (begin_delete_certificate)
#
# 4. Restore a certificate (restore_certificate_backup)
# 4. Purge a certificate (purge_deleted_certificate)
#
# 5. Restore a certificate (restore_certificate_backup)
# ----------------------------------------------------------------------------------------------------------

# Instantiate a certificate client that will be used to call the service.
Expand Down Expand Up @@ -60,13 +62,22 @@

# The storage account certificate is no longer in use, so you can delete it.
print("\n.. Delete the certificate")
client.begin_delete_certificate(cert_name).wait()
print("Deleted certificate '{0}'".format(cert_name))
delete_operation = client.begin_delete_certificate(cert_name)
deleted_certificate = delete_operation.result()
print("Deleted certificate with name '{0}'".format(deleted_certificate.name))

# Wait for the deletion to complete before purging the certificate.
# The purge will take some time, so wait before restoring the backup to avoid a conflict.
delete_operation.wait()
print("\n.. Purge the certificate")
client.purge_deleted_certificate(deleted_certificate.name)
time.sleep(60)
print("Purged certificate with name '{0}'".format(deleted_certificate.name))

# In future, if the certificate is required again, we can use the backup value to restore it in the Key Vault.
# In the future, if the certificate is required again, we can use the backup value to restore it in the Key Vault.
print("\n.. Restore the certificate from the backup")
certificate = client.restore_certificate_backup(certificate_backup)
print("Restored Certificate with name '{0}'".format(certificate.name))
print("Restored certificate with name '{0}'".format(certificate.name))

except HttpResponseError as e:
print("\nrun_sample has caught an error. {0}".format(e.message))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#
# 2. azure-keyvault-certificates and azure-identity packages (pip install these)
#
# 3. Set Environment variables AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET, vault_url
# 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)
#
# ----------------------------------------------------------------------------------------------------------
Expand All @@ -27,7 +27,9 @@
#
# 3. Delete a certificate (delete_certificate)
#
# 4. Restore a certificate (restore_certificate_backup)
# 4. Purge a certificate (purge_deleted_certificate)
#
# 5. Restore a certificate (restore_certificate_backup)
# ----------------------------------------------------------------------------------------------------------


Expand Down Expand Up @@ -62,12 +64,19 @@ async def run_sample():
# The storage account certificate is no longer in use, so you can delete it.
print("\n.. Delete the certificate")
await client.delete_certificate(cert_name)
print("Deleted Certificate with name '{0}'".format(cert_name))
print("Deleted certificate with name '{0}'".format(cert_name))

# Purge the deleted certificate.
# The purge will take some time, so wait before restoring the backup to avoid a conflict.
print("\n.. Purge the certificate")
await client.purge_deleted_certificate(cert_name)
await asyncio.sleep(60)
print("Purged certificate with name '{0}'".format(cert_name))

# In future, if the certificate is required again, we can use the backup value to restore it in the Key Vault.
# In the future, if the certificate is required again, we can use the backup value to restore it in the Key Vault.
print("\n.. Restore the certificate using the backed up certificate bytes")
certificate = await client.restore_certificate_backup(certificate_backup)
print("Restored Certificate with name '{0}'".format(certificate.name))
print("Restored certificate with name '{0}'".format(certificate.name))

except HttpResponseError as e:
print("\nrun_sample has caught an error. {0}".format(e.message))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License.
# ------------------------------------
import os
import time
from azure.keyvault.keys import KeyClient
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import HttpResponseError
Expand All @@ -25,7 +26,9 @@
#
# 3. Delete a key (begin_delete_key)
#
# 4. Restore a key (restore_key_backup)
# 4. Purge a key (purge_deleted_key)
#
# 5. Restore a key (restore_key_backup)
# ----------------------------------------------------------------------------------------------------------

# Instantiate a key client that will be used to call the service.
Expand All @@ -50,13 +53,22 @@

# The rsa key is no longer in use, so you delete it.
print("\n.. Delete the key")
deleted_key = client.begin_delete_key(key.name).result()
print("Deleted Key with name '{0}'".format(deleted_key.name))
delete_operation = client.begin_delete_key(key.name)
deleted_key = delete_operation.result()
print("Deleted key with name '{0}'".format(deleted_key.name))

# Wait for the deletion to complete before purging the key.
# The purge will take some time, so wait before restoring the backup to avoid a conflict.
delete_operation.wait()
print("\n.. Purge the key")
client.purge_deleted_key(key.name)
time.sleep(60)
print("Purged key with name '{0}'".format(deleted_key.name))

# In future, if the key is required again, we can use the backup value to restore it in the Key Vault.
# In the future, if the key is required again, we can use the backup value to restore it in the Key Vault.
print("\n.. Restore the key using the backed up key bytes")
key = client.restore_key_backup(key_backup)
print("Restored Key with name '{0}'".format(key.name))
print("Restored key with name '{0}'".format(key.name))

except HttpResponseError as e:
print("\nThis sample has caught an error. {0}".format(e.message))
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#
# 3. Delete a key (delete_key)
#
# 4. Restore a key (restore_key_backup)
# 4. Purge a key (purge_deleted_key)
#
# 5. Restore a key (restore_key_backup)
# ----------------------------------------------------------------------------------------------------------
async def run_sample():
# Instantiate a key client that will be used to call the service.
Expand All @@ -51,12 +53,19 @@ async def run_sample():

# The rsa key is no longer in use, so you delete it.
deleted_key = await client.delete_key(key.name)
print("Deleted Key with name '{0}'".format(deleted_key.name))
print("Deleted key with name '{0}'".format(deleted_key.name))

# Purge the deleted key.
# The purge will take some time, so wait before restoring the backup to avoid a conflict.
print("\n.. Purge the key")
await client.purge_deleted_key(key.name)
await asyncio.sleep(60)
print("Purged key with name '{0}'".format(deleted_key.name))

# In future, if the key is required again, we can use the backup value to restore it in the Key Vault.
# In the future, if the key is required again, we can use the backup value to restore it in the Key Vault.
print("\n.. Restore the key using the backed up key bytes")
key = await client.restore_key_backup(key_backup)
print("Restored Key with name '{0}'".format(key.name))
print("Restored key with name '{0}'".format(key.name))

except HttpResponseError as e:
print("\nrun_sample has caught an error. {0}".format(e.message))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License.
# ------------------------------------
import os
import time
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import HttpResponseError
Expand All @@ -25,7 +26,9 @@
#
# 3. Delete a secret (begin_delete_secret)
#
# 4. Restore a secret (restore_secret_backup)
# 4. Purge a secret (purge_deleted_secret)
#
# 5. Restore a secret (restore_secret_backup)
# ----------------------------------------------------------------------------------------------------------

# Instantiate a secret client that will be used to call the service.
Expand All @@ -50,13 +53,22 @@

# 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))
delete_operation = client.begin_delete_secret(secret.name)
deleted_secret = delete_operation.result()
print("Deleted secret with name '{0}'".format(deleted_secret.name))

# Wait for the deletion to complete before purging the secret.
# The purge will take some time, so wait before restoring the backup to avoid a conflict.
delete_operation.wait()
print("\n.. Purge the secret")
client.purge_deleted_secret(deleted_secret.name)
time.sleep(60)
print("Purged 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.
# In the 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))
print("Restored secret with name '{0}'".format(secret.name))

except HttpResponseError as e:
print("\nThis sample has caught an error. {0}".format(e.message))
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
#
# 3. Delete a secret (delete_secret)
#
# 4. Restore a secret (restore_secret_backup)
# 4. Purge a secret (purge_deleted_secret)
#
# 5. Restore a secret (restore_secret_backup)
# ----------------------------------------------------------------------------------------------------------
async def run_sample():
# Instantiate a secret client that will be used to call the service.
Expand All @@ -53,12 +55,19 @@ async def run_sample():
# The storage account secret is no longer in use, so you delete it.
print("\n.. Deleting secret...")
await client.delete_secret(secret.name)
print("Deleted Secret with name '{0}'".format(secret.name))
print("Deleted secret with name '{0}'".format(secret.name))

# Purge the deleted secret.
# The purge will take some time, so wait before restoring the backup to avoid a conflict.
print("\n.. Purge the secret")
await client.purge_deleted_secret(secret.name)
await asyncio.sleep(60)
print("Purged 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.
# In the 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 = await client.restore_secret_backup(secret_backup)
print("Restored Secret with name '{0}'".format(secret.name))
print("Restored secret with name '{0}'".format(secret.name))

except HttpResponseError as e:
print("\nrun_sample has caught an error. {0}".format(e.message))
Expand Down