Skip to content

Commit 1629eda

Browse files
committed
Implement hot-reload for TLS client certificate
Signed-off-by: Tero Saarni <[email protected]>
1 parent c4c7d2b commit 1629eda

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

tls.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ func createTLSConfig(pemFile, pemCertFile, pemPrivateKeyFile string, insecureSki
2222
tlsConfig.RootCAs = rootCerts
2323
}
2424
if len(pemCertFile) > 0 && len(pemPrivateKeyFile) > 0 {
25-
clientPrivateKey, err := loadPrivateKeyFrom(pemCertFile, pemPrivateKeyFile)
25+
// Load files once to catch configuration error early.
26+
_, err := loadPrivateKeyFrom(pemCertFile, pemPrivateKeyFile)
2627
if err != nil {
2728
log.Fatalf("Couldn't setup client authentication. Got %s.", err)
2829
return nil
2930
}
30-
tlsConfig.Certificates = []tls.Certificate{*clientPrivateKey}
31+
// Define a function to load certificate and key lazily at TLS handshake to
32+
// ensure that the latest files are used in case they have been rotated.
33+
tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
34+
return loadPrivateKeyFrom(pemCertFile, pemPrivateKeyFile)
35+
}
3136
}
3237
return &tlsConfig
3338
}

0 commit comments

Comments
 (0)