@@ -86,11 +86,9 @@ type extensionSignatureList struct {
86
86
Signatures []extensionSignature `json:"signatures"`
87
87
}
88
88
89
+ // bearerToken records a cached token we can use to authenticate.
89
90
type bearerToken struct {
90
- Token string `json:"token"`
91
- AccessToken string `json:"access_token"`
92
- ExpiresIn int `json:"expires_in"`
93
- IssuedAt time.Time `json:"issued_at"`
91
+ token string
94
92
expirationTime time.Time
95
93
}
96
94
@@ -147,37 +145,6 @@ const (
147
145
noAuth
148
146
)
149
147
150
- // newBearerTokenFromHTTPResponseBody parses a http.Response to obtain a bearerToken.
151
- // The caller is still responsible for ensuring res.Body is closed.
152
- func newBearerTokenFromHTTPResponseBody (res * http.Response ) (* bearerToken , error ) {
153
- blob , err := iolimits .ReadAtMost (res .Body , iolimits .MaxAuthTokenBodySize )
154
- if err != nil {
155
- return nil , err
156
- }
157
-
158
- token := new (bearerToken )
159
- if err := json .Unmarshal (blob , & token ); err != nil {
160
- const bodySampleLength = 50
161
- bodySample := blob
162
- if len (bodySample ) > bodySampleLength {
163
- bodySample = bodySample [:bodySampleLength ]
164
- }
165
- return nil , fmt .Errorf ("decoding bearer token (last URL %q, body start %q): %w" , res .Request .URL .Redacted (), string (bodySample ), err )
166
- }
167
- if token .Token == "" {
168
- token .Token = token .AccessToken
169
- }
170
- if token .ExpiresIn < minimumTokenLifetimeSeconds {
171
- token .ExpiresIn = minimumTokenLifetimeSeconds
172
- logrus .Debugf ("Increasing token expiration to: %d seconds" , token .ExpiresIn )
173
- }
174
- if token .IssuedAt .IsZero () {
175
- token .IssuedAt = time .Now ().UTC ()
176
- }
177
- token .expirationTime = token .IssuedAt .Add (time .Duration (token .ExpiresIn ) * time .Second )
178
- return token , nil
179
- }
180
-
181
148
// dockerCertDir returns a path to a directory to be consumed by tlsclientconfig.SetupCertificates() depending on ctx and hostPort.
182
149
func dockerCertDir (sys * types.SystemContext , hostPort string ) (string , error ) {
183
150
if sys != nil && sys .DockerCertPath != "" {
@@ -786,7 +753,7 @@ func (c *dockerClient) setupRequestAuth(req *http.Request, extraScope *authScope
786
753
token = * t
787
754
c .tokenCache .Store (cacheKey , token )
788
755
}
789
- registryToken = token .Token
756
+ registryToken = token .token
790
757
}
791
758
req .Header .Set ("Authorization" , fmt .Sprintf ("Bearer %s" , registryToken ))
792
759
return nil
@@ -889,6 +856,48 @@ func (c *dockerClient) getBearerToken(ctx context.Context, challenge challenge,
889
856
return newBearerTokenFromHTTPResponseBody (res )
890
857
}
891
858
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
+
892
901
// detectPropertiesHelper performs the work of detectProperties which executes
893
902
// it at most once.
894
903
func (c * dockerClient ) detectPropertiesHelper (ctx context.Context ) error {
0 commit comments