Skip to content

Commit 5f8a17a

Browse files
authoredNov 23, 2021
Client: Validate product only on successful responses (#370)
1 parent 89fd237 commit 5f8a17a

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed
 

‎elasticsearch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
213213
res, err := c.Transport.Perform(req)
214214

215215
// ResponseCheck, we run the header check on the first answer from ES.
216-
if err == nil && (res.StatusCode != http.StatusForbidden && res.StatusCode != http.StatusUnauthorized) {
216+
if err == nil && (res.StatusCode >= 200 && res.StatusCode < 300) {
217217
checkHeader := func() error { return genuineCheckHeader(res.Header) }
218218
if err := c.doProductCheck(checkHeader); err != nil {
219219
res.Body.Close()

‎elasticsearch_internal_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ func TestResponseCheckOnly(t *testing.T) {
406406
{
407407
name: "Valid answer without header",
408408
response: &http.Response{
409+
StatusCode: http.StatusOK,
409410
Body: ioutil.NopCloser(strings.NewReader("{}")),
410411
},
411412
wantErr: true,
@@ -421,6 +422,7 @@ func TestResponseCheckOnly(t *testing.T) {
421422
{
422423
name: "Valid answer without header and response check",
423424
response: &http.Response{
425+
StatusCode: http.StatusOK,
424426
Body: ioutil.NopCloser(strings.NewReader("{}")),
425427
},
426428
wantErr: true,
@@ -438,7 +440,7 @@ func TestResponseCheckOnly(t *testing.T) {
438440
Body: ioutil.NopCloser(strings.NewReader("")),
439441
},
440442
requestErr: nil,
441-
wantErr: true,
443+
wantErr: false,
442444
},
443445
{
444446
name: "Valid request, 404 response",
@@ -447,7 +449,7 @@ func TestResponseCheckOnly(t *testing.T) {
447449
Body: ioutil.NopCloser(strings.NewReader("")),
448450
},
449451
requestErr: nil,
450-
wantErr: true,
452+
wantErr: false,
451453
},
452454
{
453455
name: "Valid request, 403 response",
@@ -494,14 +496,15 @@ func TestProductCheckError(t *testing.T) {
494496
w.WriteHeader(http.StatusBadGateway)
495497
return
496498
}
499+
497500
w.Header().Set("X-Elastic-Product", "Elasticsearch")
498501
w.Write([]byte("{}"))
499502
}))
500503
defer server.Close()
501504

502505
c, _ := NewClient(Config{Addresses: []string{server.URL}, DisableRetry: true})
503-
if _, err := c.Cat.Indices(); err == nil {
504-
t.Fatal("expected error")
506+
if _, err := c.Cat.Indices(); err != nil {
507+
t.Fatalf("unexpected error: %s", err)
505508
}
506509
if c.productCheckSuccess {
507510
t.Fatalf("product check should be invalid, got %v", c.productCheckSuccess)

0 commit comments

Comments
 (0)
Please sign in to comment.