From 3b0176ff34288ab7bb93952aff610533c79df3a9 Mon Sep 17 00:00:00 2001 From: Ahmed Abdrabo Date: Tue, 3 Nov 2020 19:04:57 +0100 Subject: [PATCH] Add a config option to read password from a file Instead of hard coding it in the config file. --- cmd/collect.go | 12 +++++++++++- connectors/collection.go | 13 ++++++++++++- dora.yaml | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cmd/collect.go b/cmd/collect.go index e5c746c6..8ba85c6e 100644 --- a/cmd/collect.go +++ b/cmd/collect.go @@ -38,7 +38,6 @@ usage: dora collect `, Run: func(cmd *cobra.Command, args []string) { configItems := []string{ - "bmc_pass", "bmc_user", "collector.concurrency", "collector.dump_invalid_payloads", @@ -56,6 +55,17 @@ usage: dora collect } } + bmcPass := viper.IsSet("bmc_pass") + bmcPassFile := viper.IsSet("bmc_pass_file") + if !bmcPass && !bmcPassFile { + fmt.Printf("One of the bmc_pass/bmc_pass_file parameters is missing in the config file\n") + os.Exit(1) + } + if bmcPass && bmcPassFile { + fmt.Printf("Only one of the bmc_pass/bmc_pass_file parameters is allowed in the config file\n") + os.Exit(1) + } + // This will avoid a deadlock in metrics. They are not setup at this stage viper.Set("metrics.enabled", false) diff --git a/connectors/collection.go b/connectors/collection.go index 0c03af10..696cf892 100644 --- a/connectors/collection.go +++ b/connectors/collection.go @@ -2,6 +2,7 @@ package connectors import ( "fmt" + "io/ioutil" "net" "sync" "time" @@ -24,7 +25,17 @@ import ( func collect(input <-chan string, source *string, db *gorm.DB) { bmcUser := viper.GetString("bmc_user") - bmcPass := viper.GetString("bmc_pass") + bmcPass := "" + if viper.IsSet("bmc_pass") { + bmcPass = viper.GetString("bmc_pass") + } else { + bmcPassFile := viper.GetString("bmc_pass_file") + bmcPassBytes, err := ioutil.ReadFile(bmcPassFile) + if err != nil { + log.WithFields(log.Fields{"operation": "scan"}).Error(err) + } + bmcPass = string(bmcPassBytes) + } for host := range input { log.WithFields(log.Fields{"operation": "scan", "ip": host}).Debug("collection started") diff --git a/dora.yaml b/dora.yaml index bb9c6a9c..52c76b8b 100644 --- a/dora.yaml +++ b/dora.yaml @@ -2,7 +2,7 @@ debug: true site: - all bmc_user: Priest -bmc_pass: Wololo +bmc_pass_file: /my/password/file url: http://service.example.com/v1 database_type: postgres database_options: host=0.0.0.0 user=postgres port=32768 dbname=postgres password=mysecretpassword