Skip to content

Commit

Permalink
- Set by the default the credentials for connecting to Kibana the sam…
Browse files Browse the repository at this point in the history
…e as for Elasticsearch (elastic#4867)

- Raise an error in case there are no dashboards to be imported
  • Loading branch information
monicasarbu authored and tsg committed Aug 14, 2017
1 parent 3f60cec commit a7b5064
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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*

Expand Down
84 changes: 39 additions & 45 deletions libbeat/dashboards/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions libbeat/dashboards/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions libbeat/dashboards/kibana_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a7b5064

Please sign in to comment.