policy.go 29 KB

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