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
'<tag>@<digest>') 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 <xorokhotnikov@gmail.com>
(cherry picked from commit c837838cc2)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
IvanHunters authored and Myasnikov Daniil committed 2026-11-10 12:50:17 +00:00
1 parent 6621606ae9
commit 07cd563436
3 files changed
+159

No files matched your search

@@ -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.
@@ -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
// '<tag>@<digest>' and a digest revision as the bare '<digest>', 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
@@ -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
// '<tag>@<digest>').
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])
}
}