Skip to content

Instantly share code, notes, and snippets.

@psenger
Last active February 26, 2024 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save psenger/f7c72ed2ea7a7df6d18c8b17cd4f0b94 to your computer and use it in GitHub Desktop.
Save psenger/f7c72ed2ea7a7df6d18c8b17cd4f0b94 to your computer and use it in GitHub Desktop.
[OpenSSL Encrypted Environment Variables] #OpenSSL #TravisCi #CodeShip #AWS #Docker #Swarm
U2FsdGVkX18bL0goCbiTjHFGnkwWagZSYjhvkaU1hXA=
U2FsdGVkX19I2jmLcLYbddr8SGhfh3n/BuKY2uDmwis=
U2FsdGVkX19fH47Rl6T+HzlJiFK1ZJLXNN8sh87yN4A=

OpenSSL Encrypted Environment Variables

Both TravisCi, CodeShip, AWS, Docker Swarm and many professional platforms provide mechanisms to encrypt environment variables and pass them to the container. The key for the variables is stored in their key store mechanism.

However, if you wanted to use something universal, Good luck. I spent hours and couldn't find anything. If you find a solution please feel free to let me know, I would appreciate it.

I decided to use Open SSL with two variables. The variables are a Symmetric Encryption Key and Destination Environment value. These two variables are used to decrypt the correct env file and expand the variables into the shell. These two variables are sent to the container with the env files that are encrypted.

As you can guess, this technique has problems ( as do all techniques ):

  • The key and destiny environment values are passed as clear text to the container ( but can be encrypted with the platforms techniques ).
  • The deployment has to decrypt the values and pass them to the shell... writing a file would be bad and this makes things complicated.
  • All the environment variables ( encrypted ) have to be bundled with the deployment.

Layout and files

files .env.<ENV>.enc were is DEV, PROD, TEST are the encrypted environment files.

files <ENV>-example.sh were is DEV, PROD, TEST are test files and should be deleted.

.
|____.env.DEV.enc
|____.env.PROD.enc
|____.env.TEST.enc
|____TEST-example.sh
|____DEV-example.sh
|____PROD-example.sh

The file that is encrypted looks like this....

export X=TEST

Environment variables

for this example. you need these two.

I used the same key for all three environments ( bad idea ) ENVPASSWORD

ENV tell this script which file to decrypt.

export ENVPASSWORD=ZuCmYE2qD6UU3C2Yh9BcB9Yin
export ENV=TEST
  • Phil
# Not a good idea to leave the decrypted file in the system. You might need to see it, this is how.
openssl aes-256-cbc -d -a -in ./.env.DEV.enc -pass env:ENVPASSWORD -out ./.env
export $(openssl aes-256-cbc -d -a -in ./.env.$ENV.enc -pass env:ENVPASSWORD | xargs) && echo $X
export ENVPASSWORD=ZuCmYE2qD6UU3C2Yh9BcB9Yin
export ENV=DEV
export $(openssl aes-256-cbc -d -a -in ./.env.$ENV.enc -pass env:ENVPASSWORD | xargs)
export ENVPASSWORD=ZuCmYE2qD6UU3C2Yh9BcB9Yin
export ENV=PROD
export $(openssl aes-256-cbc -d -a -in ./.env.$ENV.enc -pass env:ENVPASSWORD | xargs)
export ENVPASSWORD=ZuCmYE2qD6UU3C2Yh9BcB9Yin
export ENV=TEST
export $(openssl aes-256-cbc -d -a -in ./.env.$ENV.enc -pass env:ENVPASSWORD | xargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment