הגדרת מדיניות Binary Authorization ב-GKE
במדריך למתחילים הזה נסביר איך להגדיר ולבדוק כלל בסיסי במדיניות של Binary Authorization.
במדריך למתחילים הזה, נראה לכם איך לצפות בכלל ברירת המחדל במדיניות ואיך להגדיר אותו. הכלל שמוגדר כברירת מחדל מאפשר פריסה של כל התמונות. כדי לבדוק את זה, פורסים קובץ אימג' של קונטיינר באשכול Google Kubernetes Engine (GKE). לאחר מכן מגדירים את כלל ברירת המחדל כך שלא תהיה אפשרות לפרוס תמונות, ומנסים לפרוס תמונה.
לפני שמתחילים
- נכנסים לחשבון 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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry, Binary Authorization APIs.
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.-
התקינו את ה-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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry, Binary Authorization APIs.
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.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
gcloud init - מתקינים את
kubectl.
התפקידים הנדרשים
כדי לקבל את ההרשאות שדרושות להגדרה של מדיניות Binary Authorization באמצעות GKE, צריך לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים בפרויקט:
- עורך מדיניות Binary Authorization (
roles/binaryauthorization.policyEditor) - צפייה ב-Compute (
roles/compute.viewer) - אדמין ב-Kubernetes Engine (
roles/container.admin) - משתמש בחשבון שירות (
roles/iam.serviceAccountUser)
להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.
יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.
יצירת אשכול עם אכיפה של Binary Authorization
עכשיו יוצרים אשכול GKE עם Binary Authorization מופעל. זהו האשכול שבו רוצים להפעיל את קובצי האימג' של הקונטיינרים שפרסתם.
התכונה Binary Authorization פועלת עם אשכולות במצב Autopilot או במצב Standard.
מסוף Google Cloud
בשלבים הבאים מוסבר איך להגדיר אשכול Autopilot.
במסוף Google Cloud , עוברים לדף GKE Kubernetes clusters:
לוחצים על יצירה.
בקטע Create an Autopilot cluster, מבצעים את הפעולות הבאות:
בשדה שם מזינים
test-cluster.בתפריט אזור, בוחרים באפשרות
us-central1.מרחיבים את הקטע הגדרות מתקדמות.
לוחצים על הקישור אבטחה כדי להציג את החלונית אבטחה.
בחלונית אבטחה, מסמנים את התיבה הפעלת Binary Authorization.
בוחרים באפשרות אכיפה בלבד.
לוחצים על הבא ואז על הבא:בדיקה ויצירה.
כדי להתחיל ליצור את האשכול, לוחצים על Create.
gcloud
מריצים את הפקודה gcloud container clusters create עם הסימון --binauthz-evaluation-mode=PROJECT_SINGLETON_POLICY_ENFORCE.
gcloud container clusters create \
--binauthz-evaluation-mode=PROJECT_SINGLETON_POLICY_ENFORCE \
--zone us-central1-a \
test-cluster
יצירת אשכול יכולה להימשך כמה דקות.
מדיניות ברירת המחדל
כברירת מחדל, מדיניות Binary Authorization מוגדרת כך שכל קובצי האימג' של קונטיינרים יכולים להיפרס.
מסוף Google Cloud
כדי לראות את מדיניות ברירת המחדל:
נכנסים לדף Binary Authorization במסוף Google Cloud .
פרטי המדיניות מוצגים במסוף.
לוחצים על עריכת המדיניות.
בקטע Project Default Rule, האפשרות Allow All Images מסומנת.
gcloud
כדי לראות את מדיניות ברירת המחדל, מייצאים את קובץ ה-YAML של המדיניות באופן הבא:
gcloud container binauthz policy export
כברירת מחדל, הקובץ מכיל את התוכן הבא:
globalPolicyEvaluationMode: ENABLE defaultAdmissionRule: evaluationMode: ALWAYS_ALLOW enforcementMode: ENFORCED_BLOCK_AND_AUDIT_LOG name: projects/PROJECT_ID/policy
API בארכיטקטורת REST
כדי לראות את מדיניות ברירת המחדל, מאחזרים אותה בפורמט JSON באופן הבא:
curl \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "x-goog-user-project: ${PROJECT_ID}" \
"https://binaryauthorization.googleapis.com/v1/projects/${PROJECT_ID}/policy"
הפלט של הפקודה אמור להיראות כך:
{
"name": "projects/PROJECT_ID/policy",
"globalPolicyEvaluationMode": "ENABLE",
"defaultAdmissionRule": {
"evaluationMode": "ALWAYS_ALLOW",
"enforcementMode": "ENFORCED_BLOCK_AND_AUDIT_LOG"
}
}
בדיקה של מדיניות האכיפה
כדי לבדוק את מדיניות האכיפה, אפשר לנסות לפרוס קובץ אימג' לדוגמה של קונטיינר באשכול.
במדריך למתחילים הזה משתמשים בקובץ אימג' לדוגמה של קונטיינר שנמצא בנתיב us-docker.pkg.dev/google-samples/containers/gke/hello-app ב-Artifact Registry. זוהי קובץ אימג' של קונטיינר ציבורי שנוצרה על ידי Google ומכילה אפליקציה לדוגמה של 'Hello, World!'.
מסוף Google Cloud
כדי לבדוק את המדיניות:
נכנסים לדף Clusters של GKE במסוףGoogle Cloud .
לוחצים על פריסה.
במסוף מוצגת בקשה להזנת פרטים על הפריסה.
מזינים
hello-serverבשדה שם הפריסה.לוחצים על הבא: פרטי מאגר התגים.
בוחרים באפשרות Existing Container Image (תמונת מאגר תגים קיימת).
מזינים את
us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0כנתיב של קובץ אימג' של קונטיינר.לוחצים על פריסה.
kubectl
כדי לבדוק את המדיניות:
מעדכנים את הקובץ המקומי
kubeconfig:gcloud container clusters get-credentials \ --zone us-central1-a \ test-clusterהפעולה הזו מספקת את פרטי הכניסה ופרטי נקודת הקצה שנדרשים כדי לגשת לאוסף ב-GKE.
פריסת התמונה:
kubectl run hello-server --image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 --port 8080
עכשיו צריך לוודא ש-Binary Authorization איפשר את הפריסה.
מסוף Google Cloud
כדי לוודא שהתמונה נפרסה, עוברים לדף Workloads ב-GKE במסוף Google Cloud .
עומס העבודה של הפריסה מופיע עם סמל ירוק שמציין שהתמונה נפרסה בהצלחה.
kubectl
כדי לוודא שהתמונה נפרסה, מבצעים את הפעולות הבאות:
kubectl get pods
הפקודה תציג הודעה דומה להודעה הבאה, שמציינת שהפריסה בוצעה בהצלחה:
NAME READY STATUS RESTARTS AGE hello-server-579859fb5b-h2k8s 1/1 Running 0 1m
חשוב למחוק את הפריסה כדי להמשיך לשלב הבא:
מסוף Google Cloud
כדי למחוק את הפריסה:
חוזרים לדף Workloads ב-GKE במסוףGoogle Cloud .
בוחרים את עומס העבודה
hello-server.לוחצים על Delete.
kubectl
כדי למחוק את הפריסה:
kubectl delete deployment hello-server
הגדרת מדיניות האכיפה כך שלא תאפשר הצגה של תמונות
עכשיו משנים את המדיניות כך שהמערכת תחסום את הפריסה של כל התמונות במקום לאפשר אותה.
מסוף Google Cloud
כדי לשנות את המדיניות:
חוזרים לדף Binary Authorization במסוף Google Cloud .
לוחצים על עריכת המדיניות.
בוחרים באפשרות Disallow All Images (איסור שימוש בכל התמונות).
לוחצים על שמירת המדיניות.
gcloud
כדי לשנות את המדיניות:
מייצאים את קובץ ה-YAML של המדיניות:
gcloud container binauthz policy export > /tmp/policy.yaml
בכלי לעריכת טקסט, משנים את
evaluationModeמ-ALWAYS_ALLOWל-ALWAYS_DENY.קובץ ה-YAML של המדיניות צריך להיראות כך:
globalPolicyEvaluationMode: ENABLE defaultAdmissionRule: evaluationMode: ALWAYS_DENY enforcementMode: ENFORCED_BLOCK_AND_AUDIT_LOG name: projects/PROJECT_ID/policy
מייבאים את קובץ ה-YAML של המדיניות בחזרה ל-Binary Authorization:
gcloud container binauthz policy import /tmp/policy.yaml
API בארכיטקטורת REST
כדי לשנות את המדיניות:
יוצרים קובץ טקסט עם המדיניות המעודכנת בפורמט JSON:
cat > /tmp/policy.json << EOM { "name": "projects/${PROJECT_ID}/policy", "globalPolicyEvaluationMode": "ENABLE", "defaultAdmissionRule": { "evaluationMode": "ALWAYS_DENY", "enforcementMode": "ENFORCED_BLOCK_AND_AUDIT_LOG" } } EOMשולחים את המדיניות המעודכנת אל ה-API בארכיטקטורת REST:
curl -X PUT \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ -H "x-goog-user-project: ${PROJECT_ID}" \ --data-binary @/tmp/policy.json \ "https://binaryauthorization.googleapis.com/v1/projects/${PROJECT_ID}/policy"
בדיקה מחדש של המדיניות
שוב, בודקים את המדיניות על ידי פריסת קובץ אימג' של קונטיינר לדוגמה באשכול. במקרה הזה, Binary Authorization חוסם את הפריסה של התמונה.
מסוף Google Cloud
פריסת התמונה:
נכנסים לדף Clusters של GKE במסוףGoogle Cloud .
לוחצים על פריסה.
במסוף מוצגת בקשה להזנת פרטים על הפריסה.
מזינים
hello-serverבשדה שם הפריסה.לוחצים על הבא: פרטי מאגר התגים.
בוחרים באפשרות Existing Container Image (תמונת מאגר תגים קיימת).
מזינים את
us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0כנתיב של קובץ אימג' של קונטיינר.לוחצים על פריסה.
kubectl
פריסת התמונה:
kubectl run hello-server --image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 --port 8080
עכשיו אפשר לוודא שהמדיניות נחסמה:
מסוף Google Cloud
כדי לוודא שהתמונה לא נפרסה:
חוזרים לדף Workloads ב-GKE במסוףGoogle Cloud .
עומס העבודה של קובץ אימג' של קונטיינר מופיע עם סמל אדום שמציין שהפריסה של התמונה נכשלה.
kubectl
כדי לוודא שהתמונה לא נפרסה, מריצים את הפקודה הבאה:
kubectl get pods
הפקודה מדפיסה את ההודעה הבאה, שמציינת שהתמונה לא נפרסה:
No resources found.
אפשר לקבל פרטים נוספים על הפריסה:
kubectl get event --template \
'{{range.items}}{{"\033[0;36m"}}{{.reason}}:{{"\033[0m"}}{{.message}}{{"\n"}}{{end}}'
תופיע תגובה שדומה לתגובה הבאה:
FailedCreate: Error creating: pods POD_NAME is forbidden: admission webhook "imagepolicywebhook.image-policy.k8s.io" denied the request: Image IMAGE_NAME denied by Binary Authorization default admission rule. Denied by always_deny admission rule
בפלט הזה:
- POD_NAME: שם ה-Pod.
- IMAGE_NAME: שם התמונה.
- ATTESTOR_NAME: השם של הגורם המאמת.
הסרת המשאבים
כדי לא לצבור חיובים לחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, פועלים לפי השלבים הבאים:
מוחקים את האשכול שיצרתם ב-GKE:
המסוף
כדי למחוק את האשכול:
נכנסים לדף Clusters של GKE במסוףGoogle Cloud .
בוחרים את אשכול
test-clusterולוחצים על מחיקה.
gcloud
כדי למחוק את האשכול:
gcloud container clusters delete \
--zone=us-central1-a \
test-cluster
המאמרים הבאים
- שימוש ב
built-by-cloud-buildגורם מאמת (attestor) כדי לפרוס רק קובצי אימג' שנוצרו על ידי Cloud Build (גרסת טרום-השקה (Preview)). - מדריך מקיף בנושא דרישת אישורים:
- אפשר גם לקרוא את המשאבים שלנו בנושא DevOps ולעיין בתוכנית המחקר DevOps Research and Assessment (DORA).