GSP698

總覽
CFT Scorecard 是 Forseti Config Validator 的開放原始碼指令列用戶端,也是 Cloud Foundation Toolkit 的其中一項工具。有了這項工具,您就能掌握 Google Cloud 資源、專案、資料夾或組織中是否有設定錯誤和違反既定標準的情形。
Google Cloud 的資源超過 86 種,而且仍在增加。遷移至公有雲,即可更輕鬆地將雲端作業和資源部署工作委派給不同員工。不過隨著基礎架構、資源和政策部署作業的整合程度與靈活彈性增加,持續遵守政策和標準變得日益困難。
在這個實驗室,您會設定 CFT Scorecard,進一步掌握 Google Cloud 專案的情況並偵測錯誤設定。
實驗室學習重點
這個實驗室著重說明多位使用者同時使用雲端時面臨的挑戰。您會啟用 CFT Scorecard,並透過整合 Cloud Asset Inventory 和開放原始碼政策庫,擴充其資源設定監控與違規情形偵測功能。您會設定工具來偵測錯誤設定和公開程度過高的資源,同時讓團隊中的其他人員與 Google Cloud 專案都能在既定政策範圍內保有靈活彈性。
涵蓋主題
- 設定 CFT Scorecard。
- 透過 CFT Scorecard 執行評估。
- 新增 CFT Scorecard 政策。

情境
您是 3 人團隊的技術主管,與遠端工作的團隊成員莉莉和阿超密切合作,在一項共用 Google Cloud 專案部署多項資源。共事幾週之後,您開始留意到一些警訊,並很快發現莉莉和阿超不按照規定行事,貪快套用了您認為有問題的專案設定,其中一項錯誤設定導致 Cloud Storage bucket 對外公開。這只是您發現的其中一項錯誤設定,而您擔心可能只是冰山一角。
您透過 Google 快速搜尋,並找到 Cloud Foundation Toolkit (CFT) Scorecard CLI 這個公用程式。概略看完說明之後,您認為這項工具可協助管理 Google Cloud 環境中的政策,以及判斷設定有誤的地方,因此決定試用。
設定和需求
瞭解以下事項後,再點選「Start Lab」按鈕
請詳閱以下操作說明。實驗室活動會計時,且中途無法暫停。點選「Start Lab」後就會開始計時,顯示可使用 Google Cloud 資源的時間。
您將在真正的雲端環境完成實作實驗室活動,而不是模擬或示範環境。為此,我們會提供新的暫時憑證,供您在實驗室活動期間登入及存取 Google Cloud。
為了順利完成這個實驗室,請先確認:
- 可以使用標準的網際網路瀏覽器 (Chrome 瀏覽器為佳)。
注意事項:請使用無痕模式 (建議選項) 或私密瀏覽視窗執行此實驗室,這可以防止個人帳戶和學員帳戶之間的衝突,避免個人帳戶產生額外費用。
- 是時候完成實驗室活動了!別忘了,活動一旦開始將無法暫停。
注意事項:務必使用實驗室專用的學員帳戶。如果使用其他 Google Cloud 帳戶,可能會產生額外費用。
如何開始研究室及登入 Google Cloud 控制台
-
點選「Start Lab」按鈕。如果實驗室會產生費用,畫面上會出現選擇付款方式的對話方塊。左側的「Lab Details」窗格會顯示下列項目:
- 「Open Google Cloud console」按鈕
- 剩餘時間
- 必須在這個研究室中使用的臨時憑證
- 完成這個實驗室所需的其他資訊 (如有)
-
點選「Open Google Cloud console」;如果使用 Chrome 瀏覽器,也能按一下滑鼠右鍵,選取「在無痕視窗中開啟連結」。
接著,實驗室會啟動相關資源,並開啟另一個分頁,顯示「登入」頁面。
提示:您可以在不同的視窗中並排開啟分頁。
注意:如果頁面中顯示「選擇帳戶」對話方塊,請點選「使用其他帳戶」。
-
如有必要,請將下方的 Username 貼到「登入」對話方塊。
{{{user_0.username | "Username"}}}
您也可以在「Lab Details」窗格找到 Username。
-
點選「下一步」。
-
複製下方的 Password,並貼到「歡迎使用」對話方塊。
{{{user_0.password | "Password"}}}
您也可以在「Lab Details」窗格找到 Password。
-
點選「下一步」。
重要事項:請務必使用實驗室提供的憑證,而非自己的 Google Cloud 帳戶憑證。
注意:如果使用自己的 Google Cloud 帳戶來進行這個實驗室,可能會產生額外費用。
-
按過後續的所有頁面:
- 接受條款及細則。
- 由於這是臨時帳戶,請勿新增救援選項或雙重驗證機制。
- 請勿申請免費試用。
Google Cloud 控制台稍後會在這個分頁開啟。
注意:如要使用 Google Cloud 產品和服務,請點選「導覽選單」,或在「搜尋」欄位輸入服務或產品名稱。
啟動 Cloud Shell
Cloud Shell 是搭載多項開發工具的虛擬機器,提供永久的 5 GB 主目錄,而且在 Google Cloud 中運作。Cloud Shell 提供指令列存取權,方便您使用 Google Cloud 資源。
-
點按 Google Cloud 控制台頂端的「啟用 Cloud Shell」圖示
。
-
系統顯示視窗時,請按照下列步驟操作:
- 繼續操作 Cloud Shell 視窗。
- 授權 Cloud Shell 使用您的憑證發出 Google Cloud API 呼叫。
連線建立完成即代表已通過驗證,而且專案已設為您的 Project_ID:。輸出內容中有一行文字,宣告本工作階段的 Project_ID:
Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}
gcloud
是 Google Cloud 的指令列工具,已預先安裝於 Cloud Shell,並支援 Tab 鍵自動完成功能。
- (選用) 您可以執行下列指令來列出使用中的帳戶:
gcloud auth list
- 點按「授權」。
輸出內容:
ACTIVE: *
ACCOUNT: {{{user_0.username | "ACCOUNT"}}}
To set the active account, run:
$ gcloud config set account `ACCOUNT`
- (選用) 您可以使用下列指令來列出專案 ID:
gcloud config list project
輸出內容:
[core]
project = {{{project_0.project_id | "PROJECT_ID"}}}
注意:如需 gcloud
的完整說明,請前往 Google Cloud 參閱 gcloud CLI 總覽指南。
工作 1:設定環境
- 請先開啟 Cloud Shell,並設定幾項環境變數:
export GOOGLE_PROJECT=$DEVSHELL_PROJECT_ID
export CAI_BUCKET_NAME=cai-$GOOGLE_PROJECT
- 閱讀說明文件之後,您瞭解 CFT Scorecard 有兩項依附元件:
- Cloud Asset Inventory
- 政策庫
- 接著,請在專案啟用 Cloud Asset API:
gcloud services enable cloudasset.googleapis.com \
--project $GOOGLE_PROJECT
- 執行下列指令,建立預設的 Cloud Asset 服務帳戶:
gcloud beta services identity create --service=cloudasset.googleapis.com --project=$GOOGLE_PROJECT
- 將儲存空間管理員角色授予雲端資產服務帳戶:
gcloud projects add-iam-policy-binding ${GOOGLE_PROJECT} \
--member=serviceAccount:service-$(gcloud projects list --filter="$GOOGLE_PROJECT" --format="value(PROJECT_NUMBER)")@gcp-sa-cloudasset.iam.gserviceaccount.com \
--role=roles/storage.admin
- 複製政策庫:
git clone https://github.com/forseti-security/policy-library.git
- 您發現政策庫會強制執行 policy-library/policies/constraints 資料夾內的政策,因此您可以將範例政策從範例目錄複製到限制目錄。
cp policy-library/samples/storage_denylist_public.yaml policy-library/policies/constraints/
- 建立 bucket 來存放 Cloud Asset Inventory (CAI) 匯出的資料:
gsutil mb -l {{{project_0.default_region | REGION}}} -p $GOOGLE_PROJECT gs://$CAI_BUCKET_NAME
點選「Check my progress」,確認目標已達成。
建立 CAI bucket
工作 2:使用 Cloud Asset Inventory (CAI) 收集資料
環境設定完畢之後,即可開始為 CFT Scorecard 收集資料。
如前文所述,CFT Scorecard 的輸入內容為資源和 IAM 資料,以及政策庫資料夾。
您必須使用 CAI,為專案產生資源和 IAM 政策資訊。
- 請使用下列指令建立資料:
# Export resource data
gcloud asset export \
--output-path=gs://$CAI_BUCKET_NAME/resource_inventory.json \
--content-type=resource \
--project=$GOOGLE_PROJECT
# Export IAM data
gcloud asset export \
--output-path=gs://$CAI_BUCKET_NAME/iam_inventory.json \
--content-type=iam-policy \
--project=$GOOGLE_PROJECT
# Export org policy data
gcloud asset export \
--output-path=gs://$CAI_BUCKET_NAME/org_policy_inventory.json \
--content-type=org-policy \
--project=$GOOGLE_PROJECT
# Export access policy data
gcloud asset export \
--output-path=gs://$CAI_BUCKET_NAME/access_policy_inventory.json \
--content-type=access-policy \
--project=$GOOGLE_PROJECT
輸出內容範例:
Export in progress for root asset [projects/qwiklabs-gcp-01-68169ed6dd00].
Use [gcloud asset operations describe projects/97186664469/operations/ExportAssets/RESOURCE/2295255602305764396] to check the status of the operation.
Export in progress for root asset [projects/qwiklabs-gcp-01-68169ed6dd00].
Use [gcloud asset operations describe projects/97186664469/operations/ExportAssets/IAM_POLICY/11771734913762837428] to check the status of the operation.
- 確認 CAI 已完成資料收集。查看先前指令的輸出內容,並使用上述指令輸出內容中的
gcloud asset operations describe
確認作業已完成。檢查進度可能需要一些時間。
點選「Check my progress」,確認目標已達成。
確認 CAI 檔案已建立完畢
工作 3:使用 CFT Scorecard 分析 CAI 資料
- 請下載 CFT Scorecard 應用程式,並設為可執行:
curl -o cft https://storage.googleapis.com/cft-cli/latest/cft-linux-amd64
# make executable
chmod +x cft
- 現在一切都設定完畢,請執行 CFT Scorecard 應用程式:
./cft scorecard --policy-path=policy-library/ --bucket=$CAI_BUCKET_NAME
輸出內容範例:
Generating CFT scorecard
1 total issues found
Operational Efficiency: 0 issues found
----------
Security: 1 issues found
----------
denylist_public_users: 1 issues
- //storage.googleapis.com/fun-bucket-qwiklabs-gcp-00-2d8ed2a5cc0e is publicly accessable
Reliability: 0 issues found
----------
Other: 0 issues found
----------
這是您稍早發現的公開 bucket。
工作 4:在 CFT Scorecard 新增更多限制條件
- 別忘了 IAM!除了許可清單中的使用者之外,您也需要完整掌握誰具備
roles/owner
角色,因此請新增下列限制:
# Add a new policy to blacklist the IAM Owner Role
cat > policy-library/policies/constraints/iam_allowlist_owner.yaml << EOF
apiVersion: constraints.gatekeeper.sh/v1alpha1
kind: GCPIAMAllowedBindingsConstraintV3
metadata:
name: allowlist_owner
annotations:
description: List any users granted Owner
spec:
severity: high
match:
target: ["organizations/**"]
exclude: []
parameters:
mode: allowlist
assetType: cloudresourcemanager.googleapis.com/Project
role: roles/owner
members:
- "serviceAccount:admiral@qwiklabs-services-prod.iam.gserviceaccount.com"
EOF
- 再次執行 CFT Scorecard:
./cft scorecard --policy-path=policy-library/ --bucket=$CAI_BUCKET_NAME
看起來沒問題,但還是檢查一下 roles/editor
。
- 請額外設定兩項變數,以便建立新的限制:
export USER_ACCOUNT="$(gcloud config get-value core/account)"
export PROJECT_NUMBER=$(gcloud projects describe $GOOGLE_PROJECT --format="get(projectNumber)")
- 建立下列限制,將各個有效的帳戶加入許可清單:
# Add a new policy to allowlist the IAM Editor Role
cat > policy-library/policies/constraints/iam_identify_outside_editors.yaml << EOF
apiVersion: constraints.gatekeeper.sh/v1alpha1
kind: GCPIAMAllowedBindingsConstraintV3
metadata:
name: identify_outside_editors
annotations:
description: list any users outside the organization granted Editor
spec:
severity: high
match:
target: ["organizations/**"]
exclude: []
parameters:
mode: allowlist
assetType: cloudresourcemanager.googleapis.com/Project
role: roles/editor
members:
- "user:$USER_ACCOUNT"
- "serviceAccount:**$PROJECT_NUMBER**gserviceaccount.com"
- "serviceAccount:$GOOGLE_PROJECT**gserviceaccount.com"
EOF
- 再次執行 CFT Scorecard:
./cft scorecard --policy-path=policy-library/ --bucket=$CAI_BUCKET_NAME
輸出內容範例:
Generating CFT scorecard
3 total issues found
Reliability: 0 issues found
----------
Other: 2 issues found
----------
identify_outside_editors: 1 issues
- IAM policy for //cloudresourcemanager.googleapis.com/projects/1044418630080 grants roles/editor to user:qwiklabs.lab.user@gmail.com
Operational Efficiency: 0 issues found
----------
Security: 1 issues found
----------
denylist_public_users: 1 issues
- //storage.googleapis.com/fun-bucket-qwiklabs-gcp-00-2d8ed2a5cc0e is publicly accessable
此時,您應該會發現一位組織外的編輯者。該與莉莉和阿超談談了!
恭喜!
您已學會如何下載、設定及使用 CFT Scorecard,分析 Google Cloud 專案。
Google Cloud 教育訓練與認證
協助您瞭解如何充分運用 Google Cloud 的技術。我們的課程會介紹專業技能和最佳做法,讓您可以快速掌握要領並持續進修。我們提供從基本到進階等級的訓練課程,並有隨選、線上和虛擬課程等選項,方便您抽空參加。認證可協助您驗證及證明自己在 Google Cloud 技術方面的技能和專業知識。
使用手冊上次更新日期:2024 年 4 月 12 日
實驗室上次測試日期:2024 年 4 月 12 日
Copyright 2025 Google LLC 保留所有權利。Google 和 Google 標誌是 Google LLC 的商標,其他公司和產品名稱則有可能是其關聯公司的商標。