Skip to content

Commit 53896d7

Browse files
Merge pull request #3335 from dmcgowan/fix-user-agent
Add user agent header to all requests
2 parents 2f69be5 + bb00872 commit 53896d7

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
@@ -111,6 +111,7 @@ type dockerResolver struct {
111111
auth Authorizer
112112
host func(string) (string, error)
113113
headers http.Header
114+
uagent string
114115
plainHTTP bool
115116
client *http.Client
116117
tracker StatusTracker
@@ -135,16 +136,22 @@ func NewResolver(options ResolverOptions) remotes.Resolver {
135136
ocispec.MediaTypeImageManifest,
136137
ocispec.MediaTypeImageIndex, "*"}, ", "))
137138
}
138-
if _, ok := options.Headers["User-Agent"]; !ok {
139-
options.Headers.Set("User-Agent", "containerd/"+version.Version)
139+
ua := options.Headers.Get("User-Agent")
140+
if ua != "" {
141+
options.Headers.Del("User-Agent")
142+
} else {
143+
ua = "containerd/" + version.Version
140144
}
145+
141146
if options.Authorizer == nil {
142147
options.Authorizer = NewAuthorizer(options.Client, options.Credentials)
148+
options.Authorizer.(*dockerAuthorizer).ua = ua
143149
}
144150
return &dockerResolver{
145151
auth: options.Authorizer,
146152
host: options.Host,
147153
headers: options.Headers,
154+
uagent: ua,
148155
plainHTTP: options.PlainHTTP,
149156
client: options.Client,
150157
tracker: options.Tracker,
@@ -351,6 +358,7 @@ func (r *dockerResolver) Pusher(ctx context.Context, ref string) (remotes.Pusher
351358
type dockerBase struct {
352359
refspec reference.Spec
353360
base url.URL
361+
uagent string
354362

355363
client *http.Client
356364
auth Authorizer
@@ -382,6 +390,7 @@ func (r *dockerResolver) base(refspec reference.Spec) (*dockerBase, error) {
382390
return &dockerBase{
383391
refspec: refspec,
384392
base: base,
393+
uagent: r.uagent,
385394
client: r.client,
386395
auth: r.auth,
387396
}, nil
@@ -407,6 +416,7 @@ func (r *dockerBase) authorize(ctx context.Context, req *http.Request) error {
407416
func (r *dockerBase) doRequest(ctx context.Context, req *http.Request) (*http.Response, error) {
408417
ctx = log.WithLogger(ctx, log.G(ctx).WithField("url", req.URL.String()))
409418
log.G(ctx).WithField("request.headers", req.Header).WithField("request.method", req.Method).Debug("do request")
419+
req.Header.Set("User-Agent", r.uagent)
410420
if err := r.authorize(ctx, req); err != nil {
411421
return nil, errors.Wrap(err, "failed to authorize")
412422
}

0 commit comments

Comments
 (0)