Debian Codegraph @

pkg/server/config_test.go

raw
1 /*
2 Copyright 2017 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package server
18
19 import (
20 "context"
21 "errors"
22 "fmt"
23 "io"
24 "net/http"
25 "net/http/httptest"
26 "net/http/httputil"
27 "reflect"
28 "testing"
29 "time"
30
31 "k8s.io/apimachinery/pkg/runtime"
32 "k8s.io/apimachinery/pkg/runtime/serializer"
33 "k8s.io/apimachinery/pkg/util/json"
34 "k8s.io/apimachinery/pkg/util/sets"
35 "k8s.io/apimachinery/pkg/util/wait"
36 "k8s.io/apimachinery/pkg/util/waitgroup"
37 auditinternal "k8s.io/apiserver/pkg/apis/audit"
38 "k8s.io/apiserver/pkg/audit"
39 "k8s.io/apiserver/pkg/audit/policy"
40 "k8s.io/apiserver/pkg/authentication/authenticator"
41 "k8s.io/apiserver/pkg/authentication/user"
42 "k8s.io/apiserver/pkg/authorization/authorizer"
43 "k8s.io/apiserver/pkg/endpoints/request"
44 "k8s.io/apiserver/pkg/features"
45 "k8s.io/apiserver/pkg/server/healthz"
46 utilfeature "k8s.io/apiserver/pkg/util/feature"
47 "k8s.io/client-go/informers"
48 "k8s.io/client-go/kubernetes/fake"
49 "k8s.io/client-go/rest"
50 basecompatibility "k8s.io/component-base/compatibility"
51 featuregatetesting "k8s.io/component-base/featuregate/testing"
52 "k8s.io/component-base/tracing"
53 "k8s.io/klog/v2/ktesting"
54 netutils "k8s.io/utils/net"
55 )
56
57 func TestAuthorizeClientBearerTokenNoops(t *testing.T) {
58 // All of these should do nothing (not panic, no side-effects)
59 cfgGens := []func() *rest.Config{
60 func() *rest.Config { return nil },
61 func() *rest.Config { return &rest.Config{} },
62 func() *rest.Config { return &rest.Config{BearerToken: "mu"} },
63 }
64 authcGens := []func() *AuthenticationInfo{
65 func() *AuthenticationInfo { return nil },
66 func() *AuthenticationInfo { return &AuthenticationInfo{} },
67 }
68 authzGens := []func() *AuthorizationInfo{
69 func() *AuthorizationInfo { return nil },
70 func() *AuthorizationInfo { return &AuthorizationInfo{} },
71 }
72 for _, cfgGen := range cfgGens {
73 for _, authcGen := range authcGens {
74 for _, authzGen := range authzGens {
75 pConfig := cfgGen()
76 pAuthc := authcGen()
77 pAuthz := authzGen()
78 AuthorizeClientBearerToken(pConfig, pAuthc, pAuthz)
79 if before, after := authcGen(), pAuthc; !reflect.DeepEqual(before, after) {
80 t.Errorf("AuthorizeClientBearerToken(%v, %#+v, %v) changed %#+v", pConfig, pAuthc, pAuthz, *before)
81 }
82 if before, after := authzGen(), pAuthz; !reflect.DeepEqual(before, after) {
83 t.Errorf("AuthorizeClientBearerToken(%v, %v, %#+v) changed %#+v", pConfig, pAuthc, pAuthz, *before)
84 }
85 }
86 }
87 }
88 }
89
90 func TestAuthorizeClientBearerTokenRequiredGroups(t *testing.T) {
91 fakeAuthenticator := authenticator.RequestFunc(func(req *http.Request) (*authenticator.Response, bool, error) {
92 return &authenticator.Response{User: &user.DefaultInfo{}}, false, nil
93 })
94 fakeAuthorizer := authorizer.AuthorizerFunc(func(ctx context.Context, a authorizer.Attributes) (authorizer.Decision, string, error) {
95 return authorizer.DecisionAllow, "", nil
96 })
97 target := &rest.Config{BearerToken: "secretToken"}
98 authN := &AuthenticationInfo{Authenticator: fakeAuthenticator}
99 authC := &AuthorizationInfo{Authorizer: fakeAuthorizer}
100
101 AuthorizeClientBearerToken(target, authN, authC)
102
103 fakeRequest, err := http.NewRequest("", "", nil)
104 if err != nil {
105 t.Fatal(err)
106 }
107 fakeRequest.Header.Set("Authorization", "bearer secretToken")
108 rsp, _, err := authN.Authenticator.AuthenticateRequest(fakeRequest)
109 if err != nil {
110 t.Fatal(err)
111 }
112 expectedGroups := []string{user.AllAuthenticated, user.SystemPrivilegedGroup}
113 if !reflect.DeepEqual(expectedGroups, rsp.User.GetGroups()) {
114 t.Fatalf("unexpected groups = %v returned, expected = %v", rsp.User.GetGroups(), expectedGroups)
115 }
116 }
117
118 func TestNewWithDelegate(t *testing.T) {
119 _, ctx := ktesting.NewTestContext(t)
120 ctx, cancel := context.WithCancelCause(ctx)
121 defer cancel(errors.New("test is done"))
122 delegateConfig := NewConfig(codecs)
123 delegateConfig.ExternalAddress = "192.168.10.4:443"
124 delegateConfig.PublicAddress = netutils.ParseIPSloppy("192.168.10.4")
125 delegateConfig.LegacyAPIGroupPrefixes = sets.NewString("/api")
126 delegateConfig.LoopbackClientConfig = &rest.Config{}
127 delegateConfig.EffectiveVersion = basecompatibility.NewEffectiveVersionFromString("", "", "")
128 clientset := fake.NewSimpleClientset()
129 if clientset == nil {
130 t.Fatal("unable to create fake client set")
131 }
132
133 delegateConfig.HealthzChecks = append(delegateConfig.HealthzChecks, healthz.NamedCheck("delegate-health", func(r *http.Request) error {
134 return fmt.Errorf("delegate failed healthcheck")
135 }))
136
137 sharedInformers := informers.NewSharedInformerFactory(clientset, delegateConfig.LoopbackClientConfig.Timeout)
138 delegateServer, err := delegateConfig.Complete(sharedInformers).New("test", NewEmptyDelegate())
139 if err != nil {
140 t.Fatal(err)
141 }
142 delegateServer.Handler.NonGoRestfulMux.HandleFunc("/foo", func(w http.ResponseWriter, _ *http.Request) {
143 w.WriteHeader(http.StatusForbidden)
144 })
145
146 delegatePostStartHookChan := make(chan struct{})
147 delegateServer.AddPostStartHookOrDie("delegate-post-start-hook", func(context PostStartHookContext) error {
148 defer close(delegatePostStartHookChan)
149 return nil
150 })
151
152 // this wires up swagger
153 delegateServer.PrepareRun()
154
155 wrappingConfig := NewConfig(codecs)
156 wrappingConfig.ExternalAddress = "192.168.10.4:443"
157 wrappingConfig.PublicAddress = netutils.ParseIPSloppy("192.168.10.4")
158 wrappingConfig.LegacyAPIGroupPrefixes = sets.NewString("/api")
159 wrappingConfig.LoopbackClientConfig = &rest.Config{}
160 wrappingConfig.EffectiveVersion = basecompatibility.NewEffectiveVersionFromString("", "", "")
161
162 wrappingConfig.HealthzChecks = append(wrappingConfig.HealthzChecks, healthz.NamedCheck("wrapping-health", func(r *http.Request) error {
163 return fmt.Errorf("wrapping failed healthcheck")
164 }))
165
166 sharedInformers = informers.NewSharedInformerFactory(clientset, wrappingConfig.LoopbackClientConfig.Timeout)
167 wrappingServer, err := wrappingConfig.Complete(sharedInformers).New("test", delegateServer)
168 if err != nil {
169 t.Fatal(err)
170 }
171 wrappingServer.Handler.NonGoRestfulMux.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
172 w.WriteHeader(http.StatusUnauthorized)
173 })
174
175 wrappingPostStartHookChan := make(chan struct{})
176 wrappingServer.AddPostStartHookOrDie("wrapping-post-start-hook", func(context PostStartHookContext) error {
177 defer close(wrappingPostStartHookChan)
178 return nil
179 })
180
181 wrappingServer.PrepareRun()
182 wrappingServer.RunPostStartHooks(ctx)
183
184 server := httptest.NewServer(wrappingServer.Handler)
185 defer server.Close()
186
187 // Wait for the hooks to finish before checking the response
188 <-delegatePostStartHookChan
189 <-wrappingPostStartHookChan
190 expectedPaths := []string{
191 "/apis",
192 "/bar",
193 "/foo",
194 "/healthz",
195 "/healthz/delegate-health",
196 "/healthz/log",
197 "/healthz/ping",
198 "/healthz/poststarthook/delegate-post-start-hook",
199 "/healthz/poststarthook/generic-apiserver-start-informers",
200 "/healthz/poststarthook/max-in-flight-filter",
201 "/healthz/poststarthook/storage-object-count-tracker-hook",
202 "/healthz/poststarthook/wrapping-post-start-hook",
203 "/healthz/wrapping-health",
204 "/livez",
205 "/livez/delegate-health",
206 "/livez/log",
207 "/livez/ping",
208 "/livez/poststarthook/delegate-post-start-hook",
209 "/livez/poststarthook/generic-apiserver-start-informers",
210 "/livez/poststarthook/max-in-flight-filter",
211 "/livez/poststarthook/storage-object-count-tracker-hook",
212 "/livez/poststarthook/wrapping-post-start-hook",
213 "/metrics",
214 "/metrics/slis",
215 "/readyz",
216 "/readyz/delegate-health",
217 "/readyz/informer-sync",
218 "/readyz/log",
219 "/readyz/ping",
220 "/readyz/poststarthook/delegate-post-start-hook",
221 "/readyz/poststarthook/generic-apiserver-start-informers",
222 "/readyz/poststarthook/max-in-flight-filter",
223 "/readyz/poststarthook/storage-object-count-tracker-hook",
224 "/readyz/poststarthook/wrapping-post-start-hook",
225 "/readyz/shutdown",
226 }
227 checkExpectedPathsAtRoot(server.URL, expectedPaths, t)
228
229 // wait for health (max-in-flight-filter is initialized asynchronously, can take a few milliseconds to initialize)
230 if err := wait.PollImmediate(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) {
231 // healthz checks are installed in PrepareRun
232 resp, err := http.Get(server.URL + "/healthz?exclude=wrapping-health&exclude=delegate-health")
233 if err != nil {
234 t.Fatal(err)
235 }
236 data, _ := io.ReadAll(resp.Body)
237 if http.StatusOK != resp.StatusCode {
238 t.Logf("got %d", resp.StatusCode)
239 t.Log(string(data))
240 return false, nil
241 }
242 return true, nil
243 }); err != nil {
244 t.Fatal(err)
245 }
246 checkPath(server.URL+"/healthz", http.StatusInternalServerError, `[+]ping ok
247 [+]log ok
248 [-]wrapping-health failed: reason withheld
249 [-]delegate-health failed: reason withheld
250 [+]poststarthook/generic-apiserver-start-informers ok
251 [+]poststarthook/max-in-flight-filter ok
252 [+]poststarthook/storage-object-count-tracker-hook ok
253 [+]poststarthook/delegate-post-start-hook ok
254 [+]poststarthook/wrapping-post-start-hook ok
255 healthz check failed
256 `, t)
257
258 checkPath(server.URL+"/healthz/delegate-health", http.StatusInternalServerError, `internal server error: delegate failed healthcheck
259 `, t)
260 checkPath(server.URL+"/healthz/wrapping-health", http.StatusInternalServerError, `internal server error: wrapping failed healthcheck
261 `, t)
262 checkPath(server.URL+"/healthz/poststarthook/delegate-post-start-hook", http.StatusOK, `ok`, t)
263 checkPath(server.URL+"/healthz/poststarthook/wrapping-post-start-hook", http.StatusOK, `ok`, t)
264 checkPath(server.URL+"/foo", http.StatusForbidden, ``, t)
265 checkPath(server.URL+"/bar", http.StatusUnauthorized, ``, t)
266 }
267
268 func checkPath(url string, expectedStatusCode int, expectedBody string, t *testing.T) {
269 t.Run(url, func(t *testing.T) {
270 resp, err := http.Get(url)
271 if err != nil {
272 t.Fatal(err)
273 }
274 dump, _ := httputil.DumpResponse(resp, true)
275 t.Log(string(dump))
276
277 body, err := io.ReadAll(resp.Body)
278 if err != nil {
279 t.Fatal(err)
280 }
281
282 if e, a := expectedBody, string(body); e != a {
283 t.Errorf("%q expected %v, got %v", url, e, a)
284 }
285 if e, a := expectedStatusCode, resp.StatusCode; e != a {
286 t.Errorf("%q expected %v, got %v", url, e, a)
287 }
288 })
289 }
290
291 func checkExpectedPathsAtRoot(url string, expectedPaths []string, t *testing.T) {
292 t.Run(url, func(t *testing.T) {
293 resp, err := http.Get(url)
294 if err != nil {
295 t.Fatal(err)
296 }
297 dump, _ := httputil.DumpResponse(resp, true)
298 t.Log(string(dump))
299
300 body, err := io.ReadAll(resp.Body)
301 if err != nil {
302 t.Fatal(err)
303 }
304 var result map[string]interface{}
305 json.Unmarshal(body, &result)
306 paths, ok := result["paths"].([]interface{})
307 if !ok {
308 t.Errorf("paths not found")
309 }
310 pathset := sets.NewString()
311 for _, p := range paths {
312 pathset.Insert(p.(string))
313 }
314 expectedset := sets.NewString(expectedPaths...)
315 for p := range pathset.Difference(expectedset) {
316 t.Errorf("Got %v path, which we did not expect", p)
317 }
318 for p := range expectedset.Difference(pathset) {
319 t.Errorf(" Expected %v path which we did not get", p)
320 }
321 })
322 }
323
324 func TestAuthenticationAuditAnnotationsDefaultChain(t *testing.T) {
325 authn := authenticator.RequestFunc(func(req *http.Request) (*authenticator.Response, bool, error) {
326 // confirm that we can set an audit annotation in a handler before WithAudit
327 audit.AddAuditAnnotation(req.Context(), "pandas", "are awesome")
328
329 return &authenticator.Response{User: &user.DefaultInfo{}}, true, nil
330 })
331 backend := &testBackend{}
332 c := &Config{
333 Authentication: AuthenticationInfo{Authenticator: authn},
334 AuditBackend: backend,
335 AuditPolicyRuleEvaluator: policy.NewFakePolicyRuleEvaluator(auditinternal.LevelMetadata, nil),
336
337 // avoid nil panics
338 NonLongRunningRequestWaitGroup: &waitgroup.SafeWaitGroup{},
339 RequestInfoResolver: &request.RequestInfoFactory{},
340 RequestTimeout: 10 * time.Second,
341 LongRunningFunc: func(_ *http.Request, _ *request.RequestInfo) bool { return false },
342 lifecycleSignals: newLifecycleSignals(),
343 TracerProvider: tracing.NewNoopTracerProvider(),
344 FeatureGate: utilfeature.DefaultFeatureGate,
345 }
346
347 h := DefaultBuildHandlerChain(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
348 // confirm this is a no-op
349 if r.Context() != audit.WithAuditContext(r.Context()) {
350 t.Error("unexpected double wrapping of context")
351 }
352
353 // confirm that we have an audit event
354 ae := audit.AuditEventFrom(r.Context())
355 if ae == nil {
356 t.Error("unexpected nil audit event")
357 }
358
359 // confirm that the indirect way of setting audit annotations later in the chain also works
360 audit.AddAuditAnnotation(r.Context(), "dogs", "are okay")
361
362 if _, err := w.Write([]byte("done")); err != nil {
363 t.Errorf("failed to write response: %v", err)
364 }
365 }), c)
366 w := httptest.NewRecorder()
367
368 h.ServeHTTP(w, httptest.NewRequest("GET", "https://ignored.com", nil))
369
370 r := w.Result()
371 if ok := r.StatusCode == http.StatusOK && w.Body.String() == "done" && len(r.Header.Get(auditinternal.HeaderAuditID)) > 0; !ok {
372 t.Errorf("invalid response: %#v", w)
373 }
374 if len(backend.events) == 0 {
375 t.Error("expected audit events, got none")
376 }
377 // these should all be the same because the handler chain mutates the event in place
378 want := map[string]string{"pandas": "are awesome", "dogs": "are okay"}
379 for _, event := range backend.events {
380 if event.Stage != auditinternal.StageResponseComplete {
381 t.Errorf("expected event stage to be complete, got: %s", event.Stage)
382 }
383
384 for wantK, wantV := range want {
385 gotV, ok := event.Annotations[wantK]
386 if !ok {
387 t.Errorf("expected to find annotation key %q in %#v", wantK, event.Annotations)
388 continue
389 }
390 if wantV != gotV {
391 t.Errorf("expected the annotation value to match, key: %q, want: %q got: %q", wantK, wantV, gotV)
392 }
393 }
394 }
395 }
396
397 type testBackend struct {
398 events []*auditinternal.Event
399
400 audit.Backend // nil panic if anything other than ProcessEvents called
401 }
402
403 func (b *testBackend) ProcessEvents(events ...*auditinternal.Event) bool {
404 b.events = append(b.events, events...)
405 return true
406 }
407
408 func TestNewErrorForbiddenSerializer(t *testing.T) {
409 config := CompletedConfig{
410 &completedConfig{
411 Config: &Config{
412 Serializer: runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{
413 MediaType: "application/cbor",
414 }),
415 },
416 },
417 }
418 _, err := config.New("test", NewEmptyDelegate())
419 if err == nil {
420 t.Error("successfully created a new server configured with cbor support")
421 } else if err.Error() != `refusing to create new apiserver "test" with support for media type "application/cbor" (allowed media types are: application/json, application/yaml, application/vnd.kubernetes.protobuf)` {
422 t.Errorf("unexpected error: %v", err)
423 }
424 }
425
426 func TestNewFeatureGatedSerializer(t *testing.T) {
427 featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CBORServingAndStorage, true)
428
429 config := NewConfig(serializer.NewCodecFactory(scheme, serializer.WithSerializer(func(creater runtime.ObjectCreater, typer runtime.ObjectTyper) runtime.SerializerInfo {
430 return runtime.SerializerInfo{
431 MediaType: "application/cbor",
432 MediaTypeType: "application",
433 MediaTypeSubType: "cbor",
434 }
435 })))
436 config.ExternalAddress = "192.168.10.4:443"
437 config.EffectiveVersion = basecompatibility.NewEffectiveVersionFromString("", "", "")
438 config.LoopbackClientConfig = &rest.Config{}
439
440 if _, err := config.Complete(nil).New("test", NewEmptyDelegate()); err != nil {
441 t.Errorf("unexpected error: %v", err)
442 }
443 }