From f1ebcaa8d7de11ba0b6a2ce9d9b2f3677b003dbb Mon Sep 17 00:00:00 2001 From: sabbene <11637137+sabbene@users.noreply.github.com> Date: Thu, 12 May 2022 17:00:28 -0700 Subject: [PATCH 1/5] new argument to alert the channel on critical events Signed-off-by: sabbene <sabbene@0hy.es> --- README.md | 2 ++ main.go | 31 ++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 28139485..7664fe62 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Available Commands: version Print the version number of this plugin Flags: + -a, --alert-on-critical The Slack notification will alert the channel with @channel -c, --channel string The channel to post messages to (default "#general") -t, --description-template string The Slack notification output template, in Golang text/template format (default "{{ .Check.Output }}") -h, --help help for sensu-slack-handler @@ -53,6 +54,7 @@ Flags: |--username |SLACK_USERNAME | |--icon-url |SLACK_ICON_URL | |--description-template |SLACK_DESCRIPTION_TEMPLATE | +|--alert-on-critical |SENSU_SLACK_ALERT_CRITICAL | **Security Note:** Care should be taken to not expose the webhook URL for this handler by specifying it diff --git a/main.go b/main.go index 46a0db2f..599f5205 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "strconv" "strings" "github.com/bluele/slack" @@ -19,6 +20,7 @@ type HandlerConfig struct { slackUsername string slackIconURL string slackDescriptionTemplate string + slackAlertCritical bool } const ( @@ -27,11 +29,13 @@ const ( username = "username" iconURL = "icon-url" descriptionTemplate = "description-template" + alertCritical = "alert-on-critical" - defaultChannel = "#general" - defaultIconURL = "https://www.sensu.io/img/sensu-logo.png" - defaultUsername = "sensu" - defaultTemplate = "{{ .Check.Output }}" + defaultChannel = "#general" + defaultIconURL = "https://www.sensu.io/img/sensu-logo.png" + defaultUsername = "sensu" + defaultTemplate = "{{ .Check.Output }}" + defaultAlert bool = false ) var ( @@ -89,6 +93,15 @@ var ( Usage: "The Slack notification output template, in Golang text/template format", Value: &config.slackDescriptionTemplate, }, + { + Path: alertCritical, + Env: "SLACK_ALERT_CRITICAL", + Argument: alertCritical, + Shorthand: "a", + Default: defaultAlert, + Usage: "The Slack notification will alert the channel with @channel", + Value: &config.slackAlertCritical, + }, } ) @@ -111,6 +124,9 @@ func checkArgs(_ *corev2.Event) error { if icon := os.Getenv("SENSU_SLACK_ICON_URL"); icon != "" && config.slackIconURL == defaultIconURL { config.slackIconURL = icon } + if alert := os.Getenv("SENSU_SLACK_ALERT_CRITICAL"); alertCritical != "" && config.slackAlertCritical == defaultAlert { + config.slackAlertCritical, _ = strconv.ParseBool(alert) + } if len(config.slackwebHookURL) == 0 { return fmt.Errorf("--%s or SLACK_WEBHOOK_URL environment variable is required", webHookURL) @@ -164,7 +180,11 @@ func messageStatus(event *corev2.Event) string { case 0: return "Resolved" case 2: - return "Critical" + if config.slackAlertCritical == true { + return "<!channel> Critical" + } else { + return "Critical" + } default: return "Warning" } @@ -175,6 +195,7 @@ func messageAttachment(event *corev2.Event) *slack.Attachment { if err != nil { fmt.Printf("%s: Error processing template: %s", config.PluginConfig.Name, err) } + description = strings.Replace(description, `\n`, "\n", -1) attachment := &slack.Attachment{ Title: "Description", From 5a506e729bea57df89a227518500bf22b3e7ec0e Mon Sep 17 00:00:00 2001 From: Stephen Abbene <11637137+sabbene@users.noreply.github.com> Date: Thu, 12 May 2022 17:46:48 -0700 Subject: [PATCH 2/5] Update lint.yml --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 0891e1f8..88763f97 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -13,4 +13,4 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - name: Run golangci-lint - uses: actions-contrib/golangci-lint@v1 + uses: actions-contrib/golangci-lint@@v1.1.0 From f23a3875e1ccd23275a66881f5daa6b5d1479a48 Mon Sep 17 00:00:00 2001 From: Stephen Abbene <11637137+sabbene@users.noreply.github.com> Date: Thu, 12 May 2022 17:50:42 -0700 Subject: [PATCH 3/5] Update lint.yml --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 88763f97..35131fdb 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -13,4 +13,4 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - name: Run golangci-lint - uses: actions-contrib/golangci-lint@@v1.1.0 + uses: matoous/golangci-lint-action@v1.23.3 From 28cef10372edb63bb29a33048b59f0b4ebc8ff31 Mon Sep 17 00:00:00 2001 From: sabbene <11637137+sabbene@users.noreply.github.com> Date: Thu, 12 May 2022 17:59:38 -0700 Subject: [PATCH 4/5] make pass golang linter tests Signed-off-by: sabbene <11637137+sabbene@users.noreply.github.com> --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 599f5205..fd449495 100644 --- a/main.go +++ b/main.go @@ -124,7 +124,7 @@ func checkArgs(_ *corev2.Event) error { if icon := os.Getenv("SENSU_SLACK_ICON_URL"); icon != "" && config.slackIconURL == defaultIconURL { config.slackIconURL = icon } - if alert := os.Getenv("SENSU_SLACK_ALERT_CRITICAL"); alertCritical != "" && config.slackAlertCritical == defaultAlert { + if alert := os.Getenv("SENSU_SLACK_ALERT_CRITICAL"); alertCritical != "" && !config.slackAlertCritical { config.slackAlertCritical, _ = strconv.ParseBool(alert) } @@ -180,7 +180,7 @@ func messageStatus(event *corev2.Event) string { case 0: return "Resolved" case 2: - if config.slackAlertCritical == true { + if config.slackAlertCritical { return "<!channel> Critical" } else { return "Critical" From b442ff88add903a8d2b32e069c9bd58f0de41c62 Mon Sep 17 00:00:00 2001 From: sabbene <11637137+sabbene@users.noreply.github.com> Date: Wed, 25 May 2022 12:21:39 -0700 Subject: [PATCH 5/5] update pr for code review --- .github/workflows/lint.yml | 12 ++++++++---- README.md | 2 +- main.go | 6 +----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 35131fdb..15ebf510 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -10,7 +10,11 @@ jobs: matrix: os: [ubuntu-latest] steps: - - name: Checkout code - uses: actions/checkout@v2 - - name: Run golangci-lint - uses: matoous/golangci-lint-action@v1.23.3 + - uses: actions/setup-go@v3 + with: + go-version: 1.17 + - uses: actions/checkout@v3 + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: latest diff --git a/README.md b/README.md index 7664fe62..df816c1b 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Flags: |--username |SLACK_USERNAME | |--icon-url |SLACK_ICON_URL | |--description-template |SLACK_DESCRIPTION_TEMPLATE | -|--alert-on-critical |SENSU_SLACK_ALERT_CRITICAL | +|--alert-on-critical |SLACK_ALERT_ON_CRITICAL | **Security Note:** Care should be taken to not expose the webhook URL for this handler by specifying it diff --git a/main.go b/main.go index fd449495..bcbc64dd 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "fmt" "os" - "strconv" "strings" "github.com/bluele/slack" @@ -95,7 +94,7 @@ var ( }, { Path: alertCritical, - Env: "SLACK_ALERT_CRITICAL", + Env: "SLACK_ALERT_ON_CRITICAL", Argument: alertCritical, Shorthand: "a", Default: defaultAlert, @@ -124,9 +123,6 @@ func checkArgs(_ *corev2.Event) error { if icon := os.Getenv("SENSU_SLACK_ICON_URL"); icon != "" && config.slackIconURL == defaultIconURL { config.slackIconURL = icon } - if alert := os.Getenv("SENSU_SLACK_ALERT_CRITICAL"); alertCritical != "" && !config.slackAlertCritical { - config.slackAlertCritical, _ = strconv.ParseBool(alert) - } if len(config.slackwebHookURL) == 0 { return fmt.Errorf("--%s or SLACK_WEBHOOK_URL environment variable is required", webHookURL)