使用 CIS Google Kubernetes Engine 基准 v1.5.0 政策限制条件

Policy Controller 附带默认的限制条件模板库,您可以将该库与 Center for Internet Security (CIS) GKE v1.5.0 政策包搭配使用,根据 CIS Google Kubernetes Engine (GKE) 基准 v1.5.0 审核 GKE on Google Cloud 集群的合规性。此基准是一系列用于配置 GKE 的建议安全控制措施。

此限制条件软件包包含以下网域中的控件:

  • RBAC 和服务账号
  • Pod 安全政策
  • 网络政策和 CNI
  • 密文管理
  • 可扩展的准许控制
  • 常规政策
  • 代管式服务

CIS GKE v1.5.0 政策包限制条件

限制条件名称 限制条件说明 控件 ID
cis-gke-v1.5.0-apparmor 限制 Pod 的 AppArmor 配置文件。 4.2.1
cis-gke-v1.5.0-restrict-creation-with-default-serviceaccount 使用默认 ServiceAccount 限制资源的创建。 4.1.5
cis-gke-v1.5.0-restrict-loadbalancer 禁止类型为 LoadBalancer 的所有服务。 5.6.8
cis-gke-v1.5.0-restrict-secrets-of-type-basic-auth 限制使用基本-身份验证类型 Secret。 4.4.2
cis-gke-v1.5.0-require-binauthz 要求 Binary Authorization 验证准入 webhook。 4.5.1、5.10.4
cis-gke-v1.5.0-require-cos-node-image 需要 Container-Optimized OS 作为节点操作系统映像。 5.5.1
cis-gke-v1.5.0-require-gke-metadata-server-enabled 要求集群中的每个节点上启用 GKE 元数据服务。 5.4.2
cis-gke-v1.5.0-require-ingress.class-gce 要求所有 Ingress 对象都具有 kubernetes.io/ingress.class: gce 注解。 5.6.8
cis-gke-v1.5.0-require-managed-certificates 要求所有 Ingress 对象都具有 networking.gke.io/managed-certificates 注解。 5.6.8
cis-gke-v1.5.0-require-namespace-networkpolicy 要求集群中定义的每个 Namespace 都具有一个 NetworkPolicy 4.3.2
cis-gke-v1.5.0-require-seccomp-default seccomp 配置文件不是 runtime/default 的情况下,容器无法运行。 4.6.2
cis-gke-v1.5.0-require-securitycontext 要求 pod 定义 securityContext 4.6.3
cis-gke-v1.5.0-capabilities 除了所列功能之外,不允许添加其他功能。 4.2.1
cis-gke-v1.5.0-restrict-cluster-admin-role 限制 cluster-admin 角色的使用。 4.1.1
cis-gke-v1.5.0-restrict-default-namespace 限制 pod 使用默认 Namespace 4.6.1, 4.6.4
cis-gke-v1.5.0-restrict-env-var-secrets 限制将 Secret 用作容器定义中的环境变量。 4.4.1
cis-gke-v1.5.0-host-namespace 禁止容器的 hostPIDhostIPC 设置为 true。 4.2.1
cis-gke-v1.5.0-restrict-pods-create 限制在 RolesClusterRoles 中创建 pod。 4.1.4
cis-gke-v1.5.0-restrict-privileged-containers 限制 securityContext.privileged 设置为 true 的容器。 4.2.1
cis-gke-v1.5.0-restrict-rbac-subjects 将 RBAC 主体中的名称限制为只能使用指定的值。 5.8.2
cis-gke-v1.5.0-restrict-role-wildcards 禁止在 RolesClusterRoles 中使用通配符。 4.1.3
cis-gke-v1.5.0-restrict-role-secrets 限制在 RolesClusterRoles 中使用 Secret。 4.1.2
cis-gke-v1.5.0-restrict-automountserviceaccounttoken 限制 pod 的服务账号令牌的使用。 4.1.5, 4.1.6
cis-gke-v1.5.0-selinux 无法设置 SELinux 类型或设置自定义 SELinux 用户或角色选项。 4.2.1
cis-gke-v1.5.0-host-port 应该不允许使用 HostPorts 4.2.1
cis-gke-v1.5.0-hostpath-volumes 必须禁止使用 HostPath 卷。 4.2.1
cis-gke-v1.5.0-hostprocess securityContext.windowsOptions.hostProcess 设置为 true 的情况下,容器和 Pod 无法运行。 4.2.1
cis-gke-v1.5.0-proc-mount-type procMount 设置为 Default 以外的值时,容器无法运行。 4.2.1
cis-gke-v1.5.0-restrict-bind-escalate-impersonate 限制对 RolesClusterRoles 中的绑定、上报、模拟角色/集群角色的访问权限。 4.1.8
cis-gke-v1.5.0-restrict-certificatesigningrequests-approval 客户端证书的审批会受到限制。 4.1.11
cis-gke-v1.5.0-restrict-nodes-proxy 限制对 RolesClusterRoles 中节点的代理子资源的访问权限。 4.1.10
cis-gke-v1.5.0-restrict-persistent-volume 限制在 RolesClusterRoles 中创建 persistentvolumes 4.1.9
cis-gke-v1.5.0-restrict-serviceaccounts-token 限制在 RolesClusterRoles 中创建服务帐号令牌。 4.1.13
cis-gke-v1.5.0-restrict-system-masters-group 禁止使用 system:masters 组。 5.1.7
cis-gke-v1.5.0-restrict-system-masters-group 限制对 RolesClusterRoles 中的网络钩子配置对象的访问权限。 4.1.12
cis-gke-v1.5.0-seccomp Seccomp 配置文件不得设置为 Unconfined 4.2.1
cis-gke-v1.5.0-sysctls 容器只能设置 allowedSysctls 字段中列出的 sysctl。 4.2.1

准备工作

  1. 安装并初始化 Google Cloud CLI,它提供了以下说明中使用的 gcloudkubectl 命令。如果您使用 Cloud Shell,则系统会预安装 Google Cloud CLI。
  2. 使用默认限制条件模板库在集群上安装 Policy Controller v1.16.2 或更高版本。您还必须启用对参照限制条件的支持,因为此包包含参照限制条件。

配置参照限制条件

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

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

    kubectl apply -f policycontroller-config.yaml
    

配置集群和工作负载

  1. cis-gke-v1.5.0-restrict-rbac-subjects 中需要启用和配置 Google RBAC 群组
  2. 需要在 cis-gke-v1.5.0-require-binauthz 中启用和配置 Binary Authorization

审核政策

政策控制器允许您为 Kubernetes 集群强制执行政策。为了帮助测试您的工作负载及其对上表中列出的 CIS GKE v1.5.0 政策的合规性,您可以在“审核”模式下部署这些限制条件,以发现和解决违规问题。

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

kubectl

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

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

    kubectl apply -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/cis-gke-v1.5.0
    

    输出如下所示:

    k8savoiduseofsystemmastersgroup.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-system-masters-group created
    k8sblockcreationwithdefaultserviceaccount.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-creation-with-default-serviceaccount created
    k8sblockloadbalancer.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-loadbalancer created
    k8sblockobjectsoftype.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-secrets-of-type-basic-auth created
    k8snoenvvarsecrets.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-env-var-secrets created
    k8spspapparmor.constraints.gatekeeper.sh/cis-gke-v1.5.0-apparmor created
    k8spspcapabilities.constraints.gatekeeper.sh/cis-gke-v1.5.0-capabilities created
    k8spspforbiddensysctls.constraints.gatekeeper.sh/cis-gke-v1.5.0-sysctls created
    k8spsphostfilesystem.constraints.gatekeeper.sh/cis-gke-v1.5.0-hostpath-volumes created
    k8spsphostnamespace.constraints.gatekeeper.sh/cis-gke-v1.5.0-host-namespace created
    k8spsphostnetworkingports.constraints.gatekeeper.sh/cis-gke-v1.5.0-host-port created
    k8spspprivilegedcontainer.constraints.gatekeeper.sh/cis-gke-v1.5.0-privileged-containers created
    k8spspprocmount.constraints.gatekeeper.sh/cis-gke-v1.5.0-proc-mount-type created
    k8spspselinuxv2.constraints.gatekeeper.sh/cis-gke-v1.5.0-selinux created
    k8spspseccomp.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-seccomp-default created
    k8spspseccomp.constraints.gatekeeper.sh/cis-gke-v1.5.0-seccomp created
    k8spspwindowshostprocess.constraints.gatekeeper.sh/cis-gke-v1.5.0-hostprocess created
    k8spodsrequiresecuritycontext.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-securitycontext created
    k8sprohibitrolewildcardaccess.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-role-wildcards created
    k8srequirebinauthz.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-binauthz created
    k8srequirecosnodeimage.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-cos-node-image created
    k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-namespace-networkpolicy created
    k8srequiredannotations.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-ingress.class-gce created
    k8srequiredannotations.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-managed-certificates created
    k8srequiredlabels.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-gke-metadata-server-enabled created
    k8srestrictautomountserviceaccounttokens.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-automountserviceaccounttoken created
    k8srestrictnamespaces.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-default-namespace created
    k8srestrictrbacsubjects.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-rbac-subjects created
    k8srestrictrolebindings.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-cluster-admin-role created
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-bind-escalate-impersonate created
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-certificatesigningrequests-approval created
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-nodes-proxy created
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-persistent-volume created
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-pods-create created
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-role-secrets created
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-serviceaccounts-token created
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-webhook-config created
    
  3. 验证政策限制条件是否已安装,并检查集群中是否存在违规行为:

    kubectl get constraints -l policycontroller.gke.io/bundleName=cis-gke-v1.5.0
    

    输出类似于以下内容:

    NAME                                                                                                     ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8savoiduseofsystemmastersgroup.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-system-masters-group   dryrun               0
    
    NAME                                                                                                                               ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8sblockcreationwithdefaultserviceaccount.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-creation-with-default-serviceaccount   dryrun               0
    
    NAME                                                                                  ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8sblockloadbalancer.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-loadbalancer   dryrun               0
    
    NAME                                                                                                 ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8sblockobjectsoftype.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-secrets-of-type-basic-auth   dryrun               0
    
    NAME                                                                                   ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8snoenvvarsecrets.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-env-var-secrets   dryrun               0
    
    NAME                                                                                             ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spodsrequiresecuritycontext.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-securitycontext   dryrun               0
    
    NAME                                                                                             ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8sprohibitrolewildcardaccess.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-role-wildcards   dryrun               0
    
    NAME                                                               ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspapparmor.constraints.gatekeeper.sh/cis-gke-v1.5.0-apparmor   dryrun               0
    
    NAME                                                                       ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspcapabilities.constraints.gatekeeper.sh/cis-gke-v1.5.0-capabilities   dryrun               0
    
    NAME                                                                      ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspforbiddensysctls.constraints.gatekeeper.sh/cis-gke-v1.5.0-sysctls   dryrun               0
    
    NAME                                                                             ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spsphostfilesystem.constraints.gatekeeper.sh/cis-gke-v1.5.0-hostpath-volumes   dryrun               0
    
    NAME                                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spsphostnamespace.constraints.gatekeeper.sh/cis-gke-v1.5.0-host-namespace   dryrun               0
    
    NAME                                                                           ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spsphostnetworkingports.constraints.gatekeeper.sh/cis-gke-v1.5.0-host-port   dryrun               0
    
    NAME                                                                                       ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspprivilegedcontainer.constraints.gatekeeper.sh/cis-gke-v1.5.0-privileged-containers   dryrun               0
    
    NAME                                                                       ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspprocmount.constraints.gatekeeper.sh/cis-gke-v1.5.0-proc-mount-type   dryrun               0
    
    NAME                                                                             ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspseccomp.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-seccomp-default   dryrun               0
    k8spspseccomp.constraints.gatekeeper.sh/cis-gke-v1.5.0-seccomp                   dryrun               0
    
    NAME                                                               ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspselinuxv2.constraints.gatekeeper.sh/cis-gke-v1.5.0-selinux   dryrun               0
    
    NAME                                                                            ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspwindowshostprocess.constraints.gatekeeper.sh/cis-gke-v1.5.0-hostprocess   dryrun               0
    
    NAME                                                                           ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srequirebinauthz.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-binauthz   dryrun               0
    
    NAME                                                                                     ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srequirecosnodeimage.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-cos-node-image   dryrun               0
    
    NAME                                                                                           ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srequiredannotations.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-ingress.class-gce      dryrun               0
    k8srequiredannotations.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-managed-certificates   dryrun               0
    
    NAME                                                                                             ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srequiredlabels.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-gke-metadata-server-enabled   dryrun               0
    
    NAME                                                                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/cis-gke-v1.5.0-require-namespace-networkpolicy   dryrun               0
    
    NAME                                                                                                                      ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictautomountserviceaccounttokens.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-automountserviceaccounttoken   dryrun               0
    
    NAME                                                                                        ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictnamespaces.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-default-namespace   dryrun               0
    
    NAME                                                                                      ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictrbacsubjects.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-rbac-subjects   dryrun               0
    
    NAME                                                                                           ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictrolebindings.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-cluster-admin-role   dryrun               0
    
    NAME                                                                                                         ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-bind-escalate-impersonate             dryrun               0
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-certificatesigningrequests-approval   dryrun               0
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-nodes-proxy                           dryrun               0
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-persistent-volume                     dryrun               0
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-pods-create                           dryrun               0
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-role-secrets                          dryrun               0
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-serviceaccounts-token                 dryrun               0
    k8srestrictrolerules.constraints.gatekeeper.sh/cis-gke-v1.5.0-restrict-webhook-config                        dryrun               0
    

kpt

  1. 安装并设置 kpt。在这些说明中,kpt 用于自定义和部署 Kubernetes 资源。

  2. 使用 kpt 从 GitHub 下载 CIS 政策包:

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/cis-gke-v1.5.0
    
  3. 运行 set-enforcement-action kpt 函数将政策的强制执行操作设置为 dryrun

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

    cd cis-gke-v1.5.0
    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 将政策部署到其集群的运维人员可以按照以下说明操作:

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

    cd SYNC_ROOT_DIR
    

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

    echo resourcegroup.yaml >> .gitignore
    

  2. 创建一个专用的 policies 目录:

    mkdir -p policies
    
  3. 使用 kpt 从 GitHub 下载 CIS GKE v1.5.0 政策包:

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/cis-gke-v1.5.0 policies/cis-gke-v1.5.0
    
  4. 运行 set-enforcement-action kpt 函数将政策的强制执行操作设置为 dryrun

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

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

    rm SYNC_ROOT_DIR/policies/cis-gke-v1.5.0/kustomization.yaml
    
  7. 将更改推送到 Config Sync 代码库:

    git add SYNC_ROOT_DIR/policies/cis-gke-v1.5.0
    git commit -m 'Adding CIS GKE v1.5.0 policy bundle'
    git push
    
  8. 验证安装状态:

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

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

查看违规问题

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

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

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

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

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

更改 CIS GKE v1.5.0 政策包强制执行操作

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

kubectl

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

    kubectl get constraint -l policycontroller.gke.io/bundleName=cis-gke-v1.5.0 -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=cis-gke-v1.5.0
    

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/cis-gke-v1.5.0 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  3. 将更改推送到 Config Sync 代码库:

    git add SYNC_ROOT_DIR/policies/cis-gke-v1.5.0
    git commit -m 'Adding CIS GKE v1.5.0 policy bundle 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: [cis-gke-v1.5.0-restrict-default-namespace] <default> namespace is restricted
Warning: [cis-gke-v1.5.0-require-seccomp-default] Seccomp profile 'not configured' is not allowed for container 'wordpress'. Found at: no explicit profile found. Allowed profiles: {"RuntimeDefault", "runtime/default"}
Warning: [cis-gke-v1.5.0-require-securitycontext] securityContext must be defined for all Pod containers
pod/wp-non-compliant configured

移除 CIS GKE v1.5.0 政策包

如果需要,可以从集群中移除 CIS GKE v1.5.0 政策包。

kubectl

使用 kubectl 移除政策:

  kubectl delete constraint -l policycontroller.gke.io/bundleName=cis-gke-v1.5.0
  

kpt

移除政策:

  kpt live destroy
  

Config Sync

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

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

    git rm -r SYNC_ROOT_DIR/policies/cis-gke-v1.5.0
    git commit -m 'Removing CIS GKE v1.5.0 policy bundle'
    git push
    
  2. 验证状态:

    gcloud alpha anthos config sync repo list --project PROJECT_ID
    

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