Skip to content

Commit 1af8e6e

Browse files
committed
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]>
1 parent 71f5018 commit 1af8e6e

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

remotes/docker/authorizer.go

Lines changed: 17 additions & 1 deletion
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,7 +197,16 @@ func (a *dockerAuthorizer) fetchTokenWithOAuth(ctx context.Context, to tokenOpti
194197
form.Set("password", to.secret)
195198
}
196199

197-
resp, err := ctxhttp.PostForm(ctx, a.client, to.realm, form)
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)
198210
if err != nil {
199211
return "", err
200212
}
@@ -240,6 +252,10 @@ func (a *dockerAuthorizer) fetchToken(ctx context.Context, to tokenOptions) (str
240252
return "", err
241253
}
242254

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

245261
if to.service != "" {

remotes/docker/resolver.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/containerd/containerd/log"
3030
"github.com/containerd/containerd/reference"
3131
"github.com/containerd/containerd/remotes"
32+
"github.com/containerd/containerd/version"
3233
digest "github.com/opencontainers/go-digest"
3334
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3435
"github.com/pkg/errors"
@@ -105,6 +106,7 @@ func DefaultHost(ns string) (string, error) {
105106
type dockerResolver struct {
106107
auth Authorizer
107108
host func(string) (string, error)
109+
uagent string
108110
plainHTTP bool
109111
client *http.Client
110112
tracker StatusTracker
@@ -118,12 +120,15 @@ func NewResolver(options ResolverOptions) remotes.Resolver {
118120
if options.Host == nil {
119121
options.Host = DefaultHost
120122
}
123+
ua := "containerd/" + version.Version
121124
if options.Authorizer == nil {
122125
options.Authorizer = NewAuthorizer(options.Client, options.Credentials)
126+
options.Authorizer.(*dockerAuthorizer).ua = ua
123127
}
124128
return &dockerResolver{
125129
auth: options.Authorizer,
126130
host: options.Host,
131+
uagent: ua,
127132
plainHTTP: options.PlainHTTP,
128133
client: options.Client,
129134
tracker: options.Tracker,
@@ -293,6 +298,7 @@ func (r *dockerResolver) Pusher(ctx context.Context, ref string) (remotes.Pusher
293298
type dockerBase struct {
294299
refspec reference.Spec
295300
base url.URL
301+
uagent string
296302

297303
client *http.Client
298304
auth Authorizer
@@ -324,6 +330,7 @@ func (r *dockerResolver) base(refspec reference.Spec) (*dockerBase, error) {
324330
return &dockerBase{
325331
refspec: refspec,
326332
base: base,
333+
uagent: r.uagent,
327334
client: r.client,
328335
auth: r.auth,
329336
}, nil
@@ -349,6 +356,7 @@ func (r *dockerBase) authorize(ctx context.Context, req *http.Request) error {
349356
func (r *dockerBase) doRequest(ctx context.Context, req *http.Request) (*http.Response, error) {
350357
ctx = log.WithLogger(ctx, log.G(ctx).WithField("url", req.URL.String()))
351358
log.G(ctx).WithField("request.headers", req.Header).WithField("request.method", req.Method).Debug("do request")
359+
req.Header.Set("User-Agent", r.uagent)
352360
if err := r.authorize(ctx, req); err != nil {
353361
return nil, errors.Wrap(err, "failed to authorize")
354362
}

0 commit comments

Comments
 (0)