From 96c29b4a28e675d2d2dc5b7dae8316c2e175b418 Mon Sep 17 00:00:00 2001 From: Scott Motte Date: Thu, 15 Jun 2023 16:38:32 -0700 Subject: [PATCH 1/3] Add ability to change the target from process.env to your own object --- lib/main.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 367d02e7..20d71dff 100644 --- a/lib/main.js +++ b/lib/main.js @@ -162,7 +162,14 @@ function _configVault (options) { const parsed = DotenvModule._parseVault(options) - DotenvModule.populate(process.env, parsed, options) + let processEnv = process.env + if (options) { + if (options.processEnv != null) { + processEnv = options.processEnv + } + } + + DotenvModule.populate(processEnv, parsed, options) return { parsed } } @@ -185,7 +192,14 @@ function configDotenv (options) { // Specifying an encoding returns a string instead of a buffer const parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding })) - DotenvModule.populate(process.env, parsed, options) + let processEnv = process.env + if (options) { + if (options.processEnv != null) { + processEnv = options.processEnv + } + } + + DotenvModule.populate(processEnv, parsed, options) return { parsed } } catch (e) { From 431521d2193c2c4f5afae5eeed6b53c3fb3bc55e Mon Sep 17 00:00:00 2001 From: Scott Motte Date: Thu, 15 Jun 2023 21:34:10 -0700 Subject: [PATCH 2/3] Update CHANGELOG and README --- CHANGELOG.md | 5 ++++- README.md | 37 +++++++++++++++++++++++++++++++++---- lib/main.js | 12 ++++-------- tests/test-config-vault.js | 15 +++++++++++++++ tests/test-config.js | 14 ++++++++++++++ 5 files changed, 70 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abd5cd41..bfd140c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. -## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.1.0...master) +## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.2.0...master) + +## [16.2.0](https://github.com/motdotla/dotenv/compare/v16.1.4...v16.2.0) (2023-06-15) ### Added +- Optionally write to your own target object rather than `process.env`. Defaults to `process.env`. [#753](https://github.com/motdotla/dotenv/pull/753) - Add import type URL to types file [#751](https://github.com/motdotla/dotenv/pull/751) ## [16.1.4](https://github.com/motdotla/dotenv/compare/v16.1.3...v16.1.4) (2023-06-04) diff --git a/README.md b/README.md index 13f7e05a..9fa9ee23 100644 --- a/README.md +++ b/README.md @@ -283,7 +283,7 @@ You can additionally, pass options to `config`. #### Options -##### Path +##### path Default: `path.resolve(process.cwd(), '.env')` @@ -293,7 +293,7 @@ Specify a custom path if your file containing environment variables is located e require('dotenv').config({ path: '/custom/path/to/.env' }) ``` -##### Encoding +##### encoding Default: `utf8` @@ -303,7 +303,7 @@ Specify the encoding of your file containing environment variables. require('dotenv').config({ encoding: 'latin1' }) ``` -##### Debug +##### debug Default: `false` @@ -313,7 +313,7 @@ Turn on logging to help debug why certain keys or values are not being set as yo require('dotenv').config({ debug: process.env.DEBUG }) ``` -##### Override +##### override Default: `false` @@ -323,6 +323,19 @@ Override any environment variables that have already been set on your machine wi require('dotenv').config({ override: true }) ``` +##### processEnv + +Default: `process.env` + +Specify an object to write your secrets to. Defaults to `process.env` environment variables. + +```js +const myObject = {} +require('dotenv').config({ processEnv: myObject }) + +// myObject will receive the values from your .env file rather than process.env +``` + ### Parse The engine which parses the contents of your file containing environment @@ -393,6 +406,22 @@ Default: `false` Override any environment variables that have already been set. +### Decrypt + +The engine which decrypts the ciphertext contents of your .env.vault file is available for use. It accepts a ciphertext and a decryption key. It uses AES-256-GCM encryption. + +For example, decrypting a simple ciphertext: + +```js +const dotenv = require('dotenv') +const ciphertext = 's7NYXa809k/bVSPwIAmJhPJmEGTtU0hG58hOZy7I0ix6y5HP8LsHBsZCYC/gw5DDFy5DgOcyd18R' +const decryptionKey = 'ddcaa26504cd70a6fef9801901c3981538563a1767c297cb8416e8a38c62fe00' + +const decrypted = dotenv.decrypt(ciphertext, decryptionKey) + +console.log(decrypted) // # development@v6\nALPHA="zeta" +``` + ## ❓ FAQ ### Why is the `.env` file not loading my environment variables successfully? diff --git a/lib/main.js b/lib/main.js index 20d71dff..69cb939f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -163,10 +163,8 @@ function _configVault (options) { const parsed = DotenvModule._parseVault(options) let processEnv = process.env - if (options) { - if (options.processEnv != null) { - processEnv = options.processEnv - } + if (options && options.processEnv != null) { + processEnv = options.processEnv } DotenvModule.populate(processEnv, parsed, options) @@ -193,10 +191,8 @@ function configDotenv (options) { const parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding })) let processEnv = process.env - if (options) { - if (options.processEnv != null) { - processEnv = options.processEnv - } + if (options && options.processEnv != null) { + processEnv = options.processEnv } DotenvModule.populate(processEnv, parsed, options) diff --git a/tests/test-config-vault.js b/tests/test-config-vault.js index 16d3e774..d8cd313c 100644 --- a/tests/test-config-vault.js +++ b/tests/test-config-vault.js @@ -194,6 +194,21 @@ t.test('does write over keys already in process.env if override turned on', ct = ct.equal(process.env.ALPHA, 'zeta') }) +t.test('can write to a different object rather than process.env', ct => { + ct.plan(3) + + process.env.ALPHA = 'other' // reset process.env + + logStub = sinon.stub(console, 'log') + + const myObject = {} + + const result = dotenv.config({ path: testPath, processEnv: myObject }) + ct.equal(result.parsed.ALPHA, 'zeta') + ct.equal(process.env.ALPHA, 'other') + ct.equal(myObject.ALPHA, 'zeta') +}) + t.test('logs when debug and override are turned on', ct => { ct.plan(1) diff --git a/tests/test-config.js b/tests/test-config.js index ea390a5e..65ef1433 100644 --- a/tests/test-config.js +++ b/tests/test-config.js @@ -135,6 +135,20 @@ t.test( } ) +t.test('can write to a different object rather than process.env', ct => { + ct.plan(3) + + process.env.test = 'other' // reset process.env + + const myObject = {} + const env = dotenv.config({ processEnv: myObject }) + + ct.equal(env.parsed && env.parsed.test, mockParseResponse.test) + console.log('logging', process.env.test) + ct.equal(process.env.test, 'other') + ct.equal(myObject.test, mockParseResponse.test) +}) + t.test('returns parsed object', ct => { ct.plan(2) From ea8db8cb6d321e054ba657d24e893ca9cc91d2e6 Mon Sep 17 00:00:00 2001 From: Scott Motte Date: Thu, 15 Jun 2023 21:36:43 -0700 Subject: [PATCH 3/3] update README --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9fa9ee23..f9a60045 100644 --- a/README.md +++ b/README.md @@ -333,7 +333,8 @@ Specify an object to write your secrets to. Defaults to `process.env` environmen const myObject = {} require('dotenv').config({ processEnv: myObject }) -// myObject will receive the values from your .env file rather than process.env +console.log(myObject) // values from .env or .env.vault live here now. +console.log(process.env) // this was not changed or written to ``` ### Parse @@ -351,7 +352,7 @@ console.log(typeof config, config) // object { BASIC : 'basic' } #### Options -##### Debug +##### debug Default: `false` @@ -392,7 +393,7 @@ dotenv.populate(target, parsed, { override: true, debug: true }) console.log(target) // { HELLO: 'universe' } ``` -#### Options +#### options ##### Debug @@ -400,7 +401,7 @@ Default: `false` Turn on logging to help debug why certain keys or values are not being populated as you expect. -##### Override +##### override Default: `false`