Skip to content

Commit 89fd237

Browse files
authored
Client: Do not trigger product check on 401 or 403 and forward response to user (#369)
1 parent 75263a5 commit 89fd237

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

elasticsearch.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,15 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
212212
// Retrieve the original request.
213213
res, err := c.Transport.Perform(req)
214214

215-
// ResponseCheck path continues, we run the header check on the first answer from ES.
216-
if err == nil {
215+
// ResponseCheck, we run the header check on the first answer from ES.
216+
if err == nil && (res.StatusCode != http.StatusForbidden && res.StatusCode != http.StatusUnauthorized) {
217217
checkHeader := func() error { return genuineCheckHeader(res.Header) }
218218
if err := c.doProductCheck(checkHeader); err != nil {
219219
res.Body.Close()
220220
return nil, err
221221
}
222222
}
223+
223224
return res, err
224225
}
225226

elasticsearch_internal_test.go

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
//go:build !integration
1819
// +build !integration
1920

2021
package elasticsearch
@@ -389,10 +390,10 @@ func TestClientMetrics(t *testing.T) {
389390

390391
func TestResponseCheckOnly(t *testing.T) {
391392
tests := []struct {
392-
name string
393-
response *http.Response
394-
requestErr error
395-
wantErr bool
393+
name string
394+
response *http.Response
395+
requestErr error
396+
wantErr bool
396397
}{
397398
{
398399
name: "Valid answer with header",
@@ -410,14 +411,62 @@ func TestResponseCheckOnly(t *testing.T) {
410411
wantErr: true,
411412
},
412413
{
413-
name: "Valid answer with http error code",
414+
name: "Valid answer with header and response check",
414415
response: &http.Response{
415-
StatusCode: http.StatusUnauthorized,
416416
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
417-
Body: ioutil.NopCloser(strings.NewReader("{}")),
417+
Body: ioutil.NopCloser(strings.NewReader("{}")),
418418
},
419419
wantErr: false,
420420
},
421+
{
422+
name: "Valid answer without header and response check",
423+
response: &http.Response{
424+
Body: ioutil.NopCloser(strings.NewReader("{}")),
425+
},
426+
wantErr: true,
427+
},
428+
{
429+
name: "Request failed",
430+
response: nil,
431+
requestErr: errors.New("request failed"),
432+
wantErr: true,
433+
},
434+
{
435+
name: "Valid request, 500 response",
436+
response: &http.Response{
437+
StatusCode: http.StatusInternalServerError,
438+
Body: ioutil.NopCloser(strings.NewReader("")),
439+
},
440+
requestErr: nil,
441+
wantErr: true,
442+
},
443+
{
444+
name: "Valid request, 404 response",
445+
response: &http.Response{
446+
StatusCode: http.StatusNotFound,
447+
Body: ioutil.NopCloser(strings.NewReader("")),
448+
},
449+
requestErr: nil,
450+
wantErr: true,
451+
},
452+
{
453+
name: "Valid request, 403 response",
454+
response: &http.Response{
455+
StatusCode: http.StatusForbidden,
456+
Body: ioutil.NopCloser(strings.NewReader("")),
457+
},
458+
requestErr: nil,
459+
wantErr: false,
460+
},
461+
{
462+
name: "Valid request, 401 response",
463+
response: &http.Response{
464+
StatusCode: http.StatusUnauthorized,
465+
Body: ioutil.NopCloser(strings.NewReader("")),
466+
},
467+
requestErr: nil,
468+
wantErr: false,
469+
},
421470
}
422471

423472
for _, tt := range tests {
@@ -435,7 +484,6 @@ func TestResponseCheckOnly(t *testing.T) {
435484
}
436485
}
437486

438-
439487
func TestProductCheckError(t *testing.T) {
440488
var requestPaths []string
441489
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -472,7 +520,6 @@ func TestProductCheckError(t *testing.T) {
472520
}
473521
}
474522

475-
476523
func TestFingerprint(t *testing.T) {
477524
body := []byte(`{"body": true"}"`)
478525
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -482,7 +529,7 @@ func TestFingerprint(t *testing.T) {
482529
defer server.Close()
483530

484531
config := Config{
485-
Addresses: []string{server.URL},
532+
Addresses: []string{server.URL},
486533
DisableRetry: true,
487534
}
488535

@@ -507,4 +554,4 @@ func TestFingerprint(t *testing.T) {
507554
if bytes.Compare(data, body) != 0 {
508555
t.Fatalf("unexpected payload returned: expected: %s, got: %s", body, data)
509556
}
510-
}
557+
}

0 commit comments

Comments
 (0)