Compute Engine Service Account Best Practices
What is Service Account ?
說明 GCP Service Account 以前,得先理解 GCP 的 IAM 設計機制
GCP IAM 組成機制
由上圖可見,GCP IAM 可拆分成
- 帳號(principal):通常為 email,屬性通常為
- 真人帳號:如果為個人使用者,則可為個人 gmail,如為組織使用者,可為 GoogleWorkSpace 帳號或者 Cloud Identity 帳號
- Service Account 帳號:非人類使用之帳號,但仍需要一個身分識別
- 群組帳號:通常集合一部分真人帳號,提供群組中成員特定權限,以便於管理
- 角色(role):由眾多 permissions 所組成一個 role 便於管理,可自訂也可使用官方定義好的角色
- 權限(permission):最細粒度的權限,通常由
服務名稱.服務資源.動詞組成,如:pubsub.subscriptions.consume - 分為官方預設好的角色(predefined role)與自定義角色(custom role)
- predefined role 會由 GCP 進行維護,如未來有任何權限調整,會自動跟著 GCP 調整
- custom role 使用者必須自行維護,但可完全依照自身需求進行權限配置調整
- 權限(permission):最細粒度的權限,通常由
- 應用時,會將帳號綁定角色,一個帳號可擁有多重角色
更多細節可參考官方文件
GCE (GCP Compute Engine) 預設 Service Account 權限
- 預設 GCE (GCP Compute Engine) 會使用
PROJECT_ID[email protected] 這組預設 service account - 其預設 Role 為 Editor,而 Editor 權限可以說相當大,因此會搭配 Access scopes 來進行管理
- 預設的 Access Scopes 會開啟權限如下
- Read-only access to Cloud Storage:
- Write access to write Compute Engine logs:
- Write access to publish metric data to your Google Cloud projects:
- Read-only access to Service Management features required for Google Cloud Endpoints(Alpha):
- Read or write access to Service Control features required for Google Cloud Endpoints(Alpha):
- Write access to Cloud Trace allows an application running on a VM to write trace data to a project.
- 或使用自訂 access scope 如下圖所示
- 預設的 Access Scopes 會開啟權限如下

- Access scopes 套用範圍為以 VM 為單位,不同 VM 可套用不同範圍 access scopes,如
- 會連接到 CloudSQL 主機需額外設定 Access Scopes 不然會無法連線至 CloudSQL
- Access scope 為 legacy 管理 service account 權限做法
GCE Service Account Best Practice
- 大原則
- 不要使用預設 Service Account 與 Access Scopes 設定
- 除了無法妥善管理 Google Cloud Storage 服務權限,預設 Service Account 具有 Read-only access to Cloud Storage 權限,
此部分對於資料權限管理有極高風險,官方文件也有所呼籲
- 除了無法妥善管理 Google Cloud Storage 服務權限,預設 Service Account 具有 Read-only access to Cloud Storage 權限,
- 一個 Service Account 一個用途,如:同一 GCE VM 群組,使用同一 Service Account
- 命名原則可使用
vm-開頭以便於識別
- 不要使用預設 Service Account 與 Access Scopes 設定
- 建議權限,基本上都是監控會使用的權限
roles/logging.logWriter:Ops Agentroles/monitoring.metricWriter:Ops Agentroles/stackdriver.resourceMetadata.writer:GKE node 會使用的權限- 其他會以 GCE VM 存取之 GCP 服務權限
- 基於最小權限原則,如:使用 Google Cloud Storage (GCS),可將該 GCE 使用之 Service Account 加入特定 Bucket 中特定權限,如此一來便可限縮某些主機僅能存取特定 bucket 資料
- 防火牆可使用 service account 替代,可更統一管理資料流向,如下圖所示
