TLS Support
Getting Started
Building
To build with TLS support you'll need OpenSSL development libraries (e.g. libssl-dev on Debian/Ubuntu).
To build TLS support as Redis built-in:
Run make BUILD_TLS=yes.
Or to build TLS as Redis module:
Run make BUILD_TLS=module.
Note that sentinel mode does not support TLS module.
Tests
To run Redis test suite with TLS, you'll need TLS support for TCL (i.e.
tcl-tls package on Debian/Ubuntu).
Run
./utils/gen-test-certs.shto generate a root CA and a server certificate.Run
./runtest --tlsor./runtest-cluster --tlsto run Redis and Redis Cluster tests in TLS mode.Run
./runtest --tls-moduleor./runtest-cluster --tls-moduleto run Redis and Redis cluster tests in TLS mode with Redis module.
Running manually
To manually run a Redis server with TLS mode (assuming gen-test-certs.sh was
invoked so sample certificates/keys are available):
For TLS built-in mode: ./src/redis-server --tls-port 6379 --port 0 \ --tls-cert-file ./tests/tls/redis.crt \ --tls-key-file ./tests/tls/redis.key \ --tls-ca-cert-file ./tests/tls/ca.crt
For TLS module mode: ./src/redis-server --tls-port 6379 --port 0 \ --tls-cert-file ./tests/tls/redis.crt \ --tls-key-file ./tests/tls/redis.key \ --tls-ca-cert-file ./tests/tls/ca.crt \ --loadmodule src/redis-tls.so
To connect to this Redis server with redis-cli:
./src/redis-cli --tls \
--cert ./tests/tls/redis.crt \
--key ./tests/tls/redis.key \
--cacert ./tests/tls/ca.crt
This will disable TCP and enable TLS on port 6379. It's also possible to have both TCP and TLS available, but you'll need to assign different ports.
Note why that redis-cli invocation passes a certificate of its own: by default
clients on a TLS port must present one that verifies against the configured CA,
replicas connecting to a master included. tls-auth-clients no accepts no client
certificate at all, and tls-auth-clients optional validates one only when it is
offered. Both relax the client port alone — see "Cluster bus protection" below for
why neither can relax the cluster bus. Setting tls-auth-clients-user CN
additionally maps a validated certificate's Common Name to a Redis ACL user, so
the certificate authenticates the client without an AUTH command.
To make a Replica connect to the master using TLS, use --tls-replication yes,
and to make Redis Cluster use TLS across nodes use --tls-cluster yes. Note that
a cluster node started without tls-cluster warns that its bus is
unauthenticated; see "Cluster bus protection" below.
Cluster bus protection
The cluster bus protocol has no authentication of its own: any host able to reach
a node's cluster bus port can speak the protocol — a MEET from an unknown host
is enough to have it added as a node, with its gossip trusted — and so threaten
the whole cluster. What authenticates the bus is tls-cluster, which makes every
peer present a certificate that verifies against the configured CA — in both the
accepting and the connecting direction, and tls-cluster also makes configuring
a CA mandatory. This does not depend on tls-auth-clients, which governs the
client port only.
For that reason a node started with cluster-enabled yes but no tls-cluster
records in its log that its cluster bus is unauthenticated. Setting
cluster-bus-port-protected-mode to yes — it defaults to no — turns that
into a refusal to start, so that keeping an unauthenticated cluster bus becomes an
explicit choice, acknowledging that the port is reachable by trusted hosts only —
because a firewall, a VPN or a private network keeps untrusted hosts away from it.
The option has no effect outside cluster mode, as only a cluster node opens a
cluster bus port.
Peer certificate verification
By default, Redis TLS validates only that a peer certificate chains to the
trusted CA (tls-ca-cert-file / tls-ca-cert-dir) and is not expired. It does
not verify that the certificate belongs to the specific peer being contacted.
Trust model and the CA assumption
This default is safe only under a dedicated, per-cluster CA: since that CA signs certificates exclusively for your own nodes, "chains to the CA" is equivalent to "is a legitimate peer". This is the recommended Redis TLS deployment model.
If you instead use a shared, organizational, or public CA, that CA also signs certificates for machines that are not part of your deployment. In that case CA validation alone is insufficient:
- A machine-in-the-middle holding any certificate signed by the same CA can
impersonate a master/peer on outbound connections (replication, cluster bus,
MIGRATE) and, for replication, capture theAUTH <masteruser> <masterauth>credentials a replica sends. - Because the cluster bus has no per-message authentication (a message's sender
is identified only by the public node ID in its header), such a certificate can
also open an inbound cluster-bus connection and forge messages that impersonate
a real member — for example a
FAILmessage that marks a healthy node as failed.
tls-expected-peer-name
To close this gap under a shared CA, set tls-expected-peer-name. When set, the
peer certificate must additionally carry one of the configured names in its
Subject Alternative Name (SAN) field (CN is used only as a fallback), verified as
part of CA chain validation. Issue every node's certificate with a shared
cluster-identity SAN (e.g. node.my-redis-cluster.example.com) and set this
option to that name on every node; a certificate signed by the same CA but
lacking the name is then rejected.
The expected name is taken from local configuration only — never from the address dialed or from any data received over the wire (such as a gossip-announced hostname), since the whole purpose is to distinguish a real peer from an impostor.
The check is enforced in both directions of server-to-server TLS:
- Outbound — the peer's server certificate is verified when this node dials
a master, cluster peer, or
MIGRATEtarget. - Inbound cluster bus — the connecting peer's client certificate is verified on accept, which is what blocks the impersonation/forgery described above.
Because a node's outbound (client) certificate is now verified by its peers, the
identity SAN must be present on whichever certificate the node presents in both
roles. In practice this means the SAN must be on tls-client-cert-file (used
for outbound cluster/replication connections) as well as on tls-cert-file; when
no separate client certificate is configured, tls-cert-file is used for both
roles and only needs the SAN once. If a separate client certificate without the
SAN is configured, peers will reject this node's inbound cluster-bus connections
and the cluster will not form.
Notes:
- Multiple names may be supplied as a single space-separated value (quote it in
the config file, e.g.
tls-expected-peer-name "a.example.com b.example.com"); a match against any one of them succeeds. - Full-label wildcards (
*.example.com) match; partial-label wildcards (f*.example.com) do not. - The option is opt-in and defaults to unset (no peer-name check), preserving the previous behavior.
- It does not apply to ordinary client connections on the data port, whose
identity and authorization are handled by
AUTH/ACL (and optionallytls-auth-clients-user). - Peer-name verification uses the OpenSSL
X509_VERIFY_PARAMhost API, available since OpenSSL 1.0.2. Building the TLS support against an older OpenSSL fails with an error by default. DefineTLS_NO_PEER_NAME_VERIFICATION(e.g.make BUILD_TLS=yes CFLAGS=-DTLS_NO_PEER_NAME_VERIFICATION) to compile the feature out — either to build against an older OpenSSL, or to disable it on any OpenSSL version. When compiled out, iftls-expected-peer-nameis set each affected connection logs a warning and proceeds without the name check (CA chain validation still applies).
Connections
All socket operations now go through a connection abstraction layer that hides I/O and read/write event handling from the caller.
Multi-threading I/O is not currently supported for TLS, as a TLS connection needs to do its own manipulation of AE events which is not thread safe. The solution is probably to manage independent AE loops for I/O threads and longer term association of connections with threads. This may potentially improve overall performance as well.
Sync IO for TLS is currently implemented in a hackish way, i.e. making the socket blocking and configuring socket-level timeout. This means the timeout value may not be so accurate, and there would be a lot of syscall overhead. However I believe that getting rid of syncio completely in favor of pure async work is probably a better move than trying to fix that. For replication it would probably not be so hard. For cluster keys migration it might be more difficult, but there are probably other good reasons to improve that part anyway.
To-Do List
- [ ] redis-benchmark support. The current implementation is a mix of using hiredis for parsing and basic networking (establishing connections), but directly manipulating sockets for most actions. This will need to be cleaned up for proper TLS support. The best approach is probably to migrate to hiredis async mode.
- [ ] redis-cli
--slaveand--rdbsupport.
Multi-port
Consider the implications of allowing TLS to be configured on a separate port, making Redis listening on multiple ports:
- Startup banner port notification
- Proctitle
- How slaves announce themselves
- Cluster bus port calculation
