If you’re an existing user of InsightConnect and have created a workflow, you already have experience with Handlebars, even if you didn’t know it. The syntax that you see in the product for expanding variables for inputs/outputs uses the Handlebars.js templating engine for rendering (specifically this implementation). For example, with an output named My Output
, the Handlebars expression for accessing that output variable is {{[“My Output”]}}
.
Aside from that basic example, there is a TON more functionality that handlebars provides. Let’s look at a few quick workflow examples to illustrate that.
Loops for Formatting
When building out workflows, we’ll frequently be working with a list of values from a plugin, for instance a list of IP addresses from the Shodan Query action in the Shodan plugin. Using the Loop step is great if we need to perform an action on each, but what if we just need to list them in a message that gets sent via the ChatOps step?
No problem, Handlebars is here to help. Using a #each
block in the Message input on our Post Message action allows us to nicely break the list out with a new line for each IP:
{{#each ["Search"].[ip_str] as |ip|}}
{{ip}}
{{/each}}
Which would give us a message like this for a Shodan query of domain: help.rapid7.com
:
Conditionals in Inputs
Often there are times when building out a workflow that you need to set a default value for a variable in the event that it doesn’t exist on an output. For instance let’s say that you need to have a default value for a particular input, in the event that it doesn’t exist or is empty.
In this case, we’ll use an email address as an example. If we receive an email address in our trigger, we’ll use it as the recipient for an email in the SMTP plugin. Otherwise, we can set it to a default:
This can simplify a workflow that would normally require a Decision step.
Data Variables for Special Conditions
One last fun feature. If we need to treat the first or last item in a list special, Handlebars makes it easy with the @first
and @last
data variables. Adding to the prior loop example, we could add some conditionals to apply special formatting to the first and last IPs:
{{#each ["Search"].[ip_str] as |ip|}}
{{#if @first}}
Special IP: {{ip}}
{{else if @last}}
The last one: {{ip}}
{{else}}
{{ip}}
{{/if}}
{{/each}}
Or more usefully for this example, we could create an ordered list using the @index
data variable:
{{#each ["Search"].[ip_str] as |ip|}}
{{@index}}. {{ip}}
{{/each}}`
Which turns our prior message into this:
You can learn more about data variables here.
As you can see Handlebars is pretty powerful. For more examples, be sure to check out our string formatting documentation. If you have any other cool ways that you’ve been using it, be sure to share!