From 4c11f6db87b7ce0f2839607ead2e52b969a33369 Mon Sep 17 00:00:00 2001 From: Qiming Teng Date: Sat, 26 Feb 2022 10:07:47 +0800 Subject: [PATCH] [zh] Translate enforcing PSS This PR translates two files missing in zh localization and syncs two related files for consistency. --- .../concepts/policy/pod-security-policy.md | 12 +- .../security/pod-security-admission.md | 327 ++++++++++++++++++ .../security/pod-security-standards.md | 131 +++---- .../enforcing-pod-security-standards.md | 154 +++++++++ 4 files changed, 543 insertions(+), 81 deletions(-) create mode 100644 content/zh/docs/concepts/security/pod-security-admission.md create mode 100644 content/zh/docs/setup/best-practices/enforcing-pod-security-standards.md diff --git a/content/zh/docs/concepts/policy/pod-security-policy.md b/content/zh/docs/concepts/policy/pod-security-policy.md index 813bfce688..7be672cb0b 100644 --- a/content/zh/docs/concepts/policy/pod-security-policy.md +++ b/content/zh/docs/concepts/policy/pod-security-policy.md @@ -14,12 +14,20 @@ weight: 30 {{< feature-state for_k8s_version="v1.21" state="deprecated" >}} +{{< caution >}} -PodSecurityPolicy 在 Kubernetes v1.21 版本中被弃用,将在 v1.25 中删除。 +PodSecurityPolicy 在 Kubernetes v1.21 版本中被弃用,**将在 v1.25 中删除**。 +我们建议迁移到 [Pod 安全性准入](/zh/docs/concepts/security/pod-security-admission), +或者第三方的准入插件。 +若需了解迁移指南,可参阅[从 PodSecurityPolicy 迁移到内置的 PodSecurity 准入控制器](/zh/docs/tasks/configure-pod-container/migrate-from-psp/)。 关于弃用的更多信息,请查阅 [PodSecurityPolicy Deprecation: Past, Present, and Future](/blog/2021/04/06/podsecuritypolicy-deprecation-past-present-and-future/)。 +{{< /caution >}} + + + +{{< feature-state for_k8s_version="v1.23" state="beta" >}} + + +Kubernetes [Pod 安全性标准(Security Standards)](/zh/docs/concepts/security/pod-security-standards/) +为 Pod 定义不同的隔离级别。这些标准能够让你以一种清晰、一致的方式定义如何限制 Pod 行为。 + + +作为一项 Beta 功能特性,Kubernetes 提供一种内置的 _Pod 安全性_ +{{< glossary_tooltip text="准入控制器" term_id="admission-controller" >}}, +作为 [PodSecurityPolicies](/zh/docs/concepts/policy/pod-security-policy/) +特性的后继演化版本。Pod 安全性限制是在 Pod 被创建时在 +{{< glossary_tooltip text="名字空间" term_id="namespace" >}}层面实施的。 + +{{< note >}} + +PodSecurityPolicy API 已经被废弃,会在 Kubernetes v1.25 发行版中 +[移除](/zh/docs/reference/using-api/deprecation-guide/#v1-25)。 +{{< /note >}} + + + + +## 启用 `PodSecurity` 准入插件 {#enabling-the-podsecurity-admission-plugin} + +在 v1.23 中,`PodSecurity` [特性门控](/zh/docs/reference/command-line-tools-reference/feature-gates/) +是一项 Beta 功能特性,默认被启用。 + +在 v1.22 中,`PodSecurity` [特性门控](/zh/docs/reference/command-line-tools-reference/feature-gates/) +是一项 Alpha 功能特性,必须在 `kube-apiserver` 上启用才能使用内置的准入插件。 + +```shell +--feature-gates="...,PodSecurity=true" +``` + + +## 替代方案:安装 `PodSecurity` 准入 Webhook {#webhook} + +对于无法应用内置 `PodSecurity` 准入插件的环境,无论是因为集群版本低于 v1.22, +或者 `PodSecurity` 特性无法被启用,都可以使用 Beta 版本的 +[验证性准入 Webhook](https://git.k8s.io/pod-security-admission/webhook)。 +来使用 `PodSecurity` 准入逻辑。 + + +在 [https://git.k8s.io/pod-security-admission/webhook](https://git.k8s.io/pod-security-admission/webhook) +上可以找到一个预先构建的容器镜像、证书生成脚本以及一些示例性质的清单。 + +```shell +git clone git@github.com:kubernetes/pod-security-admission.git +cd pod-security-admission/webhook +make certs +kubectl apply -k . +``` + +{{< note >}} + +所生成的证书合法期限为 2 年。在证书过期之前, +需要重新生成证书或者去掉 Webhook 以使用内置的准入查件。 +{{< /note >}} + + +## Pod 安全性级别 {#pod-security-levels} + + +Pod 安全性准入插件对 Pod 的[安全性上下文](/zh/docs/tasks/configure-pod-container/security-context/) +有一定的要求,并且依据 [Pod 安全性标准](/zh/docs/concepts/security/pod-security-standards) +所定义的三个级别(`privileged`、`baseline` 和 `restricted`)对其他字段也有要求。 +关于这些需求的更进一步讨论,请参阅 +[Pod 安全性标准](/zh/docs/concepts/security/pod-security-standards/)页面。 + + +## 为名字空间设置 Pod 安全性准入控制标签 + +一旦特性被启用或者安装了 Webhook,你可以配置名字空间以定义每个名字空间中 +Pod 安全性准入控制模式。 +Kubernetes 定义了一组{{< glossary_tooltip term_id="label" text="标签" >}}, +你可以设置这些标签来定义某个名字空间上要使用的预定义的 Pod 安全性标准级别。 +你所选择的标签定义了检测到潜在违例时,{{< glossary_tooltip text="控制面" term_id="control-plane" >}} +要采取什么样的动作。 + + +{{< table caption="Pod 安全准入模式" >}} +模式 | 描述 +:---------|:------------ +**enforce** | 策略违例会导致 Pod 被拒绝 +**audit** | 策略违例会触发[审计日志](/zh/docs/tasks/debug-application-cluster/audit/)中记录新事件时添加审计注解;但是 Pod 仍是被接受的。 +**warn** | 策略违例会触发用户可见的警告信息,但是 Pod 仍是被接受的。 +{{< /table >}} + + +名字空间可以配置任何一种或者所有模式,或者甚至为不同的模式设置不同的级别。 + +对于每种模式,决定所使用策略的标签有两个: + + +``` +# 针对模式的级别标签用来标示针对该模式所应用的策略级别 +# +# MODE 必须是 `enforce`、`audit` 或 `warn` 之一 +# LEVEL 必须是 `privileged`、baseline` 或 `restricted` 之一 +pod-security.kubernetes.io/: + +# 可选:针对每个模式版本的版本标签可以将策略锁定到 +# 给定 Kubernetes 小版本号所附带的版本(例如 v{{< skew latestVersion >}}) +# +# MODE 必须是 `enforce`、`audit` 或 `warn` 之一 +# VERSION 必须是一个合法的 Kubernetes 小版本号或者 `latest` +pod-security.kubernetes.io/-version: +``` + + +关于用法示例,可参阅 +[使用名字空间标签来强制实施 Pod 安全标准](/zh/docs/tasks/configure-pod-container/enforce-standards-namespace-labels/)。 + + +## 负载资源和 Pod 模板 {#workload-resources-and-pod-templates} + +Pod 通常是通过创建 {{< glossary_tooltip term_id="deployment" >}} 或 +{{< glossary_tooltip term_id="job">}} 这类[工作负载对象](/zh/docs/concepts/workloads/controllers/) +来间接创建的。工作负载对象为工作负载资源定义一个 _Pod 模板_ 和一个对应的 +负责基于该模板来创建 Pod 的{{< glossary_tooltip term_id="controller" text="控制器" >}}。 +为了尽早地捕获违例状况,`audit` 和 `warn` 模式都应用到负载资源。 +不过,`enforce` 模式并 **不** 应用到工作负载资源,仅应用到所生成的 Pod 对象上。 + + +## 豁免 {#exemptions} + +你可以为 Pod 安全性的实施设置 _豁免(Exemptions)_ 规则, +从而允许创建一些本来会被与给定名字空间相关的策略所禁止的 Pod。 +豁免规则可以在[准入控制器配置](/zh/docs/tasks/configure-pod-container/enforce-standards-admission-controller/#configure-the-admission-controller) +中静态配置。 + + +豁免规则可以显式枚举。满足豁免标准的请求会被准入控制器 _忽略_ +(所有 `enforce`、`audit` 和 `warn` 行为都会被略过)。 +豁免的维度包括: + + +- **Username:** 来自用户名已被豁免的、已认证的(或伪装的)的用户的请求会被忽略。 +- **RuntimeClassName:** 指定了已豁免的运行时类名称的 Pod + 和[负载资源](#workload-resources-and-pod-templates)会被忽略。 +- **Namespace:** 位于被豁免的名字空间中的 Pod 和[负载资源](#workload-resources-and-pod-templates) + 会被忽略。 + +{{< caution >}} + +大多数 Pod 是作为对[工作负载资源](#workload-resources-and-pod-templates)的响应, +由控制器所创建的,这意味着为某最终用户提供豁免时,只会当该用户直接创建 Pod +时对其实施安全策略的豁免。用户创建工作负载资源时不会被豁免。 +控制器服务账号(例如:`system:serviceaccount:kube-system:replicaset-controller`) +通常不应该被豁免,因为豁免这类服务账号隐含着对所有能够创建对应工作负载资源的用户豁免。 +{{< /caution >}} + + +策略检查时会对以下 Pod 字段的更新操作予以豁免,这意味着如果 Pod +更新请求仅改变这些字段时,即使 Pod 违反了当前的策略级别,请求也不会被拒绝。 + + +- 除了对 seccomp 或 AppArmor 注解之外的所有 meatadata 更新操作: + - `seccomp.security.alpha.kubernetes.io/pod` (已弃用) + - `container.seccomp.security.alpha.kubernetes.io/*` (已弃用) + - `container.apparmor.security.beta.kubernetes.io/*` +- 对 `.spec.activeDeadlineSeconds` 的合法更新 +- 对 `.spec.tolerations` 的合法更新 + +## {{% heading "whatsnext" %}} + + +- [Pod 安全性标准](/zh/docs/concepts/security/pod-security-standards/) +- [强制实施 Pod 安全性标准](/zh/docs/setup/best-practices/enforcing-pod-security-standards/) +- [通过配置内置的准入控制器强制实施 Pod 安全性标准](/zh/docs/tasks/configure-pod-container/enforce-standards-admission-controller/) +- [使用名字空间标签来实施 Pod 安全性标准](/zh/docs/tasks/configure-pod-container/enforce-standards-namespace-labels/) +- [从 PodSecurityPolicy 迁移到内置的 PodSecurity 准入控制器](/zh/docs/tasks/configure-pod-container/migrate-from-psp/) + diff --git a/content/zh/docs/concepts/security/pod-security-standards.md b/content/zh/docs/concepts/security/pod-security-standards.md index 7a78e66b4c..e1a2ce9bb3 100644 --- a/content/zh/docs/concepts/security/pod-security-standards.md +++ b/content/zh/docs/concepts/security/pod-security-standards.md @@ -46,8 +46,8 @@ Pod 安全性标准定义了三种不同的 _策略(Policy)_,以广泛覆 ### Privileged **_Baseline_ 策略的目标是便于常见的容器化应用采用,同时禁止已知的特权提升。** 此策略针对的是应用运维人员和非关键性应用的开发人员。 下面列举的控制应该被实施(禁止): {{< note >}} + 在下述表格中,通配符(`*`)意味着一个列表中的所有元素。 例如 `spec.containers[*].securityContext` 表示 _所定义的所有容器_ 的安全性上下文对象。 如果所列出的任一容器不能满足要求,整个 Pod 将无法通过校验。 @@ -90,8 +91,8 @@ fail validation. Baseline 策略规范 - 控制(Control) - 策略(Policy) + 控制(Control) + 策略(Policy) @@ -564,31 +565,19 @@ fail validation.

In addition to restricting HostPath volumes, the restricted policy limits usage of non-core volume types to those defined through PersistentVolumes.

Restricted Fields

    -
  • spec.volumes[*].hostPath
  • -
  • spec.volumes[*].gcePersistentDisk
  • -
  • spec.volumes[*].awsElasticBlockStore
  • -
  • spec.volumes[*].gitRepo
  • -
  • spec.volumes[*].nfs
  • -
  • spec.volumes[*].iscsi
  • -
  • spec.volumes[*].glusterfs
  • -
  • spec.volumes[*].rbd
  • -
  • spec.volumes[*].flexVolume
  • -
  • spec.volumes[*].cinder
  • -
  • spec.volumes[*].cephfs
  • -
  • spec.volumes[*].flocker
  • -
  • spec.volumes[*].fc
  • -
  • spec.volumes[*].azureFile
  • -
  • spec.volumes[*].vsphereVolume
  • -
  • spec.volumes[*].quobyte
  • -
  • spec.volumes[*].azureDisk
  • -
  • spec.volumes[*].portworxVolume
  • -
  • spec.volumes[*].scaleIO
  • -
  • spec.volumes[*].storageos
  • -
  • spec.volumes[*].photonPersistentDisk
  • +
  • spec.volumes[*]

Allowed Values

+ Every item in the spec.volumes[*] list must set one of the following fields to a non-null value:
    -
  • Undefined/nil
  • +
  • spec.volumes[*].configMap
  • +
  • spec.volumes[*].csi
  • +
  • spec.volumes[*].downwardAPI
  • +
  • spec.volumes[*].emptyDir
  • +
  • spec.volumes[*].ephemeral
  • +
  • spec.volumes[*].persistentVolumeClaim
  • +
  • spec.volumes[*].projected
  • +
  • spec.volumes[*].secret
--> 卷类型 @@ -596,31 +585,19 @@ fail validation.

除了限制 HostPath 卷之外,此类策略还限制可以通过 PersistentVolumes 定义的非核心卷类型。

限制的字段

    -
  • spec.volumes[*].hostPath
  • -
  • spec.volumes[*].gcePersistentDisk
  • -
  • spec.volumes[*].awsElasticBlockStore
  • -
  • spec.volumes[*].gitRepo
  • -
  • spec.volumes[*].nfs
  • -
  • spec.volumes[*].iscsi
  • -
  • spec.volumes[*].glusterfs
  • -
  • spec.volumes[*].rbd
  • -
  • spec.volumes[*].flexVolume
  • -
  • spec.volumes[*].cinder
  • -
  • spec.volumes[*].cephfs
  • -
  • spec.volumes[*].flocker
  • -
  • spec.volumes[*].fc
  • -
  • spec.volumes[*].azureFile
  • -
  • spec.volumes[*].vsphereVolume
  • -
  • spec.volumes[*].quobyte
  • -
  • spec.volumes[*].azureDisk
  • -
  • spec.volumes[*].portworxVolume
  • -
  • spec.volumes[*].scaleIO
  • -
  • spec.volumes[*].storageos
  • -
  • spec.volumes[*].photonPersistentDisk
  • +
  • spec.volumes[*]

允许的值

+ spec.volumes[*] 列表中的每个条目必须将下面字段之一设置为非空值:
    -
  • 未定义/nil
  • +
  • spec.volumes[*].configMap
  • +
  • spec.volumes[*].csi
  • +
  • spec.volumes[*].downwardAPI
  • +
  • spec.volumes[*].emptyDir
  • +
  • spec.volumes[*].ephemeral
  • +
  • spec.volumes[*].persistentVolumeClaim
  • +
  • spec.volumes[*].projected
  • +
  • spec.volumes[*].secret
@@ -696,40 +673,36 @@ fail validation. - - 非 root 组(可选) + + 非 root 用户(v1.23+) - -

禁止容器使用 root 作为主要或辅助 GID 来运行。

+

Containers 不可以将 runAsUser 设置为 0

限制的字段

    -
  • spec.securityContext.runAsGroup
  • -
  • spec.securityContext.supplementalGroups[*]
  • -
  • spec.securityContext.fsGroup
  • -
  • spec.containers[*].securityContext.runAsGroup
  • -
  • spec.initContainers[*].securityContext.runAsGroup
  • -
  • spec.ephemeralContainers[*].securityContext.runAsGroup
  • +
  • spec.securityContext.runAsUser
  • +
  • spec.containers[*].securityContext.runAsUser
  • +
  • spec.initContainers[*].securityContext.runAsUser
  • +
  • spec.ephemeralContainers[*].securityContext.runAsUser
-

允许的值

+

允许的字段

    -
  • 未定义/nil(*.runAsGroup 除外)
  • -
  • 非零值
  • +
  • any non-zero value
  • +
  • 未定义/空值
@@ -859,11 +832,11 @@ of individual policies are not defined here. [**Pod 安全性准入控制器**](/zh/docs/concepts/security/pod-security-admission/) -- {{< example file="security/podsecurity-privileged.yaml" >}}Privileged namespace{{< /example >}} -- {{< example file="security/podsecurity-baseline.yaml" >}}Baseline namespace{{< /example >}} -- {{< example file="security/podsecurity-restricted.yaml" >}}Restricted namespace{{< /example >}} +- {{< example file="security/podsecurity-privileged.yaml" >}}Privileged 名字空间{{< /example >}} +- {{< example file="security/podsecurity-baseline.yaml" >}}Baseline 名字空间{{< /example >}} +- {{< example file="security/podsecurity-restricted.yaml" >}}Restricted 名字空间{{< /example >}} -[**PodSecurityPolicy**](/zh/docs/concepts/policy/pod-security-policy/) +[**PodSecurityPolicy**](/zh/docs/concepts/policy/pod-security-policy/) (已弃用) - {{< example file="policy/privileged-psp.yaml" >}}Privileged{{< /example >}} - {{< example file="policy/baseline-psp.yaml" >}}Baseline{{< /example >}} diff --git a/content/zh/docs/setup/best-practices/enforcing-pod-security-standards.md b/content/zh/docs/setup/best-practices/enforcing-pod-security-standards.md new file mode 100644 index 0000000000..7326dd5aaa --- /dev/null +++ b/content/zh/docs/setup/best-practices/enforcing-pod-security-standards.md @@ -0,0 +1,154 @@ +--- +title: 强制实施 Pod 安全性标准 +weight: 40 +--- + + + + + + +本页提供实施 [Pod 安全标准(Pod Security Standards)](/zh/docs/concepts/security/pod-security-standards) +时的一些最佳实践。 + + + + +## 使用内置的 Pod 安全性准入控制器 + +{{< feature-state for_k8s_version="v1.23" state="beta" >}} + + +[Pod 安全性准入控制器](/zh/docs/reference/access-authn-authz/admission-controllers/#podsecurity) +尝试替换已被废弃的 PodSecurityPolicies。 + + +### 配置所有集群名字空间 {#configure-all-cluster-namespaces} + + +完全未经配置的名字空间应该被视为集群安全模型中的重大缺陷。 +我们建议花一些时间来分析在每个名字空间中执行的负载的类型, +并通过引用 Pod 安全性标准来确定每个负载的合适级别。 +未设置标签的名字空间应该视为尚未被评估。 + + +针对所有名字空间中的所有负载都具有相同的安全性需求的场景, +我们提供了一个[示例](/zh/docs/concepts/security/pod-security-admission/#applying-to-all-namespaces) +用来展示如何批量应用 Pod 安全性标签。 + + +### 拥抱最小特权原则 + +在一个理想环境中,每个名字空间中的每个 Pod 都会满足 `restricted` 策略的需求。 +不过,这既不可能也不现实,某些负载会因为合理的原因而需要特权上的提升。 + + +- 允许 `privileged` 负载的名字空间需要建立并实施适当的访问控制机制。 +- 对于运行在特权宽松的名字空间中的负载,需要维护其独特安全性需求的文档。 + 如果可能的话,要考虑如何进一步约束这些需求。 + + +### 采用多种模式的策略 + +Pod 安全性标准准入控制器的 `audit` 和 `warn` 模式(mode) +能够在不影响现有负载的前提下,让该控制器更方便地收集关于 Pod 的重要的安全信息。 + + +针对所有名字空间启用这些模式是一种好的实践,将它们设置为你最终打算 `enforce` 的 + _期望的_ 级别和版本。这一阶段中所生成的警告和审计注解信息可以帮助你到达这一状态。 +如果你期望负载的作者能够作出变更以便适应期望的级别,可以启用 `warn` 模式。 +如果你希望使用审计日志了监控和驱动变更,以便负载能够适应期望的级别,可以启用 `audit` 模式。 + + +当你将 `enforce` 模式设置为期望的取值时,这些模式在不同的场合下仍然是有用的: + +- 通过将 `warn` 设置为 `enforce` 相同的级别,客户可以在尝试创建无法通过合法检查的 Pod + (或者包含 Pod 模板的资源)时收到警告信息。这些信息会帮助于更新资源使其合规。 +- 在将 `enforce` 锁定到特定的非最新版本的名字空间中,将 `audit` 和 `warn` + 模式设置为 `enforce` 一样的级别而非 `latest` 版本, + 这样可以方便看到之前版本所允许但当前最佳实践中被禁止的设置。 + + +## 第三方替代方案 {#third-party-alternatives} + +{{% thirdparty-content %}} + + +Kubernetes 生态系统中也有一些其他强制实施安全设置的替代方案处于开发状态中: + +- [Kubewarden](https://github.com/kubewarden). +- [Kyverno](https://kyverno.io/policies/). +- [OPA Gatekeeper](https://github.com/open-policy-agent/gatekeeper). + + +采用 _内置的_ 方案(例如 PodSecurity 准入控制器)还是第三方工具, +这一决策完全取决于你自己的情况。在评估任何解决方案时,对供应链的信任都是至关重要的。 +最终,使用前述方案中的 _任何_ 一种都好过放任自流。 +