Skip to content

Commit e6275a0

Browse files
dmcgowanthaJeztah
authored andcommitted
Add user agent header to all requests
Currently the user agent is only being used on the initial resolve request, then switching to the default user agent. This ensures the correct user agent is always used. There is a larger fix in progress which does this is a cleaner way, but the scope of this change is fixing the user agent issue. Signed-off-by: Derek McGowan <[email protected]> (cherry picked from commit bb00872) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 4bffd88 commit e6275a0

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

remotes/docker/authorizer.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131

3232
"github.com/containerd/containerd/errdefs"
3333
"github.com/containerd/containerd/log"
34+
"github.com/containerd/containerd/version"
3435
"github.com/pkg/errors"
3536
"github.com/sirupsen/logrus"
3637
"golang.org/x/net/context/ctxhttp"
@@ -40,6 +41,7 @@ type dockerAuthorizer struct {
4041
credentials func(string) (string, string, error)
4142

4243
client *http.Client
44+
ua string
4345
mu sync.Mutex
4446

4547
auth map[string]string
@@ -54,6 +56,7 @@ func NewAuthorizer(client *http.Client, f func(string) (string, string, error))
5456
return &dockerAuthorizer{
5557
credentials: f,
5658
client: client,
59+
ua: "containerd/" + version.Version,
5760
auth: map[string]string{},
5861
}
5962
}
@@ -194,11 +197,16 @@ func (a *dockerAuthorizer) fetchTokenWithOAuth(ctx context.Context, to tokenOpti
194197
form.Set("password", to.secret)
195198
}
196199

197-
resp, err := ctxhttp.Post(
198-
ctx, a.client, to.realm,
199-
"application/x-www-form-urlencoded; charset=utf-8",
200-
strings.NewReader(form.Encode()),
201-
)
200+
req, err := http.NewRequest("POST", to.realm, strings.NewReader(form.Encode()))
201+
if err != nil {
202+
return "", err
203+
}
204+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
205+
if a.ua != "" {
206+
req.Header.Set("User-Agent", a.ua)
207+
}
208+
209+
resp, err := ctxhttp.Do(ctx, a.client, req)
202210
if err != nil {
203211
return "", err
204212
}
@@ -244,6 +252,10 @@ func (a *dockerAuthorizer) fetchToken(ctx context.Context, to tokenOptions) (str
244252
return "", err
245253
}
246254

255+
if a.ua != "" {
256+
req.Header.Set("User-Agent", a.ua)
257+
}
258+
247259
reqParams := req.URL.Query()
248260

249261
if to.service != "" {

remotes/docker/resolver.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ type dockerResolver struct {
110110
auth Authorizer
111111
host func(string) (string, error)
112112
headers http.Header
113+
uagent string
113114
plainHTTP bool
114115
client *http.Client
115116
tracker StatusTracker
@@ -134,16 +135,22 @@ func NewResolver(options ResolverOptions) remotes.Resolver {
134135
ocispec.MediaTypeImageManifest,
135136
ocispec.MediaTypeImageIndex, "*"}, ", "))
136137
}
137-
if _, ok := options.Headers["User-Agent"]; !ok {
138-
options.Headers.Set("User-Agent", "containerd/"+version.Version)
138+
ua := options.Headers.Get("User-Agent")
139+
if ua != "" {
140+
options.Headers.Del("User-Agent")
141+
} else {
142+
ua = "containerd/" + version.Version
139143
}
144+
140145
if options.Authorizer == nil {
141146
options.Authorizer = NewAuthorizer(options.Client, options.Credentials)
147+
options.Authorizer.(*dockerAuthorizer).ua = ua
142148
}
143149
return &dockerResolver{
144150
auth: options.Authorizer,
145151
host: options.Host,
146152
headers: options.Headers,
153+
uagent: ua,
147154
plainHTTP: options.PlainHTTP,
148155
client: options.Client,
149156
tracker: options.Tracker,
@@ -308,6 +315,7 @@ func (r *dockerResolver) Pusher(ctx context.Context, ref string) (remotes.Pusher
308315
type dockerBase struct {
309316
refspec reference.Spec
310317
base url.URL
318+
uagent string
311319

312320
client *http.Client
313321
auth Authorizer
@@ -339,6 +347,7 @@ func (r *dockerResolver) base(refspec reference.Spec) (*dockerBase, error) {
339347
return &dockerBase{
340348
refspec: refspec,
341349
base: base,
350+
uagent: r.uagent,
342351
client: r.client,
343352
auth: r.auth,
344353
}, nil
@@ -364,6 +373,7 @@ func (r *dockerBase) authorize(ctx context.Context, req *http.Request) error {
364373
func (r *dockerBase) doRequest(ctx context.Context, req *http.Request) (*http.Response, error) {
365374
ctx = log.WithLogger(ctx, log.G(ctx).WithField("url", req.URL.String()))
366375
log.G(ctx).WithField("request.headers", req.Header).WithField("request.method", req.Method).Debug("do request")
376+
req.Header.Set("User-Agent", r.uagent)
367377
if err := r.authorize(ctx, req); err != nil {
368378
return nil, errors.Wrap(err, "failed to authorize")
369379
}

0 commit comments

Comments
 (0)