Skip to content

Instantly share code, notes, and snippets.

@lcamilo15
Forked from psenger/.env.DEV.enc
Created February 26, 2024 05:11
Show Gist options
  • Select an option

  • Save lcamilo15/bc6b6f8dca9fa27c5641d20006bc7e1d to your computer and use it in GitHub Desktop.

Select an option

Save lcamilo15/bc6b6f8dca9fa27c5641d20006bc7e1d to your computer and use it in GitHub Desktop.

Revisions

  1. @psenger psenger revised this gist Feb 1, 2020. No changes.
  2. @psenger psenger created this gist Jan 5, 2018.
    1 change: 1 addition & 0 deletions .env.DEV.enc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    U2FsdGVkX18bL0goCbiTjHFGnkwWagZSYjhvkaU1hXA=
    1 change: 1 addition & 0 deletions .env.PROD.enc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    U2FsdGVkX19I2jmLcLYbddr8SGhfh3n/BuKY2uDmwis=
    1 change: 1 addition & 0 deletions .env.TEST.enc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    U2FsdGVkX19fH47Rl6T+HzlJiFK1ZJLXNN8sh87yN4A=
    3 changes: 3 additions & 0 deletions DEV-example.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    export ENVPASSWORD=ZuCmYE2qD6UU3C2Yh9BcB9Yin
    export ENV=DEV
    export $(openssl aes-256-cbc -d -a -in ./.env.$ENV.enc -pass env:ENVPASSWORD | xargs)
    3 changes: 3 additions & 0 deletions PROD-example.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    export ENVPASSWORD=ZuCmYE2qD6UU3C2Yh9BcB9Yin
    export ENV=PROD
    export $(openssl aes-256-cbc -d -a -in ./.env.$ENV.enc -pass env:ENVPASSWORD | xargs)
    48 changes: 48 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    # 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 <ENV> is ``DEV``, ``PROD``, ``TEST`` are the encrypted environment files.

    files ```<ENV>-example.sh``` were <ENV> 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
    3 changes: 3 additions & 0 deletions TEST-example.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    export ENVPASSWORD=ZuCmYE2qD6UU3C2Yh9BcB9Yin
    export ENV=TEST
    export $(openssl aes-256-cbc -d -a -in ./.env.$ENV.enc -pass env:ENVPASSWORD | xargs)
    5 changes: 5 additions & 0 deletions decrypt.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # 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