From: Aki Tuomi Date: Tue, 9 Mar 2021 10:58:40 +0000 (+0200) Subject: dnsdist-lua: newserver - Warn about invalid and unused parameters X-Git-Tag: dnsdist-1.8.0-rc1~43^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c29eedcb0f4b8d55389ea909107cb5acc2238b28;p=thirdparty%2Fpdns.git dnsdist-lua: newserver - Warn about invalid and unused parameters --- diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index 829e02b0bd..000ca1e11b 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -305,24 +305,23 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) [client, configCheck](boost::variant pvars, boost::optional qps) { setLuaSideEffect(); - newserver_t vars; + boost::optional vars = newserver_t(); DownstreamState::Config config; std::string serverAddressStr; if (auto addrStr = boost::get(&pvars)) { serverAddressStr = *addrStr; if (qps) { - vars["qps"] = std::to_string(*qps); + (*vars)["qps"] = std::to_string(*qps); } } else { vars = boost::get(pvars); - serverAddressStr = boost::get(vars["address"]); + getOptionalValue(vars, "address", serverAddressStr); } - // FIXME: Check vars for unknown keys, needs refactoring to move creation at the end */ - - if (vars.count("source")) { + std::string source; + if (getOptionalValue(vars, "source", source) > 0) { /* handle source in the following forms: - v4 address ("192.0.2.1") - v6 address ("2001:DB8::1") @@ -330,7 +329,6 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) - v4 address and interface name ("192.0.2.1@eth0") - v6 address and interface name ("2001:DB8::1@eth0") */ - const string source = boost::get(vars["source"]); bool parsed = false; std::string::size_type pos = source.find("@"); if (pos == std::string::npos) { @@ -368,25 +366,26 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } } - if (vars.count("sockets")) { - config.d_numberOfSockets = std::stoul(boost::get(vars["sockets"])); + std::string valueStr; + if (getOptionalValue(vars, "sockets", valueStr) > 0) { + config.d_numberOfSockets = std::stoul(valueStr); if (config.d_numberOfSockets == 0) { - warnlog("Dismissing invalid number of sockets '%s', using 1 instead", boost::get(vars["sockets"])); + warnlog("Dismissing invalid number of sockets '%s', using 1 instead", valueStr); config.d_numberOfSockets = 1; } } - if (vars.count("qps")) { - config.d_qpsLimit = std::stoi(boost::get(vars["qps"])); + if (getOptionalValue(vars, "qps", valueStr) > 0) { + config.d_qpsLimit = std::stoi(valueStr); } - if (vars.count("order")) { - config.order = std::stoi(boost::get(vars["order"])); + if (getOptionalValue(vars, "order", valueStr) > 0) { + config.order = std::stoi(valueStr); } - if (vars.count("weight")) { + if (getOptionalValue(vars, "weight", valueStr) > 0) { try { - config.d_weight = std::stoi(boost::get(vars["weight"])); + config.d_weight = std::stoi(valueStr); if (config.d_weight < 1) { errlog("Error creating new server: downstream weight value must be greater than 0."); @@ -400,55 +399,55 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } } - if (vars.count("retries")) { - config.d_retries = std::stoi(boost::get(vars["retries"])); + if (getOptionalValue(vars, "retries", valueStr) > 0) { + config.d_retries = std::stoi(valueStr); } - if (vars.count("checkInterval")) { - config.checkInterval = static_cast(std::stoul(boost::get(vars["checkInterval"]))); + if (getOptionalValue(vars, "checkInterval", valueStr) > 0) { + config.checkInterval = static_cast(std::stoul(valueStr)); } - if (vars.count("tcpConnectTimeout")) { - config.tcpConnectTimeout = std::stoi(boost::get(vars["tcpConnectTimeout"])); + if (getOptionalValue(vars, "tcpConnectTimeout", valueStr) > 0) { + config.tcpConnectTimeout = std::stoi(boost::get(valueStr)); } - if (vars.count("tcpSendTimeout")) { - config.tcpSendTimeout = std::stoi(boost::get(vars["tcpSendTimeout"])); + if (getOptionalValue(vars, "tcpSendTimeout", valueStr) > 0) { + config.tcpSendTimeout = std::stoi(valueStr); } - if (vars.count("tcpRecvTimeout")) { - config.tcpRecvTimeout = std::stoi(boost::get(vars["tcpRecvTimeout"])); + if (getOptionalValue(vars, "tcpRecvTimeout", valueStr) > 0) { + config.tcpRecvTimeout = std::stoi(valueStr); } - if (vars.count("tcpFastOpen")) { - bool fastOpen = boost::get(vars["tcpFastOpen"]); + bool fastOpen{false}; + if (getOptionalValue(vars, "tcpFastOpen", fastOpen) > 0) { if (fastOpen) { #ifdef MSG_FASTOPEN config.tcpFastOpen = true; #else - warnlog("TCP Fast Open has been configured on downstream server %s but is not supported", boost::get(vars["address"])); + warnlog("TCP Fast Open has been configured on downstream server %s but is not supported", serverAddressStr); #endif } } - if (vars.count("maxInFlight")) { - config.d_maxInFlightQueriesPerConn = std::stoi(boost::get(vars["maxInFlight"])); + if (getOptionalValue(vars, "maxInFlight", valueStr) > 0) { + config.d_maxInFlightQueriesPerConn = std::stoi(valueStr); } - if (vars.count("maxConcurrentTCPConnections")) { - config.d_tcpConcurrentConnectionsLimit = std::stoi(boost::get(vars.at("maxConcurrentTCPConnections"))); + if (getOptionalValue(vars, "maxConcurrentTCPConnections", valueStr) > 0) { + config.d_tcpConcurrentConnectionsLimit = std::stoi(valueStr); } - if (vars.count("name")) { - config.name = boost::get(vars["name"]); + if (getOptionalValue(vars, "name", valueStr) > 0) { + config.name = valueStr; } - if (vars.count("id")) { - config.id = boost::uuids::string_generator()(boost::get(vars["id"])); + if (getOptionalValue(vars, "id", valueStr) > 0) { + config.id = boost::uuids::string_generator()(valueStr); } - if (vars.count("healthCheckMode")) { - const auto& mode = boost::get(vars.at("healthCheckMode")); + if (getOptionalValue(vars, "healthCheckMode", valueStr) > 0) { + const auto& mode = valueStr; if (pdns_iequals(mode, "auto")) { config.availability = DownstreamState::Availability::Auto; } @@ -466,74 +465,61 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } } - if (vars.count("checkName")) { - config.checkName = DNSName(boost::get(vars["checkName"])); - } - - if (vars.count("checkType")) { - config.checkType = boost::get(vars["checkType"]); + if (getOptionalValue(vars, "checkName", valueStr) > 0) { + config.checkName = DNSName(valueStr); } - if (vars.count("checkClass")) { - config.checkClass = std::stoi(boost::get(vars["checkClass"])); - } + getOptionalValue(vars, "checkType", config.checkType); - if (vars.count("checkFunction")) { - config.checkFunction = boost::get(vars["checkFunction"]); + if (getOptionalValue(vars, "checkClass", valueStr) > 0) { + config.checkClass = std::stoi(valueStr); } - if (vars.count("checkTimeout")) { - config.checkTimeout = std::stoi(boost::get(vars["checkTimeout"])); - } + getOptionalValue(vars, "checkFunction", config.checkFunction); - if (vars.count("checkTCP")) { - config.d_tcpCheck = boost::get(vars.at("checkTCP")); + if (getOptionalValue(vars, "checkTimeout", valueStr) > 0) { + config.checkTimeout = std::stoi(valueStr); } - if (vars.count("setCD")) { - config.setCD = boost::get(vars["setCD"]); - } + getOptionalValue(vars, "checkTCP", config.d_tcpCheck); + getOptionalValue(vars, "setCD", config.setCD); + getOptionalValue(vars, "mustResolve", config.mustResolve); - if (vars.count("mustResolve")) { - config.mustResolve = boost::get(vars["mustResolve"]); - } - - if (vars.count("lazyHealthCheckSampleSize")) { - const auto& value = std::stoi(boost::get(vars.at("lazyHealthCheckSampleSize"))); + if (getOptionalValue(vars, "lazyHealthCheckSampleSize", valueStr) > 0) { + const auto& value = std::stoi(valueStr); checkParameterBound("lazyHealthCheckSampleSize", value); config.d_lazyHealthCheckSampleSize = value; } - if (vars.count("lazyHealthCheckMinSampleCount")) { - const auto& value = std::stoi(boost::get(vars.at("lazyHealthCheckMinSampleCount"))); + if (getOptionalValue(vars, "lazyHealthCheckMinSampleCount", valueStr) > 0) { + const auto& value = std::stoi(valueStr); checkParameterBound("lazyHealthCheckMinSampleCount", value); config.d_lazyHealthCheckMinSampleCount = value; } - if (vars.count("lazyHealthCheckThreshold")) { - const auto& value = std::stoi(boost::get(vars.at("lazyHealthCheckThreshold"))); + if (getOptionalValue(vars, "lazyHealthCheckThreshold", valueStr) > 0) { + const auto& value = std::stoi(valueStr); checkParameterBound("lazyHealthCheckThreshold", value, std::numeric_limits::max()); config.d_lazyHealthCheckThreshold = value; } - if (vars.count("lazyHealthCheckFailedInterval")) { - const auto& value = std::stoi(boost::get(vars.at("lazyHealthCheckFailedInterval"))); + if (getOptionalValue(vars, "lazyHealthCheckFailedInterval", valueStr) > 0) { + const auto& value = std::stoi(valueStr); checkParameterBound("lazyHealthCheckFailedInterval", value); config.d_lazyHealthCheckFailedInterval = value; } - if (vars.count("lazyHealthCheckUseExponentialBackOff")) { - config.d_lazyHealthCheckUseExponentialBackOff = boost::get(vars.at("lazyHealthCheckUseExponentialBackOff")); - } + getOptionalValue(vars, "lazyHealthCheckUseExponentialBackOff", config.d_lazyHealthCheckUseExponentialBackOff); - if (vars.count("lazyHealthCheckMaxBackOff")) { - const auto& value = std::stoi(boost::get(vars.at("lazyHealthCheckMaxBackOff"))); + + if (getOptionalValue(vars, "lazyHealthCheckMaxBackOff", valueStr) > 0) { + const auto& value = std::stoi(valueStr); checkParameterBound("lazyHealthCheckMaxBackOff", value); config.d_lazyHealthCheckMaxBackOff = value; } - if (vars.count("lazyHealthCheckMode")) { - const auto& mode = boost::get(vars.at("lazyHealthCheckMode")); + if (getOptionalValue(vars, "lazyHealthCheckMode", valueStr) > 0) { + const auto& mode = valueStr; if (pdns_iequals(mode, "TimeoutOnly")) { config.d_lazyHealthCheckMode = DownstreamState::LazyHealthCheckMode::TimeoutOnly; } @@ -545,77 +531,60 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } } - if (vars.count("lazyHealthCheckWhenUpgraded")) { - config.d_upgradeToLazyHealthChecks = boost::get(vars.at("lazyHealthCheckWhenUpgraded")); - } + getOptionalValue(vars, "lazyHealthCheckWhenUpgraded", config.d_upgradeToLazyHealthChecks); - if (vars.count("useClientSubnet")) { - config.useECS = boost::get(vars["useClientSubnet"]); - } - - if (vars.count("useProxyProtocol")) { - config.useProxyProtocol = boost::get(vars["useProxyProtocol"]); - } - - if (vars.count("disableZeroScope")) { - config.disableZeroScope = boost::get(vars["disableZeroScope"]); - } + getOptionalValue(vars, "useClientSubnet", config.useECS); + getOptionalValue(vars, "useProxyProtocol", config.useProxyProtocol); + getOptionalValue(vars, "disableZeroScoping", config.disableZeroScope); + getOptionalValue(vars, "ipBindAddrNoPort", config.ipBindAddrNoPort); - if (vars.count("ipBindAddrNoPort")) { - config.ipBindAddrNoPort = boost::get(vars["ipBindAddrNoPort"]); - } - - if (vars.count("addXPF")) { - config.xpfRRCode = std::stoi(boost::get(vars["addXPF"])); + if (getOptionalValue(vars, "addXPF", valueStr) > 0) { + try { + config.xpfRRCode = std::stoi(valueStr); + } catch (std::exception& e) { + warnlog("addXPF must be integer, not '%s' - ignoring", valueStr); + } } - if (vars.count("maxCheckFailures")) { - config.maxCheckFailures = std::stoi(boost::get(vars["maxCheckFailures"])); + if (getOptionalValue(vars, "maxCheckFailures", valueStr) > 0) { + try { + config.maxCheckFailures = std::stoi(valueStr); + } catch (std::exception& e) { + warnlog("maxCheckFailures must be integer, not '%s' - ignoring", valueStr); + } } - if (vars.count("rise")) { - config.minRiseSuccesses = std::stoi(boost::get(vars["rise"])); + if (getOptionalValue(vars, "rise", valueStr) > 0) { + try { + config.minRiseSuccesses = std::stoi(valueStr); + } catch (std::exception& e) { + warnlog("rise must be integer, not '%s' - ignoring", valueStr); + } } - if (vars.count("reconnectOnUp")) { - config.reconnectOnUp = boost::get(vars["reconnectOnUp"]); - } + getOptionalValue(vars, "reconnectOnUp", config.reconnectOnUp); - if (vars.count("cpus")) { - for (const auto& cpu : boost::get>(vars["cpus"])) { + LuaArray cpuMap; + if (getOptionalValue(vars, "cpus", cpuMap) > 0) { + for (const auto& cpu : cpuMap) { config.d_cpus.insert(std::stoi(cpu.second)); } } - if (vars.count("tcpOnly")) { - config.d_tcpOnly = boost::get(vars.at("tcpOnly")); - } + getOptionalValue(vars, "tcpOnly", config.d_tcpOnly); std::shared_ptr tlsCtx; - if (vars.count("ciphers")) { - config.d_tlsParams.d_ciphers = boost::get(vars.at("ciphers")); - } - if (vars.count("ciphers13")) { - config.d_tlsParams.d_ciphers13 = boost::get(vars.at("ciphers13")); - } - if (vars.count("caStore")) { - config.d_tlsParams.d_caStore = boost::get(vars.at("caStore")); - } - if (vars.count("validateCertificates")) { - config.d_tlsParams.d_validateCertificates = boost::get(vars.at("validateCertificates")); - } - if (vars.count("releaseBuffers")) { - config.d_tlsParams.d_releaseBuffers = boost::get(vars.at("releaseBuffers")); - } - if (vars.count("enableRenegotiation")) { - config.d_tlsParams.d_enableRenegotiation = boost::get(vars.at("enableRenegotiation")); - } - if (vars.count("subjectName")) { - config.d_tlsSubjectName = boost::get(vars.at("subjectName")); - } - else if (vars.count("subjectAddr")) { + getOptionalValue(vars, "ciphers", config.d_tlsParams.d_ciphers); + getOptionalValue(vars, "ciphers13", config.d_tlsParams.d_ciphers13); + getOptionalValue(vars, "caStore", config.d_tlsParams.d_caStore); + getOptionalValue(vars, "validateCertificates", config.d_tlsParams.d_validateCertificates); + getOptionalValue(vars, "releaseBuffers", config.d_tlsParams.d_releaseBuffers); + getOptionalValue(vars, "enableRenegotiation", config.d_tlsParams.d_enableRenegotiation); + getOptionalValue(vars, "subjectName", config.d_tlsSubjectName); + + if (getOptionalValue(vars, "subjectAddr", valueStr) > 0) { try { - ComboAddress ca(boost::get(vars.at("subjectAddr"))); + ComboAddress ca(valueStr); config.d_tlsSubjectName = ca.toString(); config.d_tlsSubjectIsAddr = true; } @@ -627,22 +596,20 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) uint16_t serverPort = 53; - if (vars.count("tls")) { + if (getOptionalValue(vars, "tls", valueStr) > 0) { serverPort = 853; - config.d_tlsParams.d_provider = boost::get(vars.at("tls")); + config.d_tlsParams.d_provider = valueStr; tlsCtx = getTLSContext(config.d_tlsParams); - if (vars.count("dohPath")) { + if (getOptionalValue(vars, "dohPath", valueStr) > 0) { #ifndef HAVE_NGHTTP2 throw std::runtime_error("Outgoing DNS over HTTPS support requested (via 'dohPath' on newServer()) but nghttp2 support is not available"); #endif serverPort = 443; - config.d_dohPath = boost::get(vars.at("dohPath")); + config.d_dohPath = valueStr; - if (vars.count("addXForwardedHeaders")) { - config.d_addXForwardedHeaders = boost::get(vars.at("addXForwardedHeaders")); - } + getOptionalValue(vars, "addXForwardedHeaders", config.d_addXForwardedHeaders); } } @@ -666,15 +633,13 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) return std::shared_ptr(); } - if (vars.count("pool")) { - if (auto* pool = boost::get(&vars["pool"])) { - config.pools.insert(*pool); - } - else { - auto pools = boost::get>(vars["pool"]); - for (auto& p : pools) { - config.pools.insert(p.second); - } + LuaArray pools; + if (getOptionalValue(vars, "pool", valueStr) > 0) { + config.pools.insert(valueStr); + } + else if (getOptionalValue(vars, "pool", pools) > 0) { + for (auto& p : pools) { + config.pools.insert(p.second); } } @@ -684,26 +649,21 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) uint16_t upgradeDoHKey = dnsdist::ServiceDiscovery::s_defaultDoHSVCKey; std::string upgradePool; - if (vars.count("autoUpgrade") && boost::get(vars.at("autoUpgrade"))) { - autoUpgrade = true; - - if (vars.count("autoUpgradeInterval")) { + getOptionalValue(vars, "autoUpgrade", autoUpgrade); + if (autoUpgrade) { + if (getOptionalValue(vars, "autoUpgradeInterval", valueStr) > 0) { try { - upgradeInterval = static_cast(std::stoul(boost::get(vars.at("autoUpgradeInterval")))); + upgradeInterval = static_cast(std::stoul(valueStr)); } catch (const std::exception& e) { warnlog("Error parsing 'autoUpgradeInterval' value: %s", e.what()); } } - if (vars.count("autoUpgradeKeep")) { - keepAfterUpgrade = boost::get(vars.at("autoUpgradeKeep")); - } - if (vars.count("autoUpgradePool")) { - upgradePool = boost::get(vars.at("autoUpgradePool")); - } - if (vars.count("autoUpgradeDoHKey")) { + getOptionalValue(vars, "autoUpgradeKeep", keepAfterUpgrade); + getOptionalValue(vars, "autoUpgradePool", upgradePool); + if (getOptionalValue(vars, "autoUpgradeDoHKey", valueStr) > 0) { try { - upgradeDoHKey = static_cast(std::stoul(boost::get(vars.at("autoUpgradeDoHKey")))); + upgradeDoHKey = static_cast(std::stoul(valueStr)); } catch (const std::exception& e) { warnlog("Error parsing 'autoUpgradeDoHKey' value: %s", e.what()); @@ -751,6 +711,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) return a->d_config.order < b->d_config.order; }); g_dstates.setState(states); + checkAllParametersConsumed("newServer", vars); return ret; });