k8s.io/kubernetes@v1.29.3/pkg/registry/authorization/localsubjectaccessreview/rest.go (about) 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 localsubjectaccessreview 18 19 import ( 20 "context" 21 "fmt" 22 23 apierrors "k8s.io/apimachinery/pkg/api/errors" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/apiserver/pkg/authorization/authorizer" 27 genericapirequest "k8s.io/apiserver/pkg/endpoints/request" 28 "k8s.io/apiserver/pkg/registry/rest" 29 authorizationapi "k8s.io/kubernetes/pkg/apis/authorization" 30 authorizationvalidation "k8s.io/kubernetes/pkg/apis/authorization/validation" 31 authorizationutil "k8s.io/kubernetes/pkg/registry/authorization/util" 32 ) 33 34 type REST struct { 35 authorizer authorizer.Authorizer 36 } 37 38 func NewREST(authorizer authorizer.Authorizer) *REST { 39 return &REST{authorizer} 40 } 41 42 func (r *REST) NamespaceScoped() bool { 43 return true 44 } 45 46 func (r *REST) New() runtime.Object { 47 return &authorizationapi.LocalSubjectAccessReview{} 48 } 49 50 // Destroy cleans up resources on shutdown. 51 func (r *REST) Destroy() { 52 // Given no underlying store, we don't destroy anything 53 // here explicitly. 54 } 55 56 var _ rest.SingularNameProvider = &REST{} 57 58 func (r *REST) GetSingularName() string { 59 return "localsubjectaccessreview" 60 } 61 62 func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) { 63 localSubjectAccessReview, ok := obj.(*authorizationapi.LocalSubjectAccessReview) 64 if !ok { 65 return nil, apierrors.NewBadRequest(fmt.Sprintf("not a LocaLocalSubjectAccessReview: %#v", obj)) 66 } 67 if errs := authorizationvalidation.ValidateLocalSubjectAccessReview(localSubjectAccessReview); len(errs) > 0 { 68 return nil, apierrors.NewInvalid(authorizationapi.Kind(localSubjectAccessReview.Kind), "", errs) 69 } 70 namespace := genericapirequest.NamespaceValue(ctx) 71 if len(namespace) == 0 { 72 return nil, apierrors.NewBadRequest(fmt.Sprintf("namespace is required on this type: %v", namespace)) 73 } 74 if namespace != localSubjectAccessReview.Namespace { 75 return nil, apierrors.NewBadRequest(fmt.Sprintf("spec.resourceAttributes.namespace must match namespace: %v", namespace)) 76 } 77 78 if createValidation != nil { 79 if err := createValidation(ctx, obj.DeepCopyObject()); err != nil { 80 return nil, err 81 } 82 } 83 84 authorizationAttributes := authorizationutil.AuthorizationAttributesFrom(localSubjectAccessReview.Spec) 85 decision, reason, evaluationErr := r.authorizer.Authorize(ctx, authorizationAttributes) 86 87 localSubjectAccessReview.Status = authorizationapi.SubjectAccessReviewStatus{ 88 Allowed: (decision == authorizer.DecisionAllow), 89 Denied: (decision == authorizer.DecisionDeny), 90 Reason: reason, 91 } 92 if evaluationErr != nil { 93 localSubjectAccessReview.Status.EvaluationError = evaluationErr.Error() 94 } 95 96 return localSubjectAccessReview, nil 97 }