storage.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 policybased implements a standard storage for RoleBinding that prevents privilege escalation.
  14. package policybased
  15. import (
  16. "context"
  17. rbacv1 "k8s.io/api/rbac/v1"
  18. "k8s.io/apimachinery/pkg/api/errors"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/apiserver/pkg/authorization/authorizer"
  22. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  23. "k8s.io/apiserver/pkg/registry/rest"
  24. kapihelper "k8s.io/kubernetes/pkg/apis/core/helper"
  25. "k8s.io/kubernetes/pkg/apis/rbac"
  26. rbacv1helpers "k8s.io/kubernetes/pkg/apis/rbac/v1"
  27. rbacregistry "k8s.io/kubernetes/pkg/registry/rbac"
  28. rbacregistryvalidation "k8s.io/kubernetes/pkg/registry/rbac/validation"
  29. )
  30. var groupResource = rbac.Resource("rolebindings")
  31. type Storage struct {
  32. rest.StandardStorage
  33. authorizer authorizer.Authorizer
  34. ruleResolver rbacregistryvalidation.AuthorizationRuleResolver
  35. }
  36. func NewStorage(s rest.StandardStorage, authorizer authorizer.Authorizer, ruleResolver rbacregistryvalidation.AuthorizationRuleResolver) *Storage {
  37. return &Storage{s, authorizer, ruleResolver}
  38. }
  39. func (r *Storage) NamespaceScoped() bool {
  40. return true
  41. }
  42. func (r *Storage) StorageVersion() runtime.GroupVersioner {
  43. svp, ok := r.StandardStorage.(rest.StorageVersionProvider)
  44. if !ok {
  45. return nil
  46. }
  47. return svp.StorageVersion()
  48. }
  49. var _ rest.StorageVersionProvider = &Storage{}
  50. func (s *Storage) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
  51. if rbacregistry.EscalationAllowed(ctx) {
  52. return s.StandardStorage.Create(ctx, obj, createValidation, options)
  53. }
  54. // Get the namespace from the context (populated from the URL).
  55. // The namespace in the object can be empty until StandardStorage.Create()->BeforeCreate() populates it from the context.
  56. namespace, ok := genericapirequest.NamespaceFrom(ctx)
  57. if !ok {
  58. return nil, errors.NewBadRequest("namespace is required")
  59. }
  60. roleBinding := obj.(*rbac.RoleBinding)
  61. if rbacregistry.BindingAuthorized(ctx, roleBinding.RoleRef, namespace, s.authorizer) {
  62. return s.StandardStorage.Create(ctx, obj, createValidation, options)
  63. }
  64. v1RoleRef := rbacv1.RoleRef{}
  65. err := rbacv1helpers.Convert_rbac_RoleRef_To_v1_RoleRef(&roleBinding.RoleRef, &v1RoleRef, nil)
  66. if err != nil {
  67. return nil, err
  68. }
  69. rules, err := s.ruleResolver.GetRoleReferenceRules(v1RoleRef, namespace)
  70. if err != nil {
  71. return nil, err
  72. }
  73. if err := rbacregistryvalidation.ConfirmNoEscalation(ctx, s.ruleResolver, rules); err != nil {
  74. return nil, errors.NewForbidden(groupResource, roleBinding.Name, err)
  75. }
  76. return s.StandardStorage.Create(ctx, obj, createValidation, options)
  77. }
  78. func (s *Storage) Update(ctx context.Context, name string, obj rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
  79. if rbacregistry.EscalationAllowed(ctx) {
  80. return s.StandardStorage.Update(ctx, name, obj, createValidation, updateValidation, forceAllowCreate, options)
  81. }
  82. nonEscalatingInfo := rest.WrapUpdatedObjectInfo(obj, func(ctx context.Context, obj runtime.Object, oldObj runtime.Object) (runtime.Object, error) {
  83. // Get the namespace from the context (populated from the URL).
  84. // The namespace in the object can be empty until StandardStorage.Update()->BeforeUpdate() populates it from the context.
  85. namespace, ok := genericapirequest.NamespaceFrom(ctx)
  86. if !ok {
  87. return nil, errors.NewBadRequest("namespace is required")
  88. }
  89. roleBinding := obj.(*rbac.RoleBinding)
  90. // if we're only mutating fields needed for the GC to eventually delete this obj, return
  91. if rbacregistry.IsOnlyMutatingGCFields(obj, oldObj, kapihelper.Semantic) {
  92. return obj, nil
  93. }
  94. // if we're explicitly authorized to bind this role, return
  95. if rbacregistry.BindingAuthorized(ctx, roleBinding.RoleRef, namespace, s.authorizer) {
  96. return obj, nil
  97. }
  98. // Otherwise, see if we already have all the permissions contained in the referenced role
  99. v1RoleRef := rbacv1.RoleRef{}
  100. err := rbacv1helpers.Convert_rbac_RoleRef_To_v1_RoleRef(&roleBinding.RoleRef, &v1RoleRef, nil)
  101. if err != nil {
  102. return nil, err
  103. }
  104. rules, err := s.ruleResolver.GetRoleReferenceRules(v1RoleRef, namespace)
  105. if err != nil {
  106. return nil, err
  107. }
  108. if err := rbacregistryvalidation.ConfirmNoEscalation(ctx, s.ruleResolver, rules); err != nil {
  109. return nil, errors.NewForbidden(groupResource, roleBinding.Name, err)
  110. }
  111. return obj, nil
  112. })
  113. return s.StandardStorage.Update(ctx, name, nonEscalatingInfo, createValidation, updateValidation, forceAllowCreate, options)
  114. }