Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix webhook certificates not being reloaded on change #470

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions cmd/marble-injector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
package main

import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"

Expand Down Expand Up @@ -38,11 +40,28 @@ func main() {
mux.HandleFunc("/mutate", w.HandleMutate)

s := &http.Server{
// Addresse forwarding to 443 should be handled by the marble-injector service object
// Address forwarding to 443 should be handled by the marble-injector service object
Addr: ":8443",
Handler: mux,
TLSConfig: &tls.Config{
GetCertificate: loadWebhookCert(certFile, keyFile),
},
}

log.Println("Starting Server")
log.Fatal(s.ListenAndServeTLS(certFile, keyFile))
log.Fatal(s.ListenAndServeTLS("", ""))
}

// loadWebhookCert loads the certificate and key file for the webhook server.
// We need to use this function since the certificate may be updated by cert-manager,
// requiring us to reload the certificate.
func loadWebhookCert(certFile, keyFile string) func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) {
return func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) {
pair, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, fmt.Errorf("failed loading tls key pair: %w", err)
}

return &pair, nil
}
}