@basaltkit/deploy 0.4.1 → 0.5.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 +284 -23
- package/dist/answers.d.ts +70 -0
- package/dist/answers.js +196 -0
- package/dist/apache.js +11 -2
- package/dist/audit.d.ts +20 -0
- package/dist/audit.js +26 -0
- package/dist/backup.d.ts +17 -1
- package/dist/backup.js +56 -1
- package/dist/bin.js +9 -1
- package/dist/bootstrap.d.ts +68 -18
- package/dist/bootstrap.js +177 -45
- package/dist/caddy.js +2 -1
- package/dist/check.js +18 -5
- package/dist/checks.js +1 -1
- package/dist/cli.d.ts +22 -1
- package/dist/cli.js +498 -57
- package/dist/compose.d.ts +1 -1
- package/dist/compose.js +6 -1
- package/dist/config.d.ts +25 -2
- package/dist/config.js +65 -3
- package/dist/context.d.ts +43 -13
- package/dist/context.js +50 -7
- package/dist/detach.d.ts +23 -0
- package/dist/detach.js +105 -0
- package/dist/env-step.d.ts +16 -1
- package/dist/env-step.js +61 -5
- package/dist/events.d.ts +168 -0
- package/dist/events.js +477 -0
- package/dist/host.d.ts +179 -7
- package/dist/host.js +317 -42
- package/dist/index.d.ts +10 -5
- package/dist/index.js +7 -3
- package/dist/init.d.ts +2 -0
- package/dist/init.js +2 -1
- package/dist/install-plan.d.ts +40 -0
- package/dist/install-plan.js +437 -0
- package/dist/json-host.d.ts +32 -0
- package/dist/json-host.js +106 -0
- package/dist/lock.d.ts +45 -0
- package/dist/lock.js +128 -0
- package/dist/pipeline.d.ts +51 -3
- package/dist/pipeline.js +339 -173
- package/dist/protocol.d.ts +364 -0
- package/dist/protocol.js +134 -0
- package/dist/runs.d.ts +75 -0
- package/dist/runs.js +287 -0
- package/dist/state.d.ts +26 -13
- package/dist/state.js +68 -4
- package/dist/trust.d.ts +63 -0
- package/dist/trust.js +159 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +10 -0
- package/package.json +5 -1
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import type { DeployConfig } from './config.js';
|
|
2
|
+
import type { LogHandle, Output } from './host.js';
|
|
3
|
+
import { type DeployEvent, type EventBody, type StepId } from './protocol.js';
|
|
4
|
+
/**
|
|
5
|
+
* One reporter, two renderers (RFC 0008 §5). Every command reports what it
|
|
6
|
+
* does through {@link Output}; the CLI reports its own beginning and end
|
|
7
|
+
* (`hello`, `error`, `done`) as events through a {@link Reporter}. A terminal
|
|
8
|
+
* renders those events as the text it always printed ({@link humanReporter});
|
|
9
|
+
* `--json` writes them as NDJSON ({@link EventWriter}), with the pipeline's
|
|
10
|
+
* Output turned into events by {@link eventOutput}.
|
|
11
|
+
*/
|
|
12
|
+
export interface Reporter {
|
|
13
|
+
emit(event: EventBody): void;
|
|
14
|
+
}
|
|
15
|
+
/** Events as the terminal has always shown them (nothing new is printed). */
|
|
16
|
+
export declare function humanReporter(out: Output): Reporter;
|
|
17
|
+
/**
|
|
18
|
+
* The pipeline's Output as events: steps begin and end, lines are `log`
|
|
19
|
+
* events of the step that runs, findings and results are structured.
|
|
20
|
+
*/
|
|
21
|
+
export type EventOutput = Output & Required<Pick<Output, 'stepEnd' | 'finding' | 'result'>> & {
|
|
22
|
+
readonly current: StepId | undefined;
|
|
23
|
+
};
|
|
24
|
+
export declare function eventOutput(reporter: Reporter): EventOutput;
|
|
25
|
+
/** Text without terminal escape sequences or control characters (tab kept; newlines only when asked). */
|
|
26
|
+
export declare function sanitize(text: string, keepNewlines?: boolean): string;
|
|
27
|
+
export declare const MAX_LINE_BYTES: number;
|
|
28
|
+
/** At most {@link MAX_LINE_BYTES} of UTF-8, never half a character, never half a `[redacted:KEY]` marker. */
|
|
29
|
+
export declare function truncate(text: string, max?: number): {
|
|
30
|
+
text: string;
|
|
31
|
+
truncated: boolean;
|
|
32
|
+
};
|
|
33
|
+
/** One line of a stream; `cut` when it had no end within the bound and the rest of it was dropped. */
|
|
34
|
+
export interface SplitLine {
|
|
35
|
+
text: string;
|
|
36
|
+
cut: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Splits a stream of text into lines: `\n`, `\r\n`, and a lone `\r` (a
|
|
40
|
+
* progress bar redrawing its line). A line longer than `maxPending` without
|
|
41
|
+
* an end is cut there and the rest of it, up to its end, is dropped: memory
|
|
42
|
+
* stays bounded, and a secret the cut splits never goes out as a second
|
|
43
|
+
* piece that no longer matches it (the writer drops the first piece's tail).
|
|
44
|
+
*/
|
|
45
|
+
export declare class LineSplitter {
|
|
46
|
+
private readonly maxPending;
|
|
47
|
+
private pending;
|
|
48
|
+
/** Inside a line already cut: everything up to its end is dropped. */
|
|
49
|
+
private skipping;
|
|
50
|
+
constructor(maxPending?: number);
|
|
51
|
+
push(text: string): SplitLine[];
|
|
52
|
+
end(): SplitLine[];
|
|
53
|
+
}
|
|
54
|
+
/** Shorter values are not matched — they would shred ordinary text; a `secret-short` warning names them. */
|
|
55
|
+
export declare const MIN_REDACTED_LENGTH = 6;
|
|
56
|
+
/**
|
|
57
|
+
* The secret values this command knows, replaced by `[redacted:KEY]` in every
|
|
58
|
+
* string of every event — raw and URL-encoded, longest first. Only grows.
|
|
59
|
+
*/
|
|
60
|
+
export declare class Redactor {
|
|
61
|
+
private readonly needles;
|
|
62
|
+
private sorted;
|
|
63
|
+
/** Keys whose value is too short to be matched. */
|
|
64
|
+
readonly short: Set<string>;
|
|
65
|
+
/** `short` when the value is too short to match; `added` or `known` otherwise. */
|
|
66
|
+
add(key: string, value: string): 'added' | 'known' | 'short' | 'empty';
|
|
67
|
+
get size(): number;
|
|
68
|
+
/** The length of the longest needle: a cut can split at most `longest - 1` characters of one. */
|
|
69
|
+
get longest(): number;
|
|
70
|
+
apply(text: string): {
|
|
71
|
+
text: string;
|
|
72
|
+
hit: boolean;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** Whether the value of `key` is a secret for this config: generated, asked as a secret, or named like one. */
|
|
76
|
+
export declare function isSecretKey(key: string, config?: DeployConfig): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Whether the whole value of `key` is learnt as a secret. A key that is a
|
|
79
|
+
* secret only by its `_URL`/`_DSN` suffix and holds a bare origin
|
|
80
|
+
* (`APP_URL=https://app.example.com`) is not: that is the app's public
|
|
81
|
+
* address, and redacting it would hide the result's `url` and every health
|
|
82
|
+
* line. A URL with a path, a query or a user is learnt whole (a webhook URL's
|
|
83
|
+
* path is its secret); a URL's password is always learnt on its own.
|
|
84
|
+
*/
|
|
85
|
+
export declare function isSecretValue(key: string, value: string, config?: DeployConfig): boolean;
|
|
86
|
+
/** The password inside a `scheme://user:password@…` value, as written and decoded. */
|
|
87
|
+
export declare function urlPasswords(value: string): string[];
|
|
88
|
+
/**
|
|
89
|
+
* Learns the secrets of key/value pairs (a `.env`, a child's environment).
|
|
90
|
+
* Returns the keys that are too short to be redacted, the first time each is seen.
|
|
91
|
+
*/
|
|
92
|
+
export declare function learnSecrets(redactor: Redactor, pairs: Iterable<[string, string]>, config?: DeployConfig): string[];
|
|
93
|
+
/** {@link learnSecrets} over the text of a `.env`. */
|
|
94
|
+
export declare const learnEnvText: (redactor: Redactor, text: string, config?: DeployConfig) => string[];
|
|
95
|
+
/** A run log takes no more `log` events past this size (RFC 0008 §8.7). */
|
|
96
|
+
export declare const RUN_LOG_CAP: number;
|
|
97
|
+
export interface WriterOptions {
|
|
98
|
+
now: () => Date;
|
|
99
|
+
/** Where events go first: the real stdout (captured before anything replaced it). */
|
|
100
|
+
stdout: (line: string) => void;
|
|
101
|
+
redactor: Redactor;
|
|
102
|
+
runId?: string;
|
|
103
|
+
/** stdout is the run log (a detached child): the cap applies to it. */
|
|
104
|
+
stdoutIsRunLog?: boolean;
|
|
105
|
+
/** Keep the events in memory until a run log is attached (they are written to it first). */
|
|
106
|
+
expectRunLog?: boolean;
|
|
107
|
+
cap?: number;
|
|
108
|
+
/** One plain line when an event cannot be written (stderr). */
|
|
109
|
+
onFailure?: (message: string) => void;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The one place events are serialised: it assigns `seq` and `ts`, redacts
|
|
113
|
+
* and sanitises every string field BEFORE `JSON.stringify` (a secret with `"`
|
|
114
|
+
* or `\` would be escaped in the JSON and missed afterwards; a child's stderr
|
|
115
|
+
* quoted in an error message carries no terminal escape), cuts a `log` line
|
|
116
|
+
* only once it is redacted (a secret across the cut would match neither
|
|
117
|
+
* half), then writes the line to stdout and to the run log. Nothing is
|
|
118
|
+
* written after `done`.
|
|
119
|
+
*/
|
|
120
|
+
export declare class EventWriter implements Reporter {
|
|
121
|
+
private readonly options;
|
|
122
|
+
private seq;
|
|
123
|
+
private logBytes;
|
|
124
|
+
private capped;
|
|
125
|
+
private log;
|
|
126
|
+
private backlog;
|
|
127
|
+
/** Every event written, in order (what `done` and the detaching parent look at). */
|
|
128
|
+
readonly last: {
|
|
129
|
+
error?: DeployEvent;
|
|
130
|
+
result?: DeployEvent;
|
|
131
|
+
done?: DeployEvent;
|
|
132
|
+
};
|
|
133
|
+
constructor(options: WriterOptions);
|
|
134
|
+
get runId(): string | undefined;
|
|
135
|
+
get nextSeq(): number;
|
|
136
|
+
emit(body: EventBody): void;
|
|
137
|
+
private write;
|
|
138
|
+
/** Writes what came before, then every event, to the run log. */
|
|
139
|
+
attachLog(log: LogHandle): void;
|
|
140
|
+
/** No run log for this command after all. */
|
|
141
|
+
dropBacklog(): void;
|
|
142
|
+
close(): void;
|
|
143
|
+
private appendLog;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Text written to the deploy's stdout or stderr by code that is not this
|
|
147
|
+
* tool's — the config's `console.log` — as `log{stream:'foreign'}` events
|
|
148
|
+
* (RFC 0008 §5.4): split, sanitised, redacted and cut by the writer, never
|
|
149
|
+
* interpreted.
|
|
150
|
+
*
|
|
151
|
+
* Until `release()`, the lines are held (at most {@link MAX_HELD_BYTES}):
|
|
152
|
+
* the config prints while it loads, before the redactor has read the `.env`
|
|
153
|
+
* that holds the secrets it might print.
|
|
154
|
+
*/
|
|
155
|
+
export interface ForeignSink {
|
|
156
|
+
write(text: string): void;
|
|
157
|
+
/** The secrets are known: what was held is emitted, and every line from now on. */
|
|
158
|
+
release(): void;
|
|
159
|
+
/** The text after the last newline, then {@link release}. */
|
|
160
|
+
end(): void;
|
|
161
|
+
}
|
|
162
|
+
export declare const MAX_HELD_BYTES: number;
|
|
163
|
+
export declare function foreignSink(reporter: Reporter, step: () => StepId | undefined): ForeignSink;
|
|
164
|
+
/** A child's output as `log{stream:'child'}` events: stdout `info`, stderr `warn`. */
|
|
165
|
+
export declare function childSink(reporter: Reporter, source: string, step: StepId | undefined): {
|
|
166
|
+
push(text: string, stream: 'stdout' | 'stderr'): void;
|
|
167
|
+
end(): void;
|
|
168
|
+
};
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
import { formatFinding } from './context.js';
|
|
2
|
+
import { envValues, parseEnv } from './dotenv.js';
|
|
3
|
+
import { SENSITIVE } from './init.js';
|
|
4
|
+
import { isStepId } from './protocol.js';
|
|
5
|
+
/** Events as the terminal has always shown them (nothing new is printed). */
|
|
6
|
+
export function humanReporter(out) {
|
|
7
|
+
return {
|
|
8
|
+
emit(event) {
|
|
9
|
+
switch (event.type) {
|
|
10
|
+
case 'step-start':
|
|
11
|
+
out.step(event.title, event.id);
|
|
12
|
+
return;
|
|
13
|
+
case 'log':
|
|
14
|
+
out[event.level](event.text);
|
|
15
|
+
return;
|
|
16
|
+
case 'finding':
|
|
17
|
+
if (event.level === 'error')
|
|
18
|
+
out.error(formatFinding(event));
|
|
19
|
+
else
|
|
20
|
+
out.warn(formatFinding(event));
|
|
21
|
+
return;
|
|
22
|
+
case 'error':
|
|
23
|
+
out.error(event.message);
|
|
24
|
+
return;
|
|
25
|
+
default: return;
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function eventOutput(reporter) {
|
|
31
|
+
let current;
|
|
32
|
+
const log = (level) => (text) => {
|
|
33
|
+
reporter.emit({ type: 'log', stream: 'tool', level, text: sanitize(text, true), redacted: false, ...(current ? { step: current } : {}) });
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
get current() { return current; },
|
|
37
|
+
step(title, id) {
|
|
38
|
+
const step = id ?? (isStepId(title) ? title : undefined);
|
|
39
|
+
if (!step) {
|
|
40
|
+
log('info')(title);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
current = step;
|
|
44
|
+
reporter.emit({ type: 'step-start', id: step, title });
|
|
45
|
+
},
|
|
46
|
+
info: log('info'),
|
|
47
|
+
ok: log('ok'),
|
|
48
|
+
warn: log('warn'),
|
|
49
|
+
error: log('error'),
|
|
50
|
+
stepEnd(id, status, durationMs) {
|
|
51
|
+
reporter.emit({ type: 'step-end', id, status, durationMs: Math.max(0, Math.round(durationMs)) });
|
|
52
|
+
if (current === id)
|
|
53
|
+
current = undefined;
|
|
54
|
+
},
|
|
55
|
+
finding(finding) {
|
|
56
|
+
reporter.emit({ type: 'finding', id: finding.id, level: finding.level, message: finding.message, ...(finding.fix ? { fix: finding.fix } : {}) });
|
|
57
|
+
},
|
|
58
|
+
result(command, data) {
|
|
59
|
+
reporter.emit({ type: 'result', command, data });
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
// ── text from outside: children, the config ──────────────────────────────
|
|
64
|
+
/** CSI, OSC, DCS/SOS/PM/APC, then any other escape (`ESC c`, `ESC ( B`). */
|
|
65
|
+
const ESCAPES = /\u001b\[[0-?]*[ -/]*[@-~]|\u001b\][^\u0007\u001b]*(?:\u0007|\u001b\\)?|\u001b[PX^_][^\u001b]*(?:\u001b\\)?|\u001b[ -/]*[0-~]|\u009b[0-?]*[ -/]*[@-~]/g;
|
|
66
|
+
const CONTROLS = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/g;
|
|
67
|
+
const CONTROLS_AND_NEWLINE = /[\u0000-\u0008\u000a-\u001f\u007f-\u009f]/g;
|
|
68
|
+
/** Text without terminal escape sequences or control characters (tab kept; newlines only when asked). */
|
|
69
|
+
export function sanitize(text, keepNewlines = false) {
|
|
70
|
+
return text.replace(ESCAPES, '').replace(keepNewlines ? CONTROLS : CONTROLS_AND_NEWLINE, '');
|
|
71
|
+
}
|
|
72
|
+
export const MAX_LINE_BYTES = 8 * 1024;
|
|
73
|
+
/** At most {@link MAX_LINE_BYTES} of UTF-8, never half a character, never half a `[redacted:KEY]` marker. */
|
|
74
|
+
export function truncate(text, max = MAX_LINE_BYTES) {
|
|
75
|
+
const bytes = Buffer.from(text, 'utf8');
|
|
76
|
+
if (bytes.length <= max)
|
|
77
|
+
return { text, truncated: false };
|
|
78
|
+
const cut = bytes.subarray(0, max).toString('utf8').replace(/\uFFFD+$/, '');
|
|
79
|
+
return { text: cut.replace(/\[(?:r(?:e(?:d(?:a(?:c(?:t(?:e(?:d(?::[A-Za-z0-9_]*)?)?)?)?)?)?)?)?)?$/, ''), truncated: true };
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Splits a stream of text into lines: `\n`, `\r\n`, and a lone `\r` (a
|
|
83
|
+
* progress bar redrawing its line). A line longer than `maxPending` without
|
|
84
|
+
* an end is cut there and the rest of it, up to its end, is dropped: memory
|
|
85
|
+
* stays bounded, and a secret the cut splits never goes out as a second
|
|
86
|
+
* piece that no longer matches it (the writer drops the first piece's tail).
|
|
87
|
+
*/
|
|
88
|
+
export class LineSplitter {
|
|
89
|
+
maxPending;
|
|
90
|
+
pending = '';
|
|
91
|
+
/** Inside a line already cut: everything up to its end is dropped. */
|
|
92
|
+
skipping = false;
|
|
93
|
+
constructor(maxPending = 64 * 1024) {
|
|
94
|
+
this.maxPending = maxPending;
|
|
95
|
+
}
|
|
96
|
+
push(text) {
|
|
97
|
+
this.pending += text;
|
|
98
|
+
const lines = [];
|
|
99
|
+
for (;;) {
|
|
100
|
+
const match = /\r\n|\n|\r/.exec(this.pending);
|
|
101
|
+
if (!match)
|
|
102
|
+
break;
|
|
103
|
+
// A '\r' at the very end may be the first half of '\r\n'.
|
|
104
|
+
if (match[0] === '\r' && match.index === this.pending.length - 1)
|
|
105
|
+
break;
|
|
106
|
+
if (!this.skipping)
|
|
107
|
+
lines.push({ text: this.pending.slice(0, match.index), cut: false });
|
|
108
|
+
this.skipping = false;
|
|
109
|
+
this.pending = this.pending.slice(match.index + match[0].length);
|
|
110
|
+
}
|
|
111
|
+
if (this.skipping) {
|
|
112
|
+
// Keep a lone '\r' at the end: it may begin the '\r\n' that ends the dropped line.
|
|
113
|
+
this.pending = this.pending.endsWith('\r') ? '\r' : '';
|
|
114
|
+
}
|
|
115
|
+
else if (this.pending.length > this.maxPending) {
|
|
116
|
+
lines.push({ text: this.pending.slice(0, this.maxPending), cut: true });
|
|
117
|
+
this.pending = '';
|
|
118
|
+
this.skipping = true;
|
|
119
|
+
}
|
|
120
|
+
return lines;
|
|
121
|
+
}
|
|
122
|
+
end() {
|
|
123
|
+
const rest = this.skipping ? '' : this.pending.replace(/\r$/, '');
|
|
124
|
+
this.pending = '';
|
|
125
|
+
this.skipping = false;
|
|
126
|
+
return rest ? [{ text: rest, cut: false }] : [];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// ── the redactor (RFC 0008 §6.2) ───────────────────────────────────────────
|
|
130
|
+
/** Shorter values are not matched — they would shred ordinary text; a `secret-short` warning names them. */
|
|
131
|
+
export const MIN_REDACTED_LENGTH = 6;
|
|
132
|
+
/**
|
|
133
|
+
* The secret values this command knows, replaced by `[redacted:KEY]` in every
|
|
134
|
+
* string of every event — raw and URL-encoded, longest first. Only grows.
|
|
135
|
+
*/
|
|
136
|
+
export class Redactor {
|
|
137
|
+
needles = new Map();
|
|
138
|
+
sorted;
|
|
139
|
+
/** Keys whose value is too short to be matched. */
|
|
140
|
+
short = new Set();
|
|
141
|
+
/** `short` when the value is too short to match; `added` or `known` otherwise. */
|
|
142
|
+
add(key, value) {
|
|
143
|
+
if (value === '')
|
|
144
|
+
return 'empty';
|
|
145
|
+
if (value.length < MIN_REDACTED_LENGTH) {
|
|
146
|
+
if (this.short.has(key))
|
|
147
|
+
return 'known';
|
|
148
|
+
this.short.add(key);
|
|
149
|
+
return 'short';
|
|
150
|
+
}
|
|
151
|
+
let added = false;
|
|
152
|
+
for (const form of new Set([value, encodeURIComponent(value)])) {
|
|
153
|
+
if (form.length < MIN_REDACTED_LENGTH || this.needles.has(form))
|
|
154
|
+
continue;
|
|
155
|
+
this.needles.set(form, key);
|
|
156
|
+
added = true;
|
|
157
|
+
}
|
|
158
|
+
if (added)
|
|
159
|
+
this.sorted = undefined;
|
|
160
|
+
return added ? 'added' : 'known';
|
|
161
|
+
}
|
|
162
|
+
get size() {
|
|
163
|
+
return this.needles.size;
|
|
164
|
+
}
|
|
165
|
+
/** The length of the longest needle: a cut can split at most `longest - 1` characters of one. */
|
|
166
|
+
get longest() {
|
|
167
|
+
let longest = 0;
|
|
168
|
+
for (const needle of this.needles.keys())
|
|
169
|
+
longest = Math.max(longest, needle.length);
|
|
170
|
+
return longest;
|
|
171
|
+
}
|
|
172
|
+
apply(text) {
|
|
173
|
+
if (this.needles.size === 0 || text.length < MIN_REDACTED_LENGTH)
|
|
174
|
+
return { text, hit: false };
|
|
175
|
+
this.sorted ??= [...this.needles].sort((a, b) => b[0].length - a[0].length);
|
|
176
|
+
let out = text;
|
|
177
|
+
let hit = false;
|
|
178
|
+
for (const [needle, key] of this.sorted) {
|
|
179
|
+
if (!out.includes(needle))
|
|
180
|
+
continue;
|
|
181
|
+
out = out.split(needle).join(`[redacted:${key}]`);
|
|
182
|
+
hit = true;
|
|
183
|
+
}
|
|
184
|
+
return { text: out, hit };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const INFRA_SECRETS = /^BASALT_(PG|REDIS|MINIO)_/;
|
|
188
|
+
/** Whether the value of `key` is a secret for this config: generated, asked as a secret, or named like one. */
|
|
189
|
+
export function isSecretKey(key, config) {
|
|
190
|
+
if (SENSITIVE.test(key) || INFRA_SECRETS.test(key))
|
|
191
|
+
return true;
|
|
192
|
+
return declaredSecret(key, config);
|
|
193
|
+
}
|
|
194
|
+
function declaredSecret(key, config) {
|
|
195
|
+
if (!config)
|
|
196
|
+
return false;
|
|
197
|
+
if (config.env.generate && key in config.env.generate)
|
|
198
|
+
return true;
|
|
199
|
+
const question = config.env.ask?.[key];
|
|
200
|
+
return question?.secret === true;
|
|
201
|
+
}
|
|
202
|
+
const URL_NAMED = /_URL$|_DSN$/;
|
|
203
|
+
/** `scheme://host[:port][/]` — no user, password, path, query or fragment: nothing in it can be a secret. */
|
|
204
|
+
const BARE_ORIGIN = /^[a-z][a-z0-9+.-]*:\/\/[^/?#@\s]+\/?$/i;
|
|
205
|
+
/**
|
|
206
|
+
* Whether the whole value of `key` is learnt as a secret. A key that is a
|
|
207
|
+
* secret only by its `_URL`/`_DSN` suffix and holds a bare origin
|
|
208
|
+
* (`APP_URL=https://app.example.com`) is not: that is the app's public
|
|
209
|
+
* address, and redacting it would hide the result's `url` and every health
|
|
210
|
+
* line. A URL with a path, a query or a user is learnt whole (a webhook URL's
|
|
211
|
+
* path is its secret); a URL's password is always learnt on its own.
|
|
212
|
+
*/
|
|
213
|
+
export function isSecretValue(key, value, config) {
|
|
214
|
+
if (!isSecretKey(key, config))
|
|
215
|
+
return false;
|
|
216
|
+
if (!URL_NAMED.test(key) || !BARE_ORIGIN.test(value))
|
|
217
|
+
return true;
|
|
218
|
+
return SENSITIVE.test(key.replace(URL_NAMED, '')) || INFRA_SECRETS.test(key) || declaredSecret(key, config);
|
|
219
|
+
}
|
|
220
|
+
/** The password inside a `scheme://user:password@…` value, as written and decoded. */
|
|
221
|
+
export function urlPasswords(value) {
|
|
222
|
+
const match = /^[a-z][a-z0-9+.-]*:\/\/[^/@:]*:([^/@]+)@/i.exec(value);
|
|
223
|
+
if (!match)
|
|
224
|
+
return [];
|
|
225
|
+
const raw = match[1];
|
|
226
|
+
let decoded = raw;
|
|
227
|
+
try {
|
|
228
|
+
decoded = decodeURIComponent(raw);
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
// Not valid percent-encoding: the raw form is what can appear.
|
|
232
|
+
}
|
|
233
|
+
return [...new Set([raw, decoded])];
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Learns the secrets of key/value pairs (a `.env`, a child's environment).
|
|
237
|
+
* Returns the keys that are too short to be redacted, the first time each is seen.
|
|
238
|
+
*/
|
|
239
|
+
export function learnSecrets(redactor, pairs, config) {
|
|
240
|
+
const short = [];
|
|
241
|
+
const note = (key, value) => { if (redactor.add(key, value) === 'short')
|
|
242
|
+
short.push(key); };
|
|
243
|
+
for (const [key, value] of pairs) {
|
|
244
|
+
if (isSecretValue(key, value, config))
|
|
245
|
+
note(key, value);
|
|
246
|
+
for (const password of urlPasswords(value))
|
|
247
|
+
note(key, password);
|
|
248
|
+
}
|
|
249
|
+
return short;
|
|
250
|
+
}
|
|
251
|
+
/** {@link learnSecrets} over the text of a `.env`. */
|
|
252
|
+
export const learnEnvText = (redactor, text, config) => learnSecrets(redactor, envValues(parseEnv(text)), config);
|
|
253
|
+
// ── the writer (RFC 0008 §5.2) ─────────────────────────────────────────────
|
|
254
|
+
/** A run log takes no more `log` events past this size (RFC 0008 §8.7). */
|
|
255
|
+
export const RUN_LOG_CAP = 64 * 1024 * 1024;
|
|
256
|
+
const CAP_NOTICE = 'run log cap reached: further output lines dropped';
|
|
257
|
+
/**
|
|
258
|
+
* The one place events are serialised: it assigns `seq` and `ts`, redacts
|
|
259
|
+
* and sanitises every string field BEFORE `JSON.stringify` (a secret with `"`
|
|
260
|
+
* or `\` would be escaped in the JSON and missed afterwards; a child's stderr
|
|
261
|
+
* quoted in an error message carries no terminal escape), cuts a `log` line
|
|
262
|
+
* only once it is redacted (a secret across the cut would match neither
|
|
263
|
+
* half), then writes the line to stdout and to the run log. Nothing is
|
|
264
|
+
* written after `done`.
|
|
265
|
+
*/
|
|
266
|
+
export class EventWriter {
|
|
267
|
+
options;
|
|
268
|
+
seq = 0;
|
|
269
|
+
logBytes = 0;
|
|
270
|
+
capped = false;
|
|
271
|
+
log;
|
|
272
|
+
backlog;
|
|
273
|
+
/** Every event written, in order (what `done` and the detaching parent look at). */
|
|
274
|
+
last = {};
|
|
275
|
+
constructor(options) {
|
|
276
|
+
this.options = options;
|
|
277
|
+
if (options.expectRunLog)
|
|
278
|
+
this.backlog = [];
|
|
279
|
+
}
|
|
280
|
+
get runId() {
|
|
281
|
+
return this.options.runId;
|
|
282
|
+
}
|
|
283
|
+
get nextSeq() {
|
|
284
|
+
return this.seq;
|
|
285
|
+
}
|
|
286
|
+
emit(body) {
|
|
287
|
+
this.write(body, false);
|
|
288
|
+
}
|
|
289
|
+
write(body, notice) {
|
|
290
|
+
// `done` is the last line: a late answer (a finding) or a stray write after it is dropped.
|
|
291
|
+
if (this.last.done)
|
|
292
|
+
return;
|
|
293
|
+
let line;
|
|
294
|
+
let event;
|
|
295
|
+
const capApplies = Boolean(this.log || this.backlog || this.options.stdoutIsRunLog);
|
|
296
|
+
try {
|
|
297
|
+
const { redactor } = this.options;
|
|
298
|
+
// A line cut by the splitter: its tail may hold the first half of a secret whose rest was dropped.
|
|
299
|
+
const source = body.type === 'log' && body.truncated ? { ...body, text: body.text.slice(0, Math.max(0, body.text.length - Math.max(0, redactor.longest - 1))) } : body;
|
|
300
|
+
const { value, hit } = redactDeep(source, redactor);
|
|
301
|
+
const redacted = value;
|
|
302
|
+
if (redacted['type'] === 'log') {
|
|
303
|
+
redacted['redacted'] = hit || redacted['redacted'] === true;
|
|
304
|
+
const cut = truncate(redacted['text']);
|
|
305
|
+
if (cut.truncated) {
|
|
306
|
+
redacted['text'] = cut.text;
|
|
307
|
+
redacted['truncated'] = true;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
event = {
|
|
311
|
+
v: 1,
|
|
312
|
+
ts: this.options.now().toISOString(),
|
|
313
|
+
seq: this.seq,
|
|
314
|
+
...(this.options.runId ? { runId: this.options.runId } : {}),
|
|
315
|
+
...(hit ? { redacted: true } : {}),
|
|
316
|
+
...redacted,
|
|
317
|
+
};
|
|
318
|
+
line = `${JSON.stringify(event)}\n`;
|
|
319
|
+
}
|
|
320
|
+
catch (error) {
|
|
321
|
+
this.options.onFailure?.(`basalt-deploy: an event could not be written: ${error.message}`);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
const bytes = Buffer.byteLength(line);
|
|
325
|
+
if (capApplies && body.type === 'log' && !notice) {
|
|
326
|
+
if (this.capped)
|
|
327
|
+
return;
|
|
328
|
+
if (this.logBytes + bytes > (this.options.cap ?? RUN_LOG_CAP)) {
|
|
329
|
+
// Dropped lines take no seq: the stream stays contiguous (RFC 0008 §4.5).
|
|
330
|
+
this.capped = true;
|
|
331
|
+
this.write({ type: 'log', stream: 'tool', level: 'warn', text: CAP_NOTICE, redacted: false }, true);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
this.seq++;
|
|
336
|
+
if (capApplies)
|
|
337
|
+
this.logBytes += bytes;
|
|
338
|
+
if (event.type === 'error')
|
|
339
|
+
this.last.error = event;
|
|
340
|
+
if (event.type === 'result')
|
|
341
|
+
this.last.result = event;
|
|
342
|
+
if (event.type === 'done')
|
|
343
|
+
this.last.done = event;
|
|
344
|
+
this.options.stdout(line);
|
|
345
|
+
if (this.log)
|
|
346
|
+
this.appendLog(line);
|
|
347
|
+
else
|
|
348
|
+
this.backlog?.push(line);
|
|
349
|
+
}
|
|
350
|
+
/** Writes what came before, then every event, to the run log. */
|
|
351
|
+
attachLog(log) {
|
|
352
|
+
this.log = log;
|
|
353
|
+
for (const line of this.backlog ?? [])
|
|
354
|
+
this.appendLog(line);
|
|
355
|
+
this.backlog = undefined;
|
|
356
|
+
}
|
|
357
|
+
/** No run log for this command after all. */
|
|
358
|
+
dropBacklog() {
|
|
359
|
+
this.backlog = undefined;
|
|
360
|
+
if (!this.log)
|
|
361
|
+
this.logBytes = 0;
|
|
362
|
+
}
|
|
363
|
+
close() {
|
|
364
|
+
try {
|
|
365
|
+
this.log?.close();
|
|
366
|
+
}
|
|
367
|
+
catch {
|
|
368
|
+
// Closing a log that is gone changes nothing.
|
|
369
|
+
}
|
|
370
|
+
this.log = undefined;
|
|
371
|
+
}
|
|
372
|
+
appendLog(line) {
|
|
373
|
+
try {
|
|
374
|
+
this.log?.append(line);
|
|
375
|
+
}
|
|
376
|
+
catch (error) {
|
|
377
|
+
// The run log failing (disk full) must not stop the deploy; stdout still has every event.
|
|
378
|
+
this.log = undefined;
|
|
379
|
+
this.options.onFailure?.(`basalt-deploy: the run log could not be written: ${error.message}`);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
/** Every string redacted, and stripped of escapes and control characters (newlines and tabs kept). */
|
|
384
|
+
function redactDeep(value, redactor) {
|
|
385
|
+
if (typeof value === 'string') {
|
|
386
|
+
const { text, hit } = redactor.apply(value);
|
|
387
|
+
return { value: sanitize(text, true), hit };
|
|
388
|
+
}
|
|
389
|
+
if (Array.isArray(value)) {
|
|
390
|
+
let hit = false;
|
|
391
|
+
const out = value.map((item) => {
|
|
392
|
+
const r = redactDeep(item, redactor);
|
|
393
|
+
hit ||= r.hit;
|
|
394
|
+
return r.value;
|
|
395
|
+
});
|
|
396
|
+
return { value: out, hit };
|
|
397
|
+
}
|
|
398
|
+
if (value !== null && typeof value === 'object') {
|
|
399
|
+
let hit = false;
|
|
400
|
+
const out = {};
|
|
401
|
+
for (const [key, item] of Object.entries(value)) {
|
|
402
|
+
const r = redactDeep(item, redactor);
|
|
403
|
+
hit ||= r.hit;
|
|
404
|
+
out[key] = r.value;
|
|
405
|
+
}
|
|
406
|
+
return { value: out, hit };
|
|
407
|
+
}
|
|
408
|
+
return { value, hit: false };
|
|
409
|
+
}
|
|
410
|
+
export const MAX_HELD_BYTES = 1024 * 1024;
|
|
411
|
+
export function foreignSink(reporter, step) {
|
|
412
|
+
const lines = new LineSplitter();
|
|
413
|
+
let held = [];
|
|
414
|
+
let heldBytes = 0;
|
|
415
|
+
let dropped = 0;
|
|
416
|
+
const take = (line) => {
|
|
417
|
+
if (!held) {
|
|
418
|
+
emitLine(reporter, 'foreign', 'warn', line, step());
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
heldBytes += line.text.length;
|
|
422
|
+
if (heldBytes > MAX_HELD_BYTES) {
|
|
423
|
+
dropped++;
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
held.push({ line, step: step() });
|
|
427
|
+
};
|
|
428
|
+
const release = () => {
|
|
429
|
+
if (!held)
|
|
430
|
+
return;
|
|
431
|
+
const lines = held;
|
|
432
|
+
held = undefined;
|
|
433
|
+
for (const { line, step: at } of lines)
|
|
434
|
+
emitLine(reporter, 'foreign', 'warn', line, at);
|
|
435
|
+
if (dropped > 0)
|
|
436
|
+
reporter.emit({ type: 'log', stream: 'tool', level: 'warn', text: `${dropped} line(s) the config printed while it loaded were dropped (more than ${MAX_HELD_BYTES / 1024} KiB)`, redacted: false });
|
|
437
|
+
};
|
|
438
|
+
return {
|
|
439
|
+
write(text) { for (const line of lines.push(text))
|
|
440
|
+
take(line); },
|
|
441
|
+
release,
|
|
442
|
+
end() {
|
|
443
|
+
for (const line of lines.end())
|
|
444
|
+
take(line);
|
|
445
|
+
release();
|
|
446
|
+
},
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
/** A child's output as `log{stream:'child'}` events: stdout `info`, stderr `warn`. */
|
|
450
|
+
export function childSink(reporter, source, step) {
|
|
451
|
+
const splitters = { stdout: new LineSplitter(), stderr: new LineSplitter() };
|
|
452
|
+
const level = (stream) => (stream === 'stdout' ? 'info' : 'warn');
|
|
453
|
+
return {
|
|
454
|
+
push(text, stream) {
|
|
455
|
+
for (const line of splitters[stream].push(text))
|
|
456
|
+
emitLine(reporter, 'child', level(stream), line, step, source);
|
|
457
|
+
},
|
|
458
|
+
end() {
|
|
459
|
+
for (const stream of ['stdout', 'stderr']) {
|
|
460
|
+
for (const line of splitters[stream].end())
|
|
461
|
+
emitLine(reporter, 'child', level(stream), line, step, source);
|
|
462
|
+
}
|
|
463
|
+
},
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
/** One line from outside, sanitised; the writer redacts it, then cuts it at {@link MAX_LINE_BYTES}. */
|
|
467
|
+
function emitLine(reporter, stream, level, line, step, source) {
|
|
468
|
+
const text = sanitize(line.text);
|
|
469
|
+
if (text.trim() === '')
|
|
470
|
+
return;
|
|
471
|
+
reporter.emit({
|
|
472
|
+
type: 'log', stream, level, text, redacted: false,
|
|
473
|
+
...(step ? { step } : {}),
|
|
474
|
+
...(source ? { source } : {}),
|
|
475
|
+
...(line.cut ? { truncated: true } : {}),
|
|
476
|
+
});
|
|
477
|
+
}
|