Add config for host check

This commit is contained in:
Antonio Andelic 2022-04-21 12:30:35 +00:00
parent b2770fbe1c
commit 792b06cc66

View File

@ -5,6 +5,8 @@
#include <filesystem> #include <filesystem>
#include <Common/isLocalAddress.h> #include <Common/isLocalAddress.h>
#include <Common/DNSResolver.h> #include <Common/DNSResolver.h>
#include "Access/Common/AllowedClientHosts.h"
#include <ifaddrs.h>
namespace DB namespace DB
{ {
@ -19,16 +21,7 @@ namespace
bool isLoopback(const std::string & hostname) bool isLoopback(const std::string & hostname)
{ {
try return hostname == "localhost";
{
return DNSResolver::instance().resolveHost(hostname).isLoopback();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
return false;
} }
bool isLocalhost(const std::string & hostname) bool isLocalhost(const std::string & hostname)
@ -47,18 +40,19 @@ bool isLocalhost(const std::string & hostname)
std::unordered_map<UInt64, std::string> getClientPorts(const Poco::Util::AbstractConfiguration & config) std::unordered_map<UInt64, std::string> getClientPorts(const Poco::Util::AbstractConfiguration & config)
{ {
static const char * config_port_names[] = { using namespace std::string_literals;
"keeper_server.tcp_port", static const std::array config_port_names = {
"keeper_server.tcp_port_secure", "keeper_server.tcp_port"s,
"interserver_http_port", "keeper_server.tcp_port_secure"s,
"interserver_https_port", "interserver_http_port"s,
"tcp_port", "interserver_https_port"s,
"tcp_with_proxy_port", "tcp_port"s,
"tcp_port_secure", "tcp_with_proxy_port"s,
"mysql_port", "tcp_port_secure"s,
"postgresql_port", "mysql_port"s,
"grpc_port", "postgresql_port"s,
"prometheus.port", "grpc_port"s,
"prometheus.port"s,
}; };
std::unordered_map<UInt64, std::string> ports; std::unordered_map<UInt64, std::string> ports;
@ -80,7 +74,7 @@ std::unordered_map<UInt64, std::string> getClientPorts(const Poco::Util::Abstrac
/// 5. Our ID present in hostnames list /// 5. Our ID present in hostnames list
KeeperStateManager::KeeperConfigurationWrapper KeeperStateManager::parseServersConfiguration(const Poco::Util::AbstractConfiguration & config, bool allow_without_us) const KeeperStateManager::KeeperConfigurationWrapper KeeperStateManager::parseServersConfiguration(const Poco::Util::AbstractConfiguration & config, bool allow_without_us) const
{ {
const bool verify_hosts = config.getBool(config_prefix + ".verify_hosts", true); const bool host_checks_enabled = config.getBool(config_prefix + ".host_checks_enabled", true);
KeeperConfigurationWrapper result; KeeperConfigurationWrapper result;
result.cluster_config = std::make_shared<nuraft::cluster_config>(); result.cluster_config = std::make_shared<nuraft::cluster_config>();
@ -117,7 +111,7 @@ KeeperStateManager::KeeperConfigurationWrapper KeeperStateManager::parseServersC
hostname, port, client_ports[port]); hostname, port, client_ports[port]);
} }
if (verify_hosts) if (host_checks_enabled)
{ {
if (isLoopback(hostname)) if (isLoopback(hostname))
{ {
@ -173,7 +167,7 @@ KeeperStateManager::KeeperConfigurationWrapper KeeperStateManager::parseServersC
if (result.servers_start_as_followers.size() == total_servers) if (result.servers_start_as_followers.size() == total_servers)
throw Exception(ErrorCodes::RAFT_ERROR, "At least one of servers should be able to start as leader (without <start_as_follower>)"); throw Exception(ErrorCodes::RAFT_ERROR, "At least one of servers should be able to start as leader (without <start_as_follower>)");
if (verify_hosts) if (host_checks_enabled)
{ {
if (!loopback_hostname.empty() && !non_local_hostname.empty()) if (!loopback_hostname.empty() && !non_local_hostname.empty())
{ {