Debian Codegraph @

pkg/endpoints/filters/authorization.go

raw
1 /*
2 Copyright 2016 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 filters
18
19 import (
20 "context"
21 "errors"
22 "net/http"
23 "time"
24
25 "k8s.io/apimachinery/pkg/fields"
26 "k8s.io/apimachinery/pkg/labels"
27 genericfeatures "k8s.io/apiserver/pkg/features"
28 utilfeature "k8s.io/apiserver/pkg/util/feature"
29
30 "k8s.io/klog/v2"
31
32 "k8s.io/apimachinery/pkg/runtime"
33 "k8s.io/apiserver/pkg/audit"
34 "k8s.io/apiserver/pkg/authorization/authorizer"
35 "k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
36 "k8s.io/apiserver/pkg/endpoints/request"
37 )
38
39 const (
40 // Annotation key names set in advanced audit
41 decisionAnnotationKey = "authorization.k8s.io/decision"
42 reasonAnnotationKey = "authorization.k8s.io/reason"
43
44 // Annotation values set in advanced audit
45 decisionAllow = "allow"
46 decisionForbid = "forbid"
47 reasonError = "internal error"
48 )
49
50 type recordAuthorizationMetricsFunc func(ctx context.Context, authorized authorizer.Decision, err error, authStart time.Time, authFinish time.Time)
51
52 // WithAuthorization passes all authorized requests on to handler, and returns a forbidden error otherwise.
53 func WithAuthorization(hhandler http.Handler, auth authorizer.Authorizer, s runtime.NegotiatedSerializer) http.Handler {
54 return withAuthorization(hhandler, auth, s, recordAuthorizationMetrics)
55 }
56
57 func withAuthorization(handler http.Handler, a authorizer.Authorizer, s runtime.NegotiatedSerializer, metrics recordAuthorizationMetricsFunc) http.Handler {
58 if a == nil {
59 klog.Warning("Authorization is disabled")
60 return handler
61 }
62 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
63 ctx := req.Context()
64 authorizationStart := time.Now()
65
66 attributes, err := GetAuthorizerAttributes(ctx)
67 if err != nil {
68 responsewriters.InternalError(w, req, err)
69 return
70 }
71 authorized, reason, err := a.Authorize(ctx, attributes)
72
73 authorizationFinish := time.Now()
74 defer func() {
75 metrics(ctx, authorized, err, authorizationStart, authorizationFinish)
76 request.TrackAuthorizationLatency(ctx, authorizationFinish.Sub(authorizationStart))
77 }()
78
79 // an authorizer like RBAC could encounter evaluation errors and still allow the request, so authorizer decision is checked before error here.
80 if authorized == authorizer.DecisionAllow {
81 audit.AddAuditAnnotations(ctx,
82 decisionAnnotationKey, decisionAllow,
83 reasonAnnotationKey, reason)
84 handler.ServeHTTP(w, req)
85 return
86 }
87 if err != nil {
88 audit.AddAuditAnnotation(ctx, reasonAnnotationKey, reasonError)
89 responsewriters.InternalError(w, req, err)
90 return
91 }
92
93 klog.V(4).InfoS("Forbidden", "URI", req.RequestURI, "reason", reason)
94 audit.AddAuditAnnotations(ctx,
95 decisionAnnotationKey, decisionForbid,
96 reasonAnnotationKey, reason)
97 responsewriters.Forbidden(ctx, attributes, w, req, reason, s)
98 })
99 }
100
101 func GetAuthorizerAttributes(ctx context.Context) (authorizer.Attributes, error) {
102 attribs := authorizer.AttributesRecord{}
103
104 user, ok := request.UserFrom(ctx)
105 if ok {
106 attribs.User = user
107 }
108
109 requestInfo, found := request.RequestInfoFrom(ctx)
110 if !found {
111 return nil, errors.New("no RequestInfo found in the context")
112 }
113
114 // Start with common attributes that apply to resource and non-resource requests
115 attribs.ResourceRequest = requestInfo.IsResourceRequest
116 attribs.Path = requestInfo.Path
117 attribs.Verb = requestInfo.Verb
118
119 attribs.APIGroup = requestInfo.APIGroup
120 attribs.APIVersion = requestInfo.APIVersion
121 attribs.Resource = requestInfo.Resource
122 attribs.Subresource = requestInfo.Subresource
123 attribs.Namespace = requestInfo.Namespace
124 attribs.Name = requestInfo.Name
125
126 if utilfeature.DefaultFeatureGate.Enabled(genericfeatures.AuthorizeWithSelectors) {
127 // parsing here makes it easy to keep the AttributesRecord type value-only and avoids any mutex copies when
128 // doing shallow copies in other steps.
129 if len(requestInfo.FieldSelector) > 0 {
130 fieldSelector, err := fields.ParseSelector(requestInfo.FieldSelector)
131 if err != nil {
132 attribs.FieldSelectorRequirements, attribs.FieldSelectorParsingErr = nil, err
133 } else {
134 if requirements := fieldSelector.Requirements(); len(requirements) > 0 {
135 attribs.FieldSelectorRequirements, attribs.FieldSelectorParsingErr = fieldSelector.Requirements(), nil
136 }
137 }
138 }
139
140 if len(requestInfo.LabelSelector) > 0 {
141 labelSelector, err := labels.Parse(requestInfo.LabelSelector)
142 if err != nil {
143 attribs.LabelSelectorRequirements, attribs.LabelSelectorParsingErr = nil, err
144 } else {
145 if requirements, _ /*selectable*/ := labelSelector.Requirements(); len(requirements) > 0 {
146 attribs.LabelSelectorRequirements, attribs.LabelSelectorParsingErr = requirements, nil
147 }
148 }
149 }
150 }
151
152 return &attribs, nil
153 }