storage.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 ClusterRoleBinding 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. "k8s.io/apiserver/pkg/registry/rest"
  23. kapihelper "k8s.io/kubernetes/pkg/apis/core/helper"
  24. "k8s.io/kubernetes/pkg/apis/rbac"
  25. rbacv1helpers "k8s.io/kubernetes/pkg/apis/rbac/v1"
  26. rbacregistry "k8s.io/kubernetes/pkg/registry/rbac"
  27. rbacregistryvalidation "k8s.io/kubernetes/pkg/registry/rbac/validation"
  28. )
  29. var groupResource = rbac.Resource("clusterrolebindings")
  30. type Storage struct {
  31. rest.StandardStorage
  32. authorizer authorizer.Authorizer
  33. ruleResolver rbacregistryvalidation.AuthorizationRuleResolver
  34. }
  35. func NewStorage(s rest.StandardStorage, authorizer authorizer.Authorizer, ruleResolver rbacregistryvalidation.AuthorizationRuleResolver) *Storage {
  36. return &Storage{s, authorizer, ruleResolver}
  37. }
  38. func (r *Storage) NamespaceScoped() bool {
  39. return false
  40. }
  41. func (r *Storage) StorageVersion() runtime.GroupVersioner {
  42. svp, ok := r.StandardStorage.(rest.StorageVersionProvider)
  43. if !ok {
  44. return nil
  45. }
  46. return svp.StorageVersion()
  47. }
  48. var _ rest.StorageVersionProvider = &Storage{}
  49. func (s *Storage) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
  50. if rbacregistry.EscalationAllowed(ctx) {
  51. return s.StandardStorage.Create(ctx, obj, createValidation, options)
  52. }
  53. clusterRoleBinding := obj.(*rbac.ClusterRoleBinding)
  54. if rbacregistry.BindingAuthorized(ctx, clusterRoleBinding.RoleRef, metav1.NamespaceNone, s.authorizer) {
  55. return s.StandardStorage.Create(ctx, obj, createValidation, options)
  56. }
  57. v1RoleRef := rbacv1.RoleRef{}
  58. err := rbacv1helpers.Convert_rbac_RoleRef_To_v1_RoleRef(&clusterRoleBinding.RoleRef, &v1RoleRef, nil)
  59. if err != nil {
  60. return nil, err
  61. }
  62. rules, err := s.ruleResolver.GetRoleReferenceRules(v1RoleRef, metav1.NamespaceNone)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if err := rbacregistryvalidation.ConfirmNoEscalation(ctx, s.ruleResolver, rules); err != nil {
  67. return nil, errors.NewForbidden(groupResource, clusterRoleBinding.Name, err)
  68. }
  69. return s.StandardStorage.Create(ctx, obj, createValidation, options)
  70. }
  71. 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) {
  72. if rbacregistry.EscalationAllowed(ctx) {
  73. return s.StandardStorage.Update(ctx, name, obj, createValidation, updateValidation, forceAllowCreate, options)
  74. }
  75. nonEscalatingInfo := rest.WrapUpdatedObjectInfo(obj, func(ctx context.Context, obj runtime.Object, oldObj runtime.Object) (runtime.Object, error) {
  76. clusterRoleBinding := obj.(*rbac.ClusterRoleBinding)
  77. // if we're only mutating fields needed for the GC to eventually delete this obj, return
  78. if rbacregistry.IsOnlyMutatingGCFields(obj, oldObj, kapihelper.Semantic) {
  79. return obj, nil
  80. }
  81. // if we're explicitly authorized to bind this clusterrole, return
  82. if rbacregistry.BindingAuthorized(ctx, clusterRoleBinding.RoleRef, metav1.NamespaceNone, s.authorizer) {
  83. return obj, nil
  84. }
  85. // Otherwise, see if we already have all the permissions contained in the referenced clusterrole
  86. v1RoleRef := rbacv1.RoleRef{}
  87. err := rbacv1helpers.Convert_rbac_RoleRef_To_v1_RoleRef(&clusterRoleBinding.RoleRef, &v1RoleRef, nil)
  88. if err != nil {
  89. return nil, err
  90. }
  91. rules, err := s.ruleResolver.GetRoleReferenceRules(v1RoleRef, metav1.NamespaceNone)
  92. if err != nil {
  93. return nil, err
  94. }
  95. if err := rbacregistryvalidation.ConfirmNoEscalation(ctx, s.ruleResolver, rules); err != nil {
  96. return nil, errors.NewForbidden(groupResource, clusterRoleBinding.Name, err)
  97. }
  98. return obj, nil
  99. })
  100. return s.StandardStorage.Update(ctx, name, nonEscalatingInfo, createValidation, updateValidation, forceAllowCreate, options)
  101. }