I am trying to persist keys fetched form key vault as environment variable for a given user on linux server. The script does not work. I am not even able to see the if the variable was set temporarily in the shell.
This is my script.
#!/usr/bin/env bash
KEY_VAULT=$1
function fetch_secret_from_keyvault() {
local SECRET_NAME=$1
az keyvault secret show --vault-name "${KEY_VAULT}" --name "${SECRET_NAME}" --query "value"
}
function store_secret_from_keyvault() {
local SECRET_VAR=$1
local SECRET_NAME=$2
local SECRET_VALUE=`fetch_secret_from_keyvault "${SECRET_NAME}"`
store_secret "${SECRET_VAR}" "${SECRET_VALUE}"
}
function store_secret() {
local SECRET_VAR=$1
local SECRET_VALUE=$2
echo "export ${SECRET_VAR}=${SECRET_VALUE}"
}
echo "# ----------------------- "
echo "# Fetched the following secret from ${KEY_VAULT} on "`date`
store_secret_from_keyvault "MONGO_URI" "local-dev-mongo-uri"
I have read that export only temporarily stores the variable.
The script runs, but the variables are not set at the end. I would like to see them when executing
printenv
exportcommand makes it available in the shell process running your script and in subprocesses of that shell (i.e. commands, other scripts etc run from that script), but not outside that process (i.e. to whatever process ran the script). Printingexportas you're currently doing, doesn't actually make it available anywhere (unless something else catches the output and executes it?). Where are you trying to make that variable available?