@dotenvx/dotenvx 2.33.0 → 2.34.1

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 (45) hide show
  1. package/CHANGELOG.md +16 -1
  2. package/README.md +0 -1
  3. package/package.json +4 -4
  4. package/src/cli/actions/get.js +1 -2
  5. package/src/cli/actions/keypair.js +1 -0
  6. package/src/cli/actions/run.js +2 -12
  7. package/src/cli/dotenvx.js +1 -6
  8. package/src/db/session.js +1 -1
  9. package/src/lib/custodians/index.js +12 -10
  10. package/src/lib/custodians/local/bitwarden.js +5 -15
  11. package/src/lib/custodians/local/native/backend.js +4 -10
  12. package/src/lib/custodians/local/native/index.js +1 -1
  13. package/src/lib/custodians/local/onepassword.js +4 -24
  14. package/src/lib/custodians/managed/armor/index.js +0 -10
  15. package/src/lib/custodians/resolveLocalKey.js +3 -4
  16. package/src/lib/decryptors/armor/index.js +6 -7
  17. package/src/lib/helpers/errors.js +0 -13
  18. package/src/lib/helpers/matchesStoredKey.js +1 -12
  19. package/src/lib/helpers/parseWithDecryptor.js +27 -4
  20. package/src/lib/helpers/prompts.js +18 -13
  21. package/src/lib/helpers/selectKeyStorage.js +1 -1
  22. package/src/lib/helpers/storeKeyStorage.js +0 -1
  23. package/src/lib/main.d.ts +0 -4
  24. package/src/lib/main.js +3 -10
  25. package/src/lib/providers/index.js +53 -53
  26. package/src/lib/resolvers/envs.js +5 -14
  27. package/src/lib/resolvers/get.js +0 -1
  28. package/src/lib/resolvers/keypair.js +39 -4
  29. package/src/lib/services/validate.js +0 -1
  30. package/src/lib/transforms/encrypt.js +2 -2
  31. package/src/cli/actions/lock/down.js +0 -54
  32. package/src/cli/actions/lock/up.js +0 -62
  33. package/src/cli/commands/lock.js +0 -36
  34. package/src/lib/api/postArmorDecrypt.js +0 -49
  35. package/src/lib/api/postArmorKeyring.js +0 -50
  36. package/src/lib/custodians/lock.js +0 -80
  37. package/src/lib/custodians/managed/armor/get.js +0 -44
  38. package/src/lib/helpers/lockedValue.js +0 -24
  39. package/src/lib/helpers/resolveLockPassword.js +0 -5
  40. package/src/lib/helpers/unlockedValue.js +0 -26
  41. package/src/lib/helpers/withLockedKeys.js +0 -41
  42. package/src/lib/providers/provider-worker.js +0 -6
  43. package/src/lib/services/armorKeyring.js +0 -119
  44. package/src/lib/services/lockDown.js +0 -157
  45. package/src/lib/services/lockUp.js +0 -150
@@ -1,44 +0,0 @@
1
- const Session = require('../../../../db/session')
2
- const ArmorKeyring = require('../../../services/armorKeyring')
3
- const armoredKeyDisplay = require('../../../helpers/armoredKeyDisplay')
4
- const isNetworkError = require('../../../helpers/isNetworkError')
5
- const listenForOpenKey = require('../../../helpers/listenForOpenKey')
6
- const openUrl = require('../../../helpers/openUrl')
7
-
8
- async function index (publicKeyHex, options = {}) {
9
- const sesh = new Session()
10
-
11
- const hostname = sesh.hostname()
12
- const token = sesh.token()
13
- const devicePublicKey = sesh.devicePublicKey()
14
-
15
- const keyring = new ArmorKeyring(
16
- hostname,
17
- token,
18
- devicePublicKey,
19
- publicKeyHex
20
- )
21
- let cleanupOpenKeyListener = () => {}
22
- if (options.onStatus) {
23
- keyring.onApprovalRequired = ({ approvalUri, code }) => {
24
- const keyDisplay = armoredKeyDisplay(publicKeyHex)
25
- const keySuffix = keyDisplay ? ` (${keyDisplay})` : ''
26
- options.onStatus(`[${code}] press Enter to open [${approvalUri}] and approve${keySuffix}`)
27
- cleanupOpenKeyListener = listenForOpenKey(() => openUrl(approvalUri))
28
- }
29
- }
30
-
31
- try {
32
- return await keyring.run() // { "publicKey": "privateKey" }
33
- } catch (error) {
34
- if (isNetworkError(error)) {
35
- return {}
36
- }
37
-
38
- throw error
39
- } finally {
40
- cleanupOpenKeyListener()
41
- }
42
- }
43
-
44
- module.exports = index
@@ -1,24 +0,0 @@
1
- const crypto = require('crypto')
2
-
3
- function lockedValue (privateKey, passphrase, publicKey) {
4
- const salt = crypto.randomBytes(16)
5
- const iv = crypto.randomBytes(12)
6
- const key = crypto.scryptSync(passphrase, salt, 32)
7
- const cipher = crypto.createCipheriv('aes-256-gcm', key, iv)
8
- const ciphertext = Buffer.concat([
9
- cipher.update(privateKey, 'utf8'),
10
- cipher.final()
11
- ])
12
- const tag = cipher.getAuthTag()
13
- const payload = Buffer.concat([
14
- Buffer.from([1]),
15
- salt,
16
- iv,
17
- tag,
18
- ciphertext
19
- ]).toString('base64url')
20
-
21
- return `locked:${publicKey}:${payload}`
22
- }
23
-
24
- module.exports = lockedValue
@@ -1,5 +0,0 @@
1
- function resolveLockPassword (options = {}) {
2
- return options.lockPassword !== undefined ? options.lockPassword : process.env.DOTENVX_LOCK_PASSWORD
3
- }
4
-
5
- module.exports = resolveLockPassword
@@ -1,26 +0,0 @@
1
- const crypto = require('crypto')
2
-
3
- function unlockedValue (lockedPrivateKey, passphrase) {
4
- const parts = lockedPrivateKey.split(':')
5
- const payload = Buffer.from(parts.slice(2).join(':'), 'base64url')
6
- const version = payload.subarray(0, 1)[0]
7
- const salt = payload.subarray(1, 17)
8
- const iv = payload.subarray(17, 29)
9
- const tag = payload.subarray(29, 45)
10
- const ciphertext = payload.subarray(45)
11
- const key = crypto.scryptSync(passphrase, salt, 32)
12
- const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv)
13
-
14
- if (version !== 1) {
15
- throw new Error('unsupported locked private key version')
16
- }
17
-
18
- decipher.setAuthTag(tag)
19
-
20
- return Buffer.concat([
21
- decipher.update(ciphertext),
22
- decipher.final()
23
- ]).toString('utf8')
24
- }
25
-
26
- module.exports = unlockedValue
@@ -1,41 +0,0 @@
1
- const fs = require('fs')
2
- const { scan } = require('@dotenvx/primitives')
3
- const protection = require('../custodians/lock')
4
-
5
- // Primitives already resolve raw file/environment keys first. This provider
6
- // supplies locked keys only when the requested private key is still missing.
7
- function withLockedKeys (options, sync = false) {
8
- const candidates = []
9
- const paths = Array.isArray(options.fk) ? options.fk : [options.fk || '.env.keys']
10
- for (const filepath of paths) {
11
- let src
12
- try { src = fs.readFileSync(filepath, 'utf8') } catch { continue }
13
- for (const [name, values] of Object.entries(scan(src).parsed)) {
14
- if (name.startsWith('DOTENV_PRIVATE_KEY')) candidates.push(...values.flatMap(value => value.split(',')))
15
- }
16
- }
17
- // Environment assignments take precedence over key files.
18
- for (const [name, value] of Object.entries(options.processEnv || process.env)) {
19
- if (name.startsWith('DOTENV_PRIVATE_KEY') && typeof value === 'string') candidates.push(...value.split(','))
20
- }
21
- if (!candidates.some(value => value.startsWith('locked:'))) return options
22
- candidates.reverse()
23
- function find (publicKey) {
24
- return candidates.find(value => value.startsWith(`locked:${publicKey}:`))
25
- }
26
- const provider = options.provider
27
- return {
28
- ...options,
29
- provider: sync
30
- ? publicKey => {
31
- const value = find(publicKey)
32
- return value ? protection.unlockSync(publicKey, { [publicKey]: value }, options) : (provider ? provider(publicKey) : {})
33
- }
34
- : async publicKey => {
35
- const value = find(publicKey)
36
- return value ? protection.unlock(publicKey, { [publicKey]: value }, options) : (provider ? provider(publicKey) : {})
37
- }
38
- }
39
- }
40
-
41
- module.exports = withLockedKeys
@@ -1,6 +0,0 @@
1
- const { runAsWorker } = require('@dotenvx/tooling')
2
- const provider = require('../custodians/managed/armor').get
3
-
4
- runAsWorker(async (publicKeyHex) => {
5
- return provider(publicKeyHex)
6
- })
@@ -1,119 +0,0 @@
1
- const PostArmorKeyring = require('../api/postArmorKeyring')
2
-
3
- const ACCESS_APPROVAL_REQUIRED = 'ACCESS_APPROVAL_REQUIRED'
4
- const ACCESS_APPROVAL_PENDING = 'ACCESS_APPROVAL_PENDING'
5
- const ACCESS_PENDING = 'ACCESS_PENDING'
6
- const POLL_INTERVAL = 1000
7
- const POLL_TIMEOUT = 5 * 60 * 1000
8
-
9
- function grantTokenFromError (error) {
10
- return error.meta && error.meta.grant_token
11
- }
12
-
13
- function isAccessApprovalRequired (error) {
14
- return error.code === ACCESS_APPROVAL_REQUIRED
15
- }
16
-
17
- function isAccessPending (error) {
18
- return error.code === ACCESS_PENDING || error.code === ACCESS_APPROVAL_PENDING
19
- }
20
-
21
- function approvalUriFromError (error) {
22
- if (error.meta) {
23
- return error.meta.approval_uri
24
- }
25
-
26
- return error.json && error.json.error && error.json.error.approval_uri
27
- }
28
-
29
- async function sleep (ms) {
30
- await new Promise(resolve => setTimeout(resolve, ms))
31
- }
32
-
33
- function approvalTimeoutError () {
34
- const code = 'ACCESS_APPROVAL_TIMEOUT'
35
- const error = new Error(`[${code}] approval timed out after 5 minutes`)
36
- error.code = code
37
- return error
38
- }
39
-
40
- class ArmorKeyring {
41
- constructor (hostname, token, devicePublicKey, publicKey) {
42
- this.hostname = hostname
43
- this.token = token
44
- this.devicePublicKey = devicePublicKey
45
- this.publicKey = publicKey
46
- this.onApprovalRequired = null
47
- }
48
-
49
- async request (grantToken) {
50
- if (grantToken !== undefined) {
51
- return await new PostArmorKeyring(
52
- this.hostname,
53
- this.token,
54
- this.devicePublicKey,
55
- this.publicKey,
56
- grantToken
57
- ).run()
58
- }
59
-
60
- return await new PostArmorKeyring(
61
- this.hostname,
62
- this.token,
63
- this.devicePublicKey,
64
- this.publicKey
65
- ).run()
66
- }
67
-
68
- async requestWithApprovalPolling () {
69
- try {
70
- return await this.request()
71
- } catch (error) {
72
- if (!isAccessApprovalRequired(error)) {
73
- throw error
74
- }
75
-
76
- const grantToken = grantTokenFromError(error)
77
- if (!grantToken) {
78
- throw error
79
- }
80
-
81
- const approvalUri = approvalUriFromError(error)
82
- if (!approvalUri) {
83
- throw error
84
- }
85
-
86
- if (this.onApprovalRequired) {
87
- this.onApprovalRequired({ approvalUri, code: error.code })
88
- }
89
-
90
- return await this.poll(grantToken)
91
- }
92
- }
93
-
94
- async poll (grantToken) {
95
- const startedAt = Date.now()
96
-
97
- while (true) {
98
- try {
99
- return await this.request(grantToken)
100
- } catch (error) {
101
- if (!isAccessPending(error)) {
102
- throw error
103
- }
104
-
105
- if (Date.now() - startedAt >= POLL_TIMEOUT) {
106
- throw approvalTimeoutError()
107
- }
108
-
109
- await sleep(POLL_INTERVAL)
110
- }
111
- }
112
- }
113
-
114
- async run () {
115
- return await this.requestWithApprovalPolling()
116
- }
117
- }
118
-
119
- module.exports = ArmorKeyring
@@ -1,157 +0,0 @@
1
- const fs = require('fs')
2
- const { derive, publickeys, scan } = require('@dotenvx/primitives')
3
-
4
- const Errors = require('../helpers/errors')
5
- const upsertEnvKey = require('../helpers/upsertEnvKey')
6
-
7
- function latestValues (parsed) {
8
- const result = {}
9
-
10
- for (const [key, values] of Object.entries(parsed)) {
11
- result[key] = values[values.length - 1]
12
- }
13
-
14
- return result
15
- }
16
-
17
- function lockedCandidates (keysSrc) {
18
- const { parsed } = scan(keysSrc)
19
- const values = latestValues(parsed)
20
- const candidates = []
21
-
22
- for (const [privateKeyName, privateKey] of Object.entries(values)) {
23
- if (!privateKeyName.startsWith('DOTENV_PRIVATE_KEY')) continue
24
- if (!privateKey.startsWith('locked:')) continue
25
-
26
- const parts = privateKey.split(':')
27
- if (parts.length < 3) continue
28
-
29
- candidates.push({
30
- privateKeyName,
31
- lockedPrivateKeyValue: privateKey,
32
- publicKeyValue: parts[1]
33
- })
34
- }
35
-
36
- return candidates
37
- }
38
-
39
- function privateKeyring (keysSrc) {
40
- const { parsed } = scan(keysSrc)
41
- const values = latestValues(parsed)
42
- const keyring = {}
43
-
44
- for (const [privateKeyName, privateKey] of Object.entries(values)) {
45
- if (!privateKeyName.startsWith('DOTENV_PRIVATE_KEY')) continue
46
- if (privateKey.startsWith('locked:')) continue
47
-
48
- try {
49
- const publicKey = derive(privateKey)
50
- keyring[publicKey] = { privateKeyName, privateKeyValue: privateKey }
51
- } catch {}
52
- }
53
-
54
- return keyring
55
- }
56
-
57
- class LockDown {
58
- constructor (envFile = '.env', envKeysFile = '.env.keys') {
59
- this.envFile = envFile
60
- this.envKeysFile = envKeysFile
61
- }
62
-
63
- lockedCandidates () {
64
- const keysSrc = fs.readFileSync(this.envKeysFile, 'utf8')
65
- const candidates = lockedCandidates(keysSrc)
66
-
67
- if (candidates.length < 1) {
68
- throw new Errors({ key: 'DOTENV_PRIVATE_KEY' }).missingKey()
69
- }
70
-
71
- return candidates
72
- }
73
-
74
- plan () {
75
- const envSrc = fs.readFileSync(this.envFile, 'utf8')
76
- const keysSrc = fs.readFileSync(this.envKeysFile, 'utf8')
77
- const publicKeyValues = publickeys(envSrc)
78
- const lockedByPublicKey = {}
79
- const plaintextByPublicKey = privateKeyring(keysSrc)
80
- const locked = []
81
- const alreadyUnlocked = []
82
-
83
- for (const lockedPrivateKey of lockedCandidates(keysSrc)) {
84
- lockedByPublicKey[lockedPrivateKey.publicKeyValue] = lockedPrivateKey
85
- }
86
-
87
- for (const publicKey of publicKeyValues) {
88
- const lockedPrivateKey = lockedByPublicKey[publicKey]
89
- if (lockedPrivateKey) {
90
- locked.push(lockedPrivateKey)
91
- continue
92
- }
93
-
94
- const privateKey = plaintextByPublicKey[publicKey]
95
- if (privateKey) {
96
- alreadyUnlocked.push({
97
- changed: false,
98
- privateKeyName: privateKey.privateKeyName,
99
- privateKeyValue: privateKey.privateKeyValue,
100
- lockedPrivateKeyValue: undefined,
101
- publicKeyValue: publicKey
102
- })
103
- }
104
- }
105
-
106
- if (locked.length < 1 && alreadyUnlocked.length < 1) {
107
- throw new Errors({ key: 'DOTENV_PRIVATE_KEY' }).missingKey()
108
- }
109
-
110
- return { locked, alreadyUnlocked }
111
- }
112
-
113
- run (unlockPrivateKey) {
114
- const { locked, alreadyUnlocked } = this.plan()
115
- const results = [...alreadyUnlocked]
116
-
117
- if (locked.length > 0 && !unlockPrivateKey) {
118
- throw new Error('missing unlock private key function')
119
- }
120
-
121
- for (const lockedPrivateKey of locked) {
122
- let privateKeyValue
123
-
124
- try {
125
- privateKeyValue = unlockPrivateKey(lockedPrivateKey.lockedPrivateKeyValue)
126
- } catch (error) {
127
- throw new Errors({ privateKeyName: lockedPrivateKey.privateKeyName }).invalidPassphrase()
128
- }
129
-
130
- const publicKeyValue = derive(privateKeyValue)
131
-
132
- if (publicKeyValue !== lockedPrivateKey.publicKeyValue) continue
133
-
134
- const result = upsertEnvKey(lockedPrivateKey.privateKeyName, privateKeyValue, this.envKeysFile)
135
-
136
- results.push({
137
- changed: result.changed,
138
- privateKeyName: lockedPrivateKey.privateKeyName,
139
- privateKeyValue,
140
- lockedPrivateKeyValue: lockedPrivateKey.lockedPrivateKeyValue,
141
- publicKeyValue
142
- })
143
- }
144
-
145
- if (results.length < 1) {
146
- throw new Errors({ key: 'DOTENV_PRIVATE_KEY' }).missingKey()
147
- }
148
-
149
- return {
150
- changed: results.some(result => result.changed),
151
- results
152
- }
153
- }
154
- }
155
-
156
- module.exports = LockDown
157
- module.exports.lockedCandidates = lockedCandidates
@@ -1,150 +0,0 @@
1
- const fs = require('fs')
2
- const { derive, publickeys, scan } = require('@dotenvx/primitives')
3
-
4
- const Errors = require('../helpers/errors')
5
- const upsertEnvKey = require('../helpers/upsertEnvKey')
6
-
7
- function latestValues (parsed) {
8
- const result = {}
9
-
10
- for (const [key, values] of Object.entries(parsed)) {
11
- result[key] = values[values.length - 1]
12
- }
13
-
14
- return result
15
- }
16
-
17
- function privateKeyring (keysSrc) {
18
- const { parsed } = scan(keysSrc)
19
- const values = latestValues(parsed)
20
- const keyring = {}
21
-
22
- for (const [privateKeyName, privateKey] of Object.entries(values)) {
23
- if (!privateKeyName.startsWith('DOTENV_PRIVATE_KEY')) continue
24
-
25
- try {
26
- const publicKey = derive(privateKey)
27
- keyring[publicKey] = { privateKeyName, privateKeyValue: privateKey }
28
- } catch {}
29
- }
30
-
31
- return keyring
32
- }
33
-
34
- function lockedKeyring (keysSrc) {
35
- const { parsed } = scan(keysSrc)
36
- const values = latestValues(parsed)
37
- const keyring = {}
38
-
39
- for (const [privateKeyName, privateKey] of Object.entries(values)) {
40
- if (!privateKeyName.startsWith('DOTENV_PRIVATE_KEY')) continue
41
- if (!privateKey.startsWith('locked:')) continue
42
-
43
- const parts = privateKey.split(':')
44
- if (parts.length < 3) continue
45
-
46
- const publicKey = parts[1]
47
- keyring[publicKey] = {
48
- privateKeyName,
49
- lockedPrivateKeyValue: privateKey,
50
- publicKeyValue: publicKey
51
- }
52
- }
53
-
54
- return keyring
55
- }
56
-
57
- class LockUp {
58
- constructor (envFile = '.env', envKeysFile = '.env.keys') {
59
- this.envFile = envFile
60
- this.envKeysFile = envKeysFile
61
- }
62
-
63
- plan () {
64
- const envFile = this.envFile
65
- const envKeysFile = this.envKeysFile
66
-
67
- const envSrc = fs.readFileSync(envFile, 'utf8')
68
- const keysSrc = fs.readFileSync(envKeysFile, 'utf8')
69
- const ring = privateKeyring(keysSrc)
70
- const lockedRing = lockedKeyring(keysSrc)
71
- const matches = []
72
- const alreadyLocked = []
73
-
74
- for (const publicKey of publickeys(envSrc)) {
75
- const privateKey = ring[publicKey]
76
- if (privateKey) {
77
- matches.push({
78
- privateKeyName: privateKey.privateKeyName,
79
- privateKeyValue: privateKey.privateKeyValue,
80
- publicKeyValue: publicKey
81
- })
82
- continue
83
- }
84
-
85
- const lockedPrivateKey = lockedRing[publicKey]
86
- if (lockedPrivateKey) {
87
- alreadyLocked.push(lockedPrivateKey)
88
- }
89
- }
90
-
91
- if (matches.length < 1 && alreadyLocked.length < 1) {
92
- throw new Errors({ key: 'DOTENV_PRIVATE_KEY' }).missingKey()
93
- }
94
-
95
- return { matches, alreadyLocked }
96
- }
97
-
98
- matches () {
99
- const { matches } = this.plan()
100
-
101
- if (matches.length < 1) {
102
- throw new Errors({ key: 'DOTENV_PRIVATE_KEY' }).missingKey()
103
- }
104
-
105
- return matches
106
- }
107
-
108
- run (lockPrivateKey) {
109
- if (!lockPrivateKey) {
110
- throw new Error('missing lock private key function')
111
- }
112
-
113
- const envKeysFile = this.envKeysFile
114
- const results = []
115
-
116
- const { matches, alreadyLocked } = this.plan()
117
-
118
- for (const privateKey of matches) {
119
- const lockedPrivateKey = lockPrivateKey(privateKey.privateKeyValue, privateKey.publicKeyValue)
120
- const result = upsertEnvKey(privateKey.privateKeyName, lockedPrivateKey, envKeysFile)
121
-
122
- results.push({
123
- changed: result.changed,
124
- privateKeyName: privateKey.privateKeyName,
125
- privateKeyValue: privateKey.privateKeyValue,
126
- lockedPrivateKeyValue: lockedPrivateKey,
127
- publicKeyValue: privateKey.publicKeyValue
128
- })
129
- }
130
-
131
- for (const lockedPrivateKey of alreadyLocked) {
132
- results.push({
133
- changed: false,
134
- privateKeyName: lockedPrivateKey.privateKeyName,
135
- privateKeyValue: undefined,
136
- lockedPrivateKeyValue: lockedPrivateKey.lockedPrivateKeyValue,
137
- publicKeyValue: lockedPrivateKey.publicKeyValue,
138
- alreadyLocked: true
139
- })
140
- }
141
-
142
- return {
143
- changed: results.some(result => result.changed),
144
- results
145
- }
146
- }
147
- }
148
-
149
- module.exports = LockUp
150
- module.exports.privateKeyring = privateKeyring