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」タイプのシークレットの使用を制限します。
nsa-cisa-k8s-v1.2-capabilities コンテナはすべての機能を削除する必要があり、機能を追加し直すことはできません。
nsa-cisa-k8s-v1.2-cpu-and-memory-limits-required すべてのワークロード Pod に「cpu」と「memory」の上限を指定する必要があります。
nsa-cisa-k8s-v1.2-host-namespaces 「hostPID」または「hostIPC」が「true」に設定されているコンテナを制限します。
nsa-cisa-k8s-v1.2-host-namespaces-hostnetwork ホスト Namespace の共有は禁止する必要があります。
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.privilege」が「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 コンテナが root ユーザーとして実行されないように制限します。
nsa-cisa-k8s-v1.2-seccomp Seccomp プロファイルは、明示的に「Unconfined」に設定する必要はありません。
nsa-cisa-k8s-v1.2-selinux SELinux タイプの設定や、カスタム SELinux のユーザーまたはロール オプションの設定はできません。

準備

  1. この手順で使用する gcloud コマンドと kubectl コマンドを含む Google Cloud CLI をインストールして初期化します。Cloud Shell を使用する場合、Google Cloud CLI がプリインストールされています。
  2. 制約テンプレートのデフォルト ライブラリを使用して、クラスタに Policy Controller をインストールします。このバンドルには参照制約が含まれているため、参照制約のサポートを有効にする必要があります。

参照制約用に Policy Controller を構成する

  1. 次の YAML マニフェストを policycontroller-config.yaml という名前のファイルに保存します。このマニフェストでは、特定の種類のオブジェクトを監視するように Policy Controller を構成します。

    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 のポリシー バンドルを監査する

Policy Controller を使用すると、Kubernetes クラスタにポリシーを適用できます。上の表にある NSA CISA Kubernetes 強化ガイド v1.2 ポリシーに関するワークロードとそのコンプライアンスをテストする場合、これらの制約を監査モードでデプロイして、違反を確認できます。さらに重要な点として、必要に応じて Kubernetes クラスタに適用する前に、問題を修正することが可能になります。

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

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

    rm SYNC_ROOT_DIR/policies/nsa-cisa-k8s-v1.2/kustomization.yaml
    
  8. 変更を Config Sync リポジトリに push します。

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

ポリシー違反を確認する

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

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

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