소프트웨어 공급 보호

1. 개요

Artifact Registry를 사용하면 여러 아티팩트 유형을 저장하고, 단일 프로젝트에 여러 저장소를 만들고, 특정 리전 또는 멀티 리전을 각 저장소와 연결할 수 있습니다. 저장소 모드는 여러 가지입니다. 각 모드는 서로 다른 용도로 사용됩니다. 다음은 여러 모드에서 저장소를 함께 사용할 수 있는 여러 방법 중 하나를 보여주는 다이어그램입니다. 이 다이어그램은 두 Google Cloud 프로젝트의 워크플로를 보여줍니다. 이 중 하나의 개발 프로젝트에서 개발자는 Java 애플리케이션을 빌드합니다. 또 다른 런타임 프로젝트에서는 다른 빌드가 Google Kubernetes Engine에 배포할 애플리케이션과 함께 컨테이너 이미지를 만듭니다.

5af5e4da3ccfdff3.png

이 실습에서는 다음 작업을 수행하는 방법을 알아봅니다.

  • 표준 저장소를 사용하여 비공개 패키지 배포
  • 원격 저장소를 사용하여 Maven Central 패키지 캐시
  • 가상 저장소를 사용하여 하나의 구성에서 여러 업스트림 저장소 결합

자습형 환경 설정

  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를 생성할 수 있습니다. 또는 직접 시도해 보고 사용 가능한지 확인할 수도 있습니다. 이 단계 이후에는 변경할 수 없으며 프로젝트 기간 동안 유지됩니다.
  • 참고로 세 번째 값은 일부 API에서 사용하는 프로젝트 번호입니다. 이 세 가지 값에 대한 자세한 내용은 문서를 참고하세요.
  1. 다음으로 Cloud 리소스/API를 사용하려면 Cloud 콘솔에서 결제를 사용 설정해야 합니다. 이 Codelab 실행에는 많은 비용이 들지 않습니다. 이 튜토리얼이 끝난 후에 요금이 청구되지 않도록 리소스를 종료하려면 만든 리소스 또는 전체 프로젝트를 삭제하면 됩니다. Google Cloud 새 사용자에게는 미화 $300 상당의 무료 체험판 프로그램에 참여할 수 있는 자격이 부여됩니다.

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 - 저장소로 이동하여 새로 생성된 Maven 저장소 container-dev-java-repo를 확인합니다. 이 저장소를 클릭하면 현재 비어 있음을 알 수 있습니다.

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 섹션은 프로젝트가 배포될 때 푸시할 원격 저장소를 지정합니다.
  • extensions 섹션은 Artifact Registry에 연결하는 데 필요한 인증 및 전송 계층을 지원하는 artifactregistry-maven-wagon을 추가합니다.
  • 참고: 확장 프로그램은 pom.xml 또는 extensions.xml에 있을 수 있습니다. 프로젝트가 상위 프로젝트에 종속되는 경우 pom.xml의 나머지 항목이 로드되기 전에 이러한 종속 항목에 액세스합니다. 상위 항목이 확장 프로그램에 액세스할 수 있도록 하려면 pom.xml 전에 로드되는 extensions.xml 파일에 배치하여 상위 항목 종속 항목에 사용할 수 있도록 하면 됩니다.

세 섹션을 복사한 후 Cloud Shell 편집기에서 pom.xml을 열고 반환된 설정을 닫는 project 태그 바로 아래 파일 하단에 추가합니다.

도움말: Cloud Shell에서 터미널에 다음 명령어를 실행하여 현재 디렉터리에서 편집기를 엽니다.

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>

Artifact Registry에 Java 패키지 업로드

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. 원격 저장소

원격 저장소는 안정성과 보안을 높이기 위해 서드 파티 패키지를 캐시하는 기능을 제공합니다.

원격 저장소 만들기

참고: 인증 및 구성에 대한 자세한 내용은 제품 문서를 참고하세요.

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가 있도록 새로 추가된 저장소의 ID를 'central'로 변경합니다.

예: (프로젝트 이름은 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

원격 저장소에서 종속 항목 가져오기

다음 명령어를 실행하여 원격 저장소를 통해 애플리케이션을 컴파일합니다.

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의 전체 저장소 섹션을 출력의 가상 저장소 섹션 하나로 바꿉니다.

예: (프로젝트 이름은 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>


가상 저장소에서 종속 항목 가져오기

가상 저장소는 패스 스루이며 실제 패키지를 저장하지 않으므로 프로세스를 명확하게 보여주기 위해 이전에 만든 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.png

5. 축하합니다.

축하합니다. Codelab을 완료했습니다.

학습한 내용

  • 표준 저장소를 사용하여 비공개 패키지 배포
  • 원격 저장소를 사용하여 Maven Central 패키지 캐시
  • 가상 저장소를 사용하여 하나의 구성에서 여러 업스트림 저장소 결합

삭제

다음 명령어를 실행하여 프로젝트를 삭제합니다.

gcloud projects delete ${PROJECT_ID}

최종 업데이트: 2023년 3월 22일