ソフトウェア供給の保護

1. 概要

Artifact Registry を使用すると、さまざまなアーティファクト タイプを保存し、1 つのプロジェクトに複数のリポジトリを作成して、特定のリージョンまたはマルチリージョンを各リポジトリに関連付けることができます。リポジトリのモードは複数あります。各モードはそれぞれ異なる目的を果たします。次の図は、リポジトリをさまざまなモードで一緒に使用するさまざまな方法の一つを示しています。この図は、2 つの Google Cloud プロジェクトにまたがるワークフローを示しています。開発プロジェクトでは、開発者が Java アプリケーションを構築します。別のランタイム プロジェクトで、アプリケーションを含むコンテナ イメージが別のビルドによって作成され、Google Kubernetes Engine にデプロイされます。

5af5e4da3ccfdff3.png

このラボでは、次のタスクの実行方法について学びます。

  • プライベート パッケージのデプロイに標準リポジトリを使用する
  • Remote Repositories を使用して Maven セントラル パッケージをキャッシュに保存する
  • Virtual Repositories を使用して複数のアップストリーム リポジトリを 1 つの構成にまとめる

セルフペース型の環境設定

  1. Google Cloud Console にログインして、プロジェクトを新規作成するか、既存のプロジェクトを再利用します。Gmail アカウントも Google Workspace アカウントもまだお持ちでない場合は、アカウントを作成してください。

b35bf95b8bf3d5d8.png

a99b7ace416376c4.png

bd84a6d3004737c5.png

  • プロジェクト名は、このプロジェクトの参加者に表示される名称です。Google API では使用されない文字列です。この値はいつでも更新できます。
  • プロジェクト ID は、すべての Google Cloud プロジェクトにおいて一意でなければならず、不変です(設定後は変更できません)。Cloud コンソールでは一意の文字列が自動生成されます。通常、それが何であるかは関係ありません。ほとんどの Codelab では、プロジェクト ID を参照する必要があります(通常は PROJECT_ID として識別されます)。生成された ID が気に入らない場合は、別のランダムな ID を生成できます。または、ご自身でお試しになることもできます。このステップを終えた後は変更できず、プロジェクト期間中は維持されます。
  • なお、3 つ目の値は、一部の API で使用される [プロジェクト番号] です。これら 3 つの値について詳しくは、こちらのドキュメントをご覧ください。
  1. 次に、Cloud のリソースや API を使用するために、Cloud コンソールで課金を有効にする必要があります。この Codelab の操作をすべて行って、費用が生じたとしても、少額です。このチュートリアルの終了後に課金されないようにリソースをシャットダウンするには、作成したリソースを削除するか、プロジェクト全体を削除します。Google Cloud の新規ユーザーは、300 米ドル分の無料トライアル プログラムをご利用いただけます。

Workspace の設定

gcloud を設定する

Cloud Shell で、プロジェクト ID とプロジェクト番号を設定します。これらを PROJECT_ID 変数と PROJECT_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 セクションでは、デプロイ時にプロジェクトが push するリモート リポジトリを指定します。
  • extensions セクションに artifactregistry-maven-wagon が追加され、Artifact Registry への接続に必要な認証レイヤとトランスポート レイヤが有効になります
  • 注: 拡張機能は pom.xml または extensions.xml に存在できます。プロジェクトが親プロジェクトに依存している場合、pom.xml の残りのエントリが読み込まれる前に、それらの依存関係にアクセスします。親が拡張機能にアクセスできるようにするには、pom.xml の前に読み込まれる extensions.xml ファイルに配置し、親依存関係で使用できるようにします。

3 つのセクションをコピーしてから、Cloud Shell エディタで pom.xml を開き、返された設定をファイルの末尾の project タグのすぐ内に追加します。

ヒント: cloudshell で、ターミナルで以下のコマンドを実行して、現在のディレクトリでエディタを開きます。

cloudshell workspace .

例: (プロジェクト名は URL によって異なります)

  ...

  <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.png

3. リモート リポジトリ

Remote Repositories には、信頼性とセキュリティを高める目的で、サードパーティ パッケージをキャッシュに保存する機能があります。

リモート リポジトリを作成する

注: 認証と構成の詳細については、プロダクトのドキュメントをご覧ください。

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 を持つようにします。

例: (プロジェクト名は URL によって異なります)

  ...

  <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

リモート リポジトリから依存関係を pull する

次のコマンドを実行して、リモート リポジトリを使用してアプリケーションをコンパイルします。

rm -rf ~/.m2/repository 
mvn compile

コンソールでパッケージを確認する

Cloud コンソール - Artifact Registry - リポジトリに移動します。maven-central-cache をクリックして、バイナリ アーティファクトがキャッシュに保存されていることを確認します。

9deea93caa5fefd7.png

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 の repositories セクション全体を、出力の 1 つの仮想リポジトリ セクションに置き換えます。

例: (プロジェクト名は URL によって異なります)

  ...


  <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>


仮想リポジトリから依存関係を pull する

仮想リポジトリはパススルーであり、実際のパッケージは保存されないため、プロセスをわかりやすくするために、前に作成した 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 をクリックして、バイナリ アーティファクトが仮想リポジトリから pull するように構成されていて、最終的に maven-central-cache から pull されていることを確認します。

9deea93caa5fefd7.png

5. 完了

お疲れさまでした。これでこの Codelab は終了です。

学習した内容

  • プライベート パッケージのデプロイに Standard リポジトリを使用
  • Remote Repositories を使用して Maven セントラル パッケージをキャッシュに保存
  • Virtual Repositories を使用して複数のアップストリーム リポジトリを 1 つの構成にまとめる

クリーンアップ

次のコマンドを実行してプロジェクトを削除します。

gcloud projects delete ${PROJECT_ID}

最終更新日: 2023 年 3 月 22 日