Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/istio/control/http/check_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class CheckData {
// Returns true if connection is mutual TLS enabled.
virtual bool IsMutualTLS() const = 0;

// Get requested server name, SNI in case of TLS
virtual std::string GetRequestedServerName() const = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there are cases data is not available, the coding style is to return a bool. It should be:

// return true if data is valid.
bool GetRequestedServerName(std::string* name);


// These headers are extracted into top level attributes.
// This is for standard HTTP headers. It supports both HTTP/1.1 and HTTP2
// They can be retrieved at O(1) speed by environment (Envoy).
Expand Down
3 changes: 3 additions & 0 deletions include/istio/control/tcp/check_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class CheckData {
// Returns true if connection is mutual TLS enabled.
virtual bool IsMutualTLS() const = 0;

// Get requested server name, SNI in case of TLS
virtual std::string GetRequestedServerName() const = 0;

// Get downstream tcp connection id.
virtual std::string GetConnectionId() const = 0;
};
Expand Down
1 change: 1 addition & 0 deletions include/istio/utils/attribute_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct AttributeName {
static const char kConnectionSendTotalBytes[];
static const char kConnectionDuration[];
static const char kConnectionMtls[];
static const char kConnectionRequestedServerName[];
static const char kConnectionId[];
// Record TCP connection status: open, continue, close
static const char kConnectionEvent[];
Expand Down
8 changes: 8 additions & 0 deletions src/envoy/http/mixer/check_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ std::map<std::string, std::string> CheckData::GetRequestHeaders() const {

bool CheckData::IsMutualTLS() const { return Utils::IsMutualTLS(connection_); }

std::string CheckData::GetRequestedServerName() const {
if (connection_) {
return std::string(connection_->requestedServerName());
}

return "";
}

bool CheckData::FindHeaderByType(HttpCheckData::HeaderType header_type,
std::string* value) const {
switch (header_type) {
Expand Down
1 change: 1 addition & 0 deletions src/envoy/http/mixer/check_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CheckData : public ::istio::control::http::CheckData,
std::map<std::string, std::string> GetRequestHeaders() const override;

bool IsMutualTLS() const override;
std::string GetRequestedServerName() const override;

bool FindHeaderByType(
::istio::control::http::CheckData::HeaderType header_type,
Expand Down
4 changes: 4 additions & 0 deletions src/envoy/tcp/mixer/filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ bool Filter::IsMutualTLS() const {
return Utils::IsMutualTLS(&filter_callbacks_->connection());
}

std::string Filter::GetRequestedServerName() const {
return std::string(filter_callbacks_->connection().requestedServerName());
}

bool Filter::GetDestinationIpPort(std::string* str_ip, int* port) const {
if (filter_callbacks_->upstreamHost() &&
filter_callbacks_->upstreamHost()->address()) {
Expand Down
1 change: 1 addition & 0 deletions src/envoy/tcp/mixer/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Filter : public Network::Filter,
bool GetSourceIpPort(std::string* str_ip, int* port) const override;
bool GetSourceUser(std::string* user) const override;
bool IsMutualTLS() const override;
std::string GetRequestedServerName() const override;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to apply the same changes to http filter in src/envoy/http/mixer


// ReportData virtual functions.
bool GetDestinationIpPort(std::string* str_ip, int* port) const override;
Expand Down
6 changes: 6 additions & 0 deletions src/istio/control/http/attributes_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ void AttributesBuilder::ExtractCheckAttributes(CheckData *check_data) {
builder.AddBool(utils::AttributeName::kConnectionMtls,
check_data->IsMutualTLS());

std::string requested_server_name = check_data->GetRequestedServerName();
if (!requested_server_name.empty()) {
builder.AddString(utils::AttributeName::kConnectionRequestedServerName,
requested_server_name);
}

builder.AddTimestamp(utils::AttributeName::kRequestTime,
std::chrono::system_clock::now());

Expand Down
10 changes: 10 additions & 0 deletions src/istio/control/http/attributes_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ attributes {
bool_value: true
}
}
attributes {
key: "connection.requested_server_name"
value {
string_value: "www.google.com"
}
}
attributes {
key: "source.principal"
value {
Expand Down Expand Up @@ -286,6 +292,8 @@ TEST(AttributesBuilderTest, TestCheckAttributes) {
EXPECT_CALL(mock_data, IsMutualTLS()).WillOnce(Invoke([]() -> bool {
return true;
}));
EXPECT_CALL(mock_data, GetRequestedServerName())
.WillOnce(testing::Return("www.google.com"));
EXPECT_CALL(mock_data, GetRequestHeaders())
.WillOnce(Invoke([]() -> std::map<std::string, std::string> {
std::map<std::string, std::string> map;
Expand Down Expand Up @@ -341,6 +349,8 @@ TEST(AttributesBuilderTest, TestCheckAttributesWithAuthNResult) {
EXPECT_CALL(mock_data, IsMutualTLS()).WillOnce(Invoke([]() -> bool {
return true;
}));
EXPECT_CALL(mock_data, GetRequestedServerName())
.WillOnce(testing::Return("www.google.com"));
EXPECT_CALL(mock_data, GetRequestHeaders())
.WillOnce(Invoke([]() -> std::map<std::string, std::string> {
std::map<std::string, std::string> map;
Expand Down
1 change: 1 addition & 0 deletions src/istio/control/http/mock_check_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class MockCheckData : public CheckData {
MOCK_CONST_METHOD1(GetAuthenticationResult,
bool(istio::authn::Result *result));
MOCK_CONST_METHOD0(IsMutualTLS, bool());
MOCK_CONST_METHOD0(GetRequestedServerName, std::string());
};

// The mock object for HeaderUpdate interface.
Expand Down
6 changes: 6 additions & 0 deletions src/istio/control/tcp/attributes_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ void AttributesBuilder::ExtractCheckAttributes(CheckData* check_data) {
builder.AddBool(utils::AttributeName::kConnectionMtls,
check_data->IsMutualTLS());

std::string requested_server_name = check_data->GetRequestedServerName();
if (!requested_server_name.empty()) {
builder.AddString(utils::AttributeName::kConnectionRequestedServerName,
requested_server_name);
}

builder.AddTimestamp(utils::AttributeName::kContextTime,
std::chrono::system_clock::now());
builder.AddString(utils::AttributeName::kContextProtocol, "tcp");
Expand Down
8 changes: 8 additions & 0 deletions src/istio/control/tcp/attributes_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ attributes {
bool_value: true
}
}
attributes {
key: "connection.requested_server_name"
value {
string_value: "www.google.com"
}
}
attributes {
key: "source.principal"
value {
Expand Down Expand Up @@ -305,6 +311,8 @@ TEST(AttributesBuilderTest, TestCheckAttributes) {
return true;
}));
EXPECT_CALL(mock_data, GetConnectionId()).WillOnce(Return("1234-5"));
EXPECT_CALL(mock_data, GetRequestedServerName())
.WillOnce(Return("www.google.com"));

RequestContext request;
AttributesBuilder builder(&request);
Expand Down
1 change: 1 addition & 0 deletions src/istio/control/tcp/mock_check_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MockCheckData : public CheckData {
MOCK_CONST_METHOD2(GetSourceIpPort, bool(std::string* ip, int* port));
MOCK_CONST_METHOD1(GetSourceUser, bool(std::string* user));
MOCK_CONST_METHOD0(IsMutualTLS, bool());
MOCK_CONST_METHOD0(GetRequestedServerName, std::string());
MOCK_CONST_METHOD0(GetConnectionId, std::string());
};

Expand Down
3 changes: 3 additions & 0 deletions src/istio/utils/attribute_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const char AttributeName::kConnectionSendTotalBytes[] =
"connection.sent.bytes_total";
const char AttributeName::kConnectionDuration[] = "connection.duration";
const char AttributeName::kConnectionMtls[] = "connection.mtls";
const char AttributeName::kConnectionRequestedServerName[] =
"connection.requested_server_name";

// Downstream TCP connection id.
const char AttributeName::kConnectionId[] = "connection.id";
const char AttributeName::kConnectionEvent[] = "connection.event";
Expand Down