接收 Cloud 稽核記錄事件


本教學課程說明如何部署已驗證的 Cloud Run 服務,透過 Cloud 稽核記錄接收 Cloud Storage 的事件。請參閱本教學課程,瞭解如何部署正式環境工作負載。Eventarc 觸發條件會根據 Cloud 稽核記錄項目篩選事件。詳情請參閱「決定 Cloud 稽核記錄的事件篩選器」。

您可以使用 Google Cloud 控制台或 Google Cloud CLI 完成本教學課程。

目標

在這個教學課程中,您將執行下列操作:

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

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

  3. 建立 Eventarc 觸發條件。

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

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

事前準備

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

如果您是專案建立者,系統會授予基本「擁有者」角色 (roles/owner)。根據預設,這個身分與存取權管理 (IAM) 角色包含完全存取大多數 Google Cloud資源所需的權限,因此您可以略過這個步驟。

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

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

所需權限

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

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

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

使用 Google Cloud 控制台或 gcloud CLI 完成下列步驟:

控制台

  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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  4. Enable the Artifact Registry, Cloud Build, Logging, Pub/Sub, Cloud Run, Cloud Storage, and Eventarc APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

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

    Enable the APIs

  8. 如要設定要在稽核記錄中記錄哪些資料存取作業,請為 Google Cloud Storage 啟用「管理員讀取」、「資料讀取」和「資料寫入」記錄類型:

    前往「稽核記錄」

  9. 前往 Google Cloud 控制台的「Service accounts」(服務帳戶) 頁面。

    前往「Service Accounts」(服務帳戶)

  10. 按一下「建立服務帳戶」
  11. 輸入要顯示在 Google Cloud 控制台的服務帳戶名稱。

    Google Cloud 控制台會根據這個名稱產生服務帳戶 ID。請視需要編輯 ID,ID 設定後即無法變更。

  12. 選用:輸入服務帳戶的說明。
  13. 如果您現在不想設定存取權控管機制,請按一下「Done」(完成) 以完成服務帳戶建立程序。

    如要立即設定存取權控管,請按一下「Create and continue」(建立並繼續),然後繼續進行下一個步驟。

  14. 選取「Cloud Run Invoker」(Cloud Run 叫用者) 和「Eventarc Event Receiver」(Eventarc 事件接收者) 角色,授予與 Eventarc 觸發條件相關聯的服務帳戶專案權限。

    為進行測試,這會將 Cloud Run Invoker 角色授予專案中的所有 Cloud Run 服務和工作;不過,您可以將角色授予服務。詳情請參閱「授予 Cloud Run 服務權限」。

    請注意,如果您為經過驗證的 Cloud Run 服務建立觸發條件,但未授予 Cloud Run 叫用者角色,系統仍會成功建立並啟用觸發條件。不過,觸發程序無法正常運作,記錄中會顯示類似下列內容的訊息:

    The request was not authenticated. Either allow unauthenticated invocations or set the proper Authorization header.
  15. 新增角色之後,請按一下「繼續」和「完成」,即可完成建立服務帳戶。

gcloud

  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, Logging, Pub/Sub, Cloud Run, Cloud Storage, and Eventarc APIs:

    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com 
          logging.googleapis.com  pubsub.googleapis.com  run.googleapis.com 
          storage.googleapis.com  and eventarc.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, Logging, Pub/Sub, Cloud Run, Cloud Storage, and Eventarc APIs:

    gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.com 
          logging.googleapis.com  pubsub.googleapis.com  run.googleapis.com 
          storage.googleapis.com  and eventarc.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}
    export SERVICE_NAME=helloworld-events
  17. 如要設定稽核記錄中記錄的資料存取作業,請為 storage.googleapis.com 服務啟用 ADMIN_READDATA_READDATA_WRITE 記錄類型:
    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 政策開始。
  18. 為專案建立服務帳戶:
    gcloud iam service-accounts create sample-service-account \
        --description="A sample service account" \
        --display-name="Sample service account"
    建立服務帳戶後,最多可能需要七分鐘才能使用。如果您在建立服務帳戶後立即嘗試使用,並收到錯誤訊息,請至少等待 60 秒,然後再試一次。
  19. 如要確認 sample-service-account 是否已建立,請執行:
    gcloud iam service-accounts list
    輸出內容應如下所示:
    DISPLAY NAME                     EMAIL                                                               DISABLED
    Default compute service account  PROJECT_NUMBER-compute@developer.gserviceaccount.com                False
    Sample service account           sample-service-account@PROJECT_ID.iam.gserviceaccount.com           False
  20. 將專案的「Cloud Run Invoker」(Cloud Run 叫用者) (run.invoker) 和「Eventarc Event Receiver」(Eventarc 事件接收器) (roles/eventarc.eventReceiver) 角色授予與 Eventarc 觸發條件相關聯的服務帳戶:
    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:sample-service-account@PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/run.invoker"
    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member="serviceAccount:sample-service-account@PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/eventarc.eventReceiver"

    PROJECT_ID 替換為專案 ID。 Google Cloud

    為進行測試,這會將 Cloud Run 叫用者角色授予專案中的所有 Cloud Run 服務和工作;不過,您可以將角色授予服務。詳情請參閱「授予 Cloud Run 服務權限」。

    請注意,如果您為經過驗證的 Cloud Run 服務建立觸發條件,但未授予 Cloud Run 叫用者角色,系統仍會成功建立並啟用觸發條件。不過,觸發程序無法正常運作,記錄中會顯示類似下列內容的訊息:

    The request was not authenticated. Either allow unauthenticated invocations or set the proper Authorization header.

建立 Artifact Registry 標準存放區

建立 Artifact Registry 標準存放區,用於儲存容器映像檔。

控制台

  1. 前往 Google Cloud 控制台的「Repositories」頁面。

    前往「Repositories」(存放區)

  2. 按一下 「建立存放區」
  3. 設定存放區:
    1. 輸入不重複的名稱
    2. 在「格式」部分選擇「Docker」
    3. 針對「模式」,請選擇「標準」
    4. 在「位置類型」中選擇「區域」
    5. 在「Region」(區域) 清單中,選取「us-central1 (Iowa)」(us-central1 (愛荷華州))
  4. 其餘設定均採用預設值,然後按一下「建立」

gcloud

gcloud artifacts repositories create REPOSITORY \
    --repository-format=docker \
    --location=$REGION

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

建立 Cloud Storage 值區

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

控制台

  1. 前往 Google Cloud 控制台的「Bucket」頁面。

    前往值區

  2. 按一下 「建立 bucket」
  3. 輸入 bucket 資訊,並點選「繼續」來完成各個步驟:
    1. 輸入不重複的名稱。例如:eventarcbucket
    2. 選取「區域」做為「位置類型」
    3. 選取「us-central1 (Iowa)」(us-central1 (愛荷華州)) 做為「Location」(位置)
    4. 在「預設儲存空間級別」部分選取「Standard」
    5. 在「存取權控管」部分選取「統一」
  4. 點選「建立」

gcloud

gcloud storage buckets create gs://events-tutorial-PROJECT_ID/ --location=$REGION

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

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

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

控制台

  1. 在 GitHub 帳戶中複製範例存放區:
  2. Go

    1. 在 GitHub 中,前往 GoogleCloudPlatform/golang-samples
    2. 按一下「Fork」
    3. 「Fork」按鈕的螢幕截圖。
    4. 如果系統提示,請選取要分叉存放區的位置。

    Java

    1. 在 GitHub 中,前往 GoogleCloudPlatform/java-docs-samples
    2. 按一下「Fork」
    3. 「Fork」按鈕的螢幕截圖。
    4. 如果系統提示,請選取要分叉存放區的位置。

    .NET

    1. 在 GitHub 中,前往 GoogleCloudPlatform/dotnet-docs-samples
    2. 按一下「Fork」
    3. 「Fork」按鈕的螢幕截圖。
    4. 如果系統提示,請選取要分叉存放區的位置。

    Node.js

    1. 在 GitHub 中,前往 GoogleCloudPlatform/nodejs-docs-samples
    2. 按一下「Fork」
    3. 「Fork」按鈕的螢幕截圖。
    4. 如果系統提示,請選取要分叉存放區的位置。

    Python

    1. 在 GitHub 中,前往 GoogleCloudPlatform/python-docs-samples
    2. 按一下「Fork」
    3. 「Fork」按鈕的螢幕截圖。
    4. 如果系統提示,請選取要分叉存放區的位置。
  3. 前往 Google Cloud 控制台的「Services」頁面。
  4. 前往「服務」分頁

  5. 按一下「Create service」(建立服務),顯示「Create service」(建立服務) 表單。
  6. 選取「從存放區持續部署」

    GitHub 存放區的異動內容會自動更新至 Artifact Registry 中的容器映像檔,並部署至 Cloud Run。

  7. 按一下「Set up with Cloud Build」(設定 Cloud Build),開啟「Set up with Cloud Build」(設定 Cloud Build) 表單。
    1. 如果出現提示,請啟用 Cloud Build APIArtifact Analysis API
    2. 選取「GitHub」做為「存放區供應商」
    3. 如果系統提示,請按一下「Install Google Cloud Build」(安裝 Google Cloud Build)
    4. 選取您建立分支的 GitHub 存放區做為存放區
    5. 點選「下一步」
    6. 在「Branch」(分支) 欄位中輸入 ^main$
    7. 選取「Dockerfile」做為「Build Type」(建構類型),並提供 Dockerfile 的來源位置:
      • eventarc/audit-storage/Dockerfile

      • eventarc/audit_storage/Dockerfile(Go)
    8. 按一下 [儲存]
  8. 在「建立服務」表單中,輸入服務名稱。 例如:helloworld-events
  9. 選取「us-central1 (Iowa)」(us-central1 (愛荷華州)) 做為服務位置的區域
  10. 根據您要在 Cloud Run 服務上允許的輸入流量,選取任一「輸入」選項。
  11. 選取「需要驗證」
  12. 點選「建立」

gcloud

  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/$SERVICE_NAME:v1
    
  3. 將容器映像檔部署至 Cloud Run:
    gcloud run deploy $SERVICE_NAME \
        --image $REGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/$SERVICE_NAME:v1
    

    系統會提示是否允許未經驗證的叫用,請輸入 n

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

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

建立 Eventarc 觸發條件

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

控制台

  1. 前往 Google Cloud 控制台的「Triggers」(觸發條件) 頁面。

    前往「Triggers」(觸發條件)

  2. 按一下「建立觸發條件」
  3. 輸入觸發條件名稱

    這是觸發條件的 ID,開頭必須是英文字母。最多可包含 63 個小寫英文字母、數字或連字號。

  4. 選取「觸發條件類型」
    • 第一方:篩選從 Google Cloud供應商傳送的事件 (直接傳送或透過 Cloud 稽核記錄項目),或是使用 Pub/Sub 訊息的供應商。
    • 第三方:篩選第三方供應商傳送的事件。
  5. 在「Event provider」(事件提供者) 清單中,選取「Cloud Storage」做為事件來源。
  6. 在「Event type」(事件類型) 清單中,選取「storage.objects.create」
  7. 在「Region」(區域) 清單中,選取「us-central1」做為接收事件的區域。
  8. 選取您建立的「服務帳戶」。例如:SERVICE_ACCOUNT_ID@PROJECT_ID.iam.gserviceaccount.com。
  9. 在「Event destination」(事件目的地) 清單中,選取「Cloud Run」
  10. 在「Select a Cloud Run service」(選取 Cloud Run 服務) 清單中,選取「helloworld-events」
  11. 點選「建立」
  12. 請注意,您也可以使用 Cloud Run 控制台頁面建立 Eventarc 觸發條件

gcloud

  1. 建立用來篩選 Cloud Storage 事件的觸發條件,並使用您建立的服務帳戶:
    gcloud eventarc triggers create events-tutorial-trigger \
        --destination-run-service=$SERVICE_NAME \
        --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=sample-service-account@PROJECT_ID.iam.gserviceaccount.com
    PROJECT_ID 替換為專案 ID。 Google Cloud

    其中:

    • type:指定在符合觸發條件的篩選條件時,建立稽核記錄。
    • serviceName:寫入稽核記錄的服務,也就是 Cloud Storage。
    • methodName:受稽核的作業,即 storage.objects.create
    這項操作會建立名為 events-tutorial-trigger 的觸發條件。
  2. 如要確認 events-tutorial-trigger 是否已建立妥當,請執行:
    gcloud eventarc triggers list --location=$REGION
  3. 觸發條件 events-tutorial-trigger 會列出目標 helloworld-events

產生及查看活動

  1. 如要產生事件,請按照下列步驟操作:

    控制台

    1. 建立檔案名稱為 random.txt 且包含「Hello World」文字的文字檔案。
    2. 前往 Google Cloud 控制台的「Bucket」頁面。

      前往值區

    3. 選取您建立的儲存空間 bucket。
    4. 在「Objects」(物件) 分頁中,按一下「Upload files」(上傳檔案),然後上傳 random.txt 檔案。

    gcloud

    將文字檔上傳至 Cloud Storage:

    echo "Hello World" > random.txt
    gcloud storage cp random.txt gs://events-tutorial-PROJECT_ID/random.txt
  2. 如要查看記錄項目:
  3. 控制台

    1. 前往 Google Cloud 控制台的「Services」頁面。

      前往「服務」分頁

    2. 在服務清單中,按一下您建立的服務名稱,前往「服務詳細資料」頁面。
    3. 按一下「記錄」分頁標籤,取得此服務所有修訂版本的要求和容器記錄。您可以依記錄的嚴重性等級進行篩選。
    4. 尋找類似下列內容的記錄項目:
      Detected change in Cloud Storage bucket: storage.googleapis.com/projects/_/buckets/BUCKET_NAME/objects/random.txt
      其中 BUCKET_NAME 是 Cloud Storage 值區的名稱。

    gcloud

    1. gcloud logging read "resource.labels.service_name=helloworld-events AND textPayload:random.txt" --format=json
    2. 尋找類似下列內容的記錄項目:
      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

後續步驟