A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(rexctl): pin 3rd party images so they are upgraded (#3870)
Zoey Gieglera3106953ea5a
diff --git a/.github/workflows/release-image-set.yaml b/.github/workflows/release-image-set.yamlindex e0e7b92d7..c3ab5b74b 100644--- a/.github/workflows/release-image-set.yaml+++ b/.github/workflows/release-image-set.yaml@@ -215,11 +215,11 @@ jobs:key: layer-sizes-${{ github.run_id }}restore-keys: layer-sizes-- - name: add image sizes to the image set+ - name: add image sizes to the image set and also pin 3rd party imagesworking-directory: fluxer_rexctlenv:GHCR_TOKEN: ${{ github.token }}- run: go run ./internal/stack/layersizes -set internal/stack/imageset.json -cache "${RUNNER_TEMP}/layer-sizes.json"+ run: go run ./internal/stack/layersizes -pin -set internal/stack/imageset.json -cache "${RUNNER_TEMP}/layer-sizes.json"- name: govulncheckworking-directory: fluxer_rexctldiff --git a/fluxer_rexctl/internal/backup/create.go b/fluxer_rexctl/internal/backup/create.goindex f88797e73..c243f03e1 100644--- a/fluxer_rexctl/internal/backup/create.go+++ b/fluxer_rexctl/internal/backup/create.go@@ -346,11 +346,15 @@ func uploadsRef(v volumeRef, have []engine.Volume, mark bool) *UploadsRef {}func UploadsDefinition(set *stack.Set, st *instance.State) string {- def, err := stack.ServicesDefinition(set, stack.OverlaysFor(st.Settings), st.Images.Refs, stack.UploadsServices...)+ refs := maps.Clone(st.Images.Refs)+ for _, svc := range stack.UploadsServices {+ delete(refs, svc)+ }+ def, err := stack.ServicesDefinition(set, stack.OverlaysFor(st.Settings), refs, stack.UploadsServices...)if err != nil {return ""}- return stack.Hash([]byte(settings.ImageRef(st.Settings, st.Images.Refs, "seaweedfs") + "\n" + def))+ return stack.Hash([]byte(settings.ImageRef(st.Settings, refs, "seaweedfs") + "\n" + def))}func uploadsDefinition(inst *instance.Instance) string {diff --git a/fluxer_rexctl/internal/backup/uploadsdefinition_test.go b/fluxer_rexctl/internal/backup/uploadsdefinition_test.gonew file mode 100644index 000000000..67af98423--- /dev/null+++ b/fluxer_rexctl/internal/backup/uploadsdefinition_test.go@@ -0,0 +1,39 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package backup++import (+ "maps"+ "strings"+ "testing"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"+)++func TestUploadsDefinitionIgnoresThePinnedSeaweedfsDigest(t *testing.T) {+ set := stack.Embedded()+ digest := "@sha256:" + strings.Repeat("ab", 32)+ fluxer := map[string]string{}+ for svc := range stack.RegistryServices() {+ fluxer[svc] = "ghcr.io/fluxerapp/fluxer-" + svc + digest+ }+ pinned := maps.Clone(fluxer)+ pinned["seaweedfs"] = "chrislusf/seaweedfs:4.47" + digest+ pinned["seaweedfs-init"] = pinned["seaweedfs"]+ moved := maps.Clone(pinned)+ moved["seaweedfs"] = "chrislusf/seaweedfs:4.47@sha256:" + strings.Repeat("cd", 32)+ moved["seaweedfs-init"] = moved["seaweedfs"]+ state := func(refs map[string]string) *instance.State {+ return &instance.State{Settings: map[string]string{}, Images: instance.Images{Refs: refs}}+ }+ want := UploadsDefinition(set, state(fluxer))+ if want == "" {+ t.Fatal("no uploads definition")+ }+ for name, refs := range map[string]map[string]string{"pinned": pinned, "moved": moved} {+ if got := UploadsDefinition(set, state(refs)); got != want {+ t.Fatalf("%s seaweedfs image changed the uploads definition: %s, want %s", name, got, want)+ }+ }+}diff --git a/fluxer_rexctl/internal/checks/disk.go b/fluxer_rexctl/internal/checks/disk.goindex cf480ef9d..7285689c3 100644--- a/fluxer_rexctl/internal/checks/disk.go+++ b/fluxer_rexctl/internal/checks/disk.go@@ -324,7 +324,13 @@ func imageFor(svc, value string, fluxer map[string]string, chosen func(string) sreturn svc}def, ok := stack.ImageDefault(value)+ if pinned := fluxer[svc]; pinned != "" {+ def = pinned+ }if !ok {+ if def != "" {+ return def+ }return value}name := strings.TrimPrefix(strings.SplitN(value, ":-", 2)[0], "${")diff --git a/fluxer_rexctl/internal/checks/disk_test.go b/fluxer_rexctl/internal/checks/disk_test.goindex 888761379..747846d92 100644--- a/fluxer_rexctl/internal/checks/disk_test.go+++ b/fluxer_rexctl/internal/checks/disk_test.go@@ -38,3 +38,17 @@ func TestRunningRefsLeavesOutServicesThatDontRun(t *testing.T) {t.Fatalf("a stack without voice or object store still runs them: %v", lean)}}++func TestRunningRefsUsesThePinned3rdPartyImage(t *testing.T) {+ set := stack.Embedded()+ pinned := "postgres:16-alpine@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"+ refs := RunningRefs(set, map[string]string{"postgres": pinned}, Running{})+ if !slices.Contains(refs, pinned) || slices.Contains(refs, "postgres:16-alpine") {+ t.Fatalf("didn't use the pinned postgres image: %v", refs)+ }+ chosen := map[string]string{"FLUXER_POSTGRES_IMAGE": "postgres:16.9-alpine"}+ refs = RunningRefs(set, map[string]string{"postgres": pinned}, Running{Chosen: func(k string) string { return chosen[k] }})+ if !slices.Contains(refs, "postgres:16.9-alpine") || slices.Contains(refs, pinned) {+ t.Fatalf("used the pinned image instead of FLUXER_POSTGRES_IMAGE: %v", refs)+ }+}diff --git a/fluxer_rexctl/internal/instance/eject.go b/fluxer_rexctl/internal/instance/eject.goindex a055ca779..5edbbbc98 100644--- a/fluxer_rexctl/internal/instance/eject.go+++ b/fluxer_rexctl/internal/instance/eject.go@@ -228,12 +228,13 @@ func handUpdate(ctx context.Context, dir string, e *engine.Engine, s *State, cusreturn nil, err}own := stack.ComposeImages(custom)+ registry := stack.RegistryServices()var updated *stack.ImageSetfor _, seen := range []map[string]string{stack.ComposeImages(compose), live.images} {maps.DeleteFunc(seen, func(svc, ref string) bool {_, fluxer := shipped.Images[svc]_, mine := own[svc]- return !fluxer || mine || strings.Contains(ref, "${")+ return !fluxer || !registry[svc] || mine || strings.Contains(ref, "${")})switch {case sameImages(seen, s.Images.Refs):diff --git a/fluxer_rexctl/internal/instance/import.go b/fluxer_rexctl/internal/instance/import.goindex d9d1021c5..55c4e205a 100644--- a/fluxer_rexctl/internal/instance/import.go+++ b/fluxer_rexctl/internal/instance/import.go@@ -263,8 +263,9 @@ func setsImage(custom []byte, images map[string]string) bool {if err := yaml.Unmarshal(custom, &doc); err != nil {return true}+ fluxer := stack.RegistryServices()for name, svc := range doc.Services {- if _, ok := svc["image"]; ok && images[name] != "" {+ if _, ok := svc["image"]; ok && fluxer[name] && images[name] != "" {return true}}diff --git a/fluxer_rexctl/internal/instance/import_images_test.go b/fluxer_rexctl/internal/instance/import_images_test.gonew file mode 100644index 000000000..3fc3b31b2--- /dev/null+++ b/fluxer_rexctl/internal/instance/import_images_test.go@@ -0,0 +1,15 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package instance++import "testing"++func TestSetsImageOnlyCountsFluxerImages(t *testing.T) {+ images := map[string]string{"api": "ghcr.io/fluxerapp/fluxer-api@sha256:1", "postgres": "postgres:16-alpine@sha256:2"}+ if setsImage([]byte("services:\n postgres:\n image: postgres:16.9-alpine\n"), images) {+ t.Fatal("custom postgres image counted as replacing a Fluxer image")+ }+ if !setsImage([]byte("services:\n api:\n image: example/api:dev\n"), images) {+ t.Fatal("custom api image didn't count")+ }+}diff --git a/fluxer_rexctl/internal/stack/images.go b/fluxer_rexctl/internal/stack/images.goindex 9eda3c46f..74142e79e 100644--- a/fluxer_rexctl/internal/stack/images.go+++ b/fluxer_rexctl/internal/stack/images.go@@ -221,11 +221,25 @@ func UnpinImages(compose []byte) []byte {if err != nil || prefix == "" {return compose}+ shippedValue := map[string]string{}+ for _, t := range templates {+ shippedValue[t.service] = t.value+ }lines := strings.Split(string(compose), "\n")for _, img := range found {- repo, digest, pinned := strings.Cut(img.value, "@sha256:")i := img.line - 1- if !pinned || len(digest) != 64 || i < 0 || i >= len(lines) || img.column-1 > len(lines[i]) {+ if i < 0 || i >= len(lines) || img.column-1 > len(lines[i]) {+ continue+ }+ if name, ref, ok := defaultedVar(img.value); ok {+ tmplName, _, tmplOK := defaultedVar(shippedValue[img.service])+ if tmplOK && tmplName == name && digestPinned(ref) {+ lines[i] = lines[i][:img.column-1] + shippedValue[img.service]+ }+ continue+ }+ repo, digest, pinned := strings.Cut(img.value, "@sha256:")+ if !pinned || len(digest) != 64 {continue}lines[i] = lines[i][:img.column-1] + prefix + path.Base(repo) + suffix@@ -233,6 +247,37 @@ func UnpinImages(compose []byte) []byte {return []byte(strings.Join(lines, "\n"))}+func RegistryServices() map[string]bool {+ out := map[string]bool{}+ compose, err := ReadFile(Embedded(), ComposeFile)+ if err != nil {+ return out+ }+ found, err := imageLines(compose)+ if err != nil {+ return out+ }+ for _, img := range found {+ if strings.HasPrefix(img.value, "${FLUXER_REGISTRY") {+ out[img.service] = true+ }+ }+ return out+}++func digestPinned(ref string) bool {+ _, digest, ok := strings.Cut(ref, "@sha256:")+ if !ok || len(digest) != 64 {+ return false+ }+ for _, r := range digest {+ if (r < '0' || r > '9') && (r < 'a' || r > 'f') {+ return false+ }+ }+ return true+}+func ImageDefault(value string) (string, bool) {_, def, ok := defaultedVar(value)return def, okdiff --git a/fluxer_rexctl/internal/stack/layersizes/main.go b/fluxer_rexctl/internal/stack/layersizes/main.goindex dc6db5699..b9aa3f098 100644--- a/fluxer_rexctl/internal/stack/layersizes/main.go+++ b/fluxer_rexctl/internal/stack/layersizes/main.go@@ -41,8 +41,9 @@ const (func main() {setPath := flag.String("set", "internal/stack/imageset.json", "the image set file to add sizes to")cachePath := flag.String("cache", "", "a file that keeps layer sizes between runs")+ pin := flag.Bool("pin", false, "pin 3rd party images in the compose file to the digest of the tag")flag.Parse()- if err := run(context.Background(), *setPath, *cachePath); err != nil {+ if err := run(context.Background(), *setPath, *cachePath, *pin); err != nil {fmt.Fprintln(os.Stderr, err)os.Exit(1)}@@ -76,7 +77,7 @@ type blob struct {diffID string}-func run(ctx context.Context, setPath, cachePath string) error {+func run(ctx context.Context, setPath, cachePath string, pin bool) error {data, err := os.ReadFile(setPath)if err != nil {return err@@ -85,10 +86,20 @@ func run(ctx context.Context, setPath, cachePath string) error {if err := json.Unmarshal(data, &set); err != nil {return err}- refs, err := thirdParty()+ third, err := thirdParty()if err != nil {return err}+ c := &client{http: &http.Client{Timeout: 30 * time.Minute}, tokens: map[string]string{}}+ if pin {+ if err := pinThirdParty(ctx, c, &set, third); err != nil {+ return err+ }+ }+ var refs []string+ if !pin {+ refs = slices.Collect(maps.Values(third))+ }for _, ref := range set.Images {refs = append(refs, ref)}@@ -104,7 +115,6 @@ func run(ctx context.Context, setPath, cachePath string) error {}}- c := &client{http: &http.Client{Timeout: 30 * time.Minute}, tokens: map[string]string{}}set.Tags = map[string]string{}set.Layers = map[string]stack.Layer{}set.Platforms = map[string]map[string][]string{}@@ -190,24 +200,63 @@ func run(ctx context.Context, setPath, cachePath string) error {return writeJSON(setPath, set)}-func thirdParty() ([]string, error) {+func thirdParty() (map[string]string, error) {compose, err := stack.ReadFile(stack.Embedded(), stack.ComposeFile)if err != nil {return nil, err}- var out []string- for _, value := range stack.ComposeImages(compose) {+ out := map[string]string{}+ for svc, value := range stack.ComposeImages(compose) {if strings.HasPrefix(value, "${FLUXER_REGISTRY") {continue}if def, ok := stack.ImageDefault(value); ok {value = def}- out = append(out, value)+ out[svc] = value}return out, nil}+type resolver interface {+ digest(ctx context.Context, ref string) (string, error)+}++func (c *client) digest(ctx context.Context, ref string) (string, error) {+ r, err := parse(ref)+ if err != nil {+ return "", err+ }+ _, d, err := c.manifest(ctx, r, r.ref)+ return d, err+}++func pinThirdParty(ctx context.Context, c resolver, set *stack.ImageSet, third map[string]string) error {+ if set.Images == nil {+ set.Images = map[string]string{}+ }+ resolved := map[string]string{}+ for _, svc := range slices.Sorted(maps.Keys(third)) {+ ref := third[svc]+ if _, ok := set.Images[svc]; ok {+ return fmt.Errorf("%s already has an image in the image set", svc)+ }+ if strings.Contains(ref, "@") {+ set.Images[svc] = ref+ continue+ }+ if _, ok := resolved[ref]; !ok {+ d, err := c.digest(ctx, ref)+ if err != nil {+ return fmt.Errorf("%s: %w", ref, err)+ }+ resolved[ref] = d+ }+ set.Images[svc] = ref + "@" + resolved[ref]+ }+ return nil+}+func writeJSON(path string, v any) error {b, err := json.MarshalIndent(v, "", " ")if err != nil {diff --git a/fluxer_rexctl/internal/stack/layersizes/pin_test.go b/fluxer_rexctl/internal/stack/layersizes/pin_test.gonew file mode 100644index 000000000..b51a37ad4--- /dev/null+++ b/fluxer_rexctl/internal/stack/layersizes/pin_test.go@@ -0,0 +1,64 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package main++import (+ "context"+ "testing"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"+)++type fakeResolver map[string]string++func (f fakeResolver) digest(_ context.Context, ref string) (string, error) {+ return f[ref], nil+}++func TestPinThirdPartyAddsTagAndDigest(t *testing.T) {+ set := stack.ImageSet{Images: map[string]string{"api": "ghcr.io/fluxerapp/fluxer-api@sha256:1"}}+ third := map[string]string{+ "postgres": "postgres:16-alpine",+ "seaweedfs": "chrislusf/seaweedfs:4.47",+ "seaweedfs-init": "chrislusf/seaweedfs:4.47",+ "nats": "nats:2.14-alpine@sha256:9",+ }+ calls := 0+ r := fakeResolver{"postgres:16-alpine": "sha256:pg", "chrislusf/seaweedfs:4.47": "sha256:sw"}+ counting := countingResolver{r, &calls}+ if err := pinThirdParty(context.Background(), counting, &set, third); err != nil {+ t.Fatal(err)+ }+ want := map[string]string{+ "api": "ghcr.io/fluxerapp/fluxer-api@sha256:1",+ "postgres": "postgres:16-alpine@sha256:pg",+ "seaweedfs": "chrislusf/seaweedfs:4.47@sha256:sw",+ "seaweedfs-init": "chrislusf/seaweedfs:4.47@sha256:sw",+ "nats": "nats:2.14-alpine@sha256:9",+ }+ for svc, ref := range want {+ if set.Images[svc] != ref {+ t.Fatalf("%s = %q, want %q", svc, set.Images[svc], ref)+ }+ }+ if calls != 2 {+ t.Fatalf("looked up %d digests, want 2", calls)+ }+}++func TestPinThirdPartyRefusesAServiceTheSetAlreadyHas(t *testing.T) {+ set := stack.ImageSet{Images: map[string]string{"postgres": "x"}}+ if err := pinThirdParty(context.Background(), fakeResolver{}, &set, map[string]string{"postgres": "postgres:16-alpine"}); err == nil {+ t.Fatal("pinned over an existing image")+ }+}++type countingResolver struct {+ fakeResolver+ calls *int+}++func (c countingResolver) digest(ctx context.Context, ref string) (string, error) {+ *c.calls+++ return c.fakeResolver.digest(ctx, ref)+}diff --git a/fluxer_rexctl/internal/stack/thirdparty_pin_test.go b/fluxer_rexctl/internal/stack/thirdparty_pin_test.gonew file mode 100644index 000000000..ed2bc7ed0--- /dev/null+++ b/fluxer_rexctl/internal/stack/thirdparty_pin_test.go@@ -0,0 +1,84 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package stack++import (+ "path"+ "strings"+ "testing"+)++func pinnedSet(t *testing.T) (shipped []byte, images map[string]string) {+ t.Helper()+ shipped, err := ReadFile(Embedded(), ComposeFile)+ if err != nil {+ t.Fatal(err)+ }+ found, err := imageLines(shipped)+ if err != nil {+ t.Fatal(err)+ }+ digest := "@sha256:" + strings.Repeat("ab", 32)+ images = map[string]string{}+ for _, img := range found {+ if strings.HasPrefix(img.value, "${FLUXER_REGISTRY") {+ head, _, _ := strings.Cut(img.value, ":${FLUXER_IMAGE_TAG")+ images[img.service] = "ghcr.io/fluxerapp/" + path.Base(head) + digest+ continue+ }+ def, ok := ImageDefault(img.value)+ if !ok {+ t.Fatalf("%s has no default image: %s", img.service, img.value)+ }+ images[img.service] = def + digest+ }+ return shipped, images+}++func TestPinImagesPinsThirdPartyDefaultsAndKeepsTheOverride(t *testing.T) {+ shipped, images := pinnedSet(t)+ got, err := pinImages(shipped, "test", images)+ if err != nil {+ t.Fatal(err)+ }+ pinned := ComposeImages(got)+ want := "${FLUXER_POSTGRES_IMAGE:-" + images["postgres"] + "}"+ if pinned["postgres"] != want {+ t.Fatalf("postgres image = %q, want %q", pinned["postgres"], want)+ }+ for _, svc := range []string{"nats", "edge", "valkey"} {+ if !strings.Contains(pinned[svc], "@sha256:") {+ t.Fatalf("%s isn't pinned: %q", svc, pinned[svc])+ }+ }+ def, _ := ImageDefault(pinned["postgres"])+ if major, err := PostgresMajor(def); err != nil || major != 16 {+ t.Fatalf("PostgresMajor(%q) = %d, %v", def, major, err)+ }+}++func TestUnpinImagesRestoresPinnedThirdPartyImages(t *testing.T) {+ shipped, images := pinnedSet(t)+ pinned, err := pinImages(shipped, "test", images)+ if err != nil {+ t.Fatal(err)+ }+ if got := UnpinImages(pinned); string(got) != string(shipped) {+ t.Fatalf("unpinning didn't give back the shipped compose file:\n%s", got)+ }+}++func TestUnpinImagesKeepsAnImageTheUserChose(t *testing.T) {+ shipped, _ := pinnedSet(t)+ mine := strings.Replace(string(shipped), "${FLUXER_POSTGRES_IMAGE:-postgres:16-alpine}", "${FLUXER_POSTGRES_IMAGE:-postgres:16.9-alpine}", 1)+ if got := UnpinImages([]byte(mine)); string(got) != mine {+ t.Fatal("unpinning changed an image that wasn't pinned by digest")+ }+}++func TestRegistryServicesLeavesOutThirdPartyImages(t *testing.T) {+ got := RegistryServices()+ if !got["api"] || got["postgres"] || got["edge"] {+ t.Fatalf("RegistryServices() = %v", got)+ }+}