2021-02-06 00:17:01 +03:00
|
|
|
# ------------------------------------
|
|
|
|
# Copyright (c) Microsoft Corporation.
|
|
|
|
# Licensed under the MIT License.
|
|
|
|
# ------------------------------------
|
2021-03-06 00:35:11 +03:00
|
|
|
import asyncio
|
2021-02-06 00:17:01 +03:00
|
|
|
from dotenv import load_dotenv
|
2021-03-06 00:35:11 +03:00
|
|
|
import os
|
2021-02-06 00:17:01 +03:00
|
|
|
|
2024-10-17 23:35:56 +03:00
|
|
|
from azure.identity.aio import ClientSecretCredential
|
2021-03-06 00:35:11 +03:00
|
|
|
from azure.keyvault.certificates.aio import CertificateClient
|
|
|
|
from azure.keyvault.keys.aio import KeyClient
|
|
|
|
from azure.keyvault.secrets.aio import SecretClient
|
2021-02-06 00:17:01 +03:00
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
if "AZURE_KEYVAULT_URL" not in os.environ:
|
|
|
|
raise EnvironmentError("Missing a Key Vault URL")
|
2024-10-17 23:35:56 +03:00
|
|
|
if "KEYVAULT_TENANT_ID" not in os.environ:
|
|
|
|
raise EnvironmentError("Missing a tenant ID for Key Vault")
|
|
|
|
if "KEYVAULT_CLIENT_ID" not in os.environ:
|
|
|
|
raise EnvironmentError("Missing a client ID for Key Vault")
|
|
|
|
if "KEYVAULT_CLIENT_SECRET" not in os.environ:
|
|
|
|
raise EnvironmentError("Missing a client secret for Key Vault")
|
2021-07-07 19:30:55 +03:00
|
|
|
|
2024-05-09 21:08:49 +03:00
|
|
|
hsm_present = "AZURE_MANAGEDHSM_URL" in os.environ
|
2021-02-06 00:17:01 +03:00
|
|
|
|
2024-10-17 23:35:56 +03:00
|
|
|
credential = ClientSecretCredential(
|
|
|
|
tenant_id=os.environ["KEYVAULT_TENANT_ID"],
|
|
|
|
client_id=os.environ["KEYVAULT_CLIENT_ID"],
|
|
|
|
client_secret=os.environ["KEYVAULT_CLIENT_SECRET"]
|
|
|
|
)
|
|
|
|
|
2021-02-06 00:17:01 +03:00
|
|
|
cert_client = CertificateClient(os.environ["AZURE_KEYVAULT_URL"], credential)
|
|
|
|
key_client = KeyClient(os.environ["AZURE_KEYVAULT_URL"], credential)
|
2021-07-07 19:30:55 +03:00
|
|
|
hsm_client = KeyClient(os.environ["AZURE_MANAGEDHSM_URL"], credential) if hsm_present else None
|
2021-02-06 00:17:01 +03:00
|
|
|
secret_client = SecretClient(os.environ["AZURE_KEYVAULT_URL"], credential)
|
|
|
|
|
2021-03-06 00:35:11 +03:00
|
|
|
async def delete_certificates():
|
|
|
|
coroutines = []
|
|
|
|
|
|
|
|
test_certificates = cert_client.list_properties_of_certificates()
|
|
|
|
async for certificate in test_certificates:
|
|
|
|
if certificate.name.startswith("livekvtest"):
|
|
|
|
coroutines.append(cert_client.delete_certificate(certificate.name))
|
|
|
|
|
|
|
|
return await asyncio.gather(*coroutines)
|
|
|
|
|
|
|
|
async def delete_keys_and_secrets():
|
|
|
|
coroutines = []
|
|
|
|
|
|
|
|
test_keys = key_client.list_properties_of_keys()
|
|
|
|
async for key in test_keys:
|
|
|
|
if key.name.startswith("livekvtest"):
|
|
|
|
coroutines.append(key_client.delete_key(key.name))
|
|
|
|
|
2021-07-07 19:30:55 +03:00
|
|
|
if hsm_client:
|
|
|
|
hsm_test_keys = hsm_client.list_properties_of_keys()
|
|
|
|
async for key in hsm_test_keys:
|
|
|
|
if key.name.startswith("livekvtest"):
|
|
|
|
coroutines.append(hsm_client.delete_key(key.name))
|
|
|
|
|
2021-03-06 00:35:11 +03:00
|
|
|
test_secrets = secret_client.list_properties_of_secrets()
|
|
|
|
async for secret in test_secrets:
|
|
|
|
if secret.name.startswith("livekvtest"):
|
|
|
|
coroutines.append(secret_client.delete_secret(secret.name))
|
|
|
|
|
|
|
|
return await asyncio.gather(*coroutines)
|
|
|
|
|
|
|
|
async def purge_resources():
|
|
|
|
coroutines = []
|
|
|
|
|
|
|
|
deleted_test_certificates = cert_client.list_deleted_certificates(include_pending=True)
|
|
|
|
async for certificate in deleted_test_certificates:
|
|
|
|
if certificate.name.startswith("livekvtest"):
|
|
|
|
coroutines.append(cert_client.purge_deleted_certificate(certificate.name))
|
|
|
|
|
|
|
|
deleted_test_keys = key_client.list_deleted_keys()
|
|
|
|
async for key in deleted_test_keys:
|
|
|
|
if key.name.startswith("livekvtest"):
|
|
|
|
coroutines.append(key_client.purge_deleted_key(key.name))
|
|
|
|
|
2021-07-07 19:30:55 +03:00
|
|
|
if hsm_client:
|
|
|
|
hsm_deleted_test_keys = hsm_client.list_deleted_keys()
|
|
|
|
async for key in hsm_deleted_test_keys:
|
|
|
|
if key.name.startswith("livekvtest"):
|
|
|
|
coroutines.append(hsm_client.purge_deleted_key(key.name))
|
|
|
|
|
2021-03-06 00:35:11 +03:00
|
|
|
deleted_test_secrets = secret_client.list_deleted_secrets()
|
|
|
|
async for secret in deleted_test_secrets:
|
|
|
|
if secret.name.startswith("livekvtest"):
|
|
|
|
coroutines.append(secret_client.purge_deleted_secret(secret.name))
|
|
|
|
|
|
|
|
return await asyncio.gather(*coroutines)
|
|
|
|
|
|
|
|
async def close_sessions():
|
|
|
|
await credential.close()
|
|
|
|
await cert_client.close()
|
|
|
|
await key_client.close()
|
2021-07-07 19:30:55 +03:00
|
|
|
if hsm_client:
|
|
|
|
await hsm_client.close()
|
2021-03-06 00:35:11 +03:00
|
|
|
await secret_client.close()
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(delete_certificates())
|
|
|
|
loop.run_until_complete(delete_keys_and_secrets())
|
|
|
|
loop.run_until_complete(purge_resources())
|
|
|
|
loop.run_until_complete(close_sessions())
|
|
|
|
loop.close()
|