github.com/mforkel/docker-ce-i386@v17.12.1-ce-rc2+incompatible/components/engine/registry/config.go (about)

     1  package registry
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"net/url"
     7  	"regexp"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/docker/distribution/reference"
    12  	registrytypes "github.com/docker/docker/api/types/registry"
    13  	"github.com/pkg/errors"
    14  	"github.com/sirupsen/logrus"
    15  )
    16  
    17  // ServiceOptions holds command line options.
    18  type ServiceOptions struct {
    19  	AllowNondistributableArtifacts []string `json:"allow-nondistributable-artifacts,omitempty"`
    20  	Mirrors                        []string `json:"registry-mirrors,omitempty"`
    21  	InsecureRegistries             []string `json:"insecure-registries,omitempty"`
    22  
    23  	// V2Only controls access to legacy registries.  If it is set to true via the
    24  	// command line flag the daemon will not attempt to contact v1 legacy registries
    25  	V2Only bool `json:"disable-legacy-registry,omitempty"`
    26  }
    27  
    28  // serviceConfig holds daemon configuration for the registry service.
    29  type serviceConfig struct {
    30  	registrytypes.ServiceConfig
    31  	V2Only bool
    32  }
    33  
    34  var (
    35  	// DefaultNamespace is the default namespace
    36  	DefaultNamespace = "docker.io"
    37  	// DefaultRegistryVersionHeader is the name of the default HTTP header
    38  	// that carries Registry version info
    39  	DefaultRegistryVersionHeader = "Docker-Distribution-Api-Version"
    40  
    41  	// IndexHostname is the index hostname
    42  	IndexHostname = "index.docker.io"
    43  	// IndexServer is used for user auth and image search
    44  	IndexServer = "https://" + IndexHostname + "/v1/"
    45  	// IndexName is the name of the index
    46  	IndexName = "docker.io"
    47  
    48  	// NotaryServer is the endpoint serving the Notary trust server
    49  	NotaryServer = "https://notary.docker.io"
    50  
    51  	// DefaultV2Registry is the URI of the default v2 registry
    52  	DefaultV2Registry = &url.URL{
    53  		Scheme: "https",
    54  		Host:   "registry-1.docker.io",
    55  	}
    56  )
    57  
    58  var (
    59  	// ErrInvalidRepositoryName is an error returned if the repository name did
    60  	// not have the correct form
    61  	ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")")
    62  
    63  	emptyServiceConfig, _ = newServiceConfig(ServiceOptions{})
    64  )
    65  
    66  var (
    67  	validHostPortRegex = regexp.MustCompile(`^` + reference.DomainRegexp.String() + `$`)
    68  )
    69  
    70  // for mocking in unit tests
    71  var lookupIP = net.LookupIP
    72  
    73  // newServiceConfig returns a new instance of ServiceConfig
    74  func newServiceConfig(options ServiceOptions) (*serviceConfig, error) {
    75  	config := &serviceConfig{
    76  		ServiceConfig: registrytypes.ServiceConfig{
    77  			InsecureRegistryCIDRs: make([]*registrytypes.NetIPNet, 0),
    78  			IndexConfigs:          make(map[string]*registrytypes.IndexInfo),
    79  			// Hack: Bypass setting the mirrors to IndexConfigs since they are going away
    80  			// and Mirrors are only for the official registry anyways.
    81  		},
    82  		V2Only: options.V2Only,
    83  	}
    84  	if err := config.LoadAllowNondistributableArtifacts(options.AllowNondistributableArtifacts); err != nil {
    85  		return nil, err
    86  	}
    87  	if err := config.LoadMirrors(options.Mirrors); err != nil {
    88  		return nil, err
    89  	}
    90  	if err := config.LoadInsecureRegistries(options.InsecureRegistries); err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	return config, nil
    95  }
    96  
    97  // LoadAllowNondistributableArtifacts loads allow-nondistributable-artifacts registries into config.
    98  func (config *serviceConfig) LoadAllowNondistributableArtifacts(registries []string) error {
    99  	cidrs := map[string]*registrytypes.NetIPNet{}
   100  	hostnames := map[string]bool{}
   101  
   102  	for _, r := range registries {
   103  		if _, err := ValidateIndexName(r); err != nil {
   104  			return err
   105  		}
   106  		if validateNoScheme(r) != nil {
   107  			return fmt.Errorf("allow-nondistributable-artifacts registry %s should not contain '://'", r)
   108  		}
   109  
   110  		if _, ipnet, err := net.ParseCIDR(r); err == nil {
   111  			// Valid CIDR.
   112  			cidrs[ipnet.String()] = (*registrytypes.NetIPNet)(ipnet)
   113  		} else if err := validateHostPort(r); err == nil {
   114  			// Must be `host:port` if not CIDR.
   115  			hostnames[r] = true
   116  		} else {
   117  			return fmt.Errorf("allow-nondistributable-artifacts registry %s is not valid: %v", r, err)
   118  		}
   119  	}
   120  
   121  	config.AllowNondistributableArtifactsCIDRs = make([]*(registrytypes.NetIPNet), 0)
   122  	for _, c := range cidrs {
   123  		config.AllowNondistributableArtifactsCIDRs = append(config.AllowNondistributableArtifactsCIDRs, c)
   124  	}
   125  
   126  	config.AllowNondistributableArtifactsHostnames = make([]string, 0)
   127  	for h := range hostnames {
   128  		config.AllowNondistributableArtifactsHostnames = append(config.AllowNondistributableArtifactsHostnames, h)
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  // LoadMirrors loads mirrors to config, after removing duplicates.
   135  // Returns an error if mirrors contains an invalid mirror.
   136  func (config *serviceConfig) LoadMirrors(mirrors []string) error {
   137  	mMap := map[string]struct{}{}
   138  	unique := []string{}
   139  
   140  	for _, mirror := range mirrors {
   141  		m, err := ValidateMirror(mirror)
   142  		if err != nil {
   143  			return err
   144  		}
   145  		if _, exist := mMap[m]; !exist {
   146  			mMap[m] = struct{}{}
   147  			unique = append(unique, m)
   148  		}
   149  	}
   150  
   151  	config.Mirrors = unique
   152  
   153  	// Configure public registry since mirrors may have changed.
   154  	config.IndexConfigs[IndexName] = &registrytypes.IndexInfo{
   155  		Name:     IndexName,
   156  		Mirrors:  config.Mirrors,
   157  		Secure:   true,
   158  		Official: true,
   159  	}
   160  
   161  	return nil
   162  }
   163  
   164  // LoadInsecureRegistries loads insecure registries to config
   165  func (config *serviceConfig) LoadInsecureRegistries(registries []string) error {
   166  	// Localhost is by default considered as an insecure registry
   167  	// This is a stop-gap for people who are running a private registry on localhost (especially on Boot2docker).
   168  	//
   169  	// TODO: should we deprecate this once it is easier for people to set up a TLS registry or change
   170  	// daemon flags on boot2docker?
   171  	registries = append(registries, "127.0.0.0/8")
   172  
   173  	// Store original InsecureRegistryCIDRs and IndexConfigs
   174  	// Clean InsecureRegistryCIDRs and IndexConfigs in config, as passed registries has all insecure registry info.
   175  	originalCIDRs := config.ServiceConfig.InsecureRegistryCIDRs
   176  	originalIndexInfos := config.ServiceConfig.IndexConfigs
   177  
   178  	config.ServiceConfig.InsecureRegistryCIDRs = make([]*registrytypes.NetIPNet, 0)
   179  	config.ServiceConfig.IndexConfigs = make(map[string]*registrytypes.IndexInfo)
   180  
   181  skip:
   182  	for _, r := range registries {
   183  		// validate insecure registry
   184  		if _, err := ValidateIndexName(r); err != nil {
   185  			// before returning err, roll back to original data
   186  			config.ServiceConfig.InsecureRegistryCIDRs = originalCIDRs
   187  			config.ServiceConfig.IndexConfigs = originalIndexInfos
   188  			return err
   189  		}
   190  		if strings.HasPrefix(strings.ToLower(r), "http://") {
   191  			logrus.Warnf("insecure registry %s should not contain 'http://' and 'http://' has been removed from the insecure registry config", r)
   192  			r = r[7:]
   193  		} else if strings.HasPrefix(strings.ToLower(r), "https://") {
   194  			logrus.Warnf("insecure registry %s should not contain 'https://' and 'https://' has been removed from the insecure registry config", r)
   195  			r = r[8:]
   196  		} else if validateNoScheme(r) != nil {
   197  			// Insecure registry should not contain '://'
   198  			// before returning err, roll back to original data
   199  			config.ServiceConfig.InsecureRegistryCIDRs = originalCIDRs
   200  			config.ServiceConfig.IndexConfigs = originalIndexInfos
   201  			return fmt.Errorf("insecure registry %s should not contain '://'", r)
   202  		}
   203  		// Check if CIDR was passed to --insecure-registry
   204  		_, ipnet, err := net.ParseCIDR(r)
   205  		if err == nil {
   206  			// Valid CIDR. If ipnet is already in config.InsecureRegistryCIDRs, skip.
   207  			data := (*registrytypes.NetIPNet)(ipnet)
   208  			for _, value := range config.InsecureRegistryCIDRs {
   209  				if value.IP.String() == data.IP.String() && value.Mask.String() == data.Mask.String() {
   210  					continue skip
   211  				}
   212  			}
   213  			// ipnet is not found, add it in config.InsecureRegistryCIDRs
   214  			config.InsecureRegistryCIDRs = append(config.InsecureRegistryCIDRs, data)
   215  
   216  		} else {
   217  			if err := validateHostPort(r); err != nil {
   218  				config.ServiceConfig.InsecureRegistryCIDRs = originalCIDRs
   219  				config.ServiceConfig.IndexConfigs = originalIndexInfos
   220  				return fmt.Errorf("insecure registry %s is not valid: %v", r, err)
   221  
   222  			}
   223  			// Assume `host:port` if not CIDR.
   224  			config.IndexConfigs[r] = &registrytypes.IndexInfo{
   225  				Name:     r,
   226  				Mirrors:  make([]string, 0),
   227  				Secure:   false,
   228  				Official: false,
   229  			}
   230  		}
   231  	}
   232  
   233  	// Configure public registry.
   234  	config.IndexConfigs[IndexName] = &registrytypes.IndexInfo{
   235  		Name:     IndexName,
   236  		Mirrors:  config.Mirrors,
   237  		Secure:   true,
   238  		Official: true,
   239  	}
   240  
   241  	return nil
   242  }
   243  
   244  // allowNondistributableArtifacts returns true if the provided hostname is part of the list of registries
   245  // that allow push of nondistributable artifacts.
   246  //
   247  // The list can contain elements with CIDR notation to specify a whole subnet. If the subnet contains an IP
   248  // of the registry specified by hostname, true is returned.
   249  //
   250  // hostname should be a URL.Host (`host:port` or `host`) where the `host` part can be either a domain name
   251  // or an IP address. If it is a domain name, then it will be resolved to IP addresses for matching. If
   252  // resolution fails, CIDR matching is not performed.
   253  func allowNondistributableArtifacts(config *serviceConfig, hostname string) bool {
   254  	for _, h := range config.AllowNondistributableArtifactsHostnames {
   255  		if h == hostname {
   256  			return true
   257  		}
   258  	}
   259  
   260  	return isCIDRMatch(config.AllowNondistributableArtifactsCIDRs, hostname)
   261  }
   262  
   263  // isSecureIndex returns false if the provided indexName is part of the list of insecure registries
   264  // Insecure registries accept HTTP and/or accept HTTPS with certificates from unknown CAs.
   265  //
   266  // The list of insecure registries can contain an element with CIDR notation to specify a whole subnet.
   267  // If the subnet contains one of the IPs of the registry specified by indexName, the latter is considered
   268  // insecure.
   269  //
   270  // indexName should be a URL.Host (`host:port` or `host`) where the `host` part can be either a domain name
   271  // or an IP address. If it is a domain name, then it will be resolved in order to check if the IP is contained
   272  // in a subnet. If the resolving is not successful, isSecureIndex will only try to match hostname to any element
   273  // of insecureRegistries.
   274  func isSecureIndex(config *serviceConfig, indexName string) bool {
   275  	// Check for configured index, first.  This is needed in case isSecureIndex
   276  	// is called from anything besides newIndexInfo, in order to honor per-index configurations.
   277  	if index, ok := config.IndexConfigs[indexName]; ok {
   278  		return index.Secure
   279  	}
   280  
   281  	return !isCIDRMatch(config.InsecureRegistryCIDRs, indexName)
   282  }
   283  
   284  // isCIDRMatch returns true if URLHost matches an element of cidrs. URLHost is a URL.Host (`host:port` or `host`)
   285  // where the `host` part can be either a domain name or an IP address. If it is a domain name, then it will be
   286  // resolved to IP addresses for matching. If resolution fails, false is returned.
   287  func isCIDRMatch(cidrs []*registrytypes.NetIPNet, URLHost string) bool {
   288  	host, _, err := net.SplitHostPort(URLHost)
   289  	if err != nil {
   290  		// Assume URLHost is of the form `host` without the port and go on.
   291  		host = URLHost
   292  	}
   293  
   294  	addrs, err := lookupIP(host)
   295  	if err != nil {
   296  		ip := net.ParseIP(host)
   297  		if ip != nil {
   298  			addrs = []net.IP{ip}
   299  		}
   300  
   301  		// if ip == nil, then `host` is neither an IP nor it could be looked up,
   302  		// either because the index is unreachable, or because the index is behind an HTTP proxy.
   303  		// So, len(addrs) == 0 and we're not aborting.
   304  	}
   305  
   306  	// Try CIDR notation only if addrs has any elements, i.e. if `host`'s IP could be determined.
   307  	for _, addr := range addrs {
   308  		for _, ipnet := range cidrs {
   309  			// check if the addr falls in the subnet
   310  			if (*net.IPNet)(ipnet).Contains(addr) {
   311  				return true
   312  			}
   313  		}
   314  	}
   315  
   316  	return false
   317  }
   318  
   319  // ValidateMirror validates an HTTP(S) registry mirror
   320  func ValidateMirror(val string) (string, error) {
   321  	uri, err := url.Parse(val)
   322  	if err != nil {
   323  		return "", fmt.Errorf("invalid mirror: %q is not a valid URI", val)
   324  	}
   325  	if uri.Scheme != "http" && uri.Scheme != "https" {
   326  		return "", fmt.Errorf("invalid mirror: unsupported scheme %q in %q", uri.Scheme, uri)
   327  	}
   328  	if (uri.Path != "" && uri.Path != "/") || uri.RawQuery != "" || uri.Fragment != "" {
   329  		return "", fmt.Errorf("invalid mirror: path, query, or fragment at end of the URI %q", uri)
   330  	}
   331  	if uri.User != nil {
   332  		// strip password from output
   333  		uri.User = url.UserPassword(uri.User.Username(), "xxxxx")
   334  		return "", fmt.Errorf("invalid mirror: username/password not allowed in URI %q", uri)
   335  	}
   336  	return strings.TrimSuffix(val, "/") + "/", nil
   337  }
   338  
   339  // ValidateIndexName validates an index name.
   340  func ValidateIndexName(val string) (string, error) {
   341  	// TODO: upstream this to check to reference package
   342  	if val == "index.docker.io" {
   343  		val = "docker.io"
   344  	}
   345  	if strings.HasPrefix(val, "-") || strings.HasSuffix(val, "-") {
   346  		return "", fmt.Errorf("invalid index name (%s). Cannot begin or end with a hyphen", val)
   347  	}
   348  	return val, nil
   349  }
   350  
   351  func validateNoScheme(reposName string) error {
   352  	if strings.Contains(reposName, "://") {
   353  		// It cannot contain a scheme!
   354  		return ErrInvalidRepositoryName
   355  	}
   356  	return nil
   357  }
   358  
   359  func validateHostPort(s string) error {
   360  	// Split host and port, and in case s can not be splitted, assume host only
   361  	host, port, err := net.SplitHostPort(s)
   362  	if err != nil {
   363  		host = s
   364  		port = ""
   365  	}
   366  	// If match against the `host:port` pattern fails,
   367  	// it might be `IPv6:port`, which will be captured by net.ParseIP(host)
   368  	if !validHostPortRegex.MatchString(s) && net.ParseIP(host) == nil {
   369  		return fmt.Errorf("invalid host %q", host)
   370  	}
   371  	if port != "" {
   372  		v, err := strconv.Atoi(port)
   373  		if err != nil {
   374  			return err
   375  		}
   376  		if v < 0 || v > 65535 {
   377  			return fmt.Errorf("invalid port %q", port)
   378  		}
   379  	}
   380  	return nil
   381  }
   382  
   383  // newIndexInfo returns IndexInfo configuration from indexName
   384  func newIndexInfo(config *serviceConfig, indexName string) (*registrytypes.IndexInfo, error) {
   385  	var err error
   386  	indexName, err = ValidateIndexName(indexName)
   387  	if err != nil {
   388  		return nil, err
   389  	}
   390  
   391  	// Return any configured index info, first.
   392  	if index, ok := config.IndexConfigs[indexName]; ok {
   393  		return index, nil
   394  	}
   395  
   396  	// Construct a non-configured index info.
   397  	index := &registrytypes.IndexInfo{
   398  		Name:     indexName,
   399  		Mirrors:  make([]string, 0),
   400  		Official: false,
   401  	}
   402  	index.Secure = isSecureIndex(config, indexName)
   403  	return index, nil
   404  }
   405  
   406  // GetAuthConfigKey special-cases using the full index address of the official
   407  // index as the AuthConfig key, and uses the (host)name[:port] for private indexes.
   408  func GetAuthConfigKey(index *registrytypes.IndexInfo) string {
   409  	if index.Official {
   410  		return IndexServer
   411  	}
   412  	return index.Name
   413  }
   414  
   415  // newRepositoryInfo validates and breaks down a repository name into a RepositoryInfo
   416  func newRepositoryInfo(config *serviceConfig, name reference.Named) (*RepositoryInfo, error) {
   417  	index, err := newIndexInfo(config, reference.Domain(name))
   418  	if err != nil {
   419  		return nil, err
   420  	}
   421  	official := !strings.ContainsRune(reference.FamiliarName(name), '/')
   422  
   423  	return &RepositoryInfo{
   424  		Name:     reference.TrimNamed(name),
   425  		Index:    index,
   426  		Official: official,
   427  	}, nil
   428  }
   429  
   430  // ParseRepositoryInfo performs the breakdown of a repository name into a RepositoryInfo, but
   431  // lacks registry configuration.
   432  func ParseRepositoryInfo(reposName reference.Named) (*RepositoryInfo, error) {
   433  	return newRepositoryInfo(emptyServiceConfig, reposName)
   434  }
   435  
   436  // ParseSearchIndexInfo will use repository name to get back an indexInfo.
   437  func ParseSearchIndexInfo(reposName string) (*registrytypes.IndexInfo, error) {
   438  	indexName, _ := splitReposSearchTerm(reposName)
   439  
   440  	indexInfo, err := newIndexInfo(emptyServiceConfig, indexName)
   441  	if err != nil {
   442  		return nil, err
   443  	}
   444  	return indexInfo, nil
   445  }