rest.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 selfsubjectaccessreview
  14. import (
  15. "context"
  16. "fmt"
  17. apierrors "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 false
  35. }
  36. func (r *REST) New() runtime.Object {
  37. return &authorizationapi.SelfSubjectAccessReview{}
  38. }
  39. func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
  40. selfSAR, ok := obj.(*authorizationapi.SelfSubjectAccessReview)
  41. if !ok {
  42. return nil, apierrors.NewBadRequest(fmt.Sprintf("not a SelfSubjectAccessReview: %#v", obj))
  43. }
  44. if errs := authorizationvalidation.ValidateSelfSubjectAccessReview(selfSAR); len(errs) > 0 {
  45. return nil, apierrors.NewInvalid(authorizationapi.Kind(selfSAR.Kind), "", errs)
  46. }
  47. userToCheck, exists := genericapirequest.UserFrom(ctx)
  48. if !exists {
  49. return nil, apierrors.NewBadRequest("no user present on request")
  50. }
  51. if createValidation != nil {
  52. if err := createValidation(obj.DeepCopyObject()); err != nil {
  53. return nil, err
  54. }
  55. }
  56. var authorizationAttributes authorizer.AttributesRecord
  57. if selfSAR.Spec.ResourceAttributes != nil {
  58. authorizationAttributes = authorizationutil.ResourceAttributesFrom(userToCheck, *selfSAR.Spec.ResourceAttributes)
  59. } else {
  60. authorizationAttributes = authorizationutil.NonResourceAttributesFrom(userToCheck, *selfSAR.Spec.NonResourceAttributes)
  61. }
  62. decision, reason, evaluationErr := r.authorizer.Authorize(authorizationAttributes)
  63. selfSAR.Status = authorizationapi.SubjectAccessReviewStatus{
  64. Allowed: (decision == authorizer.DecisionAllow),
  65. Denied: (decision == authorizer.DecisionDeny),
  66. Reason: reason,
  67. }
  68. if evaluationErr != nil {
  69. selfSAR.Status.EvaluationError = evaluationErr.Error()
  70. }
  71. return selfSAR, nil
  72. }