@kaynooo/cli-env 0.8.1 → 0.9.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.
- package/README.md +6 -4
- package/dist/ev.js +200 -76
- package/dist/redact.js +91 -28
- package/package.json +7 -4
- package/src/audit.ts +167 -0
- package/src/commands/audit.ts +135 -0
- package/src/commands/compare.ts +77 -0
- package/src/commands/edit.ts +181 -0
- package/src/commands/pins.ts +98 -0
- package/src/commands/read.ts +193 -0
- package/src/dotenv.ts +8 -4
- package/src/ev.ts +31 -0
- package/src/files.ts +242 -0
- package/src/fix.ts +294 -0
- package/src/guard-dumps.ts +114 -0
- package/src/guard-names.ts +107 -0
- package/src/guard-paths.ts +80 -0
- package/src/guard-pipes.ts +76 -0
- package/src/guard-search.ts +181 -0
- package/src/guard.ts +51 -0
- package/src/mask.ts +264 -0
- package/src/pins.ts +268 -0
- package/src/redact.ts +43 -0
- package/src/redactor.ts +166 -0
package/src/files.ts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import type { Dirent } from 'node:fs'
|
|
2
|
+
import type { Dotenv } from './dotenv'
|
|
3
|
+
import type { KnownSecrets } from './mask'
|
|
4
|
+
import { chmod, chown, readdir, realpath, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
5
|
+
import { basename, dirname, join } from 'node:path'
|
|
6
|
+
import { fail, isSecretName } from '@kaynooo/cli-kit'
|
|
7
|
+
import { entries, parseDotenv, serializeDotenv } from './dotenv'
|
|
8
|
+
import { knownSecrets, urlPasswords } from './mask'
|
|
9
|
+
|
|
10
|
+
// A dotenv file's name (`.env`, `.env.local`, `prod.env`, `.envrc`), and the templates among them that hold no values.
|
|
11
|
+
export const ENV_FILE = /^(?:\.env(?:\..+)?|.+\.env|\.envrc)$/
|
|
12
|
+
export const TEMPLATE = /\.(?:example|sample|template|dist|defaults)$/
|
|
13
|
+
|
|
14
|
+
// Folders searched for secrets below the current one, and the ones never entered.
|
|
15
|
+
const KNOWN_DEPTH = 3
|
|
16
|
+
const SKIPPED = new Set(['node_modules', '.git', 'vendor'])
|
|
17
|
+
const LOCK_WAIT_MS = 5000
|
|
18
|
+
const LOCK_STALE_MS = 30_000
|
|
19
|
+
// A reap takes a few milliseconds, so a guard older than this belongs to a reaper that died.
|
|
20
|
+
const REAP_STALE_MS = 5000
|
|
21
|
+
|
|
22
|
+
// A regular file, or a link to one (a dangling link or a link to a folder is not).
|
|
23
|
+
export async function isFileEntry(entry: Dirent, path: string): Promise<boolean> {
|
|
24
|
+
if (entry.isFile())
|
|
25
|
+
return true
|
|
26
|
+
|
|
27
|
+
if (!entry.isSymbolicLink())
|
|
28
|
+
return false
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
return (await stat(path)).isFile()
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// The .env* files of a folder, sorted.
|
|
39
|
+
export async function envFiles(folder = process.cwd()): Promise<string[]> {
|
|
40
|
+
const found = await readdir(folder, { withFileTypes: true })
|
|
41
|
+
const names: string[] = []
|
|
42
|
+
|
|
43
|
+
for (const entry of found) {
|
|
44
|
+
if (ENV_FILE.test(entry.name) && await isFileEntry(entry, join(folder, entry.name)))
|
|
45
|
+
names.push(entry.name)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return names.sort()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function readEnv(path: string): Promise<Dotenv> {
|
|
52
|
+
const file = Bun.file(path)
|
|
53
|
+
|
|
54
|
+
if (!await file.exists())
|
|
55
|
+
fail(`${path} does not exist`)
|
|
56
|
+
|
|
57
|
+
return parseDotenv(await file.text())
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Writes through a temp file in the same folder renamed over the target, so a crash or a full disk never leaves a half-written file. The target keeps its mode, owner and group and a new file is 0600.
|
|
61
|
+
export async function writeAtomic(path: string, text: string): Promise<void> {
|
|
62
|
+
let target = path
|
|
63
|
+
let mode = 0o600
|
|
64
|
+
let owner: { uid: number, gid: number } | undefined
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
target = await realpath(path)
|
|
68
|
+
|
|
69
|
+
const current = await stat(target)
|
|
70
|
+
|
|
71
|
+
mode = current.mode & 0o777
|
|
72
|
+
owner = current
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT')
|
|
76
|
+
throw error
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const temp = join(dirname(target), `.${basename(target)}.${process.pid}.${crypto.randomUUID()}.tmp`)
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
await writeFile(temp, text, { mode: 0o600, flag: 'wx' })
|
|
83
|
+
|
|
84
|
+
if (owner !== undefined)
|
|
85
|
+
await keepOwner(temp, owner)
|
|
86
|
+
|
|
87
|
+
await chmod(temp, mode)
|
|
88
|
+
await rename(temp, target)
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
await rm(temp, { force: true })
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Gives the file back to its owner when a root run would otherwise take it; anyone else cannot change owners and already owns what they write.
|
|
96
|
+
async function keepOwner(path: string, owner: { uid: number, gid: number }): Promise<void> {
|
|
97
|
+
try {
|
|
98
|
+
await chown(path, owner.uid, owner.gid)
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if ((error as NodeJS.ErrnoException).code !== 'EPERM')
|
|
102
|
+
throw error
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function writeEnv(path: string, file: Dotenv): Promise<void> {
|
|
107
|
+
await writeAtomic(path, serializeDotenv(file))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Runs `fn` holding a lock file beside each path, so two ev runs never read-modify-write the same file at once. A lock older than LOCK_STALE_MS belongs to a dead run and is taken over.
|
|
111
|
+
export async function withLocks<T>(paths: string[], fn: () => Promise<T>): Promise<T> {
|
|
112
|
+
const held: string[] = []
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
for (const path of [...new Set(paths)].sort())
|
|
116
|
+
held.push(await acquire(path))
|
|
117
|
+
|
|
118
|
+
return await fn()
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
for (const lock of held)
|
|
122
|
+
await rm(lock, { force: true })
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Removes a stale lock under a guard file, so that of two waiters seeing it stale the second one reads the fresh lock the first one's successor wrote instead of removing it.
|
|
127
|
+
async function reap(lock: string): Promise<void> {
|
|
128
|
+
const guard = `${lock}.reap`
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
await writeFile(guard, String(process.pid), { flag: 'wx', mode: 0o600 })
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
try {
|
|
135
|
+
if (Date.now() - (await stat(guard)).mtimeMs > REAP_STALE_MS)
|
|
136
|
+
await rm(guard, { force: true })
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// released between the two calls
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
if (Date.now() - (await stat(lock)).mtimeMs > LOCK_STALE_MS)
|
|
147
|
+
await rm(lock, { force: true })
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
// released between the two calls
|
|
151
|
+
}
|
|
152
|
+
finally {
|
|
153
|
+
await rm(guard, { force: true })
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function acquire(path: string): Promise<string> {
|
|
158
|
+
let real = path
|
|
159
|
+
|
|
160
|
+
try {
|
|
161
|
+
real = await realpath(path)
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
// a missing file is locked by its own name
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const lock = join(dirname(real), `.${basename(real)}.lock`)
|
|
168
|
+
const deadline = Date.now() + LOCK_WAIT_MS
|
|
169
|
+
|
|
170
|
+
while (true) {
|
|
171
|
+
try {
|
|
172
|
+
await writeFile(lock, String(process.pid), { flag: 'wx', mode: 0o600 })
|
|
173
|
+
|
|
174
|
+
return lock
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
if ((error as NodeJS.ErrnoException).code !== 'EEXIST')
|
|
178
|
+
throw error
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
await reap(lock)
|
|
182
|
+
|
|
183
|
+
if (Date.now() > deadline)
|
|
184
|
+
fail(`${path} is locked by another ev run (${lock})`, 1)
|
|
185
|
+
|
|
186
|
+
await Bun.sleep(25)
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// The secrets of a parsed file: values of secret-looking keys and the password of any URL value.
|
|
191
|
+
export function secretPairs(file: Dotenv): [string, string][] {
|
|
192
|
+
const pairs: [string, string][] = []
|
|
193
|
+
|
|
194
|
+
for (const [key, value] of entries(file)) {
|
|
195
|
+
if (isSecretName(key))
|
|
196
|
+
pairs.push([key, value])
|
|
197
|
+
else
|
|
198
|
+
pairs.push(...urlPasswords(value).map(password => [key, password] as [string, string]))
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return pairs
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// The .env* files of a folder and of the folders below it, down to KNOWN_DEPTH.
|
|
205
|
+
async function envFilesBelow(folder: string, depth = 0): Promise<string[]> {
|
|
206
|
+
const found: string[] = []
|
|
207
|
+
let children: Dirent[]
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
children = await readdir(folder, { withFileTypes: true })
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
return found
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
for (const entry of children) {
|
|
217
|
+
const path = join(folder, entry.name)
|
|
218
|
+
|
|
219
|
+
if (entry.isDirectory() && depth < KNOWN_DEPTH && !SKIPPED.has(entry.name))
|
|
220
|
+
found.push(...await envFilesBelow(path, depth + 1))
|
|
221
|
+
else if (ENV_FILE.test(entry.name) && await isFileEntry(entry, path))
|
|
222
|
+
found.push(path)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return found
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// The secrets of the folder's .env* files and of those in the folders below it, plus any `extra` parsed file, ready to be searched for in output.
|
|
229
|
+
export async function loadKnown(folder = process.cwd(), extra: Dotenv[] = []): Promise<KnownSecrets> {
|
|
230
|
+
const pairs = extra.flatMap(secretPairs)
|
|
231
|
+
|
|
232
|
+
for (const path of await envFilesBelow(folder)) {
|
|
233
|
+
try {
|
|
234
|
+
pairs.push(...secretPairs(parseDotenv(await Bun.file(path).text())))
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
// a file that cannot be read has no secret to learn
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return knownSecrets(pairs)
|
|
242
|
+
}
|
package/src/fix.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { chmod, chown, lstat, rm, stat } from 'node:fs/promises'
|
|
2
|
+
import { dirname, join } from 'node:path'
|
|
3
|
+
import { compose } from '@kaynooo/cli-kit'
|
|
4
|
+
import { isInlineSecret } from './audit'
|
|
5
|
+
import { entries, escapeDouble, LINE_BREAK, parseDotenv, serializeDotenv, setLine } from './dotenv'
|
|
6
|
+
import { writeAtomic } from './files'
|
|
7
|
+
|
|
8
|
+
export interface Left {
|
|
9
|
+
key: string
|
|
10
|
+
why: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ComposeFix {
|
|
14
|
+
path: string
|
|
15
|
+
env: string
|
|
16
|
+
moved: string[]
|
|
17
|
+
left: Left[]
|
|
18
|
+
// The compose file or .env another account owns, so its mode could not be changed.
|
|
19
|
+
open: string[]
|
|
20
|
+
// How `docker compose config` compared before and after: undefined when nothing moved.
|
|
21
|
+
check?: 'same' | 'rolled back' | 'no docker' | 'fails before'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// One environment entry of a compose line: its value as compose reads it, and the line with the value swapped for a reference, or why it cannot be swapped.
|
|
25
|
+
type Entry = { key: string, value: string } & ({ swap: (ref: string) => string } | { unmovable: string })
|
|
26
|
+
|
|
27
|
+
const MAP_ENTRY = /^(\s*)(["']?)([A-Z0-9_]+)\2(\s*:)(.*)$/
|
|
28
|
+
const LIST_ENTRY = /^(\s*-[ \t]*)(["']?)([A-Z0-9_]+)=(.*)$/
|
|
29
|
+
const TRAILING = /^\s*(?:#.*)?$/
|
|
30
|
+
const PLAIN_COMMENT = /\s+#.*$/
|
|
31
|
+
const NOT_PLAIN = /^[|>&*![{]/
|
|
32
|
+
const ENV_PLAIN = /^[^\s#"'\\$`]*$/
|
|
33
|
+
|
|
34
|
+
// The inside of a YAML quoted scalar starting after its opening quote: double quotes take only \\ and \" escapes here.
|
|
35
|
+
function quoted(text: string, quote: string): { value: string, after: string } | undefined {
|
|
36
|
+
let value = ''
|
|
37
|
+
|
|
38
|
+
for (let index = 0; index < text.length; index++) {
|
|
39
|
+
const char = text[index] ?? ''
|
|
40
|
+
const next = text[index + 1]
|
|
41
|
+
|
|
42
|
+
if (quote === '\'' && char === '\'' && next === '\'') {
|
|
43
|
+
value += '\''
|
|
44
|
+
index++
|
|
45
|
+
}
|
|
46
|
+
else if (quote === '"' && char === '\\') {
|
|
47
|
+
if (next !== '\\' && next !== '"')
|
|
48
|
+
return undefined
|
|
49
|
+
|
|
50
|
+
value += next
|
|
51
|
+
index++
|
|
52
|
+
}
|
|
53
|
+
else if (char === quote) {
|
|
54
|
+
return { value, after: text.slice(index + 1) }
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
value += char
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return undefined
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function plain(text: string): { value: string, after: string } {
|
|
65
|
+
const value = text.replace(PLAIN_COMMENT, '').trimEnd()
|
|
66
|
+
|
|
67
|
+
return { value, after: text.slice(value.length) }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function composeEntry(line: string): Entry | undefined {
|
|
71
|
+
const map = MAP_ENTRY.exec(line)
|
|
72
|
+
|
|
73
|
+
if (map !== null) {
|
|
74
|
+
const [, indent = '', keyQuote = '', key = '', colon = '', spaced = ''] = map
|
|
75
|
+
const rest = spaced.trimStart()
|
|
76
|
+
const separator = colon + spaced.slice(0, spaced.length - rest.length)
|
|
77
|
+
const quote = rest[0] === '"' || rest[0] === '\'' ? rest[0] : ''
|
|
78
|
+
const scalar = quote === '' ? plain(rest) : quoted(rest.slice(1), quote)
|
|
79
|
+
|
|
80
|
+
if (scalar === undefined || !TRAILING.test(scalar.after))
|
|
81
|
+
return { key, value: rest, unmovable: 'its value is written in a form ev does not rewrite' }
|
|
82
|
+
|
|
83
|
+
if (quote === '' && NOT_PLAIN.test(scalar.value))
|
|
84
|
+
return { key, value: scalar.value, unmovable: 'its value is a block, anchor, alias, tag or collection' }
|
|
85
|
+
|
|
86
|
+
return { key, value: scalar.value, swap: ref => `${indent}${keyQuote}${key}${keyQuote}${separator}${quote}${ref}${quote}${scalar.after}` }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const list = LIST_ENTRY.exec(line)
|
|
90
|
+
|
|
91
|
+
if (list === null)
|
|
92
|
+
return undefined
|
|
93
|
+
|
|
94
|
+
const [, dash = '', quote = '', key = '', rest = ''] = list
|
|
95
|
+
const scalar = quote === '' ? plain(rest) : quoted(rest, quote)
|
|
96
|
+
|
|
97
|
+
if (scalar === undefined || !TRAILING.test(scalar.after))
|
|
98
|
+
return { key, value: rest, unmovable: 'its value is written in a form ev does not rewrite' }
|
|
99
|
+
|
|
100
|
+
return { key, value: scalar.value, swap: ref => `${dash}${quote}${key}=${ref}${quote}${scalar.after}` }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The .env line holding a value compose and Bun both read literally: plain when nothing needs quoting, single-quoted otherwise, double-quoted with each `$` escaped when it holds one since Bun expands it even in single quotes.
|
|
104
|
+
export function envLine(key: string, value: string): string | undefined {
|
|
105
|
+
if (ENV_PLAIN.test(value))
|
|
106
|
+
return `${key}=${value}`
|
|
107
|
+
|
|
108
|
+
if (LINE_BREAK.test(value))
|
|
109
|
+
return undefined
|
|
110
|
+
|
|
111
|
+
if (value.includes('$'))
|
|
112
|
+
return `${key}="${escapeDouble(value)}"`
|
|
113
|
+
|
|
114
|
+
return value.includes('\'') ? undefined : `${key}='${value}'`
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// A link, dangling or not: writing through it would change a file elsewhere.
|
|
118
|
+
async function isLink(path: string): Promise<boolean> {
|
|
119
|
+
try {
|
|
120
|
+
return (await lstat(path)).isSymbolicLink()
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT')
|
|
124
|
+
return false
|
|
125
|
+
|
|
126
|
+
throw error
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// What `docker compose config` resolves the file to, never printed; undefined when it fails.
|
|
131
|
+
async function resolved(path: string): Promise<string | undefined> {
|
|
132
|
+
const { code, stdout } = await compose(['config', '--format', 'json'], { flags: ['-f', path], cwd: dirname(path) })
|
|
133
|
+
|
|
134
|
+
return code === 0 ? stdout : undefined
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
interface Resolved {
|
|
138
|
+
services?: Record<string, { environment?: Record<string, unknown> }>
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// The services a key reached only once moved: they load the .env through env_file.
|
|
142
|
+
function gainedBy(before: string, after: string, key: string): string[] {
|
|
143
|
+
const services = (config: string) => Object.entries((JSON.parse(config) as Resolved).services ?? {})
|
|
144
|
+
const had = new Set(services(before).filter(([, service]) => service.environment?.[key] !== undefined).map(([name]) => name))
|
|
145
|
+
|
|
146
|
+
return services(after).filter(([name, service]) => service.environment?.[key] !== undefined && !had.has(name)).map(([name]) => name)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Takes others, and the group too when `group`, out of the file's mode. False when another account owns the file, so only it or root can change its mode.
|
|
150
|
+
export async function shutOthersOut(path: string, group = false): Promise<boolean> {
|
|
151
|
+
try {
|
|
152
|
+
await chmod(path, (await stat(path)).mode & (group ? 0o700 : 0o770))
|
|
153
|
+
|
|
154
|
+
return true
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
if ((error as NodeJS.ErrnoException).code === 'EPERM')
|
|
158
|
+
return false
|
|
159
|
+
|
|
160
|
+
throw error
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Moves each inline secret of a compose file into the .env beside it, swapping the value for `${KEY}`, then shuts others out of both. A key the .env sets to another value stays put, and a move `docker compose config` resolves differently is put back.
|
|
165
|
+
export async function fixCompose(path: string, group = false): Promise<ComposeFix> {
|
|
166
|
+
const env = join(dirname(path), '.env')
|
|
167
|
+
const compose = await Bun.file(path).text()
|
|
168
|
+
const envBefore = await Bun.file(env).exists() ? await Bun.file(env).text() : undefined
|
|
169
|
+
const dotenv = parseDotenv(envBefore ?? '')
|
|
170
|
+
const known = entries(dotenv)
|
|
171
|
+
const fix: ComposeFix = { path, env, moved: [], left: [], open: [] }
|
|
172
|
+
const linked = await isLink(env)
|
|
173
|
+
|
|
174
|
+
const lines = compose.split('\n').map((line) => {
|
|
175
|
+
const entry = composeEntry(line)
|
|
176
|
+
|
|
177
|
+
if (entry === undefined || !isInlineSecret(entry.key, entry.value))
|
|
178
|
+
return line
|
|
179
|
+
|
|
180
|
+
const { key } = entry
|
|
181
|
+
|
|
182
|
+
if ('unmovable' in entry) {
|
|
183
|
+
fix.left.push({ key, why: entry.unmovable })
|
|
184
|
+
|
|
185
|
+
return line
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (linked) {
|
|
189
|
+
fix.left.push({ key, why: 'its .env is a symbolic link' })
|
|
190
|
+
|
|
191
|
+
return line
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (entry.value.replaceAll('$$', '').includes('$')) {
|
|
195
|
+
fix.left.push({ key, why: 'its value reads another variable' })
|
|
196
|
+
|
|
197
|
+
return line
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const value = entry.value.replaceAll('$$', '$')
|
|
201
|
+
const existing = known.get(key)
|
|
202
|
+
|
|
203
|
+
if (existing === undefined) {
|
|
204
|
+
const raw = envLine(key, value)
|
|
205
|
+
|
|
206
|
+
if (raw === undefined) {
|
|
207
|
+
fix.left.push({ key, why: 'its value holds a quote or line break a .env cannot keep literal' })
|
|
208
|
+
|
|
209
|
+
return line
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
setLine(dotenv, key, value, raw)
|
|
213
|
+
known.set(key, value)
|
|
214
|
+
}
|
|
215
|
+
else if (existing !== value) {
|
|
216
|
+
fix.left.push({ key, why: `${env.slice(env.lastIndexOf('/') + 1)} already sets it to another value` })
|
|
217
|
+
|
|
218
|
+
return line
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (!fix.moved.includes(key))
|
|
222
|
+
fix.moved.push(key)
|
|
223
|
+
|
|
224
|
+
return entry.swap(`\${${key}}`)
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
if (fix.moved.length > 0)
|
|
228
|
+
await move(fix, compose, lines.join('\n'), envBefore, serializeDotenv(dotenv))
|
|
229
|
+
|
|
230
|
+
for (const file of [path, env]) {
|
|
231
|
+
if (!(file === env && linked) && await Bun.file(file).exists() && !await shutOthersOut(file, group))
|
|
232
|
+
fix.open.push(file)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return fix
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function move(fix: ComposeFix, compose: string, composeAfter: string, envBefore: string | undefined, envAfter: string): Promise<void> {
|
|
239
|
+
const { path, env } = fix
|
|
240
|
+
const docker = Bun.which('docker') !== null
|
|
241
|
+
const before = docker ? await resolved(path) : undefined
|
|
242
|
+
|
|
243
|
+
if (envBefore === undefined) {
|
|
244
|
+
await writeAtomic(env, envAfter)
|
|
245
|
+
|
|
246
|
+
// A root run gives the .env to whoever owns the compose file; anyone else already owns what they write.
|
|
247
|
+
const { uid, gid } = await stat(path)
|
|
248
|
+
|
|
249
|
+
try {
|
|
250
|
+
await chown(env, uid, gid)
|
|
251
|
+
}
|
|
252
|
+
catch {}
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
await writeAtomic(env, envAfter)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
await writeAtomic(path, composeAfter)
|
|
259
|
+
|
|
260
|
+
if (!docker) {
|
|
261
|
+
fix.check = 'no docker'
|
|
262
|
+
|
|
263
|
+
return
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (before === undefined) {
|
|
267
|
+
fix.check = 'fails before'
|
|
268
|
+
|
|
269
|
+
return
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const after = await resolved(path)
|
|
273
|
+
|
|
274
|
+
if (after === before) {
|
|
275
|
+
fix.check = 'same'
|
|
276
|
+
|
|
277
|
+
return
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
await writeAtomic(path, compose)
|
|
281
|
+
|
|
282
|
+
if (envBefore === undefined)
|
|
283
|
+
await rm(env)
|
|
284
|
+
else
|
|
285
|
+
await writeAtomic(env, envBefore)
|
|
286
|
+
|
|
287
|
+
fix.check = 'rolled back'
|
|
288
|
+
fix.left.push(...fix.moved.map((key) => {
|
|
289
|
+
const gained = after === undefined ? [] : gainedBy(before, after, key)
|
|
290
|
+
|
|
291
|
+
return { key, why: gained.length > 0 ? `${gained.join(', ')} ${gained.length > 1 ? 'load' : 'loads'} the .env through env_file and would gain it, so it was put back` : 'docker compose config resolved differently once moved, so it was put back' }
|
|
292
|
+
}))
|
|
293
|
+
fix.moved = []
|
|
294
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { splitCommand, tokenize } from '@kaynooo/cli-kit/shell'
|
|
2
|
+
|
|
3
|
+
const OPENER = /^[({]+/
|
|
4
|
+
const DUMP_FLAGS = /^-[a-z]*[px]/
|
|
5
|
+
// Git commands whose output carries the content of tracked files.
|
|
6
|
+
const GIT_PATCHES = new Set(['diff', 'log', 'show', 'whatchanged', 'format-patch', 'grep'])
|
|
7
|
+
const GIT_NO_PATCH = new Set(['--stat', '--numstat', '--shortstat', '--name-only', '--name-status', '--summary', '-s', '--no-patch', '--check', '--quiet'])
|
|
8
|
+
const GIT_VALUE_OPTIONS = new Set(['-C', '-c', '--git-dir', '--work-tree', '--namespace', '--exec-path'])
|
|
9
|
+
const GIT_PATCH_FLAGS = new Set(['-p', '-u', '--patch', '-c', '--cc', '--patch-with-stat', '--patch-with-raw'])
|
|
10
|
+
|
|
11
|
+
export const GIT_EXCLUDE = /^:(?:!|\^|\(exclude\))/
|
|
12
|
+
// Programs printing a process's whole environment.
|
|
13
|
+
const ENV_PRINTERS = new Set(['env', 'printenv'])
|
|
14
|
+
const DOCKER = new Set(['docker', 'docker-compose'])
|
|
15
|
+
const SHELLS = new Set(['sh', 'bash', 'zsh', 'ash'])
|
|
16
|
+
// `-c`, and `-lc`, `-ic` or `-ce` where the short flags are combined.
|
|
17
|
+
const SHELL_COMMAND_FLAG = /^-[abd-z]*c[a-z]*$/
|
|
18
|
+
// Options taking a value, before a docker or compose subcommand, and before exec's container.
|
|
19
|
+
const DOCKER_VALUE_OPTIONS = new Set(['-f', '--file', '-p', '--project-name', '--profile', '--env-file', '--project-directory', '--ansi', '--progress', '--parallel', '-H', '--host', '-c', '--context', '--config', '-l', '--log-level'])
|
|
20
|
+
const EXEC_VALUE_OPTIONS = new Set(['-e', '--env', '--env-file', '-u', '--user', '-w', '--workdir', '--index', '--detach-keys'])
|
|
21
|
+
// `compose config` options printing names, never values.
|
|
22
|
+
const CONFIG_LISTS = new Set(['--services', '--volumes', '--profiles', '--images', '--networks', '--hash', '-q', '--quiet'])
|
|
23
|
+
const FORMAT_OPTIONS = new Set(['-f', '--format'])
|
|
24
|
+
// An inspect format still printing the environment, or everything.
|
|
25
|
+
const WHOLE_FORMAT = /Env|\{\{\s*json\s+\.\s*\}\}|^json$/
|
|
26
|
+
|
|
27
|
+
// The index of the first word that is no option, past the values the options given take.
|
|
28
|
+
function firstOperand(words: readonly string[], valueOptions: ReadonlySet<string>): number {
|
|
29
|
+
for (let i = 0; i < words.length; i++) {
|
|
30
|
+
const word = words[i] ?? ''
|
|
31
|
+
|
|
32
|
+
if (valueOptions.has(word))
|
|
33
|
+
i++
|
|
34
|
+
else if (!word.startsWith('-'))
|
|
35
|
+
return i
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return words.length
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// The command a container runs prints its environment: `env` or `printenv` as the program, or as one run by `sh -c`.
|
|
42
|
+
function printsEnv([program = '', ...args]: readonly string[]): boolean {
|
|
43
|
+
const flags = args.findIndex(arg => !arg.startsWith('-') || SHELL_COMMAND_FLAG.test(arg))
|
|
44
|
+
|
|
45
|
+
if (SHELLS.has(program) && SHELL_COMMAND_FLAG.test(args[flags] ?? ''))
|
|
46
|
+
return splitCommand(args[flags + 1] ?? '').some(piece => ENV_PRINTERS.has(tokenize(piece.command)[0] ?? ''))
|
|
47
|
+
|
|
48
|
+
return ENV_PRINTERS.has(program)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// A docker call printing a container's environment: `env` or `printenv` run through exec, `compose config` and `inspect`, which spell it out.
|
|
52
|
+
export function dumpsContainerEnv(program: string, args: string[]): boolean {
|
|
53
|
+
if (!DOCKER.has(program))
|
|
54
|
+
return false
|
|
55
|
+
|
|
56
|
+
const afterOptions = (words: readonly string[]) => words.slice(firstOperand(words, DOCKER_VALUE_OPTIONS))
|
|
57
|
+
let rest = afterOptions(args)
|
|
58
|
+
const isCompose = program === 'docker-compose' || rest[0] === 'compose'
|
|
59
|
+
|
|
60
|
+
if (program === 'docker' && isCompose)
|
|
61
|
+
rest = afterOptions(rest.slice(1))
|
|
62
|
+
|
|
63
|
+
const [subcommand = '', ...after] = rest
|
|
64
|
+
|
|
65
|
+
if (subcommand === 'exec')
|
|
66
|
+
return printsEnv(after.slice(firstOperand(after, EXEC_VALUE_OPTIONS) + 1))
|
|
67
|
+
|
|
68
|
+
if (isCompose)
|
|
69
|
+
return subcommand === 'config' && !after.some(word => CONFIG_LISTS.has(word))
|
|
70
|
+
|
|
71
|
+
if (subcommand !== 'inspect' && (subcommand !== 'container' || after[0] !== 'inspect'))
|
|
72
|
+
return false
|
|
73
|
+
|
|
74
|
+
const format = after.find((_, i) => FORMAT_OPTIONS.has(after[i - 1] ?? '')) ?? after.find(word => word.startsWith('--format='))?.slice('--format='.length)
|
|
75
|
+
|
|
76
|
+
return format === undefined || WHOLE_FORMAT.test(format)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Programs printing the environment of the shell they run in: `env` and `printenv` alone, `export -p`, `declare -x`, a bare `set`.
|
|
80
|
+
export function dumpsEnvironment(program: string, args: readonly string[], tokens: readonly string[]): boolean {
|
|
81
|
+
const isBare = args.every(arg => arg.startsWith('-'))
|
|
82
|
+
|
|
83
|
+
if (program === 'printenv' || program === 'export')
|
|
84
|
+
return isBare
|
|
85
|
+
|
|
86
|
+
if (program === 'declare' || program === 'typeset')
|
|
87
|
+
return isBare && args.some(arg => DUMP_FLAGS.test(arg))
|
|
88
|
+
|
|
89
|
+
if (program === 'set')
|
|
90
|
+
return args.length === 0
|
|
91
|
+
|
|
92
|
+
return program === '' && tokens.some(token => token.replace(OPENER, '') === 'env')
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Whether a git command prints the content of a tracked env file: a patch or a search with no pathspec leaving env files out.
|
|
96
|
+
export function printsTrackedEnv(program: string, args: readonly string[]): boolean {
|
|
97
|
+
if (program !== 'git')
|
|
98
|
+
return false
|
|
99
|
+
|
|
100
|
+
const at = args.findIndex((arg, i) => !arg.startsWith('-') && !GIT_VALUE_OPTIONS.has(args[i - 1] ?? ''))
|
|
101
|
+
const name = args[at] ?? ''
|
|
102
|
+
const rest = args.slice(at + 1)
|
|
103
|
+
|
|
104
|
+
if (!GIT_PATCHES.has(name) || rest.some(arg => GIT_EXCLUDE.test(arg)))
|
|
105
|
+
return false
|
|
106
|
+
|
|
107
|
+
if (name === 'grep')
|
|
108
|
+
return true
|
|
109
|
+
|
|
110
|
+
if (name === 'log' || name === 'whatchanged')
|
|
111
|
+
return rest.some(arg => GIT_PATCH_FLAGS.has(arg))
|
|
112
|
+
|
|
113
|
+
return !rest.some(arg => GIT_NO_PATCH.has(arg))
|
|
114
|
+
}
|