From cea1e48ed8324cdb80d8f66f923229b9c89c5ab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 29 May 2023 22:47:11 +0200 Subject: [PATCH] Move bearerToken.readFromJSONBlob closer to its callers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only moves unchanged code, should not change behavior. Signed-off-by: Miloslav Trmač --- docker/docker_client.go | 58 ++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/docker/docker_client.go b/docker/docker_client.go index 42c56ed58a..0182c056b1 100644 --- a/docker/docker_client.go +++ b/docker/docker_client.go @@ -141,35 +141,6 @@ const ( noAuth ) -// readFromJSONBlob sets token data in dest from the provided JSON. -func (bt *bearerToken) readFromJSONBlob(blob []byte) error { - var token struct { - Token string `json:"token"` - AccessToken string `json:"access_token"` - ExpiresIn int `json:"expires_in"` - IssuedAt time.Time `json:"issued_at"` - expirationTime time.Time - } - if err := json.Unmarshal(blob, &token); err != nil { - return err - } - - bt.token = token.Token - if bt.token == "" { - bt.token = token.AccessToken - } - - if token.ExpiresIn < minimumTokenLifetimeSeconds { - token.ExpiresIn = minimumTokenLifetimeSeconds - logrus.Debugf("Increasing token expiration to: %d seconds", token.ExpiresIn) - } - if token.IssuedAt.IsZero() { - token.IssuedAt = time.Now().UTC() - } - bt.expirationTime = token.IssuedAt.Add(time.Duration(token.ExpiresIn) * time.Second) - return nil -} - // this is cloned from docker/go-connections because upstream docker has changed // it and make deps here fails otherwise. // We'll drop this once we upgrade to docker 1.13.x deps. @@ -838,6 +809,35 @@ func (c *dockerClient) getBearerToken(ctx context.Context, dest *bearerToken, ch return dest.readFromJSONBlob(tokenBlob) } +// readFromJSONBlob sets token data in dest from the provided JSON. +func (bt *bearerToken) readFromJSONBlob(blob []byte) error { + var token struct { + Token string `json:"token"` + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` + IssuedAt time.Time `json:"issued_at"` + expirationTime time.Time + } + if err := json.Unmarshal(blob, &token); err != nil { + return err + } + + bt.token = token.Token + if bt.token == "" { + bt.token = token.AccessToken + } + + if token.ExpiresIn < minimumTokenLifetimeSeconds { + token.ExpiresIn = minimumTokenLifetimeSeconds + logrus.Debugf("Increasing token expiration to: %d seconds", token.ExpiresIn) + } + if token.IssuedAt.IsZero() { + token.IssuedAt = time.Now().UTC() + } + bt.expirationTime = token.IssuedAt.Add(time.Duration(token.ExpiresIn) * time.Second) + return nil +} + // detectPropertiesHelper performs the work of detectProperties which executes // it at most once. func (c *dockerClient) detectPropertiesHelper(ctx context.Context) error {