Menyiapkan kebijakan otorisasi di sidecar di GKE
Halaman ini memberikan petunjuk untuk menyiapkan berbagai jenis kebijakan otorisasi di sidecar Cloud Service Mesh di GKE.
Sebelum memulai
Anda harus memahami Gateway API dan Ekstensi Otorisasi.
Sebelum membuat kebijakan otorisasi, Anda harus melakukan langkah-langkah berikut:
- Login keakun Anda. Google Cloud Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
-
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 di seluruh Namespace
Jika Anda ingin menolak pola traffic yang diharapkan untuk seluruh namespace, konfigurasikan kebijakan otorisasi untuk menolak permintaan HTTP masuk ke semua workload di namespace:
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
Perhatikan bahwa selector: {} kosong menargetkan semua Pod di namespace.
Jika Anda ingin mengizinkan pola traffic yang diharapkan untuk seluruh namespace, konfigurasikan kebijakan otorisasi untuk mengizinkan permintaan HTTP masuk ke semua workload di namespace:
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
Perhatikan bahwa selector: {} kosong menargetkan semua Pod di namespace.
Menolak permintaan masuk ke workload
Jika Anda memiliki workload yang hanya boleh melakukan panggilan keluar, seperti tugas cron, konfigurasikan kebijakan otorisasi untuk menolak permintaan HTTP masuk ke workload:
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
Outputnya mirip dengan:
gcpauthzpolicy.networking.gke.io/my-workload-authz created
Setelah kebijakan ini diterapkan, setiap permintaan HTTP masuk ke jalur /deny_path di Pod yang cocok dengan aplikasi: EXAMPLE_APP akan ditolak dan pemanggil akan menerima kode respons HTTP 403 Forbidden.
Mengizinkan permintaan masuk tertentu ke workload
Anda juga dapat mengonfigurasi kebijakan ALLOW yang hanya mengizinkan permintaan yang cocok dengan kriteria tertentu, sekaligus menolak permintaan lainnya.
Contoh berikut mengonfigurasi kebijakan otorisasi pada Deployment example-app untuk hanya mengizinkan permintaan mTLS dari Pod dengan identitas 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
Outputnya mirip dengan:
gcpauthzpolicy.networking.gke.io/my-workload-authz created
Hanya permintaan masuk yang diautentikasi menggunakan mTLS yang menampilkan identitas SPIFFE yang sama persis spiffe://cluster.local/NAMESPACE/pod1 yang akan menerima HTTP 200 OK. Permintaan lainnya akan ditolak oleh proxy dengan HTTP 403 Forbidden.
Mendelegasikan ke mesin otorisasi eksternal
Anda dapat menggunakan mesin otorisasi sendiri dan mengonfigurasi Cloud Service Mesh untuk mendelegasikan semua keputusan otorisasi untuk workload ke mesin yang dikonfigurasi.
Instal CRD
GCPAuthzExtensionjika belum diinstal:kubectl apply -f https://raw.githubusercontent.com/GoogleCloudPlatform/gke-gateway-api/main/config/crd/networking.gke.io_gcpauthzextensions.yamlDeploy workload ekstensi otorisasi berbasis callout di Kubernetes yang mendukung protokol
ext_proc gRPCdan mengonfigurasi resource ekstensi otorisasi: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.yamlPerintah ini membuat ekstensi otorisasi berbasis callout yang berjalan sebagai layanan Kubernetes
authz-service.Outputnya mirip dengan:
gcpauthzextension.networking.gke.io/my-authz-ext createdSiapkan kebijakan otorisasi untuk mendelegasikan keputusan otorisasi untuk workload ke ekstensi otorisasi yang sebelumnya dikonfigurasi:
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.yamlOutputnya mirip dengan:
gcpauthzpolicy.networking.gke.io/my-workload-authz createdProxy akan menunggu layanan kustom Anda menampilkan respons
OKatauDENYsebelum meneruskan permintaan ke aplikasi atau menampilkan HTTP 403 ke pemanggil.