...

Source file src/k8s.io/kubernetes/pkg/registry/authentication/selfsubjectreview/rest.go

Documentation: k8s.io/kubernetes/pkg/registry/authentication/selfsubjectreview

     1  /*
     2  Copyright 2022 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 selfsubjectreview
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
    28  	"k8s.io/apiserver/pkg/registry/rest"
    29  	authenticationapi "k8s.io/kubernetes/pkg/apis/authentication"
    30  )
    31  
    32  var _ interface {
    33  	rest.Creater
    34  	rest.NamespaceScopedStrategy
    35  	rest.Scoper
    36  	rest.Storage
    37  } = &REST{}
    38  
    39  // REST implements a RESTStorage for selfsubjectreviews.
    40  type REST struct {
    41  }
    42  
    43  // NewREST returns a RESTStorage object that will work against selfsubjectreviews.
    44  func NewREST() *REST {
    45  	return &REST{}
    46  }
    47  
    48  // NamespaceScoped fulfill rest.Scoper
    49  func (r *REST) NamespaceScoped() bool {
    50  	return false
    51  }
    52  
    53  // New creates a new selfsubjectreview object.
    54  func (r *REST) New() runtime.Object {
    55  	return &authenticationapi.SelfSubjectReview{}
    56  }
    57  
    58  // Destroy cleans up resources on shutdown.
    59  func (r *REST) Destroy() {
    60  	// Given no underlying store, we don't destroy anything
    61  	// here explicitly.
    62  }
    63  
    64  // Create returns attributes of the subject making the request.
    65  func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
    66  	if createValidation != nil {
    67  		if err := createValidation(ctx, obj.DeepCopyObject()); err != nil {
    68  			return nil, err
    69  		}
    70  	}
    71  
    72  	_, ok := obj.(*authenticationapi.SelfSubjectReview)
    73  	if !ok {
    74  		return nil, apierrors.NewBadRequest(fmt.Sprintf("not a SelfSubjectReview: %#v", obj))
    75  	}
    76  
    77  	user, ok := genericapirequest.UserFrom(ctx)
    78  	if !ok {
    79  		return nil, apierrors.NewBadRequest("no user present on request")
    80  	}
    81  
    82  	extra := user.GetExtra()
    83  
    84  	selfSR := &authenticationapi.SelfSubjectReview{
    85  		ObjectMeta: metav1.ObjectMeta{
    86  			CreationTimestamp: metav1.NewTime(time.Now()),
    87  		},
    88  		Status: authenticationapi.SelfSubjectReviewStatus{
    89  			UserInfo: authenticationapi.UserInfo{
    90  				Username: user.GetName(),
    91  				UID:      user.GetUID(),
    92  				Groups:   user.GetGroups(),
    93  				Extra:    make(map[string]authenticationapi.ExtraValue, len(extra)),
    94  			},
    95  		},
    96  	}
    97  	for key, attr := range extra {
    98  		selfSR.Status.UserInfo.Extra[key] = attr
    99  	}
   100  
   101  	return selfSR, nil
   102  }
   103  
   104  var _ rest.SingularNameProvider = &REST{}
   105  
   106  func (r *REST) GetSingularName() string {
   107  	return "selfsubjectreview"
   108  }
   109  

View as plain text