oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package release
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "encoding/json"
7 "fmt"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "testing"
12)
13
14// Production may receive only the bytes one release build made and a
15// green staging run recorded: scripts/staged-artifacts.py writes the
16// build's provenance manifest, and verify and check refuse a missing
17// manifest, a missing record and any changed artifact.
18func TestStagedArtifactsGate(t *testing.T) {
19 root := t.TempDir()
20 script, err := os.ReadFile("../../scripts/staged-artifacts.py")
21 if err != nil {
22 t.Fatal(err)
23 }
24 must := func(err error) {
25 t.Helper()
26 if err != nil {
27 t.Fatal(err)
28 }
29 }
30 must(os.MkdirAll(filepath.Join(root, "scripts"), 0700))
31 must(os.WriteFile(filepath.Join(root, "scripts", "staged-artifacts.py"), script, 0700))
32 runner := filepath.Join(root, "releases", "equipo-runner-linux-amd64-abc1234")
33 must(os.MkdirAll(runner, 0700))
34 write := func(path, body string) { must(os.WriteFile(path, []byte(body), 0600)) }
35 instance := filepath.Join(root, "releases", "equipo-linux-arm64-abc1234")
36 write(instance, "instance")
37 write(instance+".COMMIT", "abc1234full\n")
38 write(filepath.Join(runner, "equipo-runner"), "runner")
39 write(filepath.Join(runner, "equipo-tool-proxy"), "proxy")
40 write(filepath.Join(runner, "gitleaks"), "scanner")
41 must(os.MkdirAll(filepath.Join(runner, "darwin-arm64"), 0700))
42 write(filepath.Join(runner, "darwin-arm64", "equipo-runner"), "mac runner")
43 write(filepath.Join(runner, "darwin-arm64", "gitleaks"), "mac scanner")
44 renderer := writeRendererTar(t, filepath.Join(runner, "renderer.tar.gz"), "dir usr\n")
45 write(filepath.Join(runner, "SOURCES"), "sources")
46 gate := func(verb string, args ...string) error {
47 return exec.Command("python3", append([]string{filepath.Join(root, "scripts", "staged-artifacts.py"), verb, "abc1234"}, args...)...).Run()
48 }
49 hash := func(s string) string { sum := sha256.Sum256([]byte(s)); return hex.EncodeToString(sum[:]) }
50 // What make release-runner leaves: the release manifest with the tool
51 // proxy it built, and the install manifest with every target's digests.
52 released := func(proxy, linux, mac string) {
53 tarball, err := os.ReadFile(filepath.Join(runner, "renderer.tar.gz"))
54 must(err)
55 archive := `,"renderer_archive_sha256":"` + hash(string(tarball)) + `","renderer_archive_size":` + fmt.Sprint(len(tarball))
56 release := `{"schema":3,"binaries":[{"name":"bwrap","sha256":"` + hash("bwrap") + `"},{"name":"equipo-tool-proxy","sha256":"` + hash(proxy) + `"}]}`
57 write(filepath.Join(runner, "manifest.json"), release)
58 write(filepath.Join(runner, "install-manifest.json"), `{"gitleaks":"v8.30.1","version":"abc1234","runner_sha256":"`+hash(linux)+`","release":`+release+`,"targets":{"linux/amd64":{"runner_sha256":"`+hash(linux)+`","tool_proxy_sha256":"`+hash(proxy)+`","gitleaks_sha256":"`+hash("scanner")+`","renderer_sha256":"`+renderer+`"`+archive+`},"darwin/arm64":{"runner_sha256":"`+hash(mac)+`","gitleaks_sha256":"`+hash("mac scanner")+`"}}}`)
59 }
60 write(filepath.Join(runner, "COMMIT"), "abc1234full\n")
61 bin := t.TempDir()
62 // go version -m: what each binary says it was built from.
63 vcs := filepath.Join(bin, "vcs")
64 built := func(revision, modified string) {
65 write(vcs, "\tbuild\tvcs.revision="+revision+"\n\tbuild\tvcs.modified="+modified+"\n")
66 }
67 built("abc1234full", "false")
68 must(os.WriteFile(filepath.Join(bin, "go"), []byte(`#!/bin/sh
69if [ "$1" = version ]; then
70 case "$3" in */gitleaks) printf '\tmod\tgithub.com/zricethezav/gitleaks/v8\t%s\th1:fixture\n' "${FIXTURE_SCANNER_VERSION:-v8.30.1}"; if [ "${FIXTURE_SCANNER_REPLACED:-}" = yes ]; then printf '\t=>\t./untrusted\n'; fi; exit 0;; esac
71 echo "$3: go1.26.11"
72 cat "`+vcs+`"
73 exit 0
74fi
75echo go1.26.11
76`), 0700))
77 t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
78 // No provenance manifest: nothing verifies, records or deploys.
79 released("proxy", "runner", "mac runner")
80 for _, verb := range []string{"verify", "check"} {
81 if gate(verb) == nil {
82 t.Fatal(verb + " passed with no provenance manifest")
83 }
84 }
85 if gate("record", "--instance", hash("instance"), "--runner", hash("runner"), "--tool-proxy", hash("proxy")) == nil {
86 t.Fatal("recorded a release with no provenance manifest")
87 }
88 // A runner release that does not name its own bytes records nothing.
89 for name, change := range map[string]func(){
90 "another tool proxy": func() { released("another proxy", "runner", "mac runner") },
91 "another runner": func() { released("proxy", "another runner", "mac runner") },
92 "another Mac runner": func() { released("proxy", "runner", "another mac runner") },
93 "another commit": func() { write(filepath.Join(runner, "COMMIT"), "def5678full\n") },
94 "another renderer": func() { writeRendererTar(t, filepath.Join(runner, "renderer.tar.gz"), "dir etc\n") },
95 // The same tree, other archive bytes: the recorded digest is not
96 // the file's.
97 "another archive": func() {
98 f, err := os.OpenFile(filepath.Join(runner, "renderer.tar.gz"), os.O_APPEND|os.O_WRONLY, 0)
99 must(err)
100 f.Write([]byte{0})
101 f.Close()
102 },
103 } {
104 change()
105 if gate("manifest") == nil {
106 t.Fatal("a provenance manifest written for a release with " + name)
107 }
108 writeRendererTar(t, filepath.Join(runner, "renderer.tar.gz"), "dir usr\n")
109 released("proxy", "runner", "mac runner")
110 write(filepath.Join(runner, "COMMIT"), "abc1234full\n")
111 }
112 t.Setenv("FIXTURE_SCANNER_VERSION", "v8.30.0")
113 if gate("manifest") == nil {
114 t.Fatal("scanner from another module version accepted")
115 }
116 t.Setenv("FIXTURE_SCANNER_VERSION", "v8.30.1")
117 t.Setenv("FIXTURE_SCANNER_REPLACED", "yes")
118 if gate("manifest") == nil {
119 t.Fatal("locally replaced scanner source accepted")
120 }
121 t.Setenv("FIXTURE_SCANNER_REPLACED", "")
122 // A binary that names another commit, a modified tree, or nothing at
123 // all about where it came from, records nothing.
124 for name, change := range map[string]func(){
125 "another commit's binaries": func() { built("def5678full", "false") },
126 "a modified tree": func() { built("abc1234full", "true") },
127 "no build information": func() { write(vcs, "") },
128 } {
129 change()
130 if gate("manifest") == nil {
131 t.Fatal("a provenance manifest written for " + name)
132 }
133 if _, err := os.Stat(instance + ".manifest.json"); !os.IsNotExist(err) {
134 t.Fatal("a refused manifest was written for " + name)
135 }
136 }
137 built("abc1234full", "false")
138 must(gate("manifest"))
139 must(gate("verify"))
140 var made struct {
141 Version, Commit, Go string
142 SHA256 map[string]string
143 }
144 raw, err := os.ReadFile(instance + ".manifest.json")
145 must(err)
146 must(json.Unmarshal(raw, &made))
147 if made.Version != "abc1234" || made.Commit != "abc1234full" || made.Go != "go1.26.11" || made.SHA256["instance"] != hash("instance") || made.SHA256["runner"] != hash("runner") || made.SHA256["tool_proxy"] != hash("proxy") || made.SHA256["darwin_runner"] != hash("mac runner") || made.SHA256["gitleaks"] != hash("scanner") || made.SHA256["darwin_gitleaks"] != hash("mac scanner") || made.SHA256["renderer_sources"] != hash("sources") || made.SHA256["renderer_archive"] == "" || made.SHA256["install_manifest"] == "" {
148 t.Fatalf("provenance manifest %s", raw)
149 }
150 // Each artifact is held to the build that recorded it.
151 for _, path := range []string{instance, filepath.Join(runner, "equipo-runner"), filepath.Join(runner, "equipo-tool-proxy"), filepath.Join(runner, "darwin-arm64", "equipo-runner"), filepath.Join(runner, "gitleaks"), filepath.Join(runner, "darwin-arm64", "gitleaks"), filepath.Join(runner, "SOURCES"), filepath.Join(runner, "renderer.tar.gz"), filepath.Join(runner, "install-manifest.json")} {
152 kept, err := os.ReadFile(path)
153 must(err)
154 write(path, "rebuilt")
155 if gate("verify") == nil {
156 t.Fatal("verified a rebuilt " + path)
157 }
158 must(os.WriteFile(path, kept, 0600))
159 }
160 write(instance+".COMMIT", "abc1234other\n")
161 if gate("verify") == nil {
162 t.Fatal("verified artifacts at another commit")
163 }
164 write(instance+".COMMIT", "abc1234full\n")
165 must(gate("verify"))
166 if gate("check") == nil {
167 t.Fatal("deploy allowed without a staging record")
168 }
169 // A record must name what staging ran, and only the release's bytes.
170 if gate("record") == nil {
171 t.Fatal("recorded without the hashes staging ran")
172 }
173 if gate("record", "--instance", hash("instance"), "--runner", hash("an older runner"), "--tool-proxy", hash("proxy")) == nil {
174 t.Fatal("recorded a release staging did not run")
175 }
176 if gate("check") == nil {
177 t.Fatal("a refused record allowed a deploy")
178 }
179 must(gate("record", "--instance", hash("instance"), "--runner", hash("runner"), "--tool-proxy", hash("proxy")))
180 must(gate("check"))
181 write(filepath.Join(runner, "equipo-runner"), "rebuilt runner")
182 if gate("check") == nil {
183 t.Fatal("deploy allowed with a runner staging never ran")
184 }
185 write(filepath.Join(runner, "equipo-runner"), "runner")
186 // Both targets are recorded: a rebuilt Mac runner is not what staging
187 // recorded either.
188 write(filepath.Join(runner, "darwin-arm64", "equipo-runner"), "rebuilt mac runner")
189 if gate("check") == nil {
190 t.Fatal("deploy allowed with a Mac runner other than the recorded one")
191 }
192 write(filepath.Join(runner, "darwin-arm64", "equipo-runner"), "mac runner")
193 write(filepath.Join(runner, "darwin-arm64", "gitleaks"), "mac scanner")
194 must(gate("check"))
195 write(instance, "rebuilt instance")
196 if gate("check") == nil {
197 t.Fatal("deploy allowed with an instance staging never ran")
198 }
199}
200