Browse code

registry: parse INDEXSERVERADDRESS into a URL for easier check in isSecure

Signed-off-by: Tibor Vass <teabee89@gmail.com>

Tibor Vass authored on 2014/11/13 23:56:36
Showing 4 changed files
... ...
@@ -7,6 +7,7 @@ import (
7 7
 	"fmt"
8 8
 	"io/ioutil"
9 9
 	"net/http"
10
+	"net/url"
10 11
 	"os"
11 12
 	"path"
12 13
 	"strings"
... ...
@@ -27,8 +28,17 @@ const (
27 27
 
28 28
 var (
29 29
 	ErrConfigFileMissing = errors.New("The Auth config file is missing")
30
+	IndexServerURL       *url.URL
30 31
 )
31 32
 
33
+func init() {
34
+	url, err := url.Parse(INDEXSERVER)
35
+	if err != nil {
36
+		panic(err)
37
+	}
38
+	IndexServerURL = url
39
+}
40
+
32 41
 type AuthConfig struct {
33 42
 	Username      string `json:"username,omitempty"`
34 43
 	Password      string `json:"password,omitempty"`
... ...
@@ -35,21 +35,18 @@ func scanForAPIVersion(hostname string) (string, APIVersion) {
35 35
 }
36 36
 
37 37
 func NewEndpoint(hostname string, insecureRegistries []string) (*Endpoint, error) {
38
-	endpoint, err := newEndpoint(hostname)
38
+	endpoint, err := newEndpoint(hostname, insecureRegistries)
39 39
 	if err != nil {
40 40
 		return nil, err
41 41
 	}
42 42
 
43
-	secure := isSecure(endpoint.URL.Host, insecureRegistries)
44
-	endpoint.secure = secure
45
-
46 43
 	// Try HTTPS ping to registry
47 44
 	endpoint.URL.Scheme = "https"
48 45
 	if _, err := endpoint.Ping(); err != nil {
49 46
 
50 47
 		//TODO: triggering highland build can be done there without "failing"
51 48
 
52
-		if secure {
49
+		if endpoint.secure {
53 50
 			// If registry is secure and HTTPS failed, show user the error and tell them about `--insecure-registry`
54 51
 			// in case that's what they need. DO NOT accept unknown CA certificates, and DO NOT fallback to HTTP.
55 52
 			return nil, fmt.Errorf("Invalid registry endpoint %s: %v. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry %s` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/%s/ca.crt", endpoint, err, endpoint.URL.Host, endpoint.URL.Host)
... ...
@@ -68,9 +65,9 @@ func NewEndpoint(hostname string, insecureRegistries []string) (*Endpoint, error
68 68
 
69 69
 	return endpoint, nil
70 70
 }
71
-func newEndpoint(hostname string) (*Endpoint, error) {
71
+func newEndpoint(hostname string, insecureRegistries []string) (*Endpoint, error) {
72 72
 	var (
73
-		endpoint        = Endpoint{secure: true}
73
+		endpoint        = Endpoint{}
74 74
 		trimmedHostname string
75 75
 		err             error
76 76
 	)
... ...
@@ -82,6 +79,7 @@ func newEndpoint(hostname string) (*Endpoint, error) {
82 82
 	if err != nil {
83 83
 		return nil, err
84 84
 	}
85
+	endpoint.secure = isSecure(endpoint.URL.Host, insecureRegistries)
85 86
 	return &endpoint, nil
86 87
 }
87 88
 
... ...
@@ -155,7 +153,7 @@ func (e Endpoint) Ping() (RegistryInfo, error) {
155 155
 // isSecure returns false if the provided hostname is part of the list of insecure registries.
156 156
 // Insecure registries accept HTTP and/or accept HTTPS with certificates from unknown CAs.
157 157
 func isSecure(hostname string, insecureRegistries []string) bool {
158
-	if hostname == IndexServerAddress() {
158
+	if hostname == IndexServerURL.Host {
159 159
 		return true
160 160
 	}
161 161
 
... ...
@@ -12,7 +12,7 @@ func TestEndpointParse(t *testing.T) {
12 12
 		{"0.0.0.0:5000", "https://0.0.0.0:5000/v1/"},
13 13
 	}
14 14
 	for _, td := range testData {
15
-		e, err := newEndpoint(td.str)
15
+		e, err := newEndpoint(td.str, insecureRegistries)
16 16
 		if err != nil {
17 17
 			t.Errorf("%q: %s", td.str, err)
18 18
 		}
... ...
@@ -326,6 +326,7 @@ func TestIsSecure(t *testing.T) {
326 326
 		insecureRegistries []string
327 327
 		expected           bool
328 328
 	}{
329
+		{IndexServerURL.Host, nil, true},
329 330
 		{"example.com", []string{}, true},
330 331
 		{"example.com", []string{"example.com"}, false},
331 332
 		{"localhost", []string{"localhost:5000"}, false},