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

Co-Author- https://medium.com/@kushanjali838

Rohit Singh
Another Integration Blog
3 min readJul 31, 2023

--

This blog explains how to make POST outbound calls from custom policy in Flex Gateway. In the Part 1, we’ve discussed how to make GET outbound calls with request headers.

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 POST 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 POST Request.

Here, we are sending the “Authorization” header from postman using flex gateway url and making the POST request body with it and sending it to the external api that we are calling using REST custom policy.

fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
// Log policy version
debug!(“Status 1.0.21”);
// Get Authorization header
let auth = match self.get_http_request_header(“authorization”) {
Some(auth_value) => auth_value,
None => {
error!(“Authorization header is missing”);
let response_body = serde_json::to_vec(
&json!(
{
“success”: “false”,
“message”: “Authorization header is missing” } ), ) .unwrap_or_default();
// Stopping execution since header is missing
self.send_http_response(
401,
vec![(“content-type”, “application/json”)],
Some(&response_body),
);
return Action::Continue;
}
};
// Create request_body to attach it with HTTP Token Introspection call
let request_body = serde_json::to_vec(
&json!(
{
“token”: auth
}
)
).unwrap_or_default();
// Call Token endpoint
match self.dispatch_http_call(
“outboundservice.default.svc”,
vec![
(“:method”, “POST”),
(“:path”, “/new”),
(“:authority”, “rust-end-api-15march.us-e1.cloudhub.io”),
(“authorization”, &self.secret),
(“content-type”, “application/json”),
],
Some(&request_body),
vec![],
Duration::from_secs(10),
) {
Ok(id) => debug!(“Successfully dispatched token endpoint call with id: {}”, id),
Err(e) => error!(“Failed to dispatch the token endpoint call with error: {:#?}”, e),
};
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. POST Call through the Flex Gateway:

We have to make a POST call using Flex Gateway. In the attached below screenshot, you can see that the header(“authorization”) we are passing in to the postman which will be used in creating the request body for 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/@rohittavatiya/outbound-calls-from-a-custom-policy-in-flex-gateway-part-1-896f5d796872

--

--