diff --git a/target/scripts/startup/variables-stack.sh b/target/scripts/startup/variables-stack.sh index 54de70ae..7bcfe234 100644 --- a/target/scripts/startup/variables-stack.sh +++ b/target/scripts/startup/variables-stack.sh @@ -249,24 +249,27 @@ function __environment_variables_export() { # This function sets any environment variable with a value from a referenced file # when an equivalent ENV with a `__FILE` suffix exists with a valid file path as the value. function __environment_variables_from_files() { - for ENV_WITH_FILE_REF in $(env | grep -Po '^.+?__FILE'); do + while read -r ENV_WITH_FILE_REF; do # Store the ENV name without the `__FILE` suffix: - local TARGET_ENV="${ENV_WITH_FILE_REF/__FILE/}" + local TARGET_ENV_NAME="${ENV_WITH_FILE_REF/__FILE/}" + local -n TARGET_ENV="${ENV_WITH_FILE_REF/__FILE/}" # Store the value of the `__FILE` ENV: local FILE_PATH="${!ENV_WITH_FILE_REF}" - # Skip sourcing form `__FILE` if ENV is already set: - if [[ -n "${!TARGET_ENV}" ]]; then - _log 'warn' "ENV value will not be sourced from '${ENV_WITH_FILE_REF}' since '${TARGET_ENV}' is already set" + # Skip sourcing from `__FILE` if ENV is already set: + if [[ -v TARGET_ENV ]]; then + _log 'warn' "Ignoring '${TARGET_ENV_NAME}' since '${ENV_WITH_FILE_REF}' is also set" continue fi - # Otherwise retrieve the value from file and set the ENV or fail if invalid reference: - if [[ -f "${FILE_PATH}" ]]; then - _log 'info' "Getting secret ${TARGET_ENV} from ${FILE_PATH}" - printf -v "${TARGET_ENV}" '%s' "$(< "${FILE_PATH}")" - else - _log 'error' "File ${FILE_PATH} does not exist, defined in ${ENV_WITH_FILE_REF}" + # Otherwise, retrieve the value from a file and set + # the ENV, or fail if the reference is invalid: + if [[ ! -f ${FILE_PATH} ]]; then + _log 'warn' "File defined for secret '${TARGET_ENV_NAME}' with path '${FILE_PATH}' does not exist" + continue fi - done + + _log 'info' "Getting secret '${TARGET_ENV_NAME}' from '${FILE_PATH}'" + TARGET_ENV="$(< "${FILE_PATH}")" + done < <(env | grep -Po '^.+?__FILE') }