在 AKS 和 EKS 上啟用 Workload Identity Federation

本主題說明如何在 AKSEKS 平台上,為 Apigee Hybrid 安裝啟用 Workload Identity。

總覽

工作負載身分聯盟可讓在 Google Cloud 外部執行的應用程式,使用外部身分識別提供者的憑證模擬 Google Cloud Platform 服務帳戶。

使用工作負載身分聯盟可讓應用程式使用外部環境提供的驗證機制,有助於提升安全性,並取代服務帳戶金鑰

如需總覽,請參閱「使用 Workload Identity 聯盟的最佳做法」。

設定 Workload Identity 聯盟

如要搭配 Apigee Hybrid 使用 Workload Identity Federation,請先設定叢集,然後將這項功能套用至 Apigee Hybrid 安裝作業。

設定叢集,使用 Workload Identity 聯盟。

請按照 Google Cloud 的指示為 Kubernetes 設定工作負載身分聯盟,並進行下列修改:

  • 使用下列指令列出 IAM 服務帳戶和 Kubernetes 服務帳戶:
    • IAM 服務帳戶:您很可能已在首次使用 create-service-account 工具安裝 Apigee Hybrid 時,建立 IAM 服務帳戶 (也稱為「Google 服務帳戶」)。如需 Apigee Hybrid 必須使用的 IAM 服務帳戶清單,請參閱「關於服務帳戶」。

      您可以使用下列指令,查看專案中的 IAM 服務帳戶清單:

      gcloud iam service-accounts list --project PROJECT_ID
    • Kubernetes 服務帳戶:執行 helm installhelm update 指令時,Apigee Hybrid 圖表會為每個元件建立必要的 Kubernetes 服務帳戶。

      您可以使用 kubectl get sa 指令查看叢集中的 Kubernetes 服務帳戶:

      kubectl get sa -n APIGEE_NAMESPACE
      kubectl get sa -n apigee-system
  • 在「設定 Workload Identity 聯盟」步驟中,所建立 Workload Identity 集區和提供者的預設對象如下。使用這個預設值或設定自訂預期目標對象,並儲存這個值以供日後使用。
    https://iam.googleapis.com/projects/PROJECT_NUM/locations/global/workloadIdentityPools/POOL_ID/providers/PROVIDER_ID
  • 在「部署 Kubernetes 工作負載」一節中,完成步驟 1 後停止。每個 Google 服務帳戶都會有一個憑證設定檔。儲存每個憑證設定檔,並儲存為 --credential-source-file 參數輸入的路徑,例如:/var/run/service-account/token

設定 Apigee Hybrid 使用 Workload Identity 聯盟

  1. 將憑證來源檔案和輸出檔案 (credential-configuration.json) 複製到下列圖表目錄。這些值是您在「部署 Kubernetes 工作負載」步驟 1 中提供。
    • apigee-datastore/
    • apigee-env
    • apigee-org/
    • apigee-telemetry/
  2. 對叢集的覆寫檔案進行下列全域變更:
    gcp:
      workloadIdentity:
        enabled: false # must be set to false to use Workload Identity Federation
      federatedWorkloadIdentity:
        enabled: true
        audience: "AUDIENCE"
        credentialSourceFile: "CREDENTIAL_SOURCE_FILE"
    

    其中:

    • AUDIENCE 是 Workload Identity 提供者的允許對象,也就是您在「部署 Kubernetes 工作負載」一節的步驟 1 中設定的憑證設定 JSON 檔案中,.audience 下方的值。
    • CREDENTIAL_SOURCE_FILE 是 workload identity federation 用來取得服務帳戶憑證的憑證來源檔案名稱和路徑。這是您在「部署 Kubernetes 工作負載」的步驟 1 中,使用 create-cred-config 指令設定 Workload Identity 聯盟時為 credential-source-file 提供的值。例如:
    • 例如:

      gcp:
        workloadIdentity:
          enabled: false
        federatedWorkloadIdentity:
          enabled: true
          audience: "//iam.googleapis.com/projects/123456789012/locations/global/workloadIdentityPools/aws-pool/providers/aws-provider"
          credentialSourceFile: "/var/run/service-account/token"
      
  3. 使用 Workload Identity 聯盟,為每個元件設定覆寫。請根據安裝方式,選取憑證檔案、Kubernetes 密鑰或 Vault 的操作說明。

    認證檔案

    serviceAccountPath 的值替換成憑證來源檔案。這必須是相對於圖表目錄的路徑。例如:

    udca:
      serviceAccountPath: fwi/credential-configuration.json
    

    K8s 密鑰

    1. 使用憑證來源檔案建立新的 Kubernetes 密鑰。
      kubectl create secret -n apigee generic SECRET_NAME --from-file="client_secret.json=CREDENTIAL_CONFIGURATION_FILE"

      例如:

      kubectl create secret -n apigee generic udca-fwi-secret --from-file="client_secret.json=./fwi/credential-configuration.json"
    2. serviceAccountRef 的值替換成新密鑰。例如:
      udca:
        serviceAccountRef: udca-fwi-secret
      

    保管箱

    使用憑證來源檔案更新 Vault 中的服務帳戶金鑰 SAKEY。舉例來說,如果是 UDCA (所有元件的程序都類似):

    SAKEY=$(cat ./fwi/credential-configuration.json); kubectl -n apigee exec vault-0 -- vault kv patch secret/apigee/orgsakeys udca="$SAKEY"
  4. 使用 helm update 指令,將變更套用至每個受影響的元件:

    如果您是首次透過這個叢集使用 Vault,請更新 apigee-operator 圖表:

    helm upgrade operator apigee-operator/ \
      --namespace apigee-system \
      --atomic \
      -f overrides.yaml
    

    按照下列順序更新其餘受影響的圖表:

    helm upgrade datastore apigee-datastore/ \
      --namespace apigee \
      --atomic \
      -f overrides.yaml
    
    helm upgrade telemetry apigee-telemetry/ \
      --namespace apigee \
      --atomic \
      -f overrides.yaml
    
    helm upgrade $ORG_NAME apigee-org/ \
      --namespace apigee \
      --atomic \
      -f overrides.yaml
    

    更新每個環境的 apigee-env 圖表,每次都取代 ENV_NAME

    helm upgrade $ENV_NAME apigee-env/ \
      --namespace apigee \
      --atomic \
      --set env=$ENV_NAME \
      -f overrides.yaml
    

    如需元件清單和對應的圖表,請參閱 Apigee Hybrid Helm 參考資料

如要進一步瞭解 Workload Identity 聯盟和最佳做法,請參閱「使用 Workload Identity 聯盟的最佳做法」。