package agentimage import ( "context" "net/http/httptest" "os" "path/filepath" "slices" "strings" "testing" "time" "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/registry" v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/remote" "git.zb-server.de/ZB-Server/uptimesebi/internal/signing" ) func fakeDist(t *testing.T, version string, bins ...string) string { t.Helper() dist := t.TempDir() for _, p := range Platforms { if err := os.MkdirAll(filepath.Join(dist, p.Dir), 0o755); err != nil { t.Fatal(err) } for _, b := range bins { if err := os.WriteFile(filepath.Join(dist, p.Dir, b), []byte(b+" "+version+" for "+p.Dir), 0o755); err != nil { t.Fatal(err) } } } return dist } func TestBuild(t *testing.T) { o := Options{ Name: "statuspage", Dist: fakeDist(t, "v1.2.3", "statuspage", "launcher"), Version: "v1.2.3", Commit: "abc123", Env: []string{"UPTIME_STATUSPAGE_CACHE_DIR=/var/lib/uptimesebi"}, Ports: []string{"8082/tcp"}, Created: time.Unix(1700000000, 0).UTC(), } for _, launcher := range []bool{false, true} { o.Launcher = launcher idx, err := Build(o) if err != nil { t.Fatal(err) } m, err := idx.IndexManifest() if err != nil { t.Fatal(err) } if len(m.Manifests) != len(Platforms) { t.Fatalf("index has %d manifests", len(m.Manifests)) } for i, desc := range m.Manifests { want := Platforms[i] if desc.Platform == nil || !desc.Platform.Equals(want.Platform) { t.Errorf("manifest %d platform = %v, want %v", i, desc.Platform, want.Platform) } img, err := idx.Image(desc.Digest) if err != nil { t.Fatal(err) } cf, err := img.ConfigFile() if err != nil { t.Fatal(err) } entry := []string{"/usr/local/bin/statuspage"} if launcher { entry = []string{"/usr/local/bin/launcher"} } if cf.Architecture != want.Architecture || cf.Variant != want.Variant || cf.Config.User != "65532:65532" || !slices.Equal(cf.Config.Entrypoint, entry) || !slices.Equal(cf.Config.Healthcheck.Test, []string{"CMD", "/usr/local/bin/statuspage", "health"}) || !slices.Contains(cf.Config.Env, "UPTIME_STATUSPAGE_CACHE_DIR=/var/lib/uptimesebi") || slices.Contains(cf.Config.Env, "UPTIME_LAUNCHER_AGENT=statuspage") != launcher || cf.Config.Labels["org.opencontainers.image.version"] != "v1.2.3" { t.Errorf("%s launcher=%v config = %+v", want.Dir, launcher, cf.Config) } bin, err := ReadFile(img, "usr/local/bin/statuspage", 1<<20) if err != nil || string(bin) != "statuspage v1.2.3 for "+want.Dir { t.Errorf("%s binary = %q, %v", want.Dir, bin, err) } _, err = ReadFile(img, "usr/local/bin/launcher", 1<<20) if (err == nil) != launcher { t.Errorf("launcher=%v but launcher file err = %v", launcher, err) } } } // The same input gives the same image. o.Launcher = false a, _ := Build(o) b, _ := Build(o) d1, _ := a.Digest() d2, _ := b.Digest() if d1 != d2 { t.Errorf("rebuild changed the digest: %s, %s", d1, d2) } o.Dist = t.TempDir() if _, err := Build(o); err == nil { t.Error("built without binaries") } } func TestChannelTags(t *testing.T) { for _, c := range []struct { ref, suffix string want []string }{ {"refs/heads/main", "", []string{"20261123180000-0123456789ab", "edge"}}, {"refs/tags/v1.4.2", "", []string{"1.4.2", "1.4", "stable", "latest"}}, {"refs/tags/v1.5.0-beta.3", "", []string{"1.5.0-beta.3", "beta"}}, {"refs/tags/v1.4.2", "autoupdate", []string{"1.4.2-autoupdate", "1.4-autoupdate", "stable-autoupdate", "latest-autoupdate"}}, {"refs/tags/nightly", "", nil}, {"refs/pull/7/merge", "", nil}, {"refs/heads/feature", "", nil}, } { if got := ChannelTags(c.ref, "20261123180000-0123456789ab", c.suffix); !slices.Equal(got, c.want) { t.Errorf("%s %s: tags = %v, want %v", c.ref, c.suffix, got, c.want) } } } // publishSigned builds and pushes a checker release to the registry. func publishSigned(t *testing.T, host, version string, key signing.PrivateKey) v1.Hash { t.Helper() repo := host + "/zb/uptimesebi-checker" idx, err := Build(Options{Name: "checker", Dist: fakeDist(t, version, "checker"), Version: version}) if err != nil { t.Fatal(err) } ctx := context.Background() tags := ChannelTags("refs/tags/"+version, version, "") if err := Publish(ctx, idx, repo, tags, Credentials{}); err != nil { t.Fatal(err) } if err := Sign(ctx, idx, repo, "checker", version, key, Credentials{}); err != nil { t.Fatal(err) } d, _ := idx.Digest() return d } func TestSignFetch(t *testing.T) { reg := httptest.NewServer(registry.New()) defer reg.Close() host := strings.TrimPrefix(reg.URL, "http://") repo := host + "/zb/uptimesebi-checker" key, _ := signing.GenerateKey() keys := []signing.PublicKey{key.Public()} ctx := context.Background() piZero := v1.Platform{OS: "linux", Architecture: "arm", Variant: "v6"} digest := publishSigned(t, host, "v1.0.0", key) for _, tag := range []string{"1.0.0", "latest"} { ref, _ := name.NewTag(repo + ":" + tag) if desc, err := remote.Get(ref); err != nil || desc.Digest != digest { t.Errorf("%s = %v, %v", tag, desc, err) } } bin, err := Fetch(ctx, repo+":1.0.0", "checker", "v1.0.0", piZero, keys) if err != nil || string(bin) != "checker v1.0.0 for linux_arm_v6" { t.Fatalf("Fetch = %q, %v", bin, err) } if _, err := Fetch(ctx, repo+":1.0.0", "checker", "v1.0.1", piZero, keys); err == nil || !strings.Contains(err.Error(), "signed as") { t.Errorf("an old release passed off as a new version: %v", err) } other, _ := signing.GenerateKey() if _, err := Fetch(ctx, repo+":1.0.0", "checker", "v1.0.0", piZero, []signing.PublicKey{other.Public()}); err == nil { t.Error("accepted a signature by an unknown key") } if _, err := Fetch(ctx, repo+":1.0.0", "statuspage", "v1.0.0", piZero, keys); err == nil { t.Error("the checker's signature passed for the status page") } if _, err := Fetch(ctx, repo+":1.0.0", "checker", "v1.0.0", v1.Platform{OS: "linux", Architecture: "riscv64"}, keys); err == nil { t.Error("fetched for a platform without image") } // An attacker who can push moves the tag to an image signed by no one. evil, _ := Build(Options{Name: "checker", Dist: fakeDist(t, "evil", "checker"), Version: "v1.0.0"}) if err := Publish(ctx, evil, repo, []string{"1.0.0"}, Credentials{}); err != nil { t.Fatal(err) } if _, err := Fetch(ctx, repo+":1.0.0", "checker", "v1.0.0", piZero, keys); err == nil || !strings.Contains(err.Error(), "no signature") { t.Errorf("unsigned image accepted: %v", err) } // ...or signs it with a key of their own. if err := Sign(ctx, evil, repo, "checker", "v1.0.0", other, Credentials{}); err != nil { t.Fatal(err) } if _, err := Fetch(ctx, repo+":1.0.0", "checker", "v1.0.0", piZero, keys); err == nil { t.Error("image signed by a foreign key accepted") } }