Greetings
I'm implementing a spam detection tool using Watcher. Right now, I'm just going through the last 5 minutes of logs, and reporting any messages (grouped by the subject) which exceed some defined thresholds.
The message contains a list of all Subjects which exceeded the thresholds, and some extra detail. I would like the subjects to be links to Kibana, showing all entries relevant to the subject. This is my attempt (inside the watcher action → email → body → html):
Found events :
<ul>
{{#ctx.vars.offenders}}
<li>
Subject: <a href="https://my-kibana/app/kibana#/discover?_g=(refreshInterval:(display:'30 seconds',pause:!f,section:1,value:30000),time:(mode:quick,from:now-12h,to:now))&_a=(columns:!(hdr_msgid,hdr_from,mailfrom,ip,geoip.country_name,geoip.city_name),index:edba35e0-8e7d-11e8-bdc5-a11e25f3d3ed,interval:m,query:(language:kuery,query:'hdr_subject:"{{key}}"'),sort:!('@timestamp',desc))">{{key}}</a> (Found {{doc_count}} times)
<ul>
{{#source_ips.buckets}}
<li>{{key}} (Found {{doc_count}} times)</li>
{{/source_ips.buckets}}
</ul>
</li>
{{/ctx.vars.offenders}}
</ul>
Details on search: <a href="https://my-kibana/app/kibana#/management/elasticsearch/watcher/watches/watch/ESRD/status">Kibana link</a>
However, this only works if the subject field is uncomplicated (i.e. no ampersands, quote marks, or non-UTF8 characters). Is there a way to HTML-sanitise the ctx.vars.offenders.key
so that it can be injected into the Kibana URL? Or is there a better way to achieve this?
Also, is there a way for the "Details on search" link to point to the relevant event, instead of the entire history of this watcher?