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

Pass the certificate as a parameter to the import_dashboards script #3139

Merged
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
60 changes: 48 additions & 12 deletions libbeat/dashboards/import_dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
lbeat "github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/fmtstr"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/libbeat/outputs/elasticsearch"
"github.com/elastic/beats/libbeat/outputs/outil"
"github.com/elastic/beats/libbeat/outputs/transport"
)

var usage = fmt.Sprintf(`
Expand All @@ -42,18 +44,21 @@ For more details, check https://www.elastic.co/guide/en/beats/libbeat/5.0/import
var beat string

type Options struct {
KibanaIndex string
ES string
Index string
Dir string
File string
Beat string
URL string
User string
Pass string
OnlyDashboards bool
OnlyIndex bool
Snapshot bool
KibanaIndex string
ES string
Index string
Dir string
File string
Beat string
URL string
User string
Pass string
Certificate string
CertificateKey string
CertificateAuthority string
OnlyDashboards bool
OnlyIndex bool
Snapshot bool
}

type CommandLine struct {
Expand Down Expand Up @@ -91,6 +96,9 @@ func DefineCommandLine() (*CommandLine, error) {
cl.flagSet.BoolVar(&cl.opt.OnlyDashboards, "only-dashboards", false, "Import only dashboards together with visualizations and searches. By default import both, dashboards and the index-pattern.")
cl.flagSet.BoolVar(&cl.opt.OnlyIndex, "only-index", false, "Import only the index-pattern. By default imports both, dashboards and the index pattern.")
cl.flagSet.BoolVar(&cl.opt.Snapshot, "snapshot", false, "Import dashboards from snapshot builds.")
cl.flagSet.StringVar(&cl.opt.CertificateAuthority, "cacert", "", "Certificate Authority for server verification")
cl.flagSet.StringVar(&cl.opt.Certificate, "cert", "", "Certificate for SSL client authentication in PEM format.")
cl.flagSet.StringVar(&cl.opt.CertificateKey, "key", "", "Client Certificate Key in PEM format.")

return &cl, nil
}
Expand All @@ -107,6 +115,14 @@ func (cl *CommandLine) ParseCommandLine() error {
return errors.New("ERROR: Missing input. Please specify one of the options -file, -url or -dir")
}

if cl.opt.Certificate != "" && cl.opt.CertificateKey == "" {
return errors.New("ERROR: A certificate key needs to be passed as well by using the -key option.")
}

if cl.opt.CertificateKey != "" && cl.opt.Certificate == "" {
return errors.New("ERROR: A certificate needs to be passed as well by using the -cert option.")
}

return nil
}

Expand All @@ -133,11 +149,31 @@ func New() (*Importer, error) {
}
indexSel := outil.MakeSelector(outil.FmtSelectorExpr(fmtstr, ""))

var tlsConfig outputs.TLSConfig
var tls *transport.TLSConfig

if len(cl.opt.Certificate) > 0 && len(cl.opt.CertificateKey) > 0 {
tlsConfig.Certificate = outputs.CertificateConfig{
Certificate: cl.opt.Certificate,
Key: cl.opt.CertificateKey,
}
}

if len(cl.opt.CertificateAuthority) > 0 {
tlsConfig.CAs = []string{cl.opt.CertificateAuthority}
}

tls, err = outputs.LoadTLSConfig(&tlsConfig)
if err != nil {
return nil, fmt.Errorf("fail to load the SSL certificate: %s", err)
}

/* connect to Elasticsearch */
client, err := elasticsearch.NewClient(
elasticsearch.ClientSettings{
URL: cl.opt.ES,
Index: indexSel,
TLS: tls,
Username: cl.opt.User,
Password: cl.opt.Pass,
Timeout: 60 * time.Second,
Expand Down