helpers.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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
  14. import (
  15. "fmt"
  16. "strings"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. )
  20. // ResourceMatches returns the result of the rule.Resources matching.
  21. func ResourceMatches(rule *PolicyRule, combinedRequestedResource, requestedSubresource string) bool {
  22. for _, ruleResource := range rule.Resources {
  23. // if everything is allowed, we match
  24. if ruleResource == ResourceAll {
  25. return true
  26. }
  27. // if we have an exact match, we match
  28. if ruleResource == combinedRequestedResource {
  29. return true
  30. }
  31. // We can also match a */subresource.
  32. // if there isn't a subresource, then continue
  33. if len(requestedSubresource) == 0 {
  34. continue
  35. }
  36. // if the rule isn't in the format */subresource, then we don't match, continue
  37. if len(ruleResource) == len(requestedSubresource)+2 &&
  38. strings.HasPrefix(ruleResource, "*/") &&
  39. strings.HasSuffix(ruleResource, requestedSubresource) {
  40. return true
  41. }
  42. }
  43. return false
  44. }
  45. // SubjectsStrings returns users, groups, serviceaccounts, unknown for display purposes.
  46. func SubjectsStrings(subjects []Subject) ([]string, []string, []string, []string) {
  47. users := []string{}
  48. groups := []string{}
  49. sas := []string{}
  50. others := []string{}
  51. for _, subject := range subjects {
  52. switch subject.Kind {
  53. case ServiceAccountKind:
  54. sas = append(sas, fmt.Sprintf("%s/%s", subject.Namespace, subject.Name))
  55. case UserKind:
  56. users = append(users, subject.Name)
  57. case GroupKind:
  58. groups = append(groups, subject.Name)
  59. default:
  60. others = append(others, fmt.Sprintf("%s/%s/%s", subject.Kind, subject.Namespace, subject.Name))
  61. }
  62. }
  63. return users, groups, sas, others
  64. }
  65. func (r PolicyRule) String() string {
  66. return "PolicyRule" + r.CompactString()
  67. }
  68. // CompactString exposes a compact string representation for use in escalation error messages
  69. func (r PolicyRule) CompactString() string {
  70. formatStringParts := []string{}
  71. formatArgs := []interface{}{}
  72. if len(r.APIGroups) > 0 {
  73. formatStringParts = append(formatStringParts, "APIGroups:%q")
  74. formatArgs = append(formatArgs, r.APIGroups)
  75. }
  76. if len(r.Resources) > 0 {
  77. formatStringParts = append(formatStringParts, "Resources:%q")
  78. formatArgs = append(formatArgs, r.Resources)
  79. }
  80. if len(r.NonResourceURLs) > 0 {
  81. formatStringParts = append(formatStringParts, "NonResourceURLs:%q")
  82. formatArgs = append(formatArgs, r.NonResourceURLs)
  83. }
  84. if len(r.ResourceNames) > 0 {
  85. formatStringParts = append(formatStringParts, "ResourceNames:%q")
  86. formatArgs = append(formatArgs, r.ResourceNames)
  87. }
  88. if len(r.Verbs) > 0 {
  89. formatStringParts = append(formatStringParts, "Verbs:%q")
  90. formatArgs = append(formatArgs, r.Verbs)
  91. }
  92. formatString := "{" + strings.Join(formatStringParts, ", ") + "}"
  93. return fmt.Sprintf(formatString, formatArgs...)
  94. }
  95. // PolicyRuleBuilder let's us attach methods. A no-no for API types.
  96. // We use it to construct rules in code. It's more compact than trying to write them
  97. // out in a literal and allows us to perform some basic checking during construction
  98. // +k8s:deepcopy-gen=false
  99. type PolicyRuleBuilder struct {
  100. PolicyRule PolicyRule
  101. }
  102. // NewRule returns new PolicyRule made by input verbs.
  103. func NewRule(verbs ...string) *PolicyRuleBuilder {
  104. return &PolicyRuleBuilder{
  105. PolicyRule: PolicyRule{Verbs: sets.NewString(verbs...).List()},
  106. }
  107. }
  108. // Groups combines the PolicyRule.APIGroups and input groups.
  109. func (r *PolicyRuleBuilder) Groups(groups ...string) *PolicyRuleBuilder {
  110. r.PolicyRule.APIGroups = combine(r.PolicyRule.APIGroups, groups)
  111. return r
  112. }
  113. // Resources combines the PolicyRule.Rule and input resources.
  114. func (r *PolicyRuleBuilder) Resources(resources ...string) *PolicyRuleBuilder {
  115. r.PolicyRule.Resources = combine(r.PolicyRule.Resources, resources)
  116. return r
  117. }
  118. // Names combines the PolicyRule.ResourceNames and input names.
  119. func (r *PolicyRuleBuilder) Names(names ...string) *PolicyRuleBuilder {
  120. r.PolicyRule.ResourceNames = combine(r.PolicyRule.ResourceNames, names)
  121. return r
  122. }
  123. // URLs combines the PolicyRule.NonResourceURLs and input urls.
  124. func (r *PolicyRuleBuilder) URLs(urls ...string) *PolicyRuleBuilder {
  125. r.PolicyRule.NonResourceURLs = combine(r.PolicyRule.NonResourceURLs, urls)
  126. return r
  127. }
  128. // RuleOrDie calls the binding method and panics if there is an error.
  129. func (r *PolicyRuleBuilder) RuleOrDie() PolicyRule {
  130. ret, err := r.Rule()
  131. if err != nil {
  132. panic(err)
  133. }
  134. return ret
  135. }
  136. func combine(s1, s2 []string) []string {
  137. s := sets.NewString(s1...)
  138. s.Insert(s2...)
  139. return s.List()
  140. }
  141. // Rule returns PolicyRule and error.
  142. func (r *PolicyRuleBuilder) Rule() (PolicyRule, error) {
  143. if len(r.PolicyRule.Verbs) == 0 {
  144. return PolicyRule{}, fmt.Errorf("verbs are required: %#v", r.PolicyRule)
  145. }
  146. switch {
  147. case len(r.PolicyRule.NonResourceURLs) > 0:
  148. if len(r.PolicyRule.APIGroups) != 0 || len(r.PolicyRule.Resources) != 0 || len(r.PolicyRule.ResourceNames) != 0 {
  149. return PolicyRule{}, fmt.Errorf("non-resource rule may not have apiGroups, resources, or resourceNames: %#v", r.PolicyRule)
  150. }
  151. case len(r.PolicyRule.Resources) > 0:
  152. // resource rule may not have nonResourceURLs
  153. if len(r.PolicyRule.APIGroups) == 0 {
  154. // this a common bug
  155. return PolicyRule{}, fmt.Errorf("resource rule must have apiGroups: %#v", r.PolicyRule)
  156. }
  157. // if resource names are set, then the verb must not be list, watch, create, or deletecollection
  158. // since verbs are largely opaque, we don't want to accidentally prevent things like "impersonate", so
  159. // we will backlist common mistakes, not whitelist acceptable options.
  160. if len(r.PolicyRule.ResourceNames) != 0 {
  161. illegalVerbs := []string{}
  162. for _, verb := range r.PolicyRule.Verbs {
  163. switch verb {
  164. case "list", "watch", "create", "deletecollection":
  165. illegalVerbs = append(illegalVerbs, verb)
  166. }
  167. }
  168. if len(illegalVerbs) > 0 {
  169. return PolicyRule{}, fmt.Errorf("verbs %v do not have names available: %#v", illegalVerbs, r.PolicyRule)
  170. }
  171. }
  172. default:
  173. return PolicyRule{}, fmt.Errorf("a rule must have either nonResourceURLs or resources: %#v", r.PolicyRule)
  174. }
  175. return r.PolicyRule, nil
  176. }
  177. // ClusterRoleBindingBuilder let's us attach methods. A no-no for API types.
  178. // We use it to construct bindings in code. It's more compact than trying to write them
  179. // out in a literal.
  180. // +k8s:deepcopy-gen=false
  181. type ClusterRoleBindingBuilder struct {
  182. ClusterRoleBinding ClusterRoleBinding
  183. }
  184. // NewClusterBinding creates a ClusterRoleBinding builder that can be used
  185. // to define the subjects of a cluster role binding. At least one of
  186. // the `Groups`, `Users` or `SAs` method must be called before
  187. // calling the `Binding*` methods.
  188. func NewClusterBinding(clusterRoleName string) *ClusterRoleBindingBuilder {
  189. return &ClusterRoleBindingBuilder{
  190. ClusterRoleBinding: ClusterRoleBinding{
  191. ObjectMeta: metav1.ObjectMeta{Name: clusterRoleName},
  192. RoleRef: RoleRef{
  193. APIGroup: GroupName,
  194. Kind: "ClusterRole",
  195. Name: clusterRoleName,
  196. },
  197. },
  198. }
  199. }
  200. // Groups adds the specified groups as the subjects of the ClusterRoleBinding.
  201. func (r *ClusterRoleBindingBuilder) Groups(groups ...string) *ClusterRoleBindingBuilder {
  202. for _, group := range groups {
  203. r.ClusterRoleBinding.Subjects = append(r.ClusterRoleBinding.Subjects, Subject{Kind: GroupKind, APIGroup: GroupName, Name: group})
  204. }
  205. return r
  206. }
  207. // Users adds the specified users as the subjects of the ClusterRoleBinding.
  208. func (r *ClusterRoleBindingBuilder) Users(users ...string) *ClusterRoleBindingBuilder {
  209. for _, user := range users {
  210. r.ClusterRoleBinding.Subjects = append(r.ClusterRoleBinding.Subjects, Subject{Kind: UserKind, APIGroup: GroupName, Name: user})
  211. }
  212. return r
  213. }
  214. // SAs adds the specified sas as the subjects of the ClusterRoleBinding.
  215. func (r *ClusterRoleBindingBuilder) SAs(namespace string, serviceAccountNames ...string) *ClusterRoleBindingBuilder {
  216. for _, saName := range serviceAccountNames {
  217. r.ClusterRoleBinding.Subjects = append(r.ClusterRoleBinding.Subjects, Subject{Kind: ServiceAccountKind, Namespace: namespace, Name: saName})
  218. }
  219. return r
  220. }
  221. // BindingOrDie calls the binding method and panics if there is an error.
  222. func (r *ClusterRoleBindingBuilder) BindingOrDie() ClusterRoleBinding {
  223. ret, err := r.Binding()
  224. if err != nil {
  225. panic(err)
  226. }
  227. return ret
  228. }
  229. // Binding builds and returns the ClusterRoleBinding API object from the builder
  230. // object.
  231. func (r *ClusterRoleBindingBuilder) Binding() (ClusterRoleBinding, error) {
  232. if len(r.ClusterRoleBinding.Subjects) == 0 {
  233. return ClusterRoleBinding{}, fmt.Errorf("subjects are required: %#v", r.ClusterRoleBinding)
  234. }
  235. return r.ClusterRoleBinding, nil
  236. }
  237. // RoleBindingBuilder let's us attach methods. It is similar to
  238. // ClusterRoleBindingBuilder above.
  239. // +k8s:deepcopy-gen=false
  240. type RoleBindingBuilder struct {
  241. RoleBinding RoleBinding
  242. }
  243. // NewRoleBinding creates a RoleBinding builder that can be used
  244. // to define the subjects of a role binding. At least one of
  245. // the `Groups`, `Users` or `SAs` method must be called before
  246. // calling the `Binding*` methods.
  247. func NewRoleBinding(roleName, namespace string) *RoleBindingBuilder {
  248. return &RoleBindingBuilder{
  249. RoleBinding: RoleBinding{
  250. ObjectMeta: metav1.ObjectMeta{
  251. Name: roleName,
  252. Namespace: namespace,
  253. },
  254. RoleRef: RoleRef{
  255. APIGroup: GroupName,
  256. Kind: "Role",
  257. Name: roleName,
  258. },
  259. },
  260. }
  261. }
  262. // NewRoleBindingForClusterRole creates a RoleBinding builder that can be used
  263. // to define the subjects of a cluster role binding. At least one of
  264. // the `Groups`, `Users` or `SAs` method must be called before
  265. // calling the `Binding*` methods.
  266. func NewRoleBindingForClusterRole(roleName, namespace string) *RoleBindingBuilder {
  267. return &RoleBindingBuilder{
  268. RoleBinding: RoleBinding{
  269. ObjectMeta: metav1.ObjectMeta{
  270. Name: roleName,
  271. Namespace: namespace,
  272. },
  273. RoleRef: RoleRef{
  274. APIGroup: GroupName,
  275. Kind: "ClusterRole",
  276. Name: roleName,
  277. },
  278. },
  279. }
  280. }
  281. // Groups adds the specified groups as the subjects of the RoleBinding.
  282. func (r *RoleBindingBuilder) Groups(groups ...string) *RoleBindingBuilder {
  283. for _, group := range groups {
  284. r.RoleBinding.Subjects = append(r.RoleBinding.Subjects, Subject{Kind: GroupKind, APIGroup: GroupName, Name: group})
  285. }
  286. return r
  287. }
  288. // Users adds the specified users as the subjects of the RoleBinding.
  289. func (r *RoleBindingBuilder) Users(users ...string) *RoleBindingBuilder {
  290. for _, user := range users {
  291. r.RoleBinding.Subjects = append(r.RoleBinding.Subjects, Subject{Kind: UserKind, APIGroup: GroupName, Name: user})
  292. }
  293. return r
  294. }
  295. // SAs adds the specified service accounts as the subjects of the
  296. // RoleBinding.
  297. func (r *RoleBindingBuilder) SAs(namespace string, serviceAccountNames ...string) *RoleBindingBuilder {
  298. for _, saName := range serviceAccountNames {
  299. r.RoleBinding.Subjects = append(r.RoleBinding.Subjects, Subject{Kind: ServiceAccountKind, Namespace: namespace, Name: saName})
  300. }
  301. return r
  302. }
  303. // BindingOrDie calls the binding method and panics if there is an error.
  304. func (r *RoleBindingBuilder) BindingOrDie() RoleBinding {
  305. ret, err := r.Binding()
  306. if err != nil {
  307. panic(err)
  308. }
  309. return ret
  310. }
  311. // Binding builds and returns the RoleBinding API object from the builder
  312. // object.
  313. func (r *RoleBindingBuilder) Binding() (RoleBinding, error) {
  314. if len(r.RoleBinding.Subjects) == 0 {
  315. return RoleBinding{}, fmt.Errorf("subjects are required: %#v", r.RoleBinding)
  316. }
  317. return r.RoleBinding, nil
  318. }
  319. // SortableRuleSlice is the slice of PolicyRule.
  320. type SortableRuleSlice []PolicyRule
  321. func (s SortableRuleSlice) Len() int { return len(s) }
  322. func (s SortableRuleSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  323. func (s SortableRuleSlice) Less(i, j int) bool {
  324. return strings.Compare(s[i].String(), s[j].String()) < 0
  325. }