From 4a26cb92c633b025441568388abcd1a2f8122bf4 Mon Sep 17 00:00:00 2001 From: Andrew Hackmann <5140848+bossinc@users.noreply.github.com> Date: Fri, 1 Aug 2025 11:01:25 -0500 Subject: [PATCH] ElasticSearch: Fix inline casting bug when validating the index (#108951) split out inline casts --- pkg/tsdb/elasticsearch/healthcheck.go | 10 +++++++--- pkg/tsdb/elasticsearch/healthcheck_test.go | 10 ++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/tsdb/elasticsearch/healthcheck.go b/pkg/tsdb/elasticsearch/healthcheck.go index a3e04aafb6f..928945691de 100644 --- a/pkg/tsdb/elasticsearch/healthcheck.go +++ b/pkg/tsdb/elasticsearch/healthcheck.go @@ -191,11 +191,15 @@ func validateIndex(ctx context.Context, ds *es.DatasourceInfo) (message string, return "Failed to unmarshal field capabilities response", "error" } if fieldCaps["error"] != nil { - if errorMessage, ok := fieldCaps["error"].(map[string]any)["reason"].(string); ok { - return fmt.Sprintf("Error validating index: %s", errorMessage), "warning" - } else { + errorMap, ok := fieldCaps["error"].(map[string]any) + if !ok { return "Error validating index", "warning" } + errorMessage, ok := errorMap["reason"].(string) + if !ok { + return "Error validating index", "warning" + } + return fmt.Sprintf("Error validating index: %s", errorMessage), "warning" } fields, ok := fieldCaps["fields"].(map[string]any) diff --git a/pkg/tsdb/elasticsearch/healthcheck_test.go b/pkg/tsdb/elasticsearch/healthcheck_test.go index b3f6dc97c93..48fd00e8adc 100644 --- a/pkg/tsdb/elasticsearch/healthcheck_test.go +++ b/pkg/tsdb/elasticsearch/healthcheck_test.go @@ -58,6 +58,16 @@ func Test_validateIndex_Warning_ErrorValidatingIndex(t *testing.T) { assert.Equal(t, "Elasticsearch data source is healthy. Warning: Error validating index: index_not_found", res.Message) } +func Test_validateIndex_Warning_ErrorValidatingIndex2(t *testing.T) { + service := GetMockService(http.StatusOK, "200 OK", `{"status":"green"}`, `{"error":"not a map"}`) + res, _ := service.CheckHealth(mockedCfg, &backend.CheckHealthRequest{ + PluginContext: backend.PluginContext{}, + Headers: nil, + }) + assert.Equal(t, backend.HealthStatusOk, res.Status) + assert.Equal(t, "Elasticsearch data source is healthy. Warning: Error validating index", res.Message) +} + func Test_validateIndex_Warning_WrongTimestampType(t *testing.T) { service := GetMockService(http.StatusOK, "200 OK", `{"status":"green"}`, `{"fields":{"timestamp":{"float":{"metadata_field":true}}}}`) res, _ := service.CheckHealth(mockedCfg, &backend.CheckHealthRequest{