policy.go 29 KB

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