Skip to content

Commit 80219ae

Browse files
authored
Merge pull request #754 from motdotla/dotenv-key-option
Add failing test demonstrating need for DOTENV_KEY option
2 parents 4f48954 + dacd450 commit 80219ae

File tree

7 files changed

+52
-5
lines changed

7 files changed

+52
-5
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
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.
44

5-
## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.2.0...master)
5+
## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.3.0...master)
6+
7+
## [16.3.0](https://github.com/motdotla/dotenv/compare/v16.2.0...v16.3.0) (2023-06-16)
8+
9+
### Added
10+
11+
- Optionally pass `DOTENV_KEY` to options rather than relying on `process.env.DOTENV_KEY`. Defaults to `process.env.DOTENV_KEY` [#754](https://github.com/motdotla/dotenv/pull/754)
612

713
## [16.2.0](https://github.com/motdotla/dotenv/compare/v16.1.4...v16.2.0) (2023-06-15)
814

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ console.log(myObject) // values from .env or .env.vault live here now.
339339
console.log(process.env) // this was not changed or written to
340340
```
341341

342+
##### DOTENV_KEY
343+
344+
Default: `process.env.DOTENV_KEY`
345+
346+
Pass the `DOTENV_KEY` directly to config options. Defaults to looking for `process.env.DOTENV_KEY` environment variable. Note this only applies to decrypting `.env.vault` files. If passed as null or undefined, or not passed at all, dotenv falls back to its traditional job of parsing a `.env` file.
347+
348+
```js
349+
require('dotenv').config({ DOTENV_KEY: 'dotenv://:key_1234…@dotenv.org/vault/.env.vault?environment=production' })
350+
```
351+
342352
### Parse
343353

344354
The engine which parses the contents of your file containing environment

lib/cli-options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const re = /^dotenv_config_(encoding|path|debug|override)=(.+)$/
1+
const re = /^dotenv_config_(encoding|path|debug|override|DOTENV_KEY)=(.+)$/
22

33
module.exports = function optionMatcher (args) {
44
return args.reduce(function (acc, cur) {

lib/env-options.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ if (process.env.DOTENV_CONFIG_OVERRIDE != null) {
1717
options.override = process.env.DOTENV_CONFIG_OVERRIDE
1818
}
1919

20+
if (process.env.DOTENV_CONFIG_DOTENV_KEY != null) {
21+
options.DOTENV_KEY = process.env.DOTENV_CONFIG_DOTENV_KEY
22+
}
23+
2024
module.exports = options

lib/main.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function _parseVault (options) {
5858

5959
// handle scenario for comma separated keys - for use with key rotation
6060
// example: DOTENV_KEY="dotenv://:[email protected]/vault/.env.vault?environment=prod,dotenv://:[email protected]/vault/.env.vault?environment=prod"
61-
const keys = _dotenvKey().split(',')
61+
const keys = _dotenvKey(options).split(',')
6262
const length = keys.length
6363

6464
let decrypted
@@ -99,11 +99,18 @@ function _debug (message) {
9999
console.log(`[dotenv@${version}][DEBUG] ${message}`)
100100
}
101101

102-
function _dotenvKey () {
102+
function _dotenvKey (options) {
103+
// prioritize developer directly setting options.DOTENV_KEY
104+
if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
105+
return options.DOTENV_KEY
106+
}
107+
108+
// secondary infra already contains a DOTENV_KEY environment variable
103109
if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
104110
return process.env.DOTENV_KEY
105111
}
106112

113+
// fallback to empty string
107114
return ''
108115
}
109116

@@ -212,7 +219,7 @@ function config (options) {
212219
const vaultPath = _vaultPath(options)
213220

214221
// fallback to original dotenv if DOTENV_KEY is not set
215-
if (_dotenvKey().length === 0) {
222+
if (_dotenvKey(options).length === 0) {
216223
return DotenvModule.configDotenv(options)
217224
}
218225

tests/test-config-vault.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ t.test('does write over keys already in process.env if override turned on', ct =
194194
ct.equal(process.env.ALPHA, 'zeta')
195195
})
196196

197+
t.test('when DOTENV_KEY is passed as an option it successfully decrypts and injects', ct => {
198+
envStub.restore()
199+
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('')
200+
201+
ct.plan(2)
202+
203+
const result = dotenv.config({ path: testPath, DOTENV_KEY: dotenvKey })
204+
205+
ct.equal(result.parsed.ALPHA, 'zeta')
206+
ct.equal(process.env.ALPHA, 'zeta')
207+
208+
ct.end()
209+
})
210+
197211
t.test('can write to a different object rather than process.env', ct => {
198212
ct.plan(3)
199213

tests/test-env-options.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const e = process.env.DOTENV_CONFIG_ENCODING
99
const p = process.env.DOTENV_CONFIG_PATH
1010
const d = process.env.DOTENV_CONFIG_DEBUG
1111
const o = process.env.DOTENV_CONFIG_OVERRIDE
12+
const dk = process.env.DOTENV_CONFIG_DOTENV_KEY
1213

1314
// get fresh object for each test
1415
function options () {
@@ -30,6 +31,7 @@ delete process.env.DOTENV_CONFIG_ENCODING
3031
delete process.env.DOTENV_CONFIG_PATH
3132
delete process.env.DOTENV_CONFIG_DEBUG
3233
delete process.env.DOTENV_CONFIG_OVERRIDE
34+
delete process.env.DOTENV_CONFIG_DOTENV_KEY
3335

3436
t.same(options(), {})
3537

@@ -45,8 +47,12 @@ testOption('DOTENV_CONFIG_DEBUG', 'true', { debug: 'true' })
4547
// sets override option
4648
testOption('DOTENV_CONFIG_OVERRIDE', 'true', { override: 'true' })
4749

50+
// sets DOTENV_KEY option
51+
testOption('DOTENV_CONFIG_DOTENV_KEY', 'dotenv://:[email protected]/vault/.env.vault?environment=development', { DOTENV_KEY: 'dotenv://:[email protected]/vault/.env.vault?environment=development' })
52+
4853
// restore existing env
4954
process.env.DOTENV_CONFIG_ENCODING = e
5055
process.env.DOTENV_CONFIG_PATH = p
5156
process.env.DOTENV_CONFIG_DEBUG = d
5257
process.env.DOTENV_CONFIG_OVERRIDE = o
58+
process.env.DOTENV_CONFIG_DOTENV_KEY = dk

0 commit comments

Comments
 (0)