From f7083a9aaef7fefcf811a2bbca17e129a912101d Mon Sep 17 00:00:00 2001 From: Kevin Loubser Date: Thu, 5 Nov 2020 05:44:07 +0200 Subject: [PATCH 1/2] Fix RPC default binding --- .../RPCSettingsTest.cs | 77 +++++++++++++++++++ .../RpcSettings.cs | 39 ++++------ 2 files changed, 93 insertions(+), 23 deletions(-) diff --git a/src/Stratis.Bitcoin.Features.RPC.Tests/RPCSettingsTest.cs b/src/Stratis.Bitcoin.Features.RPC.Tests/RPCSettingsTest.cs index f0aa6ceaba..3addf0ffd3 100644 --- a/src/Stratis.Bitcoin.Features.RPC.Tests/RPCSettingsTest.cs +++ b/src/Stratis.Bitcoin.Features.RPC.Tests/RPCSettingsTest.cs @@ -202,6 +202,83 @@ public void GetUrls_NoBindsConfigured_ReturnsEmptyArray() Assert.Empty(urls); } + [Fact] + public void GetUrls_NoBindsConfigured_AllowIp_Configured_Returns_DefaultBindings() + { + string dir = CreateTestDir(this); + string confFile = Path.Combine(dir, "bitcoin.conf"); + var configLines = new List() + { + "server=true", + "rpcuser=testuser", + "rpcpassword=testpassword", + "rpcallowip=0.0.0.0" + }; + + WriteConfigurationToFile(confFile, configLines); + + var nodeSettings = new NodeSettings(this.Network, args: new string[] { "-conf=" + confFile }); + + var rpcSettings = new RpcSettings(nodeSettings); + string[] urls = rpcSettings.GetUrls(); + + Assert.Equal("http://[::]:18332/", urls[0]); + Assert.Equal("http://0.0.0.0:18332/", urls[1]); + } + + [Fact] + public void GetUrls_NoBindsConfigured_NoUserPassword_AllowIp_Configured_Returns_DefaultBindings() + { + string dir = CreateTestDir(this); + string confFile = Path.Combine(dir, "bitcoin.conf"); + var configLines = new List() + { + "server=true", + "rpcallowip=0.0.0.0" + }; + + WriteConfigurationToFile(confFile, configLines); + + var nodeSettings = new NodeSettings(this.Network, args: new string[] { "-conf=" + confFile }); + + var rpcSettings = new RpcSettings(nodeSettings); + string[] urls = rpcSettings.GetUrls(); + + Assert.Equal("http://[::]:18332/", urls[0]); + Assert.Equal("http://0.0.0.0:18332/", urls[1]); + } + + [Fact] + public void Load_ValidNodeSettings_UpdatesRpcSettingsFromNodeSettings_Empty_Username_And_Password() + { + string dir = CreateTestDir(this); + string confFile = Path.Combine(dir, "bitcoin.conf"); + var configLines = new List() + { + "server=true", + "rpcport=1378", + "rpcallowip=0.0.0.0", + "rpcbind=127.0.0.1" + }; + + WriteConfigurationToFile(confFile, configLines); + + var nodeSettings = new NodeSettings(this.Network, args: new string[] { "-conf=" + confFile }); + + var rpcSettings = new RpcSettings(nodeSettings); + + Assert.True(rpcSettings.Server); + Assert.Equal(1378, rpcSettings.RPCPort); + Assert.Equal(null, rpcSettings.RpcUser); + Assert.Equal(null, rpcSettings.RpcPassword); + Assert.NotEmpty(rpcSettings.Bind); + Assert.Equal("127.0.0.1:1378", rpcSettings.Bind[0].ToString()); + Assert.NotEmpty(rpcSettings.DefaultBindings); + Assert.Equal("127.0.0.1:1378", rpcSettings.DefaultBindings[0].ToString()); + Assert.NotEmpty(rpcSettings.AllowIp); + Assert.Equal("0.0.0.0", rpcSettings.AllowIp[0].ToString()); + } + private static void WriteConfigurationToFile(string confFile, List configurationFileLines) { File.WriteAllLines(confFile, configurationFileLines.ToArray()); diff --git a/src/Stratis.Bitcoin.Features.RPC/RpcSettings.cs b/src/Stratis.Bitcoin.Features.RPC/RpcSettings.cs index c47eb39af7..119b656687 100644 --- a/src/Stratis.Bitcoin.Features.RPC/RpcSettings.cs +++ b/src/Stratis.Bitcoin.Features.RPC/RpcSettings.cs @@ -104,36 +104,15 @@ private void LoadSettingsFromConfig(NodeSettings nodeSettings) { throw new ConfigurationException("Invalid rpcbind value"); } - } - } - - /// - /// Checks the validity of the RPC settings or forces them to be valid. - /// - /// Logger to use. - private void CheckConfigurationValidity(ILogger logger) - { - // Check that the settings are valid or force them to be valid - // (Note that these values will not be set if server = false in the config) - if (this.RpcPassword == null && this.RpcUser != null) - throw new ConfigurationException("rpcpassword should be provided"); - if (this.RpcUser == null && this.RpcPassword != null) - throw new ConfigurationException("rpcuser should be provided"); - // We can now safely assume that server was set to true in the config or that the - // "AddRpc" callback provided a user and password implying that the Rpc feature will be used. - if (this.RpcPassword != null && this.RpcUser != null) - { - // this.Server = true; - - // If the "Bind" list has not been specified via callback.. + // If the "Bind" list has not been specified via callback. if (this.Bind.Count == 0) this.Bind = this.DefaultBindings; if (this.AllowIp.Count == 0) { if (this.Bind.Count > 0) - logger.LogWarning("WARNING: RPC bind selection (-rpcbind) was ignored because allowed ip's (-rpcallowip) were not specified, refusing to allow everyone to connect"); + this.logger.LogWarning("WARNING: RPC bind selection (-rpcbind) was ignored because allowed ip's (-rpcallowip) were not specified, refusing to allow everyone to connect"); this.Bind.Clear(); this.Bind.Add(new IPEndPoint(IPAddress.Parse("::1"), this.RPCPort)); @@ -148,6 +127,20 @@ private void CheckConfigurationValidity(ILogger logger) } } + /// + /// Checks the validity of the RPC settings or forces them to be valid. + /// + /// Logger to use. + private void CheckConfigurationValidity(ILogger logger) + { + // Check that the settings are valid or force them to be valid + // (Note that these values will not be set if server = false in the config) + if (this.RpcPassword == null && this.RpcUser != null) + throw new ConfigurationException("rpcpassword should be provided"); + if (this.RpcUser == null && this.RpcPassword != null) + throw new ConfigurationException("rpcuser should be provided"); + } + /// Prints the help information on how to configure the rpc settings to the logger. /// The network to use. public static void PrintHelp(Network network) From cd2e981e51fa6b4d2f093c5cba6a41535977a461 Mon Sep 17 00:00:00 2001 From: Kevin Loubser Date: Thu, 5 Nov 2020 10:09:45 +0200 Subject: [PATCH 2/2] Fix RPC test --- .../Controller/RPCControllerTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Stratis.Bitcoin.Features.RPC.Tests/Controller/RPCControllerTest.cs b/src/Stratis.Bitcoin.Features.RPC.Tests/Controller/RPCControllerTest.cs index 2d33a143af..ae9cf62d7d 100644 --- a/src/Stratis.Bitcoin.Features.RPC.Tests/Controller/RPCControllerTest.cs +++ b/src/Stratis.Bitcoin.Features.RPC.Tests/Controller/RPCControllerTest.cs @@ -53,7 +53,7 @@ public RPCControllerTest() this.rpcClient = new Mock(); this.rpcSettings.Bind.Add(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0)); - this.rpcClientFactory.Setup(r => r.Create(It.IsAny(), It.Is(u => u.ToString() == "http://127.0.0.1:0/"), It.IsAny())) + this.rpcClientFactory.Setup(r => r.Create(It.IsAny(), It.Is(u => u.ToString().StartsWith("http://127.0.0.1")), It.IsAny())) .Returns(this.rpcClient.Object); this.fullNode.Setup(f => f.RPCHost)