Update target/scripts/startup/variables-stack.sh

This commit is contained in:
Brennan Kinney 2025-05-13 14:44:22 +12:00 committed by GitHub
parent 77ae5554b1
commit 1dff3e9a93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -249,23 +249,27 @@ function __environment_variables_export() {
sort -o /etc/dms-settings /etc/dms-settings sort -o /etc/dms-settings /etc/dms-settings
} }
# This function reads any environment variable ending with `__FILE` from its # This function sets any environment variable with a value from a referenced file
# referenced file, then makes it available under the same name without `__FILE`. # when an equivalent ENV with a `__FILE` suffix exists with a valid file path as the value.
function __environment_variables_from_files() { function __environment_variables_from_files() {
for file_env_var in $(env | grep -Po '^.+?__FILE'); do for ENV_WITH_FILE_REF in $(env | grep -Po '^.+?__FILE'); do
local env_var="${file_env_var/__FILE/}" # Store the ENV name without the `__FILE` suffix:
local file_path="${!file_env_var}" local TARGET_ENV="${ENV_WITH_FILE_REF/__FILE/}"
# Store the value of the `__FILE` ENV:
local FILE_PATH="${!ENV_WITH_FILE_REF}"
if [[ -n "${!env_var}" ]]; then # Skip sourcing form `__FILE` if ENV is already set:
_log 'warn' "Ignoring ${env_var} since ${file_env_var} is also set" if [[ -n "${!TARGET_ENV}" ]]; then
_log 'warn' "Ignoring ${TARGET_ENV} since ${ENV_WITH_FILE_REF} is also set"
continue continue
fi fi
if [[ -f "${file_path}" ]]; then # Otherwise retrieve the value from file and set the ENV or fail if invalid reference:
_log 'info' "Getting secret ${env_var} from ${file_path}" if [[ -f "${FILE_PATH}" ]]; then
printf -v "${env_var}" '%s' "$(< "${file_path}")" _log 'info' "Getting secret ${TARGET_ENV} from ${FILE_PATH}"
printf -v "${TARGET_ENV}" '%s' "$(< "${FILE_PATH}")"
else else
_log 'error' "File ${file_path} does not exist, defined in ${file_env_var}" _log 'error' "File ${FILE_PATH} does not exist, defined in ${ENV_WITH_FILE_REF}"
fi fi
done done
} }