
With this patch, the user-agent has information about the containerd-client
version and the storage-driver that's used when using the containerd-integration;
time="2023-06-01T11:27:07.959822887Z" level=info msg="listening on [::]:5000" go.version=go1.19.9 instance.id=53590f34-096a-4fd1-9c58-d3b8eb7e5092 service=registry version=2.8.2
...
172.18.0.1 - - [01/Jun/2023:11:30:12 +0000] "HEAD /v2/multifoo/blobs/sha256:c7ec7661263e5e597156f2281d97b160b91af56fa1fd2cc045061c7adac4babd HTTP/1.1" 404 157 "" "docker/dev go/go1.20.4 git-commit/8d67d0c1a8 kernel/5.15.49-linuxkit-pr os/linux arch/arm64 containerd-client/1.6.21+unknown storage-driver/overlayfs UpstreamClient(Docker-Client/24.0.2 \\(linux\\))"
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit d099e47e00
)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
99 lines
3 KiB
Go
99 lines
3 KiB
Go
package containerd
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/containerd/containerd/remotes"
|
|
"github.com/containerd/containerd/remotes/docker"
|
|
"github.com/containerd/containerd/version"
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
|
"github.com/docker/docker/dockerversion"
|
|
"github.com/docker/docker/pkg/useragent"
|
|
"github.com/docker/docker/registry"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func (i *ImageService) newResolverFromAuthConfig(ctx context.Context, authConfig *registrytypes.AuthConfig) (remotes.Resolver, docker.StatusTracker) {
|
|
tracker := docker.NewInMemoryTracker()
|
|
hostsFn := i.registryHosts.RegistryHosts()
|
|
|
|
hosts := hostsWrapper(hostsFn, authConfig, i.registryService)
|
|
headers := http.Header{}
|
|
headers.Set("User-Agent", dockerversion.DockerUserAgent(ctx, useragent.VersionInfo{Name: "containerd-client", Version: version.Version}, useragent.VersionInfo{Name: "storage-driver", Version: i.snapshotter}))
|
|
|
|
return docker.NewResolver(docker.ResolverOptions{
|
|
Hosts: hosts,
|
|
Tracker: tracker,
|
|
Headers: headers,
|
|
}), tracker
|
|
}
|
|
|
|
func hostsWrapper(hostsFn docker.RegistryHosts, optAuthConfig *registrytypes.AuthConfig, regService RegistryConfigProvider) docker.RegistryHosts {
|
|
var authorizer docker.Authorizer
|
|
if optAuthConfig != nil {
|
|
authorizer = docker.NewDockerAuthorizer(authorizationCredsFromAuthConfig(*optAuthConfig))
|
|
}
|
|
|
|
return func(n string) ([]docker.RegistryHost, error) {
|
|
hosts, err := hostsFn(n)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for i := range hosts {
|
|
if hosts[i].Authorizer == nil {
|
|
hosts[i].Authorizer = authorizer
|
|
isInsecure := regService.IsInsecureRegistry(hosts[i].Host)
|
|
if hosts[i].Client.Transport != nil && isInsecure {
|
|
hosts[i].Client.Transport = httpFallback{super: hosts[i].Client.Transport}
|
|
}
|
|
}
|
|
}
|
|
return hosts, nil
|
|
}
|
|
}
|
|
|
|
func authorizationCredsFromAuthConfig(authConfig registrytypes.AuthConfig) docker.AuthorizerOpt {
|
|
cfgHost := registry.ConvertToHostname(authConfig.ServerAddress)
|
|
if cfgHost == "" || cfgHost == registry.IndexHostname {
|
|
cfgHost = registry.DefaultRegistryHost
|
|
}
|
|
|
|
return docker.WithAuthCreds(func(host string) (string, string, error) {
|
|
if cfgHost != host {
|
|
logrus.WithFields(logrus.Fields{
|
|
"host": host,
|
|
"cfgHost": cfgHost,
|
|
}).Warn("Host doesn't match")
|
|
return "", "", nil
|
|
}
|
|
if authConfig.IdentityToken != "" {
|
|
return "", authConfig.IdentityToken, nil
|
|
}
|
|
return authConfig.Username, authConfig.Password, nil
|
|
})
|
|
}
|
|
|
|
type httpFallback struct {
|
|
super http.RoundTripper
|
|
}
|
|
|
|
func (f httpFallback) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
resp, err := f.super.RoundTrip(r)
|
|
var tlsErr tls.RecordHeaderError
|
|
if errors.As(err, &tlsErr) && string(tlsErr.RecordHeader[:]) == "HTTP/" {
|
|
// server gave HTTP response to HTTPS client
|
|
plainHttpUrl := *r.URL
|
|
plainHttpUrl.Scheme = "http"
|
|
|
|
plainHttpRequest := *r
|
|
plainHttpRequest.URL = &plainHttpUrl
|
|
|
|
return http.DefaultTransport.RoundTrip(&plainHttpRequest)
|
|
}
|
|
|
|
return resp, err
|
|
}
|