@dotenvx/dotenvx 0.4.0 → 0.4.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +28 -0
- package/README.md +225 -251
- package/bin/dotenvx.cjs +34 -0
- package/config.cjs +1 -0
- package/config.d.ts +1 -0
- package/config.mjs +2 -0
- package/index.cjs +141 -0
- package/index.d.ts +66 -0
- package/index.mjs +3 -0
- package/lib/cli.cjs +43 -0
- package/lib/native-package.cjs +21 -0
- package/lib/platforms.cjs +18 -0
- package/lib/redactOutput.cjs +72 -0
- package/lib/redaction.cjs +64 -0
- package/lib/validateOptions.cjs +57 -0
- package/native.cjs +23 -0
- package/package.json +63 -36
- package/src/cli/dotenvx.js +0 -218
- package/src/cli/helpers.js +0 -147
- package/src/lib/main.js +0 -63
- package/src/shared/logger.js +0 -30
- package/src/shared/packageJson.js +0 -8
package/src/cli/dotenvx.js
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const fs = require('fs')
|
|
4
|
-
const { Command } = require('commander')
|
|
5
|
-
const dotenv = require('dotenv')
|
|
6
|
-
const program = new Command()
|
|
7
|
-
|
|
8
|
-
// constants
|
|
9
|
-
const ENCODING = 'utf8'
|
|
10
|
-
|
|
11
|
-
const logger = require('./../shared/logger')
|
|
12
|
-
const helpers = require('./helpers')
|
|
13
|
-
const packageJson = require('./../shared/packageJson')
|
|
14
|
-
const main = require('./../lib/main')
|
|
15
|
-
|
|
16
|
-
// global log levels
|
|
17
|
-
program
|
|
18
|
-
.option('-l, --log-level <level>', 'set log level', 'info')
|
|
19
|
-
.option('-q, --quiet', 'sets log level to error')
|
|
20
|
-
.option('-v, --verbose', 'sets log level to verbose')
|
|
21
|
-
.option('-d, --debug', 'sets log level to debug')
|
|
22
|
-
.hook('preAction', (thisCommand, actionCommand) => {
|
|
23
|
-
const options = thisCommand.opts()
|
|
24
|
-
|
|
25
|
-
if (options.logLevel) {
|
|
26
|
-
logger.level = options.logLevel
|
|
27
|
-
logger.debug(`setting log level to ${options.logLevel}`)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// --quiet overides --log-level. only errors will be shown
|
|
31
|
-
if (options.quiet) {
|
|
32
|
-
logger.level = 'error'
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// --verbose overrides --quiet
|
|
36
|
-
if (options.verbose) {
|
|
37
|
-
logger.level = 'verbose'
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// --debug overrides --verbose
|
|
41
|
-
if (options.debug) {
|
|
42
|
-
logger.level = 'debug'
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
// cli
|
|
47
|
-
program
|
|
48
|
-
.name(packageJson.name)
|
|
49
|
-
.description(packageJson.description)
|
|
50
|
-
.version(packageJson.version)
|
|
51
|
-
|
|
52
|
-
// dotenvx run -- node index.js
|
|
53
|
-
program.command('run')
|
|
54
|
-
.description('inject env variables into your application process')
|
|
55
|
-
.option('-f, --env-file <paths...>', 'path(s) to your env file(s)', '.env')
|
|
56
|
-
.option('-o, --overload', 'override existing env variables')
|
|
57
|
-
.action(function () {
|
|
58
|
-
const options = this.opts()
|
|
59
|
-
logger.debug('configuring options')
|
|
60
|
-
logger.debug(options)
|
|
61
|
-
|
|
62
|
-
// convert to array if needed
|
|
63
|
-
let optionEnvFile = options.envFile
|
|
64
|
-
if (!Array.isArray(optionEnvFile)) {
|
|
65
|
-
optionEnvFile = [optionEnvFile]
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const env = {}
|
|
69
|
-
const readableFilepaths = new Set()
|
|
70
|
-
const written = new Set()
|
|
71
|
-
|
|
72
|
-
for (const envFilepath of optionEnvFile) {
|
|
73
|
-
const filepath = helpers.resolvePath(envFilepath)
|
|
74
|
-
|
|
75
|
-
logger.verbose(`injecting env from ${filepath}`)
|
|
76
|
-
|
|
77
|
-
try {
|
|
78
|
-
logger.debug(`reading env from ${filepath}`)
|
|
79
|
-
const src = fs.readFileSync(filepath, { encoding: ENCODING })
|
|
80
|
-
|
|
81
|
-
logger.debug(`parsing env from ${filepath}`)
|
|
82
|
-
const parsed = main.parse(src)
|
|
83
|
-
|
|
84
|
-
logger.debug(`writing env from ${filepath}`)
|
|
85
|
-
const result = main.write(process.env, parsed, options.overload)
|
|
86
|
-
|
|
87
|
-
readableFilepaths.add(envFilepath)
|
|
88
|
-
result.written.forEach(key => written.add(key))
|
|
89
|
-
} catch (e) {
|
|
90
|
-
logger.warn(e)
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (readableFilepaths.size > 0) {
|
|
95
|
-
logger.info(`injecting ${written.size} environment ${helpers.pluralize('variable', written.size)} from ${[...readableFilepaths]}`)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Extract command and arguments after '--'
|
|
99
|
-
const commandIndex = process.argv.indexOf('--')
|
|
100
|
-
if (commandIndex === -1 || commandIndex === process.argv.length - 1) {
|
|
101
|
-
logger.error('at least one argument is required after the run command, received 0.')
|
|
102
|
-
process.exit(1)
|
|
103
|
-
} else {
|
|
104
|
-
const subCommand = process.argv.slice(commandIndex + 1)
|
|
105
|
-
|
|
106
|
-
helpers.executeCommand(subCommand, env)
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
// dotenvx encrypt
|
|
111
|
-
program.command('encrypt')
|
|
112
|
-
.description('encrypt .env.* to .env.vault')
|
|
113
|
-
.option('-f, --env-file <paths...>', 'path(s) to your env file(s)', helpers.findEnvFiles('./'))
|
|
114
|
-
.action(function () {
|
|
115
|
-
const options = this.opts()
|
|
116
|
-
logger.debug('configuring options')
|
|
117
|
-
logger.debug(options)
|
|
118
|
-
|
|
119
|
-
let optionEnvFile = options.envFile
|
|
120
|
-
if (!Array.isArray(optionEnvFile)) {
|
|
121
|
-
optionEnvFile = [optionEnvFile]
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
try {
|
|
125
|
-
logger.verbose(`generating .env.keys from ${optionEnvFile}`)
|
|
126
|
-
|
|
127
|
-
const dotenvKeys = (dotenv.configDotenv({ path: '.env.keys' }).parsed || {})
|
|
128
|
-
|
|
129
|
-
for (const envFilepath of optionEnvFile) {
|
|
130
|
-
const filepath = helpers.resolvePath(envFilepath)
|
|
131
|
-
if (!fs.existsSync(filepath)) {
|
|
132
|
-
throw new Error(`file does not exist: ${filepath}`)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const environment = helpers.guessEnvironment(filepath)
|
|
136
|
-
const key = `DOTENV_KEY_${environment.toUpperCase()}`
|
|
137
|
-
|
|
138
|
-
let value = dotenvKeys[key]
|
|
139
|
-
|
|
140
|
-
// first time seeing new DOTENV_KEY_${environment}
|
|
141
|
-
if (!value || value.length === 0) {
|
|
142
|
-
logger.verbose(`generating ${key}`)
|
|
143
|
-
value = helpers.generateDotenvKey(environment)
|
|
144
|
-
logger.debug(`generating ${key} as ${value}`)
|
|
145
|
-
|
|
146
|
-
dotenvKeys[key] = value
|
|
147
|
-
} else {
|
|
148
|
-
logger.verbose(`existing ${key}`)
|
|
149
|
-
logger.debug(`existing ${key} as ${value}`)
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
let keysData = `#/!!!!!!!!!!!!!!!!!!!.env.keys!!!!!!!!!!!!!!!!!!!!!!/
|
|
154
|
-
#/ DOTENV_KEYs. DO NOT commit to source control /
|
|
155
|
-
#/ [how it works](https://dotenv.org/env-keys) /
|
|
156
|
-
#/--------------------------------------------------/\n`
|
|
157
|
-
|
|
158
|
-
for (const key in dotenvKeys) {
|
|
159
|
-
const value = dotenvKeys[key]
|
|
160
|
-
keysData += `${key}="${value}"\n`
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
fs.writeFileSync('.env.keys', keysData)
|
|
164
|
-
} catch (e) {
|
|
165
|
-
logger.error(e)
|
|
166
|
-
process.exit(1)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
try {
|
|
170
|
-
logger.verbose(`generating .env.vault from ${optionEnvFile}`)
|
|
171
|
-
|
|
172
|
-
const dotenvKeys = (dotenv.configDotenv({ path: '.env.keys' }).parsed || {})
|
|
173
|
-
const dotenvVaults = (dotenv.configDotenv({ path: '.env.vault' }).parsed || {})
|
|
174
|
-
|
|
175
|
-
for (const envFilepath of optionEnvFile) {
|
|
176
|
-
const filepath = helpers.resolvePath(envFilepath)
|
|
177
|
-
const environment = helpers.guessEnvironment(filepath)
|
|
178
|
-
const vault = `DOTENV_VAULT_${environment.toUpperCase()}`
|
|
179
|
-
|
|
180
|
-
let ciphertext = dotenvVaults[vault]
|
|
181
|
-
const dotenvKey = dotenvKeys[`DOTENV_KEY_${environment.toUpperCase()}`]
|
|
182
|
-
|
|
183
|
-
if (!ciphertext || ciphertext.length === 0 || helpers.changed(ciphertext, dotenvKey, filepath, ENCODING)) {
|
|
184
|
-
logger.verbose(`encrypting ${vault}`)
|
|
185
|
-
ciphertext = helpers.encryptFile(filepath, dotenvKey, ENCODING)
|
|
186
|
-
logger.verbose(`encrypting ${vault} as ${ciphertext}`)
|
|
187
|
-
|
|
188
|
-
dotenvVaults[vault] = ciphertext
|
|
189
|
-
} else {
|
|
190
|
-
logger.verbose(`existing ${vault}`)
|
|
191
|
-
logger.debug(`existing ${vault} as ${ciphertext}`)
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
let vaultData = `#/-------------------.env.vault---------------------/
|
|
196
|
-
#/ cloud-agnostic vaulting standard /
|
|
197
|
-
#/ [how it works](https://dotenv.org/env-vault) /
|
|
198
|
-
#/--------------------------------------------------/\n\n`
|
|
199
|
-
|
|
200
|
-
for (const vault in dotenvVaults) {
|
|
201
|
-
const value = dotenvVaults[vault]
|
|
202
|
-
const environment = vault.replace('DOTENV_VAULT_', '').toLowerCase()
|
|
203
|
-
vaultData += `# ${environment}\n`
|
|
204
|
-
vaultData += `${vault}="${value}"\n\n`
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
fs.writeFileSync('.env.vault', vaultData)
|
|
208
|
-
} catch (e) {
|
|
209
|
-
logger.error(e)
|
|
210
|
-
process.exit(1)
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
logger.info(`encrypted ${optionEnvFile} to .env.vault`)
|
|
214
|
-
|
|
215
|
-
// logger.info(`encrypting`)
|
|
216
|
-
})
|
|
217
|
-
|
|
218
|
-
program.parse(process.argv)
|
package/src/cli/helpers.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
const fs = require('fs')
|
|
2
|
-
const path = require('path')
|
|
3
|
-
const crypto = require('crypto')
|
|
4
|
-
const { spawn } = require('child_process')
|
|
5
|
-
const xxhash = require('xxhashjs')
|
|
6
|
-
const XXHASH_SEED = 0xABCD
|
|
7
|
-
|
|
8
|
-
const main = require('./../lib/main')
|
|
9
|
-
|
|
10
|
-
const RESERVED_ENV_FILES = ['.env.vault', '.env.projects', '.env.keys', '.env.me', '.env.x']
|
|
11
|
-
|
|
12
|
-
// resolve path based on current running process location
|
|
13
|
-
const resolvePath = function (filepath) {
|
|
14
|
-
return path.resolve(process.cwd(), filepath)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const executeCommand = function (subCommand, env) {
|
|
18
|
-
const subprocess = spawn(subCommand[0], subCommand.slice(1), {
|
|
19
|
-
stdio: 'inherit',
|
|
20
|
-
shell: true,
|
|
21
|
-
env: { ...process.env, ...env }
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
subprocess.on('close', (code) => {
|
|
25
|
-
process.exit(code)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
subprocess.on('error', (_err) => {
|
|
29
|
-
process.exit(1)
|
|
30
|
-
})
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const pluralize = function (word, count) {
|
|
34
|
-
// simple pluralization: add 's' at the end
|
|
35
|
-
if (count === 0 || count > 1) {
|
|
36
|
-
return word + 's'
|
|
37
|
-
} else {
|
|
38
|
-
return word
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const findEnvFiles = function (directory) {
|
|
43
|
-
const files = fs.readdirSync(directory)
|
|
44
|
-
|
|
45
|
-
const envFiles = files.filter(file =>
|
|
46
|
-
file.startsWith('.env') &&
|
|
47
|
-
!file.endsWith('.previous') &&
|
|
48
|
-
!RESERVED_ENV_FILES.includes(file)
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
return envFiles
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const guessEnvironment = function (file) {
|
|
55
|
-
const splitFile = file.split('.')
|
|
56
|
-
const possibleEnvironment = splitFile[2] // ['', 'env', environment']
|
|
57
|
-
|
|
58
|
-
if (!possibleEnvironment || possibleEnvironment.length === 0) {
|
|
59
|
-
return 'development'
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return possibleEnvironment
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const generateDotenvKey = function (environment) {
|
|
66
|
-
const rand = crypto.randomBytes(32).toString('hex')
|
|
67
|
-
|
|
68
|
-
return `dotenv://:key_${rand}@dotenvx.com/vault/.env.vault?environment=${environment.toLowerCase()}`
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const encryptFile = function (filepath, dotenvKey, encoding) {
|
|
72
|
-
const key = this._parseEncryptionKeyFromDotenvKey(dotenvKey)
|
|
73
|
-
const message = fs.readFileSync(filepath, encoding)
|
|
74
|
-
|
|
75
|
-
const ciphertext = this.encrypt(key, message)
|
|
76
|
-
|
|
77
|
-
return ciphertext
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const encrypt = function (key, message) {
|
|
81
|
-
// set up nonce
|
|
82
|
-
const nonce = this._generateNonce()
|
|
83
|
-
|
|
84
|
-
// set up cipher
|
|
85
|
-
const cipher = crypto.createCipheriv('aes-256-gcm', key, nonce)
|
|
86
|
-
|
|
87
|
-
// generate ciphertext
|
|
88
|
-
let ciphertext = ''
|
|
89
|
-
ciphertext += cipher.update(message, 'utf8', 'hex')
|
|
90
|
-
ciphertext += cipher.final('hex')
|
|
91
|
-
ciphertext += cipher.getAuthTag().toString('hex')
|
|
92
|
-
|
|
93
|
-
// prepend nonce
|
|
94
|
-
ciphertext = nonce.toString('hex') + ciphertext
|
|
95
|
-
|
|
96
|
-
// base64 encode output
|
|
97
|
-
return Buffer.from(ciphertext, 'hex').toString('base64')
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const changed = function (ciphertext, dotenvKey, filepath, encoding) {
|
|
101
|
-
const key = this._parseEncryptionKeyFromDotenvKey(dotenvKey)
|
|
102
|
-
const decrypted = main.decrypt(ciphertext, key)
|
|
103
|
-
const raw = fs.readFileSync(filepath, encoding)
|
|
104
|
-
|
|
105
|
-
return this.hash(decrypted) !== this.hash(raw)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const hash = function (str) {
|
|
109
|
-
return xxhash.h32(str, XXHASH_SEED).toString(16)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const _parseEncryptionKeyFromDotenvKey = function (dotenvKey) {
|
|
113
|
-
// Parse DOTENV_KEY. Format is a URI
|
|
114
|
-
const uri = new URL(dotenvKey)
|
|
115
|
-
|
|
116
|
-
// Get decrypt key
|
|
117
|
-
const key = uri.password
|
|
118
|
-
if (!key) {
|
|
119
|
-
throw new Error('INVALID_DOTENV_KEY: Missing key part')
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return Buffer.from(key.slice(-64), 'hex')
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const _generateNonce = function () {
|
|
126
|
-
return crypto.randomBytes(this._nonceBytes())
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const _nonceBytes = function () {
|
|
130
|
-
return 12
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
module.exports = {
|
|
134
|
-
resolvePath,
|
|
135
|
-
executeCommand,
|
|
136
|
-
pluralize,
|
|
137
|
-
findEnvFiles,
|
|
138
|
-
guessEnvironment,
|
|
139
|
-
generateDotenvKey,
|
|
140
|
-
encryptFile,
|
|
141
|
-
encrypt,
|
|
142
|
-
changed,
|
|
143
|
-
hash,
|
|
144
|
-
_parseEncryptionKeyFromDotenvKey,
|
|
145
|
-
_generateNonce,
|
|
146
|
-
_nonceBytes
|
|
147
|
-
}
|
package/src/lib/main.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
const logger = require('./../shared/logger')
|
|
2
|
-
const dotenv = require('dotenv')
|
|
3
|
-
|
|
4
|
-
const config = function (options) {
|
|
5
|
-
return dotenv.config(options)
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const decrypt = function (encrypted, keyStr) {
|
|
9
|
-
return dotenv.decrypt(encrypted, keyStr)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const parse = function (src) {
|
|
13
|
-
const result = dotenv.parse(src)
|
|
14
|
-
|
|
15
|
-
logger.debug(result)
|
|
16
|
-
|
|
17
|
-
return result
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const write = function (processEnv = {}, parsed = {}, overload = false) {
|
|
21
|
-
if (typeof parsed !== 'object') {
|
|
22
|
-
throw new Error('OBJECT_REQUIRED: Please check the parsed argument being passed to write')
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const written = new Set()
|
|
26
|
-
const preExisting = new Set()
|
|
27
|
-
|
|
28
|
-
// set processEnv
|
|
29
|
-
for (const key of Object.keys(parsed)) {
|
|
30
|
-
if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
|
|
31
|
-
if (overload === true) {
|
|
32
|
-
processEnv[key] = parsed[key]
|
|
33
|
-
written.add(key)
|
|
34
|
-
|
|
35
|
-
logger.verbose(`${key} set`)
|
|
36
|
-
logger.debug(`${key} set to ${parsed[key]}`)
|
|
37
|
-
} else {
|
|
38
|
-
preExisting.add(key)
|
|
39
|
-
|
|
40
|
-
logger.verbose(`${key} pre-exists`)
|
|
41
|
-
logger.debug(`${key} pre-exists as ${processEnv[key]}`)
|
|
42
|
-
}
|
|
43
|
-
} else {
|
|
44
|
-
processEnv[key] = parsed[key]
|
|
45
|
-
written.add(key)
|
|
46
|
-
|
|
47
|
-
logger.verbose(`${key} set`)
|
|
48
|
-
logger.debug(`${key} set to ${parsed[key]}`)
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
written,
|
|
54
|
-
preExisting
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
module.exports = {
|
|
59
|
-
config,
|
|
60
|
-
decrypt,
|
|
61
|
-
parse,
|
|
62
|
-
write
|
|
63
|
-
}
|
package/src/shared/logger.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
const winston = require('winston')
|
|
2
|
-
|
|
3
|
-
const printf = winston.format.printf
|
|
4
|
-
const combine = winston.format.combine
|
|
5
|
-
const createLogger = winston.createLogger
|
|
6
|
-
const transports = winston.transports
|
|
7
|
-
|
|
8
|
-
const packageJson = require('./packageJson')
|
|
9
|
-
|
|
10
|
-
function pad (word) {
|
|
11
|
-
return word.padEnd(9, ' ')
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const dotenvxFormat = printf(({ level, message, label, timestamp }) => {
|
|
15
|
-
const formattedMessage = typeof message === 'object' ? JSON.stringify(message) : message
|
|
16
|
-
|
|
17
|
-
return `[dotenvx@${packageJson.version}]${pad(`[${level.toUpperCase()}]`)} ${formattedMessage}`
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
const logger = createLogger({
|
|
21
|
-
level: 'info',
|
|
22
|
-
format: combine(
|
|
23
|
-
dotenvxFormat
|
|
24
|
-
),
|
|
25
|
-
transports: [
|
|
26
|
-
new transports.Console()
|
|
27
|
-
]
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
module.exports = logger
|