Skip to content

feat: support hash-only password secrets for users (password-sha256) - #169

Open
0dataloss wants to merge 5 commits into
chideat:mainfrom
0dataloss:hashed_password
Open

0dataloss wants to merge 5 commits into
chideat:mainfrom
0dataloss:hashed_password

Conversation

@0dataloss

@0dataloss 0dataloss commented Nov 18, 2026 •

Copy link
Copy Markdown

Closes #167
Fixes #168

What

A User's password Secret can now hold password-sha256, the hex SHA-256 of the password, instead of password. Only the hash is ever stored in the cluster:

read -rs P; H=$(printf '%s' "$P" | sha256sum | cut -d' ' -f1); unset P
kubectl -n NAMESPACE create secret generic NAME --from-literal=password-sha256=$H
  • Webhook: a password-sha256 Secret is checked for a well-formed digest (64 hex characters), not for the plaintext rules. A Secret that sets both keys is rejected.
  • Operator: applies #<hash> instead of >password with ACL SETUSER on every node.
  • Startup helper: writes #<hash> into the ACL file, so the user survives restarts and failovers.

Changing the password still works by updating the Secret. Plaintext Secrets work exactly as before.

Why

Valkey checks passwords against their SHA-256, so it doesn't need the plaintext. For application accounts, keeping the plaintext in the cluster is avoidable exposure. See #167.

Safety changes in the same PR

  • Exactly one password key. A Secret must set exactly one of password and password-sha256. This is checked by which keys are present, so a hash next to an empty password key is rejected.
  • The system account can't use a hash. The operator logs in with it, so password-sha256 on a system account is rejected by the webhook and by the controller.
  • No silent nopass for a User. A Secret that gives no password now fails the User, which goes to Fail with a message and is retried. Nothing is applied to the nodes.
  • No nopass for the operator user (The operator user gets nopass when its password Secret has no usable password #168). An unusable operator Secret is now an error, in both the startup helper and the operator, instead of a discarded error that becomes nopass. Both also check the operator's final password, so an entry that lost its Secret reference fails instead of becoming nopass.
  • No nopass for a configured default user. An unusable default Secret fails ACL generation. Skipping the user would let Valkey recreate the built-in default user with no password.
  • operator is a reserved username. The webhook, the reconciler and the handler reject a custom User named operator, which would otherwise replace the operator's credentials.
  • The sentinel password stays plaintext. requirepass has no hashed form, so a hash-only or empty defaultPasswordSecret is rejected by the operator, and run_sentinel.sh refuses to start with it.
  • Every passwordSecrets entry survives a restart. The ACL ConfigMap keeps all references in a new additionalPasswords field, so a rotation with two Secrets keeps working across pod restarts. Older helper images ignore the field.
  • Other users with an unusable Secret are left out of the ACL file and can't log in. That isn't an error, so one bad Secret can't keep every pod from starting.

Limits

  • Where a hash can't be used: the system account and the sentinel password (spec.sentinel.access.defaultPasswordSecret) still need the plaintext, because the operator logs in with them. The Cluster and Failover default user is a regular User, so it can use a hash.
  • Password strength: it can't be checked from a hash, and SHA-256 is fast and unsalted, so a weak password could be recovered from the Secret. The docs warn about this and recommend a long random password.

Testing

  • make test passes.
  • New unit tests:
    • Secret parsing and ACLRule (pkg/types/user);
    • the ACL file line and loadUserPassword (cmd/helper/commands/helper);
    • webhook cases for password-sha256 (internal/webhook/v1alpha1);
    • the first tests for the User handler (internal/controller/user).
  • System test with an image built from this branch, on Kubernetes v1.36.4 with Valkey 9.1.2, architecture replica (1 node, TLS). All 40 checks passed:
    • Hash-only Secret and User: the node holds exactly the hash, the right password logs in, a wrong one is refused, the Secret holds only password-sha256, and the ACL ConfigMap holds only the Secret name.
    • Pod restart: the helper writes #<hash> (no > rule) into the ACL file, and logins still work.
    • Password change: after a new hash was written into the Secret, the node had it within about 1 second. The new password works and the old one is refused, also after a restart.
    • Webhook: it rejects a Secret with both keys, a malformed hash, and a hash on a system account, and accepts a valid hash on a custom account.
    • Emptied Secret: the User goes to Fail, while the node keeps the previous hash and is never nopass.
    • Broken operator Secret (password missing, or both keys set): the helper fails, run_failover.sh exits, and Valkey never starts with that ACL. No nopass operator account is reachable. Once the Secret is fixed, the pod comes back with the password.
    • Removal: deleting the test User removes it from the node and from the ACL ConfigMap.
  • After the review, on an image built from eb8d0a5: the 40 checks above pass again, plus:
    • Review follow-ups on the same instance: a custom User named operator is rejected. Two passwordSecrets both work, also after a restart, and a rotation works. A configured default user with a broken Secret stops the pod instead of starting with no password. An operator entry without a Secret reference stops the pod instead of starting as nopass.
    • Sentinel failover, 3 Valkey pods and 3 sentinels, 24 checks: after the master is deleted, the promoted master and the old master (back as a replica) both accept the hash-only application user's password and keep the data.
    • Sentinel password: a hash-only or empty defaultPasswordSecret stops the sentinel from starting.

Commits

  1. feat(user): the feature itself, across the webhook, operator, helper and controller.
  2. fix(acl): no nopass for the operator user (The operator user gets nopass when its password Secret has no usable password #168).
  3. fix(webhook): password-sha256 rejected on system accounts at admission.
  4. docs: user guide and API reference.
  5. fix(acl): review follow-ups (default user, reserved operator username, sentinel password, additionalPasswords, key-presence check).

Docs

  • docs/guides/user-guide.md: new section, "Storing only a hash of the password".
  • docs/api/v1alpha1-user.md: describes the password-sha256 key.

🤖 Generated with Claude Code

Matteo Castellani and others added 4 commits November 18, 2026 09:08
Both ACL loaders ignored the error from user.NewPassword, so an unusable
operator secret produced a nil password:

- the startup helper turned it into "nopass" in the ACL file, leaving
  the operator account open on every pod that restarted;
- the operator-side loader kept the user without its secret name, and
  writing it back to the ACL ConfigMap would lead to the same "nopass"
  at the next pod start.

The password-sha256 support adds a new way to hit this (a secret with
both password and password-sha256), so the operator user now fails
loudly instead: a missing or empty password, both keys, or a hash is an
error. The operator authenticates with this account, so it needs a
non-empty plaintext. The new user.ValidateOperatorPassword holds that
rule for both loaders.

Other users keep their behaviour: an unusable secret leaves the user
out of the ACL file, so it can not log in. That is not an error,
because the helper runs at every pod start and one bad custom secret
must not keep the whole instance from starting.

The helper logic moves into loadUserPassword so it can be unit tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The operator authenticates with the system account, so it needs the
plaintext password. The webhook returned early for system accounts and
accepted a password-sha256 secret; the User was then only rejected
later by the controller. Check the secrets at admission instead, so the
mistake fails at once with a clear message.

A system-account secret that does not exist yet is still accepted, as
before, and left to the controller.

Also adds webhook tests for password-sha256 secrets: a hash on a custom
account skips the plaintext check, and a malformed hash or a secret
with both keys is rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Explain how to keep only the SHA-256 of a password in a User secret:
how to create it without the password ever being shown or saved, why
printf '%s' rather than echo, the one-key-only rule, and how to move
an existing secret such as the default user's with a patch instead of
recreating it, which would drop the operator's finalizer and owner
reference.

State the limits: the operator's own system account and the sentinel
password still need the plaintext, because the operator logs in with
them.

Warn that password strength can not be checked from a hash, and that
SHA-256 is fast and unsalted, so a weak password can be recovered by
anyone who can read the secret. Choosing a strong password stays with
whoever creates it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chideat

chideat commented Nov 20, 2026

Copy link
Copy Markdown
Owner

I reviewed b2dff63 against #167 and #168. I’d keep both issues open for now.

#168 · The reported nopass bug is fixed when the Secret reference is present. Both ACL loaders now reject an operator Secret with a missing or empty password, and the startup script exits on the helper error. The issue also describes older ConfigMaps that may already have lost the Secret reference. Both loaders skip the new check in that case, so the helper can still emit nopass. Please validate the final operator credential in both loaders and add a regression test that ACL generation fails for an existing entry with no Secret reference. Helper · Controller loader

#167 · A valid hash works for an application user with one Secret. The controller sends #<hash> to the nodes, and the startup helper writes the same rule to the ACL file. The following cases still need attention.

  • A configured default user needs explicit handling when its credential is invalid. The helper skips the entry, and Valkey recreates its built-in default user when the ACL file omits it. Please fail ACL generation for this case and cover it separately from an ordinary application user. Helper · Valkey ACL loader
  • Reserve the operator username for the system account in admission and reconciliation. The hash restriction checks accountType, and the live ACL update selects the account by username. Validator · Live update
  • Enforce the plaintext requirement when Sentinel loads a configured defaultPasswordSecret. NewSentinelUser now accepts a hash-only Secret. The startup script reads only password and can omit requirepass. Sentinel credential loader · Startup script
  • Preserve all spec.passwordSecrets references for restart. In a fake-client check with two valid hash Secrets, only the second reference was stored in the ACL ConfigMap. This existing limitation also affects hash-based password rotation. Handler
  • Check mutual exclusion by key presence. The validator and parser accept a valid hash alongside an empty password key, and the new test expects success. Support hash-only password secrets for users (password-sha256) #167 requires exactly one key. Test case

All 41 packages with tests passed locally. The PR’s system test covered restart and rotation on a single TLS-enabled replica node. A Sentinel failover test would also verify that the promoted node accepts the application user’s password.

Operator user (chideat#168): check the operator's final password in both ACL
loaders, not only when its entry references a secret. An older ACL
configmap whose operator entry lost the secret reference now fails ACL
generation instead of being applied as "nopass".

Default user: a configured default user with an unusable secret fails
ACL generation instead of being skipped, since Valkey recreates its
built-in default user without a password when the ACL file leaves it
out. A weak plaintext default password is still applied: its strength
is checked on admission, and dropping it would leave no password.

Reserve the operator username for the system account in the webhook,
the reconciler and the handler: the live ACL update and the ACL
configmap select an account by username, so a custom User named
operator would replace the operator's credentials.

Sentinel: reject a hash-only or empty defaultPasswordSecret in
NewSentinelUser, and make the startup script refuse to start when the
mounted secret has password-sha256 or no password, since requirepass
has no hashed form.

Keep every spec.passwordSecrets reference in the ACL configmap through
a new additionalPasswords field, and apply all of them on restart, so a
password rotation with two secrets survives a pod restart.

Check that a secret sets exactly one of password and password-sha256 by
key presence: a hash with an empty password key is now rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@0dataloss

Copy link
Copy Markdown
Author

Thanks for the comment @chideat .
Everything is addressed in eb8d0a5:

#168: operator user

  • Both ACL loaders (the startup helper and the operator) now check the operator's final password, not only when its entry references a Secret.
  • An operator entry with no Secret reference now fails ACL generation with operator user has no password secret. Before, it was applied as nopass.
  • Regression tests cover this case in both loaders.

#167: review points

  • Default user: if a default user is configured and its Secret is unusable, ACL generation now fails instead of skipping the user. Skipping it would let Valkey recreate the built-in default user with no password.
  • Reserved username: operator is now reserved for the system account. The webhook, the reconciler and the handler all reject a custom User with that name.
  • Sentinel password: NewSentinelUser rejects a defaultPasswordSecret that is hash-only or has an empty password. run_sentinel.sh refuses to start if the mounted Secret has password-sha256 or no password.
  • Several passwordSecrets: every reference is now kept in the ACL ConfigMap, using a new additionalPasswords field, and all of them are applied at pod start. A rotation using two Secrets now survives a pod restart.
  • One password key per Secret: "exactly one of password and password-sha256" is now checked by which keys are present, not by their values. A hash next to an empty password key is rejected.

Decisions you may want to weigh in on

  1. Weak default password. A weak plaintext default password is still applied. Its strength is checked at admission, and dropping it at pod start would leave the user with no password at all, which is worse.
  2. Existing ConfigMaps. An existing ConfigMap whose operator or default entry is already broken now stops the pods from starting, with a clear error, where before it silently gave a nopass user. The operator does not repair such an entry by itself; in my test it stayed broken until I fixed it.
  3. Mixed image versions. Helper images older than this PR ignore additionalPasswords. During an upgrade, a pod still on an old helper applies only the first Secret.

Testing

  • make test passes: 41 packages, all ok.
  • make lint reports typecheck errors in internal/valkey/sentinel/node.go. main has the same errors, so they're unrelated to this PR (I think).

I ran the system tests with an image built from eb8d0a5, on Kubernetes v1.36.4 with Valkey 9.1.2.

Regression, replica architecture (1 node, TLS): all 40 checks from the first round pass.

Review follow-ups on the same instance:

Case Result
Custom User with username operator Rejected by the webhook: username operator is reserved for the system account
User with two passwordSecrets The ConfigMap holds password plus additionalPasswords. Both hashes are on the node and both passwords log in, also after a pod restart. Removing the old Secret from the list: the old password is refused within about 1 s, also after a restart.
Configured default user whose Secret has no usable key The helper fails with load default user password failed, missing password or password-sha256 field… and the pod does not start. The User goes to Fail. No default user without a password is ever reachable. Restoring the Secret brings the pod back.
operator entry without its Secret reference The helper fails with operator user has no password secret and no nopass operator account is reachable. Restoring the entry brings the pod back.

Sentinel failover, as you suggested. I used a temporary failover instance with 3 Valkey pods and 3 sentinels, no TLS, and an application user whose Secret holds only password-sha256. All 24 checks pass:

  • Before the failover, every node holds only the hash and the user logs in on the master and on both replicas.
  • I deleted the master and Sentinel promoted a replica after about 8 s.
  • On the promoted master, the user logs in with its password and a wrong password is refused. The data written before the failover is there, and the user can write.
  • The other replica follows the new master and sees the write made after the failover.
  • The old master comes back as a replica with replication up. The user logs in on it, it holds only the hash, and it sees the write made after the failover.

Sentinel password, on a 2-pod run of the same setup:

  • With defaultPasswordSecret changed to hash-only, the sentinel refuses to start with sentinel password secret can not use password-sha256, set password instead. The operator logs sentinel secret sentinel-pw can not use password-sha256.
  • With an empty password, it refuses with sentinel password secret is mounted but has no password.
  • In both cases no sentinel is reachable without a password, and restoring the Secret brings the sentinel back.

Found while testing (not caused by this PR, I think)

When a Secret is removed from a User's spec.passwordSecrets, which is how you rotate a password, the Secret keeps the buf.red/user-finalizer finalizer.

  • When the User is deleted, the controller removes the finalizer only from the Secrets still in the list (internal/controller/user_controller.go).
  • So the dropped Secret can never be deleted, and it blocks deletion of its namespace.

main has the same code.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The operator user gets nopass when its password Secret has no usable password Support hash-only password secrets for users (password-sha256)

2 participants