Skip to content

Stringmatcher: factory context for all remaining #33058

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

Merged
merged 8 commits into from
Mar 22, 2024
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
11 changes: 7 additions & 4 deletions contrib/checksum/filters/http/source/checksum_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ namespace HttpFilters {
namespace ChecksumFilter {

static std::vector<ChecksumFilterConfig::ChecksumMatcher> buildMatchers(
const envoy::extensions::filters::http::checksum::v3alpha::ChecksumConfig& proto_config) {
const envoy::extensions::filters::http::checksum::v3alpha::ChecksumConfig& proto_config,
Server::Configuration::CommonFactoryContext& context) {
std::vector<ChecksumFilterConfig::ChecksumMatcher> matchers;
for (const auto& checksum : proto_config.checksums()) {
std::vector<uint8_t> bytes = Hex::decode(checksum.sha256());
matchers.emplace_back(std::make_unique<Matchers::PathMatcher>(checksum.path_matcher()),
matchers.emplace_back(std::make_unique<Matchers::PathMatcher>(checksum.path_matcher(), context),
Sha256Checksum{bytes.begin(), bytes.end()});
}
return matchers;
}

ChecksumFilterConfig::ChecksumFilterConfig(
const envoy::extensions::filters::http::checksum::v3alpha::ChecksumConfig& proto_config)
: matchers_(buildMatchers(proto_config)), reject_unmatched_(proto_config.reject_unmatched()) {}
const envoy::extensions::filters::http::checksum::v3alpha::ChecksumConfig& proto_config,
Server::Configuration::CommonFactoryContext& context)
: matchers_(buildMatchers(proto_config, context)),
reject_unmatched_(proto_config.reject_unmatched()) {}

OptRef<const Sha256Checksum> ChecksumFilterConfig::expectedChecksum(absl::string_view path) {
for (const auto& m : matchers_) {
Expand Down
3 changes: 2 additions & 1 deletion contrib/checksum/filters/http/source/checksum_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class ChecksumFilterConfig {
using ChecksumMatcher = std::pair<std::unique_ptr<Matchers::PathMatcher>, Sha256Checksum>;

ChecksumFilterConfig(
const envoy::extensions::filters::http::checksum::v3alpha::ChecksumConfig& proto_config);
const envoy::extensions::filters::http::checksum::v3alpha::ChecksumConfig& proto_config,
Server::Configuration::CommonFactoryContext& context);

bool rejectUnmatched() const { return reject_unmatched_; }
// Returns nullopt on no match.
Expand Down
5 changes: 3 additions & 2 deletions contrib/checksum/filters/http/source/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ namespace ChecksumFilter {

Http::FilterFactoryCb ChecksumFilterFactory::createFilterFactoryFromProtoTyped(
const envoy::extensions::filters::http::checksum::v3alpha::ChecksumConfig& proto_config,
const std::string&, Server::Configuration::FactoryContext&) {
ChecksumFilterConfigSharedPtr filter_config(new ChecksumFilterConfig(proto_config));
const std::string&, Server::Configuration::FactoryContext& context) {
ChecksumFilterConfigSharedPtr filter_config(
new ChecksumFilterConfig(proto_config, context.serverFactoryContext()));
return [filter_config](Http::FilterChainFactoryCallbacks& callbacks) -> void {
callbacks.addStreamFilter(std::make_shared<ChecksumFilter>(filter_config));
};
Expand Down
1 change: 1 addition & 0 deletions contrib/checksum/filters/http/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ envoy_cc_test(
"//source/common/buffer:buffer_lib",
"//source/common/http:header_map_lib",
"//test/mocks/http:http_mocks",
"//test/mocks/server:server_factory_context_mocks",
"//test/mocks/upstream:upstream_mocks",
"//test/test_common:utility_lib",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "source/common/http/header_map_impl.h"

#include "test/mocks/http/mocks.h"
#include "test/mocks/server/server_factory_context.h"
#include "test/test_common/printers.h"
#include "test/test_common/utility.h"

Expand Down Expand Up @@ -32,14 +33,15 @@ class ChecksumFilterTest : public testing::Test {
// The sha256 of the string "banana"
entry->set_sha256("b493d48364afe44d11c0165cf470a4164d1e2609911ef998be868d46ade3de4e");
}
config_ = std::make_shared<ChecksumFilterConfig>(config);
config_ = std::make_shared<ChecksumFilterConfig>(config, context_);
filter_ = std::make_unique<ChecksumFilter>(config_);
filter_->setDecoderFilterCallbacks(decoder_callbacks_);
filter_->setEncoderFilterCallbacks(encoder_callbacks_);
}

~ChecksumFilterTest() override { filter_->onDestroy(); }

NiceMock<Server::Configuration::MockServerFactoryContext> context_;
std::shared_ptr<ChecksumFilterConfig> config_;
std::unique_ptr<ChecksumFilter> filter_;
NiceMock<Http::MockStreamDecoderFilterCallbacks> decoder_callbacks_;
Expand Down
17 changes: 9 additions & 8 deletions contrib/generic_proxy/filters/network/source/match.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@ REGISTER_FACTORY(PropertyMatchDataInputFactory, Matcher::DataInputFactory<Reques

REGISTER_FACTORY(RequestMatchDataInputFactory, Matcher::DataInputFactory<Request>);

using StringMatcherImpl = Matchers::StringMatcherImpl<StringMatcherProto>;
using StringMatcherImpl = Matchers::StringMatcherImplWithContext<StringMatcherProto>;

RequestMatchInputMatcher::RequestMatchInputMatcher(const RequestMatcherProto& proto_config) {
RequestMatchInputMatcher::RequestMatchInputMatcher(
const RequestMatcherProto& proto_config, Server::Configuration::CommonFactoryContext& context) {

if (proto_config.has_host()) {
host_ = std::make_unique<StringMatcherImpl>(proto_config.host());
host_ = std::make_unique<StringMatcherImpl>(proto_config.host(), context);
}
if (proto_config.has_path()) {
path_ = std::make_unique<StringMatcherImpl>(proto_config.path());
path_ = std::make_unique<StringMatcherImpl>(proto_config.path(), context);
}
if (proto_config.has_method()) {
method_ = std::make_unique<StringMatcherImpl>(proto_config.method());
method_ = std::make_unique<StringMatcherImpl>(proto_config.method(), context);
}

for (const auto& property : proto_config.properties()) {
properties_.push_back(
{property.name(), std::make_unique<StringMatcherImpl>(property.string_match())});
{property.name(), std::make_unique<StringMatcherImpl>(property.string_match(), context)});
}
}

Expand Down Expand Up @@ -96,8 +97,8 @@ Matcher::InputMatcherFactoryCb RequestMatchDataInputMatcherFactory::createInputM
const auto& proto_config = MessageUtil::downcastAndValidate<const RequestMatcherProto&>(
config, factory_context.messageValidationVisitor());

return [proto_config]() -> Matcher::InputMatcherPtr {
return std::make_unique<RequestMatchInputMatcher>(proto_config);
return [proto_config, &factory_context]() -> Matcher::InputMatcherPtr {
return std::make_unique<RequestMatchInputMatcher>(proto_config, factory_context);
};
}

Expand Down
3 changes: 2 additions & 1 deletion contrib/generic_proxy/filters/network/source/match.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ class RequestMatchDataInputFactory : public Matcher::DataInputFactory<Request> {

class RequestMatchInputMatcher : public Matcher::InputMatcher {
public:
RequestMatchInputMatcher(const RequestMatcherProto& config);
RequestMatchInputMatcher(const RequestMatcherProto& config,
Server::Configuration::CommonFactoryContext& context);

bool match(const Matcher::MatchingDataType& input) override;

Expand Down
13 changes: 7 additions & 6 deletions contrib/generic_proxy/filters/network/test/match_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ TEST(RequestMatchInputMatcherTest, RequestMatchInputMatcherTest) {
}

TEST(RequestMatchInputMatcherTest, SpecificRequestMatchInputMatcherTest) {
NiceMock<Server::Configuration::MockFactoryContext> context;
// Empty matcher.
{
RequestMatcherProto matcher_proto;
RequestMatchInputMatcher matcher(matcher_proto);
RequestMatchInputMatcher matcher(matcher_proto, context.serverFactoryContext());

FakeStreamCodecFactory::FakeRequest request;
EXPECT_TRUE(matcher.match(request));
Expand All @@ -160,7 +161,7 @@ TEST(RequestMatchInputMatcherTest, SpecificRequestMatchInputMatcherTest) {

TestUtility::loadFromYaml(config_yaml, matcher_proto);

RequestMatchInputMatcher matcher(matcher_proto);
RequestMatchInputMatcher matcher(matcher_proto, context.serverFactoryContext());

FakeStreamCodecFactory::FakeRequest request;
request.host_ = "another_fake_host";
Expand All @@ -180,7 +181,7 @@ TEST(RequestMatchInputMatcherTest, SpecificRequestMatchInputMatcherTest) {

TestUtility::loadFromYaml(config_yaml, matcher_proto);

RequestMatchInputMatcher matcher(matcher_proto);
RequestMatchInputMatcher matcher(matcher_proto, context.serverFactoryContext());

FakeStreamCodecFactory::FakeRequest request;
request.host_ = "fake_host";
Expand All @@ -203,7 +204,7 @@ TEST(RequestMatchInputMatcherTest, SpecificRequestMatchInputMatcherTest) {

TestUtility::loadFromYaml(config_yaml, matcher_proto);

RequestMatchInputMatcher matcher(matcher_proto);
RequestMatchInputMatcher matcher(matcher_proto, context.serverFactoryContext());

FakeStreamCodecFactory::FakeRequest request;
request.host_ = "fake_host";
Expand Down Expand Up @@ -231,7 +232,7 @@ TEST(RequestMatchInputMatcherTest, SpecificRequestMatchInputMatcherTest) {

TestUtility::loadFromYaml(config_yaml, matcher_proto);

RequestMatchInputMatcher matcher(matcher_proto);
RequestMatchInputMatcher matcher(matcher_proto, context.serverFactoryContext());

FakeStreamCodecFactory::FakeRequest request;
request.host_ = "fake_host";
Expand Down Expand Up @@ -260,7 +261,7 @@ TEST(RequestMatchInputMatcherTest, SpecificRequestMatchInputMatcherTest) {

TestUtility::loadFromYaml(config_yaml, matcher_proto);

RequestMatchInputMatcher matcher(matcher_proto);
RequestMatchInputMatcher matcher(matcher_proto, context.serverFactoryContext());

FakeStreamCodecFactory::FakeRequest request;
request.host_ = "fake_host";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Router {
RouteEntryImpl::RouteEntryImpl(
const envoy::extensions::filters::network::rocketmq_proxy::v3::Route& route,
Server::Configuration::CommonFactoryContext& context)
: topic_name_(route.match().topic()), cluster_name_(route.route().cluster()),
: topic_name_(route.match().topic(), context), cluster_name_(route.route().cluster()),
config_headers_(
Http::HeaderUtility::buildHeaderDataVector(route.match().headers(), context)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RouteEntryImpl : public RouteEntry,
private:
bool headersMatch(const Http::HeaderMap& headers) const;

const Matchers::StringMatcherImpl<envoy::type::matcher::v3::StringMatcher> topic_name_;
const Matchers::StringMatcherImplWithContext<envoy::type::matcher::v3::StringMatcher> topic_name_;
const std::string cluster_name_;
const std::vector<Http::HeaderUtility::HeaderDataPtr> config_headers_;
Envoy::Router::MetadataMatchCriteriaConstPtr metadata_match_criteria_;
Expand Down
9 changes: 5 additions & 4 deletions source/common/access_log/access_log_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ FilterPtr FilterFactory::fromProto(const envoy::config::accesslog::v3::AccessLog
MessageUtil::validate(config, validation_visitor);
return FilterPtr{new GrpcStatusFilter(config.grpc_status_filter())};
case envoy::config::accesslog::v3::AccessLogFilter::FilterSpecifierCase::kMetadataFilter:
return FilterPtr{new MetadataFilter(config.metadata_filter())};
return FilterPtr{new MetadataFilter(config.metadata_filter(), context.serverFactoryContext())};
case envoy::config::accesslog::v3::AccessLogFilter::FilterSpecifierCase::kLogTypeFilter:
return FilterPtr{new LogTypeFilter(config.log_type_filter())};
case envoy::config::accesslog::v3::AccessLogFilter::FilterSpecifierCase::kExtensionFilter:
Expand Down Expand Up @@ -290,7 +290,8 @@ bool LogTypeFilter::evaluate(const Formatter::HttpFormatterContext& context,
return exclude_ ? !found : found;
}

MetadataFilter::MetadataFilter(const envoy::config::accesslog::v3::MetadataFilter& filter_config)
MetadataFilter::MetadataFilter(const envoy::config::accesslog::v3::MetadataFilter& filter_config,
Server::Configuration::CommonFactoryContext& context)
: default_match_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(filter_config, match_if_key_not_found, true)),
filter_(filter_config.matcher().filter()) {

Expand All @@ -303,13 +304,13 @@ MetadataFilter::MetadataFilter(const envoy::config::accesslog::v3::MetadataFilte

// Matches if the value equals the configured 'MetadataMatcher' value.
const auto& val = matcher_config.value();
value_matcher_ = Matchers::ValueMatcher::create(val);
value_matcher_ = Matchers::ValueMatcher::create(val, context);
}

// Matches if the value is present in dynamic metadata
auto present_val = envoy::type::matcher::v3::ValueMatcher();
present_val.set_present_match(true);
present_matcher_ = Matchers::ValueMatcher::create(present_val);
present_matcher_ = Matchers::ValueMatcher::create(present_val, context);
}

bool MetadataFilter::evaluate(const Formatter::HttpFormatterContext&,
Expand Down
3 changes: 2 additions & 1 deletion source/common/access_log/access_log_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ class LogTypeFilter : public Filter {
*/
class MetadataFilter : public Filter {
public:
MetadataFilter(const envoy::config::accesslog::v3::MetadataFilter& filter_config);
MetadataFilter(const envoy::config::accesslog::v3::MetadataFilter& filter_config,
Server::Configuration::CommonFactoryContext& context);

bool evaluate(const Formatter::HttpFormatterContext& context,
const StreamInfo::StreamInfo& info) const override;
Expand Down
Loading