|
@@ -1,6 +1,9 @@
|
|
|
package containerd
|
|
|
|
|
|
import (
|
|
|
+ "net/http"
|
|
|
+ "strings"
|
|
|
+
|
|
|
"github.com/containerd/containerd/remotes"
|
|
|
"github.com/containerd/containerd/remotes/docker"
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
|
@@ -9,10 +12,10 @@ import (
|
|
|
)
|
|
|
|
|
|
func (i *ImageService) newResolverFromAuthConfig(authConfig *registrytypes.AuthConfig) (remotes.Resolver, docker.StatusTracker) {
|
|
|
+ tracker := docker.NewInMemoryTracker()
|
|
|
hostsFn := i.registryHosts.RegistryHosts()
|
|
|
- hosts := hostsAuthorizerWrapper(hostsFn, authConfig)
|
|
|
|
|
|
- tracker := docker.NewInMemoryTracker()
|
|
|
+ hosts := hostsWrapper(hostsFn, authConfig, i.registryService)
|
|
|
|
|
|
return docker.NewResolver(docker.ResolverOptions{
|
|
|
Hosts: hosts,
|
|
@@ -20,24 +23,29 @@ func (i *ImageService) newResolverFromAuthConfig(authConfig *registrytypes.AuthC
|
|
|
}), tracker
|
|
|
}
|
|
|
|
|
|
-func hostsAuthorizerWrapper(hostsFn docker.RegistryHosts, authConfig *registrytypes.AuthConfig) docker.RegistryHosts {
|
|
|
- return docker.RegistryHosts(func(n string) ([]docker.RegistryHost, error) {
|
|
|
+func hostsWrapper(hostsFn docker.RegistryHosts, authConfig *registrytypes.AuthConfig, regService registry.Service) docker.RegistryHosts {
|
|
|
+ return func(n string) ([]docker.RegistryHost, error) {
|
|
|
hosts, err := hostsFn(n)
|
|
|
- if err == nil {
|
|
|
- for idx, host := range hosts {
|
|
|
- if host.Authorizer == nil {
|
|
|
- var opts []docker.AuthorizerOpt
|
|
|
- if authConfig != nil {
|
|
|
- opts = append(opts, authorizationCredsFromAuthConfig(*authConfig))
|
|
|
- }
|
|
|
- host.Authorizer = docker.NewDockerAuthorizer(opts...)
|
|
|
- hosts[idx] = host
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ for i := range hosts {
|
|
|
+ if hosts[i].Authorizer == nil {
|
|
|
+ var opts []docker.AuthorizerOpt
|
|
|
+ if authConfig != nil {
|
|
|
+ opts = append(opts, authorizationCredsFromAuthConfig(*authConfig))
|
|
|
+ }
|
|
|
+ hosts[i].Authorizer = docker.NewDockerAuthorizer(opts...)
|
|
|
+
|
|
|
+ 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, err
|
|
|
- })
|
|
|
+ return hosts, nil
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func authorizationCredsFromAuthConfig(authConfig registrytypes.AuthConfig) docker.AuthorizerOpt {
|
|
@@ -57,3 +65,20 @@ func authorizationCredsFromAuthConfig(authConfig registrytypes.AuthConfig) docke
|
|
|
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)
|
|
|
+ if err != nil {
|
|
|
+ if strings.Contains(err.Error(), "http: server gave HTTP response to HTTPS client") {
|
|
|
+ plain := r.Clone(r.Context())
|
|
|
+ plain.URL.Scheme = "http"
|
|
|
+ return http.DefaultTransport.RoundTrip(plain)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return resp, err
|
|
|
+}
|