Skip to content

Commit cc30d3c

Browse files
committed
fix(marketplace): re-check auto-register on every reconcile; de-register on privileged flip
A self-review with the cozy-review methodology found the privileged-auto-register guard was create-time only: a repository could tap with a benign default variant (auto-registered, standing Package created) and then ship a revision whose default variant is privileged. The materializer only re-checked at Package creation, so the existing Package survived and the Package reconciler installed the now-privileged component with no confirmation and no admin action. reconcileRegistrationPackage now converges the registration Package on every reconcile: it de-registers (deletes) a tap-owned Package when the default variant becomes privileged or is dropped, and records a durable, truthful reason on the PackageSource (RegistrationStateAnnotation) alongside the Warning Event, instead of a Package that stands while the event claims it did not register. Also from the review: - An unmarked Package of the same name (a user's cozypkg add, or one from before this version) is treated as registration already satisfied: reconcile returns nil instead of hard-erroring, which had hot-looped the whole tap and re-pulled the artifact every backoff. It is neither adopted nor deleted. - Delete the registration Package BEFORE the PackageSource in pruneMaterialized and in the aggregated Tap Delete, so a transient Package-delete failure is retryable instead of stranding the Package; a failed Package delete/check in Delete now returns an error rather than only logging. - runUntap aborts on a non-NotFound Package Get error instead of swallowing it and deleting the PackageSource and source anyway. - Correct the cozypkg tap output (apps register automatically except privileged). - Hoist the duplicated privileged-component helper to collision. Tests: privileged-flip de-register (mutation-verified non-vacuous), default-variant dropped, unmarked-Package left, foreign-tap Package left, Package dry-run propagation, operator foreign-Package ownership. Signed-off-by: IvanHunters <[email protected]>
1 parent 5963f46 commit cc30d3c

8 files changed

Lines changed: 391 additions & 98 deletions

File tree

‎cmd/cozypkg/cmd/add.go‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727

2828
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
29+
"github.com/cozystack/cozystack/internal/marketplace/collision"
2930
"github.com/spf13/cobra"
3031
apierrors "k8s.io/apimachinery/pkg/api/errors"
3132
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -466,7 +467,7 @@ func installPackage(ctx context.Context, k8sClient client.Client, packageSourceN
466467

467468
// A privileged component runs with elevated access; require an explicit
468469
// confirmation (or --allow-privileged) before installing it.
469-
if privileged := privilegedComponents(ps, variant); len(privileged) > 0 && !addCmdFlags.allowPrivileged {
470+
if privileged := collision.PrivilegedInstallComponents(ps, variant); len(privileged) > 0 && !addCmdFlags.allowPrivileged {
470471
ok, err := confirmPrivileged(pkgName, variant, privileged)
471472
if err != nil {
472473
return err
@@ -573,23 +574,6 @@ func selectVariantInteractive(ps *cozyv1alpha1.PackageSource) (string, error) {
573574
}
574575
}
575576

576-
// privilegedComponents returns the names of components in the given variant
577-
// that declare install.privileged: true.
578-
func privilegedComponents(ps *cozyv1alpha1.PackageSource, variantName string) []string {
579-
var names []string
580-
for _, v := range ps.Spec.Variants {
581-
if v.Name != variantName {
582-
continue
583-
}
584-
for _, c := range v.Components {
585-
if c.Install != nil && c.Install.Privileged {
586-
names = append(names, c.Name)
587-
}
588-
}
589-
}
590-
return names
591-
}
592-
593577
// confirmPrivileged prompts the operator to confirm installing privileged
594578
// components. It defaults to No.
595579
func confirmPrivileged(pkgName, variant string, components []string) (bool, error) {

‎cmd/cozypkg/cmd/add_test.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
23+
"github.com/cozystack/cozystack/internal/marketplace/collision"
2324
)
2425

2526
func TestPrivilegedComponents(t *testing.T) {
@@ -41,13 +42,13 @@ func TestPrivilegedComponents(t *testing.T) {
4142
},
4243
}
4344

44-
if got := privilegedComponents(ps, "default"); len(got) != 1 || got[0] != "risky" {
45+
if got := collision.PrivilegedInstallComponents(ps, "default"); len(got) != 1 || got[0] != "risky" {
4546
t.Errorf("default variant privileged = %v, want [risky]", got)
4647
}
47-
if got := privilegedComponents(ps, "safe"); len(got) != 0 {
48+
if got := collision.PrivilegedInstallComponents(ps, "safe"); len(got) != 0 {
4849
t.Errorf("safe variant should have no privileged components, got %v", got)
4950
}
50-
if got := privilegedComponents(ps, "nonexistent"); len(got) != 0 {
51+
if got := collision.PrivilegedInstallComponents(ps, "nonexistent"); len(got) != 0 {
5152
t.Errorf("unknown variant should return none, got %v", got)
5253
}
5354
}

‎cmd/cozypkg/cmd/tap.go‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ charts in your management cluster: tap only sources you trust.`,
330330
for _, n := range names {
331331
fmt.Fprintf(cmd.OutOrStdout(), " PackageSource/%s\n", n)
332332
}
333-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "The repository's applications are registered automatically; browse them in the dashboard catalog and create instances there.\n")
333+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "The repository's applications register in the dashboard catalog automatically, except a variant needing privileged install (register that with 'cozypkg add --allow-privileged'). Check the source's events if an app does not appear.\n")
334334
return nil
335335
},
336336
}
@@ -378,8 +378,13 @@ func runUntap(ctx context.Context, k8sClient client.Client, name string, allowYe
378378
// THIS source (label AND source annotation); a Package a later tap created
379379
// under a reused name, or a foreign Package, is left in place and blocks the
380380
// untap without --yes.
381+
//
382+
// The Package is handled BEFORE the PackageSource: a transient error here must
383+
// abort the untap, not fall through to delete the PackageSource and OCIRepository
384+
// and strand the Package where no later cleanup can find it.
381385
pkg := &cozyv1alpha1.Package{}
382-
if err := k8sClient.Get(ctx, client.ObjectKey{Name: name}, pkg); err == nil {
386+
switch err := k8sClient.Get(ctx, client.ObjectKey{Name: name}, pkg); {
387+
case err == nil:
383388
if collision.Owns(pkg, srcName) {
384389
if err := k8sClient.Delete(ctx, pkg); err != nil {
385390
return fmt.Errorf("failed to delete registration Package %s: %w", name, err)
@@ -388,6 +393,10 @@ func runUntap(ctx context.Context, k8sClient client.Client, name string, allowYe
388393
} else if !allowYes {
389394
return fmt.Errorf("package %s is still installed from this source; delete it with 'cozypkg del %s' first, or pass --yes to untap anyway (the Package stays installed)", name, name)
390395
}
396+
case apierrors.IsNotFound(err):
397+
// No registration Package; nothing to de-register.
398+
default:
399+
return fmt.Errorf("failed to check registration Package %s: %w", name, err)
391400
}
392401

393402
if err := k8sClient.Delete(ctx, ps); err != nil {

‎internal/marketplace/collision/collision.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ func Owns(obj metav1.Object, sourceName string) bool {
3232
obj.GetAnnotations()[tapconst.SourceAnnotation] == sourceName
3333
}
3434

35+
// PrivilegedInstallComponents lists the names of the given variant's
36+
// install-marked components that request privileged access. Both the CLI
37+
// (cozypkg add) and the operator's tap materializer gate on it, so it lives here
38+
// as the single shared definition.
39+
func PrivilegedInstallComponents(ps *cozyv1alpha1.PackageSource, variant string) []string {
40+
var out []string
41+
for i := range ps.Spec.Variants {
42+
if ps.Spec.Variants[i].Name != variant {
43+
continue
44+
}
45+
for _, c := range ps.Spec.Variants[i].Components {
46+
if c.Install != nil && c.Install.Privileged {
47+
out = append(out, c.Name)
48+
}
49+
}
50+
}
51+
return out
52+
}
53+
3554
// PackageSourceName returns an error if a PackageSource named name already
3655
// exists and is not the given tap source's own materialization.
3756
func PackageSourceName(ctx context.Context, cl client.Client, name, sourceName string) error {

‎internal/marketplace/tapconst/tapconst.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ const (
2828
// (e.g. a name collision with a core component), so the failure surfaces on
2929
// the Tap resource and dashboard instead of only in the operator log.
3030
MaterializeErrorAnnotation = "apps.cozystack.io/tap-error"
31+
// RegistrationStateAnnotation records, on a materialized PackageSource, why
32+
// its apps were not auto-registered on connect (no "default" variant, or a
33+
// privileged default variant that needs a deliberate `cozypkg add
34+
// --allow-privileged`). Unlike a Warning Event, which expires, this is a
35+
// durable reason the dashboard and operator can read later. It is cleared
36+
// once the apps are registered.
37+
RegistrationStateAnnotation = "apps.cozystack.io/registration-state"
3138
// Finalizer keeps the OCIRepository until its materialized PackageSources
3239
// are cleaned up.
3340
Finalizer = "apps.cozystack.io/tap-materializer"

‎internal/operator/tapmaterializer_reconciler.go‎

Lines changed: 116 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (r *TapMaterializerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
171171
// the catalog, mirroring how the platform ships a Package per built-in
172172
// app. The app components themselves carry no install block and are not
173173
// deployed by the Package; they are instantiated per user resource.
174-
if err := r.ensureRegistrationPackage(ctx, ps, &repo); err != nil {
174+
if err := r.reconcileRegistrationPackage(ctx, ps, &repo); err != nil {
175175
return ctrl.Result{}, fmt.Errorf("register apps for %s: %w", ps.GetName(), err)
176176
}
177177
applied[ps.GetName()] = true
@@ -223,61 +223,127 @@ func (r *TapMaterializerReconciler) failMaterialize(ctx context.Context, repo *s
223223
return ctrl.Result{}, nil
224224
}
225225

226-
// ensureRegistrationPackage creates a tap-managed Package for a materialized
227-
// PackageSource, so its install-marked components (the ApplicationDefinition
228-
// registrations) deploy and its apps become browsable in the catalog on connect
229-
// rather than only after a manual `cozypkg add`. The Package is labelled and
230-
// annotated with its source so untap removes exactly the one this tap created.
226+
// reconcileRegistrationPackage converges the tap-managed registration Package
227+
// for a materialized PackageSource on EVERY reconcile, not only at connect. Its
228+
// install-marked components (the ApplicationDefinition registrations) deploy so
229+
// the apps become browsable in the catalog rather than only after a manual
230+
// `cozypkg add`; the Package is labelled and annotated with its source so untap
231+
// removes exactly the one this tap created.
231232
//
232-
// It only ever auto-registers the "default" variant (leaving Spec.Variant empty,
233-
// which the Package reconciler resolves to "default"), so the install target is
234-
// deterministic rather than "whichever variant the artifact lists first". It
235-
// refuses to auto-install a variant carrying privileged components, because the
236-
// connect path has none of the confirmation `cozypkg add --allow-privileged`
237-
// enforces; the operator registers those manually. Both skips are surfaced as a
238-
// Warning Event on the source.
239-
func (r *TapMaterializerReconciler) ensureRegistrationPackage(ctx context.Context, ps *cozyv1alpha1.PackageSource, repo *sourcev1.OCIRepository) error {
233+
// Auto-registration is confined to a "default" variant with no privileged
234+
// install components: the connect path has none of the confirmation `cozypkg add
235+
// --allow-privileged` enforces, so a privileged (or absent) default variant is
236+
// never auto-installed. This is checked on every reconcile, not just at create
237+
// time: a repository that ships a benign revision and later flips its default
238+
// variant to privileged (or drops it) must not keep an installed, unconfirmed
239+
// privileged workload, so a Package THIS tap previously created is de-registered
240+
// (deleted, which garbage-collects its registration HelmReleases and their
241+
// ApplicationDefinitions). A user's own Package (created by `cozypkg add`, no tap
242+
// label) is never adopted or deleted; its owner is responsible for it.
243+
func (r *TapMaterializerReconciler) reconcileRegistrationPackage(ctx context.Context, ps *cozyv1alpha1.PackageSource, repo *sourcev1.OCIRepository) error {
244+
name := ps.GetName()
245+
skip := autoRegisterSkipReason(ps)
246+
247+
var existing cozyv1alpha1.Package
248+
getErr := r.Get(ctx, types.NamespacedName{Name: name}, &existing)
249+
if getErr != nil && !apierrors.IsNotFound(getErr) {
250+
return getErr
251+
}
252+
exists := getErr == nil
253+
ours := exists && collision.Owns(&existing, repo.Name)
254+
255+
if skip != "" {
256+
// Must not auto-register. De-register only a Package THIS tap created;
257+
// leave a user's or another tap's Package alone.
258+
if ours {
259+
if err := client.IgnoreNotFound(r.Delete(ctx, &existing)); err != nil {
260+
return err
261+
}
262+
r.warnNotAutoRegistered(repo, ps, "de-registered because "+skip)
263+
return r.setRegistrationState(ctx, ps, "de-registered: "+skip)
264+
}
265+
r.warnNotAutoRegistered(repo, ps, "not auto-registered because "+skip)
266+
return r.setRegistrationState(ctx, ps, "not auto-registered: "+skip)
267+
}
268+
269+
// Should be registered.
270+
if !exists {
271+
pkg := &cozyv1alpha1.Package{
272+
ObjectMeta: metav1.ObjectMeta{
273+
Name: name,
274+
Labels: map[string]string{tapconst.Label: "true"},
275+
Annotations: map[string]string{tapconst.SourceAnnotation: repo.Name},
276+
},
277+
// Spec.Variant left empty: the Package reconciler resolves it to "default".
278+
}
279+
if err := r.Create(ctx, pkg); err != nil && !apierrors.IsAlreadyExists(err) {
280+
return err
281+
}
282+
return r.setRegistrationState(ctx, ps, "")
283+
}
284+
if ours {
285+
// Already ours from an earlier reconcile: registration stands.
286+
return r.setRegistrationState(ctx, ps, "")
287+
}
288+
if existing.GetLabels()[tapconst.Label] == "true" {
289+
// Tap-labelled but a different source owns it: a stale leftover, since
290+
// collision.PackageSourceName already blocks two live taps sharing a
291+
// PackageSource name. Surface it durably; do not adopt or hot-loop.
292+
reason := fmt.Sprintf("a Package named %q is owned by another tap", name)
293+
r.warnNotAutoRegistered(repo, ps, reason)
294+
return r.setRegistrationState(ctx, ps, reason)
295+
}
296+
// Unmarked: a user's manual `cozypkg add` (or a Package from before this
297+
// version). Its Package already registers the apps, so the goal is met;
298+
// leave it as the user's to manage rather than erroring (which would hot-loop
299+
// the whole tap) or adopting it (which would delete it on untap).
300+
return r.setRegistrationState(ctx, ps, "")
301+
}
302+
303+
// autoRegisterSkipReason returns why a PackageSource's apps must NOT be
304+
// auto-registered on connect, or "" when they may be. Auto-registration is
305+
// limited to a "default" variant with no privileged install components.
306+
func autoRegisterSkipReason(ps *cozyv1alpha1.PackageSource) string {
240307
if !hasDefaultVariant(ps) {
241-
r.warnNotAutoRegistered(repo, ps, `it declares no "default" variant; register it manually with 'cozypkg add'`)
242-
return nil
308+
return `it declares no "default" variant; register it manually with 'cozypkg add'`
243309
}
244-
if privileged := privilegedInstallComponents(ps, "default"); len(privileged) > 0 {
245-
r.warnNotAutoRegistered(repo, ps, fmt.Sprintf("its default variant has privileged install component(s) %v; register it manually with 'cozypkg add --allow-privileged'", privileged))
310+
if privileged := collision.PrivilegedInstallComponents(ps, "default"); len(privileged) > 0 {
311+
return fmt.Sprintf("its default variant has privileged install component(s) %v; register it manually with 'cozypkg add --allow-privileged'", privileged)
312+
}
313+
return ""
314+
}
315+
316+
// setRegistrationState records (or clears, on reason == "") the durable reason a
317+
// PackageSource's apps are not auto-registered, on the PackageSource itself, so
318+
// the dashboard and operator can read it after the Warning Event has expired. It
319+
// is a no-op when the annotation already holds the wanted value.
320+
func (r *TapMaterializerReconciler) setRegistrationState(ctx context.Context, ps *cozyv1alpha1.PackageSource, reason string) error {
321+
current := ps.GetAnnotations()[tapconst.RegistrationStateAnnotation]
322+
if current == reason {
246323
return nil
247324
}
248-
pkg := &cozyv1alpha1.Package{
249-
ObjectMeta: metav1.ObjectMeta{
250-
Name: ps.GetName(),
251-
Labels: map[string]string{tapconst.Label: "true"},
252-
Annotations: map[string]string{tapconst.SourceAnnotation: repo.Name},
253-
},
254-
// Spec.Variant left empty: the Package reconciler resolves it to "default".
325+
base := ps.DeepCopy()
326+
ann := ps.GetAnnotations()
327+
if ann == nil {
328+
ann = map[string]string{}
255329
}
256-
if err := r.Create(ctx, pkg); err != nil {
257-
if !apierrors.IsAlreadyExists(err) {
258-
return err
259-
}
260-
// It already exists; confirm it is this tap's own before treating the
261-
// registration as done, so a foreign Package of the same name is not
262-
// silently assumed to register the apps.
263-
var existing cozyv1alpha1.Package
264-
if gerr := r.Get(ctx, types.NamespacedName{Name: ps.GetName()}, &existing); gerr != nil {
265-
return gerr
266-
}
267-
if !collision.Owns(&existing, repo.Name) {
268-
return fmt.Errorf("a Package named %q already exists and is not managed by this tap", ps.GetName())
269-
}
330+
if reason == "" {
331+
delete(ann, tapconst.RegistrationStateAnnotation)
332+
} else {
333+
ann[tapconst.RegistrationStateAnnotation] = reason
270334
}
271-
return nil
335+
ps.SetAnnotations(ann)
336+
return r.Patch(ctx, ps, client.MergeFrom(base))
272337
}
273338

274-
// warnNotAutoRegistered records that a materialized PackageSource was left
275-
// unregistered, with the reason, so the operator can see why its apps are not in
276-
// the catalog and register them manually.
339+
// warnNotAutoRegistered records on the source, as a Warning Event, that a
340+
// materialized PackageSource's apps were not auto-registered (or were
341+
// de-registered), with the reason. The durable copy lives on the PackageSource
342+
// via setRegistrationState; this is the immediate, human-facing signal.
277343
func (r *TapMaterializerReconciler) warnNotAutoRegistered(repo *sourcev1.OCIRepository, ps *cozyv1alpha1.PackageSource, reason string) {
278344
if r.Recorder != nil {
279345
r.Recorder.Event(repo, corev1.EventTypeWarning, "AppsNotAutoRegistered",
280-
fmt.Sprintf("did not auto-register PackageSource %s: %s", ps.GetName(), reason))
346+
fmt.Sprintf("PackageSource %s: %s", ps.GetName(), reason))
281347
}
282348
}
283349

@@ -292,23 +358,6 @@ func hasDefaultVariant(ps *cozyv1alpha1.PackageSource) bool {
292358
return false
293359
}
294360

295-
// privilegedInstallComponents lists the names of the variant's install-marked
296-
// components that request privileged access.
297-
func privilegedInstallComponents(ps *cozyv1alpha1.PackageSource, variant string) []string {
298-
var out []string
299-
for i := range ps.Spec.Variants {
300-
if ps.Spec.Variants[i].Name != variant {
301-
continue
302-
}
303-
for _, c := range ps.Spec.Variants[i].Components {
304-
if c.Install != nil && c.Install.Privileged {
305-
out = append(out, c.Name)
306-
}
307-
}
308-
}
309-
return out
310-
}
311-
312361
// deleteMaterialized removes every PackageSource materialized from the given
313362
// tap source. Installed Packages are intentionally left in place.
314363
func (r *TapMaterializerReconciler) deleteMaterialized(ctx context.Context, sourceName string) error {
@@ -327,13 +376,14 @@ func (r *TapMaterializerReconciler) pruneMaterialized(ctx context.Context, sourc
327376
if ps.Annotations[tapconst.SourceAnnotation] != sourceName || keep[ps.Name] {
328377
continue
329378
}
330-
if err := r.Delete(ctx, ps); err != nil && client.IgnoreNotFound(err) != nil {
379+
// Delete the registration Package BEFORE the PackageSource. If the
380+
// Package delete fails transiently, the PackageSource is still present so
381+
// the next reconcile lists it and retries; deleting the PackageSource
382+
// first would leave a stranded Package no later pass can find.
383+
if err := r.deleteRegistrationPackage(ctx, ps.Name, sourceName); err != nil {
331384
return err
332385
}
333-
// Remove the registration Package too, so a package the current artifact
334-
// no longer contains (or a whole tap being torn down) leaves the catalog
335-
// instead of stranding its ApplicationDefinition.
336-
if err := r.deleteRegistrationPackage(ctx, ps.Name, sourceName); err != nil {
386+
if err := r.Delete(ctx, ps); err != nil && client.IgnoreNotFound(err) != nil {
337387
return err
338388
}
339389
}

0 commit comments

Comments
 (0)