Memindai paket Python secara otomatis

Pelajari cara mengaktifkan pemindaian otomatis, mengirim image container di Artifact Registry, dan melihat daftar kerentanan yang ditemukan dalam image.

Artifact Analysis memindai kerentanan setiap kali image dikirim ke Artifact Registry. Administrator platform dan developer aplikasi dapat menggunakan fitur pemindaian otomatis ini untuk membantu mengidentifikasi risiko dalam pipeline pengiriman software mereka.

Panduan memulai ini menggunakan paket sederhana yang tersedia secara publik untuk mendemonstrasikan satu jenis pemindaian paket. Secara default, Artifact Analysis memindai kerentanan dalam beberapa jenis paket setelah Anda mengaktifkan API pemindaian. Jenis paket yang didukung meliputi: OS, Go, Java (Maven), Python, dan Node.js (npm).

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. Install the Google Cloud CLI.

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

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

    gcloud init
  5. Create or select 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.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  7. Enable the Artifact Registry and Container Scanning 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.

    gcloud services enable containerscanning.googleapis.com  artifactregistry.googleapis.com
  8. Install the Google Cloud CLI.

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

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

    gcloud init
  11. Create or select 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.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

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

  13. Enable the Artifact Registry and Container Scanning 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.

    gcloud services enable containerscanning.googleapis.com  artifactregistry.googleapis.com
  14. Membuat repositori Docker di Artifact Registry

    Buat repositori Docker untuk menyimpan image sampel untuk panduan memulai ini.

    1. Buka halaman Repositori di Google Cloud konsol.

      Buka halaman Repositori

    2. Klik Create Repository.

    3. Di halaman Create Repository, masukkan setelan berikut:

      • Nama: quickstart-docker-repo
      • Format: Docker
      • Mode: Standar
      • Location type: Region
      • Region: us-central1
    4. Klik Create.

    Anda akan melihat halaman Repositories. Repositori quickstart-docker-repo Anda ditambahkan ke daftar repositori.

    Mengonfigurasi autentikasi

    Sebelum dapat mengirim atau mengambil image dengan Artifact Registry, Anda harus mengonfigurasi Docker agar menggunakan Google Cloud CLI untuk mengautentikasi permintaan ke Artifact Registry.

    Di Cloud Shell atau shell lokal Anda, siapkan autentikasi untuk repositori Docker di region us-central1:

    gcloud auth configure-docker us-central1-docker.pkg.dev
    

    Mendapatkan paket

    1. Ubah ke direktori tempat Anda ingin menyimpan image container.

    2. Salin gambar ke direktori lokal Anda. Misalnya, Anda dapat menggunakan TensorFlow. Gambar ini berisi Python dan memiliki beberapa kerentanan.

    Tujuan panduan ini hanya untuk memindai gambar dan melihat hasilnya. Pengetahuan Tensorflow sebelumnya tidak diperlukan.

    Jalankan perintah berikut untuk mendapatkan image:

    docker pull amancevice/pandas:1.2.5-jupyter
    

    Beri tag pada paket Anda dengan nama repositori

    Saat ingin mengirim paket ke Artifact Registry, Anda perlu mengonfigurasi perintah docker push untuk mengirim image ke lokasi tertentu.

    Jalankan perintah berikut untuk memberi tag pada image sebagai quickstart-image:tag1:

    docker tag amancevice/pandas:1.2.5-jupyter \
    us-central1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1

    Dengan:

    • PROJECT adalah Google Cloud project ID Anda. Jika project ID Anda berisi titik dua (:), lihat Project yang tercakup dalam domain.
    • us-central1 adalah region repositori Artifact Registry.
    • docker.pkg.dev adalah nama host untuk repositori Docker.
    • quickstart-image adalah nama image yang ingin Anda gunakan di repositori. Nama image dapat berbeda dengan nama image lokal.
    • tag1 adalah tag yang Anda tambahkan ke image Docker. Jika Anda tidak menentukan tag, Docker akan menerapkan tag default latest.

    Mengirim image ke Artifact Registry

    Artifact Analysis otomatis memindai paket Python baru dalam image container saat diupload ke Artifact Registry.

    Untuk mengirim image ke repositori Docker di Artifact Registry, jalankan perintah berikut:

    docker push us-central1-docker.pkg.dev/PROJECT/quickstart-docker-repo/quickstart-image:tag1

    Ganti PROJECT dengan Google Cloud project ID Anda. Jika project ID Anda berisi titik dua (:), lihat Project yang tercakup dalam domain.

    Melihat hasil pemindaian kerentanan

    Cara lain untuk melihat hasil

    Konsol Google Cloud menampilkan hingga 1.200 kerentanan. Jika image Anda memiliki lebih dari 1.200 kerentanan, Anda harus menggunakan gcloud atau API untuk melihat daftar lengkapnya.

    Pembersihan

    Agar akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan pada halaman ini, hapus project Google Cloud yang berisi resource tersebut.

    Jika Anda membuat project baru untuk panduan ini, Anda dapat menghapus project tersebut sekarang.

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

    Langkah berikutnya