עדכון מדיניות של Binary Authorization

במאמר הזה מוסבר איך להחריג תמונות בכללי מדיניות של Binary Authorization.

במסמך הזה, מעדכנים את המדיניות כדי להחריג קובצי אימג' של קונטיינרים ב-Artifact Registry מאכיפת Binary Authorization, ומגדירים את כלל ברירת המחדל כך שלא תהיה אפשרות לפרוס את כל שאר הקונטיינרים.

לפני שמתחילים

  1. נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloud ליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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.enable permission. 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.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. 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.enable permission. 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.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init
  14. אם אתם לא משתמשים ב-Cloud Shell, אתם צריכים להתקין את kubectl.

יצירת אשכול עם Binary Authorization מופעל

יוצרים אשכול GKE עם Binary Authorization מופעל. זהו האשכול שבו רוצים להפעיל את קובצי האימג' של הקונטיינרים שפרסתם.

מסוף Google Cloud

  1. נכנסים לדף Clusters של GKE במסוףGoogle Cloud .

    מעבר אל GKE

    במסוף מוצגת רשימה של אשכולות GKE בפרויקט Google Cloud .

  2. לוחצים על Create Cluster (יצירת אשכול).

  3. מזינים test-cluster בשדה שם.

    שדה השם בתבנית האשכול הרגילה

  4. בוחרים באפשרות Zonal באפשרויות של Location Type.

  5. בתפריט הנפתח Zone, בוחרים באפשרות us-central1-a.

  6. בחלונית הניווט, בקטע Cluster (אשכול), לוחצים על Advanced settings (הגדרות מתקדמות).

  7. בשורה Binary authorization לוחצים על Edit configuration.

  8. בוחרים באפשרות Binary Authorization.

    הפעלת האפשרות Binary Authorization

  9. בוחרים באפשרות אכיפה בלבד ואז לוחצים על שמירה.

  10. לוחצים על יצירה.

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

כדי לראות את מדיניות ברירת המחדל:

  1. נכנסים לדף Binary Authorization במסוף Google Cloud .

    מעבר אל Binary Authorization

    פרטי המדיניות מוצגים במסוף.

  2. לוחצים על עריכת המדיניות.

  3. בקטע 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/<var>PROJECT_ID</var>/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"

ה-API בארכיטקטורת REST מחזיר את הפלט הבא:

{
  "name": "projects/PROJECT_ID/policy",
  "globalPolicyEvaluationMode": "ENABLE",
  "defaultAdmissionRule": {
    "evaluationMode": "ALWAYS_ALLOW",
    "enforcementMode": "ENFORCED_BLOCK_AND_AUDIT_LOG"
  }
}

אפשר להעלות רק תמונות שפטורות מאיסור

בקטע הזה מוסבר איך לשנות את המדיניות כדי להחריג תמונות ולא לאפשר פריסה של תמונות אחרות.

מסוף Google Cloud

כדי לשנות את המדיניות:

  1. חוזרים לדף Binary Authorization במסוף Google Cloud .

    מעבר אל Binary Authorization

  2. לוחצים על עריכת המדיניות.

  3. בוחרים באפשרות Disallow All Images (איסור שימוש בכל התמונות).

  4. בקטע Images exempt from deployment rules (תמונות שפטורות מכללי הפריסה), מרחיבים את Image paths (נתיבי תמונות).

  5. לוחצים על הוספת נתיבי תמונות.

    Artifact Registry

    בשדה נתיב תמונה חדש, מדביקים את הנתיב הבא ממאגר Artifact Registry:

    us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
    
  6. לוחצים על סיום כדי לשמור את נתיב התמונה.

  7. לוחצים על שמירת המדיניות.

gcloud

כדי לשנות את המדיניות כך שתאפשר גישה לתמונות לדוגמה מ-Artifact Registry, צריך לבצע את הפעולות הבאות:

  1. מייצאים את קובץ ה-YAML של המדיניות:

    gcloud container binauthz policy export  > /tmp/policy.yaml
    
  2. בכלי לעריכת טקסט, משנים את evaluationMode מ-ALWAYS_ALLOW ל-ALWAYS_DENY ומוסיפים תמונות שפטורות מאישור ל-admissionWhitelistPatterns.

    כדי להחריג את התמונות הבאות לדוגמה מ-Artifact Registry, משנים את קובץ ה-YAML של המדיניות באופן הבא:

    admissionWhitelistPatterns:
    - namePattern: us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
    globalPolicyEvaluationMode: ENABLE
    defaultAdmissionRule:
      evaluationMode: ALWAYS_DENY
      enforcementMode: ENFORCED_BLOCK_AND_AUDIT_LOG
    name: projects/<var>PROJECT_ID</var>/policy
    
  3. מייבאים את קובץ ה-YAML של המדיניות בחזרה אל Binary Authorization:

    gcloud container binauthz policy import /tmp/policy.yaml
    

‫API בארכיטקטורת REST

כדי לשנות את המדיניות:

  1. יוצרים קובץ טקסט עם המדיניות המעודכנת בפורמט JSON:

    cat > /tmp/policy.json << EOM
    {
      "name": "projects/${PROJECT_ID}/policy",
      "admissionWhitelistPatterns": [
        {
          "namePattern": "us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0"
        }
      ],
      "globalPolicyEvaluationMode": "ENABLE",
      "defaultAdmissionRule": {
        "evaluationMode": "ALWAYS_DENY",
        "enforcementMode": "ENFORCED_BLOCK_AND_AUDIT_LOG"
      }
    }
    EOM
    
  2. שולחים את המדיניות המעודכנת ל-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"
    

בדיקת המדיניות

כדי לבדוק את המדיניות על ידי פריסת תמונה שהוספתם לרשימת ההיתרים Exempt image, צריך:

  1. פריסת תמונה שפטורה מאימות באשכול.

    מסוף Google Cloud

    פריסת תמונה שפטורה מהכלל:

    1. נכנסים לדף Clusters של GKE במסוףGoogle Cloud .

      מעבר אל GKE

    2. לוחצים על פריסה.

      במסוף מוצגת בקשה להזנת פרטים על הפריסה.

    3. בוחרים באפשרות קובץ אימג' של קונטיינר קיים.

    4. מזינים את הנתיב של קובץ האימג' של הקונטיינר.

      Artifact Registry

      כדי לפרוס תמונה מ-Artifact Registry, מזינים את הפקודה הבאה:

      us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
      
    5. לוחצים על Continue.

    6. מזינים hello-server בשדה שם האפליקציה.

    7. לוחצים על פריסה.

    kubectl

    Artifact Registry

    פריסת התמונה מ-Artifact Registry:

    kubectl run hello-server --image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 --port 8080
    
  2. מוודאים שהתמונה הורשתה לפריסה:

    מסוף Google Cloud

    כדי לוודא שהתמונה נפרסה, מבצעים את הפעולות הבאות:

    1. עוברים לדף GKE.

    מעבר אל GKE

    1. עוברים לדף Workloads.

    עומס העבודה hello-server מופיע עם סמל ירוק, שמציין שהוא פועל.

    kubectl

    כדי לוודא שהתמונה הורשתה לפריסה, מזינים את הפקודה הבאה:

    kubectl get pods
    

    התמונה תפעל.

  3. מוחקים את ה-pod.

    מסוף Google Cloud

    בדף Workloads ב-GKE:

    1. בוחרים את עומס העבודה hello-server.

    2. לוחצים על Delete.

    3. כשמוצגת בקשה למחיקת משאבים, לוחצים על מחיקה.

    kubectl

    kubectl delete pod hello-server
    

הסרת המשאבים

כדי לא לצבור חיובים לחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה:

מוחקים את האשכול שיצרתם ב-GKE:

המסוף

כדי למחוק את האשכול:

  1. נכנסים לדף Clusters של GKE במסוףGoogle Cloud .

    מעבר אל GKE

  2. בוחרים את אשכול test-cluster ולוחצים על מחיקה.

gcloud

כדי למחוק את האשכול, מזינים את הפקודה הבאה:

gcloud container clusters delete \
    --zone=us-central1-a \
    test-cluster

המאמרים הבאים