Repository navigation
Add TLS certificate-based automatic client authentication - #14610
Conversation
ShooterIT
left a comment
There was a problem hiding this comment.
interesting feature.
if some code is copied from valkey, we may add something at the begin of file, like https://github.com/redis/redis/blob/unstable/src/networking.c#L5
besides, we should add the authors of valkey commit to co-authors, right @sundb
| sds username = connGetPeerUsername(conn); | ||
| if (username != NULL) { | ||
| user *u = ACLGetUserByName(username, sdslen(username)); | ||
| if (u) { |
There was a problem hiding this comment.
now yes, thanks , the validation is necessary to ensure that a disabled user cannot authenticate. I’ve added the relevant changes as well as the tests
| assert {$after == $before + 1} | ||
| } | ||
|
|
||
| test {TLS: Disabled user cannot auto-authenticate via certificate} { |
There was a problem hiding this comment.
if you write this test, maybe you can merge above two tests into one, since there are many duplicated codes,
foreach user_type {"non-existent" "disable"} {
test {TLS: $user_type user cannot auto-authenticate via certificate} {
}
}
shahsb
left a comment
There was a problem hiding this comment.
Thanks @StavRLevi for your efforts on this
|
|
||
| configEnum tls_client_auth_user_enum[] = { | ||
| {"CN", TLS_CLIENT_FIELD_CN}, | ||
| {"off", TLS_CLIENT_FIELD_OFF}, |
There was a problem hiding this comment.
Ensure the new option is clearly documented in redis.conf with examples for CA trust stores and cert validation modes (e.g., require exact match vs. wildcard subjects).
There was a problem hiding this comment.
agree, i can add examples
| {"CN", TLS_CLIENT_FIELD_CN}, | ||
| {"off", TLS_CLIENT_FIELD_OFF}, | ||
| {NULL, 0} | ||
| }; |
There was a problem hiding this comment.
Suggestion: Add a tls-client-cert-require-subject or similar to enforce specific cert attributes. If not already, consider integrating with Redis's user/group system for role mapping based on cert CN.
There was a problem hiding this comment.
I think it might be a good idea to follow up.. not for this PR. Current implementation covers the most common use case (CN mapping). Adding O, OU, EMAIL fields or wildcard matching could be a follow up enhancement..
| # INFO stats should report that acl_access_denied_tls_cert increased by 1 | ||
| set info_after [r INFO stats] | ||
| regexp {acl_access_denied_tls_cert:(\d+)} $info_after -> after | ||
| assert {$after == $before + 1} |
There was a problem hiding this comment.
Add unit/integration tests for:
- Successful auth with valid cert.
- Rejection with invalid/expired/missing cert.
- Fallback to password auth if cert fails.
- Cluster mode compatibility (node-to-node cert auth?).
There was a problem hiding this comment.
Hi, Successful auth with valid cert - is covered by Auto-authenticate using tls-auth-clients-user test.
Rejection with invalid/expired/missing cert - invalid/expired cert tests need certificate generation infrastructure (TLS-layer failures). Current coverage is solid for my opinion.
and for Fallback to password auth if cert fails. I added a test.
| {NULL, 0} | ||
| }; | ||
|
|
||
| configEnum tls_client_auth_user_enum[] = { |
There was a problem hiding this comment.
Performance: Benchmark handshake latency with/without this feature under load (e.g., via redis-benchmark with --tls).
There was a problem hiding this comment.
thanks, i think that the impact should be minimal (one cert parse per connection, not per command), but i can benchmark to confirm.
| } else if (reason == ACL_DENIED_CHANNEL) { | ||
| server.acl_info.invalid_channel_accesses++; | ||
| } else if (reason == ACL_INVALID_TLS_CERT_AUTH) { | ||
| server.acl_info.acl_access_denied_tls_cert++; |
There was a problem hiding this comment.
Compatibility: Confirm no breakage for Redis clients without client certs (should still allow connections if not required).
There was a problem hiding this comment.
we can see that: sds username = connGetPeerUsername(conn);
if (username != NULL) {
// auth logic
}
// If no cert or no CN, client stays as "default" user.. so already handled.. If no cert/CN, client remains as default user. Could add explicit test maybe
|
|
||
| if (nid == -1) return 0; | ||
|
|
||
| X509_NAME *subject = X509_get_subject_name(cert); |
There was a problem hiding this comment.
Security Review: Audit for cert parsing vulns (e.g., buffer overflows in subject extraction). Consider fuzzing with libFuzzer.
There was a problem hiding this comment.
We use standard OpenSSL APIs which are well tested... will try fuzzing as a hardening step
ShooterIT
left a comment
There was a problem hiding this comment.
LGTM, please trigger a daily CI run and make sure the TLS tests are green. I did not test it locally :)
| # Supported values: CN, off. Default: off. | ||
| # | ||
| # Matches certificate CN to Redis username (exact match only). | ||
| # Example: Cert CN=myapp → authenticates as user "myapp" |
There was a problem hiding this comment.
| # Example: Cert CN=myapp → authenticates as user "myapp" | |
| # Example: Cert CN=myapp -> authenticates as user "myapp" |
let's use ASCII characters only
2c5566c to
4b5f51e
Compare
|
is this scenario handled here? |
|
Hi @sandeepkunusoth |
If I want password auth for some users and TLS client-cert (CN) auth for others, there’s no clean way today to prevent a CN user from logging in without a client cert when tls-auth-clients optional, because a CN user needs nopass to be cert-only — but nopass also enables AUTH with no cert. If I remove nopass from the CN user, then the client must provide both: a client certificate, and AUTH Is that the intended/expected behavior? Or is there a supported way to have cert-only users and password-only users safely in the same deployment without this gap? This below scenario happens even with tls-auth-clients yes |
|
@sandeepkunusoth |
Hi, I understand that nopass is not required for certificate-based login. What I was trying to convey is that a user should authenticate either via password or via certificate (using the certificate CN), but not both at the same time. Password-based login would apply to non-certificate users. Certificate-based login would apply to certificate users, where authentication is done using the certificate CN and no password is required. In this current implementation, a non-certificate user could still connect via CN username by passing any valid mTLS certificate, even if the certificate CN does not match the username. |
|
@sandeepkunusoth I believe that the key point is how certificate auto-auth and AUTH are separated. I think that the main thing is that for this current implementation, certificate is not representing identity.. so if we do want it we should be considered as a future enhancement ( maybe by a cert-only flag) rather than a bug in the current implementation. When tls-auth-clients-user=CN is enabled, the server derives the username strictly from the certificate CN during the TLS handshake. A client with a certificate CN=bob cannot authenticate as admin via the certificate flow — CN mismatches are not accepted. So the behavior where a user can authenticate without a certificate is due to nopass, not the TLS CN feature. @ShooterIT WDYT? |
|
i think they are different things. |
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
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_certNew ACL LOG reason
"tls-cert"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.