...

Source file src/k8s.io/kubernetes/pkg/registry/authorization/subjectaccessreview/rest.go

Documentation: k8s.io/kubernetes/pkg/registry/authorization/subjectaccessreview

     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 subjectaccessreview
    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  	"k8s.io/apiserver/pkg/registry/rest"
    28  	authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
    29  	authorizationvalidation "k8s.io/kubernetes/pkg/apis/authorization/validation"
    30  	authorizationutil "k8s.io/kubernetes/pkg/registry/authorization/util"
    31  )
    32  
    33  type REST struct {
    34  	authorizer authorizer.Authorizer
    35  }
    36  
    37  func NewREST(authorizer authorizer.Authorizer) *REST {
    38  	return &REST{authorizer}
    39  }
    40  
    41  func (r *REST) NamespaceScoped() bool {
    42  	return false
    43  }
    44  
    45  func (r *REST) New() runtime.Object {
    46  	return &authorizationapi.SubjectAccessReview{}
    47  }
    48  
    49  // Destroy cleans up resources on shutdown.
    50  func (r *REST) Destroy() {
    51  	// Given no underlying store, we don't destroy anything
    52  	// here explicitly.
    53  }
    54  
    55  var _ rest.SingularNameProvider = &REST{}
    56  
    57  func (r *REST) GetSingularName() string {
    58  	return "subjectaccessreview"
    59  }
    60  
    61  func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
    62  	subjectAccessReview, ok := obj.(*authorizationapi.SubjectAccessReview)
    63  	if !ok {
    64  		return nil, apierrors.NewBadRequest(fmt.Sprintf("not a SubjectAccessReview: %#v", obj))
    65  	}
    66  	if errs := authorizationvalidation.ValidateSubjectAccessReview(subjectAccessReview); len(errs) > 0 {
    67  		return nil, apierrors.NewInvalid(authorizationapi.Kind(subjectAccessReview.Kind), "", errs)
    68  	}
    69  
    70  	if createValidation != nil {
    71  		if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
    72  			return nil, err
    73  		}
    74  	}
    75  
    76  	authorizationAttributes := authorizationutil.AuthorizationAttributesFrom(subjectAccessReview.Spec)
    77  	decision, reason, evaluationErr := r.authorizer.Authorize(ctx, authorizationAttributes)
    78  
    79  	subjectAccessReview.Status = authorizationapi.SubjectAccessReviewStatus{
    80  		Allowed: (decision == authorizer.DecisionAllow),
    81  		Denied:  (decision == authorizer.DecisionDeny),
    82  		Reason:  reason,
    83  	}
    84  	if evaluationErr != nil {
    85  		subjectAccessReview.Status.EvaluationError = evaluationErr.Error()
    86  	}
    87  
    88  	return subjectAccessReview, nil
    89  }
    90  

View as plain text