From d93b7c8cb185af5a8af8408fbfcc55a21e3d9e5a Mon Sep 17 00:00:00 2001 From: Karen Almog Date: Thu, 12 Nov 2020 15:23:54 +0100 Subject: [PATCH 1/2] pkg/types: Support Unix sockets in NewURLS Resolves #12450 This commits adds support to unix/unixs socket URLs, which currently fail with the message "URL address does not have the form "host:port". It also replaces the work started in #11747. --- client/pkg/types/urls.go | 21 +++++++++++++-------- pkg/flags/urls_test.go | 3 --- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/client/pkg/types/urls.go b/client/pkg/types/urls.go index 9e5d03ff6..49a38967e 100644 --- a/client/pkg/types/urls.go +++ b/client/pkg/types/urls.go @@ -36,20 +36,25 @@ func NewURLs(strs []string) (URLs, error) { if err != nil { return nil, err } - if u.Scheme != "http" && u.Scheme != "https" && u.Scheme != "unix" && u.Scheme != "unixs" { + + switch u.Scheme { + case "http", "https": + if _, _, err := net.SplitHostPort(u.Host); err != nil { + return nil, fmt.Errorf(`URL address does not have the form "host:port": %s`, in) + } + + if u.Path != "" { + return nil, fmt.Errorf("URL must not contain a path: %s", in) + } + case "unix", "unixs": + break + default: return nil, fmt.Errorf("URL scheme must be http, https, unix, or unixs: %s", in) } - if _, _, err := net.SplitHostPort(u.Host); err != nil { - return nil, fmt.Errorf(`URL address does not have the form "host:port": %s`, in) - } - if u.Path != "" { - return nil, fmt.Errorf("URL must not contain a path: %s", in) - } all[i] = *u } us := URLs(all) us.Sort() - return us, nil } diff --git a/pkg/flags/urls_test.go b/pkg/flags/urls_test.go index ff4bda8d4..75c2f6414 100644 --- a/pkg/flags/urls_test.go +++ b/pkg/flags/urls_test.go @@ -29,9 +29,6 @@ func TestValidateURLsValueBad(t *testing.T) { // bad port specification "127.0.0.1:foo", "127.0.0.1:", - // unix sockets not supported - "unix://", - "unix://tmp/etcd.sock", // bad strings "somewhere", "234#$", From 75747f24bcdebefff22e27853f2ac0b71ec91ea0 Mon Sep 17 00:00:00 2001 From: Karen Almog Date: Wed, 23 Dec 2020 09:47:43 +0100 Subject: [PATCH 2/2] Add unix socket test to TestNewURLsValue --- pkg/flags/urls_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/flags/urls_test.go b/pkg/flags/urls_test.go index 75c2f6414..ebc9a2674 100644 --- a/pkg/flags/urls_test.go +++ b/pkg/flags/urls_test.go @@ -53,6 +53,9 @@ func TestNewURLsValue(t *testing.T) { {s: "http://10.1.1.1:80", exp: []url.URL{{Scheme: "http", Host: "10.1.1.1:80"}}}, {s: "http://localhost:80", exp: []url.URL{{Scheme: "http", Host: "localhost:80"}}}, {s: "http://:80", exp: []url.URL{{Scheme: "http", Host: ":80"}}}, + {s: "unix://tmp/etcd.sock", exp: []url.URL{{Scheme: "unix", Host: "tmp", Path: "/etcd.sock"}}}, + {s: "unix:///tmp/127.27.84.4:23432", exp: []url.URL{{Scheme: "unix", Path: "/tmp/127.27.84.4:23432"}}}, + {s: "unix://127.0.0.5:1456", exp: []url.URL{{Scheme: "unix", Host: "127.0.0.5:1456"}}}, { s: "http://localhost:1,https://localhost:2", exp: []url.URL{