Run 324 polled 127.0.0.1:5173 instead of the service's HOST and PORT: each env file overwrote the value found in the previous one, even when it did not define the variable, so the check failed against a healthy server. The rollback then stopped at once, because its local previous_commit redeclared the read-only global of the same name. A file now keeps the value of the previous one unless it defines the variable, and the rollback uses its own name. A test runs the lookup on real env files and another forbids locals that shadow read-only globals; both fail on the previous script. The fix applies once the script is reinstalled on the server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
181 lines
9.9 KiB
JavaScript
181 lines
9.9 KiB
JavaScript
// Rôle : vérifie que le workflow de livraison conserve ses barrières de validation et de provenance.
|
|
import assert from "node:assert/strict";
|
|
import { execFile } from "node:child_process";
|
|
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { test } from "node:test";
|
|
import { promisify } from "node:util";
|
|
|
|
const deployWorkflowPath = ".forgejo/workflows/deploy-prod.yml";
|
|
const packageLockPath = "package-lock.json";
|
|
const remoteScriptPath = "scripts/deploy-sokko-g.sh";
|
|
const servicePath = "ops/systemd/sokko-g.service";
|
|
const validationWorkflowPath = ".forgejo/workflows/validate-main.yml";
|
|
const execFileAsync = promisify(execFile);
|
|
|
|
test("main pushes validate then automatically deploy the same current commit", async () => {
|
|
const workflow = await readFile(validationWorkflowPath, "utf8");
|
|
const validateJob = workflow.match(/ {2}validate:\n[\s\S]*?(?=\n {2}deploy:)/)?.[0] || "";
|
|
|
|
assert.ok(validateJob);
|
|
assert.match(workflow, /push:\s+[\s\S]*branches:\s+[\s\S]*- main/);
|
|
assert.match(workflow, /ref: \$\{\{ gitea\.sha \}\}/);
|
|
assert.match(workflow, /container: "mcr\.microsoft\.com\/playwright:v1\.62\.1-noble@sha256:[0-9a-f]{64}"/);
|
|
assert.match(workflow, /npm ci/);
|
|
assert.match(workflow, /npm run check:full/);
|
|
assert.match(workflow, /deploy:\s+[\s\S]*needs: validate/);
|
|
assert.match(workflow, /DEPLOY_COMMIT: \$\{\{ needs\.validate\.outputs\.commit \}\}/);
|
|
assert.match(workflow, />> "\$GITHUB_OUTPUT"/);
|
|
assert.match(workflow, /steps\.freshness\.outputs\.deploy == 'true'/);
|
|
assert.match(workflow, /current_commit[\s\S]*DEPLOY_COMMIT/);
|
|
assert.match(workflow, /deploy-contract/);
|
|
assert.match(workflow, /deploy-sokko-g --commit/);
|
|
assert.match(workflow, /\[webapps\.shinuwa\.fr\]:2911/);
|
|
assert.match(workflow, /shinuwa@webapps\.shinuwa\.fr/);
|
|
assert.match(workflow, /StrictHostKeyChecking=yes/);
|
|
assert.doesNotMatch(workflow, /workflow_dispatch:/);
|
|
assert.doesNotMatch(workflow, /StrictHostKeyChecking=no/);
|
|
assert.doesNotMatch(workflow, /192\.168\.0\.100/);
|
|
assert.doesNotMatch(workflow, /GITEA_OUTPUT/);
|
|
assert.doesNotMatch(validateJob, /secrets\./);
|
|
assert.doesNotMatch(validateJob, /\bssh\b/);
|
|
});
|
|
|
|
test("production deployment requires a validated exact commit and pinned SSH host", async () => {
|
|
const workflow = await readFile(deployWorkflowPath, "utf8");
|
|
|
|
assert.match(workflow, /workflow_dispatch:\s+[\s\S]*commit:/);
|
|
assert.doesNotMatch(workflow, /push:/);
|
|
assert.match(workflow, /REQUESTED_COMMIT[\s\S]*\^\[0-9a-f\]\{40\}\$/);
|
|
assert.match(workflow, /deploy:\s+[\s\S]*needs: validate/);
|
|
assert.match(workflow, /validate:\s+[\s\S]*container: "mcr\.microsoft\.com\/playwright:v1\.62\.1-noble@sha256:[0-9a-f]{64}"/);
|
|
assert.match(workflow, /npm ci/);
|
|
assert.match(workflow, /npm run check:full/);
|
|
assert.match(workflow, /git merge-base --is-ancestor "\$commit" origin\/main/);
|
|
assert.match(workflow, />> "\$GITHUB_OUTPUT"/);
|
|
assert.match(workflow, /DEPLOY_COMMIT: \$\{\{ needs\.validate\.outputs\.commit \}\}/);
|
|
assert.match(workflow, /deploy-contract/);
|
|
assert.match(workflow, /deploy-sokko-g --commit/);
|
|
assert.match(workflow, /\[webapps\.shinuwa\.fr\]:2911/);
|
|
assert.match(workflow, /shinuwa@webapps\.shinuwa\.fr/);
|
|
assert.match(workflow, /StrictHostKeyChecking=yes/);
|
|
assert.doesNotMatch(workflow, /StrictHostKeyChecking=no/);
|
|
assert.doesNotMatch(workflow, /192\.168\.0\.100/);
|
|
assert.doesNotMatch(workflow, /GITEA_OUTPUT/);
|
|
assert.doesNotMatch(workflow, /\$\{\{\s*forgejo\./);
|
|
});
|
|
|
|
test("validation containers match the locked Playwright release", async () => {
|
|
const [lockContents, validationWorkflow, deployWorkflow] = await Promise.all([
|
|
readFile(packageLockPath, "utf8"),
|
|
readFile(validationWorkflowPath, "utf8"),
|
|
readFile(deployWorkflowPath, "utf8")
|
|
]);
|
|
const lock = JSON.parse(lockContents);
|
|
const playwrightVersion = lock.packages["node_modules/@playwright/test"].version;
|
|
const expectedImage = `mcr.microsoft.com/playwright:v${playwrightVersion}-noble@sha256:`;
|
|
|
|
assert.ok(validationWorkflow.includes(expectedImage));
|
|
assert.ok(deployWorkflow.includes(expectedImage));
|
|
assert.doesNotMatch(validationWorkflow, /playwright install --with-deps/);
|
|
assert.doesNotMatch(deployWorkflow, /playwright install --with-deps/);
|
|
});
|
|
|
|
test("the versioned remote script builds and activates only the requested commit", async () => {
|
|
await execFileAsync("bash", ["-n", remoteScriptPath]);
|
|
const script = await readFile(remoteScriptPath, "utf8");
|
|
|
|
assert.match(script, /\$1 == "--commit"/);
|
|
assert.match(script, /\^\[0-9a-f\]\{40\}\$/);
|
|
assert.match(script, /flock --wait 900/);
|
|
assert.match(script, /merge-base --is-ancestor/);
|
|
assert.match(script, /worktree add --detach/);
|
|
assert.match(script, /env PATH="\$NODE_HOME\/bin:\/usr\/bin:\/bin" "\$NPM_BIN" --version/);
|
|
assert.match(script, /"\$NPM_BIN" --prefix "\$worktree" ci/);
|
|
assert.match(script, /"\$NPM_BIN" --prefix "\$worktree" run build/);
|
|
assert.match(script, /checkout --detach "\$requested_commit"/);
|
|
assert.match(script, /write_state previous-commit/);
|
|
assert.match(script, /systemctl is-active --quiet/);
|
|
assert.match(script, /systemctl show --property=ExecStart/);
|
|
assert.doesNotMatch(script, /git\s+pull/);
|
|
});
|
|
|
|
// Le service ne doit jamais tourner avec les dépendances d'une livraison précédente, ni être déclaré livré sans avoir démarré.
|
|
test("the remote script ships the commit's dependencies and waits for a healthy server", async () => {
|
|
const script = await readFile(remoteScriptPath, "utf8");
|
|
|
|
assert.match(script, /"\$NPM_BIN" --prefix "\$worktree" prune --omit=dev/);
|
|
assert.match(script, /as_app_user mv "\$APP_ROOT\/node_modules" "\$backup_node_modules"/);
|
|
assert.match(script, /as_app_user mv "\$worktree\/node_modules" "\$APP_ROOT\/node_modules"/);
|
|
assert.match(script, /as_app_user mv "\$backup_node_modules" "\$APP_ROOT\/node_modules"/);
|
|
assert.match(script, /\/api\/health/);
|
|
assert.match(script, /systemctl restart "\$SERVICE_NAME" \|\| ! wait_until_healthy/);
|
|
for (const workflow of [validationWorkflowPath, deployWorkflowPath]) {
|
|
assert.match(await readFile(workflow, "utf8"), /sokkog\.shinuwa\.fr\/api\/health \| grep -Fq '"ok":true'/);
|
|
}
|
|
});
|
|
|
|
// Exécute une fonction du script seule, avec ses dépendances remplacées par celles du test.
|
|
async function runScriptFunction(name, prelude, call) {
|
|
const script = await readFile(remoteScriptPath, "utf8");
|
|
const body = script.match(new RegExp(`^${name}\\(\\) \\{[\\s\\S]*?^\\}$`, "m"))?.[0];
|
|
assert.ok(body, name);
|
|
const { stdout } = await execFileAsync("bash", ["-c", `set -Eeuo pipefail\n${prelude}\n${body}\n${call}`]);
|
|
return stdout;
|
|
}
|
|
|
|
test("the health check reads HOST and PORT from whichever file defines them, the service file winning", async (t) => {
|
|
const directory = await mkdtemp(join(tmpdir(), "sokko-deploy-env-"));
|
|
t.after(() => rm(directory, { recursive: true, force: true }));
|
|
await writeFile(join(directory, ".env"), "PORT=3102\nHOST=10.0.0.1\n");
|
|
await writeFile(join(directory, "cards.env"), "CARDS_ENABLED=true\nHOST=\"192.168.0.100\"\n");
|
|
const prelude = `APP_ROOT=${JSON.stringify(directory)}; SERVICE_ENV_FILE=${JSON.stringify(join(directory, "cards.env"))}`;
|
|
|
|
assert.equal(await runScriptFunction("service_env_value", prelude, "service_env_value PORT"), "3102");
|
|
assert.equal(await runScriptFunction("service_env_value", prelude, "service_env_value HOST"), "192.168.0.100");
|
|
assert.equal(await runScriptFunction("service_env_value", prelude, "service_env_value MISSING"), "");
|
|
});
|
|
|
|
test("no function of the remote script redeclares one of its read-only globals", async () => {
|
|
const script = await readFile(remoteScriptPath, "utf8");
|
|
const readonlyNames = [...script.matchAll(/^readonly ([A-Za-z_]\w*)=/gm)].map((match) => match[1]);
|
|
const localNames = new Set([...script.matchAll(/^\s+local ([^=\n]+)/gm)].flatMap((match) => match[1].trim().split(/\s+/)).map((name) => name.split("=")[0]));
|
|
assert.ok(readonlyNames.includes("previous_commit"));
|
|
assert.deepEqual(readonlyNames.filter((name) => localNames.has(name)), []);
|
|
});
|
|
|
|
test("the production service uses the same pinned Node runtime", async () => {
|
|
const service = await readFile(servicePath, "utf8");
|
|
|
|
assert.match(service, /ExecStart=\/home\/shinuwa\/\.nvm\/versions\/node\/v24\.11\.1\/bin\/npm start/);
|
|
assert.match(service, /Environment=PATH=\/home\/shinuwa\/\.nvm\/versions\/node\/v24\.11\.1\/bin:/);
|
|
assert.doesNotMatch(service, /v26\.3\.0/);
|
|
});
|
|
|
|
test("the remote script never runs files owned by the service account as root", async () => {
|
|
const script = await readFile(remoteScriptPath, "utf8");
|
|
|
|
assert.match(script, /as_app_user "\$NODE_BIN" --version/);
|
|
assert.match(script, /as_app_user env PATH="\$NODE_HOME\/bin:\/usr\/bin:\/bin" "\$NPM_BIN" --version/);
|
|
assert.doesNotMatch(script, /\$\(\$NODE_BIN/);
|
|
assert.doesNotMatch(script, /\$\(env PATH=/);
|
|
assert.doesNotMatch(script, /chown -R/);
|
|
assert.doesNotMatch(script, /^\s*(?:mv .*(?:\$APP_ROOT|\$worktree|\$backup_dist|\$backup_node_modules)|rm -rf -- "\$APP_ROOT)/m);
|
|
});
|
|
|
|
test("the services cannot gain privileges through sudo", async () => {
|
|
for (const path of [servicePath, "ops/systemd/sokko-g-cards-backup.service"]) {
|
|
assert.match(await readFile(path, "utf8"), /^NoNewPrivileges=true$/m);
|
|
}
|
|
});
|
|
|
|
test("deployment secrets stay in one step and actions are pinned by commit", async () => {
|
|
for (const path of [deployWorkflowPath, validationWorkflowPath]) {
|
|
const workflow = await readFile(path, "utf8");
|
|
assert.equal(workflow.match(/secrets\.DEPLOY_SSH_KEY/g)?.length, 1);
|
|
assert.doesNotMatch(workflow.match(/ {2}deploy:\n[\s\S]*?\n {4}steps:/)?.[0] || "", /secrets\./);
|
|
assert.match(workflow, /- name: Configure the pinned production host key\n(?: {8}if: .*\n)? {8}env:\n {10}DEPLOY_SSH_KEY: \$\{\{ secrets\.DEPLOY_SSH_KEY \}\}/);
|
|
for (const [, reference] of workflow.matchAll(/uses: \S+@(\S+)/g)) assert.match(reference, /^[0-9a-f]{40}$/);
|
|
}
|
|
});
|