node_authorizer.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. Copyright 2017 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 node
  14. import (
  15. "fmt"
  16. "k8s.io/klog"
  17. rbacv1 "k8s.io/api/rbac/v1"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. "k8s.io/apiserver/pkg/authorization/authorizer"
  20. utilfeature "k8s.io/apiserver/pkg/util/feature"
  21. "k8s.io/component-base/featuregate"
  22. coordapi "k8s.io/kubernetes/pkg/apis/coordination"
  23. api "k8s.io/kubernetes/pkg/apis/core"
  24. storageapi "k8s.io/kubernetes/pkg/apis/storage"
  25. "k8s.io/kubernetes/pkg/auth/nodeidentifier"
  26. "k8s.io/kubernetes/pkg/features"
  27. "k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac"
  28. "k8s.io/kubernetes/third_party/forked/gonum/graph"
  29. "k8s.io/kubernetes/third_party/forked/gonum/graph/traverse"
  30. )
  31. // NodeAuthorizer authorizes requests from kubelets, with the following logic:
  32. // 1. If a request is not from a node (NodeIdentity() returns isNode=false), reject
  33. // 2. If a specific node cannot be identified (NodeIdentity() returns nodeName=""), reject
  34. // 3. If a request is for a secret, configmap, persistent volume or persistent volume claim, reject unless the verb is get, and the requested object is related to the requesting node:
  35. // node <- configmap
  36. // node <- pod
  37. // node <- pod <- secret
  38. // node <- pod <- configmap
  39. // node <- pod <- pvc
  40. // node <- pod <- pvc <- pv
  41. // node <- pod <- pvc <- pv <- secret
  42. // 4. For other resources, authorize all nodes uniformly using statically defined rules
  43. type NodeAuthorizer struct {
  44. graph *Graph
  45. identifier nodeidentifier.NodeIdentifier
  46. nodeRules []rbacv1.PolicyRule
  47. // allows overriding for testing
  48. features featuregate.FeatureGate
  49. }
  50. // NewAuthorizer returns a new node authorizer
  51. func NewAuthorizer(graph *Graph, identifier nodeidentifier.NodeIdentifier, rules []rbacv1.PolicyRule) authorizer.Authorizer {
  52. return &NodeAuthorizer{
  53. graph: graph,
  54. identifier: identifier,
  55. nodeRules: rules,
  56. features: utilfeature.DefaultFeatureGate,
  57. }
  58. }
  59. var (
  60. configMapResource = api.Resource("configmaps")
  61. secretResource = api.Resource("secrets")
  62. pvcResource = api.Resource("persistentvolumeclaims")
  63. pvResource = api.Resource("persistentvolumes")
  64. vaResource = storageapi.Resource("volumeattachments")
  65. svcAcctResource = api.Resource("serviceaccounts")
  66. leaseResource = coordapi.Resource("leases")
  67. csiNodeResource = storageapi.Resource("csinodes")
  68. )
  69. func (r *NodeAuthorizer) Authorize(attrs authorizer.Attributes) (authorizer.Decision, string, error) {
  70. nodeName, isNode := r.identifier.NodeIdentity(attrs.GetUser())
  71. if !isNode {
  72. // reject requests from non-nodes
  73. return authorizer.DecisionNoOpinion, "", nil
  74. }
  75. if len(nodeName) == 0 {
  76. // reject requests from unidentifiable nodes
  77. klog.V(2).Infof("NODE DENY: unknown node for user %q", attrs.GetUser().GetName())
  78. return authorizer.DecisionNoOpinion, fmt.Sprintf("unknown node for user %q", attrs.GetUser().GetName()), nil
  79. }
  80. // subdivide access to specific resources
  81. if attrs.IsResourceRequest() {
  82. requestResource := schema.GroupResource{Group: attrs.GetAPIGroup(), Resource: attrs.GetResource()}
  83. switch requestResource {
  84. case secretResource:
  85. return r.authorizeReadNamespacedObject(nodeName, secretVertexType, attrs)
  86. case configMapResource:
  87. return r.authorizeReadNamespacedObject(nodeName, configMapVertexType, attrs)
  88. case pvcResource:
  89. if r.features.Enabled(features.ExpandPersistentVolumes) {
  90. if attrs.GetSubresource() == "status" {
  91. return r.authorizeStatusUpdate(nodeName, pvcVertexType, attrs)
  92. }
  93. }
  94. return r.authorizeGet(nodeName, pvcVertexType, attrs)
  95. case pvResource:
  96. return r.authorizeGet(nodeName, pvVertexType, attrs)
  97. case vaResource:
  98. if r.features.Enabled(features.CSIPersistentVolume) {
  99. return r.authorizeGet(nodeName, vaVertexType, attrs)
  100. }
  101. return authorizer.DecisionNoOpinion, fmt.Sprintf("disabled by feature gate %s", features.CSIPersistentVolume), nil
  102. case svcAcctResource:
  103. if r.features.Enabled(features.TokenRequest) {
  104. return r.authorizeCreateToken(nodeName, serviceAccountVertexType, attrs)
  105. }
  106. return authorizer.DecisionNoOpinion, fmt.Sprintf("disabled by feature gate %s", features.TokenRequest), nil
  107. case leaseResource:
  108. if r.features.Enabled(features.NodeLease) {
  109. return r.authorizeLease(nodeName, attrs)
  110. }
  111. return authorizer.DecisionNoOpinion, fmt.Sprintf("disabled by feature gate %s", features.NodeLease), nil
  112. case csiNodeResource:
  113. if r.features.Enabled(features.KubeletPluginsWatcher) && r.features.Enabled(features.CSINodeInfo) {
  114. return r.authorizeCSINode(nodeName, attrs)
  115. }
  116. return authorizer.DecisionNoOpinion, fmt.Sprintf("disabled by feature gates %s and %s", features.KubeletPluginsWatcher, features.CSINodeInfo), nil
  117. }
  118. }
  119. // Access to other resources is not subdivided, so just evaluate against the statically defined node rules
  120. if rbac.RulesAllow(attrs, r.nodeRules...) {
  121. return authorizer.DecisionAllow, "", nil
  122. }
  123. return authorizer.DecisionNoOpinion, "", nil
  124. }
  125. // authorizeStatusUpdate authorizes get/update/patch requests to status subresources of the specified type if they are related to the specified node
  126. func (r *NodeAuthorizer) authorizeStatusUpdate(nodeName string, startingType vertexType, attrs authorizer.Attributes) (authorizer.Decision, string, error) {
  127. switch attrs.GetVerb() {
  128. case "update", "patch":
  129. // ok
  130. default:
  131. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  132. return authorizer.DecisionNoOpinion, "can only get/update/patch this type", nil
  133. }
  134. if attrs.GetSubresource() != "status" {
  135. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  136. return authorizer.DecisionNoOpinion, "can only update status subresource", nil
  137. }
  138. return r.authorize(nodeName, startingType, attrs)
  139. }
  140. // authorizeGet authorizes "get" requests to objects of the specified type if they are related to the specified node
  141. func (r *NodeAuthorizer) authorizeGet(nodeName string, startingType vertexType, attrs authorizer.Attributes) (authorizer.Decision, string, error) {
  142. if attrs.GetVerb() != "get" {
  143. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  144. return authorizer.DecisionNoOpinion, "can only get individual resources of this type", nil
  145. }
  146. if len(attrs.GetSubresource()) > 0 {
  147. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  148. return authorizer.DecisionNoOpinion, "cannot get subresource", nil
  149. }
  150. return r.authorize(nodeName, startingType, attrs)
  151. }
  152. // authorizeReadNamespacedObject authorizes "get", "list" and "watch" requests to single objects of a
  153. // specified types if they are related to the specified node.
  154. func (r *NodeAuthorizer) authorizeReadNamespacedObject(nodeName string, startingType vertexType, attrs authorizer.Attributes) (authorizer.Decision, string, error) {
  155. if attrs.GetVerb() != "get" && attrs.GetVerb() != "list" && attrs.GetVerb() != "watch" {
  156. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  157. return authorizer.DecisionNoOpinion, "can only read resources of this type", nil
  158. }
  159. if len(attrs.GetSubresource()) > 0 {
  160. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  161. return authorizer.DecisionNoOpinion, "cannot read subresource", nil
  162. }
  163. if len(attrs.GetNamespace()) == 0 {
  164. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  165. return authorizer.DecisionNoOpinion, "can only read namespaced object of this type", nil
  166. }
  167. return r.authorize(nodeName, startingType, attrs)
  168. }
  169. func (r *NodeAuthorizer) authorize(nodeName string, startingType vertexType, attrs authorizer.Attributes) (authorizer.Decision, string, error) {
  170. if len(attrs.GetName()) == 0 {
  171. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  172. return authorizer.DecisionNoOpinion, "No Object name found", nil
  173. }
  174. ok, err := r.hasPathFrom(nodeName, startingType, attrs.GetNamespace(), attrs.GetName())
  175. if err != nil {
  176. klog.V(2).Infof("NODE DENY: %v", err)
  177. return authorizer.DecisionNoOpinion, fmt.Sprintf("no relationship found between node %q and this object", nodeName), nil
  178. }
  179. if !ok {
  180. klog.V(2).Infof("NODE DENY: %q %#v", nodeName, attrs)
  181. return authorizer.DecisionNoOpinion, fmt.Sprintf("no relationship found between node %q and this object", nodeName), nil
  182. }
  183. return authorizer.DecisionAllow, "", nil
  184. }
  185. // authorizeCreateToken authorizes "create" requests to serviceaccounts 'token'
  186. // subresource of pods running on a node
  187. func (r *NodeAuthorizer) authorizeCreateToken(nodeName string, startingType vertexType, attrs authorizer.Attributes) (authorizer.Decision, string, error) {
  188. if attrs.GetVerb() != "create" || len(attrs.GetName()) == 0 {
  189. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  190. return authorizer.DecisionNoOpinion, "can only create tokens for individual service accounts", nil
  191. }
  192. if attrs.GetSubresource() != "token" {
  193. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  194. return authorizer.DecisionNoOpinion, "can only create token subresource of serviceaccount", nil
  195. }
  196. ok, err := r.hasPathFrom(nodeName, startingType, attrs.GetNamespace(), attrs.GetName())
  197. if err != nil {
  198. klog.V(2).Infof("NODE DENY: %v", err)
  199. return authorizer.DecisionNoOpinion, fmt.Sprintf("no relationship found between node %q and this object", nodeName), nil
  200. }
  201. if !ok {
  202. klog.V(2).Infof("NODE DENY: %q %#v", nodeName, attrs)
  203. return authorizer.DecisionNoOpinion, fmt.Sprintf("no relationship found between node %q and this object", nodeName), nil
  204. }
  205. return authorizer.DecisionAllow, "", nil
  206. }
  207. // authorizeLease authorizes node requests to coordination.k8s.io/leases.
  208. func (r *NodeAuthorizer) authorizeLease(nodeName string, attrs authorizer.Attributes) (authorizer.Decision, string, error) {
  209. // allowed verbs: get, create, update, patch, delete
  210. verb := attrs.GetVerb()
  211. if verb != "get" &&
  212. verb != "create" &&
  213. verb != "update" &&
  214. verb != "patch" &&
  215. verb != "delete" {
  216. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  217. return authorizer.DecisionNoOpinion, "can only get, create, update, patch, or delete a node lease", nil
  218. }
  219. // the request must be against the system namespace reserved for node leases
  220. if attrs.GetNamespace() != api.NamespaceNodeLease {
  221. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  222. return authorizer.DecisionNoOpinion, fmt.Sprintf("can only access leases in the %q system namespace", api.NamespaceNodeLease), nil
  223. }
  224. // the request must come from a node with the same name as the lease
  225. // note we skip this check for create, since the authorizer doesn't know the name on create
  226. // the noderestriction admission plugin is capable of performing this check at create time
  227. if verb != "create" && attrs.GetName() != nodeName {
  228. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  229. return authorizer.DecisionNoOpinion, "can only access node lease with the same name as the requesting node", nil
  230. }
  231. return authorizer.DecisionAllow, "", nil
  232. }
  233. // authorizeCSINode authorizes node requests to CSINode storage.k8s.io/csinodes
  234. func (r *NodeAuthorizer) authorizeCSINode(nodeName string, attrs authorizer.Attributes) (authorizer.Decision, string, error) {
  235. // allowed verbs: get, create, update, patch, delete
  236. verb := attrs.GetVerb()
  237. if verb != "get" &&
  238. verb != "create" &&
  239. verb != "update" &&
  240. verb != "patch" &&
  241. verb != "delete" {
  242. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  243. return authorizer.DecisionNoOpinion, "can only get, create, update, patch, or delete a CSINode", nil
  244. }
  245. if len(attrs.GetSubresource()) > 0 {
  246. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  247. return authorizer.DecisionNoOpinion, "cannot authorize CSINode subresources", nil
  248. }
  249. // the request must come from a node with the same name as the CSINode
  250. // note we skip this check for create, since the authorizer doesn't know the name on create
  251. // the noderestriction admission plugin is capable of performing this check at create time
  252. if verb != "create" && attrs.GetName() != nodeName {
  253. klog.V(2).Infof("NODE DENY: %s %#v", nodeName, attrs)
  254. return authorizer.DecisionNoOpinion, "can only access CSINode with the same name as the requesting node", nil
  255. }
  256. return authorizer.DecisionAllow, "", nil
  257. }
  258. // hasPathFrom returns true if there is a directed path from the specified type/namespace/name to the specified Node
  259. func (r *NodeAuthorizer) hasPathFrom(nodeName string, startingType vertexType, startingNamespace, startingName string) (bool, error) {
  260. r.graph.lock.RLock()
  261. defer r.graph.lock.RUnlock()
  262. nodeVertex, exists := r.graph.getVertex_rlocked(nodeVertexType, "", nodeName)
  263. if !exists {
  264. return false, fmt.Errorf("unknown node %q cannot get %s %s/%s", nodeName, vertexTypes[startingType], startingNamespace, startingName)
  265. }
  266. startingVertex, exists := r.graph.getVertex_rlocked(startingType, startingNamespace, startingName)
  267. if !exists {
  268. return false, fmt.Errorf("node %q cannot get unknown %s %s/%s", nodeName, vertexTypes[startingType], startingNamespace, startingName)
  269. }
  270. // Fast check to see if we know of a destination edge
  271. if r.graph.destinationEdgeIndex[startingVertex.ID()].has(nodeVertex.ID()) {
  272. return true, nil
  273. }
  274. found := false
  275. traversal := &traverse.VisitingDepthFirst{
  276. EdgeFilter: func(edge graph.Edge) bool {
  277. if destinationEdge, ok := edge.(*destinationEdge); ok {
  278. if destinationEdge.DestinationID() != nodeVertex.ID() {
  279. // Don't follow edges leading to other nodes
  280. return false
  281. }
  282. // We found an edge leading to the node we want
  283. found = true
  284. }
  285. // Visit this edge
  286. return true
  287. },
  288. }
  289. traversal.Walk(r.graph.graph, startingVertex, func(n graph.Node) bool {
  290. if n.ID() == nodeVertex.ID() {
  291. // We found the node we want
  292. found = true
  293. }
  294. // Stop visiting if we've found the node we want
  295. return found
  296. })
  297. if !found {
  298. return false, fmt.Errorf("node %q cannot get %s %s/%s, no relationship to this object was found in the node authorizer graph", nodeName, vertexTypes[startingType], startingNamespace, startingName)
  299. }
  300. return true, nil
  301. }