From 4a001c87eebf7ad8e3781b4ce5219696d693c0f9 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Wed, 1 Dec 2021 09:18:37 -0500 Subject: [PATCH] Alerting: Fix panic while proxying 4xx responses of requests to cortex/loki (#42570) (#42584) Fixes a panic that would ocurr as we proxy 4xx responses. When this happens and the content type of the response is JSON we try to check if the response has a "message" key. Then, we assume that the key will contain a value of string but we don't take into account that this value can potentially be `null`. This adds a type assertion check to to this assumption so that we can keep the original JSON body as the response if we're unable to extract an `message`. (cherry picked from commit 5b64c4f6843767b88831d0bcc5621aecc0e20dfb) Co-authored-by: gotjosh --- pkg/services/ngalert/api/util.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/services/ngalert/api/util.go b/pkg/services/ngalert/api/util.go index f3e9e35c7f2..1c435295472 100644 --- a/pkg/services/ngalert/api/util.go +++ b/pkg/services/ngalert/api/util.go @@ -116,7 +116,10 @@ func (p *AlertingProxy) withReq( var m map[string]interface{} if err := json.Unmarshal(resp.Body(), &m); err == nil { if message, ok := m["message"]; ok { - errMessage = message.(string) + errMessageStr, isString := message.(string) + if isString { + errMessage = errMessageStr + } } } } else if strings.HasPrefix(resp.Header().Get("Content-Type"), "text/html") {