Mengonfigurasi kebijakan Otorisasi Biner dengan GKE

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

Dalam panduan memulai cepat ini, Anda akan melihat dan mengonfigurasi aturan default dalam kebijakan. Aturan default mengizinkan semua image di-deploy. Anda mengujinya dengan men-deploy image container di cluster Google Kubernetes Engine (GKE). Kemudian, Anda 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, 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

  6. Install the Google Cloud CLI.

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

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

    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, 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

  13. Install the Google Cloud CLI.

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

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

    gcloud init
  16. Instal kubectl.

Peran yang diperlukan

Untuk mendapatkan izin yang Anda perlukan guna mengonfigurasi kebijakan Otorisasi Biner dengan GKE, minta administrator untuk memberi Anda peran IAM berikut pada 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 penerapan 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 Standard.

Konsol Google Cloud

Langkah-langkah berikut mengonfigurasi cluster Autopilot.

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

    Buka GKE

  2. Klik Create.

  3. Di bagian 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 Khusus penerapan.

    7. Klik Berikutnya, lalu klik Berikutnya:Tinjau dan Buat.

    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 dapat memerlukan waktu beberapa menit.

Kebijakan default

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

Konsol Google Cloud

Untuk melihat kebijakan default, lakukan hal berikut:

  1. Buka halaman Otorisasi Biner di konsol Google Cloud .

    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 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 akan menghasilkan output berikut:

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

Menguji kebijakan terkait Proses Penegakan Kebijakan

Anda dapat menguji kebijakan terkait Proses Penegakan Kebijakan dengan mencoba men-deploy image container contoh ke cluster.

Untuk panduan memulai ini, Anda akan menggunakan contoh image container 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!".

Konsol Google Cloud

Untuk menguji kebijakan, lakukan hal berikut:

  1. Buka halaman Clusters GKE di Google Cloud console.

    Buka GKE

  2. Klik Deploy.

    Konsol akan meminta Anda memasukkan detail tentang deployment.

  3. Masukkan hello-server di kolom Deployment name.

  4. Klik Berikutnya: Detail penampung.

  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 kredensial dan informasi endpoint 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, pastikan deployment diizinkan oleh Otorisasi Biner.

Konsol Google Cloud

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

Buka GKE

Workload untuk deployment 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:

Konsol Google Cloud

Untuk menghapus deployment, lakukan langkah-langkah berikut:

  1. Kembali ke halaman Workloads GKE di konsolGoogle Cloud .

    Buka GKE

  2. Pilih workload hello-server.

  3. Klik Hapus.

kubectl

Untuk menghapus deployment, lakukan langkah-langkah berikut:

kubectl delete deployment hello-server

Mengonfigurasi kebijakan terkait Proses Penegakan Kebijakan untuk melarang semua image

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

Konsol Google Cloud

Untuk mengubah kebijakan, lakukan hal berikut:

  1. Kembali ke halaman Otorisasi Biner di konsol Google Cloud .

    Buka Otorisasi Biner

  2. Klik Edit Kebijakan.

  3. Pilih Jangan Izinkan Semua Gambar.

  4. Klik Simpan Kebijakan.

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 telah diupdate 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"
    

Tes ulang kebijakan

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

Konsol Google Cloud

Deploy image:

  1. Buka halaman Clusters GKE di Google Cloud console.

    Buka GKE

  2. Klik Deploy.

    Konsol akan meminta Anda memasukkan detail tentang deployment.

  3. Masukkan hello-server di kolom Deployment name.

  4. Klik Berikutnya: Detail penampung.

  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 telah diblokir:

Konsol Google Cloud

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

Kembali ke halaman Workloads GKE di konsolGoogle Cloud .

Buka GKE

Workload untuk image container 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 akan mencetak pesan berikut, yang menunjukkan bahwa image tidak di-deploy:

No resources found.

Anda bisa mendapatkan 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 menyerupai berikut:

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 akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan pada halaman ini, ikuti langkah-langkah berikut.

Hapus cluster yang Anda buat di GKE:

Konsol

Untuk menghapus cluster, lakukan langkah-langkah berikut:

  1. Buka halaman Clusters GKE di Google Cloud console.

    Buka GKE

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

gcloud

Untuk menghapus cluster, lakukan langkah-langkah berikut:

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

Langkah berikutnya