Pod Security Standards Restricted ポリシー制約を使用する

Policy Controller には、Pod Security Standards Restricted バンドルで使用できる制約テンプレートのデフォルト ライブラリが付属しています。このバンドルを使用すると、Kubernetes Pod Security Standards(PSS)Restricted ポリシーと同じ多くの保護機能を実施できます。また、ポリシーを適用する前に特定のポリシーをテストして、特定のリソースの対象範囲を除外することもできます。

バンドルには次の制約が含まれています。これらの制約は、次の Kubernetes Pod Security Standards(PSS)Restricted ポリシーの制御にマッピングされています。

制約名 制約の説明 設定名
pss-restricted-v2022-psp-volume-types ボリューム タイプの使用 ボリューム タイプ
pss-restricted-v2022-privilege-escalation root 権限への昇格の制限 権限昇格
pss-restricted-v2022-running-as-non-root コンテナの runAsNonRoot root 以外として実行
pss-restricted-v2022-running-as-non-root-user コンテナのユーザー ID root 以外のユーザーとして実行
pss-restricted-v2022-seccomp コンテナで使用される seccomp プロファイル Seccomp
pss-restricted-v2022-capabilities Linux 機能 機能

準備

  1. この手順で使用する gcloud コマンドと kubectl コマンドを含む Google Cloud CLI をインストールして初期化します。Cloud Shell を使用する場合、Google Cloud CLI がプリインストールされています。
  2. 制約テンプレートのデフォルト ライブラリを使用して、クラスタに Policy Controller v1.11.1 以降をインストールします。

Audit Pod Security Standards Restricted ポリシー バンドル

Policy Controller を使用すると、Kubernetes クラスタにポリシーを適用できます。前の表に概要を記載している、Google 推奨のベスト プラクティスに関するワークロードとそのコンプライアンスをテストする場合は、これらの制約を「監査」モードでデプロイできます。これにより、違反が明確になり、Kubernetes クラスタに適用する前に修正できます。

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

kubectl

  1. (省略可)kubectl でポリシーの制約をプレビューします。

    kubectl kustomize https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/pss-restricted-v2022
    
  2. kubectl でポリシーの制約を適用します。

    kubectl apply -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/pss-restricted-v2022
    

    次のような出力が表示されます。

    k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/pss-restricted-v2022-privilege-escalation created
    k8spspallowedusers.constraints.gatekeeper.sh/pss-restricted-v2022-running-as-non-root created
    k8spspcapabilities.constraints.gatekeeper.sh/pss-restricted-v2022-capabilities created
    k8spspseccomp.constraints.gatekeeper.sh/pss-restricted-v2022-seccomp created
    k8spspvolumetypes.constraints.gatekeeper.sh/pss-restricted-v2022-psp-volume-types created
    
  3. ポリシーの制約がインストールされていることを確認し、クラスタ全体で違反の存在を確認します。

    kubectl get -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/pss-restricted-v2022
    

    出力は次のようになります。

    NAME                                                                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/pss-restricted-v2022-privilege-escalation   dryrun               0
    
    NAME                                                                                    ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspallowedusers.constraints.gatekeeper.sh/pss-restricted-v2022-running-as-non-root   dryrun               0
    
    NAME                                                                             ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspcapabilities.constraints.gatekeeper.sh/pss-restricted-v2022-capabilities   dryrun               0
    
    NAME                                                                   ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspseccomp.constraints.gatekeeper.sh/pss-restricted-v2022-seccomp   dryrun               0
    
    NAME                                                                                ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
    k8spspvolumetypes.constraints.gatekeeper.sh/pss-restricted-v2022-psp-volume-types   dryrun               0
    

kpt

  1. kpt をインストールして設定します。これらの手順では、kpt を使用して Kubernetes リソースのカスタマイズとデプロイを行います。

  2. kpt を使用して GitHub から Pod Security Standards(PSS)Restricted v2022 ポリシー バンドルをダウンロードします。

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/pss-restricted-v2022
    
  3. set-enforcement-action kpt 関数を実行して、ポリシーの適用アクションを dryrun に設定します。

    kpt fn eval pss-restricted-v2022 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 \
      -- enforcementAction=dryrun
    
  4. kpt を使用して作業ディレクトリを初期化します。これにより、変更を追跡するためのリソースが作成されます。

    cd pss-restricted-v2022
    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 から Pod Security Standards(PSS)Restricted v2022 ポリシー バンドルをダウンロードします。

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/pss-restricted-v2022 policies/pss-restricted-v2022
    
  4. set-enforcement-action kpt 関数を実行して、ポリシーの適用アクションを dryrun に設定します。

    kpt fn eval policies/pss-restricted-v2022 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=dryrun
    
  5. (省略可)作成されるポリシー制約をプレビューします。

    kpt live init policies/pss-restricted-v2022
    kpt live apply --dry-run policies/pss-restricted-v2022
    
  6. Config Sync の同期ディレクトリで Kustomize を使用している場合は、ルート kustomization.yamlpolicies/pss-baseline-v2022 を追加します。 それ以外の場合は、policies/pss-baseline-v2022/kustomization.yaml ファイルを削除します。

    rm SYNC_ROOT_DIR/policies/pss-baseline-v2022/kustomization.yaml
    
  7. 変更を Config Sync リポジトリに push します。

    git add SYNC_ROOT_DIR/policies/pss-restricted-v2022
    git commit -m 'Adding Pod Security Standards Restricted audit enforcement'
    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=pss-restricted-v2022 -o json | jq -cC '.items[]| [.metadata.name,.status.totalViolations]'

違反がある場合は、次のコマンドを実行すると、制約ごとに違反メッセージのリストが表示されます。

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

Pod Security Standards Restricted ポリシーバンドルの適用アクションを変更する

クラスタのポリシー違反を確認したら、アドミッション コントローラが warn または deny のいずれかでクラスタへの非遵守リソースの適用をブロックするように、強制適用モードを変更することを検討できます。

kubectl

  1. kubectl を使用して、ポリシーの適用アクションを warn に設定します。

    kubectl get constraint -l policycontroller.gke.io/bundleName=pss-restricted-v2022 -o name | xargs -I {} kubectl patch {} --type='json' -p='[{"op":"replace","path":"/spec/enforcementAction","value":"warn"}]'
    
  2. ポリシー制約の適用アクションが更新されていることを確認します。

    kubectl get constraint -l policycontroller.gke.io/bundleName=pss-restricted-v2022
    

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/pss-restricted-v2022 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  3. 変更を Config Sync リポジトリに push します。

    git add SYNC_ROOT_DIR/pss-restricted-v2022
    git commit -m 'Adding Pod Security Standards Restricted 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
        hostPort: 80
        name: wordpress
EOF

次の例のように、アドミッション コントローラにより、このリソースが違反しているポリシー違反の警告の一覧が生成されるはずです。

Warning: [pss-baseline-v2022-host-ports] The specified hostNetwork and hostPort are not allowed, pod: wp-non-compliant. Allowed values: {"max": 0, "min": 0}
pod/wp-non-compliant created

Remove Pod Security Standards Restricted ポリシー バンドルを削除する

必要に応じて、Remove Pod Security Standards Restricted ポリシー バンドルをクラスタから削除できます。

kubectl

  • kubectl を使用してポリシーを削除します。

    kubectl delete constraint -l policycontroller.gke.io/bundleName=pss-restricted-v2022
    

kpt

  • ポリシーを削除します。

    kpt live destroy
    

Config Sync

Config Sync を使用してクラスタにポリシーをデプロイするオペレータは、次の手順を使用できます。

  1. 変更を Config Sync リポジトリに push します。

    git rm -r SYNC_ROOT_DIR/policies/pss-restricted-v2022
    git commit -m 'Removing Pod Security Standards Restricted policies'
    git push
    
  2. ステータスを確認します。

    gcloud alpha anthos config sync repo list --project PROJECT_ID
    

    SYNCED 列に表示されたリポジトリで、ポリシーが削除されていることを確認します。