|
@@ -4,6 +4,8 @@ import (
|
|
|
"fmt"
|
|
|
"net"
|
|
|
"net/url"
|
|
|
+ "regexp"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
@@ -62,6 +64,10 @@ var (
|
|
|
emptyServiceConfig = newServiceConfig(ServiceOptions{})
|
|
|
)
|
|
|
|
|
|
+var (
|
|
|
+ validHostPortRegex = regexp.MustCompile(`^` + reference.DomainRegexp.String() + `$`)
|
|
|
+)
|
|
|
+
|
|
|
// for mocking in unit tests
|
|
|
var lookupIP = net.LookupIP
|
|
|
|
|
@@ -178,6 +184,12 @@ skip:
|
|
|
config.InsecureRegistryCIDRs = append(config.InsecureRegistryCIDRs, data)
|
|
|
|
|
|
} else {
|
|
|
+ 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)
|
|
|
+
|
|
|
+ }
|
|
|
// Assume `host:port` if not CIDR.
|
|
|
config.IndexConfigs[r] = ®istrytypes.IndexInfo{
|
|
|
Name: r,
|
|
@@ -288,6 +300,30 @@ func validateNoScheme(reposName string) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+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
|
|
|
+}
|
|
|
+
|
|
|
// newIndexInfo returns IndexInfo configuration from indexName
|
|
|
func newIndexInfo(config *serviceConfig, indexName string) (*registrytypes.IndexInfo, error) {
|
|
|
var err error
|