fix: use reader over readcloser, regen cookie-jar (#11482)

This commit is contained in:
Joshua Powers 2022-07-18 13:05:28 -06:00 committed by GitHub
parent c984cd87fe
commit b5076363e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 12 deletions

View File

@ -54,11 +54,6 @@ func (c *CookieAuthConfig) initializeClient(client *http.Client) (err error) {
c.Method = http.MethodPost c.Method = http.MethodPost
} }
// add cookie jar to HTTP client
if c.client.Jar, err = cookiejar.New(nil); err != nil {
return err
}
return c.auth() return c.auth()
} }
@ -77,10 +72,19 @@ func (c *CookieAuthConfig) authRenewal(ctx context.Context, ticker *clockutil.Ti
} }
func (c *CookieAuthConfig) auth() error { func (c *CookieAuthConfig) auth() error {
var body io.ReadCloser var err error
// everytime we auth we clear out the cookie jar to ensure that the cookie
// is not used as a part of re-authing. The only way to empty or reset is
// to create a new cookie jar.
c.client.Jar, err = cookiejar.New(nil)
if err != nil {
return err
}
var body io.Reader
if c.Body != "" { if c.Body != "" {
body = io.NopCloser(strings.NewReader(c.Body)) body = strings.NewReader(c.Body)
defer body.Close()
} }
req, err := http.NewRequest(c.Method, c.URL, body) req, err := http.NewRequest(c.Method, c.URL, body)
@ -106,15 +110,17 @@ func (c *CookieAuthConfig) auth() error {
} }
defer resp.Body.Close() defer resp.Body.Close()
if _, err = io.Copy(io.Discard, resp.Body); err != nil { respBody, err := io.ReadAll(resp.Body)
if err != nil {
return err return err
} }
// check either 200 or 201 as some devices may return either // check either 200 or 201 as some devices may return either
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return fmt.Errorf("cookie auth renewal received status code: %v (%v)", return fmt.Errorf("cookie auth renewal received status code: %v (%v) [%v]",
resp.StatusCode, resp.StatusCode,
http.StatusText(resp.StatusCode), http.StatusText(resp.StatusCode),
string(respBody),
) )
} }

View File

@ -189,7 +189,7 @@ func TestAuthConfig_Start(t *testing.T) {
renewal: renewal, renewal: renewal,
endpoint: authEndpointWithBasicAuth, endpoint: authEndpointWithBasicAuth,
}, },
wantErr: fmt.Errorf("cookie auth renewal received status code: 401 (Unauthorized)"), wantErr: fmt.Errorf("cookie auth renewal received status code: 401 (Unauthorized) []"),
firstAuthCount: 0, firstAuthCount: 0,
lastAuthCount: 0, lastAuthCount: 0,
firstHTTPResponse: http.StatusForbidden, firstHTTPResponse: http.StatusForbidden,
@ -220,7 +220,7 @@ func TestAuthConfig_Start(t *testing.T) {
renewal: renewal, renewal: renewal,
endpoint: authEndpointWithBody, endpoint: authEndpointWithBody,
}, },
wantErr: fmt.Errorf("cookie auth renewal received status code: 401 (Unauthorized)"), wantErr: fmt.Errorf("cookie auth renewal received status code: 401 (Unauthorized) []"),
firstAuthCount: 0, firstAuthCount: 0,
lastAuthCount: 0, lastAuthCount: 0,
firstHTTPResponse: http.StatusForbidden, firstHTTPResponse: http.StatusForbidden,