2014-10-06 21:54:52 -04:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2017-02-05 02:58:12 -05:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2015-01-07 18:42:01 -05:00
|
|
|
"strings"
|
2014-10-06 21:54:52 -04:00
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-06 14:18:12 -04:00
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2014-10-06 21:54:52 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2017-01-11 16:54:52 -05:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-06-21 16:42:47 -04:00
|
|
|
"github.com/spf13/pflag"
|
2014-10-06 21:54:52 -04:00
|
|
|
)
|
|
|
|
|
2016-03-08 16:03:37 -05:00
|
|
|
// ServiceOptions holds command line options.
|
|
|
|
type ServiceOptions struct {
|
2017-05-09 17:00:31 -04:00
|
|
|
AllowNondistributableArtifacts []string `json:"allow-nondistributable-artifacts,omitempty"`
|
|
|
|
Mirrors []string `json:"registry-mirrors,omitempty"`
|
|
|
|
InsecureRegistries []string `json:"insecure-registries,omitempty"`
|
2016-03-08 16:03:37 -05:00
|
|
|
|
|
|
|
// V2Only controls access to legacy registries. If it is set to true via the
|
|
|
|
// command line flag the daemon will not attempt to contact v1 legacy registries
|
|
|
|
V2Only bool `json:"disable-legacy-registry,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// serviceConfig holds daemon configuration for the registry service.
|
|
|
|
type serviceConfig struct {
|
|
|
|
registrytypes.ServiceConfig
|
|
|
|
V2Only bool
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:53:25 -05:00
|
|
|
var (
|
2015-08-04 19:30:00 -04:00
|
|
|
// DefaultNamespace is the default namespace
|
|
|
|
DefaultNamespace = "docker.io"
|
|
|
|
// DefaultRegistryVersionHeader is the name of the default HTTP header
|
|
|
|
// that carries Registry version info
|
|
|
|
DefaultRegistryVersionHeader = "Docker-Distribution-Api-Version"
|
|
|
|
|
2016-11-05 18:53:11 -04:00
|
|
|
// IndexHostname is the index hostname
|
|
|
|
IndexHostname = "index.docker.io"
|
|
|
|
// IndexServer is used for user auth and image search
|
|
|
|
IndexServer = "https://" + IndexHostname + "/v1/"
|
2015-08-04 19:30:00 -04:00
|
|
|
// IndexName is the name of the index
|
|
|
|
IndexName = "docker.io"
|
|
|
|
|
|
|
|
// NotaryServer is the endpoint serving the Notary trust server
|
|
|
|
NotaryServer = "https://notary.docker.io"
|
|
|
|
|
2016-03-10 15:26:32 -05:00
|
|
|
// DefaultV2Registry is the URI of the default v2 registry
|
|
|
|
DefaultV2Registry = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "registry-1.docker.io",
|
|
|
|
}
|
2015-08-04 19:30:00 -04:00
|
|
|
)
|
|
|
|
|
2015-01-07 18:42:01 -05:00
|
|
|
var (
|
2015-07-21 15:40:36 -04:00
|
|
|
// ErrInvalidRepositoryName is an error returned if the repository name did
|
|
|
|
// not have the correct form
|
2015-01-07 18:42:01 -05:00
|
|
|
ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")")
|
2015-07-21 15:40:36 -04:00
|
|
|
|
2016-03-08 16:03:37 -05:00
|
|
|
emptyServiceConfig = newServiceConfig(ServiceOptions{})
|
2015-01-07 18:42:01 -05:00
|
|
|
)
|
|
|
|
|
2017-02-05 02:58:12 -05:00
|
|
|
var (
|
|
|
|
validHostPortRegex = regexp.MustCompile(`^` + reference.DomainRegexp.String() + `$`)
|
|
|
|
)
|
|
|
|
|
2016-03-01 02:07:41 -05:00
|
|
|
// for mocking in unit tests
|
|
|
|
var lookupIP = net.LookupIP
|
|
|
|
|
2016-03-08 16:03:37 -05:00
|
|
|
// InstallCliFlags adds command-line options to the top-level flag parser for
|
2014-10-06 21:54:52 -04:00
|
|
|
// the current process.
|
2016-06-21 16:42:47 -04:00
|
|
|
func (options *ServiceOptions) InstallCliFlags(flags *pflag.FlagSet) {
|
2017-05-09 17:00:31 -04:00
|
|
|
ana := opts.NewNamedListOptsRef("allow-nondistributable-artifacts", &options.AllowNondistributableArtifacts, ValidateIndexName)
|
2016-03-08 16:03:37 -05:00
|
|
|
mirrors := opts.NewNamedListOptsRef("registry-mirrors", &options.Mirrors, ValidateMirror)
|
|
|
|
insecureRegistries := opts.NewNamedListOptsRef("insecure-registries", &options.InsecureRegistries, ValidateIndexName)
|
|
|
|
|
2017-05-09 17:00:31 -04:00
|
|
|
flags.Var(ana, "allow-nondistributable-artifacts", "Allow push of nondistributable artifacts to registry")
|
2016-06-21 16:42:47 -04:00
|
|
|
flags.Var(mirrors, "registry-mirror", "Preferred Docker registry mirror")
|
|
|
|
flags.Var(insecureRegistries, "insecure-registry", "Enable insecure registry communication")
|
|
|
|
|
|
|
|
options.installCliPlatformFlags(flags)
|
2016-03-08 16:03:37 -05:00
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
|
2016-03-08 16:03:37 -05:00
|
|
|
// newServiceConfig returns a new instance of ServiceConfig
|
|
|
|
func newServiceConfig(options ServiceOptions) *serviceConfig {
|
|
|
|
config := &serviceConfig{
|
|
|
|
ServiceConfig: registrytypes.ServiceConfig{
|
|
|
|
InsecureRegistryCIDRs: make([]*registrytypes.NetIPNet, 0),
|
|
|
|
IndexConfigs: make(map[string]*registrytypes.IndexInfo, 0),
|
|
|
|
// Hack: Bypass setting the mirrors to IndexConfigs since they are going away
|
|
|
|
// and Mirrors are only for the official registry anyways.
|
|
|
|
},
|
|
|
|
V2Only: options.V2Only,
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
2016-04-24 22:51:28 -04:00
|
|
|
|
2017-05-09 17:00:31 -04:00
|
|
|
config.LoadAllowNondistributableArtifacts(options.AllowNondistributableArtifacts)
|
2016-12-21 05:31:05 -05:00
|
|
|
config.LoadMirrors(options.Mirrors)
|
2016-04-24 22:51:28 -04:00
|
|
|
config.LoadInsecureRegistries(options.InsecureRegistries)
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:00:31 -04:00
|
|
|
// LoadAllowNondistributableArtifacts loads allow-nondistributable-artifacts registries into config.
|
|
|
|
func (config *serviceConfig) LoadAllowNondistributableArtifacts(registries []string) error {
|
|
|
|
cidrs := map[string]*registrytypes.NetIPNet{}
|
|
|
|
hostnames := map[string]bool{}
|
|
|
|
|
|
|
|
for _, r := range registries {
|
|
|
|
if _, err := ValidateIndexName(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if validateNoScheme(r) != nil {
|
|
|
|
return fmt.Errorf("allow-nondistributable-artifacts registry %s should not contain '://'", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ipnet, err := net.ParseCIDR(r); err == nil {
|
|
|
|
// Valid CIDR.
|
|
|
|
cidrs[ipnet.String()] = (*registrytypes.NetIPNet)(ipnet)
|
|
|
|
} else if err := validateHostPort(r); err == nil {
|
|
|
|
// Must be `host:port` if not CIDR.
|
|
|
|
hostnames[r] = true
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("allow-nondistributable-artifacts registry %s is not valid: %v", r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config.AllowNondistributableArtifactsCIDRs = make([]*(registrytypes.NetIPNet), 0)
|
|
|
|
for _, c := range cidrs {
|
|
|
|
config.AllowNondistributableArtifactsCIDRs = append(config.AllowNondistributableArtifactsCIDRs, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.AllowNondistributableArtifactsHostnames = make([]string, 0)
|
|
|
|
for h := range hostnames {
|
|
|
|
config.AllowNondistributableArtifactsHostnames = append(config.AllowNondistributableArtifactsHostnames, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-21 05:31:05 -05:00
|
|
|
// LoadMirrors loads mirrors to config, after removing duplicates.
|
|
|
|
// Returns an error if mirrors contains an invalid mirror.
|
|
|
|
func (config *serviceConfig) LoadMirrors(mirrors []string) error {
|
|
|
|
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.
|
|
|
|
config.IndexConfigs[IndexName] = ®istrytypes.IndexInfo{
|
|
|
|
Name: IndexName,
|
|
|
|
Mirrors: config.Mirrors,
|
|
|
|
Secure: true,
|
|
|
|
Official: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-24 22:51:28 -04:00
|
|
|
// LoadInsecureRegistries loads insecure registries to config
|
|
|
|
func (config *serviceConfig) LoadInsecureRegistries(registries []string) error {
|
|
|
|
// Localhost is by default considered as an insecure registry
|
|
|
|
// This is a stop-gap for people who are running a private registry on localhost (especially on Boot2docker).
|
|
|
|
//
|
|
|
|
// TODO: should we deprecate this once it is easier for people to set up a TLS registry or change
|
|
|
|
// daemon flags on boot2docker?
|
|
|
|
registries = append(registries, "127.0.0.0/8")
|
|
|
|
|
|
|
|
// Store original InsecureRegistryCIDRs and IndexConfigs
|
|
|
|
// Clean InsecureRegistryCIDRs and IndexConfigs in config, as passed registries has all insecure registry info.
|
|
|
|
originalCIDRs := config.ServiceConfig.InsecureRegistryCIDRs
|
|
|
|
originalIndexInfos := config.ServiceConfig.IndexConfigs
|
|
|
|
|
|
|
|
config.ServiceConfig.InsecureRegistryCIDRs = make([]*registrytypes.NetIPNet, 0)
|
|
|
|
config.ServiceConfig.IndexConfigs = make(map[string]*registrytypes.IndexInfo, 0)
|
|
|
|
|
|
|
|
skip:
|
|
|
|
for _, r := range registries {
|
|
|
|
// validate insecure registry
|
|
|
|
if _, err := ValidateIndexName(r); err != nil {
|
|
|
|
// before returning err, roll back to original data
|
|
|
|
config.ServiceConfig.InsecureRegistryCIDRs = originalCIDRs
|
|
|
|
config.ServiceConfig.IndexConfigs = originalIndexInfos
|
|
|
|
return err
|
|
|
|
}
|
2017-01-08 22:30:58 -05:00
|
|
|
if strings.HasPrefix(strings.ToLower(r), "http://") {
|
|
|
|
logrus.Warnf("insecure registry %s should not contain 'http://' and 'http://' has been removed from the insecure registry config", r)
|
|
|
|
r = r[7:]
|
|
|
|
} else if strings.HasPrefix(strings.ToLower(r), "https://") {
|
|
|
|
logrus.Warnf("insecure registry %s should not contain 'https://' and 'https://' has been removed from the insecure registry config", r)
|
|
|
|
r = r[8:]
|
|
|
|
} else if validateNoScheme(r) != nil {
|
|
|
|
// Insecure registry should not contain '://'
|
|
|
|
// before returning err, roll back to original data
|
|
|
|
config.ServiceConfig.InsecureRegistryCIDRs = originalCIDRs
|
|
|
|
config.ServiceConfig.IndexConfigs = originalIndexInfos
|
|
|
|
return fmt.Errorf("insecure registry %s should not contain '://'", r)
|
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
// Check if CIDR was passed to --insecure-registry
|
|
|
|
_, ipnet, err := net.ParseCIDR(r)
|
|
|
|
if err == nil {
|
2016-04-24 22:51:28 -04:00
|
|
|
// Valid CIDR. If ipnet is already in config.InsecureRegistryCIDRs, skip.
|
|
|
|
data := (*registrytypes.NetIPNet)(ipnet)
|
|
|
|
for _, value := range config.InsecureRegistryCIDRs {
|
|
|
|
if value.IP.String() == data.IP.String() && value.Mask.String() == data.Mask.String() {
|
|
|
|
continue skip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ipnet is not found, add it in config.InsecureRegistryCIDRs
|
|
|
|
config.InsecureRegistryCIDRs = append(config.InsecureRegistryCIDRs, data)
|
|
|
|
|
2014-10-06 21:54:52 -04:00
|
|
|
} else {
|
2017-02-05 02:58:12 -05:00
|
|
|
if err := validateHostPort(r); err != nil {
|
|
|
|
config.ServiceConfig.InsecureRegistryCIDRs = originalCIDRs
|
|
|
|
config.ServiceConfig.IndexConfigs = originalIndexInfos
|
|
|
|
return fmt.Errorf("insecure registry %s is not valid: %v", r, err)
|
|
|
|
|
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
// Assume `host:port` if not CIDR.
|
2015-12-11 21:14:52 -05:00
|
|
|
config.IndexConfigs[r] = ®istrytypes.IndexInfo{
|
2014-10-06 21:54:52 -04:00
|
|
|
Name: r,
|
|
|
|
Mirrors: make([]string, 0),
|
|
|
|
Secure: false,
|
|
|
|
Official: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure public registry.
|
2015-12-11 21:14:52 -05:00
|
|
|
config.IndexConfigs[IndexName] = ®istrytypes.IndexInfo{
|
2015-07-21 15:40:36 -04:00
|
|
|
Name: IndexName,
|
2015-02-12 13:23:22 -05:00
|
|
|
Mirrors: config.Mirrors,
|
2014-10-06 21:54:52 -04:00
|
|
|
Secure: true,
|
|
|
|
Official: true,
|
|
|
|
}
|
|
|
|
|
2016-04-24 22:51:28 -04:00
|
|
|
return nil
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
2015-01-07 18:42:01 -05:00
|
|
|
|
2017-05-21 19:24:07 -04:00
|
|
|
// allowNondistributableArtifacts returns true if the provided hostname is part of the list of registries
|
2017-05-09 17:00:31 -04: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.
|
|
|
|
func allowNondistributableArtifacts(config *serviceConfig, hostname string) bool {
|
|
|
|
for _, h := range config.AllowNondistributableArtifactsHostnames {
|
|
|
|
if h == hostname {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return isCIDRMatch(config.AllowNondistributableArtifactsCIDRs, hostname)
|
|
|
|
}
|
|
|
|
|
2015-01-07 18:42:01 -05: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.
|
2016-03-08 16:03:37 -05:00
|
|
|
func isSecureIndex(config *serviceConfig, indexName string) bool {
|
2015-01-07 18:42:01 -05:00
|
|
|
// Check for configured index, first. This is needed in case isSecureIndex
|
2015-12-11 21:14:52 -05:00
|
|
|
// is called from anything besides newIndexInfo, in order to honor per-index configurations.
|
2015-01-07 18:42:01 -05:00
|
|
|
if index, ok := config.IndexConfigs[indexName]; ok {
|
|
|
|
return index.Secure
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:00:31 -04: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.
|
|
|
|
func isCIDRMatch(cidrs []*registrytypes.NetIPNet, URLHost string) bool {
|
|
|
|
host, _, err := net.SplitHostPort(URLHost)
|
2015-01-07 18:42:01 -05:00
|
|
|
if err != nil {
|
2017-05-09 17:00:31 -04:00
|
|
|
// Assume URLHost is of the form `host` without the port and go on.
|
|
|
|
host = URLHost
|
2015-01-07 18:42:01 -05: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 17:00:31 -04:00
|
|
|
for _, ipnet := range cidrs {
|
2015-01-07 18:42:01 -05:00
|
|
|
// check if the addr falls in the subnet
|
|
|
|
if (*net.IPNet)(ipnet).Contains(addr) {
|
2017-05-09 17:00:31 -04:00
|
|
|
return true
|
2015-01-07 18:42:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 17:00:31 -04:00
|
|
|
return false
|
2015-01-07 18:42:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateMirror validates an HTTP(S) registry mirror
|
|
|
|
func ValidateMirror(val string) (string, error) {
|
|
|
|
uri, err := url.Parse(val)
|
|
|
|
if err != nil {
|
2016-12-21 05:31:05 -05:00
|
|
|
return "", fmt.Errorf("invalid mirror: %q is not a valid URI", val)
|
2015-01-07 18:42:01 -05:00
|
|
|
}
|
|
|
|
if uri.Scheme != "http" && uri.Scheme != "https" {
|
2016-12-21 05:31:05 -05:00
|
|
|
return "", fmt.Errorf("invalid mirror: unsupported scheme %q in %q", uri.Scheme, uri)
|
2015-01-07 18:42:01 -05:00
|
|
|
}
|
2016-12-21 05:31:05 -05:00
|
|
|
if (uri.Path != "" && uri.Path != "/") || uri.RawQuery != "" || uri.Fragment != "" {
|
|
|
|
return "", fmt.Errorf("invalid mirror: path, query, or fragment at end of the URI %q", uri)
|
2015-01-07 18:42:01 -05:00
|
|
|
}
|
2016-12-21 05:31:05 -05:00
|
|
|
if uri.User != nil {
|
|
|
|
// strip password from output
|
|
|
|
uri.User = url.UserPassword(uri.User.Username(), "xxxxx")
|
|
|
|
return "", fmt.Errorf("invalid mirror: username/password not allowed in URI %q", uri)
|
|
|
|
}
|
|
|
|
return strings.TrimSuffix(val, "/") + "/", nil
|
2015-01-07 18:42:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateIndexName validates an index name.
|
|
|
|
func ValidateIndexName(val string) (string, error) {
|
2017-01-25 19:54:18 -05:00
|
|
|
// TODO: upstream this to check to reference package
|
|
|
|
if val == "index.docker.io" {
|
|
|
|
val = "docker.io"
|
2015-01-07 18:42:01 -05:00
|
|
|
}
|
2015-05-13 02:23:13 -04:00
|
|
|
if strings.HasPrefix(val, "-") || strings.HasSuffix(val, "-") {
|
2017-08-17 15:16:30 -04:00
|
|
|
return "", fmt.Errorf("invalid index name (%s). Cannot begin or end with a hyphen", val)
|
2015-05-13 02:23:13 -04:00
|
|
|
}
|
2015-01-07 18:42:01 -05:00
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2016-03-29 02:36:38 -04:00
|
|
|
func validateNoScheme(reposName string) error {
|
2015-01-07 18:42:01 -05:00
|
|
|
if strings.Contains(reposName, "://") {
|
|
|
|
// It cannot contain a scheme!
|
|
|
|
return ErrInvalidRepositoryName
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-05 02:58:12 -05: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 {
|
|
|
|
return fmt.Errorf("invalid host %q", host)
|
|
|
|
}
|
|
|
|
if port != "" {
|
|
|
|
v, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if v < 0 || v > 65535 {
|
|
|
|
return fmt.Errorf("invalid port %q", port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-11 21:14:52 -05:00
|
|
|
// newIndexInfo returns IndexInfo configuration from indexName
|
2016-03-08 16:03:37 -05:00
|
|
|
func newIndexInfo(config *serviceConfig, indexName string) (*registrytypes.IndexInfo, error) {
|
2015-01-07 18:42:01 -05: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.
|
2015-12-11 21:14:52 -05:00
|
|
|
index := ®istrytypes.IndexInfo{
|
2015-01-07 18:42:01 -05:00
|
|
|
Name: indexName,
|
|
|
|
Mirrors: make([]string, 0),
|
|
|
|
Official: false,
|
|
|
|
}
|
2015-12-11 21:14:52 -05:00
|
|
|
index.Secure = isSecureIndex(config, indexName)
|
2015-01-07 18:42:01 -05:00
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-12-11 21:14:52 -05:00
|
|
|
func GetAuthConfigKey(index *registrytypes.IndexInfo) string {
|
2015-01-07 18:42:01 -05:00
|
|
|
if index.Official {
|
2015-07-21 15:40:36 -04:00
|
|
|
return IndexServer
|
2015-01-07 18:42:01 -05:00
|
|
|
}
|
|
|
|
return index.Name
|
|
|
|
}
|
|
|
|
|
2015-12-11 21:14:52 -05:00
|
|
|
// newRepositoryInfo validates and breaks down a repository name into a RepositoryInfo
|
2016-03-08 16:03:37 -05:00
|
|
|
func newRepositoryInfo(config *serviceConfig, name reference.Named) (*RepositoryInfo, error) {
|
2017-01-11 16:54:52 -05:00
|
|
|
index, err := newIndexInfo(config, reference.Domain(name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
official := !strings.ContainsRune(reference.FamiliarName(name), '/')
|
|
|
|
|
2016-11-15 18:06:48 -05:00
|
|
|
return &RepositoryInfo{
|
2017-01-25 19:54:18 -05:00
|
|
|
Name: reference.TrimNamed(name),
|
2016-11-15 18:06:48 -05:00
|
|
|
Index: index,
|
|
|
|
Official: official,
|
|
|
|
}, nil
|
2015-01-07 18:42:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRepositoryInfo performs the breakdown of a repository name into a RepositoryInfo, but
|
|
|
|
// lacks registry configuration.
|
2015-11-18 17:20:54 -05:00
|
|
|
func ParseRepositoryInfo(reposName reference.Named) (*RepositoryInfo, error) {
|
2015-12-11 21:14:52 -05:00
|
|
|
return newRepositoryInfo(emptyServiceConfig, reposName)
|
2015-09-22 07:44:40 -04:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
// ParseSearchIndexInfo will use repository name to get back an indexInfo.
|
2015-12-11 21:14:52 -05:00
|
|
|
func ParseSearchIndexInfo(reposName string) (*registrytypes.IndexInfo, error) {
|
2015-11-18 17:20:54 -05:00
|
|
|
indexName, _ := splitReposSearchTerm(reposName)
|
2015-09-22 07:44:40 -04:00
|
|
|
|
2015-12-11 21:14:52 -05:00
|
|
|
indexInfo, err := newIndexInfo(emptyServiceConfig, indexName)
|
2015-09-22 07:44:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return indexInfo, nil
|
2015-01-07 18:42:01 -05:00
|
|
|
}
|