fix(credentials): delete a leftover account Secret only at its read resourceVersion

store.Ensure replaced a leftover account Secret under a UID-only delete precondition, so a concurrent create or mint that adopted and re-passworded the leftover between the read and the delete, keeping its UID but changing its resourceVersion, had its Secret deleted anyway. The delete now carries the resourceVersion read with the UID, so it fails rather than destroy a Secret a live release owns.

Assisted-by: LLM
Signed-off-by: IvanHunters <xorokhotnikov@gmail.com>
This commit is contained in:
IvanHunters committed 2026-11-18 08:50:44 +03:00
1 parent 66cf74f4cb
commit 7e117559d8
2 files changed
+49 -1

No files matched your search

+6 -1
View File
@@ -163,7 +163,12 @@ func Ensure(ctx context.Context, c client.Client, app App, accounts []account.Ac
return written, fmt.Errorf("%s: %w", name, ErrLiveOwner)
}
}
if err := c.Delete(ctx, cur, client.Preconditions{UID: &cur.UID}); err != nil && !apierrors.IsNotFound(err) {
// Delete the exact object read here, by UID and resourceVersion: a
// concurrent create or mint can have adopted and re-passworded this
// leftover between the read above and here, leaving the UID but a new
// resourceVersion, and the precondition then fails rather than destroy
// the Secret a live release now owns.
if err := c.Delete(ctx, cur, client.Preconditions{UID: &cur.UID, ResourceVersion: &cur.ResourceVersion}); err != nil && !apierrors.IsNotFound(err) {
return written, fmt.Errorf("replace account Secret %s: %w", name, err)
}
case !apierrors.IsNotFound(err):
+43
View File
@@ -6,6 +6,7 @@ import (
"context"
"errors"
"strings"
"sync/atomic"
"testing"
helmv2 "github.com/fluxcd/helm-controller/api/v2"
@@ -16,6 +17,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
"github.com/cozystack/cozystack/pkg/apis/apps/defaulting"
@@ -126,6 +128,47 @@ func TestEnsureSeedsAndKeepsTheirs(t *testing.T) {
}
}
func TestEnsureKeepsALeftoverAdoptedMidReplace(t *testing.T) {
ctx := context.Background()
var adopted atomic.Bool
c := fake.NewClientBuilder().WithScheme(newScheme(t)).WithInterceptorFuncs(interceptor.Funcs{
Delete: func(ctx context.Context, wc client.WithWatch, obj client.Object, opts ...client.DeleteOption) error {
// A concurrent create adopts and re-passwords the leftover between
// Ensure's read and this delete, which bumps its resourceVersion.
if sec, ok := obj.(*corev1.Secret); ok && sec.Name == "postgres-orders.web.account" && adopted.CompareAndSwap(false, true) {
live := &corev1.Secret{}
if err := wc.Get(ctx, client.ObjectKeyFromObject(obj), live); err != nil {
return err
}
live.OwnerReferences = []metav1.OwnerReference{{APIVersion: helmv2.GroupVersion.String(), Kind: helmv2.HelmReleaseKind, Name: "postgres-orders", UID: "uid-winner"}}
live.Data[account.PasswordKey] = []byte("minted")
if err := wc.Update(ctx, live); err != nil {
return err
}
}
return wc.Delete(ctx, obj, opts...)
},
}).Build()
app := testApp()
leftover, err := SeedSecret(app, account.Account{Username: "web", Mapped: "web"}, nil, metav1.Now(), "earlier")
if err != nil {
t.Fatal(err)
}
leftover.UID = "uid-leftover"
if err := c.Create(ctx, leftover); err != nil {
t.Fatal(err)
}
if _, err := Ensure(ctx, c, app, accounts("web"), nil, metav1.Now(), "jane"); err == nil {
t.Fatal("Ensure must fail rather than replace a leftover adopted under it")
}
sec := getSecret(t, c, "postgres-orders.web.account")
if len(sec.OwnerReferences) != 1 || sec.OwnerReferences[0].UID != "uid-winner" || string(sec.Data[account.PasswordKey]) != "minted" {
t.Fatalf("the adopted Secret must survive: %+v data=%q", sec.ObjectMeta, sec.Data[account.PasswordKey])
}
}
func TestEnsureReplacesStale(t *testing.T) {
ctx := context.Background()
c := fake.NewClientBuilder().WithScheme(newScheme(t)).Build()