הגדרת מדיניות 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, if any are not already enabled.
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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
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, if any are not already enabled.
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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
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 , נכנסים לדף Kubernetes clusters ב-GKE:
לוחצים על יצירה.
בקטע Create an Autopilot cluster, מבצעים את הפעולות הבאות:
בשדה Name, מזינים
test-cluster.בתפריט Region, בוחרים באפשרות
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בשדה Deployment name (שם הפריסה).לוחצים על הבא: פרטי מאגר התגים.
בוחרים באפשרות קובץ אימג' של קונטיינר קיים.
מזינים את
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 .
לוחצים על Deploy.
במסוף מוצגת בקשה להזנת פרטים על הפריסה.
מזינים
hello-serverבשדה Deployment name (שם הפריסה).לוחצים על הבא: פרטי מאגר התגים.
בוחרים באפשרות קובץ אימג' של קונטיינר קיים.
מזינים את
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כדי לפרוס רק קובצי אימג' שנוצרו על ידי Cloud Build (תצוגה מקדימה). - מדריך מקיף בנושא דרישת אישורים זמין כאן:
- אפשר גם לקרוא את המשאבים שלנו בנושא DevOps ולעיין בתוכנית המחקר DevOps Research and Assessment (DORA).