Skip to content

Add support for automatic client authentication via TLS certificate fields - #1920

Merged
zuiderkwast merged 8 commits into
valkey-io:unstablefrom
omanges:auto_auth_tls
Jul 12, 2025
Merged

zuiderkwast merged 8 commits into
valkey-io:unstablefrom
omanges:auto_auth_tls

Conversation

@omanges

@omanges omanges commented Apr 6, 2025 •

Copy link
Copy Markdown
Contributor

This PR implements support for automatic client authentication based on a field in the client's TLS certificate.

API Changes:

  • New configuration directive tls-auth-clients-user, values CN | off, default off. CN means take username from the CommonName field in the client's certificate.
  • New INFO field acl_access_denied_tls_cert under the Stats section, indicating the number of failed authentications using this feature, i.e. client certificates for which no matching username was found.
  • New reason "tls-cert" in the ACL log, logged when a client certificate's CommonName fails to match any existing username.

🔒 Feature Overview:

  • Introduces a new configuration directive: tls-auth-clients-user.

    • Allows specifying a certificate field (only CN for now) to map to a Valkey user.

    • If a matching user is found, the client is automatically authenticated as that user upon TLS handshake.

    • Falls back to the default user if no match is found.

⚙️ Implementation Details:

  • Added getCertFieldByName() utility to extract fields from peer certificates.

  • Added autoAuthenticateClientFromCert() to handle automatic login logic post-handshake.

  • Integrated automatic authentication into the TLSAccept function after handshake completion.

  • Updated test suite (tests/integration/tls.tcl) to validate the feature.

✅ Benefits:

  • Enables smoother integration with mTLS-based infrastructures.

  • Reduces the need for clients to manually send AUTH commands.

  • Enhances security by tightly coupling certificate identity with Valkey access control.

🔬 Example:

tls-auth-clients-user CN

This will authenticate the client as the user matching the certificate's Common Name.

Closes #1866

@omanges
omanges marked this pull request as draft April 6, 2025 13:02
@omanges
omanges marked this pull request as ready for review April 6, 2025 13:04
@zuiderkwast
zuiderkwast self-requested a review April 7, 2025 19:10
@codecov

codecov Bot commented Apr 7, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 43.75000% with 9 lines in your changes missing coverage. Please review.

Project coverage is 71.46%. Comparing base (d37dc52) to head (4495f63).
Report is 30 commits behind head on unstable.

Files with missing lines Patch % Lines
src/networking.c 11.11% 8 Missing ⚠️
src/connection.h 66.66% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #1920      +/-   ##
============================================
- Coverage     71.46%   71.46%   -0.01%     
============================================
  Files           123      123              
  Lines         66927    67132     +205     
============================================
+ Hits          47831    47973     +142     
- Misses        19096    19159      +63     
Files with missing lines Coverage Δ
src/acl.c 90.58% <100.00%> (-0.14%) ⬇️
src/config.c 78.47% <ø> (ø)
src/server.c 88.10% <100.00%> (+0.02%) ⬆️
src/server.h 100.00% <ø> (ø)
src/tls.c 100.00% <ø> (ø)
src/connection.h 87.09% <66.66%> (-0.69%) ⬇️
src/networking.c 88.39% <11.11%> (+<0.01%) ⬆️

... and 27 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@zuiderkwast zuiderkwast left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I didn't do a complete review. For example I didn't look at the tests at all yet.

I'm not sure if the config should allow any field. Maybe only CN makes sense ever and then the config should maybe not be selecting a field. But let's discuss it and think about what we can do.

I found the corresponding features in ETCD and Kubernetes. The both use only common name as username:

Comment thread src/config.c Resolved
Comment thread src/tls.c Resolved
Comment thread src/tls.c Resolved
Comment thread src/tls.c Resolved
Comment thread src/tls.c Resolved
Comment thread src/connection.h Resolved
Comment thread src/tls.c Resolved
@zuiderkwast zuiderkwast moved this to Optional for next release candidate in Valkey 9.0 Apr 22, 2025
@omanges
omanges force-pushed the auto_auth_tls branch 3 times, most recently from 6574657 to e2fd2c4 Compare May 5, 2025 06:39
Comment thread src/connection.c Resolved
Comment thread src/connection.h Resolved
Comment thread src/networking.c Resolved
@madolson

madolson commented May 5, 2025

Copy link
Copy Markdown
Member

@valkey-io/core-team Trying to make an asynchronous decision.

You can read the feature at the high level. We are discussing adding a new config flag, tls-auth-clients-user (yes|no) which automatically authenticates the client to an ACL user based on Common Name (CN) field of an x.509 certificate. There was a discussion about making it more generic, and allow the admin to configure which field to use, which might have a different config like tls-auth-clients-user (CN|O|off).

Please 👍 if you are okay with the recommendation of just a simple yes/no bool. Otherwise, please comment.

@zuiderkwast

zuiderkwast commented May 5, 2025 •

Copy link
Copy Markdown
Contributor

I haven't seen any system using "O" as the user name, but I'm OK with it if we also anticipate that we'll support other fields. Common Name (CN) seems to be the most common, but I've seen read about systems and/or recommendations for using these:

What about combinations? If we use an enum, we'd need to define enum values like "CN or SAN DNS". Or shouldn't we accept combinations?

@madolson

madolson commented May 5, 2025

Copy link
Copy Markdown
Member

Even if there is a remote chance of having some amount of customizations would lead me to recommend we should just use an enum with one option for now CN.

Comment thread src/networking.c Resolved
Comment thread src/server.h Resolved
Comment thread src/networking.c Resolved
@PingXie

PingXie commented May 29, 2025

Copy link
Copy Markdown
Member

@valkey-io/core-team Trying to make an asynchronous decision.

You can read the feature at the high level. We are discussing adding a new config flag, tls-auth-clients-user (yes|no) which automatically authenticates the client to an ACL user based on Common Name (CN) field of an x.509 certificate. There was a discussion about making it more generic, and allow the admin to configure which field to use, which might have a different config like tls-auth-clients-user (CN|O|off).

Please 👍 if you are okay with the recommendation of just a simple yes/no bool. Otherwise, please comment.

@madolson I thought you were advocating for using O but you voted for a fixed CN field? I would've been siding with a boolean flag if I didn't see your O comment. if we still see a need to support O in the future, is the plan to introduce a second config?

@madolson

Copy link
Copy Markdown
Member

I thought you were advocating for using O but you voted for a fixed CN field? I would've been siding with a boolean flag if I didn't see your O comment. if we still see a need to support O in the future, is the plan to introduce a second config?

There is a team at amazon who wanted it off the O field, but they honestly said they didn't care. They wrote all this logic in a module so they probably won't adopt what we build regardless.

@PingXie

PingXie commented May 29, 2025

Copy link
Copy Markdown
Member

I thought you were advocating for using O but you voted for a fixed CN field? I would've been siding with a boolean flag if I didn't see your O comment. if we still see a need to support O in the future, is the plan to introduce a second config?

There is a team at amazon who wanted it off the O field, but they honestly said they didn't care. They wrote all this logic in a module so they probably won't adopt what we build regardless.

gotcha. I am less concerned about a specific team's implementation decision but if they see this being a legit use case maybe we should consider a special config instead, just to leave that door open? I feel that authentication on a team/org level isn't a uncommon use case.

@madolson madolson added the major-decision-approved Major decision approved by TSC team label Jun 2, 2025
Comment thread src/config.c Resolved
@madolson

Copy link
Copy Markdown
Member

@omanges Will you have time to update this? We would like to have in Valkey 9.0, and just trying to gauge the status of open PRs.

omanges added a commit to omanges/valkey that referenced this pull request Jul 17, 2025
1. To check if the user is enabled
2. To use clientSetUser
3. To notify user change has happened

Signed-off-by: Omkar Mestry <[email protected]>
@omanges

omanges commented Jul 17, 2025

Copy link
Copy Markdown
Contributor Author

@madolson can you please review the PR :- #2364

I have addressed all the open comments

@sandeepkunusoth

sandeepkunusoth commented Dec 22, 2025 •

Copy link
Copy Markdown
Member

Hi for automatic TLS cert based authentication, when user name with CN is having nopass in ACL how are we restricting this user to login without certificates when tls-auth-clients is optional. we have usecase where we are supporitng both password based and certfifcate based. in this case if user tries to login with CN as username without client certs and without password they are able to login #2962

StavRLevi added a commit to redis/redis that referenced this pull request Dec 25, 2025
This PR implements support for automatic client authentication based on
a field in the client's TLS certificate.
We adopt ValKey’s PR: valkey-io/valkey#1920

API Changes:

Add New configuration tls-auth-clients-user  
  -  Allowed values: `off` (default), `CN`.
  - `off` – disable TLS certificate–based auto-authentication.
- `CN` – derive the ACL username from the Common Name (CN) field of the
client certificate.
 
New INFO stat
  - `acl_access_denied_tls_cert`
- Counts failed TLS certificate–based authentication attempts, i.e. TLS
connections where a client certificate was presented, a username was
derived from it, but no matching ACL user was found.

New ACL LOG reason
  - Reason string: `"tls-cert"`
- Emitted when a client certificate’s Common Name fails to match any
existing ACL user.


Implementation Details:

- Added getCertFieldByName() utility to extract fields from peer
certificates.

- Added autoAuthenticateClientFromCert() to handle automatic login logic
post-handshake.

- Integrated automatic authentication into the TLSAccept function after
handshake completion.

- Updated test suite (tests/integration/tls.tcl) to validate the
feature.
@zuiderkwast zuiderkwast added the needs-doc-pr This change needs to update a documentation page. Remove label once doc PR is open. label May 8, 2026
@zuiderkwast

Copy link
Copy Markdown
Contributor

We should document the new ACL LOG reason. Maybe we can do it together with some other docs, such as in valkey-io/valkey-doc#439

madolson added a commit that referenced this pull request Aug 31, 2026
`tlsGetPeerUser()` derived the ACL username from the certificate CN with
`strlen()`, so an embedded NUL truncated the identity before the lookup.
A certificate whose CA vouched subject is `CN=admin\0attacker`
authenticated as the ACL user `admin` and got its privileges. The
sibling SAN URI path already passed an explicit length to
`ACLGetUserByName()`. The CN path did not.

No explicit NUL check is needed. `ACL SETUSER` refuses a username
containing one, so comparing the full length is sufficient. Dropping the
buffer also removes the silent truncation of a CN longer than 255 bytes.

The CN is handed back whether or not it matches, so a failed
authentication now produces an `ACL LOG` entry and bumps
`acl_access_denied_tls_cert` instead of leaving no trace at the default
loglevel.

Affects 9.0 and 9.1, CN path only. The feature arrived in #1920.

Not fixed here: `X509_NAME_get_index_by_NID()` takes the first CN and
ignores the rest, so `CN=admin, CN=attacker` authenticates as `admin`.
Refusing that would break certificates that authenticate today and
contradicts `valkey.conf:275-277`, which documents first match wins for
URI mode. Its own PR.

The openssl CLI strips a NUL from a name, which is why #3078 could not
cover this when it added the URI path, so the fixture is issued with a
placeholder byte and re-signed. The new test fails on unstable and
passes with the fix. `unit/tls` and `unit/acl` pass under `--tls` and
`--tls-module`, and `unit/tls` is clean under `SANITIZER=address`.

Signed-off-by: Madelyn Olson <[email protected]>
valkeyrie-ops Bot pushed a commit that referenced this pull request Aug 31, 2026
`tlsGetPeerUser()` derived the ACL username from the certificate CN with
`strlen()`, so an embedded NUL truncated the identity before the lookup.
A certificate whose CA vouched subject is `CN=admin\0attacker`
authenticated as the ACL user `admin` and got its privileges. The
sibling SAN URI path already passed an explicit length to
`ACLGetUserByName()`. The CN path did not.

No explicit NUL check is needed. `ACL SETUSER` refuses a username
containing one, so comparing the full length is sufficient. Dropping the
buffer also removes the silent truncation of a CN longer than 255 bytes.

The CN is handed back whether or not it matches, so a failed
authentication now produces an `ACL LOG` entry and bumps
`acl_access_denied_tls_cert` instead of leaving no trace at the default
loglevel.

Affects 9.0 and 9.1, CN path only. The feature arrived in #1920.

Not fixed here: `X509_NAME_get_index_by_NID()` takes the first CN and
ignores the rest, so `CN=admin, CN=attacker` authenticates as `admin`.
Refusing that would break certificates that authenticate today and
contradicts `valkey.conf:275-277`, which documents first match wins for
URI mode. Its own PR.

The openssl CLI strips a NUL from a name, which is why #3078 could not
cover this when it added the URI path, so the fixture is issued with a
placeholder byte and re-signed. The new test fails on unstable and
passes with the fix. `unit/tls` and `unit/acl` pass under `--tls` and
`--tls-module`, and `unit/tls` is clean under `SANITIZER=address`.

Signed-off-by: Madelyn Olson <[email protected]>
Backport-Source-PR: 4577
madolson added a commit that referenced this pull request Aug 31, 2026
`tlsGetPeerUser()` derived the ACL username from the certificate CN with
`strlen()`, so an embedded NUL truncated the identity before the lookup.
A certificate whose CA vouched subject is `CN=admin\0attacker`
authenticated as the ACL user `admin` and got its privileges. The
sibling SAN URI path already passed an explicit length to
`ACLGetUserByName()`. The CN path did not.

No explicit NUL check is needed. `ACL SETUSER` refuses a username
containing one, so comparing the full length is sufficient. Dropping the
buffer also removes the silent truncation of a CN longer than 255 bytes.

The CN is handed back whether or not it matches, so a failed
authentication now produces an `ACL LOG` entry and bumps
`acl_access_denied_tls_cert` instead of leaving no trace at the default
loglevel.

Affects 9.0 and 9.1, CN path only. The feature arrived in #1920.

Not fixed here: `X509_NAME_get_index_by_NID()` takes the first CN and
ignores the rest, so `CN=admin, CN=attacker` authenticates as `admin`.
Refusing that would break certificates that authenticate today and
contradicts `valkey.conf:275-277`, which documents first match wins for
URI mode. Its own PR.

The openssl CLI strips a NUL from a name, which is why #3078 could not
cover this when it added the URI path, so the fixture is issued with a
placeholder byte and re-signed. The new test fails on unstable and
passes with the fix. `unit/tls` and `unit/acl` pass under `--tls` and
`--tls-module`, and `unit/tls` is clean under `SANITIZER=address`.

Signed-off-by: Madelyn Olson <[email protected]>
Backport-Source-PR: 4577
valkeyrie-ops Bot pushed a commit that referenced this pull request Aug 31, 2026
`tlsGetPeerUser()` derived the ACL username from the certificate CN with
`strlen()`, so an embedded NUL truncated the identity before the lookup.
A certificate whose CA vouched subject is `CN=admin\0attacker`
authenticated as the ACL user `admin` and got its privileges. The
sibling SAN URI path already passed an explicit length to
`ACLGetUserByName()`. The CN path did not.

No explicit NUL check is needed. `ACL SETUSER` refuses a username
containing one, so comparing the full length is sufficient. Dropping the
buffer also removes the silent truncation of a CN longer than 255 bytes.

The CN is handed back whether or not it matches, so a failed
authentication now produces an `ACL LOG` entry and bumps
`acl_access_denied_tls_cert` instead of leaving no trace at the default
loglevel.

Affects 9.0 and 9.1, CN path only. The feature arrived in #1920.

Not fixed here: `X509_NAME_get_index_by_NID()` takes the first CN and
ignores the rest, so `CN=admin, CN=attacker` authenticates as `admin`.
Refusing that would break certificates that authenticate today and
contradicts `valkey.conf:275-277`, which documents first match wins for
URI mode. Its own PR.

The openssl CLI strips a NUL from a name, which is why #3078 could not
cover this when it added the URI path, so the fixture is issued with a
placeholder byte and re-signed. The new test fails on unstable and
passes with the fix. `unit/tls` and `unit/acl` pass under `--tls` and
`--tls-module`, and `unit/tls` is clean under `SANITIZER=address`.

Signed-off-by: Madelyn Olson <[email protected]>
Backport-Source-PR: 4577
madolson added a commit that referenced this pull request Sep 1, 2026
`tlsGetPeerUser()` derived the ACL username from the certificate CN with
`strlen()`, so an embedded NUL truncated the identity before the lookup.
A certificate whose CA vouched subject is `CN=admin\0attacker`
authenticated as the ACL user `admin` and got its privileges. The
sibling SAN URI path already passed an explicit length to
`ACLGetUserByName()`. The CN path did not.

No explicit NUL check is needed. `ACL SETUSER` refuses a username
containing one, so comparing the full length is sufficient. Dropping the
buffer also removes the silent truncation of a CN longer than 255 bytes.

The CN is handed back whether or not it matches, so a failed
authentication now produces an `ACL LOG` entry and bumps
`acl_access_denied_tls_cert` instead of leaving no trace at the default
loglevel.

Affects 9.0 and 9.1, CN path only. The feature arrived in #1920.

Not fixed here: `X509_NAME_get_index_by_NID()` takes the first CN and
ignores the rest, so `CN=admin, CN=attacker` authenticates as `admin`.
Refusing that would break certificates that authenticate today and
contradicts `valkey.conf:275-277`, which documents first match wins for
URI mode. Its own PR.

The openssl CLI strips a NUL from a name, which is why #3078 could not
cover this when it added the URI path, so the fixture is issued with a
placeholder byte and re-signed. The new test fails on unstable and
passes with the fix. `unit/tls` and `unit/acl` pass under `--tls` and
`--tls-module`, and `unit/tls` is clean under `SANITIZER=address`.

Signed-off-by: Madelyn Olson <[email protected]>
Backport-Source-PR: 4577
hanzo-dev pushed a commit to hanzoai/redis that referenced this pull request Sep 11, 2026
This PR implements support for automatic client authentication based on
a field in the client's TLS certificate.
We adopt ValKey’s PR: valkey-io/valkey#1920

API Changes:

Add New configuration tls-auth-clients-user  
  -  Allowed values: `off` (default), `CN`.
  - `off` – disable TLS certificate–based auto-authentication.
- `CN` – derive the ACL username from the Common Name (CN) field of the
client certificate.
 
New INFO stat
  - `acl_access_denied_tls_cert`
- Counts failed TLS certificate–based authentication attempts, i.e. TLS
connections where a client certificate was presented, a username was
derived from it, but no matching ACL user was found.

New ACL LOG reason
  - Reason string: `"tls-cert"`
- Emitted when a client certificate’s Common Name fails to match any
existing ACL user.


Implementation Details:

- Added getCertFieldByName() utility to extract fields from peer
certificates.

- Added autoAuthenticateClientFromCert() to handle automatic login logic
post-handshake.

- Integrated automatic authentication into the TLSAccept function after
handshake completion.

- Updated test suite (tests/integration/tls.tcl) to validate the
feature.
zuiderkwast pushed a commit to tartelettie/valkey that referenced this pull request Sep 15, 2026
`tlsGetPeerUser()` derived the ACL username from the certificate CN with
`strlen()`, so an embedded NUL truncated the identity before the lookup.
A certificate whose CA vouched subject is `CN=admin\0attacker`
authenticated as the ACL user `admin` and got its privileges. The
sibling SAN URI path already passed an explicit length to
`ACLGetUserByName()`. The CN path did not.

No explicit NUL check is needed. `ACL SETUSER` refuses a username
containing one, so comparing the full length is sufficient. Dropping the
buffer also removes the silent truncation of a CN longer than 255 bytes.

The CN is handed back whether or not it matches, so a failed
authentication now produces an `ACL LOG` entry and bumps
`acl_access_denied_tls_cert` instead of leaving no trace at the default
loglevel.

Affects 9.0 and 9.1, CN path only. The feature arrived in valkey-io#1920.

Not fixed here: `X509_NAME_get_index_by_NID()` takes the first CN and
ignores the rest, so `CN=admin, CN=attacker` authenticates as `admin`.
Refusing that would break certificates that authenticate today and
contradicts `valkey.conf:275-277`, which documents first match wins for
URI mode. Its own PR.

The openssl CLI strips a NUL from a name, which is why valkey-io#3078 could not
cover this when it added the URI path, so the fixture is issued with a
placeholder byte and re-signed. The new test fails on unstable and
passes with the fix. `unit/tls` and `unit/acl` pass under `--tls` and
`--tls-module`, and `unit/tls` is clean under `SANITIZER=address`.

Signed-off-by: Madelyn Olson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

major-decision-approved Major decision approved by TSC team needs-doc-pr This change needs to update a documentation page. Remove label once doc PR is open. release-notes This issue should get a line item in the release notes

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[NEW] Take username from client certificate

5 participants