evaluation_helpers.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright 2018 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 v1
  14. import (
  15. "fmt"
  16. "strings"
  17. rbacv1 "k8s.io/api/rbac/v1"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. )
  20. func RoleRefGroupKind(roleRef rbacv1.RoleRef) schema.GroupKind {
  21. return schema.GroupKind{Group: roleRef.APIGroup, Kind: roleRef.Kind}
  22. }
  23. func VerbMatches(rule *rbacv1.PolicyRule, requestedVerb string) bool {
  24. for _, ruleVerb := range rule.Verbs {
  25. if ruleVerb == rbacv1.VerbAll {
  26. return true
  27. }
  28. if ruleVerb == requestedVerb {
  29. return true
  30. }
  31. }
  32. return false
  33. }
  34. func APIGroupMatches(rule *rbacv1.PolicyRule, requestedGroup string) bool {
  35. for _, ruleGroup := range rule.APIGroups {
  36. if ruleGroup == rbacv1.APIGroupAll {
  37. return true
  38. }
  39. if ruleGroup == requestedGroup {
  40. return true
  41. }
  42. }
  43. return false
  44. }
  45. func ResourceMatches(rule *rbacv1.PolicyRule, combinedRequestedResource, requestedSubresource string) bool {
  46. for _, ruleResource := range rule.Resources {
  47. // if everything is allowed, we match
  48. if ruleResource == rbacv1.ResourceAll {
  49. return true
  50. }
  51. // if we have an exact match, we match
  52. if ruleResource == combinedRequestedResource {
  53. return true
  54. }
  55. // We can also match a */subresource.
  56. // if there isn't a subresource, then continue
  57. if len(requestedSubresource) == 0 {
  58. continue
  59. }
  60. // if the rule isn't in the format */subresource, then we don't match, continue
  61. if len(ruleResource) == len(requestedSubresource)+2 &&
  62. strings.HasPrefix(ruleResource, "*/") &&
  63. strings.HasSuffix(ruleResource, requestedSubresource) {
  64. return true
  65. }
  66. }
  67. return false
  68. }
  69. func ResourceNameMatches(rule *rbacv1.PolicyRule, requestedName string) bool {
  70. if len(rule.ResourceNames) == 0 {
  71. return true
  72. }
  73. for _, ruleName := range rule.ResourceNames {
  74. if ruleName == requestedName {
  75. return true
  76. }
  77. }
  78. return false
  79. }
  80. func NonResourceURLMatches(rule *rbacv1.PolicyRule, requestedURL string) bool {
  81. for _, ruleURL := range rule.NonResourceURLs {
  82. if ruleURL == rbacv1.NonResourceAll {
  83. return true
  84. }
  85. if ruleURL == requestedURL {
  86. return true
  87. }
  88. if strings.HasSuffix(ruleURL, "*") && strings.HasPrefix(requestedURL, strings.TrimRight(ruleURL, "*")) {
  89. return true
  90. }
  91. }
  92. return false
  93. }
  94. // subjectsStrings returns users, groups, serviceaccounts, unknown for display purposes.
  95. func SubjectsStrings(subjects []rbacv1.Subject) ([]string, []string, []string, []string) {
  96. users := []string{}
  97. groups := []string{}
  98. sas := []string{}
  99. others := []string{}
  100. for _, subject := range subjects {
  101. switch subject.Kind {
  102. case rbacv1.ServiceAccountKind:
  103. sas = append(sas, fmt.Sprintf("%s/%s", subject.Namespace, subject.Name))
  104. case rbacv1.UserKind:
  105. users = append(users, subject.Name)
  106. case rbacv1.GroupKind:
  107. groups = append(groups, subject.Name)
  108. default:
  109. others = append(others, fmt.Sprintf("%s/%s/%s", subject.Kind, subject.Namespace, subject.Name))
  110. }
  111. }
  112. return users, groups, sas, others
  113. }
  114. func String(r rbacv1.PolicyRule) string {
  115. return "PolicyRule" + CompactString(r)
  116. }
  117. // CompactString exposes a compact string representation for use in escalation error messages
  118. func CompactString(r rbacv1.PolicyRule) string {
  119. formatStringParts := []string{}
  120. formatArgs := []interface{}{}
  121. if len(r.APIGroups) > 0 {
  122. formatStringParts = append(formatStringParts, "APIGroups:%q")
  123. formatArgs = append(formatArgs, r.APIGroups)
  124. }
  125. if len(r.Resources) > 0 {
  126. formatStringParts = append(formatStringParts, "Resources:%q")
  127. formatArgs = append(formatArgs, r.Resources)
  128. }
  129. if len(r.NonResourceURLs) > 0 {
  130. formatStringParts = append(formatStringParts, "NonResourceURLs:%q")
  131. formatArgs = append(formatArgs, r.NonResourceURLs)
  132. }
  133. if len(r.ResourceNames) > 0 {
  134. formatStringParts = append(formatStringParts, "ResourceNames:%q")
  135. formatArgs = append(formatArgs, r.ResourceNames)
  136. }
  137. if len(r.Verbs) > 0 {
  138. formatStringParts = append(formatStringParts, "Verbs:%q")
  139. formatArgs = append(formatArgs, r.Verbs)
  140. }
  141. formatString := "{" + strings.Join(formatStringParts, ", ") + "}"
  142. return fmt.Sprintf(formatString, formatArgs...)
  143. }
  144. type SortableRuleSlice []rbacv1.PolicyRule
  145. func (s SortableRuleSlice) Len() int { return len(s) }
  146. func (s SortableRuleSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  147. func (s SortableRuleSlice) Less(i, j int) bool {
  148. return strings.Compare(s[i].String(), s[j].String()) < 0
  149. }