Before You Begin
Purpose
This tutorial shows you how to configure the key validation and header-based routing policies in Oracle API Platform Cloud Service.
Time to Complete
30 minutes
Background
Oracle API Platform Cloud Service abstracts the complexity of your implementations and provides simpler interfaces so that you can easily protect your APIs. You can use the Oracle API Platform Cloud Service Management Portal to configure and apply as many policies as you need.
Scenario
The API manager wants to set a restriction on the Energy API because he's noticed suspicious incoming requests. From now on, every request must include an application key corresponding to a registered application, the key is passed to the back-end service. This means that the API will service requests only for those applications that are registered.
To balance the incoming requests for the Energy API, there are two back-end services that must be set as endpoints for this API. Each request sends a header with a server number, and based on this number, the request is redirected to a specific back-end service.
Context
This is the eighth tutorial in a series of nine. This tutorial shows you how to manage and secure your APIs in Oracle API Platform Cloud Service by configuring security and routing policies. In previous tutorials, you learned how to implement, deploy, and apply basic policies to the Energy API in Oracle API Platform Cloud Service.
What Do You Need?
- Access to an Oracle API Platform Cloud Service instance with the Deploy API or Manage API grant for the API, and the Deploy to Gateway grant issued for the gateway.
- A registered and active gateway node for which you have deployment rights in Oracle API Platform Cloud Service.
- The Energy API deployed in your Oracle API Platform Cloud Service instance. (See Implementing APIs in Oracle API Platform Cloud Service.)
- A registered application to the Energy API. (See Managing Your API Catalog in the Oracle API Platform Cloud Service Developer Portal.)
- A different implementation of the Energy API on Apiary to identify the different responses when configuring the header-based routing policy. (See Implementing APIs in Oracle API Platform Cloud Service.)
- An installed client Postman.
Logging In to the Portal
Learn how to log in to the API Platform Cloud Service Management Portal, and how to navigate to the Energy API page.
-
From a web browser, go to the API Platform Cloud Service Management Portal by entering the URL in this format:
http://hostname.domain:port/apiplatform -
On the API Platform Cloud Management Portal login page, enter your user name and password, and then click Sign In.
Description of this image -
On the APIs page, click Energy.
Description of this image
Removing Previous Policies
Learn how to remove previously applied policies from the Energy API. Skip this section if no policies are applied.
-
On the API Implementation tab, hold the cursor over API Rate Limiting, and click Delete.
Description of this image -
On the API Implementation tab, hold the cursor over Header Validation, and click Delete.
Description of this image -
On the API Implementation tab, hold the cursor over Resource Based Routing, and click Delete.
Description of this image -
On the APIs page, click Save Changes.
Description of this image
Configuring a Security Policy
Adding a Key Validation Policy
You can use a key validation policy to identify the incoming requests from registered applications by setting a header that includes the application key. This helps you to avoid servicing anonymous requests.
-
On the API Implementation tab, in the Available Policies section, expand Security, hold the cursor over Key Validation, and click Apply.
Description of this image -
On the Apply Policy page, click Next.
Description of this image -
On the Apply Policy page, enter or select the following values in these fields, and then click Apply:
- Key Delivery Approach:
Header - Key Header:
my-key
Description of this image The Key Validation policy appears on the Request tab of the API Implementation panel.
Description of this image - Key Delivery Approach:
-
On the APIs page, click Save Changes.
Description of this image
Redeploying the API After Adding the Key Validation Policy
After you add the key validation policy to the Energy API, you must redeploy the API to activate the policy.
-
On the Deployments tab, hold the cursor over the production gateway name where the API is deployed, and then click Redeploy.
Description of this image -
On the Redeploy menu, click Latest Iteration.
Description of this image -
On the Deployment page, enter a comment about the reason to redeploy, and click Yes.
Description of this image -
On the Deployments tab, wait until the deployment is complete before testing your API.
Description of this image Note: If the deployment isn't moved from the Waiting tab to the Deployed tab, then try refreshing the page.
The Deployed tab is automatically updated when the API is deployed, and the date is updated with a new deployment date.
Description of this image
Testing the Key Validation Policy
After the redeployment is complete, the API applies the key validation policy to the incoming requests, and the requests that don't include the application key, or include a wrong key, are rejected.
-
Build your API URL using this format:
http://hostname.domain:port/api_endpoint_url/resource_path -
Open the Postman client, enter or select the following values in these fields, and then click Send:
- Method:
GET - API URL:
your API URL
Description of this image The request is rejected because it didn't include the my-key header.
Description of this image - Method:
-
In the Postman client, enter or select the following values in these fields, and then click Send:
- Method:
GET - API URL:
your API URL - Header key/value:
my-key / test
Description of this image The request is rejected because the my-key header included an invalid key.
Description of this image - Method:
-
In the Postman client, enter or select the following values in these fields, and then click Send:
- Method:
GET - API URL:
your API URL - Header key/value:
my-key /your application key
Description of this image Note: If you don't remember how to find your application key, go to the Applications page and then go to the Settings page of the registered application, where the App Key is displayed.
Description of this image The request retrieves the correct response because the application key was included in the request headers.
Description of this image - Method:
Configuring a Routing Policy
Adding a Header-Based Routing Policy
Header-based routing policies are used to route the incoming requests to specific service request URLs based on a header value. This policy lets you split a service into different back-end services to load balance the API traffic by including specific header values.
-
On the API Implementation tab, in the Available Policies section, expand Routing, hold the cursor over Header Based Routing, and click Apply.
Description of this image -
On the Apply Policy page, select Key Validation from the Place after the following policy list, and then click Next.
Description of this image -
On the Apply Policy page, enter or select the following values in these fields, and then click Add a new condition.
- Condition 1 Header:
server - Condition 1 Expression:
<= - Condition 1 Value:
1 - Condition 1 Set Service Request
URL To:
your_first_energy_api_implementation
Description of this image Note: If you don't have your own implementation of the Energy API, then use this service request URL as an example:
http://private-b4f96-energy1.apiary-mock.com. - Condition 1 Header:
-
In the Condition 2 section, enter or select the following values in these fields, and then click Apply:
- Header:
server - Expression:
>= - Value:
2 - Set Service Request URL To:
your_second_energy_api_implementation - OTHERWISE:
Keep Default Service Request URL
Description of this image Note: If you don't have a second implementation of the Energy API, then use this service request URL as an example:
http://private-bc194-energy7.apiary-mock.com.The Header Based Routing policy appears on the Request tab of the API Implementation panel.
Description of this image - Header:
-
On the APIs page, click Save Changes.
Description of this image
Redeploying the API After Adding the Header-Based Routing Policy
After you add the header-based routing policy to the Energy API, you must redeploy the API to activate the policy.
-
On the Deployments tab, hold the cursor over the gateway name where the API is deployed, and then click Redeploy.
Description of this image -
On the Redeploy menu, click Latest Iteration.
Description of this image -
On the Deployment page, enter a comment about the reason to redeploy, and click Yes.
Description of this image -
On the Deployments tab, wait until the deployment is complete before testing your API.
Description of this image Note: If the deployment isn't moved from the Waiting tab to the Deployed tab, then try refreshing the page.
The Deployed tab is automatically updated when the API is deployed, and the date is updated with a new deployment date.
Description of this image
Testing the Header-Based Routing Policy
After the redeployment is complete, the API applies the header-based routing policy to the incoming requests.
-
Build your API URL using this format:
http://hostname.domain:port/api_endpoint_url/resource_path -
In the Postman client, enter or select the following values in these fields, and then click Send:
- Method:
GET - API URL:
your API URL - Header key/value:
my-key /your application key - Header key/value:
server / 0
Description of this image The result you get is retrieved from the first back-end service.
Description of this image - Method:
-
In the Postman client, enter or select the following values in these fields, and then click Send:
- Method:
GET - API URL:
your API URL - Header key/value:
my-key /your application key - Header key/value:
server / 5
Description of this image The retrieved result is from the second back-end service.
Description of this image - Method: