@dotenvx/dotenvx 2.38.2 → 3.0.0-beta.2

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 (271) hide show
  1. package/README.md +197 -3314
  2. package/bin/dotenvx.cjs +34 -0
  3. package/config.cjs +1 -0
  4. package/config.d.ts +1 -0
  5. package/config.mjs +2 -0
  6. package/index.cjs +141 -0
  7. package/index.d.ts +66 -0
  8. package/index.mjs +3 -0
  9. package/lib/cli.cjs +43 -0
  10. package/lib/native-package.cjs +21 -0
  11. package/lib/platforms.cjs +18 -0
  12. package/{src/lib/helpers/redactOutput.js → lib/redactOutput.cjs} +2 -48
  13. package/lib/redaction.cjs +64 -0
  14. package/lib/validateOptions.cjs +57 -0
  15. package/native.cjs +23 -0
  16. package/package.json +57 -65
  17. package/CHANGELOG.md +0 -2733
  18. package/src/cli/actions/armor/down.js +0 -38
  19. package/src/cli/actions/armor/invite.js +0 -21
  20. package/src/cli/actions/armor/move.js +0 -43
  21. package/src/cli/actions/armor/open.js +0 -28
  22. package/src/cli/actions/armor/pull.js +0 -38
  23. package/src/cli/actions/armor/push.js +0 -38
  24. package/src/cli/actions/armor/status.js +0 -7
  25. package/src/cli/actions/armor/up.js +0 -38
  26. package/src/cli/actions/check.js +0 -78
  27. package/src/cli/actions/curl.js +0 -71
  28. package/src/cli/actions/decrypt.js +0 -119
  29. package/src/cli/actions/del.js +0 -54
  30. package/src/cli/actions/doctor.js +0 -22
  31. package/src/cli/actions/encrypt.js +0 -104
  32. package/src/cli/actions/ext/genexample.js +0 -36
  33. package/src/cli/actions/ext/gitignore.js +0 -106
  34. package/src/cli/actions/ext/prebuild.js +0 -7
  35. package/src/cli/actions/ext/precommit.js +0 -38
  36. package/src/cli/actions/ext/scan.js +0 -33
  37. package/src/cli/actions/feedback.js +0 -42
  38. package/src/cli/actions/get.js +0 -128
  39. package/src/cli/actions/init.js +0 -67
  40. package/src/cli/actions/keychain/down.js +0 -30
  41. package/src/cli/actions/keychain/pull.js +0 -30
  42. package/src/cli/actions/keychain/push.js +0 -30
  43. package/src/cli/actions/keychain/up.js +0 -30
  44. package/src/cli/actions/keypair.js +0 -84
  45. package/src/cli/actions/login.js +0 -64
  46. package/src/cli/actions/logout.js +0 -37
  47. package/src/cli/actions/ls.js +0 -76
  48. package/src/cli/actions/policy.js +0 -14
  49. package/src/cli/actions/primitivesCheck.js +0 -32
  50. package/src/cli/actions/primitivesProfile.js +0 -15
  51. package/src/cli/actions/primitivesRedact.js +0 -19
  52. package/src/cli/actions/protect.js +0 -75
  53. package/src/cli/actions/protectDocker.js +0 -30
  54. package/src/cli/actions/protectProcess.js +0 -126
  55. package/src/cli/actions/protectStdin.js +0 -80
  56. package/src/cli/actions/run.js +0 -203
  57. package/src/cli/actions/set.js +0 -103
  58. package/src/cli/actions/settings/device.js +0 -23
  59. package/src/cli/actions/settings/hostname.js +0 -20
  60. package/src/cli/actions/settings/off.js +0 -14
  61. package/src/cli/actions/settings/on.js +0 -14
  62. package/src/cli/actions/settings/path.js +0 -20
  63. package/src/cli/actions/settings/token.js +0 -23
  64. package/src/cli/actions/settings/username.js +0 -20
  65. package/src/cli/actions/update.js +0 -15
  66. package/src/cli/commandAction.js +0 -16
  67. package/src/cli/commands/armor.js +0 -129
  68. package/src/cli/commands/custody.js +0 -45
  69. package/src/cli/commands/ext.js +0 -88
  70. package/src/cli/commands/fileOptions.js +0 -22
  71. package/src/cli/commands/native.js +0 -54
  72. package/src/cli/commands/primitives.js +0 -73
  73. package/src/cli/commands/settings.js +0 -63
  74. package/src/cli/dotenvx.js +0 -443
  75. package/src/cli/help.js +0 -179
  76. package/src/db/device.js +0 -67
  77. package/src/db/session.js +0 -330
  78. package/src/lib/api/getAccount.js +0 -32
  79. package/src/lib/api/getConnectionResources.js +0 -21
  80. package/src/lib/api/getTeamConnections.js +0 -27
  81. package/src/lib/api/getTeamServices.js +0 -23
  82. package/src/lib/api/getTeams.js +0 -28
  83. package/src/lib/api/postArmorDown.js +0 -48
  84. package/src/lib/api/postArmorMove.js +0 -48
  85. package/src/lib/api/postArmorPull.js +0 -48
  86. package/src/lib/api/postArmorPush.js +0 -48
  87. package/src/lib/api/postArmorUp.js +0 -53
  88. package/src/lib/api/postConnectionKeypair.js +0 -23
  89. package/src/lib/api/postFeedback.js +0 -31
  90. package/src/lib/api/postLogout.js +0 -34
  91. package/src/lib/api/postOauthDeviceCode.js +0 -38
  92. package/src/lib/api/postOauthToken.js +0 -35
  93. package/src/lib/api/postTeamInvitation.js +0 -28
  94. package/src/lib/api/teamConnectionSetup.js +0 -31
  95. package/src/lib/config.d.ts +0 -1
  96. package/src/lib/config.js +0 -1
  97. package/src/lib/conventions/environment.js +0 -27
  98. package/src/lib/conventions/filepaths.js +0 -9
  99. package/src/lib/conventions/keynames.js +0 -38
  100. package/src/lib/custodians/index.js +0 -75
  101. package/src/lib/custodians/local/bitwarden.js +0 -165
  102. package/src/lib/custodians/local/file.js +0 -18
  103. package/src/lib/custodians/local/native/backend.js +0 -56
  104. package/src/lib/custodians/local/native/index.js +0 -28
  105. package/src/lib/custodians/local/native/store.js +0 -24
  106. package/src/lib/custodians/local/onepassword.js +0 -149
  107. package/src/lib/custodians/managed/armor/index.js +0 -14
  108. package/src/lib/custodians/managed/armor/store.js +0 -48
  109. package/src/lib/custodians/resolveLocalKey.js +0 -67
  110. package/src/lib/decryptors/armor/index.js +0 -23
  111. package/src/lib/decryptors/decryptor-worker.js +0 -6
  112. package/src/lib/decryptors/index.js +0 -69
  113. package/src/lib/envfile/discovery/discoverEnvFiles.js +0 -29
  114. package/src/lib/envfile/discovery/extractEnvKeys.js +0 -173
  115. package/src/lib/envfile/discovery/scanSource.js +0 -79
  116. package/src/lib/envfile/parsing/formatEnvfileSyntaxError.js +0 -24
  117. package/src/lib/envfile/parsing/readEnvfile.js +0 -43
  118. package/src/lib/envfile/parsing/resolveEnvfilePath.js +0 -5
  119. package/src/lib/envfile/rendering/normalizeEnvfile.js +0 -53
  120. package/src/lib/envfile/rendering/previewEnvfile.js +0 -43
  121. package/src/lib/envfile/rendering/renderEnvfile.js +0 -30
  122. package/src/lib/envfile/validation/diagnosticLocation.js +0 -29
  123. package/src/lib/envfile/validation/protectEnvfile.js +0 -40
  124. package/src/lib/envfile/validation/validateEnvfile.js +0 -14
  125. package/src/lib/events/backends/armor.js +0 -31
  126. package/src/lib/events/background.js +0 -59
  127. package/src/lib/events/catalog.js +0 -14
  128. package/src/lib/events/cli.js +0 -40
  129. package/src/lib/events/delivery.js +0 -89
  130. package/src/lib/events/index.js +0 -73
  131. package/src/lib/events/metadata.js +0 -25
  132. package/src/lib/events/sdk.js +0 -44
  133. package/src/lib/events/worker.js +0 -29
  134. package/src/lib/events/workerEnvironment.js +0 -14
  135. package/src/lib/helpers/armoredKeyDisplay.js +0 -10
  136. package/src/lib/helpers/arrayToTree.js +0 -27
  137. package/src/lib/helpers/buildApiError.js +0 -19
  138. package/src/lib/helpers/buildCommandEnvs.js +0 -40
  139. package/src/lib/helpers/buildConfigEnvs.js +0 -13
  140. package/src/lib/helpers/buildEnvs.js +0 -24
  141. package/src/lib/helpers/buildOauthError.js +0 -14
  142. package/src/lib/helpers/canonicalEnvFilename.js +0 -13
  143. package/src/lib/helpers/catchAndLog.js +0 -16
  144. package/src/lib/helpers/checklist.js +0 -50
  145. package/src/lib/helpers/chomp.js +0 -5
  146. package/src/lib/helpers/colorDepth.js +0 -23
  147. package/src/lib/helpers/conventions.js +0 -28
  148. package/src/lib/helpers/createElapsedStatus.js +0 -17
  149. package/src/lib/helpers/createProtectSpinner.js +0 -13
  150. package/src/lib/helpers/createSpinner.js +0 -40
  151. package/src/lib/helpers/cryptography/decryptKeyValue.js +0 -50
  152. package/src/lib/helpers/cryptography/index.js +0 -7
  153. package/src/lib/helpers/cryptography/isDotenvPublicKey.js +0 -7
  154. package/src/lib/helpers/cryptography/isPlainKey.js +0 -7
  155. package/src/lib/helpers/cryptography/mutateKeysSrc.js +0 -22
  156. package/src/lib/helpers/cryptography/mutateSrc.js +0 -24
  157. package/src/lib/helpers/custodySelect.js +0 -25
  158. package/src/lib/helpers/decryptDeviceValue.js +0 -7
  159. package/src/lib/helpers/detectEncoding.js +0 -22
  160. package/src/lib/helpers/detectEncodingSync.js +0 -22
  161. package/src/lib/helpers/dotenvOptionPaths.js +0 -25
  162. package/src/lib/helpers/dotenvPrivateKeyNames.js +0 -7
  163. package/src/lib/helpers/enclavedHint.js +0 -11
  164. package/src/lib/helpers/encryptDeviceValue.js +0 -7
  165. package/src/lib/helpers/envResolution/determine.js +0 -49
  166. package/src/lib/helpers/envResolution/index.js +0 -8
  167. package/src/lib/helpers/errors.js +0 -374
  168. package/src/lib/helpers/escape.js +0 -6
  169. package/src/lib/helpers/execute.js +0 -12
  170. package/src/lib/helpers/executeCommand.js +0 -208
  171. package/src/lib/helpers/executeDynamic.js +0 -86
  172. package/src/lib/helpers/executeExtension.js +0 -39
  173. package/src/lib/helpers/filterKeys.js +0 -15
  174. package/src/lib/helpers/findEnvFiles.js +0 -25
  175. package/src/lib/helpers/formatCode.js +0 -11
  176. package/src/lib/helpers/fsx.js +0 -110
  177. package/src/lib/helpers/guessPrivateKeyFilename.js +0 -15
  178. package/src/lib/helpers/hasPlaintextSecrets.js +0 -7
  179. package/src/lib/helpers/http.js +0 -14
  180. package/src/lib/helpers/installPrecommitHook.js +0 -82
  181. package/src/lib/helpers/installProtectFilter.js +0 -57
  182. package/src/lib/helpers/installProtectIgnore.js +0 -27
  183. package/src/lib/helpers/isPublicKey.js +0 -3
  184. package/src/lib/helpers/isTeamRequiredError.js +0 -10
  185. package/src/lib/helpers/jsonToEnv.js +0 -7
  186. package/src/lib/helpers/keypairMetadata.js +0 -90
  187. package/src/lib/helpers/kits/sample.js +0 -5
  188. package/src/lib/helpers/linuxSecretService.js +0 -52
  189. package/src/lib/helpers/listenForOpenKey.js +0 -46
  190. package/src/lib/helpers/logProtectedFiles.js +0 -9
  191. package/src/lib/helpers/logSetSummary.js +0 -21
  192. package/src/lib/helpers/macosKeychain.js +0 -52
  193. package/src/lib/helpers/mask.js +0 -22
  194. package/src/lib/helpers/maskEnvSrc.js +0 -15
  195. package/src/lib/helpers/maskProcessedEnvs.js +0 -29
  196. package/src/lib/helpers/matchesStoredKey.js +0 -5
  197. package/src/lib/helpers/nativeStoreError.js +0 -10
  198. package/src/lib/helpers/normalizeDotenvConfigConvention.js +0 -13
  199. package/src/lib/helpers/normalizeDotenvConfigIgnore.js +0 -18
  200. package/src/lib/helpers/normalizeDotenvConfigPath.js +0 -14
  201. package/src/lib/helpers/normalizeDotenvConfigQuiet.js +0 -13
  202. package/src/lib/helpers/normalizeToken.js +0 -5
  203. package/src/lib/helpers/openUrl.js +0 -7
  204. package/src/lib/helpers/packageJson.js +0 -3
  205. package/src/lib/helpers/parseWithDecryptor.js +0 -115
  206. package/src/lib/helpers/plaintextKeys.js +0 -9
  207. package/src/lib/helpers/precommitEnvPatterns.js +0 -1
  208. package/src/lib/helpers/prependPublicKey.js +0 -17
  209. package/src/lib/helpers/preserveShebang.js +0 -16
  210. package/src/lib/helpers/prompts.js +0 -169
  211. package/src/lib/helpers/protectSettings.js +0 -64
  212. package/src/lib/helpers/ptyCommand.js +0 -27
  213. package/src/lib/helpers/readEnvKey.js +0 -32
  214. package/src/lib/helpers/redact.js +0 -9
  215. package/src/lib/helpers/redactProcessOutput.js +0 -58
  216. package/src/lib/helpers/redactedValues.js +0 -29
  217. package/src/lib/helpers/removeDynamicHelpSection.js +0 -21
  218. package/src/lib/helpers/removeEnvKey.js +0 -51
  219. package/src/lib/helpers/removeLegacyProtectFilter.js +0 -43
  220. package/src/lib/helpers/removeOptionsHelpParts.js +0 -59
  221. package/src/lib/helpers/resolveBitwardenPassword.js +0 -165
  222. package/src/lib/helpers/resolveDirectoryFilepath.js +0 -16
  223. package/src/lib/helpers/resolveEnvKeysFile.js +0 -11
  224. package/src/lib/helpers/resolveHome.js +0 -12
  225. package/src/lib/helpers/resolveOnePassword.js +0 -82
  226. package/src/lib/helpers/sanitizeCommandForMetadata.js +0 -64
  227. package/src/lib/helpers/selectConnection.js +0 -51
  228. package/src/lib/helpers/selectConnectionResource.js +0 -41
  229. package/src/lib/helpers/selectEnvfileEnvs.js +0 -29
  230. package/src/lib/helpers/selectKeyStorage.js +0 -140
  231. package/src/lib/helpers/statusReporter.js +0 -8
  232. package/src/lib/helpers/storeKeyStorage.js +0 -39
  233. package/src/lib/helpers/teamChoicesFromMeta.js +0 -8
  234. package/src/lib/helpers/truncate.js +0 -10
  235. package/src/lib/helpers/uninstallPrecommitHook.js +0 -67
  236. package/src/lib/helpers/upsertEnvKey.js +0 -62
  237. package/src/lib/helpers/windowsCredentialManager.js +0 -166
  238. package/src/lib/main.d.ts +0 -509
  239. package/src/lib/main.js +0 -468
  240. package/src/lib/providers/index.js +0 -86
  241. package/src/lib/providers/native/index.js +0 -2
  242. package/src/lib/resolvers/envs.js +0 -416
  243. package/src/lib/resolvers/get.js +0 -87
  244. package/src/lib/resolvers/keypair.js +0 -135
  245. package/src/lib/resolvers/ls.js +0 -69
  246. package/src/lib/services/armorDown.js +0 -73
  247. package/src/lib/services/armorInvite.js +0 -36
  248. package/src/lib/services/armorMove.js +0 -55
  249. package/src/lib/services/armorOpen.js +0 -26
  250. package/src/lib/services/armorPull.js +0 -73
  251. package/src/lib/services/armorPush.js +0 -66
  252. package/src/lib/services/armorUp.js +0 -71
  253. package/src/lib/services/custodyTransfer.js +0 -50
  254. package/src/lib/services/doctor.js +0 -93
  255. package/src/lib/services/genexample.js +0 -112
  256. package/src/lib/services/init.js +0 -83
  257. package/src/lib/services/keychainDown.js +0 -60
  258. package/src/lib/services/keychainPull.js +0 -46
  259. package/src/lib/services/keychainPush.js +0 -51
  260. package/src/lib/services/keychainUp.js +0 -72
  261. package/src/lib/services/login.js +0 -51
  262. package/src/lib/services/logout.js +0 -26
  263. package/src/lib/services/prebuild.js +0 -91
  264. package/src/lib/services/precommit.js +0 -144
  265. package/src/lib/services/validate.js +0 -57
  266. package/src/lib/transforms/decrypt.js +0 -98
  267. package/src/lib/transforms/del.js +0 -58
  268. package/src/lib/transforms/encrypt.js +0 -164
  269. package/src/lib/transforms/set.js +0 -144
  270. package/src/shared/colors.js +0 -67
  271. package/src/shared/logger.js +0 -148
@@ -1,98 +0,0 @@
1
- const fsx = require('./../helpers/fsx')
2
- const path = require('path')
3
- const { upsert } = require('@dotenvx/primitives')
4
-
5
- const TYPE_ENV_FILE = 'envFile'
6
-
7
- const Errors = require('./../helpers/errors')
8
- const enclavedHint = require('../helpers/enclavedHint')
9
- const { determine } = require('./../helpers/envResolution')
10
- const detectEncoding = require('./../helpers/detectEncoding')
11
- const providers = require('./../providers')
12
- const decryptors = require('./../decryptors')
13
- const parseWithDecryptor = require('./../helpers/parseWithDecryptor')
14
-
15
- function parseError (error) {
16
- return new Errors({
17
- code: error.code,
18
- message: error.message,
19
- help: error.help
20
- }).custom()
21
- }
22
-
23
- async function decrypt (options = {}) {
24
- const envs = options.envs || []
25
- const ik = options.ik
26
- const ek = options.ek
27
- const fk = options.fk
28
- const sources = new Map()
29
- const provider = await providers({
30
- ...options,
31
- onProvider: (name, key) => {
32
- sources.set(key, name)
33
- if (options.onProvider) options.onProvider(name, key)
34
- }
35
- })
36
- if (provider) provider.sourceFor = key => sources.get(key)
37
- const decryptor = await decryptors(options)
38
-
39
- const processedEnvs = []
40
- const changedFilepaths = []
41
- const unchangedFilepaths = []
42
-
43
- for (const env of determine(envs, process.env)) {
44
- if (env.type !== TYPE_ENV_FILE) {
45
- continue
46
- }
47
-
48
- const envFilepath = env.envFilepath || env.value
49
- const filepath = env.filepath || path.resolve(envFilepath)
50
- const row = { keys: [], type: TYPE_ENV_FILE, filepath, envFilepath, changed: false }
51
-
52
- try {
53
- const encoding = await detectEncoding(filepath)
54
- row.envSrc = await fsx.readFileX(filepath, { encoding })
55
-
56
- const { parsed, errors, keySources } = await parseWithDecryptor.arrays(row.envSrc, { fk, ik, ek, provider, decryptor })
57
-
58
- row.keySources = keySources
59
-
60
- if (errors.length > 0) {
61
- row.error = parseError(errors[0])
62
- }
63
-
64
- for (const [key, values] of Object.entries(parsed)) {
65
- const before = row.envSrc
66
- const after = upsert(before, key, values)
67
- if (after !== before) {
68
- row.envSrc = after
69
- row.changed = true
70
- row.keys.push(key)
71
- }
72
- }
73
-
74
- if (row.changed) {
75
- changedFilepaths.push(envFilepath)
76
- } else {
77
- unchangedFilepaths.push(envFilepath)
78
- }
79
- } catch (error) {
80
- if (error.code === 'ENOENT') {
81
- row.error = new Errors({ envFilepath, filepath }).missingEnvFile()
82
- } else {
83
- row.error = error
84
- }
85
- }
86
-
87
- enclavedHint(row.error, envFilepath)
88
- processedEnvs.push(row)
89
- }
90
-
91
- return {
92
- processedEnvs,
93
- changedFilepaths,
94
- unchangedFilepaths
95
- }
96
- }
97
-
98
- module.exports = decrypt
@@ -1,58 +0,0 @@
1
- const fsx = require('./../helpers/fsx')
2
- const path = require('path')
3
- const { remove } = require('@dotenvx/primitives')
4
-
5
- const TYPE_ENV_FILE = 'envFile'
6
-
7
- const Errors = require('./../helpers/errors')
8
- const { determine } = require('./../helpers/envResolution')
9
- const detectEncoding = require('./../helpers/detectEncoding')
10
-
11
- async function delTransform (options = {}) {
12
- const envs = options.envs || []
13
- const key = options.key
14
-
15
- const processedEnvs = []
16
- const changedFilepaths = []
17
- const unchangedFilepaths = []
18
-
19
- for (const env of determine(envs, process.env)) {
20
- if (env.type !== TYPE_ENV_FILE) {
21
- continue
22
- }
23
-
24
- const envFilepath = env.envFilepath || env.value
25
- const filepath = env.filepath || path.resolve(envFilepath)
26
- const row = { key, type: TYPE_ENV_FILE, filepath, envFilepath, changed: false }
27
-
28
- try {
29
- const encoding = await detectEncoding(filepath)
30
- const before = await fsx.readFileX(filepath, { encoding })
31
- const after = remove(before, key)
32
-
33
- row.envSrc = after
34
- if (after !== before) {
35
- row.changed = true
36
- changedFilepaths.push(envFilepath)
37
- } else {
38
- unchangedFilepaths.push(envFilepath)
39
- }
40
- } catch (error) {
41
- if (error.code === 'ENOENT') {
42
- row.error = new Errors({ envFilepath, filepath }).missingEnvFile()
43
- } else {
44
- row.error = error
45
- }
46
- }
47
-
48
- processedEnvs.push(row)
49
- }
50
-
51
- return {
52
- processedEnvs,
53
- changedFilepaths,
54
- unchangedFilepaths
55
- }
56
- }
57
-
58
- module.exports = delTransform
@@ -1,164 +0,0 @@
1
- const fsx = require('./../helpers/fsx')
2
- const path = require('path')
3
- const { encrypted, encrypt, scan, upsert, publickeys } = require('@dotenvx/primitives')
4
-
5
- const TYPE_ENV_FILE = 'envFile'
6
- const SAMPLE_ENV_KIT = require('../helpers/kits/sample')
7
-
8
- const Errors = require('../helpers/errors')
9
- const selectEnvfileEnvs = require('../helpers/selectEnvfileEnvs')
10
- const detectEncoding = require('./../helpers/detectEncoding')
11
- const { isDotenvPublicKey, isPlainKey, mutateSrc } = require('../helpers/cryptography')
12
- const keynames = require('../conventions/keynames')
13
-
14
- const storeKeyStorage = require('../helpers/storeKeyStorage')
15
- const readEnvfile = require('../envfile/parsing/readEnvfile')
16
-
17
- async function encryptTransform (options = {}) {
18
- const envs = options.envs || []
19
- const ik = options.ik
20
- const ek = options.ek
21
- const fk = options.fk || '.env.keys'
22
- let storage
23
- const custodyContext = { token: options.token }
24
- const noCreate = options.noCreate
25
-
26
- const processedEnvs = []
27
- const changedFilepaths = []
28
- const unchangedFilepaths = []
29
-
30
- // set up keysSrc
31
- let keysSrc
32
- if (options.with === undefined && await fsx.exists(fk)) {
33
- try {
34
- const encoding = await detectEncoding(fk)
35
- keysSrc = await fsx.readFileX(fk, { encoding })
36
- } catch (err) {
37
- if (err.code === 'EACCES' || err.code === 'EPERM') {
38
- // do nothing (scenario: chmod a-r .env.keys)
39
- } else {
40
- throw err
41
- }
42
- }
43
- }
44
-
45
- for (const env of selectEnvfileEnvs(envs, options)) {
46
- if (env.type !== TYPE_ENV_FILE) {
47
- continue
48
- }
49
-
50
- const envFilepath = env.envFilepath || env.value
51
- const filepath = env.filepath || path.resolve(envFilepath)
52
- const row = { keys: [], type: TYPE_ENV_FILE, filepath, envFilepath, changed: false }
53
-
54
- try {
55
- const schema = readEnvfile(undefined, [filepath])
56
- const fileExists = await fsx.exists(filepath)
57
- if (!fileExists && !noCreate) {
58
- row.envSrc = SAMPLE_ENV_KIT
59
- row.changed = true
60
- } else {
61
- const encoding = await detectEncoding(filepath)
62
- row.envSrc = await fsx.readFileX(filepath, { encoding })
63
- }
64
-
65
- if (!schema.exists && row.envSrc.trim().length === 0) {
66
- row.envSrc = SAMPLE_ENV_KIT
67
- row.changed = true
68
- }
69
-
70
- if (options.with !== undefined) {
71
- // Inspect every assignment before selection or schema rules can hide it.
72
- const { parsed: existing } = scan(row.envSrc)
73
- const declaredKeys = Object.entries(existing).filter(([key]) => isDotenvPublicKey(key)).flatMap(([, values]) => values)
74
- const matchingKey = declaredKeys.length > 0 && declaredKeys.every(value => value.toLowerCase() === options.with.toLowerCase())
75
- if (!matchingKey && (declaredKeys.length > 0 || Object.values(existing).some(values => values.some(encrypted)))) {
76
- throw new Errors({ envFilepath }).alreadyEncrypted()
77
- }
78
- const { publicKeyName } = keynames(envFilepath)
79
- try {
80
- if (!/^(?:0[23][0-9a-f]{64}|04[0-9a-f]{128})$/i.test(options.with)) throw new Error('Invalid public key format')
81
- encrypt(options.with, '') // Validate the curve point even if no values need encryption.
82
- } catch {
83
- throw new Errors({ publicKeyName, publicKey: options.with }).invalidPublicKey()
84
- }
85
- if (!matchingKey) {
86
- row.envSrc = mutateSrc({ envSrc: row.envSrc, envFilepath, publicKeyName, publicKeyValue: options.with }).envSrc
87
- row.changed = true
88
- }
89
- }
90
-
91
- const { parsed } = scan(row.envSrc, { ik, ek })
92
- const selected = Object.entries(parsed).filter(([key]) => {
93
- if (isDotenvPublicKey(key)) return false
94
- return schema.exists ? (schema.keys[key]?.fs.encrypted ?? true) : !isPlainKey(key)
95
- })
96
- if (schema.exists && !selected.some(([, values]) => values.some(value => !encrypted(value)))) {
97
- // A missing file still needs writing even when its initial values are
98
- // exempt from encryption. Avoid generating keys, but preserve creation.
99
- if (row.changed) changedFilepaths.push(envFilepath)
100
- else unchangedFilepaths.push(envFilepath)
101
- processedEnvs.push(row)
102
- continue
103
- }
104
-
105
- let publicKey = publickeys(row.envSrc)[0]
106
-
107
- if (!publicKey) {
108
- const { publicKeyName, privateKeyName } = keynames(envFilepath)
109
- const comment = path.basename(envFilepath)
110
-
111
- const result = await storeKeyStorage(storage, undefined, undefined, options, Object.assign(custodyContext, { envFilepath: filepath, keysSrc, privateKeyName, comment, keysFilepath: fk }))
112
- storage = result.storage
113
- publicKey = result.publicKey
114
- row.envSrc = mutateSrc({ envSrc: row.envSrc, envFilepath, keysFilepath: fk, publicKeyName, publicKeyValue: publicKey }).envSrc
115
- row.keySources = { [publicKey]: Object.prototype.hasOwnProperty.call(result.stored, 'keysSrc') ? 'file' : (typeof storage === 'string' ? storage : storage.id) }
116
- const stored = result.stored
117
- if (Object.prototype.hasOwnProperty.call(stored, 'keysSrc')) keysSrc = stored.keysSrc
118
- if (stored.nativePrivateKeyAdded) row.nativePrivateKeyAdded = true
119
- }
120
-
121
- for (const [key, values] of selected) {
122
- const transformedValues = []
123
- for (const value of values) {
124
- if (encrypted(value)) {
125
- transformedValues.push(value) // unchanged
126
- } else {
127
- const encryptedValue = encrypt(publicKey, value)
128
- transformedValues.push(encryptedValue)
129
- }
130
- }
131
-
132
- const before = row.envSrc
133
- row.envSrc = upsert(row.envSrc, key, transformedValues)
134
- if (row.envSrc !== before) {
135
- row.changed = true
136
- row.keys.push(key)
137
- }
138
- }
139
-
140
- if (row.changed) {
141
- changedFilepaths.push(envFilepath)
142
- } else {
143
- unchangedFilepaths.push(envFilepath)
144
- }
145
- } catch (error) {
146
- if (error?.code === 'ENOENT') {
147
- row.error = new Errors({ envFilepath, filepath }).missingEnvFile()
148
- } else {
149
- row.error = error instanceof Error ? error : new Error('Encryption aborted before private key storage completed')
150
- }
151
- }
152
-
153
- processedEnvs.push(row)
154
- }
155
-
156
- return {
157
- keysSrc,
158
- processedEnvs,
159
- changedFilepaths,
160
- unchangedFilepaths
161
- }
162
- }
163
-
164
- module.exports = encryptTransform
@@ -1,144 +0,0 @@
1
- const fsx = require('./../helpers/fsx')
2
- const path = require('path')
3
- const { encrypt, upsert, publickeys } = require('@dotenvx/primitives')
4
-
5
- const TYPE_ENV_FILE = 'envFile'
6
-
7
- const getResolver = require('./../resolvers/get')
8
- const selectEnvfileEnvs = require('../helpers/selectEnvfileEnvs')
9
- const readEnvfile = require('../envfile/parsing/readEnvfile')
10
- const detectEncoding = require('./../helpers/detectEncoding')
11
- const { isPlainKey, mutateSrc } = require('../helpers/cryptography')
12
- const keynames = require('../conventions/keynames')
13
- const Errors = require('../helpers/errors')
14
-
15
- const storeKeyStorage = require('../helpers/storeKeyStorage')
16
-
17
- async function setTransform (options = {}) {
18
- const envs = options.envs || []
19
- const key = options.key
20
- const value = options.value
21
- const fk = options.fk || '.env.keys'
22
- const noArmor = options.noArmor
23
- let storage
24
- const custodyContext = { token: options.token }
25
- const noNative = options.noNative
26
- const noCreate = options.noCreate
27
-
28
- const processedEnvs = []
29
- const changedFilepaths = []
30
- const unchangedFilepaths = []
31
-
32
- // set up keysSrc
33
- let keysSrc
34
- if (await fsx.exists(fk)) {
35
- try {
36
- const encoding = await detectEncoding(fk)
37
- keysSrc = await fsx.readFileX(fk, { encoding })
38
- } catch (err) {
39
- if (err.code === 'EACCES' || err.code === 'EPERM') {
40
- // do nothing (scenario: chmod a-r .env.keys)
41
- } else {
42
- throw err
43
- }
44
- }
45
- }
46
-
47
- for (const env of selectEnvfileEnvs(envs, options)) {
48
- if (env.type !== TYPE_ENV_FILE) {
49
- continue
50
- }
51
-
52
- const envFilepath = env.envFilepath || env.value
53
- const filepath = env.filepath || path.resolve(envFilepath)
54
- const row = { key, value, type: TYPE_ENV_FILE, filepath, envFilepath, changed: false }
55
-
56
- try {
57
- const schema = readEnvfile(undefined, [filepath])
58
- const noEncrypt = !options.encrypt || (schema.exists ? schema.keys[key]?.fs.encrypted === false : isPlainKey(key))
59
- const fileExists = await fsx.exists(filepath)
60
- if (!fileExists && !noCreate) {
61
- row.envSrc = ''
62
- row.changed = true
63
- } else {
64
- const encoding = await detectEncoding(filepath)
65
- row.envSrc = await fsx.readFileX(filepath, { encoding })
66
- }
67
-
68
- if (row.envSrc.trim().length === 0) {
69
- row.envSrc = ''
70
- row.changed = true
71
- }
72
-
73
- let publicKey = publickeys(row.envSrc)[0]
74
-
75
- // only create if missing public key and encryption needed
76
- if (!publicKey && !noEncrypt) {
77
- const { publicKeyName, privateKeyName } = keynames(envFilepath)
78
- const comment = path.basename(envFilepath)
79
-
80
- const result = await storeKeyStorage(storage, undefined, undefined, options, Object.assign(custodyContext, { envFilepath: filepath, keysSrc, privateKeyName, comment, keysFilepath: fk }))
81
- storage = result.storage
82
- publicKey = result.publicKey
83
- row.envSrc = mutateSrc({ envSrc: row.envSrc, envFilepath, keysFilepath: fk, publicKeyName, publicKeyValue: publicKey }).envSrc
84
- row.keySources = { [publicKey]: Object.prototype.hasOwnProperty.call(result.stored, 'keysSrc') ? 'file' : (typeof storage === 'string' ? storage : storage.id) }
85
- const stored = result.stored
86
- if (Object.prototype.hasOwnProperty.call(stored, 'keysSrc')) keysSrc = stored.keysSrc
87
- if (stored.nativePrivateKeyAdded) row.nativePrivateKeyAdded = true
88
- }
89
-
90
- if (noEncrypt) {
91
- const before = row.envSrc
92
- row.envSrc = upsert(row.envSrc, key, value)
93
- if (row.envSrc !== before) {
94
- row.changed = true
95
- }
96
- } else {
97
- // Compare plaintext before rewriting ciphertext; surface any provider approval.
98
- const { parsed, keySources } = await getResolver({
99
- includeKeySources: true,
100
- key,
101
- envs: [env],
102
- all: true,
103
- envKeysFile: fk,
104
- noArmor,
105
- noNative,
106
- no1Password: options.no1Password,
107
- noBitwarden: options.noBitwarden,
108
- onStatus: options.onStatus
109
- })
110
-
111
- row.keySources = row.keySources || keySources
112
- const before = parsed[key]
113
- if (value !== before) {
114
- const encryptedValue = encrypt(publicKey, value)
115
- row.envSrc = upsert(row.envSrc, key, encryptedValue)
116
- row.changed = true
117
- }
118
- }
119
-
120
- if (row.changed) {
121
- changedFilepaths.push(envFilepath)
122
- } else {
123
- unchangedFilepaths.push(envFilepath)
124
- }
125
- } catch (error) {
126
- if (error.code === 'ENOENT') {
127
- row.error = new Errors({ envFilepath, filepath }).missingEnvFile()
128
- } else {
129
- row.error = error
130
- }
131
- }
132
-
133
- processedEnvs.push(row)
134
- }
135
-
136
- return {
137
- keysSrc,
138
- processedEnvs,
139
- changedFilepaths,
140
- unchangedFilepaths
141
- }
142
- }
143
-
144
- module.exports = setTransform
@@ -1,67 +0,0 @@
1
- const depth = require('../lib/helpers/colorDepth')
2
- const Errors = require('../lib/helpers/errors')
3
-
4
- const colors16 = new Map([
5
- ['amber', 33],
6
- ['blue', 34],
7
- ['gray', 37],
8
- ['green', 32],
9
- ['olive', 33],
10
- ['orangered', 33], // mapped to yellow/brown
11
- ['plum', 35], // mapped to magenta
12
- ['red', 31],
13
- ['electricblue', 36],
14
- ['dodgerblue', 36]
15
- ])
16
-
17
- const colors256 = new Map([
18
- ['amber', 136],
19
- ['blue', 21],
20
- ['gray', 244],
21
- ['green', 34],
22
- ['olive', 142],
23
- ['orangered', 130], // burnished copper
24
- ['plum', 182],
25
- ['red', 124], // brighter garnet
26
- ['electricblue', 45],
27
- ['dodgerblue', 33]
28
- ])
29
-
30
- const colorsTrueColor = new Map([
31
- ['amber', [236, 213, 63]],
32
- ['orangered', [138, 90, 43]], // #8A5A2B burnished copper
33
- ['red', [140, 35, 50]] // #8C2332 brighter garnet
34
- ])
35
-
36
- function getColor (color, stream) {
37
- const colorDepth = depth.getColorDepth(stream)
38
- if (!colors256.has(color)) {
39
- throw new Errors({ color }).invalidColor()
40
- }
41
- if (colorDepth >= 24 && colorsTrueColor.has(color)) {
42
- const [r, g, b] = colorsTrueColor.get(color)
43
- return (message) => `\x1b[38;2;${r};${g};${b}m${message}\x1b[39m`
44
- }
45
- if (colorDepth >= 8) {
46
- const code = colors256.get(color)
47
- return (message) => `\x1b[38;5;${code}m${message}\x1b[39m`
48
- }
49
- if (colorDepth >= 4) {
50
- const code = colors16.get(color)
51
- return (message) => `\x1b[${code}m${message}\x1b[39m`
52
- }
53
- return (message) => message
54
- }
55
-
56
- function bold (message, stream) {
57
- if (depth.getColorDepth(stream) >= 4) {
58
- return `\x1b[1m${message}\x1b[22m`
59
- }
60
-
61
- return message
62
- }
63
-
64
- module.exports = {
65
- getColor,
66
- bold
67
- }
@@ -1,148 +0,0 @@
1
- const Errors = require('../lib/helpers/errors')
2
- const { getColor, bold } = require('./colors')
3
-
4
- const levels = {
5
- error: 0,
6
- infoerror: 0,
7
- warn: 1,
8
- success: 2,
9
- info: 2,
10
- help: 2,
11
- verbose: 4,
12
- debug: 5,
13
- silly: 6
14
- }
15
-
16
- const error = (m, stream) => bold(getColor('red', stream)(`☠ ${m}`), stream)
17
- const infoerror = (m, stream) => getColor('gray', stream)(m) // actually an error
18
- const warn = (m, stream) => getColor('orangered', stream)(`⚠ ${m}`)
19
- const success = (m, stream) => getColor('amber', stream)(m)
20
- const info = (m, stream) => getColor('gray', stream)(m)
21
- const help = (m, stream) => getColor('dodgerblue', stream)(m)
22
- const verbose = (m, stream) => getColor('plum', stream)(`┆ ${m}`)
23
- const debug = (m, stream) => getColor('plum', stream)(`┆ ${m}`)
24
-
25
- let currentLevel = levels.info // default log level
26
-
27
- function stderr (level, message) {
28
- if (levels[level] === undefined) {
29
- throw new Errors({ level }).missingLogLevel()
30
- }
31
-
32
- if (levels[level] <= currentLevel) {
33
- const formattedMessage = formatMessage(level, message, process.stderr)
34
- console.error(formattedMessage)
35
- }
36
- }
37
-
38
- function stdout (level, message) {
39
- if (levels[level] === undefined) {
40
- throw new Errors({ level }).missingLogLevel()
41
- }
42
-
43
- if (levels[level] <= currentLevel) {
44
- const formattedMessage = formatMessage(level, message, process.stdout)
45
- console.log(formattedMessage)
46
- }
47
- }
48
-
49
- function formatMessage (level, message, stream) {
50
- const formattedMessage = typeof message === 'object' ? JSON.stringify(message) : message
51
-
52
- switch (level.toLowerCase()) {
53
- // errors
54
- case 'error':
55
- return error(formattedMessage, stream)
56
- case 'infoerror':
57
- return infoerror(formattedMessage, stream)
58
- // warns
59
- case 'warn':
60
- return warn(formattedMessage, stream)
61
- // successes
62
- case 'success':
63
- return success(formattedMessage, stream)
64
- // info
65
- case 'info':
66
- return info(formattedMessage, stream)
67
- // help
68
- case 'help':
69
- return help(formattedMessage, stream)
70
- // verbose
71
- case 'verbose':
72
- return verbose(formattedMessage, stream)
73
- // debug
74
- case 'debug':
75
- return debug(formattedMessage, stream)
76
- }
77
- }
78
-
79
- const logger = {
80
- // track level
81
- level: 'info',
82
-
83
- // errors
84
- error: (msg) => stderr('error', msg),
85
- infoerror: (msg) => stderr('infoerror', msg),
86
- // warns
87
- warn: (msg) => stderr('warn', msg),
88
- // success
89
- success: (msg) => stderr('success', msg),
90
- // info
91
- info: (msg) => stdout('info', msg),
92
- // help
93
- help: (msg) => stderr('help', msg),
94
- // verbose
95
- verbose: (msg) => stderr('verbose', msg),
96
- // debug
97
- debug: (msg) => stderr('debug', msg),
98
- setLevel: (level) => {
99
- if (levels[level] !== undefined) {
100
- currentLevel = levels[level]
101
- logger.level = level
102
- }
103
- },
104
- setName: (name) => {
105
- logger.name = name
106
- },
107
- setVersion: (version) => {
108
- logger.version = version
109
- }
110
- }
111
-
112
- function setLogLevel (options) {
113
- const logLevel = options.debug
114
- ? 'debug'
115
- : options.verbose
116
- ? 'verbose'
117
- : options.quiet
118
- ? 'error'
119
- : options.logLevel
120
-
121
- if (!logLevel) return
122
- logger.setLevel(logLevel)
123
- // Only log which level it's setting if it's not set to quiet mode
124
- if (!options.quiet || (options.quiet && logLevel !== 'error')) {
125
- logger.debug(`setting log level to: ${logLevel}`)
126
- }
127
- }
128
-
129
- function setLogName (options) {
130
- const logName = options.logName
131
- if (!logName) return
132
- logger.setName(logName)
133
- }
134
-
135
- function setLogVersion (options) {
136
- const logVersion = options.logVersion
137
- if (!logVersion) return
138
- logger.setVersion(logVersion)
139
- }
140
-
141
- module.exports = {
142
- logger,
143
- getColor,
144
- setLogLevel,
145
- setLogName,
146
- setLogVersion,
147
- levels
148
- }