Skip to content

Commit 94cdb49

Browse files
author
Satheesha Chattenahalli Hanume Gowda
committed
Automatic TLS Certificate Reload
Signed-off-by: Satheesha Chattenahalli Hanume Gowda <satheesha@apple.com>
1 parent ffdf222 commit 94cdb49

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/config.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,6 +3399,7 @@ standardConfig static_configs[] = {
33993399

34003400
/* Tls configs */
34013401
createIntConfig("tls-port", NULL, MODIFIABLE_CONFIG, 0, 65535, server.tls_port, 0, INTEGER_CONFIG, NULL, applyTLSPort), /* TCP port. */
3402+
createIntConfig("tls-cert-reload-interval_mins", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.tls_ctx_config.cert_reload_interval_mins, 0, INTEGER_CONFIG, NULL, applyTlsCfg),
34023403
createIntConfig("tls-session-cache-size", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.tls_ctx_config.session_cache_size, 20 * 1024, INTEGER_CONFIG, NULL, applyTlsCfg),
34033404
createIntConfig("tls-session-cache-timeout", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, server.tls_ctx_config.session_cache_timeout, 300, INTEGER_CONFIG, NULL, applyTlsCfg),
34043405
createBoolConfig("tls-cluster", NULL, MODIFIABLE_CONFIG, server.tls_cluster, 0, NULL, applyTlsCfg),

src/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,7 @@ typedef struct serverTLSContextConfig {
15871587
int session_caching;
15881588
int session_cache_size;
15891589
int session_cache_timeout;
1590+
int cert_reload_interval_mins;
15901591
} serverTLSContextConfig;
15911592

15921593
/*-----------------------------------------------------------------------------

src/tls.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ static SSL_CTX *createSSLContext(serverTLSContextConfig *ctx_config, int protoco
284284
return NULL;
285285
}
286286

287+
static long long lastTlsConfigureTime = 0LL;
288+
287289
/* Attempt to configure/reconfigure TLS. This operation is atomic and will
288290
* leave the SSL_CTX unchanged if fails.
289291
* @priv: config of serverTLSContextConfig.
@@ -296,6 +298,9 @@ static int tlsConfigure(void *priv, int reconfigure) {
296298
SSL_CTX *ctx = NULL;
297299
SSL_CTX *client_ctx = NULL;
298300

301+
serverLog(LL_DEBUG, "Configuring TLS");
302+
lastTlsConfigureTime = server.ustime;
303+
299304
if (!reconfigure && valkey_tls_ctx) {
300305
return C_OK;
301306
}
@@ -419,6 +424,18 @@ static int tlsConfigure(void *priv, int reconfigure) {
419424
return C_ERR;
420425
}
421426

427+
static void tlsReconfigureIfNeeded(void) {
428+
if (server.tls_ctx_config.cert_reload_interval_mins > 0) {
429+
const long long configAgeMicros = server.ustime - lastTlsConfigureTime;
430+
const long long configAgeMinutes = ((configAgeMicros / 1000) / 1000) / 60;
431+
if (configAgeMinutes > server.tls_ctx_config.cert_reload_interval_mins) {
432+
if (tlsConfigure(&server.tls_ctx_config, 1) == C_ERR) {
433+
serverLog(LL_WARNING, "Unable to update TLS configuration. Check server logs.");
434+
}
435+
}
436+
}
437+
}
438+
422439
static ConnectionType CT_TLS;
423440

424441
/* Normal socket connections have a simple events/handler correlation.
@@ -465,6 +482,8 @@ static void updateTLSError(tls_connection *conn) {
465482
}
466483

467484
static connection *createTLSConnection(int client_side) {
485+
// Reload the cert if needed for new connections
486+
tlsReconfigureIfNeeded();
468487
SSL_CTX *ctx = valkey_tls_ctx;
469488
if (client_side && valkey_tls_client_ctx) ctx = valkey_tls_client_ctx;
470489
tls_connection *conn = zcalloc(sizeof(tls_connection));

valkey.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ tcp-keepalive 300
324324
#
325325
# tls-session-cache-timeout 60
326326

327+
# Specifies a periodic interval to check and hot-reload updated TLS certificate files from disk.
328+
# This is useful when certificates are renewed while the server is running.
329+
# A value of 0 (default) disables periodic cert reloading.
330+
#
331+
# tls-cert-reload-interval_mins 1440
332+
327333
################################### RDMA ######################################
328334

329335
# Valkey Over RDMA is experimental, it may be changed or be removed in any minor or major version.

0 commit comments

Comments
 (0)