@dotenvx/dotenvx 1.55.0 → 1.56.0

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +18 -1
  2. package/package.json +3 -3
  3. package/src/cli/actions/encrypt.js +0 -3
  4. package/src/cli/actions/get.js +4 -2
  5. package/src/cli/actions/keypair.js +3 -2
  6. package/src/cli/actions/rotate.js +10 -5
  7. package/src/cli/actions/run.js +1 -14
  8. package/src/cli/actions/set.js +3 -2
  9. package/src/cli/commands/ext.js +0 -3
  10. package/src/cli/dotenvx.js +12 -9
  11. package/src/db/session.js +21 -0
  12. package/src/lib/extensions/ops.js +98 -0
  13. package/src/lib/helpers/buildEnvs.js +2 -15
  14. package/src/lib/helpers/{decryptKeyValue.js → cryptography/decryptKeyValue.js} +1 -1
  15. package/src/lib/helpers/cryptography/index.js +14 -0
  16. package/src/lib/helpers/{isPublicKey.js → cryptography/isPublicKey.js} +1 -1
  17. package/src/lib/helpers/{keypair.js → cryptography/localKeypair.js} +2 -2
  18. package/src/lib/helpers/cryptography/mutateKeysSrc.js +38 -0
  19. package/src/lib/helpers/cryptography/mutateSrc.js +24 -0
  20. package/src/lib/helpers/cryptography/opsKeypair.js +14 -0
  21. package/src/lib/helpers/cryptography/provision.js +47 -0
  22. package/src/lib/helpers/cryptography/provisionWithPrivateKey.js +26 -0
  23. package/src/lib/helpers/envResolution/determine.js +46 -0
  24. package/src/lib/helpers/{guessEnvironment.js → envResolution/environment.js} +4 -6
  25. package/src/lib/helpers/envResolution/index.js +8 -0
  26. package/src/lib/helpers/errors.js +13 -0
  27. package/src/lib/helpers/findEnvFiles.js +1 -1
  28. package/src/lib/helpers/isFullyEncrypted.js +3 -3
  29. package/src/lib/helpers/keyResolution/index.js +13 -0
  30. package/src/lib/helpers/keyResolution/keyNames.js +24 -0
  31. package/src/lib/helpers/keyResolution/keyValues.js +85 -0
  32. package/src/lib/helpers/keyResolution/readFileKey.js +15 -0
  33. package/src/lib/helpers/keyResolution/readProcessKey.js +7 -0
  34. package/src/lib/helpers/parse.js +1 -1
  35. package/src/lib/helpers/prependPublicKey.js +17 -0
  36. package/src/lib/helpers/preserveShebang.js +16 -0
  37. package/src/lib/main.d.ts +19 -3
  38. package/src/lib/main.js +9 -17
  39. package/src/lib/services/decrypt.js +23 -19
  40. package/src/lib/services/encrypt.js +41 -136
  41. package/src/lib/services/get.js +3 -3
  42. package/src/lib/services/keypair.js +12 -16
  43. package/src/lib/services/prebuild.js +2 -2
  44. package/src/lib/services/precommit.js +2 -2
  45. package/src/lib/services/rotate.js +59 -42
  46. package/src/lib/services/run.js +29 -112
  47. package/src/lib/services/sets.js +40 -124
  48. package/src/lib/helpers/decrypt.js +0 -31
  49. package/src/lib/helpers/deprecationNotice.js +0 -17
  50. package/src/lib/helpers/determineEnvs.js +0 -65
  51. package/src/lib/helpers/encrypt.js +0 -29
  52. package/src/lib/helpers/findPrivateKey.js +0 -25
  53. package/src/lib/helpers/findPublicKey.js +0 -15
  54. package/src/lib/helpers/guessPrivateKeyName.js +0 -18
  55. package/src/lib/helpers/guessPublicKeyName.js +0 -18
  56. package/src/lib/helpers/parseEncryptionKeyFromDotenvKey.js +0 -19
  57. package/src/lib/helpers/parseEnvironmentFromDotenvKey.js +0 -19
  58. package/src/lib/helpers/smartDotenvPrivateKey.js +0 -89
  59. package/src/lib/helpers/smartDotenvPublicKey.js +0 -42
  60. package/src/lib/services/ops.js +0 -111
  61. /package/src/lib/helpers/{encryptValue.js → cryptography/encryptValue.js} +0 -0
  62. /package/src/lib/helpers/{isEncrypted.js → cryptography/isEncrypted.js} +0 -0
@@ -0,0 +1,46 @@
1
+ const dotenvPrivateKeyNames = require('./../dotenvPrivateKeyNames')
2
+ const guessPrivateKeyFilename = require('./../guessPrivateKeyFilename')
3
+
4
+ const TYPE_ENV_FILE = 'envFile'
5
+ const DEFAULT_ENVS = [{ type: TYPE_ENV_FILE, value: '.env' }]
6
+
7
+ function envsFromDotenvPrivateKey (privateKeyNames) {
8
+ const envs = []
9
+
10
+ for (const privateKeyName of privateKeyNames) {
11
+ const filename = guessPrivateKeyFilename(privateKeyName)
12
+ envs.push({ type: TYPE_ENV_FILE, value: filename })
13
+ }
14
+
15
+ return envs
16
+ }
17
+
18
+ function determine (envs = [], processEnv) {
19
+ const privateKeyNames = dotenvPrivateKeyNames(processEnv)
20
+ if (!envs || envs.length <= 0) {
21
+ // if process.env.DOTENV_PRIVATE_KEY or process.env.DOTENV_PRIVATE_KEY_${environment} is set, assume inline encryption methodology
22
+ if (privateKeyNames.length > 0) {
23
+ return envsFromDotenvPrivateKey(privateKeyNames)
24
+ }
25
+
26
+ return DEFAULT_ENVS // default to .env file expectation
27
+ } else {
28
+ let fileAlreadySpecified = false
29
+
30
+ for (const env of envs) {
31
+ if (env.type === TYPE_ENV_FILE) {
32
+ fileAlreadySpecified = true
33
+ }
34
+ }
35
+
36
+ // return early since envs array objects already contain 1 .env file
37
+ if (fileAlreadySpecified) {
38
+ return envs
39
+ }
40
+
41
+ // no .env file specified as a flag so default to .env
42
+ return [...DEFAULT_ENVS, ...envs]
43
+ }
44
+ }
45
+
46
+ module.exports = determine
@@ -1,7 +1,7 @@
1
1
  const path = require('path')
2
2
 
3
- function guessEnvironment (filepath) {
4
- const filename = path.basename(filepath)
3
+ function environment (filepath) {
4
+ const filename = path.basename(filepath).toLowerCase()
5
5
 
6
6
  const parts = filename.split('.')
7
7
  const possibleEnvironmentList = [...parts.slice(2)]
@@ -17,13 +17,11 @@ function guessEnvironment (filepath) {
17
17
  return possibleEnvironmentList[0]
18
18
  }
19
19
 
20
- if (
21
- possibleEnvironmentList.length === 2
22
- ) {
20
+ if (possibleEnvironmentList.length === 2) {
23
21
  return possibleEnvironmentList.join('_')
24
22
  }
25
23
 
26
24
  return possibleEnvironmentList.slice(0, 2).join('_')
27
25
  }
28
26
 
29
- module.exports = guessEnvironment
27
+ module.exports = environment
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ buildEnvs: require('./../buildEnvs'),
3
+ determine: require('./determine'),
4
+ findEnvFiles: require('./../findEnvFiles'),
5
+ dotenvOptionPaths: require('./../dotenvOptionPaths'),
6
+ environment: require('./environment'),
7
+ conventions: require('./../conventions')
8
+ }
@@ -8,6 +8,8 @@ class Errors {
8
8
  this.key = options.key
9
9
  this.privateKey = options.privateKey
10
10
  this.privateKeyName = options.privateKeyName
11
+ this.publicKey = options.publicKey
12
+ this.publicKeyExisting = options.publicKeyExisting
11
13
  this.command = options.command
12
14
 
13
15
  this.message = options.message
@@ -55,6 +57,17 @@ class Errors {
55
57
  return e
56
58
  }
57
59
 
60
+ mispairedPrivateKey () {
61
+ const code = 'MISPAIRED_PRIVATE_KEY'
62
+ const message = `[${code}] private key's derived public key (${truncate(this.publicKey)}) does not match the existing public key (${truncate(this.publicKeyExisting)})`
63
+ const help = `[${code}] https://github.com/dotenvx/dotenvx/issues/752`
64
+
65
+ const e = new Error(message)
66
+ e.code = code
67
+ e.help = help
68
+ return e
69
+ }
70
+
58
71
  looksWrongPrivateKey () {
59
72
  const code = 'WRONG_PRIVATE_KEY'
60
73
  const message = `[${code}] could not decrypt ${this.key} using private key '${this.privateKeyName}=${truncate(this.privateKey)}'`
@@ -1,6 +1,6 @@
1
1
  const fsx = require('./fsx')
2
2
 
3
- const RESERVED_ENV_FILES = ['.env.vault', '.env.project', '.env.keys', '.env.me', '.env.x', '.env.example']
3
+ const RESERVED_ENV_FILES = ['.env.project', '.env.keys', '.env.me', '.env.x', '.env.example']
4
4
 
5
5
  function findEnvFiles (directory) {
6
6
  try {
@@ -1,6 +1,6 @@
1
1
  const dotenvParse = require('./dotenvParse')
2
- const isEncrypted = require('./isEncrypted')
3
- const isPublicKey = require('./isPublicKey')
2
+ const isEncrypted = require('./cryptography/isEncrypted')
3
+ const isPublicKey = require('./cryptography/isPublicKey')
4
4
 
5
5
  function isFullyEncrypted (src) {
6
6
  const parsed = dotenvParse(src, false, false, true) // collect all values
@@ -14,7 +14,7 @@ function isFullyEncrypted (src) {
14
14
  //
15
15
  // key => [value1, ...]
16
16
  for (const value of values) {
17
- const result = isEncrypted(value) || isPublicKey(key, value)
17
+ const result = isEncrypted(value) || isPublicKey(key)
18
18
  if (!result) {
19
19
  return false
20
20
  }
@@ -0,0 +1,13 @@
1
+ module.exports = {
2
+ keyNames: require('./keyNames'),
3
+ keyValues: require('./keyValues'),
4
+
5
+ // private
6
+ // private keys are resolved via keyValues()
7
+
8
+ // other
9
+ readProcessKey: require('./readProcessKey'),
10
+ readFileKey: require('./readFileKey'),
11
+ guessPrivateKeyFilename: require('./../guessPrivateKeyFilename'),
12
+ dotenvPrivateKeyNames: require('./../dotenvPrivateKeyNames')
13
+ }
@@ -0,0 +1,24 @@
1
+ const path = require('path')
2
+ const environment = require('./../envResolution/environment')
3
+
4
+ function keyNames (filepath) {
5
+ const filename = path.basename(filepath).toLowerCase()
6
+
7
+ // .env
8
+ if (filename === '.env') {
9
+ return {
10
+ publicKeyName: 'DOTENV_PUBLIC_KEY',
11
+ privateKeyName: 'DOTENV_PRIVATE_KEY'
12
+ }
13
+ }
14
+
15
+ // .env.ENVIRONMENT
16
+ const resolvedEnvironment = environment(filename).toUpperCase()
17
+
18
+ return {
19
+ publicKeyName: `DOTENV_PUBLIC_KEY_${resolvedEnvironment}`,
20
+ privateKeyName: `DOTENV_PRIVATE_KEY_${resolvedEnvironment}`
21
+ }
22
+ }
23
+
24
+ module.exports = keyNames
@@ -0,0 +1,85 @@
1
+ const path = require('path')
2
+
3
+ const fsx = require('./../fsx')
4
+ const dotenvParse = require('./../dotenvParse')
5
+ const keyNames = require('./keyNames')
6
+ const readProcessKey = require('./readProcessKey')
7
+ const readFileKey = require('./readFileKey')
8
+ const opsKeypair = require('../cryptography/opsKeypair')
9
+
10
+ function invertForPrivateKeyName (filepath) {
11
+ const PUBLIC_KEY_SCHEMA = 'DOTENV_PUBLIC_KEY'
12
+ const PRIVATE_KEY_SCHEMA = 'DOTENV_PRIVATE_KEY'
13
+
14
+ if (!fsx.existsSync(filepath)) {
15
+ return null
16
+ }
17
+
18
+ const envSrc = fsx.readFileX(filepath)
19
+ const envParsed = dotenvParse(envSrc)
20
+
21
+ let publicKeyName
22
+ for (const keyName of Object.keys(envParsed)) {
23
+ if (keyName === PUBLIC_KEY_SCHEMA || keyName.startsWith(PUBLIC_KEY_SCHEMA)) {
24
+ publicKeyName = keyName // find DOTENV_PUBLIC_KEY* in filename
25
+ }
26
+ }
27
+
28
+ if (publicKeyName) {
29
+ return publicKeyName.replace(PUBLIC_KEY_SCHEMA, PRIVATE_KEY_SCHEMA) // return inverted (DOTENV_PUBLIC_KEY* -> DOTENV_PRIVATE_KEY*) if found
30
+ }
31
+
32
+ return null
33
+ }
34
+
35
+ function keyValues (filepath, opts = {}) {
36
+ let keysFilepath = opts.keysFilepath || null
37
+ const opsOn = opts.opsOn === true
38
+ const names = keyNames(filepath)
39
+ const publicKeyName = names.publicKeyName // DOTENV_PUBLIC_KEY_${ENVIRONMENT}
40
+ let privateKeyName = names.privateKeyName // DOTENV_PRIVATE_KEY_${ENVIRONMENT}
41
+
42
+ let publicKey = null
43
+ let privateKey = null
44
+
45
+ // public key: process.env first, then .env*
46
+ publicKey = readProcessKey(publicKeyName)
47
+ if (!publicKey) {
48
+ publicKey = readFileKey(publicKeyName, filepath) || null
49
+ }
50
+
51
+ // private key: process.env first, then .env.keys, then invert public key
52
+ privateKey = readProcessKey(privateKeyName)
53
+ if (!privateKey) {
54
+ if (keysFilepath) { // user specified -fk flag
55
+ keysFilepath = path.resolve(keysFilepath)
56
+ } else {
57
+ keysFilepath = path.resolve(path.dirname(filepath), '.env.keys') // typical scenario
58
+ }
59
+
60
+ privateKey = readFileKey(privateKeyName, keysFilepath)
61
+ }
62
+ // invert
63
+ if (!privateKey) {
64
+ privateKeyName = invertForPrivateKeyName(filepath)
65
+ if (privateKeyName) {
66
+ privateKey = readProcessKey(privateKeyName)
67
+ if (!privateKey) {
68
+ privateKey = readFileKey(privateKeyName, keysFilepath)
69
+ }
70
+ }
71
+ }
72
+
73
+ // ops
74
+ if (opsOn && !privateKey && publicKey && publicKey.length > 0) {
75
+ const kp = opsKeypair(publicKey)
76
+ privateKey = kp.privateKey
77
+ }
78
+
79
+ return {
80
+ publicKeyValue: publicKey || null, // important to make sure name is rendered
81
+ privateKeyValue: privateKey || null // importan to make sure name is rendered
82
+ }
83
+ }
84
+
85
+ module.exports = keyValues
@@ -0,0 +1,15 @@
1
+ const fsx = require('./../fsx')
2
+ const dotenvParse = require('./../dotenvParse')
3
+
4
+ function readFileKey (keyName, filepath) {
5
+ if (fsx.existsSync(filepath)) {
6
+ const src = fsx.readFileX(filepath)
7
+ const parsed = dotenvParse(src)
8
+
9
+ if (parsed[keyName] && parsed[keyName].length > 0) {
10
+ return parsed[keyName]
11
+ }
12
+ }
13
+ }
14
+
15
+ module.exports = readFileKey
@@ -0,0 +1,7 @@
1
+ function readProcessKey (keyName) {
2
+ if (process.env[keyName] && process.env[keyName].length > 0) {
3
+ return process.env[keyName]
4
+ }
5
+ }
6
+
7
+ module.exports = readProcessKey
@@ -1,4 +1,4 @@
1
- const decryptKeyValue = require('./decryptKeyValue')
1
+ const decryptKeyValue = require('./cryptography/decryptKeyValue')
2
2
  const evalKeyValue = require('./evalKeyValue')
3
3
  const resolveEscapeSequences = require('./resolveEscapeSequences')
4
4
 
@@ -0,0 +1,17 @@
1
+ function prependPublicKey (publicKeyName, publicKey, filename, relativeFilepath = '.env.keys') {
2
+ const comment = relativeFilepath === '.env.keys'
3
+ ? ''
4
+ : ` # -fk ${relativeFilepath}`
5
+
6
+ return [
7
+ '#/-------------------[DOTENV_PUBLIC_KEY]--------------------/',
8
+ '#/ public-key encryption for .env files /',
9
+ '#/ [how it works](https://dotenvx.com/encryption) /',
10
+ '#/----------------------------------------------------------/',
11
+ `${publicKeyName}="${publicKey}"${comment}`,
12
+ '',
13
+ `# ${filename}`
14
+ ].join('\n')
15
+ }
16
+
17
+ module.exports = prependPublicKey
@@ -0,0 +1,16 @@
1
+ function preserveShebang (envSrc) {
2
+ const [firstLine, ...remainingLines] = envSrc.split('\n')
3
+ let firstLinePreserved = ''
4
+
5
+ if (firstLine.startsWith('#!')) {
6
+ firstLinePreserved = firstLine + '\n'
7
+ envSrc = remainingLines.join('\n')
8
+ }
9
+
10
+ return {
11
+ firstLinePreserved,
12
+ envSrc
13
+ }
14
+ }
15
+
16
+ module.exports = preserveShebang
package/src/lib/main.d.ts CHANGED
@@ -113,10 +113,10 @@ export interface DotenvConfigOptions {
113
113
  envKeysFile?: string;
114
114
 
115
115
  /**
116
- * 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.
116
+ * Legacy option retained for compatibility.
117
117
  *
118
118
  * @default undefined
119
- * @example require('@dotenvx/dotenvx').config({ DOTENV_KEY: 'dotenv://:key_1234…@dotenvx.com/vault/.env.vault?environment=production' })
119
+ * @example require('@dotenvx/dotenvx').config({ DOTENV_KEY: 'dotenv://:key_1234' })
120
120
  */
121
121
  DOTENV_KEY?: string;
122
122
 
@@ -166,7 +166,7 @@ export interface DotenvPopulateInput {
166
166
  }
167
167
 
168
168
  /**
169
- * Loads `.env` file contents into process.env by default. If `DOTENV_KEY` is present, it smartly attempts to load encrypted `.env.vault` file contents into process.env.
169
+ * Loads `.env` file contents into process.env by default.
170
170
  *
171
171
  * @see https://dotenvx.com/docs
172
172
  *
@@ -205,6 +205,14 @@ export interface SetOptions {
205
205
  * @example require('@dotenvx/dotenvx').config(key, value, { encrypt: false } })
206
206
  */
207
207
  encrypt?: boolean;
208
+
209
+ /**
210
+ * Turn off Dotenvx Ops features - https://dotenvx.com/ops
211
+ *
212
+ * @default false
213
+ * @example require('@dotenvx/dotenvx').set(key, value, { opsOff: true })
214
+ */
215
+ opsOff?: boolean;
208
216
  }
209
217
 
210
218
  export type SetProcessedEnv = {
@@ -272,6 +280,14 @@ export interface GetOptions {
272
280
  * @example require('@dotenvx/dotenvx').get('KEY', { strict: true })
273
281
  */
274
282
  strict?: boolean;
283
+
284
+ /**
285
+ * Turn off Dotenvx Ops features - https://dotenvx.com/ops
286
+ *
287
+ * @default false
288
+ * @example require('@dotenvx/dotenvx').get('KEY', { opsOff: true })
289
+ */
290
+ opsOff?: boolean;
275
291
  }
276
292
 
277
293
  /**
package/src/lib/main.js CHANGED
@@ -39,12 +39,6 @@ const config = function (options = {}) {
39
39
  // envKeysFile
40
40
  const envKeysFile = options.envKeysFile
41
41
 
42
- // DOTENV_KEY (DEPRECATED)
43
- let DOTENV_KEY = process.env.DOTENV_KEY
44
- if (options && options.DOTENV_KEY) {
45
- DOTENV_KEY = options.DOTENV_KEY
46
- }
47
-
48
42
  // dotenvx-ops related
49
43
  const opsOn = options.opsOff !== true
50
44
 
@@ -55,12 +49,12 @@ const config = function (options = {}) {
55
49
  }
56
50
 
57
51
  try {
58
- const envs = buildEnvs(options, DOTENV_KEY)
52
+ const envs = buildEnvs(options)
59
53
  const {
60
54
  processedEnvs,
61
55
  readableFilepaths,
62
56
  uniqueInjectedKeys
63
- } = new Run(envs, overload, DOTENV_KEY, processEnv, envKeysFile, opsOn).run()
57
+ } = new Run(envs, overload, processEnv, envKeysFile, opsOn).run()
64
58
 
65
59
  if (opsOn) {
66
60
  // removed radar feature for now. contact me at mot@dotenvx.com if still needed for your organization.
@@ -71,11 +65,6 @@ const config = function (options = {}) {
71
65
  /** @type {Record<string, string>} */
72
66
  const parsedAll = {}
73
67
  for (const processedEnv of processedEnvs) {
74
- if (processedEnv.type === 'envVaultFile') {
75
- logger.verbose(`loading env from encrypted ${processedEnv.filepath} (${path.resolve(processedEnv.filepath)})`)
76
- logger.debug(`decrypting encrypted env from ${processedEnv.filepath} (${path.resolve(processedEnv.filepath)})`)
77
- }
78
-
79
68
  if (processedEnv.type === 'envFile') {
80
69
  logger.verbose(`loading env from ${processedEnv.filepath} (${path.resolve(processedEnv.filepath)})`)
81
70
  }
@@ -192,12 +181,13 @@ const set = function (key, value, options = {}) {
192
181
 
193
182
  const envs = buildEnvs(options)
194
183
  const envKeysFilepath = options.envKeysFile
184
+ const opsOn = options.opsOff !== true
195
185
 
196
186
  const {
197
187
  processedEnvs,
198
188
  changedFilepaths,
199
189
  unchangedFilepaths
200
- } = new Sets(key, value, envs, encrypt, envKeysFilepath).run()
190
+ } = new Sets(key, value, envs, encrypt, envKeysFilepath, opsOn).run()
201
191
 
202
192
  let withEncryption = ''
203
193
 
@@ -257,11 +247,12 @@ const set = function (key, value, options = {}) {
257
247
  /* @type {import('./main').get} */
258
248
  const get = function (key, options = {}) {
259
249
  const envs = buildEnvs(options)
250
+ const opsOn = options.opsOff !== true
260
251
 
261
252
  // ignore
262
253
  const ignore = options.ignore || []
263
254
 
264
- const { parsed, errors } = new Get(key, envs, options.overload, process.env.DOTENV_KEY, options.all, options.envKeysFile).run()
255
+ const { parsed, errors } = new Get(key, envs, options.overload, options.all, options.envKeysFile, opsOn).run()
265
256
 
266
257
  for (const error of errors || []) {
267
258
  if (ignore.includes(error.code)) {
@@ -317,8 +308,9 @@ const genexample = function (directory, envFile) {
317
308
  }
318
309
 
319
310
  /** @type {import('./main').keypair} */
320
- const keypair = function (envFile, key, envKeysFile = null) {
321
- const keypairs = new Keypair(envFile, envKeysFile).run()
311
+ const keypair = function (envFile, key, envKeysFile = null, opsOff = false) {
312
+ const opsOn = opsOff !== true
313
+ const keypairs = new Keypair(envFile, envKeysFile, opsOn).run()
322
314
  if (key) {
323
315
  return keypairs[key]
324
316
  } else {
@@ -5,19 +5,28 @@ const picomatch = require('picomatch')
5
5
  const TYPE_ENV_FILE = 'envFile'
6
6
 
7
7
  const Errors = require('./../helpers/errors')
8
- const guessPrivateKeyName = require('./../helpers/guessPrivateKeyName')
9
- const { findPrivateKey } = require('./../helpers/findPrivateKey')
10
- const findPublicKey = require('./../helpers/findPublicKey')
11
- const decryptKeyValue = require('./../helpers/decryptKeyValue')
12
- const isEncrypted = require('./../helpers/isEncrypted')
13
- const dotenvParse = require('./../helpers/dotenvParse')
8
+
9
+ const {
10
+ determine
11
+ } = require('./../helpers/envResolution')
12
+
13
+ const {
14
+ keyNames,
15
+ keyValues
16
+ } = require('./../helpers/keyResolution')
17
+
18
+ const {
19
+ decryptKeyValue,
20
+ isEncrypted
21
+ } = require('./../helpers/cryptography')
22
+
14
23
  const replace = require('./../helpers/replace')
24
+ const dotenvParse = require('./../helpers/dotenvParse')
15
25
  const detectEncoding = require('./../helpers/detectEncoding')
16
- const determineEnvs = require('./../helpers/determineEnvs')
17
26
 
18
27
  class Decrypt {
19
- constructor (envs = [], key = [], excludeKey = [], envKeysFilepath = null, opsOn = true) {
20
- this.envs = determineEnvs(envs, process.env)
28
+ constructor (envs = [], key = [], excludeKey = [], envKeysFilepath = null, opsOn = false) {
29
+ this.envs = determine(envs, process.env)
21
30
  this.key = key
22
31
  this.excludeKey = excludeKey
23
32
  this.envKeysFilepath = envKeysFilepath
@@ -63,15 +72,14 @@ class Decrypt {
63
72
  row.envFilepath = envFilepath
64
73
 
65
74
  try {
66
- const encoding = this._detectEncoding(filepath)
75
+ const encoding = detectEncoding(filepath)
67
76
  let envSrc = fsx.readFileX(filepath, { encoding })
68
77
  const envParsed = dotenvParse(envSrc)
69
78
 
70
- const publicKey = findPublicKey(envFilepath)
71
- const privateKey = findPrivateKey(envFilepath, this.envKeysFilepath, this.opsOn, publicKey)
72
- const privateKeyName = guessPrivateKeyName(envFilepath)
79
+ const { privateKeyName } = keyNames(envFilepath)
80
+ const { privateKeyValue } = keyValues(envFilepath, { keysFilepath: this.envKeysFilepath, opsOn: this.opsOn })
73
81
 
74
- row.privateKey = privateKey
82
+ row.privateKey = privateKeyValue
75
83
  row.privateKeyName = privateKeyName
76
84
  row.changed = false // track possible changes
77
85
 
@@ -90,7 +98,7 @@ class Decrypt {
90
98
  if (encrypted) {
91
99
  row.keys.push(key) // track key(s)
92
100
 
93
- const decryptedValue = decryptKeyValue(key, value, privateKeyName, privateKey)
101
+ const decryptedValue = decryptKeyValue(key, value, privateKeyName, privateKeyValue)
94
102
  // once newSrc is built write it out
95
103
  envSrc = replace(envSrc, key, decryptedValue)
96
104
 
@@ -130,10 +138,6 @@ class Decrypt {
130
138
 
131
139
  return this.excludeKey
132
140
  }
133
-
134
- _detectEncoding (filepath) {
135
- return detectEncoding(filepath)
136
- }
137
141
  }
138
142
 
139
143
  module.exports = Decrypt