Files
sencho/backend/src/services/preflight/effectiveModel.ts
T
Anso 77f1611971 feat: Compose Network Inspector and exposure intent guard (#1360)
* feat: add Compose Network Inspector facts engine

Render a stack's authored effective model and pair it with the live
Docker snapshot to derive per-stack networking facts: project networks
with external and internal flags, service-to-network membership and
aliases, published ports with host-binding scope, network_mode, and
extra_hosts, plus runtime drift (runtime-only attachments, foreign
networks, and declared-but-unused or missing networks).

Extend the effective-model parser with service network membership,
extra_hosts, and label keys (key names only, never values), and add a
key-space normalized network model with adapters from both the rendered
model and the raw declared compose so the Inspector and drift share one
comparison. Expose GET /api/stacks/:stackName/networking: advisory and
read-only, it renders the authored model only and never returns or logs
raw stderr, env values, or label values.

* feat: store and edit per-stack and per-service exposure intent

Add a stack_exposure_intent table (intent values constrained by a CHECK,
unique per node, stack, and service) with DAO methods to read, upsert,
clear one row, and clear all rows for a stack. The classification is
stored independently of the generated networking facts so a later
mismatch stays detectable; service rows are kept separately from the
stack-level row (service '').

Expose GET and PUT /api/stacks/:stackName/exposure: GET requires read
access, PUT requires edit access and validates the intent against the
allowed set. Sending intent null clears that row, returning the scope to
unset so a service inherits the stack intent again. Intent rows are
cleared when the stack is deleted and when the owning node is removed,
so a later same-named stack never picks up stale classification.

* feat: add exposure-aware Compose Doctor findings

Feed the Compose Doctor's effective-model context with the stored
exposure intent (resolved into a stack-level value plus per-service
overrides) and the dossier's documented access-URL ports, read fail-soft
so a metadata read error skips these checks rather than failing the
preflight. Add five deterministic findings on top of that context:

- a service classified internal or same-node that publishes a host port
  (same-node tolerates a loopback bind),
- a sensitive database or admin image published on all interfaces,
- a port-publishing stack with no exposure intent set,
- a published port not reflected in the documented access URLs,
- reverse-proxy labels with no documented URL or reverse-proxy intent.

The rules stay pure functions over the preflight context; the registry
completeness test pins the new rule set.

* feat: detect compose network drift in the drift ledger

Extend the spatial drift engine with two network-level findings: a
running container attached to a stack-owned or foreign network that
compose does not declare (one finding per service), and a declared
network that no running service uses or that is absent from the runtime
(one stack-level finding, every network named by its resolved runtime
name). The comparison reuses the same helper the Network Inspector uses,
so the two surfaces never disagree.

Network drift runs only when the stack has running containers and the
runtime is reachable, preserving the existing missing-runtime,
parse-error, and unreachable behavior. The findings persist through the
existing drift ledger and surface on the Drift tab, which now labels the
two new kinds.

* feat: link a Docker network back to its owning stack

Add a cross-component open-stack event and make the owning-stack badge on
a managed network in Resources a link: clicking it loads that stack on
its node and opens the editor, reusing the existing fleet navigation. A
latest-ref keeps the window listener current without re-subscribing each
render. Image and volume badges are unchanged; only a managed network
opts in via the new optional handler.

* feat: add the Networking tab to the stack detail panel

Add a capability-gated Networking tab that reads the per-stack networking
facts and exposure intent. It shows the project networks (with external,
internal, and created-by-stack flags), per-service network membership and
aliases, published ports with their host-binding scope, network_mode and
extra_hosts, and runtime drift, degrading to the declared model when the
runtime is unavailable. Users can classify the stack and each service
(internal, LAN, reverse proxy, public, and so on) or clear a row to
inherit; the controls are read-only when the user cannot edit, and a
broken exposure response never tears down the facts view.

A new compose-networking capability is added to both registries so older
nodes hide the tab, and the tab cross-links to the Doctor for the deploy
and security findings.

* docs: document the Compose Networking tab

Add a feature page covering the Networking tab: the network facts,
published ports and host bindings, the exposure-intent classification
and inheritance, the exposure-aware Doctor findings, runtime drift, and
a troubleshooting section. Register it in the docs navigation next to
Compose Doctor.

* feat: add a redacted network summary to the Stack Dossier export

Append a network exposure section to the dossier Markdown: the stack and
per-service exposure intents, the networks with their external and
internal flags, and each service's published ports with their binding
scope. It carries only names, intents, port numbers, and scope, never an
env value or a label value.

The summary is fetched only when the user exports (copy or download), so
opening the panel costs nothing, and it degrades to omitting the section
when the data is unavailable. The whole-fleet dossier export collects the
same summary per stack, rethrowing the unauthorized sentinel like the
sibling loaders.

* feat: add a Fleet networking filter for exposure and drift

Add a per-node networking summary that classifies a node's stacks as
exposed (a host port published beyond loopback), unknown-exposure
(publishes ports with no exposure intent set), or network-drift. It
reads each stack's compose with the light dependency parser and one
Docker snapshot, so it stays cheap across a node's full stack set, and
it skips drift when the runtime is unreachable rather than inventing it.

Serve it node-locally at GET /api/networking/summary, and aggregate it
fleet-wide at GET /api/fleet/networking-summary: the hub computes its own
summary in-process and reaches each remote through its node-local route,
degrading an unreachable or older node to a skip. Because the aggregate
lives under the proxy-exempt /api/fleet prefix it is never wrongly
proxied. The Fleet overview gains a networking filter chip backed by that
aggregate, fetched fail-soft and detached so it never gates the grid.

* fix: spin the Networking refresh button while it reloads

The refresh button silently refetched the same data, so a click gave no
feedback. Track a refreshing state and spin the icon while the load is in
flight, disabling the button, matching the Compose Doctor preflight
button.

* fix: apply effective per-service exposure intent to unclassified checks

The "unclassified exposure" decisions only consulted the stack-level intent
row, so a service classified directly (with no stack row) was still reported
as unclassified, and a service explicitly marked unknown over a classified
stack was missed.

Both the exposure-unclassified preflight rule and the networking summary's
unknown-exposure bucket now resolve the effective intent per publishing
service (service row overrides stack row), matching the precedence already
used by the exposure-internal-published rule.

* fix: resolve drift network names via the compose top-level name

When a compose file sets a top-level name:, Docker prefixes resource names
with that project name instead of the stack directory. The light dependency
parser dropped name:, so network-drift normalization compared runtime
networks against directory-prefixed names and reported false
network-undeclared / network-missing findings.

Carry the parsed project name through DeclaredCompose and use it when
normalizing declared networks for drift, while still filtering containers by
the stack directory.
2026-06-12 02:15:11 -04:00

253 lines
9.2 KiB
TypeScript

/**
* Parser for the output of `docker compose config` (the fully-resolved
* effective model). It extracts only the STRUCTURAL facts the preflight rules
* need; it never retains an environment VALUE. Service environment is read for
* its key NAMES only (to detect PUID/PGID style directives), and render errors
* are handled by the caller, not here.
*/
/** A host-published port range declared by a service (start==end for one port). */
export interface EffPortSpec {
startPort: number;
endPort: number;
/** '' / '0.0.0.0' / '::' means all interfaces. */
hostIp: string;
protocol: string;
}
export interface EffBind {
/** Absolute source path (compose config resolves relative binds to absolute). */
source: string;
target: string;
}
/** A service's membership in one top-level network, keyed by the network KEY
* (not the resolved docker name) so it lines up with the `networks` map and
* with the authored `DeclaredService.networks`. */
export interface EffServiceNetwork {
key: string;
aliases: string[];
}
export interface EffService {
name: string;
image?: string;
ports: EffPortSpec[];
binds: EffBind[];
namedVolumes: string[];
privileged: boolean;
networkMode?: string;
restart?: string;
hasHealthcheck: boolean;
/** Raw deploy block (read for key presence only, never values; undefined = none). */
deploy?: Record<string, unknown>;
containerName?: string;
user?: string;
/** Environment KEY names only. Values are never extracted. */
envKeys: string[];
/** Network membership by network key, with any aliases. */
networks: EffServiceNetwork[];
/** `extra_hosts` entries as `host:value` strings (host names / static IPs, never secrets). */
extraHosts: string[];
/** Label KEY names only. Values are never extracted (a label value can carry a secret). */
labelKeys: string[];
}
export interface EffResource {
/** Resolved docker name (compose config fills this in). */
name: string;
external: boolean;
/** Top-level `internal: true` (no outbound/host connectivity for the network). */
internal: boolean;
}
export interface EffectiveModel {
projectName: string;
services: EffService[];
networks: Record<string, EffResource>;
volumes: Record<string, EffResource>;
}
function str(v: unknown): string | undefined {
if (typeof v === 'string') return v;
if (typeof v === 'number') return String(v);
return undefined;
}
/** Parse a `start[-end]` published-port string into a clamped range, or null if invalid. */
function parsePortRange(raw: string): { startPort: number; endPort: number } | null {
const [a, b] = raw.split('-');
const start = parseInt(a, 10);
if (!Number.isFinite(start) || start <= 0) return null;
const end = b !== undefined ? parseInt(b, 10) : start;
return { startPort: start, endPort: Number.isFinite(end) && end >= start ? end : start };
}
/** Parse one rendered `ports:` entry (long object form, with a short-string fallback). */
function parsePortSpec(entry: unknown): EffPortSpec | null {
if (entry && typeof entry === 'object') {
const o = entry as Record<string, unknown>;
const publishedRaw = str(o.published);
if (publishedRaw === undefined || publishedRaw === '') return null; // container-only
const range = parsePortRange(publishedRaw);
if (!range) return null;
return { ...range, hostIp: str(o.host_ip) ?? '', protocol: str(o.protocol) ?? 'tcp' };
}
const short = str(entry);
if (short === undefined) return null;
const [spec, proto] = short.split('/');
const parts = spec.split(':');
let hostIp = '';
let hostPart: string | undefined;
if (parts.length >= 3) { hostIp = parts[0]; hostPart = parts[1]; }
else if (parts.length === 2) { hostPart = parts[0]; }
else return null; // container-only EXPOSE
const range = parsePortRange(hostPart ?? '');
if (!range) return null;
return { ...range, hostIp, protocol: proto || 'tcp' };
}
/** Split a service `volumes:` list into bind mounts and named-volume sources. */
function parseVolumes(volumes: unknown): { binds: EffBind[]; named: string[] } {
const binds: EffBind[] = [];
const named: string[] = [];
if (!Array.isArray(volumes)) return { binds, named };
for (const v of volumes) {
if (v && typeof v === 'object') {
const o = v as Record<string, unknown>;
const type = str(o.type);
const source = str(o.source);
const target = str(o.target) ?? '';
if (type === 'bind' && source) binds.push({ source, target });
else if (type === 'volume' && source) named.push(source);
continue;
}
const s = str(v);
if (!s) continue;
const parts = s.split(':');
if (parts.length < 2) continue; // anonymous volume, nothing to check
const source = parts[0];
const target = parts[1];
const isPath = source.startsWith('/') || source.startsWith('.') || source.startsWith('~') || /^[a-zA-Z]:[\\/]/.test(source);
if (isPath) binds.push({ source, target });
else named.push(source);
}
return { binds, named };
}
/** Environment KEY names only. Never returns a value. */
function envKeysOf(env: unknown): string[] {
if (Array.isArray(env)) {
return env
.map(e => str(e))
.filter((s): s is string => s !== undefined)
.map(s => s.split('=')[0])
.filter(Boolean);
}
if (env && typeof env === 'object') return Object.keys(env as Record<string, unknown>);
return [];
}
/** Label KEY names only. A label VALUE can carry a secret, so it is never read. */
function labelKeysOf(labels: unknown): string[] {
if (Array.isArray(labels)) {
return labels
.map(e => str(e))
.filter((s): s is string => s !== undefined)
.map(s => s.split('=')[0])
.filter(Boolean);
}
if (labels && typeof labels === 'object') return Object.keys(labels as Record<string, unknown>);
return [];
}
/** Service network membership (list or map form), keyed by network key, with aliases. */
function parseServiceNetworks(networks: unknown): EffServiceNetwork[] {
if (Array.isArray(networks)) {
return networks
.map(n => str(n))
.filter((s): s is string => s !== undefined)
.map(key => ({ key, aliases: [] as string[] }));
}
if (networks && typeof networks === 'object') {
return Object.entries(networks as Record<string, unknown>).map(([key, cfg]) => {
const aliasesRaw = (cfg && typeof cfg === 'object') ? (cfg as Record<string, unknown>).aliases : undefined;
const aliases = Array.isArray(aliasesRaw)
? aliasesRaw.map(a => str(a)).filter((a): a is string => a !== undefined)
: [];
return { key, aliases };
});
}
return [];
}
/** `extra_hosts` (list `host:ip` or map `{host: ip}`) → `host:value` strings. Infra facts, not secrets. */
function parseExtraHosts(extraHosts: unknown): string[] {
if (Array.isArray(extraHosts)) {
return extraHosts.map(e => str(e)).filter((s): s is string => s !== undefined);
}
if (extraHosts && typeof extraHosts === 'object') {
return Object.entries(extraHosts as Record<string, unknown>).map(([host, ip]) => `${host}:${str(ip) ?? ''}`);
}
return [];
}
function parseResources(value: unknown): Record<string, EffResource> {
const out: Record<string, EffResource> = {};
if (value && typeof value === 'object' && !Array.isArray(value)) {
for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {
const o = (entry ?? {}) as Record<string, unknown>;
out[key] = { name: str(o.name) ?? key, external: o.external === true, internal: o.internal === true };
}
}
return out;
}
/**
* Build an EffectiveModel from the parsed JSON of `docker compose config
* --format json`. Tolerant of missing fields; an empty/garbage input yields an
* empty model rather than throwing.
*/
export function parseEffectiveModel(parsed: unknown, fallbackProjectName: string): EffectiveModel {
const root = (parsed ?? {}) as Record<string, unknown>;
const rawServices = (root.services && typeof root.services === 'object') ? root.services as Record<string, unknown> : {};
const services: EffService[] = [];
for (const [name, raw] of Object.entries(rawServices)) {
const svc = (raw ?? {}) as Record<string, unknown>;
const ports = Array.isArray(svc.ports)
? svc.ports.map(parsePortSpec).filter((p): p is EffPortSpec => p !== null)
: [];
const { binds, named } = parseVolumes(svc.volumes);
const healthcheck = svc.healthcheck;
const hasHealthcheck = !!healthcheck
&& typeof healthcheck === 'object'
&& (healthcheck as Record<string, unknown>).disable !== true;
services.push({
name,
image: str(svc.image),
ports,
binds,
namedVolumes: named,
privileged: svc.privileged === true,
networkMode: str(svc.network_mode),
restart: str(svc.restart),
hasHealthcheck,
deploy: (svc.deploy && typeof svc.deploy === 'object') ? svc.deploy as Record<string, unknown> : undefined,
containerName: str(svc.container_name),
user: str(svc.user),
envKeys: envKeysOf(svc.environment),
networks: parseServiceNetworks(svc.networks),
extraHosts: parseExtraHosts(svc.extra_hosts),
labelKeys: labelKeysOf(svc.labels),
});
}
return {
projectName: str(root.name) ?? fallbackProjectName,
services,
networks: parseResources(root.networks),
volumes: parseResources(root.volumes),
};
}