[NOD-428] Require RPC user and password, and do not create a default config file for btcctl if rpc login details were provided (#510)

* [NOD-428] Required RPC user and password, and do not create a default config file for btcctl if rpc login details were provided

* [NOD-428] Don't check rpc user and password if rpc is disabled

* [NOD-428] Fix error message
This commit is contained in:
Ori Newman 2019-12-03 11:18:28 +02:00 committed by Svarog
parent 1fea2a9421
commit bcd73012de
2 changed files with 32 additions and 7 deletions

View File

@ -7,6 +7,7 @@ package main
import (
"fmt"
"github.com/daglabs/btcd/config"
"github.com/pkg/errors"
"io/ioutil"
"net"
"os"
@ -198,13 +199,16 @@ func loadConfig() (*ConfigFlags, []string, error) {
os.Exit(0)
}
if _, err := os.Stat(preCfg.ConfigFile); os.IsNotExist(err) {
// Use config file for RPC server to create default btcctl config
serverConfigPath := filepath.Join(btcdHomeDir, "btcd.conf")
err := createDefaultConfigFile(preCfg.ConfigFile, serverConfigPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating a default config file: %s\n", err)
// If no rpc user and password were configured, create
// a btcctl default config file based on the rpc login
// details written in the RPC server configuration file
if preCfg.RPCUser == "" && preCfg.RPCPassword == "" {
if _, err := os.Stat(preCfg.ConfigFile); os.IsNotExist(err) {
serverConfigPath := filepath.Join(btcdHomeDir, "btcd.conf")
err := createDefaultConfigFile(preCfg.ConfigFile, serverConfigPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating a default config file: %s\n", err)
}
}
}
@ -250,6 +254,9 @@ func loadConfig() (*ConfigFlags, []string, error) {
func createDefaultConfigFile(destinationPath, serverConfigPath string) error {
// Read the RPC server config
serverConfigFile, err := os.Open(serverConfigPath)
if os.IsNotExist(err) {
return errors.Errorf("the RPC server configuration file could not be found at %s", serverConfigPath)
}
if err != nil {
return err
}

View File

@ -417,6 +417,24 @@ func loadConfig() (*Config, []string, error) {
return nil, nil, err
}
if !activeConfig.DisableRPC {
if activeConfig.RPCUser == "" {
str := "%s: rpcuser cannot be empty"
err := errors.Errorf(str, funcName)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
if activeConfig.RPCPass == "" {
str := "%s: rpcpass cannot be empty"
err := errors.Errorf(str, funcName)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
}
err = activeConfig.ResolveNetwork(parser)
if err != nil {
return nil, nil, err