@kaynooo/cli-env 0.9.1 → 0.11.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/dist/ev.js +436 -93
- package/dist/redact.js +263 -71
- package/package.json +2 -2
- package/src/commands/read.ts +18 -4
- package/src/commands/redact.ts +39 -0
- package/src/ev.ts +2 -0
- package/src/redact.ts +3 -39
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import process from 'node:process'
|
|
2
|
+
import { parse, readInput } from '@kaynooo/cli-kit'
|
|
3
|
+
import { loadKnown } from '../files'
|
|
4
|
+
import { check, Redactor } from '../redactor'
|
|
5
|
+
|
|
6
|
+
const usage = `redact [--check]
|
|
7
|
+
|
|
8
|
+
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\`.\n\nExits 0 when it copied the input or found no secret, 1 when --check finds one, 2 on a usage error.`
|
|
10
|
+
|
|
11
|
+
const options = {
|
|
12
|
+
'check': { type: 'boolean', description: 'list where secrets are instead of masking them, exit 1 when found' },
|
|
13
|
+
'no-env': { type: 'boolean', description: 'do not search for the values of the .env* files' },
|
|
14
|
+
} as const
|
|
15
|
+
|
|
16
|
+
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']] })
|
|
18
|
+
|
|
19
|
+
const known = parsed.values['no-env'] === true ? undefined : await loadKnown()
|
|
20
|
+
|
|
21
|
+
if (parsed.values.check === true) {
|
|
22
|
+
const findings = check(await readInput('-'), known)
|
|
23
|
+
|
|
24
|
+
for (const { file, line, kind } of findings)
|
|
25
|
+
console.log(`${file}:${line}: ${kind}`)
|
|
26
|
+
|
|
27
|
+
return findings.length === 0 ? 0 : 1
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const redactor = new Redactor(known)
|
|
31
|
+
const decoder = new TextDecoder()
|
|
32
|
+
|
|
33
|
+
for await (const chunk of Bun.stdin.stream())
|
|
34
|
+
process.stdout.write(redactor.push(decoder.decode(chunk, { stream: true })))
|
|
35
|
+
|
|
36
|
+
process.stdout.write(redactor.push(decoder.decode()) + redactor.end())
|
|
37
|
+
|
|
38
|
+
return 0
|
|
39
|
+
}
|
package/src/ev.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { diff, missing } from './commands/compare'
|
|
|
7
7
|
import { cp, mv, set, unset } from './commands/edit'
|
|
8
8
|
import { pins } from './commands/pins'
|
|
9
9
|
import { get, grep, list, show } from './commands/read'
|
|
10
|
+
import { redact } from './commands/redact'
|
|
10
11
|
|
|
11
12
|
process.exitCode = await runCli({
|
|
12
13
|
name: 'ev',
|
|
@@ -25,6 +26,7 @@ process.exitCode = await runCli({
|
|
|
25
26
|
diff: { run: diff, summary: 'keys set in one file and not the other' },
|
|
26
27
|
missing: { run: missing, summary: 'keys of .env.example that .env lacks' },
|
|
27
28
|
pins: { run: pins, summary: 'the env vars reaching outside that an app\'s tests leave to a developer\'s env' },
|
|
29
|
+
redact: { run: redact, summary: 'stdin copied to stdout with secrets masked, or --check to list where they are' },
|
|
28
30
|
audit: { run: audit, summary: '.env files others can read or git tracks, compose files writing secrets out' },
|
|
29
31
|
},
|
|
30
32
|
aliases: { ls: 'list' },
|
package/src/redact.ts
CHANGED
|
@@ -1,43 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env -S bun --no-env-file
|
|
2
2
|
|
|
3
3
|
import process from 'node:process'
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { check, Redactor } from './redactor'
|
|
4
|
+
import { runMain } from '@kaynooo/cli-kit'
|
|
5
|
+
import { redact } from './commands/redact'
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
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.
|
|
11
|
-
--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\`.\n\nExits 0 when it copied the input or found no secret, 1 when --check finds one, 2 on a usage error.`
|
|
12
|
-
|
|
13
|
-
const options = {
|
|
14
|
-
'check': { type: 'boolean', description: 'list where secrets are instead of masking them, exit 1 when found' },
|
|
15
|
-
'no-env': { type: 'boolean', description: 'do not search for the values of the .env* files' },
|
|
16
|
-
} as const
|
|
17
|
-
|
|
18
|
-
async function run(args: string[]): Promise<number> {
|
|
19
|
-
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
|
-
|
|
21
|
-
const known = parsed.values['no-env'] === true ? undefined : await loadKnown()
|
|
22
|
-
|
|
23
|
-
if (parsed.values.check === true) {
|
|
24
|
-
const findings = check(await readInput('-'), known)
|
|
25
|
-
|
|
26
|
-
for (const { file, line, kind } of findings)
|
|
27
|
-
console.log(`${file}:${line}: ${kind}`)
|
|
28
|
-
|
|
29
|
-
return findings.length === 0 ? 0 : 1
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const redactor = new Redactor(known)
|
|
33
|
-
const decoder = new TextDecoder()
|
|
34
|
-
|
|
35
|
-
for await (const chunk of Bun.stdin.stream())
|
|
36
|
-
process.stdout.write(redactor.push(decoder.decode(chunk, { stream: true })))
|
|
37
|
-
|
|
38
|
-
process.stdout.write(redactor.push(decoder.decode()) + redactor.end())
|
|
39
|
-
|
|
40
|
-
return 0
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
process.exitCode = await runMain({ name: 'redact', run })
|
|
7
|
+
process.exitCode = await runMain({ name: 'redact', run: redact })
|