From 4772b8e7602ef85780208031347b4cf7b3a354b8 Mon Sep 17 00:00:00 2001 From: IvanHunters Date: Wed, 18 Nov 2026 08:56:38 +0300 Subject: [PATCH] fix(credentials): close two concurrent-update account-Secret races seedBeforeUpdate seeded a renamed account before the release write, so a rename that changed only the case of a user name replaced the live account's Secret with a new password and lost it if the write then failed; it now refuses such a rename, since the live account is not actually gone. pruneAfterUpdate deleted Secrets by the request's account list, so a concurrent update that re-added an account and wrote it back had its Secret pruned from under the live release; prune now reads the live release uncached and keeps the accounts it declares. Assisted-by: LLM Signed-off-by: IvanHunters --- pkg/registry/apps/application/rest.go | 2 +- .../apps/application/rest_credentials.go | 40 +++++++++++++++-- .../apps/application/rest_credentials_test.go | 44 +++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/pkg/registry/apps/application/rest.go b/pkg/registry/apps/application/rest.go index 681141558..cd5d1bbe8 100644 --- a/pkg/registry/apps/application/rest.go +++ b/pkg/registry/apps/application/rest.go @@ -673,7 +673,7 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje // Accounts the write removed are gone from the chart now, so their // Secrets can go. - r.pruneAfterUpdate(ctx, app, accounts, options != nil && dryrun.IsDryRun(options.DryRun)) + r.pruneAfterUpdate(ctx, app, options != nil && dryrun.IsDryRun(options.DryRun)) // Convert the updated HelmRelease back to Application convertedApp, err := r.ConvertHelmReleaseToApplication(ctx, helmRelease) diff --git a/pkg/registry/apps/application/rest_credentials.go b/pkg/registry/apps/application/rest_credentials.go index 0fea27d66..5ca90c882 100644 --- a/pkg/registry/apps/application/rest_credentials.go +++ b/pkg/registry/apps/application/rest_credentials.go @@ -181,8 +181,24 @@ func (r *REST) seedBeforeUpdate(ctx context.Context, app *appsv1alpha1.Applicati klog.Warningf("accounts of %s/%s before the update cannot be resolved, seeding none: %v", cur.Namespace, cur.Name, err) return nil } + release := r.storeApp(app.Namespace, app.Name).Release + added := store.Added(prev, accounts) + // An added account whose mapped name already holds a different account is a + // rename that changed only the case of a user name: both map to one Secret. + // Seeding it would replace the live account's Secret with a new password + // before the release write, and a failed write would lose that password with + // no way back. Refuse it, since the live account is not actually gone. + for _, acc := range added { + ex := &corev1.Secret{} + switch err := r.direct().Get(ctx, client.ObjectKey{Namespace: app.Namespace, Name: account.SecretName(release, acc.Mapped)}, ex); { + case err == nil && account.IsAccountSecret(ex) && string(ex.Data[account.UsernameKey]) != acc.Username: + return r.invalidAccounts(app, fmt.Errorf("account %q maps to the name %q, which already belongs to account %q; rename it to a name that does not collide", acc.Username, acc.Mapped, string(ex.Data[account.UsernameKey]))) + case err != nil && !apierrors.IsNotFound(err): + return apierrors.NewInternalError(fmt.Errorf("read account Secret %s: %w", account.SecretName(release, acc.Mapped), err)) + } + } owner := store.OwnerReference(cur) - if _, err := store.Ensure(ctx, r.direct(), r.storeApp(app.Namespace, app.Name), store.Added(prev, accounts), &owner, metav1.Now(), actor(ctx)); err != nil { + if _, err := store.Ensure(ctx, r.direct(), r.storeApp(app.Namespace, app.Name), added, &owner, metav1.Now(), actor(ctx)); err != nil { return apierrors.NewInternalError(fmt.Errorf("seed account Secrets: %w", err)) } return nil @@ -190,11 +206,29 @@ func (r *REST) seedBeforeUpdate(ctx context.Context, app *appsv1alpha1.Applicati // pruneAfterUpdate deletes the account Secrets of the accounts the update // removed, once the HelmRelease write has dropped them from the chart. -func (r *REST) pruneAfterUpdate(ctx context.Context, app *appsv1alpha1.Application, accounts []account.Account, dryRun bool) { +func (r *REST) pruneAfterUpdate(ctx context.Context, app *appsv1alpha1.Application, dryRun bool) { if r.credentials == nil || dryRun { return } - if _, err := store.Prune(ctx, r.direct(), r.storeApp(app.Namespace, app.Name), accounts); err != nil { + // Prune by the accounts the live release declares now, not the ones this + // request sent: a concurrent update can have re-added an account and written + // it back, and its Secret is referenced by the live release, so deleting it + // by this request's older list would break the release. Read uncached, and + // prune nothing when the release or its accounts cannot be resolved. + cur := &helmv2.HelmRelease{} + switch err := r.direct().Get(ctx, client.ObjectKey{Namespace: app.Namespace, Name: r.storeApp(app.Namespace, app.Name).Release}, cur); { + case apierrors.IsNotFound(err): + return + case err != nil: + klog.Warningf("failed to read the live HelmRelease of %s/%s before pruning, pruning none: %v", app.Namespace, app.Name, err) + return + } + keep, err := r.accountsOf(cur.Spec.Values) + if err != nil { + klog.Warningf("accounts of the live %s/%s cannot be resolved, pruning none: %v", app.Namespace, app.Name, err) + return + } + if _, err := store.Prune(ctx, r.direct(), r.storeApp(app.Namespace, app.Name), keep); err != nil { klog.Errorf("failed to prune account Secrets of %s/%s: %v", app.Namespace, app.Name, err) warning.AddWarning(ctx, "", fmt.Sprintf("account Secrets of removed users were not all deleted: %v", err)) } diff --git a/pkg/registry/apps/application/rest_credentials_test.go b/pkg/registry/apps/application/rest_credentials_test.go index d7d81d3f2..ebada7eb8 100644 --- a/pkg/registry/apps/application/rest_credentials_test.go +++ b/pkg/registry/apps/application/rest_credentials_test.go @@ -198,6 +198,50 @@ func TestUpdateSeedsAddedAndPrunesRemoved(t *testing.T) { } } +func TestUpdateRefusesACaseRename(t *testing.T) { + r, c := newCredentialsREST(t) + ctx := credentialsCtx() + if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); err != nil { + t.Fatal(err) + } + before := accountSecrets(t, c)["postgres-orders.web.account"] + + updated := postgresApp(`{"users":{"Web":{}}}`) + _, _, err := r.Update(ctx, "orders", newDefaultUpdatedObjectInfo(updated), nil, nil, false, &metav1.UpdateOptions{}) + if !apierrors.IsInvalid(err) || !strings.Contains(err.Error(), "already belongs to account") { + t.Fatalf("expected Invalid for a case-only rename, got %v", err) + } + after := accountSecrets(t, c)["postgres-orders.web.account"] + if after == nil || after.UID != before.UID || string(after.Data[account.PasswordKey]) != string(before.Data[account.PasswordKey]) { + t.Fatal("a refused rename must leave the live account's Secret untouched") + } +} + +func TestPruneAfterUpdatePrunesByTheLiveRelease(t *testing.T) { + r, c := newCredentialsREST(t) + ctx := credentialsCtx() + if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{},"api":{}}}`), nil, &metav1.CreateOptions{}); err != nil { + t.Fatal(err) + } + // The live release still declares web and api; a stale request whose app no + // longer lists api must not prune api, because a concurrent update may have + // written it back and the live release references it. + r.pruneAfterUpdate(ctx, postgresApp(`{"users":{"web":{}}}`), false) + if accountSecrets(t, c)["postgres-orders.api.account"] == nil { + t.Fatal("prune deleted a Secret the live release still declares") + } + // Once the live release drops api, pruning removes its Secret. + hr := getRelease(t, c) + hr.Spec.Values = &apiextv1.JSON{Raw: []byte(`{"users":{"web":{}}}`)} + if err := c.Update(ctx, hr); err != nil { + t.Fatal(err) + } + r.pruneAfterUpdate(ctx, postgresApp(`{"users":{"web":{}}}`), false) + if accountSecrets(t, c)["postgres-orders.api.account"] != nil { + t.Fatal("an account the live release no longer declares must be pruned") + } +} + func TestDeleteRefusesSuspendedRelease(t *testing.T) { r, c := newCredentialsREST(t) ctx := credentialsCtx()