CIS Kubernetes Benchmark ポリシーの制約を使用する
Policy Controller には、CIS Kubernetes Benchmark に対するクラスタのコンプライアンスを監査するために CIS バンドルで使用できる制約テンプレートのデフォルト ライブラリが付属しています。このベンチマークは、強固なセキュリティ体制がサポートされるように Kubernetes を構成するための一連の推奨事項です。
この制約のバンドルは、次のドメインに対応し、ポリシーを適用します。
- RBAC とサービス アカウント
- Pod のセキュリティ ポリシー
- ネットワーク ポリシーと CNI
- シークレット管理
- 一般ポリシー
これらはまだ CIS で認定されていません。
バンドルには、CIS ベンチマークの対応するコントロールを満たすために次の制約が含まれています。
制約 | コントロール ID | コントロールの説明 | 参照 |
---|---|---|---|
psp-privileged-container | 5.2.1 | 特権付きコンテナの使用を最小限に抑える | × |
psp-host-namespace | 5.2.2 | ホストプロセス ID の Namespace を共有するコンテナの許可を最小限に抑える | × |
5.2.3 | ホスト IPC の Namespace を共有するコンテナの許可を最小限に抑える | × | |
psp-host-network-ports | 5.2.4 | ホスト ネットワークの Namespace を共有するコンテナの許可を最小限に抑える | × |
psp-allow-privilege-escalation-container | 5.2.5 | allowPrivilegeEscalation を使用してコンテナの許可を最小限に抑える | × |
psp-pods-must-run-as-nonroot | 5.2.6 | ルートコンテナの許可を最小限に抑える | × |
psp-capabilities | 5.2.7 | NET_RAW 機能を使用してコンテナの許可を最小限に抑える | × |
5.2.8 | 追加された機能を使用してコンテナの許可を最小限に抑える | × | |
5.2.9 | 割り当てられた機能を使用してコンテナの許可を最小限に抑える | × | |
require-namespace-network-policies | 5.3.2 | すべての Namespace にネットワーク ポリシーが定義されていることを確認する | ○ |
no-secrets-as-env-vars | 5.4.1 | Secret を環境変数としてではなくファイルとして使用する | × |
psp-seccomp-default | 5.7.2 | Pod の定義で seccomp プロファイルが docker/default に設定されていることを確認する | × |
pods-require-security-context | 5.7.3 | セキュリティ コンテキストを Pod とコンテナに適用する | × |
始める前に
- この手順で使用する
gcloud
コマンドとkubectl
コマンドを含む Google Cloud CLI をインストールして初期化します。Cloud Shell を使用する場合、Google Cloud CLI がプリインストールされています。 - 制約テンプレートのデフォルト ライブラリを使用して、クラスタに Policy Controller をインストールします。また、このバンドルには参照制約が含まれているため、参照制約のサポートを有効にする必要があります。この参照制約は概要テーブルに表示されます。
参照制約用に Policy Controller を構成する
次の YAML マニフェストを
policycontroller-config.yaml
という名前のファイルに保存します。このマニフェストでは、特定の種類のオブジェクトを監視するように Policy Controller を構成します。apiVersion: config.gatekeeper.sh/v1alpha1 kind: Config metadata: name: config namespace: "gatekeeper-system" spec: sync: syncOnly: - group: "" version: "v1" kind: "Namespace" - group: "networking.k8s.io" version: "v1" kind: "NetworkPolicy"
policycontroller-config.yaml
マニフェストを適用します。kubectl apply -f policycontroller-config.yaml
Policy Controller を使用して CIS ポリシーを監査する
Policy Controller を使用すると、Kubernetes クラスタにポリシーを適用できます。前の表にある CIS ポリシーに関するワークロードとそのコンプライアンスをテストする場合、これらの制約を監査モードでデプロイできます。これにより、違反がより明確になり、Kubernetes クラスタに適用する前に修正することが可能になります。
kubectl、kpt、または Config Sync を使用して spec.enforcementAction
を dryrun
に設定し、これらのポリシーを適用できます。
kubectl
(省略可)kubectl でポリシーの制約をプレビューします。
kubectl kustomize https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1
kubectl でポリシーの制約を適用します。
kubectl apply -k https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1
次のような出力が表示されます。
k8snoenvvarsecrets.constraints.gatekeeper.sh/cis-k8s-v1.5.1-no-secrets-as-env-vars created k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-allow-privilege-escalation created k8spspallowedusers.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-pods-must-run-as-nonroot created k8spspcapabilities.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-capabilities created k8spsphostnamespace.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-namespace created k8spsphostnetworkingports.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-network-ports created k8spspprivilegedcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-privileged-container created k8spspseccomp.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-seccomp-default created k8spodsrequiresecuritycontext.constraints.gatekeeper.sh/cis-k8s-v1.5.1-pods-require-security-context created k8sprohibitrolewildcardaccess.constraints.gatekeeper.sh/cis-k8s-v1.5.1-prohibit-role-wildcard-access created k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/cis-k8s-v1.5.1-require-namespace-network-policies created k8srestrictrolebindings.constraints.gatekeeper.sh/cis-k8s-v1.5.1-restrict-clusteradmin-rolebindings created
ポリシーの制約がインストールされていることを確認し、クラスタ全体で違反の存在を確認します。
kubectl get -k https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1
出力は次のようになります。
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8snoenvvarsecrets.constraints.gatekeeper.sh/cis-k8s-v1.5.1-no-secrets-as-env-vars dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-allow-privilege-escalation dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspallowedusers.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-pods-must-run-as-nonroot dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspcapabilities.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-capabilities dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spsphostnamespace.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-namespace dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spsphostnetworkingports.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-network-ports dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspprivilegedcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-privileged-container dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spspseccomp.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-seccomp-default dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8spodsrequiresecuritycontext.constraints.gatekeeper.sh/cis-k8s-v1.5.1-pods-require-security-context dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8sprohibitrolewildcardaccess.constraints.gatekeeper.sh/cis-k8s-v1.5.1-prohibit-role-wildcard-access dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/cis-k8s-v1.5.1-require-namespace-network-policies dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS k8srestrictrolebindings.constraints.gatekeeper.sh/cis-k8s-v1.5.1-restrict-clusteradmin-rolebindings dryrun 0
kpt
kpt をインストールして設定します。これらの手順では、kpt を使用して Kubernetes リソースのカスタマイズとデプロイを行います。
kpt を使用して GitHub から CIS ポリシー バンドルをダウンロードします。
kpt pkg get https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1
set-enforcement-action
kpt 関数を実行して、ポリシーの適用アクションをdryrun
に設定します。kpt fn eval cis-k8s-v1.5.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 \ -- enforcementAction=dryrun
kpt を使用して作業ディレクトリを初期化します。これにより、変更を追跡するためのリソースが作成されます。
cd cis-k8s-v1.5.1 kpt live init
kpt を使用してポリシーの制約を適用します。
kpt live apply
次のような出力が表示されます。
k8spspseccomp.constraints.gatekeeper.sh/psp-seccomp-docker-default created k8spodsrequiresecuritycontext.constraints.gatekeeper.sh/pods-require-security-context created k8sprohibitrolewildcardaccess.constraints.gatekeeper.sh/prohibit-role-wildcard-access created k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/require-namespace-network-policies created k8snoenvvarsecrets.constraints.gatekeeper.sh/no-secrets-as-env-vars created k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/psp-allow-privilege-escalation-container created k8spspallowedusers.constraints.gatekeeper.sh/psp-pods-must-run-as-nonroot created k8spsphostnetworkingports.constraints.gatekeeper.sh/psp-host-network-ports created k8srestrictrolebindings.constraints.gatekeeper.sh/restrict-clusteradmin-rolebindings created k8spspcapabilities.constraints.gatekeeper.sh/psp-capabilities created k8spsphostnamespace.constraints.gatekeeper.sh/psp-host-namespace created k8spspprivilegedcontainer.constraints.gatekeeper.sh/psp-privileged-container created 13 resource(s) applied. 13 created, 0 unchanged, 0 configured, 0 failed
ポリシーの制約がインストールされていることを確認し、クラスタ全体で違反の存在を確認します。
kpt live status --output table --poll-until current
ステータスが
CURRENT
の場合、制約が正常にインストールされています。
Config Sync
kpt をインストールして設定します。これらの手順では、kpt を使用して Kubernetes リソースのカスタマイズとデプロイを行います。
Anthos Config Management を初めて使用する場合は、Anthos Config Management を有効にします。
Config Sync を使用してクラスタにクラスタをデプロイするオペレータは、次の手順を使用できます。
Config Sync の同期ディレクトリに移動します。
cd SYNC_ROOT_DIR
専用の
policies
ディレクトリを作成します。mkdir -p policies
kpt を使用して GitHub から CIS ポリシー バンドルをダウンロードします。
kpt pkg get https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1 policies/cis-k8s-v1.5.1
set-enforcement-action
kpt 関数を実行して、ポリシーの適用アクションをdryrun
に設定します。kpt fn eval policies/cis-k8s-v1.5.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=dryrun
(省略可)作成されるポリシー制約をプレビューします。
kpt live init policies/cis-k8s-v1.5.1 kpt live apply --dry-run policies/cis-k8s-v1.5.1
次のような出力が表示されます。
Dry-run strategy: client inventory update started inventory update finished apply phase started k8snoenvvarsecrets.constraints.gatekeeper.sh/cis-k8s-v1.5.1-no-secrets-as-env-vars apply successful k8spspallowprivilegeescalationcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-allow-privilege-escalation apply successful k8spspallowedusers.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-pods-must-run-as-nonroot apply successful k8spspcapabilities.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-capabilities apply successful k8spsphostnamespace.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-namespace apply successful k8spsphostnetworkingports.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-host-network-ports apply successful k8spspprivilegedcontainer.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-privileged-container apply successful k8spspseccomp.constraints.gatekeeper.sh/cis-k8s-v1.5.1-psp-seccomp-default apply successful k8spodsrequiresecuritycontext.constraints.gatekeeper.sh/cis-k8s-v1.5.1-pods-require-security-context apply successful k8sprohibitrolewildcardaccess.constraints.gatekeeper.sh/cis-k8s-v1.5.1-prohibit-role-wildcard-access apply successful k8srequirenamespacenetworkpolicies.constraints.gatekeeper.sh/cis-k8s-v1.5.1-require-namespace-network-policies apply successful k8srestrictrolebindings.constraints.gatekeeper.sh/cis-k8s-v1.5.1-restrict-clusteradmin-rolebindings apply successful apply phase finished inventory update started inventory update finished apply result: 12 attempted, 12 successful, 0 skipped, 0 failed
Config Sync の同期ディレクトリで Kustomize を使用している場合は、ルート
kustomization.yaml
にpolicies/cis-k8s-v1.5.1
を追加します。それ以外の場合は、policies/cis-k8s-v1.5.1/kustomization.yaml
ファイルを削除します。rm SYNC_ROOT_DIR/policies/cis-k8s-v1.5.1/kustomization.yaml
変更を Config Sync リポジトリに push します。
git add SYNC_ROOT_DIR/policies/cis-k8s-v1.5.1 git commit -m 'Adding CIS policy audit enforcement' git push
インストールのステータスを確認します。
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-k8s-v1.5.1 -o json | jq -cC '.items[]| [.metadata.name,.status.totalViolations]'
違反がある場合は、次のコマンドを実行すると、制約ごとに違反メッセージのリストが表示されます。
kubectl get constraint -l policycontroller.gke.io/bundleName=cis-k8s-v1.5.1 -o json | jq -C '.items[]| select(.status.totalViolations>0)| [.metadata.name,.status.violations[]?]'
CIS ポリシーを適用する
クラスタのポリシー違反を確認したら、これらのポリシーを適用して、非遵守のリソースがクラスタに適用されないようにします。
kubectl
kubectl を使用して、ポリシーの適用アクションを
deny
に設定します。kubectl get constraint -l policycontroller.gke.io/bundleName=cis-k8s-v1.5.1 -o json | jq '.items[].spec.enforcementAction="deny"' | kubectl apply -f -
ポリシー制約の適用アクションが更新されていることを確認します。
kubectl get -k https://github.com/GoogleCloudPlatform/acm-policy-controller-library.git/bundles/cis-k8s-v1.5.1
kpt
set-enforcement-action
kpt 関数を実行して、ポリシーの適用アクションをdeny
に設定します。kpt fn eval -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=deny
ポリシー制約を適用します。
kpt live apply
Config Sync
Config Sync を使用してクラスタにクラスタをデプロイするオペレータは、次の手順を使用できます。
Config Sync の同期ディレクトリに移動します。
cd SYNC_ROOT_DIR
set-enforcement-action
kpt 関数を実行して、ポリシーの適用アクションをdeny
に設定します。kpt fn eval policies/cis-k8s-v1.5.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=deny
変更を Config Sync リポジトリに push します。
git add SYNC_ROOT_DIR/policies/cis-k8s-v1.5.1 git commit -m 'Enforcing CIS policies for GKE' git push
インストールのステータスを確認します。
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
アドミッション コントローラにより、このリソースが違反しているポリシー違反のエラー一覧が生成されるはずです。次に例を示します。
Error from server (Forbidden): error when creating "STDIN": admission webhook "validation.gatekeeper.sh" denied the request: [cis-k8s-v1.5.1-psp-pods-must-run-as-nonroot] Container wordpress is attempting to run without a required securityContext/runAsNonRoot or securityContext/runAsUser != 0 [cis-k8s-v1.5.1-psp-allow-privilege-escalation] Privilege escalation container is not allowed: wordpress [cis-k8s-v1.5.1-psp-seccomp-default] Seccomp profile 'not configured' is not allowed for container 'wordpress'. Found at: no explicit profile found. Allowed profiles: {"RuntimeDefault", "docker/default", "runtime/default"} [cis-k8s-v1.5.1-psp-capabilities] containeris not dropping all required capabilities. Container must drop all of ["NET_RAW"] or "ALL" [cis-k8s-v1.5.1-pods-require-security-context] securityContext must be defined for all Pod containers