From ce235cbc1dfd9de4677a6dc18d382082f734b8b8 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Wed, 9 Sep 2020 16:15:45 +0200 Subject: [PATCH] multi: extract config validation As a preparation to add more configuration paths for TLS, we first extract the config validation and path cleanup into its own function. --- cmd/poold/main.go | 12 ++++++----- config.go | 51 +++++++++++++++++++++++++++++++++++++++++++++-- run.go | 4 ---- server.go | 6 ++---- 4 files changed, 58 insertions(+), 15 deletions(-) diff --git a/cmd/poold/main.go b/cmd/poold/main.go index 68a24dd..2198cfd 100644 --- a/cmd/poold/main.go +++ b/cmd/poold/main.go @@ -43,12 +43,9 @@ func start() error { } // Parse ini file. - networkDir := filepath.Join(config.BaseDir, config.Network) - if err := os.MkdirAll(networkDir, os.ModePerm); err != nil { - return err - } + poolDir := filepath.Join(config.BaseDir, config.Network) + configFile := filepath.Join(poolDir, defaultConfigFilename) - configFile := filepath.Join(networkDir, defaultConfigFilename) if err := flags.IniParse(configFile, &config); err != nil { // If it's a parsing related error, then we'll return // immediately, otherwise we can proceed as possibly the config @@ -65,6 +62,11 @@ func start() error { return err } + // Make sure the passed configuration is valid. + if err := pool.Validate(&config); err != nil { + return err + } + // Enable http profiling and Validate profile port number if reqeusted. if config.Profile != "" { profilePort, err := strconv.Atoi(config.Profile) diff --git a/config.go b/config.go index 5cc7129..da23481 100644 --- a/config.go +++ b/config.go @@ -1,11 +1,14 @@ package pool import ( + "fmt" "net" + "os" "path/filepath" "time" "github.com/btcsuite/btcutil" + "github.com/lightningnetwork/lnd/lncfg" "google.golang.org/grpc" ) @@ -16,6 +19,9 @@ var ( // created. DefaultBaseDir = btcutil.AppDataDir("pool", false) + // DefaultNetwork is the default bitcoin network pool runs on. + DefaultNetwork = "mainnet" + // DefaultLogFilename is the default name that is given to the pool log // file. DefaultLogFilename = "poold.log" @@ -45,7 +51,7 @@ type Config struct { TLSPathAuctSrv string `long:"tlspathauctserver" description:"Path to auction server tls certificate"` RPCListen string `long:"rpclisten" description:"Address to listen on for gRPC clients"` RESTListen string `long:"restlisten" description:"Address to listen on for REST clients"` - BaseDir string `long:"basedir" description:"The base directory where pool stores all its data"` + BaseDir string `long:"basedir" description:"The base directory where pool stores all its data. If set, this option overwrites --logdir."` LogDir string `long:"logdir" description:"Directory to log output."` MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation)"` @@ -87,7 +93,7 @@ const ( // DefaultConfig returns the default value for the Config struct. func DefaultConfig() Config { return Config{ - Network: "mainnet", + Network: DefaultNetwork, RPCListen: "localhost:12010", RESTListen: "localhost:8281", Insecure: false, @@ -103,3 +109,44 @@ func DefaultConfig() Config { }, } } + +// Validate cleans up paths in the config provided and validates it. +func Validate(cfg *Config) error { + // Cleanup any paths before we use them. + cfg.BaseDir = lncfg.CleanAndExpandPath(cfg.BaseDir) + cfg.LogDir = lncfg.CleanAndExpandPath(cfg.LogDir) + + // Since our pool directory overrides our log dir value, make sure that + // they are not set when base dir is set. We hard here rather than + // overwriting and potentially confusing the user. + baseDirSet := cfg.BaseDir != DefaultBaseDir + + if baseDirSet { + logDirSet := cfg.LogDir != defaultLogDir + + if logDirSet { + return fmt.Errorf("basedir overwrites logdir, please " + + "only set one value") + } + + // Once we are satisfied that no other config value was set, we + // replace them with our pool dir. + cfg.LogDir = filepath.Join(cfg.BaseDir, defaultLogDirname) + } + + // Append the network type to the log and base directory so it is + // "namespaced" per network in the same fashion as the data directory. + cfg.LogDir = filepath.Join(cfg.LogDir, cfg.Network) + cfg.BaseDir = filepath.Join(cfg.BaseDir, cfg.Network) + + // If either of these directories do not exist, create them. + if err := os.MkdirAll(cfg.BaseDir, os.ModePerm); err != nil { + return err + } + + if err := os.MkdirAll(cfg.LogDir, os.ModePerm); err != nil { + return err + } + + return nil +} diff --git a/run.go b/run.go index 572ea18..b2dd4c5 100644 --- a/run.go +++ b/run.go @@ -10,10 +10,6 @@ import ( // Run starts the trader daemon and blocks until it's shut down again. func Run(cfg *Config) error { - // Append the network type to the log directory so it is - // "namespaced" per network in the same fashion as the data directory. - cfg.LogDir = filepath.Join(cfg.LogDir, cfg.Network) - // Initialize logging at the default logging level. err := logWriter.InitLogRotator( filepath.Join(cfg.LogDir, DefaultLogFilename), diff --git a/server.go b/server.go index a0f3bd8..3ba3d12 100644 --- a/server.go +++ b/server.go @@ -8,7 +8,6 @@ import ( "fmt" "net" "net/http" - "path/filepath" "sync" "sync/atomic" @@ -255,14 +254,13 @@ func (s *Server) setupClient() error { // Open the main database. var err error - networkDir := filepath.Join(s.cfg.BaseDir, s.cfg.Network) - s.db, err = clientdb.New(networkDir) + s.db, err = clientdb.New(s.cfg.BaseDir) if err != nil { return err } // Setup the LSAT interceptor for the client. - s.lsatStore, err = lsat.NewFileStore(networkDir) + s.lsatStore, err = lsat.NewFileStore(s.cfg.BaseDir) if err != nil { return err }