Chapter 6. Transforming 3scale message content using policy extensions in Fuse
You can create highly flexible policy extensions for Red Hat 3scale API Management using Red Hat Fuse. You can do this by creating policy extensions in Fuse on OpenShift and configuring them in the 3scale Admin Portal. This enables you to use an APIcast proxy policy to perform complex transformations to request/response message content (for example, XML to JSON) that are implemented in the Apache Camel integration framework.
This also enables you to 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) available using 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.6
- 3scale On-premises
- APIcast self-managed or embedded
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.
Additional resources
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.6 and 3scale 2.8 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
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)); } }
- Deploy your Camel application as described in Creating and Deploying Applications on Fuse on OpenShift.
The HTTP over TLS (HTTPS) protocol is not supported in this example.
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 product to use an 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.
The policy extension code is created in an Apache Camel application in Fuse on OpenShift and cannot be modified or deleted from 3scale.
Prerequisites
You must have Fuse on OpenShift 7.6 and 3scale 2.8 deployed on the same OpenShift cluster. For installation details, see:
- You must have implemented a 3scale policy extension using an Apache Camel application in Fuse on OpenShift.
- You must have deployed the Apache Camel application in a pod and exposed it as a service on OpenShift. For details, see Creating and Deploying Applications on Fuse on OpenShift.
Procedure
- Select Integration > Configuration > edit APIcast configuration in the 3scale Admin Portal.
- Select POLICIES > Add Policy > Proxy service.
Enter the route for the Camel proxy service in the appropriate field:
-
https_proxy
: Connects to an HTTPS proxy service. -
http_proxy
: Connects to an HTTP proxy service. all_proxy
: Connects to 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
-
- Click Update & test in staging environment to apply the new proxy policy.
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
- Confirm that the message content has been transformed, which in this example means converted to uppercase.
Additional resources