Skip to content

Commit

Permalink
Call load_dotenv in test-resources-cleanup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mccoyp committed Feb 2, 2021
1 parent e1140f0 commit 794713d
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions sdk/keyvault/test-resources-cleanup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import os
from dotenv import load_dotenv

from azure.identity import ClientSecretCredential
from azure.keyvault.certificates import CertificateClient
from azure.keyvault.keys import KeyClient
from azure.keyvault.secrets import SecretClient

load_dotenv()

if "AZURE_KEYVAULT_URL" not in os.environ:
raise EnvironmentError("Missing a Key Vault URL")
if "KEYVAULT_TENANT_ID" not in os.environ:
Expand All @@ -15,33 +22,32 @@
raise EnvironmentError("Missing a client secret for Key Vault")

credential = ClientSecretCredential(
tenant_id=os.environ.get("KEYVAULT_TENANT_ID"),
client_id=os.environ.get("KEYVAULT_CLIENT_ID"),
client_secret=os.environ.get("KEYVAULT_CLIENT_SECRET")
tenant_id=os.environ["KEYVAULT_TENANT_ID"],
client_id=os.environ["KEYVAULT_CLIENT_ID"],
client_secret=os.environ["KEYVAULT_CLIENT_SECRET"]
)

if "AZURE_KEYVAULT_URL" in os.environ:
cert_client = CertificateClient(os.environ.get("AZURE_KEYVAULT_URL"), credential)
key_client = KeyClient(os.environ.get("AZURE_KEYVAULT_URL"), credential)
secret_client = SecretClient(os.environ.get("AZURE_KEYVAULT_URL"), credential)

test_certificates = [c for c in cert_client.list_properties_of_certificates() if c.name.startswith("livekvtest")]
for certificate in test_certificates:
cert_client.begin_delete_certificate(certificate.name).wait()
deleted_test_certificates = [c for c in cert_client.list_deleted_certificates() if c.name.startswith("livekvtest")]
for certificate in deleted_test_certificates:
cert_client.purge_deleted_certificate(certificate.name)

test_keys = [k for k in key_client.list_properties_of_keys() if k.name.startswith("livekvtest")]
for key in test_keys:
key_client.begin_delete_key(key.name).wait()
deleted_test_keys = [k for k in key_client.list_deleted_keys() if k.name.startswith("livekvtest")]
for key in deleted_test_keys:
key_client.purge_deleted_key(key.name)

test_secrets = [s for s in secret_client.list_properties_of_secrets() if s.name.startswith("livekvtest")]
for secret in test_secrets:
secret_client.begin_delete_secret(secret.name).wait()
deleted_test_secrets = [s for s in secret_client.list_deleted_secrets() if s.name.startswith("livekvtest")]
for secret in deleted_test_secrets:
secret_client.purge_deleted_secret(secret.name)
cert_client = CertificateClient(os.environ["AZURE_KEYVAULT_URL"], credential)
key_client = KeyClient(os.environ["AZURE_KEYVAULT_URL"], credential)
secret_client = SecretClient(os.environ["AZURE_KEYVAULT_URL"], credential)

test_certificates = [c for c in cert_client.list_properties_of_certificates() if c.name.startswith("livekvtest")]
for certificate in test_certificates:
cert_client.begin_delete_certificate(certificate.name).wait()
deleted_test_certificates = [c for c in cert_client.list_deleted_certificates() if c.name.startswith("livekvtest")]
for certificate in deleted_test_certificates:
cert_client.purge_deleted_certificate(certificate.name)

test_keys = [k for k in key_client.list_properties_of_keys() if k.name.startswith("livekvtest")]
for key in test_keys:
key_client.begin_delete_key(key.name).wait()
deleted_test_keys = [k for k in key_client.list_deleted_keys() if k.name.startswith("livekvtest")]
for key in deleted_test_keys:
key_client.purge_deleted_key(key.name)

test_secrets = [s for s in secret_client.list_properties_of_secrets() if s.name.startswith("livekvtest")]
for secret in test_secrets:
secret_client.begin_delete_secret(secret.name).wait()
deleted_test_secrets = [s for s in secret_client.list_deleted_secrets() if s.name.startswith("livekvtest")]
for secret in deleted_test_secrets:
secret_client.purge_deleted_secret(secret.name)

0 comments on commit 794713d

Please sign in to comment.