אתם יכולים לכתוב תצורת build שמורה ל-Cloud Build לאמת את התשתית כקוד (IaC) שמהווה חלק מה-build. אימות של IaC מאפשר לכם לקבוע אם הגדרות המשאבים של Terraform מפרות את מדיניות הארגון הקיימת ואת גלאי Security Health Analytics שמוחלים על המשאבים שלכם ב- Google Cloud .
מידע נוסף על אימות IaC זמין במאמר בנושא אימות IaC בהתאם למדיניות של Google Cloud הארגון.
לפני שמתחילים
כדי להתחיל להשתמש באימות IaC באמצעות Cloud Build, צריך להשלים את המשימות הבאות.
הפעלת רמת Premium או רמת Enterprise של Security Command Center
מוודאים שמהדורת Premium או Enterprise של Security Command Center מופעלת ברמת הארגון.
הפעלת Security Command Center מפעילה את ממשקי ה-API securityposture.googleapis.com ו-securitycentermanagement.googleapis.com.
הגדרת ההרשאות
-
Make sure that you have the following role or roles on the organization:
- Security Posture Shift-Left Validator
- Log Writer
- Storage Writer
- Storage Reader
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the organization.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
כניסה לדף IAM - בוחרים את הארגון.
- לוחצים על Grant access.
-
בשדה New principals, מזינים את מזהה המשתמש. בדרך כלל מזהה המשתמש הוא כתובת האימייל של חשבון Google.
- לוחצים על בחירת תפקיד ומחפשים את התפקיד.
- כדי לתת עוד תפקידים, לוחצים על Add another role ומוסיפים את כולם.
- לוחצים על Save.
מידע נוסף על הרשאות אימות של IaC זמין במאמר IAM להפעלות ברמת הארגון.
הפעלת Cloud Build API
-
Enable the Cloud Build API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
הגדרת כללי המדיניות
הגדרת מדיניות ארגונית וגלאים של Security Health Analytics. כדי להגדיר את המדיניות הזו באמצעות מצב אבטחה, צריך לבצע את המשימות שבמאמר יצירה ופריסה של מצב אבטחה.
יצירת קוד Terraform
הוראות מפורטות מופיעות במאמר יצירת קוד Terraform.
אימות ה-IAC ב-Cloud Build
מוסיפים את המשימות הבאות לקובץ
cloudbuild.yaml:מאתחלים את Terraform:
- name: hashicorp/terraform args: - '-c' - | terraform init \ -backend-config="bucket=STATE_BUCKET" \ -backend-config="prefix=REPOSITORY_NAME" \ dir: FOLDER id: Terraform Init entrypoint: shמחליפים את מה שכתוב בשדות הבאים:
-
STATE_BUCKETעם שם הקטגוריה של Cloud Storage שבה רוצים לאחסן את מצב Terraform -
REPOSITORY_NAMEבשם המאגר שמארח את קוד Terraform. -
FOLDERבשם התיקייה שבה רוצים לשמור את הארטיפקטים של Terraform.
-
יוצרים קובץ תוכנית:
- name: hashicorp/terraform args: - '-c' - | terraform plan -out tf.plan dir: FOLDER id: Terraform Plan entrypoint: shממירים את קובץ התוכנית לפורמט JSON:
- name: hashicorp/terraform args: - '-c' - | terraform show -json tf.plan > plan.json dir: FOLDER id: Terraform Show entrypoint: shיוצרים את דוח האימות של IaC:
- name: gcr.io/cloud-builders/gcloud args: - '-c' - | gcloud scc iac-validation-reports create \ organizations/ORGANIZATION_ID/locations/global --tf-plan-file=plan.json \ --format="json(response.iacValidationReport)" > IaCScanReport_$BUILD_ID.json dir: FOLDER id: Run IaC scan entrypoint: /bin/bashמחליפים את
ORGANIZATION_IDבמזהה הארגון.אם אתם משתמשים ב-Cloud Storage, אתם יכולים להעלות את קובץ התוצאות בפורמט JSON ל-Cloud Storage:
- name: gcr.io/cloud-builders/gcloud args: - storage - cp - IaCScanReport_$BUILD_ID.json - SCAN_RESULT_FILE_BUCKET dir: FOLDER id: Upload report fileמחליפים את הערך ב-
SCAN_RESULT_FILE_BUCKETבקטגוריה של Cloud Storage שאליה רוצים להעלות את קובץ התוצאות.כדי לראות את התוצאות בפורמט SARIF, מבצעים את הפעולות הבאות:
ממירים את הקובץ:
- name: golang args: - '-c' - | go run github.com/google/gcp-scc-iac-validation-utils/SARIFConverter@latest \ --inputFilePath=IaCScanReport_$BUILD_ID.json --outputFilePath=IaCScanReport_$BUILD_ID.sarif.json dir: FOLDER id: Convert to SARIF format entrypoint: /bin/bashאופציונלי: מעלים את הקובץ ל-Cloud Storage:
- name: gcr.io/cloud-builders/gcloud args: - storage - cp - IaCScanReport_$BUILD_ID.sarif.json - SCAN_RESULT_FILE_BUCKET dir: FOLDER id: Upload report file
מאמתים את התוצאות. משלימים את השלב הזה בקובץ ה-JSON של התוצאות שלא הומר לפורמט SARIF:
- name: golang args: - '-c' - | go run github.com/google/gcp-scc-iac-validation-utils/ReportValidator@latest \ --inputFilePath=IaCScanReport_$BUILD_ID.json --failure_expression=FAILURE_CRITERIA dir: FOLDER id: Validate results entrypoint: /bin/bashמחליפים את
FAILURE_CRITERIAבקריטריון של סף הכשל שקובע מתי ה-build נכשל. קריטריון הסף מבוסס על מספר הבעיות ברמות החומרה הקריטית, הגבוהה, הבינונית והנמוכה שמתגלות בסריקת האימות של IaC.FAILURE_CRITERIAמציין כמה בעיות מכל רמת חומרה מותרות, וגם מציין איך הבעיות מצטברות (ANDאוOR). לדוגמה, אם רוצים שה-build ייכשל אם תזוהה בעיה קריטית אחת או בעיה אחת ברמת חומרה גבוהה, צריך להגדיר אתFAILURE_CRITERIAלערךCritical:1,High:1,Operator:OR. ערך ברירת המחדל הואCritical:1,High:1,Medium:1,Low:1,Operator:OR, כלומר אם הסריקה של IaC לאימות נתקלת בהפרה בכל רמת חומרה, ה-build חייב להיכשל.אם ה-build נכשל, פותרים את ההפרות בקוד Terraform.
המאמרים הבאים
- איך צופים בדוח האימות של IaC ב-Cloud Storage
- מעיינים בסקריפטים לאימות IaC ב-GitHub.
- מעיינים בדוגמה של
cloud.yaml.