-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
self_subject_reviews.go
338 lines (307 loc) · 12.1 KB
/
self_subject_reviews.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package proxy
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"path"
"slices"
"github.com/gravitational/trace"
"github.com/julienschmidt/httprouter"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
oteltrace "go.opentelemetry.io/otel/trace"
authv1 "k8s.io/api/authorization/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/httplib"
"github.com/gravitational/teleport/lib/kube/proxy/responsewriters"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
)
// selfSubjectAccessReviews intercepts self subject access reviews requests and pre-validates
// them by applying the kubernetes resources RBAC rules to the request.
// If the self subject access review is allowed, the request is forwarded to the
// kubernetes API server for final validation.
func (f *Forwarder) selfSubjectAccessReviews(authCtx *authContext, w http.ResponseWriter, req *http.Request, p httprouter.Params) (resp any, err error) {
ctx, span := f.cfg.tracer.Start(
req.Context(),
"kube.Forwarder/selfSubjectAccessReviews",
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
oteltrace.WithAttributes(
semconv.RPCServiceKey.String(f.cfg.KubeServiceType),
semconv.RPCMethodKey.String("selfSubjectAccessReviews"),
semconv.RPCSystemKey.String("kube"),
),
)
req = req.WithContext(ctx)
defer span.End()
sess, err := f.newClusterSession(req.Context(), *authCtx)
if err != nil {
// This error goes to kubernetes client and is not visible in the logs
// of the teleport server if not logged here.
f.log.Errorf("Failed to create cluster session: %v.", err)
return nil, trace.Wrap(err)
}
// sess.Close cancels the connection monitor context to release it sooner.
// When the server is under heavy load it can take a while to identify that
// the underlying connection is gone. This change prevents that and releases
// the resources as soon as we know the session is no longer active.
defer sess.close()
sess.upgradeToHTTP2 = true
sess.forwarder, err = f.makeSessionForwarder(sess)
if err != nil {
return nil, trace.Wrap(err)
}
// only allow self subject access reviews for the service that proxies the
// request to the kubernetes API server.
if f.isLocalKubeCluster(sess.teleportCluster.isRemote, sess.kubeClusterName) {
if err := f.validateSelfSubjectAccessReview(sess, w, req); trace.IsAccessDenied(err) {
return nil, nil
} else if err != nil {
return nil, trace.Wrap(err)
}
}
if err := f.setupForwardingHeaders(sess, req, true /* withImpersonationHeaders */); err != nil {
// This error goes to kubernetes client and is not visible in the logs
// of the teleport server if not logged here.
f.log.Errorf("Failed to set up forwarding headers: %v.", err)
return nil, trace.Wrap(err)
}
rw := httplib.NewResponseStatusRecorder(w)
sess.forwarder.ServeHTTP(rw, req)
f.emitAuditEvent(req, sess, rw.Status())
return nil, nil
}
// validateSelfSubjectAccessReview validates the self subject access review
// request by applying the kubernetes resources RBAC rules to the request.
func (f *Forwarder) validateSelfSubjectAccessReview(sess *clusterSession, w http.ResponseWriter, req *http.Request) error {
negotiator := newClientNegotiator(sess.codecFactory)
encoder, decoder, err := newEncoderAndDecoderForContentType(responsewriters.GetContentTypeHeader(req.Header), negotiator)
if err != nil {
return trace.Wrap(err)
}
accessReview, err := parseSelfSubjectAccessReviewRequest(decoder, req)
if err != nil {
return trace.Wrap(err)
}
if accessReview.Spec.ResourceAttributes == nil {
return nil
}
namespace := accessReview.Spec.ResourceAttributes.Namespace
resource := sess.rbacSupportedResources.getResourceWithKey(
allowedResourcesKey{
apiGroup: accessReview.Spec.ResourceAttributes.Group,
resourceKind: accessReview.Spec.ResourceAttributes.Resource,
},
)
// If the request is for a resource that Teleport does not support, return
// nil and let the Kubernetes API server handle the request.
if resource == "" {
return nil
}
name := accessReview.Spec.ResourceAttributes.Name
authPref, err := f.cfg.CachingAuthClient.GetAuthPreference(req.Context())
if err != nil {
return trace.Wrap(err)
}
actx := sess.authContext
state := actx.GetAccessState(authPref)
switch err := actx.Checker.CheckAccess(
actx.kubeCluster,
state,
services.RoleMatchers{
// Append a matcher that validates if the Kubernetes resource is allowed
// by the roles that satisfy the Kubernetes Cluster.
&kubernetesResourceMatcher{
types.KubernetesResource{
Kind: resource,
Name: name,
Namespace: namespace,
Verbs: []string{accessReview.Spec.ResourceAttributes.Verb},
},
},
}...); {
case errors.Is(err, services.ErrTrustedDeviceRequired):
return trace.Wrap(err)
case err != nil &&
!slices.Contains(types.KubernetesClusterWideResourceKinds, resource) &&
resource != utils.KubeCustomResource:
namespaceNameToString := func(namespace, name string) string {
switch {
case namespace == "" && name == "":
return ""
case namespace != "" && name != "":
return path.Join(namespace, name)
case namespace != "":
return path.Join(namespace, "*")
default:
return path.Join("*", name)
}
}
accessReview.Status = authv1.SubjectAccessReviewStatus{
Allowed: false,
Denied: true,
Reason: fmt.Sprintf(
"access to %s %s denied by Teleport: please ensure that %q field in your Teleport role defines access to the desired resource.\n\n"+
"Valid example:\n"+
"kubernetes_resources:\n"+
"- kind: %s\n"+
" name: %s\n"+
" namespace: %s\n"+
" verbs: [%s]\n", accessReview.Spec.ResourceAttributes.Resource, namespaceNameToString(namespace, name), kubernetesResourcesKey, resource, emptyOrWildcard(name), emptyOrWildcard(namespace), emptyOrWildcard("")),
}
responsewriters.SetContentTypeHeader(w, req.Header)
if encodeErr := encoder.Encode(accessReview, w); encodeErr != nil {
return trace.Wrap(encodeErr)
}
return trace.Wrap(err)
case err != nil && resource == utils.KubeCustomResource:
// If the request is for a custom resource, we need grant access to the
// the namespace that the custom resource is in.
resource = types.KindKubeNamespace
name = namespace
fallthrough
case err != nil:
// If the request is for a cluster-wide resource, we need to grant access
// to it.
accessReview.Status = authv1.SubjectAccessReviewStatus{
Allowed: false,
Denied: true,
Reason: fmt.Sprintf(
"access to %s %s denied by Teleport: please ensure that %q field in your Teleport role defines access to the desired resource.\n\n"+
"Valid example:\n"+
"kubernetes_resources:\n"+
"- kind: %s\n"+
" name: %s\n"+
" verbs: [%s]\n", accessReview.Spec.ResourceAttributes.Resource, name, kubernetesResourcesKey, resource, emptyOrWildcard(name), emptyOrWildcard("")),
}
responsewriters.SetContentTypeHeader(w, req.Header)
if encodeErr := encoder.Encode(accessReview, w); encodeErr != nil {
return trace.Wrap(encodeErr)
}
return trace.Wrap(err)
}
return nil
}
// emptyOrWildcard returns the string s if it is not empty, otherwise it returns
// '*'.
func emptyOrWildcard(s string) string {
if s == "" {
return fmt.Sprintf("'%s'", types.Wildcard)
}
return s
}
// parseSelfSubjectAccessReviewRequest parses the request body into a SelfSubjectAccessReview object
// and replaces the body so it can be read again.
func parseSelfSubjectAccessReviewRequest(decoder runtime.Decoder, req *http.Request) (*authv1.SelfSubjectAccessReview, error) {
payload, err := io.ReadAll(req.Body)
if err != nil {
return nil, trace.Wrap(err)
}
req.Body.Close()
req.Body = io.NopCloser(bytes.NewReader(payload))
gvk := authv1.SchemeGroupVersion.WithKind("SelfSubjectAccessReview")
obj, err := decodeAndSetGVK(decoder, payload, &gvk)
if err != nil {
return nil, trace.Wrap(err)
}
switch o := obj.(type) {
case *authv1.SelfSubjectAccessReview:
return o, nil
default:
return nil, trace.BadParameter("unexpected object type: %T", obj)
}
}
// kubernetesResourceMatcher matches a role against a Kubernetes Resource.
// This matcher is different form services.KubernetesResourceMatcher because
// if skips some validations if the user doesn't ask for a specific resource.
// If name and namespace are empty, it means that the user wants to match all
// resources of the specified kind for which the user might have access to.
// If the user asks for name="", namespace="" and the role has a matcher
// with name="foo", namespace="bar", the matcher will match but the user
// might not be able to see any resource if the resource does not exist
// in the cluster.
// This matcher assumes the role's kubernetes_resources configured eventually
// match with resources that exist in the cluster.
type kubernetesResourceMatcher struct {
resource types.KubernetesResource
}
// Match matches a Kubernetes Resource against provided role and condition.
func (m *kubernetesResourceMatcher) Match(role types.Role, condition types.RoleConditionType) (bool, error) {
resources := role.GetKubeResources(condition)
if len(resources) == 0 {
return false, nil
}
kind := m.resource.Kind
name := m.resource.Name
namespace := m.resource.Namespace
if kind == utils.KubeCustomResource {
kind = types.KindKubeNamespace
name = m.resource.Namespace
namespace = ""
}
for _, resource := range resources {
isResourceTheSameKind := kind == resource.Kind || resource.Kind == types.Wildcard
namespaceScopeMatch := resource.Kind == types.KindKubeNamespace && !slices.Contains(types.KubernetesClusterWideResourceKinds, kind)
if !isResourceTheSameKind && !namespaceScopeMatch {
continue
}
if len(m.resource.Verbs) == 1 && !isVerbAllowed(resource.Verbs, m.resource.Verbs[0]) {
continue
}
// If the resource name and namespace are empty, it means that the
// user wants to match all resources of the specified kind.
// We can return true immediately because the user is allowed to get resources
// of the specified kind but might not be able to see any if the matchers do not
// match with any resource.
if name == "" && namespace == "" {
return true, nil
}
// If the resource name isn't empty but the resource kind is a namespace scope
// match - i.e. the resource.Kind==types.KindKubeNamespace and the desired
// resource kind is not a cluster-wide resource - we should skip the resource
// name validation.
if name != "" && !namespaceScopeMatch {
switch ok, err := utils.SliceMatchesRegex(name, []string{resource.Name}); {
case err != nil:
return false, trace.Wrap(err)
case !ok:
continue
}
}
if resource.Kind == types.KindKubeNamespace && namespace != "" {
if ok, err := utils.SliceMatchesRegex(namespace, []string{resource.Name}); err != nil || ok {
return ok, trace.Wrap(err)
}
} else {
if ok, err := utils.SliceMatchesRegex(namespace, []string{resource.Namespace}); err != nil || ok || namespace == "" {
return ok || namespace == "", trace.Wrap(err)
}
}
}
return false, nil
}
// isVerbAllowed returns true if the verb is allowed in the resource.
// If the resource has a wildcard verb, it matches all verbs, otherwise
// the resource must have the verb we're looking for.
func isVerbAllowed(allowedVerbs []string, verb string) bool {
return len(allowedVerbs) != 0 && (allowedVerbs[0] == types.Wildcard || slices.Contains(allowedVerbs, verb))
}