subject_locator.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 rbac implements the authorizer.Authorizer interface using roles base access control.
  14. package rbac
  15. import (
  16. rbacv1 "k8s.io/api/rbac/v1"
  17. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  18. "k8s.io/apiserver/pkg/authentication/user"
  19. "k8s.io/apiserver/pkg/authorization/authorizer"
  20. rbacregistryvalidation "k8s.io/kubernetes/pkg/registry/rbac/validation"
  21. )
  22. type RoleToRuleMapper interface {
  23. // GetRoleReferenceRules attempts to resolve the role reference of a RoleBinding or ClusterRoleBinding. The passed namespace should be the namespace
  24. // of the role binding, the empty string if a cluster role binding.
  25. GetRoleReferenceRules(roleRef rbacv1.RoleRef, namespace string) ([]rbacv1.PolicyRule, error)
  26. }
  27. type SubjectLocator interface {
  28. AllowedSubjects(attributes authorizer.Attributes) ([]rbacv1.Subject, error)
  29. }
  30. var _ = SubjectLocator(&SubjectAccessEvaluator{})
  31. type SubjectAccessEvaluator struct {
  32. superUser string
  33. roleBindingLister rbacregistryvalidation.RoleBindingLister
  34. clusterRoleBindingLister rbacregistryvalidation.ClusterRoleBindingLister
  35. roleToRuleMapper RoleToRuleMapper
  36. }
  37. func NewSubjectAccessEvaluator(roles rbacregistryvalidation.RoleGetter, roleBindings rbacregistryvalidation.RoleBindingLister, clusterRoles rbacregistryvalidation.ClusterRoleGetter, clusterRoleBindings rbacregistryvalidation.ClusterRoleBindingLister, superUser string) *SubjectAccessEvaluator {
  38. subjectLocator := &SubjectAccessEvaluator{
  39. superUser: superUser,
  40. roleBindingLister: roleBindings,
  41. clusterRoleBindingLister: clusterRoleBindings,
  42. roleToRuleMapper: rbacregistryvalidation.NewDefaultRuleResolver(
  43. roles, roleBindings, clusterRoles, clusterRoleBindings,
  44. ),
  45. }
  46. return subjectLocator
  47. }
  48. // AllowedSubjects returns the subjects that can perform an action and any errors encountered while computing the list.
  49. // It is possible to have both subjects and errors returned if some rolebindings couldn't be resolved, but others could be.
  50. func (r *SubjectAccessEvaluator) AllowedSubjects(requestAttributes authorizer.Attributes) ([]rbacv1.Subject, error) {
  51. subjects := []rbacv1.Subject{{Kind: rbacv1.GroupKind, APIGroup: rbacv1.GroupName, Name: user.SystemPrivilegedGroup}}
  52. if len(r.superUser) > 0 {
  53. subjects = append(subjects, rbacv1.Subject{Kind: rbacv1.UserKind, APIGroup: rbacv1.GroupName, Name: r.superUser})
  54. }
  55. errorlist := []error{}
  56. if clusterRoleBindings, err := r.clusterRoleBindingLister.ListClusterRoleBindings(); err != nil {
  57. errorlist = append(errorlist, err)
  58. } else {
  59. for _, clusterRoleBinding := range clusterRoleBindings {
  60. rules, err := r.roleToRuleMapper.GetRoleReferenceRules(clusterRoleBinding.RoleRef, "")
  61. if err != nil {
  62. // if we have an error, just keep track of it and keep processing. Since rules are additive,
  63. // missing a reference is bad, but we can continue with other rolebindings and still have a list
  64. // that does not contain any invalid values
  65. errorlist = append(errorlist, err)
  66. }
  67. if RulesAllow(requestAttributes, rules...) {
  68. subjects = append(subjects, clusterRoleBinding.Subjects...)
  69. }
  70. }
  71. }
  72. if namespace := requestAttributes.GetNamespace(); len(namespace) > 0 {
  73. if roleBindings, err := r.roleBindingLister.ListRoleBindings(namespace); err != nil {
  74. errorlist = append(errorlist, err)
  75. } else {
  76. for _, roleBinding := range roleBindings {
  77. rules, err := r.roleToRuleMapper.GetRoleReferenceRules(roleBinding.RoleRef, namespace)
  78. if err != nil {
  79. // if we have an error, just keep track of it and keep processing. Since rules are additive,
  80. // missing a reference is bad, but we can continue with other rolebindings and still have a list
  81. // that does not contain any invalid values
  82. errorlist = append(errorlist, err)
  83. }
  84. if RulesAllow(requestAttributes, rules...) {
  85. subjects = append(subjects, roleBinding.Subjects...)
  86. }
  87. }
  88. }
  89. }
  90. dedupedSubjects := []rbacv1.Subject{}
  91. for _, subject := range subjects {
  92. found := false
  93. for _, curr := range dedupedSubjects {
  94. if curr == subject {
  95. found = true
  96. break
  97. }
  98. }
  99. if !found {
  100. dedupedSubjects = append(dedupedSubjects, subject)
  101. }
  102. }
  103. return subjects, utilerrors.NewAggregate(errorlist)
  104. }