arrow_back

Securing Container Builds

登录 加入
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

Securing Container Builds

Lab 1 小时 30 分钟 universal_currency_alt 1 个积分 show_chart 入门级
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

GSP1185

Google Cloud Self-Paced Labs

Overview

Artifact Registry enables you to store different artifact types, create multiple repositories in a single project, and associate a specific region or multi-region with each repository. There are several repository modes. Each mode serves a different purpose. The following diagram shows one of many possible ways you can use repositories in different modes together. The diagram shows a workflow across two Google Cloud projects. In a development project, developers build a Java application. In a separate runtime project, another build creates a container image with the application for deployment to Google Kubernetes Engine.

diagram of modes

In this lab, you learn how to perform the following tasks.

  • Use Standard Repositories for deploying your private packages
  • Use Remote Repositories to cache maven central packages
  • Use Virtual Repositories to combine multiple upstream repos in one config

Setup and Requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is a panel populated with the temporary credentials that you must use for this lab.

    Open Google Console

  2. Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Sign in

    Tip: Open the tabs in separate windows, side-by-side.

  3. In the Sign in page, paste the username that you copied from the left panel. Then copy and paste the password.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Training credentials. If you have your own Google Cloud account, do not use it for this lab (avoids incurring charges).

  4. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Cloud Shell icon

Click Continue.

cloudshell_continue.png

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:

Cloud Shell Terminal

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

You can list the active account name with this command:

gcloud auth list

(Output)

Credentialed accounts: - <myaccount>@<mydomain>.com (active)

(Example output)

Credentialed accounts: - google1623327_student@qwiklabs.net

You can list the project ID with this command:

gcloud config list project

(Output)

[core] project = <project_ID>

(Example output)

[core] project = qwiklabs-gcp-44776a13dea667a6

Workspace Setup

  1. In Cloud Shell, set your project ID and project number. Save them as PROJECT_ID and PROJECT_NUMBER variables:
export PROJECT_ID=$(gcloud config get-value project) export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')
  1. Enable the Artifact Registry API:
gcloud services enable artifactregistry.googleapis.com
  1. Clone the repo needed for this lab, and then go to the container-analysis directory:
git clone https://github.com/GoogleCloudPlatform/java-docs-samples cd java-docs-samples/container-registry/container-analysis

Task 1. Standard repositories

Standard Repositories provide a way to store your private packages and share them across your other applications

  1. Run the following command to create a standard maven repository for Java artifacts:
gcloud artifacts repositories create container-dev-java-repo \ --repository-format=maven \ --location=us-central1 \ --description="Java package repository for Container Dev Workshop"

Click Authorize if the Cloud Shell authorization prompt appears.

  1. In the Cloud Console go to Artifact Registry > Repositories and notice your newly created Maven repository named container-dev-java-repo. If you click on it you can see that it's empty at the moment.

  2. Review the repo in the terminal:

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

Should return a response similar to the following

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'

Click Check my progress to verify the objective. Create a standard maven repository

Task 2. Configure Maven for Artifact Registry

  1. Run the following command to print the repository configuration to add to your Java project:
gcloud artifacts print-settings mvn \ --repository=container-dev-java-repo \ --location=us-central1

The previous command returns xml to be added into your projects pom.xml.

  • The repositories section specifies where Maven may download remote artifacts for use by the current project.
  • The distributionManagement section specifies which remote repository the project will push to when it is deployed.
  • The extensions section adds in artifactregistry-maven-wagon which enables the Authentication and transport layer needed for connecting to Artifact Registry
  • Note: Extensions can exist in pom.xml or extensions.xml. In cases where the project depends on a parent project, those dependencies are accessed before the rest of the entries in the pom.xml are loaded. To ensure the parent has access to the extension, it can be placed in an extensions.xml file which is loaded before the pom.xml thus making it available for the parent dependencies.
  1. Run the following command in Cloud Shell to open the Editor in the current directory:
cloudshell workspace .
  1. Copy the three sections then open the pom.xml in Cloud Shell Editor and add the returned settings to the bottom of the file just inside the closing project tag.

Example: (your project names will be different in your URLs)

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

With Artifact Registry configured in Maven, you can now use Artifact Registry to store Java jars for use by other projects in your organization.

  1. Run the following command to upload your Java package to Artifact Registry:
mvn deploy -DskipTests

If you want to run this command again, make sure to increase the version in the pom.xml.

  1. In the Cloud console go to Artifact Registry > Repositories. Click into container-dev-java-repo and check that the hello-world binary artifact is there:
Artifact Registry Repository Details

Task 3. Remote repositories

Remote Repositories provide the ability to cache third party packages for increased reliability and security.

  1. Run the following command to create a remote repository for Maven Central artifacts:
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
  1. In the Cloud console go to Artifact Registry > Repositories. Click into maven-central-cache and notice it's been created and is currently empty.

Click Check my progress to verify the objective. Create a remote repository

  1. Review the repo in the terminal:
gcloud artifacts repositories describe maven-central-cache \ --location=us-central1
  1. Run the following command to print the repository configuration to add to your Java project:
gcloud artifacts print-settings mvn \ --repository=maven-central-cache \ --location=us-central1
  1. Add the repository section into your pom.xml. Be sure not to copy the outer <repositories> tag from the output.

  2. Change the ID of the newly added repository to "central" to ensure each repository entry has a unique ID.

Example: (your project names will be different in your URLs)

... <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>
  1. Run the following commands in your terminal to create an extensions.xml for your project, To use the core extensions mechanism ensuring Maven can resolve parent or plugin dependencies from 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
  1. Run the following command to compile your application using the Remote Repository:
rm -rf ~/.m2/repository mvn compile
  1. In the Cloud console go to Artifact Registry > Repositories. Click into maven-central-cache and check that the binary artifacts cached there:
Artifact Registry Repository Details

Task 4. Virtual repositories

Virtual Repositories act as an interface for multiple repositories to be accessed through a single configuration. This simplifies client configuration for consumers of your artifacts and increases security by mitigating dependency confusion attacks.

  1. Create a policy file
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
  1. Create the virtual repository
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

Click Check my progress to verify the objective. Create a virtual repository

  1. Run the following command to print the repository configuration to add to your Java project:
gcloud artifacts print-settings mvn \ --repository=virtual-maven-repo \ --location=us-central1
  1. Replace the entire repositories section in your pom with the one virtual repositories section from the output.

Example: (your project names will be different in your URLs)

... <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 dependencies from the Virtual Repository

Since the Virtual repository is a pass through and won't store any actual packages, to clearly demonstrate the process you'll delete the maven-central-cache repo you created earlier and recreate it, to start again with an empty repository

  1. Run the following commands to recreate the cache repository
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
  1. You can review the empty repo in the console. Cloud console > Artifact Registry > Repositories

  2. Now exercise the virtual repository by building your project with the following command:

rm -rf ~/.m2/repository mvn compile
  1. Review the packages in the console. Cloud Console > Artifact Registry > Repositories. Click into maven-central-cache and check that the binary artifacts were configured to pull from the virtual repo but were ultimately pulled from the maven-central-cache.
Artifact Registry repository Details

Congratulations!

You've learned how to use several repository modes and the various purposes those repositories serve.

Next steps / Learn more

Google Cloud Training & Certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated December 06, 2023

Lab Last Tested December 06, 2023

Copyright 2023 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.