自动扫描操作系统软件包

本文档将介绍如何启用 Container Scanning API、将映像推送到 Artifact Registry,以及查看映像中发现的漏洞列表。

Artifact Analysis 可为 Artifact Registry 中的容器映像提供漏洞信息。元数据存储为备注。Container Analysis 会为与映像相关联的备注的每个实例创建一个发生实例。如需了解详情,请参阅概览价格文档。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  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. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry and Container Scanning APIs.

    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

  5. 安装 Google Cloud CLI。

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

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

    gcloud init
  8. 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

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

  10. Enable the Artifact Registry and Container Scanning APIs.

    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

  11. 安装 Google Cloud CLI。

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

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

    gcloud init
  14. 在 Artifact Registry 中创建 Docker 代码库,并将容器映像推送到该代码库。如果您不熟悉 Artifact Registry,请参阅 Docker 快速入门

启用此 API 还会启用 Artifact Registry 中的语言包扫描。请参阅支持的软件包类型

查看映像漏洞

Artifact Analysis 会在新映像上传到 Artifact Registry 时扫描这些映像。此扫描可提取有关容器中系统软件包的信息。

您可以使用 Google Cloud 控制台、Google Cloud CLI 或 Container Analysis API 查看注册表中映像的漏洞事件。如果某个映像存在漏洞,您就可以获取详细信息。

只要映像和软件包是在过去 30 天内拉取的,Artifact Analysis 就会继续扫描它们。30 天后,扫描的映像和软件包的元数据将不再更新,并且结果将过时。

Artifact Analysis 会归档过时超过 90 天的元数据。只有使用 API 才能评估此归档的元数据。 您可以拉取包含过时或归档元数据的映像,以重新扫描该映像。 刷新元数据最多可能需要 24 小时。无法重新扫描具有过时或已归档元数据的软件包。

在 Google Cloud 控制台中查看出现次数

如需查看映像中的漏洞,请执行以下操作:

  1. 获取代码库列表。

    打开“代码库”页面

  2. 在代码库列表中,点击一个代码库。

  3. 在映像列表中,点击映像名称。

    每个映像摘要的漏洞总数将显示在漏洞列中。

    存在漏洞的扫描映像

  4. 如需查看映像的漏洞列表,请点击漏洞列中的链接。

    扫描结果部分会显示扫描的软件包类型、漏洞总数、有修复的漏洞、无修复的漏洞和有效严重程度的摘要。

    “扫描结果”页面,其中列出了漏洞、修复和有效严重程度

    漏洞表格会列出发现的每个漏洞的常见漏洞和披露 (CVE) 名称、有效严重程度、通用漏洞评分系统 (CVSS) 评分、修复(如有)、包含漏洞的软件包的名称以及软件包类型。

    Google Cloud 控制台会在该表格中显示每个页面最多 1,200 个漏洞。您可以过滤和排序这些文件,以按文件扩展名检查特定文件、目录、图层或文件类型。

  5. 如需详细了解特定 CVE,请点击 CVE 名称。

  6. 如需查看漏洞出现详情(例如版本号和受影响的位置),请在包含漏洞名称的行中点击查看查看已修复。对于没有修复的漏洞,链接文字为查看;对于已应用修复的漏洞,链接文字为查看已修复

查看特定层中的漏洞

Artifact Analysis 提供特定于层的元数据,可帮助您确定哪个软件包包含漏洞,以及哪个 Docker build 命令引入了该层。

如需在 Google Cloud 控制台中查看基于层的漏洞元数据,请执行以下操作:

  1. 在 Artifact Registry 中,打开您的代码库列表。

    打开“代码库”页面

  2. 代码库页面中,点击一个代码库。

  3. 代码库详情页面中,点击某个映像。

  4. 在映像版本列表中,点击一个映像摘要。

  5. 在映像摘要详情页面中,点击漏洞标签页。

    当基于层的元数据可用时,Artifact Analysis 会在栏中显示可供选择的层列表:

    映像摘要的“漏洞”页面(包括“层”栏)的图片。图层栏有一个下拉菜单,可用于从编号列表中选择图层。

  6. 如需查看图层列表,请点击图层下拉菜单。

    系统会打开一个下拉列表,其中显示了图层的编号列表。第 0 层代表底层,后续每一层都在前一层上构建。系统只会显示已发现漏洞的层。

    一张显示打开的图层下拉菜单的图片。该菜单会显示用于构建层的 Docker 命令,以及每个层的漏洞类型和数量。

    对于每个层,Artifact Analysis 会显示用于构建该层的 Docker 命令以及在该层中发现的漏洞数量。

  7. 如需详细了解特定层中的漏洞,请从层列表中选择相应层。

    漏洞表格会更新为仅显示所选层中发现的漏洞。您可以应用更多过滤条件,也可以调查特定漏洞。

  8. 点击漏洞旁边的查看查看修复,即可查看更详细的漏洞信息。

    漏洞详情侧边栏会显示存在漏洞的软件包的文件位置。

  9. 文件位置表格中,将光标悬停在问号上,以打开其他图层元数据。

    对话框会显示层索引、层构建命令、层摘要和基本映像,您可以在其中修复漏洞。

使用 gcloud 查看出现次数

如需查看 Artifact Registry 中映像的发生实例,请运行 gcloud artifacts docker images list 命令:

  gcloud artifacts docker images list --show-occurrences \
      LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_ID

其中:

  • LOCATION 是代码库的单区域或多区域位置
  • PROJECT_ID 是您的 Google Cloud 控制台项目 ID
  • REPOSITORY 是存储了映像的代码库的名称。
  • IMAGE_ID 是代码库中的映像名称。 您无法使用此命令指定映像标记。

默认情况下,该命令会返回最近的 10 个结果。如需显示不同数量的结果,请使用 --show-occurrences-from 标志。例如,以下命令会返回最近 25 个结果:

gcloud artifacts docker images list --show-occurrences-from=25 \
    us-central1-docker.pkg.dev/my-project/my-repo/my-image

输出类似于以下内容:

图片输出示例

  {
      "name": "projects/my-project/locations/us-central1/occurrences/22d3edfb-34f2-4d3f-be08-fd90c549f010",
      "resource": {
        "uri": "https://us-central1-docker.pkg.dev/my-project/my-repo/golang-nginx@sha256:5b54ef20748417ccdb0b87afedf98c44e699e2f59bd6cv48cbdc1ca2e72c80e3"
      },
      "noteName": "projects/my-project/locations/us-central1/notes/CVE-2005-2541",
      "kind": "VULNERABILITY",
      "createTime": "2025-01-21T15:57:01.238832Z",
      "updateTime": "2025-01-21T15:57:01.238832Z",
      "vulnerability": {
        "severity": "CRITICAL",
        "cvssScore": 10,
        "packageIssue": [
          {
            "affectedLocation": {
              "cpeUri": "cpe:/o:debian:debian_linux:12",
              "package": "tar",
              "version": {
                "name": "1.34+dfsg",
                "revision": "1.2+deb12u1",
                "kind": "NORMAL"
              }
            },
            "fixedLocation": {
              "cpeUri": "cpe:/o:debian:debian_linux:12",
              "package": "tar",
              "version": {
                "kind": "MAXIMUM"
              }
            },
            "packageType": "OS",
            "effectiveSeverity": "LOW",
            "file_location": [
                {
                "file_path": "/var/lib/dpkg/status",
                "layerDetails": {
                  "index": 0,
                  "diffID": "sha256:123",
                  "buildCommand": "FROM golang:example_sha256",
                  "BaseImages": [
                    {
                      "name": "library/golang",
                    },
                  ],
                },
              },
            ],
          }
        ],
        "shortDescription": "CVE-2005-2541",
        "longDescription": "NIST vectors: AV:N/AC:L/Au:N/C:C/I:C/A:C",
        "relatedUrls": [
          {
            "url": "https://security-tracker.debian.org/tracker/CVE-2005-2541",
            "label": "More Info"
          }
        ],
        "effectiveSeverity": "LOW",
        "cvssVersion": "CVSS_VERSION_2",
        "cvssV2": {
          "baseScore": 10,
          "attackVector": "ATTACK_VECTOR_NETWORK",
          "attackComplexity": "ATTACK_COMPLEXITY_LOW",
          "authentication": "AUTHENTICATION_NONE",
          "confidentialityImpact": "IMPACT_COMPLETE",
          "integrityImpact": "IMPACT_COMPLETE",
          "availabilityImpact": "IMPACT_COMPLETE"
        }
      }
    }
  

过滤漏洞发生实例

如需过滤漏洞发生实例,请使用 --occurrence-filter 参数:

gcloud artifacts docker images list --show-occurrences \
    LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_ID \
    --occurrence-filter=FILTER_EXPRESSION

其中:

  • LOCATION 是代码库的单区域或多区域位置
  • PROJECT_ID 是您的 Google Cloud 控制台项目 ID
  • REPOSITORY 是存储了映像的代码库的名称。
  • IMAGE_ID 是代码库中的映像名称。
  • FILTER_EXPRESSION 是一个示例过滤表达式,其格式请参阅过滤漏洞发生实例

查看特定层的漏洞

您可以使用 layerDetails 字段来确定哪个特定层引入了存在安全漏洞的软件包,并查看有关该层的其他信息。例如,假设某张图片的 layerDetails 输出如下:

  "layerDetails": {
    "index": 0,
    "diffID": "sha256:123",
    "buildCommand": "FROM golang:example_sha256",
    "BaseImages": [
      {
        "name": "library/golang",
      },
    ],
  }

输出显示,映像中的层索引为 0,层哈希为 sha256:123。用于构建此层的 Docker build 命令是 FROM golang:example_sha256,该层也位于 library/golang 基础映像中。

使用 API 或代码查看出现次数

如需查看映像的发生实例,请按以下格式使用网址指定映像:

LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE_ID

API

使用 cURL

如需获取项目中发生实例的列表,请使用以下命令:

 curl -X GET -H "Content-Type: application/json" -H \
    "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences

如需获取项目中漏洞的汇总,请使用以下命令:

 curl -X GET -H "Content-Type: application/json" -H \
    "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences:vulnerabilitySummary

如需获取特定发生实例的详细信息,请执行以下操作:

 curl -X GET -H "Content-Type: application/json" -H \
    "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://containeranalysis.googleapis.com/v1/projects/PROJECT_ID/occurrences/OCCURRENCE_ID

Java

如需了解如何安装和使用 Artifact Analysis 客户端库,请参阅 Artifact Analysis 客户端库。 如需了解详情,请参阅 Artifact Analysis Java API 参考文档

如需向 Artifact Analysis 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

import com.google.cloud.devtools.containeranalysis.v1.ContainerAnalysisClient;
import io.grafeas.v1.GrafeasClient;
import io.grafeas.v1.Occurrence;
import io.grafeas.v1.ProjectName;
import java.io.IOException;
import java.lang.InterruptedException;

public class OccurrencesForImage {
  // Retrieves all the Occurrences associated with a specified image
  // Here, all Occurrences are simply printed and counted
  public static int getOccurrencesForImage(String resourceUrl, String projectId)
      throws IOException, InterruptedException {
    // String resourceUrl = "https://gcr.io/project/image@sha256:123";
    // String projectId = "my-project-id";
    final String projectName = ProjectName.format(projectId);
    final String filterStr = String.format("resourceUrl=\"%s\"", resourceUrl);

    // Initialize client that will be used to send requests. After completing all of your requests, 
    // call the "close" method on the client to safely clean up any remaining background resources.
    GrafeasClient client = ContainerAnalysisClient.create().getGrafeasClient();
    int i = 0;
    for (Occurrence o : client.listOccurrences(projectName, filterStr).iterateAll()) {
      // Write custom code to process each Occurrence here
      System.out.println(