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:
This is a port of 1403b2b to 2.1.8

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:
Jenkins: auto rebase: no

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: bogdan, raju

Differential Revision: https://phabricator.dev.yugabyte.com/D8976
  • Loading branch information
iSignal committed Jul 22, 2020
1 parent 3ba371f commit 791144c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 17 deletions.
39 changes: 28 additions & 11 deletions src/yb/server/server_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,19 @@ 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() == kWildCardHostAddress || addrs[0].port() == 0) {
auto addrs = CHECK_NOTNULL(rpc_server())->GetBoundAddresses();
RETURN_NOT_OK_PREPEND(AddHostPortPBs(addrs, reg->mutable_private_rpc_addresses()),
"Failed to add RPC endpoints to registration");
if (addrs.size() != 1 || IsWildcardAddress(addrs[0].host()) || addrs[0].port() == 0) {
vector<Endpoint> endpoints =
CHECK_NOTNULL(rpc_server())->GetBoundAddresses();
RETURN_NOT_OK_PREPEND(
AddHostPortPBs(endpoints, reg->mutable_private_rpc_addresses()),
"Failed to add RPC endpoints to registration");
for (const auto &addr : reg->private_rpc_addresses()) {
LOG(INFO) << " Using private rpc addresses: ( " << addr.ShortDebugString()
<< " )";
}
} else {
HostPortsToPBs(addrs, reg->mutable_private_rpc_addresses());
LOG(INFO) << "Using private ip address " << reg->private_rpc_addresses(0).host();
Expand All @@ -504,13 +511,23 @@ 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");
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());
reg->mutable_cloud_info()->set_placement_region(options_.placement_region());
Expand Down
31 changes: 26 additions & 5 deletions src/yb/server/webserver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,17 @@ bool Webserver::IsSecure() const {
}

Status Webserver::BuildListenSpec(string* spec) const {
std::vector<Endpoint> addrs;
RETURN_NOT_OK(ParseAddressList(http_address_, 80, &addrs));

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 @@ -249,6 +253,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 @@ -67,11 +67,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 @@ -557,6 +557,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 @@ -206,6 +206,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 791144c

Please sign in to comment.