Merge pull request #687 from michael-loki/config_env

Allow environment variable override of config
pull/690/head
michael-loki 5 years ago committed by GitHub
commit 0cd9b4c380
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,14 +16,14 @@
namespace llarp
{
bool
void
RouterConfig::fromSection(string_view key, string_view val)
{
if(key == "netid")
{
if(val.size() <= NetID::size())
{
netid.assign(val.begin(), val.end());
m_netId.assign(val.begin(), val.end());
}
else
{
@ -36,8 +36,8 @@ namespace llarp
auto ival = atoi(sVal.c_str());
if(ival > 0)
{
maxConnectedRouters = ival;
LogInfo("max connections set to ", maxConnectedRouters);
m_maxConnectedRouters = ival;
LogInfo("max connections set to ", m_maxConnectedRouters);
}
}
if(key == "min-connections")
@ -46,31 +46,31 @@ namespace llarp
auto ival = atoi(sVal.c_str());
if(ival > 0)
{
minConnectedRouters = ival;
LogInfo("min connections set to ", minConnectedRouters);
m_minConnectedRouters = ival;
LogInfo("min connections set to ", m_minConnectedRouters);
}
}
if(key == "nickname")
{
nickname.assign(val.begin(), val.end());
m_nickname.assign(val.begin(), val.end());
// set logger name here
LogContext::Instance().nodeName = nickname;
LogContext::Instance().nodeName = nickname();
}
if(key == "encryption-privkey")
{
encryption_keyfile.assign(val.begin(), val.end());
m_encryptionKeyfile.assign(val.begin(), val.end());
}
if(key == "contact-file")
{
our_rc_file.assign(val.begin(), val.end());
m_ourRcFile.assign(val.begin(), val.end());
}
if(key == "transport-privkey")
{
transport_keyfile.assign(val.begin(), val.end());
m_transportKeyfile.assign(val.begin(), val.end());
}
if((key == "identity-privkey" || key == "ident-privkey"))
{
ident_keyfile.assign(val.begin(), val.end());
m_identKeyfile.assign(val.begin(), val.end());
}
if(key == "public-address" || key == "public-ip")
{
@ -80,74 +80,73 @@ namespace llarp
// assume IPv4
llarp::Addr a(val);
llarp::LogInfo("setting public ipv4 ", a);
addrInfo.ip = *a.addr6();
publicOverride = true;
m_addrInfo.ip = *a.addr6();
m_publicOverride = true;
}
}
if(key == "public-port")
{
llarp::LogInfo("Setting public port ", val);
int p = atoi(std::string(val).c_str());
// Not needed to flip upside-down this is done in Addr(const AddressInfo&)
ip4addr.sin_port = p;
addrInfo.port = p;
publicOverride = true;
// Not needed to flip upside-down - this is done in llarp::Addr(const
// AddressInfo&)
m_ip4addr.sin_port = p;
m_addrInfo.port = p;
m_publicOverride = true;
}
if(key == "worker-threads")
{
workerThreads = atoi(std::string(val).c_str());
m_workerThreads = atoi(std::string(val).c_str());
}
if(key == "net-threads")
{
num_nethreads = atoi(std::string(val).c_str());
if(num_nethreads <= 0)
num_nethreads = 1;
m_numNetThreads = atoi(std::string(val).c_str());
if(m_numNetThreads <= 0)
{
m_numNetThreads = 1;
}
}
return true;
}
bool
void
NetworkConfig::fromSection(string_view key, string_view val)
{
if(key == "profiling")
{
if(IsTrueValue(val))
{
enableProfiling.emplace(true);
m_enableProfiling.emplace(true);
}
else if(IsFalseValue(val))
{
enableProfiling.emplace(false);
m_enableProfiling.emplace(false);
}
}
if(key == "profiles")
else if(key == "profiles")
{
routerProfilesFile.assign(val.begin(), val.end());
llarp::LogInfo("setting profiles to ", routerProfilesFile);
m_routerProfilesFile.assign(val.begin(), val.end());
llarp::LogInfo("setting profiles to ", routerProfilesFile());
}
else if(key == "strict-connect")
{
strictConnect.assign(val.begin(), val.end());
m_strictConnect.assign(val.begin(), val.end());
}
else
{
netConfig.emplace(key, val);
m_netConfig.emplace(key, val);
}
return true;
}
bool
void
NetdbConfig::fromSection(string_view key, string_view val)
{
if(key == "dir")
{
nodedb_dir.assign(val.begin(), val.end());
m_nodedbDir.assign(val.begin(), val.end());
}
return true;
}
bool
void
DnsConfig::fromSection(string_view key, string_view val)
{
if(key == "upstream")
@ -160,10 +159,9 @@ namespace llarp
llarp::LogInfo("set local dns to ", val);
netConfig.emplace("local-dns", val);
}
return true;
}
bool
void
IwpConfig::fromSection(string_view key, string_view val)
{
// try IPv4 first
@ -207,39 +205,34 @@ namespace llarp
}
else
{
servers.emplace_back(key, AF_INET, proto);
m_servers.emplace_back(key, AF_INET, proto);
}
return true;
}
bool
void
ConnectConfig::fromSection(ABSL_ATTRIBUTE_UNUSED string_view key,
string_view val)
{
routers.emplace_back(val.begin(), val.end());
return true;
}
bool
void
ServicesConfig::fromSection(string_view key, string_view val)
{
services.emplace_back(std::string(key.begin(), key.end()),
std::string(val.begin(), val.end()));
return true;
}
bool
void
SystemConfig::fromSection(string_view key, string_view val)
{
if(key == "pidfile")
{
pidfile.assign(val.begin(), val.end());
}
return true;
}
bool
void
MetricsConfig::fromSection(string_view key, string_view val)
{
if(key == "disable-metrics")
@ -263,30 +256,26 @@ namespace llarp
// consume everything else as a metric tag
metricTags[std::string(key)] = std::string(val);
}
return true;
}
bool
void
ApiConfig::fromSection(string_view key, string_view val)
{
if(key == "enabled")
{
enableRPCServer = IsTrueValue(val);
m_enableRPCServer = IsTrueValue(val);
}
if(key == "bind")
{
rpcBindAddr.assign(val.begin(), val.end());
m_rpcBindAddr.assign(val.begin(), val.end());
}
if(key == "authkey")
{
// TODO: add pubkey to whitelist
}
return true;
}
bool
void
LokidConfig::fromSection(string_view key, string_view val)
{
if(key == "service-node-seed")
@ -310,22 +299,18 @@ namespace llarp
{
lokidRPCPassword.assign(val.begin(), val.end());
}
return true;
}
bool
void
BootstrapConfig::fromSection(string_view key, string_view val)
{
if(key == "add-node")
{
routers.emplace_back(val.begin(), val.end());
}
return true;
}
bool
void
LoggingConfig::fromSection(string_view key, string_view val)
{
if(key == "type" && val == "syslog")
@ -364,8 +349,6 @@ namespace llarp
::abort();
}
}
return true;
}
template < typename Section, typename Config >
@ -377,10 +360,7 @@ namespace llarp
auto visitor = [&ret](const ConfigParser::Section_t &section) -> bool {
for(const auto &sec : section)
{
if(!ret.fromSection(sec.first, sec.second))
{
return false;
}
ret.fromSection(sec.first, sec.second);
}
return true;
};

@ -4,7 +4,10 @@
#include <crypto/types.hpp>
#include <router_contact.hpp>
#include <util/fs.hpp>
#include <util/str.hpp>
#include <absl/strings/str_cat.h>
#include <cstdlib>
#include <functional>
#include <string>
#include <utility>
@ -14,55 +17,172 @@ namespace llarp
{
struct ConfigParser;
struct RouterConfig
template < typename Type >
Type
fromEnv(const Type& val, string_view envNameSuffix)
{
std::string envName = absl::StrCat("LOKINET_", envNameSuffix);
char* ptr = std::getenv(envName.c_str());
if(ptr)
{
return ptr;
}
else
{
return val;
}
}
template <>
inline int
fromEnv< int >(const int& val, string_view envNameSuffix)
{
std::string envName = absl::StrCat("LOKINET_", envNameSuffix);
const char* ptr = std::getenv(envName.c_str());
if(ptr)
{
return std::atoi(ptr);
}
else
{
return val;
}
}
template <>
inline uint16_t
fromEnv< uint16_t >(const uint16_t& val, string_view envNameSuffix)
{
std::string envName = absl::StrCat("LOKINET_", envNameSuffix);
const char* ptr = std::getenv(envName.c_str());
if(ptr)
{
return std::atoi(ptr);
}
else
{
return val;
}
}
template <>
inline size_t
fromEnv< size_t >(const size_t& val, string_view envNameSuffix)
{
std::string envName = absl::StrCat("LOKINET_", envNameSuffix);
const char* ptr = std::getenv(envName.c_str());
if(ptr)
{
return std::atoll(ptr);
}
else
{
return val;
}
}
template <>
inline absl::optional< bool >
fromEnv< absl::optional< bool > >(const absl::optional< bool >& val,
string_view envNameSuffix)
{
std::string envName = absl::StrCat("LOKINET_", envNameSuffix);
const char* ptr = std::getenv(envName.c_str());
if(ptr)
{
return IsTrueValue(ptr);
}
else
{
return val;
}
}
class RouterConfig
{
private:
/// always maintain this many connections to other routers
size_t minConnectedRouters = 2;
size_t m_minConnectedRouters = 2;
/// hard upperbound limit on the number of router to router connections
size_t maxConnectedRouters = 2000;
size_t m_maxConnectedRouters = 2000;
std::string netid;
std::string nickname;
std::string m_netId;
std::string m_nickname;
fs::path encryption_keyfile = "encryption.key";
fs::path m_encryptionKeyfile = "encryption.key";
// path to write our self signed rc to
fs::path our_rc_file = "rc.signed";
fs::path m_ourRcFile = "rc.signed";
// transient iwp encryption key
fs::path transport_keyfile = "transport.key";
fs::path m_transportKeyfile = "transport.key";
// long term identity key
fs::path ident_keyfile = "identity.key";
fs::path m_identKeyfile = "identity.key";
bool publicOverride = false;
struct sockaddr_in ip4addr;
AddressInfo addrInfo;
bool m_publicOverride = false;
struct sockaddr_in m_ip4addr;
AddressInfo m_addrInfo;
int workerThreads;
int num_nethreads;
int m_workerThreads;
int m_numNetThreads;
bool
public:
// clang-format off
size_t minConnectedRouters() const { return fromEnv(m_minConnectedRouters, "MIN_CONNECTED_ROUTERS"); }
size_t maxConnectedRouters() const { return fromEnv(m_maxConnectedRouters, "MAX_CONNECTED_ROUTERS"); }
fs::path encryptionKeyfile() const { return fromEnv(m_encryptionKeyfile, "ENCRYPTION_KEYFILE"); }
fs::path ourRcFile() const { return fromEnv(m_ourRcFile, "OUR_RC_FILE"); }
fs::path transportKeyfile() const { return fromEnv(m_transportKeyfile, "TRANSPORT_KEYFILE"); }
fs::path identKeyfile() const { return fromEnv(m_identKeyfile, "IDENT_KEYFILE"); }
std::string netId() const { return fromEnv(m_netId, "NETID"); }
std::string nickname() const { return fromEnv(m_nickname, "NICKNAME"); }
bool publicOverride() const { return fromEnv(m_publicOverride, "PUBLIC_OVERRIDE"); }
const struct sockaddr_in& ip4addr() const { return m_ip4addr; }
const AddressInfo& addrInfo() const { return m_addrInfo; }
int workerThreads() const { return fromEnv(m_workerThreads, "WORKER_THREADS"); }
int numNetThreads() const { return fromEnv(m_numNetThreads, "NUM_NET_THREADS"); }
// clang-format on
void
fromSection(string_view key, string_view val);
};
struct NetworkConfig
class NetworkConfig
{
absl::optional< bool > enableProfiling;
std::string routerProfilesFile = "profiles.dat";
std::string strictConnect;
std::unordered_multimap< std::string, std::string > netConfig;
public:
using NetConfig = std::unordered_multimap< std::string, std::string >;
bool
private:
absl::optional< bool > m_enableProfiling;
std::string m_routerProfilesFile = "profiles.dat";
std::string m_strictConnect;
NetConfig m_netConfig;
public:
// clang-format off
absl::optional< bool > enableProfiling() const { return fromEnv(m_enableProfiling, "ENABLE_PROFILING"); }
std::string routerProfilesFile() const { return fromEnv(m_routerProfilesFile, "ROUTER_PROFILES_FILE"); }
std::string strictConnect() const { return fromEnv(m_strictConnect, "STRICT_CONNECT"); }
const NetConfig& netConfig() const { return m_netConfig; }
// clang-format on
void
fromSection(string_view key, string_view val);
};
struct NetdbConfig
class NetdbConfig
{
std::string nodedb_dir;
private:
std::string m_nodedbDir;
bool
public:
// clang-format off
std::string nodedbDir() const { return fromEnv(m_nodedbDir, "NODEDB_DIR"); }
// clang-format on
void
fromSection(string_view key, string_view val);
};
@ -70,17 +190,27 @@ namespace llarp
{
std::unordered_multimap< std::string, std::string > netConfig;
bool
void
fromSection(string_view key, string_view val);
};
struct IwpConfig
class IwpConfig
{
public:
using Servers = std::vector< std::tuple< std::string, int, uint16_t > >;
private:
uint16_t m_OutboundPort = 0;
std::vector< std::tuple< std::string, int, uint16_t > > servers;
Servers m_servers;
bool
public:
// clang-format off
uint16_t outboundPort() const { return fromEnv(m_OutboundPort, "OUTBOUND_PORT"); }
const Servers& servers() const { return m_servers; }
// clang-format on
void
fromSection(string_view key, string_view val);
};
@ -88,14 +218,14 @@ namespace llarp
{
std::vector< std::string > routers;
bool
void
fromSection(string_view key, string_view val);
};
struct ServicesConfig
{
std::vector< std::pair< std::string, std::string > > services;
bool
void
fromSection(string_view key, string_view val);
};
@ -103,7 +233,7 @@ namespace llarp
{
std::string pidfile;
bool
void
fromSection(string_view key, string_view val);
};
@ -115,16 +245,23 @@ namespace llarp
std::string metricTankHost;
std::map< std::string, std::string > metricTags;
bool
void
fromSection(string_view key, string_view val);
};
struct ApiConfig
class ApiConfig
{
bool enableRPCServer = false;
std::string rpcBindAddr = "127.0.0.1:1190";
private:
bool m_enableRPCServer = false;
std::string m_rpcBindAddr = "127.0.0.1:1190";
bool
public:
// clang-format off
bool enableRPCServer() const { return fromEnv(m_enableRPCServer, "ENABLE_RPC_SERVER"); }
std::string rpcBindAddr() const { return fromEnv(m_rpcBindAddr, "RPC_BIND_ADDR"); }
// clang-format on
void
fromSection(string_view key, string_view val);
};
@ -137,23 +274,23 @@ namespace llarp
std::string lokidRPCUser;
std::string lokidRPCPassword;
bool
void
fromSection(string_view key, string_view val);
};
struct BootstrapConfig
{
std::vector< std::string > routers;
bool
void
fromSection(string_view key, string_view val);
};
struct LoggingConfig
{
bool m_LogJSON = false;
FILE *m_LogFile = stdout;
FILE* m_LogFile = stdout;
bool
void
fromSection(string_view key, string_view val);
};
@ -161,7 +298,7 @@ namespace llarp
{
private:
bool
parse(const ConfigParser &parser);
parse(const ConfigParser& parser);
public:
RouterConfig router;
@ -179,7 +316,7 @@ namespace llarp
LoggingConfig logging;
bool
Load(const char *fname);
Load(const char* fname);
bool
LoadFromString(string_view str);
@ -188,12 +325,12 @@ namespace llarp
} // namespace llarp
void
llarp_generic_ensure_config(std::ofstream &f, std::string basepath);
llarp_generic_ensure_config(std::ofstream& f, std::string basepath);
void
llarp_ensure_router_config(std::ofstream &f, std::string basepath);
llarp_ensure_router_config(std::ofstream& f, std::string basepath);
bool
llarp_ensure_client_config(std::ofstream &f, std::string basepath);
llarp_ensure_client_config(std::ofstream& f, std::string basepath);
#endif

@ -60,10 +60,10 @@ namespace llarp
}
// Router config
if(!singleThreaded && config->router.workerThreads > 0 && !worker)
if(!singleThreaded && config->router.workerThreads() > 0 && !worker)
{
worker.reset(
llarp_init_threadpool(config->router.workerThreads, "llarp-worker"));
worker.reset(llarp_init_threadpool(config->router.workerThreads(),
"llarp-worker"));
}
if(singleThreaded)
@ -72,17 +72,17 @@ namespace llarp
}
else
{
num_nethreads = config->router.num_nethreads;
num_nethreads = config->router.numNetThreads();
}
nodedb_dir = config->netdb.nodedb_dir;
nodedb_dir = config->netdb.nodedbDir();
if(!config->metrics.disableMetrics)
{
auto &metricsConfig = config->metrics;
auto &tags = metricsConfig.metricTags;
tags["netid"] = config->router.netid;
tags["nickname"] = config->router.nickname;
tags["netid"] = config->router.netId();
tags["nickname"] = config->router.nickname();
setupMetrics(metricsConfig);
if(!config->metrics.disableMetricLogs)
{

@ -770,9 +770,9 @@ namespace llarp
Router::fromConfig(Config *conf)
{
// Set netid before anything else
if(!conf->router.netid.empty())
if(!conf->router.netId().empty())
{
const auto &netid = conf->router.netid;
const auto &netid = conf->router.netId();
llarp::LogWarn("!!!! you have manually set netid to be '", netid,
"' which does not equal '", Version::LLARP_NET_ID,
"' you will run as a different network, good luck "
@ -785,9 +785,9 @@ namespace llarp
}
// IWP config
m_OutboundPort = conf->iwp_links.m_OutboundPort;
m_OutboundPort = conf->iwp_links.outboundPort();
for(const auto &serverConfig : conf->iwp_links.servers)
for(const auto &serverConfig : conf->iwp_links.servers())
{
auto server = llarp::utp::NewServerFromRouter(this);
if(!server->EnsureKeys(transport_keyfile.string().c_str()))
@ -808,12 +808,12 @@ namespace llarp
}
// set network config
netConfig = conf->network.netConfig;
netConfig = conf->network.netConfig();
// Network config
if(conf->network.enableProfiling.has_value())
if(conf->network.enableProfiling().has_value())
{
if(conf->network.enableProfiling.value())
if(conf->network.enableProfiling().value())
{
routerProfiling().Enable();
LogInfo("router profiling explicitly enabled");
@ -825,16 +825,16 @@ namespace llarp
}
}
if(!conf->network.routerProfilesFile.empty())
if(!conf->network.routerProfilesFile().empty())
{
routerProfilesFile = conf->network.routerProfilesFile;
routerProfilesFile = conf->network.routerProfilesFile();
routerProfiling().Load(routerProfilesFile.c_str());
llarp::LogInfo("setting profiles to ", routerProfilesFile);
}
if(!conf->network.strictConnect.empty())
if(!conf->network.strictConnect().empty())
{
const auto &val = conf->network.strictConnect;
const auto &val = conf->network.strictConnect();
if(IsServiceNode())
{
llarp::LogError("cannot use strict-connect option as service node");
@ -864,8 +864,8 @@ namespace llarp
}
// API config
enableRPCServer = conf->api.enableRPCServer;
rpcBindAddr = conf->api.rpcBindAddr;
enableRPCServer = conf->api.enableRPCServer();
rpcBindAddr = conf->api.rpcBindAddr();
// Services config
for(const auto &service : conf->services.services)
@ -942,19 +942,19 @@ namespace llarp
}
// Router config
_rc.SetNick(conf->router.nickname);
maxConnectedRouters = conf->router.maxConnectedRouters;
minConnectedRouters = conf->router.minConnectedRouters;
encryption_keyfile = conf->router.encryption_keyfile;
our_rc_file = conf->router.our_rc_file;
transport_keyfile = conf->router.transport_keyfile;
addrInfo = conf->router.addrInfo;
publicOverride = conf->router.publicOverride;
ip4addr = conf->router.ip4addr;
_rc.SetNick(conf->router.nickname());
maxConnectedRouters = conf->router.maxConnectedRouters();
minConnectedRouters = conf->router.minConnectedRouters();
encryption_keyfile = conf->router.encryptionKeyfile();
our_rc_file = conf->router.ourRcFile();
transport_keyfile = conf->router.transportKeyfile();
addrInfo = conf->router.addrInfo();
publicOverride = conf->router.publicOverride();
ip4addr = conf->router.ip4addr();
if(!usingSNSeed)
{
ident_keyfile = conf->router.ident_keyfile;
ident_keyfile = conf->router.identKeyfile();
}
}

@ -92,9 +92,9 @@ metric-tank-host=52.80.56.123:2003
ASSERT_TRUE(config.LoadFromString(text));
{
using kv = decltype(config.network.netConfig)::value_type;
using kv = NetworkConfig::NetConfig::value_type;
ASSERT_THAT(config.network.netConfig,
ASSERT_THAT(config.network.netConfig(),
UnorderedElementsAre(kv("ifname", "cluster-1"),
kv("ifaddr", "10.101.0.1/16")));
}
@ -102,9 +102,9 @@ metric-tank-host=52.80.56.123:2003
ASSERT_FALSE(config.metrics.disableMetrics);
{
using kv = decltype(config.iwp_links.servers)::value_type;
using kv = IwpConfig::Servers::value_type;
ASSERT_THAT(config.iwp_links.servers,
ASSERT_THAT(config.iwp_links.servers(),
UnorderedElementsAre(kv("eth0", AF_INET, 5501)));
}

Loading…
Cancel
Save