From f49979a01e41aaa0725c79efb65adcf6d565ea42 Mon Sep 17 00:00:00 2001 From: IvanHunters Date: Thu, 22 Oct 2026 22:35:21 +0300 Subject: [PATCH] fix(marketplace): preserve verify and pull secret on CLI re-tap A re-tap applies the OCIRepository under field owner cozypkg via server-side apply; a field this owner set earlier and now omits is removed. Re-tapping to move the pin without repeating --certificate-identity/--certificate-oidc-issuer therefore silently dropped the signature-verification policy, and re-tapping a private repo without --secret dropped the pull credential. The CLI now carries an existing source's spec.verify and secretRef onto the applied object when the invocation does not set them, mirroring the API connect path, so moving the pin never silently downgrades trust or breaks a private pull. Assisted-by: LLM Signed-off-by: IvanHunters (cherry picked from commit 0fd970304ca3eb017357504202fb233771a00b43) Signed-off-by: Myasnikov Daniil --- cmd/cozypkg/cmd/tap.go | 21 +++++++++++++++++++++ cmd/cozypkg/cmd/tap_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/cmd/cozypkg/cmd/tap.go b/cmd/cozypkg/cmd/tap.go index c41a655e3..30c21fbc3 100644 --- a/cmd/cozypkg/cmd/tap.go +++ b/cmd/cozypkg/cmd/tap.go @@ -180,6 +180,24 @@ func buildTapOCIRepository(name string, r ociRef, secret string, verify *sourcev return obj } +// carryoverSourceFields preserves an existing tap source's verification policy +// and pull credential when this invocation did not set them from flags, so a +// re-tap (e.g. to move the pin to a new tag) never silently drops signature +// verification or the pull secret. A server-side apply that omits a field this +// field owner previously set would otherwise remove it, which the API connect +// path (rest.go) deliberately avoids; this keeps the CLI symmetric with it. +func carryoverSourceFields(obj, existing *sourcev1.OCIRepository, flagVerify *sourcev1.OCIRepositoryVerification, flagSecret string) { + if existing == nil { + return + } + if flagVerify == nil && existing.Spec.Verify != nil { + obj.Spec.Verify = existing.Spec.Verify + } + if flagSecret == "" && existing.Spec.SecretRef != nil { + obj.Spec.SecretRef = existing.Spec.SecretRef + } +} + // tapVerification builds a keyless cosign verification policy from the connect // flags, or nil when neither is set. Both the certificate identity (subject) // and the OIDC issuer are required together: a policy that verified a signature @@ -395,6 +413,9 @@ runs its charts in your management cluster: tap only sources you trust.`, if existing.Spec.URL != "" && existing.Spec.URL != ref.URL { return fmt.Errorf("a different repository (%s) is already tapped as %q; run 'cozypkg untap %s' before tapping %s", existing.Spec.URL, srcName, names[0], ref.URL) } + // Same repository, re-tapped: carry over a verify policy or pull secret + // this invocation did not set, so moving the pin never silently drops them. + carryoverSourceFields(ociRepo, existing, verify, tapCmdFlags.secret) } // A tapped repository keeps its own declared name. Refuse the whole tap diff --git a/cmd/cozypkg/cmd/tap_test.go b/cmd/cozypkg/cmd/tap_test.go index f3041d312..af4c03a8c 100644 --- a/cmd/cozypkg/cmd/tap_test.go +++ b/cmd/cozypkg/cmd/tap_test.go @@ -22,6 +22,7 @@ import ( "io" "testing" + fluxmeta "github.com/fluxcd/pkg/apis/meta" sourcev1 "github.com/fluxcd/source-controller/api/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -207,6 +208,40 @@ func TestTapVerification(t *testing.T) { } } +func TestCarryoverSourceFields(t *testing.T) { + verifyPol := &sourcev1.OCIRepositoryVerification{Provider: "cosign", MatchOIDCIdentity: []sourcev1.OIDCIdentityMatch{{Issuer: "iss", Subject: "subj"}}} + existing := &sourcev1.OCIRepository{Spec: sourcev1.OCIRepositorySpec{ + Verify: verifyPol, + SecretRef: &fluxmeta.LocalObjectReference{Name: "pull-creds"}, + }} + + // Re-tap without flags: an existing verify policy and pull secret are carried + // onto the object to apply, so a server-side apply that omits them does not + // remove them (SSA prunes a previously-owned field left out of the apply). + obj := &sourcev1.OCIRepository{} + carryoverSourceFields(obj, existing, nil, "") + if obj.Spec.Verify == nil { + t.Error("existing verify policy must be preserved when no --certificate-* flags are given") + } + if obj.Spec.SecretRef == nil || obj.Spec.SecretRef.Name != "pull-creds" { + t.Error("existing pull secret must be preserved when no --secret is given") + } + + // Flags present: the invocation's own values win, nothing is carried over. + flagVerify := &sourcev1.OCIRepositoryVerification{Provider: "cosign"} + obj2 := &sourcev1.OCIRepository{Spec: sourcev1.OCIRepositorySpec{Verify: flagVerify, SecretRef: &fluxmeta.LocalObjectReference{Name: "new-creds"}}} + carryoverSourceFields(obj2, existing, flagVerify, "new-creds") + if obj2.Spec.Verify != flagVerify { + t.Error("a verify policy from flags must not be overwritten by the existing one") + } + if obj2.Spec.SecretRef.Name != "new-creds" { + t.Error("a secret from --secret must not be overwritten by the existing one") + } + + // No existing source: a no-op, no panic. + carryoverSourceFields(&sourcev1.OCIRepository{}, nil, nil, "") +} + func TestBuildTapOCIRepositoryWithVerify(t *testing.T) { r := ociRef{URL: "oci://ghcr.io/foo/bar", Repo: "bar", Tag: "v1"} v := &sourcev1.OCIRepositoryVerification{