Skip to content

Commit

Permalink
Move bearerToken.readFromJSONBlob closer to its callers
Browse files Browse the repository at this point in the history
Only moves unchanged code, should not change behavior.

Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed May 30, 2024
1 parent 020f8a7 commit e3a4e96
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions docker/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,35 +146,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
}

// dockerCertDir returns a path to a directory to be consumed by tlsclientconfig.SetupCertificates() depending on ctx and hostPort.
func dockerCertDir(sys *types.SystemContext, hostPort string) (string, error) {
if sys != nil && sys.DockerCertPath != "" {
Expand Down Expand Up @@ -916,6 +887,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 {
Expand Down

0 comments on commit e3a4e96

Please sign in to comment.