package release import ( "crypto/sha256" "encoding/json" "fmt" "io" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/testci" ) func TestOnboardingInstallPackaging(t *testing.T) { root := t.TempDir() // The source manifest names no tool proxy; packaging records the one // it finds beside the runner. m := fixtureManifest([]byte("fixture helper")) binary := []byte("fixture binary, never executed") write := func(name string, b []byte) { t.Helper() if e := os.WriteFile(filepath.Join(root, name), b, 0600); e != nil { t.Fatal(e) } } raw, _ := json.Marshal(BuildManifest()) write("manifest.json", raw) write("equipo-runner", binary) write("gitleaks", binary) write("equipo-tool-proxy", []byte("fixture helper")) renderer := writeRendererTar(t, filepath.Join(root, "renderer.tar.gz"), "dir usr\n") script, e := os.ReadFile("../../packaging/install/install-v1.sh") if e != nil { t.Fatal(e) } write("install-v1.sh", script) testci.Files(t, "../../scripts/runner-install-manifest.py") cmd := exec.Command("python3", "../../scripts/runner-install-manifest.py", root, "fixture-16") if b, e := cmd.CombinedOutput(); e != nil { t.Fatalf("packaging %v %s", e, b) } raw, e = os.ReadFile(filepath.Join(root, "install-manifest.json")) if e != nil { t.Fatal(e) } var manifest InstallManifest if json.Unmarshal(raw, &manifest) != nil || manifest.Validate("fixture-16") != nil { t.Fatal("packaged policy differs from actual runner", string(raw)) } if manifest.Gitleaks != GitleaksVersion || manifest.Targets[TargetLinux].GitleaksSHA256 != fmt.Sprintf("%x", sha256.Sum256(binary)) { t.Fatal("scanner missing from release", manifest) } // The Linux target names the renderer tree by its MANIFEST, the pin // the runner is linked with. if manifest.Targets[TargetLinux].RendererSHA256 != renderer { t.Fatal("renderer pin missing from release", manifest) } // And the archive by its own digest and size: what the runner fetches // from the forge. 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)) { t.Fatal("renderer archive missing from release", manifest.Targets[TargetLinux], err) } if manifest.RunnerSHA256 != fmt.Sprintf("%x", sha256.Sum256(binary)) { t.Fatal("checksum mismatch") } hashes := 0 assets := InstallAssets{Root: root, Version: "fixture-16", Manifest: m, digest: func(r io.Reader) (string, error) { hashes++ h := sha256.New() _, e := io.Copy(h, r) return fmt.Sprintf("%x", h.Sum(nil)), e }} if !assets.Available() { t.Fatal("fixture assets unavailable") } for _, name := range []string{"install-v1.sh", "install-manifest.json", "equipo-runner", "equipo-tool-proxy", "gitleaks"} { w := httptest.NewRecorder() assets.ServeHTTP(w, httptest.NewRequest("GET", "/releases/fixture-16/"+name, nil)) if w.Code != 200 || w.Body.Len() == 0 { t.Fatal("missing release asset", name, w.Code) } } for range 3 { if !assets.Available() || !assets.CachedAvailable() { t.Fatal("cached assets unavailable") } } if hashes != 3 { t.Fatalf("rehashed unchanged assets: %d", hashes) } w := httptest.NewRecorder() req := httptest.NewRequest("GET", "/releases/fixture-16/equipo-runner", nil) req.Header.Set("Range", "bytes=0-6") assets.ServeHTTP(w, req) if w.Code != 206 || w.Body.String() != "fixture" { t.Fatal("missing streamed range", w.Code, w.Body.String()) } write("install-manifest.json", append(append([]byte(nil), raw...), []byte("{}")...)) if assets.Available() { t.Fatal("trailing manifest data accepted") } write("install-manifest.json", raw) if !assets.Available() { t.Fatal("restored manifest unavailable") } hashesBeforeMetadata := hashes // Same inode, changed size/mtime must invalidate without hashing on the page. write("equipo-runner", []byte("changed")) if assets.CachedAvailable() { t.Fatal("cached availability ignored changed file") } if hashes != hashesBeforeMetadata { t.Fatal("onboarding availability hashed files") } if assets.Available() { t.Fatal("tampered binary served") } for _, bad := range []string{"http://equipo.test", "https://equipo.test/$(touch bad)", "https://equipo.test'", "https://equipo.test#"} { if _, e := InstallCommand(bad, "fixture-16"); e == nil { t.Fatal("origin interpolation", bad) } } command, e := InstallCommand("https://equipo.test", "fixture-16", "team") if e != nil || !strings.Contains(command, "/releases/fixture-16/install-v1.sh") || !strings.HasSuffix(command, "'team'") { t.Fatal("unversioned install", command, e) } manifest.NamespaceArgs = []string{"--unshare-user"} if manifest.Validate("fixture-16") == nil { t.Fatal("weakened namespace policy") } manifest.NamespaceArgs = provider.NamespaceArguments() if manifest.Validate("fixture-16") != nil { t.Fatal("restored manifest refused") } // A release that recorded no tool proxy, or another one, installs nothing. manifest.Release = BuildManifest() if manifest.Validate("fixture-16") == nil { t.Fatal("a release with no recorded tool proxy accepted") } manifest.Release = fixtureManifest([]byte("another helper")) if manifest.Validate("fixture-16") == nil { t.Fatal("a tool proxy the release did not record accepted") } // The served proxy must be the recorded one. write("install-manifest.json", raw) write("equipo-runner", binary) write("gitleaks", binary) write("equipo-tool-proxy", []byte("another helper")) if assets.Available() { t.Fatal("a tool proxy the release did not record served") } } // A release with a Mac worker lists both targets with their digests and // serves the darwin files under darwin-arm64/; a darwin file that does // not match its digest is not served, while the Linux runner still is. func TestInstallPackagingDarwinTarget(t *testing.T) { root := t.TempDir() // The source manifest names no tool proxy; packaging records the one // it finds beside the runner. m := fixtureManifest([]byte("fixture helper")) write := func(name string, b []byte) { t.Helper() if e := os.MkdirAll(filepath.Dir(filepath.Join(root, name)), 0700); e != nil { t.Fatal(e) } if e := os.WriteFile(filepath.Join(root, name), b, 0600); e != nil { t.Fatal(e) } } raw, _ := json.Marshal(BuildManifest()) write("manifest.json", raw) write("equipo-runner", []byte("linux runner")) write("gitleaks", []byte("linux scanner")) write("equipo-tool-proxy", []byte("fixture helper")) write("install-v1.sh", []byte("#!/bin/sh\n")) write("darwin-arm64/equipo-runner", []byte("mac runner")) write("darwin-arm64/gitleaks", []byte("mac scanner")) writeRendererTar(t, filepath.Join(root, "renderer.tar.gz"), "dir usr\n") testci.Files(t, "../../scripts/runner-install-manifest.py") if b, e := exec.Command("python3", "../../scripts/runner-install-manifest.py", root, "fixture-16").CombinedOutput(); e != nil { t.Fatalf("packaging %v %s", e, b) } raw, _ = os.ReadFile(filepath.Join(root, "install-manifest.json")) var manifest InstallManifest if json.Unmarshal(raw, &manifest) != nil || manifest.Validate("fixture-16") != nil { t.Fatalf("linux manifest broken: %s", raw) } mac, err := manifest.DarwinTarget("fixture-16") // A Mac worker has no tool proxy and no renderer. if err != nil || mac.RunnerSHA256 != fmt.Sprintf("%x", sha256.Sum256([]byte("mac runner"))) || mac.ToolProxySHA256 != "" || mac.RendererSHA256 != "" { t.Fatalf("darwin target %+v %v", mac, err) } if linux := manifest.Targets["linux/amd64"]; linux.RunnerSHA256 != manifest.RunnerSHA256 { t.Fatalf("linux target %+v", linux) } if _, err := manifest.DarwinTarget("fixture-17"); err == nil { t.Fatal("another version's target accepted") } get := func(name string) (int, string) { w := httptest.NewRecorder() a := InstallAssets{Root: root, Version: "fixture-16", Manifest: m} a.ServeHTTP(w, httptest.NewRequest("GET", "/releases/fixture-16/"+name, nil)) return w.Code, w.Body.String() } if code, body := get("darwin-arm64/equipo-runner"); code != 200 || body != "mac runner" { t.Fatalf("mac runner %d %q", code, body) } if manifest.Gitleaks != GitleaksVersion || mac.GitleaksSHA256 != fmt.Sprintf("%x", sha256.Sum256([]byte("mac scanner"))) { t.Fatal("Mac scanner pin missing", manifest) } for _, name := range []string{"gitleaks", "darwin-arm64/gitleaks"} { if code, body := get(name); code != 200 || !strings.Contains(body, "scanner") { t.Fatal("scanner not served", name, code, body) } } write("darwin-arm64/gitleaks", []byte("replaced scanner")) if code, _ := get("darwin-arm64/gitleaks"); code != 404 { t.Fatal("changed Mac scanner served", code) } write("darwin-arm64/gitleaks", []byte("mac scanner")) if code, _ := get("darwin-arm64/../equipo-runner"); code == 200 { t.Fatal("path outside the served names") } write("darwin-arm64/equipo-runner", []byte("tampered")) if code, _ := get("darwin-arm64/equipo-runner"); code != 404 { t.Fatalf("tampered mac runner served: %d", code) } if code, body := get("equipo-runner"); code != 200 || body != "linux runner" { t.Fatalf("linux runner withheld: %d", code) } }