Browse code

registry: default --insecure-registry to localhost and 127.0.0.1

Signed-off-by: Johan Euphrosine <proppy@google.com>

Johan Euphrosine authored on 2014/11/01 05:00:49
Showing 2 changed files
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"encoding/json"
5 5
 	"fmt"
6 6
 	"io/ioutil"
7
+	"net"
7 8
 	"net/http"
8 9
 	"net/url"
9 10
 	"strings"
... ...
@@ -154,7 +155,16 @@ func IsSecure(hostname string, insecureRegistries []string) bool {
154 154
 	if hostname == IndexServerAddress() {
155 155
 		return true
156 156
 	}
157
-
157
+	if len(insecureRegistries) == 0 {
158
+		host, _, err := net.SplitHostPort(hostname)
159
+		if err != nil {
160
+			host = hostname
161
+		}
162
+		if host == "127.0.0.1" || host == "localhost" {
163
+			return false
164
+		}
165
+		return true
166
+	}
158 167
 	for _, h := range insecureRegistries {
159 168
 		if hostname == h {
160 169
 			return false
... ...
@@ -339,3 +339,24 @@ func TestIsSecure(t *testing.T) {
339 339
 		}
340 340
 	}
341 341
 }
342
+
343
+func TestIsSecure(t *testing.T) {
344
+	tests := []struct {
345
+		addr               string
346
+		insecureRegistries []string
347
+		expected           bool
348
+	}{
349
+		{"localhost", []string{}, false},
350
+		{"localhost:5000", []string{}, false},
351
+		{"127.0.0.1", []string{}, false},
352
+		{"localhost", []string{"example.com"}, true},
353
+		{"127.0.0.1", []string{"example.com"}, true},
354
+		{"example.com", []string{}, true},
355
+		{"example.com", []string{"example.com"}, false},
356
+	}
357
+	for _, tt := range tests {
358
+		if sec := IsSecure(tt.addr, tt.insecureRegistries); sec != tt.expected {
359
+			t.Errorf("IsSecure failed for %q %v, expected %v got %v", tt.addr, tt.insecureRegistries, tt.expected, sec)
360
+		}
361
+	}
362
+}