Add the possibility of specifying a subnet for --insecure-registry
Signed-off-by: Tibor Vass <teabee89@gmail.com>
This commit is contained in:
parent
f0920e61bf
commit
ae0ebb9d07
3 changed files with 85 additions and 20 deletions
|
@ -12,6 +12,9 @@ import (
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// for mocking in unit tests
|
||||||
|
var lookupIP = net.LookupIP
|
||||||
|
|
||||||
// scans string for api version in the URL path. returns the trimmed hostname, if version found, string and API version.
|
// scans string for api version in the URL path. returns the trimmed hostname, if version found, string and API version.
|
||||||
func scanForAPIVersion(hostname string) (string, APIVersion) {
|
func scanForAPIVersion(hostname string) (string, APIVersion) {
|
||||||
var (
|
var (
|
||||||
|
@ -79,7 +82,10 @@ func newEndpoint(hostname string, insecureRegistries []string) (*Endpoint, error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
endpoint.secure = isSecure(endpoint.URL.Host, insecureRegistries)
|
endpoint.secure, err = isSecure(endpoint.URL.Host, insecureRegistries)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &endpoint, nil
|
return &endpoint, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,30 +158,56 @@ func (e Endpoint) Ping() (RegistryInfo, error) {
|
||||||
|
|
||||||
// isSecure returns false if the provided hostname is part of the list of insecure registries.
|
// isSecure returns false if the provided hostname is part of the list of insecure registries.
|
||||||
// Insecure registries accept HTTP and/or accept HTTPS with certificates from unknown CAs.
|
// Insecure registries accept HTTP and/or accept HTTPS with certificates from unknown CAs.
|
||||||
func isSecure(hostname string, insecureRegistries []string) bool {
|
//
|
||||||
|
// 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 hostname, the latter is considered
|
||||||
|
// insecure.
|
||||||
|
//
|
||||||
|
// hostname should be a URL.Host (`host:port` or `host`)
|
||||||
|
func isSecure(hostname string, insecureRegistries []string) (bool, error) {
|
||||||
if hostname == IndexServerURL.Host {
|
if hostname == IndexServerURL.Host {
|
||||||
return true
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
host, _, err := net.SplitHostPort(hostname)
|
host, _, err := net.SplitHostPort(hostname)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// assume hostname is of the form `host` without the port and go on.
|
||||||
host = hostname
|
host = hostname
|
||||||
}
|
}
|
||||||
|
addrs, err := lookupIP(host)
|
||||||
if host == "127.0.0.1" || host == "localhost" {
|
if err != nil {
|
||||||
return false
|
ip := net.ParseIP(host)
|
||||||
|
if ip == nil {
|
||||||
|
// if resolving `host` fails, error out, since host is to be net.Dial-ed anyway
|
||||||
|
return true, fmt.Errorf("issecure: could not resolve %q: %v", host, err)
|
||||||
|
}
|
||||||
|
addrs = []net.IP{ip}
|
||||||
|
}
|
||||||
|
if len(addrs) == 0 {
|
||||||
|
return true, fmt.Errorf("issecure: could not resolve %q", host)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(insecureRegistries) == 0 {
|
for _, addr := range addrs {
|
||||||
return true
|
for _, r := range insecureRegistries {
|
||||||
}
|
// hostname matches insecure registry
|
||||||
|
if hostname == r {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
for _, h := range insecureRegistries {
|
// now assume a CIDR was passed to --insecure-registry
|
||||||
if hostname == h {
|
_, ipnet, err := net.ParseCIDR(r)
|
||||||
return false
|
if err != nil {
|
||||||
|
// if could not parse it as a CIDR, even after removing
|
||||||
|
// assume it's not a CIDR and go on with the next candidate
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the addr falls in the subnet
|
||||||
|
if ipnet.Contains(addr) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,11 @@ package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -80,6 +82,11 @@ var (
|
||||||
"latest": "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
"latest": "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
mockHosts = map[string][]net.IP{
|
||||||
|
"": {net.ParseIP("0.0.0.0")},
|
||||||
|
"localhost": {net.ParseIP("127.0.0.1"), net.ParseIP("::1")},
|
||||||
|
"example.com": {net.ParseIP("42.42.42.42")},
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -106,6 +113,25 @@ func init() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
insecureRegistries = []string{URL.Host}
|
insecureRegistries = []string{URL.Host}
|
||||||
|
|
||||||
|
// override net.LookupIP
|
||||||
|
lookupIP = func(host string) ([]net.IP, error) {
|
||||||
|
if host == "127.0.0.1" {
|
||||||
|
// I believe in future Go versions this will fail, so let's fix it later
|
||||||
|
return net.LookupIP(host)
|
||||||
|
}
|
||||||
|
for h, addrs := range mockHosts {
|
||||||
|
if host == h {
|
||||||
|
return addrs, nil
|
||||||
|
}
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if addr.String() == host {
|
||||||
|
return []net.IP{addr}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, errors.New("lookup: no such host")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlerAccessLog(handler http.Handler) http.Handler {
|
func handlerAccessLog(handler http.Handler) http.Handler {
|
||||||
|
|
|
@ -333,19 +333,26 @@ func TestIsSecure(t *testing.T) {
|
||||||
{"localhost:5000", []string{"localhost:5000"}, false},
|
{"localhost:5000", []string{"localhost:5000"}, false},
|
||||||
{"localhost", []string{"example.com"}, false},
|
{"localhost", []string{"example.com"}, false},
|
||||||
{"127.0.0.1:5000", []string{"127.0.0.1:5000"}, false},
|
{"127.0.0.1:5000", []string{"127.0.0.1:5000"}, false},
|
||||||
{"localhost", []string{}, false},
|
{"localhost", nil, false},
|
||||||
{"localhost:5000", []string{}, false},
|
{"localhost:5000", nil, false},
|
||||||
{"127.0.0.1", []string{}, false},
|
{"127.0.0.1", nil, false},
|
||||||
{"localhost", []string{"example.com"}, false},
|
{"localhost", []string{"example.com"}, false},
|
||||||
{"127.0.0.1", []string{"example.com"}, false},
|
{"127.0.0.1", []string{"example.com"}, false},
|
||||||
{"example.com", []string{}, true},
|
{"example.com", nil, true},
|
||||||
{"example.com", []string{"example.com"}, false},
|
{"example.com", []string{"example.com"}, false},
|
||||||
{"127.0.0.1", []string{"example.com"}, false},
|
{"127.0.0.1", []string{"example.com"}, false},
|
||||||
{"127.0.0.1:5000", []string{"example.com"}, false},
|
{"127.0.0.1:5000", []string{"example.com"}, false},
|
||||||
|
{"example.com:5000", []string{"42.42.0.0/16"}, false},
|
||||||
|
{"example.com", []string{"42.42.0.0/16"}, false},
|
||||||
|
{"example.com:5000", []string{"42.42.42.42/8"}, false},
|
||||||
|
{"127.0.0.1:5000", []string{"127.0.0.0/8"}, false},
|
||||||
|
{"42.42.42.42:5000", []string{"42.1.1.1/8"}, false},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
if sec := isSecure(tt.addr, tt.insecureRegistries); sec != tt.expected {
|
// TODO: remove this once we remove localhost insecure by default
|
||||||
t.Errorf("isSecure failed for %q %v, expected %v got %v", tt.addr, tt.insecureRegistries, tt.expected, sec)
|
insecureRegistries := append(tt.insecureRegistries, "127.0.0.0/8")
|
||||||
|
if sec, err := isSecure(tt.addr, insecureRegistries); err != nil || sec != tt.expected {
|
||||||
|
t.Fatalf("isSecure failed for %q %v, expected %v got %v. Error: %v", tt.addr, insecureRegistries, tt.expected, sec, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue