storage.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Role that prevents privilege escalation.
  14. package policybased
  15. import (
  16. "context"
  17. "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. "k8s.io/apiserver/pkg/registry/rest"
  22. kapihelper "k8s.io/kubernetes/pkg/apis/core/helper"
  23. "k8s.io/kubernetes/pkg/apis/rbac"
  24. rbacregistry "k8s.io/kubernetes/pkg/registry/rbac"
  25. rbacregistryvalidation "k8s.io/kubernetes/pkg/registry/rbac/validation"
  26. )
  27. var groupResource = rbac.Resource("roles")
  28. type Storage struct {
  29. rest.StandardStorage
  30. authorizer authorizer.Authorizer
  31. ruleResolver rbacregistryvalidation.AuthorizationRuleResolver
  32. }
  33. func NewStorage(s rest.StandardStorage, authorizer authorizer.Authorizer, ruleResolver rbacregistryvalidation.AuthorizationRuleResolver) *Storage {
  34. return &Storage{s, authorizer, ruleResolver}
  35. }
  36. func (r *Storage) NamespaceScoped() bool {
  37. return true
  38. }
  39. func (r *Storage) StorageVersion() runtime.GroupVersioner {
  40. svp, ok := r.StandardStorage.(rest.StorageVersionProvider)
  41. if !ok {
  42. return nil
  43. }
  44. return svp.StorageVersion()
  45. }
  46. var _ rest.StorageVersionProvider = &Storage{}
  47. func (s *Storage) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
  48. if rbacregistry.EscalationAllowed(ctx) || rbacregistry.RoleEscalationAuthorized(ctx, s.authorizer) {
  49. return s.StandardStorage.Create(ctx, obj, createValidation, options)
  50. }
  51. role := obj.(*rbac.Role)
  52. rules := role.Rules
  53. if err := rbacregistryvalidation.ConfirmNoEscalationInternal(ctx, s.ruleResolver, rules); err != nil {
  54. return nil, errors.NewForbidden(groupResource, role.Name, err)
  55. }
  56. return s.StandardStorage.Create(ctx, obj, createValidation, options)
  57. }
  58. 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) {
  59. if rbacregistry.EscalationAllowed(ctx) || rbacregistry.RoleEscalationAuthorized(ctx, s.authorizer) {
  60. return s.StandardStorage.Update(ctx, name, obj, createValidation, updateValidation, forceAllowCreate, options)
  61. }
  62. nonEscalatingInfo := rest.WrapUpdatedObjectInfo(obj, func(ctx context.Context, obj runtime.Object, oldObj runtime.Object) (runtime.Object, error) {
  63. role := obj.(*rbac.Role)
  64. // if we're only mutating fields needed for the GC to eventually delete this obj, return
  65. if rbacregistry.IsOnlyMutatingGCFields(obj, oldObj, kapihelper.Semantic) {
  66. return obj, nil
  67. }
  68. rules := role.Rules
  69. if err := rbacregistryvalidation.ConfirmNoEscalationInternal(ctx, s.ruleResolver, rules); err != nil {
  70. return nil, errors.NewForbidden(groupResource, role.Name, err)
  71. }
  72. return obj, nil
  73. })
  74. return s.StandardStorage.Update(ctx, name, nonEscalatingInfo, createValidation, updateValidation, forceAllowCreate, options)
  75. }