Skip to content

Commit 055af8e

Browse files
committed
fix(marketplace): prune stale taps, preserve source state, scope RBAC, pull by digest
Address the remaining review findings: - The materializer now prunes PackageSources it materialized from an earlier revision that the current artifact no longer contains (including a single/multi rename), so a removed package leaves the catalog; installed Packages stay. - A repeat connect updates the fetched OCIRepository's spec, label and name annotation instead of replacing the object, so the operator's finalizer and materialized-revision annotation survive; covered by a test. - OCIRepository write verbs move from the cluster-wide cozystack-api ClusterRole to a namespaced Role in cozy-system (get/list/watch stay cluster-wide for the watch cache), since the API only manages tap sources there. - 'cozypkg validate --require-signature' now pulls the exact manifest digest cosign verified, closing the tag-mutation window between verify and pull. - safeJoin rejects any '..' path segment outright (defense in depth and to satisfy the Zip-Slip scanner) on top of the existing containment check. - Document why the index gate's merge/label steps need a workflow_run split for fork pull requests. Signed-off-by: IvanHunters <[email protected]>
1 parent cff3208 commit 055af8e

10 files changed

Lines changed: 206 additions & 21 deletions

File tree

‎cmd/cozypkg/cmd/validate.go‎

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cmd
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223
"io"
2324
"io/fs"
@@ -523,30 +524,59 @@ var validateCmdFlags struct {
523524
knownSources []string
524525
}
525526

527+
var digestRe = regexp.MustCompile(`sha256:[0-9a-f]{64}`)
528+
526529
// verifyCosignSignature verifies an OCI artifact's keyless cosign signature
527530
// against the expected certificate identity and OIDC issuer, shelling out to
528531
// the cosign binary. This is the trust anchor the index CI gate relies on: a
529-
// version bump must stay signed by the entry's recorded identity.
530-
func verifyCosignSignature(ref, identity, issuer string) error {
532+
// version bump must stay signed by the entry's recorded identity. It returns
533+
// the verified manifest digest so the caller can pull that exact content and
534+
// close the tag-mutation window between verify and pull; the digest is empty
535+
// if it could not be parsed (the caller then falls back to the tag).
536+
func verifyCosignSignature(ref, identity, issuer string) (string, error) {
531537
if _, err := exec.LookPath("cosign"); err != nil {
532-
return fmt.Errorf("--require-signature needs the cosign binary in PATH: %w", err)
538+
return "", fmt.Errorf("--require-signature needs the cosign binary in PATH: %w", err)
533539
}
534540
if identity == "" || issuer == "" {
535-
return fmt.Errorf("--require-signature needs --certificate-identity and --certificate-oidc-issuer for keyless verification")
541+
return "", fmt.Errorf("--require-signature needs --certificate-identity and --certificate-oidc-issuer for keyless verification")
536542
}
537543
cosignRef := strings.TrimPrefix(ref, "oci://")
538544
ctx, cancel := context.WithTimeout(context.Background(), ociPullTimeout)
539545
defer cancel()
540546
out, err := exec.CommandContext(ctx, "cosign", "verify", cosignRef,
541547
"--certificate-identity", identity,
542-
"--certificate-oidc-issuer", issuer).CombinedOutput()
548+
"--certificate-oidc-issuer", issuer,
549+
"--output", "json").CombinedOutput()
543550
if ctx.Err() == context.DeadlineExceeded {
544-
return fmt.Errorf("timed out verifying the signature of %q", ref)
551+
return "", fmt.Errorf("timed out verifying the signature of %q", ref)
545552
}
546553
if err != nil {
547-
return fmt.Errorf("cosign verification failed for %q: %v\n%s", ref, err, strings.TrimSpace(string(out)))
554+
return "", fmt.Errorf("cosign verification failed for %q: %v\n%s", ref, err, strings.TrimSpace(string(out)))
555+
}
556+
// Extract the verified manifest digest from the JSON payload
557+
// (.critical.image."docker-manifest-digest").
558+
return parseVerifiedDigest(out), nil
559+
}
560+
561+
// parseVerifiedDigest pulls the docker-manifest-digest out of cosign's JSON
562+
// verification output, or returns "" if it is not present.
563+
func parseVerifiedDigest(out []byte) string {
564+
var payloads []struct {
565+
Critical struct {
566+
Image struct {
567+
Digest string `json:"docker-manifest-digest"`
568+
} `json:"image"`
569+
} `json:"critical"`
548570
}
549-
return nil
571+
if err := json.Unmarshal(out, &payloads); err == nil {
572+
for _, p := range payloads {
573+
if digestRe.MatchString(p.Critical.Image.Digest) {
574+
return p.Critical.Image.Digest
575+
}
576+
}
577+
}
578+
// Fallback: any sha256 digest present in the output.
579+
return digestRe.FindString(string(out))
550580
}
551581

552582
var validateCmd = &cobra.Command{
@@ -568,12 +598,21 @@ runs "helm lint" on every component chart.`,
568598
root := target
569599

570600
if isOCIRef(target) {
601+
pullRef := target
571602
if validateCmdFlags.requireSignature {
572-
if err := verifyCosignSignature(target, validateCmdFlags.certIdentity, validateCmdFlags.certIssuer); err != nil {
603+
digest, err := verifyCosignSignature(target, validateCmdFlags.certIdentity, validateCmdFlags.certIssuer)
604+
if err != nil {
573605
return err
574606
}
607+
// Pull the exact content cosign verified, closing the window in
608+
// which the tag could be moved between verify and pull.
609+
if digest != "" {
610+
if parsed, perr := parseOCIRef(target); perr == nil {
611+
pullRef = parsed.URL + "@" + digest
612+
}
613+
}
575614
}
576-
dir, cleanup, err := pullOCIArtifact(target)
615+
dir, cleanup, err := pullOCIArtifact(pullRef)
577616
if err != nil {
578617
return err
579618
}

‎cmd/cozypkg/cmd/validate_test.go‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cmd
1919
import (
2020
"os"
2121
"path/filepath"
22+
"strings"
2223
"testing"
2324
)
2425

@@ -333,11 +334,26 @@ func TestSplitYAMLDocuments(t *testing.T) {
333334
func TestVerifyCosignSignatureGuards(t *testing.T) {
334335
// Errors regardless of whether cosign is installed: absent -> missing-binary
335336
// error; present -> missing identity/issuer error. Either way, no silent pass.
336-
if err := verifyCosignSignature("oci://ghcr.io/foo/bar:v1", "", ""); err == nil {
337+
if _, err := verifyCosignSignature("oci://ghcr.io/foo/bar:v1", "", ""); err == nil {
337338
t.Fatal("expected an error when identity/issuer are missing or cosign is absent")
338339
}
339340
}
340341

342+
func TestParseVerifiedDigest(t *testing.T) {
343+
a := "sha256:" + strings.Repeat("a", 64)
344+
b := "sha256:" + strings.Repeat("b", 64)
345+
if got := parseVerifiedDigest([]byte(`[{"critical":{"image":{"docker-manifest-digest":"` + a + `"}}}]`)); got != a {
346+
t.Errorf("parseVerifiedDigest = %q", got)
347+
}
348+
// Fallback: digest present anywhere in non-JSON output.
349+
if got := parseVerifiedDigest([]byte("verified " + b)); got != b {
350+
t.Errorf("fallback parseVerifiedDigest = %q", got)
351+
}
352+
if got := parseVerifiedDigest([]byte("no digest here")); got != "" {
353+
t.Errorf("expected empty digest, got %q", got)
354+
}
355+
}
356+
341357
func TestArtifactRoot_FallsBackWhenNoPackagesDir(t *testing.T) {
342358
root := t.TempDir()
343359
if got := artifactRoot(root); got != root {

‎hack/packages-index/github-workflows/validate.yaml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ jobs:
5959
BASE_REF: origin/${{ github.base_ref }}
6060
run: bash scripts/validate-entry.sh
6161

62+
# NOTE: for a pull request from a fork (the common case for a community
63+
# index), GITHUB_TOKEN is read-only regardless of the permissions above,
64+
# so the two steps below fail with 403. Before enabling this in the real
65+
# packages-index repo, move the merge/label actions into a separate
66+
# `workflow_run` job that checks out no untrusted code (do NOT switch the
67+
# validation job to pull_request_target, which would run the contributor's
68+
# gate script with a write token). The validation job above is safe on
69+
# pull_request as-is.
6270
- name: Enable auto-merge (owner version bump, signed by recorded identity)
6371
if: steps.gate.outputs.lane == 'auto'
6472
env:

‎internal/operator/tapmaterializer_artifact.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ func verifyAndExtract(data []byte, expectedDigest, destDir string) error {
101101
return nil
102102
}
103103

104-
// safeJoin joins name under base, rejecting entries that escape base.
104+
// safeJoin joins name under base, rejecting entries that escape base. It
105+
// rejects any ".." component outright (the traversal marker) and, as a second
106+
// guard, confirms the resolved path stays within base.
105107
func safeJoin(base, name string) (string, error) {
108+
if name == ".." || strings.HasPrefix(name, "../") || strings.Contains(name, "/../") || strings.HasSuffix(name, "/..") {
109+
return "", fmt.Errorf("tar entry %q contains a path-traversal segment", name)
110+
}
106111
target := filepath.Join(base, name)
107112
rel, err := filepath.Rel(base, target)
108113
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {

‎internal/operator/tapmaterializer_reconciler.go‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func (r *TapMaterializerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
122122
base = communityBaseFromURL(repo.Spec.URL)
123123
}
124124
single := len(sources) == 1
125+
applied := make(map[string]bool, len(sources))
125126
for i := range sources {
126127
ps := sources[i].DeepCopy()
127128
origPath := "/"
@@ -140,12 +141,21 @@ func (r *TapMaterializerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
140141
if err := r.Patch(ctx, ps, client.Apply, client.FieldOwner(tapFieldOwner), client.ForceOwnership); err != nil {
141142
return ctrl.Result{}, fmt.Errorf("materialize PackageSource %s: %w", ps.GetName(), err)
142143
}
144+
applied[ps.GetName()] = true
143145
logger.Info("materialized PackageSource from tap", "name", ps.GetName(), "tap", repo.Name)
144146
}
145147
if len(sources) == 0 {
146148
logger.Info("tap artifact carried no PackageSource", "tap", repo.Name, "revision", art.Revision)
147149
}
148150

151+
// Prune PackageSources this tap materialized from an earlier revision that
152+
// the current artifact no longer contains (including a rename when the
153+
// single/multi package count flips), so a removed package leaves the
154+
// catalog. Installed Packages are left in place.
155+
if err := r.pruneMaterialized(ctx, repo.Name, applied); err != nil {
156+
return ctrl.Result{}, err
157+
}
158+
149159
// Stamp the revision so an unchanged artifact is not re-pulled every resync.
150160
if repo.Annotations == nil {
151161
repo.Annotations = map[string]string{}
@@ -157,16 +167,22 @@ func (r *TapMaterializerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
157167
return ctrl.Result{}, nil
158168
}
159169

160-
// deleteMaterialized removes the PackageSources materialized from the given tap
161-
// source. Installed Packages are intentionally left in place.
170+
// deleteMaterialized removes every PackageSource materialized from the given
171+
// tap source. Installed Packages are intentionally left in place.
162172
func (r *TapMaterializerReconciler) deleteMaterialized(ctx context.Context, sourceName string) error {
173+
return r.pruneMaterialized(ctx, sourceName, nil)
174+
}
175+
176+
// pruneMaterialized deletes the PackageSources materialized from sourceName
177+
// except those whose names are in keep (nil keep deletes all of them).
178+
func (r *TapMaterializerReconciler) pruneMaterialized(ctx context.Context, sourceName string, keep map[string]bool) error {
163179
var list cozyv1alpha1.PackageSourceList
164180
if err := r.List(ctx, &list, client.MatchingLabels{tapconst.Label: "true"}); err != nil {
165181
return err
166182
}
167183
for i := range list.Items {
168184
ps := &list.Items[i]
169-
if ps.Annotations[tapconst.SourceAnnotation] != sourceName {
185+
if ps.Annotations[tapconst.SourceAnnotation] != sourceName || keep[ps.Name] {
170186
continue
171187
}
172188
if err := r.Delete(ctx, ps); err != nil && client.IgnoreNotFound(err) != nil {

‎internal/operator/tapmaterializer_reconciler_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,31 @@ func TestDeleteMaterialized(t *testing.T) {
143143
t.Errorf("expected community.baz.qux kept, got %v", err)
144144
}
145145
}
146+
147+
func TestPruneMaterializedKeepsCurrentSet(t *testing.T) {
148+
scheme := tapScheme(t)
149+
src := "community-foo-bar"
150+
keepPS := &cozyv1alpha1.PackageSource{ObjectMeta: metav1.ObjectMeta{
151+
Name: "community.foo.bar",
152+
Labels: map[string]string{tapconst.Label: "true"},
153+
Annotations: map[string]string{tapconst.SourceAnnotation: src},
154+
}}
155+
stalePS := &cozyv1alpha1.PackageSource{ObjectMeta: metav1.ObjectMeta{
156+
Name: "community.foo.bar.removed",
157+
Labels: map[string]string{tapconst.Label: "true"},
158+
Annotations: map[string]string{tapconst.SourceAnnotation: src},
159+
}}
160+
cl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(keepPS, stalePS).Build()
161+
r := &TapMaterializerReconciler{Client: cl, Scheme: scheme}
162+
163+
// The new revision still contains community.foo.bar but not .removed.
164+
if err := r.pruneMaterialized(context.Background(), src, map[string]bool{"community.foo.bar": true}); err != nil {
165+
t.Fatalf("pruneMaterialized: %v", err)
166+
}
167+
if err := cl.Get(context.Background(), client.ObjectKey{Name: "community.foo.bar"}, &cozyv1alpha1.PackageSource{}); err != nil {
168+
t.Errorf("expected kept PS to survive, got %v", err)
169+
}
170+
if err := cl.Get(context.Background(), client.ObjectKey{Name: "community.foo.bar.removed"}, &cozyv1alpha1.PackageSource{}); !apierrors.IsNotFound(err) {
171+
t.Errorf("expected stale PS pruned, got %v", err)
172+
}
173+
}

‎packages/system/cozystack-api/templates/rbac.yaml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ rules:
2828
resources: ["*"]
2929
verbs: ["get", "watch", "list"]
3030
# Marketplace Tap resource: disconnecting a community tap deletes its
31-
# PackageSource, and connecting/disconnecting manages the tap's Flux
32-
# OCIRepository. Both run with this ServiceAccount, so it needs delete on
33-
# packagesources and full management of the tap OCIRepositories.
31+
# (cluster-scoped) PackageSource. The tap's Flux OCIRepository is read here for
32+
# List/Get/Watch; its write verbs are scoped to a namespaced Role in cozy-system
33+
# (see tap-source-role.yaml), since the API only ever manages tap sources there.
3434
- apiGroups: ["cozystack.io"]
3535
resources: ["packagesources"]
3636
verbs: ["delete"]
3737
- apiGroups: ["source.toolkit.fluxcd.io"]
3838
resources: ["ocirepositories"]
39-
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
39+
verbs: ["get", "list", "watch"]
4040
- apiGroups: ["helm.toolkit.fluxcd.io"]
4141
resources: ["*"]
4242
verbs: ["*"]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# The cozystack-api ServiceAccount creates, updates, and deletes a community
2+
# tap's Flux OCIRepository, but only ever in cozy-system. Keep those write verbs
3+
# in a namespaced Role here (list/watch/get stay cluster-wide in the ClusterRole
4+
# for the aggregated watch cache) rather than granting them cluster-wide.
5+
apiVersion: rbac.authorization.k8s.io/v1
6+
kind: Role
7+
metadata:
8+
name: cozystack-api-tap-sources
9+
namespace: cozy-system
10+
rules:
11+
- apiGroups: ["source.toolkit.fluxcd.io"]
12+
resources: ["ocirepositories"]
13+
verbs: ["create", "update", "patch", "delete"]
14+
---
15+
apiVersion: rbac.authorization.k8s.io/v1
16+
kind: RoleBinding
17+
metadata:
18+
name: cozystack-api-tap-sources
19+
namespace: cozy-system
20+
roleRef:
21+
apiGroup: rbac.authorization.k8s.io
22+
kind: Role
23+
name: cozystack-api-tap-sources
24+
subjects:
25+
- kind: ServiceAccount
26+
name: cozystack-api
27+
namespace: cozy-system

‎pkg/registry/core/tap/rest.go‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,23 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
179179
if gerr != nil {
180180
return nil, apierrors.NewInternalError(fmt.Errorf("update Flux source for tap %s: %w", target.PackageSourceName, gerr))
181181
}
182-
repo.SetResourceVersion(cur.GetResourceVersion())
183-
if _, err := src.Update(ctx, repo, metav1.UpdateOptions{FieldManager: "cozystack-api"}); err != nil {
182+
// Update only the fields this API owns (spec, the tap label and name
183+
// annotation) on the FETCHED object, so the operator's finalizer and
184+
// materialized-revision annotation on the existing source survive.
185+
cur.Object["spec"] = repo.Object["spec"]
186+
labels := cur.GetLabels()
187+
if labels == nil {
188+
labels = map[string]string{}
189+
}
190+
labels[tapconst.Label] = "true"
191+
cur.SetLabels(labels)
192+
ann := cur.GetAnnotations()
193+
if ann == nil {
194+
ann = map[string]string{}
195+
}
196+
ann[tapconst.NameAnnotation] = target.PackageSourceName
197+
cur.SetAnnotations(ann)
198+
if _, err := src.Update(ctx, cur, metav1.UpdateOptions{FieldManager: "cozystack-api"}); err != nil {
184199
return nil, apierrors.NewInternalError(fmt.Errorf("update Flux source for tap %s: %w", target.PackageSourceName, err))
185200
}
186201
}

‎pkg/registry/core/tap/rest_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,37 @@ func TestCreateGuards(t *testing.T) {
172172
}
173173
}
174174

175+
func TestCreateRepeatPreservesFinalizerAndRevision(t *testing.T) {
176+
// A source already connected and materialized carries the operator's
177+
// finalizer and materialized-revision annotation; a repeat connect must
178+
// update the tag without stripping them.
179+
existing := ociRepoObj("community-foo-bar")
180+
existing.SetFinalizers([]string{"apps.cozystack.io/tap-materializer"})
181+
existing.SetAnnotations(map[string]string{"apps.cozystack.io/materialized-revision": "rev-1"})
182+
r := fakeREST(existing)
183+
184+
in := &corev1alpha1.Tap{Spec: corev1alpha1.TapSpec{URL: "oci://ghcr.io/foo/bar:v2"}}
185+
if _, err := r.Create(context.Background(), in, nil, nil); err != nil {
186+
t.Fatalf("repeat create: %v", err)
187+
}
188+
u, err := r.dyn.Resource(gvrOCIRepos).Namespace("cozy-system").Get(context.Background(), "community-foo-bar", metav1.GetOptions{})
189+
if err != nil {
190+
t.Fatal(err)
191+
}
192+
if len(u.GetFinalizers()) == 0 {
193+
t.Error("repeat connect stripped the operator finalizer")
194+
}
195+
if u.GetAnnotations()["apps.cozystack.io/materialized-revision"] != "rev-1" {
196+
t.Error("repeat connect stripped the materialized-revision annotation")
197+
}
198+
if tag, _, _ := unstructured.NestedString(u.Object, "spec", "ref", "tag"); tag != "v2" {
199+
t.Errorf("repeat connect did not update the tag, got %q", tag)
200+
}
201+
if u.GetAnnotations()["apps.cozystack.io/tap-name"] != "community.foo.bar" {
202+
t.Error("repeat connect did not set the tap-name annotation")
203+
}
204+
}
205+
175206
func TestCreateMakesLabeledSource(t *testing.T) {
176207
r := fakeREST()
177208
in := &corev1alpha1.Tap{Spec: corev1alpha1.TapSpec{URL: "oci://ghcr.io/foo/bar:v1", SecretRef: "pull-creds"}}

0 commit comments

Comments
 (0)