@@ -60,9 +60,13 @@ type Server struct {
60
60
CertDir string
61
61
62
62
// CertName is the server certificate name. Defaults to tls.crt.
63
+ //
64
+ // Note: This option should only be set when TLSOpts does not override GetCertificate.
63
65
CertName string
64
66
65
67
// KeyName is the server key name. Defaults to tls.key.
68
+ //
69
+ // Note: This option should only be set when TLSOpts does not override GetCertificate.
66
70
KeyName string
67
71
68
72
// ClientCAName is the CA certificate name which server used to verify remote(client)'s certificate.
@@ -169,32 +173,40 @@ func (s *Server) Start(ctx context.Context) error {
169
173
baseHookLog := log .WithName ("webhooks" )
170
174
baseHookLog .Info ("Starting webhook server" )
171
175
172
- certPath := filepath .Join (s .CertDir , s .CertName )
173
- keyPath := filepath .Join (s .CertDir , s .KeyName )
174
-
175
- certWatcher , err := certwatcher .New (certPath , keyPath )
176
- if err != nil {
177
- return err
178
- }
179
-
180
- go func () {
181
- if err := certWatcher .Start (ctx ); err != nil {
182
- log .Error (err , "certificate watcher error" )
183
- }
184
- }()
185
-
186
176
tlsMinVersion , err := tlsVersion (s .TLSMinVersion )
187
177
if err != nil {
188
178
return err
189
179
}
190
180
191
181
cfg := & tls.Config { //nolint:gosec
192
- NextProtos : []string {"h2" },
193
- GetCertificate : certWatcher .GetCertificate ,
194
- MinVersion : tlsMinVersion ,
182
+ NextProtos : []string {"h2" },
183
+ MinVersion : tlsMinVersion ,
184
+ }
185
+ // fallback TLS config ready, will now mutate if passer wants full control over it
186
+ for _ , op := range s .TLSOpts {
187
+ op (cfg )
188
+ }
189
+
190
+ if cfg .GetCertificate == nil {
191
+ certPath := filepath .Join (s .CertDir , s .CertName )
192
+ keyPath := filepath .Join (s .CertDir , s .KeyName )
193
+
194
+ // Create the certificate watcher and
195
+ // set the config's GetCertificate on the TLSConfig
196
+ certWatcher , err := certwatcher .New (certPath , keyPath )
197
+ if err != nil {
198
+ return err
199
+ }
200
+ cfg .GetCertificate = certWatcher .GetCertificate
201
+
202
+ go func () {
203
+ if err := certWatcher .Start (ctx ); err != nil {
204
+ log .Error (err , "certificate watcher error" )
205
+ }
206
+ }()
195
207
}
196
208
197
- // load CA to verify client certificate
209
+ // Load CA to verify client certificate, if configured.
198
210
if s .ClientCAName != "" {
199
211
certPool := x509 .NewCertPool ()
200
212
clientCABytes , err := os .ReadFile (filepath .Join (s .CertDir , s .ClientCAName ))
@@ -211,11 +223,6 @@ func (s *Server) Start(ctx context.Context) error {
211
223
cfg .ClientAuth = tls .RequireAndVerifyClientCert
212
224
}
213
225
214
- // fallback TLS config ready, will now mutate if passer wants full control over it
215
- for _ , op := range s .TLSOpts {
216
- op (cfg )
217
- }
218
-
219
226
listener , err := tls .Listen ("tcp" , net .JoinHostPort (s .Host , strconv .Itoa (s .Port )), cfg )
220
227
if err != nil {
221
228
return err
0 commit comments