在 AKS 和 EKS 上,使用 Sidecar 驗證進行 Workload Identity 聯盟

Workload Identity Federation 可讓 Apigee 工作負載取得私下來源的聯盟憑證,並換取可存取 Apigee API 的 Google 權杖。本頁面說明如何為 Apigee Hybrid 設定 Workload Identity Federation,以便使用邊車容器取得這些私有聯合憑證。如「在 AKS 和 EKS 上啟用 Workload Identity Federation」一文所述,這個方法是使用 Kubernetes Projected 服務帳戶權杖的替代方案。如需使用外部或自訂的識別資訊提供者 (IdP) 擷取驗證權杖,請使用 Sidecar 方法。

使用這個方法時,您可以在非 GKE 平台上,搭配 Workload Identity Federation 使用邊車容器,從偏好的 IdP 掛接安全權杖,以進行服務帳戶驗證。

事前準備

完成這項程序前,請務必先完成下列工作:

  • 為 Google Cloud 專案設定 Workload Identity 聯盟,包括建立身分識別集區和提供者,以及授予 Kubernetes 服務帳戶 IAM 權限。詳情請參閱「在 AKS 和 EKS 上啟用 Workload Identity Federation」。
  • 建構及推送自訂邊車容器映像檔,從身分識別提供者 (IdP) 擷取憑證,並將安全權杖寫入共用磁碟區 (例如 /shared/token)。

設定 Apigee Hybrid

如要使用 Sidecar 驗證啟用 Workload Identity Federation,請在 overrides.yaml 檔案中設定 Sidecar。您必須指定邊車容器,以及在邊車與 Apigee hybrid 元件之間共用資料所需的任何磁碟區。Apigee 會將邊車容器新增為 Kubernetes 原生 Sidecar (含 restartPolicy: Always 的 init 容器),因此您也必須指定 startupProbe。Kubernetes 會等待這項探測作業成功,再啟動 Apigee 元件容器。舉例來說,您可以設定探查器,在共用權杖檔案上執行 cat,確認權杖已寫入。如果您使用保管箱,必須新增必要註解,允許 Sidecar 從 IdP 擷取權杖。

您只需要為所有元件設定 overrides.yaml 檔案中的 gcp.federatedWorkloadIdentity.sidecarSpec 區段。套用設定後,Helm 資訊圖表會將每個 APIGEE_COMPONENT 執行個體替換為對應的元件名稱。元件名稱為 apigee-cassandra、apigee-guardrails、apigee-logger、apigee-mart、apigee-mcp-server、apigee-metrics、apigee-mint-task-scheduler、apigee-runtime、apigee-synchronizer 和 apigee-watcher。

在 overrides.yaml 檔案中新增下列設定。選取符合您用途的範例 (Kubernetes Secret 或檔案,或是 Vault 代理程式注入):

密鑰或檔案

gcp:
  federatedWorkloadIdentity:
    enabled: true
    type: "sidecar"
    sidecarSpec:
      volumes:
      - name: source-creds
        secret:
          secretName: apigee-hybrid-APIGEE_COMPONENT
      container:
        env: 
        - name: "apigee-component"
          value: APIGEE_COMPONENT
        image: CUSTOM_SIDECAR_IMAGE
        startupProbe:
          exec:
            command:
            - cat
            - /shared/token
        volumeMounts:
        - name: shared-mount
          mountPath: /shared
        - name: source-creds
          mountPath: /source-creds
          readOnly: true

保管箱代理程式注入

gcp:
  federatedWorkloadIdentity:
    enabled: true
    type: "sidecar"
    sidecarSpec:
      annotations:
        vault.hashicorp.com/agent-inject: "true"
        vault.hashicorp.com/agent-pre-populate-only: "false"
        vault.hashicorp.com/log-level: "trace"
        vault.hashicorp.com/ca-cert: "VAULT_CA_CERT_PATH"
        vault.hashicorp.com/namespace: "VAULT_NAMESPACE"
        vault.hashicorp.com/agent-inject-secret-source-token: "VAULT_TOKEN" # For example: '/path/to/secret' or '/path/to/APIGEE_COMPONENT/secret' if the secret is per-component.
        vault.hashicorp.com/secret-volume-path: "VAULT_SECRET_VOLUME_PATH" # For example: '/source-creds'
        vault.hashicorp.com/service: "VAULT_SERVICE_URL:8200" # For example: '127.0.0.1:8200' or 'https://vault.example.com:8200'
      volumes:
      - name: source-creds
        secret:
          secretName: apigee-hybrid-APIGEE_COMPONENT
      container:
        env: 
        - name: "apigee-component"
          value: APIGEE_COMPONENT
        image: CUSTOM_SIDECAR_IMAGE
        startupProbe:
          exec:
            command:
            - cat
            - /shared/token
        volumeMounts:
        - name: shared-mount
          mountPath: /shared
        - name: source-creds
          mountPath: /source-creds
          readOnly: true

如果您的 Sidecar 需要使用 Vault 註解注入每個元件的密鑰,且您無法使用 APIGEE_COMPONENT 替代機制,可以將註解新增至需要 Sidecar 的每個元件。例如:

mart:
  annotations:
    vault.hashicorp.com/agent-inject: "true"
    vault.hashicorp.com/role: "VAULT_ROLE"
    vault.hashicorp.com/agent-inject-secret-token: "VAULT_SECRET_PATH"
    vault.hashicorp.com/agent-inject-template-token: |
      {{- with secret "VAULT_SECRET_PATH" -}}
      {{ .Data.data.token }}
      {{- end -}}

runtime:
  annotations:
    vault.hashicorp.com/agent-inject: "true"
    vault.hashicorp.com/role: "VAULT_ROLE"
    vault.hashicorp.com/agent-inject-secret-token: "VAULT_SECRET_PATH"
    vault.hashicorp.com/agent-inject-template-token: |
      {{- with secret "VAULT_SECRET_PATH" -}}
      {{ .Data.data.token }}
      {{- end -}}

... other components ...

其中:

  • gcp.federatedWorkloadIdentity.enabled:必須是 true。
  • gcp.federatedWorkloadIdentity.type:必須是 "sidecar"。這會告知 Apigee Hybrid 使用 Sidecar 方法進行 Workload Identity 聯盟。
  • gcp.federatedWorkloadIdentity.sidecarSpec.container:自訂 Sidecar 的 Kubernetes 容器規格。您必須指定可從 IdP 擷取權杖的自訂 image。
  • gcp.federatedWorkloadIdentity.sidecarSpec.container.env:(選用) 要在邊車容器中設定的環境變數清單。舉例來說,您可以將 apigee-component 設為 APIGEE_COMPONENT,讓 Sidecar 識別與其搭配執行的 Apigee 元件。
  • gcp.federatedWorkloadIdentity.sidecarSpec.container.startupProbe:適用於邊車容器的 Kubernetes 啟動探查。探測器會檢查共用磁碟區中是否有權杖檔案。
  • gcp.federatedWorkloadIdentity.sidecarSpec.container.volumeMounts:邊車容器的磁碟區掛接清單。您必須為共用磁碟區加入名為 shared-mount 的磁碟區掛接,其中包含 Apigee 元件的權杖。
  • gcp.federatedWorkloadIdentity.sidecarSpec.volumes:(選用) 要新增至混合 Pod 的其他磁碟區清單,供邊車容器使用。
  • gcp.federatedWorkloadIdentity.sidecarSpec.annotations:(僅限 Vault) 要套用至混合式 Pod 的註解對應。
  • [component].annotations:(僅限 Vault,選用) 如有需要,可將註解對應至個別混合元件。

更新 overrides.yaml 檔案後,請將變更套用至叢集。 執行下列指令,更新每個混合式元件:

  1. 確認您位於 apigee-charts/ 目錄。
  2. 升級 Apigee Operator/Controller:
    helm upgrade operator apigee-operator/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  3. 升級 Apigee 資料儲存庫:
    helm upgrade datastore apigee-datastore/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  4. 升級 Apigee 遙測功能:
    helm upgrade telemetry apigee-telemetry/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  5. 升級 Apigee Redis:
    helm upgrade redis apigee-redis/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  6. 升級 Apigee Ingress 管理工具:
    helm upgrade ingress-manager apigee-ingress-manager/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  7. 升級 Apigee 機構:
    helm upgrade ORG_NAME apigee-org/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      -f OVERRIDES_FILE
    
  8. 升級環境。

    一次只能安裝一個環境。使用 --set env=ENV_NAME 指定環境。

    helm upgrade ENV_RELEASE_NAME apigee-env/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      --set env=ENV_NAME \
      -f OVERRIDES_FILE
    
  9. 升級環境群組 (virtualhosts)。

    您一次只能升級一個環境群組 (虛擬主機)。使用 --set envgroup=ENV_GROUP_NAME 指定環境群組。針對 overrides.yaml 檔案中提及的每個環境群組,重複執行下列指令:

    helm upgrade ENV_GROUP_RELEASE_NAME apigee-virtualhost/ \
      --install \
      --namespace APIGEE_NAMESPACE \
      --set envgroup=ENV_GROUP_NAME \
      -f OVERRIDES_FILE
    

如需詳細資訊,請參閱: