Securing Microservices in Cloud Run

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

In this lab, you’ll be tasked with deploying an off-the-shelf Cloud Run application that takes headless screenshots of web page URLs. The screenshots are written to a Google Cloud Storage bucket. However, to reduce an attack vector and pass an internal security audit, you must ensure your public web service has no actual access to Google Cloud Storage. To accomplish this, you will deploy a public frontend, which can only speak to the backend. The backend itself must also follow the principle of least privilege. To perform this hands-on lab, you should be familiar with the Cloud Shell and have some experience with Cloud Run.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Build the Frontend and Backend Container Images
  1. Obtain the code from GitHub:

    git clone https://github.com/timhberry/screenshot screenshot-backend
  2. Create a Cloud Storage bucket in your project with the naming convention: your-project-screenshots.

  3. Update screenshot.js in the backend application, replacing the values of GOOGLE_CLOUD_PROJECT_ID and BUCKET_NAME with your project ID and the name of the bucket you just created.

  4. Use Cloud Build to create container images called screenshot-frontend and screenshot-backend, and store them in Container Registry in your project.

Deploy the Backend Service
  1. Create a service account for the backend service called backend-identity.

  2. Create an IAM policy binding for the project that grants this service account the role of roles/storage.objectCreator. (Hint: Use the gcloud projects add-iam-policy-binding command.)

  3. Deploy the screenshot-backend service, using the backend-identity service account as its runtime identity. Do not allow unauthenticated invocations, and grant 512 mebibytes of memory to the service.

  4. Test that the service has been deployed successfully with curl in the Cloud Shell terminal. First, obtain an auth token:

    TOKEN=$(gcloud auth print-identity-token)
  5. Access your service URL, with the token in the header, specifying a further URL to screenshot (in this example, we screenshot google.com):

    curl -H "Authorization: Bearer $TOKEN" https://<SCREENSHOT-BACKEND_URL>.run.app/?url=https://www.google.com
Deploy the Frontend Service
  1. Create a service account for the frontend service called frontend-identity.
  2. Create an IAM policy binding that says the frontend-identity service account is permitted to invoke the screenshot-backend service. (Hint: Use the gcloud run services add-iam-policy-binding command.)
  3. Deploy the screenshot-frontend service, using the frontend-identity service account as its runtime identity. Allow unauthenticated invocations, and use --set-env-vars to set a BACKEND_URL environment variable with the backend-service URL as its value.
  4. If you are successful, you should be able to access your frontend service and provide it with a URL to screenshot, then find that screenshot in your GCS bucket.

Additional Resources

Log in to Google Cloud Platform by right-clicking Open Google Console and selecting the option to open it in a new private browser window. Then, sign in using the credentials provided on the lab page.

The code for the screenshot-frontend application can be found in this Git repo.

The modified fork of Google's screenshot service that writes to GCS can be found in this Git repo. For simplicity, you can clone it into a directory called screenshot-backend with this command:

git clone https://github.com/timhberry/screenshot screenshot-backend

You can enable the APIs required for this lab with:

gcloud services enable cloudbuild.googleapis.com run.googleapis.com

You should also set some defaults for Cloud Run:

gcloud config set run/platform managed
gcloud config set run/region us-east1

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!