rest.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package localsubjectaccessreview
  14. import (
  15. "context"
  16. "fmt"
  17. kapierrors "k8s.io/apimachinery/pkg/api/errors"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apiserver/pkg/authorization/authorizer"
  21. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  22. "k8s.io/apiserver/pkg/registry/rest"
  23. authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
  24. authorizationvalidation "k8s.io/kubernetes/pkg/apis/authorization/validation"
  25. authorizationutil "k8s.io/kubernetes/pkg/registry/authorization/util"
  26. )
  27. type REST struct {
  28. authorizer authorizer.Authorizer
  29. }
  30. func NewREST(authorizer authorizer.Authorizer) *REST {
  31. return &REST{authorizer}
  32. }
  33. func (r *REST) NamespaceScoped() bool {
  34. return true
  35. }
  36. func (r *REST) New() runtime.Object {
  37. return &authorizationapi.LocalSubjectAccessReview{}
  38. }
  39. func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
  40. localSubjectAccessReview, ok := obj.(*authorizationapi.LocalSubjectAccessReview)
  41. if !ok {
  42. return nil, kapierrors.NewBadRequest(fmt.Sprintf("not a LocaLocalSubjectAccessReview: %#v", obj))
  43. }
  44. if errs := authorizationvalidation.ValidateLocalSubjectAccessReview(localSubjectAccessReview); len(errs) > 0 {
  45. return nil, kapierrors.NewInvalid(authorizationapi.Kind(localSubjectAccessReview.Kind), "", errs)
  46. }
  47. namespace := genericapirequest.NamespaceValue(ctx)
  48. if len(namespace) == 0 {
  49. return nil, kapierrors.NewBadRequest(fmt.Sprintf("namespace is required on this type: %v", namespace))
  50. }
  51. if namespace != localSubjectAccessReview.Namespace {
  52. return nil, kapierrors.NewBadRequest(fmt.Sprintf("spec.resourceAttributes.namespace must match namespace: %v", namespace))
  53. }
  54. if createValidation != nil {
  55. if err := createValidation(obj.DeepCopyObject()); err != nil {
  56. return nil, err
  57. }
  58. }
  59. authorizationAttributes := authorizationutil.AuthorizationAttributesFrom(localSubjectAccessReview.Spec)
  60. decision, reason, evaluationErr := r.authorizer.Authorize(authorizationAttributes)
  61. localSubjectAccessReview.Status = authorizationapi.SubjectAccessReviewStatus{
  62. Allowed: (decision == authorizer.DecisionAllow),
  63. Denied: (decision == authorizer.DecisionDeny),
  64. Reason: reason,
  65. }
  66. if evaluationErr != nil {
  67. localSubjectAccessReview.Status.EvaluationError = evaluationErr.Error()
  68. }
  69. return localSubjectAccessReview, nil
  70. }