Chapter 6. Transforming 3scale message content using policy extensions in Fuse

You can use Red Hat Fuse to create highly flexible policy extensions for Red Hat 3scale API Management. You can do this by creating policy extensions in Fuse on OpenShift and then configuring them as policies in the 3scale Admin Portal. Using an APIcast Camel proxy policy, you can perform complex transformations to request and response message content, for example, XML to JSON, which are implemented in the Apache Camel integration framework.

In addition, you can add or modify custom policy extensions dynamically in Camel, instead of rebuilding and redeploying the static APIcast container image. You can use any Camel Enterprise Integration Pattern (EIP) written in Camel Domain Specific Language (DSL) to implement an APIcast policy extension. This enables you to write policy extensions using a familiar programming language such as Java or XML. The example in this topic uses the Camel Netty4 HTTP component to implement the HTTP proxy in Java.

Required software components

You must have the following Red Hat Integration components deployed on the same OpenShift cluster:

  • Fuse on OpenShift 7.7
  • 3scale On-premises
  • APIcast self-managed or embedded
Note

You should install these components on the same OpenShift cluster, but not necessarily in the same namespace or project. There is no direct communication between Fuse and 3scale. All communication occurs through OpenShift only.

Architecturally, it is best to have Fuse custom code running in a different project than 3scale. However, you must ensure that communication between both projects is possible. For details, see Configuring network policy with OpenShift SDN.

6.1. Integrating APIcast with Apache Camel transformations in Fuse

You can integrate APIcast with a transformation written as an Apache Camel application in Fuse on OpenShift. When the policy extension transformation is configured and deployed in 3scale, the 3scale traffic goes through the Camel policy extension, which transforms the message content. In this case, Camel works as a reverse HTTP proxy, where APIcast sends the 3scale traffic to Camel, and Camel then sends the traffic on to the API backend.

The example in this topic creates the HTTP proxy using the Camel Netty4 HTTP component:

  • The request received over the HTTP proxy protocol is forwarded to the target service with the HTTP body converted to uppercase.
  • The response from the target service is processed by converting it to uppercase and then returned to the client.

Prerequisites

  • You must have Fuse on OpenShift 7.7 and 3scale 2.9 deployed on the same OpenShift cluster. For installation details, see:

  • You will need cluster administrator privileges to install Fuse on OpenShift and 3scale and to create projects. However, you can create deployment configurations, deploy pods, or create services with edit access privileges per project.

Procedure

  1. Write an Apache Camel application in Java using the Camel netty4-http component to implement the HTTP proxy. You can then use any Camel component to transform the message.

    The following simple example performs an uppercase transformation of the request and the response from the service:

    import java.util.Locale;
    import org.apache.camel.Exchange;
    import org.apache.camel.Message;
    import org.apache.camel.builder.RouteBuilder;
    
    public class ProxyRoute extends RouteBuilder {
    
        @Override
        public void configure() throws Exception {
            from("netty4-http:proxy://0.0.0.0:8080")
                .process(ProxyRoute::uppercase)
                .toD("netty4-http:"
                    + "${headers." + Exchange.HTTP_SCHEME + "}://"
                    + "${headers." + Exchange.HTTP_HOST + "}:"
                    + "${headers." + Exchange.HTTP_PORT + "}"
                    + "${headers." + Exchange.HTTP_PATH + "}")
                .process(ProxyRoute::uppercase);
        }
    
        public static void uppercase(final Exchange exchange) {
            final Message message = exchange.getIn();
            final String body = message.getBody(String.class);
            message.setBody(body.toUpperCase(Locale.US));
        }
    }
  2. Deploy your Camel application as described in Creating and Deploying Applications on Fuse on OpenShift.

6.2. Configuring an APIcast policy extension created using Apache Camel in Fuse on OpenShift

After you have created the Apache Camel transformation, you can configure it as a policy extension in the policy chain in the 3scale Admin Portal.

The policy extension enables you to configure a 3scale service to use a Camel HTTP proxy. This service is used to send the 3scale traffic over the HTTP proxy to perform request/response modifications in a third-party proxy. In this case, the third-party proxy is Apache Camel implemented in Fuse on OpenShift.

Note

The policy extension code is created in an Apache Camel application in Fuse on OpenShift and cannot be modified or deleted from 3scale.

Prerequisites

Procedure

  1. Select Integration > Policies in the 3scale Admin Portal.
  2. Select POLICIES > Add policy > Camel Service.
  3. Enter the route for the Camel proxy service in the appropriate field:

    • https_proxy: Connects to the Camel proxy service over HTTPS.
    • http_proxy: Connects to the Camel proxy service over HTTP.
    • all_proxy: Connects to Camel proxy services when the protocol is not specified.

      For example, the following shows the route for an HTTP proxy service:

      http://camel-proxy.my-3scale-management-project.svc
  4. Promote the updated policy configuration to your staging or production environment. For example, click Promote v. 3 to Staging APIcast.
  5. Test the HTTP proxy using curl: For example:

    curl "https://testapi-3scale-apicast-staging.myuser.app.dev.3sca.net:443/?user_key=MY_USER_KEY" -k
  6. Confirm that the message content has been transformed, which in this example means converted to uppercase.