Skip to content

Commit 9dfcc36

Browse files
authored
Merge pull request kubernetes#84864 from deads2k/optional-verify-opts
allow a verifyoptionsfunc to indicate that no certpool is available
2 parents 3510ae5 + cd675cc commit 9dfcc36

File tree

9 files changed

+39
-15
lines changed

9 files changed

+39
-15
lines changed

staging/src/k8s.io/apiserver/pkg/authentication/authenticatorfactory/requestheader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ type CAContentProvider interface {
4444
// CurrentCABundleContent provides ca bundle byte content
4545
CurrentCABundleContent() []byte
4646
// VerifyOptions provides VerifyOptions for authenticators
47-
VerifyOptions() x509.VerifyOptions
47+
VerifyOptions() (x509.VerifyOptions, bool)
4848
}

staging/src/k8s.io/apiserver/pkg/authentication/request/x509/verify_options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525

2626
// StaticVerifierFn is a VerifyOptionFunc that always returns the same value. This allows verify options that cannot change.
2727
func StaticVerifierFn(opts x509.VerifyOptions) VerifyOptionFunc {
28-
return func() x509.VerifyOptions {
29-
return opts
28+
return func() (x509.VerifyOptions, bool) {
29+
return opts, true
3030
}
3131
}
3232

staging/src/k8s.io/apiserver/pkg/authentication/request/x509/x509.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ func (f UserConversionFunc) User(chain []*x509.Certificate) (*authenticator.Resp
8383
}
8484

8585
// VerifyOptionFunc is function which provides a shallow copy of the VerifyOptions to the authenticator. This allows
86-
// for cases where the options (particularly the CAs) can change.
87-
type VerifyOptionFunc func() x509.VerifyOptions
86+
// for cases where the options (particularly the CAs) can change. If the bool is false, then the returned VerifyOptions
87+
// are ignored and the authenticator will express "no opinion". This allows a clear signal for cases where a CertPool
88+
// is eventually expected, but not currently present.
89+
type VerifyOptionFunc func() (x509.VerifyOptions, bool)
8890

8991
// Authenticator implements request.Authenticator by extracting user info from verified client certificates
9092
type Authenticator struct {
@@ -111,7 +113,11 @@ func (a *Authenticator) AuthenticateRequest(req *http.Request) (*authenticator.R
111113
}
112114

113115
// Use intermediates, if provided
114-
optsCopy := a.verifyOptionsFn()
116+
optsCopy, ok := a.verifyOptionsFn()
117+
// if there are intentionally no verify options, then we cannot authenticate this request
118+
if !ok {
119+
return nil, false, nil
120+
}
115121
if optsCopy.Intermediates == nil && len(req.TLS.PeerCertificates) > 1 {
116122
optsCopy.Intermediates = x509.NewCertPool()
117123
for _, intermediate := range req.TLS.PeerCertificates[1:] {
@@ -169,7 +175,11 @@ func (a *Verifier) AuthenticateRequest(req *http.Request) (*authenticator.Respon
169175
}
170176

171177
// Use intermediates, if provided
172-
optsCopy := a.verifyOptionsFn()
178+
optsCopy, ok := a.verifyOptionsFn()
179+
// if there are intentionally no verify options, then we cannot authenticate this request
180+
if !ok {
181+
return nil, false, nil
182+
}
173183
if optsCopy.Intermediates == nil && len(req.TLS.PeerCertificates) > 1 {
174184
optsCopy.Intermediates = x509.NewCertPool()
175185
for _, intermediate := range req.TLS.PeerCertificates[1:] {

staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/client_ca.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type CAContentProvider interface {
2929
// the value. By the time you get here, you should always be returning a value that won't fail.
3030
CurrentCABundleContent() []byte
3131
// VerifyOptions provides VerifyOptions for authenticators
32-
VerifyOptions() x509.VerifyOptions
32+
VerifyOptions() (x509.VerifyOptions, bool)
3333
}
3434

3535
// dynamicCertificateContent holds the content that overrides the baseTLSConfig

staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/dynamic_cafile_content.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,13 @@ func (c *DynamicFileCAContent) CurrentCABundleContent() (cabundle []byte) {
215215
}
216216

217217
// VerifyOptions provides verifyoptions compatible with authenticators
218-
func (c *DynamicFileCAContent) VerifyOptions() x509.VerifyOptions {
219-
return c.caBundle.Load().(*caBundleAndVerifier).verifyOptions
218+
func (c *DynamicFileCAContent) VerifyOptions() (x509.VerifyOptions, bool) {
219+
uncastObj := c.caBundle.Load()
220+
if uncastObj == nil {
221+
return x509.VerifyOptions{}, false
222+
}
223+
224+
return uncastObj.(*caBundleAndVerifier).verifyOptions, true
220225
}
221226

222227
// newVerifyOptions creates a new verification func from a file. It reads the content and then fails.

staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/static_content.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func (c *staticCAContent) CurrentCABundleContent() (cabundle []byte) {
6666
return c.caBundle.caBundle
6767
}
6868

69-
func (c *staticCAContent) VerifyOptions() x509.VerifyOptions {
70-
return c.caBundle.verifyOptions
69+
func (c *staticCAContent) VerifyOptions() (x509.VerifyOptions, bool) {
70+
return c.caBundle.verifyOptions, true
7171
}
7272

7373
type staticCertKeyContent struct {

staging/src/k8s.io/apiserver/pkg/server/dynamiccertificates/union_content.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ func (c unionCAContent) CurrentCABundleContent() []byte {
5555
}
5656

5757
// CurrentCABundleContent provides ca bundle byte content
58-
func (c unionCAContent) VerifyOptions() x509.VerifyOptions {
58+
func (c unionCAContent) VerifyOptions() (x509.VerifyOptions, bool) {
59+
currCABundle := c.CurrentCABundleContent()
60+
if len(currCABundle) == 0 {
61+
return x509.VerifyOptions{}, false
62+
}
63+
5964
// TODO make more efficient. This isn't actually used in any of our mainline paths. It's called to build the TLSConfig
6065
// TODO on file changes, but the actual authentication runs against the individual items, not the union.
6166
ret, err := newCABundleAndVerifier(c.Name(), c.CurrentCABundleContent())
@@ -64,7 +69,7 @@ func (c unionCAContent) VerifyOptions() x509.VerifyOptions {
6469
panic(err)
6570
}
6671

67-
return ret.verifyOptions
72+
return ret.verifyOptions, true
6873
}
6974

7075
// AddListener adds a listener to be notified when the CA content changes.

test/integration/scheduler/extender_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"testing"
2828
"time"
2929

30-
"k8s.io/api/core/v1"
30+
v1 "k8s.io/api/core/v1"
3131
"k8s.io/apimachinery/pkg/api/resource"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3333
"k8s.io/apimachinery/pkg/util/wait"

test/integration/scheduler/taint_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ func TestTaintNodeByCondition(t *testing.T) {
8585
defer algorithmprovider.ApplyFeatureGates()()
8686

8787
context = initTestScheduler(t, context, false, nil)
88+
defer cleanupTest(t, context)
89+
8890
cs := context.clientSet
8991
informers := context.informerFactory
9092
nsName := context.ns.Name
@@ -655,6 +657,8 @@ func TestTaintBasedEvictions(t *testing.T) {
655657
for i, test := range tests {
656658
t.Run(test.name, func(t *testing.T) {
657659
context := initTestMaster(t, "taint-based-evictions", admission)
660+
defer cleanupTest(t, context)
661+
658662
// Build clientset and informers for controllers.
659663
externalClientset := kubernetes.NewForConfigOrDie(&restclient.Config{
660664
QPS: -1,

0 commit comments

Comments
 (0)