Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filling alert details template with HTML fields/tags #2246

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
allow filling alert details template with HTML fields/tags
SuddenSelect committed Sep 5, 2019
commit 74e80aaa2da9b8981e17dd16f5abf1ed0dd6ca8c
3 changes: 3 additions & 0 deletions alert.go
Original file line number Diff line number Diff line change
@@ -126,6 +126,9 @@ func newAlertNode(et *ExecutingTask, n *pipeline.AlertNode, d NodeDiagnostic) (a

return html.JS(tmpBuffer.String())
},
"safeHtml": func(v interface{}) html.HTML {
return html.HTML(v.(string))
},
}).Parse(n.Details)
if err != nil {
return nil, err
328 changes: 328 additions & 0 deletions integrations/streamer_test.go
Original file line number Diff line number Diff line change
@@ -9,13 +9,15 @@ import (
"html"
"io/ioutil"
"math/rand"
"mime/quotedprintable"
"net/http"
"net/http/httptest"
"net/mail"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"sync/atomic"
"testing"
"text/template"
@@ -10200,6 +10202,332 @@ Value: 10
}
}

func TestStream_AlertEmail_HtmlEscape_Field(t *testing.T) {
var script = `
stream
|from()
.measurement('cpu')
.where(lambda: "host" == 'serverA')
.groupBy('host')
|window()
.period(10s)
.every(10s)
|count('value')
|eval(lambda: 'HTML <i arg="1">escape</i> test')
.keep()
.as('htmlFieldTest')
|alert()
.id('')
.details('<b arg="11">Details</b> with {{ index .Fields "htmlFieldTest" }} field.')
.crit(lambda: "count" > 8.0)
.email('user1@example.com')
`
expEscape := html.EscapeString("HTML <i arg=\"1\">escape</i> test")
expDetails := fmt.Sprintf("<b arg=\"11\">Details</b> with %v field.\n", expEscape)
var quotedPrintableDetails bytes.Buffer
quotedPrintableWriter := quotedprintable.NewWriter(&quotedPrintableDetails)
_, _ = quotedPrintableWriter.Write([]byte(expDetails))
_ = quotedPrintableWriter.Close()

expMail := []*smtptest.Message{
{
Header: mail.Header{
"Mime-Version": []string{"1.0"},
"Content-Type": []string{"text/html; charset=UTF-8"},
"Content-Transfer-Encoding": []string{"quoted-printable"},
},
Body: strings.ReplaceAll(quotedPrintableDetails.String(), "\r", ""),
},
}

smtpServer, err := smtptest.NewServer()
if err != nil {
t.Fatal(err)
}
defer smtpServer.Close()
sc := smtp.Config{
Enabled: true,
Host: smtpServer.Host,
Port: smtpServer.Port,
From: "test@example.com",
}
smtpService := smtp.NewService(sc, diagService.NewSMTPHandler())
if err := smtpService.Open(); err != nil {
t.Fatal(err)
}
defer smtpService.Close()

tmInit := func(tm *kapacitor.TaskMaster) {
tm.SMTPService = smtpService
}

testStreamerNoOutput(t, "TestStream_Alert", script, 13*time.Second, tmInit)

// Close both client and server to ensure all message are processed
smtpService.Close()
smtpServer.Close()

errors := smtpServer.Errors()
if got, exp := len(errors), 0; got != exp {
t.Errorf("unexpected smtp server errors: %v", errors)
}

msgs := smtpServer.SentMessages()
if got, exp := len(msgs), len(expMail); got != exp {
t.Errorf("unexpected number of messages sent: got %d exp %d", got, exp)
}
for i, exp := range expMail {
got := msgs[i]
if err := exp.Compare(got); err != nil {
t.Errorf("%d %s", i, err)
}
}
}
func TestStream_AlertEmail_NoHtmlEscape_Field(t *testing.T) {
var script = `
stream
|from()
.measurement('cpu')
.where(lambda: "host" == 'serverA')
.groupBy('host')
|window()
.period(10s)
.every(10s)
|count('value')
|eval(lambda: 'HTML <i arg="1">escape</i> test')
.keep()
.as('htmlFieldTest')
|alert()
.id('')
.details('<b arg="11">Details</b> with {{ index .Fields "htmlFieldTest" | safeHtml }} field.')
.crit(lambda: "count" > 8.0)
.email('user1@example.com')
`
expDetails := "<b arg=\"11\">Details</b> with HTML <i arg=\"1\">escape</i> test field.\n"
var quotedPrintableDetails bytes.Buffer
quotedPrintableWriter := quotedprintable.NewWriter(&quotedPrintableDetails)
_, _ = quotedPrintableWriter.Write([]byte(expDetails))
_ = quotedPrintableWriter.Close()

expMail := []*smtptest.Message{
{
Header: mail.Header{
"Mime-Version": []string{"1.0"},
"Content-Type": []string{"text/html; charset=UTF-8"},
"Content-Transfer-Encoding": []string{"quoted-printable"},
},
Body: strings.ReplaceAll(quotedPrintableDetails.String(), "\r", ""),
},
}

smtpServer, err := smtptest.NewServer()
if err != nil {
t.Fatal(err)
}
defer smtpServer.Close()
sc := smtp.Config{
Enabled: true,
Host: smtpServer.Host,
Port: smtpServer.Port,
From: "test@example.com",
}
smtpService := smtp.NewService(sc, diagService.NewSMTPHandler())
if err := smtpService.Open(); err != nil {
t.Fatal(err)
}
defer smtpService.Close()

tmInit := func(tm *kapacitor.TaskMaster) {
tm.SMTPService = smtpService
}

testStreamerNoOutput(t, "TestStream_Alert", script, 13*time.Second, tmInit)

// Close both client and server to ensure all message are processed
smtpService.Close()
smtpServer.Close()

errors := smtpServer.Errors()
if got, exp := len(errors), 0; got != exp {
t.Errorf("unexpected smtp server errors: %v", errors)
}

msgs := smtpServer.SentMessages()
if got, exp := len(msgs), len(expMail); got != exp {
t.Errorf("unexpected number of messages sent: got %d exp %d", got, exp)
}
for i, exp := range expMail {
got := msgs[i]
if err := exp.Compare(got); err != nil {
t.Errorf("%d %s", i, err)
}
}
}

func TestStream_AlertEmail_HtmlEscape_Tags(t *testing.T) {
var script = `
stream
|from()
.measurement('cpu')
.where(lambda: "host" == 'serverA')
.groupBy('host')
|window()
.period(10s)
.every(10s)
|count('value')
|eval(lambda: 'HTML <i arg="1">escape</i> test')
.keep()
.as('htmlTagsTest')
.tags('htmlTagsTest')
|alert()
.id('')
.details('<b arg="11">Details</b> with {{ index .Tags "htmlTagsTest" }} tag.')
.crit(lambda: "count" > 8.0)
.email('user1@example.com')
`
expEscape := html.EscapeString("HTML <i arg=\"1\">escape</i> test")
expDetails := fmt.Sprintf("<b arg=\"11\">Details</b> with %v tag.\n", expEscape)
var quotedPrintableDetails bytes.Buffer
quotedPrintableWriter := quotedprintable.NewWriter(&quotedPrintableDetails)
_, _ = quotedPrintableWriter.Write([]byte(expDetails))
_ = quotedPrintableWriter.Close()

expMail := []*smtptest.Message{
{
Header: mail.Header{
"Mime-Version": []string{"1.0"},
"Content-Type": []string{"text/html; charset=UTF-8"},
"Content-Transfer-Encoding": []string{"quoted-printable"},
},
Body: strings.ReplaceAll(quotedPrintableDetails.String(), "\r", ""),
},
}

smtpServer, err := smtptest.NewServer()
if err != nil {
t.Fatal(err)
}
defer smtpServer.Close()
sc := smtp.Config{
Enabled: true,
Host: smtpServer.Host,
Port: smtpServer.Port,
From: "test@example.com",
}
smtpService := smtp.NewService(sc, diagService.NewSMTPHandler())
if err := smtpService.Open(); err != nil {
t.Fatal(err)
}
defer smtpService.Close()

tmInit := func(tm *kapacitor.TaskMaster) {
tm.SMTPService = smtpService
}

testStreamerNoOutput(t, "TestStream_Alert", script, 13*time.Second, tmInit)

// Close both client and server to ensure all message are processed
smtpService.Close()
smtpServer.Close()

errors := smtpServer.Errors()
if got, exp := len(errors), 0; got != exp {
t.Errorf("unexpected smtp server errors: %v", errors)
}

msgs := smtpServer.SentMessages()
if got, exp := len(msgs), len(expMail); got != exp {
t.Errorf("unexpected number of messages sent: got %d exp %d", got, exp)
}
for i, exp := range expMail {
got := msgs[i]
if err := exp.Compare(got); err != nil {
t.Errorf("%d %s", i, err)
}
}
}
func TestStream_AlertEmail_NoHtmlEscape_Tags(t *testing.T) {
var script = `
stream
|from()
.measurement('cpu')
.where(lambda: "host" == 'serverA')
.groupBy('host')
|window()
.period(10s)
.every(10s)
|count('value')
|eval(lambda: 'HTML <i arg="1">escape</i> test')
.keep()
.as('htmlTagsTest')
.tags('htmlTagsTest')
|alert()
.id('')
.details('<b arg="11">Details</b> with {{ index .Tags "htmlTagsTest" | safeHtml }} tag.')
.crit(lambda: "count" > 8.0)
.email('user1@example.com')
`
expDetails := "<b arg=\"11\">Details</b> with HTML <i arg=\"1\">escape</i> test tag.\n"
var quotedPrintableDetails bytes.Buffer
quotedPrintableWriter := quotedprintable.NewWriter(&quotedPrintableDetails)
_, _ = quotedPrintableWriter.Write([]byte(expDetails))
_ = quotedPrintableWriter.Close()

expMail := []*smtptest.Message{
{
Header: mail.Header{
"Mime-Version": []string{"1.0"},
"Content-Type": []string{"text/html; charset=UTF-8"},
"Content-Transfer-Encoding": []string{"quoted-printable"},
},
Body: strings.ReplaceAll(quotedPrintableDetails.String(), "\r", ""),
},
}

smtpServer, err := smtptest.NewServer()
if err != nil {
t.Fatal(err)
}
defer smtpServer.Close()
sc := smtp.Config{
Enabled: true,
Host: smtpServer.Host,
Port: smtpServer.Port,
From: "test@example.com",
}
smtpService := smtp.NewService(sc, diagService.NewSMTPHandler())
if err := smtpService.Open(); err != nil {
t.Fatal(err)
}
defer smtpService.Close()

tmInit := func(tm *kapacitor.TaskMaster) {
tm.SMTPService = smtpService
}

testStreamerNoOutput(t, "TestStream_Alert", script, 13*time.Second, tmInit)

// Close both client and server to ensure all message are processed
smtpService.Close()
smtpServer.Close()

errors := smtpServer.Errors()
if got, exp := len(errors), 0; got != exp {
t.Errorf("unexpected smtp server errors: %v", errors)
}

msgs := smtpServer.SentMessages()
if got, exp := len(msgs), len(expMail); got != exp {
t.Errorf("unexpected number of messages sent: got %d exp %d", got, exp)
}
for i, exp := range expMail {
got := msgs[i]
if err := exp.Compare(got); err != nil {
t.Errorf("%d %s", i, err)
}
}
}

func TestStream_AlertSNMPTrap(t *testing.T) {

var script = `