本文說明如何以程式輔助方式向 Secure Web Proxy 進行驗證。您向 Secure Web Proxy 驗證的方式,取決於您用來存取 API 的介面,以及程式碼執行的環境。
如要進一步瞭解 Google Cloud 驗證,請參閱「驗證方法」。
透過 API 存取
Secure Web Proxy 支援程式輔助存取。您可以透過下列方式存取 API:
REST
您可以使用 gcloud CLI 憑證或應用程式預設憑證,向 Secure Web Proxy API 驗證。如要進一步瞭解如何驗證 REST 要求,請參閱「驗證 REST 使用權限」。如要瞭解憑證類型,請參閱「gcloud CLI 憑證和 ADC 憑證」。
設定 Secure Web Proxy 的驗證機制
設定驗證方法的方式取決於程式碼執行的環境。
以下是設定驗證程序最常用的選項。如要進一步瞭解驗證的選項和相關資訊,請參閱「驗證方法」。
本機開發環境
您可以透過下列方式設定本機開發環境的憑證:
用戶端程式庫或第三方工具
在本機環境中設定應用程式預設憑證 (ADC):
-
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.
-
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。
如要傳送要求,請展開以下其中一個選項:
curl (Linux、macOS 或 Cloud Shell)
執行下列指令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://iam.googleapis.com/v1/projects/PROJECT_ID /serviceAccounts"
PowerShell (Windows)
執行下列指令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://iam.googleapis.com/v1/projects/PROJECT_ID /serviceAccounts" | Select-Object -Expand Content
如要進一步瞭解如何使用 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_accountSERVICE_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 建立服務帳戶,並將該帳戶附加至資源:
-
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.
-
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create
SERVICE_ACCOUNT_NAME Replace
SERVICE_ACCOUNT_NAME
with 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.serviceAccountUserReplace 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
-
-
建立要執行程式碼的資源,並將服務帳戶附加至該資源。舉例來說,如果您使用 Compute Engine:
Create a Compute Engine instance. Configure the instance as follows:-
將
INSTANCE_NAME
替換為您偏好的執行個體名稱。 -
將
--zone
標記設定為您要在其中建立執行個體的區域。 -
將
--service-account
標記設為您建立的服務帳戶電子郵件地址。
gcloud compute instances create
INSTANCE_NAME --zone=ZONE --service-account=SERVICE_ACCOUNT_EMAIL -
將
如要進一步瞭解如何驗證 Google API,請參閱「驗證方法」。
地端部署或其他雲端供應商
建議您使用 Workload Identity 聯盟,在 Google Cloud 外部設定驗證機制。詳情請參閱驗證說明文件中的「為地端部署或其他雲端服務供應商設定 ADC」。
Secure Web Proxy 的存取權控管
對 Secure Web Proxy 進行驗證後,您必須獲得授權才能存取 Google Cloud 資源。Secure Web Proxy 會使用身分與存取權管理 (IAM) 進行授權。
如要進一步瞭解安全 Web Proxy 的角色,請參閱「使用 IAM 控管存取權」。如要進一步瞭解身分與存取權管理和授權,請參閱 身分與存取權管理總覽。
後續步驟
- 瞭解 Google Cloud 驗證方法。
- 請參閱驗證用途清單。