diff --git a/cmd/export.go b/cmd/export.go index abe05e2d01..71bf9a2cad 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -32,6 +32,7 @@ func setupExportCommand() *cobraext.Command { } exportDashboardCmd.Flags().StringSliceP(cobraext.DashboardIDsFlagName, "d", nil, cobraext.DashboardIDsFlagDescription) exportDashboardCmd.Flags().Bool(cobraext.TLSSkipVerifyFlagName, false, cobraext.TLSSkipVerifyFlagDescription) + exportDashboardCmd.Flags().Bool(cobraext.AllowSnapshotFlagName, false, cobraext.AllowSnapshotDescription) cmd := &cobra.Command{ Use: "export", @@ -59,11 +60,29 @@ func exportDashboardsCmd(cmd *cobra.Command, args []string) error { opts = append(opts, kibana.TLSSkipVerify()) } + allowSnapshot, _ := cmd.Flags().GetBool(cobraext.AllowSnapshotFlagName) + if err != nil { + return cobraext.FlagParsingError(err, cobraext.AllowSnapshotFlagName) + } + kibanaClient, err := kibana.NewClient(opts...) if err != nil { return errors.Wrap(err, "can't create Kibana client") } + kibanaVersion, err := kibanaClient.Version() + if err != nil { + return errors.Wrap(err, "can't get Kibana status information") + } + + if kibanaVersion.IsSnapshot() { + message := fmt.Sprintf("exporting dashboards from a SNAPSHOT version of Kibana (%s) is discouraged. It could lead to invalid dashboards (for example if they use features that are reverted or modified before the final release)", kibanaVersion.Version()) + if !allowSnapshot { + return errors.Errorf("%s. --%s flag can be used to ignore this error", message, cobraext.AllowSnapshotFlagName) + } + fmt.Printf("Warning: %s\n", message) + } + if len(dashboardIDs) == 0 { dashboardIDs, err = promptDashboardIDs(kibanaClient) if err != nil { diff --git a/internal/cobraext/flags.go b/internal/cobraext/flags.go index 0a19277119..22d4244750 100644 --- a/internal/cobraext/flags.go +++ b/internal/cobraext/flags.go @@ -26,6 +26,9 @@ const ( AgentPolicyFlagName = "agent-policy" AgentPolicyDescription = "name of the agent policy" + AllowSnapshotFlagName = "allow-snapshot" + AllowSnapshotDescription = "allow to export dashboards from a Elastic stack SNAPSHOT version" + BenchNumTopProcsFlagName = "num-top-procs" BenchNumTopProcsFlagDescription = "number of top processors to show in the benchmarks results" diff --git a/internal/kibana/injected_metadata.go b/internal/kibana/injected_metadata.go deleted file mode 100644 index 3b3f6e99a4..0000000000 --- a/internal/kibana/injected_metadata.go +++ /dev/null @@ -1,62 +0,0 @@ -// 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 deleted file mode 100644 index d08dbd7cde..0000000000 --- a/internal/kibana/injected_metadata_test.go +++ /dev/null @@ -1,36 +0,0 @@ -// 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