本文說明如何以程式輔助方式向 Secure Source Manager 進行驗證。向 Secure Source Manager 進行驗證的方法,取決於您用來存取 API 的介面,以及執行程式碼的環境。
如要進一步瞭解 Google Cloud 驗證,請參閱「驗證方式」。
API 存取權
Secure Source Manager 支援程式輔助存取。您可以透過下列方式存取 API:
REST
您可以使用 gcloud CLI 憑證或應用程式預設憑證,向 Secure Source Manager API 進行驗證。如要進一步瞭解 REST 要求的驗證機制,請參閱「使用 REST 進行驗證」。如要瞭解憑證類型,請參閱「gcloud CLI 憑證和 ADC 憑證」。
設定 Secure Source Manager 的驗證機制
驗證機制的設定方式取決於執行程式碼的環境。
以下是最常用的驗證機制設定方式。如要瞭解更多驗證選項和相關資訊,請參閱「驗證方式」。
適用於本機開發環境
您可以透過下列方式,為本機開發環境設定憑證:
用戶端程式庫或第三方工具
在本機環境中設定應用程式預設憑證 (ADC):
-
Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:
gcloud initIf you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
登入畫面會隨即顯示。登入後,您的憑證會儲存在 ADC 使用的本機憑證檔案中。
如要進一步瞭解如何在本機環境中使用 ADC,請參閱「為本機開發環境設定 ADC」。
透過指令列發出 REST 要求
透過指令列發出 REST 要求時,只要在用於傳送要求的指令中加入 gcloud auth print-access-token,即可使用 gcloud CLI 憑證。
下列範例會列出指定專案的服務帳戶。您可以將相同模式用於任何 REST 要求。
使用任何要求資料之前,請先替換以下項目:
- PROJECT_ID:您的 Google Cloud 專案 ID。
如要傳送要求,請展開以下其中一個選項:
如要進一步瞭解如何使用 REST 和 gRPC 進行驗證,請參閱「使用 REST 進行驗證」。如要瞭解本機 ADC 憑證與 gcloud CLI 憑證的差異,請參閱「gcloud CLI 驗證設定和 ADC 設定」。
服務帳戶模擬
在多數情況下,您可以使用使用者憑證,從本機開發環境進行驗證。如果無法這麼做,或者需要測試指派給服務帳戶的權限,可以使用服務帳戶模擬功能。您必須具備 iam.serviceAccounts.getAccessToken 權限,這項權限包含在服務帳戶權杖建立者 (roles/iam.serviceAccountTokenCreator) 身分與存取權管理角色中。
您可以使用 gcloud config set 指令,設定 gcloud CLI 來使用服務帳戶模擬功能:
gcloud config set auth/impersonate_service_account SERVICE_ACCT_EMAIL
對於特定語言,您可以使用服務帳戶模擬功能建立本機 ADC 檔案,供用戶端程式庫使用。這種做法僅適用於 Go、Java、Node.js 和 Python 用戶端程式庫,不適用於其他語言。如要使用服務帳戶模擬功能設定本機 ADC 檔案,請搭配 gcloud auth application-default login 指令使用 --impersonate-service-account 旗標:
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 建立服務帳戶,並附加至資源:
-
Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:
gcloud initIf you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
Set up authentication:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAMEwith a name for the service account. -
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 accountPROJECT_ID: the project ID where you created the service accountROLE: the role to grant
- To grant another role to the service account, run the command as you did in the previous step.
-
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 accountPROJECT_ID: the project ID where you created the service accountUSER_EMAIL: the email address for a Google Account
-
Ensure that you have the Create Service Accounts IAM role
(
-
建立要執行程式碼的資源,並將服務帳戶附加至該資源。例如,如果您使用 Compute Engine:
Create a Compute Engine instance. Configure the instance as follows:-
Replace
INSTANCE_NAMEwith your preferred instance name. -
Set the
--zoneflag to the zone in which you want to create your instance. -
將
--service-account旗標設為您建立的服務帳戶電子郵件地址。
gcloud compute instances create INSTANCE_NAME --zone=ZONE --service-account=SERVICE_ACCOUNT_EMAIL
-
Replace
如要進一步瞭解如何向 Google API 進行驗證,請參閱「驗證方式」。
地端部署或其他雲端服務供應商
如要從 Google Cloud 外部設定驗證,建議使用 workload identity federation。詳情請參閱驗證說明文件中的「為地端部署或其他雲端服務供應商設定 ADC」。
Secure Source Manager 的存取權控管
向 Secure Source Manager 進行驗證後,您必須獲得授權才能存取 Google Cloud 資源。Secure Source Manager 使用 Identity and Access Management (IAM) 進行授權。
如要進一步瞭解 Secure Source Manager 的角色,請參閱「使用 IAM 控管存取權」一文;如要進一步瞭解 IAM 和授權,請參閱「IAM 總覽」一文。
後續步驟
- 瞭解Google Cloud 驗證方式。
- 查看驗證用途清單。