Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default credentials for Kibana #4867

Merged
merged 1 commit into from
Aug 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
@@ -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*

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