הגדרת מדיניות הרשאות ב-sidecars ב-GKE
בדף הזה מוסבר איך להגדיר סוגים שונים של מדיניות הרשאות ב-sidecars של Cloud Service Mesh ב-GKE.
לפני שמתחילים
מומלץ להכיר את Gateway API ואת תוספי ההרשאות.
לפני שיוצרים מדיניות הרשאות, צריך לבצע את השלבים הבאים:
- נכנסים לחשבון 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 Network Security, Network Services 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.-
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 Network Security, Network Services 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.
מדיניות AuthzPolicy ברמת מרחב השמות
אם רוצים לדחות דפוס תנועה צפוי עבור כל מרחב השמות, צריך להגדיר מדיניות הרשאה לדחיית כל בקשות ה-HTTP הנכנסות לכל עומסי העבודה במרחב השמות:
cat >ns-authz-policy-deny.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
name: ns-authz
namespace: NAMESPACE
spec:
targetRefs:
- kind: Pod
selector: {}
rules:
- to:
operations:
- paths:
- type: Prefix
value: "/bad_token"
action: DENY
EOF
kubectl apply -f ns-authz-policy-deny.yaml
שימו לב: היעד הריק selector: {} מכוון לכל ה-Pods במרחב השמות.
אם רוצים לאפשר דפוס תנועה צפוי למרחב השמות כולו, צריך להגדיר מדיניות הרשאה שתאפשר לכל בקשות ה-HTTP הנכנסות לגשת לכל עומסי העבודה במרחב השמות:
cat >ns-authz-policy-allow.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
name: ns-authz
namespace: NAMESPACE
spec:
targetRefs:
- kind: Pod
selector: {}
rules:
- to:
operations:
- paths:
- type: Prefix
value: "/headers"
methods: ["GET"]
action: ALLOW
EOF
kubectl apply -f ns-authz-policy-allow.yaml
שימו לב: היעד הריק selector: {} מכוון לכל ה-Pods במרחב השמות.
דחיית בקשות נכנסות לעומס עבודה
אם יש לכם עומס עבודה שאמור לבצע רק שיחות יוצאות, כמו משימת cron, אתם יכולים להגדיר מדיניות הרשאה שתדחה כל בקשות HTTP נכנסות לעומס העבודה:
cat >deny-path-authz-policy.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
name: my-workload-authz
namespace: NAMESPACE
spec:
targetRefs:
- kind: Pod
selector:
matchLabels:
app: EXAMPLE_APP
rules:
- to:
operations:
- paths:
- type: Prefix
value: "/deny_path"
action: DENY
EOF
kubectl apply -f deny-path-authz-policy.yaml
הפלט אמור להיראות כך:
gcpauthzpolicy.networking.gke.io/my-workload-authz created
אחרי החלת המדיניות הזו, כל בקשת HTTP נכנסת לנתיב /deny_path באפליקציה התואמת של Pods: EXAMPLE_APP תידחה והמתקשר יקבל קוד תגובה מסוג HTTP 403 Forbidden.
אישור בקשות נכנסות ספציפיות לעומס עבודה
אפשר גם להגדיר ALLOW מדיניות שמאשרת רק בקשות שתואמות לקריטריונים ספציפיים, ודוחה את השאר.
בדוגמה הבאה מוגדרת מדיניות הרשאות בפריסת example-app כדי לאפשר רק בקשות mTLS מ-Pods עם הזהות spiffe://cluster.local/namespace/pod1.
cat >allow-authz-policy.yaml <<EOF
apiVersion: networking.gke.io/v1
kind: GCPAuthzPolicy
metadata:
name: my-workload-authz
namespace: NAMESPACE
spec:
targetRefs:
- kind: Pod
selector:
matchLabels:
app: EXAMPLE_APP
rules:
- from:
sources:
- principals:
- principal:
exact: "spiffe://cluster.local/NAMESPACE/pod1"
action: ALLOW
EOF
kubectl apply -f allow-authz-policy.yaml
הפלט אמור להיראות כך:
gcpauthzpolicy.networking.gke.io/my-workload-authz created
רק בקשות נכנסות שאומתו באמצעות mTLS עם הזהות המדויקת של SPIFFE spiffe://cluster.local/NAMESPACE/pod1 יקבלו HTTP 200 OK. כל בקשה אחרת תידחה על ידי ה-proxy עם שגיאת HTTP 403 Forbidden.
העברה של הרשאות למנוע הרשאות חיצוני
אתם יכולים להשתמש במנוע הרשאות משלכם ולהגדיר את Cloud Service Mesh כך שיעביר את כל ההחלטות לגבי הרשאות של עומס עבודה למנוע שהגדרתם.
אם CRD עדיין לא מותקן, מתקינים אותו:
GCPAuthzExtensionkubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/gke-gateway-api/main/config/crd/networking.gke.io_gcpauthzextensions.yamlפריסת עומס עבודה של תוסף הרשאה מבוסס-קריאה ב-Kubernetes עם תמיכה בפרוטוקול
ext_proc gRPCוהגדרת משאב של תוסף הרשאה:cat >authz-extension.yaml <<EOF apiVersion: networking.gke.io/v1 kind: GCPAuthzExtension metadata: name: my-authz-ext namespace: ns1 spec: backendRef: kind: Service name: authz-service loadBalancingScheme: INTERNAL_SELF_MANAGED forwardHeaders: - Authorization failOpen: false timeout: "0.1s" wireFormat: EXT_PROC_GRPC EOF kubectl apply -f authz-extension.yamlהפקודה הזו יוצרת תוסף הרשאה מבוסס-callout שפועל כשירות Kubernetes
authz-service.הפלט אמור להיראות כך:
gcpauthzextension.networking.gke.io/my-authz-ext createdמגדירים את מדיניות ההרשאות כדי להעביר את ההחלטות לגבי הרשאות לעומס העבודה לתוסף ההרשאות שהוגדר קודם:
cat >authz-policy.yaml <<EOF apiVersion: networking.gke.io/v1 kind: GCPAuthzPolicy metadata: name: my-workload-authz namespace: NAMESPACE spec: targetRefs: - kind: Pod selector: matchLabels: app: EXAMPLE_APP rules: - from: sources: - principals: - principal: exact: "spiffe://cluster.local/NAMESPACE/pod1" to: operations: - paths: - type: Exact value: "/api/payments-schedule" action: CUSTOM customProvider: authzExtension: targetRefs: - kind: GCPAuthzExtension name: my-authz-ext EOF kubectl apply -f authz-policy.yamlהפלט אמור להיראות כך:
gcpauthzpolicy.networking.gke.io/my-workload-authz createdהפרוקסי ימתין עד שהשירות המותאם אישית יחזיר תשובה מסוג
OKאוDENYלפני שיעביר את הבקשה לאפליקציה או יחזיר קוד HTTP 403 למתקשר.