驗證 Compute Engine

本文說明如何以程式輔助方式對 Compute Engine 進行驗證。您向 Compute Engine 驗證的方式,取決於您用來存取 API 的介面,以及程式碼執行的環境。

如果您需要查看驗證在 Compute Engine 上執行的應用程式和工作負載的 Google Cloud API 的不同選項,請參閱「選擇工作負載驗證方法」。

如要進一步瞭解 Google Cloud 驗證,請參閱「驗證方法」。

透過 API 存取

Compute Engine 支援程式碼存取權。您可以透過下列方式存取 API:

用戶端程式庫

Compute Engine 用戶端程式庫提供高階語言支援,可透過程式設計驗證 Compute Engine。為了驗證對 Google Cloud API 的呼叫,用戶端程式庫支援應用程式預設憑證 (ADC);這些程式庫會在一系列定義的位置尋找憑證,然後使用這些憑證驗證對 API 的要求。有了 ADC,您就能在各種環境 (例如本機開發或正式版) 中為應用程式提供憑證,而無須修改應用程式程式碼。

Google Cloud CLI

使用 gcloud CLI 存取 Compute Engine 時,您必須使用使用者帳戶登入 gcloud CLI,這樣才能提供 gcloud CLI 指令使用的憑證。

如果貴機構的安全性政策禁止使用者帳戶取得必要權限,您可以使用服務帳戶冒用功能。

詳情請參閱「驗證 gcloud CLI 使用權限」。如要進一步瞭解如何搭配 Compute Engine 使用 gcloud CLI,請參閱 gcloud CLI 參考頁面

REST

您可以使用 gcloud CLI 憑證或應用程式預設憑證,對 Compute Engine API 進行驗證。如要進一步瞭解如何驗證 REST 要求,請參閱「驗證 REST 使用權」。如要瞭解憑證類型,請參閱「gcloud CLI 憑證和 ADC 憑證」。

設定 Compute Engine 的驗證機制

設定驗證方法的方式取決於程式碼執行的環境。

以下是設定驗證程序最常用的選項。如要進一步瞭解驗證的選項和資訊,請參閱「驗證方法」。

本機開發環境

您可以透過下列方式設定本機開發環境的憑證:

用戶端程式庫或第三方工具

在本機環境中設定應用程式預設憑證 (ADC)

  1. After installing the Google Cloud CLI, initialize it by running the following command:

    gcloud init

    If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  2. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

    系統隨即會顯示登入畫面。登入後,憑證會儲存在 ADC 使用的本機憑證檔案中。

如要進一步瞭解如何在本機環境中使用 ADC,請參閱「為本機開發環境設定 ADC」。

透過指令列發出 REST 要求

透過指令列提出 REST 要求時,您可以使用 gcloud CLI 憑證,方法是將 gcloud auth print-access-token 納入傳送要求的指令。

以下範例列出指定專案的服務帳戶。您可以對任何 REST 要求使用相同的模式。

使用任何要求資料之前,請先替換以下項目:

  • PROJECT_ID:您的 Google Cloud 專案 ID。

如要傳送要求,請展開以下其中一個選項:

 

如要進一步瞭解如何使用 REST 和 gRPC 進行驗證,請參閱「使用 REST 進行驗證」。如要瞭解本機 ADC 憑證和 gcloud CLI 憑證的差異,請參閱「gcloud CLI 驗證設定和 ADC 設定」。

服務帳戶模擬

在多數情況下,您可以使用使用者憑證,透過本機開發環境進行驗證。如果無法執行這項操作,或是您需要測試指派給服務帳戶的權限,可以使用服務帳戶冒用功能。您必須具備 iam.serviceAccounts.getAccessToken 權限,該權限包含在 服務帳戶權杖建立者 (roles/iam.serviceAccountTokenCreator) IAM 角色中。

您可以使用 gcloud config set 指令,設定 gcloud CLI 使用服務帳戶冒用功能:

gcloud config set auth/impersonate_service_account SERVICE_ACCT_EMAIL

針對特定語言,您可以使用服務帳戶冒用功能,建立用戶端程式庫可用的本機 ADC 檔案。這種做法僅適用於 Go、Java、Node.js 和 Python 用戶端程式庫,其他語言不支援。如要設定本機 ADC 檔案並冒用服務帳戶,請使用 --impersonate-service-account 標記搭配 gcloud auth application-default login 指令

gcloud auth application-default login --impersonate-service-account=SERVICE_ACCT_EMAIL

如要進一步瞭解服務帳戶模擬功能,請參閱「使用服務帳戶模擬功能」。

Google Cloud

如要驗證在 Google Cloud上執行的工作負載,您可以使用服務帳戶的憑證,該帳戶已連結至程式碼執行所在的運算資源,例如 Compute Engine 虛擬機器 (VM) 執行個體。在 Google Cloud 運算資源上執行程式碼時,建議使用此方法進行驗證。

對於大多數服務,您必須在建立要執行程式碼的資源時附加服務帳戶;您無法在日後新增或取代服務帳戶。不過,Compute Engine 例外,您隨時可以將服務帳戶附加至 VM 執行個體。

使用 gcloud CLI 建立服務帳戶,並將該帳戶附加至資源:

  1. After installing the Google Cloud CLI, initialize it by running the following command:

    gcloud init

    If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  2. Set up authentication:

    1. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with a name for the service account.

    2. To provide access to your project and your resources, grant a role to the service account:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=ROLE

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
      • ROLE: the role to grant
    3. To grant another role to the service account, run the command as you did in the previous step.
    4. Grant the required role to the principal that will attach the service account to other resources.

      gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com --member="user:USER_EMAIL" --role=roles/iam.serviceAccountUser

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
      • USER_EMAIL: the email address for a Google Account
  3. 建立將執行程式碼的資源,並將服務帳戶附加至該資源。舉例來說,如果您使用 Compute Engine:

    Create a Compute Engine instance. Configure the instance as follows:
    • Replace INSTANCE_NAME with your preferred instance name.
    • Set the --zone flag to the zone in which you want to create your instance.
    • Set the --service-account flag to the email address for the service account that you created.
    gcloud compute instances create INSTANCE_NAME --zone=ZONE --service-account=SERVICE_ACCOUNT_EMAIL

如要進一步瞭解如何驗證 Google API,請參閱「驗證方法」。

地端部署或其他雲端供應商

在 Google Cloud 之外設定驗證功能的首選方法,是使用 Workload Identity 聯盟。詳情請參閱驗證說明文件中的「為地端部署或其他雲端服務供應商設定 ADC」。

Compute Engine 的存取權控管

向 Compute Engine 驗證身分後,您必須獲得授權,才能存取 Google Cloud 資源。Compute Engine 會使用 Identity and Access Management (IAM) 進行授權。

如要進一步瞭解 Compute Engine 的角色,請參閱「Compute Engine 身分與存取權管理角色和權限 」。如要進一步瞭解身分與存取權管理和授權,請參閱 身分與存取權管理總覽

後續步驟