diff --git a/config/src/comments.rs b/config/src/comments.rs index a303c62..55aec0f 100644 --- a/config/src/comments.rs +++ b/config/src/comments.rs @@ -53,6 +53,11 @@ fn comments() -> HashMap { retval.insert( "owner_api_listen_port".to_string(), " +#owner api address, for example 0.0.0.0:3420 for docker +#overrides owner_api_listen_port when set +#do not expose the owner api to an untrusted network +#owner_api_listen_addr = \"127.0.0.1:3420\" + #port for wallet owner api " .to_string(), diff --git a/config/src/types.rs b/config/src/types.rs index aff6687..ec6894c 100644 --- a/config/src/types.rs +++ b/config/src/types.rs @@ -28,9 +28,9 @@ pub struct WalletConfig { pub chain_type: Option, /// The port this wallet will run on pub api_listen_port: u16, - /// Listen interface for the owner API, should be hidden from config by default - #[serde(skip_serializing)] - pub owner_api_listen_interface: Option, + /// Address this wallet's owner API will listen on + #[serde(skip_serializing_if = "Option::is_none")] + pub owner_api_listen_addr: Option, /// The port this wallet's owner API will run on pub owner_api_listen_port: Option, /// Location of the secret for basic auth on the Owner API @@ -66,7 +66,7 @@ impl Default for WalletConfig { WalletConfig { chain_type: Some(ChainTypes::Mainnet), api_listen_port: 3415, - owner_api_listen_interface: Some(WalletConfig::default_owner_api_listen_interface()), + owner_api_listen_addr: None, owner_api_listen_port: Some(WalletConfig::default_owner_api_listen_port()), api_secret_path: Some(".owner_api_secret".to_string()), node_api_secret_path: Some(".foreign_api_secret".to_string()), @@ -92,11 +92,6 @@ impl WalletConfig { format!("127.0.0.1:{}", self.api_listen_port) } - /// Default listener port - pub fn default_owner_api_listen_interface() -> String { - "127.0.0.1".to_string() - } - /// Default listener port pub fn default_owner_api_listen_port() -> u16 { 3420 @@ -107,13 +102,6 @@ impl WalletConfig { 500_000 } - /// Use value from config file, defaulting to sensible value if missing. - pub fn owner_api_listen_interface(&self) -> String { - self.owner_api_listen_interface - .clone() - .unwrap_or_else(|| WalletConfig::default_owner_api_listen_interface()) - } - /// Use value from config file, defaulting to sensible value if missing. pub fn owner_api_listen_port(&self) -> u16 { self.owner_api_listen_port @@ -122,11 +110,9 @@ impl WalletConfig { /// Owner API listen address pub fn owner_api_listen_addr(&self) -> String { - format!( - "{}:{}", - self.owner_api_listen_interface(), - self.owner_api_listen_port() - ) + self.owner_api_listen_addr + .clone() + .unwrap_or_else(|| format!("127.0.0.1:{}", self.owner_api_listen_port())) } /// Accept fee base @@ -144,6 +130,21 @@ impl WalletConfig { ) } } + +#[cfg(test)] +mod tests { + use super::WalletConfig; + + #[test] + fn owner_api_address() { + let mut config = WalletConfig::default(); + assert_eq!(config.owner_api_listen_addr(), "127.0.0.1:3420"); + + config.owner_api_listen_addr = Some("0.0.0.0:3420".to_string()); + assert_eq!(config.owner_api_listen_addr(), "0.0.0.0:3420"); + } +} + /// Error type wrapping config errors. #[derive(Debug)] pub enum ConfigError { diff --git a/controller/src/controller.rs b/controller/src/controller.rs index c2f2c4e..3df3bd3 100644 --- a/controller/src/controller.rs +++ b/controller/src/controller.rs @@ -240,7 +240,9 @@ where let mut apis = ApiServer::new(); warn!("Starting HTTP Owner API server at {}.", addr); - let socket_addr: SocketAddr = addr.parse().expect("unable to parse socket address"); + let socket_addr: SocketAddr = addr + .parse() + .map_err(|_| Error::GenericError(format!("Invalid owner API address: {}", addr)))?; let api_thread = apis .start(socket_addr, router, tls_config, api_chan) .map_err(|_| Error::GenericError("API thread failed to start".to_string()))?; diff --git a/src/cmd/wallet_args.rs b/src/cmd/wallet_args.rs index 18f0af6..1a31532 100644 --- a/src/cmd/wallet_args.rs +++ b/src/cmd/wallet_args.rs @@ -410,6 +410,7 @@ pub fn parse_owner_api_args( args: &ArgMatches, ) -> Result<(), ParseError> { if let Some(port) = args.value_of("port") { + config.owner_api_listen_addr = None; config.owner_api_listen_port = Some(port.parse().unwrap()); } if args.is_present("run_foreign") {