Memperbarui kebijakan Otorisasi Biner

Dokumen ini menjelaskan cara mengecualikan image dalam kebijakan Otorisasi Biner.

Dalam dokumen ini, Anda akan memperbarui kebijakan untuk mengecualikan image container di Container Registry dan Artifact Registry dari penerapan Otorisasi Biner dan menetapkan aturan default untuk melarang semua container lainnya di-deploy.

Sebelum memulai

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry, Binary Authorization APIs.

    Enable the APIs

  5. Install the Google Cloud CLI.
  6. To initialize the gcloud CLI, run the following command:

    gcloud init
  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  8. Make sure that billing is enabled for your Google Cloud project.

  9. Enable the Artifact Registry, Binary Authorization APIs.

    Enable the APIs

  10. Install the Google Cloud CLI.
  11. To initialize the gcloud CLI, run the following command:

    gcloud init
  12. Jika Anda tidak menggunakan Cloud Shell, instal kubectl.

Membuat cluster dengan Otorisasi Biner diaktifkan

Buat cluster GKE dengan Otorisasi Biner yang diaktifkan. Ini adalah cluster tempat Anda ingin image container yang di-deploy berjalan.

Konsol Google Cloud

  1. Buka halaman Clusters GKE di konsol Google Cloud.

    Buka GKE

    Konsol menampilkan daftar cluster GKE dalam project Google Cloud Anda.

  2. Klik Create Cluster.

  3. Masukkan test-cluster di kolom Name.

    Kolom nama di template cluster standar

  4. Pilih Zonal di opsi Location Type.

  5. Pilih us-central1-a dari menu drop-down Zone.

  6. Klik link Keamanan untuk menampilkan panel Keamanan.

  7. Di panel Security, pilih Enable Binary Authorization.

    Opsi Aktifkan Otorisasi Biner

  8. Pilih Hanya terapkan.

  9. Klik Buat.

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

Melihat 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 Binary Authorization di konsol Google Cloud.

    Buka Otorisasi Biner

    Konsol akan menampilkan detail tentang kebijakan.

  2. Klik Edit Policy.

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

gcloud

Untuk melihat kebijakan default, ekspor file YAML kebijakan, lakukan hal 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/<var>PROJECT_ID</var>/policy

REST API

Untuk melihat kebijakan default, ambil 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"

REST API menampilkan hal berikut:

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

Hanya izinkan gambar yang dikecualikan

Bagian ini menjelaskan cara mengubah kebijakan untuk mengecualikan gambar dan tidak mengizinkan deployment semua gambar lainnya.

Konsol Google Cloud

Untuk mengubah kebijakan, lakukan hal berikut:

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

    Buka Otorisasi Biner

  2. Klik Edit Policy.

  3. Pilih Larang Semua Gambar.

  4. Di bagian Gambar yang dikecualikan dari aturan deployment, luaskan Jalur image.

  5. Klik Tambahkan Jalur Gambar.

    Container Registry

    Di Jalur image baru, tempel jalur berikut dari repositori Container Registry:

    gcr.io/google-samples/hello-app:1.0
    

    Artifact Registry

    Di New image path, tempel jalur berikut dari repositori Artifact Registry:

    us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
    
  6. Klik Done untuk menyimpan jalur gambar.

  7. Klik Simpan Kebijakan.

gcloud

Untuk mengubah kebijakan agar mengizinkan contoh image dari Container Registry dan Artifact Registry, 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 dan tambahkan gambar yang dikecualikan ke admissionWhitelistPatterns.

    Untuk mengecualikan contoh image berikut dari Container Registry dan Artifact Registry, ubah file YAML kebijakan Anda sebagai berikut:

    admissionWhitelistPatterns:
    - namePattern: gcr.io/google-samples/hello-app:1.0
    - 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. Impor file YAML kebijakan kembali 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",
      "admissionWhitelistPatterns": [
        {
          "namePattern": "gcr.io/google-samples/hello-app:1.0"
        },
        {
          "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. Kirim kebijakan yang telah 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 kebijakan

Untuk menguji kebijakan dengan men-deploy image yang Anda tambahkan ke daftar yang diizinkan Exempt image, lakukan tindakan berikut:

  1. Deploy image yang dikecualikan ke cluster.

    Konsol Google Cloud

    Men-deploy image yang dikecualikan:

    1. Buka halaman Clusters GKE di konsol Google Cloud.

      Buka GKE

    2. Klik Deploy.

      Konsol akan meminta Anda memasukkan detail tentang deployment.

    3. Pilih Image Container yang Ada.

    4. Masukkan jalur image container.

      Container Registry

      Untuk men-deploy image dari Container Registry, masukkan hal berikut:

      gcr.io/google-samples/hello-app:1.0
      

      Artifact Registry

      Untuk men-deploy image dari Artifact Registry, masukkan hal berikut:

      us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
      
    5. Klik Lanjutkan.

    6. Masukkan hello-server di kolom Application Name.

    7. Klik Deploy.

    kubectl

    Container Registry

    Deploy image dari Container Registry:

    kubectl run hello-server --image gcr.io/google-samples/hello-app:1.0 --port 8080
    

    Artifact Registry

    Deploy image dari Artifact Registry:

    kubectl run hello-server --image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 --port 8080
    
  2. Pastikan image diizinkan untuk di-deploy:

    Konsol Google Cloud

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

    1. Buka halaman GKE.

    Buka GKE

    1. Buka halaman Workloads.

    Beban kerja hello-server muncul dengan ikon hijau, yang menunjukkan bahwa beban kerja tersebut sedang berjalan.

    kubectl

    Untuk memverifikasi bahwa image diizinkan untuk di-deploy, masukkan perintah berikut:

    kubectl get pods
    

    Anda akan melihat gambar berjalan.

  3. Hapus pod.

    Konsol Google Cloud

    Di halaman Workloads GKE:

    1. Pilih beban kerja hello-server.

    2. Klik Hapus.

    3. Saat diminta untuk menghapus resource, klik Hapus.

    kubectl

    kubectl delete pod hello-server
    

Pembersihan

Agar akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan di 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 konsol Google Cloud.

    Buka GKE

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

gcloud

Untuk menghapus cluster, masukkan perintah berikut:

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

Langkah berikutnya