From a974ba3e09c24aed76cf40f71ea98e8ca544553b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Rigault?= Date: Thu, 9 Feb 2023 09:19:03 +0100 Subject: [PATCH] always close the HTTP response body when the auth service return an HTTP error code we make sure to close the HTTP response body to avoid a socket leak. --- registry/tokentransport.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/registry/tokentransport.go b/registry/tokentransport.go index 4e858b04..b7a6ccf5 100644 --- a/registry/tokentransport.go +++ b/registry/tokentransport.go @@ -30,19 +30,19 @@ type authToken struct { } func (t *TokenTransport) authAndRetry(authService *authService, req *http.Request) (*http.Response, error) { - token, authResp, err := t.auth(authService) + token, err := t.auth(authService) if err != nil { - return authResp, err + return nil, err } retryResp, err := t.retry(req, token) return retryResp, err } -func (t *TokenTransport) auth(authService *authService) (string, *http.Response, error) { +func (t *TokenTransport) auth(authService *authService) (string, error) { authReq, err := authService.Request(t.Username, t.Password) if err != nil { - return "", nil, err + return "", err } client := http.Client{ @@ -51,22 +51,21 @@ func (t *TokenTransport) auth(authService *authService) (string, *http.Response, response, err := client.Do(authReq) if err != nil { - return "", nil, err + return "", err } - + defer response.Body.Close() if response.StatusCode != http.StatusOK { - return "", response, err + return "", fmt.Errorf("could not authenticate (error %d)", response.StatusCode) } - defer response.Body.Close() var authToken authToken decoder := json.NewDecoder(response.Body) err = decoder.Decode(&authToken) if err != nil { - return "", nil, err + return "", err } - return authToken.Token, nil, nil + return authToken.Token, nil } func (t *TokenTransport) retry(req *http.Request, token string) (*http.Response, error) {