Policy Controller 隨附預設的限制範本庫,可以與美國國家安全局 (NSA) 網路安全和基礎架構安全局 (CISA) Kubernetes 強化指南 1.2 版政策套裝組合搭配使用,評估叢集資源是否符合美國國家安全局 (NSA) 網路安全和基礎架構安全局 (CISA) Kubernetes 強化指南 1.2 版的某些部分。
本頁說明如何手動套用政策組合。 或者,您也可以直接套用政策組合。
如果您是 IT 管理員和營運人員,想確保雲端平台中執行的所有資源都符合機構的法規遵循規定,請參閱本頁面,瞭解如何提供及維護自動化稽核或強制執行功能。如要進一步瞭解我們在內容中提及的常見角色和範例工作,請參閱「常見的 GKE Enterprise 使用者角色和工作」。 Google Cloud
NSA CISA Kubernetes Hardening 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 | 必須禁止共用主機命名空間。 |
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.privileged」設為「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」和「ClusterRoles」中使用 `pods/exec`。 |
nsa-cisa-k8s-v1.2-running-as-non-root | 限制容器以超級使用者身分執行。 |
nsa-cisa-k8s-v1.2-seccomp | Seccomp 設定檔不得明確設為 `Unconfined`。 |
nsa-cisa-k8s-v1.2-selinux | 無法設定 SELinux 類型,或設定自訂 SELinux 使用者或角色選項。 |
事前準備
- 安裝並初始化 Google Cloud CLI,其中提供這些操作說明中使用的
gcloud
和kubectl
指令。如果您使用 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: "networking.k8s.io" version: "v1" kind: "NetworkPolicy"
套用
policycontroller-config.yaml
資訊清單:kubectl apply -f policycontroller-config.yaml
稽核 NSA CISA Kubernetes Hardening v1.2 政策套裝組合
您可以透過 Policy Controller,對 Kubernetes 叢集強制執行政策。如要測試工作負載及其是否符合上表列出的 NSA CISA Kubernetes Hardening Guide v1.2 政策,您可以「稽核」模式部署這些限制,找出違規事項,更重要的是,您可以在 Kubernetes 叢集上選擇強制執行這些限制前,先修正違規事項。
您可以使用 kubectl、kpt 或 Config Sync,將 spec.enforcementAction
設為 dryrun
,套用這些政策。
kubectl
(選用) 使用 kubectl 預覽政策限制:
kubectl kustomize https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/nsa-cisa-k8s-v1.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
確認已安裝政策限制,並檢查叢集是否發生違規情形:
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
安裝及設定 kpt。
這些操作說明會使用 kpt 自訂及部署 Kubernetes 資源。
使用 kpt 從 GitHub 下載 NSA CISA Kubernetes 強化指南 1.2 版政策套裝組合:
kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/anthos-bundles/nsa-cisa-k8s-v1.2
執行
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
使用 kpt 初始化工作目錄,這會建立資源來追蹤變更:
cd nsa-cisa-k8s-v1.2 kpt live init
使用 kpt 套用政策限制:
kpt live apply
確認已安裝政策限制,並檢查叢集是否發生違規情形:
kpt live status --output table --poll-until current
如果狀態為
CURRENT
,表示限制條件已成功安裝。
Config Sync
安裝及設定 kpt。
這些操作說明會使用 kpt 自訂及部署 Kubernetes 資源。
如果運算子使用 Config Sync 將政策部署至叢集,可以按照下列指示操作:
變更為 Config Sync 的同步目錄:
cd SYNC_ROOT_DIR
如要建立或附加
.gitignore
(使用resourcegroup.yaml
):echo resourcegroup.yaml >> .gitignore
建立專屬的
policies
目錄:mkdir -p policies
使用 kpt 從 GitHub 下載 NSA CISA Kubernetes 強化指南 1.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
執行
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
(選用) 預覽要建立的政策限制:
kpt live init policies/nsa-cisa-k8s-v1.2 kpt live apply --dry-run policies/nsa-cisa-k8s-v1.2
如果 Config Sync 的同步目錄使用 Kustomize,請將
policies/nsa-cisa-k8s-v1.2
新增至根目錄kustomization.yaml
。否則請移除policies/nsa-cisa-k8s-v1.2/kustomization.yaml
檔案:rm SYNC_ROOT_DIR/policies/nsa-cisa-k8s-v1.2/kustomization.yaml
將變更推送至 Config Sync 存放區:
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
驗證安裝狀態:
watch gcloud beta container fleet config-management status --project PROJECT_ID
如果狀態為
SYNCED
,表示政策已安裝完成。
查看政策違規事項
以稽核模式安裝政策限制後,您可以使用 Policy Controller 資訊主頁,在 UI 中查看叢集的違規情形。
您也可以使用 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 Hardening v1.2 政策套裝組合的強制執行動作
審查叢集中的政策違規事項後,您可以考慮變更強制執行模式,讓 Admission Controller warn
或 deny
阻止不符規定的資源套用至叢集。
kubectl
使用 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"}]'
確認政策限制強制執行動作已更新:
kubectl get constraints -l policycontroller.gke.io/bundleName=nsa-cisa-k8s-v1.2
KPT
執行
set-enforcement-action
kpt 函式,將政策的強制執行動作設為warn
:kpt fn eval -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
套用政策限制:
kpt live apply
Config Sync
如果運算子使用 Config Sync 將政策部署至叢集,可以按照下列指示操作:
變更為 Config Sync 的同步目錄:
cd SYNC_ROOT_DIR
執行
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
將變更推送至 Config Sync 存放區:
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
驗證安裝狀態:
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: [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 Hardening v1.2 政策套裝組合
如有需要,可以從叢集移除 NSA CISA Kubernetes Hardening 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 將政策部署至叢集,可以按照下列指示操作:
將變更推送至 Config Sync 存放區:
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
確認狀態:
nomos status
叢集應會顯示
SYNCED
狀態,且資源已移除。