From 9b43674741d807723990c6a3f7d8ef34ebe19c96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 29 May 2023 22:09:07 +0200 Subject: [PATCH] Remove unnecessary fields from bearerToken MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These fields need to exist when parsing JSON; but we can just record the outcome of processing them. Should not change behavior. Signed-off-by: Miloslav Trmač --- docker/docker_client.go | 23 +++++++++-------------- docker/docker_client_test.go | 10 +++------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/docker/docker_client.go b/docker/docker_client.go index b5657387e..b683203e9 100644 --- a/docker/docker_client.go +++ b/docker/docker_client.go @@ -89,9 +89,6 @@ type extensionSignatureList struct { // bearerToken records a cached token we can use to authenticate. type bearerToken struct { token string - AccessToken string - ExpiresIn int - IssuedAt time.Time expirationTime time.Time } @@ -173,22 +170,20 @@ func newBearerTokenFromHTTPResponseBody(res *http.Response) (*bearerToken, error } bt := &bearerToken{ - token: token.Token, - AccessToken: token.AccessToken, - ExpiresIn: token.ExpiresIn, - IssuedAt: token.IssuedAt, + token: token.Token, } if bt.token == "" { - bt.token = bt.AccessToken + bt.token = token.AccessToken } - if bt.ExpiresIn < minimumTokenLifetimeSeconds { - bt.ExpiresIn = minimumTokenLifetimeSeconds - logrus.Debugf("Increasing token expiration to: %d seconds", bt.ExpiresIn) + + if token.ExpiresIn < minimumTokenLifetimeSeconds { + token.ExpiresIn = minimumTokenLifetimeSeconds + logrus.Debugf("Increasing token expiration to: %d seconds", token.ExpiresIn) } - if bt.IssuedAt.IsZero() { - bt.IssuedAt = time.Now().UTC() + if token.IssuedAt.IsZero() { + token.IssuedAt = time.Now().UTC() } - bt.expirationTime = bt.IssuedAt.Add(time.Duration(bt.ExpiresIn) * time.Second) + bt.expirationTime = token.IssuedAt.Add(time.Duration(token.ExpiresIn) * time.Second) return bt, nil } diff --git a/docker/docker_client_test.go b/docker/docker_client_test.go index c01957f4b..16e2c286b 100644 --- a/docker/docker_client_test.go +++ b/docker/docker_client_test.go @@ -117,15 +117,15 @@ func TestNewBearerTokenFromHTTPResponseBody(t *testing.T) { }, { // "token" input: `{"token":"IAmAToken","expires_in":100,"issued_at":"2018-01-01T10:00:02+00:00"}`, - expected: &bearerToken{token: "IAmAToken", ExpiresIn: 100, IssuedAt: time.Unix(1514800802, 0), expirationTime: time.Unix(1514800802+100, 0)}, + expected: &bearerToken{token: "IAmAToken", expirationTime: time.Unix(1514800802+100, 0)}, }, { // "access_token" input: `{"access_token":"IAmAToken","expires_in":100,"issued_at":"2018-01-01T10:00:02+00:00"}`, - expected: &bearerToken{token: "IAmAToken", ExpiresIn: 100, IssuedAt: time.Unix(1514800802, 0), expirationTime: time.Unix(1514800802+100, 0)}, + expected: &bearerToken{token: "IAmAToken", expirationTime: time.Unix(1514800802+100, 0)}, }, { // Small expiry input: `{"token":"IAmAToken","expires_in":1,"issued_at":"2018-01-01T10:00:02+00:00"}`, - expected: &bearerToken{token: "IAmAToken", ExpiresIn: 60, IssuedAt: time.Unix(1514800802, 0), expirationTime: time.Unix(1514800802+60, 0)}, + expected: &bearerToken{token: "IAmAToken", expirationTime: time.Unix(1514800802+60, 0)}, }, } { token, err := newBearerTokenFromHTTPResponseBody(testTokenHTTPResponse(t, c.input)) @@ -134,9 +134,6 @@ func TestNewBearerTokenFromHTTPResponseBody(t *testing.T) { } else { require.NoError(t, err, c.input) assert.Equal(t, c.expected.token, token.token, c.input) - assert.Equal(t, c.expected.ExpiresIn, token.ExpiresIn, c.input) - assert.True(t, c.expected.IssuedAt.Equal(token.IssuedAt), - "expected [%s] to equal [%s], it did not", token.IssuedAt, c.expected.IssuedAt) assert.True(t, c.expected.expirationTime.Equal(token.expirationTime), "expected [%s] to equal [%s], it did not", token.expirationTime, c.expected.expirationTime) } @@ -149,7 +146,6 @@ func TestNewBearerTokenFromHTTPResponseBodyIssuedAtZero(t *testing.T) { tokenBlob := fmt.Sprintf(`{"token":"IAmAToken","expires_in":100,"issued_at":"%s"}`, zeroTime) token, err := newBearerTokenFromHTTPResponseBody(testTokenHTTPResponse(t, tokenBlob)) require.NoError(t, err) - assert.False(t, token.IssuedAt.Before(now), "expected [%s] not to be before [%s]", token.IssuedAt, now) expectedExpiration := now.Add(time.Duration(100) * time.Second) require.False(t, token.expirationTime.Before(expectedExpiration), "expected [%s] not to be before [%s]", token.expirationTime, expectedExpiration)