如果您的架構使用多項服務,可能需要憑證才能啟用服務間的通訊。Cloud Build 內建支援 OpenID Connect (OIDC) 標準,可確保服務之間的驗證和授權安全無虞。
您可以使用 Cloud Build 產生 ID 權杖。有了這些權杖,您就能從 Cloud Build 內呼叫安全端點。
舉例來說,如果您執行 Cloud Run 函式、Cloud Run 或 App Engine 等無伺服器平台應用程式,就能從 Cloud Build 工作負載中安全地與應用程式互動。
事前準備
-
Enable the Cloud Build and IAM APIs.
如果您打算使用這個帳戶建立短期憑證,則也需要啟用 IAM 服務帳戶憑證 API。
Enable the IAM Service Account Credentials API.
如要使用本指南提供的指令列範例,請安裝及設定 Google Cloud CLI。
請務必建立要使用的服務帳戶。您必須在執行建構作業的 Google Cloud 專案 Google Cloud 中建立帳戶。
必要 IAM 權限
使用者指定的服務帳戶必須具備 iam.serviceAccounts.getOpenIdToken
權限。
- 在您建立服務帳戶的專案中,將服務帳戶 OpenID Connect 身分識別權杖建立者 (
roles/iam.serviceAccountOpenIdTokenCreator
) 角色授予使用者指定的服務帳戶。
如需為服務帳戶授予 IAM 角色的操作說明,請參閱管理服務帳戶的存取權。
取得 ID 權杖的方法
您可以透過兩種方式設定建構步驟,以取得 ID 權杖:
- 使用 gcloud CLI
- 直接向中繼資料伺服器傳送要求
透過 gcloud 取得 ID 權杖
在本節中,下列程式碼片段示範如何使用 gcloud CLI 取得 ID 權杖:
YAML
steps:
- name: 'gcr.io/cloud-builders/gcloud'
script: 'gcloud auth print-identity-token --audiences ${_TOKEN_AUDIENCE} > /workspace/identity_token.txt'
env:
- _TOKEN_AUDIENCE=${_TOKEN_AUDIENCE}
service_account: '$_SERVICE_ACCOUNT'
substitutions:
_TOKEN_AUDIENCE: 'TOKEN_AUDIENCE'
_SERVICE_ACCOUNT_ID: 'SERVICE_ACCOUNT_ID'
_SERVICE_ACCOUNT: 'projects/${PROJECT_ID}/serviceAccounts/${_SERVICE_ACCOUNT_ID}'
logsBucket: 'LOGS_BUCKET_LOCATION'
options:
logging: GCS_ONLY
dynamic_substitutions: true
JSON
{
"steps": [
{
"name": "gcr.io/cloud-builders/gcloud",
"script": "gcloud auth print-identity-token --audiences ${_TOKEN_AUDIENCE} > /workspace/identity_token.txt"
"env": [
"_TOKEN_AUDIENCE=${_TOKEN_AUDIENCE}"
]
}
],
"service_account": "$_SERVICE_ACCOUNT",
"substitutions": {
"_TOKEN_AUDIENCE": "TOKEN_AUDIENCE",
"_SERVICE_ACCOUNT_ID": "SERVICE_ACCOUNT_ID",
"_SERVICE_ACCOUNT": "projects/${PROJECT_ID}/serviceAccounts/${_SERVICE_ACCOUNT_ID}"
},
"logsBucket": "LOGS_BUCKET_LOCATION",
"options": {
"logging": "GCS_ONLY",
"dynamic_substitutions": true
}
}
更改下列內容:
TOKEN_AUDIENCE
是要取得 ID 權杖的網址或目標對象,例如http://www.example.com
。SERVICE_ACCOUNT_ID
是使用者指定服務帳戶的電子郵件地址或專屬 ID。例如:service-account-name@project-id.iam.gserviceaccount.com
。LOGS_BUCKET_LOCATION
是用來儲存建構作業記錄的 Cloud Storage bucket。例如:gs://mylogsbucket
。
直接向中繼資料伺服器傳送要求
在本節中,下列程式碼片段示範如何直接向中繼資料伺服器發出要求,以取得 ID 權杖:
YAML
steps:
- name: 'gcr.io/cloud-builders/curl'
id: 'printTokenFromCurl'
script: |
curl -H 'Metadata-Flavor: Google' http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=${_TOKEN_AUDIENCE} -o /workspace/identity_token.txt
env:
- _TOKEN_AUDIENCE=${_TOKEN_AUDIENCE}
service_account: '$_SERVICE_ACCOUNT'
substitutions:
_TOKEN_AUDIENCE: 'TOKEN_AUDIENCE'
_SERVICE_ACCOUNT_ID: 'SERVICE_ACCOUNT_ID'
_SERVICE_ACCOUNT: 'projects/${PROJECT_ID}/serviceAccounts/${_SERVICE_ACCOUNT_ID}'
logsBucket: 'LOGS_BUCKET_LOCATION'
options:
logging: GCS_ONLY
dynamic_substitutions: true
JSON
{
"steps": [
{
"name": "gcr.io/cloud-builders/curl",
"id": "printTokenFromCurl"
"script": "curl -H 'Metadata-Flavor: Google' http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=${_TOKEN_AUDIENCE} -o /workspace/identity_token.txt"
"env":
"_TOKEN_AUDIENCE=${_TOKEN_AUDIENCE}"
}
],
"service_account": "$_SERVICE_ACCOUNT",
"substitutions": {
"_TOKEN_AUDIENCE": "TOKEN_AUDIENCE",
"_SERVICE_ACCOUNT_ID": "SERVICE_ACCOUNT_ID",
"_SERVICE_ACCOUNT": "projects/${PROJECT_ID}/serviceAccounts/${_SERVICE_ACCOUNT_ID}"
},
"logsBucket": "LOGS_BUCKET_LOCATION",
"options": {
"logging": "GCS_ONLY",
"dynamic_substitutions": true
}
}
更改下列內容:
TOKEN_AUDIENCE
是要取得 ID 權杖的網址或目標對象,例如http://www.example.com
。SERVICE_ACCOUNT_ID
是使用者指定服務帳戶的電子郵件地址或專屬 ID。例如:service-account-name@project-id.iam.gserviceaccount.com
。LOGS_BUCKET_LOCATION
是用來儲存建構作業記錄的 Cloud Storage bucket。例如:gs://mylogsbucket
。
如需在工作負載中產生及使用 ID 權杖的其他操作說明,請參閱「取得 ID 權杖的方法」。