Skip to content

Commit

Permalink
dnsdist: Load YAML and/or Lua configuration depending on the extension
Browse files Browse the repository at this point in the history
  • Loading branch information
rgacogne committed Dec 4, 2024
1 parent 3d9793b commit 4d9b01a
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 51 deletions.
91 changes: 48 additions & 43 deletions pdns/dnsdistdist/dnsdist-configuration-yaml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ static bool getOptionalLuaFunction(T& destination, const ::rust::string& functio
return true;
}

static std::shared_ptr<DownstreamState> createBackendFromConfiguration(const dnsdist::rust::settings::BackendsConfiguration& config)
static std::shared_ptr<DownstreamState> createBackendFromConfiguration(const dnsdist::rust::settings::BackendsConfiguration& config, bool configCheck)
{
DownstreamState::Config backendConfig;
std::shared_ptr<TLSCtx> tlsCtx;
Expand Down Expand Up @@ -396,7 +396,7 @@ static std::shared_ptr<DownstreamState> createBackendFromConfiguration(const dns

#warning handle XSK

auto downstream = std::make_shared<DownstreamState>(std::move(backendConfig), std::move(tlsCtx), true);
auto downstream = std::make_shared<DownstreamState>(std::move(backendConfig), std::move(tlsCtx), !configCheck);

const auto& autoUpgradeConf = config.auto_upgrade;
if (autoUpgradeConf.enabled && downstream->getProtocol() != dnsdist::Protocol::DoT && downstream->getProtocol() != dnsdist::Protocol::DoH) {
Expand All @@ -407,7 +407,7 @@ static std::shared_ptr<DownstreamState> createBackendFromConfiguration(const dns
}
#endif /* defined(HAVE_YAML_CONFIGURATION) */

bool loadConfigurationFromFile(const std::string fileName)
bool loadConfigurationFromFile(const std::string fileName, bool isClient, bool configCheck)

Check warning on line 410 in pdns/dnsdistdist/dnsdist-configuration-yaml.cc

View workflow job for this annotation

GitHub Actions / Analyze (cpp, dnsdist)

the const qualified parameter 'fileName' is copied for each invocation; consider making it a reference (performance-unnecessary-value-param - Level=Warning)
{
#if defined(HAVE_YAML_CONFIGURATION)
auto file = std::ifstream(fileName);
Expand All @@ -426,6 +426,23 @@ bool loadConfigurationFromFile(const std::string fileName)

auto globalConfig = dnsdist::rust::settings::from_yaml_string(data);

if (!globalConfig.console.listen_address.empty()) {
const auto& consoleConf = globalConfig.console;
dnsdist::configuration::updateRuntimeConfiguration([consoleConf](dnsdist::configuration::RuntimeConfiguration& config) {
config.d_consoleServerAddress = ComboAddress(std::string(consoleConf.listen_address), 5199);
config.d_consoleEnabled = true;
config.d_consoleACL.clear();
for (const auto& aclEntry : consoleConf.acl) {
config.d_consoleACL.addMask(std::string(aclEntry));
}
B64Decode(std::string(consoleConf.key), config.d_consoleKey);
});
}

if (isClient) {
return true;
}

if (!globalConfig.acl.empty()) {
dnsdist::configuration::updateRuntimeConfiguration([&acl = globalConfig.acl](dnsdist::configuration::RuntimeConfiguration& config) {
config.d_ACL.clear();
Expand Down Expand Up @@ -478,7 +495,7 @@ bool loadConfigurationFromFile(const std::string fileName)
}

for (const auto& backend : globalConfig.backends) {
auto downstream = createBackendFromConfiguration(backend);
auto downstream = createBackendFromConfiguration(backend, configCheck);

if (!downstream->d_config.pools.empty()) {
for (const auto& poolName : downstream->d_config.pools) {
Expand All @@ -492,19 +509,6 @@ bool loadConfigurationFromFile(const std::string fileName)
dnsdist::backend::registerNewBackend(downstream);
}

if (!globalConfig.console.listen_address.empty()) {
const auto& consoleConf = globalConfig.console;
dnsdist::configuration::updateRuntimeConfiguration([consoleConf](dnsdist::configuration::RuntimeConfiguration& config) {
config.d_consoleServerAddress = ComboAddress(std::string(consoleConf.listen_address), 5199);
config.d_consoleEnabled = true;
config.d_consoleACL.clear();
for (const auto& aclEntry : consoleConf.acl) {
config.d_consoleACL.addMask(std::string(aclEntry));
}
B64Decode(std::string(consoleConf.key), config.d_consoleKey);
});
}

if (!globalConfig.proxy_protocol.acl.empty()) {
dnsdist::configuration::updateRuntimeConfiguration([globalConfig](dnsdist::configuration::RuntimeConfiguration& config) {
config.d_proxyProtocolACL.clear();
Expand Down Expand Up @@ -559,36 +563,37 @@ bool loadConfigurationFromFile(const std::string fileName)
#endif /* DISABLE_CARBON */

#if !defined(DISABLE_PROTOBUF)

for (const auto& protobufLogger : globalConfig.remote_logging.protobuf_loggers) {
auto object = std::shared_ptr<RemoteLoggerInterface>(std::make_shared<RemoteLogger>(ComboAddress(std::string(protobufLogger.address)), protobufLogger.timeout, protobufLogger.max_queued_entries * 100, protobufLogger.reconnect_wait_time, false));
registerType<RemoteLoggerInterface>(object, protobufLogger.name);
}
if (!configCheck) {
for (const auto& protobufLogger : globalConfig.remote_logging.protobuf_loggers) {
auto object = std::shared_ptr<RemoteLoggerInterface>(std::make_shared<RemoteLogger>(ComboAddress(std::string(protobufLogger.address)), protobufLogger.timeout, protobufLogger.max_queued_entries * 100, protobufLogger.reconnect_wait_time, false));
registerType<RemoteLoggerInterface>(object, protobufLogger.name);
}

#if defined(HAVE_FSTRM)
for (const auto& dnstapLogger : globalConfig.remote_logging.dnstap_loggers) {
auto transport = boost::to_lower_copy(std::string(dnstapLogger.transport));
int family{0};
if (transport == "unix") {
family = AF_UNIX;
}
else if (transport == "tcp") {
family = AF_INET;
}
else {
throw std::runtime_error("Unsupport dnstap transport type '" + transport + "'");
}
for (const auto& dnstapLogger : globalConfig.remote_logging.dnstap_loggers) {
auto transport = boost::to_lower_copy(std::string(dnstapLogger.transport));
int family{0};
if (transport == "unix") {
family = AF_UNIX;
}
else if (transport == "tcp") {
family = AF_INET;
}
else {
throw std::runtime_error("Unsupport dnstap transport type '" + transport + "'");
}

std::unordered_map<string, unsigned int> options;
options["bufferHint"] = dnstapLogger.buffer_hint;
options["flushTimeout"] = dnstapLogger.flush_timeout;
options["inputQueueSize"] = dnstapLogger.input_queue_size;
options["outputQueueSize"] = dnstapLogger.output_queue_size;
options["queueNotifyThreshold"] = dnstapLogger.queue_notify_threshold;
options["reopenInterval"] = dnstapLogger.reopen_interval;
std::unordered_map<string, unsigned int> options;
options["bufferHint"] = dnstapLogger.buffer_hint;
options["flushTimeout"] = dnstapLogger.flush_timeout;
options["inputQueueSize"] = dnstapLogger.input_queue_size;
options["outputQueueSize"] = dnstapLogger.output_queue_size;
options["queueNotifyThreshold"] = dnstapLogger.queue_notify_threshold;
options["reopenInterval"] = dnstapLogger.reopen_interval;

auto object = std::shared_ptr<RemoteLoggerInterface>(std::make_shared<FrameStreamLogger>(family, std::string(dnstapLogger.address), false, options));
registerType<RemoteLoggerInterface>(object, dnstapLogger.name);
auto object = std::shared_ptr<RemoteLoggerInterface>(std::make_shared<FrameStreamLogger>(family, std::string(dnstapLogger.address), false, options));
registerType<RemoteLoggerInterface>(object, dnstapLogger.name);
}
}
#endif /* HAVE_FSTRM*/
#endif /* DISABLE_PROTOBUF */
Expand Down
2 changes: 1 addition & 1 deletion pdns/dnsdistdist/dnsdist-configuration-yaml.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@

namespace dnsdist::configuration::yaml
{
bool loadConfigurationFromFile(const std::string fileName);
bool loadConfigurationFromFile(const std::string fileName, bool isClient, bool configCheck);
}
11 changes: 10 additions & 1 deletion pdns/dnsdistdist/dnsdist-lua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3185,7 +3185,9 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
});
}

void setupLua(LuaContext& luaCtx, bool client, bool configCheck, const std::string& config)
namespace dnsdist::lua
{
void setupLua(LuaContext& luaCtx, bool client, bool configCheck)
{
setupLuaActions(luaCtx);
setupLuaConfig(luaCtx, client, configCheck);
Expand All @@ -3208,7 +3210,13 @@ void setupLua(LuaContext& luaCtx, bool client, bool configCheck, const std::stri
#ifdef LUAJIT_VERSION
luaCtx.executeCode(getLuaFFIWrappers());
#endif
}
}

namespace dnsdist::configuration::lua
{
void loadLuaConfigurationFile(LuaContext& luaCtx, const std::string& config, bool configCheck)
{
std::ifstream ifs(config);
if (!ifs) {
if (configCheck) {
Expand All @@ -3222,3 +3230,4 @@ void setupLua(LuaContext& luaCtx, bool client, bool configCheck, const std::stri

luaCtx.executeCode(ifs);
}
}
10 changes: 9 additions & 1 deletion pdns/dnsdistdist/dnsdist-lua.hh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ std::shared_ptr<DNSRule> makeRule(const luadnsrule_t& var, const std::string& ca
void parseRuleParams(boost::optional<luaruleparams_t>& params, boost::uuids::uuid& uuid, std::string& name, uint64_t& creationOrder);
void checkParameterBound(const std::string& parameter, uint64_t value, size_t max = std::numeric_limits<uint16_t>::max());

void setupLua(LuaContext& luaCtx, bool client, bool configCheck, const std::string& config);
void setupLuaActions(LuaContext& luaCtx);
void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck);
void setupLuaBindingsDNSCrypt(LuaContext& luaCtx, bool client);
Expand All @@ -64,6 +63,15 @@ void setupLuaVars(LuaContext& luaCtx);
void setupLuaWeb(LuaContext& luaCtx);
void setupLuaLoadBalancingContext(LuaContext& luaCtx);

namespace dnsdist::lua
{
void setupLua(LuaContext& luaCtx, bool client, bool configCheck);
}
namespace dnsdist::configuration::lua
{
void loadLuaConfigurationFile(LuaContext& luaCtx, const std::string& config, bool configCheck);
}

/**
* getOptionalValue(vars, key, value)
*
Expand Down
49 changes: 44 additions & 5 deletions pdns/dnsdistdist/dnsdist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "config.h"

#include <cstdint>
#include <filesystem>
#include <fstream>
#include <getopt.h>
#include <grp.h>
Expand All @@ -43,6 +44,7 @@
#include "dnsdist-cache.hh"
#include "dnsdist-carbon.hh"
#include "dnsdist-configuration.hh"
#include "dnsdist-configuration-yaml.hh"
#include "dnsdist-console.hh"
#include "dnsdist-crypto.hh"
#include "dnsdist-discovery.hh"
Expand Down Expand Up @@ -3265,7 +3267,42 @@ static ListeningSockets initListeningSockets()
return result;
}

#include "dnsdist-configuration-yaml.hh"
static std::optional<std::string> lookForTentativeConfigurationFileWithExtension(const std::string& configurationFile, const std::string& extension)
{
auto dotPos = configurationFile.rfind('.');
if (dotPos == std::string::npos) {
return std::nullopt;
}
auto tentativeFile = configurationFile.substr(0, dotPos + 1) + extension;
if (!std::filesystem::exists(tentativeFile)) {
return std::nullopt;
}
return tentativeFile;
}

static void loadConfigurationFromFile(const std::string& configurationFile, bool isClient, bool configCheck)
{
if (boost::ends_with(configurationFile, ".yml")) {
if (auto tentativeLuaConfFile = lookForTentativeConfigurationFileWithExtension(configurationFile, "lua")) {
vinfolog("Loading configuration from auto-discovered Lua file %s", *tentativeLuaConfFile);
dnsdist::configuration::lua::loadLuaConfigurationFile(*(g_lua.lock()), *tentativeLuaConfFile, configCheck);
}
vinfolog("Loading configuration from YAML file %s", configurationFile);
dnsdist::configuration::yaml::loadConfigurationFromFile(configurationFile, isClient, configCheck);
}
else if (boost::ends_with(configurationFile, ".lua")) {
vinfolog("Loading configuration from Lua file %s", configurationFile);
dnsdist::configuration::lua::loadLuaConfigurationFile(*(g_lua.lock()), configurationFile, configCheck);
if (auto tentativeYamlConfFile = lookForTentativeConfigurationFileWithExtension(configurationFile, "yml")) {
vinfolog("Loading configuration from auto-discovered YAML file %s", *tentativeYamlConfFile);
dnsdist::configuration::yaml::loadConfigurationFromFile(*tentativeYamlConfFile, isClient, configCheck);
}
}
else {
vinfolog("Loading configuration from Lua file %s", configurationFile);
dnsdist::configuration::lua::loadLuaConfigurationFile(*(g_lua.lock()), configurationFile, configCheck);
}
}

int main(int argc, char** argv)
{
Expand Down Expand Up @@ -3316,7 +3353,8 @@ int main(int argc, char** argv)
});

if (cmdLine.beClient || !cmdLine.command.empty()) {
setupLua(*(g_lua.lock()), true, false, cmdLine.config);
dnsdist::lua::setupLua(*(g_lua.lock()), true, false);
loadConfigurationFromFile(cmdLine.config, true, false);
if (clientAddress != ComboAddress()) {
dnsdist::configuration::updateRuntimeConfiguration([&clientAddress](dnsdist::configuration::RuntimeConfiguration& config) {
config.d_consoleServerAddress = clientAddress;
Expand Down Expand Up @@ -3346,7 +3384,8 @@ int main(int argc, char** argv)
dnsdist::webserver::registerBuiltInWebHandlers();

if (cmdLine.checkConfig) {
setupLua(*(g_lua.lock()), false, true, cmdLine.config);
dnsdist::lua::setupLua(*(g_lua.lock()), false, true);
loadConfigurationFromFile(cmdLine.config, false, true);
// No exception was thrown
infolog("Configuration '%s' OK!", cmdLine.config);
#ifdef COVERAGE
Expand All @@ -3364,8 +3403,8 @@ int main(int argc, char** argv)
/* create the default pool no matter what */
createPoolIfNotExists("");

//dnsdist::configuration::yaml::loadConfigurationFromFile("/home/remi/PowerDNS/confs/dnsdist.yml");
setupLua(*(g_lua.lock()), false, false, cmdLine.config);
dnsdist::lua::setupLua(*(g_lua.lock()), false, false);
loadConfigurationFromFile(cmdLine.config, false, false);

setupPools();

Expand Down

0 comments on commit 4d9b01a

Please sign in to comment.