Skip to content

Commit

Permalink
strip newlines from check output
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Kolberg <[email protected]>
  • Loading branch information
amdprophet committed Feb 21, 2024
1 parent ada996f commit 95c6caf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/sensu/sensu-pagerduty-handler/pagerduty"
"log"
"net/url"
"os"
"regexp"
"strings"
"time"

"github.com/sensu/sensu-pagerduty-handler/pagerduty"

corev2 "github.com/sensu/core/v2"
"github.com/sensu/sensu-plugin-sdk/sensu"
"github.com/sensu/sensu-plugin-sdk/templates"
Expand Down Expand Up @@ -491,6 +492,11 @@ func getSummary(event *corev2.Event) (string, error) {

func getDetails(event *corev2.Event) (details interface{}, err error) {
if len(config.detailsTemplate) > 0 {
// TODO: find a better solution to sanitize all event fields before
// template evaluation. Certain escape characters like '\n' cannot be
// parsed.
event.Check.Output = strings.ReplaceAll(event.Check.Output, "\n", " ")

detailsStr, err := templates.EvalTemplate("details", config.detailsTemplate, event)
if err != nil {
return "", fmt.Errorf("failed to evaluate template %s: %v", config.detailsTemplate, err)
Expand Down
8 changes: 8 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,17 @@ func Test_GetDetailsTemplate(t *testing.T) {
event := corev2.FixtureEvent("foo", "bar")
config.detailsTemplate = "{{.Entity.Name}}-{{.Check.Name}}"

// Valid event
details, err := getDetails(event)
assert.Nil(t, err)
assert.Equal(t, "foo-bar", details)

// Invalid event with newline in check name
config.detailsFormat = "json"
config.detailsTemplate = "{\"Output\": \"`{{.Check.Output}}`\"}"
event.Check.Output = "bar\nxaz\n"
details, err = getDetails(event)

Check failure on line 178 in main_test.go

View workflow job for this annotation

GitHub Actions / staticcheck (project)

this value of details is never used (SA4006)
assert.Nil(t, err)
}

func Test_GetDetailsObj(t *testing.T) {
Expand Down

0 comments on commit 95c6caf

Please sign in to comment.