Skip to content

Instantly share code, notes, and snippets.

@gbaeke
Last active Nov 4, 2022
Embed
What would you like to do?
from azure.appconfiguration.provider import (
AzureAppConfigurationProvider,
SettingSelector,
AzureAppConfigurationKeyVaultOptions
)
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
def retrieve_secret(uri):
try:
# uri is in format: https://<keyvaultname>.vault.azure.net/secrets/<secretname>
# retrieve key vault uri and secret name from uri
vault_uri = "https://" + uri.split('/')[2]
secret_name = uri.split('/')[-1]
print(f"Retrieving secret {secret_name} from {vault_uri}...")
# retrieve the secret from Key Vault; CREDENTIAL was set globally
secret_client = SecretClient(vault_url=vault_uri, credential=CREDENTIAL)
# get secret value from Key Vault
secret_value = secret_client.get_secret(secret_name).value
except Exception as ex:
print(f"retrieving secret: {ex}", 1)
return secret_value
try:
CREDENTIAL = DefaultAzureCredential(exclude_visual_studio_code_credential=True)
except Exception as ex:
print(f"error setting credentials: {ex}")
app = 'app1'
env = 'dev'
selects = {SettingSelector(key_filter=f"{app}:*", label_filter=env)}
trimmed_key_prefixes = {f"{app}:"}
key_vault_options = AzureAppConfigurationKeyVaultOptions(secret_resolver=retrieve_secret)
connection_string = 'CONNSTR'
app_config = AzureAppConfigurationProvider.load(connection_string=connection_string,
selects=selects, key_vault_options=key_vault_options,
trimmed_key_prefixes=trimmed_key_prefixes)
print(app_config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment