diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 4299acecf0f..28aff7008f5 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -60,6 +60,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta1...master[Check the HEAD di *Affecting all Beats* - Update init scripts to use the `test config` subcommand instead of the deprecated `-configtest` flag. {issue}4600[4600] +- Get by default the credentials for connecting to Kibana from the Elasticsearch output configuration. {pull}4867[4867] *Auditbeat* diff --git a/libbeat/dashboards/dashboards.go b/libbeat/dashboards/dashboards.go index f40465a79cf..080885deae1 100644 --- a/libbeat/dashboards/dashboards.go +++ b/libbeat/dashboards/dashboards.go @@ -29,34 +29,40 @@ func ImportDashboards(beatName, beatVersion, homePath string, return err } - if esConfig != nil { - status, err := ImportDashboardsViaElasticsearch(esConfig, &dashConfig, msgOutputter) - if err != nil { - return err - } - if status { - // the dashboards were imported via Elasticsearch - return nil - } - } - - err = ImportDashboardsViaKibana(kibanaConfig, &dashConfig, msgOutputter) + esLoader, err := NewElasticsearchLoader(esConfig, &dashConfig, msgOutputter) if err != nil { - return err + return fmt.Errorf("fail to create the Elasticsearch loader: %v", err) } + defer esLoader.Close() - return nil -} + esLoader.statusMsg("Elasticsearch URL %v", esLoader.client.Connection.URL) -func ImportDashboardsViaKibana(config *common.Config, dashConfig *Config, msgOutputter MessageOutputter) error { - if config == nil { - config = common.NewConfig() + majorVersion, _, err := getMajorAndMinorVersion(esLoader.version) + if err != nil { + return fmt.Errorf("wrong Elasticsearch version: %v", err) } - if !config.Enabled() { - return nil + + if majorVersion < 6 { + return ImportDashboardsViaElasticsearch(esLoader) + } + + logp.Info("For Elasticsearch version >= 6.0.0, the Kibana dashboards need to be imported via the Kibana API.") + + if kibanaConfig == nil { + kibanaConfig = common.NewConfig() } - kibanaLoader, err := NewKibanaLoader(config, dashConfig, msgOutputter) + // In Cloud, the Kibana URL is different than the Elasticsearch URL, + // but the credentials are the same. + // So, by default, use same credentials for connecting to Kibana as to Elasticsearch + if !kibanaConfig.HasField("username") && len(esLoader.client.Username) > 0 { + kibanaConfig.SetString("username", -1, esLoader.client.Username) + } + if !kibanaConfig.HasField("password") && len(esLoader.client.Password) > 0 { + kibanaConfig.SetString("password", -1, esLoader.client.Password) + } + + kibanaLoader, err := NewKibanaLoader(kibanaConfig, &dashConfig, msgOutputter) if err != nil { return fmt.Errorf("fail to create the Kibana loader: %v", err) } @@ -65,11 +71,16 @@ func ImportDashboardsViaKibana(config *common.Config, dashConfig *Config, msgOut kibanaLoader.statusMsg("Kibana URL %v", kibanaLoader.client.Connection.URL) + return ImportDashboardsViaKibana(kibanaLoader) +} + +func ImportDashboardsViaKibana(kibanaLoader *KibanaLoader) error { + if !isKibanaAPIavailable(kibanaLoader.version) { return fmt.Errorf("Kibana API is not available in Kibana version %s", kibanaLoader.version) } - importer, err := NewImporter("default", dashConfig, *kibanaLoader) + importer, err := NewImporter("default", kibanaLoader.config, kibanaLoader) if err != nil { return fmt.Errorf("fail to create a Kibana importer for loading the dashboards: %v", err) } @@ -81,39 +92,22 @@ func ImportDashboardsViaKibana(config *common.Config, dashConfig *Config, msgOut return nil } -func ImportDashboardsViaElasticsearch(config *common.Config, dashConfig *Config, msgOutputter MessageOutputter) (bool, error) { - esLoader, err := NewElasticsearchLoader(config, dashConfig, msgOutputter) - if err != nil { - return false, fmt.Errorf("fail to create the Elasticsearch loader: %v", err) - } - defer esLoader.Close() - - esLoader.statusMsg("Elasticsearch URL %v", esLoader.client.Connection.URL) - - majorVersion, _, err := getMajorAndMinorVersion(esLoader.version) - if err != nil { - return false, fmt.Errorf("wrong Elasticsearch version: %v", err) - } - - if majorVersion >= 6 { - logp.Info("For Elasticsearch version >= 6.0.0, the Kibana dashboards need to be imported via the Kibana API.") - return false, nil - } +func ImportDashboardsViaElasticsearch(esLoader *ElasticsearchLoader) error { if err := esLoader.CreateKibanaIndex(); err != nil { - return false, fmt.Errorf("fail to create the kibana index: %v", err) + return fmt.Errorf("fail to create the kibana index: %v", err) } - importer, err := NewImporter("5.x", dashConfig, *esLoader) + importer, err := NewImporter("5.x", esLoader.config, esLoader) if err != nil { - return false, fmt.Errorf("fail to create an Elasticsearch importer for loading the dashboards: %v", err) + return fmt.Errorf("fail to create an Elasticsearch importer for loading the dashboards: %v", err) } if err := importer.Import(); err != nil { - return false, fmt.Errorf("fail to import the dashboards in Elasticsearch: %v", err) + return fmt.Errorf("fail to import the dashboards in Elasticsearch: %v", err) } - return true, nil + return nil } func getMajorAndMinorVersion(version string) (int, int, error) { diff --git a/libbeat/dashboards/importer.go b/libbeat/dashboards/importer.go index 4de3b32d4ab..6d09da5f586 100644 --- a/libbeat/dashboards/importer.go +++ b/libbeat/dashboards/importer.go @@ -272,11 +272,22 @@ func (imp Importer) ImportKibanaDir(dir string) error { return fmt.Errorf("The directory %s does not contain the %s subdirectory."+ " There is nothing to import into Kibana.", dir, strings.Join(check, " or ")) } + + importDashboards := false for _, t := range types { err = imp.ImportDir(t, dir) if err != nil { return fmt.Errorf("Failed to import %s: %v", t, err) } + + if t == "dashboard" { + importDashboards = true + } + } + + if !importDashboards { + return fmt.Errorf("No dashboards to import. Please make sure the %s directory contains a dashboard directory.", + dir) } return nil } diff --git a/libbeat/dashboards/kibana_loader.go b/libbeat/dashboards/kibana_loader.go index 952f927ee3e..882a606d28f 100644 --- a/libbeat/dashboards/kibana_loader.go +++ b/libbeat/dashboards/kibana_loader.go @@ -21,6 +21,11 @@ type KibanaLoader struct { } func NewKibanaLoader(cfg *common.Config, dashboardsConfig *Config, msgOutputter MessageOutputter) (*KibanaLoader, error) { + + if cfg == nil || !cfg.Enabled() { + return nil, fmt.Errorf("Kibana is not configured or enabled") + } + client, err := kibana.NewKibanaClient(cfg) if err != nil { return nil, fmt.Errorf("Error creating Kibana client: %v", err)