suite: init-script rejects a username outside the safe charset # The username is interpolated into the init-script shell and role SQL and into # the Cluster's managed roles. A name with a shell metacharacter is a # command-injection shape. The chart must reject it at render time, naming the # offending username, so the metacharacter never reaches the script. Reverting # the guard in init-script.yaml lets the render succeed, so the first test fails. templates: - templates/init-script.yaml - templates/db.yaml release: name: pg-test namespace: tenant-test set: _cluster: scheduling: {} tests: - it: fails the render for a username carrying a shell metacharacter template: templates/init-script.yaml set: users: 'evil";id;echo "x': {} asserts: - failedTemplate: errorMessage: 'invalid username "evil\";id;echo \"x": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)' - it: fails the render for a username with a space template: templates/init-script.yaml set: users: 'a b': {} asserts: - failedTemplate: errorMessage: 'invalid username "a b": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)' # "." and ".." match the charset regex but are rejected by k8s IsConfigMapKey as # Secret keys, so the guard rejects them explicitly (the length cap is a separate, # PostgreSQL-driven concern pinned by the 63/64 boundary tests below). These pin # the guard to the full IsConfigMapKey contract. - it: fails the render for the reserved name "." template: templates/init-script.yaml set: users: '.': {} asserts: - failedTemplate: errorMessage: 'invalid username ".": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)' - it: fails the render for the reserved name ".." template: templates/init-script.yaml set: users: '..': {} asserts: - failedTemplate: errorMessage: 'invalid username "..": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)' # IsConfigMapKey rejects ANY name starting with ".." (hasChDirPrefix), not just # the literal "..", so "..foo" is charset-legal yet cannot install. - it: fails the render for a name starting with ".." template: templates/init-script.yaml set: users: '..foo': {} asserts: - failedTemplate: errorMessage: 'invalid username "..foo": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)' # A username is a PostgreSQL role name: CREATE ROLE silently truncates past 63 # bytes (NAMEDATALEN-1) with a NOTICE, not an error, so a 64-char name would be # created as its 63-byte prefix and then dropped by the delete pass (catalog # truncated != declared full name) in the same hook. The cap is 63, not the # 253-char Secret-key limit. 64 chars fails; 63 renders. Reverting the cap to 253 # lets the 64-char case through. - it: fails the render for a username of 64 characters (past the 63-byte role limit) template: templates/init-script.yaml set: users: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa': {} asserts: - failedTemplate: errorMessage: 'invalid username "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": a username may contain only letters, digits and the characters -._ , must not be "." or start with "..", and must be at most 63 characters (it is a PostgreSQL role name, which CREATE ROLE silently truncates past 63 bytes, and the account name derived from it is a Kubernetes object name)' - it: renders normally for a username at the 63-character boundary set: users: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa': {} template: templates/db.yaml documentSelector: path: kind value: Cluster asserts: - contains: path: spec.managed.roles content: name: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa any: true - it: renders normally for names using the full safe charset set: users: a-b.c_1: {} template: templates/db.yaml documentSelector: path: kind value: Cluster asserts: - contains: path: spec.managed.roles content: name: a-b.c_1 passwordSecret: name: pg-test.a-b-c-1.account any: true # `postgres` is the CNPG-managed superuser and is forbidden as an app role. The # rejection must not depend on bootstrap.enabled: while it is true the # create-users block is gated off, so a restore target carrying users.postgres # installs, and the restore controller's automatic bootstrap clear # (disablePostgresAppBootstrap) then wedges the next reconcile against this fail # — a state the controller records as Succeeded. Rejecting it regardless of # bootstrap keeps the controller from converging a release into an unrenderable # one. Moving the guard back inside the `not .Values.bootstrap.enabled` block # lets the bootstrap case render, so this test fails. - it: rejects users.postgres even while bootstrap.enabled is true template: templates/init-script.yaml set: bootstrap: enabled: true users: postgres: replication: false asserts: - failedTemplate: errorMessage: '`users.postgres` is forbidden by policy. Use a different username.' - it: rejects users.postgres on the normal (non-bootstrap) path too template: templates/init-script.yaml set: users: postgres: replication: false asserts: - failedTemplate: errorMessage: '`users.postgres` is forbidden by policy. Use a different username.'