Browse code

registry: refactor registry.IsSecure calls into registry.NewEndpoint

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

Tibor Vass authored on 2014/11/12 07:37:44
Showing 6 changed files
... ...
@@ -113,9 +113,7 @@ func (s *TagStore) CmdPull(job *engine.Job) engine.Status {
113 113
 		return job.Error(err)
114 114
 	}
115 115
 
116
-	secure := registry.IsSecure(hostname, s.insecureRegistries)
117
-
118
-	endpoint, err := registry.NewEndpoint(hostname, secure)
116
+	endpoint, err := registry.NewEndpoint(hostname, s.insecureRegistries)
119 117
 	if err != nil {
120 118
 		return job.Error(err)
121 119
 	}
... ...
@@ -214,9 +214,7 @@ func (s *TagStore) CmdPush(job *engine.Job) engine.Status {
214 214
 		return job.Error(err)
215 215
 	}
216 216
 
217
-	secure := registry.IsSecure(hostname, s.insecureRegistries)
218
-
219
-	endpoint, err := registry.NewEndpoint(hostname, secure)
217
+	endpoint, err := registry.NewEndpoint(hostname, s.insecureRegistries)
220 218
 	if err != nil {
221 219
 		return job.Error(err)
222 220
 	}
... ...
@@ -34,12 +34,15 @@ func scanForAPIVersion(hostname string) (string, APIVersion) {
34 34
 	return hostname, DefaultAPIVersion
35 35
 }
36 36
 
37
-func NewEndpoint(hostname string, secure bool) (*Endpoint, error) {
38
-	endpoint, err := newEndpoint(hostname, secure)
37
+func NewEndpoint(hostname string, insecureRegistries []string) (*Endpoint, error) {
38
+	endpoint, err := newEndpoint(hostname)
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
+
43 46
 	// Try HTTPS ping to registry
44 47
 	endpoint.URL.Scheme = "https"
45 48
 	if _, err := endpoint.Ping(); err != nil {
... ...
@@ -65,9 +68,9 @@ func NewEndpoint(hostname string, secure bool) (*Endpoint, error) {
65 65
 
66 66
 	return endpoint, nil
67 67
 }
68
-func newEndpoint(hostname string, secure bool) (*Endpoint, error) {
68
+func newEndpoint(hostname string) (*Endpoint, error) {
69 69
 	var (
70
-		endpoint        = Endpoint{secure: secure}
70
+		endpoint        = Endpoint{secure: true}
71 71
 		trimmedHostname string
72 72
 		err             error
73 73
 	)
... ...
@@ -149,10 +152,9 @@ func (e Endpoint) Ping() (RegistryInfo, error) {
149 149
 	return info, nil
150 150
 }
151 151
 
152
-// IsSecure returns false if the provided hostname is part of the list of insecure registries.
152
+// isSecure returns false if the provided hostname is part of the list of insecure registries.
153 153
 // Insecure registries accept HTTP and/or accept HTTPS with certificates from unknown CAs.
154
-func IsSecure(hostname string, insecureRegistries []string) bool {
155
-
154
+func isSecure(hostname string, insecureRegistries []string) bool {
156 155
 	if hostname == IndexServerAddress() {
157 156
 		return true
158 157
 	}
... ...
@@ -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, true)
15
+		e, err := newEndpoint(td.str)
16 16
 		if err != nil {
17 17
 			t.Errorf("%q: %s", td.str, err)
18 18
 		}
... ...
@@ -343,8 +343,8 @@ func TestIsSecure(t *testing.T) {
343 343
 		{"127.0.0.1:5000", []string{"example.com"}, false},
344 344
 	}
345 345
 	for _, tt := range tests {
346
-		if sec := IsSecure(tt.addr, tt.insecureRegistries); sec != tt.expected {
347
-			t.Errorf("IsSecure failed for %q %v, expected %v got %v", tt.addr, tt.insecureRegistries, tt.expected, sec)
346
+		if sec := isSecure(tt.addr, tt.insecureRegistries); sec != tt.expected {
347
+			t.Errorf("isSecure failed for %q %v, expected %v got %v", tt.addr, tt.insecureRegistries, tt.expected, sec)
348 348
 		}
349 349
 	}
350 350
 }
... ...
@@ -40,7 +40,7 @@ func (s *Service) Auth(job *engine.Job) engine.Status {
40 40
 	job.GetenvJson("authConfig", authConfig)
41 41
 
42 42
 	if addr := authConfig.ServerAddress; addr != "" && addr != IndexServerAddress() {
43
-		endpoint, err := NewEndpoint(addr, IsSecure(addr, s.insecureRegistries))
43
+		endpoint, err := NewEndpoint(addr, s.insecureRegistries)
44 44
 		if err != nil {
45 45
 			return job.Error(err)
46 46
 		}
... ...
@@ -92,9 +92,7 @@ func (s *Service) Search(job *engine.Job) engine.Status {
92 92
 		return job.Error(err)
93 93
 	}
94 94
 
95
-	secure := IsSecure(hostname, s.insecureRegistries)
96
-
97
-	endpoint, err := NewEndpoint(hostname, secure)
95
+	endpoint, err := NewEndpoint(hostname, s.insecureRegistries)
98 96
 	if err != nil {
99 97
 		return job.Error(err)
100 98
 	}