escalation_check.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "context"
  16. "fmt"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  19. "k8s.io/apiserver/pkg/authentication/user"
  20. "k8s.io/apiserver/pkg/authorization/authorizer"
  21. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  22. "k8s.io/kubernetes/pkg/apis/rbac"
  23. )
  24. // EscalationAllowed checks if the user associated with the context is a superuser
  25. func EscalationAllowed(ctx context.Context) bool {
  26. u, ok := genericapirequest.UserFrom(ctx)
  27. if !ok {
  28. return false
  29. }
  30. // system:masters is special because the API server uses it for privileged loopback connections
  31. // therefore we know that a member of system:masters can always do anything
  32. for _, group := range u.GetGroups() {
  33. if group == user.SystemPrivilegedGroup {
  34. return true
  35. }
  36. }
  37. return false
  38. }
  39. var roleResources = map[schema.GroupResource]bool{
  40. rbac.SchemeGroupVersion.WithResource("clusterroles").GroupResource(): true,
  41. rbac.SchemeGroupVersion.WithResource("roles").GroupResource(): true,
  42. }
  43. // RoleEscalationAuthorized checks if the user associated with the context is explicitly authorized to escalate the role resource associated with the context
  44. func RoleEscalationAuthorized(ctx context.Context, a authorizer.Authorizer) bool {
  45. if a == nil {
  46. return false
  47. }
  48. user, ok := genericapirequest.UserFrom(ctx)
  49. if !ok {
  50. return false
  51. }
  52. requestInfo, ok := genericapirequest.RequestInfoFrom(ctx)
  53. if !ok {
  54. return false
  55. }
  56. if !requestInfo.IsResourceRequest {
  57. return false
  58. }
  59. requestResource := schema.GroupResource{Group: requestInfo.APIGroup, Resource: requestInfo.Resource}
  60. if !roleResources[requestResource] {
  61. return false
  62. }
  63. attrs := authorizer.AttributesRecord{
  64. User: user,
  65. Verb: "escalate",
  66. APIGroup: requestInfo.APIGroup,
  67. Resource: requestInfo.Resource,
  68. Name: requestInfo.Name,
  69. Namespace: requestInfo.Namespace,
  70. ResourceRequest: true,
  71. }
  72. decision, _, err := a.Authorize(attrs)
  73. if err != nil {
  74. utilruntime.HandleError(fmt.Errorf(
  75. "error authorizing user %#v to escalate %#v named %q in namespace %q: %v",
  76. user, requestResource, requestInfo.Name, requestInfo.Namespace, err,
  77. ))
  78. }
  79. return decision == authorizer.DecisionAllow
  80. }
  81. // BindingAuthorized returns true if the user associated with the context is explicitly authorized to bind the specified roleRef
  82. func BindingAuthorized(ctx context.Context, roleRef rbac.RoleRef, bindingNamespace string, a authorizer.Authorizer) bool {
  83. if a == nil {
  84. return false
  85. }
  86. user, ok := genericapirequest.UserFrom(ctx)
  87. if !ok {
  88. return false
  89. }
  90. attrs := authorizer.AttributesRecord{
  91. User: user,
  92. Verb: "bind",
  93. // check against the namespace where the binding is being created (or the empty namespace for clusterrolebindings).
  94. // this allows delegation to bind particular clusterroles in rolebindings within particular namespaces,
  95. // and to authorize binding a clusterrole across all namespaces in a clusterrolebinding.
  96. Namespace: bindingNamespace,
  97. ResourceRequest: true,
  98. }
  99. // This occurs after defaulting and conversion, so values pulled from the roleRef won't change
  100. // Invalid APIGroup or Name values will fail validation
  101. switch roleRef.Kind {
  102. case "ClusterRole":
  103. attrs.APIGroup = roleRef.APIGroup
  104. attrs.Resource = "clusterroles"
  105. attrs.Name = roleRef.Name
  106. case "Role":
  107. attrs.APIGroup = roleRef.APIGroup
  108. attrs.Resource = "roles"
  109. attrs.Name = roleRef.Name
  110. default:
  111. return false
  112. }
  113. decision, _, err := a.Authorize(attrs)
  114. if err != nil {
  115. utilruntime.HandleError(fmt.Errorf(
  116. "error authorizing user %#v to bind %#v in namespace %s: %v",
  117. user, roleRef, bindingNamespace, err,
  118. ))
  119. }
  120. return decision == authorizer.DecisionAllow
  121. }