Skip to content

Commit b561eaa

Browse files
upgrade to latest dependencies (#492)
bumping knative.dev/pkg 64ab22b...552bbc1: > 552bbc1 Support the webhook serving over non-TLS. (# 2204) Signed-off-by: Knative Automation <[email protected]>
1 parent 55ec44b commit b561eaa

File tree

4 files changed

+67
-55
lines changed

4 files changed

+67
-55
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ require (
1212
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd
1313
knative.dev/hack v0.0.0-20210622141627-e28525d8d260
1414
knative.dev/hack/schema v0.0.0-20210622141627-e28525d8d260
15-
knative.dev/pkg v0.0.0-20210731072840-64ab22bbaab9
15+
knative.dev/pkg v0.0.0-20210803032247-552bbc106170
1616
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,8 @@ knative.dev/hack v0.0.0-20210622141627-e28525d8d260 h1:f2eMtOubAOc/Q7JlvFPDKXiPl
10551055
knative.dev/hack v0.0.0-20210622141627-e28525d8d260/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI=
10561056
knative.dev/hack/schema v0.0.0-20210622141627-e28525d8d260 h1:YkMkZ7qdafyRHNIuKttYzEmM1ilKTGyEtPWeVLcLcDE=
10571057
knative.dev/hack/schema v0.0.0-20210622141627-e28525d8d260/go.mod h1:ffjwmdcrH5vN3mPhO8RrF2KfNnbHeCE2C60A+2cv3U0=
1058-
knative.dev/pkg v0.0.0-20210731072840-64ab22bbaab9 h1:eeRutJPRJ6tR7LVkeaD7H2BaKeKwadHaR66+Z1QRVcs=
1059-
knative.dev/pkg v0.0.0-20210731072840-64ab22bbaab9/go.mod h1:NYZRIPU+Pv39VfbZV1BtMIe4kCavNle1udsPrvOLm+Y=
1058+
knative.dev/pkg v0.0.0-20210803032247-552bbc106170 h1:9391x1TMB/+oFjP5L2YLk61k+mEpm8VLgu6adXDBdcY=
1059+
knative.dev/pkg v0.0.0-20210803032247-552bbc106170/go.mod h1:NYZRIPU+Pv39VfbZV1BtMIe4kCavNle1udsPrvOLm+Y=
10601060
pgregory.net/rapid v0.3.3/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU=
10611061
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
10621062
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=

vendor/knative.dev/pkg/webhook/webhook.go

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"go.uber.org/zap"
3434
"golang.org/x/sync/errgroup"
3535
admissionv1 "k8s.io/api/admission/v1"
36-
corelisters "k8s.io/client-go/listers/core/v1"
3736
"knative.dev/pkg/logging"
3837
"knative.dev/pkg/network"
3938
"knative.dev/pkg/system"
@@ -50,6 +49,7 @@ type Options struct {
5049
// server key/cert are used to serve the webhook and the CA cert
5150
// is provided to k8s apiserver during admission controller
5251
// registration.
52+
// If no SecretName is provided, then the webhook serves without TLS.
5353
SecretName string
5454

5555
// Port where the webhook is served. Per k8s admission
@@ -87,8 +87,10 @@ type Webhook struct {
8787
// before shutting down.
8888
gracePeriod time.Duration
8989

90-
mux http.ServeMux
91-
secretlister corelisters.SecretLister
90+
mux http.ServeMux
91+
92+
// The TLS configuration to use for serving (or nil for non-TLS)
93+
tlsConfig *tls.Config
9294
}
9395

9496
// New constructs a Webhook
@@ -104,13 +106,6 @@ func New(
104106
}
105107
}()
106108

107-
// Injection is too aggressive for this case because by simply linking this
108-
// library we force consumers to have secret access. If we require that one
109-
// of the admission controllers' informers *also* require the secret
110-
// informer, then we can fetch the shared informer factory here and produce
111-
// a new secret informer from it.
112-
secretInformer := kubeinformerfactory.Get(ctx).Core().V1().Secrets()
113-
114109
opts := GetOptions(ctx)
115110
if opts == nil {
116111
return nil, errors.New("context must have Options specified")
@@ -128,11 +123,51 @@ func New(
128123
syncCtx, cancel := context.WithCancel(context.Background())
129124

130125
webhook = &Webhook{
131-
Options: *opts,
132-
secretlister: secretInformer.Lister(),
133-
Logger: logger,
134-
synced: cancel,
135-
gracePeriod: network.DefaultDrainTimeout,
126+
Options: *opts,
127+
Logger: logger,
128+
synced: cancel,
129+
gracePeriod: network.DefaultDrainTimeout,
130+
}
131+
132+
if opts.SecretName != "" {
133+
// Injection is too aggressive for this case because by simply linking this
134+
// library we force consumers to have secret access. If we require that one
135+
// of the admission controllers' informers *also* require the secret
136+
// informer, then we can fetch the shared informer factory here and produce
137+
// a new secret informer from it.
138+
secretInformer := kubeinformerfactory.Get(ctx).Core().V1().Secrets()
139+
140+
webhook.tlsConfig = &tls.Config{
141+
MinVersion: tls.VersionTLS12,
142+
143+
// If we return (nil, error) the client sees - 'tls: internal error"
144+
// If we return (nil, nil) the client sees - 'tls: no certificates configured'
145+
//
146+
// We'll return (nil, nil) when we don't find a certificate
147+
GetCertificate: func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
148+
secret, err := secretInformer.Lister().Secrets(system.Namespace()).Get(opts.SecretName)
149+
if err != nil {
150+
logger.Errorw("failed to fetch secret", zap.Error(err))
151+
return nil, nil
152+
}
153+
154+
serverKey, ok := secret.Data[certresources.ServerKey]
155+
if !ok {
156+
logger.Warn("server key missing")
157+
return nil, nil
158+
}
159+
serverCert, ok := secret.Data[certresources.ServerCert]
160+
if !ok {
161+
logger.Warn("server cert missing")
162+
return nil, nil
163+
}
164+
cert, err := tls.X509KeyPair(serverCert, serverKey)
165+
if err != nil {
166+
return nil, err
167+
}
168+
return &cert, nil
169+
},
170+
}
136171
}
137172

138173
webhook.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@@ -176,46 +211,23 @@ func (wh *Webhook) Run(stop <-chan struct{}) error {
176211
}
177212

178213
server := &http.Server{
179-
Handler: drainer,
180-
Addr: fmt.Sprint(":", wh.Options.Port),
181-
TLSConfig: &tls.Config{
182-
MinVersion: tls.VersionTLS12,
183-
184-
// If we return (nil, error) the client sees - 'tls: internal error"
185-
// If we return (nil, nil) the client sees - 'tls: no certificates configured'
186-
//
187-
// We'll return (nil, nil) when we don't find a certificate
188-
GetCertificate: func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
189-
secret, err := wh.secretlister.Secrets(system.Namespace()).Get(wh.Options.SecretName)
190-
if err != nil {
191-
logger.Errorw("failed to fetch secret", zap.Error(err))
192-
return nil, nil
193-
}
194-
195-
serverKey, ok := secret.Data[certresources.ServerKey]
196-
if !ok {
197-
logger.Warn("server key missing")
198-
return nil, nil
199-
}
200-
serverCert, ok := secret.Data[certresources.ServerCert]
201-
if !ok {
202-
logger.Warn("server cert missing")
203-
return nil, nil
204-
}
205-
cert, err := tls.X509KeyPair(serverCert, serverKey)
206-
if err != nil {
207-
return nil, err
208-
}
209-
return &cert, nil
210-
},
211-
},
214+
Handler: drainer,
215+
Addr: fmt.Sprint(":", wh.Options.Port),
216+
TLSConfig: wh.tlsConfig,
212217
}
213218

214219
eg, ctx := errgroup.WithContext(ctx)
215220
eg.Go(func() error {
216-
if err := server.ListenAndServeTLS("", ""); err != nil && !errors.Is(err, http.ErrServerClosed) {
217-
logger.Errorw("ListenAndServeTLS for admission webhook returned error", zap.Error(err))
218-
return err
221+
if server.TLSConfig != nil {
222+
if err := server.ListenAndServeTLS("", ""); err != nil && !errors.Is(err, http.ErrServerClosed) {
223+
logger.Errorw("ListenAndServeTLS for admission webhook returned error", zap.Error(err))
224+
return err
225+
}
226+
} else {
227+
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
228+
logger.Errorw("ListenAndServe for admission webhook returned error", zap.Error(err))
229+
return err
230+
}
219231
}
220232
return nil
221233
})

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ knative.dev/hack/schema/commands
671671
knative.dev/hack/schema/docs
672672
knative.dev/hack/schema/registry
673673
knative.dev/hack/schema/schema
674-
# knative.dev/pkg v0.0.0-20210731072840-64ab22bbaab9
674+
# knative.dev/pkg v0.0.0-20210803032247-552bbc106170
675675
## explicit
676676
knative.dev/pkg/apis
677677
knative.dev/pkg/apis/duck

0 commit comments

Comments
 (0)