アプリケーションをビルドしてセキュリティ分析情報を表示する

このクイックスタートでは、アプリケーションをビルドする方法と、 Google Cloud コンソールの [セキュリティ分析情報] パネルで、ビルドのセキュリティ分析情報を表示する方法について説明します。

次のことを行います。

  • Cloud Build を使用して Java アプリケーションをビルドおよびコンテナ化し、そのコンテナ イメージを Artifact Registry Docker リポジトリに push します。
  • ビルドに関する次のセキュリティ分析情報を表示します。

    • ソフトウェア アーティファクトのためのサプライチェーン レベル(SLSA)のレベル。SLSA 仕様に従って、ソフトウェア ビルドプロセスの成熟度を示します。
    • ビルド アーティファクトの脆弱性
    • ビルド アーティファクトのソフトウェア部品構成表(SBOM)
    • ビルドの来歴。ビルドに関する検証可能なメタデータのコレクションです。これには、ビルドされたイメージのダイジェスト、入力ソースの場所、ビルド ツールチェーン、ビルドステップ、ビルド時間などの詳細情報が含まれます。

始める前に

  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. 外部 ID プロバイダ(IdP)を使用している場合は、まずフェデレーション ID を使用して gcloud CLI にログインする必要があります。

  4. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  5. Create or select a Google Cloud project.

    • 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 Cloud Build, Artifact Registry, and Container Scanning APIs:

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

  9. 外部 ID プロバイダ(IdP)を使用している場合は、まずフェデレーション ID を使用して gcloud CLI にログインする必要があります。

  10. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  11. Create or select a Google Cloud project.

    • 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 Cloud Build, Artifact Registry, and Container Scanning APIs:

    gcloud services enable cloudbuild.googleapis.com  artifactregistry.googleapis.com  containerscanning.googleapis.com
  14. 環境を準備する

    1. プロジェクト ID を環境変数として設定します。

      export PROJECT_ID=$(gcloud config get project)
      
    2. ビルドとコンテナ化を行う Java コードサンプルを含むリポジトリのクローンを作成します。

      git clone https://github.com/googlecloudplatform/software-delivery-shield-demo-java.git
      cd software-delivery-shield-demo-java/backend
      

    イメージ用の Artifact Registry リポジトリを作成する

    1. ロケーション us-central1containers という名前の Docker リポジトリを作成し、「Docker repository」という説明を指定します。

      gcloud artifacts repositories create containers \
          --repository-format=docker \
          --location=us-central1 --description="Docker repository"
      
    2. リポジトリが作成されたことを確認します。

      gcloud artifacts repositories list
      

      表示されたリポジトリのリストに containers が表示されます。

    アプリケーションのビルド

    Cloud Build を使用して Java アプリケーションをビルドし、コンテナ化します。 次のコマンドは、Java アプリケーションをビルドしてコンテナ化し、ビルドしたコンテナを Artifact Registry Docker リポジトリに保存します。

    gcloud builds submit --config=cloudbuild.yaml --region=us-central1
    

    ビルドが完了すると、次のような成功ステータス メッセージが表示されます。

    <pre class="none lang-sh">
    DONE
    -----------------------------------------------------------------------------
    ID: 3e08565f-7f57-4449-bc68-51c46cf33d03
    CREATE_TIME: 2022-09-19T15:41:07+00:00
    DURATION: 54S
    SOURCE: gs://sds-docs-project_cloudbuild/source/1663602066.777581-6ebe4b2d6fd741ffa18936d7f78055e9.tgz
    IMAGES: us-central1-docker.pkg.dev/sds-docs-project/containers/java-guestbook-backend:quickstart
    STATUS: SUCCESS
    </pre>
    

    ビルドされたイメージの SBOM を生成する

    SBOM は、アプリケーションの完全なインベントリであり、ソフトウェアが依存するパッケージを特定します。その内容には、ベンダーのサードパーティ ソフトウェア、内部アーティファクト、オープンソース ライブラリが含まれます。

    前のセクションでビルドしたイメージの SBOM を生成します。

    gcloud artifacts sbom export \
        --uri=us-central1-docker.pkg.dev/${PROJECT_ID}/containers/java-guestbook-backend:quickstart
    

    セキュリティ分析情報を表示する

    Google Cloud コンソールの Cloud Build UI には、[セキュリティ分析情報] パネルがあります。このパネルには、SLSA レベル、依存関係における脆弱性、ビルドの来歴など、ビルドに関連するセキュリティ情報が表示されます。

    [セキュリティ分析情報] パネルを表示するには:

    1. Google Cloud コンソールで [ビルド履歴] ページを開きます。

      [ビルド履歴] ページを開く

    2. 実際のプロジェクトを選択し、[開く] をクリックします。

    3. [リージョン] プルダウン メニューで、[us-central1] を選択します。

    4. ビルドの表で、実行したビルドの行を見つけます。

    5. [セキュリティ分析情報] 列で [表示] をクリックします。

    ビルドの [セキュリティ分析情報] パネルが表示されます。

    このパネルには、次の情報が表示されます。

    • SLSA レベル: このビルドは SLSA レベル 3 を取得しています。[詳細] リンクをクリックして、このセキュリティ レベルの意味を確認してください。

    • 脆弱性: アーティファクトに見つかった脆弱性。イメージ名([java-guestbook-backend])をクリックすると、脆弱性スキャンが完了したアーティファクトが表示されます。

    • Artifact Registry にあるビルド済みコンテナ イメージの依存関係

    • ビルドの詳細: ビルダーやログを表示するためのリンクなど、ビルドの詳細。

    クリーンアップ

    このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、リソースを含む Google Cloud プロジェクトを削除します。

    1. Container Scanning API を無効にします。

      gcloud services disable containerscanning.googleapis.com --force
      
    2. Artifact Registry リポジトリを削除します。

      gcloud artifacts repositories delete containers \
          --location=us-central1 --async
      

      以上で、このクイックスタートで作成したリポジトリの削除が完了しました。

    次のステップ