构建应用并查看安全数据分析

此快速入门介绍了如何在 Google Cloud 控制台的安全数据分析面板中构建应用并查看构建作业的安全数据分析。

您将学习以下内容:

  • 使用 Cloud Build 构建 Java 应用并将其容器化,然后将容器映像推送到 Artifact Registry Docker 代码库。
  • 查看 build 的以下安全性数据分析:

    • 软件制品的供应链等级 (SLSA) 级别,用于根据 SLSA 规范确定软件构建流程的成熟度级别。
    • build 工件中的漏洞
    • 构建工件的软件物料清单 (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. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  4. 如需初始化 gcloud CLI,请运行以下命令:

    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 (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 Cloud Build, 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 cloudbuild.googleapis.com  artifactregistry.googleapis.com  containerscanning.googleapis.com
  8. Install the Google Cloud CLI.

  9. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  10. 如需初始化 gcloud CLI,请运行以下命令:

    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 (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 Cloud Build, 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 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-central1 中新建一个名为 containers 且说明文字为“Docker repository”(Docker 代码库)的 Docker 代码库:

      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 界面包含安全数据分析面板,其中显示与 build 相关的安全信息,例如 SLSA 级别、依赖项中的任何漏洞以及 build 出处。

    如需查看安全性数据洞见面板,请执行以下操作:

    1. 在 Google Cloud 控制台中打开构建记录页面:

      打开“构建历史记录”页面

    2. 选择您的项目,然后点击打开

    3. 区域下拉菜单中,选择 us-central1

    4. 在包含 build 的表格中,找到您刚刚执行的 build 所在的行。

    5. 安全性数据分析列下,点击查看

    您会看到相应 build 的安全性数据洞见面板:

    此面板会显示以下信息:

    • SLSA 级别:此 build 已达到 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
      

      您现在已经删除了在此快速入门中创建的代码库。

    后续步骤