使用 Cloud Service Mesh、Config Sync 和 Policy Controller 強化應用程式的安全性

本教學課程說明如何提升叢集和應用程式的安全狀態。假設您是平台管理員,貴機構使用 Cloud Service Mesh 管理線上商店的應用程式。這套工具可協助您監控及管理可靠的服務網格。您有責任確保網格和應用程式安全無虞。

您可以使用 Policy ControllerConfig Sync,防止設定錯誤並自動驗證 Cloud Service Mesh 政策。Policy Controller 能對叢集強制執行完全程式化的政策。Policy Controller 也隨附預設的限制範本庫,可以與 Cloud Service Mesh 安全性套裝組合搭配使用,檢查您的網格安全漏洞機制與最佳做法是否符合法規。Config Sync 會持續使用一組核心 Kubernetes 宣告式設定檔來協調叢集狀態。同時使用 Policy Controller 和 Config Sync,即可持續對 Cloud Service Mesh 政策設定強制執行限制

下圖概述本教學課程中,Cloud Service Mesh、Policy Controller 和 Config Sync 如何共同運作,管理及保護您在本教學課程中使用的Ingress 閘道Online Boutique 範例應用程式

顯示您為本教學課程建立的架構的圖表

準備環境

在本節中,您將準備環境,以便安裝 Cloud Service Mesh、Policy Controller 和 Config Sync:

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 升級至最新版 Google Cloud CLI:

    gcloud components update
    
  3. 如要儲存您在本教學課程中建立的檔案,請建立目錄:

    mkdir ~/asm-acm-tutorial-dir
    
  4. 為簡化本教學課程的其餘部分,請建立下列環境變數:

    PROJECT_ID=PROJECT_ID
    gcloud config set project $PROJECT_ID
    CLUSTER=asm-acm-tutorial
    CLUSTER_ZONE=us-east4-a
    MEMBERSHIP=asm-acm-tutorial
    PROJECT_NUMBER=$(gcloud projects describe ${PROJECT_ID} --format='get(projectNumber)')
    

    PROJECT_ID 替換為您要在本教學課程中使用的專案 ID。

    如果系統提示您授權 Cloud Shell,請點選「授權」完成作業。

  5. 啟用本教學課程所需的 API:

    gcloud

    gcloud services enable \
        mesh.googleapis.com \
        anthos.googleapis.com
    

    Config Connector

    本教學課程包含 Config Connector 資源。您可以使用這些資源完成與「gcloud」分頁中相同的任務。如要使用這些資源,請安裝 Config Connector,並以最適合您環境的方式套用資源。

    使用下列 Services 資訊清單:

    apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
    kind: Service
    metadata:
      annotations:
        cnrm.cloud.google.com/deletion-policy: "abandon"
        cnrm.cloud.google.com/disable-dependent-services: "false"
      name: mesh.googleapis.com
    spec:
      resourceID: mesh.googleapis.com
      projectRef:
        external: PROJECT_ID
    ---
    apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1
    kind: Service
    metadata:
      annotations:
        cnrm.cloud.google.com/deletion-policy: "abandon"
        cnrm.cloud.google.com/disable-dependent-services: "false"
      name: anthos.googleapis.com
    spec:
      resourceID: anthos.googleapis.com
      projectRef:
        external: PROJECT_ID
    

    這項作業可能需要一分鐘以上才能完成。

  6. 設定 GKE 叢集

    在本節中,您將建立 GKE 叢集,然後將其註冊至機群。「機群」是Google Cloud 按照邏輯整理叢集和其他資源的概念,可讓您使用及管理多叢集功能,並在不同系統中套用一致的政策。

    您在本節中建立的叢集,就是安裝 Cloud Service Mesh、Policy Controller 和 Config Sync 的叢集。您也會在這個叢集中部署 Online Boutique 範例應用程式。

    如要設定叢集,請完成下列步驟:

    1. 建立 GKE 叢集:

      gcloud

      gcloud container clusters create ${CLUSTER} \
          --zone ${CLUSTER_ZONE} \
          --machine-type=e2-standard-4 \
          --num-nodes 4 \
          --workload-pool ${PROJECT_ID}.svc.id.goog \
          --labels mesh_id=proj-${PROJECT_NUMBER}
      

      Config Connector

      使用下列 ContainerClusterContainerNodePool 資訊清單:

      apiVersion: container.cnrm.cloud.google.com/v1beta1
      kind: ContainerNodePool
      metadata:
        annotations:
          cnrm.cloud.google.com/project-id: PROJECT_ID
        name: asm-acm-tutorial
      spec:
        clusterRef:
          name: asm-acm-tutorial
        location: us-east4-a
        nodeConfig:
          machineType: e2-standard-4
        nodeCount: 4
      ---
      apiVersion: container.cnrm.cloud.google.com/v1beta1
      kind: ContainerCluster
      metadata:
        annotations:
          cnrm.cloud.google.com/project-id: PROJECT_ID
          cnrm.cloud.google.com/remove-default-node-pool: "true"
        labels:
          mesh_id: proj-PROJECT_NUMBER
        name: asm-acm-tutorial
      spec:
        location: us-east4-a
        initialNodeCount: 1
        workloadIdentityConfig:
          workloadPool: PROJECT_ID.svc.id.goog
      

      PROJECT_NUMBER 替換為先前擷取的 PROJECT_NUMBER 環境變數值。

      這項作業可能需要五分鐘以上才能完成。

    2. 為確保 GKE 叢集建立成功,請說明叢集狀態:

      gcloud container clusters list \
          --zone ${CLUSTER_ZONE} \
          --project ${PROJECT_ID}
      

      輸出結果會與下列內容相似:

      NAME                LOCATION      MASTER_VERSION   MASTER_IP      MACHINE_TYPE   NODE_VERSION     NUM_NODES  STATUS
      asm-acm-tutorial    us-east4-a    1.23.12-gke.100  35.186.179.30  e2-standard-4  1.23.12-gke.100  3          RUNNING
      
    3. 連線至 GKE 叢集:

      gcloud container clusters get-credentials ${CLUSTER} \
          --zone ${CLUSTER_ZONE} \
          --project ${PROJECT_ID}
      
    4. 將叢集註冊至機群:

      gcloud

      gcloud container fleet memberships register ${MEMBERSHIP} \
          --project ${PROJECT_ID} \
          --gke-cluster ${CLUSTER_ZONE}/${CLUSTER} \
          --enable-workload-identity
      

      輸出結果會與下列內容相似:

      kubeconfig entry generated for asm-acm-tutorial.
      Waiting for membership to be created...done.
      Created a new membership [projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial] for the cluster [asm-acm-tutorial]
      Generating the Connect Agent manifest...
      Deploying the Connect Agent on cluster [asm-acm-tutorial] in namespace [gke-connect]...
      Deployed the Connect Agent on cluster [asm-acm-tutorial] in namespace [gke-connect].
      Finished registering the cluster [asm-acm-tutorial] with the Fleet.
      

      Config Connector

      使用下列 GKEHubMembership 資訊清單:

      apiVersion: gkehub.cnrm.cloud.google.com/v1beta1
      kind: GKEHubMembership
      metadata:
        annotations:
          cnrm.cloud.google.com/project-id: PROJECT_ID
        name: asm-acm-tutorial
      spec:
        location: global
        authority:
          issuer: https://container.googleapis.com/v1/projects/PROJECT_ID/locations/us-east4-a/clusters/asm-acm-tutorial
        endpoint:
          gkeCluster:
            resourceRef:
              name: asm-acm-tutorial
      
    5. 如要確保 GKE 叢集註冊成功,請說明其狀態:

      gcloud container fleet memberships list
      

      輸出結果會與下列內容相似:

      NAME              EXTERNAL_ID                           LOCATION
      asm-acm-tutorial  0e12258c-8831-4d81-b5c0-5e7099a468cc  global
      

    探索存放區

    在下方的安裝部分,您會套用資訊清單 acm-config.yaml 檔案。這個資訊清單會將叢集設定為從範例存放區的 asm-acm-tutorial 資料夾同步。這個資料夾包含完成本教學課程其餘部分所需的所有設定檔。

    為簡化本教學課程,您會使用 sed 指令更新 acm-config.yaml。有了 acm-config.yaml 檔案,Config Sync 就能部署本教學課程每個步驟所需的資訊清單。更新單一檔案可協助您專注於保護叢集、網格和應用程式的概念和流程,不必重複操作檔案和執行 git 指令。

    如要使用 Config Sync 的同步處理多個存放區功能,請使用下列資源:

    • root-sync 做為 RootSync 存放區,包含叢集中的所有設定,包括 RepoSyncsConstraintsClusterRoleRoleBindings,以及某些系統命名空間 (例如 istio-system) 中包含的資源。
    • ingress-gateway,其中包含部署 Ingress 閘道所需的所有資源,並在整個教學課程中逐步確保安全。RepoSync
    • online-boutique,做為第二個 RepoSync,包含部署 Online Boutique 應用程式所需的所有資源,並在本教學課程中逐步確保這些資源的安全。

    安裝 Policy Controller、Config Sync 和代管型 Cloud Service Mesh

    建立及註冊叢集後,您可以在叢集上安裝 Config Sync、Policy Controller 和 Cloud Service Mesh,並將叢集設定為從預設 RootSync 的設定檔同步:

    1. 啟用 ConfigManagement 運算子,管理 Config Sync 和 Policy Controller:

      gcloud

      gcloud beta container fleet config-management enable
      

      Config Connector

      使用下列 GKEHubFeature 資訊清單:

      apiVersion: gkehub.cnrm.cloud.google.com/v1beta1
      kind: GKEHubFeature
      metadata:
        name: configmanagement
      spec:
        projectRef:
          external: PROJECT_ID
        location: global
        resourceID: configmanagement
      
    2. 在機群中啟用 Cloud Service Mesh。

      gcloud

      gcloud container fleet mesh enable
      

      Config Connector

      使用下列 GKEHubFeature 資訊清單:

      apiVersion: gkehub.cnrm.cloud.google.com/v1beta1
      kind: GKEHubFeature
      metadata:
        name: servicemesh
      spec:
        projectRef:
          external: PROJECT_ID
        location: global
        resourceID: servicemesh
      
    3. 啟用 Cloud Service Mesh 自動管理功能,讓 Google 採用代管型 Cloud Service Mesh 的建議設定:

      gcloud

      gcloud container fleet mesh update \
          --management automatic \
          --memberships ${MEMBERSHIP}
      

      Config Connector

      使用下列 GKEHubFeatureMembership 資訊清單:

      apiVersion: gkehub.cnrm.cloud.google.com/v1beta1
      kind: GKEHubFeatureMembership
      metadata:
        name: servicemesh-membership
      spec:
        projectRef:
          external: PROJECT_ID
        location: global
        membershipRef:
          name: asm-acm-tutorial
        featureRef:
          name: servicemesh
        mesh:
          management: MANAGEMENT_AUTOMATIC
      
    4. 啟用 Config Sync 和 Policy Controller:

      gcloud

      將下列資訊清單儲存為 ~/asm-acm-tutorial-dir 目錄中的 acm-config.yaml

      applySpecVersion: 1
      spec:
        configSync:
          enabled: true
          policyDir: asm-acm-tutorial/root-sync/init
          secretType: none
          sourceFormat: unstructured
          syncRepo: https://github.com/GoogleCloudPlatform/anthos-config-management-samples
          syncBranch: main
        policyController:
          enabled: true
          referentialRulesEnabled: true
          templateLibraryInstalled: true
      

      如要進一步瞭解 Google Cloud CLI 設定欄位,請參閱「gcloud 套用規格欄位」。

      套用檔案:

      gcloud beta container fleet config-management apply \
          --membership ${MEMBERSHIP} \
          --config ~/asm-acm-tutorial-dir/acm-config.yaml
      

      Config Connector

      使用下列 GKEHubFeatureMembership 資訊清單:

      apiVersion: gkehub.cnrm.cloud.google.com/v1beta1
      kind: GKEHubFeatureMembership
      metadata:
        name: configmanagement-membership
      spec:
        projectRef:
          external: PROJECT_ID
        location: global
        membershipRef:
          name: asm-acm-tutorial
        featureRef:
          name: configmanagement
        configmanagement:
          configSync:
            sourceFormat: unstructured
            git:
              policyDir: asm-acm-tutorial/root-sync/init
              secretType: none
              syncBranch: main
              syncRepo: https://github.com/GoogleCloudPlatform/anthos-config-management-samples
          policyController:
            enabled: true
            referentialRulesEnabled: true
            templateLibraryInstalled: true
      

      叢集上已安裝 Policy Controller 和 Config Sync。接著,Config Sync 會開始將預設 RootSync 的所有設定同步至叢集。這些設定檔會安裝及設定下列主要元件:

      • 設定 Online Boutique 應用程式和 Ingress 閘道的 RepoSync 物件會同步處理:

        apiVersion: configsync.gke.io/v1beta1
        kind: RepoSync
        metadata:
          name: repo-sync
        spec:
          override:
            enableShellInRendering: true
          sourceFormat: unstructured
          git:
            repo: https://github.com/GoogleCloudPlatform/anthos-config-management-samples
            revision: HEAD
            branch: main
            dir: asm-acm-tutorial/online-boutique/init
            auth: none
        apiVersion: configsync.gke.io/v1beta1
        kind: RepoSync
        metadata:
          name: repo-sync
        spec:
          override:
            enableShellInRendering: true
          sourceFormat: unstructured
          git:
            repo: https://github.com/GoogleCloudPlatform/anthos-config-management-samples
            revision: HEAD
            branch: main
            dir: asm-acm-tutorial/ingress-gateway/init
            auth: none
      • 由於 RepoSync 調解器需要額外權限才能建立 Istio 資源,因此系統也會將 ClusterRole 和兩個 RoleBinding 物件套用至叢集,以授予這些權限:

        apiVersion: rbac.authorization.k8s.io/v1
        kind: ClusterRole
        metadata:
          labels:
            rbac.authorization.k8s.io/aggregate-to-edit: "true"
          name: custom:aggregate-to-edit:istio
        rules:
        - apiGroups:
          - "networking.istio.io"
          - "security.istio.io"
          resources:
          - "virtualservices"
          - "authorizationpolicies"
          - "gateways"
          verbs:
          - "*"
        apiVersion: rbac.authorization.k8s.io/v1
        kind: RoleBinding
        metadata:
          name: repo-sync
        subjects:
        - kind: ServiceAccount
          name: ns-reconciler-onlineboutique
          namespace: config-management-system
        roleRef:
          kind: ClusterRole
          name: edit
          apiGroup: rbac.authorization.k8s.io
        apiVersion: rbac.authorization.k8s.io/v1
        kind: RoleBinding
        metadata:
          name: repo-sync
        subjects:
        - kind: ServiceAccount
          name: ns-reconciler-asm-ingress
          namespace: config-management-system
        roleRef:
          kind: ClusterRole
          name: edit
          apiGroup: rbac.authorization.k8s.io
    5. 為確保 Policy Controller 和 Config Sync 安裝成功,請檢查狀態:

      gcloud beta container fleet config-management status
      

      輸出結果會與下列內容相似:

      Name: asm-acm-tutorial
      Status: SYNCED
      Last_Synced_Token: 4b3384d
      Sync_Branch: main
      Last_Synced_Time: 2022-05-04T21:32:58Z
      Policy_Controller: INSTALLED
      

      如果 StatusPolicy_Controller 列中顯示 PENDINGNOT_INSTALLED,請稍候幾分鐘,然後再次執行 gcloud beta container fleet config-management status

    6. 為確保順利安裝 Cloud Service Mesh,請說明其狀態:

      gcloud container fleet mesh describe
      

      輸出結果會與下列內容相似:

      createTime: '2022-09-13T23:12:56.477042921Z'
      membershipSpecs:
        projects/PROJECT_NUMBER/locations/global/memberships/asm-acm-tutorial:
          mesh:
            management: MANAGEMENT_AUTOMATIC
      membershipStates:
        projects/PROJECT_NUMBER/locations/global/memberships/asm-acm-tutorial:
          servicemesh:
            controlPlaneManagement:
              details:
              - code: REVISION_READY
                details: 'Ready: asm-managed'
              state: ACTIVE
            dataPlaneManagement:
              details:
              - code: OK
                details: Service is running.
              state: ACTIVE
          state:
            code: OK
            description: |-
              Revision(s) ready for use: asm-managed.
              All Canonical Services have been reconciled successfully.
            updateTime: '2022-09-14T00:19:10.571552206Z'
      name: projects/PROJECT_ID/locations/global/features/servicemesh
      resourceState:
        state: ACTIVE
      spec: {}
      state:
        state: {}
      updateTime: '2022-09-14T00:19:14.135113118Z'
      

      如果看到 state.code: ERROR 而不是 state.code: OK,請稍候幾分鐘,然後再次執行 gcloud container fleet mesh describe。繼續進行本教學課程之前,請先確認 servicemesh.controlPlaneManagement.details.code 欄位是否具有 REVISION_READY 值。

    部署 Ingress 閘道和範例應用程式

    在本節中,您將部署 Online Boutique 範例應用程式輸入閘道,以管理輸入流量。

    1. 部署 Online Boutique 範例應用程式和 Ingress 閘道。

      下列指令會使用 sed 更新 acm-config.yaml 資訊清單,讓 Config Sync 部署您需要的資源,以部署 Ingress 閘道和範例應用程式。

      sed -i "s,root-sync/init,root-sync/deployments,g" ~/asm-acm-tutorial-dir/acm-config.yaml
      gcloud beta container fleet config-management apply \
          --membership ${MEMBERSHIP} \
          --config ~/asm-acm-tutorial-dir/acm-config.yaml
      

      請注意,這個步驟可能需要幾分鐘才能完成。

    2. 查看 RootSync 和兩個 RepoSyncs 的 Config Sync 狀態:

      gcloud alpha anthos config sync repo describe
      

      輸出內容類似如下:

      getting 3 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial
      [
        {
          "clusters": [
            "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial"
          ],
          "commit": "95a30c052566357afb9db3d7f6153d9c0f219c03",
          "errors": [],
          "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/deployments@main",
          "status": "SYNCED"
        },
        {
          "clusters": [
            "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial"
          ],
          "commit": "95a30c052566357afb9db3d7f6153d9c0f219c03",
          "errors": [],
          "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/ingress-gateway/deployments@main",
          "status": "SYNCED"
        },
        {
          "clusters": [
            "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial"
          ],
          "commit": "95a30c052566357afb9db3d7f6153d9c0f219c03",
          "errors": [],
          "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/online-boutique/deployments@main",
          "status": "SYNCED"
        }
      ]
      

      如果看到 status: RECONCILING 而不是 status: SYNCED,請稍候幾分鐘,然後再次執行 gcloud alpha anthos config sync repo describe

      如要只查看一個存放區的資訊,可以使用 --sync-name--sync-namespace 旗標。如要查看受管理資源的詳細資料,請新增 --managed-resources 旗標。詳情請參閱「查看多個叢集的 Config Sync 狀態」。

    3. 等待系統佈建 Ingress Gateway 的公開 IP 位址:

      until kubectl -n asm-ingress get svc asm-ingressgateway -o jsonpath='{.status.loadBalancer}' | grep "ingress"; do : ; done
      
    4. 取得 Ingress Gateway 的公開 IP 位址:

      EXTERNAL_IP=$(kubectl get svc asm-ingressgateway -n asm-ingress -o jsonpath="{.status.loadBalancer.ingress[*].ip}")
      
    5. 在瀏覽器中前往 IP 位址,確認 Online Boutique 應用程式已成功部署:

      echo http://${EXTERNAL_IP}
      

    強制執行政策以保護網格

    在下列各節中,您會利用 Policy Controller 建立限制,強制執行Cloud Service Mesh 政策套裝組合中的政策。

    強制插入補充 Proxy

    在本節中,您將強制執行政策,確保網格中的所有工作負載都已啟用自動 Sidecar 插入

    1. 如要強制插入補充 Proxy,請套用限制。

      下列指令會使用 sed 更新 acm-config.yaml 資訊清單,讓 Config Sync 部署相關聯的資源。

      sed -i "s,root-sync/deployments,root-sync/enforce-sidecar-injection,g" ~/asm-acm-tutorial-dir/acm-config.yaml
      gcloud beta container fleet config-management apply \
          --membership ${MEMBERSHIP} \
          --config ~/asm-acm-tutorial-dir/acm-config.yaml
      

      上述指令會部署下列資源:

      • K8sRequiredLabels Constraint,要求網格中的任何 Namespace 都必須包含特定Cloud Service Mesh 補充 Proxy 插入標籤

        apiVersion: constraints.gatekeeper.sh/v1beta1
        kind: K8sRequiredLabels
        metadata:
          name: namespace-sidecar-injection-label
        spec:
          enforcementAction: deny
          match:
            kinds:
            - apiGroups:
              - ""
              kinds:
              - Namespace
            excludedNamespaces:
            - config-management-monitoring
            - config-management-system
            - default
            - gatekeeper-system
            - gke-connect
            - istio-system
            - kube-node-lease
            - kube-public
            - kube-system
            - resource-group-system
          parameters:
            labels:
            - allowedRegex: enabled
              key: istio-injection
      • AsmSidecarInjection Constraint,禁止網格中的任何 Pod 略過 Istio Proxy 補充容器插入作業:

        apiVersion: constraints.gatekeeper.sh/v1beta1
        kind: AsmSidecarInjection
        metadata:
          name: pod-sidecar-injection-annotation
        spec:
          enforcementAction: deny
          match:
            kinds:
            - apiGroups:
              - ""
              kinds:
              - Pod
            excludedNamespaces:
            - kube-system
          parameters:
            strictnessLevel: High
    2. 查看 RootSync 的 Config Sync 狀態:

      gcloud alpha anthos config sync repo describe \
          --sync-name root-sync \
          --sync-namespace config-management-system
      

      輸出內容類似如下:

      getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial
      [
        {
          "clusters": [
            "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial"
          ],
          "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884",
          "errors": [],
          "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/enforce-sidecar-injection@main",
          "status": "SYNCED"
        }
      ]
      

      如果看到 status: RECONCILING 而不是 status: SYNCED,請稍候幾分鐘,然後再次執行 gcloud alpha anthos config sync repo describe

    3. 確認 Constraints 是否已建立:

      kubectl get constraints
      

      Policy Controller 可能需要幾分鐘才能評估這些限制。如果 TOTAL-VIOLATIONS 欄中沒有顯示值,請稍候並再次執行 kubectl get constraints

      輸出內容類似如下:

      NAME                                                                                       ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      podsidecarinjectionannotation.constraints.gatekeeper.sh/pod-sidecar-injection-annotation   deny                 0
      
      NAME                                                                            ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      k8srequiredlabels.constraints.gatekeeper.sh/namespace-sidecar-injection-label   deny                 0
      

      由於我們已正確設定 NamespacesPods,因此這些 Constraints0TOTAL-VIOLATIONSConstraints

    4. 如要查看這些 Constraints 的運作情形,請嘗試在叢集中建立 Namespace,但不要使用 labelannotation

      kubectl create namespace test
      

      輸出內容會與下列錯誤類似:

      Error from server (Forbidden): admission webhook "validation.gatekeeper.sh" denied the request: [namespace-sidecar-injection-label] you must provide labels: {"istio-injection"}
      

    強制執行流量加密

    在本節中,您將強制執行政策,確保網格中的所有流量都經過加密

    1. 如要強制執行流量加密,請套用限制。

      下列指令會使用 sed 更新 acm-config.yaml 資訊清單,讓 Config Sync 部署相關聯的資源。

      sed -i "s,root-sync/enforce-sidecar-injection,root-sync/enforce-strict-mtls,g" ~/asm-acm-tutorial-dir/acm-config.yaml
      gcloud beta container fleet config-management apply \
          --membership ${MEMBERSHIP} \
          --config ~/asm-acm-tutorial-dir/acm-config.yaml
      

      上述指令會部署下列資源:

      • AsmPeerAuthnMeshStrictMtls Constraint,用於在 PeerAuthentication 命名空間中強制執行網格層級的 mTLS istio-system

        apiVersion: constraints.gatekeeper.sh/v1beta1
        kind: AsmPeerAuthnMeshStrictMtls
        metadata:
          name: mesh-level-strict-mtls
        spec:
          enforcementAction: deny
          parameters:
            rootNamespace: istio-system
            strictnessLevel: High
      • 參照限制gatekeeper-system 命名空間中的 Config。這項參照限制可讓 AsmPeerAuthnMeshStrictMtls Constraint 參照定義中的另一個物件 (例如在 istio-system Namespace 中搜尋任何 PeerAuthentication):

        apiVersion: config.gatekeeper.sh/v1alpha1
        kind: Config
        metadata:
          name: config
        spec:
          sync:
            syncOnly:
              - group: ""
                version: "v1"
                kind: "Namespace"
              - group: "security.istio.io"
                version: "v1beta1"
                kind: "PeerAuthentication"
              - group: "security.istio.io"
                version: "v1beta1"
                kind: "AuthorizationPolicy"
      • DestinationRuleTLSEnabled Constraint,禁止在 Istio DestinationRules 中停用所有主機和主機子集的 TLS:

        apiVersion: constraints.gatekeeper.sh/v1beta1
        kind: DestinationRuleTLSEnabled
        metadata:
          name: destination-rule-tls-enabled
        spec:
          enforcementAction: deny
          match:
            kinds:
            - apiGroups:
              - networking.istio.io
              kinds:
              - DestinationRule
      • AsmPeerAuthnStrictMtls Constraint,強制所有 PeerAuthentications 不得覆寫 mTLS:STRICT

        apiVersion: constraints.gatekeeper.sh/v1beta1
        kind: AsmPeerAuthnStrictMtls
        metadata:
          name: peerauthentication-strict-mtls
        spec:
          enforcementAction: deny
          match:
            kinds:
            - apiGroups:
              - security.istio.io
              kinds:
              - PeerAuthentication
          parameters:
            strictnessLevel: High
    2. 查看 RootSync 的 Config Sync 狀態:

      gcloud alpha anthos config sync repo describe \
          --sync-name root-sync \
          --sync-namespace config-management-system
      

      輸出內容類似如下:

      getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial
      [
        {
          "clusters": [
            "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial"
          ],
          "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884",
          "errors": [],
          "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/enforce-strict-mtls@main",
          "status": "SYNCED"
        }
      ]
      

      如果看到 status: RECONCILING 而不是 status: SYNCED,請稍候幾分鐘,然後再次執行 gcloud alpha anthos config sync repo describe

    3. 執行下列指令,進一步瞭解 PeerAuthentication 違規情形:

      kubectl get asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/mesh-level-strict-mtls -ojsonpath='{.status.violations}'  | jq
      

      輸出內容類似如下:

      [
        {
          "enforcementAction": "deny",
          "group": "constraints.gatekeeper.sh",
          "kind": "AsmPeerAuthnMeshStrictMtls",
          "message": "Root namespace <istio-system> does not have a strict mTLS PeerAuthentication",
          "name": "mesh-level-strict-mtls",
          "version": "v1beta1"
        }
      ]
      
    4. 如要修正問題,請在 istio-system 中部署 PeerAuthentication。如要禁止網格中的所有服務接受純文字流量,請設定網格範圍的 PeerAuthentication 政策,並將 mTLS 模式設為 STRICT。部署政策後,控制層會自動佈建 TLS 憑證,讓工作負載彼此驗證。

      下列指令會使用 sed 更新 acm-config.yaml 資訊清單,讓 Config Sync 部署相關聯的資源。

      sed -i "s,root-sync/enforce-strict-mtls,root-sync/fix-strict-mtls,g" ~/asm-acm-tutorial-dir/acm-config.yaml
      gcloud beta container fleet config-management apply \
          --membership ${MEMBERSHIP} \
          --config ~/asm-acm-tutorial-dir/acm-config.yaml
      

      上述指令會在 istio-system 命名空間中部署下列 STRICT mTLS PeerAuthentication。這會將 mTLS STRICT 套用至整個網格:

      apiVersion: security.istio.io/v1beta1
      kind: PeerAuthentication
      metadata:
        name: default
      spec:
        mtls:
          mode: STRICT
    5. 查看 RootSync 的 Config Sync 狀態:

      gcloud alpha anthos config sync repo describe \
          --sync-name root-sync \
          --sync-namespace config-management-system
      

      輸出內容類似如下:

      getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial
      [
        {
          "clusters": [
            "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial"
          ],
          "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884",
          "errors": [],
          "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/fix-strict-mtls@main",
          "status": "SYNCED"
        }
      ]
      

      如果看到 status: RECONCILING 而不是 status: SYNCED,請稍候幾分鐘,然後再次執行 gcloud alpha anthos config sync repo describe

    6. 確認 Constraints 是否已建立:

      kubectl get constraints
      

      請注意,這可能需要幾分鐘的時間,才能讓 Policy Controller 評估這些 Constraints。稍後請再次執行 kubectl get constraints 指令,直到每行都顯示 TOTAL-VIOLATIONS 欄的值。

      輸出內容類似如下:

      NAME                                                                            ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      k8srequiredlabels.constraints.gatekeeper.sh/namespace-sidecar-injection-label   deny                 0
      NAME                                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/mesh-level-strict-mtls   deny                 0
      NAME                                                                               ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      destinationruletlsenabled.constraints.gatekeeper.sh/destination-rule-tls-enabled   deny                 0
      NAME                                                                              ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      asmpeerauthnstrictmtls.constraints.gatekeeper.sh/peerauthentication-strict-mtls   deny                 0
      NAME                                                                             ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      asmsidecarinjection.constraints.gatekeeper.sh/pod-sidecar-injection-annotation   deny                 0
      

    強制執行精細的存取權控管機制

    在本節中,您會強制執行政策,確保網格中的所有工作負載都具有精細的存取權控管機制

    1. 如要強制執行精細的存取權控管,請套用限制。

      下列指令會使用 sed 更新 acm-config.yaml 資訊清單,讓 Config Sync 部署相關聯的資源。

      sed -i "s,root-sync/fix-strict-mtls,root-sync/enforce-authorization-policies,g" ~/asm-acm-tutorial-dir/acm-config.yaml
      gcloud beta container fleet config-management apply \
          --membership ${MEMBERSHIP} \
          --config ~/asm-acm-tutorial-dir/acm-config.yaml
      

      上述指令會部署下列資源:

      • AsmAuthzPolicyDefaultDeny Constraint,會在 istio-system 命名空間中強制執行網格層級的預設拒絕 AuthorizationPolicy

        apiVersion: constraints.gatekeeper.sh/v1beta1
        kind: AsmAuthzPolicyDefaultDeny
        metadata:
          name: default-deny-authorization-policies
        spec:
          enforcementAction: deny
          parameters:
            rootNamespace: istio-system
            strictnessLevel: High
      • AsmAuthzPolicyEnforceSourcePrincipals Constraint ,可強制規定任何 AuthorizationPolicies 都會定義細微的來源主體 (「*」除外)。只有 asm-ingress 名稱空間中的 Ingress 閘道是這項規則的例外狀況,可接收來自使用者的流量,並將流量重新導向至 Online Boutique 的 frontend 應用程式。

        apiVersion: constraints.gatekeeper.sh/v1beta1
        kind: AsmAuthzPolicyEnforceSourcePrincipals
        metadata:
          name: authz-source-principals-not-all
        spec:
          enforcementAction: deny
          match:
            kinds:
            - apiGroups:
              - security.istio.io
              kinds:
              - AuthorizationPolicy
            excludedNamespaces:
              - asm-ingress
    2. 查看 RootSync 的 Config Sync 狀態:

      gcloud alpha anthos config sync repo describe \
          --sync-name root-sync \
          --sync-namespace config-management-system
      

      輸出內容類似如下:

      getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial
      [
        {
          "clusters": [
            "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial"
          ],
          "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884",
          "errors": [],
          "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/enforce-authorization-policies@main",
          "status": "SYNCED"
        }
      ]
      

      如果看到 status: RECONCILING 而不是 status: SYNCED,請稍候幾分鐘,然後再次執行 gcloud alpha anthos config sync repo describe

    3. 執行下列指令,進一步瞭解相關違規事項:

      kubectl get asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/default-deny-authorization-policies -ojsonpath='{.status.violations}'  | jq
      

      輸出內容類似如下:

      [
        {
          "enforcementAction": "deny",
          "group": "constraints.gatekeeper.sh",
          "kind": "AsmAuthzPolicyDefaultDeny",
          "message": "Root namespace <istio-system> does not have a default deny AuthorizationPolicy",
          "name": "default-deny-authorization-policies",
          "version": "v1beta1"
        }
      ]
      
    4. 如要修正問題,請在 istio-system 命名空間中部署 AuthorizationPolicy

      下列指令會使用 sed 更新 acm-config.yaml 資訊清單,讓 Config Sync 部署相關聯的資源。

      sed -i "s,root-sync/enforce-authorization-policies,root-sync/fix-default-deny-authorization-policy,g" ~/asm-acm-tutorial-dir/acm-config.yaml
      gcloud beta container fleet config-management apply \
          --membership ${MEMBERSHIP} \
          --config ~/asm-acm-tutorial-dir/acm-config.yaml
      

      上述指令會在 istio-system 命名空間中部署下列「全部拒絕」AuthorizationPolicy

      apiVersion: security.istio.io/v1beta1
      kind: AuthorizationPolicy
      metadata:
        name: deny-all
      spec:
        {}
    5. 查看 RootSync 的 Config Sync 狀態:

      gcloud alpha anthos config sync repo describe \
          --sync-name root-sync \
          --sync-namespace config-management-system
      

      輸出內容類似如下:

      getting 1 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial
      [
        {
          "clusters": [
            "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial"
          ],
          "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884",
          "errors": [],
          "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/fix-default-deny-authorization-policy@main",
          "status": "SYNCED"
        }
      ]
      

      如果看到 status: RECONCILING 而不是 status: SYNCED,請稍候幾分鐘,然後再次執行 gcloud alpha anthos config sync repo describe

    6. 確認 Constraints 是否已建立:

      kubectl get constraints
      

      請注意,這可能需要幾分鐘的時間,才能讓 Policy Controller 評估這些 Constraints。稍後請再次執行 kubectl get constraints 指令,直到每行都顯示 TOTAL-VIOLATIONS 欄的值。

      輸出內容類似如下:

      NAME                                                                             ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      asmsidecarinjection.constraints.gatekeeper.sh/pod-sidecar-injection-annotation   deny                 0
      NAME                                                                            ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      k8srequiredlabels.constraints.gatekeeper.sh/namespace-sidecar-injection-label   deny                 0
      NAME                                                                                      ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      asmauthzpolicydefaultdeny.constraints.gatekeeper.sh/default-deny-authorization-policies   deny                 0
      NAME                                                                          ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      asmpeerauthnmeshstrictmtls.constraints.gatekeeper.sh/mesh-level-strict-mtls   deny                 0
      NAME                                                                               ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      destinationruletlsenabled.constraints.gatekeeper.sh/destination-rule-tls-enabled   deny                 0
      NAME                                                                              ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      asmpeerauthnstrictmtls.constraints.gatekeeper.sh/peerauthentication-strict-mtls   deny                 0
      NAME                                                                                              ENFORCEMENT-ACTION   TOTAL-VIOLATIONS
      asmauthzpolicyenforcesourceprincipals.constraints.gatekeeper.sh/authz-source-principals-not-all   deny                 0
      
    7. 在瀏覽器中前往 Online Boutique 應用程式:

      echo http://${EXTERNAL_IP}
      

      您應該會收到錯誤訊息:RBAC: access denied,確認預設拒絕 AuthorizationPolicy已套用至整個網格。

    8. 如要修正這個問題,請在 asm-ingressonlineboutique 命名空間中部署更精細的 AuthorizationPolicies

      下列指令會使用 sed 更新 acm-config.yaml 資訊清單,讓 Config Sync 部署相關聯的資源。

      sed -i "s,root-sync/fix-default-deny-authorization-policy,root-sync/deploy-authorization-policies,g" ~/asm-acm-tutorial-dir/acm-config.yaml
      gcloud beta container fleet config-management apply \
          --membership ${MEMBERSHIP} \
          --config ~/asm-acm-tutorial-dir/acm-config.yaml
      

      上述指令會部署下列資源:

      • 命名空間中的 :AuthorizationPolicyasm-ingress

        apiVersion: security.istio.io/v1beta1
        kind: AuthorizationPolicy
        metadata:
          name: asm-ingressgateway
        spec:
          selector:
            matchLabels:
              asm: ingressgateway
          rules:
          - to:
            - operation:
                ports:
                - "8080"
      • onlineboutique 命名空間中的每個應用程式都有一個 AuthorizationPolicy,以下是 cartservice 應用程式的範例:

        apiVersion: security.istio.io/v1beta1
        kind: AuthorizationPolicy
        metadata:
          name: cartservice
        spec:
          selector:
            matchLabels:
              app: cartservice
          rules:
          - from:
            - source:
                principals:
                - cluster.local/ns/onlineboutique/sa/frontend
                - cluster.local/ns/onlineboutique/sa/checkoutservice
            to:
            - operation:
                paths:
                - /hipstershop.CartService/AddItem
                - /hipstershop.CartService/GetCart
                - /hipstershop.CartService/EmptyCart
                methods:
                - POST
                ports:
                - "7070"
      • 每個應用程式在 asm-ingressonlineboutique 命名空間中都有一個 ServiceAccount,因此每個應用程式在 AuthorizationPolicies 中評估為 principal 時,都有專屬身分。ServiceAccount以下是 cartservice 應用程式的範例:

        apiVersion: v1
        kind: ServiceAccount
        metadata:
          name: cartservice
    9. 查看 RootSync 和兩個 RepoSyncs 的 Config Sync 狀態:

      gcloud alpha anthos config sync repo describe
      

      輸出內容類似如下:

      getting 3 RepoSync and RootSync from projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial
      [
        {
          "clusters": [
            "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial"
          ],
          "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884",
          "errors": [],
          "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/root-sync/deploy-authorization-policies@main",
          "status": "SYNCED"
        },
        {
          "clusters": [
            "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial"
          ],
          "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884",
          "errors": [],
          "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/ingress-gateway/authorization-policies@main",
          "status": "SYNCED"
        },
        {
          "clusters": [
            "projects/PROJECT_ID/locations/global/memberships/asm-acm-tutorial"
          ],
          "commit": "7d15d49af13c44aa531a4565b2277ddcf8b81884",
          "errors": [],
          "source": "https://github.com/GoogleCloudPlatform/anthos-config-management-samples//asm-acm-tutorial/online-boutique/authorization-policies@main",
          "status": "SYNCED"
        }
      ]
      

      如果看到 status: RECONCILING 而不是 status: SYNCED,請稍候幾分鐘,然後再次執行 gcloud alpha anthos config sync repo describe

      如要只查看一個存放區的資訊,可以使用 --sync-name--sync-namespace 旗標。如要詳細查看受管理資源,可以新增 --managed-resources 旗標。詳情請參閱「查看多個叢集的 Config Sync 狀態」。

    10. 透過瀏覽器再次造訪 Online Boutique 應用程式:

      echo http://${EXTERNAL_IP}
      

      稍候幾分鐘後,網站應該就能正常運作。

    查看 GKE Enterprise 安全性功能的狀態

    您可以在 Google Cloud 控制台中查看 GKE Enterprise 安全性功能的狀態,包括驗證和授權政策。

    1. 前往 Google Cloud 控制台的「GKE Enterprise Security」頁面。

      前往 GKE Enterprise 安全性

      「政策摘要」會顯示應用程式安全防護狀態,包括服務存取權控管 (AuthorizationPolicies) 和 mTLS。

    2. 按一下「政策稽核」,即可查看叢集和兩個命名空間 (asm-ingressonlineboutique) 的工作負載政策狀態。

      「服務存取權控管」和「雙向傳輸層安全標準狀態」資訊卡提供高階總覽。

      服務存取權控管和雙向傳輸層安全標準狀態的高層級總覽

      「工作負載」清單會顯示每個工作負載的服務存取權控管和雙向傳輸層安全標準狀態。

      每個工作負載的詳細清單,以及服務存取權控管和 mTLS 狀態

    您現在已透過 Policy Controller 和 Config Sync 保護叢集和網格。