Reputation: 131
I want to write a Python code to read a secret stored in Azure Key Vault. This code will run on the cloud on an Azure container. It goes like this:
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import HttpResponseError
import os
VAULT_URL = os.environ["VAULT_URL"]
credential = DefaultAzureCredential()
client = SecretClient(vault_url=VAULT_URL, credential=credential)
print("\n.. Get a Secret by name")
company_secret = client.get_secret('secret_name')
print("Secret with name '{0}' was found with value '{1}'.".format(company_secret.name, company_secret.value))
The thing is, I should specify some Environment variables like Vault URL and I don't know how to do that. I also don't know where I should put my credentials to actually access the Key Vault.
The main question is: how do I establish environment variables?
Thank you
Upvotes: 0
Views: 2089
Reputation: 839
Environment variables are just a dict, so setting them is very similar to getting them.
# Gets env var
VAULT_URL = os.environ["VAULT_URL"]
# Sets env var
os.environ['URL_VAULT'] = 'Some value'
You may wish to verify they do not exist prior to overwriting:
if 'URL_VAULT' not in os.environ:
os.environ['URL_VAULT'] = 'Some value'
Upvotes: 2