Skip to content

Commit 241f073

Browse files
committed
feat: rpc external users are comma separated list
1 parent 68def97 commit 241f073

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/httpserver.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ static struct evhttp* eventHTTP = nullptr;
154154
static std::vector<CSubNet> rpc_allow_subnets;
155155
//! Work queue for handling longer requests off the event loop thread
156156
static std::unique_ptr<WorkQueue<HTTPClosure>> g_work_queue{nullptr};
157+
//! List of 'external' RPC users
158+
static std::vector<std::string> g_external_usernames;
157159
//! Handlers for (sub)paths
158160
static std::vector<HTTPPathHandler> pathHandlers;
159161
//! Bound listening sockets
@@ -270,8 +272,7 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
270272
}
271273
}
272274
const bool is_external_request = [&hreq]() -> bool {
273-
const std::string external_username = gArgs.GetArg("-rpcexternaluser", "");
274-
if (external_username.empty()) return false;
275+
if (g_external_usernames.empty()) return false;
275276

276277
const std::string strAuth = hreq->GetHeader("authorization").second;
277278
if (strAuth.substr(0, 6) != "Basic ")
@@ -283,8 +284,8 @@ static void http_request_cb(struct evhttp_request* req, void* arg)
283284
if (invalid) return false;
284285

285286
if (strUserPass.find(':') == std::string::npos) return false;
286-
287-
return strUserPass.substr(0, strUserPass.find(':')) == external_username;
287+
const std::string username{strUserPass.substr(0, strUserPass.find(':'))};
288+
return find(g_external_usernames.begin(), g_external_usernames.end(), username) != g_external_usernames.end();
288289
}();
289290

290291
// Dispatch to worker thread
@@ -432,12 +433,11 @@ bool InitHTTPServer()
432433
LogPrint(BCLog::HTTP, "Initialized HTTP server\n");
433434
int workQueueDepth = std::max((long)gArgs.GetArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L);
434435
int workQueueDepthExternal = 0;
435-
if (!gArgs.GetArg("-rpcexternaluser", "").empty()) {
436-
LogPrintf("HTTP: creating external work queue of depth %d\n", workQueueDepthExternal);
436+
if (const std::string rpc_externaluser{gArgs.GetArg("-rpcexternaluser", "")}; !rpc_externaluser.empty()) {
437437
workQueueDepthExternal = std::max((long)gArgs.GetArg("-rpcexternalworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L);
438+
g_external_usernames = SplitString(rpc_externaluser, ',');
438439
}
439440
LogPrintf("HTTP: creating work queue of depth %d external_depth %d\n", workQueueDepth, workQueueDepthExternal);
440-
441441
g_work_queue = std::make_unique<WorkQueue<HTTPClosure>>(workQueueDepth, workQueueDepthExternal);
442442
// transfer ownership to eventBase/HTTP via .release()
443443
eventBase = base_ctr.release();

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ void SetupServerArgs(NodeContext& node)
764764
argsman.AddArg("-rpcauth=<userpw>", "Username and HMAC-SHA-256 hashed password for JSON-RPC connections. The field <userpw> comes in the format: <USERNAME>:<SALT>$<HASH>. A canonical python script is included in share/rpcuser. The client then connects normally using the rpcuser=<USERNAME>/rpcpassword=<PASSWORD> pair of arguments. This option can be specified multiple times", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::RPC);
765765
argsman.AddArg("-rpcbind=<addr>[:port]", "Bind to given address to listen for JSON-RPC connections. Do not expose the RPC server to untrusted networks such as the public internet! This option is ignored unless -rpcallowip is also passed. Port is optional and overrides -rpcport. Use [host]:port notation for IPv6. This option can be specified multiple times (default: 127.0.0.1 and ::1 i.e., localhost, or if -rpcallowip has been specified, 0.0.0.0 and :: i.e., all addresses)", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY | ArgsManager::SENSITIVE, OptionsCategory::RPC);
766766
argsman.AddArg("-rpccookiefile=<loc>", "Location of the auth cookie. Relative paths will be prefixed by a net-specific datadir location. (default: data dir)", ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
767-
argsman.AddArg("-rpcexternaluser=<user>", "Username for JSON-RPC external connections", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::RPC);
767+
argsman.AddArg("-rpcexternaluser=<users>", "List of comma-separated usernames for JSON-RPC external connections", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::RPC);
768768
argsman.AddArg("-rpcexternalworkqueue=<n>", strprintf("Set the depth of the work queue to service external RPC calls (default: %d)", DEFAULT_HTTP_WORKQUEUE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::RPC);
769769
argsman.AddArg("-rpcpassword=<pw>", "Password for JSON-RPC connections", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::RPC);
770770
argsman.AddArg("-rpcport=<port>", strprintf("Listen for JSON-RPC connections on <port> (default: %u, testnet: %u, regtest: %u)", defaultBaseParams->RPCPort(), testnetBaseParams->RPCPort(), regtestBaseParams->RPCPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC);

test/functional/rpc_platform_filter.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def test_command(method, params, auth, expected_status, should_not_match=False):
114114
test_command("debug", ["1"], rpcuser_authpair_operator, 200)
115115

116116

117-
self.log.info("Restart node with -rpcexternaluser...")
117+
self.log.info("Restart node with -rpcexternaluser")
118118
self.restart_node(0, extra_args=["-rpcexternaluser=platform-user"])
119119

120120
external_log_str = "HTTP: Calling handler for external user"
@@ -124,6 +124,13 @@ def test_command(method, params, auth, expected_status, should_not_match=False):
124124
with self.nodes[0].assert_debug_log(expected_msgs=[expected_log_str], unexpected_msgs = [external_log_str]):
125125
test_command("getbestblockhash", [], rpcuser_authpair_operator, 200)
126126

127+
self.log.info("Restart node with multiple external users")
128+
self.restart_node(0, extra_args=["-rpcexternaluser=platform-user,operator"])
129+
with self.nodes[0].assert_debug_log(expected_msgs=[expected_log_str, external_log_str]):
130+
test_command("getbestblockhash", [], rpcuser_authpair_platform, 200)
131+
with self.nodes[0].assert_debug_log(expected_msgs=[expected_log_str, external_log_str]):
132+
test_command("getbestblockhash", [], rpcuser_authpair_operator, 200)
133+
127134

128135

129136
if __name__ == '__main__':

0 commit comments

Comments
 (0)