policy.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 bootstrappolicy
  14. import (
  15. rbacv1 "k8s.io/api/rbac/v1"
  16. "k8s.io/apimachinery/pkg/api/meta"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apiserver/pkg/authentication/user"
  20. utilfeature "k8s.io/apiserver/pkg/util/feature"
  21. rbacv1helpers "k8s.io/kubernetes/pkg/apis/rbac/v1"
  22. "k8s.io/kubernetes/pkg/features"
  23. )
  24. var (
  25. Write = []string{"create", "update", "patch", "delete", "deletecollection"}
  26. ReadWrite = []string{"get", "list", "watch", "create", "update", "patch", "delete", "deletecollection"}
  27. Read = []string{"get", "list", "watch"}
  28. ReadUpdate = []string{"get", "list", "watch", "update", "patch"}
  29. Label = map[string]string{"kubernetes.io/bootstrapping": "rbac-defaults"}
  30. Annotation = map[string]string{rbacv1.AutoUpdateAnnotationKey: "true"}
  31. )
  32. const (
  33. legacyGroup = ""
  34. appsGroup = "apps"
  35. authenticationGroup = "authentication.k8s.io"
  36. authorizationGroup = "authorization.k8s.io"
  37. autoscalingGroup = "autoscaling"
  38. batchGroup = "batch"
  39. certificatesGroup = "certificates.k8s.io"
  40. coordinationGroup = "coordination.k8s.io"
  41. discoveryGroup = "discovery.k8s.io"
  42. extensionsGroup = "extensions"
  43. policyGroup = "policy"
  44. rbacGroup = "rbac.authorization.k8s.io"
  45. storageGroup = "storage.k8s.io"
  46. resMetricsGroup = "metrics.k8s.io"
  47. customMetricsGroup = "custom.metrics.k8s.io"
  48. networkingGroup = "networking.k8s.io"
  49. eventsGroup = "events.k8s.io"
  50. )
  51. func addDefaultMetadata(obj runtime.Object) {
  52. metadata, err := meta.Accessor(obj)
  53. if err != nil {
  54. // if this happens, then some static code is broken
  55. panic(err)
  56. }
  57. labels := metadata.GetLabels()
  58. if labels == nil {
  59. labels = map[string]string{}
  60. }
  61. for k, v := range Label {
  62. labels[k] = v
  63. }
  64. metadata.SetLabels(labels)
  65. annotations := metadata.GetAnnotations()
  66. if annotations == nil {
  67. annotations = map[string]string{}
  68. }
  69. for k, v := range Annotation {
  70. annotations[k] = v
  71. }
  72. metadata.SetAnnotations(annotations)
  73. }
  74. func addClusterRoleLabel(roles []rbacv1.ClusterRole) {
  75. for i := range roles {
  76. addDefaultMetadata(&roles[i])
  77. }
  78. return
  79. }
  80. func addClusterRoleBindingLabel(rolebindings []rbacv1.ClusterRoleBinding) {
  81. for i := range rolebindings {
  82. addDefaultMetadata(&rolebindings[i])
  83. }
  84. return
  85. }
  86. func NodeRules() []rbacv1.PolicyRule {
  87. nodePolicyRules := []rbacv1.PolicyRule{
  88. // Needed to check API access. These creates are non-mutating
  89. rbacv1helpers.NewRule("create").Groups(authenticationGroup).Resources("tokenreviews").RuleOrDie(),
  90. rbacv1helpers.NewRule("create").Groups(authorizationGroup).Resources("subjectaccessreviews", "localsubjectaccessreviews").RuleOrDie(),
  91. // Needed to build serviceLister, to populate env vars for services
  92. rbacv1helpers.NewRule(Read...).Groups(legacyGroup).Resources("services").RuleOrDie(),
  93. // Nodes can register Node API objects and report status.
  94. // Use the NodeRestriction admission plugin to limit a node to creating/updating its own API object.
  95. rbacv1helpers.NewRule("create", "get", "list", "watch").Groups(legacyGroup).Resources("nodes").RuleOrDie(),
  96. rbacv1helpers.NewRule("update", "patch").Groups(legacyGroup).Resources("nodes/status").RuleOrDie(),
  97. rbacv1helpers.NewRule("update", "patch").Groups(legacyGroup).Resources("nodes").RuleOrDie(),
  98. // TODO: restrict to the bound node as creator in the NodeRestrictions admission plugin
  99. rbacv1helpers.NewRule("create", "update", "patch").Groups(legacyGroup).Resources("events").RuleOrDie(),
  100. // TODO: restrict to pods scheduled on the bound node once field selectors are supported by list/watch authorization
  101. rbacv1helpers.NewRule(Read...).Groups(legacyGroup).Resources("pods").RuleOrDie(),
  102. // Needed for the node to create/delete mirror pods.
  103. // Use the NodeRestriction admission plugin to limit a node to creating/deleting mirror pods bound to itself.
  104. rbacv1helpers.NewRule("create", "delete").Groups(legacyGroup).Resources("pods").RuleOrDie(),
  105. // Needed for the node to report status of pods it is running.
  106. // Use the NodeRestriction admission plugin to limit a node to updating status of pods bound to itself.
  107. rbacv1helpers.NewRule("update", "patch").Groups(legacyGroup).Resources("pods/status").RuleOrDie(),
  108. // Needed for the node to create pod evictions.
  109. // Use the NodeRestriction admission plugin to limit a node to creating evictions for pods bound to itself.
  110. rbacv1helpers.NewRule("create").Groups(legacyGroup).Resources("pods/eviction").RuleOrDie(),
  111. // Needed for imagepullsecrets, rbd/ceph and secret volumes, and secrets in envs
  112. // Needed for configmap volume and envs
  113. // Use the Node authorization mode to limit a node to get secrets/configmaps referenced by pods bound to itself.
  114. rbacv1helpers.NewRule("get", "list", "watch").Groups(legacyGroup).Resources("secrets", "configmaps").RuleOrDie(),
  115. // Needed for persistent volumes
  116. // Use the Node authorization mode to limit a node to get pv/pvc objects referenced by pods bound to itself.
  117. rbacv1helpers.NewRule("get").Groups(legacyGroup).Resources("persistentvolumeclaims", "persistentvolumes").RuleOrDie(),
  118. // TODO: add to the Node authorizer and restrict to endpoints referenced by pods or PVs bound to the node
  119. // Needed for glusterfs volumes
  120. rbacv1helpers.NewRule("get").Groups(legacyGroup).Resources("endpoints").RuleOrDie(),
  121. // Used to create a certificatesigningrequest for a node-specific client certificate, and watch
  122. // for it to be signed. This allows the kubelet to rotate it's own certificate.
  123. rbacv1helpers.NewRule("create", "get", "list", "watch").Groups(certificatesGroup).Resources("certificatesigningrequests").RuleOrDie(),
  124. // Leases
  125. rbacv1helpers.NewRule("get", "create", "update", "patch", "delete").Groups("coordination.k8s.io").Resources("leases").RuleOrDie(),
  126. // CSI
  127. rbacv1helpers.NewRule("get").Groups(storageGroup).Resources("volumeattachments").RuleOrDie(),
  128. }
  129. if utilfeature.DefaultFeatureGate.Enabled(features.ExpandPersistentVolumes) {
  130. // Use the Node authorization mode to limit a node to update status of pvc objects referenced by pods bound to itself.
  131. // Use the NodeRestriction admission plugin to limit a node to just update the status stanza.
  132. pvcStatusPolicyRule := rbacv1helpers.NewRule("get", "update", "patch").Groups(legacyGroup).Resources("persistentvolumeclaims/status").RuleOrDie()
  133. nodePolicyRules = append(nodePolicyRules, pvcStatusPolicyRule)
  134. }
  135. if utilfeature.DefaultFeatureGate.Enabled(features.TokenRequest) {
  136. // Use the Node authorization to limit a node to create tokens for service accounts running on that node
  137. // Use the NodeRestriction admission plugin to limit a node to create tokens bound to pods on that node
  138. tokenRequestRule := rbacv1helpers.NewRule("create").Groups(legacyGroup).Resources("serviceaccounts/token").RuleOrDie()
  139. nodePolicyRules = append(nodePolicyRules, tokenRequestRule)
  140. }
  141. // CSI
  142. if utilfeature.DefaultFeatureGate.Enabled(features.CSIDriverRegistry) {
  143. csiDriverRule := rbacv1helpers.NewRule("get", "watch", "list").Groups("storage.k8s.io").Resources("csidrivers").RuleOrDie()
  144. nodePolicyRules = append(nodePolicyRules, csiDriverRule)
  145. }
  146. if utilfeature.DefaultFeatureGate.Enabled(features.CSINodeInfo) {
  147. csiNodeInfoRule := rbacv1helpers.NewRule("get", "create", "update", "patch", "delete").Groups("storage.k8s.io").Resources("csinodes").RuleOrDie()
  148. nodePolicyRules = append(nodePolicyRules, csiNodeInfoRule)
  149. }
  150. // RuntimeClass
  151. if utilfeature.DefaultFeatureGate.Enabled(features.RuntimeClass) {
  152. nodePolicyRules = append(nodePolicyRules, rbacv1helpers.NewRule("get", "list", "watch").Groups("node.k8s.io").Resources("runtimeclasses").RuleOrDie())
  153. }
  154. return nodePolicyRules
  155. }
  156. // ClusterRoles returns the cluster roles to bootstrap an API server with
  157. func ClusterRoles() []rbacv1.ClusterRole {
  158. roles := []rbacv1.ClusterRole{
  159. {
  160. // a "root" role which can do absolutely anything
  161. ObjectMeta: metav1.ObjectMeta{Name: "cluster-admin"},
  162. Rules: []rbacv1.PolicyRule{
  163. rbacv1helpers.NewRule("*").Groups("*").Resources("*").RuleOrDie(),
  164. rbacv1helpers.NewRule("*").URLs("*").RuleOrDie(),
  165. },
  166. },
  167. {
  168. // a role which provides just enough power to determine if the server is ready and discover API versions for negotiation
  169. ObjectMeta: metav1.ObjectMeta{Name: "system:discovery"},
  170. Rules: []rbacv1.PolicyRule{
  171. rbacv1helpers.NewRule("get").URLs(
  172. "/livez", "/readyz", "/healthz",
  173. "/version", "/version/",
  174. "/openapi", "/openapi/*",
  175. "/api", "/api/*",
  176. "/apis", "/apis/*",
  177. ).RuleOrDie(),
  178. },
  179. },
  180. {
  181. // a role which provides minimal resource access to allow a "normal" user to learn information about themselves
  182. ObjectMeta: metav1.ObjectMeta{Name: "system:basic-user"},
  183. Rules: []rbacv1.PolicyRule{
  184. // TODO add future selfsubjectrulesreview, project request APIs, project listing APIs
  185. rbacv1helpers.NewRule("create").Groups(authorizationGroup).Resources("selfsubjectaccessreviews", "selfsubjectrulesreviews").RuleOrDie(),
  186. },
  187. },
  188. {
  189. // a role which provides just enough power read insensitive cluster information
  190. ObjectMeta: metav1.ObjectMeta{Name: "system:public-info-viewer"},
  191. Rules: []rbacv1.PolicyRule{
  192. rbacv1helpers.NewRule("get").URLs(
  193. "/livez", "/readyz", "/healthz", "/version", "/version/",
  194. ).RuleOrDie(),
  195. },
  196. },
  197. {
  198. // a role for a namespace level admin. It is `edit` plus the power to grant permissions to other users.
  199. ObjectMeta: metav1.ObjectMeta{Name: "admin"},
  200. AggregationRule: &rbacv1.AggregationRule{
  201. ClusterRoleSelectors: []metav1.LabelSelector{
  202. {MatchLabels: map[string]string{"rbac.authorization.k8s.io/aggregate-to-admin": "true"}},
  203. },
  204. },
  205. },
  206. {
  207. // a role for a namespace level editor. It grants access to all user level actions in a namespace.
  208. // It does not grant powers for "privileged" resources which are domain of the system: `/status`
  209. // subresources or `quota`/`limits` which are used to control namespaces
  210. ObjectMeta: metav1.ObjectMeta{Name: "edit", Labels: map[string]string{"rbac.authorization.k8s.io/aggregate-to-admin": "true"}},
  211. AggregationRule: &rbacv1.AggregationRule{
  212. ClusterRoleSelectors: []metav1.LabelSelector{
  213. {MatchLabels: map[string]string{"rbac.authorization.k8s.io/aggregate-to-edit": "true"}},
  214. },
  215. },
  216. },
  217. {
  218. // a role for namespace level viewing. It grants Read-only access to non-escalating resources in
  219. // a namespace.
  220. ObjectMeta: metav1.ObjectMeta{Name: "view", Labels: map[string]string{"rbac.authorization.k8s.io/aggregate-to-edit": "true"}},
  221. AggregationRule: &rbacv1.AggregationRule{
  222. ClusterRoleSelectors: []metav1.LabelSelector{
  223. {MatchLabels: map[string]string{"rbac.authorization.k8s.io/aggregate-to-view": "true"}},
  224. },
  225. },
  226. },
  227. {
  228. // a role for a namespace level admin. It is `edit` plus the power to grant permissions to other users.
  229. ObjectMeta: metav1.ObjectMeta{Name: "system:aggregate-to-admin", Labels: map[string]string{"rbac.authorization.k8s.io/aggregate-to-admin": "true"}},
  230. Rules: []rbacv1.PolicyRule{
  231. // additional admin powers
  232. rbacv1helpers.NewRule("create").Groups(authorizationGroup).Resources("localsubjectaccessreviews").RuleOrDie(),
  233. rbacv1helpers.NewRule(ReadWrite...).Groups(rbacGroup).Resources("roles", "rolebindings").RuleOrDie(),
  234. },
  235. },
  236. {
  237. // a role for a namespace level editor. It grants access to all user level actions in a namespace.
  238. // It does not grant powers for "privileged" resources which are domain of the system: `/status`
  239. // subresources or `quota`/`limits` which are used to control namespaces
  240. ObjectMeta: metav1.ObjectMeta{Name: "system:aggregate-to-edit", Labels: map[string]string{"rbac.authorization.k8s.io/aggregate-to-edit": "true"}},
  241. Rules: []rbacv1.PolicyRule{
  242. // Allow read on escalating resources
  243. rbacv1helpers.NewRule(Read...).Groups(legacyGroup).Resources("pods/attach", "pods/proxy", "pods/exec", "pods/portforward", "secrets", "services/proxy").RuleOrDie(),
  244. rbacv1helpers.NewRule("impersonate").Groups(legacyGroup).Resources("serviceaccounts").RuleOrDie(),
  245. rbacv1helpers.NewRule(Write...).Groups(legacyGroup).Resources("pods", "pods/attach", "pods/proxy", "pods/exec", "pods/portforward").RuleOrDie(),
  246. rbacv1helpers.NewRule(Write...).Groups(legacyGroup).Resources("replicationcontrollers", "replicationcontrollers/scale", "serviceaccounts",
  247. "services", "services/proxy", "endpoints", "persistentvolumeclaims", "configmaps", "secrets").RuleOrDie(),
  248. rbacv1helpers.NewRule(Write...).Groups(appsGroup).Resources(
  249. "statefulsets", "statefulsets/scale",
  250. "daemonsets",
  251. "deployments", "deployments/scale", "deployments/rollback",
  252. "replicasets", "replicasets/scale").RuleOrDie(),
  253. rbacv1helpers.NewRule(Write...).Groups(autoscalingGroup).Resources("horizontalpodautoscalers").RuleOrDie(),
  254. rbacv1helpers.NewRule(Write...).Groups(batchGroup).Resources("jobs", "cronjobs").RuleOrDie(),
  255. rbacv1helpers.NewRule(Write...).Groups(extensionsGroup).Resources("daemonsets",
  256. "deployments", "deployments/scale", "deployments/rollback", "ingresses",
  257. "replicasets", "replicasets/scale", "replicationcontrollers/scale",
  258. "networkpolicies").RuleOrDie(),
  259. rbacv1helpers.NewRule(Write...).Groups(policyGroup).Resources("poddisruptionbudgets").RuleOrDie(),
  260. rbacv1helpers.NewRule(Write...).Groups(networkingGroup).Resources("networkpolicies", "ingresses").RuleOrDie(),
  261. },
  262. },
  263. {
  264. // a role for namespace level viewing. It grants Read-only access to non-escalating resources in
  265. // a namespace.
  266. ObjectMeta: metav1.ObjectMeta{Name: "system:aggregate-to-view", Labels: map[string]string{"rbac.authorization.k8s.io/aggregate-to-view": "true"}},
  267. Rules: []rbacv1.PolicyRule{
  268. rbacv1helpers.NewRule(Read...).Groups(legacyGroup).Resources("pods", "replicationcontrollers", "replicationcontrollers/scale", "serviceaccounts",
  269. "services", "services/status", "endpoints", "persistentvolumeclaims", "persistentvolumeclaims/status", "configmaps").RuleOrDie(),
  270. rbacv1helpers.NewRule(Read...).Groups(legacyGroup).Resources("limitranges", "resourcequotas", "bindings", "events",
  271. "pods/status", "resourcequotas/status", "namespaces/status", "replicationcontrollers/status", "pods/log").RuleOrDie(),
  272. // read access to namespaces at the namespace scope means you can read *this* namespace. This can be used as an
  273. // indicator of which namespaces you have access to.
  274. rbacv1helpers.NewRule(Read...).Groups(legacyGroup).Resources("namespaces").RuleOrDie(),
  275. rbacv1helpers.NewRule(Read...).Groups(appsGroup).Resources(
  276. "controllerrevisions",
  277. "statefulsets", "statefulsets/status", "statefulsets/scale",
  278. "daemonsets", "daemonsets/status",
  279. "deployments", "deployments/status", "deployments/scale",
  280. "replicasets", "replicasets/status", "replicasets/scale").RuleOrDie(),
  281. rbacv1helpers.NewRule(Read...).Groups(autoscalingGroup).Resources("horizontalpodautoscalers", "horizontalpodautoscalers/status").RuleOrDie(),
  282. rbacv1helpers.NewRule(Read...).Groups(batchGroup).Resources("jobs", "cronjobs", "cronjobs/status", "jobs/status").RuleOrDie(),
  283. rbacv1helpers.NewRule(Read...).Groups(extensionsGroup).Resources("daemonsets", "daemonsets/status", "deployments", "deployments/scale", "deployments/status",
  284. "ingresses", "ingresses/status", "replicasets", "replicasets/scale", "replicasets/status", "replicationcontrollers/scale",
  285. "networkpolicies").RuleOrDie(),
  286. rbacv1helpers.NewRule(Read...).Groups(policyGroup).Resources("poddisruptionbudgets", "poddisruptionbudgets/status").RuleOrDie(),
  287. rbacv1helpers.NewRule(Read...).Groups(networkingGroup).Resources("networkpolicies", "ingresses", "ingresses/status").RuleOrDie(),
  288. },
  289. },
  290. {
  291. // a role to use for heapster's connections back to the API server
  292. ObjectMeta: metav1.ObjectMeta{Name: "system:heapster"},
  293. Rules: []rbacv1.PolicyRule{
  294. rbacv1helpers.NewRule(Read...).Groups(legacyGroup).Resources("events", "pods", "nodes", "namespaces").RuleOrDie(),
  295. rbacv1helpers.NewRule(Read...).Groups(extensionsGroup).Resources("deployments").RuleOrDie(),
  296. },
  297. },
  298. {
  299. // a role for nodes to use to have the access they need for running pods
  300. ObjectMeta: metav1.ObjectMeta{Name: "system:node"},
  301. Rules: NodeRules(),
  302. },
  303. {
  304. // a role to use for node-problem-detector access. It does not get bound to default location since
  305. // deployment locations can reasonably vary.
  306. ObjectMeta: metav1.ObjectMeta{Name: "system:node-problem-detector"},
  307. Rules: []rbacv1.PolicyRule{
  308. rbacv1helpers.NewRule("get").Groups(legacyGroup).Resources("nodes").RuleOrDie(),
  309. rbacv1helpers.NewRule("patch").Groups(legacyGroup).Resources("nodes/status").RuleOrDie(),
  310. eventsRule(),
  311. },
  312. },
  313. {
  314. // a role to use for full access to the kubelet API
  315. ObjectMeta: metav1.ObjectMeta{Name: "system:kubelet-api-admin"},
  316. Rules: []rbacv1.PolicyRule{
  317. // Allow read-only access to the Node API objects
  318. rbacv1helpers.NewRule("get", "list", "watch").Groups(legacyGroup).Resources("nodes").RuleOrDie(),
  319. // Allow all API calls to the nodes
  320. rbacv1helpers.NewRule("proxy").Groups(legacyGroup).Resources("nodes").RuleOrDie(),
  321. rbacv1helpers.NewRule("*").Groups(legacyGroup).Resources("nodes/proxy", "nodes/metrics", "nodes/spec", "nodes/stats", "nodes/log").RuleOrDie(),
  322. },
  323. },
  324. {
  325. // a role to use for bootstrapping a node's client certificates
  326. ObjectMeta: metav1.ObjectMeta{Name: "system:node-bootstrapper"},
  327. Rules: []rbacv1.PolicyRule{
  328. // used to create a certificatesigningrequest for a node-specific client certificate, and watch for it to be signed
  329. rbacv1helpers.NewRule("create", "get", "list", "watch").Groups(certificatesGroup).Resources("certificatesigningrequests").RuleOrDie(),
  330. },
  331. },
  332. {
  333. // a role to use for allowing authentication and authorization delegation
  334. ObjectMeta: metav1.ObjectMeta{Name: "system:auth-delegator"},
  335. Rules: []rbacv1.PolicyRule{
  336. // These creates are non-mutating
  337. rbacv1helpers.NewRule("create").Groups(authenticationGroup).Resources("tokenreviews").RuleOrDie(),
  338. rbacv1helpers.NewRule("create").Groups(authorizationGroup).Resources("subjectaccessreviews").RuleOrDie(),
  339. },
  340. },
  341. {
  342. // a role to use for the API registry, summarization, and proxy handling
  343. ObjectMeta: metav1.ObjectMeta{Name: "system:kube-aggregator"},
  344. Rules: []rbacv1.PolicyRule{
  345. // it needs to see all services so that it knows whether the ones it points to exist or not
  346. rbacv1helpers.NewRule(Read...).Groups(legacyGroup).Resources("services", "endpoints").RuleOrDie(),
  347. },
  348. },
  349. {
  350. // a role to use for bootstrapping the kube-controller-manager so it can create the shared informers
  351. // service accounts, and secrets that we need to create separate identities for other controllers
  352. ObjectMeta: metav1.ObjectMeta{Name: "system:kube-controller-manager"},
  353. Rules: []rbacv1.PolicyRule{
  354. eventsRule(),
  355. // Needed for leader election.
  356. rbacv1helpers.NewRule("create").Groups(coordinationGroup).Resources("leases").RuleOrDie(),
  357. rbacv1helpers.NewRule("get", "update").Groups(coordinationGroup).Resources("leases").Names("kube-controller-manager").RuleOrDie(),
  358. // TODO: Remove once we fully migrate to lease in leader-election.
  359. rbacv1helpers.NewRule("create").Groups(legacyGroup).Resources("endpoints").RuleOrDie(),
  360. rbacv1helpers.NewRule("get", "update").Groups(legacyGroup).Resources("endpoints").Names("kube-controller-manager").RuleOrDie(),
  361. // Fundamental resources.
  362. rbacv1helpers.NewRule("create").Groups(legacyGroup).Resources("secrets", "serviceaccounts").RuleOrDie(),
  363. rbacv1helpers.NewRule("delete").Groups(legacyGroup).Resources("secrets").RuleOrDie(),
  364. rbacv1helpers.NewRule("get").Groups(legacyGroup).Resources("namespaces", "secrets", "serviceaccounts", "configmaps").RuleOrDie(),
  365. rbacv1helpers.NewRule("update").Groups(legacyGroup).Resources("secrets", "serviceaccounts").RuleOrDie(),
  366. // Needed to check API access. These creates are non-mutating
  367. rbacv1helpers.NewRule("create").Groups(authenticationGroup).Resources("tokenreviews").RuleOrDie(),
  368. rbacv1helpers.NewRule("create").Groups(authorizationGroup).Resources("subjectaccessreviews").RuleOrDie(),
  369. // Needed for all shared informers
  370. rbacv1helpers.NewRule("list", "watch").Groups("*").Resources("*").RuleOrDie(),
  371. rbacv1helpers.NewRule("create").Groups(legacyGroup).Resources("serviceaccounts/token").RuleOrDie(),
  372. },
  373. },
  374. {
  375. // a role to use for the kube-dns pod
  376. ObjectMeta: metav1.ObjectMeta{Name: "system:kube-dns"},
  377. Rules: []rbacv1.PolicyRule{
  378. rbacv1helpers.NewRule("list", "watch").Groups(legacyGroup).Resources("endpoints", "services").RuleOrDie(),
  379. },
  380. },
  381. {
  382. // a role for an external/out-of-tree persistent volume provisioner
  383. ObjectMeta: metav1.ObjectMeta{Name: "system:persistent-volume-provisioner"},
  384. Rules: []rbacv1.PolicyRule{
  385. rbacv1helpers.NewRule("get", "list", "watch", "create", "delete").Groups(legacyGroup).Resources("persistentvolumes").RuleOrDie(),
  386. // update is needed in addition to read access for setting lock annotations on PVCs
  387. rbacv1helpers.NewRule("get", "list", "watch", "update").Groups(legacyGroup).Resources("persistentvolumeclaims").RuleOrDie(),
  388. rbacv1helpers.NewRule(Read...).Groups(storageGroup).Resources("storageclasses").RuleOrDie(),
  389. // Needed for watching provisioning success and failure events
  390. rbacv1helpers.NewRule("watch").Groups(legacyGroup).Resources("events").RuleOrDie(),
  391. eventsRule(),
  392. },
  393. },
  394. {
  395. // a role making the csrapprover controller approve a node client CSR
  396. ObjectMeta: metav1.ObjectMeta{Name: "system:certificates.k8s.io:certificatesigningrequests:nodeclient"},
  397. Rules: []rbacv1.PolicyRule{
  398. rbacv1helpers.NewRule("create").Groups(certificatesGroup).Resources("certificatesigningrequests/nodeclient").RuleOrDie(),
  399. },
  400. },
  401. {
  402. // a role making the csrapprover controller approve a node client CSR requested by the node itself
  403. ObjectMeta: metav1.ObjectMeta{Name: "system:certificates.k8s.io:certificatesigningrequests:selfnodeclient"},
  404. Rules: []rbacv1.PolicyRule{
  405. rbacv1helpers.NewRule("create").Groups(certificatesGroup).Resources("certificatesigningrequests/selfnodeclient").RuleOrDie(),
  406. },
  407. },
  408. {
  409. ObjectMeta: metav1.ObjectMeta{Name: "system:volume-scheduler"},
  410. Rules: []rbacv1.PolicyRule{
  411. rbacv1helpers.NewRule(ReadUpdate...).Groups(legacyGroup).Resources("persistentvolumes").RuleOrDie(),
  412. rbacv1helpers.NewRule(Read...).Groups(storageGroup).Resources("storageclasses").RuleOrDie(),
  413. rbacv1helpers.NewRule(ReadUpdate...).Groups(legacyGroup).Resources("persistentvolumeclaims").RuleOrDie(),
  414. },
  415. },
  416. }
  417. // node-proxier role is used by kube-proxy.
  418. nodeProxierRules := []rbacv1.PolicyRule{
  419. rbacv1helpers.NewRule("list", "watch").Groups(legacyGroup).Resources("services", "endpoints").RuleOrDie(),
  420. rbacv1helpers.NewRule("get", "list", "watch").Groups(legacyGroup).Resources("nodes").RuleOrDie(),
  421. eventsRule(),
  422. }
  423. if utilfeature.DefaultFeatureGate.Enabled(features.EndpointSlice) {
  424. nodeProxierRules = append(nodeProxierRules, rbacv1helpers.NewRule("list", "watch").Groups(discoveryGroup).Resources("endpointslices").RuleOrDie())
  425. }
  426. roles = append(roles, rbacv1.ClusterRole{
  427. ObjectMeta: metav1.ObjectMeta{Name: "system:node-proxier"},
  428. Rules: nodeProxierRules,
  429. })
  430. kubeSchedulerRules := []rbacv1.PolicyRule{
  431. eventsRule(),
  432. // This is for leaderlease access
  433. // TODO: scope this to the kube-system namespace
  434. rbacv1helpers.NewRule("create").Groups(coordinationGroup).Resources("leases").RuleOrDie(),
  435. rbacv1helpers.NewRule("get", "update").Groups(coordinationGroup).Resources("leases").Names("kube-scheduler").RuleOrDie(),
  436. // TODO: Remove once we fully migrate to lease in leader-election.
  437. rbacv1helpers.NewRule("create").Groups(legacyGroup).Resources("endpoints").RuleOrDie(),
  438. rbacv1helpers.NewRule("get", "update").Groups(legacyGroup).Resources("endpoints").Names("kube-scheduler").RuleOrDie(),
  439. // Fundamental resources
  440. rbacv1helpers.NewRule(Read...).Groups(legacyGroup).Resources("nodes").RuleOrDie(),
  441. rbacv1helpers.NewRule("get", "list", "watch", "delete").Groups(legacyGroup).Resources("pods").RuleOrDie(),
  442. rbacv1helpers.NewRule("create").Groups(legacyGroup).Resources("pods/binding", "bindings").RuleOrDie(),
  443. rbacv1helpers.NewRule("patch", "update").Groups(legacyGroup).Resources("pods/status").RuleOrDie(),
  444. // Things that select pods
  445. rbacv1helpers.NewRule(Read...).Groups(legacyGroup).Resources("services", "replicationcontrollers").RuleOrDie(),
  446. rbacv1helpers.NewRule(Read...).Groups(appsGroup, extensionsGroup).Resources("replicasets").RuleOrDie(),
  447. rbacv1helpers.NewRule(Read...).Groups(appsGroup).Resources("statefulsets").RuleOrDie(),
  448. // Things that pods use or applies to them
  449. rbacv1helpers.NewRule(Read...).Groups(policyGroup).Resources("poddisruptionbudgets").RuleOrDie(),
  450. rbacv1helpers.NewRule(Read...).Groups(legacyGroup).Resources("persistentvolumeclaims", "persistentvolumes").RuleOrDie(),
  451. // Needed to check API access. These creates are non-mutating
  452. rbacv1helpers.NewRule("create").Groups(authenticationGroup).Resources("tokenreviews").RuleOrDie(),
  453. rbacv1helpers.NewRule("create").Groups(authorizationGroup).Resources("subjectaccessreviews").RuleOrDie(),
  454. // Needed for volume limits
  455. rbacv1helpers.NewRule(Read...).Groups(storageGroup).Resources("csinodes").RuleOrDie(),
  456. }
  457. roles = append(roles, rbacv1.ClusterRole{
  458. // a role to use for the kube-scheduler
  459. ObjectMeta: metav1.ObjectMeta{Name: "system:kube-scheduler"},
  460. Rules: kubeSchedulerRules,
  461. })
  462. addClusterRoleLabel(roles)
  463. return roles
  464. }
  465. const systemNodeRoleName = "system:node"
  466. // ClusterRoleBindings return default rolebindings to the default roles
  467. func ClusterRoleBindings() []rbacv1.ClusterRoleBinding {
  468. rolebindings := []rbacv1.ClusterRoleBinding{
  469. rbacv1helpers.NewClusterBinding("cluster-admin").Groups(user.SystemPrivilegedGroup).BindingOrDie(),
  470. rbacv1helpers.NewClusterBinding("system:discovery").Groups(user.AllAuthenticated).BindingOrDie(),
  471. rbacv1helpers.NewClusterBinding("system:basic-user").Groups(user.AllAuthenticated).BindingOrDie(),
  472. rbacv1helpers.NewClusterBinding("system:public-info-viewer").Groups(user.AllAuthenticated, user.AllUnauthenticated).BindingOrDie(),
  473. rbacv1helpers.NewClusterBinding("system:node-proxier").Users(user.KubeProxy).BindingOrDie(),
  474. rbacv1helpers.NewClusterBinding("system:kube-controller-manager").Users(user.KubeControllerManager).BindingOrDie(),
  475. rbacv1helpers.NewClusterBinding("system:kube-dns").SAs("kube-system", "kube-dns").BindingOrDie(),
  476. rbacv1helpers.NewClusterBinding("system:kube-scheduler").Users(user.KubeScheduler).BindingOrDie(),
  477. rbacv1helpers.NewClusterBinding("system:volume-scheduler").Users(user.KubeScheduler).BindingOrDie(),
  478. // This default binding of the system:node role to the system:nodes group is deprecated in 1.7 with the availability of the Node authorizer.
  479. // This leaves the binding, but with an empty set of subjects, so that tightening reconciliation can remove the subject.
  480. {
  481. ObjectMeta: metav1.ObjectMeta{Name: systemNodeRoleName},
  482. RoleRef: rbacv1.RoleRef{APIGroup: rbacv1.GroupName, Kind: "ClusterRole", Name: systemNodeRoleName},
  483. },
  484. }
  485. addClusterRoleBindingLabel(rolebindings)
  486. return rolebindings
  487. }
  488. func ClusterRolesToAggregate() map[string]string {
  489. return map[string]string{
  490. "admin": "system:aggregate-to-admin",
  491. "edit": "system:aggregate-to-edit",
  492. "view": "system:aggregate-to-view",
  493. }
  494. }
  495. // ClusterRoleBindingsToSplit returns a map of Names of source ClusterRoleBindings
  496. // to copy Subjects, Annotations, and Labels to destination ClusterRoleBinding templates.
  497. func ClusterRoleBindingsToSplit() map[string]rbacv1.ClusterRoleBinding {
  498. bindingsToSplit := map[string]rbacv1.ClusterRoleBinding{}
  499. for _, defaultClusterRoleBinding := range ClusterRoleBindings() {
  500. switch defaultClusterRoleBinding.Name {
  501. case "system:public-info-viewer":
  502. bindingsToSplit["system:discovery"] = defaultClusterRoleBinding
  503. }
  504. }
  505. return bindingsToSplit
  506. }