From 58ab6c1912a1fcb78e06fdc94f4cd5c41141a4a5 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Mon, 27 Aug 2018 22:20:16 -0700 Subject: [PATCH 1/2] config: ensure ZMQ options when read from the config file are not equal --- config.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index 32a5403d..82ca1bf9 100644 --- a/config.go +++ b/config.go @@ -1076,6 +1076,17 @@ func parseRPCParams(cConfig *chainConfig, nodeConfig interface{}, net chainCode, } case *bitcoindConfig: + // Ensure that if the ZMQ options are set, that they are not + // equal. + if conf.ZMQPubRawBlock != "" && conf.ZMQPubRawTx != "" { + err := checkZMQOptions( + conf.ZMQPubRawBlock, conf.ZMQPubRawTx, + ) + if err != nil { + return err + } + } + // If all of RPCUser, RPCPass, ZMQBlockHost, and ZMQTxHost are // set, we assume those parameters are good to use. if conf.RPCUser != "" && conf.RPCPass != "" && @@ -1095,8 +1106,8 @@ func parseRPCParams(cConfig *chainConfig, nodeConfig interface{}, net chainCode, confFile = "litecoin" } - // If only one or two of the parameters are set, we assume the - // user did that unintentionally. + // If not all of the parameters are set, we'll assume the user + // did this unintentionally. if conf.RPCUser != "" || conf.RPCPass != "" || conf.ZMQPubRawBlock != "" || conf.ZMQPubRawTx != "" { @@ -1237,9 +1248,8 @@ func extractBitcoindRPCParams(bitcoindConfigPath string) (string, string, string } zmqBlockHost := string(zmqBlockHostSubmatches[1]) zmqTxHost := string(zmqTxHostSubmatches[1]) - if zmqBlockHost == zmqTxHost { - return "", "", "", "", errors.New("zmqpubrawblock and " + - "zmqpubrawtx must be different") + if err := checkZMQOptions(zmqBlockHost, zmqTxHost); err != nil { + return "", "", "", "", err } // Next, we'll try to find an auth cookie. We need to detect the chain @@ -1303,6 +1313,17 @@ func extractBitcoindRPCParams(bitcoindConfigPath string) (string, string, string zmqBlockHost, zmqTxHost, nil } +// checkZMQOptions ensures that the provided addresses to use as the hosts for +// ZMQ rawblock and rawtx notifications are different. +func checkZMQOptions(zmqBlockHost, zmqTxHost string) error { + if zmqBlockHost == zmqTxHost { + return errors.New("zmqpubrawblock and zmqpubrawtx must be set" + + "to different addresses") + } + + return nil +} + // normalizeNetwork returns the common name of a network type used to create // file paths. This allows differently versioned networks to use the same path. func normalizeNetwork(network string) string { From fd61c3c9b0ab182b2dd09479fb11cfb279b6f137 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Mon, 27 Aug 2018 22:21:01 -0700 Subject: [PATCH 2/2] config: defer creating the base lnd dir until all flag parsing is done In this commit, we defer creating the base lnd directory until all flag parsing is done. We do this as it's possible that the config file specifies a lnddir, but it isn't actually used as the directory has already been created. --- config.go | 60 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/config.go b/config.go index 82ca1bf9..94bae019 100644 --- a/config.go +++ b/config.go @@ -325,23 +325,42 @@ func loadConfig() (*config, error) { os.Exit(0) } + // If the config file path has not been modified by the user, then we'll + // use the default config file path. However, if the user has modified + // their lnddir, then we should assume they intend to use the config + // file within it. + configFileDir := cleanAndExpandPath(preCfg.LndDir) + configFilePath := cleanAndExpandPath(preCfg.ConfigFile) + if configFileDir != defaultLndDir { + if configFilePath == defaultConfigFile { + configFilePath = filepath.Join( + configFileDir, defaultConfigFilename, + ) + } + } + + // Next, load any additional configuration options from the file. + var configFileError error + cfg := preCfg + if err := flags.IniParse(configFilePath, &cfg); err != nil { + configFileError = err + } + + // Finally, parse the remaining command line options again to ensure + // they take precedence. + if _, err := flags.Parse(&cfg); err != nil { + return nil, err + } + // If the provided lnd directory is not the default, we'll modify the // path to all of the files and directories that will live within it. - lndDir := cleanAndExpandPath(preCfg.LndDir) - configFilePath := cleanAndExpandPath(preCfg.ConfigFile) + lndDir := cleanAndExpandPath(cfg.LndDir) if lndDir != defaultLndDir { - // If the config file path has not been modified by the user, - // then we'll use the default config file path. However, if the - // user has modified their lnddir, then we should assume they - // intend to use the config file within it. - if configFilePath == defaultConfigFile { - preCfg.ConfigFile = filepath.Join(lndDir, defaultConfigFilename) - } - preCfg.DataDir = filepath.Join(lndDir, defaultDataDirname) - preCfg.TLSCertPath = filepath.Join(lndDir, defaultTLSCertFilename) - preCfg.TLSKeyPath = filepath.Join(lndDir, defaultTLSKeyFilename) - preCfg.LogDir = filepath.Join(lndDir, defaultLogDirname) - preCfg.Tor.V2PrivateKeyPath = filepath.Join(lndDir, defaultTorV2PrivateKeyFilename) + cfg.DataDir = filepath.Join(lndDir, defaultDataDirname) + cfg.TLSCertPath = filepath.Join(lndDir, defaultTLSCertFilename) + cfg.TLSKeyPath = filepath.Join(lndDir, defaultTLSKeyFilename) + cfg.LogDir = filepath.Join(lndDir, defaultLogDirname) + cfg.Tor.V2PrivateKeyPath = filepath.Join(lndDir, defaultTorV2PrivateKeyFilename) } // Create the lnd directory if it doesn't already exist. @@ -363,19 +382,6 @@ func loadConfig() (*config, error) { return nil, err } - // Next, load any additional configuration options from the file. - var configFileError error - cfg := preCfg - if err := flags.IniParse(cfg.ConfigFile, &cfg); err != nil { - configFileError = err - } - - // Finally, parse the remaining command line options again to ensure - // they take precedence. - if _, err := flags.Parse(&cfg); err != nil { - return nil, err - } - // As soon as we're done parsing configuration options, ensure all paths // to directories and files are cleaned and expanded before attempting // to use them later on.