Browse code

Do not hardcode http as plugin URL scheme for secure connections.

Signed-off-by: David Calavera <david.calavera@gmail.com>

David Calavera authored on 2015/09/23 04:54:29
Showing 2 changed files
... ...
@@ -40,13 +40,19 @@ func NewClient(addr string, tlsConfig tlsconfig.Options) (*Client, error) {
40 40
 
41 41
 	protoAndAddr := strings.Split(addr, "://")
42 42
 	sockets.ConfigureTCPTransport(tr, protoAndAddr[0], protoAndAddr[1])
43
-	return &Client{&http.Client{Transport: tr}, protoAndAddr[1]}, nil
43
+
44
+	scheme := protoAndAddr[0]
45
+	if scheme != "https" {
46
+		scheme = "http"
47
+	}
48
+	return &Client{&http.Client{Transport: tr}, scheme, protoAndAddr[1]}, nil
44 49
 }
45 50
 
46 51
 // Client represents a plugin client.
47 52
 type Client struct {
48
-	http *http.Client // http client to use
49
-	addr string       // http address of the plugin
53
+	http   *http.Client // http client to use
54
+	scheme string       // scheme protocol of the plugin
55
+	addr   string       // http address of the plugin
50 56
 }
51 57
 
52 58
 // Call calls the specified method with the specified arguments for the plugin.
... ...
@@ -66,7 +72,7 @@ func (c *Client) callWithRetry(serviceMethod string, args interface{}, ret inter
66 66
 		return err
67 67
 	}
68 68
 	req.Header.Add("Accept", versionMimetype)
69
-	req.URL.Scheme = "http"
69
+	req.URL.Scheme = c.scheme
70 70
 	req.URL.Host = c.addr
71 71
 
72 72
 	var retries int
... ...
@@ -105,3 +105,19 @@ func TestAbortRetry(t *testing.T) {
105 105
 		}
106 106
 	}
107 107
 }
108
+
109
+func TestClientScheme(t *testing.T) {
110
+	cases := map[string]string{
111
+		"tcp://127.0.0.1:8080":          "http",
112
+		"unix:///usr/local/plugins/foo": "http",
113
+		"http://127.0.0.1:8080":         "http",
114
+		"https://127.0.0.1:8080":        "https",
115
+	}
116
+
117
+	for addr, scheme := range cases {
118
+		c, _ := NewClient(addr, tlsconfig.Options{InsecureSkipVerify: true})
119
+		if c.scheme != scheme {
120
+			t.Fatalf("URL scheme mismatch, expected %s, got %s", scheme, c.scheme)
121
+		}
122
+	}
123
+}