Fix issues with Slack contact points (#40953)

* recipient validation regex modified, validation at creation/modification implemented

* Remove validation for recipient, fix tests

* Log level changed from Warn to Error
This commit is contained in:
Santiago
2021-10-27 13:58:37 -03:00
committed by GitHub
parent 7c5de96503
commit c9654c4bc0
7 changed files with 25 additions and 96 deletions
@@ -380,7 +380,7 @@ func GetAvailableNotifiers() []*alerting.NotifierPlugin {
Label: "Recipient",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
Description: "Specify channel or user, use #channel-name, @username (has to be all lowercase, no whitespace), or user/channel Slack ID - required unless you provide a webhook",
Description: "Specify channel, private group, or IM channel (can be an encoded ID or a name) - required unless you provide a webhook",
PropertyName: "recipient",
},
// Logically, this field should be required when not using a webhook, since the Slack API needs a token.
@@ -10,7 +10,6 @@ import (
"net"
"net/http"
"net/url"
"regexp"
"strings"
"time"
@@ -42,8 +41,6 @@ type SlackNotifier struct {
Token string
}
var reRecipient *regexp.Regexp = regexp.MustCompile("^((@[a-z0-9][a-zA-Z0-9._-]*)|(#[^ .A-Z]{1,79})|([a-zA-Z0-9]+))$")
var SlackAPIEndpoint = "https://slack.com/api/chat.postMessage"
// NewSlackNotifier is the constructor for the Slack notifier
@@ -62,11 +59,7 @@ func NewSlackNotifier(model *NotificationChannelConfig, t *template.Template, fn
}
recipient := strings.TrimSpace(model.Settings.Get("recipient").MustString())
if recipient != "" {
if !reRecipient.MatchString(recipient) {
return nil, receiverInitError{Cfg: *model, Reason: fmt.Sprintf("recipient on invalid format: %q", recipient)}
}
} else if apiURL.String() == SlackAPIEndpoint {
if recipient == "" && apiURL.String() == SlackAPIEndpoint {
return nil, receiverInitError{Cfg: *model,
Reason: "recipient must be specified when using the Slack chat API",
}
@@ -219,23 +212,25 @@ var sendSlackRequest = func(request *http.Request, logger log.Logger) error {
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
logger.Warn("Slack API request failed", "url", request.URL.String(), "statusCode", resp.Status, "body", string(body))
logger.Error("Slack API request failed", "url", request.URL.String(), "statusCode", resp.Status, "body", string(body))
return fmt.Errorf("request to Slack API failed with status code %d", resp.StatusCode)
}
// Slack responds to some requests with a JSON document, that might contain an error
// Slack responds to some requests with a JSON document, that might contain an error.
rslt := struct {
Ok bool `json:"ok"`
Err string `json:"error"`
}{}
if err := json.Unmarshal(body, &rslt); err != nil {
logger.Warn("Failed to unmarshal Slack API response", "url", request.URL.String(), "statusCode", resp.Status,
// Marshaling can fail if Slack's response body is plain text (e.g. "ok").
if err := json.Unmarshal(body, &rslt); err != nil && json.Valid(body) {
logger.Error("Failed to unmarshal Slack API response", "url", request.URL.String(), "statusCode", resp.Status,
"body", string(body))
return fmt.Errorf("failed to unmarshal Slack API response: %s", err)
}
if !rslt.Ok && rslt.Err != "" {
logger.Warn("Sending Slack API request failed", "url", request.URL.String(), "statusCode", resp.Status,
logger.Error("Sending Slack API request failed", "url", request.URL.String(), "statusCode", resp.Status,
"err", rslt.Err)
return fmt.Errorf("failed to make Slack API request: %s", rslt.Err)
}
@@ -270,9 +270,8 @@ func TestSendSlackRequest(t *testing.T) {
expectError: false,
},
{
name: "No response body",
statusCode: http.StatusOK,
expectError: true,
name: "No response body",
statusCode: http.StatusOK,
},
{
name: "Success case, unexpected response body",