mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-01 20:56:02 +09:00
feat(postgres)!: issue user passwords through the credentials API
The chart generated every user's password at render time into <release>-credentials, which tenants could read again at any time and which Helm history kept for MaxHistory revisions, so a deployment whose policy requires a credential shown once could not run PostgreSQL here. The chart now renders a CredentialTemplate per user, and the users move under CloudNativePG's managed roles, each pointing at its account Secret, so the operator creates the role with the SCRAM verifier the Secret holds, applies a changed Secret live, and reverts a password set inside PostgreSQL. Every role is rendered as the init-script created it, because the operator reverts an attribute that differs, revokes a parent role missing from inRoles and clears a comment the spec leaves unset. The init-script stops creating, passwording and altering accounts, where it would be a second writer, and keeps the two things the operator has no field for: the marker membership and the reset of the createrole_self_grant an earlier chart set. Its marking pass waits a bounded time for CloudNativePG to create the declared roles before it marks them. users.app adopts the owner CloudNativePG bootstraps, whose password the instance manager applies again from the bootstrap secret on every pod start, so that secret is the app user's account Secret and both writers apply the one stored verifier. A users.<name>.password left in values draws an admission warning that points at a mint on the user's Credential. createRole is refused on every render path. A role a CREATEROLE user creates makes that user a member of it, CloudNativePG revokes every membership a managed role is not declared to hold, and the revoke fails on the dependent self-grant, so the user would sit in cannotReconcile on ordinary use; there is no per-role switch to leave memberships alone. The account-name checks render with the managed roles too, so a restore cannot bypass them: a user CloudNativePG or PostgreSQL reserves, a database whose derived <name>_admin or <name>_readonly role starts with pg_ or cnpg_ (which makes the CloudNativePG webhook reject the whole Cluster), and a user whose account name is no valid object name are refused. The chart's account naming is pinned to the API's on one table of names. A pre-upgrade hook refuses to let CloudNativePG adopt a role the chart did not mark, because the operator adopts a managed role within seconds of the spec reaching it, before any post-upgrade job could look. It reads the live Cluster first and connects to the database only when the upgrade adds a name CloudNativePG does not manage yet, so a database that is down does not block the resize that might fix it. For each absent declared database role it creates and marks the role as the superuser before the Cluster applies, so no tenant can plant it in the adoption window. The refusal and the create run in one psql session, keyed on a snapshot of the role's existence taken at the top, so a role planted in between collides with the CREATE and stops the hook; the statements are built with format() and \gexec, since psql does not substitute :'role' inside a DO $$ block. Verified on postgres:18: a fresh role is created NOINHERIT, commented and marked, a role planted after the snapshot fails the hook, and an existing unmarked role is refused. The hook reads the Cluster at run time with a ServiceAccount that may get only that Cluster, rather than through a template lookup, which nothing re-renders when the object changes. The flag an earlier chart set when a user held createRole is still read and never set, so such a release keeps refusing to adopt a role it did not mark. The EndpointAttachment e2e suite read testuser's password from that Secret to probe the attached addresses. It now mints one per probe and waits until the application's own Service accepts it, so the attached address is still queried exactly once. BREAKING CHANGE: PostgreSQL passwords are no longer readable from <release>-credentials. A password is issued once, by a mint on the account's Credential in core.cozystack.io, and replaced by the next mint. users.<name>.createRole is refused; a release that sets it keeps its previous chart version and passwords until it is false. Deleting a Postgres application whose release is suspended is refused. The conversion needs a platform variant with migrations enabled. Co-authored-by: IvanHunters <xorokhotnikov@gmail.com> Co-authored-by: Andrei Kvapil <kvapss@gmail.com> Assisted-by: LLM Signed-off-by: Timofei Larkin <lllamnyp@gmail.com> Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
This commit is contained in:
1 parent
780a358083
commit
a67fa94fdb
32 files changed
+1430
-972
No files matched your search
@@ -14,6 +14,9 @@ import (
|
||||
corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1"
|
||||
)
|
||||
|
||||
// The rows from "web" to "x." are pinned for the postgres chart's account-name
|
||||
// helper too (packages/apps/postgres/tests/managed_roles_cluster_test.yaml):
|
||||
// the chart names each managed role's Secret and must name it as the API does.
|
||||
func TestMapName(t *testing.T) {
|
||||
tests := []struct {
|
||||
in, want string
|
||||
@@ -22,6 +25,12 @@ func TestMapName(t *testing.T) {
|
||||
{in: "web", want: "web"},
|
||||
{in: "Bob.Smith", want: "bob-smith"},
|
||||
{in: "app_user", want: "app-user"},
|
||||
{in: "UPPER", want: "upper"},
|
||||
{in: "x-1", want: "x-1"},
|
||||
{in: "db.user_2", want: "db-user-2"},
|
||||
{in: "app_", wantErr: true},
|
||||
{in: "_svc", wantErr: true},
|
||||
{in: "x.", wantErr: true},
|
||||
{in: "-leading", wantErr: true},
|
||||
{in: "", wantErr: true},
|
||||
{in: strings.Repeat("a", 64), wantErr: true},
|
||||
|
||||
@@ -2357,28 +2357,34 @@ func (r *REST) warnRemovedKubernetesFields(ctx context.Context, app *appsv1alpha
|
||||
}
|
||||
}
|
||||
|
||||
// removedUserPasswordKinds are the Application kinds whose per-user `password`
|
||||
// field was removed from the chart render -- passwords are chart-generated into
|
||||
// the <release>-credentials Secret. Like the Kubernetes fields above the key is
|
||||
// still accepted and stored (the user object's schema keeps additionalProperties
|
||||
// open, and the render preserves an existing password through lookup), so a value
|
||||
// left from before the upgrade stays the LIVE credential and there is no longer a
|
||||
// values knob to change it. Warn so an operator editing it is told it has no
|
||||
// effect instead of getting a silent 200.
|
||||
var removedUserPasswordKinds = map[string]bool{
|
||||
postgresKind: true,
|
||||
mariadbKind: true,
|
||||
// removedUserPasswordKinds maps each Application kind whose per-user `password`
|
||||
// field was removed from the chart render to the warning that field draws. Like
|
||||
// the Kubernetes fields above the key is still accepted and stored (the user
|
||||
// object's schema keeps additionalProperties open), and a value left from before
|
||||
// the upgrade can still be the LIVE credential, with no values knob left to
|
||||
// change it. Warn so an operator editing it is told it has no effect instead of
|
||||
// getting a silent 200, and where the password comes from now.
|
||||
var removedUserPasswordKinds = map[string]string{
|
||||
postgresKind: mintedPasswordWarning,
|
||||
mariadbKind: "spec.users[%q].password is ignored: passwords are auto-generated into the <release>-credentials Secret and cannot be set from values. Read the current password from that Secret; editing this field has no effect.",
|
||||
}
|
||||
|
||||
// mintedPasswordWarning is the warning of a kind whose user passwords the
|
||||
// credentials API issues: the tenant cannot read <release>-credentials, so the
|
||||
// warning names the operation that sets a password instead.
|
||||
const mintedPasswordWarning = "spec.users[%q].password is ignored: passwords are issued through the credentials API and cannot be set from values. Mint a new password on the user's Credential (credentials.core.cozystack.io, mint subresource); editing this field has no effect."
|
||||
|
||||
const (
|
||||
postgresKind = "Postgres"
|
||||
mariadbKind = "MariaDB"
|
||||
)
|
||||
|
||||
// warnRemovedUserPasswords emits a client-facing admission warning for every
|
||||
// spec.users[<name>].password still present on a Postgres or MariaDB resource.
|
||||
// spec.users[<name>].password still present on a kind removedUserPasswordKinds
|
||||
// lists.
|
||||
func (r *REST) warnRemovedUserPasswords(ctx context.Context, app *appsv1alpha1.Application) {
|
||||
if !removedUserPasswordKinds[r.kindName] || app == nil || app.Spec == nil || len(app.Spec.Raw) == 0 {
|
||||
message, removed := removedUserPasswordKinds[r.kindName]
|
||||
if !removed || app == nil || app.Spec == nil || len(app.Spec.Raw) == 0 {
|
||||
return
|
||||
}
|
||||
// Decode the users map leniently, then each user on its own, so one malformed
|
||||
@@ -2411,8 +2417,7 @@ func (r *REST) warnRemovedUserPasswords(ctx context.Context, app *appsv1alpha1.A
|
||||
continue
|
||||
}
|
||||
if _, present := u["password"]; present {
|
||||
warning.AddWarning(ctx, "", fmt.Sprintf(
|
||||
"spec.users[%q].password is ignored: passwords are auto-generated into the <release>-credentials Secret and cannot be set from values. Read the current password from that Secret; editing this field has no effect.", user))
|
||||
warning.AddWarning(ctx, "", fmt.Sprintf(message, user))
|
||||
}
|
||||
}
|
||||
// MariaDB force-generates root's password (root is chart-managed), silently
|
||||
|
||||
@@ -36,14 +36,14 @@ import (
|
||||
"github.com/cozystack/cozystack/pkg/config"
|
||||
)
|
||||
|
||||
// Postgres and MariaDB dropped users[].password from the chart render but the
|
||||
// key is still accepted and stored, and a leftover value stays the live
|
||||
// credential. The warning telling an operator that editing it has no effect is
|
||||
// only useful if wired into the write path; deleting either call site
|
||||
// (rest.go Create / Update) must fail loudly. These tests drive Create and
|
||||
// Update end to end and assert the warning surfaces; TestWarnRemovedUserPasswords
|
||||
// below pins the helper in isolation (which kinds warn, and that a missing
|
||||
// password stays quiet).
|
||||
// The kinds removedUserPasswordKinds lists dropped users[].password from the
|
||||
// chart render but the key is still accepted and stored, and a leftover value
|
||||
// can stay the live credential. The warning telling an operator that editing
|
||||
// it has no effect is only useful if wired into the write path; deleting either
|
||||
// call site (rest.go Create / Update) must fail loudly. These tests drive
|
||||
// Create and Update end to end and assert the warning surfaces;
|
||||
// TestWarnRemovedUserPasswords below pins the helper in isolation (which kinds
|
||||
// warn, and that a missing password stays quiet).
|
||||
|
||||
func newPostgresWarnREST(t *testing.T, objects ...client.Object) *REST {
|
||||
t.Helper()
|
||||
@@ -191,6 +191,31 @@ func TestWarnRemovedUserPasswords(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A kind whose passwords the credentials API issues points the warning at a
|
||||
// mint on the user's Credential: its <release>-credentials Secret holds no user
|
||||
// password a tenant can read, so naming it sends the reader to a dead end.
|
||||
func TestWarnRemovedUserPasswords_PointsAtMint(t *testing.T) {
|
||||
for _, kind := range []string{postgresKind} {
|
||||
t.Run(kind, func(t *testing.T) {
|
||||
r := &REST{kindName: kind}
|
||||
app := &appsv1alpha1.Application{Spec: &apiextv1.JSON{Raw: []byte(`{"users":{"app":{"password":"hackme"}}}`)}}
|
||||
rec := &fakeWarningRecorder{}
|
||||
ctx := warning.WithWarningRecorder(context.Background(), rec)
|
||||
r.warnRemovedUserPasswords(ctx, app)
|
||||
if len(rec.warnings) != 1 {
|
||||
t.Fatalf("want one warning, got %v", rec.warnings)
|
||||
}
|
||||
w := rec.warnings[0]
|
||||
if !strings.Contains(w, `spec.users["app"].password is ignored`) || !strings.Contains(w, "credentials.core.cozystack.io, mint subresource") {
|
||||
t.Errorf("warning does not point at a mint on the Credential: %q", w)
|
||||
}
|
||||
if strings.Contains(w, "<release>-credentials") {
|
||||
t.Errorf("warning still names <release>-credentials: %q", w)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A "_"/"-" collision that an UPDATE INTRODUCES (present in the new spec, absent
|
||||
// from the old) must draw an admission warning in each namespace MariaDB renders
|
||||
// with a "_"→"-" mapping — usernames, database names, and (database,user) Grant
|
||||
|
||||
Reference in new issue
Block a user