@kungfu-tech/buildchain 3.0.2-alpha.3 → 3.0.2-alpha.5

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 (49) hide show
  1. package/README.md +1 -0
  2. package/actions/github-artifact-attestation/README.md +10 -0
  3. package/actions/promote-buildchain-ref/README.md +7 -0
  4. package/bin/buildchain.mjs +5 -0
  5. package/bin/internal/trust-release-cli.mjs +74 -3
  6. package/dist/site/artifact-schemas.json +5 -1
  7. package/dist/site/buildchain-contract.json +79 -28
  8. package/dist/site/buildchain-site.json +110 -27
  9. package/dist/site/capability-registry.json +8 -7
  10. package/dist/site/cli-registry.json +12 -0
  11. package/dist/site/controller-registry.json +40 -4
  12. package/dist/site/kfd-claims.json +203 -18
  13. package/dist/site/kfd-upstream-aggregate.json +1 -1
  14. package/dist/site/manual-registry.json +19 -5
  15. package/dist/site/node-api-registry.json +23 -10
  16. package/dist/site/page-registry.json +91 -17
  17. package/dist/site/public-surface-audit.json +125 -15
  18. package/dist/site/publication-authority-registry.json +27 -2
  19. package/dist/site/publication-registry.json +4 -4
  20. package/dist/site/release-model.json +2 -1
  21. package/dist/site/release-passport-check-manifest.json +1 -0
  22. package/dist/site/release-provenance.json +1 -0
  23. package/dist/site/schemas/release-passport-v1.schema.json +6 -0
  24. package/dist/site/site-manifest.json +17 -9
  25. package/dist/site/workflow-registry.json +84 -7
  26. package/docs/MAP.md +3 -1
  27. package/docs/binary-distribution.md +7 -0
  28. package/docs/cli.md +9 -0
  29. package/docs/dev-alpha-candidate-patrol.md +16 -4
  30. package/docs/github-artifact-attestation.md +219 -0
  31. package/docs/release-passport.md +13 -0
  32. package/docs/reusable-build-surface.md +6 -0
  33. package/package.json +2 -1
  34. package/packages/core/buildchain-contract.js +6 -0
  35. package/packages/core/buildchain-kfd-claims.js +5 -0
  36. package/packages/core/buildchain-publication-authority.js +1 -0
  37. package/packages/core/channel-candidate.js +67 -16
  38. package/packages/core/github-artifact-attestation.js +642 -0
  39. package/packages/core/index.js +19 -0
  40. package/packages/core/publication-authority.js +1 -1
  41. package/packages/core/release-passport-contract.js +2 -0
  42. package/packages/core/release-passport.js +51 -0
  43. package/scripts/check-inventory.mjs +4 -0
  44. package/scripts/create-github-artifact-attestation-policy.mjs +62 -0
  45. package/scripts/dev-alpha-candidate-patrol.mjs +256 -46
  46. package/scripts/generate-site-bundle.mjs +12 -0
  47. package/scripts/publish-github-artifact-attestation-evidence.mjs +201 -0
  48. package/scripts/release-candidate-resolver.mjs +12 -0
  49. package/scripts/stage-github-artifact-attestation-inputs.mjs +65 -0
@@ -0,0 +1,201 @@
1
+ #!/usr/bin/env node
2
+
3
+ import crypto from "node:crypto";
4
+ import fs from "node:fs";
5
+ import path from "node:path";
6
+ import process from "node:process";
7
+ import { spawnSync } from "node:child_process";
8
+
9
+ import {
10
+ createGitHubArtifactAttestationVerificationPlan,
11
+ verifyGitHubArtifactAttestationEvidence,
12
+ } from "../packages/core/github-artifact-attestation.js";
13
+
14
+ function parseArgs(argv) {
15
+ const result = {};
16
+ for (let index = 0; index < argv.length; index += 2) {
17
+ const key = argv[index];
18
+ const value = argv[index + 1];
19
+ if (!key?.startsWith("--") || value === undefined) throw new Error(`invalid argument near ${key || "<end>"}`);
20
+ result[key.slice(2)] = value;
21
+ }
22
+ return result;
23
+ }
24
+
25
+ function required(value, label) {
26
+ const normalized = String(value || "").trim();
27
+ if (!normalized) throw new Error(`${label} is required`);
28
+ return normalized;
29
+ }
30
+
31
+ function sha256(value) {
32
+ return crypto.createHash("sha256").update(value).digest("hex");
33
+ }
34
+
35
+ function sha256File(filePath) {
36
+ return `sha256:${sha256(fs.readFileSync(filePath))}`;
37
+ }
38
+
39
+ function readJson(filePath, label) {
40
+ try {
41
+ return JSON.parse(fs.readFileSync(filePath, "utf8"));
42
+ } catch (error) {
43
+ throw new Error(`${label} is not readable JSON: ${error.message}`);
44
+ }
45
+ }
46
+
47
+ function safeAssetStem(value) {
48
+ return path.basename(required(value, "subject name"))
49
+ .replace(/[^0-9A-Za-z._-]+/g, "-")
50
+ .replace(/^-+|-+$/g, "");
51
+ }
52
+
53
+ async function api({ token, apiUrl, route, method = "GET", headers = {}, body }) {
54
+ const response = await fetch(`${apiUrl}${route}`, {
55
+ method,
56
+ headers: {
57
+ accept: "application/vnd.github+json",
58
+ authorization: `Bearer ${token}`,
59
+ "x-github-api-version": "2022-11-28",
60
+ ...headers,
61
+ },
62
+ body,
63
+ });
64
+ if (!response.ok) {
65
+ const detail = (await response.text()).slice(0, 500);
66
+ throw new Error(`GitHub API ${method} ${route} failed with ${response.status}: ${detail}`);
67
+ }
68
+ return response;
69
+ }
70
+
71
+ async function remoteAssetDigest({ token, apiUrl, repository, asset }) {
72
+ const declared = String(asset.digest || "").match(/^sha256:([0-9a-f]{64})$/i);
73
+ if (declared) return `sha256:${declared[1].toLowerCase()}`;
74
+ const response = await api({
75
+ token,
76
+ apiUrl,
77
+ route: `/repos/${repository}/releases/assets/${asset.id}`,
78
+ headers: { accept: "application/octet-stream" },
79
+ });
80
+ return `sha256:${sha256(Buffer.from(await response.arrayBuffer()))}`;
81
+ }
82
+
83
+ async function uploadImmutable({ token, apiUrl, repository, release, filePath, assetName }) {
84
+ const localDigest = sha256File(filePath);
85
+ const existing = (release.assets || []).filter((asset) => asset.name === assetName);
86
+ if (existing.length > 1) throw new Error(`release asset ${assetName} exists more than once`);
87
+ if (existing.length === 1) {
88
+ const remoteDigest = await remoteAssetDigest({ token, apiUrl, repository, asset: existing[0] });
89
+ if (remoteDigest !== localDigest) {
90
+ throw new Error(`immutable release asset collision for ${assetName}: ${remoteDigest} != ${localDigest}`);
91
+ }
92
+ return { action: "preserved", name: assetName, digest: localDigest, url: existing[0].browser_download_url };
93
+ }
94
+ const uploadBase = required(release.upload_url, "release.upload_url").replace(/\{.*$/, "");
95
+ const response = await fetch(`${uploadBase}?name=${encodeURIComponent(assetName)}`, {
96
+ method: "POST",
97
+ headers: {
98
+ accept: "application/vnd.github+json",
99
+ authorization: `Bearer ${token}`,
100
+ "content-type": "application/octet-stream",
101
+ "x-github-api-version": "2022-11-28",
102
+ },
103
+ body: fs.readFileSync(filePath),
104
+ });
105
+ if (!response.ok) throw new Error(`GitHub release asset upload failed with ${response.status}: ${(await response.text()).slice(0, 500)}`);
106
+ const asset = await response.json();
107
+ release.assets = [...(release.assets || []), asset];
108
+ const remoteDigest = await remoteAssetDigest({ token, apiUrl, repository, asset });
109
+ if (remoteDigest !== localDigest) throw new Error(`release asset read-back mismatch for ${assetName}`);
110
+ return { action: "uploaded", name: assetName, digest: localDigest, url: asset.browser_download_url };
111
+ }
112
+
113
+ function appendOutput(name, value) {
114
+ if (process.env.GITHUB_OUTPUT) fs.appendFileSync(process.env.GITHUB_OUTPUT, `${name}=${String(value)}\n`);
115
+ }
116
+
117
+ async function main() {
118
+ const options = parseArgs(process.argv.slice(2));
119
+ const token = required(process.env.GITHUB_TOKEN || process.env.GH_TOKEN, "GITHUB_TOKEN");
120
+ const apiUrl = (process.env.GITHUB_API_URL || "https://api.github.com").replace(/\/$/, "");
121
+ const repository = required(options.repository || process.env.GITHUB_REPOSITORY, "repository");
122
+ const tag = required(options.tag, "tag");
123
+ const subjectPath = path.resolve(required(options.subject, "subject"));
124
+ const manifestPath = path.resolve(required(options.manifest, "manifest"));
125
+ const passportPath = path.resolve(required(options.passport, "passport"));
126
+ const bundlePath = path.resolve(required(options.bundle, "bundle"));
127
+ const evidencePath = path.resolve(required(options.evidence, "evidence"));
128
+ const predicatePath = path.resolve(required(options.predicate, "predicate"));
129
+ const providerVerificationPath = path.resolve(required(options["provider-verification"], "provider-verification"));
130
+ const receiptPath = path.resolve(required(options.receipt, "receipt"));
131
+ const evidence = readJson(evidencePath, "Buildchain attestation evidence");
132
+ const plan = createGitHubArtifactAttestationVerificationPlan({ artifactPath: subjectPath, bundlePath, evidence });
133
+ const provider = spawnSync(plan.command, plan.args, { encoding: "utf8", env: process.env });
134
+ if (provider.status !== 0) throw new Error(`gh attestation verify failed: ${String(provider.stderr || provider.stdout).slice(0, 1000)}`);
135
+ const verificationResults = JSON.parse(provider.stdout);
136
+ const local = verifyGitHubArtifactAttestationEvidence({
137
+ artifactPath: subjectPath,
138
+ platformManifestPath: manifestPath,
139
+ releasePassportPath: passportPath,
140
+ bundlePath,
141
+ evidence,
142
+ verificationResults,
143
+ });
144
+ if (!local.ok) throw new Error(`Buildchain evidence verification failed: ${local.issues.map((issue) => issue.message).join("; ")}`);
145
+ const retainedProvider = readJson(providerVerificationPath, "retained provider verification");
146
+ const retained = verifyGitHubArtifactAttestationEvidence({
147
+ artifactPath: subjectPath,
148
+ platformManifestPath: manifestPath,
149
+ releasePassportPath: passportPath,
150
+ bundlePath,
151
+ evidence,
152
+ verificationResults: retainedProvider,
153
+ });
154
+ if (!retained.ok) {
155
+ throw new Error(`retained provider verification failed: ${retained.issues.map((issue) => issue.message).join("; ")}`);
156
+ }
157
+ const releaseResponse = await api({ token, apiUrl, route: `/repos/${repository}/releases/tags/${encodeURIComponent(tag)}` });
158
+ const release = await releaseResponse.json();
159
+ const stem = safeAssetStem(evidence.subject?.name);
160
+ const declarations = [
161
+ [bundlePath, `${stem}.sigstore-bundle.json`],
162
+ [evidencePath, `${stem}.buildchain-attestation.json`],
163
+ [predicatePath, `${stem}.buildchain-predicate.json`],
164
+ [providerVerificationPath, `${stem}.github-verification.json`],
165
+ ];
166
+ const assets = [];
167
+ for (const [filePath, assetName] of declarations) {
168
+ assets.push(await uploadImmutable({ token, apiUrl, repository, release, filePath, assetName }));
169
+ }
170
+ const receipt = {
171
+ contract: "buildchain.github-artifact-attestation-publication/v1",
172
+ repository,
173
+ tag,
174
+ releaseUrl: release.html_url,
175
+ subject: evidence.subject,
176
+ evidenceRoot: evidence.evidenceRoot,
177
+ attestation: evidence.attestation,
178
+ assets,
179
+ verified: true,
180
+ };
181
+ fs.mkdirSync(path.dirname(receiptPath), { recursive: true });
182
+ fs.writeFileSync(receiptPath, `${JSON.stringify(receipt, null, 2)}\n`);
183
+ const receiptAsset = await uploadImmutable({
184
+ token,
185
+ apiUrl,
186
+ repository,
187
+ release,
188
+ filePath: receiptPath,
189
+ assetName: `${stem}.buildchain-attestation-publication.json`,
190
+ });
191
+ appendOutput("release-url", release.html_url);
192
+ appendOutput("evidence-root", evidence.evidenceRoot);
193
+ appendOutput("publication-receipt", receiptPath);
194
+ appendOutput("publication-receipt-digest", receiptAsset.digest);
195
+ process.stdout.write(`${JSON.stringify({ ...receipt, receiptAsset }, null, 2)}\n`);
196
+ }
197
+
198
+ main().catch((error) => {
199
+ console.error(error.message);
200
+ process.exitCode = 1;
201
+ });
@@ -608,6 +608,10 @@ export async function resolveReleaseCandidateArtifacts({
608
608
  }
609
609
  const passport = JSON.parse(fs.readFileSync(passportPath, "utf8"));
610
610
  const platformManifestPaths = findDownloadedFiles(payloadDir, "manifest.json");
611
+ const githubArtifactAttestationPolicyPaths = findDownloadedFiles(
612
+ payloadDir,
613
+ "github-artifact-attestation-policy.json",
614
+ );
611
615
  const npmTarballPaths = publishArtifactKind === "npm"
612
616
  ? findDownloadedFilesByExtension(payloadDir, [".tgz"])
613
617
  : [];
@@ -639,6 +643,7 @@ export async function resolveReleaseCandidateArtifacts({
639
643
  buildSummary: outputPath(buildSummaryPath),
640
644
  payloads: outputPath(payloadDir),
641
645
  platformManifests: platformManifestPaths.map(outputPath),
646
+ githubArtifactAttestationPolicies: githubArtifactAttestationPolicyPaths.map(outputPath),
642
647
  npmTarballs: npmTarballPaths.map(outputPath),
643
648
  releaseAssets: releaseAssetPaths.map(outputPath),
644
649
  publishRequiredArtifacts: outputPath(requiredArtifactsPath),
@@ -647,6 +652,7 @@ export async function resolveReleaseCandidateArtifacts({
647
652
  candidateHash: passport.candidateHash || "",
648
653
  payloadCount: payloadArtifacts.length,
649
654
  platformManifestCount: platformManifestPaths.length,
655
+ githubArtifactAttestationPolicyCount: githubArtifactAttestationPolicyPaths.length,
650
656
  npmTarballCount: npmTarballPaths.length,
651
657
  publishRequiredArtifacts: generatedRequiredArtifacts,
652
658
  };
@@ -684,6 +690,12 @@ export async function resolveReleaseCandidateArtifactsCli() {
684
690
  "release-candidate-payload-dir": result.paths?.payloads || "",
685
691
  "release-candidate-platform-manifest-paths": (result.paths?.platformManifests || []).join(","),
686
692
  "release-candidate-platform-manifest-count": String(result.platformManifestCount || 0),
693
+ "release-candidate-github-artifact-attestation-policy-paths": (
694
+ result.paths?.githubArtifactAttestationPolicies || []
695
+ ).join(","),
696
+ "release-candidate-github-artifact-attestation-policy-count": String(
697
+ result.githubArtifactAttestationPolicyCount || 0,
698
+ ),
687
699
  "release-candidate-npm-tarball-paths": (result.paths?.npmTarballs || []).join(","),
688
700
  "release-candidate-npm-tarball-count": String(result.npmTarballCount || 0),
689
701
  "release-candidate-github-release-artifact-paths": (
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import process from "node:process";
6
+
7
+ import { stageGitHubArtifactAttestationInputs } from "../packages/core/github-artifact-attestation.js";
8
+
9
+ function args(argv) {
10
+ const values = {};
11
+ for (let index = 0; index < argv.length; index += 2) {
12
+ const key = argv[index];
13
+ const value = argv[index + 1];
14
+ if (!key?.startsWith("--") || value === undefined) {
15
+ throw new Error(`invalid argument near ${key || "<end>"}`);
16
+ }
17
+ values[key.slice(2)] = value;
18
+ }
19
+ return values;
20
+ }
21
+
22
+ function splitPaths(value) {
23
+ return String(value || "")
24
+ .split(/[\n,]/)
25
+ .map((entry) => entry.trim())
26
+ .filter(Boolean);
27
+ }
28
+
29
+ function readPolicy(value) {
30
+ const candidate = path.resolve(String(value || ""));
31
+ return fs.existsSync(candidate)
32
+ ? JSON.parse(fs.readFileSync(candidate, "utf8"))
33
+ : JSON.parse(String(value || ""));
34
+ }
35
+
36
+ function appendOutput(name, value) {
37
+ const output = process.env.GITHUB_OUTPUT;
38
+ if (!output) return;
39
+ fs.appendFileSync(output, `${name}=${String(value)}\n`);
40
+ }
41
+
42
+ const options = args(process.argv.slice(2));
43
+ const result = stageGitHubArtifactAttestationInputs({
44
+ policy: readPolicy(options.policy),
45
+ subjectRoots: splitPaths(options["subject-roots"]),
46
+ platformManifestPaths: splitPaths(options["platform-manifests"]),
47
+ releasePassportPath: options["release-passport"],
48
+ outputDir: options["output-dir"],
49
+ });
50
+ appendOutput("input-dir", result.outputDir);
51
+ appendOutput("policy-json", result.policyJson);
52
+ appendOutput("subject-relative-path", result.relativePaths.subject);
53
+ appendOutput("platform-manifest-relative-path", result.relativePaths.platformManifest);
54
+ appendOutput("release-passport-relative-path", result.relativePaths.releasePassport);
55
+ appendOutput("source-sha", result.policy.caller.sourceSha);
56
+ appendOutput("signer-sha", result.policy.signer.workflowDigest);
57
+ appendOutput("buildchain-runtime-sha", result.policy.build.buildchainRuntimeSha);
58
+ appendOutput("subject-name", result.policy.subject.name);
59
+ process.stdout.write(`${JSON.stringify({
60
+ contract: result.contract,
61
+ subject: result.policy.subject,
62
+ caller: result.policy.caller,
63
+ signer: result.policy.signer,
64
+ paths: result.relativePaths,
65
+ }, null, 2)}\n`);