MAK
MAK

Reputation: 125

How to reference application settings (key vault references) in Azure Function using Powershell

I am writing a small program in Powershell which connects to Office 365 to download audit logs, make some changes and then export a CSV to an Azure Data Lake Storage account. To run this process on a schedule, I have created an Azure Function app (timer template) to run the program. To avoid hard-coded credentials, I created an Azure Key Vault to store the credential secrets. I created a managed identity in the Azure Function, created the secrets in Azure Key Vault with the credentials and then created three application settings in Azure Function under "Configuration" with the URL to point at the secrets stored in Azure Key Vault.

Key Vault References under "Configuration" in Azure Function

The three application settings are called "SecretUsername", "SecretPassword" (to point to the Office 365) and "SecretSAS" (to store the CSV in ADLS).

How do I refer to these variables in my Powershell script? I have tried different variations in my code, but none appear to work. Examples:

How do I reference the application settings in Azure Function so that I can use the stored secrets in my program?

Please assist! Many thanks!

Upvotes: 0

Views: 2355

Answers (1)

Sage Pourpre
Sage Pourpre

Reputation: 10323

To access the app settings, keyvault or not, you must retrieve it trhough : $env:APPSETTING_YourSettingName

Thus, for your keyvault referenced secret, you would access it through the following variables.

$env:APPSETTING_SecretUserName
$env:APPSETTING_SecretPassword
$env:APPSETTING_SecretSAS

And if ever you need to produce a list of them.

Get-ChildItem env:APPSETTING_*

Note, the values returned will plain text unencrypted string. Therefore, in your code, this:

 $uSecret = (Get-ChildItem ENV:SecretUsername).SecretValueText 

becomes that:

 $uSecret = $env:APPSETTING_SecretUserName

Additional note

Since it was pointed out in the comments, I'll mention it. I am not advocating the use of clear text secret in app settings at all.

App settings should be a keyvault referene for any sensitive data. I am simply stating that it can be retrieved within the function at runtime as clear-text through the $env:APPSETTING_YourSettingName variable.

Example: AppSetting name : MySecretUser AppSetting value: @Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/mysecret/ec96f02080254f109c51a1f14cdb1931) Actual secret value (In the keyvault) : I_AM_Secret

At runtime, getting the value of $env:APPSETTING_MySecretUser will return a String Object with the value I_AM_Secret

Upvotes: 3

Related Questions