Puntos de control
Modify the constraint
/ 100
Validating Policies for Terraform on Google Cloud
GSP1021
Overview
In this lab, you will learn about the command gcloud beta terraform vet
, referred as Validate through the rest of the lab. Validate is a tool for enforcing policy compliance as part of an infrastructure CI/CD pipeline. You can use Validate to detect policy violations and provide warnings or halt deployments before they reach production.
This lab will walk you through applying a constraint that enforces a domain restriction. You'll test that constraint and intentionally throw an error. You'll then modify the constraint so that your domain passes.
Objectives
In this lab, you will:
- Apply a constraint that enforces a domain restriction
- Test a constraint to intentionally throw a validation error
- Modify the constraint so that it passes validation
Prerequisites
For this lab, you should have experience using Terraform. Check out the Build Infrastructure with Terraform on Google Cloud course for more hands-on practice with Terraform.
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).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
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 the Lab Details panel with the following:
- The Open Google Cloud console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}} You can also find the Username in the Lab Details panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}} You can also find the Password in the Lab Details panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
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 Google 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.
- Click Activate Cloud Shell at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
- Click Authorize.
Output:
- (Optional) You can list the project ID with this command:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Validate a constraint
To see how a constraint is implemented, you'll copy an existing constraint, provide a test case that fails validation, and then modify the constraint so that it passes.
Copy the constraint
- Open a new Cloud Shell window and run the following command to clone the policy library repository.
- At the command prompt, enter the following to copy over the sample IAM domain restriction constraint:
- Examine the constraint you copied by printing it to the terminal.
The output looks like this:
Notice the highlighted section at the bottom. This specifies that only members from the gserviceaccount.com
domain can be present in an IAM policy.
Test the constraint
- To verify that the policy works as expected, you'll create a Terraform file within the current directory.
-
On the Cloud Shell toolbar, click Open Editor. To switch between Cloud Shell and the code editor, click Open Editor or Open Terminal as required, or click Open in new window to leave the Editor open in a separate tab.
-
Open the
policy-library/main.tf
file and copy the following code into it:
-
Replace
<YOUR PROJECT ID>
with. -
Replace
<USER>
with. -
Next, navigate back to the terminal and run the following command to initialize Terraform:
- Run the following command to export the Terraform plan. If asked, click Authorize when prompted.
- Convert the Terraform plan to JSON.
- Install the Terraform Tools component.
- Run the following command to validate that your Terraform plan complies with your policies.
Since the email address you provided in the IAM policy binding does not belong to a service account, the plan violates the constraint you set up. Your output should resemble the following:
project_id
argument in policy-library/main.tf
, or you don't have the necessary permissions on the project you specified.
Task 2. Modify the constraint
The constraint works as intended, but let's say you want to modify this so you can allow other email addresses in your domain.
-
From the Editor, navigate to the
policy-library/policies/constraints/iam_service_accounts_only.yaml
file. -
Under the
domains
section, append theqwiklabs.net
email domain to the domains allowlist:
- Navigate back to the Cloud Shell window and export a new JSON Terraform plan.
- Now validate your Terraform plan again, and this should result in no violations found.
Output:
- Lastly, apply the Terraform plan for the IAM policy to grant a role to the member.
Click Check my progress to verify the objective.
Congratulations!
In this lab you applied a constraint that enforces a domain restriction. You tested that constraint, which threw an error. Then you modified the constraint so that domains you define can pass validation.
Next Steps / Learn More
Be sure to check out the following resources for more practice with Validator and Terraform:
Google Cloud training and 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 November 29, 2024
Lab Last Tested November 29, 2024
Copyright 2024 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.