44 "context"
55 "crypto/tls"
66 "crypto/x509"
7- "fmt"
87 "log"
98 "net/http"
109 "net/http/pprof"
@@ -171,19 +170,28 @@ func main() {
171170
172171 app .authService = services .NewAuthService (app .db , ctx , jwtSecret )
173172 app .githubService = services .NewGithubService (cnf , webhookTLS )
173+ if gs , ok := app .githubService .(interface { SetCertManager (* certmanager.Manager ) }); ok {
174+ gs .SetCertManager (vaultCertManager )
175+ }
174176 app .clusterControl = services .NewClusterControlService (cnf )
177+ app .clusterControl .SetCertManager (vaultCertManager )
175178 app .clusterControl .Start (ctx )
176179 app .forgeryService = services .NewForgeryService (cnf , webhookTLS )
180+ app .forgeryService .SetCertManager (vaultCertManager )
177181 app .webhookService , err = services .NewWebhookService (cnf , webhookTLS , app .db )
178182 if err != nil {
179183 log .Fatalf ("failed to initialize webhook service: %v" , err )
180184 }
185+ if ws , ok := app .webhookService .(interface { SetCertManager (* certmanager.Manager ) }); ok {
186+ ws .SetCertManager (vaultCertManager )
187+ }
181188 app .webhookService .Start (ctx )
182189
183190 app .automationService , err = services .NewAutomationService (cnf , webhookTLS )
184191 if err != nil {
185192 log .Fatalf ("failed to initialize automation service: %v" , err )
186193 }
194+ app .automationService .SetCertManager (vaultCertManager )
187195
188196 app .authController = controllers .NewAuthController (
189197 app .authService , ctx , app .githubService , app .db ,
@@ -207,12 +215,12 @@ func main() {
207215 mtlsRouter := gin .New ()
208216 nonMTLSRouter := gin .New ()
209217
210- mtlsRouter .Use (gin . Logger ())
218+ mtlsRouter .Use (middleware . AccessLogger ())
211219 mtlsRouter .Use (cors .New (corsConfig ))
212220 mtlsRouter .Use (gootelgin .Middleware ("persys-gateway-mtls" ))
213221 mtlsRouter .Use (middleware .ServiceIdentityHeader ("persys-gateway" ))
214222
215- nonMTLSRouter .Use (gin . Logger ())
223+ nonMTLSRouter .Use (middleware . AccessLogger ())
216224 nonMTLSRouter .Use (cors .New (corsConfig ))
217225 nonMTLSRouter .Use (gootelgin .Middleware ("persys-gateway-public" ))
218226 nonMTLSRouter .Use (middleware .ServiceIdentityHeader ("persys-gateway" ))
@@ -318,7 +326,6 @@ func main() {
318326 automationGroup .Use (gwRouter .Resolve (catalog .AuthUser ))
319327 automationRouteController := routes .NewAutomationRouteController (app .automationController )
320328 automationRouteController .AutomationRoute (automationGroup )
321-
322329 // Webhook stays unauthenticated at the gateway level by design — its
323330 // own HMAC signature verification (X-Hub-Signature-256) IS its auth
324331 // mechanism, checked inside webhook.service.go. Mounted on the
@@ -359,14 +366,27 @@ func main() {
359366 mtlsServer := & http.Server {Addr : cnf .App .HTTPAddr , Handler : mtlsRouter , TLSConfig : tlsConfig }
360367 nonMTLSServer := & http.Server {Addr : cnf .App .HTTPAddrPublic , Handler : nonMTLSRouter }
361368
369+ // goroutine, heap, allocs, block, mutex, threadcreate are served via pprof.Index
370+ // through /debug/pprof/{profile-name} automatically once Index is registered
362371 debugMux := http .NewServeMux ()
363372 debugMux .HandleFunc ("/debug/pprof/" , pprof .Index )
364373 debugMux .HandleFunc ("/debug/pprof/cmdline" , pprof .Cmdline )
365374 debugMux .HandleFunc ("/debug/pprof/profile" , pprof .Profile )
366375 debugMux .HandleFunc ("/debug/pprof/symbol" , pprof .Symbol )
376+
367377 debugMux .HandleFunc ("/debug/pprof/trace" , pprof .Trace )
368- // goroutine, heap, allocs, block, mutex, threadcreate are served via pprof.Index
369- // through /debug/pprof/{profile-name} automatically once Index is registered
378+ // debugMux.HandleFunc("/debug/force-rotate", func(w http.ResponseWriter, r *http.Request) {
379+ // if r.Method != http.MethodPost {
380+ // http.Error(w, "POST only", http.StatusMethodNotAllowed)
381+ // return
382+ // }
383+ // if err := vaultCertManager.ForceRotate(r.Context()); err != nil {
384+ // http.Error(w, err.Error(), http.StatusInternalServerError)
385+ // return
386+ // }
387+ // w.Header().Set("Content-Type", "application/json")
388+ // _, _ = w.Write([]byte(`{"status":"rotated"}`))
389+ // })
370390
371391 debugServer := & http.Server {
372392 Addr : "0.0.0.0:6060" ,
@@ -414,17 +434,6 @@ func main() {
414434}
415435
416436func buildMTLSClientConfig (cnf * config.Config ) (* tls.Config , error ) {
417- cert , err := tls .LoadX509KeyPair (cnf .TLS .CertPath , cnf .TLS .KeyPath )
418- if err != nil {
419- return nil , err
420- }
421- caCert , err := os .ReadFile (cnf .TLS .CAPath )
422- if err != nil {
423- return nil , err
424- }
425- caPool := x509 .NewCertPool ()
426- if ! caPool .AppendCertsFromPEM (caCert ) {
427- return nil , fmt .Errorf ("invalid CA bundle" )
428- }
429- return & tls.Config {Certificates : []tls.Certificate {cert }, RootCAs : caPool }, nil
437+ // Live config: GetClientCertificate reloads from disk after certmanager rotates.
438+ return services .LiveClientTLSConfig (cnf .TLS .CertPath , cnf .TLS .KeyPath , cnf .TLS .CAPath )
430439}
0 commit comments