Skip to content

Commit

Permalink
webhook: load TLS certificate at runtime (#608)
Browse files Browse the repository at this point in the history
This avoid the need of restarting the webhook when the certificate is rotated

Signed-off-by: Francesco Ilario <filario@redhat.com>
  • Loading branch information
filariow authored Nov 25, 2024
1 parent ca053bf commit 6e75b2a
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -120,14 +121,22 @@ func main() {
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
NextProtos: []string{"http/1.1"}, // disable HTTP/2 for now

GetCertificate: func(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
cert, err := tls.LoadX509KeyPair("/etc/webhook/certs/"+cert.ServerCert, "/etc/webhook/certs/"+cert.ServerKey)
if err != nil {
return nil, fmt.Errorf("could not load TLS certs: %w", err)
}
return &cert, err
},
},
}

setupLog.Info("Webhook server configured.")

go func() {
setupLog.Info("Listening...")
if err := webhookServer.ListenAndServeTLS("/etc/webhook/certs/"+cert.ServerCert, "/etc/webhook/certs/"+cert.ServerKey); err != nil {
if err := webhookServer.ListenAndServeTLS("", ""); err != nil {
setupLog.Error(err, "Listening and serving TLS failed")
os.Exit(1)
}
Expand Down

0 comments on commit 6e75b2a

Please sign in to comment.