安全软件供应

1. 概览

借助 Artifact Registry,您可以存储不同的工件类型,在单个项目中创建多个代码库,以及将特定区域或多区域与每个代码库相关联。有多种仓库模式。每种模式的用途各不相同。下图显示了您可以以不同模式一起使用代码库的多种可能方式之一。此图显示了两个 Google Cloud 项目的工作流。在开发项目中,开发者构建一个 Java 应用。在单独的运行时项目中,另一个构建包含该应用创建一个容器映像,以部署到 Google Kubernetes Engine。

5af5e4da3ccfdff3.png

在本实验中,您将学习如何执行以下任务。

  • 使用标准代码库部署您的专用软件包
  • 使用远程仓库缓存 Maven 中央软件包
  • 使用虚拟代码库将多个上游代码库合并到一个配置中

自定进度的环境设置

  1. 登录 Google Cloud 控制台,然后创建一个新项目或重复使用现有项目。如果您还没有 Gmail 或 Google Workspace 账号,则必须创建一个

b35bf95b8bf3d5d8.png

a99b7ace416376c4.png

bd84a6d3004737c5.png

  • 项目名称是此项目参与者的显示名称。它是 Google API 尚未使用的字符串。您可以随时对其进行更新。
  • 项目 ID 在所有 Google Cloud 项目中是唯一的,并且是不可变的(一经设置便无法更改)。Cloud 控制台会自动生成一个唯一字符串;通常您不在乎这是什么在大多数 Codelab 中,您都需要引用项目 ID(它通常标识为 PROJECT_ID)。如果您不喜欢生成的 ID,可以再随机生成一个 ID。或者,您也可以尝试自己的项目 ID,看看是否可用。完成此步骤后便无法更改该 ID,并且该 ID 在项目期间会一直保留。
  • 此外,还有第三个值,即某些 API 使用的项目编号,供您参考。如需详细了解所有这三个值,请参阅文档
  1. 接下来,您需要在 Cloud 控制台中启用结算功能,以便使用 Cloud 资源/API。运行此 Codelab 应该不会产生太多的费用(如果有费用的话)。如需关停资源,以免产生超出本教程范围的结算费用,您可以删除自己创建的资源或删除整个项目。Google Cloud 的新用户符合参与 $300 USD 免费试用计划的条件。

Workspace 设置

设置 gcloud

在 Cloud Shell 中,设置您的项目 ID 和项目编号。将它们保存为 PROJECT_IDPROJECT_NUMBER 变量。

export PROJECT_ID=$(gcloud config get-value project)
export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')

启用 API

gcloud services enable artifactregistry.googleapis.com

克隆代码库

git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/container-registry/container-analysis

2. 标准代码库

标准代码库提供了一种存储私有软件包并在其他应用之间共享它们的方法

创建标准 Maven 制品库

在 Cloud Shell 中运行以下命令,为 Java 工件创建代码库:

gcloud artifacts repositories create container-dev-java-repo \
    --repository-format=maven \
    --location=us-central1 \
    --description="Java package repository for Container Dev Workshop"

如果出现 Cloud Shell 授权提示,请点击“授权”

转到 Google Cloud 控制台 - Artifact Registry - 仓库,并注意您新建的名为 container-dev-java-repo 的 Maven 制品库。如果您点击此仓库,就会看到该仓库目前为空。

gcloud artifacts repositories describe container-dev-java-repo \
    --location=us-central1

应返回类似于以下内容的响应

Encryption: Google-managed key
Repository Size: 0.000MB
createTime: '2023-03-21T19:01:45.461589Z'
description: Java package repository for Container Dev Workshop
format: MAVEN
mavenConfig: {}
mode: STANDARD_REPOSITORY
name: projects/qwiklabs-gcp-03-4304110dc461/locations/us-central1/repositories/container-dev-java-repo
updateTime: '2023-03-21T19:01:45.461589Z'

为 Artifact Registry 配置 Maven

运行以下命令,输出要添加到 Java 项目的代码库配置:

gcloud artifacts print-settings mvn \
    --repository=container-dev-java-repo \
    --location=us-central1

上一条命令会返回要添加到您项目的 pom.xml 中的 xml。

  • repositories 部分指定 Maven 可以在哪里下载供当前项目使用的远程工件。
  • distributionManagement 部分指定项目在部署时要推送到哪个远程代码库。
  • 扩展程序部分添加了 artifactregistry-maven-wagon,用于启用连接到 Artifact Registry 所需的身份验证和传输层
  • 注意:扩展程序可存在于 pom.xml 或 extensions.xml 中。如果项目依赖于父项目,则系统会先访问这些依赖项,然后再加载 pom.xml 中的其余条目。为确保父级有权访问扩展,可以将其放入 extensions.xml 文件中,该文件在 pom.xml 之前加载,因此可供父级依赖项使用。

复制这三个部分,然后在 Cloud Shell Editor 中打开 pom.xml,并将返回的设置添加到文件底部的 project 结束标记内。

提示:在 cloudshell 中,在终端运行以下命令,在当前目录中打开编辑器。

cloudshell workspace .

示例:(项目名称在网址中会有所不同)

  ...

  <distributionManagement>
    <snapshotRepository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </snapshotRepository>
    <repository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </repository>
  </distributionManagement>

  <repositories>
    <repository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>

  <build>
    <extensions>
      <extension>
        <groupId>com.google.cloud.artifactregistry</groupId>
        <artifactId>artifactregistry-maven-wagon</artifactId>
        <version>2.2.0</version>
      </extension>
    </extensions>
  </build>

</project>

将 Java 软件包上传到 Artifact Registry

在 Maven 中配置 Artifact Registry 后,您现在可以使用 Artifact Registry 存储 Java JAR 文件,以供组织中的其他项目使用。

运行以下命令,将 Java 软件包上传到 Artifact Registry:

mvn deploy -DskipTests

如果要再次运行此命令,请确保增加 pom.xml 中的版本。

检查 Artifact Registry 中的 Java 软件包

转到 Cloud 控制台 - Artifact Registry - 代码库。点击进入 container-dev-java-repo,检查是否存在 hello-world 二进制工件:

147eac5168648db1

3. 远程仓库

远程代码库能够缓存第三方软件包,以提高可靠性和安全性。

创建远程仓库

注意:如需详细了解身份验证和配置,请参阅产品文档

在 Cloud Shell 中运行以下命令,为 Maven Central 制品创建远程代码库:

gcloud artifacts repositories create maven-central-cache \
    --project=$PROJECT_ID \
    --repository-format=maven \
    --location=us-central1 \
    --description="Remote repository for Maven Central caching" \
    --mode=remote-repository \
    --remote-repo-config-desc="Maven Central" \
    --remote-mvn-repo=MAVEN-CENTRAL

在控制台中查看代码库

转到 Cloud 控制台 - Artifact Registry - 代码库。点击进入 maven-central-cache,您会发现它已创建且当前为空

在终端中查看代码库

gcloud artifacts repositories describe maven-central-cache \
    --location=us-central1

将代码库集成到您的项目中

运行以下命令,输出要添加到 Java 项目的代码库配置:

gcloud artifacts print-settings mvn \
    --repository=maven-central-cache \
    --location=us-central1

将仓库部分添加到您的 pom.xml 中。请勿复制外部 <repositories>标记。

将新添加的代码库的 ID 更改为“central”以确保每个代码库条目都具有唯一 ID。

示例:(项目名称在网址中会有所不同)

  ...

  <distributionManagement>
    <snapshotRepository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </snapshotRepository>
    <repository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </repository>
  </distributionManagement>

  <repositories>
    <repository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>

    <repository>
      <id>central</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/maven-central-cache</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>


  </repositories>

  <build>
    <extensions>
      <extension>
        <groupId>com.google.cloud.artifactregistry</groupId>
        <artifactId>artifactregistry-maven-wagon</artifactId>
        <version>2.2.0</version>
      </extension>
    </extensions>
  </build>

</project>

在终端中运行以下命令,为您的项目创建 extensions.xml。如需使用核心扩展机制,确保 Maven 可以从 Artifact Registry 解析父级或插件依赖项。

mkdir .mvn 
cat > .mvn/extensions.xml << EOF
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
  <extension>
    <groupId>com.google.cloud.artifactregistry</groupId>
    <artifactId>artifactregistry-maven-wagon</artifactId>
    <version>2.2.0</version>
  </extension>
</extensions>
EOF

从远程仓库中拉取依赖项

运行以下命令,使用远程仓库编译您的应用:

rm -rf ~/.m2/repository 
mvn compile

在控制台中查看软件包

转到 Cloud 控制台 - Artifact Registry - 代码库。点击进入 maven-central-cache,检查其中缓存的二进制制品:

9deea93caa5fefd7

4. 虚拟代码库

虚拟代码库充当通过单个配置访问多个代码库的接口。这样可以为制品的使用者简化客户端配置,并通过缓解依赖项混淆攻击提高安全性。

创建政策文件

cat > ./policy.json << EOF
[
  {
    "id": "private",
    "repository": "projects/${PROJECT_ID}/locations/us-central1/repositories/container-dev-java-repo",
    "priority": 100
  },
  {
    "id": "central",
    "repository": "projects/${PROJECT_ID}/locations/us-central1/repositories/maven-central-cache",
    "priority": 80
  }
]

EOF

创建虚拟代码库

gcloud artifacts repositories create virtual-maven-repo \
    --project=${PROJECT_ID} \
    --repository-format=maven \
    --mode=virtual-repository \
    --location=us-central1 \
    --description="Virtual Maven Repo" \
    --upstream-policy-file=./policy.json

将代码库集成到您的项目中

运行以下命令,输出要添加到 Java 项目的代码库配置:

gcloud artifacts print-settings mvn \
    --repository=virtual-maven-repo \
    --location=us-central1

将 pom 中的整个代码库部分替换为输出中的一个虚拟代码库部分。

示例:(项目名称在网址中会有所不同)

  ...


  <distributionManagement>
    <snapshotRepository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </snapshotRepository>
    <repository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/container-dev-java-repo</url>
    </repository>
  </distributionManagement>

  <repositories>
    <repository>
      <id>artifact-registry</id>
      <url>artifactregistry://us-central1-maven.pkg.dev/qwiklabs-gcp-04-3c51830ea757/virtual-maven-repo</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>

  <build>
    <extensions>
      <extension>
        <groupId>com.google.cloud.artifactregistry</groupId>
        <artifactId>artifactregistry-maven-wagon</artifactId>
        <version>2.2.0</version>
      </extension>
    </extensions>
  </build>

</project>


从虚拟代码库中拉取依赖项

由于虚拟仓库是一种直通车,不存储任何实际软件包,因此为了清晰地展示这个过程,您需要删除之前创建的 maven-central-cache 仓库并重新创建,以便再次使用空仓库重新开始

运行以下命令以重新创建缓存存储库

gcloud artifacts repositories delete maven-central-cache \
    --project=$PROJECT_ID \
    --location=us-central1 \
    --quiet

gcloud artifacts repositories create maven-central-cache \
    --project=$PROJECT_ID \
    --repository-format=maven \
    --location=us-central1 \
    --description="Remote repository for Maven Central caching" \
    --mode=remote-repository \
    --remote-repo-config-desc="Maven Central" \
    --remote-mvn-repo=MAVEN-CENTRAL

您可以在控制台中查看空代码库。转到 Cloud 控制台 - Artifact Registry - 代码库

现在,使用以下命令构建您的项目,以练习虚拟代码库

rm -rf ~/.m2/repository 
mvn compile

在控制台中查看软件包

前往 Cloud 控制台 - Artifact Registry - 代码库。点击进入 maven-central-cache,检查二进制制品是否配置为从虚拟代码库拉取,但最终是从 maven-central-cache 拉取:

9deea93caa5fefd7

5. 恭喜!

恭喜,您已完成此 Codelab!

所学内容

  • 使用标准代码库部署您的专用软件包
  • 使用远程仓库缓存 Maven 中央软件包
  • 使用虚拟代码库将多个上游代码库合并到一个配置中

清理

运行以下命令以删除项目

gcloud projects delete ${PROJECT_ID}

上次更新时间:2023 年 3 月 22 日