Skip to content

Commit f20e646

Browse files
committed
Add options.DOTENV_KEY
1 parent 5861f6a commit f20e646

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

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 & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,6 @@ t.test('when DOTENV_KEY is empty string falls back to .env file', ct => {
170170
ct.end()
171171
})
172172

173-
t.test('when DOTENV_KEY is passed as an option it successfully decrypts and injects', ct => {
174-
envStub.restore()
175-
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('')
176-
177-
ct.plan(1)
178-
179-
const result = dotenv.config({ path: testPath, DOTENV_KEY: dotenvKey })
180-
181-
ct.equal(result.parsed.ALPHA, 'zeta')
182-
ct.equal(process.env.ALPHA, 'bar')
183-
184-
ct.end()
185-
})
186-
187173
t.test('does not write over keys already in process.env by default', ct => {
188174
ct.plan(2)
189175

@@ -208,6 +194,20 @@ t.test('does write over keys already in process.env if override turned on', ct =
208194
ct.equal(process.env.ALPHA, 'zeta')
209195
})
210196

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+
211211
t.test('can write to a different object rather than process.env', ct => {
212212
ct.plan(3)
213213

0 commit comments

Comments
 (0)