Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telemetry test cases support 8.10 and later Kibana versions #7150

Merged
merged 6 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion test/e2e/beat/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/test/apmserver/checks_apm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}),
},
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/test/kibana/checks_kb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package kibana
import (
"encoding/json"
"fmt"
"net/http"

"github.com/pkg/errors"

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}),
}
Expand Down
22 changes: 20 additions & 2 deletions test/e2e/test/kibana/checks_telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package kibana
import (
"encoding/json"
"fmt"
"net/http"

"github.com/pkg/errors"

Expand All @@ -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
Expand All @@ -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{
Expand Down
11 changes: 10 additions & 1 deletion test/e2e/test/kibana/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down