Skip to content

Commit 187187e

Browse files
committed
Allow TLS config to be entirely configured on webhook server
Some operators might want to respect cluster-wide TLS ciphers for example, which means that these will eventually have to be passed down to the webhook server. Signed-off-by: Alex Kalenyuk <akalenyu@redhat.com>
1 parent 3f265c3 commit 187187e

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

pkg/webhook/server.go

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ type Server struct {
7676
// "", "1.0", "1.1", "1.2" and "1.3" only ("" is equivalent to "1.0" for backwards compatibility)
7777
TLSMinVersion string
7878

79+
// TLSOpts is used to allow configuring the TLS config used for the server
80+
TLSOpts []func(*tls.Config)
81+
7982
// WebhookMux is the multiplexer that handles different webhooks.
8083
WebhookMux *http.ServeMux
8184

@@ -254,6 +257,11 @@ func (s *Server) Start(ctx context.Context) error {
254257
cfg.ClientAuth = tls.RequireAndVerifyClientCert
255258
}
256259

260+
// fallback TLS config ready, will now mutate if passer wants full control over it
261+
for _, op := range s.TLSOpts {
262+
op(cfg)
263+
}
264+
257265
listener, err := tls.Listen("tcp", net.JoinHostPort(s.Host, strconv.Itoa(s.Port)), cfg)
258266
if err != nil {
259267
return err

pkg/webhook/server_test.go

+42-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package webhook_test
1818

1919
import (
2020
"context"
21+
"crypto/tls"
2122
"fmt"
2223
"io"
2324
"net"
@@ -186,7 +187,7 @@ var _ = Describe("Webhook Server", func() {
186187
})
187188
})
188189

189-
It("should serve be able to serve in unmanaged mode", func() {
190+
It("should be able to serve in unmanaged mode", func() {
190191
server = &webhook.Server{
191192
Host: servingOpts.LocalServingHost,
192193
Port: servingOpts.LocalServingPort,
@@ -207,6 +208,46 @@ var _ = Describe("Webhook Server", func() {
207208
ctxCancel()
208209
Eventually(doneCh, "4s").Should(BeClosed())
209210
})
211+
212+
It("should respect passed in TLS configurations", func() {
213+
var finalCfg *tls.Config
214+
tlsCfgFunc := func(cfg *tls.Config) {
215+
cfg.CipherSuites = []uint16{
216+
tls.TLS_AES_128_GCM_SHA256,
217+
tls.TLS_AES_256_GCM_SHA384,
218+
}
219+
// save cfg after changes to test against
220+
finalCfg = cfg
221+
}
222+
server = &webhook.Server{
223+
Host: servingOpts.LocalServingHost,
224+
Port: servingOpts.LocalServingPort,
225+
CertDir: servingOpts.LocalServingCertDir,
226+
TLSMinVersion: "1.2",
227+
TLSOpts: []func(*tls.Config){
228+
tlsCfgFunc,
229+
},
230+
}
231+
server.Register("/somepath", &testHandler{})
232+
doneCh := genericStartServer(func(ctx context.Context) {
233+
Expect(server.StartStandalone(ctx, scheme.Scheme))
234+
})
235+
236+
Eventually(func() ([]byte, error) {
237+
resp, err := client.Get(fmt.Sprintf("https://%s/somepath", testHostPort))
238+
Expect(err).NotTo(HaveOccurred())
239+
defer resp.Body.Close()
240+
return io.ReadAll(resp.Body)
241+
}).Should(Equal([]byte("gadzooks!")))
242+
Expect(finalCfg.MinVersion).To(Equal(uint16(tls.VersionTLS12)))
243+
Expect(finalCfg.CipherSuites).To(ContainElements(
244+
tls.TLS_AES_128_GCM_SHA256,
245+
tls.TLS_AES_256_GCM_SHA384,
246+
))
247+
248+
ctxCancel()
249+
Eventually(doneCh, "4s").Should(BeClosed())
250+
})
210251
})
211252

212253
type testHandler struct {

0 commit comments

Comments
 (0)