Skip to content

Commit ed288cd

Browse files
committed
Ignore the ELASTICSEARCH_URL variable when address is passed in configuration
Fixes #113 Closes #115 (cherry picked from commit 86a626c)
1 parent ddae56a commit ed288cd

File tree

3 files changed

+68
-39
lines changed

3 files changed

+68
-39
lines changed

doc.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ To configure the client, pass a Config object to the NewClient function:
3030
elasticsearch.NewClient(cfg)
3131
3232
When using the Elastic Service (https://elastic.co/cloud), you can use CloudID instead of Addresses.
33+
When either Addresses or CloudID is set, the ELASTICSEARCH_URL environment variable is ignored.
3334
3435
See the elasticsearch_integration_test.go file and the _examples folder for more information.
3536

elasticsearch.go

+23-24
Original file line numberDiff line numberDiff line change
@@ -83,36 +83,30 @@ func NewDefaultClient() (*Client, error) {
8383
// It will use the ELASTICSEARCH_URL environment variable, if set,
8484
// to configure the addresses; use a comma to separate multiple URLs.
8585
//
86-
// It's an error to set both cfg.Addresses and the ELASTICSEARCH_URL
87-
// environment variable.
86+
// If either cfg.Addresses or cfg.CloudID is set, the ELASTICSEARCH_URL
87+
// environment variable is ignored.
88+
//
89+
// It's an error to set both cfg.Addresses and cfg.CloudID.
8890
//
8991
func NewClient(cfg Config) (*Client, error) {
9092
var addrs []string
9193

92-
envAddrs := addrsFromEnvironment()
93-
94-
if len(envAddrs) > 0 && len(cfg.Addresses) > 0 {
95-
return nil, errors.New("cannot create client: both ELASTICSEARCH_URL and Addresses are set")
96-
}
97-
98-
if len(envAddrs) > 0 && cfg.CloudID != "" {
99-
return nil, errors.New("cannot create client: both ELASTICSEARCH_URL and CloudID are set")
100-
}
101-
102-
if len(cfg.Addresses) > 0 && cfg.CloudID != "" {
103-
return nil, errors.New("cannot create client: both Addresses and CloudID are set")
104-
}
94+
if len(cfg.Addresses) == 0 && cfg.CloudID == "" {
95+
addrs = addrsFromEnvironment()
96+
} else {
97+
if len(cfg.Addresses) > 0 && cfg.CloudID != "" {
98+
return nil, errors.New("cannot create client: both Addresses and CloudID are set")
99+
}
105100

106-
if cfg.CloudID != "" {
107-
cloudAddrs, err := addrFromCloudID(cfg.CloudID)
108-
if err != nil {
109-
return nil, fmt.Errorf("cannot create client: cannot parse CloudID: %s", err)
101+
if cfg.CloudID != "" {
102+
cloudAddr, err := addrFromCloudID(cfg.CloudID)
103+
if err != nil {
104+
return nil, fmt.Errorf("cannot create client: cannot parse CloudID: %s", err)
105+
}
106+
addrs = append(addrs, cloudAddr)
110107
}
111-
addrs = append(addrs, cloudAddrs)
112-
} else {
113-
if len(envAddrs) > 0 {
114-
addrs = append(addrs, envAddrs...)
115-
} else if len(cfg.Addresses) > 0 {
108+
109+
if len(cfg.Addresses) > 0 {
116110
addrs = append(addrs, cfg.Addresses...)
117111
}
118112
}
@@ -236,5 +230,10 @@ func addrFromCloudID(input string) (string, error) {
236230
return "", err
237231
}
238232
parts := strings.Split(string(data), "$")
233+
234+
if len(parts) < 2 {
235+
return "", fmt.Errorf("invalid encoded value: %s", parts)
236+
}
237+
239238
return fmt.Sprintf("%s%s.%s", scheme, parts[1], parts[0]), nil
240239
}

elasticsearch_internal_test.go

+44-15
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,20 @@ func TestClientConfiguration(t *testing.T) {
2222
t.Parallel()
2323

2424
t.Run("With empty", func(t *testing.T) {
25-
_, err := NewDefaultClient()
25+
c, err := NewDefaultClient()
2626

2727
if err != nil {
2828
t.Errorf("Unexpected error: %s", err)
2929
}
30+
31+
u := c.Transport.(*estransport.Client).URLs()[0].String()
32+
33+
if u != defaultURL {
34+
t.Errorf("Unexpected URL, want=%s, got=%s", defaultURL, u)
35+
}
3036
})
3137

32-
t.Run("With address", func(t *testing.T) {
38+
t.Run("With URL from Addresses", func(t *testing.T) {
3339
c, err := NewClient(Config{Addresses: []string{"http://localhost:8080//"}})
3440
if err != nil {
3541
t.Fatalf("Unexpected error: %s", err)
@@ -62,32 +68,36 @@ func TestClientConfiguration(t *testing.T) {
6268
os.Setenv("ELASTICSEARCH_URL", "http://example.com")
6369
defer func() { os.Setenv("ELASTICSEARCH_URL", "") }()
6470

65-
_, err := NewClient(Config{Addresses: []string{"http://localhost:8080//"}})
66-
if err == nil {
67-
t.Fatalf("Expected error, got: %v", err)
71+
c, err := NewClient(Config{Addresses: []string{"http://localhost:8080//"}})
72+
if err != nil {
73+
t.Fatalf("Unexpected error: %s", err)
6874
}
69-
match, _ := regexp.MatchString("both .* are set", err.Error())
70-
if !match {
71-
t.Errorf("Expected error when addresses from environment and configuration are used together, got: %v", err)
75+
76+
u := c.Transport.(*estransport.Client).URLs()[0].String()
77+
78+
if u != "http://localhost:8080" {
79+
t.Errorf("Unexpected URL, want=http://localhost:8080, got=%s", u)
7280
}
7381
})
7482

7583
t.Run("With URL from environment and cfg.CloudID", func(t *testing.T) {
7684
os.Setenv("ELASTICSEARCH_URL", "http://example.com")
7785
defer func() { os.Setenv("ELASTICSEARCH_URL", "") }()
7886

79-
_, err := NewClient(Config{CloudID: "foobar="})
80-
if err == nil {
81-
t.Fatalf("Expected error, got: %v", err)
87+
c, err := NewClient(Config{CloudID: "foo:YmFyLmNsb3VkLmVzLmlvJGFiYzEyMyRkZWY0NTY="})
88+
if err != nil {
89+
t.Fatalf("Unexpected error: %s", err)
8290
}
83-
match, _ := regexp.MatchString("both .* are set", err.Error())
84-
if !match {
85-
t.Errorf("Expected error when addresses from environment and configuration are used together, got: %v", err)
91+
92+
u := c.Transport.(*estransport.Client).URLs()[0].String()
93+
94+
if u != "https://abc123.bar.cloud.es.io" {
95+
t.Errorf("Unexpected URL, want=https://abc123.bar.cloud.es.io, got=%s", u)
8696
}
8797
})
8898

8999
t.Run("With cfg.Addresses and cfg.CloudID", func(t *testing.T) {
90-
_, err := NewClient(Config{Addresses: []string{"http://localhost:8080//"}, CloudID: "foobar="})
100+
_, err := NewClient(Config{Addresses: []string{"http://localhost:8080//"}, CloudID: "foo:ABC="})
91101
if err == nil {
92102
t.Fatalf("Expected error, got: %v", err)
93103
}
@@ -111,6 +121,25 @@ func TestClientConfiguration(t *testing.T) {
111121
}
112122
})
113123

124+
t.Run("With invalid CloudID", func(t *testing.T) {
125+
var err error
126+
127+
_, err = NewClient(Config{CloudID: "foo:ZZZ==="})
128+
if err == nil {
129+
t.Errorf("Expected error for CloudID, got: %v", err)
130+
}
131+
132+
_, err = NewClient(Config{CloudID: "foo:Zm9v"})
133+
if err == nil {
134+
t.Errorf("Expected error for CloudID, got: %v", err)
135+
}
136+
137+
_, err = NewClient(Config{CloudID: "foo:"})
138+
if err == nil {
139+
t.Errorf("Expected error for CloudID, got: %v", err)
140+
}
141+
})
142+
114143
t.Run("With invalid URL", func(t *testing.T) {
115144
u := ":foo"
116145
_, err := NewClient(Config{Addresses: []string{u}})

0 commit comments

Comments
 (0)