In this exercise, you will use APIcast policies to customize how requests to a product are handled.
Outcomes
You should be able to add, update, reorder, and delete policies within a product.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command creates a new product called gateways_policies to house the policies.
This product uses a basic echo backend that responds with information about the request.
[student@workstation ~]$ lab start gateways-policies
Procedure 3.2. Instructions
Explore the product and backend before applying changes via policies.
In a web browser, navigate to the Admin Portal hosted at https://3scale-admin.apps.ocp4.example.com.
Using the top pane navigation drop-down, click , and then click .
Navigate to → and click to promote the configuration to the staging environment.
In a command-line terminal, run the example curl command.
Be sure to use the correct user_key value.
[student@workstation ~]$ curl \
https://gateways-policies-3scale-apicast-staging.apps.ocp4.example.com\
:443/echo?user_key=some-user-key
{
"method": "GET",
"path": "/",
...output omitted...
}Notice that the response echoes information from the request.
Copy just the generated user_key value from the example curl command.
You will use this in one of the following steps to configure a policy.
For example, if your curl command is similar to the one in the preceding step, then the user_key to copy would be some-user-key.
Attempt to make a request to the endpoint without providing a user_key.
This request fails due to missing authentication parameters.
[student@workstation ~]$ curl \
https://gateways-policies-3scale-apicast-staging.apps.ocp4.example.com:443/echo
Authentication parameters missingCreate a policy that allows unauthenticated access to the product.
Within the Admin Portal, navigate to → .
Notice the existing policy chain contains a single policy: 3scale APIcast.
Add a policy to this chain by clicking .
Select Anonymous Access from the list of available policies.
Update the policy by clicking within the policy chain.
Ensure the policy is enabled and user_key is selected for the auth_type field.
Paste your user key value into the user_key field and click .
Within the policy chain, use the arrow icon to drag the Anonymous Access policy so that it comes before the 3scale APIcast policy.
![]() |
Click to apply the new policy to the policy chain.
Deploy the changes to the staging environment by navigating to → and clicking .
From a command-line terminal, run the example curl command, but without providing the user_key.
[student@workstation ~]$ curl \
https://gateways-policies-3scale-apicast-staging.apps.ocp4.example.com:443/echo
{
"method": "GET",
"path": "/",
...output omitted...
}Notice that the request is processed by the echo API without providing a user key. Instead, the policy provides the configured user key.
Use caution when applying the Anonymous Access policy, as anybody with a valid URL can make requests to any API available to the product.
In particular, these requests have the same permissions as the user whose key is configured within the policy.
Create a policy that puts the product in maintenance mode and returns an appropriate response.
Navigate back to → .
Add a policy to this chain, selecting the Maintenance Mode policy.
Click to apply the new policy.
Deploy the changes to the staging environment by navigating to → and clicking .
From a command-line terminal, run the example curl command.
With the Anonymous Access policy still active, do not provide the user_key.
[student@workstation ~]$curl -i \ https://gateways-policies-3scale-apicast-staging.apps.ocp4.example.com:443/echoHTTP/1.1503Service Temporarily Unavailable ...output omitted... Service Unavailable - Maintenance
Notice that the service responds with a maintenance message and a 503 status code.
Remove the maintenance mode policy to restore the service.
Navigate back to → .
Click the policy to update it. At the bottom of the form, click .
Click to apply policy changes.
Deploy the changes to the staging environment by navigating to → and clicking .
From a command-line terminal, run the example curl command.
With the Anonymous Access policy still active, do not provide the user_key.
[student@workstation ~]$ curl \
https://gateways-policies-3scale-apicast-staging.apps.ocp4.example.com:443/echo
{
"method": "GET",
"path": "/",
...output omitted...
}Notice that the service no longer responds with the maintenance message.
This concludes the guided exercise.