...

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

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

     1  /*
     2  Copyright 2017 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 selfsubjectrulesreview
    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  )
    31  
    32  // REST implements a RESTStorage for selfsubjectrulesreview.
    33  type REST struct {
    34  	ruleResolver authorizer.RuleResolver
    35  }
    36  
    37  // NewREST returns a RESTStorage object that will work against selfsubjectrulesreview.
    38  func NewREST(ruleResolver authorizer.RuleResolver) *REST {
    39  	return &REST{ruleResolver}
    40  }
    41  
    42  // NamespaceScoped fulfill rest.Scoper
    43  func (r *REST) NamespaceScoped() bool {
    44  	return false
    45  }
    46  
    47  // New creates a new selfsubjectrulesreview object.
    48  func (r *REST) New() runtime.Object {
    49  	return &authorizationapi.SelfSubjectRulesReview{}
    50  }
    51  
    52  // Destroy cleans up resources on shutdown.
    53  func (r *REST) Destroy() {
    54  	// Given no underlying store, we don't destroy anything
    55  	// here explicitly.
    56  }
    57  
    58  // Create attempts to get self subject rules in specific namespace.
    59  func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
    60  	selfSRR, ok := obj.(*authorizationapi.SelfSubjectRulesReview)
    61  	if !ok {
    62  		return nil, apierrors.NewBadRequest(fmt.Sprintf("not a SelfSubjectRulesReview: %#v", obj))
    63  	}
    64  
    65  	user, ok := genericapirequest.UserFrom(ctx)
    66  	if !ok {
    67  		return nil, apierrors.NewBadRequest("no user present on request")
    68  	}
    69  
    70  	namespace := selfSRR.Spec.Namespace
    71  	if namespace == "" {
    72  		return nil, apierrors.NewBadRequest("no namespace on request")
    73  	}
    74  
    75  	if createValidation != nil {
    76  		if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
    77  			return nil, err
    78  		}
    79  	}
    80  
    81  	resourceInfo, nonResourceInfo, incomplete, err := r.ruleResolver.RulesFor(user, namespace)
    82  
    83  	ret := &authorizationapi.SelfSubjectRulesReview{
    84  		Status: authorizationapi.SubjectRulesReviewStatus{
    85  			ResourceRules:    getResourceRules(resourceInfo),
    86  			NonResourceRules: getNonResourceRules(nonResourceInfo),
    87  			Incomplete:       incomplete,
    88  		},
    89  	}
    90  
    91  	if err != nil {
    92  		ret.Status.EvaluationError = err.Error()
    93  	}
    94  
    95  	return ret, nil
    96  }
    97  
    98  var _ rest.SingularNameProvider = &REST{}
    99  
   100  func (r *REST) GetSingularName() string {
   101  	return "selfsubjectrulesreview"
   102  }
   103  
   104  func getResourceRules(infos []authorizer.ResourceRuleInfo) []authorizationapi.ResourceRule {
   105  	rules := make([]authorizationapi.ResourceRule, len(infos))
   106  	for i, info := range infos {
   107  		rules[i] = authorizationapi.ResourceRule{
   108  			Verbs:         info.GetVerbs(),
   109  			APIGroups:     info.GetAPIGroups(),
   110  			Resources:     info.GetResources(),
   111  			ResourceNames: info.GetResourceNames(),
   112  		}
   113  	}
   114  	return rules
   115  }
   116  
   117  func getNonResourceRules(infos []authorizer.NonResourceRuleInfo) []authorizationapi.NonResourceRule {
   118  	rules := make([]authorizationapi.NonResourceRule, len(infos))
   119  	for i, info := range infos {
   120  		rules[i] = authorizationapi.NonResourceRule{
   121  			Verbs:           info.GetVerbs(),
   122  			NonResourceURLs: info.GetNonResourceURLs(),
   123  		}
   124  	}
   125  	return rules
   126  }
   127  

View as plain text