Skip to content

Commit

Permalink
Add options.DOTENV_KEY
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Jun 16, 2023
1 parent 5861f6a commit f20e646
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
13 changes: 10 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function _parseVault (options) {

// handle scenario for comma separated keys - for use with key rotation
// example: DOTENV_KEY="dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=prod,dotenv://:key_7890@dotenv.org/vault/.env.vault?environment=prod"
const keys = _dotenvKey().split(',')
const keys = _dotenvKey(options).split(',')
const length = keys.length

let decrypted
Expand Down Expand Up @@ -99,11 +99,18 @@ function _debug (message) {
console.log(`[dotenv@${version}][DEBUG] ${message}`)
}

function _dotenvKey () {
function _dotenvKey (options) {
// prioritize developer directly setting options.DOTENV_KEY
if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
return options.DOTENV_KEY
}

// secondary infra already contains a DOTENV_KEY environment variable
if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
return process.env.DOTENV_KEY
}

// fallback to empty string
return ''
}

Expand Down Expand Up @@ -212,7 +219,7 @@ function config (options) {
const vaultPath = _vaultPath(options)

// fallback to original dotenv if DOTENV_KEY is not set
if (_dotenvKey().length === 0) {
if (_dotenvKey(options).length === 0) {
return DotenvModule.configDotenv(options)
}

Expand Down
28 changes: 14 additions & 14 deletions tests/test-config-vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,6 @@ t.test('when DOTENV_KEY is empty string falls back to .env file', ct => {
ct.end()
})

t.test('when DOTENV_KEY is passed as an option it successfully decrypts and injects', ct => {
envStub.restore()
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('')

ct.plan(1)

const result = dotenv.config({ path: testPath, DOTENV_KEY: dotenvKey })

ct.equal(result.parsed.ALPHA, 'zeta')
ct.equal(process.env.ALPHA, 'bar')

ct.end()
})

t.test('does not write over keys already in process.env by default', ct => {
ct.plan(2)

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

t.test('when DOTENV_KEY is passed as an option it successfully decrypts and injects', ct => {
envStub.restore()
envStub = sinon.stub(process.env, 'DOTENV_KEY').value('')

ct.plan(2)

const result = dotenv.config({ path: testPath, DOTENV_KEY: dotenvKey })

ct.equal(result.parsed.ALPHA, 'zeta')
ct.equal(process.env.ALPHA, 'zeta')

ct.end()
})

t.test('can write to a different object rather than process.env', ct => {
ct.plan(3)

Expand Down

0 comments on commit f20e646

Please sign in to comment.