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/json"
6 "fmt"
7 "io"
8 "net/http/httptest"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "strings"
13 "testing"
14
15 "amadan.net/oficina/equipo/internal/runner/provider"
16 "amadan.net/oficina/equipo/internal/testci"
17)
18
19func TestOnboardingInstallPackaging(t *testing.T) {
20 root := t.TempDir()
21 // The source manifest names no tool proxy; packaging records the one
22 // it finds beside the runner.
23 m := fixtureManifest([]byte("fixture helper"))
24 binary := []byte("fixture binary, never executed")
25 write := func(name string, b []byte) {
26 t.Helper()
27 if e := os.WriteFile(filepath.Join(root, name), b, 0600); e != nil {
28 t.Fatal(e)
29 }
30 }
31 raw, _ := json.Marshal(BuildManifest())
32 write("manifest.json", raw)
33 write("equipo-runner", binary)
34 write("gitleaks", binary)
35 write("equipo-tool-proxy", []byte("fixture helper"))
36 renderer := writeRendererTar(t, filepath.Join(root, "renderer.tar.gz"), "dir usr\n")
37 script, e := os.ReadFile("../../packaging/install/install-v1.sh")
38 if e != nil {
39 t.Fatal(e)
40 }
41 write("install-v1.sh", script)
42 testci.Files(t, "../../scripts/runner-install-manifest.py")
43 cmd := exec.Command("python3", "../../scripts/runner-install-manifest.py", root, "fixture-16")
44 if b, e := cmd.CombinedOutput(); e != nil {
45 t.Fatalf("packaging %v %s", e, b)
46 }
47 raw, e = os.ReadFile(filepath.Join(root, "install-manifest.json"))
48 if e != nil {
49 t.Fatal(e)
50 }
51 var manifest InstallManifest
52 if json.Unmarshal(raw, &manifest) != nil || manifest.Validate("fixture-16") != nil {
53 t.Fatal("packaged policy differs from actual runner", string(raw))
54 }
55 if manifest.Gitleaks != GitleaksVersion || manifest.Targets[TargetLinux].GitleaksSHA256 != fmt.Sprintf("%x", sha256.Sum256(binary)) {
56 t.Fatal("scanner missing from release", manifest)
57 }
58 // The Linux target names the renderer tree by its MANIFEST, the pin
59 // the runner is linked with.
60 if manifest.Targets[TargetLinux].RendererSHA256 != renderer {
61 t.Fatal("renderer pin missing from release", manifest)
62 }
63 // And the archive by its own digest and size: what the runner fetches
64 // from the forge.
65 if tarball, err := os.ReadFile(filepath.Join(root, "renderer.tar.gz")); err != nil || manifest.Targets[TargetLinux].RendererArchiveSHA256 != fmt.Sprintf("%x", sha256.Sum256(tarball)) || manifest.Targets[TargetLinux].RendererArchiveSize != int64(len(tarball)) {
66 t.Fatal("renderer archive missing from release", manifest.Targets[TargetLinux], err)
67 }
68 if manifest.RunnerSHA256 != fmt.Sprintf("%x", sha256.Sum256(binary)) {
69 t.Fatal("checksum mismatch")
70 }
71 hashes := 0
72 assets := InstallAssets{Root: root, Version: "fixture-16", Manifest: m, digest: func(r io.Reader) (string, error) {
73 hashes++
74 h := sha256.New()
75 _, e := io.Copy(h, r)
76 return fmt.Sprintf("%x", h.Sum(nil)), e
77 }}
78 if !assets.Available() {
79 t.Fatal("fixture assets unavailable")
80 }
81 for _, name := range []string{"install-v1.sh", "install-manifest.json", "equipo-runner", "equipo-tool-proxy", "gitleaks"} {
82 w := httptest.NewRecorder()
83 assets.ServeHTTP(w, httptest.NewRequest("GET", "/releases/fixture-16/"+name, nil))
84 if w.Code != 200 || w.Body.Len() == 0 {
85 t.Fatal("missing release asset", name, w.Code)
86 }
87 }
88 for range 3 {
89 if !assets.Available() || !assets.CachedAvailable() {
90 t.Fatal("cached assets unavailable")
91 }
92 }
93 if hashes != 3 {
94 t.Fatalf("rehashed unchanged assets: %d", hashes)
95 }
96 w := httptest.NewRecorder()
97 req := httptest.NewRequest("GET", "/releases/fixture-16/equipo-runner", nil)
98 req.Header.Set("Range", "bytes=0-6")
99 assets.ServeHTTP(w, req)
100 if w.Code != 206 || w.Body.String() != "fixture" {
101 t.Fatal("missing streamed range", w.Code, w.Body.String())
102 }
103 write("install-manifest.json", append(append([]byte(nil), raw...), []byte("{}")...))
104 if assets.Available() {
105 t.Fatal("trailing manifest data accepted")
106 }
107 write("install-manifest.json", raw)
108 if !assets.Available() {
109 t.Fatal("restored manifest unavailable")
110 }
111 hashesBeforeMetadata := hashes
112 // Same inode, changed size/mtime must invalidate without hashing on the page.
113 write("equipo-runner", []byte("changed"))
114 if assets.CachedAvailable() {
115 t.Fatal("cached availability ignored changed file")
116 }
117 if hashes != hashesBeforeMetadata {
118 t.Fatal("onboarding availability hashed files")
119 }
120 if assets.Available() {
121 t.Fatal("tampered binary served")
122 }
123 for _, bad := range []string{"http://equipo.test", "https://equipo.test/$(touch bad)", "https://equipo.test'", "https://equipo.test#"} {
124 if _, e := InstallCommand(bad, "fixture-16"); e == nil {
125 t.Fatal("origin interpolation", bad)
126 }
127 }
128 command, e := InstallCommand("https://equipo.test", "fixture-16", "team")
129 if e != nil || !strings.Contains(command, "/releases/fixture-16/install-v1.sh") || !strings.HasSuffix(command, "'team'") {
130 t.Fatal("unversioned install", command, e)
131 }
132 manifest.NamespaceArgs = []string{"--unshare-user"}
133 if manifest.Validate("fixture-16") == nil {
134 t.Fatal("weakened namespace policy")
135 }
136 manifest.NamespaceArgs = provider.NamespaceArguments()
137 if manifest.Validate("fixture-16") != nil {
138 t.Fatal("restored manifest refused")
139 }
140 // A release that recorded no tool proxy, or another one, installs nothing.
141 manifest.Release = BuildManifest()
142 if manifest.Validate("fixture-16") == nil {
143 t.Fatal("a release with no recorded tool proxy accepted")
144 }
145 manifest.Release = fixtureManifest([]byte("another helper"))
146 if manifest.Validate("fixture-16") == nil {
147 t.Fatal("a tool proxy the release did not record accepted")
148 }
149 // The served proxy must be the recorded one.
150 write("install-manifest.json", raw)
151 write("equipo-runner", binary)
152 write("gitleaks", binary)
153 write("equipo-tool-proxy", []byte("another helper"))
154 if assets.Available() {
155 t.Fatal("a tool proxy the release did not record served")
156 }
157}
158
159// A release with a Mac worker lists both targets with their digests and
160// serves the darwin files under darwin-arm64/; a darwin file that does
161// not match its digest is not served, while the Linux runner still is.
162func TestInstallPackagingDarwinTarget(t *testing.T) {
163 root := t.TempDir()
164 // The source manifest names no tool proxy; packaging records the one
165 // it finds beside the runner.
166 m := fixtureManifest([]byte("fixture helper"))
167 write := func(name string, b []byte) {
168 t.Helper()
169 if e := os.MkdirAll(filepath.Dir(filepath.Join(root, name)), 0700); e != nil {
170 t.Fatal(e)
171 }
172 if e := os.WriteFile(filepath.Join(root, name), b, 0600); e != nil {
173 t.Fatal(e)
174 }
175 }
176 raw, _ := json.Marshal(BuildManifest())
177 write("manifest.json", raw)
178 write("equipo-runner", []byte("linux runner"))
179 write("gitleaks", []byte("linux scanner"))
180 write("equipo-tool-proxy", []byte("fixture helper"))
181 write("install-v1.sh", []byte("#!/bin/sh\n"))
182 write("darwin-arm64/equipo-runner", []byte("mac runner"))
183 write("darwin-arm64/gitleaks", []byte("mac scanner"))
184 writeRendererTar(t, filepath.Join(root, "renderer.tar.gz"), "dir usr\n")
185 testci.Files(t, "../../scripts/runner-install-manifest.py")
186 if b, e := exec.Command("python3", "../../scripts/runner-install-manifest.py", root, "fixture-16").CombinedOutput(); e != nil {
187 t.Fatalf("packaging %v %s", e, b)
188 }
189 raw, _ = os.ReadFile(filepath.Join(root, "install-manifest.json"))
190 var manifest InstallManifest
191 if json.Unmarshal(raw, &manifest) != nil || manifest.Validate("fixture-16") != nil {
192 t.Fatalf("linux manifest broken: %s", raw)
193 }
194 mac, err := manifest.DarwinTarget("fixture-16")
195 // A Mac worker has no tool proxy and no renderer.
196 if err != nil || mac.RunnerSHA256 != fmt.Sprintf("%x", sha256.Sum256([]byte("mac runner"))) || mac.ToolProxySHA256 != "" || mac.RendererSHA256 != "" {
197 t.Fatalf("darwin target %+v %v", mac, err)
198 }
199 if linux := manifest.Targets["linux/amd64"]; linux.RunnerSHA256 != manifest.RunnerSHA256 {
200 t.Fatalf("linux target %+v", linux)
201 }
202 if _, err := manifest.DarwinTarget("fixture-17"); err == nil {
203 t.Fatal("another version's target accepted")
204 }
205 get := func(name string) (int, string) {
206 w := httptest.NewRecorder()
207 a := InstallAssets{Root: root, Version: "fixture-16", Manifest: m}
208 a.ServeHTTP(w, httptest.NewRequest("GET", "/releases/fixture-16/"+name, nil))
209 return w.Code, w.Body.String()
210 }
211 if code, body := get("darwin-arm64/equipo-runner"); code != 200 || body != "mac runner" {
212 t.Fatalf("mac runner %d %q", code, body)
213 }
214 if manifest.Gitleaks != GitleaksVersion || mac.GitleaksSHA256 != fmt.Sprintf("%x", sha256.Sum256([]byte("mac scanner"))) {
215 t.Fatal("Mac scanner pin missing", manifest)
216 }
217 for _, name := range []string{"gitleaks", "darwin-arm64/gitleaks"} {
218 if code, body := get(name); code != 200 || !strings.Contains(body, "scanner") {
219 t.Fatal("scanner not served", name, code, body)
220 }
221 }
222 write("darwin-arm64/gitleaks", []byte("replaced scanner"))
223 if code, _ := get("darwin-arm64/gitleaks"); code != 404 {
224 t.Fatal("changed Mac scanner served", code)
225 }
226 write("darwin-arm64/gitleaks", []byte("mac scanner"))
227 if code, _ := get("darwin-arm64/../equipo-runner"); code == 200 {
228 t.Fatal("path outside the served names")
229 }
230 write("darwin-arm64/equipo-runner", []byte("tampered"))
231 if code, _ := get("darwin-arm64/equipo-runner"); code != 404 {
232 t.Fatalf("tampered mac runner served: %d", code)
233 }
234 if code, body := get("equipo-runner"); code != 200 || body != "linux runner" {
235 t.Fatalf("linux runner withheld: %d", code)
236 }
237}
238