本文档介绍了如何启用 Container Scanning API、将映像推送到 Artifact Registry,以及查看映像中发现的漏洞列表。
Artifact Analysis 会将漏洞信息存储为备注。Container Analysis 会为与映像相关联的备注的每个实例创建一个发生实例。如需了解详情,请参阅概览和价格文档。
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry and Container Scanning APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry and Container Scanning APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init - 在 Artifact Registry 中创建 Docker 代码库,并将包含 Java 代码的容器映像推送到该代码库。如果您不熟悉 Artifact Registry,请参阅 Docker 快速入门。
如需了解如何使用 Cloud Build 构建 Java 应用并将其容器化,请参阅 构建、测试 Java 应用并将其容器化。
查看映像漏洞
Artifact Analysis 会在新映像上传到 Artifact Registry 时扫描这些映像。此扫描可提取有关容器中软件包的信息。
您可以使用 Google Cloud 控制台、Google Cloud CLI 或 Container Analysis API 查看 Artifact Registry 中映像的漏洞事件。如果某个映像存在漏洞,您就可以获取详细信息。
只要映像和软件包是在过去 30 天内拉取的,Artifact Analysis 就会继续扫描它们。30 天后,扫描的映像和软件包的元数据将不再更新,结果也会过时。
Artifact Analysis 会归档过时超过 90 天的元数据。只有使用 API 才能评估此归档的元数据。 您可以拉取包含过时或归档元数据的映像,以重新扫描该映像。 刷新元数据最多可能需要 24 小时。无法重新扫描具有过时或已归档元数据的软件包。
在 Google Cloud 控制台中查看出现次数
如需查看映像中的漏洞,请执行以下操作:
获取代码库列表。
在代码库列表中,点击一个代码库。
在映像列表中,点击映像名称。
每个映像摘要的漏洞总数将显示在漏洞列中。
如需查看映像的漏洞列表,请点击漏洞列中的链接。
扫描结果部分会显示扫描的软件包类型、漏洞总数、有修复的漏洞、无修复的漏洞和有效严重程度的摘要。
漏洞表会列出发现的每个漏洞的常见漏洞和披露 (CVE) 名称、有效严重程度、通用漏洞评分系统 (CVSS) 评分、修复(如有)、包含漏洞的软件包的名称以及软件包类型。
Google Cloud 控制台会在该表格中显示每个页面最多 1,200 个漏洞。您可以过滤和排序这些文件,以按文件扩展名检查特定文件、目录、图层或文件类型。
如需详细了解特定 CVE,请点击该 CVE 的名称。
如需查看漏洞出现详情(例如版本号和受影响的位置),请在包含漏洞名称的行中点击查看或查看已修复。对于没有修复的漏洞,链接文字为查看;对于已应用修复的漏洞,链接文字为查看已修复。
查看特定层中的漏洞
Artifact Analysis 提供特定于层的元数据,可帮助您确定哪个软件包包含漏洞,以及哪个 Docker build 命令引入了相应层。如需在 Google Cloud 控制台中查看基于层的漏洞元数据,请执行以下操作:
在 Artifact Registry 中,打开您的代码库列表。
在代码库页面中,点击一个代码库。
在媒体库详情页面中,点击一张图片。
在映像版本列表中,点击映像摘要。
在映像摘要详情页面中,点击漏洞标签页。
当基于层的元数据可用时,Artifact Analysis 会在层栏中显示可供选择的层列表:
如需查看图层列表,请点击图层下拉菜单。
系统会打开一个下拉列表,其中显示了图层的编号列表。第 0 层代表底层,后续每一层均在前一层的基础上构建。系统只会显示已发现漏洞的层。
对于每个层,Artifact Analysis 会显示用于构建该层的 Docker 命令以及在该层中发现的漏洞数量。
如需详细了解特定层中的漏洞,请从层列表中选择相应层。
漏洞表格会更新为仅显示所选层中发现的漏洞。您可以应用更多过滤条件,也可以调查特定漏洞。
点击漏洞旁边的查看或查看修复,即可查看更详细的漏洞信息。
漏洞详情侧边栏会显示存在漏洞的软件包的文件位置。
在文件位置表格中,将光标悬停在问号上,以打开其他图层元数据。
对话框会显示层索引、层构建命令、层摘要和可用于修复漏洞的基本映像。
使用 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 查看出现次数
如需获取项目中发生实例的列表,请使用以下命令:
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