Browse code

Refactor IsSecure change

Fix issue with restoring the tag store and setting static configuration
from the daemon. i.e. the field on the TagStore struct must be made
internal or the json.Unmarshal in restore will overwrite the insecure
registries to be an empty struct.

Signed-off-by: Michael Crosby <michael@docker.com>

Conflicts:
graph/pull.go
graph/push.go
graph/tags.go

Michael Crosby authored on 2014/08/20 03:54:42
Showing 4 changed files
... ...
@@ -113,7 +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)
116
+	secure := registry.IsSecure(hostname, s.insecureRegistries)
117 117
 
118 118
 	endpoint, err := registry.NewEndpoint(hostname, secure)
119 119
 	if err != nil {
... ...
@@ -214,7 +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)
217
+	secure := registry.IsSecure(hostname, s.insecureRegistries)
218 218
 
219 219
 	endpoint, err := registry.NewEndpoint(hostname, secure)
220 220
 	if err != nil {
... ...
@@ -26,7 +26,7 @@ type TagStore struct {
26 26
 	path               string
27 27
 	graph              *Graph
28 28
 	mirrors            []string
29
-	InsecureRegistries []string
29
+	insecureRegistries []string
30 30
 	Repositories       map[string]Repository
31 31
 	sync.Mutex
32 32
 	// FIXME: move push/pull-related fields
... ...
@@ -60,11 +60,12 @@ func NewTagStore(path string, graph *Graph, mirrors []string, insecureRegistries
60 60
 	if err != nil {
61 61
 		return nil, err
62 62
 	}
63
+
63 64
 	store := &TagStore{
64 65
 		path:               abspath,
65 66
 		graph:              graph,
66 67
 		mirrors:            mirrors,
67
-		InsecureRegistries: insecureRegistries,
68
+		insecureRegistries: insecureRegistries,
68 69
 		Repositories:       make(map[string]Repository),
69 70
 		pullingPool:        make(map[string]chan struct{}),
70 71
 		pushingPool:        make(map[string]chan struct{}),
... ...
@@ -215,51 +215,45 @@ func ResolveRepositoryName(reposName string) (string, string, error) {
215 215
 
216 216
 // this method expands the registry name as used in the prefix of a repo
217 217
 // to a full url. if it already is a url, there will be no change.
218
-func ExpandAndVerifyRegistryUrl(hostname string, secure bool) (endpoint string, err error) {
219
-	if strings.HasPrefix(hostname, "http:") || strings.HasPrefix(hostname, "https:") {
220
-		// if there is no slash after https:// (8 characters) then we have no path in the url
221
-		if strings.LastIndex(hostname, "/") < 9 {
222
-			// there is no path given. Expand with default path
223
-			hostname = hostname + "/v1/"
224
-		}
225
-		if _, err := pingRegistryEndpoint(hostname); err != nil {
226
-			return "", errors.New("Invalid Registry endpoint: " + err.Error())
227
-		}
218
+func ExpandAndVerifyRegistryUrl(hostname string, secure bool) (string, error) {
219
+	if hostname == IndexServerAddress() {
228 220
 		return hostname, nil
229 221
 	}
230 222
 
231
-	// use HTTPS if secure, otherwise use HTTP
223
+	endpoint := fmt.Sprintf("http://%s/v1/", hostname)
224
+
232 225
 	if secure {
233 226
 		endpoint = fmt.Sprintf("https://%s/v1/", hostname)
234
-	} else {
235
-		endpoint = fmt.Sprintf("http://%s/v1/", hostname)
236 227
 	}
237
-	_, err = pingRegistryEndpoint(endpoint)
238
-	if err != nil {
228
+
229
+	if _, oerr := pingRegistryEndpoint(endpoint); oerr != nil {
239 230
 		//TODO: triggering highland build can be done there without "failing"
240
-		err = fmt.Errorf("Invalid registry endpoint '%s': %s ", endpoint, err)
231
+		err := fmt.Errorf("Invalid registry endpoint '%s': %s ", endpoint, oerr)
232
+
241 233
 		if secure {
242
-			err = fmt.Errorf("%s. If this private registry supports only HTTP, please add `--insecure-registry %s` to the daemon's arguments.", err, hostname)
234
+			err = fmt.Errorf("%s. If this private registry supports only HTTP, please add `--insecure-registry %s` to the daemon's arguments.", oerr, hostname)
243 235
 		}
236
+
244 237
 		return "", err
245 238
 	}
239
+
246 240
 	return endpoint, nil
247 241
 }
248 242
 
249 243
 // this method verifies if the provided hostname is part of the list of
250 244
 // insecure registries and returns false if HTTP should be used
251
-func IsSecure(hostname string, insecureRegistries []string) (secure bool) {
252
-	secure = true
245
+func IsSecure(hostname string, insecureRegistries []string) bool {
246
+	if hostname == IndexServerAddress() {
247
+		return true
248
+	}
249
+
253 250
 	for _, h := range insecureRegistries {
254 251
 		if hostname == h {
255
-			secure = false
256
-			break
252
+			return false
257 253
 		}
258 254
 	}
259
-	if hostname == IndexServerAddress() {
260
-		secure = true
261
-	}
262
-	return
255
+
256
+	return true
263 257
 }
264 258
 
265 259
 func trustedLocation(req *http.Request) bool {