storage.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 ClusterRole that prevents privilege escalation.
  14. package policybased
  15. import (
  16. "context"
  17. "errors"
  18. apierrors "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. rbacregistry "k8s.io/kubernetes/pkg/registry/rbac"
  26. rbacregistryvalidation "k8s.io/kubernetes/pkg/registry/rbac/validation"
  27. )
  28. var groupResource = rbac.Resource("clusterroles")
  29. type Storage struct {
  30. rest.StandardStorage
  31. authorizer authorizer.Authorizer
  32. ruleResolver rbacregistryvalidation.AuthorizationRuleResolver
  33. }
  34. func NewStorage(s rest.StandardStorage, authorizer authorizer.Authorizer, ruleResolver rbacregistryvalidation.AuthorizationRuleResolver) *Storage {
  35. return &Storage{s, authorizer, ruleResolver}
  36. }
  37. func (r *Storage) NamespaceScoped() bool {
  38. return false
  39. }
  40. func (r *Storage) StorageVersion() runtime.GroupVersioner {
  41. svp, ok := r.StandardStorage.(rest.StorageVersionProvider)
  42. if !ok {
  43. return nil
  44. }
  45. return svp.StorageVersion()
  46. }
  47. var _ rest.StorageVersionProvider = &Storage{}
  48. var fullAuthority = []rbac.PolicyRule{
  49. rbac.NewRule("*").Groups("*").Resources("*").RuleOrDie(),
  50. rbac.NewRule("*").URLs("*").RuleOrDie(),
  51. }
  52. func (s *Storage) Create(ctx context.Context, obj runtime.Object, createValidatingAdmission rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
  53. if rbacregistry.EscalationAllowed(ctx) || rbacregistry.RoleEscalationAuthorized(ctx, s.authorizer) {
  54. return s.StandardStorage.Create(ctx, obj, createValidatingAdmission, options)
  55. }
  56. clusterRole := obj.(*rbac.ClusterRole)
  57. rules := clusterRole.Rules
  58. if err := rbacregistryvalidation.ConfirmNoEscalationInternal(ctx, s.ruleResolver, rules); err != nil {
  59. return nil, apierrors.NewForbidden(groupResource, clusterRole.Name, err)
  60. }
  61. // to set the aggregation rule, since it can gather anything, requires * on *.*
  62. if hasAggregationRule(clusterRole) {
  63. if err := rbacregistryvalidation.ConfirmNoEscalationInternal(ctx, s.ruleResolver, fullAuthority); err != nil {
  64. return nil, apierrors.NewForbidden(groupResource, clusterRole.Name, errors.New("must have cluster-admin privileges to use the aggregationRule"))
  65. }
  66. }
  67. return s.StandardStorage.Create(ctx, obj, createValidatingAdmission, options)
  68. }
  69. 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) {
  70. if rbacregistry.EscalationAllowed(ctx) || rbacregistry.RoleEscalationAuthorized(ctx, s.authorizer) {
  71. return s.StandardStorage.Update(ctx, name, obj, createValidation, updateValidation, forceAllowCreate, options)
  72. }
  73. nonEscalatingInfo := rest.WrapUpdatedObjectInfo(obj, func(ctx context.Context, obj runtime.Object, oldObj runtime.Object) (runtime.Object, error) {
  74. clusterRole := obj.(*rbac.ClusterRole)
  75. oldClusterRole := oldObj.(*rbac.ClusterRole)
  76. // if we're only mutating fields needed for the GC to eventually delete this obj, return
  77. if rbacregistry.IsOnlyMutatingGCFields(obj, oldObj, kapihelper.Semantic) {
  78. return obj, nil
  79. }
  80. rules := clusterRole.Rules
  81. if err := rbacregistryvalidation.ConfirmNoEscalationInternal(ctx, s.ruleResolver, rules); err != nil {
  82. return nil, apierrors.NewForbidden(groupResource, clusterRole.Name, err)
  83. }
  84. // to change the aggregation rule, since it can gather anything and prevent tightening, requires * on *.*
  85. if hasAggregationRule(clusterRole) || hasAggregationRule(oldClusterRole) {
  86. if err := rbacregistryvalidation.ConfirmNoEscalationInternal(ctx, s.ruleResolver, fullAuthority); err != nil {
  87. return nil, apierrors.NewForbidden(groupResource, clusterRole.Name, errors.New("must have cluster-admin privileges to use the aggregationRule"))
  88. }
  89. }
  90. return obj, nil
  91. })
  92. return s.StandardStorage.Update(ctx, name, nonEscalatingInfo, createValidation, updateValidation, forceAllowCreate, options)
  93. }
  94. func hasAggregationRule(clusterRole *rbac.ClusterRole) bool {
  95. return clusterRole.AggregationRule != nil && len(clusterRole.AggregationRule.ClusterRoleSelectors) > 0
  96. }