Skip to content

Commit 6e21311

Browse files
committed
Move newBearerTokenFromHTTPResponseBody closer to its callers
Only moves unchanged code, should not change behavior. Signed-off-by: Miloslav Trmač <[email protected]>
1 parent 4fb2836 commit 6e21311

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

docker/docker_client.go

+42-42
Original file line numberDiff line numberDiff line change
@@ -145,48 +145,6 @@ const (
145145
noAuth
146146
)
147147

148-
// newBearerTokenFromHTTPResponseBody parses a http.Response to obtain a bearerToken.
149-
// The caller is still responsible for ensuring res.Body is closed.
150-
func newBearerTokenFromHTTPResponseBody(res *http.Response) (*bearerToken, error) {
151-
blob, err := iolimits.ReadAtMost(res.Body, iolimits.MaxAuthTokenBodySize)
152-
if err != nil {
153-
return nil, err
154-
}
155-
156-
var token struct {
157-
Token string `json:"token"`
158-
AccessToken string `json:"access_token"`
159-
ExpiresIn int `json:"expires_in"`
160-
IssuedAt time.Time `json:"issued_at"`
161-
expirationTime time.Time
162-
}
163-
if err := json.Unmarshal(blob, &token); err != nil {
164-
const bodySampleLength = 50
165-
bodySample := blob
166-
if len(bodySample) > bodySampleLength {
167-
bodySample = bodySample[:bodySampleLength]
168-
}
169-
return nil, fmt.Errorf("decoding bearer token (last URL %q, body start %q): %w", res.Request.URL.Redacted(), string(bodySample), err)
170-
}
171-
172-
bt := &bearerToken{
173-
token: token.Token,
174-
}
175-
if bt.token == "" {
176-
bt.token = token.AccessToken
177-
}
178-
179-
if token.ExpiresIn < minimumTokenLifetimeSeconds {
180-
token.ExpiresIn = minimumTokenLifetimeSeconds
181-
logrus.Debugf("Increasing token expiration to: %d seconds", token.ExpiresIn)
182-
}
183-
if token.IssuedAt.IsZero() {
184-
token.IssuedAt = time.Now().UTC()
185-
}
186-
bt.expirationTime = token.IssuedAt.Add(time.Duration(token.ExpiresIn) * time.Second)
187-
return bt, nil
188-
}
189-
190148
// dockerCertDir returns a path to a directory to be consumed by tlsclientconfig.SetupCertificates() depending on ctx and hostPort.
191149
func dockerCertDir(sys *types.SystemContext, hostPort string) (string, error) {
192150
if sys != nil && sys.DockerCertPath != "" {
@@ -898,6 +856,48 @@ func (c *dockerClient) getBearerToken(ctx context.Context, challenge challenge,
898856
return newBearerTokenFromHTTPResponseBody(res)
899857
}
900858

859+
// newBearerTokenFromHTTPResponseBody parses a http.Response to obtain a bearerToken.
860+
// The caller is still responsible for ensuring res.Body is closed.
861+
func newBearerTokenFromHTTPResponseBody(res *http.Response) (*bearerToken, error) {
862+
blob, err := iolimits.ReadAtMost(res.Body, iolimits.MaxAuthTokenBodySize)
863+
if err != nil {
864+
return nil, err
865+
}
866+
867+
var token struct {
868+
Token string `json:"token"`
869+
AccessToken string `json:"access_token"`
870+
ExpiresIn int `json:"expires_in"`
871+
IssuedAt time.Time `json:"issued_at"`
872+
expirationTime time.Time
873+
}
874+
if err := json.Unmarshal(blob, &token); err != nil {
875+
const bodySampleLength = 50
876+
bodySample := blob
877+
if len(bodySample) > bodySampleLength {
878+
bodySample = bodySample[:bodySampleLength]
879+
}
880+
return nil, fmt.Errorf("decoding bearer token (last URL %q, body start %q): %w", res.Request.URL.Redacted(), string(bodySample), err)
881+
}
882+
883+
bt := &bearerToken{
884+
token: token.Token,
885+
}
886+
if bt.token == "" {
887+
bt.token = token.AccessToken
888+
}
889+
890+
if token.ExpiresIn < minimumTokenLifetimeSeconds {
891+
token.ExpiresIn = minimumTokenLifetimeSeconds
892+
logrus.Debugf("Increasing token expiration to: %d seconds", token.ExpiresIn)
893+
}
894+
if token.IssuedAt.IsZero() {
895+
token.IssuedAt = time.Now().UTC()
896+
}
897+
bt.expirationTime = token.IssuedAt.Add(time.Duration(token.ExpiresIn) * time.Second)
898+
return bt, nil
899+
}
900+
901901
// detectPropertiesHelper performs the work of detectProperties which executes
902902
// it at most once.
903903
func (c *dockerClient) detectPropertiesHelper(ctx context.Context) error {

0 commit comments

Comments
 (0)