@@ -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,48 @@ 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
+ tlsMinVersion , err := tlsVersion (s .TLSMinVersion )
176
177
if err != nil {
177
178
return err
178
179
}
179
180
180
- go func () {
181
- if err := certWatcher .Start (ctx ); err != nil {
182
- log .Error (err , "certificate watcher error" )
181
+ cfg := & tls.Config { //nolint:gosec
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
+ switch {
191
+ case cfg .GetCertificate != nil :
192
+ if s .CertName != "" {
193
+ return fmt .Errorf ("cannot use GetCertificate and CertName at the same time" )
183
194
}
184
- }()
195
+ if s .KeyName != "" {
196
+ return fmt .Errorf ("cannot use GetCertificate and KeyName at the same time" )
197
+ }
198
+ default :
199
+ certPath := filepath .Join (s .CertDir , s .CertName )
200
+ keyPath := filepath .Join (s .CertDir , s .KeyName )
185
201
186
- tlsMinVersion , err := tlsVersion (s .TLSMinVersion )
187
- if err != nil {
188
- return err
189
- }
202
+ // Create the certificate watcher and
203
+ // set the config's GetCertificate on the TLSConfig
204
+ certWatcher , err := certwatcher .New (certPath , keyPath )
205
+ if err != nil {
206
+ return err
207
+ }
208
+ cfg .GetCertificate = certWatcher .GetCertificate
190
209
191
- cfg := & tls.Config { //nolint:gosec
192
- NextProtos : []string {"h2" },
193
- GetCertificate : certWatcher .GetCertificate ,
194
- MinVersion : tlsMinVersion ,
210
+ go func () {
211
+ if err := certWatcher .Start (ctx ); err != nil {
212
+ log .Error (err , "certificate watcher error" )
213
+ }
214
+ }()
195
215
}
196
216
197
- // load CA to verify client certificate
217
+ // Load CA to verify client certificate, if configured.
198
218
if s .ClientCAName != "" {
199
219
certPool := x509 .NewCertPool ()
200
220
clientCABytes , err := os .ReadFile (filepath .Join (s .CertDir , s .ClientCAName ))
@@ -211,11 +231,6 @@ func (s *Server) Start(ctx context.Context) error {
211
231
cfg .ClientAuth = tls .RequireAndVerifyClientCert
212
232
}
213
233
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
234
listener , err := tls .Listen ("tcp" , net .JoinHostPort (s .Host , strconv .Itoa (s .Port )), cfg )
220
235
if err != nil {
221
236
return err
0 commit comments