cast/test/envtemplate.test.ts

199 lines
7.8 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 { describe, expect, it } from "vitest";
feat(resolve): derive DATABASE_URL/REDIS_URL from the database cast created (#60) Add a ${resource:<name>.url} env-template ref that resolves to the internal URL of a database the same manifest declares, read back from the live resource's internal_db_url — never stored in the age store, never decrypted, never printed. This deletes the two-pass generated-secret bootstrap for a database's own URL rather than automating it: no placeholder, no stored copy to drift or overwrite, and a rotated password is simply followed on the next apply. Resolution runs in one function (fillDerivedEnv) against two URL maps: at diff time against databases already on the box (so a matching app shows no drift — killing the "secret DATABASE_URL differs" noise that ran on every plan), and in the executor at apply time against a database created earlier in the same run (the from-nothing case; apply acts databases-before-applications, #45). The unresolved sentinel is never written — the executor refuses, rather than write a blank that boots the app pointed at nothing, and re-running once the database is up resolves it as an ordinary update. A ${resource:X.url} naming a database the manifest does not declare, or an attribute other than .url, is a hard plan-time error refused by every verb that opens a template (apply, diff, capture). generated_secrets and the two-pass bootstrap remain for the residual class — a provider-generated value that genuinely is not derivable. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 23:46:26 +00:00
import {
DERIVED_UNRESOLVED,
feat(resolve): derive base-URL env vars from manifest domains via ${domain:...} (#66) A public base URL an app reads (LANDING_BASE_URL, ADMIN_WEB_BASE_URL) is a fact the manifest already states in `domains`/`service_domains` — the same fields cast parses to reconcile Coolify domains. Hand-transcribing it into an env template is a second copy that drifts (incubator's prod LANDING_BASE_URL silently kept a pre-apex host). So a template can now say it directly: LANDING_BASE_URL=${domain:landing} ADMIN_WEB_BASE_URL=${domain:core.admin} - ${domain:<app>} -> applications.<app>.domains[0] - ${domain:<app>.<service>} -> applications.<app>.service_domains.<service>[0] Symmetric with ${resource:...} (#60) — parse -> sentinel -> validate -> fill — but a domain is PURE MANIFEST DATA, known at plan time, so it resolves fully in desiredFromManifest against a map built from the manifest: no live read, no executor deferral, no unresolved-at-write path. Domains are PUBLIC, so they resolve to secret:false (printed in diffs) and read as plain literals downstream (no diff.ts change). Not secrets: excluded from templateRefs, never captured. assertDomainRefs is the single validation gate (apply/diff/capture), refusing an undeclared app/service, a wrong-shape ref, or an empty/blank domain list before the sentinel can escape. Applications only (Coolify 4.1.2 can't set service domains). REPORTING_TZ-style operator literals stay literal. Closes #66. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-15 12:02:33 +00:00
DOMAIN_UNRESOLVED,
feat(resolve): derive DATABASE_URL/REDIS_URL from the database cast created (#60) Add a ${resource:<name>.url} env-template ref that resolves to the internal URL of a database the same manifest declares, read back from the live resource's internal_db_url — never stored in the age store, never decrypted, never printed. This deletes the two-pass generated-secret bootstrap for a database's own URL rather than automating it: no placeholder, no stored copy to drift or overwrite, and a rotated password is simply followed on the next apply. Resolution runs in one function (fillDerivedEnv) against two URL maps: at diff time against databases already on the box (so a matching app shows no drift — killing the "secret DATABASE_URL differs" noise that ran on every plan), and in the executor at apply time against a database created earlier in the same run (the from-nothing case; apply acts databases-before-applications, #45). The unresolved sentinel is never written — the executor refuses, rather than write a blank that boots the app pointed at nothing, and re-running once the database is up resolves it as an ordinary update. A ${resource:X.url} naming a database the manifest does not declare, or an attribute other than .url, is a hard plan-time error refused by every verb that opens a template (apply, diff, capture). generated_secrets and the two-pass bootstrap remain for the residual class — a provider-generated value that genuinely is not derivable. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 23:46:26 +00:00
assertEnvVarPolicy,
fillDerivedEnv,
feat(resolve): derive base-URL env vars from manifest domains via ${domain:...} (#66) A public base URL an app reads (LANDING_BASE_URL, ADMIN_WEB_BASE_URL) is a fact the manifest already states in `domains`/`service_domains` — the same fields cast parses to reconcile Coolify domains. Hand-transcribing it into an env template is a second copy that drifts (incubator's prod LANDING_BASE_URL silently kept a pre-apex host). So a template can now say it directly: LANDING_BASE_URL=${domain:landing} ADMIN_WEB_BASE_URL=${domain:core.admin} - ${domain:<app>} -> applications.<app>.domains[0] - ${domain:<app>.<service>} -> applications.<app>.service_domains.<service>[0] Symmetric with ${resource:...} (#60) — parse -> sentinel -> validate -> fill — but a domain is PURE MANIFEST DATA, known at plan time, so it resolves fully in desiredFromManifest against a map built from the manifest: no live read, no executor deferral, no unresolved-at-write path. Domains are PUBLIC, so they resolve to secret:false (printed in diffs) and read as plain literals downstream (no diff.ts change). Not secrets: excluded from templateRefs, never captured. assertDomainRefs is the single validation gate (apply/diff/capture), refusing an undeclared app/service, a wrong-shape ref, or an empty/blank domain list before the sentinel can escape. Applications only (Coolify 4.1.2 can't set service domains). REPORTING_TZ-style operator literals stay literal. Closes #66. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-15 12:02:33 +00:00
fillDomainEnv,
feat(resolve): derive DATABASE_URL/REDIS_URL from the database cast created (#60) Add a ${resource:<name>.url} env-template ref that resolves to the internal URL of a database the same manifest declares, read back from the live resource's internal_db_url — never stored in the age store, never decrypted, never printed. This deletes the two-pass generated-secret bootstrap for a database's own URL rather than automating it: no placeholder, no stored copy to drift or overwrite, and a rotated password is simply followed on the next apply. Resolution runs in one function (fillDerivedEnv) against two URL maps: at diff time against databases already on the box (so a matching app shows no drift — killing the "secret DATABASE_URL differs" noise that ran on every plan), and in the executor at apply time against a database created earlier in the same run (the from-nothing case; apply acts databases-before-applications, #45). The unresolved sentinel is never written — the executor refuses, rather than write a blank that boots the app pointed at nothing, and re-running once the database is up resolves it as an ordinary update. A ${resource:X.url} naming a database the manifest does not declare, or an attribute other than .url, is a hard plan-time error refused by every verb that opens a template (apply, diff, capture). generated_secrets and the two-pass bootstrap remain for the residual class — a provider-generated value that genuinely is not derivable. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 23:46:26 +00:00
resolveTemplate,
feat(resolve): derive base-URL env vars from manifest domains via ${domain:...} (#66) A public base URL an app reads (LANDING_BASE_URL, ADMIN_WEB_BASE_URL) is a fact the manifest already states in `domains`/`service_domains` — the same fields cast parses to reconcile Coolify domains. Hand-transcribing it into an env template is a second copy that drifts (incubator's prod LANDING_BASE_URL silently kept a pre-apex host). So a template can now say it directly: LANDING_BASE_URL=${domain:landing} ADMIN_WEB_BASE_URL=${domain:core.admin} - ${domain:<app>} -> applications.<app>.domains[0] - ${domain:<app>.<service>} -> applications.<app>.service_domains.<service>[0] Symmetric with ${resource:...} (#60) — parse -> sentinel -> validate -> fill — but a domain is PURE MANIFEST DATA, known at plan time, so it resolves fully in desiredFromManifest against a map built from the manifest: no live read, no executor deferral, no unresolved-at-write path. Domains are PUBLIC, so they resolve to secret:false (printed in diffs) and read as plain literals downstream (no diff.ts change). Not secrets: excluded from templateRefs, never captured. assertDomainRefs is the single validation gate (apply/diff/capture), refusing an undeclared app/service, a wrong-shape ref, or an empty/blank domain list before the sentinel can escape. Applications only (Coolify 4.1.2 can't set service domains). REPORTING_TZ-style operator literals stay literal. Closes #66. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-15 12:02:33 +00:00
templateDomainRefs,
feat(resolve): derive DATABASE_URL/REDIS_URL from the database cast created (#60) Add a ${resource:<name>.url} env-template ref that resolves to the internal URL of a database the same manifest declares, read back from the live resource's internal_db_url — never stored in the age store, never decrypted, never printed. This deletes the two-pass generated-secret bootstrap for a database's own URL rather than automating it: no placeholder, no stored copy to drift or overwrite, and a rotated password is simply followed on the next apply. Resolution runs in one function (fillDerivedEnv) against two URL maps: at diff time against databases already on the box (so a matching app shows no drift — killing the "secret DATABASE_URL differs" noise that ran on every plan), and in the executor at apply time against a database created earlier in the same run (the from-nothing case; apply acts databases-before-applications, #45). The unresolved sentinel is never written — the executor refuses, rather than write a blank that boots the app pointed at nothing, and re-running once the database is up resolves it as an ordinary update. A ${resource:X.url} naming a database the manifest does not declare, or an attribute other than .url, is a hard plan-time error refused by every verb that opens a template (apply, diff, capture). generated_secrets and the two-pass bootstrap remain for the residual class — a provider-generated value that genuinely is not derivable. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 23:46:26 +00:00
templateRefs,
templateResourceRefs,
unresolvedDerived,
} from "../src/envtemplate.js";
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
describe("resolveTemplate", () => {
it("classifies literals as non-secret and ${…} as secret", () => {
const r = resolveTemplate(
"PORT=3000\nMAILGUN_KEY=${MAILGUN_KEY}\n# comment\n\n",
{
MAILGUN_KEY: "mk-123",
},
);
expect(r.vars.PORT).toEqual({ value: "3000", secret: false });
expect(r.vars.MAILGUN_KEY).toEqual({ value: "mk-123", secret: true });
});
it("throws on a missing secret, naming key and placeholder", () => {
expect(() => resolveTemplate("API_KEY=${NOPE}", {})).toThrow(
/NOPE.*API_KEY|API_KEY.*NOPE/,
);
});
it("throws on malformed lines with the line number", () => {
expect(() => resolveTemplate("PORT=3000\nnot a line", {})).toThrow(
/line 2/,
);
});
});
describe("assertEnvVarPolicy", () => {
const withFlag = { vars: { ALLOW_SEED: { value: "false", secret: false } } };
const banAllow = ["^ALLOW_"];
it("refuses when a forbidden var is PRESENT, even set false", () => {
expect(() =>
assertEnvVarPolicy("prod", { "core-api": withFlag }, banAllow),
).toThrow(/ALLOW_SEED.*core-api.*off.*absent/s);
});
it("allows the same env where the policy is not declared", () => {
expect(() =>
assertEnvVarPolicy("staging", { "core-api": withFlag }, undefined),
).not.toThrow();
});
it("is a pattern, not a fixed list — it catches unforeseen siblings", () => {
const future = {
vars: { ALLOW_WIPE_EVERYTHING: { value: "true", secret: false } },
};
expect(() =>
assertEnvVarPolicy("prod", { worker: future }, banAllow),
).toThrow(/ALLOW_WIPE_EVERYTHING/);
});
it("leaves vars that do not match the pattern alone", () => {
const ok = { vars: { PORT: { value: "3000", secret: false } } };
expect(() =>
assertEnvVarPolicy("prod", { "core-api": ok }, banAllow),
).not.toThrow();
});
});
feat(resolve): derive DATABASE_URL/REDIS_URL from the database cast created (#60) Add a ${resource:<name>.url} env-template ref that resolves to the internal URL of a database the same manifest declares, read back from the live resource's internal_db_url — never stored in the age store, never decrypted, never printed. This deletes the two-pass generated-secret bootstrap for a database's own URL rather than automating it: no placeholder, no stored copy to drift or overwrite, and a rotated password is simply followed on the next apply. Resolution runs in one function (fillDerivedEnv) against two URL maps: at diff time against databases already on the box (so a matching app shows no drift — killing the "secret DATABASE_URL differs" noise that ran on every plan), and in the executor at apply time against a database created earlier in the same run (the from-nothing case; apply acts databases-before-applications, #45). The unresolved sentinel is never written — the executor refuses, rather than write a blank that boots the app pointed at nothing, and re-running once the database is up resolves it as an ordinary update. A ${resource:X.url} naming a database the manifest does not declare, or an attribute other than .url, is a hard plan-time error refused by every verb that opens a template (apply, diff, capture). generated_secrets and the two-pass bootstrap remain for the residual class — a provider-generated value that genuinely is not derivable. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 23:46:26 +00:00
describe("derived resource refs (#60)", () => {
const template =
"PORT=3000\nMAILGUN_KEY=${MAILGUN_KEY}\nDATABASE_URL=${resource:postgres.url}\n";
it("resolveTemplate marks a ${resource:…} var derived and UNRESOLVED, not secret-missing", () => {
// No store entry for it, and yet it does not throw the way a missing secret
// does: a derived value is not in the store to be missing FROM.
const r = resolveTemplate(template, { MAILGUN_KEY: "mk" });
expect(r.vars.DATABASE_URL).toEqual({
value: DERIVED_UNRESOLVED,
secret: true,
derived: { resource: "postgres", attr: "url" },
});
// The secret and the literal are untouched by the new branch.
expect(r.vars.MAILGUN_KEY).toEqual({ value: "mk", secret: true });
expect(r.vars.PORT).toEqual({ value: "3000", secret: false });
});
it("templateResourceRefs reports the edge; templateRefs does NOT treat it as a secret", () => {
expect(templateResourceRefs(template)).toEqual([
{ key: "DATABASE_URL", resource: "postgres", attr: "url" },
]);
// capture reads templateRefs — a derived edge must never appear there, or it
// would go hunting for a store name called `resource:postgres.url`.
expect(templateRefs(template).map((r) => r.key)).toEqual(["MAILGUN_KEY"]);
});
it("fillDerivedEnv resolves against a URL map and leaves the rest alone", () => {
const env = resolveTemplate(template, { MAILGUN_KEY: "mk" });
const filled = fillDerivedEnv(env, {
postgres: "postgres://u:p@uuid:5432/db",
});
expect(filled.vars.DATABASE_URL).toEqual({
value: "postgres://u:p@uuid:5432/db",
secret: true,
derived: { resource: "postgres", attr: "url" },
});
expect(unresolvedDerived(filled)).toEqual([]);
});
it("fillDerivedEnv leaves a ref whose resource is absent (or empty) unresolved", () => {
const env = resolveTemplate(template, { MAILGUN_KEY: "mk" });
// Absent from the map, and present-but-empty, are both non-resolutions — an
// empty URL must never be written (it boots the app pointed at nothing).
expect(unresolvedDerived(fillDerivedEnv(env, {}))).toEqual([
{ key: "DATABASE_URL", resource: "postgres" },
]);
expect(unresolvedDerived(fillDerivedEnv(env, { postgres: "" }))).toEqual([
{ key: "DATABASE_URL", resource: "postgres" },
]);
});
});
feat(resolve): derive base-URL env vars from manifest domains via ${domain:...} (#66) A public base URL an app reads (LANDING_BASE_URL, ADMIN_WEB_BASE_URL) is a fact the manifest already states in `domains`/`service_domains` — the same fields cast parses to reconcile Coolify domains. Hand-transcribing it into an env template is a second copy that drifts (incubator's prod LANDING_BASE_URL silently kept a pre-apex host). So a template can now say it directly: LANDING_BASE_URL=${domain:landing} ADMIN_WEB_BASE_URL=${domain:core.admin} - ${domain:<app>} -> applications.<app>.domains[0] - ${domain:<app>.<service>} -> applications.<app>.service_domains.<service>[0] Symmetric with ${resource:...} (#60) — parse -> sentinel -> validate -> fill — but a domain is PURE MANIFEST DATA, known at plan time, so it resolves fully in desiredFromManifest against a map built from the manifest: no live read, no executor deferral, no unresolved-at-write path. Domains are PUBLIC, so they resolve to secret:false (printed in diffs) and read as plain literals downstream (no diff.ts change). Not secrets: excluded from templateRefs, never captured. assertDomainRefs is the single validation gate (apply/diff/capture), refusing an undeclared app/service, a wrong-shape ref, or an empty/blank domain list before the sentinel can escape. Applications only (Coolify 4.1.2 can't set service domains). REPORTING_TZ-style operator literals stay literal. Closes #66. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-15 12:02:33 +00:00
describe("derived domain refs (#66)", () => {
const template =
"PORT=3000\nMAILGUN_KEY=${MAILGUN_KEY}\nDATABASE_URL=${resource:postgres.url}\nLANDING_BASE_URL=${domain:landing}\nADMIN_WEB_BASE_URL=${domain:core.admin}\n";
it("resolveTemplate marks a ${domain:…} var with its ref, secret:false, and the transient sentinel — never the literal", () => {
const r = resolveTemplate(template, { MAILGUN_KEY: "mk" });
// An app ref and an app.service ref, both carrying the sentinel and the ref,
// both public (secret:false) — and NOT written through as the literal text.
expect(r.vars.LANDING_BASE_URL).toEqual({
value: DOMAIN_UNRESOLVED,
secret: false,
domain: { app: "landing" },
});
expect(r.vars.ADMIN_WEB_BASE_URL).toEqual({
value: DOMAIN_UNRESOLVED,
secret: false,
domain: { app: "core", service: "admin" },
});
expect(r.vars.LANDING_BASE_URL.value).not.toContain("${domain:");
// The three ref kinds stay mutually exclusive, and the plain secret/literal
// are untouched by the new branch.
expect(r.vars.DATABASE_URL).toEqual({
value: DERIVED_UNRESOLVED,
secret: true,
derived: { resource: "postgres", attr: "url" },
});
expect(r.vars.MAILGUN_KEY).toEqual({ value: "mk", secret: true });
expect(r.vars.PORT).toEqual({ value: "3000", secret: false });
});
it("fillDomainEnv resolves app and app.service refs verbatim, secret:false, marker dropped", () => {
const env = resolveTemplate(template, { MAILGUN_KEY: "mk" });
const filled = fillDomainEnv(env, {
landing: "https://new.heavyduty.builders",
"core.admin": "https://admin.heavyduty.builders",
});
// Resolved to the verbatim domain (scheme and all), public, and with the
// `domain` marker DROPPED — indistinguishable from a literal downstream.
expect(filled.vars.LANDING_BASE_URL).toEqual({
value: "https://new.heavyduty.builders",
secret: false,
});
expect(filled.vars.ADMIN_WEB_BASE_URL).toEqual({
value: "https://admin.heavyduty.builders",
secret: false,
});
// The non-domain vars ride through untouched.
expect(filled.vars.MAILGUN_KEY).toEqual({ value: "mk", secret: true });
expect(filled.vars.DATABASE_URL.value).toBe(DERIVED_UNRESOLVED);
});
it("fillDomainEnv leaves an unknown key as-is (sentinel and marker intact)", () => {
const env = resolveTemplate("X=${domain:landing}\n", {});
// Absent from the map, and present-but-empty, are both non-resolutions.
expect(fillDomainEnv(env, {}).vars.X).toEqual({
value: DOMAIN_UNRESOLVED,
secret: false,
domain: { app: "landing" },
});
expect(fillDomainEnv(env, { landing: "" }).vars.X).toEqual({
value: DOMAIN_UNRESOLVED,
secret: false,
domain: { app: "landing" },
});
});
it("templateDomainRefs extracts the edges; templateRefs/templateResourceRefs exclude them", () => {
expect(templateDomainRefs(template)).toEqual([
{ key: "LANDING_BASE_URL", app: "landing" },
{ key: "ADMIN_WEB_BASE_URL", app: "core", service: "admin" },
]);
// A domain is not a secret to capture, and not a resource edge either.
expect(templateRefs(template).map((r) => r.key)).toEqual(["MAILGUN_KEY"]);
expect(templateResourceRefs(template).map((r) => r.key)).toEqual([
"DATABASE_URL",
]);
});
});