
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
JSON payloads can be crafted to cause problems when loaded into JSON parsers. In this lab, you learn how to protect against this type of payload.
In this lab, you learn how to perform the following task:
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Sign in to Qwiklabs using an incognito window.
Note the lab's access time (for example, 1:15:00
), and make sure you can finish within that time.
There is no pause feature. You can restart if needed, but you have to start at the beginning.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
Click Use another account and copy/paste credentials for this lab into the prompts.
If you use other credentials, you'll receive errors or incur charges.
Accept the terms and skip the recovery resource page.
Google 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.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
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:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
These assets have already been added to the Apigee organization:
These assets will be added to the Apigee organization as soon as the runtime is available:
The highlighted items are used during this lab.
In this task, you add a JSONThreatProtection policy to protect against malicious JSON payloads.
JSON payloads can be crafted to cause issues when they are parsed. The JSONThreatProtection policy can check a JSON payload against configured limits without loading the payload into a parser.
In the Google Cloud console, on the Navigation menu (), look for Apigee in the Pinned Products section.
The Apigee console page will open.
If Apigee is not pinned, search for Apigee in the top search bar and navigate to the Apigee service.
Hover over the name, then click the pin icon ().
The Apigee console page will now be pinned to the Navigation menu.
On the left navigation menu, select Proxy development > API proxies.
Select the retail-v1 proxy.
Click the Develop tab.
You are modifying the version of the retail-v1 proxy that was created during Labs 1 through 4.
Select Proxy endpoints > default > createOrder.
The createOrder flow is selected.
On the Request createOrder flow, click Add Policy Step (+).
In the Add policy step pane, select Create new policy, and then select Security > JSON Threat Protection.
Specify the following values:
Property | Value |
---|---|
Name | JSONTP-Protect |
Display name | JSONTP-Protect |
Click Add.
Click on Policies > JSONTP-Protect.
Replace the policy's default configuration with:
The Source is set to request, so the incoming request's payload will be validated.
The numbers indicate limits that should be enforced for the structure of JSON payloads. For example, ContainerDepth indicates how many levels deep your objects and arrays can go. An object (1) containing an array (2) which contains objects (3) would have a ContainerDepth of 3. This policy configuration only allows a ContainerDepth of 2, so the request would be rejected.
To understand each of the parameters, refer to the JSONThreatProtection policy documentation.
To save the updates, click Save, and then click Save as New Revision.
Click Deploy.
To specify that you want the new revision deployed to the eval environment, select eval as the Environment, and then click Deploy.
Click Confirm.
In this task, you store the application's API key in a shell variable.
Certain assets, including API products, developers, developer apps, and KVMs, cannot be saved until the runtime is available.
For example, when navigating to the API products page, you might see an error message that reads "Products were not loaded successfully."
This is an error you should see when you are waiting for the runtime instance to be available. Once the runtime is available, refreshing the page will remove the error.
If you get this type of error, you can check the status of provisioning.
In Cloud Shell, to confirm that the runtime instance has been installed and the eval environment has been attached, run the following commands:
When the script returns ORG IS READY TO USE
, you can proceed to the next steps.
The eval environment in the Apigee organization can be called using the hostname eval.example.com. The DNS entry for this hostname has been created within your project, and it resolves to the IP address of the Apigee runtime instance. This DNS entry has been created in a private zone, which means it is only visible on the internal network.
Cloud Shell does not reside on the internal network, so Cloud Shell commands cannot resolve this DNS entry. A virtual machine (VM) within your project can access the private zone DNS. A virtual machine named apigeex-test-vm was automatically created for this purpose. You can make API proxy calls from this machine.
The curl command will be used to send API requests to an API proxy. The -k
option for curl tells it to skip verification of the TLS certificate. For this lab, the Apigee runtime uses a self-signed certificate. For a production environment, you should use certificates that have been created by a trusted certificate authority (CA).
In Cloud Shell, open a new tab, and then open an SSH connection to your test VM:
The first gcloud command retrieves the zone of the test VM, and the second opens the SSH connection to the VM.
If asked to authorize, click Authorize.
For each question asked in the Cloud Shell, click Enter or Return to specify the default input.
Your logged in identity is the owner of the project, so SSH to this machine is allowed.
Your Cloud Shell session is now running inside the VM.
The API key may be retrieved directly from the app accessible on the Publish > Apps page. It can also be retrieved via Apigee API call.
In the Cloud Shell SSH session, run the following command:
This command retrieves a Google Cloud access token for the logged-in user, sending it as a Bearer token to the Apigee API call. It retrieves the retail-app app details as a JSON response, which is parsed by jq to retrieve the app's key. That key is then put into the API_KEY environment variable, and the export command is concatenated onto the .bashrc file which runs automatically when starting a the SSH session.
In this task, you send requests and relax the limits of your policy until the request is allowed past the JSONThreatProtection policy.
To install the required package, you can use the following command:
In the Cloud Shell SSH session, execute this curl command:
This returns the error message Invalid ApiKey for given resource.
Joe's app is associated with the read-only API product. We are now trying to do a POST, which requires the full access product.
Return to the Apigee console tab.
Navigate to Distribution > Apps.
Select Joe's retail app.
Navigate to Proxy development > API Proxies.
Select the retail-v1 proxy, and then click the Develop tab.
Click Policies > JSONTP-Protect.
In the Cloud Shell SSH session, resubmit this command:
You should see a 500 error that looks like this:
The object entry name on line 2 is orderNumber, which is 11 characters long. Looking at the policy configuration, the ObjectEntryNameLength is set to 10. Note that only the first error is returned when a limit is exceeded, even though there might be multiple errors. The longest object entry name in the payload is promisedDeliveryDate, which is 20 characters long.
Set the ObjectEntryNameLength in the JSONThreatProtection policy to 30.
Click Save, and then click Save as New Revision.
Click Deploy.
To specify that you want the new revision deployed to the eval environment, click Deploy, and then click Confirm.
Wait for the updated proxy to be deployed.
Click Policies > JSONTP-Protect.
Resubmit the curl command.
You should get a new error:
The maximum depth in the payload is 3 (object containing an array which contains objects), but the policy only allows a container depth of 2.
Set the ContainerDepth in the JSONThreatProtection policy to 5.
If you look at the payload, you can also see that the maximum string length of 20 is exceeded by the deliveryNotes field.
Set the StringValueLength to 100.
Click Save, and then click Save as New Revision.
Click Deploy.
To specify that you want the new revision deployed to the eval environment, click Deploy, and then click Confirm.
Wait for the updated proxy to be deployed.
Resubmit the curl command.
This time, you should receive a 200 response that resembles this:
The request successfully passed the JSONThreatProtection policy and was sent to the backend.
You can retrieve the order by sending the following curl call, with REPLACE set to the name that was returned by the previous request:
In this lab, you protected the createOrders resource against malicious JSON by using the JSONThreatProtection policy and configuring it with the correct limits.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 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.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one