@xemahq/edition-tooling 0.0.0-stage → 0.1.0

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.
Files changed (219) hide show
  1. package/LICENSE +175 -0
  2. package/dist/cli.d.ts +7 -0
  3. package/dist/cli.d.ts.map +1 -0
  4. package/dist/cli.js +47 -0
  5. package/dist/cli.js.map +1 -0
  6. package/dist/commands/edition.d.ts +4 -0
  7. package/dist/commands/edition.d.ts.map +1 -0
  8. package/dist/commands/edition.js +58 -0
  9. package/dist/commands/edition.js.map +1 -0
  10. package/dist/edition/verify-image-evidence.bundle.mjs +72104 -0
  11. package/dist/edition/verify-image-evidence.bundle.provenance.json +4119 -0
  12. package/dist/lib/exit-codes.d.ts +6 -0
  13. package/dist/lib/exit-codes.d.ts.map +1 -0
  14. package/dist/lib/exit-codes.js +21 -0
  15. package/dist/lib/exit-codes.js.map +1 -0
  16. package/dist/runtime/edition/run-entry.d.ts +10 -0
  17. package/dist/runtime/edition/run-entry.d.ts.map +1 -0
  18. package/dist/runtime/edition/run-entry.js +49 -0
  19. package/dist/runtime/edition/run-entry.js.map +1 -0
  20. package/dist/runtime/edition/verb-map.d.ts +70 -0
  21. package/dist/runtime/edition/verb-map.d.ts.map +1 -0
  22. package/dist/runtime/edition/verb-map.js +166 -0
  23. package/dist/runtime/edition/verb-map.js.map +1 -0
  24. package/edition/README.md +291 -0
  25. package/edition/adapter-bundled-versions.mjs +128 -0
  26. package/edition/adapter-host-versions.mjs +76 -0
  27. package/edition/assemble-producer-input.mjs +1423 -0
  28. package/edition/assemble-third-party-input.mjs +348 -0
  29. package/edition/authored-source-check.mjs +141 -0
  30. package/edition/authority-binding.mjs +23 -0
  31. package/edition/authorization-base.mjs +357 -0
  32. package/edition/biome-content.mjs +327 -0
  33. package/edition/build-biome-index.mjs +180 -0
  34. package/edition/build-ledger-completeness.mjs +67 -0
  35. package/edition/build-ledger.mjs +107 -0
  36. package/edition/build-platform-service-index.mjs +292 -0
  37. package/edition/build-runtime-image-source-index.mjs +1197 -0
  38. package/edition/built-verifier.mjs +55 -0
  39. package/edition/candidate-edition.mjs +735 -0
  40. package/edition/canonical-evidence-json.mjs +82 -0
  41. package/edition/canonical-repository-url.mjs +61 -0
  42. package/edition/catalogue-edition.mjs +49 -0
  43. package/edition/catalogue-evidence.mjs +228 -0
  44. package/edition/catalogue-fetch.mjs +124 -0
  45. package/edition/catalogue-lock-field.mjs +41 -0
  46. package/edition/catalogue-pin.mjs +71 -0
  47. package/edition/catalogue.mjs +237 -0
  48. package/edition/chart-set-source-commit.mjs +97 -0
  49. package/edition/check-committed-image-evidence.mjs +412 -0
  50. package/edition/checkout-indexed-source-repositories.mjs +283 -0
  51. package/edition/collected-refusals.mjs +59 -0
  52. package/edition/comment-blanker.mjs +75 -0
  53. package/edition/connector-source-authority.mjs +60 -0
  54. package/edition/contract-authority.mjs +122 -0
  55. package/edition/cosign-certificate-constraints.mjs +50 -0
  56. package/edition/cross-repo-build-inputs.mjs +158 -0
  57. package/edition/customer-definition.mjs +72 -0
  58. package/edition/customer-edition-verbs.mjs +610 -0
  59. package/edition/customer-flags.mjs +15 -0
  60. package/edition/definition-checkout.mjs +96 -0
  61. package/edition/develop-build-record.mjs +122 -0
  62. package/edition/develop-catalogue-free-resolve.mjs +282 -0
  63. package/edition/develop-catalogue-free-vendor.mjs +118 -0
  64. package/edition/develop-channel-evidence.mjs +23 -0
  65. package/edition/develop-channel-record.mjs +49 -0
  66. package/edition/develop-channel-satellites.mjs +65 -0
  67. package/edition/develop-chart-roster.mjs +96 -0
  68. package/edition/develop-chart-set.mjs +147 -0
  69. package/edition/develop-current-resolution.mjs +29 -0
  70. package/edition/develop-edition-invariants.mjs +142 -0
  71. package/edition/develop-edition-resolve.mjs +250 -0
  72. package/edition/develop-edition.mjs +332 -0
  73. package/edition/develop-engine-image.mjs +97 -0
  74. package/edition/develop-freeze.mjs +304 -0
  75. package/edition/develop-git-bundles.mjs +296 -0
  76. package/edition/develop-graph.mjs +34 -0
  77. package/edition/develop-image-coordinate.mjs +85 -0
  78. package/edition/develop-image-probe.mjs +122 -0
  79. package/edition/develop-image-record.mjs +260 -0
  80. package/edition/develop-resolve-report.mjs +159 -0
  81. package/edition/develop-select-records.mjs +90 -0
  82. package/edition/develop-selection-line.mjs +29 -0
  83. package/edition/develop-selection-pins.mjs +36 -0
  84. package/edition/develop-snapshot-inputs.mjs +112 -0
  85. package/edition/develop-store-root.mjs +20 -0
  86. package/edition/dispatched-runtime-components.mjs +172 -0
  87. package/edition/dist-check.mjs +1174 -0
  88. package/edition/distribution-evidence-set.mjs +1064 -0
  89. package/edition/distribution-paths.mjs +191 -0
  90. package/edition/edition-artifact.mjs +529 -0
  91. package/edition/edition-biome-manifests.mjs +85 -0
  92. package/edition/edition-ceiling.mjs +182 -0
  93. package/edition/edition-channel.mjs +27 -0
  94. package/edition/edition-chart-set.mjs +104 -0
  95. package/edition/edition-config.mjs +374 -0
  96. package/edition/edition-constants.mjs +110 -0
  97. package/edition/edition-contract-parse.mjs +179 -0
  98. package/edition/edition-contract.json +100 -0
  99. package/edition/edition-image-seal.mjs +115 -0
  100. package/edition/edition-kind.mjs +10 -0
  101. package/edition/edition-list.mjs +35 -0
  102. package/edition/edition-locks.mjs +78 -0
  103. package/edition/edition-pin-verbs.mjs +183 -0
  104. package/edition/edition-pin.mjs +17 -0
  105. package/edition/edition-pinned-inputs.mjs +116 -0
  106. package/edition/edition-protocol.mjs +285 -0
  107. package/edition/edition-publisher.mjs +21 -0
  108. package/edition/edition-seal.mjs +257 -0
  109. package/edition/edition-source-pins.mjs +211 -0
  110. package/edition/edition-unpin.mjs +16 -0
  111. package/edition/edition-workspace.mjs +205 -0
  112. package/edition/evidence-checkout.mjs +439 -0
  113. package/edition/evidence-grammars.mjs +18 -0
  114. package/edition/evidence-input-mode.mjs +136 -0
  115. package/edition/evidence-trust-pin.mjs +392 -0
  116. package/edition/evidence-trust-policy.mjs +374 -0
  117. package/edition/external-source-walk.mjs +486 -0
  118. package/edition/first-party-source-ids.mjs +62 -0
  119. package/edition/fleet-coordinate-finality.mjs +363 -0
  120. package/edition/fulcio-certificate-extensions.mjs +278 -0
  121. package/edition/git-json.mjs +54 -0
  122. package/edition/git-source-auth.mjs +131 -0
  123. package/edition/image-delivery-entitlement-descriptor.mjs +106 -0
  124. package/edition/image-evidence-replay-state.mjs +242 -0
  125. package/edition/image-evidence.mjs +1328 -0
  126. package/edition/image-lock-service-rows.mjs +26 -0
  127. package/edition/image-subject-identity.mjs +19 -0
  128. package/edition/images-from-record.mjs +216 -0
  129. package/edition/indexes-at-pins.mjs +13 -0
  130. package/edition/infra-images-lock.mjs +105 -0
  131. package/edition/kernel-capability-domain.mjs +30 -0
  132. package/edition/kernel-contract-authority.mjs +526 -0
  133. package/edition/kernel-contract-source-path-set.mjs +96 -0
  134. package/edition/kernel-package.mjs +237 -0
  135. package/edition/kernel-pin-source.mjs +63 -0
  136. package/edition/kernel-protocol-choice.mjs +239 -0
  137. package/edition/kernel-signer-kind.mjs +23 -0
  138. package/edition/lib.mjs +3322 -0
  139. package/edition/locked-bundle-signer.mjs +297 -0
  140. package/edition/map-with-concurrency.mjs +19 -0
  141. package/edition/no-follow-file.mjs +94 -0
  142. package/edition/oci-evidence-verbs.mjs +421 -0
  143. package/edition/oci-image-source-evidence.mjs +377 -0
  144. package/edition/operation-grant-admission.mjs +285 -0
  145. package/edition/operation-requirements.mjs +282 -0
  146. package/edition/oras-process.mjs +35 -0
  147. package/edition/oras.mjs +664 -0
  148. package/edition/pin-source.mjs +242 -0
  149. package/edition/pinned-cosign.mjs +86 -0
  150. package/edition/plan-image-evidence-refresh.mjs +428 -0
  151. package/edition/platform-lock-readability.mjs +903 -0
  152. package/edition/platform-service-class.mjs +85 -0
  153. package/edition/platform-service-ownership.mjs +91 -0
  154. package/edition/platform-service-source-authority.mjs +105 -0
  155. package/edition/positive-closure.mjs +162 -0
  156. package/edition/previous-image-lock.mjs +25 -0
  157. package/edition/produce-distribution-completeness-evidence.mjs +317 -0
  158. package/edition/produce-edition-catalogue-evidence.mjs +252 -0
  159. package/edition/produce-image-delivery-entitlement-evidence.mjs +302 -0
  160. package/edition/produce-image-source-evidence.mjs +788 -0
  161. package/edition/producer-evidence-bundle.mjs +201 -0
  162. package/edition/producer-evidence-index-file.mjs +4 -0
  163. package/edition/record-refused.mjs +11 -0
  164. package/edition/registry-client-factory.mjs +21 -0
  165. package/edition/registry-credentials.mjs +225 -0
  166. package/edition/release-catalogue.mjs +48 -0
  167. package/edition/release-channel-guard.mjs +108 -0
  168. package/edition/release-edition-artifact.mjs +152 -0
  169. package/edition/release-edition.mjs +54 -0
  170. package/edition/remedy-command.mjs +89 -0
  171. package/edition/repin-evidence-trust.mjs +109 -0
  172. package/edition/required-resource-provider.mjs +58 -0
  173. package/edition/resolve-chart-set.mjs +376 -0
  174. package/edition/resolve-customer-edition.mjs +374 -0
  175. package/edition/resolve-distribution.mjs +2642 -0
  176. package/edition/resolve-edition.mjs +192 -0
  177. package/edition/resolve-images.mjs +2344 -0
  178. package/edition/resolve-infra-images.mjs +88 -0
  179. package/edition/seal-image-digests.mjs +954 -0
  180. package/edition/sealed-distribution-lock.mjs +22 -0
  181. package/edition/sealed-evidence-accept.mjs +82 -0
  182. package/edition/shallow-checkout.mjs +18 -0
  183. package/edition/sigstore-bundle-media-type.mjs +4 -0
  184. package/edition/source-authority.mjs +713 -0
  185. package/edition/source-closure.mjs +115 -0
  186. package/edition/store-publisher-trust.mjs +40 -0
  187. package/edition/strict-json.mjs +121 -0
  188. package/edition/string-order.mjs +5 -0
  189. package/edition/third-party-admission.mjs +118 -0
  190. package/edition/third-party-bundle-registry.mjs +349 -0
  191. package/edition/third-party-certificate-extensions.mjs +58 -0
  192. package/edition/third-party-image-pins.mjs +41 -0
  193. package/edition/third-party-pins-trusted.mjs +24 -0
  194. package/edition/third-party-producer-evidence.mjs +76 -0
  195. package/edition/third-party-source-check.mjs +28 -0
  196. package/edition/third-party-source-context.mjs +41 -0
  197. package/edition/third-party-source-evidence.mjs +326 -0
  198. package/edition/third-party-source-flags.mjs +148 -0
  199. package/edition/third-party-source-pins.mjs +270 -0
  200. package/edition/third-party-source-uniqueness.mjs +54 -0
  201. package/edition/third-party-source-walk.mjs +118 -0
  202. package/edition/third-party-source-writer.mjs +228 -0
  203. package/edition/third-party-subjects.mjs +128 -0
  204. package/edition/trust-policy-arming-verb.mjs +60 -0
  205. package/edition/trust-policy-arming.mjs +144 -0
  206. package/edition/trust-policy-image-ownership.mjs +85 -0
  207. package/edition/trusted-executable.mjs +166 -0
  208. package/edition/verifier-binding.mjs +45 -0
  209. package/edition/verifier-digest-pin.mjs +17 -0
  210. package/edition/verifier-files.mjs +8 -0
  211. package/edition/verifier-kernel-schemas.mjs +22 -0
  212. package/edition/verifier-provenance.mjs +64 -0
  213. package/edition/verify-catalogue-evidence.mjs +247 -0
  214. package/edition/verify-image-evidence.mjs +493 -0
  215. package/edition/verify-image-platforms.mjs +523 -0
  216. package/edition/why.mjs +56 -0
  217. package/edition/workspace-sections.mjs +287 -0
  218. package/package.json +83 -4
  219. package/README.md +0 -3
@@ -0,0 +1,374 @@
1
+ // The evidence-trust part of `xema-edition check`, and the state an edition
2
+ // records in its descriptor.
3
+ //
4
+ // A data root DECLARES an evidence trust policy (`evidenceTrustPolicy: {policy,
5
+ // pin}` in `edition-workspace.json`) or states that it declares none (`null`).
6
+ // No switch admits an ungated release:
7
+ //
8
+ // * a SEALED edition (either channel) and a RESOLVED RELEASE edition are
9
+ // written only against a policy ARMED on the edition's channel. No policy,
10
+ // UNARMED or PARTIALLY ARMED there -> refused (EvidenceTrustRequiredError,
11
+ // code EVIDENCE_TRUST_REQUIRED_UNARMED); edition-contract.json
12
+ // evidenceTrust.statesByKind is the one statement of this rule;
13
+ // * a RESOLVED DEVELOP edition admits not-armed and says so in one line; a
14
+ // policy PARTIALLY ARMED on the develop channel is refused;
15
+ // * `xema-edition check` FAILS when the editions tree holds a release
16
+ // edition (an edition directory holding a release lock) and the policy is
17
+ // not ARMED on the release channel (none declared, UNARMED or PARTIAL):
18
+ // that edition cannot be published as an artifact. A tree holding no
19
+ // release edition passes for develop-only use, with a notice saying so;
20
+ // * no policy declared but a policy file is present at the top level or under
21
+ // `trust/` -> refused: a policy nothing reads is a policy someone believes
22
+ // is enforced;
23
+ // * declared -> the policy parses with the kernel schema, its pinned Sigstore
24
+ // root matches, the data-root pin records the policy's digest AND this
25
+ // CLI's built verifier (a missing bundle is refused, never skipped), and
26
+ // the authority configuration is judged PER CHANNEL
27
+ // (trust-policy-arming.mjs); a channel that is partially armed is refused.
28
+
29
+ import { existsSync, readFileSync, readdirSync } from 'node:fs';
30
+ import { join } from 'node:path';
31
+
32
+ import { locateBuiltVerifier, readBuiltVerifierDigests } from './built-verifier.mjs';
33
+ import { EDITION_WORKSPACE_CONFIG_FILE, editionWorkspaceConfig } from './edition-config.mjs';
34
+ import { EDITION_CONTRACT, EditionChannel, EditionKind, EvidenceTrustState, admittedEvidenceTrustStates } from './edition-constants.mjs';
35
+ import { dataRoot as configuredDataRoot, editionsRoot as configuredEditionsRoot } from './edition-workspace.mjs';
36
+ import {
37
+ assertPinnedPolicy,
38
+ assertPinnedThirdPartySourcePins,
39
+ assertPinnedVerifier,
40
+ loadDeclaredTrustPolicy,
41
+ readEvidenceTrustPin,
42
+ } from './evidence-trust-pin.mjs';
43
+ import { loadKernelDistributionSchemas } from './lib.mjs';
44
+ import { heldReleaseLocks } from './release-channel-guard.mjs';
45
+ import {
46
+ TrustPolicyArmingState,
47
+ assertArmingChannel,
48
+ assessTrustPolicyArming,
49
+ } from './trust-policy-arming.mjs';
50
+ import { isVerifierDigestPinned } from './verifier-digest-pin.mjs';
51
+
52
+ const TAG = '[dist:check]';
53
+
54
+ /**
55
+ * The stable code of the refusal to write a RELEASE edition without an armed
56
+ * evidence trust policy. Consumers (xema-deploy, workflows) match on the code,
57
+ * never on the message; it is also listed in edition-contract.json.
58
+ */
59
+ export const EVIDENCE_TRUST_REQUIRED_UNARMED = EDITION_CONTRACT.evidenceTrust.refusal.code;
60
+
61
+ /**
62
+ * What `xema-edition check` says of a data root whose policy is not armed on
63
+ * the release channel. Sealing is not gated (nothing sealed leaves without
64
+ * `xema-edition artifact`); publishing is.
65
+ */
66
+ export const NO_RELEASE_ARTIFACT_NOTICE =
67
+ 'no release edition of this data root can be published as an artifact (written by `xema-edition artifact`) until the policy is armed on the release channel';
68
+
69
+ /**
70
+ * The release editions of the configured editions tree: the edition directories
71
+ * (those holding `xema-distribution.json`) that hold a release lock. An edition
72
+ * that is only defined, with no lock resolved yet, is not one.
73
+ */
74
+ export function releaseEditionIds() {
75
+ const root = configuredEditionsRoot();
76
+ if (!existsSync(root)) {return [];}
77
+ return readdirSync(root, { withFileTypes: true })
78
+ .filter(
79
+ (entry) =>
80
+ entry.isDirectory() &&
81
+ existsSync(join(root, entry.name, 'xema-distribution.json')) &&
82
+ heldReleaseLocks(join(root, entry.name)).length > 0,
83
+ )
84
+ .map((entry) => entry.name)
85
+ .sort();
86
+ }
87
+
88
+ /**
89
+ * The verdict of the check for a policy that is not armed on the release
90
+ * channel: red when the tree holds a release edition (it names them and the
91
+ * repair), green with the notice when it holds none.
92
+ */
93
+ function notArmedOnReleaseVerdict(what, repair) {
94
+ const releaseEditions = releaseEditionIds();
95
+ if (releaseEditions.length === 0) {
96
+ console.warn(
97
+ `${TAG} image-evidence trust policy: NOT ARMED on the release channel — ${what}. ${configuredEditionsRoot()} holds no release edition, ` +
98
+ `so this passes for develop-only use: ${NO_RELEASE_ARTIFACT_NOTICE}.`,
99
+ );
100
+ return true;
101
+ }
102
+ console.error(
103
+ `${TAG} image-evidence trust policy FAILED: ${what}, and ${configuredEditionsRoot()} holds ${String(releaseEditions.length)} release edition(s) ` +
104
+ `(${releaseEditions.join(', ')}): ${NO_RELEASE_ARTIFACT_NOTICE}. ${repair}`,
105
+ );
106
+ return false;
107
+ }
108
+
109
+ /**
110
+ * A release edition requires an armed evidence trust policy and this data root
111
+ * has none: it declares no policy, or declares one that is UNARMED or PARTIALLY
112
+ * ARMED on the release channel. Writing the release descriptor or artifact is
113
+ * refused with this error; a release edition never records `not-armed`.
114
+ */
115
+ export class EvidenceTrustRequiredError extends Error {
116
+ /**
117
+ * @param {string} dataRoot @param {string} reason what is not armed, in words
118
+ * @param {{ edition: string, channel: string }} subject the edition that requires it (e.g. "release edition", "sealed release edition") and its channel
119
+ */
120
+ constructor(dataRoot, reason, { edition, channel }) {
121
+ super(
122
+ `a ${edition} requires an armed evidence trust policy, but ${join(dataRoot, EDITION_WORKSPACE_CONFIG_FILE)} ${reason}: ` +
123
+ `declare a policy ({"policy": ..., "pin": ...}) and arm it on the ${channel} channel (\`xema-edition check\` says what is unmet).`,
124
+ );
125
+ this.name = 'EvidenceTrustRequiredError';
126
+ this.code = EVIDENCE_TRUST_REQUIRED_UNARMED;
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Load the declared policy and hold it to its data-root pin: the policy must
132
+ * hash to what the pin records, and this CLI's built verifier must be the one
133
+ * the pin records (a missing bundle is refused, never skipped).
134
+ */
135
+ function loadPinnedPolicy({ dataRoot, declaration, schemas, locateVerifier, channel }) {
136
+ const { policy, policyDigest } = loadDeclaredTrustPolicy({ dataRoot, declaration, schemas });
137
+ const pin = readEvidenceTrustPin({ dataRoot, declaration });
138
+ assertPinnedPolicy({ pin, policyDigest });
139
+ assertPinnedThirdPartySourcePins({ pin, dataRoot });
140
+ assertPinnedVerifier({ pin, actual: readBuiltVerifierDigests(locateVerifier), channel });
141
+ return { policy, policyDigest };
142
+ }
143
+
144
+ /**
145
+ * The data root's declared policy, loaded and held to its pin exactly as the
146
+ * arming checks load it, for a READ-ONLY consumer (the pinned cosign of the official lane, pinned-cosign.mjs).
147
+ * A data root that declares no policy has no pinned cosign and is refused, naming the declaration it lacks.
148
+ *
149
+ * @returns {{ policy: object, dataRoot: string }}
150
+ */
151
+ export async function loadDeclaredPinnedPolicy() {
152
+ const declaration = editionWorkspaceConfig().evidenceTrustPolicy;
153
+ const root = configuredDataRoot();
154
+ if (declaration.policy === null) {
155
+ throw new Error(`${join(root, EDITION_WORKSPACE_CONFIG_FILE)} declares no evidenceTrustPolicy; there is no pinned policy to hold cosign to`);
156
+ }
157
+ const schemas = await loadKernelDistributionSchemas();
158
+ const { policy, policyDigest } = loadDeclaredTrustPolicy({ dataRoot: root, declaration, schemas });
159
+ const pin = readEvidenceTrustPin({ dataRoot: root, declaration });
160
+ assertPinnedPolicy({ pin, policyDigest });
161
+ assertPinnedThirdPartySourcePins({ pin, dataRoot: root });
162
+ return { policy, dataRoot: root };
163
+ }
164
+
165
+ /**
166
+ * The arming of the data root's declared policy on `channel`, loaded and pinned
167
+ * exactly as `descriptorEvidenceTrust` and the check load it: `{ declared, state,
168
+ * unmet }`. A data root that declares no policy is `unarmed` with `declared:
169
+ * false` and one unmet entry; a declared policy that does not hold to its pin
170
+ * (digest, verifier) throws. Nothing is written.
171
+ *
172
+ * @param {{ channel: string, locateVerifier?: () => {bundlePath: string, provenancePath: string}, consumesCatalogue?: boolean }} options
173
+ */
174
+ export async function assessDeclaredTrustPolicyArming({ channel, locateVerifier = locateBuiltVerifier, consumesCatalogue = false }) {
175
+ const declaration = editionWorkspaceConfig().evidenceTrustPolicy;
176
+ const root = configuredDataRoot();
177
+ const schemas = await loadKernelDistributionSchemas();
178
+ assertArmingChannel(schemas, channel);
179
+ if (declaration.policy === null) {
180
+ return {
181
+ declared: false,
182
+ state: TrustPolicyArmingState.UNARMED,
183
+ unmet: [`${join(root, EDITION_WORKSPACE_CONFIG_FILE)} declares no evidenceTrustPolicy`],
184
+ };
185
+ }
186
+ const { policy } = loadPinnedPolicy({ dataRoot: root, declaration, schemas, locateVerifier, channel });
187
+ const { state, unmet } = assessTrustPolicyArming(policy, schemas, channel, { consumesCatalogue });
188
+ return { declared: true, state, unmet };
189
+ }
190
+
191
+ /** What a develop edition says, once, when it is written or checked without an armed develop channel. */
192
+ export const DEVELOP_NOT_ARMED_NOTICE = 'develop channel: trust policy not armed, evidence is not signature-verified';
193
+
194
+ /** A customer develop edition under a policy ARMED on develop: its catalogue carries no E4 evidence to verify. */
195
+ export class CatalogueEvidenceUnverifiableError extends Error {
196
+ constructor(root) {
197
+ super(
198
+ `the data root ${root} declares an evidence trust policy ARMED on the develop channel, but a develop catalogue carries no ` +
199
+ 'signed edition-catalogue (E4) evidence, so the catalogue this customer develop edition consumes cannot be verified and ' +
200
+ 'the edition is never recorded as armed. Resolve it under a policy that is not armed on develop (no develop signer rows), ' +
201
+ 'or resolve a release edition, whose catalogue evidence is verified.',
202
+ );
203
+ this.name = 'CatalogueEvidenceUnverifiableError';
204
+ }
205
+ }
206
+
207
+ /**
208
+ * The evidence state an edition of `kind` on `channel` records in its descriptor. Arming
209
+ * is judged for that ONE channel (`assessTrustPolicyArming`): the signers of
210
+ * the other channel count for nothing. Which states the kind may record on the
211
+ * channel is the contract's (`evidenceTrust.statesByKind`, admittedEvidenceTrustStates):
212
+ *
213
+ * * `armed` - the data root declares a policy, the policy is pinned (the
214
+ * policy and this CLI's verifier match the data-root pin) and
215
+ * ARMED on `channel`;
216
+ * * `not-armed` - a RESOLVED DEVELOP edition only: its channel is not armed (no
217
+ * policy, or UNARMED there), and it says so in one stderr line.
218
+ *
219
+ * Every other state is refused. A SEALED edition (either channel) and a RESOLVED
220
+ * RELEASE edition that is not ARMED on its channel (no policy, UNARMED or PARTIAL
221
+ * there) throw EvidenceTrustRequiredError: this function never returns `not-armed`
222
+ * for them. A policy edited after pinning, a drifted verifier, and a policy
223
+ * PARTIALLY ARMED on `channel` are refused for every edition.
224
+ *
225
+ * `consumesCatalogue` (a customer edition consumes a catalogue): on RELEASE it adds the edition-catalogue (E4) row to the
226
+ * required ones, and the catalogue's E4 evidence is verified before it is parsed. A DEVELOP catalogue carries no E4
227
+ * evidence (the vendor's develop-edition workflow signs the artifact but attaches no E4 statement), so on develop no E4
228
+ * row is required and nothing can verify the catalogue: a customer develop edition is never written `armed`. A policy
229
+ * ARMED on develop throws CatalogueEvidenceUnverifiableError instead of recording a claim nothing checked.
230
+ *
231
+ * @param {{ kind: string, channel: string, locateVerifier?: () => {bundlePath: string, provenancePath: string}, consumesCatalogue?: boolean }} options
232
+ */
233
+ export async function descriptorEvidenceTrust({ kind, channel, locateVerifier = locateBuiltVerifier, consumesCatalogue = false }) {
234
+ const declaration = editionWorkspaceConfig().evidenceTrustPolicy;
235
+ const root = configuredDataRoot();
236
+ const schemas = await loadKernelDistributionSchemas();
237
+ assertArmingChannel(schemas, channel);
238
+ const release = channel === EditionChannel.Release;
239
+ const notArmedAdmitted = admittedEvidenceTrustStates(kind, channel).includes(EvidenceTrustState.NotArmed);
240
+ // A resolved edition is named by its channel ("a release edition"); a seal by its kind and channel.
241
+ const subject = { edition: kind === EditionKind.Sealed ? `${kind} ${channel} edition` : `${channel} edition`, channel };
242
+ const notArmed = (reason) => {
243
+ if (!notArmedAdmitted) {throw new EvidenceTrustRequiredError(root, reason, subject);}
244
+ console.error(DEVELOP_NOT_ARMED_NOTICE);
245
+ return EvidenceTrustState.NotArmed;
246
+ };
247
+ if (declaration.policy === null) {return notArmed('declares no policy');}
248
+ const { policy } = loadPinnedPolicy({ dataRoot: root, declaration, schemas, locateVerifier, channel });
249
+ const { state, unmet } = assessTrustPolicyArming(policy, schemas, channel, { consumesCatalogue: consumesCatalogue && release });
250
+ if (state === TrustPolicyArmingState.ARMED) {
251
+ if (consumesCatalogue && !release) {throw new CatalogueEvidenceUnverifiableError(root);}
252
+ return EvidenceTrustState.Armed;
253
+ }
254
+ if (state === TrustPolicyArmingState.PARTIAL) {
255
+ if (!notArmedAdmitted) {throw new EvidenceTrustRequiredError(root, `declares a policy that is PARTIALLY ARMED on the ${channel} channel (${unmet.join('; ')})`, subject);}
256
+ throw new Error(`the declared evidence trust policy is PARTIALLY ARMED on the ${channel} channel (${unmet.join('; ')}); an edition cannot be written against it`);
257
+ }
258
+ return notArmed(`declares a policy that is UNARMED on the ${channel} channel`);
259
+ }
260
+
261
+ export const UNDECLARED_TRUST_DIRECTORY = 'trust';
262
+
263
+ /**
264
+ * Policy files the data root holds without declaring them: a top-level `*.json`
265
+ * the kernel parses as an offline trust policy, or a `trust/` directory. A file
266
+ * the kernel schema does not accept is not a policy, which is the whole test.
267
+ */
268
+ function undeclaredPolicyArtifacts(dataRoot, schemas) {
269
+ const found = [];
270
+ for (const entry of readdirSync(dataRoot, { withFileTypes: true })) {
271
+ if (entry.isDirectory() && entry.name === UNDECLARED_TRUST_DIRECTORY) {
272
+ found.push(`${UNDECLARED_TRUST_DIRECTORY}/`);
273
+ } else if (entry.isFile() && entry.name.endsWith('.json')) {
274
+ if (isTrustPolicy(join(dataRoot, entry.name), schemas)) {
275
+ found.push(entry.name);
276
+ }
277
+ }
278
+ }
279
+ return found.sort();
280
+ }
281
+
282
+ function isTrustPolicy(path, schemas) {
283
+ try {
284
+ schemas.parseSigstoreOfflineTrustPolicy(JSON.parse(readFileSync(path, 'utf8')));
285
+ return true;
286
+ } catch {
287
+ return false;
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Run the gate against the configured data root. Returns true when green; every
293
+ * refusal is printed, naming the repair.
294
+ *
295
+ * @param {{ channel: string, locateVerifier?: () => {bundlePath: string, provenancePath: string} }} options
296
+ * `channel` is the lane checked: it decides whether the verifier is held to the pin's digests (`isVerifierDigestPinned`).
297
+ * `locateVerifier` is a parameter so a test can supply a fixture bundle.
298
+ */
299
+ export async function checkEvidenceTrustPolicy({
300
+ channel,
301
+ locateVerifier = locateBuiltVerifier,
302
+ }) {
303
+ try {
304
+ const dataRoot = configuredDataRoot();
305
+ const declaration = editionWorkspaceConfig().evidenceTrustPolicy;
306
+ const schemas = await loadKernelDistributionSchemas();
307
+ if (declaration.policy === null) {
308
+ const undeclared = undeclaredPolicyArtifacts(dataRoot, schemas);
309
+ if (undeclared.length > 0) {
310
+ console.error(
311
+ `${TAG} image-evidence trust policy FAILED: ${dataRoot} holds ${undeclared.join(', ')} ` +
312
+ `but ${EDITION_WORKSPACE_CONFIG_FILE} declares no evidenceTrustPolicy, so nothing reads it. ` +
313
+ 'Declare it ({"policy": ..., "pin": ...}) or remove it.',
314
+ );
315
+ return false;
316
+ }
317
+ return notArmedOnReleaseVerdict(
318
+ `${join(dataRoot, EDITION_WORKSPACE_CONFIG_FILE)} declares no evidenceTrustPolicy (its develop editions are not evidence-gated)`,
319
+ 'Declare a policy ({"policy": ..., "pin": ...}), authorize the release signers in it, pin it with `xema-edition repin-evidence-trust`, and re-run `xema-edition check` until it reports ARMED on the release channel.',
320
+ );
321
+ }
322
+
323
+ const { policy, policyDigest } = loadPinnedPolicy({ dataRoot, declaration, schemas, locateVerifier, channel });
324
+ console.log(
325
+ `${TAG} image-evidence trust policy pin: green — ${declaration.pin} pins ${declaration.policy} ` +
326
+ `at ${policyDigest}${isVerifierDigestPinned(channel) ? " and this CLI's verifier" : ''} (channel=${channel} verifierPinned=${String(isVerifierDigestPinned(channel))}).`,
327
+ );
328
+
329
+ console.log(
330
+ `${TAG} image-evidence trust policy: schema green — pinned ${policy.cosignVersion}, ` +
331
+ `${policy.policies.length} authorized signer identities, ` +
332
+ `${policy.trustedCosignBinaryDigests.length} cosign / ` +
333
+ `${policy.trustedGitBinaryDigests.length} git binary pins.`,
334
+ );
335
+ // Each channel is judged on its own signers. The develop channel admits
336
+ // not-armed and says so; a release channel that is not armed fails the
337
+ // check as soon as the tree holds a release edition.
338
+ const partial = (channel, unmet) => {
339
+ console.error(
340
+ `${TAG} image-evidence trust policy is PARTIALLY ARMED on the ${channel} channel — this mixed state is invalid ` +
341
+ `and no ${channel} image evidence can verify against it:`,
342
+ );
343
+ for (const reason of unmet) {
344
+ console.error(`${TAG} - ${reason}`);
345
+ }
346
+ };
347
+ const develop = assessTrustPolicyArming(policy, schemas, EditionChannel.Develop);
348
+ if (develop.state === TrustPolicyArmingState.PARTIAL) {
349
+ partial(EditionChannel.Develop, develop.unmet);
350
+ return false;
351
+ }
352
+ const release = assessTrustPolicyArming(policy, schemas, EditionChannel.Release);
353
+ if (release.state === TrustPolicyArmingState.PARTIAL) {
354
+ partial(EditionChannel.Release, release.unmet);
355
+ console.error(
356
+ `${TAG} ${NO_RELEASE_ARTIFACT_NOTICE}. Complete the release channel in ${declaration.policy} (every line above), ` +
357
+ 're-pin with `xema-edition repin-evidence-trust`, and re-run `xema-edition check`.',
358
+ );
359
+ return false;
360
+ }
361
+ if (develop.state === TrustPolicyArmingState.UNARMED) {console.error(`${TAG} ${DEVELOP_NOT_ARMED_NOTICE}`);}
362
+ if (release.state === TrustPolicyArmingState.UNARMED) {
363
+ return notArmedOnReleaseVerdict(
364
+ `${declaration.policy} is declared but UNARMED on the release channel (it authorizes no release signer)`,
365
+ `Authorize the release signers and pin the cosign and git binaries in ${declaration.policy}, re-pin with \`xema-edition repin-evidence-trust\`, and re-run \`xema-edition check\` until it reports ARMED on the release channel.`,
366
+ );
367
+ }
368
+ console.log(`${TAG} image-evidence trust policy: ARMED on the release channel${develop.armed ? ' and on the develop channel' : ''}.`);
369
+ return true;
370
+ } catch (error) {
371
+ console.error(`${TAG} image-evidence trust policy FAILED: ${error.message}`);
372
+ return false;
373
+ }
374
+ }