Repository navigation
Add support for automatic client authentication via TLS certificate fields - #1920
Conversation
Codecov ReportAttention: Patch coverage is
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
🚀 New features to boost your workflow:
|
zuiderkwast
left a comment
There was a problem hiding this comment.
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:
-
As of version v3.2 if an etcd server is launched with the option --client-cert-auth=true, the field of Common Name (CN) in the client’s TLS cert will be used as an etcd user.
-
If a client certificate is presented and verified, the common name of the subject is used as the user name for the request. As of Kubernetes 1.4, client certificates can also indicate a user's group memberships using the certificate's organization fields.
6574657 to
e2fd2c4
Compare
|
@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, Please 👍 if you are okay with the recommendation of just a simple yes/no bool. Otherwise, please comment. |
|
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? |
|
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. |
@madolson I thought you were advocating for using |
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. |
|
@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. |
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]>
|
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 |
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.
|
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 |
`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]>
`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
`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
`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
`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
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.
`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]>
This PR implements support for automatic client authentication based on a field in the client's TLS certificate.
API Changes:
tls-auth-clients-user, valuesCN|off, defaultoff. CN means take username from the CommonName field in the client's certificate.acl_access_denied_tls_certunder theStatssection, indicating the number of failed authentications using this feature, i.e. client certificates for which no matching username was found.🔒 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:
This will authenticate the client as the user matching the certificate's Common Name.
Closes #1866