Outbound Calls From A Flex Gateway Custom Policy – Part 1

Author: Rohit Singh & Anjali Kushwaha

This blog explains how to make GET outbound calls using a custom policy in Flex Gateway. It covers how to configure services in Flex Gateway. Once these configurations are successfully set up, they can be used in custom policies to make outbound HTTP calls.  We’ll also see how to propagate headers getting from the application to custom policy outbound calls.

Prerequisites:
  1. Anypoint Platform
  2. GitHub Access
  3. Rust must be installed on the system (download from here if not already: https://www.rust-lang.org/tools/install)
  4. Flex Gateway created on Anypoint Platform
Follow the steps below to make GET Outbound Calls:
  • Create Service:

We have to create a service.yaml configuration file with required parameters. Change the address with your desired URL without providing the endpoint, which will be passed through the Rust library file. We’ll place this file configuration file in 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 was 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.

  • Configure the service in Custom Policy:

In the custom policy, use the service that we created in the last step. In the lib.rs file(src/lib.rs) of the customs policy created in RUST, add the code below in the on_http_request_headers function to make a GET Request. We’re also adding a request header (“authorization”) with the GET request here.

fn on_http_request_headers( &mut self , _num_headers: usize , _end_of_stream: bool) -> Action { if let Some(value) = self.get_http_request_header("x-custom-auth") {
				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 Headers : #{} -> {}", 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}
  • Configure the service in FlexGateway:

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

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

Then optimize the binary web assembly file by using the 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.

  • GET Call through the Flex Gateway:

We have to make a GET call using the Flex Gateway URL using Postman. In the below attached 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. The following log is coming from the implementation URL that we pass in API Manager.

Note: For more information regarding Flex Gateway Custom Policy Using Rust, please refer Publishing a Flex Gateway Custom Policy Using Rust (Connected Mode).

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.