Skip to content

Commit 9aefd34

Browse files
committed
endpoints.Interpret returns Host:port as ServerName
Signed-off-by: Chao Chen <[email protected]>
1 parent e59e3d7 commit 9aefd34

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

client/v3/internal/endpoint/endpoint.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ func extractHostFromHostPort(ep string) string {
4141
return host
4242
}
4343

44-
func extractHostFromPath(pathStr string) string {
45-
return extractHostFromHostPort(path.Base(pathStr))
46-
}
47-
4844
// mustSplit2 returns the values from strings.SplitN(s, sep, 2).
4945
// If sep is not found, it returns ("", "", false) instead.
5046
func mustSplit2(s, sep string) (string, string) {
@@ -96,29 +92,29 @@ func translateEndpoint(ep string) (addr string, serverName string, requireCreds
9692
if strings.HasPrefix(ep, "unix:///") || strings.HasPrefix(ep, "unixs:///") {
9793
// absolute path case
9894
schema, absolutePath := mustSplit2(ep, "://")
99-
return "unix://" + absolutePath, extractHostFromPath(absolutePath), schemeToCredsRequirement(schema)
95+
return "unix://" + absolutePath, path.Base(absolutePath), schemeToCredsRequirement(schema)
10096
}
10197
if strings.HasPrefix(ep, "unix://") || strings.HasPrefix(ep, "unixs://") {
10298
// legacy etcd local path
10399
schema, localPath := mustSplit2(ep, "://")
104-
return "unix:" + localPath, extractHostFromPath(localPath), schemeToCredsRequirement(schema)
100+
return "unix:" + localPath, path.Base(localPath), schemeToCredsRequirement(schema)
105101
}
106102
schema, localPath := mustSplit2(ep, ":")
107-
return "unix:" + localPath, extractHostFromPath(localPath), schemeToCredsRequirement(schema)
103+
return "unix:" + localPath, path.Base(localPath), schemeToCredsRequirement(schema)
108104
}
109105

110106
if strings.Contains(ep, "://") {
111107
url, err := url.Parse(ep)
112108
if err != nil {
113-
return ep, extractHostFromHostPort(ep), CREDS_OPTIONAL
109+
return ep, ep, CREDS_OPTIONAL
114110
}
115111
if url.Scheme == "http" || url.Scheme == "https" {
116-
return url.Host, url.Hostname(), schemeToCredsRequirement(url.Scheme)
112+
return url.Host, url.Host, schemeToCredsRequirement(url.Scheme)
117113
}
118-
return ep, url.Hostname(), schemeToCredsRequirement(url.Scheme)
114+
return ep, url.Host, schemeToCredsRequirement(url.Scheme)
119115
}
120116
// Handles plain addresses like 10.0.0.44:437.
121-
return ep, extractHostFromHostPort(ep), CREDS_OPTIONAL
117+
return ep, ep, CREDS_OPTIONAL
122118
}
123119

124120
// RequiresCredentials returns whether given endpoint requires

client/v3/internal/endpoint/endpoint_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,35 @@ func Test_interpret(t *testing.T) {
2727
}{
2828
{"127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
2929
{"localhost", "localhost", "localhost", CREDS_OPTIONAL},
30-
{"localhost:8080", "localhost:8080", "localhost", CREDS_OPTIONAL},
30+
{"localhost:8080", "localhost:8080", "localhost:8080", CREDS_OPTIONAL},
3131

3232
{"unix:127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
33-
{"unix:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1", CREDS_OPTIONAL},
33+
{"unix:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_OPTIONAL},
3434

3535
{"unix://127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_OPTIONAL},
36-
{"unix://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1", CREDS_OPTIONAL},
36+
{"unix://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_OPTIONAL},
3737

3838
{"unixs:127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
39-
{"unixs:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1", CREDS_REQUIRE},
39+
{"unixs:127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
4040
{"unixs://127.0.0.1", "unix:127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
41-
{"unixs://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1", CREDS_REQUIRE},
41+
{"unixs://127.0.0.1:8080", "unix:127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
4242

4343
{"http://127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_DROP},
44-
{"http://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1", CREDS_DROP},
44+
{"http://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1:8080", CREDS_DROP},
4545
{"https://127.0.0.1", "127.0.0.1", "127.0.0.1", CREDS_REQUIRE},
46-
{"https://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1", CREDS_REQUIRE},
47-
{"https://localhost:20000", "localhost:20000", "localhost", CREDS_REQUIRE},
46+
{"https://127.0.0.1:8080", "127.0.0.1:8080", "127.0.0.1:8080", CREDS_REQUIRE},
47+
{"https://localhost:20000", "localhost:20000", "localhost:20000", CREDS_REQUIRE},
4848

4949
{"unix:///tmp/abc", "unix:///tmp/abc", "abc", CREDS_OPTIONAL},
5050
{"unixs:///tmp/abc", "unix:///tmp/abc", "abc", CREDS_REQUIRE},
51-
{"unix:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc", CREDS_OPTIONAL},
52-
{"unixs:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc", CREDS_REQUIRE},
51+
{"unix:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc:1234", CREDS_OPTIONAL},
52+
{"unixs:///tmp/abc:1234", "unix:///tmp/abc:1234", "abc:1234", CREDS_REQUIRE},
5353
{"etcd.io", "etcd.io", "etcd.io", CREDS_OPTIONAL},
5454
{"http://etcd.io/abc", "etcd.io", "etcd.io", CREDS_DROP},
5555
{"dns://something-other", "dns://something-other", "something-other", CREDS_OPTIONAL},
5656

57-
{"http://[2001:db8:1f70::999:de8:7648:6e8]:100/", "[2001:db8:1f70::999:de8:7648:6e8]:100", "2001:db8:1f70::999:de8:7648:6e8", CREDS_DROP},
58-
{"[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", "2001:db8:1f70::999:de8:7648:6e8", CREDS_OPTIONAL},
57+
{"http://[2001:db8:1f70::999:de8:7648:6e8]:100/", "[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", CREDS_DROP},
58+
{"[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", "[2001:db8:1f70::999:de8:7648:6e8]:100", CREDS_OPTIONAL},
5959
{"unix:unexpected-file_name#123$456", "unix:unexpected-file_name#123$456", "unexpected-file_name#123$456", CREDS_OPTIONAL},
6060
}
6161
for _, tt := range tests {

tests/integration/grpc_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ func TestAuthority(t *testing.T) {
103103
defer kv.Close()
104104

105105
putRequestMethod := "/etcdserverpb.KV/Put"
106-
_, err := kv.Put(context.TODO(), "foo", "bar")
107-
if err != nil {
108-
t.Fatal(err)
106+
for i := 0; i < 100; i++ {
107+
_, err := kv.Put(context.TODO(), "foo", "bar")
108+
if err != nil {
109+
t.Fatal(err)
110+
}
109111
}
110112

111-
assertAuthority(t, templateAuthority(t, tc.expectAuthorityPattern, clus.Members[0]), clus, putRequestMethod)
113+
assertAuthority(t, tc.expectAuthorityPattern, clus, putRequestMethod)
112114
})
113115
}
114116
}
@@ -162,10 +164,11 @@ func templateAuthority(t *testing.T, pattern string, m *integration.Member) stri
162164
return authority
163165
}
164166

165-
func assertAuthority(t *testing.T, expectedAuthority string, clus *integration.Cluster, filterMethod string) {
167+
func assertAuthority(t *testing.T, expectedAuthorityPattern string, clus *integration.Cluster, filterMethod string) {
166168
t.Helper()
167169
requestsFound := 0
168170
for _, m := range clus.Members {
171+
expectedAuthority := templateAuthority(t, expectedAuthorityPattern, m)
169172
for _, r := range m.RecordedRequests() {
170173
if filterMethod != "" && r.FullMethod != filterMethod {
171174
continue

0 commit comments

Comments
 (0)