Policy Controller 隨附預設的限制範本庫,可以與 CIS 套裝組合搭配使用,稽核叢集是否符合 CIS Kubernetes 基準 v1.5.1。這項基準由多項建議組成,可協助您設定 Kubernetes 來支援功能強大的安全防護措施。
本頁說明如何手動套用政策組合。 或者,您也可以直接套用政策組合。
如果您是 IT 管理員和營運人員,想確保雲端平台中執行的所有資源都符合機構的法規遵循規定,請參閱本頁面,瞭解如何提供及維護自動化稽核或強制執行功能。如要進一步瞭解我們在內容中提及的常見角色和範例工作,請參閱「常見的 GKE Enterprise 使用者角色和工作」。 Google Cloud
這組限制條件可解決並強制執行下列領域的政策:
- RBAC 和服務帳戶
- Pod 安全性政策
- 網路政策和 CNI
- 密鑰管理
- 一般政策
CIS Kubernetes v1.5.1 政策套裝組合限制
限制名稱 | 控制項說明 | 控制項 ID |
---|---|---|
cis-k8s-v1.5.1-restrict-clusteradmin-rolebindings.yaml | 限制使用 `cluster-admin` 角色 | 5.1.1 |
cis-k8s-v1.5.1-prohibit-role-wildcard-access.yaml | 限制在「Roles」和「ClusterRoles」中使用萬用字元 | 5.1.3 |
cis-k8s-v1.5.1-psp-privileged-container.yaml | 盡量減少特殊權限容器的准入 | 5.2.1 |
cis-k8s-v1.5.1-psp-host-namespace.yaml | 盡量減少允許容器共用主機程序 ID 命名空間 | 5.2.2 |
盡量減少允許容器共用主機 IPC 命名空間 | 5.2.3 | |
cis-k8s-v1.5.1-psp-host-network-ports | 盡量減少允許容器共用主機網路命名空間 | 5.2.4 |
cis-k8s-v1.5.1-psp-allow-privilege-escalation-container | 盡量減少允許 allowPrivilegeEscalation 的容器 | 5.2.5 |
cis-k8s-v1.5.1-psp-pods-must-run-as-nonroot | 盡量減少根容器的許可 | 5.2.6 |
cis-k8s-v1.5.1-psp-capabilities | 盡量減少具有 NET_RAW 功能的容器 | 5.2.7 |
盡量減少新增功能的容器 | 5.2.8 | |
盡量減少指派功能的容器 | 5.2.9 | |
cis-k8s-v1.5.1-require-namespace-network-policies | 確保所有命名空間都已定義網路政策 | 5.3.2 |
cis-k8s-v1.5.1-no-secrets-as-env-vars | 建議使用 Secret 做為檔案,而非 Secret 做為環境變數 | 5.4.1 |
cis-k8s-v1.5.1-psp-seccomp-default | 確認 Pod 定義中的 seccomp 設定檔已設為 `docker/default` | 5.7.2 |
cis-k8s-v1.5.1-pods-require-security-context | 將安全性情境套用至 Pod 和容器 | 5.7.3 |
事前準備
- 安裝並初始化 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: "" version: "v1" kind: "Namespace" - group: "networking.k8s.io" version: "v1" kind: "NetworkPolicy"
套用
policycontroller-config.yaml
資訊清單:kubectl apply -f policycontroller-config.yaml
稽核 CIS Kubernetes v1.5.1 政策套裝組合
您可以透過 Policy Controller,對 Kubernetes 叢集強制執行政策。如要測試工作負載是否符合上表列出的 CIS 政策,您可以「稽核」模式部署這些限制,找出違規事項,更重要的是,在 Kubernetes 叢集強制執行限制前,您有機會修正這些問題。
您可以使用 kubectl、kpt 或 Config Sync,將 spec.enforcementAction
設為 dryrun
來套用這些政策。
kubectl
(選用) 使用 kubectl 預覽政策限制:
kubectl kustomize https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/cis-k8s-v1.5.1
使用 kubectl 套用政策限制:
kubectl apply -k https://github.com/GoogleCloudPlatform/gke-policy-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/gke-policy-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/gke-policy-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 資源。
如果運算子使用 Config Sync 將政策部署至叢集,可以按照下列指示操作:
變更為 Config Sync 的同步目錄:
cd SYNC_ROOT_DIR
如要使用
resourcegroup.yaml
建立或附加.gitignore
,請按照下列步驟操作:echo resourcegroup.yaml >> .gitignore
建立專屬的
policies
目錄:mkdir -p policies
使用 kpt 從 GitHub 下載 CIS 政策套裝組合:
kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-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,請將
policies/cis-k8s-v1.5.1
新增至根目錄kustomization.yaml
。否則請移除policies/cis-k8s-v1.5.1/kustomization.yaml
檔案:rm SYNC_ROOT_DIR/policies/cis-k8s-v1.5.1/kustomization.yaml
將變更推送至 Config Sync 存放區:
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
,表示政策已安裝完成。
查看政策違規事項
在稽核模式下安裝政策限制後,您可以使用 Policy Controller 資訊主頁,在 UI 中查看叢集違規情形。
您也可以使用 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 Kubernetes v1.5.1 政策套裝組合強制執行動作
審查叢集中的政策違規事項後,您可以考慮變更強制執行模式,讓 Admission Controller warn
或 deny
阻止不符規定的資源套用至叢集。
kubectl
使用 kubectl 將政策的強制執行動作設為
warn
:kubectl get constraint -l policycontroller.gke.io/bundleName=cis-k8s-v1.5.1 -o name | xargs -I {} kubectl patch {} --type='json' -p='[{"op":"replace","path":"/spec/enforcementAction","value":"warn"}]'
確認政策限制強制執行動作已更新:
kubectl get constraint -l policycontroller.gke.io/bundleName=cis-k8s-v1.5.1
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/cis-k8s-v1.5.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
將變更推送至 Config Sync 存放區:
git add SYNC_ROOT_DIR/policies/cis-k8s-v1.5.1 git commit -m 'Adding CIS Kubernetes v1.5.1 policy bundle 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: [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 Warning: [cis-k8s-v1.5.1-psp-allow-privilege-escalation] Privilege escalation container is not allowed: wordpress Warning: [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"} Warning: [cis-k8s-v1.5.1-psp-capabilities] container <wordpress> is not dropping all required capabilities. Container must drop all of ["NET_RAW"] or "ALL" Warning: [cis-k8s-v1.5.1-pods-require-security-context] securityContext must be defined for all Pod containers pod/wp-non-compliant created
移除 CIS Kubernetes v1.5.1 政策套裝組合
如有需要,可以從叢集中移除 CIS K8s 政策套裝組合。
kubectl
使用 kubectl 移除政策:
kubectl delete constraint -l policycontroller.gke.io/bundleName=cis-k8s-v1.5.1
KPT
移除政策:
kpt live destroy
Config Sync
如果運算子使用 Config Sync 將政策部署至叢集,可以按照下列指示操作:
將變更推送至 Config Sync 存放區:
git rm -r SYNC_ROOT_DIR/policies/cis-k8s-v1.5.1 git commit -m 'Removing CIS policies' git push
確認狀態:
nomos status
叢集應會顯示
SYNCED
狀態,且資源已移除。