From 3201ad0830c857f98ea6549fccd3d9b4be5e979c Mon Sep 17 00:00:00 2001 From: Eric Ernst Date: Wed, 25 May 2022 10:10:28 -0700 Subject: [PATCH] shim-client: ensure we check resp status for Put/Post Without this, potential errors are silently dropped. Let's ensure we return the error code as well as potenial data from the response. Signed-off-by: Eric Ernst --- .../shimclient/shim_management_client.go | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/runtime/pkg/utils/shimclient/shim_management_client.go b/src/runtime/pkg/utils/shimclient/shim_management_client.go index 81c0d70dec..3f9e686507 100644 --- a/src/runtime/pkg/utils/shimclient/shim_management_client.go +++ b/src/runtime/pkg/utils/shimclient/shim_management_client.go @@ -9,6 +9,7 @@ import ( "bytes" "fmt" "io" + "io/ioutil" "net" "net/http" "time" @@ -79,12 +80,22 @@ func DoPut(sandboxID string, timeoutInSeconds time.Duration, urlPath, contentTyp req.Header.Set("Content-Type", contentType) resp, err := client.Do(req) + if err != nil { + return err + } + defer func() { if resp != nil { resp.Body.Close() } }() - return err + + if resp.StatusCode != http.StatusOK { + data, _ := ioutil.ReadAll(resp.Body) + return fmt.Errorf("error sending put: url: %s, status code: %d, response data: %s", urlPath, resp.StatusCode, string(data)) + } + + return nil } // DoPost will make a POST request to the shim endpoint that handles the given sandbox ID @@ -95,10 +106,20 @@ func DoPost(sandboxID string, timeoutInSeconds time.Duration, urlPath, contentTy } resp, err := client.Post(fmt.Sprintf("http://shim%s", urlPath), contentType, bytes.NewBuffer(payload)) + if err != nil { + return err + } + defer func() { if resp != nil { resp.Body.Close() } }() - return err + + if resp.StatusCode != http.StatusOK { + data, _ := ioutil.ReadAll(resp.Body) + return fmt.Errorf("error sending post: url: %s, status code: %d, response data: %s", urlPath, resp.StatusCode, string(data)) + } + + return nil }