Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

header_to_metadata: refactor to preferred style #29035

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void HeaderToMetadataFilter::setEncoderFilterCallbacks(
encoder_callbacks_ = &callbacks;
}

bool HeaderToMetadataFilter::addMetadata(StructMap& map, const std::string& meta_namespace,
bool HeaderToMetadataFilter::addMetadata(StructMap& struct_map, const std::string& meta_namespace,
const std::string& key, std::string value, ValueType type,
ValueEncode encode) const {
ProtobufWkt::Value val;
Expand Down Expand Up @@ -196,15 +196,8 @@ bool HeaderToMetadataFilter::addMetadata(StructMap& map, const std::string& meta
}
}

// Have we seen this namespace before?
auto namespace_iter = map.find(meta_namespace);
if (namespace_iter == map.end()) {
map[meta_namespace] = ProtobufWkt::Struct();
namespace_iter = map.find(meta_namespace);
}

auto& keyval = namespace_iter->second;
(*keyval.mutable_fields())[key] = val;
auto& keyval = struct_map[meta_namespace];
(*keyval.mutable_fields())[key] = std::move(val);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Rule::Rule(const ProtoRule& rule) : rule_(rule) {
}

if (rule.has_on_missing() && rule.on_missing().value().empty()) {
throw EnvoyException("Cannot specify on_missing rule without non-empty value");
throw EnvoyException("Cannot specify on_missing rule with empty value");
}

if (rule.has_on_present() && rule.on_present().has_regex_value_rewrite()) {
Expand Down Expand Up @@ -72,7 +72,7 @@ const std::string& HeaderToMetadataFilter::decideNamespace(const std::string& ns
return nspace.empty() ? headerToMetadata : nspace;
}

bool HeaderToMetadataFilter::addMetadata(StructMap& map, const std::string& meta_namespace,
bool HeaderToMetadataFilter::addMetadata(StructMap& struct_map, const std::string& meta_namespace,
const std::string& key, std::string value, ValueType type,
ValueEncode encode) const {
ProtobufWkt::Value val;
Expand Down Expand Up @@ -122,15 +122,8 @@ bool HeaderToMetadataFilter::addMetadata(StructMap& map, const std::string& meta
}
}

// Have we seen this namespace before?
auto namespace_iter = map.find(meta_namespace);
if (namespace_iter == map.end()) {
map[meta_namespace] = ProtobufWkt::Struct();
namespace_iter = map.find(meta_namespace);
}

auto& keyval = namespace_iter->second;
(*keyval.mutable_fields())[key] = val;
auto& keyval = struct_map[meta_namespace];
(*keyval.mutable_fields())[key] = std::move(val);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,8 @@ bool PayloadToMetadataFilter::addMetadata(const std::string& meta_namespace, con
}
}

// Have we seen this namespace before?
auto namespace_iter = structs_by_namespace_.find(meta_namespace);
if (namespace_iter == structs_by_namespace_.end()) {
structs_by_namespace_[meta_namespace] = ProtobufWkt::Struct();
namespace_iter = structs_by_namespace_.find(meta_namespace);
}

auto& keyval = namespace_iter->second;
(*keyval.mutable_fields())[key] = val;
auto& keyval = structs_by_namespace_[meta_namespace];
(*keyval.mutable_fields())[key] = std::move(val);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@ class PayloadToMetadataFilter : public MetadataHandler,
void handleOnMissing() override;

private:
static TransportPtr createTransport(TransportType transport) {
return NamedTransportConfigFactory::getFactory(transport).createTransport();
}

static ProtocolPtr createProtocol(ProtocolType protocol) {
return NamedProtocolConfigFactory::getFactory(protocol).createProtocol();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class HeaderToMetadataTest : public testing::Test {
TEST_F(HeaderToMetadataTest, BasicRequestTest) {
initializeFilter(request_config_yaml);
Http::TestRequestHeaderMapImpl incoming_headers{{"X-VERSION", "0xdeadbeef"}};
std::map<std::string, std::string> expected = {{"version", "0xdeadbeef"}};
const std::map<std::string, std::string> expected = {{"version", "0xdeadbeef"}};

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));
Expand All @@ -111,7 +111,7 @@ TEST_F(HeaderToMetadataTest, BasicRequestTest) {
TEST_F(HeaderToMetadataTest, BasicRequestDoubleHeadersTest) {
initializeFilter(request_config_yaml);
Http::TestRequestHeaderMapImpl incoming_headers{{"X-VERSION", "foo"}, {"X-VERSION", "bar"}};
std::map<std::string, std::string> expected = {{"version", "foo,bar"}};
const std::map<std::string, std::string> expected = {{"version", "foo,bar"}};

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));
Expand All @@ -129,7 +129,7 @@ TEST_F(HeaderToMetadataTest, PerRouteOverride) {
// Global config is empty.
initializeFilter("{}");
Http::TestRequestHeaderMapImpl incoming_headers{{"X-VERSION", "0xdeadbeef"}};
std::map<std::string, std::string> expected = {{"version", "0xdeadbeef"}};
const std::map<std::string, std::string> expected = {{"version", "0xdeadbeef"}};

// Setup per route config.
envoy::extensions::filters::http::header_to_metadata::v3::Config config_proto;
Expand All @@ -154,7 +154,7 @@ TEST_F(HeaderToMetadataTest, ConfigIsCached) {
// Global config is empty.
initializeFilter("{}");
Http::TestRequestHeaderMapImpl incoming_headers{{"X-VERSION", "0xdeadbeef"}};
std::map<std::string, std::string> expected = {{"version", "0xdeadbeef"}};
const std::map<std::string, std::string> expected = {{"version", "0xdeadbeef"}};

// Setup per route config.
envoy::extensions::filters::http::header_to_metadata::v3::Config config_proto;
Expand All @@ -173,7 +173,7 @@ TEST_F(HeaderToMetadataTest, ConfigIsCached) {
TEST_F(HeaderToMetadataTest, DefaultEndpointsTest) {
initializeFilter(request_config_yaml);
Http::TestRequestHeaderMapImpl incoming_headers{{"X-FOO", "bar"}};
std::map<std::string, std::string> expected = {{"default", "true"}};
const std::map<std::string, std::string> expected = {{"default", "true"}};

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));
Expand All @@ -194,7 +194,7 @@ TEST_F(HeaderToMetadataTest, HeaderRemovedTest) {
)EOF";
initializeFilter(response_config_yaml);
Http::TestResponseHeaderMapImpl incoming_headers{{"x-authenticated", "1"}};
std::map<std::string, std::string> expected = {{"auth", "1"}};
const std::map<std::string, std::string> expected = {{"auth", "1"}};
Http::TestResponseHeaderMapImpl empty_headers;

EXPECT_CALL(encoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
Expand Down Expand Up @@ -250,7 +250,7 @@ TEST_F(HeaderToMetadataTest, StringTypeInBase64UrlTest) {
std::string data = "Non-ascii-characters";
const auto encoded = Base64::encode(data.c_str(), data.size());
Http::TestResponseHeaderMapImpl incoming_headers{{"x-authenticated", encoded}};
std::map<std::string, std::string> expected = {{"auth", data}};
const std::map<std::string, std::string> expected = {{"auth", data}};
Http::TestResponseHeaderMapImpl empty_headers;

EXPECT_CALL(encoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
Expand Down Expand Up @@ -381,7 +381,8 @@ TEST_F(HeaderToMetadataTest, MultipleHeadersMatch) {
{"X-PYTHON-VERSION", "3.7"},
{"X-IGNORE", "nothing"},
};
std::map<std::string, std::string> expected = {{"version", "v4.0"}, {"python_version", "3.7"}};
const std::map<std::string, std::string> expected = {{"version", "v4.0"},
{"python_version", "3.7"}};

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));
Expand Down Expand Up @@ -428,7 +429,7 @@ TEST_F(HeaderToMetadataTest, IgnoreHeaderValueUseConstant) {
)EOF";
initializeFilter(response_config_yaml);
Http::TestResponseHeaderMapImpl incoming_headers{{"x-something", "thing"}};
std::map<std::string, std::string> expected = {{"something", "else"}};
const std::map<std::string, std::string> expected = {{"something", "else"}};
Http::TestResponseHeaderMapImpl empty_headers;

EXPECT_CALL(encoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
Expand Down Expand Up @@ -546,7 +547,7 @@ TEST_F(HeaderToMetadataTest, RegexSubstitution) {
// Match with additional path elements.
{
Http::TestRequestHeaderMapImpl headers{{":path", "/cluster-prod-001/x/y"}};
std::map<std::string, std::string> expected = {{"cluster", "cluster-prod-001"}};
const std::map<std::string, std::string> expected = {{"cluster", "cluster-prod-001"}};

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));
Expand All @@ -556,7 +557,7 @@ TEST_F(HeaderToMetadataTest, RegexSubstitution) {
// Match with no additional path elements.
{
Http::TestRequestHeaderMapImpl headers{{":path", "/cluster-prod-001"}};
std::map<std::string, std::string> expected = {{"cluster", "cluster-prod-001"}};
const std::map<std::string, std::string> expected = {{"cluster", "cluster-prod-001"}};

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));
Expand All @@ -566,7 +567,7 @@ TEST_F(HeaderToMetadataTest, RegexSubstitution) {
// No match.
{
Http::TestRequestHeaderMapImpl headers{{":path", "/foo"}};
std::map<std::string, std::string> expected = {{"cluster", "/foo"}};
const std::map<std::string, std::string> expected = {{"cluster", "/foo"}};

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));
Expand All @@ -576,7 +577,7 @@ TEST_F(HeaderToMetadataTest, RegexSubstitution) {
// No match with additional path elements.
{
Http::TestRequestHeaderMapImpl headers{{":path", "/foo/bar?x=2"}};
std::map<std::string, std::string> expected = {{"cluster", "/foo/bar?x=2"}};
const std::map<std::string, std::string> expected = {{"cluster", "/foo/bar?x=2"}};

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));
Expand Down Expand Up @@ -670,7 +671,7 @@ TEST_F(HeaderToMetadataTest, CookieValueUsed) {
)EOF";
initializeFilter(response_config_yaml);
Http::TestResponseHeaderMapImpl incoming_headers{{"cookie", "bar=foo"}};
std::map<std::string, std::string> expected = {{"bar", "foo"}};
const std::map<std::string, std::string> expected = {{"bar", "foo"}};

EXPECT_CALL(encoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_,
Expand All @@ -693,7 +694,7 @@ TEST_F(HeaderToMetadataTest, IgnoreCookieValueUseConstant) {
)EOF";
initializeFilter(response_config_yaml);
Http::TestResponseHeaderMapImpl incoming_headers{{"cookie", "meh=foo"}};
std::map<std::string, std::string> expected = {{"meh", "some_value"}};
const std::map<std::string, std::string> expected = {{"meh", "some_value"}};

EXPECT_CALL(encoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_,
Expand All @@ -716,7 +717,7 @@ TEST_F(HeaderToMetadataTest, NoCookieValue) {
)EOF";
initializeFilter(config);
Http::TestRequestHeaderMapImpl headers{{"cookie", ""}};
std::map<std::string, std::string> expected = {{"foo", "some_value"}};
const std::map<std::string, std::string> expected = {{"foo", "some_value"}};

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));
Expand Down Expand Up @@ -744,7 +745,7 @@ TEST_F(HeaderToMetadataTest, CookieRegexSubstitution) {
// match.
{
Http::TestRequestHeaderMapImpl headers{{"cookie", "foo=cluster-prod-001"}};
std::map<std::string, std::string> expected = {{"cluster", "cluster-prod-001 matched"}};
const std::map<std::string, std::string> expected = {{"cluster", "cluster-prod-001 matched"}};

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));
Expand All @@ -754,7 +755,7 @@ TEST_F(HeaderToMetadataTest, CookieRegexSubstitution) {
// No match.
{
Http::TestRequestHeaderMapImpl headers{{"cookie", "foo=cluster"}};
std::map<std::string, std::string> expected = {{"cluster", "cluster"}};
const std::map<std::string, std::string> expected = {{"cluster", "cluster"}};

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(req_info_));
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ TEST(HeaderToMetadataFilterConfigTest, OnHeaderMissingEmptyValue) {
type: STRING
)EOF";

testForbiddenConfig(yaml, "Cannot specify on_missing rule without non-empty value");
testForbiddenConfig(yaml, "Cannot specify on_missing rule with empty value");
}

} // namespace HeaderToMetadataFilter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ TEST_F(HeaderToMetadataTest, BasicRequestTest) {
key: version
)EOF";
initializeFilter(request_config_yaml);
std::map<std::string, std::string> expected = {{"version", "0xdeadbeef"}};
const std::map<std::string, std::string> expected = {{"version", "0xdeadbeef"}};
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));

auto metadata = std::make_shared<Extensions::NetworkFilters::ThriftProxy::MessageMetadata>();
Expand All @@ -92,7 +92,7 @@ TEST_F(HeaderToMetadataTest, DefaultNamespaceTest) {
key: version
)EOF";
initializeFilter(request_config_yaml);
std::map<std::string, std::string> expected = {{"version", "0xdeadbeef"}};
const std::map<std::string, std::string> expected = {{"version", "0xdeadbeef"}};
EXPECT_CALL(req_info_,
setDynamicMetadata("envoy.filters.thrift.header_to_metadata", MapEq(expected)));

Expand All @@ -113,7 +113,7 @@ TEST_F(HeaderToMetadataTest, ReplaceValueTest) {
value: world
)EOF";
initializeFilter(request_config_yaml);
std::map<std::string, std::string> expected = {{"replace", "world"}};
const std::map<std::string, std::string> expected = {{"replace", "world"}};
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));

auto metadata = std::make_shared<Extensions::NetworkFilters::ThriftProxy::MessageMetadata>();
Expand All @@ -137,7 +137,7 @@ TEST_F(HeaderToMetadataTest, SubstituteValueTest) {
substitution: "\\1"
)EOF";
initializeFilter(request_config_yaml);
std::map<std::string, std::string> expected = {{"subbed", "world"}};
const std::map<std::string, std::string> expected = {{"subbed", "world"}};
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));

auto metadata = std::make_shared<Extensions::NetworkFilters::ThriftProxy::MessageMetadata>();
Expand All @@ -161,7 +161,7 @@ TEST_F(HeaderToMetadataTest, NoMatchSubstituteValueTest) {
substitution: "\\1"
)EOF";
initializeFilter(request_config_yaml);
std::map<std::string, std::string> expected = {{"subbed", "does not match"}};
const std::map<std::string, std::string> expected = {{"subbed", "does not match"}};
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));

auto metadata = std::make_shared<Extensions::NetworkFilters::ThriftProxy::MessageMetadata>();
Expand Down Expand Up @@ -257,7 +257,7 @@ TEST_F(HeaderToMetadataTest, StringTypeInBase64UrlTest) {
)EOF";
initializeFilter(request_config_yaml);
std::string data = "Non-ascii-characters";
std::map<std::string, std::string> expected = {{"base64_key", data}};
const std::map<std::string, std::string> expected = {{"base64_key", data}};
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));

const auto encoded = Base64::encode(data.c_str(), data.size());
Expand Down Expand Up @@ -366,7 +366,7 @@ TEST_F(HeaderToMetadataTest, SetMissingValueTest) {
value: hi
)EOF";
initializeFilter(request_config_yaml);
std::map<std::string, std::string> expected = {{"set", "hi"}};
const std::map<std::string, std::string> expected = {{"set", "hi"}};
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));

auto metadata = std::make_shared<Extensions::NetworkFilters::ThriftProxy::MessageMetadata>();
Expand Down Expand Up @@ -413,7 +413,7 @@ TEST_F(HeaderToMetadataTest, RemoveHeaderTest) {
value: world
)EOF";
initializeFilter(request_config_yaml);
std::map<std::string, std::string> expected = {{"remove", "hello"}, {"keep", "world"}};
const std::map<std::string, std::string> expected = {{"remove", "hello"}, {"keep", "world"}};
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));

auto metadata = std::make_shared<Extensions::NetworkFilters::ThriftProxy::MessageMetadata>();
Expand Down Expand Up @@ -486,7 +486,7 @@ TEST_F(HeaderToMetadataTest, MultipleRulesTest) {
value: world
)EOF";
initializeFilter(request_config_yaml);
std::map<std::string, std::string> expected = {{"set", "hello"}, {"replace", "world"}};
const std::map<std::string, std::string> expected = {{"set", "hello"}, {"replace", "world"}};
EXPECT_CALL(req_info_, setDynamicMetadata("envoy.lb", MapEq(expected)));

auto metadata = std::make_shared<Extensions::NetworkFilters::ThriftProxy::MessageMetadata>();
Expand Down
Loading