diff --git a/pdns/dnsdistdist/dnsdist-configuration-yaml.cc b/pdns/dnsdistdist/dnsdist-configuration-yaml.cc index 86cff1e37e0d5..118d509e23b63 100644 --- a/pdns/dnsdistdist/dnsdist-configuration-yaml.cc +++ b/pdns/dnsdistdist/dnsdist-configuration-yaml.cc @@ -284,7 +284,7 @@ static bool getOptionalLuaFunction(T& destination, const ::rust::string& functio return true; } -static std::shared_ptr createBackendFromConfiguration(const dnsdist::rust::settings::BackendsConfiguration& config) +static std::shared_ptr createBackendFromConfiguration(const dnsdist::rust::settings::BackendsConfiguration& config, bool configCheck) { DownstreamState::Config backendConfig; std::shared_ptr tlsCtx; @@ -396,7 +396,7 @@ static std::shared_ptr createBackendFromConfiguration(const dns #warning handle XSK - auto downstream = std::make_shared(std::move(backendConfig), std::move(tlsCtx), true); + auto downstream = std::make_shared(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) { @@ -407,7 +407,7 @@ static std::shared_ptr createBackendFromConfiguration(const dns } #endif /* defined(HAVE_YAML_CONFIGURATION) */ -bool loadConfigurationFromFile(const std::string fileName) +bool loadConfigurationFromFile(const std::string fileName, bool isClient, bool configCheck) { #if defined(HAVE_YAML_CONFIGURATION) auto file = std::ifstream(fileName); @@ -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(); @@ -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) { @@ -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(); @@ -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(std::make_shared(ComboAddress(std::string(protobufLogger.address)), protobufLogger.timeout, protobufLogger.max_queued_entries * 100, protobufLogger.reconnect_wait_time, false)); - registerType(object, protobufLogger.name); - } + if (!configCheck) { + for (const auto& protobufLogger : globalConfig.remote_logging.protobuf_loggers) { + auto object = std::shared_ptr(std::make_shared(ComboAddress(std::string(protobufLogger.address)), protobufLogger.timeout, protobufLogger.max_queued_entries * 100, protobufLogger.reconnect_wait_time, false)); + registerType(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 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 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(std::make_shared(family, std::string(dnstapLogger.address), false, options)); - registerType(object, dnstapLogger.name); + auto object = std::shared_ptr(std::make_shared(family, std::string(dnstapLogger.address), false, options)); + registerType(object, dnstapLogger.name); + } } #endif /* HAVE_FSTRM*/ #endif /* DISABLE_PROTOBUF */ diff --git a/pdns/dnsdistdist/dnsdist-configuration-yaml.hh b/pdns/dnsdistdist/dnsdist-configuration-yaml.hh index 8ceaa17f69765..d103f1ccd958a 100644 --- a/pdns/dnsdistdist/dnsdist-configuration-yaml.hh +++ b/pdns/dnsdistdist/dnsdist-configuration-yaml.hh @@ -27,5 +27,5 @@ namespace dnsdist::configuration::yaml { -bool loadConfigurationFromFile(const std::string fileName); +bool loadConfigurationFromFile(const std::string fileName, bool isClient, bool configCheck); } diff --git a/pdns/dnsdistdist/dnsdist-lua.cc b/pdns/dnsdistdist/dnsdist-lua.cc index 79b00d226eb7f..625fd4ab740ac 100644 --- a/pdns/dnsdistdist/dnsdist-lua.cc +++ b/pdns/dnsdistdist/dnsdist-lua.cc @@ -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); @@ -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) { @@ -3222,3 +3230,4 @@ void setupLua(LuaContext& luaCtx, bool client, bool configCheck, const std::stri luaCtx.executeCode(ifs); } +} diff --git a/pdns/dnsdistdist/dnsdist-lua.hh b/pdns/dnsdistdist/dnsdist-lua.hh index f8015fcebb59f..5776133b20d73 100644 --- a/pdns/dnsdistdist/dnsdist-lua.hh +++ b/pdns/dnsdistdist/dnsdist-lua.hh @@ -46,7 +46,6 @@ std::shared_ptr makeRule(const luadnsrule_t& var, const std::string& ca void parseRuleParams(boost::optional& 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::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); @@ -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) * diff --git a/pdns/dnsdistdist/dnsdist.cc b/pdns/dnsdistdist/dnsdist.cc index 8407141fb8263..d3c7884026aa7 100644 --- a/pdns/dnsdistdist/dnsdist.cc +++ b/pdns/dnsdistdist/dnsdist.cc @@ -23,6 +23,7 @@ #include "config.h" #include +#include #include #include #include @@ -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" @@ -3265,7 +3267,42 @@ static ListeningSockets initListeningSockets() return result; } -#include "dnsdist-configuration-yaml.hh" +static std::optional 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) { @@ -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; @@ -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 @@ -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();