From 186250db9b189fbd1759db50292af4ae04fe7592 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 2 Sep 2026 11:53:26 -0700 Subject: [PATCH 1/2] config: support reading bitcoin rpc password from a file Add a --bitcoin.passwordfile flag as an alternative to --bitcoin.password, so operators can avoid putting the bitcoind/btcd RPC password in plaintext on the command line or in shell history. Fixes #252. --- README.md | 8 ++++ chain/client.go | 1 + config.go | 25 ++++++++++++ config_test.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 config_test.go diff --git a/README.md b/README.md index d6dd8070..cdb99d79 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,14 @@ 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: +```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 diff --git a/chain/client.go b/chain/client.go index ab645bdb..fa5a04bc 100644 --- a/chain/client.go +++ b/chain/client.go @@ -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"` diff --git a/config.go b/config.go index c8002c44..143399a5 100644 --- a/config.go +++ b/config.go @@ -8,6 +8,7 @@ import ( "os" "path" "path/filepath" + "strings" "time" "github.com/btcsuite/btcd/btcutil" @@ -309,6 +310,30 @@ 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.User == "" || config.Bitcoin.Password == "" { return fmt.Errorf("rpc user and password " + "required when chainconn is set") diff --git a/config_test.go b/config_test.go new file mode 100644 index 00000000..b059a059 --- /dev/null +++ b/config_test.go @@ -0,0 +1,103 @@ +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) + + 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 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) + }) + } +} From 6c5748d3b0d600bde4e84bdd111d29f5daaf7045 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 2 Sep 2026 11:57:44 -0700 Subject: [PATCH 2/2] config: reject an empty bitcoin.passwordfile An empty or whitespace-only password file previously fell through to the generic 'rpc user and password required' error, which is misleading when the operator did pass --bitcoin.passwordfile. Fail with a message pointing at the file instead. Also document that surrounding whitespace and the trailing newline are stripped from the file's contents. --- README.md | 3 ++- config.go | 8 +++++--- config_test.go | 11 +++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cdb99d79..862db37a 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,8 @@ To connect Faraday to btcd: 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: +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} ``` diff --git a/config.go b/config.go index 143399a5..feaaee88 100644 --- a/config.go +++ b/config.go @@ -329,9 +329,11 @@ func ValidateConfig(config *Config) error { "bitcoin.passwordfile: %v", err) } - config.Bitcoin.Password = strings.TrimSpace( - string(pw), - ) + 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 == "" { diff --git a/config_test.go b/config_test.go index b059a059..a7783e48 100644 --- a/config_test.go +++ b/config_test.go @@ -37,6 +37,10 @@ func TestValidateConfigBitcoinPassword(t *testing.T) { 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) @@ -66,6 +70,13 @@ func TestValidateConfigBitcoinPassword(t *testing.T) { }, 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) {