sermoni

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

config.go (2493B)


      1 package config
      2 
      3 import (
      4 	"crypto/sha256"
      5 	"log"
      6 	"sermoni/internal/database"
      7 
      8 	"github.com/gorilla/securecookie"
      9 	"go.etcd.io/bbolt"
     10 )
     11 
     12 var (
     13 	defaultPassPhrase = []byte("admin")
     14 	defaultPageTitle  = []byte("sermoni")
     15 
     16 	keyPassHash   = []byte("passhash")
     17 	keyPageTitle  = []byte("pagetitle")
     18 	keySessionKey = []byte("sessionkey") // Session key
     19 	keyCSRFKey    = []byte("csrfkey")    // CSRF protection auth key
     20 )
     21 
     22 // Config is a struct that contains all configuration parameters as []byte data
     23 type Config struct {
     24 	PassHash   []byte
     25 	PageTitle  []byte
     26 	SessionKey []byte
     27 	CSRFKey    []byte
     28 }
     29 
     30 // GetConfig Creates a Config struct from the values in database
     31 // Should only be necessary to call once
     32 func GetConfig() (config *Config) {
     33 	db := database.GetDB()
     34 	db.View(func(tx *bbolt.Tx) error {
     35 		b := tx.Bucket(database.BucketKeyConfig)
     36 		config = &Config{
     37 			PassHash:   b.Get(keyPassHash),
     38 			PageTitle:  b.Get(keyPageTitle),
     39 			SessionKey: b.Get(keySessionKey),
     40 			CSRFKey:    b.Get(keyCSRFKey),
     41 		}
     42 		return nil
     43 	})
     44 	return
     45 
     46 }
     47 
     48 // SetPassphrase persists the sha256sum of the given passphrase to DB
     49 func SetPassphrase(passphrase string) {
     50 	db := database.GetDB()
     51 	passhash := sha256.Sum256([]byte(passphrase))
     52 	db.Update(func(tx *bbolt.Tx) error {
     53 		var err error
     54 		b := tx.Bucket(database.BucketKeyConfig)
     55 		err = b.Put(keyPassHash, passhash[:])
     56 		check(err)
     57 		return nil
     58 	})
     59 }
     60 
     61 // SetPageTitle persists the given page title to DB
     62 func SetPageTitle(pageTitle string) {
     63 	db := database.GetDB()
     64 	db.Update(func(tx *bbolt.Tx) error {
     65 		var err error
     66 		b := tx.Bucket(database.BucketKeyConfig)
     67 		err = b.Put(keyPageTitle, []byte(pageTitle))
     68 		check(err)
     69 		return nil
     70 	})
     71 }
     72 
     73 // InitConfig populates the config root bucket with default configurations
     74 // (Web client) passphrase and page title can be reset later
     75 func InitConfig() {
     76 	db := database.GetDB()
     77 	// TODO: Generate a random _readable_ password if none is given
     78 	passhash := sha256.Sum256([]byte(defaultPassPhrase))
     79 	sessionKey := securecookie.GenerateRandomKey(32)
     80 	CSRFKey := securecookie.GenerateRandomKey(32)
     81 	db.Update(func(tx *bbolt.Tx) error {
     82 		var err error
     83 		b := tx.Bucket(database.BucketKeyConfig)
     84 		err = b.Put(keyPassHash, passhash[:])
     85 		check(err)
     86 		err = b.Put(keyPageTitle, defaultPageTitle)
     87 		check(err)
     88 		err = b.Put(keySessionKey, sessionKey)
     89 		check(err)
     90 		err = b.Put(keyCSRFKey, CSRFKey)
     91 		check(err)
     92 		return nil
     93 	})
     94 }
     95 
     96 // check for fatal errors
     97 func check(err error) {
     98 	if err != nil {
     99 		log.Fatal(err)
    100 	}
    101 }