Outbound calls from a Custom Policy in Flex Gateway — Part 1

Anjali Kushwaha
3 min readApr 24, 2023

--

This blog explains how to make outbound calls from custom policy in Flex Gateway. It discusses setting up services configuration in Flex Gateway. Once these configurations are successfully set up, they can be utilized in custom policies for the purpose of making outbound HTTP calls. We’ll also see how to propagate headers getting from application to custom policy outbound calls.

Prerequisites:

  1. Anypoint Platform Account
  2. Flex Gateway already created on Anypoint Platform

Follow below steps to make GET outbound calls:

  1. Create Service:

We’ve to create a service.yaml configuration file with required parameters. Change the address with your desired url without providing the endpoint, that will be passed through the Rust library file.

We’ll place this file configuration file to the working directory of the Flex Gateway. In the case of Docker, move it to the folder in which your registration.yaml file is present.

apiVersion: gateway.mulesoft.com/v1alpha1
kind: Service
metadata:
name: outboundservice
spec:
address: http://rust-end-api-15march.us-e1.cloudhub.io

Run Flex Gateway. Then you’ll get to see in the logs, the details of the service that just deployed.

[flex-gateway-agent][info] Gateway default/2d2d8d763bbe: Adding Service default/outboundservice http://rust-end-api-15march.us-e1.cloudhub.io/

The configuration when used appropriately would be accessing the endpoint “http://rust-end-api-15march.us-e1.cloudhub.io/" via HTTP.

2. Configure the service in Custom Policy:

In the custom policy, Use the service that we created in the last step. In lib.rs file of custom policy created in RUST, add below code in on_http_request_headers function to make GET Request.

Here we’re also adding a request header (“Authorization”) with the GET request.

fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
if let Some(value) = self.get_http_request_header(“authorization”) {
if self.secret == value {
info!(“on_http_request_headers allowing”);
let _token = if let Some(_token) = self.get_http_request_header(“Authorization”){
info!(“This is our Authorization token: {}”,_token);
match self.dispatch_http_call(
“outboundservice.default.svc”,
vec![(“:method”, “GET”),
(“:path”, “/new”),
(“:authority”, “rust-end-api-15march.us-e1.cloudhub.io”),
(“authorization”, &_token)],
None,
vec![],
Duration::from_secs(10),){
Ok(resp) => {info!(“Service Invocation response: {}”,resp)},
Err(err) => info!(“Error: {:?}”,err)};
for (name, value) in &self.get_http_request_headers() {
info!(“HTTP Request Header : #{} -> {}”, name, value);
}
return Action::Continue;}
else
{ return Action::Continue; }}}
info!(“on_http_request_headers blocking”);
self.send_http_response(401, Vec::new(), None);
Action::Pause}

3. Configure the service in FlexGateway:

  • Now we’ll build this rust library using command:

“cargo build — target wasm32-unknown-unknown — release”.

  • Then optimize the binary web assembly file by using following command:

“wasm-gc target/wasm32-unknown-unknown/release/<<flex_custom_policy_auth_header.wasm>> -o target/<<flex_custom_policy_auth_header-final.wasm>>”

  • Deploy the custom policy to the desired API. The custom policy during its execution must be able to make outbound calls with the service configured and complete its working.

4. GET Call through the Flex Gateway:

We have to make a GET call using Flex Gateway. In the attached below screenshot, you can see that the header(“authorization”) we are passing in the postman is getting propagated to the outbound call that we have configured using our custom policy.

Note**: For more information regarding Flex Gateway Custom Policy Using Rust refer another blog links- https://medium.com/@kushanjali838/outbound-calls-from-a-custom-policy-in-flex-gateway-part-2-b3fe4bebda2b

--

--

Anjali Kushwaha

Mulesoft Mentor | Mulesoft Certified Platform Architect