Skip to content

Commit fb3a7a3

Browse files
monicasarbutsg
authored andcommitted
- Set by the default the credentials for connecting to Kibana the same as for Elasticsearch (#4867)
- Raise an error in case there are no dashboards to be imported
1 parent 8bc9c80 commit fb3a7a3

4 files changed

Lines changed: 56 additions & 45 deletions

File tree

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta1...master[Check the HEAD di
6565
*Affecting all Beats*
6666

6767
- Update init scripts to use the `test config` subcommand instead of the deprecated `-configtest` flag. {issue}4600[4600]
68+
- Get by default the credentials for connecting to Kibana from the Elasticsearch output configuration. {pull}4867[4867]
6869

6970
*Auditbeat*
7071

libbeat/dashboards/dashboards.go

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,40 @@ func ImportDashboards(beatName, beatVersion, homePath string,
2929
return err
3030
}
3131

32-
if esConfig != nil {
33-
status, err := ImportDashboardsViaElasticsearch(esConfig, &dashConfig, msgOutputter)
34-
if err != nil {
35-
return err
36-
}
37-
if status {
38-
// the dashboards were imported via Elasticsearch
39-
return nil
40-
}
41-
}
42-
43-
err = ImportDashboardsViaKibana(kibanaConfig, &dashConfig, msgOutputter)
32+
esLoader, err := NewElasticsearchLoader(esConfig, &dashConfig, msgOutputter)
4433
if err != nil {
45-
return err
34+
return fmt.Errorf("fail to create the Elasticsearch loader: %v", err)
4635
}
36+
defer esLoader.Close()
4737

48-
return nil
49-
}
38+
esLoader.statusMsg("Elasticsearch URL %v", esLoader.client.Connection.URL)
5039

51-
func ImportDashboardsViaKibana(config *common.Config, dashConfig *Config, msgOutputter MessageOutputter) error {
52-
if config == nil {
53-
config = common.NewConfig()
40+
majorVersion, _, err := getMajorAndMinorVersion(esLoader.version)
41+
if err != nil {
42+
return fmt.Errorf("wrong Elasticsearch version: %v", err)
5443
}
55-
if !config.Enabled() {
56-
return nil
44+
45+
if majorVersion < 6 {
46+
return ImportDashboardsViaElasticsearch(esLoader)
47+
}
48+
49+
logp.Info("For Elasticsearch version >= 6.0.0, the Kibana dashboards need to be imported via the Kibana API.")
50+
51+
if kibanaConfig == nil {
52+
kibanaConfig = common.NewConfig()
5753
}
5854

59-
kibanaLoader, err := NewKibanaLoader(config, dashConfig, msgOutputter)
55+
// In Cloud, the Kibana URL is different than the Elasticsearch URL,
56+
// but the credentials are the same.
57+
// So, by default, use same credentials for connecting to Kibana as to Elasticsearch
58+
if !kibanaConfig.HasField("username") && len(esLoader.client.Username) > 0 {
59+
kibanaConfig.SetString("username", -1, esLoader.client.Username)
60+
}
61+
if !kibanaConfig.HasField("password") && len(esLoader.client.Password) > 0 {
62+
kibanaConfig.SetString("password", -1, esLoader.client.Password)
63+
}
64+
65+
kibanaLoader, err := NewKibanaLoader(kibanaConfig, &dashConfig, msgOutputter)
6066
if err != nil {
6167
return fmt.Errorf("fail to create the Kibana loader: %v", err)
6268
}
@@ -65,11 +71,16 @@ func ImportDashboardsViaKibana(config *common.Config, dashConfig *Config, msgOut
6571

6672
kibanaLoader.statusMsg("Kibana URL %v", kibanaLoader.client.Connection.URL)
6773

74+
return ImportDashboardsViaKibana(kibanaLoader)
75+
}
76+
77+
func ImportDashboardsViaKibana(kibanaLoader *KibanaLoader) error {
78+
6879
if !isKibanaAPIavailable(kibanaLoader.version) {
6980
return fmt.Errorf("Kibana API is not available in Kibana version %s", kibanaLoader.version)
7081
}
7182

72-
importer, err := NewImporter("default", dashConfig, *kibanaLoader)
83+
importer, err := NewImporter("default", kibanaLoader.config, kibanaLoader)
7384
if err != nil {
7485
return fmt.Errorf("fail to create a Kibana importer for loading the dashboards: %v", err)
7586
}
@@ -81,39 +92,22 @@ func ImportDashboardsViaKibana(config *common.Config, dashConfig *Config, msgOut
8192
return nil
8293
}
8394

84-
func ImportDashboardsViaElasticsearch(config *common.Config, dashConfig *Config, msgOutputter MessageOutputter) (bool, error) {
85-
esLoader, err := NewElasticsearchLoader(config, dashConfig, msgOutputter)
86-
if err != nil {
87-
return false, fmt.Errorf("fail to create the Elasticsearch loader: %v", err)
88-
}
89-
defer esLoader.Close()
90-
91-
esLoader.statusMsg("Elasticsearch URL %v", esLoader.client.Connection.URL)
92-
93-
majorVersion, _, err := getMajorAndMinorVersion(esLoader.version)
94-
if err != nil {
95-
return false, fmt.Errorf("wrong Elasticsearch version: %v", err)
96-
}
97-
98-
if majorVersion >= 6 {
99-
logp.Info("For Elasticsearch version >= 6.0.0, the Kibana dashboards need to be imported via the Kibana API.")
100-
return false, nil
101-
}
95+
func ImportDashboardsViaElasticsearch(esLoader *ElasticsearchLoader) error {
10296

10397
if err := esLoader.CreateKibanaIndex(); err != nil {
104-
return false, fmt.Errorf("fail to create the kibana index: %v", err)
98+
return fmt.Errorf("fail to create the kibana index: %v", err)
10599
}
106100

107-
importer, err := NewImporter("5.x", dashConfig, *esLoader)
101+
importer, err := NewImporter("5.x", esLoader.config, esLoader)
108102
if err != nil {
109-
return false, fmt.Errorf("fail to create an Elasticsearch importer for loading the dashboards: %v", err)
103+
return fmt.Errorf("fail to create an Elasticsearch importer for loading the dashboards: %v", err)
110104
}
111105

112106
if err := importer.Import(); err != nil {
113-
return false, fmt.Errorf("fail to import the dashboards in Elasticsearch: %v", err)
107+
return fmt.Errorf("fail to import the dashboards in Elasticsearch: %v", err)
114108
}
115109

116-
return true, nil
110+
return nil
117111
}
118112

119113
func getMajorAndMinorVersion(version string) (int, int, error) {

libbeat/dashboards/importer.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,22 @@ func (imp Importer) ImportKibanaDir(dir string) error {
272272
return fmt.Errorf("The directory %s does not contain the %s subdirectory."+
273273
" There is nothing to import into Kibana.", dir, strings.Join(check, " or "))
274274
}
275+
276+
importDashboards := false
275277
for _, t := range types {
276278
err = imp.ImportDir(t, dir)
277279
if err != nil {
278280
return fmt.Errorf("Failed to import %s: %v", t, err)
279281
}
282+
283+
if t == "dashboard" {
284+
importDashboards = true
285+
}
286+
}
287+
288+
if !importDashboards {
289+
return fmt.Errorf("No dashboards to import. Please make sure the %s directory contains a dashboard directory.",
290+
dir)
280291
}
281292
return nil
282293
}

libbeat/dashboards/kibana_loader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ type KibanaLoader struct {
2121
}
2222

2323
func NewKibanaLoader(cfg *common.Config, dashboardsConfig *Config, msgOutputter MessageOutputter) (*KibanaLoader, error) {
24+
25+
if cfg == nil || !cfg.Enabled() {
26+
return nil, fmt.Errorf("Kibana is not configured or enabled")
27+
}
28+
2429
client, err := kibana.NewKibanaClient(cfg)
2530
if err != nil {
2631
return nil, fmt.Errorf("Error creating Kibana client: %v", err)

0 commit comments

Comments
 (0)