From a331c892a08cef4ba64ebd6075bad11c26981187 Mon Sep 17 00:00:00 2001 From: Andres Martinez Gotor Date: Fri, 28 Jul 2023 10:22:21 +0200 Subject: [PATCH] Chore: Refactor error when retrieving the plugin manifest key (#72492) --- .../keyretriever/dynamic/dynamic_retriever.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/services/pluginsintegration/keyretriever/dynamic/dynamic_retriever.go b/pkg/services/pluginsintegration/keyretriever/dynamic/dynamic_retriever.go index 0b0be7c6dc9..36ad2d6cab1 100644 --- a/pkg/services/pluginsintegration/keyretriever/dynamic/dynamic_retriever.go +++ b/pkg/services/pluginsintegration/keyretriever/dynamic/dynamic_retriever.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "net" "net/http" "net/url" @@ -134,10 +135,20 @@ func (kr *KeyRetriever) downloadKeys(ctx context.Context) error { } }() - if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + + body, err := io.ReadAll(resp.Body) + if err != nil { return err } + if err := json.Unmarshal(body, &data); err != nil { + kr.log.Debug("error unmarshalling response body", "error", err, "body", string(body)) + return fmt.Errorf("error unmarshalling response body: %w", err) + } + if len(data.Items) == 0 { return errors.New("missing public key") }