mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-10 18:56:53 +00:00
feat: add Compose Doctor preflight checks for stacks (#1348)
* feat: add Compose Doctor preflight checks for stacks Add an on-demand, advisory preflight that renders a stack's effective Compose model with `docker compose config` and runs a registry of deterministic checks before deploy, surfacing findings grouped by severity (blocker, high, warning, info) with a remediation for each. Findings cover unset env vars, host-port conflicts on the node, broad 0.0.0.0 exposure, missing bind-mount paths, a mounted Docker socket, privileged and host networking, moving image tags, missing restart policy and healthcheck, Swarm-only deploy fields, missing external networks or volumes, and container_name collisions. The report is node-scoped and stored as the last run per stack, and the route auto-proxies to the active node so a remote stack is checked on the node that owns it. A new Doctor tab on the stack detail panel runs preflight and shows the grouped findings, with a severity dot on the tab when the last run has blocker or high findings. The tab is gated on a compose-doctor capability so older nodes hide it. No environment value is ever stored, returned, or logged: only env key names and structural facts are read, and render failures surface a generic message or the missing required-variable names, never raw stderr. * fix: scroll the stack tab strip when its tabs overflow Adding the Doctor tab can push the per-stack Anatomy tab strip past the panel width on narrower layouts. Make the tab row scroll horizontally with subtle edge fades that appear only while there is more to scroll in that direction, so a panel wide enough to show every tab is unchanged. * fix: add clickable arrows and wheel scroll to the stack tab strip Hiding the scrollbar left mouse users with no way to scroll the overflowing tab row: a vertical wheel does not move a horizontal overflow and native rows do not drag-scroll. Replace the passive edge fades with clickable chevron arrows shown only when the row overflows that edge, and translate a vertical wheel over the row into horizontal scroll. * fix: inline the path-injection barrier in renderConfig CodeQL's path-injection check does not credit the wrapped isPathWithinBase helper as a sanitizer, so move the containment check inline at the spawn cwd sink, matching the canonical barrier used elsewhere in the codebase. Behavior is unchanged: the resolved stack directory must be contained in the compose base and may not be the base itself. * fix: hoist the compose-config spawn into the path-barrier scope The earlier inline barrier sat in a different scope than the spawn cwd sink (separated by the Promise-executor closure) and used a compound guard, so CodeQL did not credit it. Use the exact canonical startsWith barrier and hoist the spawn into the same scope as the check. Behavior is unchanged: the executor runs synchronously in the same tick as the spawn, so handlers still attach before any event can fire.
This commit is contained in:
@@ -674,4 +674,78 @@ export class ComposeService {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the fully-resolved effective Compose model via `docker compose
|
||||
* config --format json`. This is the AUTHORED model: it does NOT splice in
|
||||
* the Sencho Mesh override, so it stays read-only (the override is
|
||||
* write-generated) and reflects what the user actually edits. The override
|
||||
* would also add the managed `sencho_mesh` external network and per-service
|
||||
* mesh attachments, which would make preflight emit a false "external network
|
||||
* not found" finding, so rendering the authored model is both safer and more
|
||||
* accurate here.
|
||||
* Captures stderr (where Compose reports unset variables) and never rejects
|
||||
* on a non-zero exit, so the Compose Doctor can turn a failed render into a
|
||||
* finding rather than an exception. Bounded by a timeout and an output cap.
|
||||
* Rejects only when the docker binary cannot be spawned.
|
||||
*/
|
||||
public renderConfig(
|
||||
stackName: string,
|
||||
): Promise<{ rendered: string | null; stderr: string; code: number | null; timedOut: boolean }> {
|
||||
if (!isValidStackName(stackName)) {
|
||||
return Promise.reject(new Error('Invalid stack path'));
|
||||
}
|
||||
// Canonical inline js/path-injection barrier, kept in the same scope as the
|
||||
// spawn cwd sink below. CodeQL credits neither the wrapped isPathWithinBase
|
||||
// helper nor a barrier separated from the sink by the Promise-executor
|
||||
// closure, so the spawn is hoisted out of the executor. startsWith already
|
||||
// rejects the base dir itself, since base does not start with base + sep.
|
||||
const baseResolved = path.resolve(this.baseDir);
|
||||
const stackDir = path.resolve(baseResolved, stackName);
|
||||
if (!stackDir.startsWith(baseResolved + path.sep)) {
|
||||
return Promise.reject(new Error('Invalid stack path'));
|
||||
}
|
||||
const child = spawn('docker', ['compose', 'config', '--format', 'json'], {
|
||||
cwd: stackDir,
|
||||
env: {
|
||||
...process.env,
|
||||
PATH: process.env.PATH || '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
|
||||
},
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
const MAX_OUTPUT = 5 * 1024 * 1024; // 5 MiB cap on each stream
|
||||
const TIMEOUT_MS = 20_000;
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
let timedOut = false;
|
||||
let capped = false;
|
||||
let settled = false;
|
||||
const timer = setTimeout(() => { timedOut = true; child.kill('SIGKILL'); }, TIMEOUT_MS);
|
||||
const finish = (result: { rendered: string | null; stderr: string; code: number | null; timedOut: boolean }) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
resolve(result);
|
||||
};
|
||||
child.stdout.on('data', (data: Buffer) => {
|
||||
stdout += data.toString();
|
||||
if (stdout.length > MAX_OUTPUT && !capped) { capped = true; child.kill('SIGKILL'); }
|
||||
});
|
||||
child.stderr.on('data', (data: Buffer) => {
|
||||
if (stderr.length < MAX_OUTPUT) stderr += data.toString();
|
||||
});
|
||||
child.on('error', (err: NodeJS.ErrnoException) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
reject(new Error(describeSpawnError(err, { command: 'docker compose' }).message));
|
||||
});
|
||||
child.on('close', (code) => {
|
||||
if (timedOut) finish({ rendered: null, stderr: stderr.trim() || 'docker compose config timed out', code, timedOut: true });
|
||||
else if (capped) finish({ rendered: null, stderr: 'Rendered model exceeded the size limit', code, timedOut: false });
|
||||
else if (code === 0) finish({ rendered: stdout, stderr, code, timedOut: false });
|
||||
else finish({ rendered: null, stderr: stderr.trim() || `docker compose config failed with code ${code}`, code, timedOut: false });
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user