使用 Cloud Service Mesh 安全性政策限制

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 補充程式一律注入工作負載 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 輸出 PayloadToHeader 不得包含已知的 HTTP 要求標頭 1.4.1

組合商家檔案

在 Cloud Service Mesh 安全性政策套件中,您可以根據嚴格程度使用兩種設定檔。嚴格程度較低時,套用的限制較少,因此彈性較高。嚴格程度越高,套用的限制就越多,政策控管就越安全。

嚴格程度低

低嚴格程度設定檔有下列政策限制:

  • 標籤 istio:ingressgateway 只能由 Istio Ingress 閘道 Pod 使用。

  • AuthorizationPolicy 中,只有選取標籤為 istio:ingressgateway 的 Istio 輸入閘道,才能使用 hostsnotHosts 欄位。

  • AuthorizationPolicy 中,使用 methodsnotMethods 欄位時,值必須為大寫字母。

  • AuthorizationPolicy 中使用 request.headers 欄位時,值不得包含空白字元。

  • AuthorizationPolicy 中,使用 pathsnotPaths 欄位時,值必須是正規化值。

嚴格程度高

高嚴格程度包含低嚴格程度的所有限制,以及下列限制:

  • 對於所有工作負載 Pod,無法套用註解 sidecar.istio.io/inject: false 來略過 Proxy 插入作業。

  • 系統會強制執行網格層級 AuthorizationPolicy,定義預設拒絕規則。

  • AuthorizationPolicy 必須遵循「ALLOW-with-positive-matching」ALLOW-with-positive-matching或「DENY-with-negative-match」DENY-with-negative-match

  • AuthorizationPolicy 中,使用 hostsnotHosts 欄位時,值必須是 <host-name><host-name>:* 的配對。

  • 網格層級 PeerAuthentication,定義強制執行的嚴格 mTLS

  • 網格中的所有 PeerAuthentication,mTLS 模式只能是 UNSETSTRICT,以遵循嚴格的 mTLS

套裝組合設定

KPT 設定者 說明
嚴格程度 Cloud Service Mesh 組合嚴格程度設定檔,選項為「低」或「高」(預設)

事前準備

  1. 安裝並初始化 Google Cloud CLI,其中提供這些操作說明中使用的 gcloudkubectl 指令。如果您使用 Cloud Shell,Google Cloud CLI 會預先安裝。
  2. 在叢集上安裝 Policy Controller 1.11.2 以上版本,並使用預設的限制範本程式庫。此外,您也必須啟用對參照限制條件的支援,因為這個套件包含參照限制條件。
  3. 確認叢集已安裝 Cloud Service Mesh

設定 Policy Controller 的參照限制

  1. 將下列 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,監控特定類型的物件。

  2. 套用 policycontroller-config.yaml 資訊清單:

    kubectl apply -f policycontroller-config.yaml
    

稽核 Cloud Service Mesh 政策套裝組合

您可以透過 Policy Controller,對 Kubernetes 叢集強制執行政策。如要測試工作負載,以及工作負載是否符合上表列出的 Cloud Service Mesh 安全性政策,您可以「稽核」模式部署這些限制,找出違規事項,更重要的是,在 Kubernetes 叢集強制執行政策前,有機會修正這些事項。

您可以使用 kubectl、kptConfig Sync,將 spec.enforcementAction 設為 dryrun 來套用這些政策。

kubectl

  1. (選用) 使用 kubectl 預覽政策限制:

    kubectl kustomize https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1
    
  2. 使用 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
    
  3. 確認已安裝政策限制,並檢查叢集是否發生違規情形:

    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

  1. 安裝及設定 kpt。這些操作說明會使用 kpt 自訂及部署 Kubernetes 資源。

  2. 使用 kpt 從 GitHub 下載 Cloud Service Mesh 安全性政策套裝組合:

    kpt pkg get https://github.com/GoogleCloudPlatform/gke-policy-library.git/bundles/asm-policy-v0.0.1
    
  3. 執行 set-enforcement-action kpt 函式,將政策的強制執行動作設為 dryrun

    kpt fn eval asm-policy-v0.0.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 \
      -- enforcementAction=dryrun
    
  4. 執行 kpt setter 函式,設定 Cloud Service Mesh 安全性政策的特定欄位

    kpt fn eval asm-policy-v0.0.1 --image gcr.io/kpt-fn/apply-setters:v0.2.0 -- \
    strictness-level="Low"
    
  5. 使用 kpt 初始化工作目錄,這會建立資源來追蹤變更:

    cd asm-policy-v0.0.1
    kpt live init
    
  6. 使用 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
    
  7. 確認已安裝政策限制,並檢查叢集是否發生違規情形:

    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 下載 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
    
  4. 執行 set-enforcement-action kpt 函式,將政策的強制執行動作設為 dryrun

    kpt fn eval policies/asm-policy-v0.0.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=dryrun
    
  5. 執行 kpt setter 函式,設定 Cloud Service Mesh 安全性政策的特定欄位

    kpt fn eval policies/asm-policy-v0.0.1 --image gcr.io/kpt-fn/apply-setters:v0.2.0 -- \
    strictness-level="Low"
    
  6. (選用) 預覽要建立的政策限制:

    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
    
  7. 如果 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
    
  8. 將變更推送至 Config Sync 存放區:

    git add SYNC_ROOT_DIR/policies/asm-policy-v0.0.1
    git commit -m 'Adding ASM security policy audit enforcement'
    git push
    
  9. 驗證安裝狀態:

    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 warndeny 阻止不符規定的資源套用至叢集。

kubectl

  1. 使用 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"}]'
    
  2. 確認政策限制強制執行動作已更新:

    kubectl get constraint -l policycontroller.gke.io/bundleName=asm-policy-v0.0.1
    

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/asm-policy-v0.0.1 -i gcr.io/kpt-fn/set-enforcement-action:v0.1 -- enforcementAction=warn
    
  3. 將變更推送至 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
    
  4. 驗證安裝狀態:

    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 將政策部署至叢集,可以按照下列指示操作:

  1. 將變更推送至 Config Sync 存放區:

    git rm -r SYNC_ROOT_DIR/policies/asm-policy-v0.0.1
    git commit -m 'Removing Cloud Service Mesh  policies'
    git push
    
  2. 確認狀態:

    nomos status
    

    叢集應會顯示 SYNCED 狀態,且資源已移除。