depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

commit 43ac6ffc4186d64b039c27649af1b14c28e0b3d1
parent 1864e66bb6fe34de7e9777dc7c9e6cf2854c7f6b
Author: Antoine A <>
Date:   Tue, 18 Jan 2022 20:35:15 +0100

Handle bitcoin auth config

Diffstat:
Mbtc-wire/src/config.rs | 42++++++++++++++++++++++++++++++++++++------
Mbtc-wire/src/rpc.rs | 13+++++++++----
Mmakefile | 1+
Atest/btc/config.sh | 30++++++++++++++++++++++++++++++
Mtest/btc/conflict.sh | 4++--
Mtest/btc/fail.sh | 4++--
Mtest/btc/hell.sh | 4++--
Mtest/btc/lifetime.sh | 2+-
Mtest/btc/reconnect.sh | 4++--
Mtest/btc/reorg.sh | 4++--
Mtest/btc/stress.sh | 4++--
Mtest/btc/wire.sh | 4++--
Mtest/common.sh | 7+++++--
Atest/conf/bitcoin_auth.conf | 10++++++++++
Atest/conf/bitcoin_auth1.conf | 12++++++++++++
Atest/conf/bitcoin_auth2.conf | 10++++++++++
Atest/conf/bitcoin_auth3.conf | 9+++++++++
Atest/conf/bitcoin_auth4.conf | 9+++++++++
Atest/conf/bitcoin_auth5.conf | 10++++++++++
Mtest/gateway/api.sh | 2+-
20 files changed, 157 insertions(+), 28 deletions(-)

diff --git a/btc-wire/src/config.rs b/btc-wire/src/config.rs @@ -30,12 +30,20 @@ fn rpc_port(network: Network) -> u16 { } } +#[derive(Clone)] +pub enum BtcAuth { + Cookie, + Auth(String), +} + /// Bitcoin config relevant for btc-wire #[derive(Clone)] pub struct BitcoinConfig { pub network: Network, pub dir: PathBuf, pub addr: SocketAddr, + pub auth: BtcAuth, + pub cookie_path: String, } impl BitcoinConfig { @@ -43,25 +51,25 @@ impl BitcoinConfig { pub fn load(data_dir: impl AsRef<Path>) -> Result<Self, ini::Error> { let conf = ini::Ini::load_from_file(data_dir.as_ref().join("bitcoin.conf"))?; - let section = conf.general_section(); + let main = conf.general_section(); - if !section.contains_key("txindex") { + if !main.contains_key("txindex") { error!("btc_wire require a bitcoin core node running with 'txindex' option"); exit(1); } - let network = if let Some("1") = section.get("testnet") { + let network = if let Some("1") = main.get("testnet") { Network::Testnet - } else if let Some("1") = section.get("signet") { + } else if let Some("1") = main.get("signet") { Network::Signet - } else if let Some("1") = section.get("regtest") { + } else if let Some("1") = main.get("regtest") { Network::Regtest } else { Network::Bitcoin }; let section = match network { - Network::Bitcoin => Some(section), + Network::Bitcoin => Some(main), Network::Testnet => conf.section(Some("test")), Network::Signet => conf.section(Some("signet")), Network::Regtest => conf.section(Some("regtest")), @@ -81,10 +89,32 @@ impl BitcoinConfig { ([127, 0, 0, 1], port).into() }; + let auth = if let (Some(login), Some(pwd)) = ( + section.and_then(|s| s.get("rpcuser")), + section.and_then(|s| s.get("rpcpassword")), + ) { + BtcAuth::Auth(format!("{}:{}", login, pwd)) + } else if let (Some(login), Some(pwd)) = (main.get("rpcuser"), main.get("rpcpassword")) { + BtcAuth::Auth(format!("{}:{}", login, pwd)) + } else { + BtcAuth::Cookie + }; + + let cookie_path = if let Some(path) = section.and_then(|s| s.get("rpccookiefile")) { + path + } else if let Some(path) = main.get("rpccookiefile") { + path + } else { + ".cookie" + } + .to_string(); + Ok(Self { network, addr, dir: data_dir.as_ref().join(chain_dir(network)), + auth, + cookie_path, }) } } diff --git a/btc-wire/src/rpc.rs b/btc-wire/src/rpc.rs @@ -17,7 +17,7 @@ use std::{ time::Duration, }; -use crate::config::BitcoinConfig; +use crate::config::{BitcoinConfig, BtcAuth}; #[derive(Debug, serde::Serialize)] struct RpcRequest<'a, T: serde::Serialize> { @@ -86,8 +86,13 @@ impl BtcRpc { } else { String::from("/") }; - let cookie_path = config.dir.join(".cookie"); - let cookie = std::fs::read(cookie_path)?; + let token = match &config.auth { + BtcAuth::Cookie => { + let cookie_path = config.dir.join(&config.cookie_path); + std::fs::read(cookie_path)? + } + BtcAuth::Auth(s) => s.as_bytes().to_vec(), + }; // Open connection let sock = TcpStream::connect_timeout(&config.addr, Duration::from_secs(5))?; let conn = BufReader::new(sock); @@ -95,7 +100,7 @@ impl BtcRpc { Ok(Self { path, id: 0, - cookie: format!("Basic {}", base64::encode(&cookie)), + cookie: format!("Basic {}", base64::encode(token)), conn, }) } diff --git a/makefile b/makefile @@ -14,5 +14,6 @@ test_btc: test/btc/conflict.sh test/btc/reorg.sh test/btc/hell.sh + test/btc/config.sh test: install test_gateway test_btc \ No newline at end of file diff --git a/test/btc/config.sh b/test/btc/config.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +## Test btc_wire ability to configure itself from bitcoin configuration + +set -eu + +SCHEMA=btc.sql + +function test() { + echo "----- Config $1 -----" + + BTC_CONFIG=$1 + source "${BASH_SOURCE%/*}/../common.sh" + + echo "Load config file" + load_config + echo "Start database" + setup_db + echo "Start bitcoin node" + init_btc + echo "Cleanup" + cleanup +} + +for config in bitcoin_auth.conf bitcoin_auth1.conf bitcoin_auth2.conf bitcoin_auth3.conf bitcoin_auth4.conf bitcoin_auth5.conf; do + test $config +done + +echo "All tests passed!" +\ No newline at end of file diff --git a/test/btc/conflict.sh b/test/btc/conflict.sh @@ -111,4 +111,4 @@ mine_btc check_balance 9.99993744 0.00002000 echo " OK" -echo "All tests passed" -\ No newline at end of file +echo "All tests passed!" +\ No newline at end of file diff --git a/test/btc/fail.sh b/test/btc/fail.sh @@ -78,4 +78,4 @@ echo -n "Check balance:" check_balance "*" 0.00031000 echo " OK" -echo "All tests passed" -\ No newline at end of file +echo "All tests passed!" +\ No newline at end of file diff --git a/test/btc/hell.sh b/test/btc/hell.sh @@ -132,4 +132,4 @@ check_delta "incoming" "" echo " OK" -echo "All tests passed" -\ No newline at end of file +echo "All tests passed!" +\ No newline at end of file diff --git a/test/btc/lifetime.sh b/test/btc/lifetime.sh @@ -51,4 +51,4 @@ check_down $WIRE_PID btc-wire check_down $GATEWAY_PID wire-gateway echo " OK" -echo "All tests passed" +echo "All tests passed!" diff --git a/test/btc/reconnect.sh b/test/btc/reconnect.sh @@ -91,4 +91,4 @@ echo "" # Balance should not have changed check_balance 9.99990892 0.00007001 -echo "All tests passed" -\ No newline at end of file +echo "All tests passed!" +\ No newline at end of file diff --git a/test/btc/reorg.sh b/test/btc/reorg.sh @@ -122,4 +122,4 @@ next_btc check_balance "*" 0.00011000 echo " OK" -echo "All tests passed" -\ No newline at end of file +echo "All tests passed!" +\ No newline at end of file diff --git a/test/btc/stress.sh b/test/btc/stress.sh @@ -126,4 +126,4 @@ echo -n "Check balance:" check_balance "*" 0.00090000 echo " OK" -echo "All tests passed" -\ No newline at end of file +echo "All tests passed!" +\ No newline at end of file diff --git a/test/btc/wire.sh b/test/btc/wire.sh @@ -69,4 +69,4 @@ sleep 1 check_balance "*" 0.00011000 echo " OK" -echo "All tests passed" -\ No newline at end of file +echo "All tests passed!" +\ No newline at end of file diff --git a/test/common.sh b/test/common.sh @@ -6,10 +6,13 @@ set -eu # Cleanup to run whenever we exit function cleanup() { + pg_ctl stop -D $DB_DIR -w &> /dev/null for n in `jobs -p`; do kill $n &> /dev/null || true done - pg_ctl stop -D $DB_DIR -w &> /dev/null + for n in `jobs -p`; do + wait $n || true + done rm -rf $DIR &> /dev/null wait } @@ -83,7 +86,7 @@ function reset_db() { # Start a bitcoind regtest node, generate money, wallet and addresses function init_btc() { - cp ${BASH_SOURCE%/*}/conf/bitcoin.conf $BTC_DIR/bitcoin.conf + cp ${BASH_SOURCE%/*}/conf/${BTC_CONFIG:-bitcoin.conf} $BTC_DIR/bitcoin.conf bitcoind -datadir=$BTC_DIR $* &>> log/btc.log & BTC_PID="$!" # Wait for RPC server to be online diff --git a/test/conf/bitcoin_auth.conf b/test/conf/bitcoin_auth.conf @@ -0,0 +1,9 @@ +regtest=1 +txindex=1 +fallbackfee=0.00000001 +rpcuser=bob +rpcpassword=password + +[regtest] +port=8346 +rpcport=18346 +\ No newline at end of file diff --git a/test/conf/bitcoin_auth1.conf b/test/conf/bitcoin_auth1.conf @@ -0,0 +1,11 @@ +regtest=1 +txindex=1 +fallbackfee=0.00000001 +rpcuser=bob +rpcpassword=password + +[regtest] +port=8346 +rpcport=18346 +rpcuser=alice +rpcpassword=password +\ No newline at end of file diff --git a/test/conf/bitcoin_auth2.conf b/test/conf/bitcoin_auth2.conf @@ -0,0 +1,9 @@ +regtest=1 +txindex=1 +fallbackfee=0.00000001 + +[regtest] +port=8346 +rpcport=18346 +rpcuser=alice +rpcpassword=password +\ No newline at end of file diff --git a/test/conf/bitcoin_auth3.conf b/test/conf/bitcoin_auth3.conf @@ -0,0 +1,8 @@ +regtest=1 +txindex=1 +fallbackfee=0.00000001 + +[regtest] +port=8346 +rpcport=18346 +rpccookiefile=catch_me_if_you_can +\ No newline at end of file diff --git a/test/conf/bitcoin_auth4.conf b/test/conf/bitcoin_auth4.conf @@ -0,0 +1,8 @@ +regtest=1 +txindex=1 +fallbackfee=0.00000001 +rpccookiefile=catch_me_if_you_can + +[regtest] +port=8346 +rpcport=18346 +\ No newline at end of file diff --git a/test/conf/bitcoin_auth5.conf b/test/conf/bitcoin_auth5.conf @@ -0,0 +1,9 @@ +regtest=1 +txindex=1 +fallbackfee=0.00000001 +rpccookiefile=catch_me_if_you_can + +[regtest] +port=8346 +rpcport=18346 +rpccookiefile=cannot_touch_this +\ No newline at end of file diff --git a/test/gateway/api.sh b/test/gateway/api.sh @@ -131,4 +131,4 @@ printf 'HelloWorld%s' {1..1000} | pigz -z9 > $TEMP_FILE echo -n "Handle compression bomb:" test `curl -w %{http_code} -X POST -H"Content-Encoding:deflate" -s -o /dev/null --data-binary @$TEMP_FILE ${BANK_ENDPOINT}transfer` -eq 400 && echo " OK" || echo " Failed" -echo "All tests passed" +echo "All tests passed!"