Skip to content

Commit

Permalink
[#5002] Show hostnames in master/tserver web UI when hostnames are sp…
Browse files Browse the repository at this point in the history
…ecified in bind

cmd line flags

Summary:
The web UI at port 7000/9000 still shows IP addresses when --webserver_interface,
rpc_bind_addresses and server_broadcast_addresses for all nodes is set to DNS names. This diff makes
it use the same logic as rpc_bind_addresses, where the cmd line flag is used unchanged when only a
single non-wildcard address is used.

Test Plan:
1. Run masters/tservers with --webserver_interface set to a DNS name, verify that no IPs
show up for them.
1.1. Test above with an IPv6 address like [::1] and --net_address_filter=all
1.2 Test above with webserver_interface set to wildcards (0.0.0.0 and [::]) and verify regular IPs show up in the web interface.

Reviewers: hector, bogdan, raju

Reviewed By: raju

Subscribers: ybase, bogdan

Differential Revision: https://phabricator.dev.yugabyte.com/D8897
  • Loading branch information
iSignal committed Jul 21, 2020
1 parent e4e2d39 commit 1403b2b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 20 deletions.
32 changes: 18 additions & 14 deletions src/yb/server/server_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ struct CommonMemTrackers {

std::unique_ptr<CommonMemTrackers> common_mem_trackers;

static const string kWildCardHostAddressV6 = "::";
static const string kWildCardHostAddressV4 = "0.0.0.0";

} // anonymous namespace

std::shared_ptr<MemTracker> CreateMemTrackerForServer() {
Expand Down Expand Up @@ -491,10 +488,10 @@ void RpcAndWebServerBase::GetStatusPB(ServerStatusPB* status) const {

Status RpcAndWebServerBase::GetRegistration(ServerRegistrationPB* reg, RpcOnly rpc_only) const {
std::vector<HostPort> addrs = CHECK_NOTNULL(rpc_server())->GetRpcHostPort();
DCHECK_GE(addrs.size(), 1);

// Fall back to hostname resolution if the rpc hostname is a wildcard.
if (addrs.size() > 1 || addrs[0].host() == kWildCardHostAddressV4 ||
addrs[0].host() == kWildCardHostAddressV6 || addrs[0].port() == 0) {
if (addrs.size() != 1 || IsWildcardAddress(addrs[0].host()) || addrs[0].port() == 0) {
vector<Endpoint> endpoints =
CHECK_NOTNULL(rpc_server())->GetBoundAddresses();
RETURN_NOT_OK_PREPEND(
Expand All @@ -513,15 +510,22 @@ Status RpcAndWebServerBase::GetRegistration(ServerRegistrationPB* reg, RpcOnly r
HostPortsToPBs(options_.broadcast_addresses, reg->mutable_broadcast_addresses());

if (!rpc_only) {
std::vector<Endpoint> web_addrs;
RETURN_NOT_OK_PREPEND(
CHECK_NOTNULL(web_server())->GetBoundAddresses(&web_addrs),
"Unable to get bound HTTP addresses");
RETURN_NOT_OK_PREPEND(AddHostPortPBs(
web_addrs, reg->mutable_http_addresses()),
"Failed to add HTTP addresses to registration");
for (const auto &addr : reg->http_addresses()) {
LOG(INFO) << "Using http addresses: ( " << addr.ShortDebugString() << " )";
HostPort web_input_hp;
RETURN_NOT_OK(CHECK_NOTNULL(web_server())->GetInputHostPort(&web_input_hp));
if (IsWildcardAddress(web_input_hp.host()) || web_input_hp.port() == 0) {
std::vector<Endpoint> web_addrs;
RETURN_NOT_OK_PREPEND(
CHECK_NOTNULL(web_server())->GetBoundAddresses(&web_addrs),
"Unable to get bound HTTP addresses");
RETURN_NOT_OK_PREPEND(AddHostPortPBs(
web_addrs, reg->mutable_http_addresses()),
"Failed to add HTTP addresses to registration");
for (const auto &addr : reg->http_addresses()) {
LOG(INFO) << "Using http addresses: ( " << addr.ShortDebugString() << " )";
}
} else {
HostPortsToPBs({ web_input_hp }, reg->mutable_http_addresses());
LOG(INFO) << "Using http address " << reg->http_addresses(0).host();
}
}
reg->mutable_cloud_info()->set_placement_cloud(options_.placement_cloud());
Expand Down
27 changes: 22 additions & 5 deletions src/yb/server/webserver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ bool Webserver::IsSecure() const {
}

Status Webserver::BuildListenSpec(string* spec) const {
std::vector<Endpoint> addrs;
RETURN_NOT_OK(ParseAddressList(http_address_, 80, &addrs));
if (addrs.empty()) {
std::vector<Endpoint> endpoints;
RETURN_NOT_OK(ParseAddressList(http_address_, 80, &endpoints));
if (endpoints.empty()) {
return STATUS_FORMAT(
ConfigurationError,
"No IPs available for address $0", http_address_);
}
std::vector<string> parts;
for (const auto& addr : addrs) {
for (const auto& endpoint : endpoints) {
// Mongoose makes sockets with 's' suffixes accept SSL traffic only
parts.push_back(ToString(addr) + (IsSecure() ? "s" : ""));
parts.push_back(ToString(endpoint) + (IsSecure() ? "s" : ""));
}

JoinStrings(parts, ",", spec);
Expand Down Expand Up @@ -256,6 +256,23 @@ void Webserver::Stop() {
}
}

Status Webserver::GetInputHostPort(HostPort* hp) const {
std::vector<HostPort> parsed_hps;
RETURN_NOT_OK(HostPort::ParseStrings(
http_address_,
0 /* default port */,
&parsed_hps));

// Webserver always gets a single host:port specification from WebserverOptions.
DCHECK_EQ(parsed_hps.size(), 1);
if (parsed_hps.size() != 1) {
return STATUS(InvalidArgument, "Expected single host port in WebserverOptions host port");
}

*hp = parsed_hps[0];
return Status::OK();
}

Status Webserver::GetBoundAddresses(std::vector<Endpoint>* addrs_ptr) const {
if (!context_) {
return STATUS(IllegalState, "Not started");
Expand Down
4 changes: 4 additions & 0 deletions src/yb/server/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include <boost/thread/shared_mutex.hpp>
#include "yb/server/webserver_options.h"
#include "yb/util/net/sockaddr.h"
#include "yb/util/net/net_util.h"
#include "yb/util/status.h"
#include "yb/util/web_callback_registry.h"

Expand Down Expand Up @@ -81,6 +82,9 @@ class Webserver : public WebCallbackRegistry {
// bound to. Requires that the server has been Start()ed.
CHECKED_STATUS GetBoundAddresses(std::vector<Endpoint>* addrs) const;

// Return the single HostPort that this server was asked to bind on
CHECKED_STATUS GetInputHostPort(HostPort* hp) const;

virtual void RegisterPathHandler(const std::string& path, const std::string& alias,
const PathHandlerCallback& callback,
bool is_styled = true,
Expand Down
10 changes: 9 additions & 1 deletion src/yb/util/net/net_util-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,19 @@ class NetUtilTest : public YBTest {

TEST(SockaddrTest, Test) {
boost::system::error_code ec;
auto address = IpAddress::from_string("1.1.1.1", ec);
const auto& kRegularAddr = "1.1.1.1";
auto address = IpAddress::from_string(kRegularAddr, ec);
ASSERT_FALSE(ec);
Endpoint endpoint(address, 12345);
ASSERT_EQ("1.1.1.1:12345", ToString(endpoint));
ASSERT_EQ(12345, endpoint.port());

ASSERT_FALSE(IsWildcardAddress(kRegularAddr));
const auto kWildcardAddr1 = "0.0.0.0";
ASSERT_TRUE(IsWildcardAddress(kWildcardAddr1));
const auto kWildcardAddr2 = "::";
ASSERT_TRUE(IsWildcardAddress(kWildcardAddr2));
ASSERT_FALSE(IsWildcardAddress("::1"));
}

TEST_F(NetUtilTest, TestParseAddresses) {
Expand Down
6 changes: 6 additions & 0 deletions src/yb/util/net/net_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,12 @@ Result<IpAddress> HostToAddress(const std::string& host) {
return addr;
}

bool IsWildcardAddress(const std::string& host_str) {
boost::system::error_code ec;
auto addr = IpAddress::from_string(host_str, ec);
return !ec && addr.is_unspecified();
}

boost::optional<IpAddress> TryFastResolve(const std::string& host) {
boost::system::error_code ec;
auto addr = IpAddress::from_string(host, ec);
Expand Down
3 changes: 3 additions & 0 deletions src/yb/util/net/net_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ CHECKED_STATUS HostToAddresses(
Result<IpAddress> HostToAddress(const std::string& host);
boost::optional<IpAddress> TryFastResolve(const std::string& host);

// Returns true if host_str is 0.0.0.0 or [::]
bool IsWildcardAddress(const std::string& host_str);

} // namespace yb

#endif // YB_UTIL_NET_NET_UTIL_H

0 comments on commit 1403b2b

Please sign in to comment.