Skip to content

Commit 0b113bc

Browse files
committed
Remove the default port when parsing CloudID into address
Closes 81
1 parent 9bf0926 commit 0b113bc

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

elasticsearch.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,7 @@ func addrsToURLs(addrs []string) ([]*url.URL, error) {
193193
// See: https://www.elastic.co/guide/en/cloud/current/ec-cloud-id.html
194194
//
195195
func addrFromCloudID(input string) (string, error) {
196-
var (
197-
port = 9243
198-
scheme = "https://"
199-
)
196+
var scheme = "https://"
200197

201198
values := strings.Split(input, ":")
202199
if len(values) != 2 {
@@ -207,5 +204,5 @@ func addrFromCloudID(input string) (string, error) {
207204
return "", err
208205
}
209206
parts := strings.Split(string(data), "$")
210-
return fmt.Sprintf("%s%s.%s:%d", scheme, parts[1], parts[0], port), nil
207+
return fmt.Sprintf("%s%s.%s", scheme, parts[1], parts[0]), nil
211208
}

elasticsearch_internal_test.go

+25-10
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,16 @@ func TestClientConfiguration(t *testing.T) {
9898
})
9999

100100
t.Run("With CloudID", func(t *testing.T) {
101+
// bar.cloud.es.io$abc123$def456
101102
c, err := NewClient(Config{CloudID: "foo:YmFyLmNsb3VkLmVzLmlvJGFiYzEyMyRkZWY0NTY="})
102103
if err != nil {
103104
t.Fatalf("Unexpected error: %s", err)
104105
}
105106

106107
u := c.Transport.(*estransport.Client).URLs()[0].String()
107108

108-
if u != "https://abc123.bar.cloud.es.io:9243" {
109-
t.Errorf("Unexpected URL, want=https://abc123.bar.cloud.es.io:9243, got=%s", u)
109+
if u != "https://abc123.bar.cloud.es.io" {
110+
t.Errorf("Unexpected URL, want=https://abc123.bar.cloud.es.io, got=%s", u)
110111
}
111112
})
112113

@@ -237,16 +238,30 @@ func TestAddrsToURLs(t *testing.T) {
237238

238239
func TestCloudID(t *testing.T) {
239240
t.Run("Parse", func(t *testing.T) {
240-
input := "name:" + base64.StdEncoding.EncodeToString([]byte("host$es$kibana"))
241-
expected := "https://es.host:9243"
242-
243-
actual, err := addrFromCloudID(input)
244-
if err != nil {
245-
t.Errorf("Unexpected error: %s", err)
241+
var testdata = []struct {
242+
in string
243+
out string
244+
}{
245+
{
246+
in: "name:" + base64.StdEncoding.EncodeToString([]byte("host$es$kibana")),
247+
out: "https://es.host",
248+
},
249+
{
250+
in: "name:" + base64.StdEncoding.EncodeToString([]byte("host:9243$es$kibana")),
251+
out: "https://es.host:9243",
252+
},
246253
}
247-
if actual != expected {
248-
t.Errorf("Unexpected output, want=%q, got=%q", expected, actual)
254+
255+
for _, tt := range testdata {
256+
actual, err := addrFromCloudID(tt.in)
257+
if err != nil {
258+
t.Errorf("Unexpected error: %s", err)
259+
}
260+
if actual != tt.out {
261+
t.Errorf("Unexpected output, want=%q, got=%q", tt.out, actual)
262+
}
249263
}
264+
250265
})
251266

252267
t.Run("Invalid format", func(t *testing.T) {

0 commit comments

Comments
 (0)