|
| 1 | +--- |
| 2 | +title: 基于角色的访问控制良好实践 |
| 3 | +description: > |
| 4 | + 为集群操作人员提供的良好的 RBAC 设计原则和实践。 |
| 5 | +content_type: concept |
| 6 | +--- |
| 7 | + |
| 8 | +<!-- |
| 9 | +reviewers: |
| 10 | +title: Role Based Access Control Good Practices |
| 11 | +description: > |
| 12 | + Principles and practices for good RBAC design for cluster operators. |
| 13 | +content_type: concept |
| 14 | +--> |
| 15 | + |
| 16 | +<!-- overview --> |
| 17 | + |
| 18 | +<!-- |
| 19 | +Kubernetes {{< glossary_tooltip text="RBAC" term_id="rbac" >}} is a key security control |
| 20 | +to ensure that cluster users and workloads have only the access to resources required to |
| 21 | +execute their roles. It is important to ensure that, when designing permissions for cluster |
| 22 | +users, the cluster administrator understands the areas where privilge escalation could occur, |
| 23 | +to reduce the risk of excessive access leading to security incidents. |
| 24 | +
|
| 25 | +The good practices laid out here should be read in conjunction with the general [RBAC documentation](/docs/reference/access-authn-authz/rbac/#restrictions-on-role-creation-or-update). |
| 26 | +--> |
| 27 | + |
| 28 | +Kubernetes {{< glossary_tooltip text="RBAC" term_id="rbac" >}} |
| 29 | +是一项重要的安全控制措施,用于保证集群用户和工作负载只能访问履行自身角色所需的资源。 |
| 30 | +在为集群用户设计权限时,请务必确保集群管理员知道可能发生特权提级的地方, |
| 31 | +降低因过多权限而导致安全事件的风险。 |
| 32 | + |
| 33 | +此文档的良好实践应该与通用 |
| 34 | +[RBAC 文档](/zh/docs/reference/access-authn-authz/rbac/#restrictions-on-role-creation-or-update)一起阅读。 |
| 35 | + |
| 36 | +<!-- body --> |
| 37 | + |
| 38 | +<!-- |
| 39 | +## General good practice |
| 40 | +
|
| 41 | +### Least privilege |
| 42 | +--> |
| 43 | +## 通用的良好实践 {#general-good-practice} |
| 44 | + |
| 45 | +### 最小特权 {#least-privilege} |
| 46 | + |
| 47 | +<!-- |
| 48 | +Ideally minimal RBAC rights should be assigned to users and service accounts. Only permissions |
| 49 | +explicitly required for their operation should be used. Whilst each cluster will be different, |
| 50 | +some general rules that can be applied are : |
| 51 | +--> |
| 52 | +理想情况下,分配给用户和服务帐户的 RBAC 权限应该是最小的。 |
| 53 | +仅应使用操作明确需要的权限,虽然每个集群会有所不同,但可以应用的一些常规规则: |
| 54 | + |
| 55 | +<!-- |
| 56 | + - Assign permissions at the namespace level where possible. Use RoleBindings as opposed to |
| 57 | + ClusterRoleBindings to give users rights only within a specific namespace. |
| 58 | + - Avoid providing wildcard permissions when possible, especially to all resources. |
| 59 | + As Kubernetes is an extensible system, providing wildcard access gives rights |
| 60 | + not just to all object types presently in the cluster, but also to all future object types |
| 61 | + which are created in the future. |
| 62 | + - Administrators should not use `cluster-admin` accounts except where specifically needed. |
| 63 | + Providing a low privileged account with [impersonation rights](/docs/reference/access-authn-authz/authentication/#user-impersonation) |
| 64 | + can avoid accidental modification of cluster resources. |
| 65 | + - Avoid adding users to the `system:masters` group. Any user who is a member of this group |
| 66 | + bypasses all RBAC rights checks and will always have unrestricted superuser access, which cannot be |
| 67 | + revoked by removing Role Bindings or Cluster Role Bindings. As an aside, if a cluster is |
| 68 | + using an authorization webhook, membership of this group also bypasses that webhook (requests |
| 69 | + from users who are members of that group are never sent to the webhook) |
| 70 | +--> |
| 71 | +- 尽可能在命名空间级别分配权限。授予用户在特定命名空间中的权限时使用 RoleBinding |
| 72 | + 而不是 ClusterRoleBinding。 |
| 73 | +- 尽可能避免通过通配符设置权限,尤其是对所有资源的权限。 |
| 74 | + 由于 Kubernetes 是一个可扩展的系统,因此通过通配符来授予访问权限不仅会授予集群中当前的所有对象类型, |
| 75 | + 还包含所有未来被创建的所有对象类型。 |
| 76 | +- 管理员不应使用 `cluster-admin` 账号,除非特别需要。为低特权帐户提供 |
| 77 | + [伪装权限](/zh/docs/reference/access-authn-authz/authentication/#user-impersonation) |
| 78 | + 可以避免意外修改集群资源。 |
| 79 | +- 避免将用户添加到 `system:masters` 组。任何属于此组成员的用户都会绕过所有 RBAC 权限检查, |
| 80 | + 始终具有不受限制的超级用户访问权限,并且不能通过删除 `RoleBinding` 或 `ClusterRoleBinding` |
| 81 | + 来取消其权限。顺便说一句,如果集群是使用 Webhook 鉴权,此组的成员身份也会绕过该 |
| 82 | + Webhook(来自属于该组成员的用户的请求永远不会发送到 Webhook)。 |
| 83 | + |
| 84 | +<!-- |
| 85 | +### Minimize distribution of privileged tokens |
| 86 | +--> |
| 87 | +### 最大限度地减少特权令牌的分发 {#minimize-distribution-of-privileged-tokens} |
| 88 | + |
| 89 | +<!-- |
| 90 | +Ideally, pods shouldn't be assigned service accounts that have been granted powerful permissions (for example, any of the rights listed under |
| 91 | +[privilege escalation risks](#privilege-escalation-risks)). |
| 92 | +In cases where a workload requires powerful permissions, consider the following practices: |
| 93 | + - Limit the number of nodes running powerful pods. Ensure that any DaemonSets you run |
| 94 | + are necessary and are run with least privilege to limit the blast radius of container escapes. |
| 95 | + - Avoid running powerful pods alongside untrusted or publicly-exposed ones. Consider using |
| 96 | + [Taints and Toleration](/docs/concepts/scheduling-eviction/taint-and-toleration/), [NodeAffinity](/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity), or [PodAntiAffinity](/docs/concepts/scheduling-eviction/assign-pod-node/#inter-pod-affinity-and-anti-affinity) to ensure |
| 97 | + pods don't run alongside untrusted or less-trusted Pods. Pay especial attention to |
| 98 | + situations where less-trustworthy Pods are not meeting the **Restricted** Pod Security Standard. |
| 99 | +--> |
| 100 | +理想情况下,不应为 Pod 分配具有强大权限(例如,在[特权提级的风险](#privilege-escalation-risks)中列出的任一权限)的服务帐户。 |
| 101 | +如果工作负载需要比较大的权限,请考虑以下做法: |
| 102 | +- 限制运行此类 Pod 的节点数量。确保你运行的任何 DaemonSet 都是必需的, |
| 103 | + 并且以最小权限运行,以限制容器逃逸的影响范围。 |
| 104 | +- 避免将此类 Pod 与不可信任或公开的 Pod 在一起运行。 |
| 105 | + 考虑使用[污点和容忍度](/zh/docs/concepts/scheduling-eviction/taint-and-toleration/)、 |
| 106 | + [节点亲和性](/zh/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity)或 |
| 107 | + [Pod 反亲和性](/zh/docs/concepts/scheduling-eviction/assign-pod-node/#inter-pod-affinity-and-anti-affinity)确保 |
| 108 | + Pod 不会与不可信或不太受信任的 Pod 一起运行。 |
| 109 | + 特别注意可信度不高的 Pod 不符合 **Restricted** Pod 安全标准的情况。 |
| 110 | +<!-- |
| 111 | +### Hardening |
| 112 | +
|
| 113 | +Kubernetes defaults to providing access which may not be required in every cluster. Reviewing |
| 114 | +the RBAC rights provided by default can provide opportunities for security hardening. |
| 115 | +In general, changes should not be made to rights provided to `system:` accounts some options |
| 116 | +to harden cluster rights exist: |
| 117 | +--> |
| 118 | +### 加固 {#hardening} |
| 119 | + |
| 120 | +Kubernetes 默认提供访问权限并非是每个集群都需要的。 |
| 121 | +审查默认提供的 RBAC 权限为安全加固提供了机会。 |
| 122 | +一般来说,不应该更改 `system:` 帐户的某些权限,有一些方式来强化现有集群的权限: |
| 123 | + |
| 124 | +<!-- |
| 125 | +- Review bindings for the `system:unauthenticated` group and remove where possible, as this gives |
| 126 | + access to anyone who can contact the API server at a network level. |
| 127 | +- Avoid the default auto-mounting of service account tokens by setting |
| 128 | + `automountServiceAccountToken: false`. For more details, see |
| 129 | + [using default service account token](/docs/tasks/configure-pod-container/configure-service-account/#use-the-default-service-account-to-access-the-api-server). |
| 130 | + Setting this value for a Pod will overwrite the service account setting, workloads |
| 131 | + which require service account tokens can still mount them. |
| 132 | +--> |
| 133 | +- 审查 `system:unauthenticated` 组的绑定,并在可能的情况下将其删除, |
| 134 | + 因为这会给所有能够访问 API 服务器的人以网络级别的权限。 |
| 135 | +- 通过设置 `automountServiceAccountToken: false` 来避免服务账号令牌的默认自动挂载, |
| 136 | + 有关更多详细信息,请参阅[使用默认服务账号令牌](/zh/docs/tasks/configure-pod-container/configure-service-account/#use-the-default-service-account-to-access-the-api-server)。 |
| 137 | + 此参数可覆盖 Pod 服务账号设置,而需要服务账号令牌的工作负载仍可以挂载。 |
| 138 | + |
| 139 | +<!-- |
| 140 | +### Periodic review |
| 141 | +
|
| 142 | +It is vital to periodically review the Kubernetes RBAC settings for redundant entries and |
| 143 | +possible privilege escalations. |
| 144 | +If an attacker is able to create a user account with the same name as a deleted user, |
| 145 | +they can automatically inherit all the rights of the deleted user, especially the |
| 146 | +rights assigned to that user. |
| 147 | + --> |
| 148 | +### 定期检查 {#periodic-review} |
| 149 | +定期检查 Kubernetes RBAC 设置是否有冗余条目和提权可能性是至关重要的。 |
| 150 | +如果攻击者能够创建与已删除用户同名的用户账号, |
| 151 | +他们可以自动继承被删除用户的所有权限,尤其是分配给该用户的权限。 |
| 152 | + |
| 153 | +<!-- |
| 154 | +## Kubernetes RBAC - privilege escalation risks {#privilege-escalation-risks} |
| 155 | +
|
| 156 | +Within Kubernetes RBAC there are a number of privileges which, if granted, can allow a user or a service account |
| 157 | +to escalate their privileges in the cluster or affect systems outside the cluster. |
| 158 | +
|
| 159 | +This section is intended to provide visibility of the areas where cluster operators |
| 160 | +should take care, to ensure that they do not inadvertantly allow for more access to clusters than intended. |
| 161 | +--> |
| 162 | +## Kubernetes RBAC - 权限提权的风险 {#privilege-escalation-risks} |
| 163 | + |
| 164 | +在 Kubernetes RBAC 中有许多特权,如果被授予, |
| 165 | +用户或服务帐户可以提升其在集群中的权限并可能影响集群外的系统。 |
| 166 | + |
| 167 | +本节旨在提醒集群操作员需要注意的不同领域, |
| 168 | +以确保他们不会无意中授予超出预期的集群访问权限。 |
| 169 | + |
| 170 | +<!-- |
| 171 | +### Listing secrets |
| 172 | +
|
| 173 | +It is generally clear that allowing `get` access on Secrets will allow a user to read their contents. |
| 174 | +It is also important to note that `list` and `watch` access also effectively allow for users to reveal the Secret contents. |
| 175 | +For example, when a List response is returned (for example, via `kubectl get secrets -A -o yaml`), the response |
| 176 | +includes the contents of all Secrets. |
| 177 | +--> |
| 178 | +### 列举 Secret {#listing-secrets} |
| 179 | + |
| 180 | +大家都很清楚,若允许对 Secrets 执行 `get` 访问,用户就获得了访问 Secret 内容的能力。 |
| 181 | +同样需要注意的是:`list` 和 `watch` 访问也会授权用户获取 Secret 的内容。 |
| 182 | +例如,当返回 List 响应时(例如,通过 |
| 183 | +`kubectl get secrets -A -o yaml`),响应包含所有 Secret 的内容。 |
| 184 | + |
| 185 | +<!-- |
| 186 | +### Workload creation |
| 187 | +
|
| 188 | +Users who are able to create workloads (either Pods, or |
| 189 | +[workload resources](/docs/concepts/workloads/controllers/) that manage Pods) will |
| 190 | +be able to gain access to the underlying node unless restrictions based on the Kubernetes |
| 191 | +[Pod Security Standards](/docs/concepts/security/pod-security-standards/) are in place. |
| 192 | +--> |
| 193 | +### 工作负载的创建 {#workload-creation} |
| 194 | + |
| 195 | +能够创建工作负载的用户(Pod 或管理 Pod 的[工作负载资源](/zh/docs/concepts/workloads/controllers/)) |
| 196 | +能够访问下层的节点,除非基于 Kubernetes 的 |
| 197 | +[Pod 安全标准](/zh/docs/concepts/security/pod-security-standards/)做限制。 |
| 198 | + |
| 199 | +<!-- |
| 200 | +Users who can run privileged Pods can use that access to gain node access and potentially to |
| 201 | +further elevate their privileges. Where you do not fully trust a user or other principal |
| 202 | +with the ability to create suitably secure and isolated Pods, you should enforce either the |
| 203 | +**Baseline** or **Restricted** Pod Security Standard. |
| 204 | +You can use [Pod Security admission](/docs/concepts/security/pod-security-admission/) |
| 205 | +or other (third party) mechanisms to implement that enforcement. |
| 206 | +--> |
| 207 | +可以运行特权 Pod 的用户可以利用该访问权限获得节点访问权限, |
| 208 | +并可能进一步提升他们的特权。如果你不完全信任某用户或其他主体, |
| 209 | +不相信他们能够创建比较安全且相互隔离的 Pod,你应该强制实施 **Baseline** |
| 210 | +或 **Restricted** Pod 安全标准。 |
| 211 | +你可以使用 [Pod 安全性准入](/zh/docs/concepts/security/pod-security-admission/)或其他(第三方)机制来强制实施这些限制。 |
| 212 | + |
| 213 | +<!-- |
| 214 | +You can also use the deprecated [PodSecurityPolicy](/docs/concepts/policy/pod-security-policy/) mechanism |
| 215 | +to restrict users' abilities to create privileged Pods (N.B. PodSecurityPolicy is scheduled for removal |
| 216 | +in version 1.25). |
| 217 | +--> |
| 218 | +你还可以使用已弃用的 [PodSecurityPolicy](/zh/docs/concepts/policy/pod-security-policy/) |
| 219 | +机制以限制用户创建特权 Pod 的能力 (特别注意:PodSecurityPolicy 已计划在版本 1.25 中删除)。 |
| 220 | + |
| 221 | +<!-- |
| 222 | +Creating a workload in a namespace also grants indirect access to Secrets in that namespace. |
| 223 | +Creating a pod in kube-system or a similarly privileged namespace can grant a user access to |
| 224 | +Secrets they would not have through RBAC directly. |
| 225 | +--> |
| 226 | +在命名空间中创建工作负载还会授予对该命名空间中 Secret 的间接访问权限。 |
| 227 | +在 kube-system 或类似特权的命名空间中创建 Pod |
| 228 | +可以授予用户不需要通过 RBAC 即可获取 Secret 访问权限。 |
| 229 | + |
| 230 | +<!-- |
| 231 | +### Persistent volume creation |
| 232 | +
|
| 233 | +As noted in the [PodSecurityPolicy](/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems) documentation, access to create PersistentVolumes can allow for escalation of access to the underlying host. Where access to persistent storage is required trusted administrators should create |
| 234 | +PersistentVolumes, and constrained users should use PersistentVolumeClaims to access that storage. |
| 235 | +--> |
| 236 | +### 持久卷的创建 {#persistent-volume-creation} |
| 237 | + |
| 238 | +如 [PodSecurityPolicy](/zh/docs/concepts/policy/pod-security-policy/#volumes-and-file-systems) |
| 239 | +文档中所述,创建 PersistentVolumes 的权限可以提权访问底层主机。 |
| 240 | +如果需要访问 PersistentVolume,受信任的管理员应该创建 `PersistentVolume`, |
| 241 | +受约束的用户应该使用 `PersistentVolumeClaim` 访问该存储。 |
| 242 | + |
| 243 | +<!-- |
| 244 | +### Access to `proxy` subresource of Nodes |
| 245 | +
|
| 246 | +Users with access to the proxy sub-resource of node objects have rights to the Kubelet API, |
| 247 | +which allows for command execution on every pod on the node(s) which they have rights to. |
| 248 | +This access bypasses audit logging and admission control, so care should be taken before |
| 249 | +granting rights to this resource. |
| 250 | +--> |
| 251 | +### 访问 Node 的 `proxy` 子资源 {#access-to-proxy-subresource-of-nodes} |
| 252 | + |
| 253 | +有权访问 Node 对象的 proxy 子资源的用户有权访问 Kubelet API, |
| 254 | +这允许在他们有权访问的节点上的所有 Pod 上执行命令。 |
| 255 | +此访问绕过审计日志记录和准入控制,因此在授予对此资源的权限前应小心。 |
| 256 | + |
| 257 | +<!-- |
| 258 | +### Escalate verb |
| 259 | +
|
| 260 | +Generally the RBAC system prevents users from creating clusterroles with more rights than |
| 261 | +they possess. The exception to this is the `escalate` verb. As noted in the [RBAC documentation](/docs/reference/access-authn-authz/rbac/#restrictions-on-role-creation-or-update), |
| 262 | +users with this right can effectively escalate their privileges. |
| 263 | +--> |
| 264 | +### esclate 动词 {#escalate-verb} |
| 265 | +通常,RBAC 系统会阻止用户创建比他所拥有的更多权限的 `ClusterRole`。 |
| 266 | +而 `escalate` 动词是个例外。如 |
| 267 | +[RBAC 文档](/zh/docs/reference/access-authn-authz/rbac/#restrictions-on-role-creation-or-update) |
| 268 | +中所述,拥有此权限的用户可以有效地提升他们的权限。 |
| 269 | + |
| 270 | +<!-- |
| 271 | +### Bind verb |
| 272 | +
|
| 273 | +Similar to the `escalate` verb, granting users this right allows for bypass of Kubernetes |
| 274 | +in-built protections against privilege escalation, allowing users to create bindings to |
| 275 | +roles with rights they do not already have. |
| 276 | +--> |
| 277 | +### bind 动词 {#bind-verb} |
| 278 | + |
| 279 | +与 `escalate` 动作类似,授予此权限的用户可以绕过 Kubernetes |
| 280 | +对权限提升的内置保护,用户可以创建并绑定尚不具有的权限的角色。 |
| 281 | + |
| 282 | +<!-- |
| 283 | +### Impersonate verb |
| 284 | +
|
| 285 | +This verb allows users to impersonate and gain the rights of other users in the cluster. |
| 286 | +Care should be taken when granting it, to ensure that excessive permissions cannot be gained |
| 287 | +via one of the impersonated accounts. |
| 288 | +--> |
| 289 | +### impersonate 动词 {#impersonate-verb} |
| 290 | + |
| 291 | +此动词允许用户伪装并获得集群中其他用户的权限。 |
| 292 | +授予它时应小心,以确保通过其中一个伪装账号不会获得过多的权限。 |
| 293 | + |
| 294 | +<!-- |
| 295 | +### CSRs and certificate issuing |
| 296 | +
|
| 297 | +The CSR API allows for users with `create` rights to CSRs and `update` rights on `certificatesigningrequests/approval` |
| 298 | +where the signer is `kubernetes.io/kube-apiserver-client` to create new client certificates |
| 299 | +which allow users to authenticate to the cluster. Those client certificates can have arbitrary |
| 300 | +names including duplicates of Kubernetes system components. This will effectively allow for privilege escalation. |
| 301 | +--> |
| 302 | +### CSR 和证书颁发 {#csrs-and-certificate-issuing} |
| 303 | + |
| 304 | +CSR API 允许用户拥有 `create` CSR 的权限和 `update` |
| 305 | +`certificatesigningrequests/approval` 的权限, |
| 306 | +其中签名者是 `kubernetes.io/kube-apiserver-client`, |
| 307 | +通过此签名创建的客户端证书允许用户向集群进行身份验证。 |
| 308 | +这些客户端证书可以包含任意的名称,包括 Kubernetes 系统组件的副本。 |
| 309 | +这将有利于特权提级。 |
| 310 | + |
| 311 | +<!-- |
| 312 | +### Token request |
| 313 | +
|
| 314 | +Users with `create` rights on `serviceaccounts/token` can create TokenRequests to issue |
| 315 | +tokens for existing service accounts. |
| 316 | +--> |
| 317 | +### 令牌请求 {#token-request} |
| 318 | + |
| 319 | +拥有 `serviceaccounts/token` 的 `create` 权限的用户可以创建 |
| 320 | +TokenRequest 来发布现有服务帐户的令牌。 |
| 321 | + |
| 322 | +<!-- |
| 323 | +### Control admission webhooks |
| 324 | +
|
| 325 | +Users with control over `validatingwebhookconfigurations` or `mutatingwebhookconfigurations` |
| 326 | +can control webhooks that can read any object admitted to the cluster, and in the case of |
| 327 | +mutating webhooks, also mutate admitted objects. |
| 328 | +--> |
| 329 | +### 控制准入 Webhook {#control-admission-webhooks} |
| 330 | + |
| 331 | +可以控制 `validatingwebhookconfigurations` 或 `mutatingwebhookconfigurations` |
| 332 | +的用户可以控制能读取任何允许进入集群的对象的 webhook, |
| 333 | +并且在有变更 webhook 的情况下,还可以变更准入的对象。 |
| 334 | + |
| 335 | +<!-- |
| 336 | +## Kubernetes RBAC - denial of service risks {#denial-of-service-risks} |
| 337 | +
|
| 338 | +### Object creation denial-of-service {#object-creation-dos} |
| 339 | +Users who have rights to create objects in a cluster may be able to create sufficient large |
| 340 | +objects to create a denial of service condition either based on the size or number of objects, as discussed in |
| 341 | +[etcd used by Kubernetes is vulnerable to OOM attack](https://github.com/kubernetes/kubernetes/issues/107325). This may be |
| 342 | +specifically relevant in multi-tenant clusters if semi-trusted or untrusted users |
| 343 | +are allowed limited access to a system. |
| 344 | +
|
| 345 | +One option for mitigation of this issue would be to use [resource quotas](/docs/concepts/policy/resource-quotas/#object-count-quota) |
| 346 | +to limit the quantity of objects which can be created. |
| 347 | +--> |
| 348 | +## Kubernetes RBAC - 拒绝服务攻击的风险 {#denial-of-service-risks} |
| 349 | + |
| 350 | +### 对象创建拒绝服务 {#object-creation-dos} |
| 351 | +有权在集群中创建对象的用户根据创建对象的大小和数量可能会创建足够大的对象, |
| 352 | +产生拒绝服务状况,如 [Kubernetes 使用的 etcd 容易受到 OOM 攻击](https://github.com/kubernetes/kubernetes/issues/107325)中的讨论。 |
| 353 | +允许太不受信任或者不受信任的用户对系统进行有限的访问在多租户集群中是特别重要的。 |
| 354 | + |
| 355 | +缓解此问题的一种选择是使用[资源配额](/zh/docs/concepts/policy/resource-quotas/#object-count-quota)以限制可以创建的对象数量。 |
0 commit comments