Skip to content

Instantly share code, notes, and snippets.

@jnovack
Created April 25, 2018 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnovack/da459ea08fc9f4202b69e5b66baac6e5 to your computer and use it in GitHub Desktop.
Save jnovack/da459ea08fc9f4202b69e5b66baac6e5 to your computer and use it in GitHub Desktop.
Variables of Variables in /bin/sh
#!/bin/sh
file_env() {
local envVar="$1"
local fileVar="${envVar}_FILE"
eval envVarContents="\$${envVar}"
eval fileVarContents="\$${fileVar}"
if [ ! -z "$envVarContents" ] && [ ! -z "$fileVarContents" ]; then
echo >&2 "error: both $envVar and $fileVar are set (but are exclusive)"
exit 1
fi
local val
if [ ! -z "${envVarContents}" ]; then
val="${envVarContents}"
elif [ ! -z "${fileVarContents}" ]; then
val=`cat ${fileVarContents}`
elif [ ! -z "$2" ]; then
val=$2
fi
export "$envVar"="$val"
unset "$fileVar"
}
###########################
## Example Script Use
###########################
file_env 'TEST' foo
echo $TEST
###########################
## Example Command Line Use
###########################
# $ echo 'hi' > /myenv
# $ ./file_env.sh
# foo
# $ TEST=asdf ./file_env.sh
# asdf
# $ TEST_FILE=/myenv ./file_env.sh
# hi
@jnovack
Copy link
Author

jnovack commented Apr 25, 2018

I use this to get non-critical Docker Secrets into environment variables.

docker exec can be controlled by authz plugins, that is what DDC does for example. Writing to environment variables is writing the clear text to a place that the system has no real control over. The whole point is to keep secrets in only one single in memory controlled place, with as restricted access as possible. Keeping secrets out of environment variables is a deliberate design decision and is not going to be reversed.
-- justincormack commented on Feb 10, 2017

Example:

  • The first-run of a new mariadb image to specify MYSQL_ROOT_PASSWORD_FILE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment