cast/src/smoke.ts

85 lines
3.2 KiB
TypeScript
Raw Normal View History

feat: cast — the Coolify executor, extracted from the infra state repo Public tool, private state. cast holds no hostnames, no bindings, no secrets: it joins a product repo's .infra/ manifest with a state directory you point it at, and makes Coolify match. Extracted from heavy-duty/infra, which was half tool and half state — the inconsistency that made it impossible to say whether "infra" named a CLI or a runbook. rig builds the boxes; cast fills them; infra is what they are filled with. Two changes were required to make it genuinely stateless and publishable: - The implicit cwd contract (environments.yaml / secrets/ / .coolify.env resolved against the working directory, silently reading the wrong file from the wrong place) is now an explicit --state <dir> / $CAST_STATE. - BANNED_IN_PROD — a hardcoded list of one product's ALLOW_* flags, the only product knowledge in the executor — becomes the generic, operator- owned environments.<env>.forbidden_var_patterns. The guard now lives in private state, so a product-side change cannot lower its own guard, and it is a pattern rather than a list, so it catches unforeseen siblings. Age identities resolve as $CAST_AGE_KEY_FILE_<ENV> then ~/.config/cast/age-<env>.key — which is the entire attended-vs-unattended apply mechanism, with no environment names known to the tool. Instance identity (org names, the GitHub App name, founder domains) is out of the fixtures and out of register-github-app.sh, which took APP_NAME and ORG as arguments rather than baking them in. 69 tests green; bin/cast + curl installer mirror rig's shape. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 12:25:44 +00:00
import type { CoolifyClient } from "./coolify.js";
fix: never write an env var whose name Coolify injects itself (#50) Coolify injects SOURCE_COMMIT and the COOLIFY_* family into an application's runtime environment itself, and SKIPS its own injection of a name the resource already carries a var of (ApplicationDeploymentJob.php v4.1.2, line 2994 — `->where('key', 'SOURCE_COMMIT')->isEmpty()`). A resource-level var of that name therefore SUPPRESSES the platform's value. An empty one suppresses it just as completely: presence, not value. And it fails green — the deploy succeeds, health checks pass, and the only symptom is /version reporting "unknown", the endpoint a production cutover is gated on (D-266). The rule is now a property of cast, not of one code path. A new src/reserved.ts owns it, and every place cast touches an env var honors it: - resolve — every manifest read (desiredFromManifest, requiredSecrets, manifestResources) refuses a template declaring a reserved name, before any write. So apply, diff, capture and inventory all refuse identically. - draft — a reserved name read off a live box gets its own provenance, `suppressed`: out of the template, out of the age store, its live value read into no artifact, and named in UNCAPTURED.md with the consequence. - diff — promoted out of the remove-candidate orphan list ("apply never removes these; read them by eye") and printed as a FINDING with its consequence. Not clean. apply still never deletes: cast reports, the human removes it. - capture (classify) and cli (syncEnv) carry the same assertion at the file and at the wire — unreachable through the CLI today, and kept because the invariant is "cast never writes one", not "the CLI happens to check first". - smoke writes an env var too; its probe names are asserted outside the space. The rule lives in cast's code, NOT beside forbidden_var_patterns in private state: that one is policy an environment may set for itself, this one is a fact about Coolify, true on every box — nothing a manifest change could lower. 19 tests in test/reserved.test.ts, one per path. Closes #50.
2026-07-14 22:29:23 +00:00
import { assertNoReservedEnvNames, reservedHits } from "./reserved.js";
// smoke writes env vars onto a REAL application — the one thing in cast that
// mutates a live resource purely to learn something. So the reserved-name rule
// binds it too, and these are the two names it may pick from.
//
// Exported, and asserted below, because the rule is about what cast writes, not
// about what one function happens to have been written to write today: an
// operator renaming a probe (`COOLIFY_SMOKE_PROBE` reads like the natural name)
// would otherwise set the very trap this rule exists to prevent, on a live app,
// and the smoke would pass while suppressing the app's SOURCE_COMMIT injection
// for as long as the probe lived — and the delete at the end restores nothing:
// Coolify only injects at deploy time.
export const SMOKE_KEEP_KEY = "INFRA_SMOKE_KEEP";
export const SMOKE_PROBE_KEY = "INFRA_SMOKE_PROBE";
feat: cast — the Coolify executor, extracted from the infra state repo Public tool, private state. cast holds no hostnames, no bindings, no secrets: it joins a product repo's .infra/ manifest with a state directory you point it at, and makes Coolify match. Extracted from heavy-duty/infra, which was half tool and half state — the inconsistency that made it impossible to say whether "infra" named a CLI or a runbook. rig builds the boxes; cast fills them; infra is what they are filled with. Two changes were required to make it genuinely stateless and publishable: - The implicit cwd contract (environments.yaml / secrets/ / .coolify.env resolved against the working directory, silently reading the wrong file from the wrong place) is now an explicit --state <dir> / $CAST_STATE. - BANNED_IN_PROD — a hardcoded list of one product's ALLOW_* flags, the only product knowledge in the executor — becomes the generic, operator- owned environments.<env>.forbidden_var_patterns. The guard now lives in private state, so a product-side change cannot lower its own guard, and it is a pattern rather than a list, so it catches unforeseen siblings. Age identities resolve as $CAST_AGE_KEY_FILE_<ENV> then ~/.config/cast/age-<env>.key — which is the entire attended-vs-unattended apply mechanism, with no environment names known to the tool. Instance identity (org names, the GitHub App name, founder domains) is out of the fixtures and out of register-github-app.sh, which took APP_NAME and ORG as arguments rather than baking them in. 69 tests green; bin/cast + curl installer mirror rig's shape. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 12:25:44 +00:00
export async function smoke(
client: CoolifyClient,
targetAppUuid: string,
): Promise<void> {
fix: never write an env var whose name Coolify injects itself (#50) Coolify injects SOURCE_COMMIT and the COOLIFY_* family into an application's runtime environment itself, and SKIPS its own injection of a name the resource already carries a var of (ApplicationDeploymentJob.php v4.1.2, line 2994 — `->where('key', 'SOURCE_COMMIT')->isEmpty()`). A resource-level var of that name therefore SUPPRESSES the platform's value. An empty one suppresses it just as completely: presence, not value. And it fails green — the deploy succeeds, health checks pass, and the only symptom is /version reporting "unknown", the endpoint a production cutover is gated on (D-266). The rule is now a property of cast, not of one code path. A new src/reserved.ts owns it, and every place cast touches an env var honors it: - resolve — every manifest read (desiredFromManifest, requiredSecrets, manifestResources) refuses a template declaring a reserved name, before any write. So apply, diff, capture and inventory all refuse identically. - draft — a reserved name read off a live box gets its own provenance, `suppressed`: out of the template, out of the age store, its live value read into no artifact, and named in UNCAPTURED.md with the consequence. - diff — promoted out of the remove-candidate orphan list ("apply never removes these; read them by eye") and printed as a FINDING with its consequence. Not clean. apply still never deletes: cast reports, the human removes it. - capture (classify) and cli (syncEnv) carry the same assertion at the file and at the wire — unreachable through the CLI today, and kept because the invariant is "cast never writes one", not "the CLI happens to check first". - smoke writes an env var too; its probe names are asserted outside the space. The rule lives in cast's code, NOT beside forbidden_var_patterns in private state: that one is policy an environment may set for itself, this one is a fact about Coolify, true on every box — nothing a manifest change could lower. 19 tests in test/reserved.test.ts, one per path. Closes #50.
2026-07-14 22:29:23 +00:00
const KEEP_KEY = SMOKE_KEEP_KEY;
const PROBE_KEY = SMOKE_PROBE_KEY;
assertNoReservedEnvNames(
reservedHits(`smoke probe on ${targetAppUuid}`, [KEEP_KEY, PROBE_KEY]),
);
feat: cast — the Coolify executor, extracted from the infra state repo Public tool, private state. cast holds no hostnames, no bindings, no secrets: it joins a product repo's .infra/ manifest with a state directory you point it at, and makes Coolify match. Extracted from heavy-duty/infra, which was half tool and half state — the inconsistency that made it impossible to say whether "infra" named a CLI or a runbook. rig builds the boxes; cast fills them; infra is what they are filled with. Two changes were required to make it genuinely stateless and publishable: - The implicit cwd contract (environments.yaml / secrets/ / .coolify.env resolved against the working directory, silently reading the wrong file from the wrong place) is now an explicit --state <dir> / $CAST_STATE. - BANNED_IN_PROD — a hardcoded list of one product's ALLOW_* flags, the only product knowledge in the executor — becomes the generic, operator- owned environments.<env>.forbidden_var_patterns. The guard now lives in private state, so a product-side change cannot lower its own guard, and it is a pattern rather than a list, so it catches unforeseen siblings. Age identities resolve as $CAST_AGE_KEY_FILE_<ENV> then ~/.config/cast/age-<env>.key — which is the entire attended-vs-unattended apply mechanism, with no environment names known to the tool. Instance identity (org names, the GitHub App name, founder domains) is out of the fixtures and out of register-github-app.sh, which took APP_NAME and ORG as arguments rather than baking them in. 69 tests green; bin/cast + curl installer mirror rig's shape. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 12:25:44 +00:00
const envsPath = `/applications/${targetAppUuid}/envs`;
type EnvVar = {
key: string;
value: string;
is_buildtime?: boolean;
uuid: string;
};
const readEnvs = async (): Promise<EnvVar[]> =>
(await client.get(envsPath)) as EnvVar[];
// First var goes in via the singular envs endpoint — this is the
// never-delete canary the bulk write below must not disturb.
await client.post(envsPath, {
key: KEEP_KEY,
value: "1",
is_buildtime: false,
is_preview: false,
});
// Second var goes in via the bulk endpoint (the one apply's syncEnv uses,
// see cli.ts) with a payload containing ONLY the second var. The bulk
// envs endpoint is documented/verified as UPSERT-only (never deletes
// unlisted keys) — that's the load-bearing guarantee behind the iron rule
// that apply never deletes. If a Coolify upgrade regresses it to
// full-replace, KEEP_KEY will vanish from the read-back below.
await client.patch(`${envsPath}/bulk`, {
data: [
{
key: PROBE_KEY,
value: "1",
is_buildtime: false,
is_preview: false,
},
],
});
const envs = await readEnvs();
const keep = envs.find((e) => e.key === KEEP_KEY);
const probe = envs.find((e) => e.key === PROBE_KEY);
if (!keep) {
// Clean up whatever did survive before failing loudly.
if (probe) await client.delete_(`${envsPath}/${probe.uuid}`);
throw new Error(
"smoke FAIL: bulk env write is destructive (full-replace) — never-delete broken; do not apply with this Coolify version",
);
}
if (!probe) throw new Error("smoke FAIL: probe var not readable back");
if (probe.is_buildtime !== false) {
throw new Error(
`smoke FAIL: is_buildtime round-trip broken (got ${probe.is_buildtime}) — Coolify upgrade regression?`,
);
}
await client.delete_(`${envsPath}/${keep.uuid}`);
await client.delete_(`${envsPath}/${probe.uuid}`);
console.log(`smoke OK against Coolify ${await client.version()}`);
}