CIS Google Kubernetes Engine Benchmark v1.5.0 ポリシーの制約を使用する

Policy Controller には、Center for Internet Security(CIS)GKE v1.5.0 ポリシー バンドルで使用できる制約テンプレートのデフォルト ライブラリが付属しています。これにより、CIS Google Kubernetes Engine(GKE)Benchmarks v1.5.0 に対する Google Cloud クラスタの GKE のコンプライアンスを監査できます。このベンチマークは、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 basic-auth タイプのシークレットの使用を制限します。 4.4.2
cis-gke-v1.5.0-require-binauthz Binary Authorization Validating Admission Webhook が必要です。 4.5.1, 5.10.4
cis-gke-v1.5.0-require-cos-node-image ノード OS イメージとして 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 クラスタで定義されているすべての NamespaceNetworkPolicy が必要です。 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 hostPID または hostIPC が 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 securityContext.windowsOptions.hostProcesstrue に設定してコンテナと Pod を実行することはできません。 4.2.1
cis-gke-v1.5.0-proc-mount-type procMountDefault 以外の値に設定してコンテナを実行することはできません。 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 内の 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

始める前に

  1. この手順で使用する gcloud コマンドと kubectl コマンドを含む Google Cloud CLI をインストールして初期化します。Cloud Shell を使用する場合、Google Cloud CLI がプリインストールされています。
  2. 制約テンプレートのデフォルト ライブラリを使用して、クラスタに Policy Controller v1.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-subjectsRBAC 向け Google グループを有効にして構成する必要があります。
  2. cis-gke-v1.5.0-require-binauthz では、Binary Authorization の有効化と構成が必要です。

監査ポリシー

Policy Controller を使用すると、Kubernetes クラスタにポリシーを適用できます。前の表にある CIS GKE v1.5.0 ポリシーに関するワークロードとそのコンプライアンスをテストする場合、これらの制約を監査モードでデプロイして、違反を確認して対処できます。

kubectl、kpt、または Config Sync を使用して spec.enforcementActiondryrun に設定し、これらのポリシーを適用できます。

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 を使用している場合は、ルート kustomization.yamlpolicies/cis-gke-v1.5.0 を追加します。それ以外の場合は、policies/cis-gke-v1.5.0/kustomization.yaml ファイルを削除します。

    rm SYNC_ROOT_DIR/policies/cis-gke-v1.5.0/kustomization.yaml
    
  7. 変更を Config Sync リポジトリに push します。

    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 の場合、ポリシーがインストールされています。

ポリシー違反を確認する

ポリシーの制約が監査モードでインストールされると、UI の 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 リポジトリに push します。

    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 リポジトリに push します。

    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 列に表示されたリポジトリで、ポリシーが削除されていることを確認します。