Skip to content

Commit

Permalink
Return error message when healthcheck set to false and credentials ar…
Browse files Browse the repository at this point in the history
…e incorrect (#148)

Signed-off-by: Rupa Lahiri <[email protected]>
  • Loading branch information
rblcoder authored Feb 1, 2024
1 parent 1c27b66 commit 14a019f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ func getClient(conf *ProviderConf) (*elastic7.Client, error) {
info, httpStatus, err := client.Ping(conf.rawUrl).Do(ctx)
if httpStatus == http.StatusForbidden {
return nil, errors.New("HTTP 403 Forbidden: Permission denied. Please ensure that the correct credentials are being used to access the cluster.")
} else if httpStatus == http.StatusUnauthorized {
return nil, errors.New("HTTP 401 Unauthorized: Please ensure that the correct credentials are being used to access the cluster")
}
if err != nil {
// Replace the timeout error because it gives no context
Expand Down
24 changes: 24 additions & 0 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -69,6 +70,29 @@ func testAccPreCheck(t *testing.T) {
}
}

// Given:
// 1. invalid username and password and healthcheck is false
//
// this tests that 401 error is returned by getClient
func TestInvalidCredentials(t *testing.T) {
parsedUrl, _ := url.Parse("http://127.0.0.1:9200")
testConfig := &ProviderConf{
username: "1234",
password: "1234",
healthchecking: false,
rawUrl: "http://127.0.0.1:9200",
sniffing: false,
parsedUrl: parsedUrl,
pingTimeoutSeconds: 10,
}
_, err := getClient(testConfig)

errString := "HTTP 401 Unauthorized: Please ensure that the correct credentials are being used to access the cluster"
if err.Error() != errString {
t.Errorf("Error thrown should be %s", errString)
}
}

// Given:
// 1. AWS credentials are specified via environment variables
// 2. aws access key and secret access key are specified via the provider configuration
Expand Down

0 comments on commit 14a019f

Please sign in to comment.