使用 CIS Google Kubernetes Engine 基準 1.5.0 版政策限制

Policy Controller 隨附預設的限制範本庫,可與網際網路安全中心 (CIS) GKE v1.5.0 政策套裝組合搭配使用,稽核 Google Cloud 上的 GKE 叢集是否符合 CIS Google Kubernetes Engine (GKE) 基準 v1.5.0。這項基準由多項建議的安全性控管措施組成,可協助您設定 GKE。

本頁說明如何手動套用政策組合。 或者,您也可以直接套用政策組合

如果您是 IT 管理員和營運人員,想確保雲端平台中執行的所有資源都符合機構的法規遵循規定,請參閱本頁面,瞭解如何提供及維護自動化稽核或強制執行功能。如要進一步瞭解我們在內容中提及的常見角色和範例工作,請參閱「常見的 GKE Enterprise 使用者角色和工作」。 Google Cloud

這組限制包含下列網域的控制項:

  • 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 限制使用 basic-auth 類型的 Secret。 4.4.2
cis-gke-v1.5.0-require-binauthz 需要二進位授權驗證許可控制器 Webhook。 4.5.1、5.10.4
cis-gke-v1.5.0-require-cos-node-image 需要 Container-Optimized OS 做為節點 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 容器無法以 runtime/default 以外的 seccomp 設定檔執行。 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 限制在容器定義中使用密鑰做為環境變數。 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 中使用密鑰。 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 容器和 Pod 無法在 securityContext.windowsOptions.hostProcess 設為 true 的情況下執行。 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 中節點的 Proxy 子資源存取權。 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 中建立 serviceaccount 權杖。 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 中的 Webhook 設定物件。 4.1.12
cis-gke-v1.5.0-seccomp Seccomp 設定檔不得設為「Unconfined」。 4.2.1
cis-gke-v1.5.0-sysctls 容器只能設定 allowedSysctls 欄位中列出的 sysctls。 4.2.1
cis-gke-v1.5.0-host-namespaces-hostnetwork 主機命名空間 hostNetwork 只能是 false 4.2.1

事前準備

  1. 安裝並初始化 Google Cloud CLI,其中提供這些操作說明中使用的 gcloudkubectl 指令。如果您使用 Cloud Shell,Google Cloud CLI 會預先安裝。
  2. 在叢集上安裝 Policy Controller 1.16.2 以上版本,並使用預設的限制範本程式庫。此外,由於這個套裝組合包含參照限制,您也必須啟用參照限制支援

設定參照完整性限制

  1. 將下列 YAML 資訊清單儲存為 policycontroller-config.yaml 檔案。 資訊清單會設定 Policy Controller,監控特定種類的物件。

    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 中啟用及設定 RBAC 適用的 Google 群組
  2. 您必須在 cis-gke-v1.5.0-require-binauthz 中啟用及設定二進位授權

稽核政策

您可以透過 Policy Controller,對 Kubernetes 叢集強制執行政策。如要測試工作負載及其是否符合上表列出的 CIS GKE v1.5.0 政策,您可以「稽核」模式部署這些限制,找出並解決違規問題。

您可以使用 kubectl、kptConfig Sync,將 spec.enforcementAction 設為 dryrun 來套用這些政策。

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 資訊主頁,在 UI 中查看叢集的違規情形。

您也可以使用 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 政策套件強制執行動作

審查叢集中的政策違規事項後,您可以考慮變更強制執行模式,讓 Admission Controller warndeny 阻止不符規定的資源套用至叢集。

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":"/https/cloud.google.com/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. 驗證安裝狀態:

    nomos status
    

    叢集應顯示 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. 確認狀態:

    nomos status
    

    叢集應顯示 SYNCED 狀態,且資源已移除。