שימוש בסריקה על פי דרישה כחלק מפייפליין Cloud Build מאפשר לכם לחסום גרסאות build אם בקובץ אימג' של קונטיינר יש נקודות חולשה ברמת חומרה שתואמת לרמה מוגדרת מראש.
במדריך הזה נלמד איך להשתמש ב-Cloud Build כדי ליצור קובץ אימג' של קונטיינר מקוד המקור, לסרוק אותו כדי לזהות פגיעויות, לבדוק את רמות החומרה של הפגיעויות ולהעביר את קובץ האימג' בדחיפה ל-Artifact Registry אם לא נמצאו פגיעויות ברמת חומרה ספציפית.
מומלץ ליצור Google Cloud פרויקט חדש לצורך המדריך הזה, ולבצע את השלבים בסביבה מבודדת.
מטרות
- יצירת קובץ אימג' באמצעות Cloud Build.
- סורקים את התמונה שנוצרה באמצעות סריקה לפי דרישה.
- הערכה של רמות נקודות החולשה המקובלות.
- אחסון התמונה ב-Artifact Registry.
עלויות
במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:
כדי להעריך את ההוצאות בהתאם לתחזית השימוש שלכם, אתם יכולים להיעזר במחשבון העלויות.
כשמסיימים את המשימות שמתוארות במסמך הזה אפשר למחוק את המשאבים שיצרתם כדי להימנע מחיובים נוספים. מידע נוסף זמין בקטע הסרת המשאבים.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the On-Demand Scanning, Cloud Build, and Artifact Registry APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the On-Demand Scanning, Cloud Build, and Artifact Registry APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
gcloud init
התפקידים הנדרשים
חשבון השירות שבו משתמשים ב-Cloud Build צריך לכלול את התפקידים הבאים:
אדמין של סריקה על פי דרישה (
roles/ondemandscanning.admin)בעל הרשאת כתיבה ב-Artifact Registry (
roles/artifactregistry.writer)
לחשבון השירות שמוגדר כברירת מחדל ב-Cloud Build יש את ההרשאות הנדרשות למאגרי Artifact Registry באותו פרויקט. אם המאגרים נמצאים באותו פרויקט שבו אתם משתמשים ב-Cloud Build, אתם צריכים להעניק רק את התפקיד On-Demand Scanning Admin (אדמין של סריקה לפי דרישה).
אם אתם משתמשים בחשבון שירות שסופק על ידי המשתמש עבור Cloud Build, אתם צריכים להעניק את שני התפקידים.
הכנת קובץ המקור
במדריך הזה תיצרו תמונה מקובץ Docker. קובץ Dockerfile הוא קובץ מקור שמכיל הוראות ל-Docker ליצירת אימג'.
פותחים חלון של Terminal, יוצרים ספרייה חדשה בשם
ods-tutorialועוברים אליה:mkdir ods-tutorial && cd ods-tutorialיוצרים קובץ בשם
Dockerfileעם התוכן הבא:# Debian10 image FROM gcr.io/google-appengine/debian10:latest # Ensures that the built image is always unique RUN apt-get update && apt-get -y install uuid-runtime && uuidgen > /IAMUNIQUE
יצירת מאגר Artifact Registry
מגדירים את מזהה הפרויקט לאותו פרויקט שבו הפעלתם את ממשקי ה-API:
gcloud config set project PROJECT_IDיוצרים מאגר Docker בשם
ods-build-repoבמיקוםus-central1:gcloud artifacts repositories create ods-build-repo --repository-format=docker \ --location=us-central1 --description="Repository for scan and build"מוודאים שהמאגר נוצר בהצלחה:
gcloud artifacts repositories list
יצירה וסריקה
בקטע הזה תריצו את צינור עיבוד הנתונים שלכם באמצעות קובץ הגדרות build. קובץ תצורת build מורה ל-Cloud Build איך לבצע כמה משימות על סמך המפרט שלכם.
בתיקייה
ods-tutorial/, יוצרים את הקובץcloudbuild.yamlעם התוכן הבא:steps: - id: build name: gcr.io/cloud-builders/docker entrypoint: /bin/bash args: - -c - | docker build -t us-central1-docker.pkg.dev/$_PROJECT_ID/ods-build-repo/ods-test:latest -f ./Dockerfile . && docker image inspect us-central1-docker.pkg.dev/$_PROJECT_ID/ods-build-repo/ods-test:latest --format \ '{{index .RepoTags 0}}@{{.Id}}' > /workspace/image-digest.txt && cat image-digest.txt - id: scan name: gcr.io/google.com/cloudsdktool/cloud-sdk entrypoint: /bin/bash args: - -c - | gcloud artifacts docker images scan us-central1-docker.pkg.dev/$_PROJECT_ID/ods-build-repo/ods-test:latest \ --format='value(response.scan)' > /workspace/scan_id.txt - id: severity check name: gcr.io/google.com/cloudsdktool/cloud-sdk entrypoint: /bin/bash args: - -c - | gcloud artifacts docker images list-vulnerabilities $(cat /workspace/scan_id.txt) \ --format='value(vulnerability.effectiveSeverity)' | if grep -Exq $_SEVERITY; \ then echo 'Failed vulnerability check' && exit 1; else exit 0; fi - id: push name: gcr.io/cloud-builders/docker entrypoint: /bin/bash args: - -c - | docker push us-central1-docker.pkg.dev/$_PROJECT_ID/ods-build-repo/ods-test:latest images: ['us-central1-docker.pkg.dev/$_PROJECT_ID/ods-build-repo/ods-test:latest']הקובץ הזה כולל את המיקום והמאגר שנוצרו קודם ב-Artifact Registry. אם מחליטים להשתמש בערכים שונים, צריך לשנות את הקובץ
cloudbuild.yamlבהתאם. הערכים שלPROJECT_IDושלSEVERITYמועברים לסקריפט בפקודת ה-build.מציינים את הרמות של הפגיעות
SEVERITYשרוצים לחסום ומתחילים את הבנייה.אפשר להשתמש בערכים הבאים עבור
SEVERITY:CRITICALHIGHMEDIUMLOW
אפשר לציין כמה רמות חומרה באמצעות ביטוי רגולרי.
בדוגמה הבאה, מציינים את ערכי החומרה
CRITICALו-HIGH. ההוראה הזו גורמת ל-Cloud Build לבדוק אם יש נקודות חולשה שמסווגות ברמת החומרהHIGHומעלה.gcloud builds submit --substitutions=_PROJECT_ID=PROJECT_ID,_SEVERITY='"CRITICAL|HIGH"' \ --config cloudbuild.yamlכאשר:
- PROJECT_ID הוא מזהה הפרויקט.