|
| 1 | +/* |
| 2 | +Copyright 2026 The kcp 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 proxy |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "net/http" |
| 22 | + "net/http/httptest" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "k8s.io/apiserver/pkg/authentication/authenticator" |
| 26 | + "k8s.io/apiserver/pkg/authentication/user" |
| 27 | + "k8s.io/apiserver/pkg/authorization/authorizer" |
| 28 | + |
| 29 | + "github.com/kcp-dev/kcp/pkg/server/requestinfo" |
| 30 | +) |
| 31 | + |
| 32 | +// authFunc adapts a function to authenticator.Request. |
| 33 | +type authFunc func(req *http.Request) (*authenticator.Response, bool, error) |
| 34 | + |
| 35 | +func (f authFunc) AuthenticateRequest(req *http.Request) (*authenticator.Response, bool, error) { |
| 36 | + return f(req) |
| 37 | +} |
| 38 | + |
| 39 | +// authzFunc adapts a function to authorizer.Authorizer. |
| 40 | +type authzFunc func(ctx context.Context, a authorizer.Attributes) (authorizer.Decision, string, error) |
| 41 | + |
| 42 | +func (f authzFunc) Authorize(ctx context.Context, a authorizer.Attributes) (authorizer.Decision, string, error) { |
| 43 | + return f(ctx, a) |
| 44 | +} |
| 45 | + |
| 46 | +// TestWithMetricsAuthorization verifies that the /metrics guard rejects |
| 47 | +// anonymous callers (401) and authenticated-but-unauthorized callers (403), and |
| 48 | +// only invokes the wrapped handler for an authenticated, authorized caller. |
| 49 | +func TestWithMetricsAuthorization(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + authenticated := authFunc(func(req *http.Request) (*authenticator.Response, bool, error) { |
| 52 | + return &authenticator.Response{User: &user.DefaultInfo{Name: "scraper"}}, true, nil |
| 53 | + }) |
| 54 | + anonymous := authFunc(func(req *http.Request) (*authenticator.Response, bool, error) { |
| 55 | + return nil, false, nil |
| 56 | + }) |
| 57 | + allow := authzFunc(func(ctx context.Context, a authorizer.Attributes) (authorizer.Decision, string, error) { |
| 58 | + // The guard must authorize the /metrics non-resource URL. |
| 59 | + if a.IsResourceRequest() || a.GetPath() != "/metrics" || a.GetVerb() != "get" { |
| 60 | + return authorizer.DecisionDeny, "unexpected attributes", nil |
| 61 | + } |
| 62 | + return authorizer.DecisionAllow, "", nil |
| 63 | + }) |
| 64 | + deny := authzFunc(func(ctx context.Context, a authorizer.Attributes) (authorizer.Decision, string, error) { |
| 65 | + return authorizer.DecisionNoOpinion, "nope", nil |
| 66 | + }) |
| 67 | + |
| 68 | + for _, tc := range []struct { |
| 69 | + name string |
| 70 | + auth authenticator.Request |
| 71 | + authz authorizer.Authorizer |
| 72 | + wantStatus int |
| 73 | + wantServed bool |
| 74 | + }{ |
| 75 | + {name: "anonymous is rejected", auth: anonymous, authz: allow, wantStatus: http.StatusUnauthorized, wantServed: false}, |
| 76 | + {name: "authenticated but unauthorized is forbidden", auth: authenticated, authz: deny, wantStatus: http.StatusForbidden, wantServed: false}, |
| 77 | + {name: "authenticated and authorized is served", auth: authenticated, authz: allow, wantStatus: http.StatusOK, wantServed: true}, |
| 78 | + } { |
| 79 | + t.Run(tc.name, func(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + served := false |
| 82 | + inner := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 83 | + served = true |
| 84 | + w.WriteHeader(http.StatusOK) |
| 85 | + }) |
| 86 | + |
| 87 | + h := withMetricsAuthorization(inner, tc.auth, tc.authz, requestinfo.NewFactory()) |
| 88 | + |
| 89 | + rec := httptest.NewRecorder() |
| 90 | + h.ServeHTTP(rec, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/metrics", http.NoBody)) |
| 91 | + |
| 92 | + if rec.Code != tc.wantStatus { |
| 93 | + t.Errorf("status = %d, want %d (body: %s)", rec.Code, tc.wantStatus, rec.Body.String()) |
| 94 | + } |
| 95 | + if served != tc.wantServed { |
| 96 | + t.Errorf("wrapped handler served = %v, want %v", served, tc.wantServed) |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
0 commit comments