From 31c5dbc9ae582c5b520690bb83bf5b71c9353dcc Mon Sep 17 00:00:00 2001 From: mtojek Date: Wed, 3 Nov 2021 08:40:29 +0100 Subject: [PATCH 1/5] WIP Extract injected metadata --- internal/install/application_configuration.go | 5 -- internal/kibana/injected_metadata.go | 55 +++++++++++++++++++ internal/kibana/injected_metadata_test.go | 36 ++++++++++++ .../system/servicedeployer/kubernetes.go | 19 ++++++- 4 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 internal/kibana/injected_metadata.go create mode 100644 internal/kibana/injected_metadata_test.go 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..3fc2b094fd --- /dev/null +++ b/internal/kibana/injected_metadata.go @@ -0,0 +1,55 @@ +// 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" + "net/http" + + "github.com/pkg/errors" +) + +// InjectedMetadata represents the Kibana metadata structure exposed in the web UI. +type InjectedMetadata struct { + // Stack version + Version string `json:"version"` +} + +// InjectedMetadata method returns the Kibana metadata. The metadata is always present, even if the client is +// unauthorized. +func (c *Client) InjectedMetadata() (*InjectedMetadata, error) { + statusCode, respBody, err := c.get("/login") + if err != nil { + return nil, errors.Wrap(err, "could not reach login endpoint") + } + if statusCode != http.StatusOK { + return nil, 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 nil, errors.Wrap(err, "can't extract injected metadata") + } + return im, 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) { + return nil, nil // TODO +} 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