resolver: set buildkit own user-agent

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
master
Tonis Tiigi 2022-02-01 22:48:01 -08:00
parent 2633c96bac
commit dd992414a3
5 changed files with 59 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/util/contentutil"
"github.com/moby/buildkit/util/imageutil"
"github.com/moby/buildkit/version"
"github.com/moby/locker"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
@ -40,9 +41,12 @@ func New(with ...ImageMetaResolverOpt) llb.ImageMetaResolver {
for _, f := range with {
f(&opts)
}
headers := http.Header{}
headers.Set("User-Agent", version.UserAgent())
return &imageMetaResolver{
resolver: docker.NewResolver(docker.ResolverOptions{
Client: http.DefaultClient,
Client: http.DefaultClient,
Headers: headers,
}),
platform: opts.platform,
buffer: contentutil.NewBuffer(),

View File

@ -9,6 +9,7 @@ import (
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
"github.com/moby/buildkit/version"
"github.com/moby/locker"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
@ -16,8 +17,11 @@ import (
)
func ProviderFromRef(ref string) (ocispecs.Descriptor, content.Provider, error) {
headers := http.Header{}
headers.Set("User-Agent", version.UserAgent())
remote := docker.NewResolver(docker.ResolverOptions{
Client: http.DefaultClient,
Client: http.DefaultClient,
Headers: headers,
})
name, desc, err := remote.Resolve(context.TODO(), ref)
@ -33,8 +37,11 @@ func ProviderFromRef(ref string) (ocispecs.Descriptor, content.Provider, error)
}
func IngesterFromRef(ref string) (content.Ingester, error) {
headers := http.Header{}
headers.Set("User-Agent", version.UserAgent())
remote := docker.NewResolver(docker.ResolverOptions{
Client: http.DefaultClient,
Client: http.DefaultClient,
Headers: headers,
})
p, err := remote.Pusher(context.TODO(), ref)

View File

@ -18,6 +18,7 @@ import (
sessionauth "github.com/moby/buildkit/session/auth"
log "github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/flightcontrol"
"github.com/moby/buildkit/version"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@ -355,6 +356,9 @@ func (ah *authHandler) fetchToken(ctx context.Context, sm *session.Manager, g se
return nil, nil
}
hdr := http.Header{}
hdr.Set("User-Agent", version.UserAgent())
// fetch token for the resource scope
if to.Secret != "" {
defer func() {
@ -370,7 +374,7 @@ func (ah *authHandler) fetchToken(ctx context.Context, sm *session.Manager, g se
// As of September 2017, GCR is known to return 404.
// As of February 2018, JFrog Artifactory is known to return 401.
if (errStatus.StatusCode == 405 && to.Username != "") || errStatus.StatusCode == 404 || errStatus.StatusCode == 401 {
resp, err := auth.FetchTokenWithOAuth(ctx, ah.client, nil, "buildkit-client", to)
resp, err := auth.FetchTokenWithOAuth(ctx, ah.client, hdr, "buildkit-client", to)
if err != nil {
return nil, err
}
@ -390,7 +394,7 @@ func (ah *authHandler) fetchToken(ctx context.Context, sm *session.Manager, g se
return nil, nil
}
// do request anonymously
resp, err := auth.FetchToken(ctx, ah.client, nil, to)
resp, err := auth.FetchToken(ctx, ah.client, hdr, to)
if err != nil {
return nil, errors.Wrap(err, "failed to fetch anonymous token")
}

View File

@ -3,6 +3,7 @@ package resolver
import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"sync/atomic"
@ -14,6 +15,7 @@ import (
distreference "github.com/docker/distribution/reference"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/source"
"github.com/moby/buildkit/version"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
)
@ -104,8 +106,11 @@ func newResolver(hosts docker.RegistryHosts, handler *authHandlerNS, sm *session
g: g,
handler: handler,
}
headers := http.Header{}
headers.Set("User-Agent", version.UserAgent())
r.Resolver = docker.NewResolver(docker.ResolverOptions{
Hosts: r.HostsFunc,
Hosts: r.HostsFunc,
Headers: headers,
})
return r
}

View File

@ -17,14 +17,46 @@
package version
import (
"regexp"
"sync"
)
const (
defaultVersion = "0.0.0+unknown"
)
var (
// Package is filled at linking time
Package = "github.com/moby/buildkit"
// Version holds the complete version number. Filled in at linking time.
Version = "0.0.0+unknown"
Version = defaultVersion
// Revision is filled with the VCS (e.g. git) revision being used to build
// the program at linking time.
Revision = ""
)
var (
reRelease *regexp.Regexp
reDev *regexp.Regexp
reOnce sync.Once
)
func UserAgent() string {
version := defaultVersion
reOnce.Do(func() {
reRelease = regexp.MustCompile(`^(v[0-9]+\.[0-9]+)\.[0-9]+$`)
reDev = regexp.MustCompile(`^(v[0-9]+\.[0-9]+)\.[0-9]+`)
})
if matches := reRelease.FindAllStringSubmatch(version, 1); len(matches) > 0 {
version = matches[0][1]
} else if matches := reDev.FindAllStringSubmatch(version, 1); len(matches) > 0 {
version = matches[0][1] + "-dev"
}
return "buildkit/" + version
}