diff --git a/pkg/controllers/common/utils.go b/pkg/controllers/common/utils.go index e9ffe1453..ad845799f 100644 --- a/pkg/controllers/common/utils.go +++ b/pkg/controllers/common/utils.go @@ -106,3 +106,4 @@ func NodeIsMaster(node *v1.Node) bool { } return false } + diff --git a/pkg/nsx/endpoint.go b/pkg/nsx/endpoint.go index 7b8ae787c..be8c07041 100644 --- a/pkg/nsx/endpoint.go +++ b/pkg/nsx/endpoint.go @@ -6,7 +6,7 @@ package nsx import ( "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -275,13 +275,13 @@ func (ep *Endpoint) createAuthSession(certProvider auth.ClientCertProvider, toke log.Error(err, "session creation failed", "endpoint", u.Host) return err } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) defer resp.Body.Close() if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { err = fmt.Errorf("session creation failed, unexpected status code %d", resp.StatusCode) } if err != nil { - log.Error(err, "session creation failed", "endpoint", u.Host, "statusCode", resp.StatusCode, "headerDate", resp.Header["Date"], "body", body) + log.Error(err, "session creation failed", "endpoint", u.Host, "statusCode", resp.StatusCode, "headerDate", resp.Header["Date"], "body", string(body)) return err } tokens, ok := resp.Header["X-Xsrf-Token"] diff --git a/pkg/nsx/util/utils.go b/pkg/nsx/util/utils.go index cd7732790..92f1cb08c 100644 --- a/pkg/nsx/util/utils.go +++ b/pkg/nsx/util/utils.go @@ -7,7 +7,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "reflect" "sort" @@ -233,20 +233,20 @@ func httpErrortoNSXError(detail *ErrorDetail) NsxError { } func HandleHTTPResponse(response *http.Response, result interface{}, debug bool) (error, []byte) { + body, err := io.ReadAll(response.Body) + defer response.Body.Close() if !(response.StatusCode == http.StatusOK || response.StatusCode == http.StatusAccepted) { err := errors.New("received HTTP Error") - log.Error(err, "handle http response", "status", response.StatusCode, "requestUrl", response.Request.URL, "response", response) + log.Error(err, "handle http response", "status", response.StatusCode, "requestUrl", response.Request.URL, "response body", string(body)) return err, nil } + if err != nil || body == nil { + return err, body + } if result == nil { return nil, nil } - body, err := ioutil.ReadAll(response.Body) - defer response.Body.Close() - if err != nil || body == nil { - return err, body - } if debug { log.V(2).Info("received HTTP response", "response", string(body)) } diff --git a/pkg/nsx/util/utils_test.go b/pkg/nsx/util/utils_test.go index 589412da0..c3513cc2a 100644 --- a/pkg/nsx/util/utils_test.go +++ b/pkg/nsx/util/utils_test.go @@ -13,6 +13,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -169,6 +170,7 @@ func TestVCClient_handleHTTPResponse(t *testing.T) { response.Request = &http.Request{} response.Request.URL = &url.URL{Host: "10.0.0.1"} response.StatusCode = 301 + response.Body = io.NopCloser(strings.NewReader("Hello, World!")) var sessionData map[string]string // http status code > 300