2018-02-05 21:05:59 +00:00
|
|
|
package registry // import "github.com/docker/docker/registry"
|
2014-10-07 01:54:52 +00:00
|
|
|
|
|
|
|
import (
|
2023-07-30 15:18:56 +00:00
|
|
|
"context"
|
2014-10-07 01:54:52 +00:00
|
|
|
"net"
|
|
|
|
"net/url"
|
2017-02-05 07:58:12 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2015-01-07 23:42:01 +00:00
|
|
|
"strings"
|
2014-10-07 01:54:52 +00:00
|
|
|
|
2023-07-30 15:18:56 +00:00
|
|
|
"github.com/containerd/containerd/log"
|
2023-08-30 16:31:46 +00:00
|
|
|
"github.com/distribution/reference"
|
2022-02-26 18:13:43 +00:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2014-10-07 01:54:52 +00:00
|
|
|
)
|
|
|
|
|
2016-03-08 21:03:37 +00:00
|
|
|
// ServiceOptions holds command line options.
|
|
|
|
type ServiceOptions struct {
|
2017-05-09 21:00:31 +00:00
|
|
|
AllowNondistributableArtifacts []string `json:"allow-nondistributable-artifacts,omitempty"`
|
|
|
|
Mirrors []string `json:"registry-mirrors,omitempty"`
|
|
|
|
InsecureRegistries []string `json:"insecure-registries,omitempty"`
|
2016-03-08 21:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// serviceConfig holds daemon configuration for the registry service.
|
2022-02-26 15:57:46 +00:00
|
|
|
type serviceConfig registry.ServiceConfig
|
2014-10-07 01:54:52 +00:00
|
|
|
|
2022-02-26 22:13:01 +00:00
|
|
|
// TODO(thaJeztah) both the "index.docker.io" and "registry-1.docker.io" domains
|
|
|
|
// are here for historic reasons and backward-compatibility. These domains
|
|
|
|
// are still supported by Docker Hub (and will continue to be supported), but
|
|
|
|
// there are new domains already in use, and plans to consolidate all legacy
|
|
|
|
// domains to new "canonical" domains. Once those domains are decided on, we
|
|
|
|
// should update these consts (but making sure to preserve compatibility with
|
|
|
|
// existing installs, clients, and user configuration).
|
2020-10-28 10:10:32 +00:00
|
|
|
const (
|
2015-08-04 23:30:00 +00:00
|
|
|
// DefaultNamespace is the default namespace
|
|
|
|
DefaultNamespace = "docker.io"
|
2022-02-26 22:13:01 +00:00
|
|
|
// DefaultRegistryHost is the hostname for the default (Docker Hub) registry
|
|
|
|
// used for pushing and pulling images. This hostname is hard-coded to handle
|
|
|
|
// the conversion from image references without registry name (e.g. "ubuntu",
|
|
|
|
// or "ubuntu:latest"), as well as references using the "docker.io" domain
|
|
|
|
// name, which is used as canonical reference for images on Docker Hub, but
|
|
|
|
// does not match the domain-name of Docker Hub's registry.
|
|
|
|
DefaultRegistryHost = "registry-1.docker.io"
|
|
|
|
// IndexHostname is the index hostname, used for authentication and image search.
|
2016-11-05 22:53:11 +00:00
|
|
|
IndexHostname = "index.docker.io"
|
|
|
|
// IndexServer is used for user auth and image search
|
|
|
|
IndexServer = "https://" + IndexHostname + "/v1/"
|
2015-08-04 23:30:00 +00:00
|
|
|
// IndexName is the name of the index
|
|
|
|
IndexName = "docker.io"
|
2020-10-28 10:10:32 +00:00
|
|
|
)
|
2015-08-04 23:30:00 +00:00
|
|
|
|
2020-10-28 10:10:32 +00:00
|
|
|
var (
|
2022-02-26 22:13:01 +00:00
|
|
|
// DefaultV2Registry is the URI of the default (Docker Hub) registry.
|
2016-03-10 20:26:32 +00:00
|
|
|
DefaultV2Registry = &url.URL{
|
|
|
|
Scheme: "https",
|
2022-02-26 22:13:01 +00:00
|
|
|
Host: DefaultRegistryHost,
|
2016-03-10 20:26:32 +00:00
|
|
|
}
|
2015-08-04 23:30:00 +00:00
|
|
|
|
2017-09-01 14:35:04 +00:00
|
|
|
emptyServiceConfig, _ = newServiceConfig(ServiceOptions{})
|
2020-10-28 10:10:32 +00:00
|
|
|
validHostPortRegex = regexp.MustCompile(`^` + reference.DomainRegexp.String() + `$`)
|
2015-01-07 23:42:01 +00:00
|
|
|
|
2020-10-28 10:10:32 +00:00
|
|
|
// for mocking in unit tests
|
|
|
|
lookupIP = net.LookupIP
|
2022-03-25 15:21:45 +00:00
|
|
|
|
|
|
|
// certsDir is used to override defaultCertsDir.
|
|
|
|
certsDir string
|
2017-02-05 07:58:12 +00:00
|
|
|
)
|
|
|
|
|
2022-03-25 15:21:45 +00:00
|
|
|
// SetCertsDir allows the default certs directory to be changed. This function
|
|
|
|
// is used at daemon startup to set the correct location when running in
|
|
|
|
// rootless mode.
|
|
|
|
func SetCertsDir(path string) {
|
|
|
|
certsDir = path
|
|
|
|
}
|
|
|
|
|
|
|
|
// CertsDir is the directory where certificates are stored.
|
|
|
|
func CertsDir() string {
|
|
|
|
if certsDir != "" {
|
|
|
|
return certsDir
|
|
|
|
}
|
|
|
|
return defaultCertsDir
|
|
|
|
}
|
|
|
|
|
2016-03-08 21:03:37 +00:00
|
|
|
// newServiceConfig returns a new instance of ServiceConfig
|
2017-09-01 14:35:04 +00:00
|
|
|
func newServiceConfig(options ServiceOptions) (*serviceConfig, error) {
|
2022-02-26 15:57:46 +00:00
|
|
|
config := &serviceConfig{}
|
2022-02-26 11:58:41 +00:00
|
|
|
if err := config.loadAllowNondistributableArtifacts(options.AllowNondistributableArtifacts); err != nil {
|
2017-09-01 14:35:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-26 11:58:41 +00:00
|
|
|
if err := config.loadMirrors(options.Mirrors); err != nil {
|
2017-09-01 14:35:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-26 11:58:41 +00:00
|
|
|
if err := config.loadInsecureRegistries(options.InsecureRegistries); err != nil {
|
2017-09-01 14:35:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-25 02:51:28 +00:00
|
|
|
|
2017-09-01 14:35:04 +00:00
|
|
|
return config, nil
|
2016-04-25 02:51:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 15:14:50 +00:00
|
|
|
// copy constructs a new ServiceConfig with a copy of the configuration in config.
|
2022-02-26 15:57:46 +00:00
|
|
|
func (config *serviceConfig) copy() *registry.ServiceConfig {
|
|
|
|
ic := make(map[string]*registry.IndexInfo)
|
2022-02-26 15:14:50 +00:00
|
|
|
for key, value := range config.IndexConfigs {
|
|
|
|
ic[key] = value
|
|
|
|
}
|
2022-02-26 15:57:46 +00:00
|
|
|
return ®istry.ServiceConfig{
|
|
|
|
AllowNondistributableArtifactsCIDRs: append([]*registry.NetIPNet(nil), config.AllowNondistributableArtifactsCIDRs...),
|
2022-02-26 15:14:50 +00:00
|
|
|
AllowNondistributableArtifactsHostnames: append([]string(nil), config.AllowNondistributableArtifactsHostnames...),
|
2022-02-26 15:57:46 +00:00
|
|
|
InsecureRegistryCIDRs: append([]*registry.NetIPNet(nil), config.InsecureRegistryCIDRs...),
|
2022-02-26 15:14:50 +00:00
|
|
|
IndexConfigs: ic,
|
|
|
|
Mirrors: append([]string(nil), config.Mirrors...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-26 11:58:41 +00:00
|
|
|
// loadAllowNondistributableArtifacts loads allow-nondistributable-artifacts registries into config.
|
|
|
|
func (config *serviceConfig) loadAllowNondistributableArtifacts(registries []string) error {
|
2022-02-26 18:13:43 +00:00
|
|
|
cidrs := map[string]*registry.NetIPNet{}
|
2017-05-09 21:00:31 +00:00
|
|
|
hostnames := map[string]bool{}
|
|
|
|
|
|
|
|
for _, r := range registries {
|
|
|
|
if _, err := ValidateIndexName(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-26 12:04:15 +00:00
|
|
|
if hasScheme(r) {
|
2022-02-26 12:45:12 +00:00
|
|
|
return invalidParamf("allow-nondistributable-artifacts registry %s should not contain '://'", r)
|
2017-05-09 21:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ipnet, err := net.ParseCIDR(r); err == nil {
|
|
|
|
// Valid CIDR.
|
2022-02-26 18:13:43 +00:00
|
|
|
cidrs[ipnet.String()] = (*registry.NetIPNet)(ipnet)
|
2022-02-26 12:45:12 +00:00
|
|
|
} else if err = validateHostPort(r); err == nil {
|
2017-05-09 21:00:31 +00:00
|
|
|
// Must be `host:port` if not CIDR.
|
|
|
|
hostnames[r] = true
|
|
|
|
} else {
|
2022-02-26 12:45:12 +00:00
|
|
|
return invalidParamWrapf(err, "allow-nondistributable-artifacts registry %s is not valid", r)
|
2017-05-09 21:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-26 13:32:13 +00:00
|
|
|
config.AllowNondistributableArtifactsCIDRs = make([]*registry.NetIPNet, 0, len(cidrs))
|
2017-05-09 21:00:31 +00:00
|
|
|
for _, c := range cidrs {
|
|
|
|
config.AllowNondistributableArtifactsCIDRs = append(config.AllowNondistributableArtifactsCIDRs, c)
|
|
|
|
}
|
|
|
|
|
2022-02-26 13:32:13 +00:00
|
|
|
config.AllowNondistributableArtifactsHostnames = make([]string, 0, len(hostnames))
|
2017-05-09 21:00:31 +00:00
|
|
|
for h := range hostnames {
|
|
|
|
config.AllowNondistributableArtifactsHostnames = append(config.AllowNondistributableArtifactsHostnames, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-26 11:58:41 +00:00
|
|
|
// loadMirrors loads mirrors to config, after removing duplicates.
|
2016-12-21 10:31:05 +00:00
|
|
|
// Returns an error if mirrors contains an invalid mirror.
|
2022-02-26 11:58:41 +00:00
|
|
|
func (config *serviceConfig) loadMirrors(mirrors []string) error {
|
2016-12-21 10:31:05 +00:00
|
|
|
mMap := map[string]struct{}{}
|
|
|
|
unique := []string{}
|
|
|
|
|
|
|
|
for _, mirror := range mirrors {
|
|
|
|
m, err := ValidateMirror(mirror)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, exist := mMap[m]; !exist {
|
|
|
|
mMap[m] = struct{}{}
|
|
|
|
unique = append(unique, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Mirrors = unique
|
|
|
|
|
|
|
|
// Configure public registry since mirrors may have changed.
|
2022-02-26 15:57:46 +00:00
|
|
|
config.IndexConfigs = map[string]*registry.IndexInfo{
|
|
|
|
IndexName: {
|
|
|
|
Name: IndexName,
|
|
|
|
Mirrors: unique,
|
|
|
|
Secure: true,
|
|
|
|
Official: true,
|
|
|
|
},
|
2016-12-21 10:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-26 11:58:41 +00:00
|
|
|
// loadInsecureRegistries loads insecure registries to config
|
|
|
|
func (config *serviceConfig) loadInsecureRegistries(registries []string) error {
|
2022-02-26 00:06:18 +00:00
|
|
|
// Localhost is by default considered as an insecure registry. This is a
|
|
|
|
// stop-gap for people who are running a private registry on localhost.
|
2016-04-25 02:51:28 +00:00
|
|
|
registries = append(registries, "127.0.0.0/8")
|
|
|
|
|
2022-02-26 15:05:44 +00:00
|
|
|
var (
|
|
|
|
insecureRegistryCIDRs = make([]*registry.NetIPNet, 0)
|
|
|
|
indexConfigs = make(map[string]*registry.IndexInfo)
|
|
|
|
)
|
2016-04-25 02:51:28 +00:00
|
|
|
|
|
|
|
skip:
|
|
|
|
for _, r := range registries {
|
|
|
|
// validate insecure registry
|
|
|
|
if _, err := ValidateIndexName(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-09 03:30:58 +00:00
|
|
|
if strings.HasPrefix(strings.ToLower(r), "http://") {
|
2023-07-30 15:18:56 +00:00
|
|
|
log.G(context.TODO()).Warnf("insecure registry %s should not contain 'http://' and 'http://' has been removed from the insecure registry config", r)
|
2017-01-09 03:30:58 +00:00
|
|
|
r = r[7:]
|
|
|
|
} else if strings.HasPrefix(strings.ToLower(r), "https://") {
|
2023-07-30 15:18:56 +00:00
|
|
|
log.G(context.TODO()).Warnf("insecure registry %s should not contain 'https://' and 'https://' has been removed from the insecure registry config", r)
|
2017-01-09 03:30:58 +00:00
|
|
|
r = r[8:]
|
2022-02-26 12:04:15 +00:00
|
|
|
} else if hasScheme(r) {
|
2022-02-26 12:45:12 +00:00
|
|
|
return invalidParamf("insecure registry %s should not contain '://'", r)
|
2017-01-09 03:30:58 +00:00
|
|
|
}
|
2014-10-07 01:54:52 +00:00
|
|
|
// Check if CIDR was passed to --insecure-registry
|
|
|
|
_, ipnet, err := net.ParseCIDR(r)
|
|
|
|
if err == nil {
|
2016-04-25 02:51:28 +00:00
|
|
|
// Valid CIDR. If ipnet is already in config.InsecureRegistryCIDRs, skip.
|
2022-02-26 18:13:43 +00:00
|
|
|
data := (*registry.NetIPNet)(ipnet)
|
2022-02-26 15:05:44 +00:00
|
|
|
for _, value := range insecureRegistryCIDRs {
|
2016-04-25 02:51:28 +00:00
|
|
|
if value.IP.String() == data.IP.String() && value.Mask.String() == data.Mask.String() {
|
|
|
|
continue skip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ipnet is not found, add it in config.InsecureRegistryCIDRs
|
2022-02-26 15:05:44 +00:00
|
|
|
insecureRegistryCIDRs = append(insecureRegistryCIDRs, data)
|
2014-10-07 01:54:52 +00:00
|
|
|
} else {
|
2017-02-05 07:58:12 +00:00
|
|
|
if err := validateHostPort(r); err != nil {
|
2022-02-26 12:45:12 +00:00
|
|
|
return invalidParamWrapf(err, "insecure registry %s is not valid", r)
|
2017-02-05 07:58:12 +00:00
|
|
|
}
|
2014-10-07 01:54:52 +00:00
|
|
|
// Assume `host:port` if not CIDR.
|
2022-02-26 15:05:44 +00:00
|
|
|
indexConfigs[r] = ®istry.IndexInfo{
|
2014-10-07 01:54:52 +00:00
|
|
|
Name: r,
|
|
|
|
Mirrors: make([]string, 0),
|
|
|
|
Secure: false,
|
|
|
|
Official: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure public registry.
|
2022-02-26 15:05:44 +00:00
|
|
|
indexConfigs[IndexName] = ®istry.IndexInfo{
|
2015-07-21 19:40:36 +00:00
|
|
|
Name: IndexName,
|
2015-02-12 18:23:22 +00:00
|
|
|
Mirrors: config.Mirrors,
|
2014-10-07 01:54:52 +00:00
|
|
|
Secure: true,
|
|
|
|
Official: true,
|
|
|
|
}
|
2022-02-26 15:05:44 +00:00
|
|
|
config.InsecureRegistryCIDRs = insecureRegistryCIDRs
|
|
|
|
config.IndexConfigs = indexConfigs
|
2014-10-07 01:54:52 +00:00
|
|
|
|
2016-04-25 02:51:28 +00:00
|
|
|
return nil
|
2014-10-07 01:54:52 +00:00
|
|
|
}
|
2015-01-07 23:42:01 +00:00
|
|
|
|
2017-05-21 23:24:07 +00:00
|
|
|
// allowNondistributableArtifacts returns true if the provided hostname is part of the list of registries
|
2017-05-09 21:00:31 +00:00
|
|
|
// that allow push of nondistributable artifacts.
|
|
|
|
//
|
|
|
|
// The list can contain elements with CIDR notation to specify a whole subnet. If the subnet contains an IP
|
|
|
|
// of the registry specified by hostname, true is returned.
|
|
|
|
//
|
|
|
|
// hostname should be a URL.Host (`host:port` or `host`) where the `host` part can be either a domain name
|
|
|
|
// or an IP address. If it is a domain name, then it will be resolved to IP addresses for matching. If
|
|
|
|
// resolution fails, CIDR matching is not performed.
|
2022-02-27 12:03:54 +00:00
|
|
|
func (config *serviceConfig) allowNondistributableArtifacts(hostname string) bool {
|
2017-05-09 21:00:31 +00:00
|
|
|
for _, h := range config.AllowNondistributableArtifactsHostnames {
|
|
|
|
if h == hostname {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return isCIDRMatch(config.AllowNondistributableArtifactsCIDRs, hostname)
|
|
|
|
}
|
|
|
|
|
2015-01-07 23:42:01 +00:00
|
|
|
// isSecureIndex returns false if the provided indexName is part of the list of insecure registries
|
|
|
|
// Insecure registries accept HTTP and/or accept HTTPS with certificates from unknown CAs.
|
|
|
|
//
|
|
|
|
// The list of insecure registries can contain an element with CIDR notation to specify a whole subnet.
|
|
|
|
// If the subnet contains one of the IPs of the registry specified by indexName, the latter is considered
|
|
|
|
// insecure.
|
|
|
|
//
|
|
|
|
// indexName should be a URL.Host (`host:port` or `host`) where the `host` part can be either a domain name
|
|
|
|
// or an IP address. If it is a domain name, then it will be resolved in order to check if the IP is contained
|
|
|
|
// in a subnet. If the resolving is not successful, isSecureIndex will only try to match hostname to any element
|
|
|
|
// of insecureRegistries.
|
2022-02-27 12:03:54 +00:00
|
|
|
func (config *serviceConfig) isSecureIndex(indexName string) bool {
|
2015-01-07 23:42:01 +00:00
|
|
|
// Check for configured index, first. This is needed in case isSecureIndex
|
2015-12-12 02:14:52 +00:00
|
|
|
// is called from anything besides newIndexInfo, in order to honor per-index configurations.
|
2015-01-07 23:42:01 +00:00
|
|
|
if index, ok := config.IndexConfigs[indexName]; ok {
|
|
|
|
return index.Secure
|
|
|
|
}
|
|
|
|
|
2017-05-09 21:00:31 +00:00
|
|
|
return !isCIDRMatch(config.InsecureRegistryCIDRs, indexName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// isCIDRMatch returns true if URLHost matches an element of cidrs. URLHost is a URL.Host (`host:port` or `host`)
|
|
|
|
// where the `host` part can be either a domain name or an IP address. If it is a domain name, then it will be
|
|
|
|
// resolved to IP addresses for matching. If resolution fails, false is returned.
|
2022-02-26 18:13:43 +00:00
|
|
|
func isCIDRMatch(cidrs []*registry.NetIPNet, URLHost string) bool {
|
2017-05-09 21:00:31 +00:00
|
|
|
host, _, err := net.SplitHostPort(URLHost)
|
2015-01-07 23:42:01 +00:00
|
|
|
if err != nil {
|
2017-05-09 21:00:31 +00:00
|
|
|
// Assume URLHost is of the form `host` without the port and go on.
|
|
|
|
host = URLHost
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err := lookupIP(host)
|
|
|
|
if err != nil {
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip != nil {
|
|
|
|
addrs = []net.IP{ip}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if ip == nil, then `host` is neither an IP nor it could be looked up,
|
|
|
|
// either because the index is unreachable, or because the index is behind an HTTP proxy.
|
|
|
|
// So, len(addrs) == 0 and we're not aborting.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try CIDR notation only if addrs has any elements, i.e. if `host`'s IP could be determined.
|
|
|
|
for _, addr := range addrs {
|
2017-05-09 21:00:31 +00:00
|
|
|
for _, ipnet := range cidrs {
|
2015-01-07 23:42:01 +00:00
|
|
|
// check if the addr falls in the subnet
|
|
|
|
if (*net.IPNet)(ipnet).Contains(addr) {
|
2017-05-09 21:00:31 +00:00
|
|
|
return true
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 21:00:31 +00:00
|
|
|
return false
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 19:46:24 +00:00
|
|
|
// ValidateMirror validates an HTTP(S) registry mirror. It is used by the daemon
|
|
|
|
// to validate the daemon configuration.
|
2015-01-07 23:42:01 +00:00
|
|
|
func ValidateMirror(val string) (string, error) {
|
|
|
|
uri, err := url.Parse(val)
|
|
|
|
if err != nil {
|
2022-02-26 12:45:12 +00:00
|
|
|
return "", invalidParamWrapf(err, "invalid mirror: %q is not a valid URI", val)
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
if uri.Scheme != "http" && uri.Scheme != "https" {
|
2022-02-26 12:45:12 +00:00
|
|
|
return "", invalidParamf("invalid mirror: unsupported scheme %q in %q", uri.Scheme, uri)
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
2016-12-21 10:31:05 +00:00
|
|
|
if (uri.Path != "" && uri.Path != "/") || uri.RawQuery != "" || uri.Fragment != "" {
|
2022-02-26 12:45:12 +00:00
|
|
|
return "", invalidParamf("invalid mirror: path, query, or fragment at end of the URI %q", uri)
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
2016-12-21 10:31:05 +00:00
|
|
|
if uri.User != nil {
|
|
|
|
// strip password from output
|
|
|
|
uri.User = url.UserPassword(uri.User.Username(), "xxxxx")
|
2022-02-26 12:45:12 +00:00
|
|
|
return "", invalidParamf("invalid mirror: username/password not allowed in URI %q", uri)
|
2016-12-21 10:31:05 +00:00
|
|
|
}
|
|
|
|
return strings.TrimSuffix(val, "/") + "/", nil
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 19:46:24 +00:00
|
|
|
// ValidateIndexName validates an index name. It is used by the daemon to
|
|
|
|
// validate the daemon configuration.
|
2015-01-07 23:42:01 +00:00
|
|
|
func ValidateIndexName(val string) (string, error) {
|
2017-01-26 00:54:18 +00:00
|
|
|
// TODO: upstream this to check to reference package
|
|
|
|
if val == "index.docker.io" {
|
|
|
|
val = "docker.io"
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
2015-05-13 06:23:13 +00:00
|
|
|
if strings.HasPrefix(val, "-") || strings.HasSuffix(val, "-") {
|
2022-02-26 12:45:12 +00:00
|
|
|
return "", invalidParamf("invalid index name (%s). Cannot begin or end with a hyphen", val)
|
2015-05-13 06:23:13 +00:00
|
|
|
}
|
2015-01-07 23:42:01 +00:00
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2022-02-26 12:04:15 +00:00
|
|
|
func hasScheme(reposName string) bool {
|
|
|
|
return strings.Contains(reposName, "://")
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-05 07:58:12 +00:00
|
|
|
func validateHostPort(s string) error {
|
|
|
|
// Split host and port, and in case s can not be splitted, assume host only
|
|
|
|
host, port, err := net.SplitHostPort(s)
|
|
|
|
if err != nil {
|
|
|
|
host = s
|
|
|
|
port = ""
|
|
|
|
}
|
|
|
|
// If match against the `host:port` pattern fails,
|
|
|
|
// it might be `IPv6:port`, which will be captured by net.ParseIP(host)
|
|
|
|
if !validHostPortRegex.MatchString(s) && net.ParseIP(host) == nil {
|
2022-02-26 12:45:12 +00:00
|
|
|
return invalidParamf("invalid host %q", host)
|
2017-02-05 07:58:12 +00:00
|
|
|
}
|
|
|
|
if port != "" {
|
|
|
|
v, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if v < 0 || v > 65535 {
|
2022-02-26 12:45:12 +00:00
|
|
|
return invalidParamf("invalid port %q", port)
|
2017-02-05 07:58:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
// newIndexInfo returns IndexInfo configuration from indexName
|
2022-02-26 18:13:43 +00:00
|
|
|
func newIndexInfo(config *serviceConfig, indexName string) (*registry.IndexInfo, error) {
|
2015-01-07 23:42:01 +00:00
|
|
|
var err error
|
|
|
|
indexName, err = ValidateIndexName(indexName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return any configured index info, first.
|
|
|
|
if index, ok := config.IndexConfigs[indexName]; ok {
|
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a non-configured index info.
|
2022-02-26 13:32:13 +00:00
|
|
|
return ®istry.IndexInfo{
|
2015-01-07 23:42:01 +00:00
|
|
|
Name: indexName,
|
|
|
|
Mirrors: make([]string, 0),
|
2022-02-27 12:03:54 +00:00
|
|
|
Secure: config.isSecureIndex(indexName),
|
2015-01-07 23:42:01 +00:00
|
|
|
Official: false,
|
2022-02-26 13:32:13 +00:00
|
|
|
}, nil
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAuthConfigKey special-cases using the full index address of the official
|
|
|
|
// index as the AuthConfig key, and uses the (host)name[:port] for private indexes.
|
2022-02-26 18:13:43 +00:00
|
|
|
func GetAuthConfigKey(index *registry.IndexInfo) string {
|
2015-01-07 23:42:01 +00:00
|
|
|
if index.Official {
|
2015-07-21 19:40:36 +00:00
|
|
|
return IndexServer
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
return index.Name
|
|
|
|
}
|
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
// newRepositoryInfo validates and breaks down a repository name into a RepositoryInfo
|
2016-03-08 21:03:37 +00:00
|
|
|
func newRepositoryInfo(config *serviceConfig, name reference.Named) (*RepositoryInfo, error) {
|
2017-01-11 21:54:52 +00:00
|
|
|
index, err := newIndexInfo(config, reference.Domain(name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
official := !strings.ContainsRune(reference.FamiliarName(name), '/')
|
|
|
|
|
2016-11-15 23:06:48 +00:00
|
|
|
return &RepositoryInfo{
|
2017-01-26 00:54:18 +00:00
|
|
|
Name: reference.TrimNamed(name),
|
2016-11-15 23:06:48 +00:00
|
|
|
Index: index,
|
|
|
|
Official: official,
|
|
|
|
}, nil
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 19:46:24 +00:00
|
|
|
// ParseRepositoryInfo performs the breakdown of a repository name into a
|
|
|
|
// [RepositoryInfo], but lacks registry configuration.
|
|
|
|
//
|
|
|
|
// It is used by the Docker cli to interact with registry-related endpoints.
|
2015-11-18 22:20:54 +00:00
|
|
|
func ParseRepositoryInfo(reposName reference.Named) (*RepositoryInfo, error) {
|
2015-12-12 02:14:52 +00:00
|
|
|
return newRepositoryInfo(emptyServiceConfig, reposName)
|
2015-09-22 11:44:40 +00:00
|
|
|
}
|