使用 Cloud 稽核記錄接收事件 (gcloud CLI)

本快速入門導覽課程說明如何使用 Eventarc,在未經驗證的 Cloud Run 服務中接收 Cloud Storage 事件。

您可以使用 Google Cloud CLI 完成本快速入門導覽課程。如需使用控制台的操作說明,請參閱「使用 Google Cloud 控制台建立觸發條件」。

在本快速入門導覽課程中,您將:

  1. 建立 Cloud Storage bucket 做為事件來源。

  2. 將事件接收器服務部署至 Cloud Run。

  3. 建立事件觸發條件。

  4. 將檔案上傳至 Cloud Storage 值區以產生事件,以及在 Cloud Run 記錄檔中查看該事件。

事前準備

貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

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

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Artifact Registry, Cloud Build, Cloud Run, Eventarc, and Pub/Sub APIs:

    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com run.googleapis.com eventarc.googleapis.com pubsub.googleapis.com
  8. Install the Google Cloud CLI.

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

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  12. Make sure that billing is enabled for your Google Cloud project.

  13. Enable the Artifact Registry, Cloud Build, Cloud Run, Eventarc, and Pub/Sub APIs:

    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com run.googleapis.com eventarc.googleapis.com pubsub.googleapis.com
  14. 更新 gcloud 元件:
    gcloud components update
  15. 登入帳戶:
    gcloud auth login
  16. 設定本快速入門導覽課程中使用的設定變數:
    export REGION=us-central1
    gcloud config set run/region ${REGION}
    gcloud config set run/platform managed
    gcloud config set eventarc/location ${REGION}
  17. 如果您是專案建立者,系統會授予基本「擁有者」角色 (roles/owner)。根據預設,這個身分與存取權管理 (IAM) 角色包含完全存取大多數 Google Cloud資源所需的權限,因此您可以略過這個步驟。

    如果您不是專案建立者,必須在專案中將必要權限授予適當的主體。舉例來說,主體可以是 Google 帳戶 (適用於使用者) 或服務帳戶 (適用於應用程式和運算工作負載)。詳情請參閱活動目的地的「角色和權限」頁面。

    請注意,根據預設,Cloud Build 權限包含上傳及下載 Artifact Registry 構件的權限

    所需權限

    如要取得完成本教學課程所需的權限,請要求管理員為您授予專案的下列 IAM 角色:

    如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

    您或許還可透過自訂角色或其他預先定義的角色取得必要權限。

  18. 在 Google Cloud Storage 中啟用 Cloud 稽核記錄的「管理員讀取」、「資料讀取」和「資料寫入」記錄類型。

    請注意,在專案層級,您需要「身分與存取權管理」角色,才能為資源設定資料存取稽核記錄。roles/owner Google Cloud

    1. 讀取專案的 IAM 政策,並儲存在檔案中:
      gcloud projects get-iam-policy PROJECT_ID > /tmp/policy.yaml
    2. 編輯 /tmp/policy.yaml 中的政策,新增或變更資料存取稽核記錄設定。

      auditConfigs:
      - auditLogConfigs:
      - logType: ADMIN_READ
      - logType: DATA_WRITE
      - logType: DATA_READ
      service: storage.googleapis.com
    3. 撰寫新的 IAM 政策:
      gcloud projects set-iam-policy PROJECT_ID /tmp/policy.yaml
      如果上述指令回報與其他變更發生衝突,請重複這些步驟,從讀取專案的 IAM 政策開始。
  19. 記下 Compute Engine 預設服務帳戶,因為您會將其附加至 Eventarc 觸發程序,代表觸發程序的身分,以利進行測試。啟用或使用採用 Compute Engine 的服務後,系統會自動建立這個服務帳戶,電子郵件地址格式如下: Google Cloud

    PROJECT_NUMBER-compute@developer.gserviceaccount.com

    PROJECT_NUMBER 替換為專案編號。 Google Cloud您可以在 Google Cloud 控制台的「歡迎」頁面找到專案編號,也可以執行下列指令:

    gcloud projects describe PROJECT_ID --format='value(projectNumber)'

    在正式環境中,我們強烈建議建立新的服務帳戶,並授予一或多個包含最低必要權限的 IAM 角色,遵循最低權限原則。

  20. 將專案的 Eventarc 事件接收者角色 (roles/eventarc.eventReceiver) 授予 Compute Engine 預設服務帳戶,讓 Eventarc 觸發條件可以接收事件供應商的事件。
    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \
        --role=roles/eventarc.eventReceiver
  21. 如果您是在 2021 年 4 月 8 日當天或之前啟用 Cloud Pub/Sub 服務代理,請將服務帳戶權杖建立者角色 (roles/iam.serviceAccountTokenCreator) 授予服務代理,以支援已驗證的 Pub/Sub 推送要求。否則,系統會預設授予這個角色:
    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com \
        --role=roles/iam.serviceAccountTokenCreator
  22. 下載並安裝 Git 原始碼管理工具。

建立 Artifact Registry 標準存放區

建立 Artifact Registry 標準存放區,用於儲存容器映像檔:
gcloud artifacts repositories create REPOSITORY \
    --repository-format=docker \
    --location=$REGION

REPOSITORY 替換成存放區的專屬名稱。

建立 Cloud Storage 值區

本快速入門導覽課程會使用 Cloud Storage 做為事件來源。如要建立儲存空間 bucket,請按照下列步驟操作:

gcloud storage buckets create gs://events-quickstart-PROJECT_ID/ --location=${REGION}

建立事件來源後,即可在 Cloud Run 上部署事件接收器服務。

將事件接收器服務部署至 Cloud Run

部署可接收及記錄事件的 Cloud Run 服務。 如要部署範例事件接收器服務,請按照下列步驟操作:

  1. 複製 GitHub 存放區:

    Go

    git clone https://github.com/GoogleCloudPlatform/golang-samples.git
    cd golang-samples/eventarc/audit_storage
    

    Java

    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
    cd java-docs-samples/eventarc/audit-storage
    

    .NET

    git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
    cd dotnet-docs-samples/eventarc/audit-storage
    

    Node.js

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
    cd nodejs-docs-samples/eventarc/audit-storage
    

    Python

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
    cd python-docs-samples/eventarc/audit-storage
    
  2. 建構容器並上傳至 Cloud Build:

    gcloud builds submit --tag $REGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/helloworld-events:v1
  3. 將容器映像檔部署至 Cloud Run:

    gcloud run deploy helloworld-events \
        --image $REGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/helloworld-events:v1 \
        --allow-unauthenticated

    部署成功後,指令列會顯示服務網址。

    您已將名為 helloworld-events 的事件接收器服務部署至 Cloud Run,現在可以設定觸發程序。

建立 Eventarc 觸發條件

Eventarc 觸發條件會將 Cloud Storage bucket 中的事件傳送至 helloworld-events Cloud Run 服務。

  1. 建立用來篩選 Cloud Storage 事件的觸發條件,並使用 Google Cloud 專案的 Compute Engine 預設服務帳戶:

    gcloud eventarc triggers create events-quickstart-trigger \
        --destination-run-service=helloworld-events \
        --destination-run-region=${REGION} \
        --event-filters="type=google.cloud.audit.log.v1.written" \
        --event-filters="serviceName=storage.googleapis.com" \
        --event-filters="methodName=storage.objects.create" \
        --service-account=PROJECT_NUMBER-compute@developer.gserviceaccount.com

    這項操作會建立名為 events-quickstart-trigger 的觸發條件。

    請注意,在 Google Cloud 專案中首次建立 Eventarc 觸發條件時,Eventarc 服務代理程式的佈建作業可能會延遲。這個問題通常可以透過再次建立觸發條件來解決。詳情請參閱「權限遭拒錯誤」。

  2. 如要確認 events-quickstart-trigger 是否已建立妥當,請執行:

    gcloud eventarc triggers list --location=${REGION}

    events-quickstart-trigger 會列出目的地,也就是 Cloud Run 服務 helloworld-events

產生及查看活動

  1. 如要產生事件,請將文字檔案上傳到 Cloud Storage:

     echo "Hello World" > random.txt
     gcloud storage cp random.txt gs://events-quickstart-PROJECT_ID/random.txt
    

    上傳作業會產生事件,而 Cloud Run 服務會記錄事件的訊息。

  2. 如要查看服務建立的事件相關記錄項目,請執行下列指令:

    gcloud logging read 'textPayload: "Detected change in Cloud Storage bucket"'
    
  3. 尋找類似下列內容的記錄項目:

    Detected change in Cloud Storage bucket: storage.googleapis.com/projects/_/buckets/BUCKET_NAME/objects/random.txt
    

    其中 BUCKET_NAME 是 Cloud Storage 值區的名稱。

您已成功將事件接收器服務部署至 Cloud Run、建立 Eventarc 觸發程序、從 Cloud Storage 產生事件,並在 Cloud Run 記錄檔中查看該事件。

清除所用資源

雖然 Cloud Run 在服務未用時不會產生費用,但您可能仍須支付將容器映像檔儲存於 Artifact Registry將檔案儲存於 Cloud Storage 值區,以及Eventarc 資源的費用。

您可以:

  1. 刪除容器映像檔

  2. 刪除儲存空間 bucket

  3. 刪除 Eventarc 觸發條件

或者,您也可以刪除 Google Cloud 專案,以免產生費用。 刪除 Google Cloud 專案後,系統就會停止對專案使用的所有資源收取費用。

Delete a Google Cloud project:

gcloud projects delete PROJECT_ID

後續步驟