From 07cd563436eb6145c3af2bd0ef5d3fac01fe079b Mon Sep 17 00:00:00 2001 From: IvanHunters Date: Thu, 22 Oct 2026 22:02:49 +0300 Subject: [PATCH] fix(marketplace): pin a tag-following tap source to the resolved digest A tapped repository followed a mutable tag: the OCIRepository carried only spec.ref.tag with a 5m interval, so Flux re-pulled whatever the tag pointed at, and a moved tag reached running tenant application instances within the interval. Whoever controlled the tag controlled what ran in the cluster. The materializer now pins the source to the digest Flux resolved for the artifact it just verified: it reads the manifest digest from status.artifact.revision (source-controller formats an OCI tag revision as '@') and writes it to spec.ref.digest, which Flux resolves ahead of the tag. The tag is kept informational. Moving the tag upstream no longer changes what the cluster pulls; an explicit re-connect is required to move the pin. A user-supplied explicit digest is never rewritten, and a revision without a well-formed digest leaves the source following its tag rather than writing a malformed reference. Assisted-by: LLM Signed-off-by: IvanHunters (cherry picked from commit c837838cc2b14f18849f5400d48603143fe283ef) Signed-off-by: Myasnikov Daniil --- internal/marketplace/tapconst/tapconst.go | 6 + .../operator/tapmaterializer_reconciler.go | 48 ++++++++ .../tapmaterializer_reconciler_test.go | 105 ++++++++++++++++++ 3 files changed, 159 insertions(+) diff --git a/internal/marketplace/tapconst/tapconst.go b/internal/marketplace/tapconst/tapconst.go index b88b99bf0..aa76a1628 100644 --- a/internal/marketplace/tapconst/tapconst.go +++ b/internal/marketplace/tapconst/tapconst.go @@ -24,6 +24,12 @@ const ( // MaterializedRevisionAnnotation records the artifact revision last // materialized, so an unchanged revision is not re-pulled. MaterializedRevisionAnnotation = "apps.cozystack.io/materialized-revision" + // PinnedDigestAnnotation records the digest the materializer pinned a + // tag-following source to (spec.ref.digest). It distinguishes an operator + // pin, which a re-connect may move, from a user-supplied explicit digest, + // which the operator never rewrites. Cleared by a re-connect so the operator + // re-pins the newly requested tag. + PinnedDigestAnnotation = "apps.cozystack.io/tap-pinned-digest" // MaterializeErrorAnnotation records why the last materialization failed // (e.g. a name collision with a core component), so the failure surfaces on // the Tap resource and dashboard instead of only in the operator log. diff --git a/internal/operator/tapmaterializer_reconciler.go b/internal/operator/tapmaterializer_reconciler.go index a2747db6a..fd4b4994c 100644 --- a/internal/operator/tapmaterializer_reconciler.go +++ b/internal/operator/tapmaterializer_reconciler.go @@ -24,6 +24,7 @@ import ( "net/http" neturl "net/url" "os" + "regexp" "strings" "time" @@ -189,6 +190,13 @@ func (r *TapMaterializerReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, err } + // Pin the resolved digest so the source stops following a mutable tag: the + // tag may move upstream, but the cluster keeps pulling this exact content + // until an explicit re-connect. This runs only after verifyAndExtract + // accepted the artifact, so the digest pinned is the digest of verified + // content. A user-supplied explicit digest is never rewritten. + pinTapDigest(&repo, art.Revision) + // Stamp the revision so an unchanged artifact is not re-pulled every resync. if repo.Annotations == nil { repo.Annotations = map[string]string{} @@ -200,6 +208,46 @@ func (r *TapMaterializerReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, nil } +// manifestDigestRe matches a full OCI manifest digest (sha256:<64 hex>). +var manifestDigestRe = regexp.MustCompile(`^sha256:[0-9a-f]{64}$`) + +// pinTapDigest pins a tag-following tap source to the digest Flux resolved for +// the current artifact, so moving the tag upstream no longer changes what the +// cluster pulls. source-controller formats an OCI tag revision as +// '@' and a digest revision as the bare '', so the +// manifest digest is the segment after the last '@'. +// +// A user-supplied explicit digest (ref.digest set without the operator's pin +// annotation) is left untouched: the operator pins only what a tag resolved to, +// or re-pins its own prior pin. A revision without a well-formed digest leaves +// the source following its tag rather than writing a malformed reference (the +// caller requires a non-empty art.Digest, so this is defensive). +func pinTapDigest(repo *sourcev1.OCIRepository, revision string) { + parts := strings.Split(revision, "@") + digest := parts[len(parts)-1] + if !manifestDigestRe.MatchString(digest) { + return + } + ref := repo.Spec.Reference + // A user-supplied explicit digest carries no matching operator pin + // annotation; never rewrite it. + if ref != nil && ref.Digest != "" && repo.Annotations[tapconst.PinnedDigestAnnotation] != ref.Digest { + return + } + if ref != nil && ref.Digest == digest && repo.Annotations[tapconst.PinnedDigestAnnotation] == digest { + return // already pinned to this digest + } + if ref == nil { + ref = &sourcev1.OCIRepositoryRef{} + repo.Spec.Reference = ref + } + ref.Digest = digest + if repo.Annotations == nil { + repo.Annotations = map[string]string{} + } + repo.Annotations[tapconst.PinnedDigestAnnotation] = digest +} + // failMaterialize records a materialization failure (e.g. a name collision with // a core component) on the tap source so it surfaces on the Tap resource and the // dashboard, and emits a warning Event. It deliberately does not stamp the diff --git a/internal/operator/tapmaterializer_reconciler_test.go b/internal/operator/tapmaterializer_reconciler_test.go index ddb124b7c..404ce6e48 100644 --- a/internal/operator/tapmaterializer_reconciler_test.go +++ b/internal/operator/tapmaterializer_reconciler_test.go @@ -18,6 +18,7 @@ package operator import ( "context" + "strings" "testing" fluxmeta "github.com/fluxcd/pkg/apis/meta" @@ -535,3 +536,107 @@ func TestReconcileBlocksNameCollision(t *testing.T) { t.Error("the collision check must not overwrite the foreign PackageSource") } } + +// A tag-following tap source must be pinned to the digest Flux resolved, so it +// stops following a moved tag. The manifest digest is the part of the artifact +// revision after '@' (source-controller formats an OCI tag revision as +// '@'). +func TestReconcilePinsDigestFromTagRevision(t *testing.T) { + scheme := tapScheme(t) + data, digest := tarGz(t, map[string]string{"packages/core/platform/sources/hello.yaml": samplePS}) + manifest := "sha256:" + strings.Repeat("a", 64) + repo := tapRepo(true) + repo.Spec.Reference = &sourcev1.OCIRepositoryRef{Tag: "v1"} + cl := fake.NewClientBuilder().WithScheme(scheme). + WithObjects(repo). + WithStatusSubresource(&sourcev1.OCIRepository{}). + Build() + setArtifact(t, cl, digest, "v1@"+manifest) + + r := materializerFor(scheme, cl, record.NewFakeRecorder(10), &data) + if _, err := r.Reconcile(context.Background(), req()); err != nil { + t.Fatalf("reconcile: %v", err) + } + + var got sourcev1.OCIRepository + if err := cl.Get(context.Background(), req().NamespacedName, &got); err != nil { + t.Fatal(err) + } + if got.Spec.Reference == nil || got.Spec.Reference.Digest != manifest { + t.Fatalf("expected ref.digest pinned to %q, got %+v", manifest, got.Spec.Reference) + } + if got.Spec.Reference.Tag != "v1" { + t.Errorf("tag must stay informational, got %q", got.Spec.Reference.Tag) + } + if got.Annotations[tapconst.PinnedDigestAnnotation] != manifest { + t.Errorf("expected pinned-digest annotation %q, got %v", manifest, got.Annotations) + } +} + +// A user-supplied explicit digest (ref.digest set with no operator pin +// annotation) must never be rewritten by the operator: the operator pins only +// tag-following sources. +func TestReconcileDoesNotRepinUserDigest(t *testing.T) { + scheme := tapScheme(t) + data, digest := tarGz(t, map[string]string{"packages/core/platform/sources/hello.yaml": samplePS}) + userDigest := "sha256:" + strings.Repeat("c", 64) + repo := tapRepo(true) + repo.Spec.Reference = &sourcev1.OCIRepositoryRef{Digest: userDigest} + cl := fake.NewClientBuilder().WithScheme(scheme). + WithObjects(repo). + WithStatusSubresource(&sourcev1.OCIRepository{}). + Build() + // A digest-pinned source reports its revision as the bare digest. + setArtifact(t, cl, digest, userDigest) + + r := materializerFor(scheme, cl, record.NewFakeRecorder(10), &data) + if _, err := r.Reconcile(context.Background(), req()); err != nil { + t.Fatalf("reconcile: %v", err) + } + + var got sourcev1.OCIRepository + if err := cl.Get(context.Background(), req().NamespacedName, &got); err != nil { + t.Fatal(err) + } + if got.Spec.Reference.Digest != userDigest { + t.Errorf("a user-supplied digest must be left as is, got %q", got.Spec.Reference.Digest) + } + if got.Annotations[tapconst.PinnedDigestAnnotation] != "" { + t.Errorf("the operator must not claim a user-supplied digest as its own pin, got %v", got.Annotations) + } +} + +// Once pinned, the source reports its revision as the bare digest; a further +// reconcile must keep the same pin rather than thrash it. +func TestReconcilePinIsIdempotent(t *testing.T) { + scheme := tapScheme(t) + data, digest := tarGz(t, map[string]string{"packages/core/platform/sources/hello.yaml": samplePS}) + manifest := "sha256:" + strings.Repeat("a", 64) + repo := tapRepo(true) + repo.Spec.Reference = &sourcev1.OCIRepositoryRef{Tag: "v1"} + cl := fake.NewClientBuilder().WithScheme(scheme). + WithObjects(repo). + WithStatusSubresource(&sourcev1.OCIRepository{}). + Build() + setArtifact(t, cl, digest, "v1@"+manifest) + r := materializerFor(scheme, cl, record.NewFakeRecorder(10), &data) + if _, err := r.Reconcile(context.Background(), req()); err != nil { + t.Fatalf("first reconcile: %v", err) + } + // Flux re-resolves by digest now and reports the bare-digest revision. + setArtifact(t, cl, digest, manifest) + if _, err := r.Reconcile(context.Background(), req()); err != nil { + t.Fatalf("second reconcile: %v", err) + } + + var got sourcev1.OCIRepository + if err := cl.Get(context.Background(), req().NamespacedName, &got); err != nil { + t.Fatal(err) + } + if got.Spec.Reference.Digest != manifest { + t.Errorf("pin must be stable, got %q", got.Spec.Reference.Digest) + } + if got.Annotations[tapconst.MaterializedRevisionAnnotation] != manifest { + t.Errorf("expected materialized revision stamped to the bare digest, got %q", got.Annotations[tapconst.MaterializedRevisionAnnotation]) + } +}