From 9c06d7699eeb6c8b281fdf826f0d3d860491d665 Mon Sep 17 00:00:00 2001 From: Istvan Nagy Date: Thu, 7 Sep 2023 09:17:15 +0200 Subject: [PATCH] Telemetry test cases support 8.10 and later Kibana versions (#7150) * telmetry api url for 8.10 and later * lintfix * compare -SNAPSHOT releases correctly * adding extra headers for 8.10+ telemetry checks * added another required header * fixed comment text --- test/e2e/beat/setup_test.go | 3 ++- test/e2e/test/apmserver/checks_apm.go | 2 +- test/e2e/test/kibana/checks_kb.go | 5 +++-- test/e2e/test/kibana/checks_telemetry.go | 22 ++++++++++++++++++++-- test/e2e/test/kibana/http_client.go | 11 ++++++++++- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/test/e2e/beat/setup_test.go b/test/e2e/beat/setup_test.go index 98d26b73e4..ab94a4b97e 100644 --- a/test/e2e/beat/setup_test.go +++ b/test/e2e/beat/setup_test.go @@ -9,6 +9,7 @@ package beat import ( "encoding/json" "fmt" + "net/http" "testing" esv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/elasticsearch/v1" @@ -124,7 +125,7 @@ func getDashboardCheck(esBuilder elasticsearch.Builder, kbBuilder kibana.Builder // name of the beat. This test will obviously break if future versions of Beats abandon this naming convention. query := fmt.Sprintf("/api/saved_objects/_find?type=dashboard&search_fields=title&search=%s", beat) body, err := kibana.DoRequest(client, kbBuilder.Kibana, password, - "GET", query, nil, + "GET", query, nil, http.Header{}, ) if err != nil { return err diff --git a/test/e2e/test/apmserver/checks_apm.go b/test/e2e/test/apmserver/checks_apm.go index e6b6dfe434..9ec7ed6208 100644 --- a/test/e2e/test/apmserver/checks_apm.go +++ b/test/e2e/test/apmserver/checks_apm.go @@ -388,7 +388,7 @@ func (c *apmClusterChecks) CheckAgentConfiguration(apm apmv1.ApmServer, k *test. if !apmVersion.GTE(version.MustParse("7.7.0")) { uri += "/new" } - _, err = kibana.DoRequest(k, kb, password, "PUT", uri, []byte(sampleDefaultAgentConfiguration)) + _, err = kibana.DoRequest(k, kb, password, "PUT", uri, []byte(sampleDefaultAgentConfiguration), http.Header{}) return err }), }, diff --git a/test/e2e/test/kibana/checks_kb.go b/test/e2e/test/kibana/checks_kb.go index 001d1f032c..81e1aacde8 100644 --- a/test/e2e/test/kibana/checks_kb.go +++ b/test/e2e/test/kibana/checks_kb.go @@ -7,6 +7,7 @@ package kibana import ( "encoding/json" "fmt" + "net/http" "github.com/pkg/errors" @@ -54,7 +55,7 @@ func (check *kbChecks) CheckKbStatusHealthy(b Builder) test.Step { if err != nil { return errors.Wrap(err, "while getting elastic password") } - body, err := DoRequest(check.client, b.Kibana, password, "GET", "/api/status", nil) + body, err := DoRequest(check.client, b.Kibana, password, "GET", "/api/status", nil, http.Header{}) if err != nil { return err } @@ -94,7 +95,7 @@ func (check *kbChecks) CheckEntSearchAccess(b Builder) test.Step { if version.MustParse(b.Kibana.Spec.Version).GTE(version.MinFor(7, 16, 0)) { path = "/internal/workplace_search/overview" } - _, err = DoRequest(check.client, b.Kibana, password, "GET", path, nil) + _, err = DoRequest(check.client, b.Kibana, password, "GET", path, nil, http.Header{}) return err }), } diff --git a/test/e2e/test/kibana/checks_telemetry.go b/test/e2e/test/kibana/checks_telemetry.go index 5e650bc8e3..1e803dcb65 100644 --- a/test/e2e/test/kibana/checks_telemetry.go +++ b/test/e2e/test/kibana/checks_telemetry.go @@ -7,6 +7,7 @@ package kibana import ( "encoding/json" "fmt" + "net/http" "github.com/pkg/errors" @@ -18,7 +19,7 @@ import ( func MakeTelemetryRequest(kbBuilder Builder, k *test.K8sClient) (StackStats, error) { kbVersion := version.MustParse(kbBuilder.Kibana.Spec.Version) apiVersion, payload := apiVersionAndTelemetryRequestBody(kbVersion) - uri := fmt.Sprintf("/api/telemetry/%s/clusters/_stats", apiVersion) + uri := telemetryAPIURI(kbVersion, apiVersion) password, err := k.GetElasticPassword(kbBuilder.ElasticsearchRef().NamespacedName()) if err != nil { return StackStats{}, err @@ -27,15 +28,32 @@ func MakeTelemetryRequest(kbBuilder Builder, k *test.K8sClient) (StackStats, err if err != nil { return StackStats{}, err } + + // a few extra headers are required by Kibana for this internal API + extraHeaders := http.Header{} + if version.WithoutPre(kbVersion).GTE(version.From(8, 10, 0)) { + extraHeaders.Add("elastic-api-version", "2") + extraHeaders.Add("kbn-xsrf", "reporting") + extraHeaders.Add("x-elastic-internal-origin", "eck-e2e-tests") + } + // this call may fail (status 500) if the .security-7 index is not fully initialized yet, // in which case we'll just retry that test step - bytes, err := DoRequest(k, kbBuilder.Kibana, password, "POST", uri, payloadBytes) + bytes, err := DoRequest(k, kbBuilder.Kibana, password, "POST", uri, payloadBytes, extraHeaders) if err != nil { return StackStats{}, err } return unmarshalTelemetryResponse(bytes, kbVersion) } +func telemetryAPIURI(kbVersion version.Version, apiVersion string) string { + uri := fmt.Sprintf("/api/telemetry/%s/clusters/_stats", apiVersion) + if version.WithoutPre(kbVersion).GTE(version.From(8, 10, 0)) { + uri = "/internal/telemetry/clusters/_stats" + } + return uri +} + func apiVersionAndTelemetryRequestBody(kbVersion version.Version) (string, telemetryRequest) { apiVersion := "v1" payload := telemetryRequest{ diff --git a/test/e2e/test/kibana/http_client.go b/test/e2e/test/kibana/http_client.go index 708a39e9cc..695bc1592f 100644 --- a/test/e2e/test/kibana/http_client.go +++ b/test/e2e/test/kibana/http_client.go @@ -33,7 +33,7 @@ func NewKibanaClient(kb kbv1.Kibana, k *test.K8sClient) (*http.Client, error) { } // DoRequest executes an HTTP request against a Kibana instance using the given password for the elastic user. -func DoRequest(k *test.K8sClient, kb kbv1.Kibana, password string, method string, pathAndQuery string, body []byte) ([]byte, error) { +func DoRequest(k *test.K8sClient, kb kbv1.Kibana, password string, method string, pathAndQuery string, body []byte, extraHeaders http.Header) ([]byte, error) { scheme := "http" if kb.Spec.HTTP.TLS.Enabled() { scheme = "https" @@ -61,10 +61,19 @@ func DoRequest(k *test.K8sClient, kb kbv1.Kibana, password string, method string req.Header.Set("Content-Type", "application/json") // send the kbn-version header expected by the Kibana server to protect against xsrf attacks req.Header.Set("kbn-version", kb.Spec.Version) + + // add any extra headers + for name, values := range extraHeaders { + for _, value := range values { + req.Header.Add(name, value) + } + } + client, err := NewKibanaClient(kb, k) if err != nil { return nil, errors.Wrap(err, "while creating kibana client") } + resp, err := client.Do(req) if err != nil { return nil, errors.Wrap(err, "while doing request")