Skip to content

Commit

Permalink
Merge pull request #145 from zeptin/rpc-20201105
Browse files Browse the repository at this point in the history
Fix RPC default binding
  • Loading branch information
zeptin authored Nov 5, 2020
2 parents 02ff5b8 + cd2e981 commit 89e5fa0
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public RPCControllerTest()

this.rpcClient = new Mock<IRPCClient>();
this.rpcSettings.Bind.Add(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0));
this.rpcClientFactory.Setup(r => r.Create(It.IsAny<RpcSettings>(), It.Is<Uri>(u => u.ToString() == "http://127.0.0.1:0/"), It.IsAny<Network>()))
this.rpcClientFactory.Setup(r => r.Create(It.IsAny<RpcSettings>(), It.Is<Uri>(u => u.ToString().StartsWith("http://127.0.0.1")), It.IsAny<Network>()))
.Returns(this.rpcClient.Object);

this.fullNode.Setup(f => f.RPCHost)
Expand Down
77 changes: 77 additions & 0 deletions src/Stratis.Bitcoin.Features.RPC.Tests/RPCSettingsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>()
{
"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<string>()
{
"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<string>()
{
"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<string> configurationFileLines)
{
File.WriteAllLines(confFile, configurationFileLines.ToArray());
Expand Down
39 changes: 16 additions & 23 deletions src/Stratis.Bitcoin.Features.RPC/RpcSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,36 +104,15 @@ private void LoadSettingsFromConfig(NodeSettings nodeSettings)
{
throw new ConfigurationException("Invalid rpcbind value");
}
}
}

/// <summary>
/// Checks the validity of the RPC settings or forces them to be valid.
/// </summary>
/// <param name="logger">Logger to use.</param>
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));
Expand All @@ -148,6 +127,20 @@ private void CheckConfigurationValidity(ILogger logger)
}
}

/// <summary>
/// Checks the validity of the RPC settings or forces them to be valid.
/// </summary>
/// <param name="logger">Logger to use.</param>
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");
}

/// <summary> Prints the help information on how to configure the rpc settings to the logger.</summary>
/// <param name="network">The network to use.</param>
public static void PrintHelp(Network network)
Expand Down

0 comments on commit 89e5fa0

Please sign in to comment.