Note:

Use OCI API Gateway, Functions and Observability to Validate JSON Content and Monitor API Headers and Body

Introduction

When we develop distributed applications, especially in architectures based on microservices, we want components that scale and perform well in their execution. They have very complex architectures, components that execute other components, that execute other components, and so on in an infinite number of endless calls.

Planning how to develop each of them is a huge task. You can expose your microservices built on a Kubernetes cluster through the Oracle Cloud Infrastructure API Gateway (OCI API Gateway). There are a series of facilities, such as performing call authentication and authorization, data validation and call optimization, to name just a few. There is also the possibility of executing calls with OCI Functions with the aim of creating personalized authentication and authorization mechanisms, when existing methods are not sufficient to solve the need.

This tutorial will show how to use the custom mechanism to validate some use cases such as:

Despite being a mechanism for authentication and authorization in the OCI API Gateway, it could help with some other needs, such as:

Objectives

Prerequisites

Task 1: Configure OCI Observability

  1. Create a log in your OCI tenancy to ingest the logs from your function. Navigate to Observability and Management and select Logs in the OCI Console.

    logging-1

  2. Click Create custom log.

    logging-2

  3. Enter a name in the Custom log name field and choose a proper compartment and Log Group.

    logging-3

Note: It is important to capture the OCID of your Log, you will need this for your code.

logging-4

Task 2: Create an OCI Function to capture the HEADERs and BODY from the API request

To execute the following steps, download code from here function.zip.

Understand the Code

This code can be found here function.zip.

Note: If you don’t know how to develop a function and call it in API Gateway, see Call a function using API Gateway.

Configure the SDK Authentication to OCI

You need to configure the config file and put your OCI private key and fingerprint with your function before deploying it to OCI. You must have the config and private key files generated on your Oracle Cloud Infrastructure Command Line Interface (OCI CLI) installation and configuration.

To install and configure your OCI CLI, see Install the OCI CLI. This installation and configuration will generate two files for you. Find config and private key file (default is oci_api_key.pem). The folder path will be informed in the installation instructions.

code-2

Download function.zip to see the code, the config file and the private key. Replace the config and private key files with your OCI CLI files.

Build and deploy the OCI Function

In this step, we will need to use the OCI CLI to create the OCI functions and deploy code into your tenancy. To create an OCI function, see OCI Functions QuickStart and search for Python option. You will need to create your function with this information:

Remember the compartment you deployed your function. You will need this information to configure your OCI API Gateway deployment.

Task 3: Configure the OCI Function in API Gateway

Let’s deploy your API and integrate with your OCI Functions to validate and send request parameters (Header and BODY) to OCI Observability. If you don’t know how to expose your backend in the OCI API Gateway, see OCI API Gateway: Setup, Create and Deploy an API.

  1. Open Edit deployment.

    config-apigw-1

  2. Click Authentication Section.

    config-apigw-2

  3. Click Single Authentication and select Authorizer Function.

    config-apigw-2a

  4. Choose your functions compartment (where you deployed your function), select fn_apigw_json application and your function python-json-header.

    config-apigw-2b

  5. Configure the Functions Arguments to capture the HEADER and BODY. Capture the HEADER named header and header2, and the BODY content that will be named as body.

    config-apigw-2c

  6. Click Routes and configure the Header Transformation. This configuration is optional, just to see the response content with the request data (HEADER and BODY content) or the errors generated on the request. It will be useful to debug your function.

    config-apigw-3

Task 4: Test your Request

Note: In your API Deployment, a cache for the Functions arguments will be activated if you configure the Authorizer Function and set up the Functions Arguments. You can establish what data type will be cached. You can configure cache for query parameter or header but not for body content.

We can test the API request. Let’s test with only one item on an array in the body.

curl --location 'https://xxxxxxxxxxxxxxxxxxxx.apigateway.us-ashburn-1.oci.customer-oci.com/path_index/path' \
    --header 'Content-Type: text/plain' \
    --header 'header: header' \
    --header 'header2: header2' \
    --header 'header3: header3' \
    --data '{"data": {"clientID": "xxxxxxxxxxxxxxxxxxx", "secretID": "xxxxxxxxxxxxxxxxxxx", "jList":[{"added_by":"Ani","description":"example description.","start_date":"2014-10-10","mark":255,"id":975}]}}' -i

The header3 was sent but showed in the log because it was not configured as a Function Argument in OCI API Gateway. There is only 1 item on BODY JSON array, so it is a valid authorization request.

test-1

Let’s put one more item on array and test.

curl --location 'https://xxxxxxxxxxxxxxxxxxxx.apigateway.us-ashburn-1.oci.customer-oci.com/path_index/path' \
--header 'Content-Type: text/plain' \
--header 'header: header' \
--header 'header2: header2' \
--header 'header3: header3' \
--data '{"data": {"clientID": "xxxxxxxxxxxxxxxxxxx", "secretID": "xxxxxxxxxxxxxxxxxxx", "jList":[{"added_by":"Ani","description":"example description.","start_date":"2014-10-10","mark":255,"id":975}, {"added_by":"Ani","description":"example description.","start_date":"2014-10-10","mark":255,"id":975}]}}' -i

test-2

Acknowledgments

More Learning Resources

Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.

For product documentation, visit Oracle Help Center.