Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ To connect Faraday to btcd:
--bitcoin.tlspath={path to btcd cert}
```

Instead of passing the password on the command line with `--bitcoin.password`,
it can be read from a file with `--bitcoin.passwordfile`, which keeps the
password out of the process list and shell history; surrounding whitespace
and the trailing newline are stripped:
```text
--bitcoin.passwordfile={path to a file containing the rpc password}
```
`--bitcoin.password` and `--bitcoin.passwordfile` are mutually exclusive.

#### RPCServer
Faraday serves requests over grpc by default on `localhost:8465`. This default can be overwritten:
```text
Expand Down
1 change: 1 addition & 0 deletions chain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type BitcoinConfig struct {
Host string `long:"host" description:"host:port of the bitcoind/btcd instance address"`
User string `long:"user" description:"bitcoind/btcd user name"`
Password string `long:"password" description:"bitcoind/btcd password"`
PasswordFile string `long:"passwordfile" description:"path to a file containing the bitcoind/btcd password, as an alternative to --bitcoin.password"`
HTTPPostMode bool `long:"httppostmode" description:"Use HTTP POST mode? bitcoind only supports this mode"`
UseTLS bool `long:"usetls" description:"Use TLS to connect? bitcoind only supports non-TLS connections"`
TLSPath string `long:"tlspath" description:"Path to btcd tls certificate, bitcoind only supports non-TLS connections"`
Expand Down
27 changes: 27 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path"
"path/filepath"
"strings"
"time"

"github.com/btcsuite/btcd/btcutil"
Expand Down Expand Up @@ -309,6 +310,32 @@ func ValidateConfig(config *Config) error {
// that we have a rpc user and password, and that tls path is set if
// required.
if config.ChainConn {
switch {
case config.Bitcoin.Password != "" &&
config.Bitcoin.PasswordFile != "":

return fmt.Errorf("bitcoin.password and " +
"bitcoin.passwordfile are mutually " +
"exclusive, please only set one")

case config.Bitcoin.PasswordFile != "":
pwPath := lncfg.CleanAndExpandPath(
config.Bitcoin.PasswordFile,
)

pw, err := os.ReadFile(pwPath)
if err != nil {
return fmt.Errorf("could not read "+
"bitcoin.passwordfile: %v", err)
}

config.Bitcoin.Password = strings.TrimSpace(string(pw))
if config.Bitcoin.Password == "" {
return fmt.Errorf("bitcoin.passwordfile %v "+
"is empty", pwPath)
}
}

if config.Bitcoin.User == "" || config.Bitcoin.Password == "" {
return fmt.Errorf("rpc user and password " +
"required when chainconn is set")
Expand Down
114 changes: 114 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package faraday

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

// newValidTestConfig returns a config that passes ValidateConfig other than
// the bitcoin backend settings, which are set up by the individual test
// cases below.
func newValidTestConfig(t *testing.T) Config {
cfg := DefaultConfig()
cfg.FaradayDir = t.TempDir()
cfg.ChainConn = true

// DefaultConfig points Bitcoin at the shared chain.DefaultConfig
// instance, so copy it before mutating to avoid leaking state
// across test cases.
bitcoinCfg := *cfg.Bitcoin
cfg.Bitcoin = &bitcoinCfg
cfg.Bitcoin.User = "user"

return cfg
}

// TestValidateConfigBitcoinPassword tests that ValidateConfig correctly
// resolves the bitcoind/btcd RPC password from either the plaintext
// --bitcoin.password flag or from a file referenced by
// --bitcoin.passwordfile, and rejects invalid combinations of the two.
func TestValidateConfigBitcoinPassword(t *testing.T) {
t.Parallel()

pwFile := filepath.Join(t.TempDir(), "pw.txt")
err := os.WriteFile(pwFile, []byte("filepassword\n"), 0644)
require.NoError(t, err)

emptyPwFile := filepath.Join(t.TempDir(), "empty-pw.txt")
err = os.WriteFile(emptyPwFile, []byte(" \n"), 0644)
require.NoError(t, err)

tests := []struct {
name string
setup func(cfg *Config)
expectedPw string
expectedErr string
}{
{
name: "plaintext password",
setup: func(cfg *Config) {
cfg.Bitcoin.Password = "plainpassword"
},
expectedPw: "plainpassword",
},
{
name: "password file",
setup: func(cfg *Config) {
cfg.Bitcoin.PasswordFile = pwFile
},
expectedPw: "filepassword",
},
{
name: "password file does not exist",
setup: func(cfg *Config) {
cfg.Bitcoin.PasswordFile = filepath.Join(
t.TempDir(), "missing.txt",
)
},
expectedErr: "could not read bitcoin.passwordfile",
},
{
name: "password file is empty",
setup: func(cfg *Config) {
cfg.Bitcoin.PasswordFile = emptyPwFile
},
expectedErr: "is empty",
},
{
name: "password and password file set",
setup: func(cfg *Config) {
cfg.Bitcoin.Password = "plainpassword"
cfg.Bitcoin.PasswordFile = pwFile
},
expectedErr: "mutually exclusive",
},
{
name: "neither password nor password file set",
setup: func(cfg *Config) {},
expectedErr: "rpc user and password required",
},
}

for _, test := range tests {
test := test

t.Run(test.name, func(t *testing.T) {
t.Parallel()

cfg := newValidTestConfig(t)
test.setup(&cfg)

err := ValidateConfig(&cfg)
if test.expectedErr != "" {
require.ErrorContains(t, err, test.expectedErr)
return
}

require.NoError(t, err)
require.Equal(t, test.expectedPw, cfg.Bitcoin.Password)
})
}
}
Loading