diff --git a/internal/install/application_configuration.go b/internal/install/application_configuration.go index eee9483f85..fed2a2c1ad 100644 --- a/internal/install/application_configuration.go +++ b/internal/install/application_configuration.go @@ -64,11 +64,6 @@ func (ir ImageRefs) AsEnv() []string { return vars } -// DefaultStackImageRefs function selects the appropriate set of Docker image references for the default stack version. -func (ac *ApplicationConfiguration) DefaultStackImageRefs() ImageRefs { - return ac.StackImageRefs(DefaultStackVersion) -} - // StackImageRefs function selects the appropriate set of Docker image references for the given stack version. func (ac *ApplicationConfiguration) StackImageRefs(version string) ImageRefs { refs := ac.c.Stack.ImageRefOverridesForVersion(version) diff --git a/internal/kibana/injected_metadata.go b/internal/kibana/injected_metadata.go new file mode 100644 index 0000000000..3b3f6e99a4 --- /dev/null +++ b/internal/kibana/injected_metadata.go @@ -0,0 +1,62 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package kibana + +import ( + "encoding/json" + "fmt" + "html" + "net/http" + "regexp" + + "github.com/pkg/errors" +) + +var kbnInjectedMetadataRegexp = regexp.MustCompile(``) + +// injectedMetadata represents the Kibana metadata structure exposed in the web UI. +type injectedMetadata struct { + // Stack version + Version string `json:"version"` +} + +// Version method returns the Kibana version. +func (c *Client) Version() (string, error) { + statusCode, respBody, err := c.get("/login") + if err != nil { + return "", errors.Wrap(err, "could not reach login endpoint") + } + if statusCode != http.StatusOK { + return "", fmt.Errorf("could not reach login endpoint; API status code = %d; response body = %s", statusCode, string(respBody)) + } + + im, err := extractInjectedMetadata(respBody) + if err != nil { + return "", errors.Wrap(err, "can't extract injected metadata") + } + return im.Version, nil +} + +func extractInjectedMetadata(body []byte) (*injectedMetadata, error) { + rawInjectedMetadata, err := extractRawInjectedMetadata(body) + if err != nil { + return nil, errors.Wrap(err, "can't extract raw metadata") + } + + var im injectedMetadata + err = json.Unmarshal(rawInjectedMetadata, &im) + if err != nil { + return nil, errors.Wrap(err, "can't unmarshal raw injected metadata") + } + return &im, nil +} + +func extractRawInjectedMetadata(body []byte) ([]byte, error) { + matches := kbnInjectedMetadataRegexp.FindSubmatch(body) + if len(matches) < 2 { // index:0 - matched regexp, index:1 - matched data + return nil, errors.New("expected to find at least one tag") + } + return []byte(html.UnescapeString(string(matches[1]))), nil +} diff --git a/internal/kibana/injected_metadata_test.go b/internal/kibana/injected_metadata_test.go new file mode 100644 index 0000000000..d08dbd7cde --- /dev/null +++ b/internal/kibana/injected_metadata_test.go @@ -0,0 +1,36 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package kibana + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +// sampleLoginPage represents Kibana login page without redundant styles, fonts, noise in metadata, etc. +var sampleLoginPage = []byte(`Elastic