Mengonfigurasi kebijakan Otorisasi Biner dengan GKE

Panduan memulai ini menunjukkan cara mengonfigurasi dan menguji aturan dasar dalam kebijakan Otorisasi Biner .

Dalam panduan memulai ini, Anda akan melihat dan mengonfigurasi aturan default dalam kebijakan. Aturan default memungkinkan semua image di-deploy. Anda akan mengujinya dengan men-deploy image container di cluster Google Kubernetes Engine (GKE). Kemudian, Anda akan menetapkan aturan default untuk melarang semua image di-deploy dan mencoba men-deploy image.

Sebelum memulai

  1. Login ke akun Google Cloud Anda. 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.
  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. 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.

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

  5. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  6. Instal Google Cloud CLI.

  7. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  8. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  9. 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

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

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

  12. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  13. Instal Google Cloud CLI.

  14. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  15. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  16. Instal kubectl.

Peran yang diperlukan

Untuk mendapatkan izin yang Anda perlukan untuk mengonfigurasi kebijakan Otorisasi Biner dengan GKE, minta administrator Anda untuk memberi Anda peran IAM berikut di project Anda:

Untuk mengetahui informasi selengkapnya tentang pemberian peran, lihat Mengelola akses ke project, folder, dan organisasi.

Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran khusus atau peran bawaan lainnya.

Membuat cluster dengan penegakan Otorisasi Biner diaktifkan

Sekarang, Anda akan membuat cluster GKE dengan Otorisasi Biner diaktifkan. Ini adalah cluster tempat Anda ingin menjalankan image container yang di-deploy.

Otorisasi Biner berfungsi dengan cluster Autopilot atau Standar.

Google Cloud Konsol

Langkah-langkah berikut mengonfigurasi cluster Autopilot.

  1. Di Google Cloud konsol, buka halaman Kubernetes clusters GKE:

    Buka GKE

  2. Klik Create.

  3. Di Create an Autopilot cluster, lakukan hal berikut:

    1. Di kolom Name, masukkan test-cluster.

    2. Di menu Region, pilih us-central1.

    3. Luaskan bagian Advanced settings.

    4. Klik link Security untuk menampilkan panel Security.

    5. Di panel Security, centang kotak Enable Binary Authorization.

    6. Pilih Enforce-only.

    7. Klik Next , lalu klik Next:Review and Create.

    8. Untuk mulai membuat cluster, klik Create.

gcloud

Jalankan gcloud container clusters create dengan flag --binauthz-evaluation-mode=PROJECT_SINGLETON_POLICY_ENFORCE diaktifkan.

gcloud container clusters create \
    --binauthz-evaluation-mode=PROJECT_SINGLETON_POLICY_ENFORCE \
    --zone us-central1-a \
    test-cluster

Pembuatan cluster memerlukan waktu beberapa menit.

Kebijakan default

Secara default, kebijakan Otorisasi Biner Anda dikonfigurasi untuk mengizinkan semua image container di-deploy.

Google Cloud Konsol

Untuk melihat kebijakan default, lakukan hal berikut:

  1. Buka halaman Binary Authorization di Google Cloud konsol.

    Buka Otorisasi Biner

    Konsol akan menampilkan detail tentang kebijakan tersebut.

  2. Klik Edit Policy.

  3. Di Project Default Rule, opsi Allow All Images dipilih.

gcloud

Untuk melihat kebijakan default, ekspor file YAML kebijakan sebagai berikut:

gcloud container binauthz policy export

Secara default, file tersebut memiliki konten berikut:

globalPolicyEvaluationMode: ENABLE
defaultAdmissionRule:
  evaluationMode: ALWAYS_ALLOW
  enforcementMode: ENFORCED_BLOCK_AND_AUDIT_LOG
name: projects/PROJECT_ID/policy

REST API

Untuk melihat kebijakan default, ambil kebijakan tersebut dalam format JSON sebagai berikut:

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"

Perintah ini menghasilkan output berikut:

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

Menguji kebijakan penegakan

Anda dapat menguji kebijakan penegakan dengan mencoba men-deploy image container contoh ke cluster.

Untuk panduan memulai ini, Anda akan menggunakan image container contoh yang terletak di jalur us-docker.pkg.dev/google-samples/containers/gke/hello-app di Artifact Registry. Ini adalah image container publik yang dibuat oleh Google yang berisi aplikasi contoh "Hello, World!".

Google Cloud Konsol

Untuk menguji kebijakan, lakukan hal berikut:

  1. Buka halaman Clusters GKE di Google Cloud konsol.

    Buka GKE

  2. Klik Deploy.

    Konsol akan meminta Anda memasukkan detail tentang deployment.

  3. Masukkan hello-server di kolom Deployment name.

  4. Klik Next: Container details.

  5. Pilih Existing Container Image.

  6. Masukkan us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 sebagai jalur image container.

  7. Klik Deploy.

kubectl

Untuk menguji kebijakan, lakukan hal berikut:

  1. Perbarui file kubeconfig lokal:

    gcloud container clusters get-credentials \
        --zone us-central1-a \
        test-cluster
    

    Tindakan ini akan memberikan informasi endpoint dan kredensial yang diperlukan untuk mengakses cluster di GKE.

  2. Deploy image:

    kubectl run hello-server --image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 --port 8080
    

Sekarang, verifikasi bahwa deployment diizinkan oleh Otorisasi Biner.

Google Cloud Konsol

Untuk memverifikasi bahwa image telah di-deploy, buka halaman GKE Workloads di Google Cloud konsol.

Buka GKE

Workload untuk deployment akan muncul dengan ikon hijau yang menunjukkan bahwa image berhasil di-deploy.

kubectl

Untuk memverifikasi bahwa image telah di-deploy, lakukan hal berikut:

kubectl get pods

Perintah ini akan mencetak pesan yang mirip dengan berikut, yang menunjukkan bahwa deployment berhasil:

NAME                            READY     STATUS    RESTARTS   AGE
hello-server-579859fb5b-h2k8s   1/1       Running   0          1m

Pastikan untuk menghapus deployment agar Anda dapat melanjutkan ke langkah berikutnya:

Google Cloud Konsol

Untuk menghapus deployment, lakukan hal berikut:

  1. Kembali ke halaman Workloads GKE di Google Cloud konsol.

    Buka GKE

  2. Pilih workload hello-server.

  3. Klik Delete.

kubectl

Untuk menghapus deployment, lakukan hal berikut:

kubectl delete deployment hello-server

Mengonfigurasi kebijakan penegakan untuk melarang semua image

Sekarang, ubah kebijakan untuk memblokir, bukan mengizinkan, semua image di-deploy.

Google Cloud Konsol

Untuk mengubah kebijakan, lakukan hal berikut:

  1. Kembali ke halaman Binary Authorization di Google Cloud konsol.

    Buka Otorisasi Biner

  2. Klik Edit Policy.

  3. Pilih Disallow All Images.

  4. Klik Save Policy.

gcloud

Untuk mengubah kebijakan, lakukan hal berikut:

  1. Ekspor file YAML kebijakan:

    gcloud container binauthz policy export  > /tmp/policy.yaml
    
  2. Di editor teks, ubah evaluationMode dari ALWAYS_ALLOW menjadi ALWAYS_DENY.

    File YAML kebijakan akan muncul sebagai berikut:

    globalPolicyEvaluationMode: ENABLE
    defaultAdmissionRule:
      evaluationMode: ALWAYS_DENY
      enforcementMode: ENFORCED_BLOCK_AND_AUDIT_LOG
    name: projects/PROJECT_ID/policy
    
  3. Impor kembali file YAML kebijakan ke Otorisasi Biner:

    gcloud container binauthz policy import /tmp/policy.yaml
    

REST API

Untuk mengubah kebijakan, lakukan hal berikut:

  1. Buat file teks dengan kebijakan yang diperbarui dalam format JSON:

    cat > /tmp/policy.json << EOM
    {
      "name": "projects/${PROJECT_ID}/policy",
      "globalPolicyEvaluationMode": "ENABLE",
      "defaultAdmissionRule": {
        "evaluationMode": "ALWAYS_DENY",
        "enforcementMode": "ENFORCED_BLOCK_AND_AUDIT_LOG"
      }
    }
    EOM
    
  2. Kirim kebijakan yang diperbarui ke REST API:

    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"
    

Menguji ulang kebijakan

Uji lagi kebijakan dengan men-deploy image container contoh ke cluster. Kali ini, Otorisasi Biner akan memblokir image agar tidak di-deploy.

Google Cloud Konsol

Deploy image:

  1. Buka halaman Clusters GKE di Google Cloud konsol.

    Buka GKE

  2. Klik Deploy.

    Konsol akan meminta Anda memasukkan detail tentang deployment.

  3. Masukkan hello-server di kolom Deployment name.

  4. Klik Next: Container details.

  5. Pilih Existing Container Image.

  6. Masukkan us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 sebagai jalur image container.

  7. Klik Deploy.

kubectl

Deploy image:

kubectl run hello-server --image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 --port 8080

Sekarang, Anda dapat memverifikasi bahwa kebijakan diblokir:

Google Cloud Konsol

Untuk memverifikasi bahwa image tidak di-deploy, lakukan hal berikut:

Kembali ke halaman Workloads GKE di Google Cloud konsol.

Buka GKE

Workload untuk image container akan muncul dengan ikon merah yang menunjukkan bahwa image gagal di-deploy.

kubectl

Untuk memverifikasi bahwa image tidak di-deploy, jalankan perintah berikut:

kubectl get pods

Perintah ini akan mencetak pesan berikut, yang menunjukkan bahwa image tidak di-deploy:

No resources found.

Anda dapat memperoleh detail lebih lanjut tentang deployment:

kubectl get event --template \
'{{range.items}}{{"\033[0;36m"}}{{.reason}}:{{"\033[0m"}}{{.message}}{{"\n"}}{{end}}'

Anda akan melihat respons yang mirip dengan berikut ini:

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

Dalam output ini:

  • POD_NAME: nama Pod.
  • IMAGE_NAME: nama image.
  • ATTESTOR_NAME: nama attestor.

Pembersihan

Agar akunAnda tidak dikenai biaya untuk resource yang digunakan pada halaman ini, ikuti langkah-langkah berikut. Google Cloud

Hapus cluster yang Anda buat di GKE:

Konsol

Untuk menghapus cluster, lakukan hal berikut:

  1. Buka halaman Clusters GKE di Google Cloud konsol.

    Buka GKE

  2. Pilih cluster test-cluster, lalu klik Delete.

gcloud

Untuk menghapus cluster, lakukan hal berikut:

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

Langkah berikutnya