@kaynooo/cli-env 0.13.0 → 0.13.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.
- package/README.md +1 -0
- package/dist/ev.js +175 -127
- package/dist/redact.js +104 -53
- package/package.json +2 -2
- package/src/commands/audit.ts +8 -6
- package/src/commands/compare.ts +7 -6
- package/src/commands/edit.ts +30 -21
- package/src/commands/pins.ts +5 -3
- package/src/commands/read.ts +13 -12
- package/src/commands/redact.ts +5 -2
- package/src/files.ts +2 -2
package/src/commands/pins.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import type { AppPins } from '../pins'
|
|
2
3
|
import { readdir, stat } from 'node:fs/promises'
|
|
3
4
|
import { join, resolve } from 'node:path'
|
|
@@ -10,8 +11,9 @@ Checks that an app's tests pin the env that reaches outside.
|
|
|
10
11
|
Reads the app's env schema (the file calling defineEnv, src/env.ts) and lists its optional URL, key, token, secret, password and host variables, then the ones no \`.env.test\` entry, test-level overrideEnv or local bunfig preload dropping what .env sets pins, and each test file that calls testSite or proxyKeyTests without pinning them all.
|
|
11
12
|
A file whose pages reach none of them says so with a \`// ev pins: none reached, <why>\` comment.
|
|
12
13
|
The dir is an app, or a monorepo holding apps one level down (the current folder by default).
|
|
13
|
-
Reads variable names only, never a value
|
|
14
|
-
|
|
14
|
+
Reads variable names only, never a value.`
|
|
15
|
+
|
|
16
|
+
const EXITS: ExitRow[] = [[0, 'everything is pinned'], [1, 'something is not'], [2, 'a usage error']]
|
|
15
17
|
|
|
16
18
|
const options = {
|
|
17
19
|
json: jsonOption('each app with its reaching, unpinned variables and flagged test files'),
|
|
@@ -57,7 +59,7 @@ function name(app: AppPins, root: string): string {
|
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
export async function pins(args: string[]): Promise<number> {
|
|
60
|
-
const parsed = parse(args, usage, options, { positionals: ['[dir]'] })
|
|
62
|
+
const parsed = parse(args, usage, options, { positionals: ['[dir]'], exits: EXITS })
|
|
61
63
|
const json = parsed.values.json === true
|
|
62
64
|
const root = resolve(parsed.positionals[0] ?? '.')
|
|
63
65
|
const except = new Set((parsed.values.except ?? '').split(',').map(entry => entry.trim()).filter(entry => entry !== ''))
|
package/src/commands/read.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import type { Dotenv } from '../dotenv'
|
|
2
3
|
import type { KnownSecrets } from '../mask'
|
|
3
4
|
import { fail, jsonOption, parse, printJson, role, table } from '@kaynooo/cli-kit'
|
|
@@ -10,17 +11,17 @@ const listUsage = `ev list [file]
|
|
|
10
11
|
|
|
11
12
|
Prints every key of the folder's .env* files, or of one file, with secret values masked: a few leading characters and the length under a key named like TOKEN, SECRET, PASSWORD, PASS, PWD, KEY, PRIVATE, DSN, AUTH, SALT or COOKIE, any value equal to a secret of the folder's .env* files, and the password and tokens of any URL.
|
|
12
13
|
|
|
13
|
-
--json prints [{file, key, value, secret}], value masked and secret true when a value was
|
|
14
|
+
--json prints [{file, key, value, secret}], value masked and secret true when a value was.`
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
const LIST_EXITS: ExitRow[] = [[0, 'it printed them'], [1, 'there is no .env* file'], [2, 'a usage error']]
|
|
16
17
|
|
|
17
18
|
const getUsage = `ev get KEY [file]
|
|
18
19
|
|
|
19
20
|
Prints the masked value of one key, from every .env* file that sets it or from one file.
|
|
20
21
|
|
|
21
|
-
--json prints [{file, key, value, secret}] for each file that sets it
|
|
22
|
+
--json prints [{file, key, value, secret}] for each file that sets it.`
|
|
22
23
|
|
|
23
|
-
|
|
24
|
+
const GET_EXITS: ExitRow[] = [[0, 'the key is set'], [1, 'it is not set anywhere'], [2, 'a usage error']]
|
|
24
25
|
|
|
25
26
|
function display(key: string, value: string, known: KnownSecrets): string {
|
|
26
27
|
return maskValue(key, value, known).replaceAll('\n', '\\n').replaceAll('\r', '\\r')
|
|
@@ -69,7 +70,7 @@ async function readAll(names: string[]): Promise<{ files: [name: string, file: D
|
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
export async function list(args: string[]): Promise<number> {
|
|
72
|
-
const parsed = parse(args, listUsage, jsonFlag)
|
|
73
|
+
const parsed = parse(args, listUsage, jsonFlag, { exits: LIST_EXITS })
|
|
73
74
|
|
|
74
75
|
const [file] = parsed.positionals
|
|
75
76
|
const names = file === undefined ? await envFiles() : [file]
|
|
@@ -95,7 +96,7 @@ export async function list(args: string[]): Promise<number> {
|
|
|
95
96
|
}
|
|
96
97
|
|
|
97
98
|
export async function get(args: string[]): Promise<number> {
|
|
98
|
-
const parsed = parse(args, getUsage, jsonFlag)
|
|
99
|
+
const parsed = parse(args, getUsage, jsonFlag, { exits: GET_EXITS })
|
|
99
100
|
|
|
100
101
|
const [key, file] = parsed.positionals
|
|
101
102
|
|
|
@@ -126,22 +127,22 @@ const grepUsage = `ev grep [-i] <regex> [file...]
|
|
|
126
127
|
|
|
127
128
|
Prints the keys matching the regular expression, from the folder's .env* files or the files given, with their values masked as ev list does. The regex is matched against the key only, never the value: \`ev grep '^DATABASE_'\` instead of \`grep ^DATABASE_ .env\`.
|
|
128
129
|
|
|
129
|
-
--json prints [{file, key, value, secret}]
|
|
130
|
+
--json prints [{file, key, value, secret}].`
|
|
130
131
|
|
|
131
|
-
|
|
132
|
+
const GREP_EXITS: ExitRow[] = [[0, 'a key matches'], [1, 'none does'], [2, 'a usage error']]
|
|
132
133
|
|
|
133
134
|
const showUsage = `ev show <rev>:<file>
|
|
134
135
|
|
|
135
136
|
Prints every key of an env file as a git revision holds it, values masked as ev list does: \`ev show HEAD~1:api/.env\` instead of \`git show HEAD~1:api/.env\`.
|
|
136
137
|
|
|
137
|
-
--json prints [{file, key, value, secret}]
|
|
138
|
+
--json prints [{file, key, value, secret}].`
|
|
138
139
|
|
|
139
|
-
|
|
140
|
+
const SHOW_EXITS: ExitRow[] = [[0, 'it printed them'], [1, 'git cannot show the file'], [2, 'a usage error']]
|
|
140
141
|
|
|
141
142
|
const REVISION = /^([^:]+):(.+)$/
|
|
142
143
|
|
|
143
144
|
export async function grep(args: string[]): Promise<number> {
|
|
144
|
-
const parsed = parse(args, grepUsage, { 'ignore-case': { type: 'boolean', short: 'i', description: 'match the key whatever its case' }, ...jsonFlag })
|
|
145
|
+
const parsed = parse(args, grepUsage, { 'ignore-case': { type: 'boolean', short: 'i', description: 'match the key whatever its case' }, ...jsonFlag }, { exits: GREP_EXITS })
|
|
145
146
|
|
|
146
147
|
const [source, ...given] = parsed.positionals
|
|
147
148
|
|
|
@@ -178,7 +179,7 @@ export async function grep(args: string[]): Promise<number> {
|
|
|
178
179
|
}
|
|
179
180
|
|
|
180
181
|
export async function show(args: string[]): Promise<number> {
|
|
181
|
-
const parsed = parse(args, showUsage, jsonFlag)
|
|
182
|
+
const parsed = parse(args, showUsage, jsonFlag, { exits: SHOW_EXITS })
|
|
182
183
|
|
|
183
184
|
const [target] = parsed.positionals
|
|
184
185
|
const [, revision, path] = REVISION.exec(target ?? '') ?? []
|
package/src/commands/redact.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import process from 'node:process'
|
|
2
3
|
import { parse, readInput } from '@kaynooo/cli-kit'
|
|
3
4
|
import { loadKnown } from '../files'
|
|
@@ -6,7 +7,9 @@ import { check, Redactor } from '../redactor'
|
|
|
6
7
|
const usage = `redact [--check]
|
|
7
8
|
|
|
8
9
|
Copies stdin to stdout line by line with secrets masked: known token formats (GitHub, GitLab, Slack, AWS, npm, Stripe, Google and SendGrid keys, sk-, JWTs, Authorization headers, password= and secret= pairs), private key blocks, the password and the tokens of any URL and the values of secret-looking keys in this folder's .env* files.
|
|
9
|
-
--check prints nothing of the input and lists the file and line of each secret found, exiting 1 when there is any: \`git diff --cached | redact --check
|
|
10
|
+
--check prints nothing of the input and lists the file and line of each secret found, exiting 1 when there is any: \`git diff --cached | redact --check\`.`
|
|
11
|
+
|
|
12
|
+
const EXITS: ExitRow[] = [[0, 'it copied the input or found no secret'], [1, '--check finds one'], [2, 'a usage error']]
|
|
10
13
|
|
|
11
14
|
const options = {
|
|
12
15
|
'check': { type: 'boolean', description: 'list where secrets are instead of masking them, exit 1 when found' },
|
|
@@ -14,7 +17,7 @@ const options = {
|
|
|
14
17
|
} as const
|
|
15
18
|
|
|
16
19
|
export async function redact(args: string[]): Promise<number> {
|
|
17
|
-
const parsed = parse(args, usage, options, { replaces: 'printing a command\'s secrets into a log or a transcript', examples: [['docker compose config | redact', 'the resolved compose file, secrets masked'], ['git diff --cached | redact --check', 'refuse a commit carrying a secret']] })
|
|
20
|
+
const parsed = parse(args, usage, options, { exits: EXITS, replaces: 'printing a command\'s secrets into a log or a transcript', examples: [['docker compose config | redact', 'the resolved compose file, secrets masked'], ['git diff --cached | redact --check', 'refuse a commit carrying a secret']] })
|
|
18
21
|
|
|
19
22
|
const known = parsed.values['no-env'] === true ? undefined : await loadKnown()
|
|
20
23
|
|
package/src/files.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { Dotenv } from './dotenv'
|
|
|
3
3
|
import type { KnownSecrets } from './mask'
|
|
4
4
|
import { chmod, chown, readdir, realpath, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
5
5
|
import { basename, dirname, join } from 'node:path'
|
|
6
|
-
import { fail,
|
|
6
|
+
import { fail, ignoredFolders, isSecretName } from '@kaynooo/cli-kit'
|
|
7
7
|
import { entries, parseDotenv, serializeDotenv } from './dotenv'
|
|
8
8
|
import { knownSecrets, urlPasswords } from './mask'
|
|
9
9
|
|
|
@@ -14,7 +14,7 @@ export const TEMPLATE = /\.(?:example|sample|template|dist|defaults)$/
|
|
|
14
14
|
// Folders searched for secrets below the current one, and the ones never entered.
|
|
15
15
|
const KNOWN_DEPTH = 3
|
|
16
16
|
|
|
17
|
-
export const SKIPPED =
|
|
17
|
+
export const SKIPPED = ignoredFolders(['.git', '.kay'])
|
|
18
18
|
const LOCK_WAIT_MS = 5000
|
|
19
19
|
const LOCK_STALE_MS = 30_000
|
|
20
20
|
// A reap takes a few milliseconds, so a guard older than this belongs to a reaper that died.
|