Skip to content

Commit

Permalink
refactor(secrets): calculate key vault URL from runtime env
Browse files Browse the repository at this point in the history
similar to how this is done in the Terraform module
  • Loading branch information
thekaveman committed Jan 30, 2024
1 parent a664126 commit 29a0d41
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions benefits/secrets.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import sys

from django.conf import settings

from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient


KEY_VAULT_URL = "https://kv-cdt-pub-calitp-{env}-001.vault.azure.net/"


if __name__ == "__main__":
args = sys.argv[1:]
if len(args) < 2:
print("Provide the Key Vault URL and the name of the secret to read")
if len(args) < 1:
print("Provide the name of the secret to read")
exit(1)

vault_url = args[0]
secret_name = args[1]
# construct the KeyVault URL from the runtime environment
# see https://docs.calitp.org/benefits/deployment/infrastructure/#environments
# and https://github.com/cal-itp/benefits/blob/dev/terraform/key_vault.tf
runtime_env = settings.RUNTIME_ENVIRONMENT()
vault_url = KEY_VAULT_URL.format(env=runtime_env[0])

secret_name = args[0]

credential = DefaultAzureCredential()
client = SecretClient(vault_url=vault_url, credential=credential)
Expand Down

0 comments on commit 29a0d41

Please sign in to comment.