使用 NSA CISA Kubernetes 安全加固政策限制条件

Policy Controller 附带默认的限制条件模板库,您可以将该库与美国国家安全局 (NSA) 网络安全和基础架构安全局 (CISA) Kubernetes 安全加固指南 v1.2 政策包搭配使用,以根据 NSA CISA Kubernetes 安全加固指南 v1.2 的某些方面评估集群资源的合规性。

NSA CISA Kubernetes 安全加固 v1.2 政策包限制条件

限制条件名称 限制条件说明
nsa-cisa-k8s-v1.2-apparmor 限制 Pod 的“AppArmor”配置文件。
nsa-cisa-k8s-v1.2-automount-serviceaccount-token-pod 限制 Pod 使用“automountServiceAccountToken”。
nsa-cisa-k8s-v1.2-block-all-ingress 限制创建“Ingress”对象。
nsa-cisa-k8s-v1.2-block-secrets-of-type-basic-auth 限制使用“kubernetes.io/basic-auth”类型 Secret。
nsa-cisa-k8s-v1.2-capabilities 容器必须丢弃所有功能,并且不得重新添加任何功能。
nsa-cisa-k8s-v1.2-cpu-and-memory-limits-required 所有工作负载 pod 都必须指定“cpu”和“内存”限制。
nsa-cisa-k8s-v1.2-host-namespaces 限制“hostPID”或“hostIPC”设为“true”的容器。
nsa-cisa-k8s-v1.2-host-namespaces-hostnetwork 必须禁止共享主机命名空间。
nsa-cisa-k8s-v1.2-host-network 限制“hostNetwork”标志设为“true”的容器运行。
nsa-cisa-k8s-v1.2-hostport 限制容器在配置有“hostPort”的情况下运行。
nsa-cisa-k8s-v1.2-privilege-escalation 限制“allowPrivilegeEscalation”设为“true”的容器。
nsa-cisa-k8s-v1.2-privileged-containers 限制“securityContext.privileged”设为“true”的容器。
nsa-cisa-k8s-v1.2-readonlyrootfilesystem 要求 Pod 容器使用只读根文件系统。
nsa-cisa-k8s-v1.2-require-namespace-network-policies 要求集群中定义的每个命名空间都具有一个“NetworkPolicy”。
nsa-cisa-k8s-v1.2-restrict-clusteradmin-rolebindings 限制使用“cluster-admin”角色。
nsa-cisa-k8s-v1.2-restrict-edit-rolebindings 限制使用“edit”角色。
nsa-cisa-k8s-v1.2-restrict-hostpath-volumes 限制使用“HostPath”卷。
nsa-cisa-k8s-v1.2-restrict-pods-exec 限制在“Roles”和“ClusterRole”中使用“pods/exec”。
nsa-cisa-k8s-v1.2-running-as-non-root 限制容器以根用户身份运行。
nsa-cisa-k8s-v1.2-seccomp Seccomp 配置文件不得明确设置为“Unconfined”。
nsa-cisa-k8s-v1.2-selinux 无法设置 SELinux 类型或设置自定义 SELinux 用户或角色选项。

准备工作

  1. 安装并初始化 Google Cloud CLI,它提供了这些说明中使用的 gcloudkubectl 命令。如果您使用 Cloud Shell,则系统会预安装 Google Cloud CLI。
  2. 使用默认限制条件模板库在集群上安装政策控制器。您还必须启用对参照限制条件的支持,因为此软件包包含参照限制条件。

为参照限制条件配置政策控制器

  1. 在文件中,将以下 YAML 清单保存为 policycontroller-config.yaml。清单将政策控制器配置为监视特定种类的对象。

    apiVersion: config.gatekeeper.sh/v1alpha1
    kind: Config
    metadata:
      name: config
      namespace: "gatekeeper-system"
    spec:
      sync:
        syncOnly:
          - group: "networking.k8s.io"
            version: "v1"
            kind: "NetworkPolicy"
    
  2. 应用 policycontroller-config.yaml 清单:

    kubectl apply -f policycontroller-config.yaml
    

审核 NSA CISA Kubernetes 安全加固 v1.2 政策包

政策控制器允许您为 Kubernetes 集群强制执行政策。为了帮助测试您的工作负载及其对上表中列出的 NSA CISA Kubernetes 安全加固指南 v1.2 政策的合规性,您可以在“审核”模式下部署这些限制条件以发现违规行为,更重要的是让您有机会在 Kubernetes 集群上酌情强制执行之前处理这些违规行为。

您可以使用 kubectl、kptConfig Sync,在 spec.enforcementAction 设置为 dryrun 的情况下应用这些政策。

kubectl

  1. (可选)使用 kubectl 预览政策限制条件:

    kubectl kustomize https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/nsa-cisa-k8s-v1.2
    
  2. 使用 kubectl 应用政策限制条件:

    kubectl apply -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/nsa-cisa-k8s-v1.2
    

    输出如下所示:

    k8sblockallingress.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-block-all-ingress created
    k8sblockobjectsoftype.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-block-secrets-of-type-basic-auth created
    k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-privilege-escalation created
    k8spspallowedusers.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-running-as-non-root created
    k8spspapparmor.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-apparmor created
    k8spspautomountserviceaccounttokenpod.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-automount-serviceaccount-token-pod created
    k8spspcapabilities.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-capabilities created
    k8spsphostfilesystem.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-restrict-hostpath-volumes created
    k8spsphostnamespace.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-host-namespaces created
    k8spsphostnetworkingports.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-host-network created
    k8spsphostnetworkingports.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-hostport created
    k8spspprivilegedcontainer.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-privileged-containers created
    k8spspreadonlyrootfilesystem.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-readonlyrootfilesystem created
    k8spspselinuxv2.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-selinux created
    k8spspseccomp.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-seccomp created
    k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-require-namespace-network-policies created
    k8srequiredresources.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-cpu-and-memory-limits-required created
    k8srestrictrolebindings.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-restrict-clusteradmin-rolebindings created
    k8srestrictrolebindings.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-restrict-edit-rolebindings created
    k8srestrictrolerules.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-restrict-pods-exec created
    
  3. 验证政策限制条件是否已安装,并检查集群中是否存在违规行为:

    kubectl get constraints -l policycontroller.gke.io/bundleName=nsa-cisa-k8s-v1.2
    

    输出类似于以下内容:

    NAME                                                                               ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8sblockallingress.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-block-all-ingress   dryrun               0
    
    NAME                                                                                                 ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8sblockobjectsoftype.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-block-secrets-of-type-basic-auth   dryrun               0
    
    NAME                                                                                 ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspallowedusers.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-running-as-non-root   dryrun               0
    
    NAME                                                                                                       ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-privilege-escalation   dryrun               0
    
    NAME                                                                  ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspapparmor.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-apparmor   dryrun               0
    
    NAME                                                                                                                   ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspautomountserviceaccounttokenpod.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-automount-serviceaccount-token-pod   dryrun               0
    
    NAME                                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspcapabilities.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-capabilities   dryrun               0
    
    NAME                                                                                         ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spsphostfilesystem.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-restrict-hostpath-volumes   dryrun               0
    
    NAME                                                                              ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spsphostnamespace.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-host-namespaces   dryrun               0
    
    NAME                                                                                 ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spsphostnetworkingports.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-host-network   dryrun               0
    k8spsphostnetworkingports.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-hostport       dryrun               0
    
    NAME                                                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspprivilegedcontainer.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-privileged-containers   dryrun               0
    
    NAME                                                                                              ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspreadonlyrootfilesystem.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-readonlyrootfilesystem   dryrun               0
    
    NAME                                                                ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspseccomp.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-seccomp   dryrun               0
    
    NAME                                                                  ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspselinuxv2.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-selinux   dryrun               0
    
    NAME                                                                                              ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srequiredresources.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-cpu-and-memory-limits-required   dryrun               0
    
    NAME                                                                                                                ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-require-namespace-network-policies   dryrun               0
    
    NAME                                                                                                     ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictrolebindings.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-restrict-clusteradmin-rolebindings   dryrun               0
    k8srestrictrolebindings.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-restrict-edit-rolebindings           dryrun               0
    
    NAME                                                                                  ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictrolerules.constraints.gatekeeper.sh/nsa-cisa-k8s-v1.2-restrict-pods-exec   dryrun               0
    

kpt

  1. 安装并设置 kpt

    在这些说明中,kpt 用于自定义和部署 Kubernetes 资源。

  2. 使用 kpt 从 GitHub 下载 NSA CISA Kubernetes 安全加固指南 v1.2 政策包:

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/nsa-cisa-k8s-v1.2
    
  3. 运行 set-enforcement-action kpt 函数,将政策的违规处置措施设置为 dryrun

    kpt fn eval nsa-cisa-k8s-v1.2 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 \
    -- enforcementAction=dryrun
    
  4. 使用 kpt 初始化工作目录,此操作会创建一个资源来跟踪更改:

    cd nsa-cisa-k8s-v1.2 kpt live init
    
  5. 使用 kpt 应用政策限制条件:

    kpt live apply
    
  6. 验证政策限制条件是否已安装,并检查集群中是否存在违规行为:

    kpt live status --output table --poll-until current
    

    状态为 CURRENT,即表示限制条件成功安装。

Config Sync

  1. 安装并设置 kpt

    在这些说明中,kpt 用于自定义和部署 Kubernetes 资源。

    使用 Config Sync 将政策部署到其集群的运维人员可以按照以下说明操作:

  2. 更改为 Config Sync 的同步目录:

    cd SYNC_ROOT_DIR
    

    创建 resourcegroup.yaml 或将其附加到 .gitignore

    echo resourcegroup.yaml >> .gitignore
    
  3. 创建一个专用的 policies 目录:

    mkdir -p policies
    
  4. 使用 kpt 从 GitHub 下载 NSA CISA Kubernetes 安全加固指南 v1.2 政策包:

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/nsa-cisa-k8s-v1.2 policies/nsa-cisa-k8s-v1.2
    
  5. 运行 set-enforcement-action kpt 函数,将政策的违规处置措施设置为 dryrun

    kpt fn eval policies/nsa-cisa-k8s-v1.2 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=dryrun
    
  6. (可选)预览待创建的政策限制条件:

    kpt live init policies/nsa-cisa-k8s-v1.2 kpt live apply --dry-run policies/nsa-cisa-k8s-v1.2
    
  7. 如果 Config Sync 的同步目录使用 Kustomize,请将 policies/nsa-cisa-k8s-v1.2 添加到根 kustomization.yaml。否则,请移除 policies/nsa-cisa-k8s-v1.2/kustomization.yaml 文件:

    rm SYNC_ROOT_DIR/policies/nsa-cisa-k8s-v1.2/kustomization.yaml
    
  8. 将更改推送到 Config Sync 代码库:

    git add SYNC_ROOT_DIR/policies/nsa-cisa-k8s-v1.2 git commit -m 'Adding NSA CISA Kubernetes Hardening v1.2 policy audit enforcement'
    git push
    
  9. 验证安装状态:

    watch gcloud beta container fleet config-management status --project PROJECT_ID
    

    状态 SYNCED 用于确认政策的安装情况。

查看违规问题

在审核模式下安装政策限制条件后,您可以使用 Policy Controller 信息中心在界面中查看集群的违规问题。

您还可以使用 kubectl 来通过以下命令查看针对集群的违规行为:

  kubectl get constraint -l policycontroller.gke.io/bundleName=nsa-cisa-k8s-v1.2 -o json | jq -cC '.items[]| [.metadata.name,.status.totalViolations]'
  

如果存在违规行为,您可以使用以下命令查看每个限制条件的违规消息列表:

  kubectl get constraint -l policycontroller.gke.io/bundleName=nsa-cisa-k8s-v1.2 -o json | jq -C '.items[]| select(.status.totalViolations>0)| [.metadata.name,.status.violations[]?]'
  

更改 NSA CISA Kubernetes 安全加固 v1.2 政策包强制执行操作

查看集群上的政策违规情况后,您可以考虑更改强制执行模式,以便准入控制器发出 warn,甚至 deny 屏蔽不合规资源应用于集群。

kubectl

  1. 使用 kubectl 将政策的强制执行操作设置为 warn

    kubectl get constraints -l policycontroller.gke.io/bundleName=nsa-cisa-k8s-v1.2 -o name | xargs -I {} kubectl patch {} --type='json' -p='[{"op":"replace","path":"/spec/enforcementAction","value":"warn"}]'
    
  2. 验证政策限制条件强制执行措施是否已更新:

    kubectl get constraints -l policycontroller.gke.io/bundleName=nsa-cisa-k8s-v1.2
    

kpt

  1. 运行 set-enforcement-action kpt 函数,将政策的违规处置措施设置为 warn

    kpt fn eval -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  2. 应用政策限制条件:

    kpt live apply
    

Config Sync

使用 Config Sync 将政策部署到其集群的运维人员可以按照以下说明操作:

  1. 更改为 Config Sync 的同步目录:

    cd SYNC_ROOT_DIR
    
  2. 运行 set-enforcement-action kpt 函数,将政策的违规处置措施设置为 warn

    kpt fn eval policies/nsa-cisa-k8s-v1.2 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  3. 将更改推送到 Config Sync 代码库:

    git add SYNC_ROOT_DIR/policies/nsa-cisa-k8s-v1.2
    git commit -m 'Adding NSA CISA Kubernetes Hardening v1.2 policy warn enforcement'
    git push
    
  4. 验证安装状态:

    gcloud alpha anthos config sync repo list --project PROJECT_ID
    

    SYNCED 列中显示您的代码库即表示政策安装成功。

测试政策执行情况

使用以下命令在集群上创建不合规的资源:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  namespace: default
  name: wp-non-compliant
  labels:
    app: wordpress
spec:
  containers:
    - image: wordpress
      name: wordpress
      ports:
      - containerPort: 80
        name: wordpress
EOF

准入控制器应生成一个警告列表,其中列出了此资源违反政策的行为,如以下示例所示:

Warning: [nsa-cisa-k8s-v1.2-automount-serviceaccount-token-pod] Automounting service account token is disallowed, pod: wp-non-compliant
Warning: [nsa-cisa-k8s-v1.2-running-as-non-root] Container wordpress is attempting to run without a required securityContext/runAsGroup. Allowed runAsGroup: {"ranges": [{"max": 65536, "min": 1000}], "rule": "MustRunAs"}
Warning: [nsa-cisa-k8s-v1.2-running-as-non-root] Container wordpress is attempting to run without a required securityContext/runAsUser
Warning: [nsa-cisa-k8s-v1.2-privilege-escalation] Privilege escalation container is not allowed: wordpress
Warning: [nsa-cisa-k8s-v1.2-cpu-and-memory-limits-required] container <wordpress> does not have <{"cpu", "memory"}> limits defined
Warning: [nsa-cisa-k8s-v1.2-capabilities] container <wordpress> is not dropping all required capabilities. Container must drop all of ["ALL"] or "ALL"
Warning: [nsa-cisa-k8s-v1.2-readonlyrootfilesystem] only read-only root filesystem container is allowed: wordpress
pod/wp-non-compliant created

移除 NSA CISA Kubernetes 安全加固 v1.2 政策包

如果需要,您可以从集群中移除 NSA CISA Kubernetes 安全加固 v1.2 政策包。

kubectl

  • 使用 kubectl 移除政策:

    kubectl delete constraint -l policycontroller.gke.io/bundleName=nsa-cisa-k8s-v1.2
    

kpt

  • 移除政策:

    kpt live destroy
    

Config Sync

使用 Config Sync 将政策部署到其集群的运维人员可以按照以下说明操作:

  1. 将更改推送到 Config Sync 代码库:

    git rm -r SYNC_ROOT_DIR/policies/nsa-cisa-k8s-v1.2
    git commit -m 'Removing NSA CISA Kubernetes Hardening Guide v1.2 policies'
    git push
    
  2. 验证状态:

    gcloud alpha anthos config sync repo list --project PROJECT_ID
    

    SYNCED 列中显示您的代码库即表示政策移除成功。