10.0: Map IP address locations using interaction automations and ipstack #10
jstanden
started this conversation in
Guides and Tutorials
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Introduction
Let's assume we have a list of client IP addresses and want to plot them on a map to visualize where activity is coming from.
First, it's important to acknowledge that locations derived from IP addresses are approximate. Clients may be using VPNs, proxies, third party services, etc. The location may be an ISP, or the contact information for the registered owner of a block of IP addresses.
Sign up at ipstack
Like geocoding, IP-based geolocation uses a large dataset. It wouldn't make sense to ship this with Cerb, so we'll be using an API from ipstack.
You can sign up for a free API key at ipstack and make 10,000 IP location requests per month at no cost.
Once you sign up, make a note of your API Access Key so you can use it within Cerb.
Import the ipstack package in Cerb
Navigate to Setup -> Packages -> Import and paste the following package:
Click the Import button.
Enter your ipstack API Access key.
Click the Import button again.
Test the automation
Navigate to Search -> Automations and filter with
ipstack.Open the card for wgm.services.ipstack.location and click the Edit button.
This is a reusable function that returns location data for an IP address.
In the Run tab at the bottom, type the following into Input:
And click the run icon.
In the Output: section you should see something like:
This demonstrates that our function is using the ipstack API to return location data for any IP address we give it.
Using the interaction
Reload the page by clicking on the logo in the top left.
Click on the floating Cerb icon in the lower right and select Location by IP from the menu.
Enter an IP address:
cerb10_interaction_ipstack.mov
You can click the reset icon to repeat the interaction multiple times before closing the popup.
How it works
Connected Service and Connected Account
The package imports a connected service for ipstack. This is an API Key service that securely appends an
access_keyparameter any URL starting withhttps://api.ipstack.com/. You can find this at Search -> Connected Services.The package also imports a connected account using the above service. This has the API key you configured. You can find it at Search -> Connected Accounts.
Automations
The package imports two automations. You can find them at Search -> Automations.
wgm.services.ipstack.location
The wgm.services.ipstack.location automation function is a reusable wrapper around the ipstack lookup API. This function can be called by any automation to fetch location data for an IP address.
The function takes an
ipkey as input and returns adatakey with location info.Because location data for an IP changes very infrequently, and ipstack charges per lookup, the function caches the results of an IP lookup for 3 days.
The storage.get: command is used to check the cache for a given IP. If the cache misses, the
on_error:event connects the ipstack API. If the cache hits, that data is returned and the automation ends.The http.request: command connects to the ipstack API. You'll see from
http.request@ref: httpRequestthat the command is defined as a reference. This makes the logic simpler to follow.The
&httpRequestuses theauthentication:option to securely add the ipstack API key to the URL at runtime. Theurl:appends theipinput and uses the |url_encode filter to encode it.If the HTTP request is successly,
on_success:decodes the JSON body and saves it as aresponse_jsonplaceholder.A
decision:then determines if the API returned successfully (the HTTP request can succeed and the API can still return an error code). If there's an error, the automation returns in theerror:state. Otherwise, the IP data is set in theip_dataplaceholder. That result is then cached with the storage.set: command.Because
http.request@ref: httpRequestis a reference, execution continues after that line. The interaction exits in thereturn:state with adatakey containing the IP details. This is how results are returned to the caller.The automation policy allows the
storage.get:andstorage.set:commands with the key prefixwgm.services.ipstack.location::. This ensures only keys in this namespace are permitted.The policy also allows the
http.request:command for URLs prefixed withhttp://api.ipstack.com/and only allowsauthentication:with thecerb:connected_account:ipstackrecord.wgm.interaction.ipstack.location
The wgm.interaction.ipstack.location interaction automation prompts a worker for an IP address, runs the
wgm.services.ipstack.locationfunction above, and displays the resulting location as a sheet and a map.The automation policy allows the function: command only for the
wgm.services.ipstack.locationfunction.Toolbar
The package registers the interaction on the global.menu toolbar. You'll find this in Search -> Toolbars.
All reactions