Skip to content

Commit 7b966d9

Browse files
committed
Merge #10267: New -includeconf argument for including external configuration files
25b7ab9 doc: Add release notes for -includeconf (Karl-Johan Alm) 0f0badd test: Test includeconf parameter. (Karl-Johan Alm) 629ff8c -includeconf=<path> support in config handler, for including external configuration files (Karl-Johan Alm) Pull request description: Fixes: #10071. Done: - adds `-includeconf=<path>`, where `<path>` is relative to `datadir` or to the path of the file being read, if in a file - protects against circular includes - updates help docs ~~~Thoughts:~~~ - ~~~I am not sure how to test this in a neat manner. Feedback on this would be nice. Will dig/think though.~~~ Tree-SHA512: cb31f1b2f69fbc0890d264948eb2e501ac05cf12f5e06a5942f9c1539eb15ea8dc3cae817f4073aecb2fcc21d0386747f14f89d990772003a76e2a6d25642553
2 parents 6b824c0 + 25b7ab9 commit 7b966d9

File tree

11 files changed

+133
-7
lines changed

11 files changed

+133
-7
lines changed

doc/release-notes-pr10267.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Changed command-line options
2+
----------------------------
3+
4+
- `-includeconf=<file>` can be used to include additional configuration files.
5+
Only works inside the `bitcoin.conf` file, not inside included files or from
6+
command-line. Multiple files may be included. Can be disabled from command-
7+
line via `-noincludeconf`. Note that multi-argument commands like
8+
`-includeconf` will override preceding `-noincludeconf`, i.e.
9+
10+
noincludeconf=1
11+
includeconf=relative.conf
12+
13+
as bitcoin.conf will still include `relative.conf`.

src/bitcoin-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static int AppInitRPC(int argc, char* argv[])
107107
return EXIT_FAILURE;
108108
}
109109
try {
110-
gArgs.ReadConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME));
110+
gArgs.ReadConfigFiles();
111111
} catch (const std::exception& e) {
112112
fprintf(stderr,"Error reading configuration file: %s\n", e.what());
113113
return EXIT_FAILURE;

src/bitcoind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static bool AppInit(int argc, char* argv[])
9292
}
9393
try
9494
{
95-
gArgs.ReadConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME));
95+
gArgs.ReadConfigFiles();
9696
} catch (const std::exception& e) {
9797
fprintf(stderr,"Error reading configuration file: %s\n", e.what());
9898
return false;

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ std::string HelpMessage(HelpMessageMode mode)
376376
strUsage += HelpMessageOpt("-debuglogfile=<file>", strprintf(_("Specify location of debug log file. Relative paths will be prefixed by a net-specific datadir location. (default: %s)"), DEFAULT_DEBUGLOGFILE));
377377
if (showDebug)
378378
strUsage += HelpMessageOpt("-feefilter", strprintf("Tell other nodes to filter invs to us by our mempool min fee (default: %u)", DEFAULT_FEEFILTER));
379+
strUsage += HelpMessageOpt("-includeconf=<file>", _("Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)"));
379380
strUsage += HelpMessageOpt("-loadblock=<file>", _("Imports blocks from external blk000??.dat file on startup"));
380381
strUsage += HelpMessageOpt("-maxmempool=<n>", strprintf(_("Keep the transaction memory pool below <n> megabytes (default: %u)"), DEFAULT_MAX_MEMPOOL_SIZE));
381382
strUsage += HelpMessageOpt("-maxorphantx=<n>", strprintf(_("Keep at most <n> unconnectable transactions in memory (default: %u)"), DEFAULT_MAX_ORPHAN_TRANSACTIONS));

src/interfaces/node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class NodeImpl : public Node
5252
{
5353
gArgs.ParseParameters(argc, argv);
5454
}
55-
void readConfigFile(const std::string& conf_path) override { gArgs.ReadConfigFile(conf_path); }
55+
void readConfigFiles() override { gArgs.ReadConfigFiles(); }
5656
bool softSetArg(const std::string& arg, const std::string& value) override { return gArgs.SoftSetArg(arg, value); }
5757
bool softSetBoolArg(const std::string& arg, bool value) override { return gArgs.SoftSetBoolArg(arg, value); }
5858
void selectParams(const std::string& network) override { SelectParams(network); }

src/interfaces/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Node
4848
virtual bool softSetBoolArg(const std::string& arg, bool value) = 0;
4949

5050
//! Load settings from configuration file.
51-
virtual void readConfigFile(const std::string& conf_path) = 0;
51+
virtual void readConfigFiles() = 0;
5252

5353
//! Choose network parameters.
5454
virtual void selectParams(const std::string& network) = 0;

src/qt/bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ int main(int argc, char *argv[])
616616
return EXIT_FAILURE;
617617
}
618618
try {
619-
node->readConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME));
619+
node->readConfigFiles();
620620
} catch (const std::exception& e) {
621621
QMessageBox::critical(0, QObject::tr(PACKAGE_NAME),
622622
QObject::tr("Error: Cannot parse configuration file: %1. Only use key=value syntax.").arg(e.what()));

src/util.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,17 @@ void ArgsManager::ParseParameters(int argc, const char* const argv[])
445445
m_override_args[key].push_back(val);
446446
}
447447
}
448+
449+
// we do not allow -includeconf from command line, so we clear it here
450+
auto it = m_override_args.find("-includeconf");
451+
if (it != m_override_args.end()) {
452+
if (it->second.size() > 0) {
453+
for (const auto& ic : it->second) {
454+
fprintf(stderr, "warning: -includeconf cannot be used from commandline; ignoring -includeconf=%s\n", ic.c_str());
455+
}
456+
m_override_args.erase(it);
457+
}
458+
}
448459
}
449460

450461
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
@@ -706,18 +717,40 @@ void ArgsManager::ReadConfigStream(std::istream& stream)
706717
}
707718
}
708719

709-
void ArgsManager::ReadConfigFile(const std::string& confPath)
720+
void ArgsManager::ReadConfigFiles()
710721
{
711722
{
712723
LOCK(cs_args);
713724
m_config_args.clear();
714725
}
715726

727+
const std::string confPath = GetArg("-conf", BITCOIN_CONF_FILENAME);
716728
fs::ifstream stream(GetConfigFile(confPath));
717729

718730
// ok to not have a config file
719731
if (stream.good()) {
720732
ReadConfigStream(stream);
733+
// if there is an -includeconf in the override args, but it is empty, that means the user
734+
// passed '-noincludeconf' on the command line, in which case we should not include anything
735+
if (m_override_args.count("-includeconf") == 0) {
736+
std::vector<std::string> includeconf(GetArgs("-includeconf"));
737+
{
738+
// We haven't set m_network yet (that happens in SelectParams()), so manually check
739+
// for network.includeconf args.
740+
std::vector<std::string> includeconf_net(GetArgs(std::string("-") + GetChainName() + ".includeconf"));
741+
includeconf.insert(includeconf.end(), includeconf_net.begin(), includeconf_net.end());
742+
}
743+
744+
for (const std::string& to_include : includeconf) {
745+
fs::ifstream include_config(GetConfigFile(to_include));
746+
if (include_config.good()) {
747+
ReadConfigStream(include_config);
748+
LogPrintf("Included configuration file %s\n", to_include.c_str());
749+
} else {
750+
fprintf(stderr, "Failed to include configuration file %s\n", to_include.c_str());
751+
}
752+
}
753+
}
721754
}
722755

723756
// If datadir is changed in .conf file:

src/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class ArgsManager
140140
void SelectConfigNetwork(const std::string& network);
141141

142142
void ParseParameters(int argc, const char*const argv[]);
143-
void ReadConfigFile(const std::string& confPath);
143+
void ReadConfigFiles();
144144

145145
/**
146146
* Log warnings for options in m_section_only_args when
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2018 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Tests the includeconf argument
6+
7+
Verify that:
8+
9+
1. adding includeconf to the configuration file causes the includeconf
10+
file to be loaded in the correct order.
11+
2. includeconf cannot be used as a command line argument.
12+
3. includeconf cannot be used recursively (ie includeconf can only
13+
be used from the base config file).
14+
4. multiple includeconf arguments can be specified in the main config
15+
file.
16+
"""
17+
import os
18+
import tempfile
19+
20+
from test_framework.test_framework import BitcoinTestFramework, assert_equal
21+
22+
class IncludeConfTest(BitcoinTestFramework):
23+
def set_test_params(self):
24+
self.setup_clean_chain = False
25+
self.num_nodes = 1
26+
27+
def setup_chain(self):
28+
super().setup_chain()
29+
# Create additional config files
30+
# - tmpdir/node0/relative.conf
31+
with open(os.path.join(self.options.tmpdir, "node0", "relative.conf"), "w", encoding="utf8") as f:
32+
f.write("uacomment=relative\n")
33+
# - tmpdir/node0/relative2.conf
34+
with open(os.path.join(self.options.tmpdir, "node0", "relative2.conf"), "w", encoding="utf8") as f:
35+
f.write("uacomment=relative2\n")
36+
with open(os.path.join(self.options.tmpdir, "node0", "bitcoin.conf"), "a", encoding='utf8') as f:
37+
f.write("uacomment=main\nincludeconf=relative.conf\n")
38+
39+
def run_test(self):
40+
self.log.info("-includeconf works from config file. subversion should end with 'main; relative)/'")
41+
42+
subversion = self.nodes[0].getnetworkinfo()["subversion"]
43+
assert subversion.endswith("main; relative)/")
44+
45+
self.log.info("-includeconf cannot be used as command-line arg. subversion should still end with 'main; relative)/'")
46+
self.stop_node(0)
47+
with tempfile.SpooledTemporaryFile(max_size=2**16) as log_stderr:
48+
self.start_node(0, extra_args=["-includeconf=relative2.conf"], stderr=log_stderr)
49+
50+
subversion = self.nodes[0].getnetworkinfo()["subversion"]
51+
assert subversion.endswith("main; relative)/")
52+
log_stderr.seek(0)
53+
stderr = log_stderr.read().decode('utf-8').strip()
54+
assert_equal(stderr, 'warning: -includeconf cannot be used from commandline; ignoring -includeconf=relative2.conf')
55+
56+
self.log.info("-includeconf cannot be used recursively. subversion should end with 'main; relative)/'")
57+
with open(os.path.join(self.options.tmpdir, "node0", "relative.conf"), "a", encoding="utf8") as f:
58+
f.write("includeconf=relative2.conf\n")
59+
60+
self.restart_node(0)
61+
62+
subversion = self.nodes[0].getnetworkinfo()["subversion"]
63+
assert subversion.endswith("main; relative)/")
64+
65+
self.log.info("multiple -includeconf args can be used from the base config file. subversion should end with 'main; relative; relative2)/'")
66+
with open(os.path.join(self.options.tmpdir, "node0", "relative.conf"), "w", encoding="utf8") as f:
67+
f.write("uacomment=relative\n")
68+
69+
with open(os.path.join(self.options.tmpdir, "node0", "bitcoin.conf"), "a", encoding='utf8') as f:
70+
f.write("includeconf=relative2.conf\n")
71+
72+
self.restart_node(0)
73+
74+
subversion = self.nodes[0].getnetworkinfo()["subversion"]
75+
assert subversion.endswith("main; relative; relative2)/")
76+
77+
if __name__ == '__main__':
78+
IncludeConfTest().main()

0 commit comments

Comments
 (0)