Policy Controller 隨附預設的限制範本庫,可以與 Cloud Service Mesh 安全性套裝組合搭配使用,檢查您的網格安全漏洞機制與最佳做法是否符合法規。
本頁面說明如何手動套用政策套裝組合。或者,您也可以直接套用政策套裝組合。
這組限制會處理並強制執行下列領域的政策:
- Cloud Service Mesh 強制執行 mTLS 流量
- Cloud Service Mesh AuthorizationPolicy 最佳做法
- Cloud Service Mesh 工作負載安全性強制執行
Cloud Service Mesh 政策套件限制
| 限制名稱 | 限制說明 | 控制項 ID |
|---|---|---|
| asm-policy-v0.0.1-asm-ingressgateway-label | 僅在 ingressgateway Pod 強制使用 istio ingressgateway 標籤 | 1.1.1 |
| asm-policy-v0.0.1-asm-sidecar-injection | 強制一律將 Istio Proxy Sidecar 插入工作負載 Pod | 1.1.2 |
| asm-policy-v0.0.1-asm-authz-policy-mesh-default-deny | 強制執行網格層級的預設拒絕 AuthorizationPolicy | 1.2.1 |
| asm-policy-v0.0.1-asm-authz-policy-normalization | 強制執行 AuthorizationPolicy 正規化 | 1.2.2 |
| asm-policy-v0.0.1-asm-authz-policy-safe-pattern | 強制執行 AuthorizationPolicy 安全模式 | 1.2.3 |
| asm-policy-v0.0.1-asm-peer-authn-mesh-strict-mtls | 強制執行網格層級的嚴格 mTLS PeerAuthentication | 1.3.1 |
| asm-policy-v0.0.1-asm-peer-authn-strict-mtls | 強制執行所有 PeerAuthentication,不得覆寫嚴格 mTLS | 1.3.2 |
| asm-policy-v0.0.1-asm-request-authn-prohibited-output-headers | 強制執行 jwtRules outputPayloadToHeader,不含常見的 HTTP 要求標頭 | 1.4.1 |
套裝組合設定檔
在 Cloud Service Mesh 安全性政策套件中,您可以根據嚴格程度使用兩種設定檔。嚴格程度較低時,套用的限制較少,因此彈性較高。高嚴格等級會套用更多限制,提供更安全的政策控管。
低嚴格程度
低嚴格程度設定檔具有下列政策限制:
標籤
istio:ingressgateway只能由 Istio 輸入閘道 Pod 使用。在
AuthorizationPolicy中,只有選取標籤為istio:ingressgateway的 Istio 輸入閘道,才能使用hosts或notHosts欄位。在
AuthorizationPolicy中,使用methods或notMethods欄位時,值必須為大寫字母。在
AuthorizationPolicy中,使用request.headers欄位時,值不得包含空格。在
AuthorizationPolicy中,使用paths或notPaths欄位時,值必須是正規化值。
高嚴格程度
高嚴格程度包含低嚴格程度的所有限制,以及下列限制:
對於所有工作負載 Pod,無法套用註解
sidecar.istio.io/inject: false來略過 Proxy 注入。AuthorizationPolicy必須遵循 ALLOW-with-positive-matching 或 DENY-with-negative-match。在
AuthorizationPolicy中,使用hosts或notHosts欄位時,值必須是<host-name>和<host-name>:*的組合。定義嚴格 mTLS 的網格層級
PeerAuthentication會強制執行。為遵循嚴格 mTLS,網格中所有
PeerAuthentication的 mTLS 模式只能是UNSET或STRICT。
套裝組合設定
| KPT 設定工具 | 說明 |
|---|---|
| strictness-level | Cloud Service Mesh 套件嚴格程度設定檔,選項為「Low」或「High」(預設) |
事前準備
- 安裝並初始化 Google Cloud CLI,其中提供這些操作說明中使用的
gcloud和kubectl指令。如果使用 Cloud Shell,系統會預先安裝 Google Cloud CLI。 - 在叢集上安裝 Policy Controller 1.11.2 以上版本,並使用預設的限制範本庫。由於這個套件包含參照限制,您也必須啟用參照限制支援。
- 確認叢集已安裝 Cloud Service Mesh。
設定 Policy Controller 以使用參照限制
將下列 YAML 資訊清單儲存為
policycontroller-config.yaml:apiVersion: config.gatekeeper.sh/v1alpha1 kind: Config metadata: name: config namespace: "gatekeeper-system" spec: sync: syncOnly: - group: "" version: "v1" kind: "Namespace" - group: "security.istio.io" version: "v1beta1" kind: "AuthorizationPolicy" - group: "security.istio.io" version: "v1beta1" kind: "PeerAuthentication"這個資訊清單會設定 Policy Controller 監控特定類型的物件。
套用
policycontroller-config.yaml資訊清單:kubectl apply -f policycontroller-config.yaml
稽核 Cloud Service Mesh 政策套件
Policy Controller 可讓您對 Kubernetes 叢集強制執行政策。為協助測試工作負載,以及確認工作負載是否符合上表所述的 Cloud Service Mesh 安全性政策,您可以在「稽核」模式中部署這些限制,藉此找出違規事項,更重要的是,您可以在 Kubernetes 叢集強制執行這些限制之前,先修正這些違規事項。
您可以使用 kubectl、kpt 或 Config Sync 套用這些政策,並將 spec.enforcementAction 設為 dryrun。
kubectl
(選用) 使用 kubectl 預覽政策限制:
kubectl kustomize https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1
使用 kubectl 套用政策限制:
kubectl apply -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1
輸出內容如下:
asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-mesh-default-deny created asmauthzpolicynormalization.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-normalization created asmauthzpolicysafepattern.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-safe-pattern created asmingressgatewaylabel.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-ingressgateway-label created asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-mesh-strict-mtls created asmpeerauthnstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-strict-mtls created asmrequestauthnprohibitedoutputheaders.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-request-authn-prohibited-output-headers created asmsidecarinjection.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-sidecar-injection created
確認政策限制已安裝,並檢查叢集是否有違規情形:
kubectl get -k https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1
輸出結果會與下列內容相似:
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-mesh-default-deny dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmauthzpolicynormalization.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-normalization dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmauthzpolicysafepattern.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-safe-pattern dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmingressgatewaylabel.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-ingressgateway-label dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-mesh-strict-mtls dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmpeerauthnstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-strict-mtls dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmrequestauthnprohibitedoutputheaders.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-request-authn-prohibited-output-headers dryrun 0 NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONS asmsidecarinjection.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-sidecar-injection dryrun 0
KPT
安裝及設定 kpt。kpt 用於自訂及部署 Kubernetes 資源。
使用 kpt 從 GitHub 下載 Cloud Service Mesh 安全性政策套件:
kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1
執行
set-enforcement-actionkpt 函式,將政策的違規處置設為dryrun:kpt fn eval asm-policy-v0.0.1 -i ghcr.io/kptdev/krm-functions-catalog/set-enforcement-action:v0.1 \ -- enforcementAction=dryrun
執行 kpt setter 函式,設定 Cloud Service Mesh 安全性政策的特定欄位:
kpt fn eval asm-policy-v0.0.1 --image ghcr.io/kptdev/krm-functions-catalog/apply-setters:v0.2 -- \ strictness-level="Low"
使用 kpt 初始化目前使用的目錄,這會建立資源來追蹤變更:
cd asm-policy-v0.0.1 kpt live init
使用 kpt 套用政策限制:
kpt live apply
輸出內容如下:
asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/asm-authz-policy-mesh-default-deny created asmauthzpolicynormalization.constraints.gatekeeper.sh/asm-authz-policy-normalization created asmauthzpolicysafepattern.constraints.gatekeeper.sh/asm-authz-policy-safe-pattern created asmingressgatewaylabel.constraints.gatekeeper.sh/asm-ingressgateway-label created asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/asm-peer-authn-mesh-strict-mtls created asmpeerauthnstrictmtls.constraints.gatekeeper.sh/asm-peer-authn-strict-mtls created asmsidecarinjection.constraints.gatekeeper.sh/asm-sidecar-injection created 7 resource(s) applied. 7 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 下載 Cloud Service Mesh 安全性政策套件:
kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1 policies/asm-policy-v0.0.1
執行
set-enforcement-actionkpt 函式,將政策的違規處置設為dryrun:kpt fn eval policies/asm-policy-v0.0.1 -i ghcr.io/kptdev/krm-functions-catalog/set-enforcement-action:v0.1 -- enforcementAction=dryrun
執行 kpt setter 函式,設定 Cloud Service Mesh 安全性政策的特定欄位:
kpt fn eval policies/asm-policy-v0.0.1 --image ghcr.io/kptdev/krm-functions-catalog/apply-setters:v0.2 -- \ strictness-level="Low"
(選用) 預覽要建立的政策限制:
kpt live init policies/asm-policy-v0.0.1 kpt live apply --dry-run policies/asm-policy-v0.0.1
輸出內容如下:
Dry-run strategy: client inventory update started inventory update finished apply phase started asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-mesh-default-deny apply successful asmauthzpolicynormalization.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-normalization apply successful asmauthzpolicysafepattern.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-authz-policy-safe-pattern apply successful asmingressgatewaylabel.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-ingressgateway-label apply successful asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-mesh-strict-mtls apply successful asmpeerauthnstrictmtls.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-peer-authn-strict-mtls apply successful asmrequestauthnprohibitedoutputheaders.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-request-authn-prohibited-output-headers apply successful asmsidecarinjection.constraints.gatekeeper.sh/asm-policy-v0.0.1-asm-sidecar-injection apply successful apply phase finished inventory update started inventory update finished apply result: 8 attempted, 8 successful, 0 skipped, 0 failed
如果 Config Sync 的同步目錄使用 Kustomize,請將
policies/asm-policy-v0.0.1新增至根目錄kustomization.yaml。否則,請移除policies/asm-policy-v0.0.1/kustomization.yaml檔案:rm SYNC_ROOT_DIR/policies/asm-policy-v0.0.1/kustomization.yaml
將變更推送至 Config Sync 存放區:
git add SYNC_ROOT_DIR/policies/asm-policy-v0.0.1 git commit -m 'Adding ASM security 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=asm-policy-v0.0.1 -o json | jq -cC '.items[]| [.metadata.name,.status.totalViolations]'
如有違規情形,您可以使用下列指令,查看各項限制的違規訊息清單:
kubectl get constraint -l policycontroller.gke.io/bundleName=asm-policy-v0.0.1 -o json | jq -C '.items[]| select(.status.totalViolations>0)| [.metadata.name,.status.violations[]?]'
變更 Cloud Service Mesh 政策套裝組合違規處置
查看叢集中的政策違規情形後,您可以考慮變更強制執行模式,讓 Admission Controller warn或甚至deny封鎖不符合規定的資源,不讓其套用至叢集。
kubectl
使用 kubectl 將政策的違規處置設為
warn:kubectl get constraint -l policycontroller.gke.io/bundleName=asm-policy-v0.0.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=asm-policy-v0.0.1
KPT
執行
set-enforcement-actionkpt 函式,將政策的違規處置設為warn:kpt fn eval -i ghcr.io/kptdev/krm-functions-catalog/set-enforcement-action:v0.1 -- enforcementAction=warn
套用政策限制:
kpt live apply
Config Sync
如果操作人員使用 Config Sync 將政策部署至叢集,可以按照下列操作說明進行:
變更為 Config Sync 的同步目錄:
cd SYNC_ROOT_DIR
執行
set-enforcement-actionkpt 函式,將政策的違規處置設為warn:kpt fn eval policies/asm-policy-v0.0.1 -i ghcr.io/kptdev/krm-functions-catalog/set-enforcement-action:v0.1 -- enforcementAction=warn
將變更推送至 Config Sync 存放區:
git add SYNC_ROOT_DIR/policies/asm-policy-v0.0.1 git commit -m 'Adding ASM security policy bundle warn enforcement' git push
驗證安裝狀態:
nomos status
叢集應顯示「
SYNCED」狀態和已安裝的政策。
測試政策執行
使用下列指令,在叢集上建立不符合規定的資源:
cat <<EOF | kubectl apply -f -
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: non-compliant-authz-policy
spec:
action: ALLOW
rules:
- to:
- operation:
methods: ["get"]
EOF
准入控制器應產生警告,列出這項資源違反的政策,如以下範例所示:
Warning: [asm-policy-v0.0.1-asm-authz-policy-normalization] in rules-to-operation, methods or notMethods must be uppercase authorizationpolicy.security.istio.io/non-compliant-authz-policy created
移除 Cloud Service Mesh 政策套件
如有需要,您可以從叢集移除 Cloud Service Mesh 政策套件。
kubectl
使用 kubectl 移除政策:
kubectl delete constraint -l policycontroller.gke.io/bundleName=asm-policy-v0.0.1
KPT
移除政策:
kpt live destroy
Config Sync
如果操作人員使用 Config Sync 將政策部署至叢集,可以按照下列操作說明進行:
將變更推送至 Config Sync 存放區:
git rm -r SYNC_ROOT_DIR/policies/asm-policy-v0.0.1 git commit -m 'Removing Cloud Service Mesh policies' git push
驗證狀態:
nomos status
叢集應會顯示
SYNCED狀態,且資源已移除。