@dotenvx/dotenvx 2.32.4 → 2.33.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.
- package/CHANGELOG.md +13 -1
- package/README.md +24 -25
- package/package.json +3 -11
- package/src/cli/actions/check.js +6 -11
- package/src/cli/actions/encrypt.js +2 -2
- package/src/cli/actions/get.js +3 -3
- package/src/cli/actions/init.js +4 -4
- package/src/cli/actions/policy.js +14 -0
- package/src/cli/actions/primitivesCheck.js +32 -0
- package/src/cli/actions/primitivesProfile.js +15 -0
- package/src/cli/actions/primitivesRedact.js +19 -0
- package/src/cli/actions/protectStdin.js +2 -2
- package/src/cli/actions/run.js +6 -34
- package/src/cli/actions/set.js +1 -1
- package/src/cli/commands/primitives.js +28 -1
- package/src/cli/dotenvx.js +5 -0
- package/src/lib/custodians/local/onepassword.js +3 -5
- package/src/lib/{envspec → envfile}/discovery/discoverEnvFiles.js +1 -1
- package/src/lib/{envspec → envfile}/discovery/scanSource.js +1 -1
- package/src/lib/{envspec/parsing/formatEnvspecSyntaxError.js → envfile/parsing/formatEnvfileSyntaxError.js} +1 -1
- package/src/lib/envfile/parsing/readEnvfile.js +43 -0
- package/src/lib/envfile/parsing/resolveEnvfilePath.js +5 -0
- package/src/lib/{envspec/rendering/normalizeEnvspec.js → envfile/rendering/normalizeEnvfile.js} +1 -1
- package/src/lib/{envspec/rendering/previewEnvspec.js → envfile/rendering/previewEnvfile.js} +7 -15
- package/src/lib/{envspec/rendering/renderEnvspec.js → envfile/rendering/renderEnvfile.js} +3 -3
- package/src/lib/{envspec/validation/protectEnvspec.js → envfile/validation/protectEnvfile.js} +7 -7
- package/src/lib/envfile/validation/validateEnvfile.js +14 -0
- package/src/lib/helpers/errors.js +3 -12
- package/src/lib/helpers/executeCommand.js +1 -2
- package/src/lib/helpers/parseWithDecryptor.js +1 -9
- package/src/lib/helpers/redactOutput.js +30 -32
- package/src/lib/helpers/redactProcessOutput.js +58 -0
- package/src/lib/helpers/redactedValues.js +2 -2
- package/src/lib/helpers/selectEnvfileEnvs.js +29 -0
- package/src/lib/main.d.ts +12 -2
- package/src/lib/main.js +13 -19
- package/src/lib/resolvers/envs.js +5 -15
- package/src/lib/resolvers/get.js +2 -2
- package/src/lib/services/init.js +4 -4
- package/src/lib/services/validate.js +20 -31
- package/src/lib/transforms/encrypt.js +5 -5
- package/src/lib/transforms/set.js +5 -3
- package/src/lib/envspec/parsing/envspec.peggy +0 -105
- package/src/lib/envspec/parsing/envspecParser.js +0 -2851
- package/src/lib/envspec/parsing/readEnvspec.js +0 -144
- package/src/lib/envspec/parsing/resolveEnvspecPath.js +0 -11
- package/src/lib/envspec/validation/encryptedSources.js +0 -15
- package/src/lib/envspec/validation/validate.js +0 -109
- package/src/lib/envspec/validation/validateEnvspec.js +0 -57
- package/src/lib/helpers/isValidEmail.js +0 -11
- package/src/lib/helpers/isValidUrl.js +0 -9
- package/src/lib/proxy/configureProxy.js +0 -55
- package/src/lib/proxy/eligibleHeaders.js +0 -16
- package/src/lib/proxy/prepareProxy.js +0 -24
- package/src/lib/proxy/proxyCertificates.js +0 -44
- package/src/lib/proxy/proxyForward.js +0 -65
- package/src/lib/proxy/proxyPreload.js +0 -21
- package/src/lib/proxy/proxyPreloadSource.js +0 -3
- package/src/lib/proxy/proxyServer.js +0 -175
- /package/src/lib/{envspec → envfile}/discovery/extractEnvKeys.js +0 -0
- /package/src/lib/{envspec → envfile}/validation/diagnosticLocation.js +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,7 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
-
[Unreleased](https://github.com/dotenvx/dotenvx/compare/v2.
|
|
5
|
+
[Unreleased](https://github.com/dotenvx/dotenvx/compare/v2.33.0...main)
|
|
6
|
+
|
|
7
|
+
## [2.33.0](https://github.com/dotenvx/dotenvx/compare/v2.32.4...v2.33.0) (2026-10-05)
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
* Add Envfile profiles for selecting .env file combinations with `--profile` or `DOTENV_ENV`.
|
|
12
|
+
* Automatically redact stdout/stderr from `config()` when an Envfile is present.
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
|
|
16
|
+
* Move Envfile processing into `@dotenvx/primitives`.
|
|
17
|
+
* First file wins when combining Envfile rules. `run` and `check` validate final values; encryption rules apply to `encrypt`, `set`, and `protect`.
|
|
6
18
|
|
|
7
19
|
## [2.32.4](https://github.com/dotenvx/dotenvx/compare/v2.32.3...v2.32.4) (2026-10-01)
|
|
8
20
|
|
package/README.md
CHANGED
|
@@ -1571,7 +1571,7 @@ Available log levels are `error, warn, info, verbose, debug, silly` ([source](ht
|
|
|
1571
1571
|
|
|
1572
1572
|
`Envfile` is like a Dockerfile for your secrets.
|
|
1573
1573
|
|
|
1574
|
-
When an `Envfile` is present, `run` and `check` validate the same requirements: required values, types, enums,
|
|
1574
|
+
When an `Envfile` is present, `run` and `check` validate the same requirements: required values, types, enums, and ranges. No validation flag is needed. `run` performs validation before starting your command.
|
|
1575
1575
|
|
|
1576
1576
|
```ruby
|
|
1577
1577
|
# Envfile
|
|
@@ -1585,11 +1585,11 @@ $ dotenvx run -- node index.js
|
|
|
1585
1585
|
[INVALID_ENV] DATABASE_URL is required; API_KEY is required
|
|
1586
1586
|
```
|
|
1587
1587
|
|
|
1588
|
-
Envfile validation failures
|
|
1588
|
+
Envfile validation failures warn by default; `run --strict` stops before launching the command. A `strict true` setting in Envfile, at the root or in an active file block, also makes its validation failures fatal. `check` always exits with failure when validation fails. Strictness changes enforcement, not which rules are checked. Invalid Envfile syntax always stops execution.
|
|
1589
1589
|
|
|
1590
|
-
Envfile values default to `redacted: true`: `run` masks their values in child stdout/stderr and resolved-value debug output, while the child still receives the real values. Set `redacted: false` for values that may appear in output. This is independent of `encrypted: true`, which
|
|
1590
|
+
Envfile values default to `redacted: true`: `run` masks their values in child stdout/stderr and resolved-value debug output, while the child still receives the real values. Set `redacted: false` for values that may appear in output. This is independent of `encrypted: true`, which controls encryption in `encrypt`, `set`, and `protect`.
|
|
1591
1591
|
|
|
1592
|
-
The synchronous `dotenvx.config()` API also reads `Envfile` from the current working directory and validates the final resolved values using the same rules, including selected file blocks
|
|
1592
|
+
The synchronous `dotenvx.config()` API also reads `Envfile` from the current working directory and validates the final resolved values using the same rules, including selected file blocks. Validation failures warn and are returned in `{ parsed, error }`; `config({ strict: true })` or an applicable Envfile `strict true` setting makes failures throw. `ignore: ['INVALID_ENV']` suppresses validation failures. Invalid Envfile syntax always throws before loading values. With an Envfile, `config()` automatically redacts subsequent writes through Node's stdout/stderr streams, including existing `console.log` and `console.error` calls. Environment values and returned values stay unchanged. Repeated calls retain earlier secrets and add newly loaded ones. An unfinished secret prefix is masked at shutdown. Direct native writes and child processes inheriting file descriptors bypass these wrappers; use `dotenvx run` to capture child output.
|
|
1593
1593
|
|
|
1594
1594
|
```ruby
|
|
1595
1595
|
file ".env.development" do
|
|
@@ -1602,13 +1602,13 @@ end
|
|
|
1602
1602
|
|
|
1603
1603
|
`spec` infers `redacted: false` for names starting with `PUBLIC`, `VITE`, `NEXT_PUBLIC`, or `NUXT_PUBLIC`, or containing `PUBLIC` anywhere (case-sensitive). This affects visibility only; public names still require encryption unless explicitly exempted. Other names, including those ending in `_PLAIN`, remain redacted.
|
|
1604
1604
|
|
|
1605
|
-
`dotenvx spec` always generates `Envfile
|
|
1605
|
+
`dotenvx spec` always generates `Envfile` with `strict true` at the top, including with `--overwrite` or `--stdout`. Generated policies stop commands on validation failures. Existing policies that omit `strict` continue to warn.
|
|
1606
1606
|
|
|
1607
1607
|
If an Envfile already exists, `spec` leaves it untouched and prints `○ Envfile already exists [edit or run: spec --overwrite]`. Use `spec --overwrite` to regenerate it from the selected inputs, replacing custom rules, comments, and blocks for files absent from this machine. Interactive overwrite runs show the full file-and-code checklist with the prompt `Recreate Envfile from .env files and code`. Noninteractive runs use `.env.example` and `.env`, or the file selected with `-f`. Input validation finishes before the old Envfile is replaced; cancelled selection leaves it untouched. Symlinked Envfiles cannot be overwritten.
|
|
1608
1608
|
|
|
1609
1609
|
Use `dotenvx spec --stdout` to print the generated Envfile without writing it, or `dotenvx spec --stdout -f .env.production` to select an input file. This works even when an Envfile already exists; `--stdout` leaves it untouched even with `--overwrite`. Prompts and progress stay on stderr so stdout contains only the generated content.
|
|
1610
1610
|
|
|
1611
|
-
Root and file-level `redacted` settings are not supported. Use per-key `redacted: false` to permit output visibility. When selected
|
|
1611
|
+
Root and file-level `redacted` settings are not supported. Use per-key `redacted: false` to permit output visibility. When selected files specify conflicting options, the first file's explicit option wins. `check` previews values with policy-driven redaction and reports issues per key.
|
|
1612
1612
|
|
|
1613
1613
|
|
|
1614
1614
|
</details>
|
|
@@ -1627,11 +1627,20 @@ file ".env.production" do
|
|
|
1627
1627
|
end
|
|
1628
1628
|
```
|
|
1629
1629
|
|
|
1630
|
-
|
|
1630
|
+
Profiles select file combinations:
|
|
1631
|
+
|
|
1632
|
+
```ruby
|
|
1633
|
+
profile "development", files: [".env.local", ".env"]
|
|
1634
|
+
profile "production", files: [".env.production"]
|
|
1635
|
+
```
|
|
1636
|
+
|
|
1637
|
+
Selection order is explicit `-f`, `--profile`, `DOTENV_FILE` / `DOTENV_PATH` / `DOTENV_F`, then `DOTENV_ENV`, `APP_ENV`, `RAILS_ENV`, `RACK_ENV`, `MIX_ENV`, and `NODE_ENV`. Without a selector, the development profile is used if declared; otherwise `.env`. Explicit unknown profiles fail.
|
|
1638
|
+
|
|
1639
|
+
Both `dotenvx run -f .env.production -- node index.js` and `dotenvx check -f .env.production` use the production rules. Block declarations inherit top-level options and override only the options they specify. Encryption and redaction are specified per key.
|
|
1631
1640
|
|
|
1632
1641
|
Paths in file blocks are relative to the Envfile. They match the selected paths exactly after path normalization (`./.env.production` matches `.env.production`); they are not basename matches or globs. Directory inputs and `DOTENV_FILE` use their resolved file paths.
|
|
1633
1642
|
|
|
1634
|
-
When no file block matches, top-level rules apply.
|
|
1643
|
+
When no file block matches, top-level rules apply. Selected file blocks override explicit options in file order: the first file wins. Policy selection is independent of where the final value comes from, including shell values and `--overload`. A selected missing file still activates its block. Blocks cannot be nested, and duplicate declarations within one scope or duplicate file blocks are errors.
|
|
1635
1644
|
|
|
1636
1645
|
</details>
|
|
1637
1646
|
<details><summary>`check`</summary><br>
|
|
@@ -1640,19 +1649,19 @@ Validate the final resolved environment against your Envfile, using the same loa
|
|
|
1640
1649
|
|
|
1641
1650
|
```sh
|
|
1642
1651
|
$ dotenvx check -f .env.local -f .env
|
|
1643
|
-
|
|
1652
|
+
✔ PORT=3000 # .env.local:1
|
|
1644
1653
|
|
|
1645
1654
|
$ dotenvx check -f .env.production
|
|
1646
|
-
☠
|
|
1655
|
+
☠ PORT=bad # .env.production:4 !port
|
|
1647
1656
|
```
|
|
1648
1657
|
|
|
1649
|
-
Envfile `file` blocks define policy; they do not select files to load. By default, `check` uses the same
|
|
1658
|
+
Envfile `file` blocks define policy; they do not select files to load. By default, `check` uses the same profile and file selection as `run`. Use repeated `-f` flags or `--convention` to select layers. `DOTENV_FILE` and its aliases are also supported.
|
|
1650
1659
|
|
|
1651
|
-
All selected sources are merged before validation. The first file wins by default, existing shell values take precedence, and `--overload` lets later sources override earlier values. Partial files can satisfy the schema together.
|
|
1660
|
+
All selected sources are merged before validation. The first file wins by default, existing shell values take precedence, and `--overload` lets later sources override earlier values. Partial files can satisfy the schema together. Validation checks final values; encryption requirements are enforced separately by `encrypt`, `set`, and `protect`. Rules are combined independently, with the first explicit file option winning.
|
|
1652
1661
|
|
|
1653
1662
|
Missing files explicitly selected with `-f` or `DOTENV_FILE` (and its aliases) fail the check. Missing optional convention layers and implicit default files are reported as `○ skipped (filename)`. `check` has no `--strict` flag. Required values must still resolve; shell or inline values can satisfy the schema without a local file. A check with no readable files, inline values, or declared shell values fails rather than reporting an empty success.
|
|
1654
1663
|
|
|
1655
|
-
|
|
1664
|
+
The preview shows redacted values and per-key diagnostics on stdout, with source locations where available. Loading errors go to stderr. `--quiet` suppresses the preview. Failed checks exit with code 1.
|
|
1656
1665
|
|
|
1657
1666
|
</details>
|
|
1658
1667
|
<details><summary>`protect with an Envfile`</summary><br>
|
|
@@ -1670,7 +1679,7 @@ Without an Envfile, protection keeps its existing rules, including exemptions fo
|
|
|
1670
1679
|
|
|
1671
1680
|
Protection inspects every assignment in the incoming Git blob, including duplicates. It does not merge files, use shell overrides, or decrypt values. Empty values, public encryption keys, and `op://` / `bw://` references remain allowed. Private-key files (`.env.keys*`) and nonempty plaintext `DOTENV_PRIVATE_KEY` values remain blocked even with `encrypted: false`.
|
|
1672
1681
|
|
|
1673
|
-
The policy is read from the working-tree Envfile beside the staged file, even if that policy itself is unstaged.
|
|
1682
|
+
The policy is read from the working-tree Envfile beside the staged file, even if that policy itself is unstaged. If no adjacent Envfile exists, the Git repository root is checked. Invalid or unreadable policies block staging with an error. Envfile encryption rules also apply to otherwise exempt files such as `.env.example`.
|
|
1674
1683
|
|
|
1675
1684
|
Accepted blobs pass through byte-for-byte. This integration applies to Git protection, not `protect --docker`.
|
|
1676
1685
|
|
|
@@ -2278,7 +2287,7 @@ $ dotenvx set HELLO_PLAIN World
|
|
|
2278
2287
|
set HELLO_PLAIN (.env)
|
|
2279
2288
|
```
|
|
2280
2289
|
|
|
2281
|
-
|
|
2290
|
+
Without an Envfile, `dotenvx set` and `dotenvx encrypt` skip keys ending in `_PLAIN`. With an Envfile, its per-key encryption rules take precedence.
|
|
2282
2291
|
|
|
2283
2292
|
</details>
|
|
2284
2293
|
<details><summary>`set KEY value --no-native`</summary><br>
|
|
@@ -3313,16 +3322,6 @@ WRONG_PRIVATE_KEY= # the supplied private key cannot decrypt the value
|
|
|
3313
3322
|
|
|
3314
3323
|
|
|
3315
3324
|
|
|
3316
|
-
## Membership
|
|
3317
|
-
|
|
3318
|
-
[](https://dotenvx.com/membership)
|
|
3319
|
-
|
|
3320
|
-
<p align="center">Dotenvx Membership. Be part of the .env story.</p>
|
|
3321
|
-
|
|
3322
|
-
<p align="center"><a href="https://dotenvx.com/membership">Membership Benefits →</a></p>
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
3325
|
## FAQ
|
|
3327
3326
|
|
|
3328
3327
|
<details><summary>How does encryption work?</summary><br>
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.
|
|
2
|
+
"version": "2.33.0",
|
|
3
3
|
"name": "@dotenvx/dotenvx",
|
|
4
4
|
"description": "a secure dotenv–from the creator of `dotenv`",
|
|
5
5
|
"author": "@motdotla",
|
|
@@ -49,15 +49,13 @@
|
|
|
49
49
|
"testshell": "shellspec --shell /bin/bash",
|
|
50
50
|
"release:check": "npm test && npm run testshell",
|
|
51
51
|
"prerelease": "npm run release:check",
|
|
52
|
-
"release": "npm run release:check && standard-version"
|
|
53
|
-
"build:envspec": "peggy --format commonjs -o src/lib/envspec/parsing/envspecParser.js src/lib/envspec/parsing/envspec.peggy"
|
|
52
|
+
"release": "npm run release:check && standard-version"
|
|
54
53
|
},
|
|
55
54
|
"funding": "https://dotenvx.com",
|
|
56
55
|
"dependencies": {
|
|
57
|
-
"@dotenvx/primitives": "^3.0
|
|
56
|
+
"@dotenvx/primitives": "^3.1.0",
|
|
58
57
|
"@dotenvx/tooling": "^1.0.3",
|
|
59
58
|
"global-agent": "^3.0.0",
|
|
60
|
-
"node-forge": "^1.4.0",
|
|
61
59
|
"undici": "^6.28.1",
|
|
62
60
|
"yocto-spinner": "^1.2.1"
|
|
63
61
|
},
|
|
@@ -65,7 +63,6 @@
|
|
|
65
63
|
"@yao-pkg/pkg": "^6.12.0",
|
|
66
64
|
"capture-console": "^1.0.2",
|
|
67
65
|
"esbuild": "^0.28.1",
|
|
68
|
-
"peggy": "^5.1.0",
|
|
69
66
|
"proxyquire": "^2.1.3",
|
|
70
67
|
"sinon": "^14.0.1",
|
|
71
68
|
"standard": "^17.1.2",
|
|
@@ -79,10 +76,5 @@
|
|
|
79
76
|
"publishConfig": {
|
|
80
77
|
"access": "public",
|
|
81
78
|
"provenance": true
|
|
82
|
-
},
|
|
83
|
-
"standard": {
|
|
84
|
-
"ignore": [
|
|
85
|
-
"src/lib/envspec/parsing/envspecParser.js"
|
|
86
|
-
]
|
|
87
79
|
}
|
|
88
80
|
}
|
package/src/cli/actions/check.js
CHANGED
|
@@ -2,12 +2,12 @@ const { logger } = require('./../../shared/logger')
|
|
|
2
2
|
const catchAndLog = require('./../../lib/helpers/catchAndLog')
|
|
3
3
|
const createSpinner = require('../../lib/helpers/createSpinner')
|
|
4
4
|
const prepareValidatedEnv = require('../../lib/services/validate')
|
|
5
|
-
const diagnosticLocations = require('../../lib/
|
|
5
|
+
const diagnosticLocations = require('../../lib/envfile/validation/diagnosticLocation')
|
|
6
6
|
const normalizeDotenvConfigQuiet = require('../../lib/helpers/normalizeDotenvConfigQuiet')
|
|
7
7
|
const normalizeDotenvConfigConvention = require('../../lib/helpers/normalizeDotenvConfigConvention')
|
|
8
8
|
const normalizeDotenvConfigIgnore = require('../../lib/helpers/normalizeDotenvConfigIgnore')
|
|
9
9
|
const path = require('node:path')
|
|
10
|
-
const
|
|
10
|
+
const previewEnvfile = require('../../lib/envfile/rendering/previewEnvfile')
|
|
11
11
|
|
|
12
12
|
async function check () {
|
|
13
13
|
const options = normalizeDotenvConfigIgnore(normalizeDotenvConfigConvention(normalizeDotenvConfigQuiet(this.opts())))
|
|
@@ -25,15 +25,14 @@ async function check () {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
try {
|
|
28
|
-
const { processedEnvs, readableFilepaths, requiredFilepaths,
|
|
28
|
+
const { processedEnvs, readableFilepaths, requiredFilepaths, validationError, schema, checked: checkedValues } = await prepareValidatedEnv({
|
|
29
29
|
envs: this.envs,
|
|
30
30
|
options,
|
|
31
|
-
onStatus: text => { if (spinner && text) spinner.text = text }
|
|
32
|
-
onKey: key => { if (spinner) spinner.text = `checking ${key}` }
|
|
31
|
+
onStatus: text => { if (spinner && text) spinner.text = text }
|
|
33
32
|
})
|
|
34
33
|
checked.push(...readableFilepaths)
|
|
35
34
|
if (processedEnvs.some(row => row.type === 'env' && Object.keys(row.parsed || {}).length)) checked.push('--env')
|
|
36
|
-
if (checked.length === 0 &&
|
|
35
|
+
if (checked.length === 0 && Object.keys(schema.keys).some(key => checkedValues[key] !== undefined)) checked.push('shell environment')
|
|
37
36
|
const location = diagnosticLocations(processedEnvs, checked.join(', ') || 'Envfile')
|
|
38
37
|
for (const row of processedEnvs) {
|
|
39
38
|
for (const error of row.errors || []) {
|
|
@@ -46,10 +45,6 @@ async function check () {
|
|
|
46
45
|
report('error', error.messageWithHelp || error.message)
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
|
-
if (proxyError) {
|
|
50
|
-
errorCount++
|
|
51
|
-
report('error', proxyError.message)
|
|
52
|
-
}
|
|
53
48
|
if (validationError && !ignore.includes(validationError.code)) {
|
|
54
49
|
errorCount++
|
|
55
50
|
for (const diagnostic of showPreview ? [] : validationError.diagnostics) {
|
|
@@ -61,7 +56,7 @@ async function check () {
|
|
|
61
56
|
if (spinner) spinner.stop()
|
|
62
57
|
if (showPreview) {
|
|
63
58
|
const diagnostics = validationError && !ignore.includes(validationError.code) ? validationError.diagnostics : []
|
|
64
|
-
const preview =
|
|
59
|
+
const preview = previewEnvfile(processedEnvs, schema, checkedValues, diagnostics)
|
|
65
60
|
if (preview) {
|
|
66
61
|
for (const line of preview.split('\n')) logger.info(line)
|
|
67
62
|
}
|
|
@@ -28,7 +28,7 @@ async function encryptAction () {
|
|
|
28
28
|
|
|
29
29
|
// stdout - should not have a try so that exit codes can surface to stdout
|
|
30
30
|
if (options.stdout) {
|
|
31
|
-
const { processedEnvs } = await encryptTransform({ envs, ik, ek, fk, noArmor, token: options.token, noCreate, noNative, no1Password, noBitwarden })
|
|
31
|
+
const { processedEnvs } = await encryptTransform({ profile: options.profile, envs, ik, ek, fk, noArmor, token: options.token, noCreate, noNative, no1Password, noBitwarden })
|
|
32
32
|
|
|
33
33
|
if (spinner) spinner.stop()
|
|
34
34
|
for (const processedEnv of processedEnvs) {
|
|
@@ -45,7 +45,7 @@ async function encryptAction () {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
try {
|
|
48
|
-
const { keysSrc, processedEnvs, changedFilepaths, unchangedFilepaths } = await encryptTransform({ envs, ik, ek, fk, noArmor, token: options.token, noCreate, noNative, no1Password, noBitwarden })
|
|
48
|
+
const { keysSrc, processedEnvs, changedFilepaths, unchangedFilepaths } = await encryptTransform({ profile: options.profile, envs, ik, ek, fk, noArmor, token: options.token, noCreate, noNative, no1Password, noBitwarden })
|
|
49
49
|
|
|
50
50
|
if (keysSrc) {
|
|
51
51
|
await fsx.writeKeyFile(fk, keysSrc)
|
package/src/cli/actions/get.js
CHANGED
|
@@ -7,8 +7,6 @@ const Session = require('../../db/session')
|
|
|
7
7
|
const getResolver = require('./../../lib/resolvers/get')
|
|
8
8
|
const normalizeDotenvConfigConvention = require('../../lib/helpers/normalizeDotenvConfigConvention')
|
|
9
9
|
const normalizeDotenvConfigIgnore = require('../../lib/helpers/normalizeDotenvConfigIgnore')
|
|
10
|
-
const normalizeDotenvConfigPath = require('../../lib/helpers/normalizeDotenvConfigPath')
|
|
11
|
-
const buildCommandEnvs = require('../../lib/helpers/buildCommandEnvs')
|
|
12
10
|
const resolveEnvKeysFile = require('../../lib/helpers/resolveEnvKeysFile')
|
|
13
11
|
const mask = require('../../lib/helpers/mask')
|
|
14
12
|
const filterKeys = require('../../lib/helpers/filterKeys')
|
|
@@ -27,7 +25,7 @@ async function get (key) {
|
|
|
27
25
|
const ignore = options.ignore || []
|
|
28
26
|
let errorCount = 0
|
|
29
27
|
|
|
30
|
-
const envs =
|
|
28
|
+
const envs = this.envs
|
|
31
29
|
|
|
32
30
|
try {
|
|
33
31
|
const sesh = new Session()
|
|
@@ -36,6 +34,8 @@ async function get (key) {
|
|
|
36
34
|
const { parsed: resolved, errors } = await getResolver({
|
|
37
35
|
key,
|
|
38
36
|
envs,
|
|
37
|
+
profile: options.profile,
|
|
38
|
+
convention: options.convention,
|
|
39
39
|
overload: options.overload,
|
|
40
40
|
all: options.all,
|
|
41
41
|
envKeysFile: resolveEnvKeysFile(options.envKeysFile),
|
package/src/cli/actions/init.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
const fs = require('node:fs')
|
|
2
2
|
const process = require('node:process')
|
|
3
3
|
const prompts = require('../../lib/helpers/prompts')
|
|
4
|
-
const
|
|
5
|
-
const scanSource = require('../../lib/
|
|
6
|
-
const discoverEnvFiles = require('../../lib/
|
|
4
|
+
const initEnvfile = require('../../lib/services/init')
|
|
5
|
+
const scanSource = require('../../lib/envfile/discovery/scanSource')
|
|
6
|
+
const discoverEnvFiles = require('../../lib/envfile/discovery/discoverEnvFiles')
|
|
7
7
|
const createSpinner = require('../../lib/helpers/createSpinner')
|
|
8
8
|
const { logger } = require('../../shared/logger')
|
|
9
9
|
const catchAndLog = require('../../lib/helpers/catchAndLog')
|
|
@@ -49,7 +49,7 @@ module.exports = async function spec () {
|
|
|
49
49
|
sourceKeys = keys
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
const { created, replaced, content } =
|
|
52
|
+
const { created, replaced, content } = initEnvfile({ envFile, envFiles, sourceKeys, overwrite, stdout, onFile })
|
|
53
53
|
if (spinner) spinner.stop()
|
|
54
54
|
if (stdout) {
|
|
55
55
|
process.stdout.write(content)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const fs = require('node:fs')
|
|
2
|
+
const { profile, policy } = require('@dotenvx/primitives')
|
|
3
|
+
const catchAndLog = require('../../lib/helpers/catchAndLog')
|
|
4
|
+
|
|
5
|
+
module.exports = function policyAction () {
|
|
6
|
+
try {
|
|
7
|
+
const source = fs.readFileSync('Envfile')
|
|
8
|
+
const file = profile(source, { f: this.opts().file ?? '.env' })
|
|
9
|
+
process.stdout.write(JSON.stringify(policy(source, file), null, 2) + '\n')
|
|
10
|
+
} catch (error) {
|
|
11
|
+
catchAndLog(error)
|
|
12
|
+
return { exitCode: 1, error }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const fs = require('node:fs')
|
|
2
|
+
const path = require('node:path')
|
|
3
|
+
const { profile, policy, parse, check } = require('@dotenvx/primitives')
|
|
4
|
+
const catchAndLog = require('../../lib/helpers/catchAndLog')
|
|
5
|
+
|
|
6
|
+
module.exports = async function primitivesCheck () {
|
|
7
|
+
try {
|
|
8
|
+
const source = fs.readFileSync('Envfile')
|
|
9
|
+
const file = profile(source, { f: this.opts().file ?? '.env' })
|
|
10
|
+
const rules = policy(source, file)
|
|
11
|
+
const processEnv = { ...process.env }
|
|
12
|
+
const keys = new Set(Object.keys(rules.keys))
|
|
13
|
+
for (const filename of file === '' ? [] : file.split(',')) {
|
|
14
|
+
const { parsed, injected, errors } = await parse(fs.readFileSync(filename), {
|
|
15
|
+
processEnv,
|
|
16
|
+
fk: path.join(path.dirname(filename), '.env.keys')
|
|
17
|
+
})
|
|
18
|
+
if (errors.length) throw new Error('Could not decrypt selected environment files')
|
|
19
|
+
for (const key of Object.keys(parsed)) keys.add(key)
|
|
20
|
+
for (const [key, value] of Object.entries(injected)) {
|
|
21
|
+
Object.defineProperty(processEnv, key, { value, enumerable: true, writable: true, configurable: true })
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const finalEnv = Object.fromEntries([...keys].filter(key => Object.hasOwn(processEnv, key)).map(key => [key, processEnv[key]]))
|
|
25
|
+
const result = check(rules, finalEnv)
|
|
26
|
+
process.stdout.write(JSON.stringify(result, null, 2) + '\n')
|
|
27
|
+
return { exitCode: result.errors.length ? 1 : 0 }
|
|
28
|
+
} catch (error) {
|
|
29
|
+
catchAndLog(error)
|
|
30
|
+
return { exitCode: 1, error }
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const fs = require('node:fs')
|
|
2
|
+
const { profile } = require('@dotenvx/primitives')
|
|
3
|
+
const catchAndLog = require('../../lib/helpers/catchAndLog')
|
|
4
|
+
|
|
5
|
+
module.exports = function primitivesProfile () {
|
|
6
|
+
try {
|
|
7
|
+
const source = fs.readFileSync('Envfile')
|
|
8
|
+
const options = this.opts()
|
|
9
|
+
const file = profile(source, { f: options.file, profile: options.profile, processEnv: process.env })
|
|
10
|
+
process.stdout.write(file + '\n')
|
|
11
|
+
} catch (error) {
|
|
12
|
+
catchAndLog(error)
|
|
13
|
+
return { exitCode: 1, error }
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const { redact } = require('@dotenvx/primitives')
|
|
2
|
+
const catchAndLog = require('../../lib/helpers/catchAndLog')
|
|
3
|
+
|
|
4
|
+
module.exports = async function primitivesRedact (text) {
|
|
5
|
+
try {
|
|
6
|
+
const options = this.opts()
|
|
7
|
+
if (options.stdin) {
|
|
8
|
+
if (text !== undefined) throw new Error('Cannot combine a text argument with --stdin')
|
|
9
|
+
process.stdin.setEncoding('utf8')
|
|
10
|
+
text = ''
|
|
11
|
+
for await (const chunk of process.stdin) text += chunk
|
|
12
|
+
}
|
|
13
|
+
if (text === undefined) throw new Error('Provide text or use --stdin')
|
|
14
|
+
process.stdout.write(redact(text, options.secret))
|
|
15
|
+
} catch (error) {
|
|
16
|
+
catchAndLog(error)
|
|
17
|
+
return { exitCode: 1, error }
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const fs = require('fs')
|
|
2
2
|
const path = require('path')
|
|
3
3
|
const plaintextKeys = require('../../lib/helpers/plaintextKeys')
|
|
4
|
-
const
|
|
4
|
+
const protectEnvfile = require('../../lib/envfile/validation/protectEnvfile')
|
|
5
5
|
const { logger } = require('../../shared/logger')
|
|
6
6
|
const createProtectSpinner = require('../../lib/helpers/createProtectSpinner')
|
|
7
7
|
const logProtectedFiles = require('../../lib/helpers/logProtectedFiles')
|
|
@@ -26,7 +26,7 @@ function check (filepath, content, beforeError = () => {}) {
|
|
|
26
26
|
let rejected = null
|
|
27
27
|
if (!privateKeyFile) {
|
|
28
28
|
try {
|
|
29
|
-
rejected =
|
|
29
|
+
rejected = protectEnvfile(filepath, content.toString('utf8'))
|
|
30
30
|
} catch (error) {
|
|
31
31
|
beforeError()
|
|
32
32
|
logger.error(error.message)
|
package/src/cli/actions/run.js
CHANGED
|
@@ -13,7 +13,6 @@ const maskEnvSrc = require('../../lib/helpers/maskEnvSrc')
|
|
|
13
13
|
const maskProcessedEnvs = require('../../lib/helpers/maskProcessedEnvs')
|
|
14
14
|
const redactedValues = require('../../lib/helpers/redactedValues')
|
|
15
15
|
const { redactOutput } = require('../../lib/helpers/redactOutput')
|
|
16
|
-
const configureProxy = require('../../lib/proxy/configureProxy')
|
|
17
16
|
|
|
18
17
|
function inferCommandArgsFromProcessArgv (argv) {
|
|
19
18
|
const runIndex = argv.indexOf('run')
|
|
@@ -25,7 +24,7 @@ function inferCommandArgsFromProcessArgv (argv) {
|
|
|
25
24
|
if (separatorIndex !== -1) return args.slice(separatorIndex + 1)
|
|
26
25
|
|
|
27
26
|
for (let i = 0; i < args.length; i++) {
|
|
28
|
-
if (args[i] === '-f' || args[i] === '--file' || args[i] === '--env-file') {
|
|
27
|
+
if (args[i] === '-f' || args[i] === '--file' || args[i] === '--env-file' || args[i] === '--profile') {
|
|
29
28
|
i++
|
|
30
29
|
continue
|
|
31
30
|
}
|
|
@@ -58,7 +57,6 @@ async function run () {
|
|
|
58
57
|
}
|
|
59
58
|
let commandEnv = process.env
|
|
60
59
|
let sensitiveValues = []
|
|
61
|
-
let closeProxy
|
|
62
60
|
|
|
63
61
|
let commandArgs = this.args
|
|
64
62
|
if (commandArgs.length < 1) {
|
|
@@ -99,26 +97,21 @@ async function run () {
|
|
|
99
97
|
const {
|
|
100
98
|
processedEnvs,
|
|
101
99
|
readableFilepaths,
|
|
102
|
-
|
|
100
|
+
hasEnvfile,
|
|
103
101
|
schema,
|
|
104
|
-
proxyCredentials,
|
|
105
|
-
proxyToken,
|
|
106
|
-
session: sesh,
|
|
107
|
-
proxyError,
|
|
108
102
|
validationError: error
|
|
109
103
|
} = await prepareValidatedEnv({
|
|
110
104
|
envs: this.envs,
|
|
111
105
|
options,
|
|
112
106
|
processEnv: process.env,
|
|
113
|
-
|
|
107
|
+
requireEnvfile: false,
|
|
114
108
|
command: commandArgs,
|
|
115
109
|
onStatus: (text) => {
|
|
116
110
|
if (spinner && text) spinner.text = text
|
|
117
111
|
}
|
|
118
112
|
})
|
|
119
|
-
if (proxyError) throw proxyError
|
|
120
113
|
|
|
121
|
-
if (
|
|
114
|
+
if (hasEnvfile || options.redact === true) {
|
|
122
115
|
sensitiveValues = redactedValues(processedEnvs, schema, process.env, options.redact === true)
|
|
123
116
|
}
|
|
124
117
|
|
|
@@ -149,10 +142,7 @@ async function run () {
|
|
|
149
142
|
continue // ignore error
|
|
150
143
|
}
|
|
151
144
|
|
|
152
|
-
|
|
153
|
-
? (schema.strictByFile?.get(path.resolve(processedEnv.filepath)) ?? schema.strict)
|
|
154
|
-
: schema.strict
|
|
155
|
-
if (options.strict || strict) throw error // throw if strict and not ignored
|
|
145
|
+
if (options.strict) throw error // file-loading failures use the CLI strict flag
|
|
156
146
|
|
|
157
147
|
if (error.code === 'MISSING_ENV_FILE' && options.convention) { // do not output error for conventions (too noisy)
|
|
158
148
|
// intentionally quiet
|
|
@@ -187,19 +177,7 @@ async function run () {
|
|
|
187
177
|
}
|
|
188
178
|
}
|
|
189
179
|
|
|
190
|
-
const proxy = await configureProxy(commandArgs, commandEnv, proxyCredentials, sesh, proxyToken)
|
|
191
|
-
closeProxy = proxy.close
|
|
192
|
-
commandArgs = proxy.commandArgs
|
|
193
|
-
commandEnv = proxy.env
|
|
194
|
-
|
|
195
|
-
const gatedKeys = new Set((proxyCredentials || [])
|
|
196
|
-
.filter(credential => commandEnv[credential.name] === credential.placeholder)
|
|
197
|
-
.map(credential => credential.name))
|
|
198
180
|
const injectedKeys = uniqueInjectedKeys(processedEnvs)
|
|
199
|
-
for (const key of gatedKeys) {
|
|
200
|
-
injectedKeys.delete(key)
|
|
201
|
-
logger.verbose(`${key} proxied via Armor proxy`)
|
|
202
|
-
}
|
|
203
181
|
|
|
204
182
|
let msg = ''
|
|
205
183
|
const envStringCount = processedEnvs.filter((processedEnv) => processedEnv.type === 'env' && processedEnv.parsed).length
|
|
@@ -212,10 +190,8 @@ async function run () {
|
|
|
212
190
|
}
|
|
213
191
|
|
|
214
192
|
if (spinner) spinner.stop()
|
|
215
|
-
if (gatedKeys.size > 0) logger.success(`⧈ proxied (${gatedKeys.size})${msg}`)
|
|
216
193
|
logger.success(`⟐ injected env (${injectedKeys.size})${msg}`)
|
|
217
194
|
} catch (error) {
|
|
218
|
-
if (closeProxy) await closeProxy()
|
|
219
195
|
if (spinner) spinner.stop()
|
|
220
196
|
if (error.code === 'PROMPT_CANCELLED') {
|
|
221
197
|
return { exitCode: 130, error }
|
|
@@ -224,11 +200,7 @@ async function run () {
|
|
|
224
200
|
return { exitCode: 1, error }
|
|
225
201
|
}
|
|
226
202
|
|
|
227
|
-
|
|
228
|
-
return await executeCommand(commandArgs, commandEnv, sensitiveValues, closeProxy)
|
|
229
|
-
} finally {
|
|
230
|
-
if (closeProxy) await closeProxy()
|
|
231
|
-
}
|
|
203
|
+
return executeCommand(commandArgs, commandEnv, sensitiveValues)
|
|
232
204
|
}
|
|
233
205
|
|
|
234
206
|
module.exports = require('../../lib/events/cli')('run', run)
|
package/src/cli/actions/set.js
CHANGED
|
@@ -63,7 +63,7 @@ async function set (key, value) {
|
|
|
63
63
|
let errorCount = 0
|
|
64
64
|
|
|
65
65
|
try {
|
|
66
|
-
const { keysSrc, processedEnvs, changedFilepaths, unchangedFilepaths } = await setTransform({ envs, key, value, fk, noArmor, token: options.token, noCreate, encrypt, noNative, no1Password, noBitwarden })
|
|
66
|
+
const { keysSrc, processedEnvs, changedFilepaths, unchangedFilepaths } = await setTransform({ profile: options.profile, envs, key, value, fk, noArmor, token: options.token, noCreate, encrypt, noNative, no1Password, noBitwarden })
|
|
67
67
|
|
|
68
68
|
if (keysSrc) {
|
|
69
69
|
await fsx.writeKeyFile(fk, keysSrc)
|
|
@@ -27,9 +27,36 @@ function output (operation, required = false) {
|
|
|
27
27
|
|
|
28
28
|
module.exports = function primitives (command) {
|
|
29
29
|
command
|
|
30
|
-
.description('standalone
|
|
30
|
+
.description('standalone primitive operations')
|
|
31
31
|
.action(function () { this.help() })
|
|
32
32
|
|
|
33
|
+
command.command('profile')
|
|
34
|
+
.description('resolve an Envfile profile to comma-separated filenames')
|
|
35
|
+
.option('--profile <name>', 'explicit profile name')
|
|
36
|
+
.option('-f, --file <paths>', 'explicit comma-separated files, overriding profile selection', (value, previous) => previous === undefined ? value : `${previous},${value}`)
|
|
37
|
+
.allowExcessArguments(false)
|
|
38
|
+
.action(commandAction(require('../actions/primitivesProfile')))
|
|
39
|
+
|
|
40
|
+
command.command('redact')
|
|
41
|
+
.description('replace secret strings in text with [REDACTED]')
|
|
42
|
+
.argument('[text]', 'text to redact')
|
|
43
|
+
.requiredOption('-s, --secret <value>', 'literal secret to redact (repeat for multiple secrets)', (value, previous) => [...(previous || []), value])
|
|
44
|
+
.option('--stdin', 'read text from stdin')
|
|
45
|
+
.allowExcessArguments(false)
|
|
46
|
+
.action(commandAction(require('../actions/primitivesRedact')))
|
|
47
|
+
|
|
48
|
+
command.command('policy')
|
|
49
|
+
.description('print the Envfile policy for selected files as JSON')
|
|
50
|
+
.option('-f, --file <paths>', 'comma-separated env files, first file wins (default: .env)', (value, previous) => previous === undefined ? value : `${previous},${value}`)
|
|
51
|
+
.allowExcessArguments(false)
|
|
52
|
+
.action(commandAction(require('../actions/policy')))
|
|
53
|
+
|
|
54
|
+
command.command('check')
|
|
55
|
+
.description('check selected env files against Envfile and print redacted JSON')
|
|
56
|
+
.option('-f, --file <paths>', 'comma-separated env files, first file wins (default: .env)', (value, previous) => previous === undefined ? value : `${previous},${value}`)
|
|
57
|
+
.allowExcessArguments(false)
|
|
58
|
+
.action(commandAction(require('../actions/primitivesCheck')))
|
|
59
|
+
|
|
33
60
|
command.command('keypair')
|
|
34
61
|
.description('generate or restore a keypair and print JSON')
|
|
35
62
|
.argument('[privateKey]', 'hex-encoded private key to restore')
|
package/src/cli/dotenvx.js
CHANGED
|
@@ -74,6 +74,7 @@ program.command('run')
|
|
|
74
74
|
.option('-fk, --env-keys-file <path>', 'path(s) to your .env.keys file(s) (default: same path as your env file)', collectEnvKeys)
|
|
75
75
|
.option('-o, --overload', 'override existing env variables (by default, existing env vars take precedence over .env files)')
|
|
76
76
|
.option('--strict', 'process.exit(1) on any errors', false)
|
|
77
|
+
.option('--profile <name>', 'select an Envfile profile')
|
|
77
78
|
.option('--convention <name>', 'load a .env convention (available conventions: [\'nextjs\', \'flow\'])')
|
|
78
79
|
.option('--ignore <errorCodes...>', 'error code(s) to ignore (example: --ignore=MISSING_ENV_FILE)')
|
|
79
80
|
.option('--token <token>', 'set Armor ⛨ token')
|
|
@@ -99,6 +100,7 @@ program.command('get')
|
|
|
99
100
|
.option('-fk, --env-keys-file <path>', 'path(s) to your .env.keys file(s) (default: same path as your env file)', collectEnvKeys)
|
|
100
101
|
.option('-o, --overload', 'override existing env variables (by default, existing env vars take precedence over .env files)')
|
|
101
102
|
.option('--strict', 'process.exit(1) on any errors', false)
|
|
103
|
+
.option('--profile <name>', 'select an Envfile profile')
|
|
102
104
|
.option('--convention <name>', 'load a .env convention (available conventions: [\'nextjs\', \'flow\'])')
|
|
103
105
|
.option('--ignore <errorCodes...>', 'error code(s) to ignore (example: --ignore=MISSING_ENV_FILE)')
|
|
104
106
|
.option('-a, --all', 'include all machine envs as well')
|
|
@@ -122,6 +124,7 @@ program.command('get')
|
|
|
122
124
|
program.command('set')
|
|
123
125
|
.usage('<KEY> [value] [options]')
|
|
124
126
|
.description('encrypt a single environment variable')
|
|
127
|
+
.option('--profile <name>', 'select an Envfile profile')
|
|
125
128
|
.addHelpText('after', help.set)
|
|
126
129
|
.allowUnknownOption()
|
|
127
130
|
.argument('KEY', 'KEY')
|
|
@@ -156,6 +159,7 @@ program.command('del')
|
|
|
156
159
|
function configureEncryptCommand (command) {
|
|
157
160
|
return command
|
|
158
161
|
.description('encrypt .env file(s)')
|
|
162
|
+
.option('--profile <name>', 'select an Envfile profile')
|
|
159
163
|
.option('-f, --env-file <path>', 'path(s) to your env file(s)', collectEnvs('envFile'), [])
|
|
160
164
|
.option('-fk, --env-keys-file <path>', 'path to your .env.keys file (default: same path as your env file)')
|
|
161
165
|
.option('-k, --key <keys...>', 'keys(s) to encrypt (default: all keys in file)')
|
|
@@ -241,6 +245,7 @@ program.command('check', { hidden: true })
|
|
|
241
245
|
.option('-f, --env-file <path>', 'path(s) to your env file(s)', collectEnvs('envFile'), [])
|
|
242
246
|
.option('-fk, --env-keys-file <path>', 'path(s) to your .env.keys file(s) (default: same path as your env file)', collectEnvKeys)
|
|
243
247
|
.option('-o, --overload', 'override existing env variables (by default, existing env vars take precedence over .env files)')
|
|
248
|
+
.option('--profile <name>', 'select an Envfile profile')
|
|
244
249
|
.option('--convention <name>', 'load a .env convention (available conventions: [\'nextjs\', \'flow\'])')
|
|
245
250
|
.option('--ignore <errorCodes...>', 'error code(s) to ignore (example: --ignore=MISSING_ENV_FILE)')
|
|
246
251
|
.option('--token <token>', 'set Armor ⛨ token')
|