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č <mitr@redhat.com>
  • Loading branch information
mtrmac committed Nov 16, 2023
1 parent d577071 commit 8148b3e
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 @@ -144,35 +144,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 @@ -905,6 +876,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 8148b3e

Please sign in to comment.