ryan.carter on Thursday, August 23, 2012

Connector Callback Testing – Local

3

using an external API can be a PITA, especially if the API uses any HTTP Callbacks or redirects such as OAuth or WebHooks. If your using any callback functionality like this then the Service Provider needs a way to callback your application and therefore be accessible to the public Internet.

When you start integrating these APIs, it’s much easier to work on your local development machine, but these are usually behind firewalls, NAT, or are otherwise not able to provide a public URL and it’s not really feasible to push to a staging environment every time you want to test something.

So we need a way to make our local applications available to the Internet; there are a few good services and tools out there to help with this such as: Tunnlr, ProxyLocal, showoff.io or you can setup your own reverse SSH Tunnel if you already have a remote system to forward you requests.

In this post, I am going to use a service called LocalTunnel and show how we can share a local Mule application with the world and customize some Connectors to receive Callbacks locally.

Installing Localtunnel

Ruby, Ruby, Ruby!

LocalTunnel can be installed by using RubyGems.

Mac - If you are using a Mac then your all set as it’s installed by default.

Linux – If you are on a Linux machine, you can run the following command to install rubygems:
$ sudo apt-get install ruby ruby1.8-dev rubygems1.8 libopenssl-ruby

Windows – Windows is a different animal, full instructions on installing RubyGems and LocalTunnel can be found here: http://blog.wearemammoth.com/2011/09/localtunnel-windows.html

Once RubyGems installed, either operating sytem can install LocalTunnel via the following command:
sudo gem install localtunnel

Drop the sudo if your using Windows.

Creating public and private keys

All these handy services for creating tunnels require a private and public key pair to enable your computer to securely communicate with theirs.To generate your public/private key pair, you can run the following command:

ssh-keygen

You should get output similar to the following:

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Ryan/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

First it will prompt you for a location to store the keys, and then a passphrase and confirmation of the passphrase. Here we have just hit enter 3 times, excepting the default location and using an empty passphrase.

First run with LocalTunnel

The first time you ever use LocalTunnel, you will have to upload the public key we generated earlier.

As you can see from the previous output, my key is stored in /Users/Ryan/.ssh/id_rsa. Using this path we can run localtunnel and upload they key using the following command:

localtunnel -k /Users/kevin/.ssh/id_rsa.pub 8081

-k indicates the key to upload followed by the path of the key. The last parameter “8081” is the local port we want localtunnel to forward to. So whatever port your app is running on should replace this value.

Future Runs

You only have to specify the key the first time you run localtunnel. Future tunnels can be setup as simple as the following command:

localtunnel 8081

Each time you run the command you should get output similar to the following:

This localtunnel service is brought to you by <a href="http://www.mulesoft.com/cloud-connectors/twilio-integration-connector"target="_blank" title="Twilio Integration Connector" >Twilio</a>.
Port 8081 is now publicly accessible from http://Rj1z.localtunnel.com ...

Take note of the URL provided as we’ll use this later to configure our callbacks.

Configuring the Callback

As Localtunnel is kindly provided by Twilio, to demonstrate I will use the Twilio Cloud Connector.  Twilio has an awesome WebHook implementation with great debugging tools. Twilio uses callbacks to tell you about the status of your requests; When you use Twilio to a place a phone call or send an SMS the Twilio API allows you to send a URL where you’ll receive information about the phone call once it ends or the status of the outbound SMS message after it’s processed.

This example uses the Twilio Cloud Connector to send a simple SMS message. The most important thing to note is that the “status-callback-flow-ref” attribute. All connector operations that support callback’s will have an optional attribute ending in “-flow-ref”. In this case : “status-callback-flow-ref”. As the name suggests, this attribute should reference a flow. This value must be a valid flow id from within your configuration. It is this flow that will be used to listen for the callback.

Notice that the flow has no inbound endpoint? This is where the magic happens; when Twilio process the SMS message it will send a callback automatically to that flow without you having to define an inbound endpoint. The connector automatically generates an inbound endpoint and sends the auto generated URL to Twilio for you.

Customizing the Callback

The URL generated for the callback URL is built using ‘localhost’ as the host, the ‘http.port’ environment variable or ‘localPort’ value as the port and the path of the URL is typically just a random generated string or static value. So if I run this locally it would send Twilio my non public address, something like http://localhost:80/fjer4380j3fkl00vv3v3er342fvvn.

Each connector that accepts HTTP callbacks will provide you with an optional http-callback-config child element to override these settings. These settings can be set at the connector’s config level as follows:

Here we have amended the previous example to add the additonal http-callback-config configuration. The configuration takes three mandatory arguments: domain, lo-calPort and remotePort. These settings will be used to constuct the URL that is passed to the external system. The URL will be the same as the default generated URL of the HTTP inbound-endpoint except that the host is replaced by the ‘domain’ setting (or its default value) and the port is replaced by the ‘remotePort’ setting (or its default value).

In this case we have used the domain from the URL that LocalTunnel generated for us earlier: Rj1z.localtunnel.com and set the remotePort to 80 as LocalTunnel just used the default 80 port.

And that’s it! If you rerun this configuration you should start seeing your callback being printed to the console. The same goes for any connectors too. If your using any connectors built using the DevKit   modules, such as the LinkedIn connector, you can configure the OAuth callback in a similar way:


3 Responses to “Connector Callback Testing – Local”

Mariano Gonzalez August 30th, 2012, 9:17 am

Great post Ryan, thank you!

One additional tip: In order for any gem install operation to work in a Mac, XCode and Command Line Tools for XCode need to be installed.

Cheers,

Ross Mason August 31st, 2012, 3:17 pm

Good post Ryan, I aslo realised that you can pretty easily set up local callbacks with CloudHub and the secure data gateway to tunnel requests. The nice thing about that approach is you don’t need to keep updating your callback URI.

Ryan Carter September 3rd, 2012, 3:29 am

Thanks. Yes, one of the main downfalls with services like these are that you have to regenerate the callback URI and update it everywhere, especially for some service providers like Bilty, where you cannot update the callback URI for an application after the original setup. I have since taken a look at the SDG from CloudHub and it looks a much more sustainable solution. Maybe a follow up post…

Leave a Comment