@intentius/chant 0.124.85 → 0.124.86
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle-check.d.ts +102 -0
- package/dist/bundle-check.d.ts.map +1 -0
- package/dist/bundle-check.js +310 -0
- package/dist/chant-install.d.ts +100 -0
- package/dist/chant-install.d.ts.map +1 -0
- package/dist/chant-install.js +158 -0
- package/dist/ci-commit.d.ts +19 -6
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +23 -4
- package/dist/ci-evidence-jobs.d.ts +54 -0
- package/dist/ci-evidence-jobs.d.ts.map +1 -0
- package/dist/ci-evidence-jobs.js +55 -0
- package/dist/ci-image.d.ts +274 -0
- package/dist/ci-image.d.ts.map +1 -0
- package/dist/ci-image.js +635 -0
- package/dist/ci-login.d.ts +150 -0
- package/dist/ci-login.d.ts.map +1 -0
- package/dist/ci-login.js +240 -0
- package/dist/ci-product.d.ts +16 -0
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +17 -0
- package/dist/cli/commands/import-index.js +2 -2
- package/dist/cli/handlers/build.d.ts +8 -0
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +22 -4
- package/dist/cli/handlers/ci-check.d.ts.map +1 -1
- package/dist/cli/handlers/ci-check.js +57 -2
- package/dist/cli/handlers/ci-evidence.d.ts +3 -0
- package/dist/cli/handlers/ci-evidence.d.ts.map +1 -0
- package/dist/cli/handlers/ci-evidence.js +171 -0
- package/dist/cli/handlers/components.js +3 -3
- package/dist/cli/handlers/dead-guards.d.ts +3 -0
- package/dist/cli/handlers/dead-guards.d.ts.map +1 -0
- package/dist/cli/handlers/dead-guards.js +35 -0
- package/dist/cli/handlers/graph.js +4 -4
- package/dist/cli/handlers/lifecycle.d.ts +2 -1
- package/dist/cli/handlers/lifecycle.d.ts.map +1 -1
- package/dist/cli/handlers/lifecycle.js +5 -4
- package/dist/cli/handlers/local-chant.d.ts +16 -0
- package/dist/cli/handlers/local-chant.d.ts.map +1 -0
- package/dist/cli/handlers/local-chant.js +109 -0
- package/dist/cli/handlers/operator.js +2 -2
- package/dist/cli/handlers/search.js +5 -5
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +38 -0
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +28 -0
- package/dist/cli/registry.d.ts +16 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/check-hooks.d.ts +49 -0
- package/dist/components/check-hooks.d.ts.map +1 -1
- package/dist/components/check-hooks.js +64 -4
- package/dist/components/check-steps.d.ts +17 -0
- package/dist/components/check-steps.d.ts.map +1 -1
- package/dist/components/check-steps.js +37 -5
- package/dist/components/product-pipeline.d.ts +23 -0
- package/dist/components/product-pipeline.d.ts.map +1 -1
- package/dist/components/product-pipeline.js +6 -2
- package/dist/components/product-plan.d.ts +137 -0
- package/dist/components/product-plan.d.ts.map +1 -0
- package/dist/components/product-plan.js +143 -0
- package/dist/components/release-compile.d.ts.map +1 -1
- package/dist/components/release-compile.js +2 -2
- package/dist/config.d.ts +39 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +35 -3
- package/dist/dead-guards.d.ts +82 -0
- package/dist/dead-guards.d.ts.map +1 -0
- package/dist/dead-guards.js +436 -0
- package/dist/discovery/files.d.ts +3 -1
- package/dist/discovery/files.d.ts.map +1 -1
- package/dist/discovery/files.js +5 -3
- package/dist/drift-issue.d.ts +152 -0
- package/dist/drift-issue.d.ts.map +1 -0
- package/dist/drift-issue.js +167 -0
- package/dist/floor-table.d.ts +30 -0
- package/dist/floor-table.d.ts.map +1 -0
- package/dist/floor-table.data.d.ts +3 -0
- package/dist/floor-table.data.d.ts.map +1 -0
- package/dist/floor-table.data.js +178 -0
- package/dist/floor-table.js +56 -0
- package/dist/forge-flags.d.ts +90 -0
- package/dist/forge-flags.d.ts.map +1 -0
- package/dist/forge-flags.js +184 -0
- package/dist/lexicon.d.ts +69 -0
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lifecycle/gated-op.d.ts +7 -1
- package/dist/lifecycle/gated-op.d.ts.map +1 -1
- package/dist/lifecycle/gated-op.js +7 -4
- package/dist/local-chant.d.ts +98 -0
- package/dist/local-chant.d.ts.map +1 -0
- package/dist/local-chant.js +307 -0
- package/dist/op/activities/predict-behaviour.js +2 -2
- package/dist/op/op-waves-evidence-spec.d.ts +13 -0
- package/dist/op/op-waves-evidence-spec.d.ts.map +1 -0
- package/dist/op/op-waves-evidence-spec.js +21 -0
- package/dist/op/op-waves-evidence.d.ts +38 -0
- package/dist/op/op-waves-evidence.d.ts.map +1 -0
- package/dist/op/op-waves-evidence.js +57 -0
- package/dist/op/op-waves-run.d.ts +4 -0
- package/dist/op/op-waves-run.d.ts.map +1 -1
- package/dist/op/op-waves-run.js +24 -1
- package/dist/op/op-waves.d.ts +17 -1
- package/dist/op/op-waves.d.ts.map +1 -1
- package/dist/op/op-waves.js +23 -0
- package/dist/op/schedule-overdue.d.ts +41 -9
- package/dist/op/schedule-overdue.d.ts.map +1 -1
- package/dist/op/schedule-overdue.js +154 -25
- package/dist/plan-summary.d.ts +25 -0
- package/dist/plan-summary.d.ts.map +1 -1
- package/dist/plan-summary.js +31 -0
- package/dist/pr-plan-note.d.ts +35 -0
- package/dist/pr-plan-note.d.ts.map +1 -1
- package/dist/pr-plan-note.js +10 -0
- package/dist/pr-post.d.ts +23 -0
- package/dist/pr-post.d.ts.map +1 -1
- package/dist/pr-post.js +125 -3
- package/dist/release-floors.d.ts +31 -1
- package/dist/release-floors.d.ts.map +1 -1
- package/dist/release-floors.js +31 -1
- package/dist/state-migration.d.ts +24 -0
- package/dist/state-migration.d.ts.map +1 -1
- package/dist/state-migration.js +100 -5
- package/dist/wave-runtime.mjs +305 -298
- package/dist/workspace/lexicon-outputs.d.ts.map +1 -1
- package/dist/workspace/lexicon-outputs.js +21 -7
- package/dist/workspace/reason-codes.d.ts +3 -0
- package/dist/workspace/reason-codes.d.ts.map +1 -1
- package/dist/workspace/reason-codes.js +4 -0
- package/dist/workspace/ref-gate.schema.json +85 -0
- package/dist/workspace/search-contract.d.ts +1 -1
- package/dist/workspace/search-contract.d.ts.map +1 -1
- package/dist/workspace/trust/check-evidence-report.d.ts +51 -0
- package/dist/workspace/trust/check-evidence-report.d.ts.map +1 -0
- package/dist/workspace/trust/check-evidence-report.js +102 -0
- package/dist/workspace/trust/check-evidence.d.ts +126 -0
- package/dist/workspace/trust/check-evidence.d.ts.map +1 -0
- package/dist/workspace/trust/check-evidence.js +237 -0
- package/dist/workspace/trust/local-evidence.d.ts +120 -0
- package/dist/workspace/trust/local-evidence.d.ts.map +1 -0
- package/dist/workspace/trust/local-evidence.js +259 -0
- package/dist/workspace/trust/policy.d.ts +84 -0
- package/dist/workspace/trust/policy.d.ts.map +1 -1
- package/dist/workspace/trust/policy.js +94 -1
- package/dist/workspace/trust/ref-gate-cli.d.ts +33 -0
- package/dist/workspace/trust/ref-gate-cli.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate-cli.js +234 -0
- package/dist/workspace/trust/ref-gate-fixture.d.ts +38 -0
- package/dist/workspace/trust/ref-gate-fixture.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate-fixture.js +97 -0
- package/dist/workspace/trust/ref-gate.d.ts +187 -0
- package/dist/workspace/trust/ref-gate.d.ts.map +1 -0
- package/dist/workspace/trust/ref-gate.js +430 -0
- package/dist/workspace/trust/trust-files.d.ts.map +1 -1
- package/dist/workspace/trust/trust-files.js +18 -1
- package/package.json +1 -1
- package/src/bundle-check.test.ts +140 -0
- package/src/bundle-check.ts +352 -0
- package/src/chant-install.test.ts +112 -0
- package/src/chant-install.ts +179 -0
- package/src/ci-commit.test.ts +45 -0
- package/src/ci-commit.ts +38 -4
- package/src/ci-evidence-jobs.ts +67 -0
- package/src/ci-image.test.ts +506 -0
- package/src/ci-image.ts +822 -0
- package/src/ci-login.test.ts +235 -0
- package/src/ci-login.ts +307 -0
- package/src/ci-product.ts +18 -0
- package/src/cli/commands/__fixtures__/source-dir-include/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-include/infra.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-include/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-root/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/infra.test.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/infra.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-root/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/app.ts +3 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/chant.config.json +1 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/infra/net.ts +1 -0
- package/src/cli/commands/__fixtures__/source-dir-scoped/scripts/run-on-load.ts +3 -0
- package/src/cli/commands/import-index.ts +2 -2
- package/src/cli/handlers/build-source-dir.test.ts +64 -0
- package/src/cli/handlers/build.ts +21 -4
- package/src/cli/handlers/ci-check.ts +56 -3
- package/src/cli/handlers/ci-evidence.ts +162 -0
- package/src/cli/handlers/components.ts +3 -3
- package/src/cli/handlers/dead-guards.ts +37 -0
- package/src/cli/handlers/graph.ts +4 -4
- package/src/cli/handlers/lifecycle.ts +5 -4
- package/src/cli/handlers/local-chant.ts +106 -0
- package/src/cli/handlers/operator.ts +2 -2
- package/src/cli/handlers/search.ts +5 -5
- package/src/cli/main.test.ts +7 -0
- package/src/cli/main.ts +38 -0
- package/src/cli/parse-args.ts +21 -0
- package/src/cli/registry.ts +16 -0
- package/src/components/check-evidence-stage.test.ts +80 -0
- package/src/components/check-hooks.ts +84 -4
- package/src/components/check-local-evidence-stage.test.ts +141 -0
- package/src/components/check-steps.ts +56 -6
- package/src/components/product-pipeline.ts +28 -3
- package/src/components/product-plan.test.ts +87 -0
- package/src/components/product-plan.ts +226 -0
- package/src/components/release-compile.ts +2 -2
- package/src/config.ts +44 -3
- package/src/dead-guards.test.ts +206 -0
- package/src/dead-guards.ts +446 -0
- package/src/discovery/files.ts +5 -3
- package/src/drift-issue-bundle.test.ts +31 -0
- package/src/drift-issue.test.ts +206 -0
- package/src/drift-issue.ts +274 -0
- package/src/fit-note.test.ts +44 -0
- package/src/floor-table.data.ts +180 -0
- package/src/floor-table.ts +77 -0
- package/src/forge-flags.test.ts +110 -0
- package/src/forge-flags.ts +208 -0
- package/src/gate-decision-bundle.test.ts +1 -0
- package/src/lexicon.ts +68 -0
- package/src/lifecycle/gated-op.ts +10 -5
- package/src/local-chant.e2e.test.ts +94 -0
- package/src/local-chant.test.ts +162 -0
- package/src/local-chant.ts +341 -0
- package/src/op/activities/predict-behaviour.ts +2 -2
- package/src/op/op-waves-evidence-spec.ts +24 -0
- package/src/op/op-waves-evidence.test.ts +130 -0
- package/src/op/op-waves-evidence.ts +57 -0
- package/src/op/op-waves-run.ts +25 -1
- package/src/op/op-waves.ts +31 -1
- package/src/op/schedule-overdue.test.ts +85 -0
- package/src/op/schedule-overdue.ts +162 -26
- package/src/plan-summary.ts +40 -0
- package/src/pr-plan-note.ts +34 -0
- package/src/pr-post-plan-note.test.ts +146 -0
- package/src/pr-post.ts +127 -3
- package/src/release-floors.ts +44 -1
- package/src/stage-hooks-bundle.test.ts +1 -0
- package/src/state-migration.test.ts +52 -1
- package/src/state-migration.ts +107 -5
- package/src/workspace/lexicon-outputs.ts +23 -6
- package/src/workspace/reason-codes.test.ts +3 -0
- package/src/workspace/reason-codes.ts +4 -0
- package/src/workspace/ref-gate.schema.json +85 -0
- package/src/workspace/trust/check-evidence-report.ts +106 -0
- package/src/workspace/trust/check-evidence.test.ts +133 -0
- package/src/workspace/trust/check-evidence.ts +257 -0
- package/src/workspace/trust/local-evidence.test.ts +127 -0
- package/src/workspace/trust/local-evidence.ts +266 -0
- package/src/workspace/trust/policy.ts +134 -1
- package/src/workspace/trust/ref-gate-cli.ts +244 -0
- package/src/workspace/trust/ref-gate-fixture.ts +125 -0
- package/src/workspace/trust/ref-gate.e2e.test.ts +76 -0
- package/src/workspace/trust/ref-gate.test.ts +247 -0
- package/src/workspace/trust/ref-gate.ts +538 -0
- package/src/workspace/trust/trust-files.test.ts +16 -0
- package/src/workspace/trust/trust-files.ts +41 -1
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://intentius.io/chant/schemas/workspace/ref-gate/v1/ref-gate.schema.json",
|
|
4
|
+
"title": "chant workspace ref-gate output",
|
|
5
|
+
"description": "What `chant workspace ref-gate check|status|pre-receive|pre-push --json` prints (#4867): for each ref update, whether the commit carries current runner evidence for every check `refs` in `.chant/trust.json` at base requires for the ref, and the decision. Version 1 of the read contract. Readers ignore fields they do not know; a field is only ever added within a version. The codes are a closed list, each in the one closed list of `reason-codes.ts`.",
|
|
6
|
+
"oneOf": [
|
|
7
|
+
{ "$ref": "#/$defs/result" },
|
|
8
|
+
{ "$ref": "#/$defs/failure" }
|
|
9
|
+
],
|
|
10
|
+
"$defs": {
|
|
11
|
+
"result": {
|
|
12
|
+
"type": "object",
|
|
13
|
+
"required": ["$schema", "contract", "chant", "ok", "decisions"],
|
|
14
|
+
"properties": {
|
|
15
|
+
"$schema": { "const": "https://intentius.io/chant/schemas/workspace/ref-gate/v1/ref-gate.schema.json" },
|
|
16
|
+
"contract": { "const": 1 },
|
|
17
|
+
"chant": { "type": "string" },
|
|
18
|
+
"ok": { "type": "boolean", "description": "True when every update is admitted." },
|
|
19
|
+
"ledger": { "type": "string", "description": "The ledger file the decisions were appended to, when they were." },
|
|
20
|
+
"decisions": { "type": "array", "items": { "$ref": "#/$defs/decision" } }
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"failure": {
|
|
24
|
+
"type": "object",
|
|
25
|
+
"required": ["$schema", "contract", "chant", "error"],
|
|
26
|
+
"properties": {
|
|
27
|
+
"$schema": { "const": "https://intentius.io/chant/schemas/workspace/ref-gate/v1/ref-gate.schema.json" },
|
|
28
|
+
"contract": { "const": 1 },
|
|
29
|
+
"chant": { "type": "string" },
|
|
30
|
+
"error": { "$ref": "#/$defs/error" }
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"error": {
|
|
34
|
+
"type": "object",
|
|
35
|
+
"required": ["code", "message"],
|
|
36
|
+
"properties": {
|
|
37
|
+
"code": { "enum": ["not-a-git-repository", "revision-unknown", "trust-policy-unreadable", "ref-update-invalid"] },
|
|
38
|
+
"message": { "type": "string" }
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"oid": { "type": ["string", "null"], "pattern": "^(?:[0-9a-f]{40}|[0-9a-f]{64})$" },
|
|
42
|
+
"decision": {
|
|
43
|
+
"type": "object",
|
|
44
|
+
"required": ["ref", "old", "new", "base", "decision", "status", "checks"],
|
|
45
|
+
"properties": {
|
|
46
|
+
"ref": { "type": "string" },
|
|
47
|
+
"old": { "$ref": "#/$defs/oid", "description": "The ref's tip before the update; null for a new ref." },
|
|
48
|
+
"new": { "$ref": "#/$defs/oid", "description": "The tip the update sets; null for a deletion." },
|
|
49
|
+
"base": { "$ref": "#/$defs/oid", "description": "The commit the policy was read at; null when there is none, and nothing is required." },
|
|
50
|
+
"decision": { "enum": ["admit", "refuse"] },
|
|
51
|
+
"status": { "type": ["string", "null"], "description": "The commit status a forge requires for the ref, when a rule matched." },
|
|
52
|
+
"checks": { "type": "array", "items": { "$ref": "#/$defs/check" } },
|
|
53
|
+
"error": { "$ref": "#/$defs/error", "description": "Set when the gate could not decide; the update is refused." }
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"check": {
|
|
57
|
+
"type": "object",
|
|
58
|
+
"required": ["check", "action", "command", "status", "reason", "subject"],
|
|
59
|
+
"properties": {
|
|
60
|
+
"check": { "type": "string" },
|
|
61
|
+
"action": { "enum": ["block", "warn"] },
|
|
62
|
+
"command": { "type": "string", "description": "The command that produces the evidence." },
|
|
63
|
+
"status": { "enum": ["current", "stale", "missing", "overridden"] },
|
|
64
|
+
"code": {
|
|
65
|
+
"enum": ["ref-evidence-missing", "ref-evidence-stale", "envelope-invalid", "envelope-untrusted", "evidence-payload-type", "evidence-statement-invalid", "evidence-runner-mismatch"],
|
|
66
|
+
"description": "Why the check has no current evidence."
|
|
67
|
+
},
|
|
68
|
+
"reason": { "type": "string" },
|
|
69
|
+
"runner": { "type": "string", "description": "The runner whose evidence counted." },
|
|
70
|
+
"subject": { "type": "string", "description": "The subject an override of this check names: ref-gate-<sha12>-<check>." },
|
|
71
|
+
"override": {
|
|
72
|
+
"type": "object",
|
|
73
|
+
"required": ["subject", "by", "at", "reason", "digest"],
|
|
74
|
+
"properties": {
|
|
75
|
+
"subject": { "type": "string" },
|
|
76
|
+
"by": { "type": "string" },
|
|
77
|
+
"at": { "type": "string" },
|
|
78
|
+
"reason": { "type": "string" },
|
|
79
|
+
"digest": { "type": "string" }
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which of the plan job's two named checks, `plan` and `policy`, passed
|
|
3
|
+
* (#4870), read from what the plan job hands its note job, and the line the
|
|
4
|
+
* plan note gets saying which were signed.
|
|
5
|
+
*
|
|
6
|
+
* The policy (Rego, CrossGuard, a product's rules) is evaluated inside the
|
|
7
|
+
* plan, so one job stands for both checks:
|
|
8
|
+
*
|
|
9
|
+
* - `plan` passed when the plan job succeeded and left a report.
|
|
10
|
+
* - `policy` passed when a policy ran and denied nothing that stands: a
|
|
11
|
+
* `pr-plan.json` whose `policy.result` is `passed` or `held`, or `denied`
|
|
12
|
+
* with an override; a plan note hand-off whose waves carry the policy's
|
|
13
|
+
* word and whose every denial has its override. A plan that ran no policy
|
|
14
|
+
* signs no `policy`.
|
|
15
|
+
*
|
|
16
|
+
* Kept apart from ./check-evidence.ts so `pr-post` reads the note file
|
|
17
|
+
* without loading the signer.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
21
|
+
import { join } from "node:path";
|
|
22
|
+
|
|
23
|
+
/** The variable a generated job hands the runner key in, as PEM (the same one the tick and `chant ci run` read). */
|
|
24
|
+
export const CHECK_EVIDENCE_KEY_ENV = "CHANT_RUNNER_KEY";
|
|
25
|
+
|
|
26
|
+
/** The variable a developer's or an agent's local evidence key is read from, as PEM (#4868). A check hook never sees it. */
|
|
27
|
+
export const LOCAL_EVIDENCE_KEY_ENV = "CHANT_LOCAL_EVIDENCE_KEY";
|
|
28
|
+
|
|
29
|
+
/** The file the signing step writes beside the plan report, which `pr-post` reads. */
|
|
30
|
+
export const CHECK_EVIDENCE_FILE = "evidence.json";
|
|
31
|
+
|
|
32
|
+
/** What the signing step did, for the note and the log. */
|
|
33
|
+
export interface CheckEvidenceNote {
|
|
34
|
+
signed: Array<{ check: string; runner: string; commit: string; tree: string }>;
|
|
35
|
+
skipped: Array<{ check: string; reason: string }>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const REPORT_FILE = "pr-plan.json";
|
|
39
|
+
const HANDOFF_FILE = "pr-plan-note.json";
|
|
40
|
+
|
|
41
|
+
function readJson(path: string): Record<string, unknown> | undefined {
|
|
42
|
+
try {
|
|
43
|
+
const v = JSON.parse(readFileSync(path, "utf-8")) as unknown;
|
|
44
|
+
return v !== null && typeof v === "object" && !Array.isArray(v) ? (v as Record<string, unknown>) : undefined;
|
|
45
|
+
} catch {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The policy's verdict in a report: passed, denied, or none ran. */
|
|
51
|
+
function policyOf(dir: string): { passed: boolean; reason: string } | null {
|
|
52
|
+
const report = existsSync(join(dir, REPORT_FILE)) ? readJson(join(dir, REPORT_FILE)) : undefined;
|
|
53
|
+
if (report) {
|
|
54
|
+
const p = report.policy as { result?: string; override?: unknown } | undefined;
|
|
55
|
+
if (!p || typeof p.result !== "string") return null;
|
|
56
|
+
if (p.result === "passed" || p.result === "held") return { passed: true, reason: `the policy ${p.result === "held" ? "held the plan for an approval and denied nothing" : "passed"}` };
|
|
57
|
+
if (p.result === "denied" && p.override) return { passed: true, reason: "the policy denied the plan and an override stands" };
|
|
58
|
+
return { passed: false, reason: p.result === "error" ? "the policy did not run or gave no readable answer" : "the policy denied the plan" };
|
|
59
|
+
}
|
|
60
|
+
const handoff = existsSync(join(dir, HANDOFF_FILE)) ? readJson(join(dir, HANDOFF_FILE)) : undefined;
|
|
61
|
+
const note = handoff?.note as { waves?: Array<{ policy?: unknown }>; overrides?: Array<{ override?: unknown }> } | undefined;
|
|
62
|
+
if (!note) return null;
|
|
63
|
+
const overrides = note.overrides ?? [];
|
|
64
|
+
const ran = overrides.length > 0 || (note.waves ?? []).some((w) => w.policy !== undefined);
|
|
65
|
+
if (!ran) return null;
|
|
66
|
+
const standing = overrides.filter((o) => !o.override).length;
|
|
67
|
+
return standing === 0 ? { passed: true, reason: "the policy denied nothing that stands" } : { passed: false, reason: `the policy denied ${standing} unit${standing === 1 ? "" : "s"} with no override` };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The checks to sign from the plan job's hand-off directory and result, and those left unsigned with why. */
|
|
71
|
+
export function planReportChecks(dir: string, result: string | undefined): { sign: Array<{ check: string; claim?: Buffer }>; skipped: CheckEvidenceNote["skipped"] } {
|
|
72
|
+
const file = [REPORT_FILE, HANDOFF_FILE].map((f) => join(dir, f)).find((f) => existsSync(f));
|
|
73
|
+
if (result !== "success") {
|
|
74
|
+
const why = `the plan job's result is ${result ?? "not given"}`;
|
|
75
|
+
return { sign: [], skipped: [{ check: "plan", reason: why }, { check: "policy", reason: why }] };
|
|
76
|
+
}
|
|
77
|
+
if (!file) {
|
|
78
|
+
const why = `the plan job left no ${REPORT_FILE} or ${HANDOFF_FILE}`;
|
|
79
|
+
return { sign: [], skipped: [{ check: "plan", reason: why }, { check: "policy", reason: why }] };
|
|
80
|
+
}
|
|
81
|
+
const claim = readFileSync(file);
|
|
82
|
+
const sign: Array<{ check: string; claim?: Buffer }> = [{ check: "plan", claim }];
|
|
83
|
+
const skipped: CheckEvidenceNote["skipped"] = [];
|
|
84
|
+
const policy = policyOf(dir);
|
|
85
|
+
if (policy === null) skipped.push({ check: "policy", reason: "the plan ran no policy" });
|
|
86
|
+
else if (policy.passed) sign.push({ check: "policy", claim });
|
|
87
|
+
else skipped.push({ check: "policy", reason: policy.reason });
|
|
88
|
+
return { sign, skipped };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The line the plan note ends with when the signing step wrote
|
|
93
|
+
* {@link CHECK_EVIDENCE_FILE} in `dir`, or undefined when it did not.
|
|
94
|
+
*/
|
|
95
|
+
export function checkEvidenceNoteLine(dir: string): string | undefined {
|
|
96
|
+
const doc = readJson(join(dir, CHECK_EVIDENCE_FILE)) as Partial<CheckEvidenceNote> | undefined;
|
|
97
|
+
if (!doc || !Array.isArray(doc.signed) || !Array.isArray(doc.skipped)) return undefined;
|
|
98
|
+
const signed = doc.signed.map((s) => `\`${s.check}\``);
|
|
99
|
+
const skipped = doc.skipped.map((s) => `\`${s.check}\` (${s.reason})`);
|
|
100
|
+
if (signed.length === 0 && skipped.length === 0) return undefined;
|
|
101
|
+
const parts = [
|
|
102
|
+
...(signed.length > 0 ? [`signed evidence for ${signed.join(", ")} as ${doc.signed[0]!.runner}`] : []),
|
|
103
|
+
...(skipped.length > 0 ? [`not signed: ${skipped.join(", ")}`] : []),
|
|
104
|
+
];
|
|
105
|
+
return `Checks: ${parts.join("; ")}.`;
|
|
106
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #4870: evidence for each named check, attached as git notes where the ref
|
|
3
|
+
* gate (#4867) reads it, found by the commit's tree and verified against the
|
|
4
|
+
* runner keys at base. The attacker
|
|
5
|
+
* controls the change: a runner key the change adds, a tree that differs
|
|
6
|
+
* from the one signed, and a person's key posing as a runner's.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { execFileSync } from "node:child_process";
|
|
10
|
+
import { generateKeyPairSync } from "node:crypto";
|
|
11
|
+
import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { describe, expect, test } from "vitest";
|
|
15
|
+
import { loadRunnerKey } from "./dsse";
|
|
16
|
+
import { checkStatement, missingCheckEvidence, readCheckEvidence, recordChecks, runnerKeyEvidenceSigner, signChecks } from "./check-evidence";
|
|
17
|
+
import { CHECK_EVIDENCE_FILE, checkEvidenceNoteLine, planReportChecks } from "./check-evidence-report";
|
|
18
|
+
|
|
19
|
+
function git(cwd: string, ...args: string[]): string {
|
|
20
|
+
return execFileSync("git", ["-c", "user.name=t", "-c", "user.email=t@example.invalid", "-c", "init.defaultBranch=main", ...args], { cwd, encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"] }).trim();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function runnerKey(): { pem: string; pub: string } {
|
|
24
|
+
const { privateKey } = generateKeyPairSync("ed25519");
|
|
25
|
+
const pem = privateKey.export({ format: "pem", type: "pkcs8" }).toString();
|
|
26
|
+
return { pem, pub: loadRunnerKey(pem).publicKey };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function trust(runners: Array<{ principal: string; key: string }>): string {
|
|
30
|
+
return JSON.stringify({ schema: 1, runners: runners.map((r) => ({ ...r, class: "runner" })) });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A repository whose base lists `ci`'s key as a runner, and a change on top of it. */
|
|
34
|
+
function repo(): { dir: string; base: string; head: string; ci: { pem: string; pub: string } } {
|
|
35
|
+
const dir = mkdtempSync(join(tmpdir(), "chant-4870-"));
|
|
36
|
+
const ci = runnerKey();
|
|
37
|
+
git(dir, "init", "--quiet");
|
|
38
|
+
mkdirSync(join(dir, ".chant"));
|
|
39
|
+
writeFileSync(join(dir, ".chant/trust.json"), trust([{ principal: "ci", key: ci.pub }]));
|
|
40
|
+
writeFileSync(join(dir, "main.tf"), "# base\n");
|
|
41
|
+
git(dir, "add", ".");
|
|
42
|
+
git(dir, "commit", "--quiet", "-m", "base");
|
|
43
|
+
const base = git(dir, "rev-parse", "HEAD");
|
|
44
|
+
writeFileSync(join(dir, "main.tf"), "# change\n");
|
|
45
|
+
git(dir, "commit", "--quiet", "-am", "change");
|
|
46
|
+
return { dir, base, head: git(dir, "rev-parse", "HEAD"), ci };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
describe("check evidence (#4870)", () => {
|
|
50
|
+
test("a signed check is current for its commit, and for another commit with the same tree", async () => {
|
|
51
|
+
const r = repo();
|
|
52
|
+
const signer = runnerKeyEvidenceSigner(r.dir, r.base, r.ci.pem);
|
|
53
|
+
const signed = signChecks(r.dir, r.head, [{ check: "plan", claim: "{}" }, { check: "policy" }], signer);
|
|
54
|
+
expect(signed.map((s) => s.check)).toEqual(["plan", "policy"]);
|
|
55
|
+
await recordChecks(r.dir, signed, { push: false });
|
|
56
|
+
expect(await readCheckEvidence(r.dir, r.head, r.base, ["plan", "policy"])).toEqual([
|
|
57
|
+
{ check: "plan", status: "current", runner: "ci", commit: r.head },
|
|
58
|
+
{ check: "policy", status: "current", runner: "ci", commit: r.head },
|
|
59
|
+
]);
|
|
60
|
+
// A squash of the same change onto the base: another commit, the same tree.
|
|
61
|
+
git(r.dir, "checkout", "--quiet", r.base);
|
|
62
|
+
git(r.dir, "checkout", "--quiet", r.head, "--", "main.tf");
|
|
63
|
+
git(r.dir, "commit", "--quiet", "-m", "squashed");
|
|
64
|
+
const squash = git(r.dir, "rev-parse", "HEAD");
|
|
65
|
+
expect(squash).not.toBe(r.head);
|
|
66
|
+
expect((await readCheckEvidence(r.dir, squash, r.base, ["plan"]))[0]).toMatchObject({ status: "current", commit: r.head });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("a commit whose tree differs has none, and the refusal names each missing check and its command", async () => {
|
|
70
|
+
const r = repo();
|
|
71
|
+
await recordChecks(r.dir, signChecks(r.dir, r.head, [{ check: "plan" }], runnerKeyEvidenceSigner(r.dir, r.base, r.ci.pem)), { push: false });
|
|
72
|
+
expect((await readCheckEvidence(r.dir, r.base, r.base, ["plan"]))[0]!.status).toBe("missing");
|
|
73
|
+
const refused = await missingCheckEvidence(r.dir, r.head, r.base, ["plan", "policy"]);
|
|
74
|
+
expect(refused).toContain("policy");
|
|
75
|
+
expect(refused).toContain("chant ci evidence sign --check-id policy");
|
|
76
|
+
expect(refused).not.toMatch(/\bplan \(/);
|
|
77
|
+
expect(await missingCheckEvidence(r.dir, r.head, r.base, ["plan"])).toBeNull();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("a runner key the change adds signs nothing the base trusts", async () => {
|
|
81
|
+
const r = repo();
|
|
82
|
+
const added = runnerKey();
|
|
83
|
+
writeFileSync(join(r.dir, ".chant/trust.json"), trust([{ principal: "ci", key: r.ci.pub }, { principal: "mine", key: added.pub }]));
|
|
84
|
+
git(r.dir, "commit", "--quiet", "-am", "add my key");
|
|
85
|
+
const head = git(r.dir, "rev-parse", "HEAD");
|
|
86
|
+
expect(() => runnerKeyEvidenceSigner(r.dir, r.base, added.pem)).toThrow(/lists no runner with this key/);
|
|
87
|
+
// Signed against the change's own policy, and kept: the base still does not trust it.
|
|
88
|
+
await recordChecks(r.dir, signChecks(r.dir, head, [{ check: "plan" }], runnerKeyEvidenceSigner(r.dir, head, added.pem)), { push: false });
|
|
89
|
+
expect((await readCheckEvidence(r.dir, head, r.base, ["plan"]))[0]).toMatchObject({ status: "untrusted" });
|
|
90
|
+
expect(await readCheckEvidence(r.dir, head, head, ["plan"])).toEqual([{ check: "plan", status: "current", runner: "mine", commit: head }]);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("the statement binds the commit, its tree and the check's name, and no record", () => {
|
|
94
|
+
const s = checkStatement({ commit: "a".repeat(40), tree: "b".repeat(40), check: "check-root:prod", runner: "ci", claim: "x" });
|
|
95
|
+
expect(s.subject).toEqual([]);
|
|
96
|
+
expect(s.predicate).toMatchObject({ commit: "a".repeat(40), tree: "b".repeat(40), check: "check-root:prod", runner: "ci" });
|
|
97
|
+
expect(s.predicate.claim?.sha256).toMatch(/^[0-9a-f]{64}$/);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe("the plan job's two checks (#4870)", () => {
|
|
102
|
+
const dir = (files: Record<string, unknown>): string => {
|
|
103
|
+
const d = mkdtempSync(join(tmpdir(), "chant-4870-report-"));
|
|
104
|
+
for (const [name, body] of Object.entries(files)) writeFileSync(join(d, name), JSON.stringify(body));
|
|
105
|
+
return d;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
test("plan and policy from a pr-plan.json whose policy passed; only plan when it denied or ran none", () => {
|
|
109
|
+
expect(planReportChecks(dir({ "pr-plan.json": { policy: { result: "passed" } } }), "success").sign.map((c) => c.check)).toEqual(["plan", "policy"]);
|
|
110
|
+
const denied = planReportChecks(dir({ "pr-plan.json": { policy: { result: "denied" } } }), "success");
|
|
111
|
+
expect(denied.sign.map((c) => c.check)).toEqual(["plan"]);
|
|
112
|
+
expect(denied.skipped).toEqual([{ check: "policy", reason: "the policy denied the plan" }]);
|
|
113
|
+
expect(planReportChecks(dir({ "pr-plan.json": { policy: { result: "denied", override: { by: "a" } } } }), "success").sign.map((c) => c.check)).toEqual(["plan", "policy"]);
|
|
114
|
+
expect(planReportChecks(dir({ "pr-plan.json": {} }), "success").skipped).toEqual([{ check: "policy", reason: "the plan ran no policy" }]);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("a plan note hand-off: policy passes when every denial has its override", () => {
|
|
118
|
+
const handoff = (overrides: unknown[]) => ({ "pr-plan-note.json": { kind: "plan-note-handoff", note: { waves: [{ policy: { outcome: "held", rules: [] } }], overrides } } });
|
|
119
|
+
expect(planReportChecks(dir(handoff([{ member: "a", rules: ["r"], override: { by: "x" }, overriders: [] }])), "success").sign.map((c) => c.check)).toEqual(["plan", "policy"]);
|
|
120
|
+
expect(planReportChecks(dir(handoff([{ member: "a", rules: ["r"], overriders: [] }])), "success").sign.map((c) => c.check)).toEqual(["plan"]);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("a failed plan job signs neither, and the note line says which were signed", () => {
|
|
124
|
+
const failed = planReportChecks(dir({ "pr-plan.json": { policy: { result: "passed" } } }), "failure");
|
|
125
|
+
expect(failed.sign).toEqual([]);
|
|
126
|
+
expect(failed.skipped.map((s) => s.check)).toEqual(["plan", "policy"]);
|
|
127
|
+
const d = dir({});
|
|
128
|
+
expect(checkEvidenceNoteLine(d)).toBeUndefined();
|
|
129
|
+
writeFileSync(join(d, CHECK_EVIDENCE_FILE), JSON.stringify({ signed: [{ check: "plan", runner: "ci", commit: "c", tree: "t" }], skipped: [{ check: "policy", reason: "the policy denied the plan" }] }));
|
|
130
|
+
expect(checkEvidenceNoteLine(d)).toBe("Checks: signed evidence for `plan` as ci; not signed: `policy` (the policy denied the plan).");
|
|
131
|
+
expect(readFileSync(join(d, CHECK_EVIDENCE_FILE), "utf-8")).toContain("plan");
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evidence for each named check a generated pipeline runs (#4870).
|
|
3
|
+
*
|
|
4
|
+
* A check job (`chant ci check`, the plan job's note job, a wave job) signs,
|
|
5
|
+
* for each check that passed, the runner evidence statement of #2553
|
|
6
|
+
* (./evidence.ts): the commit, its tree, the check's name, and the digest of
|
|
7
|
+
* what the check said (`claim`) when there is one. No record is a subject:
|
|
8
|
+
* a check reads the tree, and the statement binds the tree.
|
|
9
|
+
*
|
|
10
|
+
* The envelope is attached where #4867's ref gate reads evidence: a line
|
|
11
|
+
* of the commit's git note under `refs/notes/chant/evidence`
|
|
12
|
+
* (`EVIDENCE_NOTES_REF`, ./ref-gate.ts), which the job then pushes. The
|
|
13
|
+
* statement names no record, so it counts for the tree it was signed over:
|
|
14
|
+
* {@link readCheckEvidence} also reads the notes of other commits with the
|
|
15
|
+
* same tree, so the squash of a pull request whose tree is the head's finds
|
|
16
|
+
* the evidence the head was given, as #4868's rebase that keeps the tree
|
|
17
|
+
* will.
|
|
18
|
+
*
|
|
19
|
+
* Who signs is an {@link EvidenceSigner}: today a runner key the policy at
|
|
20
|
+
* base lists ({@link runnerKeyEvidenceSigner}, the key `CHANT_RUNNER_KEY`
|
|
21
|
+
* holds, as for the tick's tags and the run records). #4869 adds a signer
|
|
22
|
+
* that trades the job's OIDC token for a short-lived certificate and returns
|
|
23
|
+
* the same envelope with the certificate in it; nothing that calls a signer
|
|
24
|
+
* changes.
|
|
25
|
+
*
|
|
26
|
+
* Reading ({@link readCheckEvidence}) verifies each envelope against the
|
|
27
|
+
* runner keys of `.chant/trust.json` at base, so evidence a change signs
|
|
28
|
+
* with a key it adds itself does not count until that key is merged.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { execFileSync } from "node:child_process";
|
|
32
|
+
import { createHash } from "node:crypto";
|
|
33
|
+
import { canonicalJson } from "../../effect-receipt";
|
|
34
|
+
import { gitRevisionSource } from "../record-source";
|
|
35
|
+
import { IN_TOTO_PAYLOAD_TYPE, loadRunnerKey, signEnvelope, type DsseEnvelope } from "./dsse";
|
|
36
|
+
import { RUNNER_EVIDENCE_PREDICATE, STATEMENT_TYPE, verifyEvidence, type EvidenceStatement } from "./evidence";
|
|
37
|
+
import { readTrustPolicy } from "./policy";
|
|
38
|
+
import { attachEvidence, EVIDENCE_NOTES_REF, parseEvidenceNote } from "./ref-gate";
|
|
39
|
+
|
|
40
|
+
export { CHECK_EVIDENCE_KEY_ENV } from "./check-evidence-report";
|
|
41
|
+
|
|
42
|
+
export { EVIDENCE_NOTES_REF };
|
|
43
|
+
|
|
44
|
+
/** The named checks the plan job stands for (#4870): its plan, and the policy evaluated inside it. */
|
|
45
|
+
export const PLAN_EVIDENCE_CHECKS = ["plan", "policy"] as const;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Who signs a check's statement (#4870). `sign` returns the DSSE envelope
|
|
49
|
+
* of the in-toto payload it is given. The seam #4869's keyless signer plugs
|
|
50
|
+
* into.
|
|
51
|
+
*/
|
|
52
|
+
export interface EvidenceSigner {
|
|
53
|
+
/** The runner principal the statement names, as `.chant/trust.json` at base lists it. */
|
|
54
|
+
principal: string;
|
|
55
|
+
sign(payloadType: string, payload: Buffer): DsseEnvelope;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The signer a runner key makes: the principal `.chant/trust.json` at
|
|
60
|
+
* `base` lists the key under `runners`. Throws when the key is not an
|
|
61
|
+
* Ed25519 PEM key, is a signer's (a person's) key, or is not listed, so a
|
|
62
|
+
* job never keeps evidence its readers would ignore.
|
|
63
|
+
*/
|
|
64
|
+
export function runnerKeyEvidenceSigner(repo: string, base: string, pem: string): EvidenceSigner {
|
|
65
|
+
let loaded: ReturnType<typeof loadRunnerKey>;
|
|
66
|
+
try {
|
|
67
|
+
loaded = loadRunnerKey(pem);
|
|
68
|
+
} catch (err) {
|
|
69
|
+
throw new Error(`the runner key is not an Ed25519 private key in PEM: ${err instanceof Error ? err.message : String(err)}`);
|
|
70
|
+
}
|
|
71
|
+
const policy = readTrustPolicy(gitRevisionSource(repo, base), base);
|
|
72
|
+
if (policy.problems.length > 0) throw new Error(`the policy at ${base.slice(0, 12)} cannot be read: ${policy.problems.join("; ")}`);
|
|
73
|
+
if (policy.signers.some((s) => s.key === loaded.publicKey)) {
|
|
74
|
+
throw new Error(`the runner key is a signer's key in ${policy.signersPath}; check evidence is signed by a CI identity's key, never by a person's`);
|
|
75
|
+
}
|
|
76
|
+
const listed = policy.runners.find((r) => r.key === loaded.publicKey);
|
|
77
|
+
if (!listed) throw new Error(`.chant/trust.json at ${base.slice(0, 12)} lists no runner with this key, so the evidence would be ignored. Add it under "runners" first:\n ${loaded.publicKey}`);
|
|
78
|
+
return { principal: listed.principal, sign: (payloadType, payload) => signEnvelope(payloadType, payload, loaded.key, loaded.publicKey) };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function git(repo: string, args: string[]): string {
|
|
82
|
+
return execFileSync("git", args, { cwd: repo, encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"] }).trim();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The full commit id and tree of `rev`. Throws when `rev` names no commit. */
|
|
86
|
+
export function commitAndTree(repo: string, rev: string): { commit: string; tree: string } {
|
|
87
|
+
const commit = git(repo, ["rev-parse", "--verify", "--quiet", `${rev}^{commit}`]);
|
|
88
|
+
return { commit, tree: git(repo, ["rev-parse", `${commit}^{tree}`]) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The check's statement over `commit`, with the digest of what it said when given. */
|
|
92
|
+
export function checkStatement(input: { commit: string; tree: string; check: string; runner: string; claim?: Buffer | string }): EvidenceStatement {
|
|
93
|
+
if (!input.check.trim()) throw new Error("a check's evidence needs the check's name");
|
|
94
|
+
return {
|
|
95
|
+
_type: STATEMENT_TYPE,
|
|
96
|
+
subject: [],
|
|
97
|
+
predicateType: RUNNER_EVIDENCE_PREDICATE,
|
|
98
|
+
predicate: {
|
|
99
|
+
runner: input.runner,
|
|
100
|
+
commit: input.commit,
|
|
101
|
+
tree: input.tree,
|
|
102
|
+
check: input.check,
|
|
103
|
+
...(input.claim !== undefined ? { claim: { sha256: createHash("sha256").update(input.claim).digest("hex") } } : {}),
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** One check signed. */
|
|
109
|
+
export interface SignedCheck {
|
|
110
|
+
check: string;
|
|
111
|
+
commit: string;
|
|
112
|
+
tree: string;
|
|
113
|
+
runner: string;
|
|
114
|
+
envelope: DsseEnvelope;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Sign each of `checks` over `at` with `signer`. Writes nothing. */
|
|
118
|
+
export function signChecks(repo: string, at: string, checks: readonly { check: string; claim?: Buffer | string }[], signer: EvidenceSigner): SignedCheck[] {
|
|
119
|
+
const { commit, tree } = commitAndTree(repo, at);
|
|
120
|
+
return checks.map(({ check, claim }) => {
|
|
121
|
+
const statement = checkStatement({ commit, tree, check, runner: signer.principal, ...(claim !== undefined ? { claim } : {}) });
|
|
122
|
+
return {
|
|
123
|
+
check,
|
|
124
|
+
commit,
|
|
125
|
+
tree,
|
|
126
|
+
runner: signer.principal,
|
|
127
|
+
envelope: signer.sign(IN_TOTO_PAYLOAD_TYPE, Buffer.from(canonicalJson(statement), "utf8")),
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** How many times {@link recordChecks} fetches, attaches and pushes when another job pushed the notes first. */
|
|
133
|
+
const NOTES_PUSH_TRIES = 3;
|
|
134
|
+
|
|
135
|
+
function tryGit(repo: string, args: string[]): string | undefined {
|
|
136
|
+
try {
|
|
137
|
+
return git(repo, args);
|
|
138
|
+
} catch {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Attach each envelope to its commit's evidence note and push
|
|
145
|
+
* `refs/notes/chant/evidence` to `origin`. The remote's notes are fetched
|
|
146
|
+
* first, so the push adds to them; a push another job beat is tried again
|
|
147
|
+
* on the newer notes. Returns why the push did not land, when it did not;
|
|
148
|
+
* the local notes hold the envelopes either way.
|
|
149
|
+
*/
|
|
150
|
+
export async function recordChecks(repo: string, signed: readonly SignedCheck[], opts: { push?: boolean; remote?: string; notesRef?: string } = {}): Promise<{ pushed: boolean; pushWarning?: string }> {
|
|
151
|
+
if (signed.length === 0) return { pushed: false };
|
|
152
|
+
const push = opts.push !== false;
|
|
153
|
+
const remote = opts.remote ?? "origin";
|
|
154
|
+
// #4868: local evidence goes under a notes ref of its own.
|
|
155
|
+
const notesRef = opts.notesRef ?? EVIDENCE_NOTES_REF;
|
|
156
|
+
const hasRemote = push && tryGit(repo, ["remote", "get-url", remote]) !== undefined;
|
|
157
|
+
if (!hasRemote) {
|
|
158
|
+
for (const s of signed) attachEvidence(repo, s.commit, s.envelope, notesRef);
|
|
159
|
+
return push ? { pushed: false, pushWarning: `no remote ${remote}, so the evidence is attached here only` } : { pushed: false };
|
|
160
|
+
}
|
|
161
|
+
let said = "";
|
|
162
|
+
for (let attempt = 1; attempt <= NOTES_PUSH_TRIES; attempt++) {
|
|
163
|
+
// The remote's notes are the ones to add to; a CI checkout has none of its own to keep.
|
|
164
|
+
if (tryGit(repo, ["fetch", "--quiet", "--no-tags", remote, `+${notesRef}:${notesRef}`]) === undefined && attempt > 1) break;
|
|
165
|
+
for (const s of signed) attachEvidence(repo, s.commit, s.envelope, notesRef);
|
|
166
|
+
try {
|
|
167
|
+
execFileSync("git", ["push", "--quiet", remote, `${notesRef}:${notesRef}`], { cwd: repo, encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"] });
|
|
168
|
+
return { pushed: true };
|
|
169
|
+
} catch (err) {
|
|
170
|
+
said = String((err as { stderr?: unknown }).stderr ?? (err instanceof Error ? err.message : err)).trim().split("\n")[0] ?? "";
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return { pushed: false, pushWarning: `git push ${remote} ${notesRef} did not land${said ? ` (git said: ${said})` : ""}` };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Every envelope attached to a commit whose tree is `tree`: `commit`'s own first. Read from `notesRef`
|
|
178
|
+
* (default {@link EVIDENCE_NOTES_REF}; #4868 reads local evidence from its own).
|
|
179
|
+
*/
|
|
180
|
+
export function envelopesForTree(repo: string, commit: string, tree: string, notesRef: string = EVIDENCE_NOTES_REF): unknown[] {
|
|
181
|
+
const list = tryGit(repo, ["notes", `--ref=${notesRef}`, "list"]);
|
|
182
|
+
if (!list) return [];
|
|
183
|
+
const own: unknown[] = [];
|
|
184
|
+
const others: unknown[] = [];
|
|
185
|
+
for (const line of list.split("\n")) {
|
|
186
|
+
const [blob, annotated] = line.trim().split(/\s+/);
|
|
187
|
+
if (!blob || !annotated) continue;
|
|
188
|
+
const same = annotated === commit || tryGit(repo, ["rev-parse", "--verify", "--quiet", `${annotated}^{tree}`]) === tree;
|
|
189
|
+
if (!same) continue;
|
|
190
|
+
const text = tryGit(repo, ["cat-file", "blob", blob]);
|
|
191
|
+
if (text !== undefined) (annotated === commit ? own : others).push(...parseEvidenceNote(text));
|
|
192
|
+
}
|
|
193
|
+
return [...own, ...others];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** What the evidence notes hold for one check at a commit's tree. */
|
|
197
|
+
export type CheckEvidenceVerdict =
|
|
198
|
+
| { check: string; status: "current"; runner: string; commit: string }
|
|
199
|
+
| { check: string; status: "missing" | "untrusted"; reason: string };
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Read and verify the evidence for each of `checks` at `at`'s tree, against
|
|
203
|
+
* the runner keys `.chant/trust.json` lists at `base`. `missing`: nothing is
|
|
204
|
+
* kept for the tree. `untrusted`: an envelope is kept, and no runner key at
|
|
205
|
+
* base signed it, or it is not a check statement for that tree.
|
|
206
|
+
*/
|
|
207
|
+
export async function readCheckEvidence(repo: string, at: string, base: string, checks: readonly string[]): Promise<CheckEvidenceVerdict[]> {
|
|
208
|
+
const { tree, commit } = commitAndTree(repo, at);
|
|
209
|
+
const policy = readTrustPolicy(gitRevisionSource(repo, base), base);
|
|
210
|
+
const envelopes = envelopesForTree(repo, commit, tree);
|
|
211
|
+
return checks.map((check): CheckEvidenceVerdict => {
|
|
212
|
+
let untrusted: string | undefined;
|
|
213
|
+
for (const envelope of envelopes) {
|
|
214
|
+
const v = verifyEvidence(envelope, policy.runners, repo, commit);
|
|
215
|
+
if (!v.ok) {
|
|
216
|
+
if (claimedCheck(envelope) === check) untrusted ??= `the evidence attached for check ${check} does not verify against the runner keys at ${base.slice(0, 12)}: ${v.reason}`;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (v.statement.predicate.check !== check || v.statement.predicate.tree !== tree) continue;
|
|
220
|
+
if (v.status === "current") return { check, status: "current", runner: v.runner, commit: v.statement.predicate.commit };
|
|
221
|
+
}
|
|
222
|
+
if (untrusted) return { check, status: "untrusted", reason: untrusted };
|
|
223
|
+
return { check, status: "missing", reason: `no evidence for check ${check} is attached under ${EVIDENCE_NOTES_REF} to a commit with the tree of ${commit.slice(0, 12)}` };
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The check an envelope says it is for, read without verifying, so a refusal can name it. */
|
|
228
|
+
function claimedCheck(envelope: unknown): string | undefined {
|
|
229
|
+
try {
|
|
230
|
+
const payload = (envelope as { payload?: unknown }).payload;
|
|
231
|
+
if (typeof payload !== "string") return undefined;
|
|
232
|
+
const check = (JSON.parse(Buffer.from(payload, "base64").toString("utf8")) as { predicate?: { check?: unknown } }).predicate?.check;
|
|
233
|
+
return typeof check === "string" ? check : undefined;
|
|
234
|
+
} catch {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** The command that produces a check's evidence, for a refusal to name. */
|
|
240
|
+
export function checkEvidenceCommand(check: string): string {
|
|
241
|
+
return `chant ci evidence sign --check-id ${check}`;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Why `at` is refused for want of evidence, or null when every one of
|
|
246
|
+
* `required` has current evidence. Names each missing check and the
|
|
247
|
+
* command that produces it.
|
|
248
|
+
*/
|
|
249
|
+
export async function missingCheckEvidence(repo: string, at: string, base: string, required: readonly string[]): Promise<string | null> {
|
|
250
|
+
if (required.length === 0) return null;
|
|
251
|
+
const verdicts = await readCheckEvidence(repo, at, base, required);
|
|
252
|
+
const missing = verdicts.filter((v): v is Exclude<CheckEvidenceVerdict, { status: "current" }> => v.status !== "current");
|
|
253
|
+
if (missing.length === 0) return null;
|
|
254
|
+
return `${at.slice(0, 12)} lacks the evidence the policy at ${base.slice(0, 12)} requires: ${missing
|
|
255
|
+
.map((v) => `${v.check} (${v.reason}; run it in CI, where ${checkEvidenceCommand(v.check)} signs it)`)
|
|
256
|
+
.join("; ")}`;
|
|
257
|
+
}
|