sermoni

"Service monitor" / cronjob status service
Log | Files | Refs

commit 20b074689d0c7ddab1069b5eda6ef4ae50eb6e64
parent 0b81ee36fc23badd7030da5681c468c84079ff4a
Author: Vetle Haflan <vetle@haflan.dev>
Date:   Wed, 15 Apr 2020 23:17:19 +0200

Allow passphrase and page title config with flags -pass and -title

Page title is not implemented client-side, so config won't have
any effect yet

Diffstat:
Mcmd/sermoni/main.go | 8++++++++
Minternal/config/config.go | 39++++++++++++++++++++++++++-------------
2 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/cmd/sermoni/main.go b/cmd/sermoni/main.go @@ -12,6 +12,8 @@ import ( var ( port = flag.Int("p", 8080, "Port") dbFile = flag.String("d", "sermoni.db", "Database file") + passphrase = flag.String("pass", "", "Set passphrase") + pageTitle = flag.String("title", "", "Set page title") ) func main() { @@ -23,6 +25,12 @@ func main() { log.Printf("Setting up new database '%v'\n", *dbFile) config.InitConfig() } + if *passphrase != "" { + config.SetPassphrase(*passphrase) + } + if *pageTitle != "" { + config.SetPageTitle(*pageTitle) + } defer database.Close() log.Printf("Server started listening on port %v\n", *port) smhttp.StartServer(*port) diff --git a/internal/config/config.go b/internal/config/config.go @@ -45,23 +45,36 @@ func GetConfig() (config *Config) { } +// SetPassphrase persists the sha256sum of the given passphrase to DB +func SetPassphrase(passphrase string) { + db := database.GetDB() + passhash := sha256.Sum256([]byte(passphrase)) + db.Update(func(tx *bbolt.Tx) error { + var err error + b := tx.Bucket(database.BucketKeyConfig) + err = b.Put(keyPassHash, passhash[:]) + check(err) + return nil + }) +} + +// SetPageTitle persists the given page title to DB +func SetPageTitle(pageTitle string) { + db := database.GetDB() + db.Update(func(tx *bbolt.Tx) error { + var err error + b := tx.Bucket(database.BucketKeyConfig) + err = b.Put(keyPageTitle, []byte(pageTitle)) + check(err) + return nil + }) +} + // InitConfig populates the config root bucket with default configurations // (Web client) passphrase and page title can be reset later func InitConfig() { db := database.GetDB() - // TODO: Maybe this belongs elsewhere? - /* TODO: Generate a random _readable_ password if none is given - var passphraseBytes []byte - if passphrase == "" { - passphraseBytes = make([]byte, 24) - rand.Read(passphraseBytes) - passphrase = string(passphraseBytes) - fmt.Printf("Generated passphrase: %v\n", []rune(passphrase)) - } else { - passphraseBytes = []byte(passphrase) - }*/ - //sha256.Sum256([]byte(passphraseBytes)) - + // TODO: Generate a random _readable_ password if none is given passhash := sha256.Sum256([]byte(defaultPassPhrase)) sessionKey := securecookie.GenerateRandomKey(32) CSRFKey := securecookie.GenerateRandomKey(32)