Skip to content

Instantly share code, notes, and snippets.

Last active November 4, 2022 15:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
from azure.appconfiguration.provider import (
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
def retrieve_secret(uri):
# uri is in format: https://<keyvaultname><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
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,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment