| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package resolver |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"net/http" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/containerd/containerd/reference" |
| 25 |
"github.com/containerd/containerd/remotes/docker" |
| 26 |
"github.com/containerd/stargz-snapshotter/fs/source" |
| 27 |
rhttp "github.com/hashicorp/go-retryablehttp" |
| 28 |
) |
| 29 |
|
| 30 |
const defaultRequestTimeoutSec = 30 |
| 31 |
|
| 32 |
// Config is config for resolving registries. |
| 33 |
type Config struct { |
| 34 |
Host map[string]HostConfig `toml:"host"` |
| 35 |
} |
| 36 |
|
| 37 |
type HostConfig struct { |
| 38 |
Mirrors []MirrorConfig `toml:"mirrors"` |
| 39 |
} |
| 40 |
|
| 41 |
type MirrorConfig struct { |
| 42 |
|
| 43 |
// Host is the hostname of the host. |
| 44 |
Host string `toml:"host"` |
| 45 |
|
| 46 |
// Insecure is true means use http scheme instead of https. |
| 47 |
Insecure bool `toml:"insecure"` |
| 48 |
|
| 49 |
// RequestTimeoutSec is timeout seconds of each request to the registry. |
| 50 |
// RequestTimeoutSec == 0 indicates the default timeout (defaultRequestTimeoutSec). |
| 51 |
// RequestTimeoutSec < 0 indicates no timeout. |
| 52 |
RequestTimeoutSec int `toml:"request_timeout_sec"` |
| 53 |
|
| 54 |
// Header are additional headers to send to the server |
| 55 |
Header map[string]interface{} `toml:"header"` |
| 56 |
} |
| 57 |
|
| 58 |
type Credential func(string, reference.Spec) (string, string, error) |
| 59 |
|
| 60 |
// RegistryHostsFromConfig creates RegistryHosts (a set of registry configuration) from Config. |
| 61 |
func RegistryHostsFromConfig(cfg Config, credsFuncs ...Credential) source.RegistryHosts { |
| 62 |
return func(ref reference.Spec) (hosts []docker.RegistryHost, _ error) { |
| 63 |
host := ref.Hostname() |
| 64 |
for _, h := range append(cfg.Host[host].Mirrors, MirrorConfig{ |
| 65 |
Host: host, |
| 66 |
}) { |
| 67 |
client := rhttp.NewClient() |
| 68 |
client.Logger = nil // disable logging every request |
| 69 |
tr := client.StandardClient() |
| 70 |
if h.RequestTimeoutSec >= 0 { |
| 71 |
if h.RequestTimeoutSec == 0 { |
| 72 |
tr.Timeout = defaultRequestTimeoutSec * time.Second |
| 73 |
} else { |
| 74 |
tr.Timeout = time.Duration(h.RequestTimeoutSec) * time.Second |
| 75 |
} |
| 76 |
} // h.RequestTimeoutSec < 0 means "no timeout" |
| 77 |
var header http.Header |
| 78 |
var err error |
| 79 |
if h.Header != nil { |
| 80 |
header = http.Header{} |
| 81 |
for key, ty := range h.Header { |
| 82 |
switch value := ty.(type) { |
| 83 |
case string: |
| 84 |
header[key] = []string{value} |
| 85 |
case []interface{}: |
| 86 |
header[key], err = makeStringSlice(value, nil) |
| 87 |
if err != nil { |
| 88 |
return nil, err |
| 89 |
} |
| 90 |
default: |
| 91 |
return nil, fmt.Errorf("invalid type %v for header %q", ty, key) |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
config := docker.RegistryHost{ |
| 96 |
Client: tr, |
| 97 |
Host: h.Host, |
| 98 |
Scheme: "https", |
| 99 |
Path: "/v2", |
| 100 |
Capabilities: docker.HostCapabilityPull | docker.HostCapabilityResolve, |
| 101 |
Authorizer: docker.NewDockerAuthorizer( |
| 102 |
docker.WithAuthClient(tr), |
| 103 |
docker.WithAuthCreds(multiCredsFuncs(ref, credsFuncs...))), |
| 104 |
Header: header, |
| 105 |
} |
| 106 |
if localhost, _ := docker.MatchLocalhost(config.Host); localhost || h.Insecure { |
| 107 |
config.Scheme = "http" |
| 108 |
} |
| 109 |
if config.Host == "docker.io" { |
| 110 |
config.Host = "registry-1.docker.io" |
| 111 |
} |
| 112 |
hosts = append(hosts, config) |
| 113 |
} |
| 114 |
return |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func multiCredsFuncs(ref reference.Spec, credsFuncs ...Credential) func(string) (string, string, error) { |
| 119 |
return func(host string) (string, string, error) { |
| 120 |
for _, f := range credsFuncs { |
| 121 |
if username, secret, err := f(host, ref); err != nil { |
| 122 |
return "", "", err |
| 123 |
} else if !(username == "" && secret == "") { |
| 124 |
return username, secret, nil |
| 125 |
} |
| 126 |
} |
| 127 |
return "", "", nil |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// makeStringSlice is a helper func to convert from []interface{} to []string. |
| 132 |
// Additionally an optional cb func may be passed to perform string mapping. |
| 133 |
// NOTE: Ported from https://github.com/containerd/containerd/blob/v1.6.9/remotes/docker/config/hosts.go#L516-L533 |
| 134 |
func makeStringSlice(slice []interface{}, cb func(string) string) ([]string, error) { |
| 135 |
out := make([]string, len(slice)) |
| 136 |
for i, value := range slice { |
| 137 |
str, ok := value.(string) |
| 138 |
if !ok { |
| 139 |
return nil, fmt.Errorf("unable to cast %v to string", value) |
| 140 |
} |
| 141 |
|
| 142 |
if cb != nil { |
| 143 |
out[i] = cb(str) |
| 144 |
} else { |
| 145 |
out[i] = str |
| 146 |
} |
| 147 |
} |
| 148 |
return out, nil |
| 149 |
} |