From 2ef47c457311326ca2b34c7022af708dbd11fad0 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 27 Jun 2022 16:54:24 +0000 Subject: [PATCH 001/238] Added RouteUrlRewritePattern Signed-off-by: silverstar195 --- .../config/route/v3/route_components.proto | 16 + source/common/common/BUILD | 1 + source/common/common/matching/BUILD | 89 ++++ .../common/matching/url_template_matching.cc | 165 +++++++ .../common/matching/url_template_matching.h | 60 +++ .../url_template_matching_internal.cc | 381 ++++++++++++++ .../matching/url_template_matching_internal.h | 82 +++ .../url_template_matching_internal_test.cc | 466 ++++++++++++++++++ .../matching/url_template_matching_test.cc | 335 +++++++++++++ 9 files changed, 1595 insertions(+) create mode 100644 source/common/common/matching/BUILD create mode 100644 source/common/common/matching/url_template_matching.cc create mode 100644 source/common/common/matching/url_template_matching.h create mode 100644 source/common/common/matching/url_template_matching_internal.cc create mode 100644 source/common/common/matching/url_template_matching_internal.h create mode 100644 source/common/common/matching/url_template_matching_internal_test.cc create mode 100644 source/common/common/matching/url_template_matching_test.cc diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index 2a72c2f546515..1e135c2b37584 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -1362,6 +1362,22 @@ message RouteAction { MaxStreamDuration max_stream_duration = 36; } +// Holds the segments for rewrite +message RouteUrlRewritePattern { + message RewriteSegment { + oneof segment_type { + // Represents a segment of the rewritten URL, including any path segments, + // slash and prefix. + string literal = 1; + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int32 var_index = 2; + } + } + repeated RewriteSegment segments = 3; +} + // HTTP retry :ref:`architecture overview `. // [#next-free-field: 14] message RetryPolicy { diff --git a/source/common/common/BUILD b/source/common/common/BUILD index 0f6d825d52542..acf229c5a0303 100644 --- a/source/common/common/BUILD +++ b/source/common/common/BUILD @@ -297,6 +297,7 @@ envoy_cc_library( ":utility_lib", "//envoy/common:matchers_interface", "//source/common/common:regex_lib", + "//source/common/common/matching:url_template_matching", "//source/common/config:metadata_lib", "//source/common/http:path_utility_lib", "//source/common/protobuf", diff --git a/source/common/common/matching/BUILD b/source/common/common/matching/BUILD new file mode 100644 index 0000000000000..9b7d044a3b2de --- /dev/null +++ b/source/common/common/matching/BUILD @@ -0,0 +1,89 @@ +# BDN Wildcard & Pattern Matching as per go/wildcard-matching-ug + +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_extension", + "envoy_cc_library", + "envoy_cc_test", + "envoy_extension_package", +) + +licenses(["notice"]) # Apache 2 + +envoy_extension_package() + +envoy_cc_library( + name = "url_template_matching", + srcs = ["url_template_matching.cc"], + hdrs = ["url_template_matching.h"], + visibility = [ + "//source/common/common:__subpackages__", + "//test/common/common:__subpackages__", + ], + deps = [ + ":url_template_matching_internal_cc", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_googlesource_code_re2//:re2", + ], +) + +envoy_cc_library( + name = "url_template_matching_internal_cc", + srcs = ["url_template_matching_internal.cc"], + hdrs = ["url_template_matching_internal.h"], + deps = [ + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/functional:function_ref", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + "@com_google_absl//absl/types:variant", + "@com_googlesource_code_re2//:re2", + ], +) + +envoy_cc_test( + name = "url_template_matching_test", + srcs = ["url_template_matching_test.cc"], + deps = [ + ":url_template_matching", + "//test/test_common:status_utility_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + ], +) + +envoy_cc_test( + name = "url_template_matching_internal_test", + srcs = ["url_template_matching_internal_test.cc"], + deps = [ + ":url_template_matching_internal_cc", + "//test/test_common:status_utility_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_googlesource_code_re2//:re2", + ], +) + +envoy_cc_test( + name = "url_template_benchmark_test", + srcs = ["url_template_benchmark_test.cc"], + deps = [ + ":url_template_matching", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@envoy_api//envoy/src/api/envoy/config/route/v3:pkg_cc", + "@envoy_api//envoy/src/api/envoy/config/route/v3:pkg_cc_proto_library", + "@envoy_api//envoy/src/source/common/common:assert_lib", + "@envoy_api//envoy/src/source/common/router:config_lib", + "@envoy_api//envoy/src/test/mocks/server:instance_mocks", + "@envoy_api//envoy/src/test/mocks/stream_info:stream_info_mocks", + "@envoy_api//envoy/src/test/test_common:utility_lib", + ], +) diff --git a/source/common/common/matching/url_template_matching.cc b/source/common/common/matching/url_template_matching.cc new file mode 100644 index 0000000000000..2b68daeb3b551 --- /dev/null +++ b/source/common/common/matching/url_template_matching.cc @@ -0,0 +1,165 @@ +#include "source/common/common/matching/url_template_matching.h" + +// hack for now +// Silence warnings about missing initializers for members of LazyRE2. +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" + +#include +#include +#include +#include + +#include "envoy/config/route/v3/route_components.pb.h" + +#include "source/common/common/matching/url_template_matching_internal.h" +#include "re2/re2.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "absl/strings/str_split.h" + +namespace matching { + +using ::matching::url_template_matching_internal::ParsedUrlPattern; + +inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.data(), text.size()}; } + +bool IsPatternMatch(absl::string_view pattern, absl::string_view capture_regex) { + RE2 regex = RE2(ToStringPiece(capture_regex)); + return RE2::FullMatch(ToStringPiece(pattern), regex); +} + +absl::StatusOr ConvertURLPatternSyntaxToRegex(absl::string_view url_pattern) { + + absl::StatusOr status = + url_template_matching_internal::ParseURLPatternSyntax(url_pattern); + if (!status.ok()) { + return status.status(); + } + struct ParsedUrlPattern pattern = *std::move(status); + return url_template_matching_internal::ToRegexPattern(pattern); +} + +absl::StatusOr> +ParseRewritePatternHelper(absl::string_view pattern) { + std::vector result; + + // Don't allow contiguous '/' patterns. + static const LazyRE2 invalid_regex = {"^.*//.*$"}; + if (RE2::FullMatch(ToStringPiece(pattern), *invalid_regex)) { + return absl::InvalidArgumentError("Invalid rewrite literal pattern"); + } + + // The pattern should start with a '/' and thus the first segment should + // always be a literal. + if (pattern.empty() || pattern[0] != '/') { + return absl::InvalidArgumentError("Invalid rewrite variable placement"); + } + while (!pattern.empty()) { + std::vector segments1 = absl::StrSplit(pattern, absl::MaxSplits('{', 1)); + if (!segments1[0].empty()) { + if (!url_template_matching_internal::IsValidRewriteLiteral(segments1[0])) { + return absl::InvalidArgumentError("Invalid rewrite literal pattern"); + } + result.emplace_back(segments1[0], RewriteStringKind::kLiteral); + } + + if (segments1.size() < 2) { + // No more variable replacement, done. + break; + } + + std::vector segments2 = + absl::StrSplit(segments1[1], absl::MaxSplits('}', 1)); + if (segments2.size() < 2) { + return absl::InvalidArgumentError("Unmatched variable bracket"); + } + pattern = segments2[1]; + + if (!url_template_matching_internal::IsValidIdent(segments2[0])) { + return absl::InvalidArgumentError("Invalid variable name"); + } + result.emplace_back(segments2[0], RewriteStringKind::kVariable); + } + return result; +} + +absl::StatusOr +ParseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { + envoy::config::route::v3::RouteUrlRewritePattern parsed_pattern; + RE2 regex = RE2(ToStringPiece(capture_regex)); + if (!regex.ok()) { + return absl::InternalError(regex.error()); + } + + absl::StatusOr> status = ParseRewritePatternHelper(pattern); + if (!status.ok()) { + return status.status(); + } + std::vector processed_pattern = *std::move(status); + + const std::map& capture_index_map = regex.NamedCapturingGroups(); + + for (const auto& [str, kind] : processed_pattern) { + switch (kind) { + case RewriteStringKind::kLiteral: + parsed_pattern.add_segments()->set_literal(std::string(str)); + break; + case RewriteStringKind::kVariable: + auto it = capture_index_map.find(std::string(str)); + if (it == capture_index_map.end()) { + return absl::InvalidArgumentError("Nonexisting variable name"); + } + parsed_pattern.add_segments()->set_var_index(it->second); + break; + } + } + + return parsed_pattern; +} + +absl::StatusOr +RewriteURLTemplatePattern(absl::string_view url, absl::string_view capture_regex, + const envoy::config::route::v3::RouteUrlRewritePattern& rewrite_pattern) { + RE2 regex = RE2(ToStringPiece(capture_regex)); + if (!regex.ok()) { + return absl::InternalError(regex.error()); + } + // First capture is the whole matched regex pattern. + int capture_num = regex.NumberOfCapturingGroups() + 1; + std::vector captures(capture_num); + if (!regex.Match(ToStringPiece(url), /*startpos=*/0, /*endpos=*/url.size(), RE2::ANCHOR_BOTH, + captures.data(), captures.size())) { + return absl::InvalidArgumentError("Pattern not match"); + } + + std::string rewritten_url; + + for (const envoy::config::route::v3::RouteUrlRewritePattern::RewriteSegment& segment : + rewrite_pattern.segments()) { + if (segment.has_literal()) { + absl::StrAppend(&rewritten_url, segment.literal()); + } else if (segment.has_var_index()) { + if (segment.var_index() < 1 || segment.var_index() >= capture_num) { + return absl::InvalidArgumentError("Invalid variable index"); + } + absl::StrAppend(&rewritten_url, absl::string_view(captures[segment.var_index()].as_string())); + } + } + + return rewritten_url; +} + +bool IsValidPathTemplateMatchPattern(const std::string& path_template_match) { + return ConvertURLPatternSyntaxToRegex(path_template_match).ok(); +} + +bool IsValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { + return ParseRewritePatternHelper(path_template_rewrite).ok(); +} + +bool IsValidSharedVariableSet(const std::string& path_template_rewrite, + absl::string_view capture_regex) { + return ParseRewritePattern(path_template_rewrite, capture_regex).ok(); +} + +} // namespace matching \ No newline at end of file diff --git a/source/common/common/matching/url_template_matching.h b/source/common/common/matching/url_template_matching.h new file mode 100644 index 0000000000000..8096c37165314 --- /dev/null +++ b/source/common/common/matching/url_template_matching.h @@ -0,0 +1,60 @@ +#ifndef SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_H +#define SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_H + +#include + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "source/common/common/matching/url_template_matching_internal.h" +#include "envoy/config/route/v3/route_components.pb.h" + +namespace matching { + +enum class RewriteStringKind { kVariable, kLiteral }; + +struct RewritePatternSegment { + RewritePatternSegment(absl::string_view str, RewriteStringKind kind) : str(str), kind(kind) {} + absl::string_view str; + RewriteStringKind kind; +}; + +// Returns if the regex pattern matches the given url_pattern. +bool IsPatternMatch(absl::string_view pattern, absl::string_view capture_regex); + +// Returns the regex pattern that is equivalent to the given url_pattern. +// Used in the config pipeline to translate user given url pattern to +// the safe regex Envoy can understand. Strips away any variable captures. +absl::StatusOr ConvertURLPatternSyntaxToRegex(absl::string_view url_pattern); + +// Helper function that parses the pattern and breaks it down to either +// literals or variable names. To be used by ParseRewritePattern(). +// Exposed here so that the validator for the rewrite pattern can also +// use it. +absl::StatusOr> +ParseRewritePatternHelper(absl::string_view pattern); + +// Returns the parsed Url rewrite pattern to be used by +// RewriteURLTemplatePattern() in the BdnFilter. |capture_regex| should +// be the regex generated by ConvertURLPatternSyntaxToRegex(). +absl::StatusOr +ParseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); + +// Returns the rewritten URL path based on the given parsed rewrite pattern. +// Used in the BdnFilter for template-based URL rewrite. +absl::StatusOr +RewriteURLTemplatePattern(absl::string_view url, absl::string_view capture_regex, + const envoy::config::route::v3::RouteUrlRewritePattern& rewrite_pattern); + +// Returns if provided template match pattern is valid +bool IsValidPathTemplateMatchPattern(const std::string& path_template_match); + +// Returns if provided rewrite pattern is valid +bool IsValidPathTemplateRewritePattern(const std::string& path_template_rewrite); + +// Returns if path_template and rewrite_template have valid variables +bool IsValidSharedVariableSet(const std::string& path_template_rewrite, + absl::string_view capture_regex); + +} // namespace matching + +#endif // SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_H \ No newline at end of file diff --git a/source/common/common/matching/url_template_matching_internal.cc b/source/common/common/matching/url_template_matching_internal.cc new file mode 100644 index 0000000000000..039cb053d4838 --- /dev/null +++ b/source/common/common/matching/url_template_matching_internal.cc @@ -0,0 +1,381 @@ +#include "source/common/common/matching/url_template_matching_internal.h" + +// hack for now +// Silence warnings about missing initializers for members of LazyRE2. +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" + +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/flags/flag.h" +#include "absl/functional/function_ref.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_replace.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "absl/types/variant.h" +#include "re2/re2.h" + +namespace matching { + +namespace url_template_matching_internal { + +namespace { + +unsigned long pattern_matching_max_variables_per_url = 5; +unsigned long pattern_matching_max_variable_name_len = 16; +unsigned long pattern_matching_min_variable_name_len = 1; + +// Valid pchar from https://datatracker.ietf.org/doc/html/rfc3986#appendix-A +constexpr absl::string_view kLiteral = "a-zA-Z0-9-._~" // Unreserved + "%" // pct-encoded + "!$&'()+,;" // sub-delims excluding *= + ":@"; + +// Default operator used for the variable when none specified. +constexpr Operator kDefaultVariableOperator = Operator::kPathGlob; + +// Visitor for displaying debug info of a ParsedSegment/Variable.var_match. +struct ToStringVisitor { + template std::string operator()(const T& val) const; +}; + +// Formatter used to allow joining variants together with StrJoin. +struct ToStringFormatter { + template void operator()(std::string* out, const T& t) const { + absl::StrAppend(out, std::visit(ToStringVisitor(), t)); + } +}; + +// Visitor for converting a ParsedSegment variant to the regex. +struct ToRegexPatternVisitor { + template std::string operator()(const T& val) const { return ToRegexPattern(val); } +}; + +// Formatter used to allow joining variants together with StrJoin. +struct ToRegexPatternFormatter { + template void operator()(std::string* out, const T& t) const { + absl::StrAppend(out, std::visit(ToRegexPatternVisitor(), t)); + } +}; + +std::string ToString(const Literal val) { return std::string(val); } + +std::string ToString(const Operator val) { + switch (val) { + case Operator::kPathGlob: + return "*"; + case Operator::kTextGlob: + return "**"; + } +} + +std::string ToString(const Variable val) { + if (val.var_match.empty()) { + return absl::StrCat("{", val.var_name, "}"); + } + + return absl::StrCat("{", val.var_name, "=", + absl::StrJoin(val.var_match, "/", ToStringFormatter()), "}"); +} + +template std::string ToStringVisitor::operator()(const T& val) const { + return ToString(val); +} + +template +absl::StatusOr AlsoUpdatePattern( + absl::FunctionRef>(absl::string_view)> consume_func, + absl::string_view* patt) { + + absl::StatusOr> status = consume_func(*patt); + if (!status.ok()) { + return status.status(); + } + ParsedResult result = *std::move(status); + + *patt = result.unconsumed_pattern; + return result.parsed_value; +} + +} // namespace + +std::string Variable::DebugString() const { return ToString(*this); } + +std::string ParsedUrlPattern::DebugString() const { + return absl::StrCat("/", absl::StrJoin(parsed_segments, "/", ToStringFormatter()), + suffix.value_or("")); +} + +bool IsValidLiteral(absl::string_view pattern) { + static const std::string* kValidLiteralRegex = + new std::string(absl::StrCat("^[", kLiteral, "]+$")); + static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; + return RE2::FullMatch(ToStringPiece(pattern), *literal_regex); +} + +bool IsValidRewriteLiteral(absl::string_view pattern) { + static const std::string* kValidLiteralRegex = + new std::string(absl::StrCat("^[", kLiteral, "/]+$")); + static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; + return RE2::FullMatch(ToStringPiece(pattern), *literal_regex); +} + +bool IsValidIdent(absl::string_view pattern) { + static const LazyRE2 ident_regex = {"^[a-zA-Z][a-zA-Z0-9_]*$"}; + return RE2::FullMatch(ToStringPiece(pattern), *ident_regex); +} + +absl::StatusOr> ConsumeLiteral(absl::string_view pattern) { + absl::string_view lit = + std::vector(absl::StrSplit(pattern, absl::MaxSplits('/', 1)))[0]; + absl::string_view unconsumed_pattern = pattern.substr(lit.size()); + if (!IsValidLiteral(lit)) { + return absl::InvalidArgumentError("Invalid literal"); + } + return ParsedResult(lit, unconsumed_pattern); +} + +absl::StatusOr> ConsumeOperator(absl::string_view pattern) { + if (absl::StartsWith(pattern, "**")) { + return ParsedResult(Operator::kTextGlob, pattern.substr(2)); + } + if (absl::StartsWith(pattern, "*")) { + return ParsedResult(Operator::kPathGlob, pattern.substr(1)); + } + return absl::InvalidArgumentError("Invalid Operator"); +} + +absl::StatusOr> ConsumeVariable(absl::string_view pattern) { + // Locate the variable pattern to parse. + if (pattern.size() < 2 || (pattern)[0] != '{') { + return absl::InvalidArgumentError("Invalid variable"); + } + std::vector parts = absl::StrSplit(pattern.substr(1), absl::MaxSplits('}', 1)); + if (parts.size() != 2) { + return absl::InvalidArgumentError("Unmatched variable bracket"); + } + absl::string_view unconsumed_pattern = parts[1]; + + // Parse the actual variable pattern, starting with the variable name. + std::vector var_parts = absl::StrSplit(parts[0], absl::MaxSplits('=', 1)); + if (!IsValidIdent(var_parts[0])) { + return absl::InvalidArgumentError("Invalid variable name"); + } + Variable var = Variable(var_parts[0], {}); + + // Parse the variable match pattern (if any). + if (var_parts.size() < 2) { + return ParsedResult(var, unconsumed_pattern); + } + absl::string_view var_patt = var_parts[1]; + if (var_patt.empty()) { + return absl::InvalidArgumentError("Empty variable match"); + } + while (!var_patt.empty()) { + std::variant var_match; + if (var_patt[0] == '*') { + + absl::StatusOr status = AlsoUpdatePattern(ConsumeOperator, &var_patt); + if (!status.ok()) { + return status.status(); + } + var_match = *std::move(status); + + } else { + + absl::StatusOr status = AlsoUpdatePattern(ConsumeLiteral, &var_patt); + if (!status.ok()) { + return status.status(); + } + var_match = *std::move(status); + } + var.var_match.push_back(var_match); + if (!var_patt.empty()) { + if (var_patt[0] != '/' || var_patt.size() == 1) { + return absl::InvalidArgumentError("Invalid variable match"); + } + var_patt = var_patt.substr(1); + } + } + + return ParsedResult(var, unconsumed_pattern); +} + +absl::StatusOr> +GatherCaptureNames(struct ParsedUrlPattern pattern) { + absl::flat_hash_set captured_variables; + + for (const ParsedSegment& segment : pattern.parsed_segments) { + if (!std::holds_alternative(segment)) { + continue; + } + if (captured_variables.size() >= pattern_matching_max_variables_per_url) { + return absl::InvalidArgumentError("Exceeded variable count limit"); + } + absl::string_view var_name = std::get(segment).var_name; + + if (var_name.size() < pattern_matching_min_variable_name_len || + var_name.size() > pattern_matching_max_variable_name_len) { + return absl::InvalidArgumentError("Invalid variable length"); + } + if (captured_variables.contains(var_name)) { + return absl::InvalidArgumentError("Repeated variable name"); + } + captured_variables.emplace(var_name); + } + + return captured_variables; +} + +absl::Status ValidateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { + bool seen_text_glob = false; + for (const ParsedSegment& segment : pattern.parsed_segments) { + if (std::holds_alternative(segment)) { + if (seen_text_glob) { + return absl::InvalidArgumentError("Glob after text glob."); + } + seen_text_glob = (std::get(segment) == Operator::kTextGlob); + } else if (std::holds_alternative(segment)) { + const Variable& var = std::get(segment); + if (var.var_match.empty()) { + if (seen_text_glob) { + // A variable with no explicit matcher is treated as a path glob. + return absl::InvalidArgumentError("Implicit variable path glob after text glob."); + } + } else { + for (const std::variant& var_seg : var.var_match) { + if (!std::holds_alternative(var_seg)) { + continue; + } + if (seen_text_glob) { + return absl::InvalidArgumentError("Glob after text glob."); + } + seen_text_glob = (std::get(var_seg) == Operator::kTextGlob); + } + } + } + } + return absl::OkStatus(); +} + +absl::StatusOr ParseURLPatternSyntax(absl::string_view url_pattern) { + struct ParsedUrlPattern parsed_pattern; + + static const LazyRE2 printable_regex = {"^/[[:graph:]]*$"}; + if (!RE2::FullMatch(ToStringPiece(url_pattern), *printable_regex)) { + + return absl::InvalidArgumentError("Invalid pattern"); + } + + // Consume the leading '/' + url_pattern = url_pattern.substr(1); + + // Do the initial lexical parsing. + while (!url_pattern.empty()) { + ParsedSegment segment; + if (url_pattern[0] == '*') { + + absl::StatusOr status = AlsoUpdatePattern(ConsumeOperator, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } else if (url_pattern[0] == '{') { + + absl::StatusOr status = AlsoUpdatePattern(ConsumeVariable, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } else { + + absl::StatusOr status = AlsoUpdatePattern(ConsumeLiteral, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } + parsed_pattern.parsed_segments.push_back(segment); + + // Deal with trailing '/' or suffix. + if (!url_pattern.empty()) { + if (url_pattern == "/") { + // Single trailing '/' at the end, mark this with empty literal. + parsed_pattern.parsed_segments.emplace_back(""); + break; + } else if (url_pattern[0] == '/') { + // Have '/' followed by more text, consume the '/'. + url_pattern = url_pattern.substr(1); + } else { + // Not followed by '/', treat as suffix. + + absl::StatusOr status = AlsoUpdatePattern(ConsumeLiteral, &url_pattern); + if (!status.ok()) { + return status.status(); + } + parsed_pattern.suffix = *std::move(status); + + if (!url_pattern.empty()) { + // Suffix didn't consume whole remaining pattern ('/' in url_pattern). + return absl::InvalidArgumentError("Prefix match not supported."); + } + break; + } + } + } + absl::StatusOr> status = + GatherCaptureNames(parsed_pattern); + if (!status.ok()) { + return status.status(); + } + parsed_pattern.captured_variables = *std::move(status); + + absl::Status validate_status = ValidateNoOperatorAfterTextGlob(parsed_pattern); + if (!validate_status.ok()) { + return validate_status; + } + + return parsed_pattern; +} + +std::string ToRegexPattern(Literal pattern) { + return absl::StrReplaceAll( + pattern, {{"$", "\\$"}, {"(", "\\("}, {")", "\\)"}, {"+", "\\+"}, {".", "\\."}}); +} + +std::string ToRegexPattern(Operator pattern) { + static const std::string* kPathGlobRegex = new std::string(absl::StrCat("[", kLiteral, "]+")); + static const std::string* kTextGlobRegex = new std::string(absl::StrCat("[", kLiteral, "/]*")); + switch (pattern) { + case Operator::kPathGlob: // "*" + return *kPathGlobRegex; + case Operator::kTextGlob: // "**" + return *kTextGlobRegex; + } +} + +std::string ToRegexPattern(const Variable& pattern) { + return absl::StrCat("(?P<", pattern.var_name, ">", + pattern.var_match.empty() + ? ToRegexPattern(kDefaultVariableOperator) + : absl::StrJoin(pattern.var_match, "/", ToRegexPatternFormatter()), + ")"); +} + +std::string ToRegexPattern(const struct ParsedUrlPattern& pattern) { + return absl::StrCat("/", absl::StrJoin(pattern.parsed_segments, "/", ToRegexPatternFormatter()), + ToRegexPattern(pattern.suffix.value_or(""))); +} + +} // namespace url_template_matching_internal + +} // namespace matching diff --git a/source/common/common/matching/url_template_matching_internal.h b/source/common/common/matching/url_template_matching_internal.h new file mode 100644 index 0000000000000..962c988f95c78 --- /dev/null +++ b/source/common/common/matching/url_template_matching_internal.h @@ -0,0 +1,82 @@ +#ifndef SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H +#define SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H + +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "absl/types/optional.h" +#include "absl/types/variant.h" +#include "re2/re2.h" + +namespace matching { + +namespace url_template_matching_internal { + +using Literal = absl::string_view; +enum class Operator { kPathGlob, kTextGlob }; + +struct Variable { + absl::string_view var_name; + std::vector> var_match; + + Variable(absl::string_view name, std::vector> match) + : var_name(name), var_match(match) {} + + std::string DebugString() const; +}; + +using ParsedSegment = std::variant; + +struct ParsedUrlPattern { + std::vector parsed_segments; + std::optional suffix; + absl::flat_hash_set captured_variables; + + std::string DebugString() const; +}; + +bool IsValidLiteral(absl::string_view pattern); + +bool IsValidRewriteLiteral(absl::string_view pattern); + +bool IsValidIdent(absl::string_view pattern); + +// Used by the following Consume{Literal.Operator,Variable} functions +// in the return value. The functions would take the given pattern, +// parse what it can into |parsed_value| and return the unconsumed +// portion of the pattern in |unconsumed_pattern|. +template struct ParsedResult { + ParsedResult(T val, absl::string_view pattern) : parsed_value(val), unconsumed_pattern(pattern) {} + + T parsed_value; + absl::string_view unconsumed_pattern; +}; + +absl::StatusOr> ConsumeLiteral(absl::string_view pattern); + +absl::StatusOr> ConsumeOperator(absl::string_view pattern); + +absl::StatusOr> ConsumeVariable(absl::string_view pattern); + +absl::StatusOr ParseURLPatternSyntax(absl::string_view url_pattern); + +std::string ToRegexPattern(Literal pattern); + +std::string ToRegexPattern(Operator pattern); + +std::string ToRegexPattern(const Variable& pattern); + +std::string ToRegexPattern(const struct ParsedUrlPattern& pattern); + +inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.data(), text.size()}; } + +} // namespace url_template_matching_internal + +} // namespace matching + +#endif // SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H \ No newline at end of file diff --git a/source/common/common/matching/url_template_matching_internal_test.cc b/source/common/common/matching/url_template_matching_internal_test.cc new file mode 100644 index 0000000000000..5fd0c9314fc3c --- /dev/null +++ b/source/common/common/matching/url_template_matching_internal_test.cc @@ -0,0 +1,466 @@ +#include "source/common/common/matching/url_template_matching_internal.h" + +#include +#include +#include +#include +#include +#include + +#include "source/common/common/assert.h" + +#include "test/test_common/logging.h" +#include "test/test_common/utility.h" +#include "test/test_common/status_utility.h" +#include "source/common/protobuf/protobuf.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "re2/re2.h" +#include "gtest/gtest.h" + +namespace matching { + +namespace url_template_matching_internal { + +namespace { + +using ::Envoy::StatusHelpers::StatusIs; + +TEST(InternalParsing, ParsedUrlDebugString) { + ParsedUrlPattern patt1 = { + { + "abc", + "def", + Operator::kPathGlob, + Variable("var", {Operator::kPathGlob, "ghi", Operator::kTextGlob}), + }, + ".test", + {}, + }; + EXPECT_EQ(patt1.DebugString(), "/abc/def/*/{var=*/ghi/**}.test"); + + ParsedUrlPattern patt2 = {{ + Variable("var", {}), + }, + "", + {}}; + EXPECT_EQ(patt2.DebugString(), "/{var}"); +} + +TEST(InternalParsing, IsValidLiteralWorks) { + EXPECT_TRUE(IsValidLiteral("123abcABC")); + EXPECT_TRUE(IsValidLiteral("._~-")); + EXPECT_TRUE(IsValidLiteral("-._~%20!$&'()+,;:@")); + EXPECT_FALSE(IsValidLiteral("`~!@#$%^&()-_+;:,<.>'\"\\| ")); + EXPECT_FALSE(IsValidLiteral("abc/")); + EXPECT_FALSE(IsValidLiteral("ab*c")); + EXPECT_FALSE(IsValidLiteral("a**c")); + EXPECT_FALSE(IsValidLiteral("a=c")); + EXPECT_FALSE(IsValidLiteral("?abc")); + EXPECT_FALSE(IsValidLiteral("?a=c")); + EXPECT_FALSE(IsValidLiteral("{abc")); + EXPECT_FALSE(IsValidLiteral("abc}")); + EXPECT_FALSE(IsValidLiteral("{abc}")); +} + +TEST(InternalParsing, IsValidRewriteLiteralWorks) { + EXPECT_TRUE(IsValidRewriteLiteral("123abcABC")); + EXPECT_TRUE(IsValidRewriteLiteral("abc/")); + EXPECT_TRUE(IsValidRewriteLiteral("abc/def")); + EXPECT_TRUE(IsValidRewriteLiteral("/abc.def")); + EXPECT_TRUE(IsValidRewriteLiteral("._~-")); + EXPECT_TRUE(IsValidRewriteLiteral("-._~%20!$&'()+,;:@")); + EXPECT_FALSE(IsValidRewriteLiteral("`~!@#$%^&()-_+;:,<.>'\"| ")); + EXPECT_FALSE(IsValidRewriteLiteral("ab}c")); + EXPECT_FALSE(IsValidRewriteLiteral("ab{c")); + EXPECT_FALSE(IsValidRewriteLiteral("a=c")); + EXPECT_FALSE(IsValidRewriteLiteral("?a=c")); +} + +TEST(InternalParsing, IsValidIdentWorks) { + EXPECT_TRUE(IsValidIdent("abc")); + EXPECT_TRUE(IsValidIdent("ABC_def_123")); + EXPECT_TRUE(IsValidIdent("a1")); + EXPECT_TRUE(IsValidIdent("T")); + EXPECT_FALSE(IsValidIdent("123")); + EXPECT_FALSE(IsValidIdent("__undefined__")); + EXPECT_FALSE(IsValidIdent("abc-def")); + EXPECT_FALSE(IsValidIdent("abc=def")); + EXPECT_FALSE(IsValidIdent("abc def")); + EXPECT_FALSE(IsValidIdent("a!!!")); +} + +TEST(InternalParsing, ConsumeLiteralWorks) { + std::string pattern = "abc/123"; + + absl::StatusOr> result = ConsumeLiteral(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, "abc"); + EXPECT_EQ(result->unconsumed_pattern, "/123"); +} + +TEST(InternalParsing, ConsumeTextGlob) { + std::string pattern = "***abc/123"; + + absl::StatusOr> result = ConsumeOperator(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, Operator::kTextGlob); + EXPECT_EQ(result->unconsumed_pattern, "*abc/123"); +} + +TEST(InternalParsing, ConsumePathGlob) { + std::string pattern = "*/123"; + + absl::StatusOr> result = ConsumeOperator(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, Operator::kPathGlob); + EXPECT_EQ(result->unconsumed_pattern, "/123"); +} + +class ConsumeVariableSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ConsumeVariableSuccessTestSuite, ConsumeVariableSuccess, + testing::Values("{var=*}", "{Var}", "{v1=**}", "{v_1=*/abc/**}", + "{v3=abc}", "{v=123/*/*}", "{var=abc/*/def}")); + +TEST_P(ConsumeVariableSuccess, ConsumeVariableSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + absl::StatusOr> result = ConsumeVariable(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value.DebugString(), pattern); + EXPECT_TRUE(result->unconsumed_pattern.empty()); +} + +class ConsumeVariableFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ConsumeVariableFailureTestSuite, ConsumeVariableFailure, + testing::Values("{var", "{=abc}", "{_var=*}", "{1v}", "{1v=abc}", + "{var=***}", "{v-a-r}", "{var=*/abc?q=1}", "{var=abc/a*}", + "{var=*def/abc}", "{var=}", "{var=abc=def}", + "{rc=||||(A+yl/}")); + +TEST_P(ConsumeVariableFailure, ConsumeVariableFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(ConsumeVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class ParseURLPatternSyntaxSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P( + ParseURLPatternSyntaxSuccessTestSuite, ParseURLPatternSyntaxSuccess, + testing::Values("/**.m3u8", "/**.mpd", "/*_suf", "/{path=**}.m3u8", "/{foo}/**.ts", + "/media/*.m4s", "/media/{contentId=*}/**", "/media/*", "/api/*/*/**", + "/api/*/v1/**", "/api/*/v1/*", "/{version=api/*}/*", "/api/*/*/", + "/api/*/1234/", "/api/*/{resource=*}/{method=*}", + "/api/*/{resource=*}/{method=**}", "/v1/**", "/media/{country}/{lang=*}/**", + "/{foo}/{bar}/{fo}/{fum}/*", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", + "/media/{id=*}/*", "/media/{contentId=**}", + "/api/{version}/projects/{project}/locations/{location}/{resource}/" + "{name}", + "/api/{version=*}/{url=**}", "/api/{VERSION}/{version}/{verSION}", + "/api/1234/abcd", "/media/abcd/%10%20%30/{v1=*/%10%20}_suffix", "/")); + +TEST_P(ParseURLPatternSyntaxSuccess, ParseURLPatternSyntaxSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + absl::StatusOr parsed_patt = ParseURLPatternSyntax(pattern); + ASSERT_OK(parsed_patt); + EXPECT_EQ(parsed_patt->DebugString(), pattern); +} + +class ParseURLPatternSyntaxFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P( + ParseURLPatternSyntaxFailureTestSuite, ParseURLPatternSyntaxFailure, + testing::Values("/api/v*/1234", "/api/{version=v*}/1234", "/api/v{versionNum=*}/1234", + "/api/{version=*beta}/1234", "/media/eff456/ll-sd-out.{ext}", + "/media/eff456/ll-sd-out.{ext=*}", "/media/eff456/ll-sd-out.**", + "/media/{country=**}/{lang=*}/**", "/media/**/*/**", "/link/{id=*}/asset*", + "/link/{id=*}/{asset=asset*}", "/media/{id=/*}/*", "/media/{contentId=/**}", + "/api/{version}/{version}", "/api/{version.major}/{version.minor}", + "/media/***", "/media/*{*}*", "/media/{*}/", "/media/*/index?a=2", "media", + "/\001\002\003\004\005\006\007", "/*(/**", "/**/{var}", + "/{var1}/{var2}/{var3}/{var4}/{var5}/{var6}", "/{=*}", + "/{var12345678901234=*}")); + +TEST_P(ParseURLPatternSyntaxFailure, ParseURLPatternSyntaxFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(ParseURLPatternSyntax(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(InternalRegexGen, LiteralEscapes) { + EXPECT_EQ(ToRegexPattern("abcABC123/-._~%20!$&'()+,;:@"), + "abcABC123/-\\._~%20!\\$&'\\(\\)\\+,;:@"); +} + +TEST(InternalRegexGen, LiteralMatches) { + absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; + + EXPECT_TRUE(RE2::FullMatch(ToStringPiece(kPattern), ToRegexPattern(kPattern))); +} + +TEST(InternalRegexGen, LiteralMatchesInNamedCapture) { + absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; + + RE2 regex = RE2(absl::StrCat("(?P", ToRegexPattern(kPattern), ")")); + ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); + + // Full matched string + capture groups + std::vector captures(2); + ASSERT_TRUE(regex.Match(ToStringPiece(kPattern), /*startpos=*/0, /*endpos=*/kPattern.size(), + RE2::ANCHOR_BOTH, captures.data(), captures.size())); + + // Index 0 would be the full text of the matched string. + EXPECT_EQ(ToStringPiece(kPattern), captures[0]); + // Get the pattern matched with the named capture group. + EXPECT_EQ(ToStringPiece(kPattern), captures.at(regex.NamedCapturingGroups().at("var"))); +} + +TEST(InternalRegexGen, LiteralOnlyMatchesItself) { + constexpr absl::string_view kChars = "abcABC123/-._~%20!$&'()+,;:@"; + + for (const char c : kChars) { + std::string s = {'z', c, 'z'}; + EXPECT_TRUE(RE2::FullMatch(s, ToRegexPattern(s))); + EXPECT_FALSE(RE2::FullMatch("zzz", ToRegexPattern(s))); + } +} + +TEST(InternalRegexGen, RegexLikePatternIsMatchedLiterally) { + EXPECT_TRUE(RE2::FullMatch("(abc)", ToRegexPattern("(abc)"))); + EXPECT_FALSE(RE2::FullMatch("abc", ToRegexPattern("(abc)+"))); + + EXPECT_TRUE(RE2::FullMatch("(abc)+", ToRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("", ToRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("abc", ToRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("abcabc", ToRegexPattern("(abc)+"))); + + EXPECT_TRUE(RE2::FullMatch(".+", ToRegexPattern(".+"))); + EXPECT_FALSE(RE2::FullMatch("abc", ToRegexPattern(".+"))); + + EXPECT_TRUE(RE2::FullMatch("a+", ToRegexPattern("a+"))); + EXPECT_FALSE(RE2::FullMatch("aa", ToRegexPattern("a+"))); +} + +TEST(InternalRegexGen, DollarSignMatchesIfself) { + EXPECT_TRUE(RE2::FullMatch("abc$", ToRegexPattern("abc$"))); + EXPECT_FALSE(RE2::FullMatch("abc", ToRegexPattern("abc$"))); +} + +TEST(InternalRegexGen, OperatorRegexPattern) { + EXPECT_EQ(ToRegexPattern(Operator::kPathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); + EXPECT_EQ(ToRegexPattern(Operator::kTextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); +} + +TEST(InternalRegexGen, PathGlobRegex) { + EXPECT_TRUE(RE2::FullMatch("abc.123", ToRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("", ToRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc/123", ToRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("*", ToRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("**", ToRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", ToRegexPattern(Operator::kPathGlob))); +} + +TEST(InternalRegexGen, TextGlobRegex) { + EXPECT_TRUE(RE2::FullMatch("abc.123", ToRegexPattern(Operator::kTextGlob))); + EXPECT_TRUE(RE2::FullMatch("", ToRegexPattern(Operator::kTextGlob))); + EXPECT_TRUE(RE2::FullMatch("abc/123", ToRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("*", ToRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("**", ToRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", ToRegexPattern(Operator::kTextGlob))); +} + +TEST(InternalRegexGen, VariableRegexPattern) { + EXPECT_EQ(ToRegexPattern(Variable("var1", {})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"); + EXPECT_EQ(ToRegexPattern(Variable("var2", {Operator::kPathGlob, "abc", Operator::kTextGlob})), + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+/abc/" + "[a-zA-Z0-9-._~%!$&'()+,;:@/]*)"); +} + +TEST(InternalRegexGen, VariableRegexDefaultMatch) { + absl::StatusOr> var = ConsumeVariable("{var}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc", ToRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc"); +} + +TEST(InternalRegexGen, VariableRegexDefaultNotMatch) { + absl::StatusOr> var = ConsumeVariable("{var}"); + ASSERT_OK(var); + + EXPECT_FALSE(RE2::FullMatch("abc/def", ToRegexPattern(var->parsed_value))); +} + +TEST(InternalRegexGen, VariableRegexSegmentsMatch) { + absl::StatusOr> var = ConsumeVariable("{var=abc/*/def}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc/123/def", ToRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc/123/def"); +} + +TEST(InternalRegexGen, VariableRegexTextGlobMatch) { + absl::StatusOr> var = ConsumeVariable("{var=**/def}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc/123/def", ToRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc/123/def"); +} + +TEST(InternalRegexGen, VariableRegexNamedCapture) { + re2::StringPiece kPattern = "abc"; + absl::StatusOr> var = ConsumeVariable("{var=*}"); + ASSERT_OK(var); + + RE2 regex = RE2(ToRegexPattern(var->parsed_value)); + ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); + + // Full matched string + capture groups + std::vector captures(2); + ASSERT_TRUE(regex.Match(kPattern, /*startpos=*/0, /*endpos=*/kPattern.size(), RE2::ANCHOR_BOTH, + captures.data(), captures.size())); + + // Index 0 would be the full text of the matched string. + EXPECT_EQ(kPattern, captures[0]); + // Get the pattern matched with the named capture group. + EXPECT_EQ(kPattern, captures.at(regex.NamedCapturingGroups().at("var"))); +} + +TEST(InternalRegexGen, ParsedURLPatternToRegex) { + absl::StatusOr pattern = + ParseURLPatternSyntax("/abc/*/{var1}/def/{var2=*/ghi/**}.jkl"); + ASSERT_OK(pattern); + + std::string var1_capture; + std::string var2_capture; + EXPECT_TRUE(RE2::FullMatch("/abc/123/456/def/789/ghi/%20/($).jkl", + ToRegexPattern(pattern.value()), &var1_capture, &var2_capture)); + EXPECT_EQ(var1_capture, "456"); + EXPECT_EQ(var2_capture, "789/ghi/%20/($)"); +} + +struct GenPatternTestCase { + GenPatternTestCase(std::string request_path, std::string url_pattern, + std::vector> capture_pairs) + : path(request_path), pattern(url_pattern), captures(capture_pairs) {} + std::string path; + std::string pattern; + std::vector> captures; +}; + +class GenPatternRegexWithMatch : public testing::TestWithParam { +protected: + const std::string& request_path() const { return GetParam().path; } + const std::string& url_pattern() const { return GetParam().pattern; } + std::vector> const var_values() { + return GetParam().captures; + } +}; + +INSTANTIATE_TEST_SUITE_P( + GenPatternRegexWithMatchTestSuite, GenPatternRegexWithMatch, + testing::Values( + GenPatternTestCase("/media/1234/manifest.m3u8", "/**.m3u8", {}), + GenPatternTestCase("/manifest.mpd", "/**.mpd", {}), + GenPatternTestCase("/media/1234/manifest.m3u8", "/{path=**}.m3u8", + {{"path", "media/1234/manifest"}}), + GenPatternTestCase("/foo/12314341/format/123/hls/segment_0000000001.ts", "/{foo}/**.ts", + {{"foo", "foo"}}), + GenPatternTestCase("/media/eff456/ll-sd-out.js", "/media/{contentId=*}/**", + {{"contentId", "eff456"}}), + GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/*/**", {}), + GenPatternTestCase("/api/v1/1234", "/{version=api/*}/*", {{"version", "api/v1"}}), + GenPatternTestCase("/api/v1/1234/", "/api/*/*/", {}), + GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=**}", + {{"resource", "1234"}, {"method", "broadcasts/get"}}), + GenPatternTestCase("/v1/broadcasts/12345/live", "/v1/**", {}), + GenPatternTestCase("/media/us/en/12334/subtitle_enUS_00101.vtt", + "/media/{country}/{lang=*}/**", {{"country", "us"}, {"lang", "en"}}), + GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo}/{bar}/{fo}/{fum}/*", + {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), + GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", + {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), + GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{id=*}/**", {{"id", "1234"}}), + GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{contentId=**}", + {{"contentId", "1234/hls/1001011.m3u8"}}), + GenPatternTestCase("/api/v1/projects/my-project/locations/global/edgeCacheOrigins/foo", + "/api/{version}/projects/{project}/locations/{location}/{resource}/" + "{name}", + {{"version", "v1"}, + {"project", "my-project"}, + {"location", "global"}, + {"resource", "edgeCacheOrigins"}, + {"name", "foo"}}), + GenPatternTestCase("/api/v1/foo/bar/baz/", "/api/{version=*}/{url=**}", + {{"version", "v1"}, {"url", "foo/bar/baz/"}}), + GenPatternTestCase("/api/v1/v2/v3", "/api/{VERSION}/{version}/{verSION}", + {{"VERSION", "v1"}, {"version", "v2"}, {"verSION", "v3"}}))); + +TEST_P(GenPatternRegexWithMatch, WithCapture) { + absl::StatusOr pattern = ParseURLPatternSyntax(url_pattern()); + ASSERT_OK(pattern); + + RE2 regex = RE2(ToRegexPattern(pattern.value())); + ASSERT_TRUE(regex.ok()) << regex.error(); + ASSERT_EQ(regex.NumberOfCapturingGroups(), var_values().size()); + + int capture_num = regex.NumberOfCapturingGroups() + 1; + std::vector captures(capture_num); + ASSERT_TRUE(regex.Match(request_path(), /*startpos=*/0, + /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, captures.data(), + captures.size())); + + EXPECT_EQ(captures[0], ToStringPiece(request_path())); + + for (const auto& [name, value] : var_values()) { + int capture_index = regex.NamedCapturingGroups().at(name); + ASSERT_GE(capture_index, 0); + EXPECT_EQ(captures.at(capture_index), value); + } +} + +class GenPatternRegexWithoutMatch + : public testing::TestWithParam> { +protected: + const std::string& request_path() const { return std::get<0>(GetParam()); } + const std::string& url_pattern() const { return std::get<1>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWithoutMatch, + testing::ValuesIn(std::vector>( + {{"/media/12345/f/123/s00002.m4s", "/media/*.m4s"}, + {"/media/eff456/ll-sd-out.js", "/media/*"}, + {"/api/v1/1234/", "/api/*/v1/*"}, + {"/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=*}"}, + {"/api/v1/1234/", "/api/*/v1/**"}, + {"/api/*/1234/", "/api/*/1234/"}}))); + +TEST_P(GenPatternRegexWithoutMatch, WithCapture) { + absl::StatusOr pattern = ParseURLPatternSyntax(url_pattern()); + ASSERT_OK(pattern); + + RE2 regex = RE2(ToRegexPattern(pattern.value())); + ASSERT_TRUE(regex.ok()) << regex.error(); + + EXPECT_FALSE(regex.Match(request_path(), /*startpos=*/0, + /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, nullptr, 0)); +} + +} // namespace +} // namespace url_template_matching_internal +} // namespace matching \ No newline at end of file diff --git a/source/common/common/matching/url_template_matching_test.cc b/source/common/common/matching/url_template_matching_test.cc new file mode 100644 index 0000000000000..b19125583b605 --- /dev/null +++ b/source/common/common/matching/url_template_matching_test.cc @@ -0,0 +1,335 @@ +#include "source/common/common/matching/url_template_matching_internal.h" + +#include +#include +#include + +#include "source/common/common/matching/url_template_matching.h" + +#include "source/common/common/assert.h" + +#include "test/test_common/logging.h" +#include "test/test_common/utility.h" +#include "test/test_common/status_utility.h" +#include "source/common/protobuf/protobuf.h" + +#include "gtest/gtest.h" + +namespace matching { + +namespace { + +using ::Envoy::StatusHelpers::IsOkAndHolds; +using ::Envoy::StatusHelpers::StatusIs; + +// Capture regex for /{var1}/{var2}/{var3}/{var4}/{var5} +static constexpr absl::string_view kCaptureRegex = "/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"; +static constexpr absl::string_view kMatchUrl = "/val1/val2/val3/val4/val5"; + +TEST(ConvertURLPattern, ValidPattern) { + EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/abc"), IsOkAndHolds("/abc")); + EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/**.mpd"), + IsOkAndHolds("/[a-zA-Z0-9-._~%!$&'()+,;:@/]*\\.mpd")); + EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/api/*/{resource=*}/{method=**}"), + IsOkAndHolds("/api/[a-zA-Z0-9-._~%!$&'()+,;:@]+/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@/]*)")); + EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/api/{VERSION}/{version}/{verSION}"), + IsOkAndHolds("/api/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)")); +} + +TEST(ConvertURLPattern, InvalidPattern) { + EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/api/v*/1234"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/media/**/*/**"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/\001\002\003\004\005\006\007"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/{var12345678901234=*}"), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class ParseRewriteHelperSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperSuccessTestSuite, ParseRewriteHelperSuccess, + testing::Values("/{var1}", "/{var1}{var2}", "/{var1}-{var2}", + "/abc/{var1}/def", "/{var1}/abd/{var2}", + "/abc-def-{var1}/a/{var1}")); + +TEST_P(ParseRewriteHelperSuccess, ParseRewriteHelperSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_OK(ParseRewritePatternHelper(pattern)); +} + +class ParseRewriteHelperFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperFailureTestSuite, ParseRewriteHelperFailure, + testing::Values("{var1}", "/{{var1}}", "/}va1{", "var1}", + "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); + +TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(ParseRewritePatternHelper(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class ParseRewriteSuccess : public testing::TestWithParam> { +protected: + const std::string& rewrite_pattern() const { return std::get<0>(GetParam()); } + envoy::config::route::v3::RouteUrlRewritePattern expected_proto() const { + envoy::config::route::v3::RouteUrlRewritePattern expected_proto; + Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); + return expected_proto; + } +}; + +TEST(ParseRewrite, InvalidRegex) { + EXPECT_THAT(ParseRewritePattern("/{var1}", "+[abc"), StatusIs(absl::StatusCode::kInternal)); +} + +INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, + testing::ValuesIn(std::vector>({ + {"/static", R"EOF(segments: {literal: "/static"} )EOF"}, + {"/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1)EOF"}, + {"/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1)EOF"}, + {"/{var1}/{var1}/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1)EOF"}, + {"/{var3}/{var1}/{var2}", R"EOF(segments + - literal: "/" + - var_index: 3 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 2)EOF"}, + {"/{var3}/abc/def/{var2}.suffix", R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/abc/def/" + - var_index: 2 + - literal: ".suffix")EOF"}, + {"/abc/{var1}/{var2}/def", R"EOF(segments + - literal: "/abc/" + - var_index: 1 + - literal: "/" + - var_index: 2 + - literal: "/def")EOF"}, + {"/{var1}{var2}", R"EOF(segments + - literal: "/" + - var_index: 1 + - ar_index: 2)EOF"}, + {"/{var1}-{var2}/bucket-{var3}.suffix", R"EOF(segments + - literal: "/" + - var_index: 1 + - literal: "-" + - var_index: 2 + - literal: "/bucket-" + - var_index: 3 + - literal: ".suffix")EOF"}, + }))); + +TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { + absl::StatusOr rewrite = + ParseRewritePattern(rewrite_pattern(), kCaptureRegex); + ASSERT_OK(rewrite); + // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); +} + +class ParseRewriteFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteFailureTestSuite, ParseRewriteFailure, + testing::Values("{var1}", "/{var6}", "/{{var1}}", "/}va1{", "var1}", + "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); + +TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(ParseRewritePattern(pattern, kCaptureRegex), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class RewriteUrlTemplateSuccess + : public testing::TestWithParam> { +protected: + envoy::config::route::v3::RouteUrlRewritePattern rewrite_proto() const { + envoy::config::route::v3::RouteUrlRewritePattern proto; + Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); + return proto; + } + const std::string& expected_rewritten_url() const { return std::get<1>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateSuccess, + testing::ValuesIn(std::vector>( + {{R"EOF(segments: { literal: "/static" })EOF", "/static"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1)EOF", + "/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1)EOF", + "/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1)EOF", + "/val1/val1/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 2)EOF", + "/val3/val1/val2"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/abc/def/" + - var_index: 2 + - literal: ".suffix")EOF", + "/val3/abc/def/val2.suffix"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - var_index: 2 + - literal: "." + - var_index: 1)EOF", + "/val3val2.val1"}, + {R"EOF(segments: + - literal: "/abc/" + - var_index: 1 + - literal: "/" + - var_index: 5 + - literal: "/def")EOF", + "/abc/val1/val5/def"}}))); + +TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { + absl::StatusOr rewritten_url = + RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto()); + ASSERT_OK(rewritten_url); + EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); +} + +TEST(RewriteUrlTemplateFailure, BadRegex) { + envoy::config::route::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 1 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "+/bad_regex", rewrite_proto), + StatusIs(absl::StatusCode::kInternal)); +} + +TEST(RewriteUrlTemplateFailure, RegexNoMatch) { + envoy::config::route::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 1 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "/no_match_regex", rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { + envoy::config::route::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 0 + )EOF"; + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { + envoy::config::route::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 6 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class URLPatternMatchAndRewrite + : public testing::TestWithParam< + std::tuple> { +protected: + const std::string& url_pattern() const { return std::get<0>(GetParam()); } + const std::string& rewrite_pattern() const { return std::get<1>(GetParam()); } + const std::string& match_url() const { return std::get<2>(GetParam()); } + const std::string& expected_rewritten_url() const { return std::get<3>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P( + URLPatternMatchAndRewriteTestSuite, URLPatternMatchAndRewrite, + testing::ValuesIn(std::vector>( + {{"/api/users/{id}/{path=**}", "/users/{id}/{path}", "/api/users/21334/profile.json", + "/users/21334/profile.json"}, + {"/videos/*/{id}/{format}/{rendition}/{segment=**}.ts", + "/{id}/{format}/{rendition}/{segment}.ts", "/videos/lib/132939/hls/13/segment_00001.ts", + "/132939/hls/13/segment_00001.ts"}, + {"/region/{region}/bucket/{name}/{method=**}", "/{region}/bucket-{name}/{method}", + "/region/eu/bucket/prod-storage/object.pdf", "/eu/bucket-prod-storage/object.pdf"}, + {"/region/{region}/bucket/{name}/{method=**}", "/{region}{name}/{method}", + "/region/eu/bucket/prod-storage/object.pdf", "/euprod-storage/object.pdf"}}))); + +TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { + absl::StatusOr regex = ConvertURLPatternSyntaxToRegex(url_pattern()); + ASSERT_OK(regex); + + absl::StatusOr rewrite_proto = + ParseRewritePattern(rewrite_pattern(), regex.value()); + ASSERT_OK(rewrite_proto); + + absl::StatusOr rewritten_url = + RewriteURLTemplatePattern(match_url(), regex.value(), rewrite_proto.value()); + ASSERT_OK(rewritten_url); + + EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); +} + +} // namespace + +} // namespace matching \ No newline at end of file From 7f11b6e2b622fc949ac13e3db6a7a19b262ad454 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 27 Jun 2022 17:57:46 +0000 Subject: [PATCH 002/238] Clean up items Signed-off-by: silverstar195 --- .../config/route/v3/route_components.proto | 3 ++- source/common/common/matching/BUILD | 19 +------------------ .../common/matching/url_template_matching.cc | 9 +++++---- .../url_template_matching_internal.cc | 10 +++++----- 4 files changed, 13 insertions(+), 28 deletions(-) diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index 1e135c2b37584..5c63cced9a372 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -1362,7 +1362,8 @@ message RouteAction { MaxStreamDuration max_stream_duration = 36; } -// Holds the segments for rewrite +// Holds the segments for rewriting urls base on pattern templates +// [#next-free-field: 4] message RouteUrlRewritePattern { message RewriteSegment { oneof segment_type { diff --git a/source/common/common/matching/BUILD b/source/common/common/matching/BUILD index 9b7d044a3b2de..b535a0afeb67a 100644 --- a/source/common/common/matching/BUILD +++ b/source/common/common/matching/BUILD @@ -1,4 +1,4 @@ -# BDN Wildcard & Pattern Matching as per go/wildcard-matching-ug +# Wildcard & Pattern Matching load( "//bazel:envoy_build_system.bzl", @@ -70,20 +70,3 @@ envoy_cc_test( "@com_googlesource_code_re2//:re2", ], ) - -envoy_cc_test( - name = "url_template_benchmark_test", - srcs = ["url_template_benchmark_test.cc"], - deps = [ - ":url_template_matching", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - "@envoy_api//envoy/src/api/envoy/config/route/v3:pkg_cc", - "@envoy_api//envoy/src/api/envoy/config/route/v3:pkg_cc_proto_library", - "@envoy_api//envoy/src/source/common/common:assert_lib", - "@envoy_api//envoy/src/source/common/router:config_lib", - "@envoy_api//envoy/src/test/mocks/server:instance_mocks", - "@envoy_api//envoy/src/test/mocks/stream_info:stream_info_mocks", - "@envoy_api//envoy/src/test/test_common:utility_lib", - ], -) diff --git a/source/common/common/matching/url_template_matching.cc b/source/common/common/matching/url_template_matching.cc index 2b68daeb3b551..5f55d949c88f4 100644 --- a/source/common/common/matching/url_template_matching.cc +++ b/source/common/common/matching/url_template_matching.cc @@ -1,9 +1,5 @@ #include "source/common/common/matching/url_template_matching.h" -// hack for now -// Silence warnings about missing initializers for members of LazyRE2. -#pragma GCC diagnostic ignored "-Wmissing-field-initializers" - #include #include #include @@ -21,6 +17,11 @@ namespace matching { using ::matching::url_template_matching_internal::ParsedUrlPattern; +#ifndef SWIG +// Silence warnings about missing initializers for members of LazyRE2. +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.data(), text.size()}; } bool IsPatternMatch(absl::string_view pattern, absl::string_view capture_regex) { diff --git a/source/common/common/matching/url_template_matching_internal.cc b/source/common/common/matching/url_template_matching_internal.cc index 039cb053d4838..18bfc6c5a4a1a 100644 --- a/source/common/common/matching/url_template_matching_internal.cc +++ b/source/common/common/matching/url_template_matching_internal.cc @@ -1,9 +1,5 @@ #include "source/common/common/matching/url_template_matching_internal.h" -// hack for now -// Silence warnings about missing initializers for members of LazyRE2. -#pragma GCC diagnostic ignored "-Wmissing-field-initializers" - #include #include #include @@ -30,6 +26,11 @@ namespace url_template_matching_internal { namespace { +#ifndef SWIG +// Silence warnings about missing initializers for members of LazyRE2. +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + unsigned long pattern_matching_max_variables_per_url = 5; unsigned long pattern_matching_max_variable_name_len = 16; unsigned long pattern_matching_min_variable_name_len = 1; @@ -323,7 +324,6 @@ absl::StatusOr ParseURLPatternSyntax(absl::string_view url_pat return status.status(); } parsed_pattern.suffix = *std::move(status); - if (!url_pattern.empty()) { // Suffix didn't consume whole remaining pattern ('/' in url_pattern). return absl::InvalidArgumentError("Prefix match not supported."); From 0c32709f13a5d85c69347ee8239683be924d09f5 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 27 Jun 2022 19:27:21 +0000 Subject: [PATCH 003/238] clang-format Signed-off-by: silverstar195 --- source/common/common/matching/BUILD | 9 ++++++--- source/common/common/matching/url_template_matching.cc | 5 +++-- source/common/common/matching/url_template_matching.h | 6 ++++-- .../matching/url_template_matching_internal_test.cc | 10 +++++----- .../common/matching/url_template_matching_test.cc | 10 ++++------ 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/source/common/common/matching/BUILD b/source/common/common/matching/BUILD index b535a0afeb67a..ef64a17b8ccef 100644 --- a/source/common/common/matching/BUILD +++ b/source/common/common/matching/BUILD @@ -1,15 +1,17 @@ -# Wildcard & Pattern Matching - load( "//bazel:envoy_build_system.bzl", - "envoy_cc_extension", "envoy_cc_library", "envoy_cc_test", "envoy_extension_package", + "envoy_package", ) licenses(["notice"]) # Apache 2 +# Wildcard & Pattern Matching + +envoy_package() + envoy_extension_package() envoy_cc_library( @@ -26,6 +28,7 @@ envoy_cc_library( "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_googlesource_code_re2//:re2", + "@envoy_api//envoy/config/route/v3:pkg_cc_proto", ], ) diff --git a/source/common/common/matching/url_template_matching.cc b/source/common/common/matching/url_template_matching.cc index 5f55d949c88f4..beca7637f1416 100644 --- a/source/common/common/matching/url_template_matching.cc +++ b/source/common/common/matching/url_template_matching.cc @@ -8,10 +8,11 @@ #include "envoy/config/route/v3/route_components.pb.h" #include "source/common/common/matching/url_template_matching_internal.h" -#include "re2/re2.h" + #include "absl/status/statusor.h" -#include "absl/strings/string_view.h" #include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "re2/re2.h" namespace matching { diff --git a/source/common/common/matching/url_template_matching.h b/source/common/common/matching/url_template_matching.h index 8096c37165314..bb1db29baec29 100644 --- a/source/common/common/matching/url_template_matching.h +++ b/source/common/common/matching/url_template_matching.h @@ -3,10 +3,12 @@ #include +#include "envoy/config/route/v3/route_components.pb.h" + +#include "source/common/common/matching/url_template_matching_internal.h" + #include "absl/status/statusor.h" #include "absl/strings/string_view.h" -#include "source/common/common/matching/url_template_matching_internal.h" -#include "envoy/config/route/v3/route_components.pb.h" namespace matching { diff --git a/source/common/common/matching/url_template_matching_internal_test.cc b/source/common/common/matching/url_template_matching_internal_test.cc index 5fd0c9314fc3c..33ccedcd82318 100644 --- a/source/common/common/matching/url_template_matching_internal_test.cc +++ b/source/common/common/matching/url_template_matching_internal_test.cc @@ -1,5 +1,3 @@ -#include "source/common/common/matching/url_template_matching_internal.h" - #include #include #include @@ -8,15 +6,17 @@ #include #include "source/common/common/assert.h" +#include "source/common/common/matching/url_template_matching_internal.h" +#include "source/common/protobuf/protobuf.h" #include "test/test_common/logging.h" -#include "test/test_common/utility.h" #include "test/test_common/status_utility.h" -#include "source/common/protobuf/protobuf.h" +#include "test/test_common/utility.h" + #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" -#include "re2/re2.h" #include "gtest/gtest.h" +#include "re2/re2.h" namespace matching { diff --git a/source/common/common/matching/url_template_matching_test.cc b/source/common/common/matching/url_template_matching_test.cc index b19125583b605..12d6258232be7 100644 --- a/source/common/common/matching/url_template_matching_test.cc +++ b/source/common/common/matching/url_template_matching_test.cc @@ -1,17 +1,15 @@ -#include "source/common/common/matching/url_template_matching_internal.h" - #include #include #include -#include "source/common/common/matching/url_template_matching.h" - #include "source/common/common/assert.h" +#include "source/common/common/matching/url_template_matching.h" +#include "source/common/common/matching/url_template_matching_internal.h" +#include "source/common/protobuf/protobuf.h" #include "test/test_common/logging.h" -#include "test/test_common/utility.h" #include "test/test_common/status_utility.h" -#include "source/common/protobuf/protobuf.h" +#include "test/test_common/utility.h" #include "gtest/gtest.h" From 6f8bd803b2e66c5b2f87150c78e5082bd4fc1bea Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 27 Jun 2022 19:46:26 +0000 Subject: [PATCH 004/238] clang-format Signed-off-by: silverstar195 --- .../config/route/v3/route_components.proto | 3 ++- source/common/common/matching/BUILD | 2 -- .../common/matching/url_template_matching.cc | 2 +- .../common/matching/url_template_matching.h | 6 ++--- .../matching/url_template_matching_internal.h | 2 +- .../url_template_matching_internal_test.cc | 22 +++++++++---------- .../matching/url_template_matching_test.cc | 2 +- 7 files changed, 19 insertions(+), 20 deletions(-) diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index 5c63cced9a372..c3e7d975b420d 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -1363,19 +1363,20 @@ message RouteAction { } // Holds the segments for rewriting urls base on pattern templates -// [#next-free-field: 4] message RouteUrlRewritePattern { message RewriteSegment { oneof segment_type { // Represents a segment of the rewritten URL, including any path segments, // slash and prefix. string literal = 1; + // Represents an index into the RE2 capture which value should be used // to construct the rewritten URL. Note that the index should be greater // than 0 as 0 index into the whole match RE2 pattern. int32 var_index = 2; } } + repeated RewriteSegment segments = 3; } diff --git a/source/common/common/matching/BUILD b/source/common/common/matching/BUILD index ef64a17b8ccef..86b3bf4462443 100644 --- a/source/common/common/matching/BUILD +++ b/source/common/common/matching/BUILD @@ -12,8 +12,6 @@ licenses(["notice"]) # Apache 2 envoy_package() -envoy_extension_package() - envoy_cc_library( name = "url_template_matching", srcs = ["url_template_matching.cc"], diff --git a/source/common/common/matching/url_template_matching.cc b/source/common/common/matching/url_template_matching.cc index beca7637f1416..54abb72009ba2 100644 --- a/source/common/common/matching/url_template_matching.cc +++ b/source/common/common/matching/url_template_matching.cc @@ -164,4 +164,4 @@ bool IsValidSharedVariableSet(const std::string& path_template_rewrite, return ParseRewritePattern(path_template_rewrite, capture_regex).ok(); } -} // namespace matching \ No newline at end of file +} // namespace matching diff --git a/source/common/common/matching/url_template_matching.h b/source/common/common/matching/url_template_matching.h index bb1db29baec29..0a9966be1751e 100644 --- a/source/common/common/matching/url_template_matching.h +++ b/source/common/common/matching/url_template_matching.h @@ -36,13 +36,13 @@ absl::StatusOr> ParseRewritePatternHelper(absl::string_view pattern); // Returns the parsed Url rewrite pattern to be used by -// RewriteURLTemplatePattern() in the BdnFilter. |capture_regex| should +// RewriteURLTemplatePattern() |capture_regex| should // be the regex generated by ConvertURLPatternSyntaxToRegex(). absl::StatusOr ParseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); // Returns the rewritten URL path based on the given parsed rewrite pattern. -// Used in the BdnFilter for template-based URL rewrite. +// Used for template-based URL rewrite. absl::StatusOr RewriteURLTemplatePattern(absl::string_view url, absl::string_view capture_regex, const envoy::config::route::v3::RouteUrlRewritePattern& rewrite_pattern); @@ -59,4 +59,4 @@ bool IsValidSharedVariableSet(const std::string& path_template_rewrite, } // namespace matching -#endif // SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_H \ No newline at end of file +#endif // SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_H diff --git a/source/common/common/matching/url_template_matching_internal.h b/source/common/common/matching/url_template_matching_internal.h index 962c988f95c78..cf29126f68707 100644 --- a/source/common/common/matching/url_template_matching_internal.h +++ b/source/common/common/matching/url_template_matching_internal.h @@ -79,4 +79,4 @@ inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.dat } // namespace matching -#endif // SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H \ No newline at end of file +#endif // SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H diff --git a/source/common/common/matching/url_template_matching_internal_test.cc b/source/common/common/matching/url_template_matching_internal_test.cc index 33ccedcd82318..43b4622759434 100644 --- a/source/common/common/matching/url_template_matching_internal_test.cc +++ b/source/common/common/matching/url_template_matching_internal_test.cc @@ -78,16 +78,16 @@ TEST(InternalParsing, IsValidRewriteLiteralWorks) { } TEST(InternalParsing, IsValidIdentWorks) { - EXPECT_TRUE(IsValidIdent("abc")); - EXPECT_TRUE(IsValidIdent("ABC_def_123")); - EXPECT_TRUE(IsValidIdent("a1")); - EXPECT_TRUE(IsValidIdent("T")); - EXPECT_FALSE(IsValidIdent("123")); - EXPECT_FALSE(IsValidIdent("__undefined__")); - EXPECT_FALSE(IsValidIdent("abc-def")); - EXPECT_FALSE(IsValidIdent("abc=def")); - EXPECT_FALSE(IsValidIdent("abc def")); - EXPECT_FALSE(IsValidIdent("a!!!")); + EXPECT_TRUE(IsValidIndent("abc")); + EXPECT_TRUE(IsValidIndent("ABC_def_123")); + EXPECT_TRUE(IsValidIndent("a1")); + EXPECT_TRUE(IsValidIndent("T")); + EXPECT_FALSE(IsValidIndent("123")); + EXPECT_FALSE(IsValidIndent("__undefined__")); + EXPECT_FALSE(IsValidIndent("abc-def")); + EXPECT_FALSE(IsValidIndent("abc=def")); + EXPECT_FALSE(IsValidIndent("abc def")); + EXPECT_FALSE(IsValidIndent("a!!!")); } TEST(InternalParsing, ConsumeLiteralWorks) { @@ -463,4 +463,4 @@ TEST_P(GenPatternRegexWithoutMatch, WithCapture) { } // namespace } // namespace url_template_matching_internal -} // namespace matching \ No newline at end of file +} // namespace matching diff --git a/source/common/common/matching/url_template_matching_test.cc b/source/common/common/matching/url_template_matching_test.cc index 12d6258232be7..c8015eb47e7ce 100644 --- a/source/common/common/matching/url_template_matching_test.cc +++ b/source/common/common/matching/url_template_matching_test.cc @@ -330,4 +330,4 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { } // namespace -} // namespace matching \ No newline at end of file +} // namespace matching From 4bde480d6246765f12784ed79ff0d714ef068778 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 27 Jun 2022 20:32:03 +0000 Subject: [PATCH 005/238] Formting Signed-off-by: silverstar195 --- source/common/common/matching/BUILD | 1 - .../common/matching/url_template_matching.cc | 4 +++- .../common/matching/url_template_matching.h | 2 ++ .../url_template_matching_internal.cc | 22 ++++++++++--------- .../matching/url_template_matching_internal.h | 12 +++++----- .../url_template_matching_internal_test.cc | 2 ++ .../matching/url_template_matching_test.cc | 2 ++ 7 files changed, 28 insertions(+), 17 deletions(-) diff --git a/source/common/common/matching/BUILD b/source/common/common/matching/BUILD index 86b3bf4462443..0776abebe83fa 100644 --- a/source/common/common/matching/BUILD +++ b/source/common/common/matching/BUILD @@ -2,7 +2,6 @@ load( "//bazel:envoy_build_system.bzl", "envoy_cc_library", "envoy_cc_test", - "envoy_extension_package", "envoy_package", ) diff --git a/source/common/common/matching/url_template_matching.cc b/source/common/common/matching/url_template_matching.cc index 54abb72009ba2..c1eb5ca153278 100644 --- a/source/common/common/matching/url_template_matching.cc +++ b/source/common/common/matching/url_template_matching.cc @@ -14,6 +14,7 @@ #include "absl/strings/string_view.h" #include "re2/re2.h" +namespace Envoy { namespace matching { using ::matching::url_template_matching_internal::ParsedUrlPattern; @@ -77,7 +78,7 @@ ParseRewritePatternHelper(absl::string_view pattern) { } pattern = segments2[1]; - if (!url_template_matching_internal::IsValidIdent(segments2[0])) { + if (!url_template_matching_internal::IsValidIndent(segments2[0])) { return absl::InvalidArgumentError("Invalid variable name"); } result.emplace_back(segments2[0], RewriteStringKind::kVariable); @@ -165,3 +166,4 @@ bool IsValidSharedVariableSet(const std::string& path_template_rewrite, } } // namespace matching +} // namespace Envoy diff --git a/source/common/common/matching/url_template_matching.h b/source/common/common/matching/url_template_matching.h index 0a9966be1751e..f0eddea03f12d 100644 --- a/source/common/common/matching/url_template_matching.h +++ b/source/common/common/matching/url_template_matching.h @@ -10,6 +10,7 @@ #include "absl/status/statusor.h" #include "absl/strings/string_view.h" +namespace Envoy { namespace matching { enum class RewriteStringKind { kVariable, kLiteral }; @@ -58,5 +59,6 @@ bool IsValidSharedVariableSet(const std::string& path_template_rewrite, absl::string_view capture_regex); } // namespace matching +} // namespace Envoy #endif // SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_H diff --git a/source/common/common/matching/url_template_matching_internal.cc b/source/common/common/matching/url_template_matching_internal.cc index 18bfc6c5a4a1a..f46709e3e6ef0 100644 --- a/source/common/common/matching/url_template_matching_internal.cc +++ b/source/common/common/matching/url_template_matching_internal.cc @@ -20,6 +20,7 @@ #include "absl/types/variant.h" #include "re2/re2.h" +namespace Envoy { namespace matching { namespace url_template_matching_internal { @@ -52,7 +53,7 @@ struct ToStringVisitor { // Formatter used to allow joining variants together with StrJoin. struct ToStringFormatter { template void operator()(std::string* out, const T& t) const { - absl::StrAppend(out, std::visit(ToStringVisitor(), t)); + absl::StrAppend(out, absl::visit(ToStringVisitor(), t)); } }; @@ -64,7 +65,7 @@ struct ToRegexPatternVisitor { // Formatter used to allow joining variants together with StrJoin. struct ToRegexPatternFormatter { template void operator()(std::string* out, const T& t) const { - absl::StrAppend(out, std::visit(ToRegexPatternVisitor(), t)); + absl::StrAppend(out, absl::visit(ToRegexPatternVisitor(), t)); } }; @@ -130,7 +131,7 @@ bool IsValidRewriteLiteral(absl::string_view pattern) { return RE2::FullMatch(ToStringPiece(pattern), *literal_regex); } -bool IsValidIdent(absl::string_view pattern) { +bool IsValidIndent(absl::string_view pattern) { static const LazyRE2 ident_regex = {"^[a-zA-Z][a-zA-Z0-9_]*$"}; return RE2::FullMatch(ToStringPiece(pattern), *ident_regex); } @@ -168,7 +169,7 @@ absl::StatusOr> ConsumeVariable(absl::string_view pattern // Parse the actual variable pattern, starting with the variable name. std::vector var_parts = absl::StrSplit(parts[0], absl::MaxSplits('=', 1)); - if (!IsValidIdent(var_parts[0])) { + if (!IsValidIndent(var_parts[0])) { return absl::InvalidArgumentError("Invalid variable name"); } Variable var = Variable(var_parts[0], {}); @@ -182,7 +183,7 @@ absl::StatusOr> ConsumeVariable(absl::string_view pattern return absl::InvalidArgumentError("Empty variable match"); } while (!var_patt.empty()) { - std::variant var_match; + absl::variant var_match; if (var_patt[0] == '*') { absl::StatusOr status = AlsoUpdatePattern(ConsumeOperator, &var_patt); @@ -216,7 +217,7 @@ GatherCaptureNames(struct ParsedUrlPattern pattern) { absl::flat_hash_set captured_variables; for (const ParsedSegment& segment : pattern.parsed_segments) { - if (!std::holds_alternative(segment)) { + if (!absl::holds_alternative(segment)) { continue; } if (captured_variables.size() >= pattern_matching_max_variables_per_url) { @@ -240,12 +241,12 @@ GatherCaptureNames(struct ParsedUrlPattern pattern) { absl::Status ValidateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { bool seen_text_glob = false; for (const ParsedSegment& segment : pattern.parsed_segments) { - if (std::holds_alternative(segment)) { + if (absl::holds_alternative(segment)) { if (seen_text_glob) { return absl::InvalidArgumentError("Glob after text glob."); } seen_text_glob = (std::get(segment) == Operator::kTextGlob); - } else if (std::holds_alternative(segment)) { + } else if (absl::holds_alternative(segment)) { const Variable& var = std::get(segment); if (var.var_match.empty()) { if (seen_text_glob) { @@ -253,8 +254,8 @@ absl::Status ValidateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { return absl::InvalidArgumentError("Implicit variable path glob after text glob."); } } else { - for (const std::variant& var_seg : var.var_match) { - if (!std::holds_alternative(var_seg)) { + for (const absl::variant& var_seg : var.var_match) { + if (!absl::holds_alternative(var_seg)) { continue; } if (seen_text_glob) { @@ -379,3 +380,4 @@ std::string ToRegexPattern(const struct ParsedUrlPattern& pattern) { } // namespace url_template_matching_internal } // namespace matching +} // namespace Envoy diff --git a/source/common/common/matching/url_template_matching_internal.h b/source/common/common/matching/url_template_matching_internal.h index cf29126f68707..a8a2ddfe3fc0f 100644 --- a/source/common/common/matching/url_template_matching_internal.h +++ b/source/common/common/matching/url_template_matching_internal.h @@ -13,6 +13,7 @@ #include "absl/types/variant.h" #include "re2/re2.h" +namespace Envoy { namespace matching { namespace url_template_matching_internal { @@ -22,19 +23,19 @@ enum class Operator { kPathGlob, kTextGlob }; struct Variable { absl::string_view var_name; - std::vector> var_match; + std::vector> var_match; - Variable(absl::string_view name, std::vector> match) + Variable(absl::string_view name, std::vector> match) : var_name(name), var_match(match) {} std::string DebugString() const; }; -using ParsedSegment = std::variant; +using ParsedSegment = absl::variant; struct ParsedUrlPattern { std::vector parsed_segments; - std::optional suffix; + absl::optional suffix; absl::flat_hash_set captured_variables; std::string DebugString() const; @@ -44,7 +45,7 @@ bool IsValidLiteral(absl::string_view pattern); bool IsValidRewriteLiteral(absl::string_view pattern); -bool IsValidIdent(absl::string_view pattern); +bool IsValidIndent(absl::string_view pattern); // Used by the following Consume{Literal.Operator,Variable} functions // in the return value. The functions would take the given pattern, @@ -78,5 +79,6 @@ inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.dat } // namespace url_template_matching_internal } // namespace matching +} // namespace Envoy #endif // SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H diff --git a/source/common/common/matching/url_template_matching_internal_test.cc b/source/common/common/matching/url_template_matching_internal_test.cc index 43b4622759434..dd0c3e741c21c 100644 --- a/source/common/common/matching/url_template_matching_internal_test.cc +++ b/source/common/common/matching/url_template_matching_internal_test.cc @@ -18,6 +18,7 @@ #include "gtest/gtest.h" #include "re2/re2.h" +namespace Envoy { namespace matching { namespace url_template_matching_internal { @@ -464,3 +465,4 @@ TEST_P(GenPatternRegexWithoutMatch, WithCapture) { } // namespace } // namespace url_template_matching_internal } // namespace matching +} // namespace Envoy diff --git a/source/common/common/matching/url_template_matching_test.cc b/source/common/common/matching/url_template_matching_test.cc index c8015eb47e7ce..a6982591fb001 100644 --- a/source/common/common/matching/url_template_matching_test.cc +++ b/source/common/common/matching/url_template_matching_test.cc @@ -13,6 +13,7 @@ #include "gtest/gtest.h" +namespace Envoy { namespace matching { namespace { @@ -331,3 +332,4 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { } // namespace } // namespace matching +} // namespace Envoy From 3747e4b431eee6b31c4f04d06203899688cebec9 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 27 Jun 2022 20:38:20 +0000 Subject: [PATCH 006/238] Style fixes and adding namespace fixes Signed-off-by: silverstar195 --- source/common/common/matching/url_template_matching.cc | 2 +- .../common/matching/url_template_matching_internal.cc | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/source/common/common/matching/url_template_matching.cc b/source/common/common/matching/url_template_matching.cc index c1eb5ca153278..a95536bb556d5 100644 --- a/source/common/common/matching/url_template_matching.cc +++ b/source/common/common/matching/url_template_matching.cc @@ -17,7 +17,7 @@ namespace Envoy { namespace matching { -using ::matching::url_template_matching_internal::ParsedUrlPattern; +using matching::url_template_matching_internal::ParsedUrlPattern; #ifndef SWIG // Silence warnings about missing initializers for members of LazyRE2. diff --git a/source/common/common/matching/url_template_matching_internal.cc b/source/common/common/matching/url_template_matching_internal.cc index f46709e3e6ef0..a6043fa51897b 100644 --- a/source/common/common/matching/url_template_matching_internal.cc +++ b/source/common/common/matching/url_template_matching_internal.cc @@ -223,7 +223,7 @@ GatherCaptureNames(struct ParsedUrlPattern pattern) { if (captured_variables.size() >= pattern_matching_max_variables_per_url) { return absl::InvalidArgumentError("Exceeded variable count limit"); } - absl::string_view var_name = std::get(segment).var_name; + absl::string_view var_name = absl::get(segment).var_name; if (var_name.size() < pattern_matching_min_variable_name_len || var_name.size() > pattern_matching_max_variable_name_len) { @@ -245,9 +245,9 @@ absl::Status ValidateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { if (seen_text_glob) { return absl::InvalidArgumentError("Glob after text glob."); } - seen_text_glob = (std::get(segment) == Operator::kTextGlob); + seen_text_glob = (absl::get(segment) == Operator::kTextGlob); } else if (absl::holds_alternative(segment)) { - const Variable& var = std::get(segment); + const Variable& var = absl::get(segment); if (var.var_match.empty()) { if (seen_text_glob) { // A variable with no explicit matcher is treated as a path glob. @@ -261,7 +261,7 @@ absl::Status ValidateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { if (seen_text_glob) { return absl::InvalidArgumentError("Glob after text glob."); } - seen_text_glob = (std::get(var_seg) == Operator::kTextGlob); + seen_text_glob = (absl::get(var_seg) == Operator::kTextGlob); } } } From 759034a8240d77c0727ec77e4a0d6d460c09f6e7 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 27 Jun 2022 21:02:09 +0000 Subject: [PATCH 007/238] Adding test words to dict Signed-off-by: silverstar195 --- tools/spelling/spelling_dictionary.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tools/spelling/spelling_dictionary.txt b/tools/spelling/spelling_dictionary.txt index aa4aeae03eaf7..0d6f591b0cd09 100644 --- a/tools/spelling/spelling_dictionary.txt +++ b/tools/spelling/spelling_dictionary.txt @@ -29,7 +29,12 @@ BPF Bdecoded Bencoded Repick +SION TRA +abc +abcd +abd +ar btree CAS CB @@ -149,9 +154,13 @@ FQDN FREEBIND FUZZER FUZZERS +delims dereferencing differentially dnsresolvers +endpos +fo +ghi guarddog GC GCC @@ -218,6 +227,10 @@ LEDS LEV LF LHS +hls +jkl +js +lang libsxg LLVM LPT @@ -261,6 +274,7 @@ Oauth OCSP OD ODCDS +mpd oghttp OID OK @@ -963,10 +977,12 @@ parsers passphrase passthrough pathname +patt pausable pausedness pcall pcap +pchar pclose performant pfctl @@ -1172,6 +1188,7 @@ srtt ssize stackdriver stacktrace +startpos starttls startup stateful @@ -1218,6 +1235,7 @@ subtrees subtype subtypes subzone +suf superclass superset svc @@ -1309,6 +1327,7 @@ username usr util utils +va valgrind validator validators @@ -1318,6 +1337,7 @@ variadic varint vec vectorize +ver verifier verifiers versa @@ -1328,6 +1348,7 @@ vip virtualhost virtualize vptr +vtt wakeup wakeups wamr From 92db5f4219e84bf91922ccc1c5f8f3dadcf7ba49 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 28 Jun 2022 15:46:50 +0000 Subject: [PATCH 008/238] Changing style to match envoy casing Signed-off-by: silverstar195 --- .../common/matching/url_template_matching.cc | 28 +-- .../common/matching/url_template_matching.h | 14 +- .../url_template_matching_internal.cc | 70 +++--- .../matching/url_template_matching_internal.h | 24 +- .../url_template_matching_internal_test.cc | 206 +++++++++--------- .../matching/url_template_matching_test.cc | 30 +-- 6 files changed, 186 insertions(+), 186 deletions(-) diff --git a/source/common/common/matching/url_template_matching.cc b/source/common/common/matching/url_template_matching.cc index a95536bb556d5..b85e5406a5d23 100644 --- a/source/common/common/matching/url_template_matching.cc +++ b/source/common/common/matching/url_template_matching.cc @@ -26,24 +26,24 @@ using matching::url_template_matching_internal::ParsedUrlPattern; inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.data(), text.size()}; } -bool IsPatternMatch(absl::string_view pattern, absl::string_view capture_regex) { +bool isPatternMatch(absl::string_view pattern, absl::string_view capture_regex) { RE2 regex = RE2(ToStringPiece(capture_regex)); return RE2::FullMatch(ToStringPiece(pattern), regex); } -absl::StatusOr ConvertURLPatternSyntaxToRegex(absl::string_view url_pattern) { +absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { absl::StatusOr status = - url_template_matching_internal::ParseURLPatternSyntax(url_pattern); + url_template_matching_internal::parseURLPatternSyntax(url_pattern); if (!status.ok()) { return status.status(); } struct ParsedUrlPattern pattern = *std::move(status); - return url_template_matching_internal::ToRegexPattern(pattern); + return url_template_matching_internal::toRegexPattern(pattern); } absl::StatusOr> -ParseRewritePatternHelper(absl::string_view pattern) { +parseRewritePatternHelper(absl::string_view pattern) { std::vector result; // Don't allow contiguous '/' patterns. @@ -60,7 +60,7 @@ ParseRewritePatternHelper(absl::string_view pattern) { while (!pattern.empty()) { std::vector segments1 = absl::StrSplit(pattern, absl::MaxSplits('{', 1)); if (!segments1[0].empty()) { - if (!url_template_matching_internal::IsValidRewriteLiteral(segments1[0])) { + if (!url_template_matching_internal::isValidRewriteLiteral(segments1[0])) { return absl::InvalidArgumentError("Invalid rewrite literal pattern"); } result.emplace_back(segments1[0], RewriteStringKind::kLiteral); @@ -78,7 +78,7 @@ ParseRewritePatternHelper(absl::string_view pattern) { } pattern = segments2[1]; - if (!url_template_matching_internal::IsValidIndent(segments2[0])) { + if (!url_template_matching_internal::isValidIndent(segments2[0])) { return absl::InvalidArgumentError("Invalid variable name"); } result.emplace_back(segments2[0], RewriteStringKind::kVariable); @@ -87,14 +87,14 @@ ParseRewritePatternHelper(absl::string_view pattern) { } absl::StatusOr -ParseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { +parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { envoy::config::route::v3::RouteUrlRewritePattern parsed_pattern; RE2 regex = RE2(ToStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); } - absl::StatusOr> status = ParseRewritePatternHelper(pattern); + absl::StatusOr> status = parseRewritePatternHelper(pattern); if (!status.ok()) { return status.status(); } @@ -153,16 +153,16 @@ RewriteURLTemplatePattern(absl::string_view url, absl::string_view capture_regex } bool IsValidPathTemplateMatchPattern(const std::string& path_template_match) { - return ConvertURLPatternSyntaxToRegex(path_template_match).ok(); + return convertURLPatternSyntaxToRegex(path_template_match).ok(); } -bool IsValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { - return ParseRewritePatternHelper(path_template_rewrite).ok(); +bool isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { + return parseRewritePatternHelper(path_template_rewrite).ok(); } -bool IsValidSharedVariableSet(const std::string& path_template_rewrite, +bool isValidSharedVariableSet(const std::string& path_template_rewrite, absl::string_view capture_regex) { - return ParseRewritePattern(path_template_rewrite, capture_regex).ok(); + return parseRewritePattern(path_template_rewrite, capture_regex).ok(); } } // namespace matching diff --git a/source/common/common/matching/url_template_matching.h b/source/common/common/matching/url_template_matching.h index f0eddea03f12d..2c0f49135be41 100644 --- a/source/common/common/matching/url_template_matching.h +++ b/source/common/common/matching/url_template_matching.h @@ -22,25 +22,25 @@ struct RewritePatternSegment { }; // Returns if the regex pattern matches the given url_pattern. -bool IsPatternMatch(absl::string_view pattern, absl::string_view capture_regex); +bool isPatternMatch(absl::string_view pattern, absl::string_view capture_regex); // Returns the regex pattern that is equivalent to the given url_pattern. // Used in the config pipeline to translate user given url pattern to // the safe regex Envoy can understand. Strips away any variable captures. -absl::StatusOr ConvertURLPatternSyntaxToRegex(absl::string_view url_pattern); +absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); // Helper function that parses the pattern and breaks it down to either // literals or variable names. To be used by ParseRewritePattern(). // Exposed here so that the validator for the rewrite pattern can also // use it. absl::StatusOr> -ParseRewritePatternHelper(absl::string_view pattern); +parseRewritePatternHelper(absl::string_view pattern); // Returns the parsed Url rewrite pattern to be used by // RewriteURLTemplatePattern() |capture_regex| should // be the regex generated by ConvertURLPatternSyntaxToRegex(). absl::StatusOr -ParseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); +parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); // Returns the rewritten URL path based on the given parsed rewrite pattern. // Used for template-based URL rewrite. @@ -49,13 +49,13 @@ RewriteURLTemplatePattern(absl::string_view url, absl::string_view capture_regex const envoy::config::route::v3::RouteUrlRewritePattern& rewrite_pattern); // Returns if provided template match pattern is valid -bool IsValidPathTemplateMatchPattern(const std::string& path_template_match); +bool isValidPathTemplateMatchPattern(const std::string& path_template_match); // Returns if provided rewrite pattern is valid -bool IsValidPathTemplateRewritePattern(const std::string& path_template_rewrite); +bool isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); // Returns if path_template and rewrite_template have valid variables -bool IsValidSharedVariableSet(const std::string& path_template_rewrite, +bool isValidSharedVariableSet(const std::string& path_template_rewrite, absl::string_view capture_regex); } // namespace matching diff --git a/source/common/common/matching/url_template_matching_internal.cc b/source/common/common/matching/url_template_matching_internal.cc index a6043fa51897b..72d0e833678ff 100644 --- a/source/common/common/matching/url_template_matching_internal.cc +++ b/source/common/common/matching/url_template_matching_internal.cc @@ -59,7 +59,7 @@ struct ToStringFormatter { // Visitor for converting a ParsedSegment variant to the regex. struct ToRegexPatternVisitor { - template std::string operator()(const T& val) const { return ToRegexPattern(val); } + template std::string operator()(const T& val) const { return toRegexPattern(val); } }; // Formatter used to allow joining variants together with StrJoin. @@ -69,9 +69,9 @@ struct ToRegexPatternFormatter { } }; -std::string ToString(const Literal val) { return std::string(val); } +std::string toString(const Literal val) { return std::string(val); } -std::string ToString(const Operator val) { +std::string toString(const Operator val) { switch (val) { case Operator::kPathGlob: return "*"; @@ -80,7 +80,7 @@ std::string ToString(const Operator val) { } } -std::string ToString(const Variable val) { +std::string toString(const Variable val) { if (val.var_match.empty()) { return absl::StrCat("{", val.var_name, "}"); } @@ -90,7 +90,7 @@ std::string ToString(const Variable val) { } template std::string ToStringVisitor::operator()(const T& val) const { - return ToString(val); + return toString(val); } template @@ -110,43 +110,43 @@ absl::StatusOr AlsoUpdatePattern( } // namespace -std::string Variable::DebugString() const { return ToString(*this); } +std::string Variable::DebugString() const { return toString(*this); } std::string ParsedUrlPattern::DebugString() const { return absl::StrCat("/", absl::StrJoin(parsed_segments, "/", ToStringFormatter()), suffix.value_or("")); } -bool IsValidLiteral(absl::string_view pattern) { +bool isValidLiteral(absl::string_view pattern) { static const std::string* kValidLiteralRegex = new std::string(absl::StrCat("^[", kLiteral, "]+$")); static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; - return RE2::FullMatch(ToStringPiece(pattern), *literal_regex); + return RE2::FullMatch(toStringPiece(pattern), *literal_regex); } -bool IsValidRewriteLiteral(absl::string_view pattern) { +bool isValidRewriteLiteral(absl::string_view pattern) { static const std::string* kValidLiteralRegex = new std::string(absl::StrCat("^[", kLiteral, "/]+$")); static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; - return RE2::FullMatch(ToStringPiece(pattern), *literal_regex); + return RE2::FullMatch(toStringPiece(pattern), *literal_regex); } -bool IsValidIndent(absl::string_view pattern) { +bool isValidIndent(absl::string_view pattern) { static const LazyRE2 ident_regex = {"^[a-zA-Z][a-zA-Z0-9_]*$"}; - return RE2::FullMatch(ToStringPiece(pattern), *ident_regex); + return RE2::FullMatch(toStringPiece(pattern), *ident_regex); } -absl::StatusOr> ConsumeLiteral(absl::string_view pattern) { +absl::StatusOr> consumeLiteral(absl::string_view pattern) { absl::string_view lit = std::vector(absl::StrSplit(pattern, absl::MaxSplits('/', 1)))[0]; absl::string_view unconsumed_pattern = pattern.substr(lit.size()); - if (!IsValidLiteral(lit)) { + if (!isValidLiteral(lit)) { return absl::InvalidArgumentError("Invalid literal"); } return ParsedResult(lit, unconsumed_pattern); } -absl::StatusOr> ConsumeOperator(absl::string_view pattern) { +absl::StatusOr> consumeOperator(absl::string_view pattern) { if (absl::StartsWith(pattern, "**")) { return ParsedResult(Operator::kTextGlob, pattern.substr(2)); } @@ -156,7 +156,7 @@ absl::StatusOr> ConsumeOperator(absl::string_view pattern return absl::InvalidArgumentError("Invalid Operator"); } -absl::StatusOr> ConsumeVariable(absl::string_view pattern) { +absl::StatusOr> consumeVariable(absl::string_view pattern) { // Locate the variable pattern to parse. if (pattern.size() < 2 || (pattern)[0] != '{') { return absl::InvalidArgumentError("Invalid variable"); @@ -169,7 +169,7 @@ absl::StatusOr> ConsumeVariable(absl::string_view pattern // Parse the actual variable pattern, starting with the variable name. std::vector var_parts = absl::StrSplit(parts[0], absl::MaxSplits('=', 1)); - if (!IsValidIndent(var_parts[0])) { + if (!isValidIndent(var_parts[0])) { return absl::InvalidArgumentError("Invalid variable name"); } Variable var = Variable(var_parts[0], {}); @@ -186,7 +186,7 @@ absl::StatusOr> ConsumeVariable(absl::string_view pattern absl::variant var_match; if (var_patt[0] == '*') { - absl::StatusOr status = AlsoUpdatePattern(ConsumeOperator, &var_patt); + absl::StatusOr status = AlsoUpdatePattern(consumeOperator, &var_patt); if (!status.ok()) { return status.status(); } @@ -194,7 +194,7 @@ absl::StatusOr> ConsumeVariable(absl::string_view pattern } else { - absl::StatusOr status = AlsoUpdatePattern(ConsumeLiteral, &var_patt); + absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &var_patt); if (!status.ok()) { return status.status(); } @@ -213,7 +213,7 @@ absl::StatusOr> ConsumeVariable(absl::string_view pattern } absl::StatusOr> -GatherCaptureNames(struct ParsedUrlPattern pattern) { +gatherCaptureNames(struct ParsedUrlPattern pattern) { absl::flat_hash_set captured_variables; for (const ParsedSegment& segment : pattern.parsed_segments) { @@ -238,7 +238,7 @@ GatherCaptureNames(struct ParsedUrlPattern pattern) { return captured_variables; } -absl::Status ValidateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { +absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { bool seen_text_glob = false; for (const ParsedSegment& segment : pattern.parsed_segments) { if (absl::holds_alternative(segment)) { @@ -269,11 +269,11 @@ absl::Status ValidateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { return absl::OkStatus(); } -absl::StatusOr ParseURLPatternSyntax(absl::string_view url_pattern) { +absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern) { struct ParsedUrlPattern parsed_pattern; static const LazyRE2 printable_regex = {"^/[[:graph:]]*$"}; - if (!RE2::FullMatch(ToStringPiece(url_pattern), *printable_regex)) { + if (!RE2::FullMatch(toStringPiece(url_pattern), *printable_regex)) { return absl::InvalidArgumentError("Invalid pattern"); } @@ -286,21 +286,21 @@ absl::StatusOr ParseURLPatternSyntax(absl::string_view url_pat ParsedSegment segment; if (url_pattern[0] == '*') { - absl::StatusOr status = AlsoUpdatePattern(ConsumeOperator, &url_pattern); + absl::StatusOr status = AlsoUpdatePattern(consumeOperator, &url_pattern); if (!status.ok()) { return status.status(); } segment = *std::move(status); } else if (url_pattern[0] == '{') { - absl::StatusOr status = AlsoUpdatePattern(ConsumeVariable, &url_pattern); + absl::StatusOr status = AlsoUpdatePattern(consumeVariable, &url_pattern); if (!status.ok()) { return status.status(); } segment = *std::move(status); } else { - absl::StatusOr status = AlsoUpdatePattern(ConsumeLiteral, &url_pattern); + absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &url_pattern); if (!status.ok()) { return status.status(); } @@ -320,7 +320,7 @@ absl::StatusOr ParseURLPatternSyntax(absl::string_view url_pat } else { // Not followed by '/', treat as suffix. - absl::StatusOr status = AlsoUpdatePattern(ConsumeLiteral, &url_pattern); + absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &url_pattern); if (!status.ok()) { return status.status(); } @@ -334,13 +334,13 @@ absl::StatusOr ParseURLPatternSyntax(absl::string_view url_pat } } absl::StatusOr> status = - GatherCaptureNames(parsed_pattern); + gatherCaptureNames(parsed_pattern); if (!status.ok()) { return status.status(); } parsed_pattern.captured_variables = *std::move(status); - absl::Status validate_status = ValidateNoOperatorAfterTextGlob(parsed_pattern); + absl::Status validate_status = validateNoOperatorAfterTextGlob(parsed_pattern); if (!validate_status.ok()) { return validate_status; } @@ -348,12 +348,12 @@ absl::StatusOr ParseURLPatternSyntax(absl::string_view url_pat return parsed_pattern; } -std::string ToRegexPattern(Literal pattern) { +std::string toRegexPattern(absl::string_view pattern) { return absl::StrReplaceAll( pattern, {{"$", "\\$"}, {"(", "\\("}, {")", "\\)"}, {"+", "\\+"}, {".", "\\."}}); } -std::string ToRegexPattern(Operator pattern) { +std::string toRegexPattern(Operator pattern) { static const std::string* kPathGlobRegex = new std::string(absl::StrCat("[", kLiteral, "]+")); static const std::string* kTextGlobRegex = new std::string(absl::StrCat("[", kLiteral, "/]*")); switch (pattern) { @@ -364,17 +364,17 @@ std::string ToRegexPattern(Operator pattern) { } } -std::string ToRegexPattern(const Variable& pattern) { +std::string toRegexPattern(const Variable& pattern) { return absl::StrCat("(?P<", pattern.var_name, ">", pattern.var_match.empty() - ? ToRegexPattern(kDefaultVariableOperator) + ? toRegexPattern(kDefaultVariableOperator) : absl::StrJoin(pattern.var_match, "/", ToRegexPatternFormatter()), ")"); } -std::string ToRegexPattern(const struct ParsedUrlPattern& pattern) { +std::string toRegexPattern(const struct ParsedUrlPattern& pattern) { return absl::StrCat("/", absl::StrJoin(pattern.parsed_segments, "/", ToRegexPatternFormatter()), - ToRegexPattern(pattern.suffix.value_or(""))); + toRegexPattern(pattern.suffix.value_or(""))); } } // namespace url_template_matching_internal diff --git a/source/common/common/matching/url_template_matching_internal.h b/source/common/common/matching/url_template_matching_internal.h index a8a2ddfe3fc0f..97816cf0ddd2c 100644 --- a/source/common/common/matching/url_template_matching_internal.h +++ b/source/common/common/matching/url_template_matching_internal.h @@ -41,11 +41,11 @@ struct ParsedUrlPattern { std::string DebugString() const; }; -bool IsValidLiteral(absl::string_view pattern); +bool isValidLiteral(absl::string_view pattern); -bool IsValidRewriteLiteral(absl::string_view pattern); +bool isValidRewriteLiteral(absl::string_view pattern); -bool IsValidIndent(absl::string_view pattern); +bool isValidIndent(absl::string_view pattern); // Used by the following Consume{Literal.Operator,Variable} functions // in the return value. The functions would take the given pattern, @@ -58,23 +58,23 @@ template struct ParsedResult { absl::string_view unconsumed_pattern; }; -absl::StatusOr> ConsumeLiteral(absl::string_view pattern); +absl::StatusOr> consumeLiteral(absl::string_view pattern); -absl::StatusOr> ConsumeOperator(absl::string_view pattern); +absl::StatusOr> consumeOperator(absl::string_view pattern); -absl::StatusOr> ConsumeVariable(absl::string_view pattern); +absl::StatusOr> consumeVariable(absl::string_view pattern); -absl::StatusOr ParseURLPatternSyntax(absl::string_view url_pattern); +absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern); -std::string ToRegexPattern(Literal pattern); +std::string toRegexPattern(Literal pattern); -std::string ToRegexPattern(Operator pattern); +std::string toRegexPattern(Operator pattern); -std::string ToRegexPattern(const Variable& pattern); +std::string toRegexPattern(const Variable& pattern); -std::string ToRegexPattern(const struct ParsedUrlPattern& pattern); +std::string toRegexPattern(const struct ParsedUrlPattern& pattern); -inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.data(), text.size()}; } +inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } } // namespace url_template_matching_internal diff --git a/source/common/common/matching/url_template_matching_internal_test.cc b/source/common/common/matching/url_template_matching_internal_test.cc index dd0c3e741c21c..3a70998a910a5 100644 --- a/source/common/common/matching/url_template_matching_internal_test.cc +++ b/source/common/common/matching/url_template_matching_internal_test.cc @@ -48,53 +48,53 @@ TEST(InternalParsing, ParsedUrlDebugString) { EXPECT_EQ(patt2.DebugString(), "/{var}"); } -TEST(InternalParsing, IsValidLiteralWorks) { - EXPECT_TRUE(IsValidLiteral("123abcABC")); - EXPECT_TRUE(IsValidLiteral("._~-")); - EXPECT_TRUE(IsValidLiteral("-._~%20!$&'()+,;:@")); - EXPECT_FALSE(IsValidLiteral("`~!@#$%^&()-_+;:,<.>'\"\\| ")); - EXPECT_FALSE(IsValidLiteral("abc/")); - EXPECT_FALSE(IsValidLiteral("ab*c")); - EXPECT_FALSE(IsValidLiteral("a**c")); - EXPECT_FALSE(IsValidLiteral("a=c")); - EXPECT_FALSE(IsValidLiteral("?abc")); - EXPECT_FALSE(IsValidLiteral("?a=c")); - EXPECT_FALSE(IsValidLiteral("{abc")); - EXPECT_FALSE(IsValidLiteral("abc}")); - EXPECT_FALSE(IsValidLiteral("{abc}")); +TEST(InternalParsing, isValidLiteralWorks) { + EXPECT_TRUE(isValidLiteral("123abcABC")); + EXPECT_TRUE(isValidLiteral("._~-")); + EXPECT_TRUE(isValidLiteral("-._~%20!$&'()+,;:@")); + EXPECT_FALSE(isValidLiteral("`~!@#$%^&()-_+;:,<.>'\"\\| ")); + EXPECT_FALSE(isValidLiteral("abc/")); + EXPECT_FALSE(isValidLiteral("ab*c")); + EXPECT_FALSE(isValidLiteral("a**c")); + EXPECT_FALSE(isValidLiteral("a=c")); + EXPECT_FALSE(isValidLiteral("?abc")); + EXPECT_FALSE(isValidLiteral("?a=c")); + EXPECT_FALSE(isValidLiteral("{abc")); + EXPECT_FALSE(isValidLiteral("abc}")); + EXPECT_FALSE(isValidLiteral("{abc}")); } -TEST(InternalParsing, IsValidRewriteLiteralWorks) { - EXPECT_TRUE(IsValidRewriteLiteral("123abcABC")); - EXPECT_TRUE(IsValidRewriteLiteral("abc/")); - EXPECT_TRUE(IsValidRewriteLiteral("abc/def")); - EXPECT_TRUE(IsValidRewriteLiteral("/abc.def")); - EXPECT_TRUE(IsValidRewriteLiteral("._~-")); - EXPECT_TRUE(IsValidRewriteLiteral("-._~%20!$&'()+,;:@")); - EXPECT_FALSE(IsValidRewriteLiteral("`~!@#$%^&()-_+;:,<.>'\"| ")); - EXPECT_FALSE(IsValidRewriteLiteral("ab}c")); - EXPECT_FALSE(IsValidRewriteLiteral("ab{c")); - EXPECT_FALSE(IsValidRewriteLiteral("a=c")); - EXPECT_FALSE(IsValidRewriteLiteral("?a=c")); +TEST(InternalParsing, isValidRewriteLiteralWorks) { + EXPECT_TRUE(isValidRewriteLiteral("123abcABC")); + EXPECT_TRUE(isValidRewriteLiteral("abc/")); + EXPECT_TRUE(isValidRewriteLiteral("abc/def")); + EXPECT_TRUE(isValidRewriteLiteral("/abc.def")); + EXPECT_TRUE(isValidRewriteLiteral("._~-")); + EXPECT_TRUE(isValidRewriteLiteral("-._~%20!$&'()+,;:@")); + EXPECT_FALSE(isValidRewriteLiteral("`~!@#$%^&()-_+;:,<.>'\"| ")); + EXPECT_FALSE(isValidRewriteLiteral("ab}c")); + EXPECT_FALSE(isValidRewriteLiteral("ab{c")); + EXPECT_FALSE(isValidRewriteLiteral("a=c")); + EXPECT_FALSE(isValidRewriteLiteral("?a=c")); } -TEST(InternalParsing, IsValidIdentWorks) { - EXPECT_TRUE(IsValidIndent("abc")); - EXPECT_TRUE(IsValidIndent("ABC_def_123")); - EXPECT_TRUE(IsValidIndent("a1")); - EXPECT_TRUE(IsValidIndent("T")); - EXPECT_FALSE(IsValidIndent("123")); - EXPECT_FALSE(IsValidIndent("__undefined__")); - EXPECT_FALSE(IsValidIndent("abc-def")); - EXPECT_FALSE(IsValidIndent("abc=def")); - EXPECT_FALSE(IsValidIndent("abc def")); - EXPECT_FALSE(IsValidIndent("a!!!")); +TEST(InternalParsing, isValidIdentWorks) { + EXPECT_TRUE(isValidIndent("abc")); + EXPECT_TRUE(isValidIndent("ABC_def_123")); + EXPECT_TRUE(isValidIndent("a1")); + EXPECT_TRUE(isValidIndent("T")); + EXPECT_FALSE(isValidIndent("123")); + EXPECT_FALSE(isValidIndent("__undefined__")); + EXPECT_FALSE(isValidIndent("abc-def")); + EXPECT_FALSE(isValidIndent("abc=def")); + EXPECT_FALSE(isValidIndent("abc def")); + EXPECT_FALSE(isValidIndent("a!!!")); } TEST(InternalParsing, ConsumeLiteralWorks) { std::string pattern = "abc/123"; - absl::StatusOr> result = ConsumeLiteral(pattern); + absl::StatusOr> result = consumeLiteral(pattern); ASSERT_OK(result); EXPECT_EQ(result->parsed_value, "abc"); @@ -104,7 +104,7 @@ TEST(InternalParsing, ConsumeLiteralWorks) { TEST(InternalParsing, ConsumeTextGlob) { std::string pattern = "***abc/123"; - absl::StatusOr> result = ConsumeOperator(pattern); + absl::StatusOr> result = consumeOperator(pattern); ASSERT_OK(result); EXPECT_EQ(result->parsed_value, Operator::kTextGlob); @@ -114,7 +114,7 @@ TEST(InternalParsing, ConsumeTextGlob) { TEST(InternalParsing, ConsumePathGlob) { std::string pattern = "*/123"; - absl::StatusOr> result = ConsumeOperator(pattern); + absl::StatusOr> result = consumeOperator(pattern); ASSERT_OK(result); EXPECT_EQ(result->parsed_value, Operator::kPathGlob); @@ -131,7 +131,7 @@ TEST_P(ConsumeVariableSuccess, ConsumeVariableSuccessTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - absl::StatusOr> result = ConsumeVariable(pattern); + absl::StatusOr> result = consumeVariable(pattern); ASSERT_OK(result); EXPECT_EQ(result->parsed_value.DebugString(), pattern); @@ -150,13 +150,13 @@ TEST_P(ConsumeVariableFailure, ConsumeVariableFailureTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - EXPECT_THAT(ConsumeVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(consumeVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); } -class ParseURLPatternSyntaxSuccess : public testing::TestWithParam {}; +class parseURLPatternSyntaxSuccess : public testing::TestWithParam {}; INSTANTIATE_TEST_SUITE_P( - ParseURLPatternSyntaxSuccessTestSuite, ParseURLPatternSyntaxSuccess, + parseURLPatternSyntaxSuccessTestSuite, parseURLPatternSyntaxSuccess, testing::Values("/**.m3u8", "/**.mpd", "/*_suf", "/{path=**}.m3u8", "/{foo}/**.ts", "/media/*.m4s", "/media/{contentId=*}/**", "/media/*", "/api/*/*/**", "/api/*/v1/**", "/api/*/v1/*", "/{version=api/*}/*", "/api/*/*/", @@ -169,19 +169,19 @@ INSTANTIATE_TEST_SUITE_P( "/api/{version=*}/{url=**}", "/api/{VERSION}/{version}/{verSION}", "/api/1234/abcd", "/media/abcd/%10%20%30/{v1=*/%10%20}_suffix", "/")); -TEST_P(ParseURLPatternSyntaxSuccess, ParseURLPatternSyntaxSuccessTest) { +TEST_P(parseURLPatternSyntaxSuccess, parseURLPatternSyntaxSuccessTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - absl::StatusOr parsed_patt = ParseURLPatternSyntax(pattern); + absl::StatusOr parsed_patt = parseURLPatternSyntax(pattern); ASSERT_OK(parsed_patt); EXPECT_EQ(parsed_patt->DebugString(), pattern); } -class ParseURLPatternSyntaxFailure : public testing::TestWithParam {}; +class parseURLPatternSyntaxFailure : public testing::TestWithParam {}; INSTANTIATE_TEST_SUITE_P( - ParseURLPatternSyntaxFailureTestSuite, ParseURLPatternSyntaxFailure, + parseURLPatternSyntaxFailureTestSuite, parseURLPatternSyntaxFailure, testing::Values("/api/v*/1234", "/api/{version=v*}/1234", "/api/v{versionNum=*}/1234", "/api/{version=*beta}/1234", "/media/eff456/ll-sd-out.{ext}", "/media/eff456/ll-sd-out.{ext=*}", "/media/eff456/ll-sd-out.**", @@ -193,39 +193,39 @@ INSTANTIATE_TEST_SUITE_P( "/{var1}/{var2}/{var3}/{var4}/{var5}/{var6}", "/{=*}", "/{var12345678901234=*}")); -TEST_P(ParseURLPatternSyntaxFailure, ParseURLPatternSyntaxFailureTest) { +TEST_P(parseURLPatternSyntaxFailure, parseURLPatternSyntaxFailureTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - EXPECT_THAT(ParseURLPatternSyntax(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(parseURLPatternSyntax(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); } TEST(InternalRegexGen, LiteralEscapes) { - EXPECT_EQ(ToRegexPattern("abcABC123/-._~%20!$&'()+,;:@"), + EXPECT_EQ(toRegexPattern("abcABC123/-._~%20!$&'()+,;:@"), "abcABC123/-\\._~%20!\\$&'\\(\\)\\+,;:@"); } TEST(InternalRegexGen, LiteralMatches) { absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; - EXPECT_TRUE(RE2::FullMatch(ToStringPiece(kPattern), ToRegexPattern(kPattern))); + EXPECT_TRUE(RE2::FullMatch(toStringPiece(kPattern), toRegexPattern(kPattern))); } TEST(InternalRegexGen, LiteralMatchesInNamedCapture) { absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; - RE2 regex = RE2(absl::StrCat("(?P", ToRegexPattern(kPattern), ")")); + RE2 regex = RE2(absl::StrCat("(?P", toRegexPattern(kPattern), ")")); ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); // Full matched string + capture groups std::vector captures(2); - ASSERT_TRUE(regex.Match(ToStringPiece(kPattern), /*startpos=*/0, /*endpos=*/kPattern.size(), + ASSERT_TRUE(regex.Match(toStringPiece(kPattern), /*startpos=*/0, /*endpos=*/kPattern.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())); // Index 0 would be the full text of the matched string. - EXPECT_EQ(ToStringPiece(kPattern), captures[0]); + EXPECT_EQ(toStringPiece(kPattern), captures[0]); // Get the pattern matched with the named capture group. - EXPECT_EQ(ToStringPiece(kPattern), captures.at(regex.NamedCapturingGroups().at("var"))); + EXPECT_EQ(toStringPiece(kPattern), captures.at(regex.NamedCapturingGroups().at("var"))); } TEST(InternalRegexGen, LiteralOnlyMatchesItself) { @@ -233,102 +233,102 @@ TEST(InternalRegexGen, LiteralOnlyMatchesItself) { for (const char c : kChars) { std::string s = {'z', c, 'z'}; - EXPECT_TRUE(RE2::FullMatch(s, ToRegexPattern(s))); - EXPECT_FALSE(RE2::FullMatch("zzz", ToRegexPattern(s))); + EXPECT_TRUE(RE2::FullMatch(s, toRegexPattern(s))); + EXPECT_FALSE(RE2::FullMatch("zzz", toRegexPattern(s))); } } TEST(InternalRegexGen, RegexLikePatternIsMatchedLiterally) { - EXPECT_TRUE(RE2::FullMatch("(abc)", ToRegexPattern("(abc)"))); - EXPECT_FALSE(RE2::FullMatch("abc", ToRegexPattern("(abc)+"))); + EXPECT_TRUE(RE2::FullMatch("(abc)", toRegexPattern("(abc)"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); - EXPECT_TRUE(RE2::FullMatch("(abc)+", ToRegexPattern("(abc)+"))); - EXPECT_FALSE(RE2::FullMatch("", ToRegexPattern("(abc)+"))); - EXPECT_FALSE(RE2::FullMatch("abc", ToRegexPattern("(abc)+"))); - EXPECT_FALSE(RE2::FullMatch("abcabc", ToRegexPattern("(abc)+"))); + EXPECT_TRUE(RE2::FullMatch("(abc)+", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("abcabc", toRegexPattern("(abc)+"))); - EXPECT_TRUE(RE2::FullMatch(".+", ToRegexPattern(".+"))); - EXPECT_FALSE(RE2::FullMatch("abc", ToRegexPattern(".+"))); + EXPECT_TRUE(RE2::FullMatch(".+", toRegexPattern(".+"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern(".+"))); - EXPECT_TRUE(RE2::FullMatch("a+", ToRegexPattern("a+"))); - EXPECT_FALSE(RE2::FullMatch("aa", ToRegexPattern("a+"))); + EXPECT_TRUE(RE2::FullMatch("a+", toRegexPattern("a+"))); + EXPECT_FALSE(RE2::FullMatch("aa", toRegexPattern("a+"))); } TEST(InternalRegexGen, DollarSignMatchesIfself) { - EXPECT_TRUE(RE2::FullMatch("abc$", ToRegexPattern("abc$"))); - EXPECT_FALSE(RE2::FullMatch("abc", ToRegexPattern("abc$"))); + EXPECT_TRUE(RE2::FullMatch("abc$", toRegexPattern("abc$"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("abc$"))); } TEST(InternalRegexGen, OperatorRegexPattern) { - EXPECT_EQ(ToRegexPattern(Operator::kPathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); - EXPECT_EQ(ToRegexPattern(Operator::kTextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); + EXPECT_EQ(toRegexPattern(Operator::kPathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); + EXPECT_EQ(toRegexPattern(Operator::kTextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); } TEST(InternalRegexGen, PathGlobRegex) { - EXPECT_TRUE(RE2::FullMatch("abc.123", ToRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("", ToRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("abc/123", ToRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("*", ToRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("**", ToRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("abc*123", ToRegexPattern(Operator::kPathGlob))); + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kPathGlob))); } TEST(InternalRegexGen, TextGlobRegex) { - EXPECT_TRUE(RE2::FullMatch("abc.123", ToRegexPattern(Operator::kTextGlob))); - EXPECT_TRUE(RE2::FullMatch("", ToRegexPattern(Operator::kTextGlob))); - EXPECT_TRUE(RE2::FullMatch("abc/123", ToRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("*", ToRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("**", ToRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("abc*123", ToRegexPattern(Operator::kTextGlob))); + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kTextGlob))); + EXPECT_TRUE(RE2::FullMatch("", toRegexPattern(Operator::kTextGlob))); + EXPECT_TRUE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kTextGlob))); } TEST(InternalRegexGen, VariableRegexPattern) { - EXPECT_EQ(ToRegexPattern(Variable("var1", {})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"); - EXPECT_EQ(ToRegexPattern(Variable("var2", {Operator::kPathGlob, "abc", Operator::kTextGlob})), + EXPECT_EQ(toRegexPattern(Variable("var1", {})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"); + EXPECT_EQ(toRegexPattern(Variable("var2", {Operator::kPathGlob, "abc", Operator::kTextGlob})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+/abc/" "[a-zA-Z0-9-._~%!$&'()+,;:@/]*)"); } TEST(InternalRegexGen, VariableRegexDefaultMatch) { - absl::StatusOr> var = ConsumeVariable("{var}"); + absl::StatusOr> var = consumeVariable("{var}"); ASSERT_OK(var); std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc", ToRegexPattern(var->parsed_value), &capture)); + EXPECT_TRUE(RE2::FullMatch("abc", toRegexPattern(var->parsed_value), &capture)); EXPECT_EQ(capture, "abc"); } TEST(InternalRegexGen, VariableRegexDefaultNotMatch) { - absl::StatusOr> var = ConsumeVariable("{var}"); + absl::StatusOr> var = consumeVariable("{var}"); ASSERT_OK(var); - EXPECT_FALSE(RE2::FullMatch("abc/def", ToRegexPattern(var->parsed_value))); + EXPECT_FALSE(RE2::FullMatch("abc/def", toRegexPattern(var->parsed_value))); } TEST(InternalRegexGen, VariableRegexSegmentsMatch) { - absl::StatusOr> var = ConsumeVariable("{var=abc/*/def}"); + absl::StatusOr> var = consumeVariable("{var=abc/*/def}"); ASSERT_OK(var); std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc/123/def", ToRegexPattern(var->parsed_value), &capture)); + EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); EXPECT_EQ(capture, "abc/123/def"); } TEST(InternalRegexGen, VariableRegexTextGlobMatch) { - absl::StatusOr> var = ConsumeVariable("{var=**/def}"); + absl::StatusOr> var = consumeVariable("{var=**/def}"); ASSERT_OK(var); std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc/123/def", ToRegexPattern(var->parsed_value), &capture)); + EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); EXPECT_EQ(capture, "abc/123/def"); } TEST(InternalRegexGen, VariableRegexNamedCapture) { re2::StringPiece kPattern = "abc"; - absl::StatusOr> var = ConsumeVariable("{var=*}"); + absl::StatusOr> var = consumeVariable("{var=*}"); ASSERT_OK(var); - RE2 regex = RE2(ToRegexPattern(var->parsed_value)); + RE2 regex = RE2(toRegexPattern(var->parsed_value)); ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); // Full matched string + capture groups @@ -344,13 +344,13 @@ TEST(InternalRegexGen, VariableRegexNamedCapture) { TEST(InternalRegexGen, ParsedURLPatternToRegex) { absl::StatusOr pattern = - ParseURLPatternSyntax("/abc/*/{var1}/def/{var2=*/ghi/**}.jkl"); + parseURLPatternSyntax("/abc/*/{var1}/def/{var2=*/ghi/**}.jkl"); ASSERT_OK(pattern); std::string var1_capture; std::string var2_capture; EXPECT_TRUE(RE2::FullMatch("/abc/123/456/def/789/ghi/%20/($).jkl", - ToRegexPattern(pattern.value()), &var1_capture, &var2_capture)); + toRegexPattern(pattern.value()), &var1_capture, &var2_capture)); EXPECT_EQ(var1_capture, "456"); EXPECT_EQ(var2_capture, "789/ghi/%20/($)"); } @@ -413,10 +413,10 @@ INSTANTIATE_TEST_SUITE_P( {{"VERSION", "v1"}, {"version", "v2"}, {"verSION", "v3"}}))); TEST_P(GenPatternRegexWithMatch, WithCapture) { - absl::StatusOr pattern = ParseURLPatternSyntax(url_pattern()); + absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); ASSERT_OK(pattern); - RE2 regex = RE2(ToRegexPattern(pattern.value())); + RE2 regex = RE2(toRegexPattern(pattern.value())); ASSERT_TRUE(regex.ok()) << regex.error(); ASSERT_EQ(regex.NumberOfCapturingGroups(), var_values().size()); @@ -426,7 +426,7 @@ TEST_P(GenPatternRegexWithMatch, WithCapture) { /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())); - EXPECT_EQ(captures[0], ToStringPiece(request_path())); + EXPECT_EQ(captures[0], toStringPiece(request_path())); for (const auto& [name, value] : var_values()) { int capture_index = regex.NamedCapturingGroups().at(name); @@ -452,10 +452,10 @@ INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWi {"/api/*/1234/", "/api/*/1234/"}}))); TEST_P(GenPatternRegexWithoutMatch, WithCapture) { - absl::StatusOr pattern = ParseURLPatternSyntax(url_pattern()); + absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); ASSERT_OK(pattern); - RE2 regex = RE2(ToRegexPattern(pattern.value())); + RE2 regex = RE2(toRegexPattern(pattern.value())); ASSERT_TRUE(regex.ok()) << regex.error(); EXPECT_FALSE(regex.Match(request_path(), /*startpos=*/0, diff --git a/source/common/common/matching/url_template_matching_test.cc b/source/common/common/matching/url_template_matching_test.cc index a6982591fb001..9b6c2828c853d 100644 --- a/source/common/common/matching/url_template_matching_test.cc +++ b/source/common/common/matching/url_template_matching_test.cc @@ -30,27 +30,27 @@ static constexpr absl::string_view kCaptureRegex = "/(?P[a-zA-Z0-9-._~%!$& static constexpr absl::string_view kMatchUrl = "/val1/val2/val3/val4/val5"; TEST(ConvertURLPattern, ValidPattern) { - EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/abc"), IsOkAndHolds("/abc")); - EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/**.mpd"), + EXPECT_THAT(convertURLPatternSyntaxToRegex("/abc"), IsOkAndHolds("/abc")); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/**.mpd"), IsOkAndHolds("/[a-zA-Z0-9-._~%!$&'()+,;:@/]*\\.mpd")); - EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/api/*/{resource=*}/{method=**}"), + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/*/{resource=*}/{method=**}"), IsOkAndHolds("/api/[a-zA-Z0-9-._~%!$&'()+,;:@]+/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@/]*)")); - EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/api/{VERSION}/{version}/{verSION}"), + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/{VERSION}/{version}/{verSION}"), IsOkAndHolds("/api/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)")); } TEST(ConvertURLPattern, InvalidPattern) { - EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/api/v*/1234"), + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/v*/1234"), StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/media/**/*/**"), + EXPECT_THAT(convertURLPatternSyntaxToRegex("/media/**/*/**"), StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/\001\002\003\004\005\006\007"), + EXPECT_THAT(convertURLPatternSyntaxToRegex("/\001\002\003\004\005\006\007"), StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(ConvertURLPatternSyntaxToRegex("/{var12345678901234=*}"), + EXPECT_THAT(convertURLPatternSyntaxToRegex("/{var12345678901234=*}"), StatusIs(absl::StatusCode::kInvalidArgument)); } @@ -65,7 +65,7 @@ TEST_P(ParseRewriteHelperSuccess, ParseRewriteHelperSuccessTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - EXPECT_OK(ParseRewritePatternHelper(pattern)); + EXPECT_OK(parseRewritePatternHelper(pattern)); } class ParseRewriteHelperFailure : public testing::TestWithParam {}; @@ -78,7 +78,7 @@ TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - EXPECT_THAT(ParseRewritePatternHelper(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(parseRewritePatternHelper(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); } class ParseRewriteSuccess : public testing::TestWithParam> { @@ -92,7 +92,7 @@ class ParseRewriteSuccess : public testing::TestWithParam rewrite = - ParseRewritePattern(rewrite_pattern(), kCaptureRegex); + parseRewritePattern(rewrite_pattern(), kCaptureRegex); ASSERT_OK(rewrite); // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); } @@ -161,7 +161,7 @@ TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - EXPECT_THAT(ParseRewritePattern(pattern, kCaptureRegex), + EXPECT_THAT(parseRewritePattern(pattern, kCaptureRegex), StatusIs(absl::StatusCode::kInvalidArgument)); } @@ -315,11 +315,11 @@ INSTANTIATE_TEST_SUITE_P( "/region/eu/bucket/prod-storage/object.pdf", "/euprod-storage/object.pdf"}}))); TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { - absl::StatusOr regex = ConvertURLPatternSyntaxToRegex(url_pattern()); + absl::StatusOr regex = convertURLPatternSyntaxToRegex(url_pattern()); ASSERT_OK(regex); absl::StatusOr rewrite_proto = - ParseRewritePattern(rewrite_pattern(), regex.value()); + parseRewritePattern(rewrite_pattern(), regex.value()); ASSERT_OK(rewrite_proto); absl::StatusOr rewritten_url = From a17a69606384d2e3a5f26f5e6c840a7a70e059f1 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 6 Jul 2022 13:37:29 +0000 Subject: [PATCH 009/238] Matching library moved to extension and passes tests Signed-off-by: silverstar195 --- api/BUILD | 1 + .../config/route/v3/route_components.proto | 18 - api/envoy/extensions/url_template/v3/BUILD | 12 + .../v3/route_url_rewrite_pattern.proto | 32 ++ source/common/common/BUILD | 1 - source/extensions/extensions_build_config.bzl | 346 ++++++++---------- source/extensions/extensions_metadata.yaml | 5 + .../url_template}/BUILD | 11 +- .../url_template}/url_template_matching.cc | 14 +- .../url_template}/url_template_matching.h | 14 +- .../url_template_matching_internal.cc | 2 +- .../url_template_matching_internal.h | 17 +- .../url_template_matching_internal_test.cc | 2 +- .../url_template_matching_test.cc | 24 +- 14 files changed, 255 insertions(+), 244 deletions(-) create mode 100644 api/envoy/extensions/url_template/v3/BUILD create mode 100644 api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto rename source/{common/common/matching => extensions/url_template}/BUILD (87%) rename source/{common/common/matching => extensions/url_template}/url_template_matching.cc (90%) rename source/{common/common/matching => extensions/url_template}/url_template_matching.h (80%) rename source/{common/common/matching => extensions/url_template}/url_template_matching_internal.cc (99%) rename source/{common/common/matching => extensions/url_template}/url_template_matching_internal.h (79%) rename source/{common/common/matching => extensions/url_template}/url_template_matching_internal_test.cc (99%) rename source/{common/common/matching => extensions/url_template}/url_template_matching_test.cc (93%) diff --git a/api/BUILD b/api/BUILD index 7099a7abee5da..8466b4e8a5fcd 100644 --- a/api/BUILD +++ b/api/BUILD @@ -269,6 +269,7 @@ proto_library( "//envoy/extensions/upstreams/http/tcp/v3:pkg", "//envoy/extensions/upstreams/http/v3:pkg", "//envoy/extensions/upstreams/tcp/generic/v3:pkg", + "//envoy/extensions/url_template/v3:pkg", "//envoy/extensions/wasm/v3:pkg", "//envoy/extensions/watchdog/profile_action/v3:pkg", "//envoy/service/accesslog/v3:pkg", diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index c3e7d975b420d..2a72c2f546515 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -1362,24 +1362,6 @@ message RouteAction { MaxStreamDuration max_stream_duration = 36; } -// Holds the segments for rewriting urls base on pattern templates -message RouteUrlRewritePattern { - message RewriteSegment { - oneof segment_type { - // Represents a segment of the rewritten URL, including any path segments, - // slash and prefix. - string literal = 1; - - // Represents an index into the RE2 capture which value should be used - // to construct the rewritten URL. Note that the index should be greater - // than 0 as 0 index into the whole match RE2 pattern. - int32 var_index = 2; - } - } - - repeated RewriteSegment segments = 3; -} - // HTTP retry :ref:`architecture overview `. // [#next-free-field: 14] message RetryPolicy { diff --git a/api/envoy/extensions/url_template/v3/BUILD b/api/envoy/extensions/url_template/v3/BUILD new file mode 100644 index 0000000000000..1c1a6f6b44235 --- /dev/null +++ b/api/envoy/extensions/url_template/v3/BUILD @@ -0,0 +1,12 @@ +# DO NOT EDIT. This file is generated by tools/proto_format/proto_sync.py. + +load("@envoy_api//bazel:api_build_system.bzl", "api_proto_package") + +licenses(["notice"]) # Apache 2 + +api_proto_package( + deps = [ + "//envoy/config/core/v3:pkg", + "@com_github_cncf_udpa//udpa/annotations:pkg", + ], +) diff --git a/api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto b/api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto new file mode 100644 index 0000000000000..b60139af37994 --- /dev/null +++ b/api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto @@ -0,0 +1,32 @@ +syntax = "proto3"; + +package envoy.extensions.url_template.v3; + +import "udpa/annotations/status.proto"; + +option java_package = "io.envoyproxy.envoy.extensions.matching.pattern_matcher.v3"; +option java_outer_classname = "RouteUrlRewritePatternProto"; +option java_multiple_files = true; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/matching/pattern_matcher/v3;rewritepatternv3"; +option (udpa.annotations.file_status).package_version_status = ACTIVE; + +// [#protodoc-title: Pattern matcher] +// [#extension: envoy.matching.pattern_matcher] + +// TBD +message RouteUrlRewritePattern { + message RewriteSegment { + oneof segment_type { + // Represents a segment of the rewritten URL, including any path segments, + // slash and prefix. + string literal = 1; + + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int32 var_index = 2; + } + } + + repeated RewriteSegment segments = 3; +} \ No newline at end of file diff --git a/source/common/common/BUILD b/source/common/common/BUILD index acf229c5a0303..0f6d825d52542 100644 --- a/source/common/common/BUILD +++ b/source/common/common/BUILD @@ -297,7 +297,6 @@ envoy_cc_library( ":utility_lib", "//envoy/common:matchers_interface", "//source/common/common:regex_lib", - "//source/common/common/matching:url_template_matching", "//source/common/config:metadata_lib", "//source/common/http:path_utility_lib", "//source/common/protobuf", diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index dc71b3db22a93..ad0f6aacf9649 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -3,362 +3,330 @@ EXTENSIONS = { # # Access loggers # - - "envoy.access_loggers.file": "//source/extensions/access_loggers/file:config", - "envoy.access_loggers.extension_filters.cel": "//source/extensions/access_loggers/filters/cel:config", - "envoy.access_loggers.http_grpc": "//source/extensions/access_loggers/grpc:http_config", - "envoy.access_loggers.tcp_grpc": "//source/extensions/access_loggers/grpc:tcp_config", - "envoy.access_loggers.open_telemetry": "//source/extensions/access_loggers/open_telemetry:config", - "envoy.access_loggers.stdout": "//source/extensions/access_loggers/stream:config", - "envoy.access_loggers.stderr": "//source/extensions/access_loggers/stream:config", - "envoy.access_loggers.wasm": "//source/extensions/access_loggers/wasm:config", + "envoy.access_loggers.file": "//source/extensions/access_loggers/file:config", + "envoy.access_loggers.extension_filters.cel": "//source/extensions/access_loggers/filters/cel:config", + "envoy.access_loggers.http_grpc": "//source/extensions/access_loggers/grpc:http_config", + "envoy.access_loggers.tcp_grpc": "//source/extensions/access_loggers/grpc:tcp_config", + "envoy.access_loggers.open_telemetry": "//source/extensions/access_loggers/open_telemetry:config", + "envoy.access_loggers.stdout": "//source/extensions/access_loggers/stream:config", + "envoy.access_loggers.stderr": "//source/extensions/access_loggers/stream:config", + "envoy.access_loggers.wasm": "//source/extensions/access_loggers/wasm:config", # # Clusters # - - "envoy.clusters.aggregate": "//source/extensions/clusters/aggregate:cluster", - "envoy.clusters.dynamic_forward_proxy": "//source/extensions/clusters/dynamic_forward_proxy:cluster", - "envoy.clusters.redis": "//source/extensions/clusters/redis:redis_cluster", + "envoy.clusters.aggregate": "//source/extensions/clusters/aggregate:cluster", + "envoy.clusters.dynamic_forward_proxy": "//source/extensions/clusters/dynamic_forward_proxy:cluster", + "envoy.clusters.redis": "//source/extensions/clusters/redis:redis_cluster", # # Compression # - - "envoy.compression.gzip.compressor": "//source/extensions/compression/gzip/compressor:config", - "envoy.compression.gzip.decompressor": "//source/extensions/compression/gzip/decompressor:config", - "envoy.compression.brotli.compressor": "//source/extensions/compression/brotli/compressor:config", - "envoy.compression.brotli.decompressor": "//source/extensions/compression/brotli/decompressor:config", - "envoy.compression.zstd.compressor": "//source/extensions/compression/zstd/compressor:config", - "envoy.compression.zstd.decompressor": "//source/extensions/compression/zstd/decompressor:config", + "envoy.compression.gzip.compressor": "//source/extensions/compression/gzip/compressor:config", + "envoy.compression.gzip.decompressor": "//source/extensions/compression/gzip/decompressor:config", + "envoy.compression.brotli.compressor": "//source/extensions/compression/brotli/compressor:config", + "envoy.compression.brotli.decompressor": "//source/extensions/compression/brotli/decompressor:config", + "envoy.compression.zstd.compressor": "//source/extensions/compression/zstd/compressor:config", + "envoy.compression.zstd.decompressor": "//source/extensions/compression/zstd/decompressor:config", # # Config validators # - - "envoy.config.validators.minimum_clusters": "//source/extensions/config/validators/minimum_clusters:config", + "envoy.config.validators.minimum_clusters": "//source/extensions/config/validators/minimum_clusters:config", # # gRPC Credentials Plugins # - - "envoy.grpc_credentials.file_based_metadata": "//source/extensions/grpc_credentials/file_based_metadata:config", - "envoy.grpc_credentials.aws_iam": "//source/extensions/grpc_credentials/aws_iam:config", + "envoy.grpc_credentials.file_based_metadata": "//source/extensions/grpc_credentials/file_based_metadata:config", + "envoy.grpc_credentials.aws_iam": "//source/extensions/grpc_credentials/aws_iam:config", # # WASM # - - "envoy.bootstrap.wasm": "//source/extensions/bootstrap/wasm:config", + "envoy.bootstrap.wasm": "//source/extensions/bootstrap/wasm:config", # # Health checkers # - - "envoy.health_checkers.redis": "//source/extensions/health_checkers/redis:config", + "envoy.health_checkers.redis": "//source/extensions/health_checkers/redis:config", # # Input Matchers # + "envoy.matching.input_matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", + "envoy.matching.input_matchers.ip": "//source/extensions/matching/input_matchers/ip:config", - "envoy.matching.input_matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", - "envoy.matching.input_matchers.ip": "//source/extensions/matching/input_matchers/ip:config", + # + # Pattern Matcher + # + "envoy.url_template": "//source/extensions/url_template:config", # # Generic Inputs # - - "envoy.matching.common_inputs.environment_variable": "//source/extensions/matching/common_inputs/environment_variable:config", + "envoy.matching.common_inputs.environment_variable": "//source/extensions/matching/common_inputs/environment_variable:config", # # HTTP filters # - - "envoy.filters.http.adaptive_concurrency": "//source/extensions/filters/http/adaptive_concurrency:config", - "envoy.filters.http.admission_control": "//source/extensions/filters/http/admission_control:config", - "envoy.filters.http.alternate_protocols_cache": "//source/extensions/filters/http/alternate_protocols_cache:config", - "envoy.filters.http.aws_lambda": "//source/extensions/filters/http/aws_lambda:config", - "envoy.filters.http.aws_request_signing": "//source/extensions/filters/http/aws_request_signing:config", - "envoy.filters.http.bandwidth_limit": "//source/extensions/filters/http/bandwidth_limit:config", - "envoy.filters.http.buffer": "//source/extensions/filters/http/buffer:config", - "envoy.filters.http.cache": "//source/extensions/filters/http/cache:config", - "envoy.filters.http.cdn_loop": "//source/extensions/filters/http/cdn_loop:config", - "envoy.filters.http.compressor": "//source/extensions/filters/http/compressor:config", - "envoy.filters.http.cors": "//source/extensions/filters/http/cors:config", - "envoy.filters.http.composite": "//source/extensions/filters/http/composite:config", - "envoy.filters.http.csrf": "//source/extensions/filters/http/csrf:config", - "envoy.filters.http.decompressor": "//source/extensions/filters/http/decompressor:config", - "envoy.filters.http.dynamic_forward_proxy": "//source/extensions/filters/http/dynamic_forward_proxy:config", - "envoy.filters.http.ext_authz": "//source/extensions/filters/http/ext_authz:config", - "envoy.filters.http.ext_proc": "//source/extensions/filters/http/ext_proc:config", - "envoy.filters.http.fault": "//source/extensions/filters/http/fault:config", - "envoy.filters.http.file_system_buffer": "//source/extensions/filters/http/file_system_buffer:config", - "envoy.filters.http.gcp_authn": "//source/extensions/filters/http/gcp_authn:config", - "envoy.filters.http.grpc_http1_bridge": "//source/extensions/filters/http/grpc_http1_bridge:config", - "envoy.filters.http.grpc_http1_reverse_bridge": "//source/extensions/filters/http/grpc_http1_reverse_bridge:config", - "envoy.filters.http.grpc_json_transcoder": "//source/extensions/filters/http/grpc_json_transcoder:config", - "envoy.filters.http.grpc_stats": "//source/extensions/filters/http/grpc_stats:config", - "envoy.filters.http.grpc_web": "//source/extensions/filters/http/grpc_web:config", - "envoy.filters.http.header_to_metadata": "//source/extensions/filters/http/header_to_metadata:config", - "envoy.filters.http.health_check": "//source/extensions/filters/http/health_check:config", - "envoy.filters.http.ip_tagging": "//source/extensions/filters/http/ip_tagging:config", - "envoy.filters.http.jwt_authn": "//source/extensions/filters/http/jwt_authn:config", + "envoy.filters.http.adaptive_concurrency": "//source/extensions/filters/http/adaptive_concurrency:config", + "envoy.filters.http.admission_control": "//source/extensions/filters/http/admission_control:config", + "envoy.filters.http.alternate_protocols_cache": "//source/extensions/filters/http/alternate_protocols_cache:config", + "envoy.filters.http.aws_lambda": "//source/extensions/filters/http/aws_lambda:config", + "envoy.filters.http.aws_request_signing": "//source/extensions/filters/http/aws_request_signing:config", + "envoy.filters.http.bandwidth_limit": "//source/extensions/filters/http/bandwidth_limit:config", + "envoy.filters.http.buffer": "//source/extensions/filters/http/buffer:config", + "envoy.filters.http.cache": "//source/extensions/filters/http/cache:config", + "envoy.filters.http.cdn_loop": "//source/extensions/filters/http/cdn_loop:config", + "envoy.filters.http.compressor": "//source/extensions/filters/http/compressor:config", + "envoy.filters.http.cors": "//source/extensions/filters/http/cors:config", + "envoy.filters.http.composite": "//source/extensions/filters/http/composite:config", + "envoy.filters.http.csrf": "//source/extensions/filters/http/csrf:config", + "envoy.filters.http.decompressor": "//source/extensions/filters/http/decompressor:config", + "envoy.filters.http.dynamic_forward_proxy": "//source/extensions/filters/http/dynamic_forward_proxy:config", + "envoy.filters.http.ext_authz": "//source/extensions/filters/http/ext_authz:config", + "envoy.filters.http.ext_proc": "//source/extensions/filters/http/ext_proc:config", + "envoy.filters.http.fault": "//source/extensions/filters/http/fault:config", + "envoy.filters.http.file_system_buffer": "//source/extensions/filters/http/file_system_buffer:config", + "envoy.filters.http.gcp_authn": "//source/extensions/filters/http/gcp_authn:config", + "envoy.filters.http.grpc_http1_bridge": "//source/extensions/filters/http/grpc_http1_bridge:config", + "envoy.filters.http.grpc_http1_reverse_bridge": "//source/extensions/filters/http/grpc_http1_reverse_bridge:config", + "envoy.filters.http.grpc_json_transcoder": "//source/extensions/filters/http/grpc_json_transcoder:config", + "envoy.filters.http.grpc_stats": "//source/extensions/filters/http/grpc_stats:config", + "envoy.filters.http.grpc_web": "//source/extensions/filters/http/grpc_web:config", + "envoy.filters.http.header_to_metadata": "//source/extensions/filters/http/header_to_metadata:config", + "envoy.filters.http.health_check": "//source/extensions/filters/http/health_check:config", + "envoy.filters.http.ip_tagging": "//source/extensions/filters/http/ip_tagging:config", + "envoy.filters.http.jwt_authn": "//source/extensions/filters/http/jwt_authn:config", # Disabled by default - "envoy.filters.http.kill_request": "//source/extensions/filters/http/kill_request:kill_request_config", - "envoy.filters.http.local_ratelimit": "//source/extensions/filters/http/local_ratelimit:config", - "envoy.filters.http.lua": "//source/extensions/filters/http/lua:config", - "envoy.filters.http.oauth2": "//source/extensions/filters/http/oauth2:config", - "envoy.filters.http.on_demand": "//source/extensions/filters/http/on_demand:config", - "envoy.filters.http.original_src": "//source/extensions/filters/http/original_src:config", - "envoy.filters.http.ratelimit": "//source/extensions/filters/http/ratelimit:config", - "envoy.filters.http.rbac": "//source/extensions/filters/http/rbac:config", - "envoy.filters.http.router": "//source/extensions/filters/http/router:config", - "envoy.filters.http.set_metadata": "//source/extensions/filters/http/set_metadata:config", - "envoy.filters.http.tap": "//source/extensions/filters/http/tap:config", - "envoy.filters.http.wasm": "//source/extensions/filters/http/wasm:config", - "envoy.filters.http.stateful_session": "//source/extensions/filters/http/stateful_session:config", + "envoy.filters.http.kill_request": "//source/extensions/filters/http/kill_request:kill_request_config", + "envoy.filters.http.local_ratelimit": "//source/extensions/filters/http/local_ratelimit:config", + "envoy.filters.http.lua": "//source/extensions/filters/http/lua:config", + "envoy.filters.http.oauth2": "//source/extensions/filters/http/oauth2:config", + "envoy.filters.http.on_demand": "//source/extensions/filters/http/on_demand:config", + "envoy.filters.http.original_src": "//source/extensions/filters/http/original_src:config", + "envoy.filters.http.ratelimit": "//source/extensions/filters/http/ratelimit:config", + "envoy.filters.http.rbac": "//source/extensions/filters/http/rbac:config", + "envoy.filters.http.router": "//source/extensions/filters/http/router:config", + "envoy.filters.http.set_metadata": "//source/extensions/filters/http/set_metadata:config", + "envoy.filters.http.tap": "//source/extensions/filters/http/tap:config", + "envoy.filters.http.wasm": "//source/extensions/filters/http/wasm:config", + "envoy.filters.http.stateful_session": "//source/extensions/filters/http/stateful_session:config", # # Listener filters # - - "envoy.filters.listener.http_inspector": "//source/extensions/filters/listener/http_inspector:config", + "envoy.filters.listener.http_inspector": "//source/extensions/filters/listener/http_inspector:config", # NOTE: The original_dst filter is implicitly loaded if original_dst functionality is # configured on the listener. Do not remove it in that case or configs will fail to load. - "envoy.filters.listener.original_dst": "//source/extensions/filters/listener/original_dst:config", - "envoy.filters.listener.original_src": "//source/extensions/filters/listener/original_src:config", + "envoy.filters.listener.original_dst": "//source/extensions/filters/listener/original_dst:config", + "envoy.filters.listener.original_src": "//source/extensions/filters/listener/original_src:config", # NOTE: The proxy_protocol filter is implicitly loaded if proxy_protocol functionality is # configured on the listener. Do not remove it in that case or configs will fail to load. - "envoy.filters.listener.proxy_protocol": "//source/extensions/filters/listener/proxy_protocol:config", - "envoy.filters.listener.tls_inspector": "//source/extensions/filters/listener/tls_inspector:config", + "envoy.filters.listener.proxy_protocol": "//source/extensions/filters/listener/proxy_protocol:config", + "envoy.filters.listener.tls_inspector": "//source/extensions/filters/listener/tls_inspector:config", # # Network filters # - - "envoy.filters.network.connection_limit": "//source/extensions/filters/network/connection_limit:config", - "envoy.filters.network.direct_response": "//source/extensions/filters/network/direct_response:config", - "envoy.filters.network.dubbo_proxy": "//source/extensions/filters/network/dubbo_proxy:config", - "envoy.filters.network.echo": "//source/extensions/filters/network/echo:config", - "envoy.filters.network.ext_authz": "//source/extensions/filters/network/ext_authz:config", - "envoy.filters.network.http_connection_manager": "//source/extensions/filters/network/http_connection_manager:config", - "envoy.filters.network.local_ratelimit": "//source/extensions/filters/network/local_ratelimit:config", - "envoy.filters.network.mongo_proxy": "//source/extensions/filters/network/mongo_proxy:config", - "envoy.filters.network.ratelimit": "//source/extensions/filters/network/ratelimit:config", - "envoy.filters.network.rbac": "//source/extensions/filters/network/rbac:config", - "envoy.filters.network.redis_proxy": "//source/extensions/filters/network/redis_proxy:config", - "envoy.filters.network.tcp_proxy": "//source/extensions/filters/network/tcp_proxy:config", - "envoy.filters.network.thrift_proxy": "//source/extensions/filters/network/thrift_proxy:config", - "envoy.filters.network.sni_cluster": "//source/extensions/filters/network/sni_cluster:config", - "envoy.filters.network.sni_dynamic_forward_proxy": "//source/extensions/filters/network/sni_dynamic_forward_proxy:config", - "envoy.filters.network.wasm": "//source/extensions/filters/network/wasm:config", - "envoy.filters.network.zookeeper_proxy": "//source/extensions/filters/network/zookeeper_proxy:config", + "envoy.filters.network.connection_limit": "//source/extensions/filters/network/connection_limit:config", + "envoy.filters.network.direct_response": "//source/extensions/filters/network/direct_response:config", + "envoy.filters.network.dubbo_proxy": "//source/extensions/filters/network/dubbo_proxy:config", + "envoy.filters.network.echo": "//source/extensions/filters/network/echo:config", + "envoy.filters.network.ext_authz": "//source/extensions/filters/network/ext_authz:config", + "envoy.filters.network.http_connection_manager": "//source/extensions/filters/network/http_connection_manager:config", + "envoy.filters.network.local_ratelimit": "//source/extensions/filters/network/local_ratelimit:config", + "envoy.filters.network.mongo_proxy": "//source/extensions/filters/network/mongo_proxy:config", + "envoy.filters.network.ratelimit": "//source/extensions/filters/network/ratelimit:config", + "envoy.filters.network.rbac": "//source/extensions/filters/network/rbac:config", + "envoy.filters.network.redis_proxy": "//source/extensions/filters/network/redis_proxy:config", + "envoy.filters.network.tcp_proxy": "//source/extensions/filters/network/tcp_proxy:config", + "envoy.filters.network.thrift_proxy": "//source/extensions/filters/network/thrift_proxy:config", + "envoy.filters.network.sni_cluster": "//source/extensions/filters/network/sni_cluster:config", + "envoy.filters.network.sni_dynamic_forward_proxy": "//source/extensions/filters/network/sni_dynamic_forward_proxy:config", + "envoy.filters.network.wasm": "//source/extensions/filters/network/wasm:config", + "envoy.filters.network.zookeeper_proxy": "//source/extensions/filters/network/zookeeper_proxy:config", # # UDP filters # - - "envoy.filters.udp_listener.dns_filter": "//source/extensions/filters/udp/dns_filter:config", - "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", + "envoy.filters.udp_listener.dns_filter": "//source/extensions/filters/udp/dns_filter:config", + "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", # # Resource monitors # - - "envoy.resource_monitors.fixed_heap": "//source/extensions/resource_monitors/fixed_heap:config", - "envoy.resource_monitors.injected_resource": "//source/extensions/resource_monitors/injected_resource:config", + "envoy.resource_monitors.fixed_heap": "//source/extensions/resource_monitors/fixed_heap:config", + "envoy.resource_monitors.injected_resource": "//source/extensions/resource_monitors/injected_resource:config", # # Stat sinks # - - "envoy.stat_sinks.dog_statsd": "//source/extensions/stat_sinks/dog_statsd:config", - "envoy.stat_sinks.graphite_statsd": "//source/extensions/stat_sinks/graphite_statsd:config", - "envoy.stat_sinks.hystrix": "//source/extensions/stat_sinks/hystrix:config", - "envoy.stat_sinks.metrics_service": "//source/extensions/stat_sinks/metrics_service:config", - "envoy.stat_sinks.statsd": "//source/extensions/stat_sinks/statsd:config", - "envoy.stat_sinks.wasm": "//source/extensions/stat_sinks/wasm:config", + "envoy.stat_sinks.dog_statsd": "//source/extensions/stat_sinks/dog_statsd:config", + "envoy.stat_sinks.graphite_statsd": "//source/extensions/stat_sinks/graphite_statsd:config", + "envoy.stat_sinks.hystrix": "//source/extensions/stat_sinks/hystrix:config", + "envoy.stat_sinks.metrics_service": "//source/extensions/stat_sinks/metrics_service:config", + "envoy.stat_sinks.statsd": "//source/extensions/stat_sinks/statsd:config", + "envoy.stat_sinks.wasm": "//source/extensions/stat_sinks/wasm:config", # # Thrift filters # - - "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", - "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", - "envoy.filters.thrift.ratelimit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", + "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", + "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", + "envoy.filters.thrift.ratelimit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", # # Tracers # - - "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", - "envoy.tracers.lightstep": "//source/extensions/tracers/lightstep:config", - "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", - "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", - "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", - "envoy.tracers.xray": "//source/extensions/tracers/xray:config", - "envoy.tracers.skywalking": "//source/extensions/tracers/skywalking:config", - "envoy.tracers.opentelemetry": "//source/extensions/tracers/opentelemetry:config", + "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", + "envoy.tracers.lightstep": "//source/extensions/tracers/lightstep:config", + "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", + "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", + "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", + "envoy.tracers.xray": "//source/extensions/tracers/xray:config", + "envoy.tracers.skywalking": "//source/extensions/tracers/skywalking:config", + "envoy.tracers.opentelemetry": "//source/extensions/tracers/opentelemetry:config", # # Transport sockets # - - "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", - "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", - "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", - "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", - "envoy.transport_sockets.starttls": "//source/extensions/transport_sockets/starttls:config", - "envoy.transport_sockets.tcp_stats": "//source/extensions/transport_sockets/tcp_stats:config", - "envoy.transport_sockets.internal_upstream": "//source/extensions/transport_sockets/internal_upstream:config", + "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", + "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", + "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", + "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", + "envoy.transport_sockets.starttls": "//source/extensions/transport_sockets/starttls:config", + "envoy.transport_sockets.tcp_stats": "//source/extensions/transport_sockets/tcp_stats:config", + "envoy.transport_sockets.internal_upstream": "//source/extensions/transport_sockets/internal_upstream:config", # # Retry host predicates # - - "envoy.retry_host_predicates.previous_hosts": "//source/extensions/retry/host/previous_hosts:config", - "envoy.retry_host_predicates.omit_canary_hosts": "//source/extensions/retry/host/omit_canary_hosts:config", - "envoy.retry_host_predicates.omit_host_metadata": "//source/extensions/retry/host/omit_host_metadata:config", + "envoy.retry_host_predicates.previous_hosts": "//source/extensions/retry/host/previous_hosts:config", + "envoy.retry_host_predicates.omit_canary_hosts": "//source/extensions/retry/host/omit_canary_hosts:config", + "envoy.retry_host_predicates.omit_host_metadata": "//source/extensions/retry/host/omit_host_metadata:config", # # Retry priorities # - - "envoy.retry_priorities.previous_priorities": "//source/extensions/retry/priority/previous_priorities:config", + "envoy.retry_priorities.previous_priorities": "//source/extensions/retry/priority/previous_priorities:config", # # CacheFilter plugins # - "envoy.extensions.http.cache.simple": "//source/extensions/filters/http/cache/simple_http_cache:config", + "envoy.extensions.http.cache.simple": "//source/extensions/filters/http/cache/simple_http_cache:config", # # Internal redirect predicates # - "envoy.internal_redirect_predicates.allow_listed_routes": "//source/extensions/internal_redirect/allow_listed_routes:config", - "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", - "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", + "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", + "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", # # Http Upstreams (excepting envoy.upstreams.http.generic which is hard-coded into the build so not registered here) # - - "envoy.upstreams.http.http": "//source/extensions/upstreams/http/http:config", - "envoy.upstreams.http.tcp": "//source/extensions/upstreams/http/tcp:config", + "envoy.upstreams.http.http": "//source/extensions/upstreams/http/http:config", + "envoy.upstreams.http.tcp": "//source/extensions/upstreams/http/tcp:config", # # Watchdog actions # - - "envoy.watchdog.profile_action": "//source/extensions/watchdog/profile_action:config", + "envoy.watchdog.profile_action": "//source/extensions/watchdog/profile_action:config", # # WebAssembly runtimes # - - "envoy.wasm.runtime.null": "//source/extensions/wasm_runtime/null:config", - "envoy.wasm.runtime.v8": "//source/extensions/wasm_runtime/v8:config", - "envoy.wasm.runtime.wamr": "//source/extensions/wasm_runtime/wamr:config", - "envoy.wasm.runtime.wavm": "//source/extensions/wasm_runtime/wavm:config", - "envoy.wasm.runtime.wasmtime": "//source/extensions/wasm_runtime/wasmtime:config", + "envoy.wasm.runtime.null": "//source/extensions/wasm_runtime/null:config", + "envoy.wasm.runtime.v8": "//source/extensions/wasm_runtime/v8:config", + "envoy.wasm.runtime.wamr": "//source/extensions/wasm_runtime/wamr:config", + "envoy.wasm.runtime.wavm": "//source/extensions/wasm_runtime/wavm:config", + "envoy.wasm.runtime.wasmtime": "//source/extensions/wasm_runtime/wasmtime:config", # # Rate limit descriptors # - - "envoy.rate_limit_descriptors.expr": "//source/extensions/rate_limit_descriptors/expr:config", + "envoy.rate_limit_descriptors.expr": "//source/extensions/rate_limit_descriptors/expr:config", # # IO socket # - - "envoy.io_socket.user_space": "//source/extensions/io_socket/user_space:config", - "envoy.bootstrap.internal_listener": "//source/extensions/bootstrap/internal_listener:config", + "envoy.io_socket.user_space": "//source/extensions/io_socket/user_space:config", + "envoy.bootstrap.internal_listener": "//source/extensions/bootstrap/internal_listener:config", # # TLS peer certification validators # - - "envoy.tls.cert_validator.spiffe": "//source/extensions/transport_sockets/tls/cert_validator/spiffe:config", + "envoy.tls.cert_validator.spiffe": "//source/extensions/transport_sockets/tls/cert_validator/spiffe:config", # # HTTP header formatters # - - "envoy.http.stateful_header_formatters.preserve_case": "//source/extensions/http/header_formatters/preserve_case:config", + "envoy.http.stateful_header_formatters.preserve_case": "//source/extensions/http/header_formatters/preserve_case:config", # # Original IP detection # - - "envoy.http.original_ip_detection.custom_header": "//source/extensions/http/original_ip_detection/custom_header:config", - "envoy.http.original_ip_detection.xff": "//source/extensions/http/original_ip_detection/xff:config", + "envoy.http.original_ip_detection.custom_header": "//source/extensions/http/original_ip_detection/custom_header:config", + "envoy.http.original_ip_detection.xff": "//source/extensions/http/original_ip_detection/xff:config", # # Stateful session # - - "envoy.http.stateful_session.cookie": "//source/extensions/http/stateful_session/cookie:config", + "envoy.http.stateful_session.cookie": "//source/extensions/http/stateful_session/cookie:config", # # QUIC extensions # - - "envoy.quic.crypto_stream.server.quiche": "//source/extensions/quic/crypto_stream:envoy_quic_default_crypto_server_stream", - "envoy.quic.proof_source.filter_chain": "//source/extensions/quic/proof_source:envoy_quic_default_proof_source", + "envoy.quic.crypto_stream.server.quiche": "//source/extensions/quic/crypto_stream:envoy_quic_default_crypto_server_stream", + "envoy.quic.proof_source.filter_chain": "//source/extensions/quic/proof_source:envoy_quic_default_proof_source", # # UDP packet writers # - "envoy.udp_packet_writer.default": "//source/extensions/udp_packet_writer/default:config", - "envoy.udp_packet_writer.gso": "//source/extensions/udp_packet_writer/gso:config", + "envoy.udp_packet_writer.default": "//source/extensions/udp_packet_writer/default:config", + "envoy.udp_packet_writer.gso": "//source/extensions/udp_packet_writer/gso:config", # # Formatter # - - "envoy.formatter.metadata": "//source/extensions/formatter/metadata:config", - "envoy.formatter.req_without_query": "//source/extensions/formatter/req_without_query:config", + "envoy.formatter.metadata": "//source/extensions/formatter/metadata:config", + "envoy.formatter.req_without_query": "//source/extensions/formatter/req_without_query:config", # # Key value store # - - "envoy.key_value.file_based": "//source/extensions/key_value/file_based:config_lib", + "envoy.key_value.file_based": "//source/extensions/key_value/file_based:config_lib", # # RBAC matchers # - - "envoy.rbac.matchers.upstream_ip_port": "//source/extensions/filters/common/rbac/matchers:upstream_ip_port_lib", + "envoy.rbac.matchers.upstream_ip_port": "//source/extensions/filters/common/rbac/matchers:upstream_ip_port_lib", # # DNS Resolver # # c-ares DNS resolver extension is recommended to be enabled to maintain the legacy DNS resolving behavior. - "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", + "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", # apple DNS resolver extension is only needed in MacOS build plus one want to use apple library for DNS resolving. - "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", + "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", # # Custom matchers # - - "envoy.matching.custom_matchers.trie_matcher": "//source/extensions/common/matcher:trie_matcher_lib", + "envoy.matching.custom_matchers.trie_matcher": "//source/extensions/common/matcher:trie_matcher_lib", # # Header Validators # - - "envoy.http.header_validators.envoy_default": "//source/extensions/http/header_validators/envoy_default:config", + "envoy.http.header_validators.envoy_default": "//source/extensions/http/header_validators/envoy_default:config", # # Early Data option # - - "envoy.route.early_data_policy.default": "//source/extensions/early_data:default_early_data_policy_lib", + "envoy.route.early_data_policy.default": "//source/extensions/early_data:default_early_data_policy_lib", } # These can be changed to ["//visibility:public"], for downstream builds which diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index 9c24adf503959..d6598562efb83 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -734,6 +734,11 @@ envoy.matching.input_matchers.ip: - envoy.matching.input_matchers security_posture: robust_to_untrusted_downstream_and_upstream status: stable +envoy.url_template: + categories: + - envoy.url_template + security_posture: robust_to_untrusted_downstream_and_upstream + status: stable envoy.quic.proof_source.filter_chain: categories: - envoy.quic.proof_source diff --git a/source/common/common/matching/BUILD b/source/extensions/url_template/BUILD similarity index 87% rename from source/common/common/matching/BUILD rename to source/extensions/url_template/BUILD index 0776abebe83fa..3fda496428c2a 100644 --- a/source/common/common/matching/BUILD +++ b/source/extensions/url_template/BUILD @@ -1,23 +1,24 @@ load( "//bazel:envoy_build_system.bzl", + "envoy_cc_extension", "envoy_cc_library", "envoy_cc_test", - "envoy_package", + "envoy_extension_package", ) licenses(["notice"]) # Apache 2 # Wildcard & Pattern Matching -envoy_package() +envoy_extension_package() envoy_cc_library( name = "url_template_matching", srcs = ["url_template_matching.cc"], hdrs = ["url_template_matching.h"], visibility = [ - "//source/common/common:__subpackages__", - "//test/common/common:__subpackages__", + "//source/extensions/url_template:__subpackages__", + "//test/extensions/url_template:__subpackages__", ], deps = [ ":url_template_matching_internal_cc", @@ -25,7 +26,7 @@ envoy_cc_library( "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_googlesource_code_re2//:re2", - "@envoy_api//envoy/config/route/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/url_template/v3:pkg_cc_proto", ], ) diff --git a/source/common/common/matching/url_template_matching.cc b/source/extensions/url_template/url_template_matching.cc similarity index 90% rename from source/common/common/matching/url_template_matching.cc rename to source/extensions/url_template/url_template_matching.cc index b85e5406a5d23..ad78d7cc988c6 100644 --- a/source/common/common/matching/url_template_matching.cc +++ b/source/extensions/url_template/url_template_matching.cc @@ -1,13 +1,13 @@ -#include "source/common/common/matching/url_template_matching.h" +#include "source/extensions/url_template/url_template_matching.h" #include #include #include #include -#include "envoy/config/route/v3/route_components.pb.h" +#include "envoy/extensions/url_template/v3/route_url_rewrite_pattern.pb.h" -#include "source/common/common/matching/url_template_matching_internal.h" +#include "source/extensions/url_template/url_template_matching_internal.h" #include "absl/status/statusor.h" #include "absl/strings/str_split.h" @@ -86,9 +86,9 @@ parseRewritePatternHelper(absl::string_view pattern) { return result; } -absl::StatusOr +absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { - envoy::config::route::v3::RouteUrlRewritePattern parsed_pattern; + envoy::extensions::url_template::v3::RouteUrlRewritePattern parsed_pattern; RE2 regex = RE2(ToStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); @@ -122,7 +122,7 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) absl::StatusOr RewriteURLTemplatePattern(absl::string_view url, absl::string_view capture_regex, - const envoy::config::route::v3::RouteUrlRewritePattern& rewrite_pattern) { + const envoy::extensions::url_template::v3::RouteUrlRewritePattern& rewrite_pattern) { RE2 regex = RE2(ToStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); @@ -137,7 +137,7 @@ RewriteURLTemplatePattern(absl::string_view url, absl::string_view capture_regex std::string rewritten_url; - for (const envoy::config::route::v3::RouteUrlRewritePattern::RewriteSegment& segment : + for (const envoy::extensions::url_template::v3::RouteUrlRewritePattern::RewriteSegment& segment : rewrite_pattern.segments()) { if (segment.has_literal()) { absl::StrAppend(&rewritten_url, segment.literal()); diff --git a/source/common/common/matching/url_template_matching.h b/source/extensions/url_template/url_template_matching.h similarity index 80% rename from source/common/common/matching/url_template_matching.h rename to source/extensions/url_template/url_template_matching.h index 2c0f49135be41..2f5c78275af68 100644 --- a/source/common/common/matching/url_template_matching.h +++ b/source/extensions/url_template/url_template_matching.h @@ -1,11 +1,11 @@ -#ifndef SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_H -#define SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_H +#ifndef SOURCE_EXTENSIONS_URL_TEMPLATE_URL_TEMPLATE_MATCHING_H +#define SOURCE_EXTENSIONS_URL_TEMPLATE_URL_TEMPLATE_MATCHING_H #include -#include "envoy/config/route/v3/route_components.pb.h" +#include "envoy/extensions/url_template/v3/route_url_rewrite_pattern.pb.h" -#include "source/common/common/matching/url_template_matching_internal.h" +#include "source/extensions/url_template/url_template_matching_internal.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" @@ -39,14 +39,14 @@ parseRewritePatternHelper(absl::string_view pattern); // Returns the parsed Url rewrite pattern to be used by // RewriteURLTemplatePattern() |capture_regex| should // be the regex generated by ConvertURLPatternSyntaxToRegex(). -absl::StatusOr +absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); // Returns the rewritten URL path based on the given parsed rewrite pattern. // Used for template-based URL rewrite. absl::StatusOr RewriteURLTemplatePattern(absl::string_view url, absl::string_view capture_regex, - const envoy::config::route::v3::RouteUrlRewritePattern& rewrite_pattern); + const envoy::extensions::url_template::v3::RouteUrlRewritePattern& rewrite_pattern); // Returns if provided template match pattern is valid bool isValidPathTemplateMatchPattern(const std::string& path_template_match); @@ -61,4 +61,4 @@ bool isValidSharedVariableSet(const std::string& path_template_rewrite, } // namespace matching } // namespace Envoy -#endif // SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_H +#endif // SOURCE_EXTENSIONS_URL_TEMPLATE_URL_TEMPLATE_MATCHING_H diff --git a/source/common/common/matching/url_template_matching_internal.cc b/source/extensions/url_template/url_template_matching_internal.cc similarity index 99% rename from source/common/common/matching/url_template_matching_internal.cc rename to source/extensions/url_template/url_template_matching_internal.cc index 72d0e833678ff..62b4999071da3 100644 --- a/source/common/common/matching/url_template_matching_internal.cc +++ b/source/extensions/url_template/url_template_matching_internal.cc @@ -1,4 +1,4 @@ -#include "source/common/common/matching/url_template_matching_internal.h" +#include "source/extensions/url_template/url_template_matching_internal.h" #include #include diff --git a/source/common/common/matching/url_template_matching_internal.h b/source/extensions/url_template/url_template_matching_internal.h similarity index 79% rename from source/common/common/matching/url_template_matching_internal.h rename to source/extensions/url_template/url_template_matching_internal.h index 97816cf0ddd2c..af8476a4a8db7 100644 --- a/source/common/common/matching/url_template_matching_internal.h +++ b/source/extensions/url_template/url_template_matching_internal.h @@ -1,5 +1,5 @@ -#ifndef SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H -#define SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H +#ifndef SOURCE_EXTENSIONS_URL_TEMPLATE_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H +#define SOURCE_EXTENSIONS_URL_TEMPLATE_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H #include #include @@ -21,6 +21,17 @@ namespace url_template_matching_internal { using Literal = absl::string_view; enum class Operator { kPathGlob, kTextGlob }; +struct RewriteSegment { + // Represents a segment of the rewritten URL, including any path segments, + // slash and prefix. + absl::string_view literal; + + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int var_index; +}; + struct Variable { absl::string_view var_name; std::vector> var_match; @@ -81,4 +92,4 @@ inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.dat } // namespace matching } // namespace Envoy -#endif // SOURCE_COMMON_COMMON_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H +#endif // SOURCE_EXTENSIONS_URL_TEMPLATE_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H diff --git a/source/common/common/matching/url_template_matching_internal_test.cc b/source/extensions/url_template/url_template_matching_internal_test.cc similarity index 99% rename from source/common/common/matching/url_template_matching_internal_test.cc rename to source/extensions/url_template/url_template_matching_internal_test.cc index 3a70998a910a5..496088fff22ab 100644 --- a/source/common/common/matching/url_template_matching_internal_test.cc +++ b/source/extensions/url_template/url_template_matching_internal_test.cc @@ -6,7 +6,7 @@ #include #include "source/common/common/assert.h" -#include "source/common/common/matching/url_template_matching_internal.h" +#include "source/extensions/url_template/url_template_matching_internal.h" #include "source/common/protobuf/protobuf.h" #include "test/test_common/logging.h" diff --git a/source/common/common/matching/url_template_matching_test.cc b/source/extensions/url_template/url_template_matching_test.cc similarity index 93% rename from source/common/common/matching/url_template_matching_test.cc rename to source/extensions/url_template/url_template_matching_test.cc index 9b6c2828c853d..e90be3d0cb705 100644 --- a/source/common/common/matching/url_template_matching_test.cc +++ b/source/extensions/url_template/url_template_matching_test.cc @@ -3,8 +3,8 @@ #include #include "source/common/common/assert.h" -#include "source/common/common/matching/url_template_matching.h" -#include "source/common/common/matching/url_template_matching_internal.h" +#include "source/extensions/url_template/url_template_matching.h" +#include "source/extensions/url_template/url_template_matching_internal.h" #include "source/common/protobuf/protobuf.h" #include "test/test_common/logging.h" @@ -84,8 +84,8 @@ TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { class ParseRewriteSuccess : public testing::TestWithParam> { protected: const std::string& rewrite_pattern() const { return std::get<0>(GetParam()); } - envoy::config::route::v3::RouteUrlRewritePattern expected_proto() const { - envoy::config::route::v3::RouteUrlRewritePattern expected_proto; + envoy::extensions::url_template::v3::RouteUrlRewritePattern expected_proto() const { + envoy::extensions::url_template::v3::RouteUrlRewritePattern expected_proto; Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); return expected_proto; } @@ -145,7 +145,7 @@ INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, }))); TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { - absl::StatusOr rewrite = + absl::StatusOr rewrite = parseRewritePattern(rewrite_pattern(), kCaptureRegex); ASSERT_OK(rewrite); // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); @@ -168,8 +168,8 @@ TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { class RewriteUrlTemplateSuccess : public testing::TestWithParam> { protected: - envoy::config::route::v3::RouteUrlRewritePattern rewrite_proto() const { - envoy::config::route::v3::RouteUrlRewritePattern proto; + envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_proto() const { + envoy::extensions::url_template::v3::RouteUrlRewritePattern proto; Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); return proto; } @@ -233,7 +233,7 @@ TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { } TEST(RewriteUrlTemplateFailure, BadRegex) { - envoy::config::route::v3::RouteUrlRewritePattern rewrite_proto; + envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_proto; const std::string yaml = R"EOF( segments: @@ -248,7 +248,7 @@ TEST(RewriteUrlTemplateFailure, BadRegex) { } TEST(RewriteUrlTemplateFailure, RegexNoMatch) { - envoy::config::route::v3::RouteUrlRewritePattern rewrite_proto; + envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_proto; const std::string yaml = R"EOF( segments: @@ -263,7 +263,7 @@ TEST(RewriteUrlTemplateFailure, RegexNoMatch) { } TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { - envoy::config::route::v3::RouteUrlRewritePattern rewrite_proto; + envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_proto; const std::string yaml = R"EOF( segments: @@ -277,7 +277,7 @@ TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { } TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { - envoy::config::route::v3::RouteUrlRewritePattern rewrite_proto; + envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_proto; const std::string yaml = R"EOF( segments: @@ -318,7 +318,7 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { absl::StatusOr regex = convertURLPatternSyntaxToRegex(url_pattern()); ASSERT_OK(regex); - absl::StatusOr rewrite_proto = + absl::StatusOr rewrite_proto = parseRewritePattern(rewrite_pattern(), regex.value()); ASSERT_OK(rewrite_proto); From 729f71b86137d403e6f338a1aae4962ad1ec6846 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 7 Jul 2022 18:54:16 +0000 Subject: [PATCH 010/238] Testing route matching Signed-off-by: silverstar195 --- source/extensions/url_template/BUILD | 15 +++ source/extensions/url_template/config.cc | 15 +++ source/extensions/url_template/config.h | 29 ++++++ .../url_template/url_template_matching.cc | 53 ++++++++--- .../url_template/url_template_matching.h | 93 ++++++++++++------- test/extensions/url_template/BUILD | 33 +++++++ test/extensions/url_template/config_test.cc | 42 +++++++++ test/extensions/url_template/matcher_test.cc | 34 +++++++ 8 files changed, 266 insertions(+), 48 deletions(-) create mode 100644 source/extensions/url_template/config.cc create mode 100644 source/extensions/url_template/config.h create mode 100644 test/extensions/url_template/BUILD create mode 100644 test/extensions/url_template/config_test.cc create mode 100644 test/extensions/url_template/matcher_test.cc diff --git a/source/extensions/url_template/BUILD b/source/extensions/url_template/BUILD index 3fda496428c2a..58afef3dc35a7 100644 --- a/source/extensions/url_template/BUILD +++ b/source/extensions/url_template/BUILD @@ -17,11 +17,14 @@ envoy_cc_library( srcs = ["url_template_matching.cc"], hdrs = ["url_template_matching.h"], visibility = [ + "//source/common/router:__subpackages__", "//source/extensions/url_template:__subpackages__", "//test/extensions/url_template:__subpackages__", ], deps = [ ":url_template_matching_internal_cc", + "//envoy/router:router_url_template_interface", + "//source/common/http:path_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", @@ -71,3 +74,15 @@ envoy_cc_test( "@com_googlesource_code_re2//:re2", ], ) + +envoy_cc_extension( + name = "config", + srcs = ["config.cc"], + hdrs = ["config.h"], + deps = [ + ":url_template_matching", + "//envoy/registry", + "//envoy/router:router_url_template_interface", + "@envoy_api//envoy/extensions/url_template/v3:pkg_cc_proto", + ], +) diff --git a/source/extensions/url_template/config.cc b/source/extensions/url_template/config.cc new file mode 100644 index 0000000000000..b048bde658d65 --- /dev/null +++ b/source/extensions/url_template/config.cc @@ -0,0 +1,15 @@ +#include "source/extensions/url_template/config.h" + +#include "envoy/registry/registry.h" +#include "envoy/router/url_template.h" +// TODO change includes + +namespace Envoy { +namespace Extensions { +namespace UrlTemplate { + +REGISTER_FACTORY(UrlTemplatePredicateFactory, Router::UrlTemplatePredicateFactory); + +} // namespace UrlTemplate +} // namespace Extensions +} // namespace Envoy \ No newline at end of file diff --git a/source/extensions/url_template/config.h b/source/extensions/url_template/config.h new file mode 100644 index 0000000000000..c1e075bffe0b4 --- /dev/null +++ b/source/extensions/url_template/config.h @@ -0,0 +1,29 @@ +#pragma once + +#include "envoy/extensions/url_template/v3/route_url_rewrite_pattern.pb.h" +#include "envoy/router/url_template.h" + +#include "source/extensions/url_template/url_template_matching.h" + +namespace Envoy { +namespace Extensions { +namespace UrlTemplate { + +class UrlTemplatePredicateFactory : public Router::UrlTemplatePredicateFactory { +public: + Router::UrlTemplatePredicateSharedPtr + createUrlTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) override { + return std::make_shared(url_pattern, url_rewrite_pattern); + } + + std::string name() const override { return "envoy.url_template_predicates"; } + + ProtobufTypes::MessagePtr createEmptyConfigProto() override { + // may not be used to investigate + return std::make_unique(); + } +}; + +} // namespace UrlTemplate +} // namespace Extensions +} // namespace Envoy \ No newline at end of file diff --git a/source/extensions/url_template/url_template_matching.cc b/source/extensions/url_template/url_template_matching.cc index ad78d7cc988c6..d36cb56d3747e 100644 --- a/source/extensions/url_template/url_template_matching.cc +++ b/source/extensions/url_template/url_template_matching.cc @@ -8,6 +8,7 @@ #include "envoy/extensions/url_template/v3/route_url_rewrite_pattern.pb.h" #include "source/extensions/url_template/url_template_matching_internal.h" +#include "source/common/http/path_utility.h" #include "absl/status/statusor.h" #include "absl/strings/str_split.h" @@ -26,12 +27,12 @@ using matching::url_template_matching_internal::ParsedUrlPattern; inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.data(), text.size()}; } -bool isPatternMatch(absl::string_view pattern, absl::string_view capture_regex) { - RE2 regex = RE2(ToStringPiece(capture_regex)); - return RE2::FullMatch(ToStringPiece(pattern), regex); +bool UrlTemplatePredicate::match(absl::string_view pattern) const { + return RE2::FullMatch(ToStringPiece(Http::PathUtil::removeQueryAndFragment(pattern)), matching_pattern_regex_); } -absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { +absl::StatusOr +UrlTemplatePredicate::convertURLPatternSyntaxToRegex(absl::string_view url_pattern) const { absl::StatusOr status = url_template_matching_internal::parseURLPatternSyntax(url_pattern); @@ -43,7 +44,7 @@ absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url } absl::StatusOr> -parseRewritePatternHelper(absl::string_view pattern) { +UrlTemplatePredicate::parseRewritePatternHelper(absl::string_view pattern) const { std::vector result; // Don't allow contiguous '/' patterns. @@ -87,7 +88,8 @@ parseRewritePatternHelper(absl::string_view pattern) { } absl::StatusOr -parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { +UrlTemplatePredicate::parseRewritePattern(absl::string_view pattern, + absl::string_view capture_regex) const { envoy::extensions::url_template::v3::RouteUrlRewritePattern parsed_pattern; RE2 regex = RE2(ToStringPiece(capture_regex)); if (!regex.ok()) { @@ -120,9 +122,9 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) return parsed_pattern; } -absl::StatusOr -RewriteURLTemplatePattern(absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::url_template::v3::RouteUrlRewritePattern& rewrite_pattern) { +absl::StatusOr UrlTemplatePredicate::rewriteURLTemplatePattern( + absl::string_view url, absl::string_view capture_regex, + const envoy::extensions::url_template::v3::RouteUrlRewritePattern& rewrite_pattern) const { RE2 regex = RE2(ToStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); @@ -152,18 +154,45 @@ RewriteURLTemplatePattern(absl::string_view url, absl::string_view capture_regex return rewritten_url; } -bool IsValidPathTemplateMatchPattern(const std::string& path_template_match) { +bool UrlTemplatePredicate::isValidPathTemplateMatchPattern(const std::string& path_template_match) { return convertURLPatternSyntaxToRegex(path_template_match).ok(); } -bool isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { +bool UrlTemplatePredicate::isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { return parseRewritePatternHelper(path_template_rewrite).ok(); } -bool isValidSharedVariableSet(const std::string& path_template_rewrite, +bool UrlTemplatePredicate::isValidSharedVariableSet(const std::string& path_template_rewrite, absl::string_view capture_regex) { return parseRewritePattern(path_template_rewrite, capture_regex).ok(); } +absl::StatusOr UrlTemplatePredicate::rewritePattern(absl::string_view current_pattern, absl::string_view matched_path) const { + absl::StatusOr regex_pattern = convertURLPatternSyntaxToRegex(matched_path); + if (!regex_pattern.ok()) { + return absl::InvalidArgumentError("Unable to parse url pattern regex"); + } + std::string regex_pattern_str = *std::move(regex_pattern); + + absl::StatusOr rewrite_pattern = + parseRewritePattern(url_rewrite_pattern_, regex_pattern_str); + + if (!rewrite_pattern.ok()) { + return absl::InvalidArgumentError("Unable to parse url rewrite pattern"); + } + + envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_pattern_proto = + *std::move(rewrite_pattern); + + absl::StatusOr new_path = + rewriteURLTemplatePattern(current_pattern, regex_pattern_str, rewrite_pattern_proto); + + if (!new_path.ok()) { + return absl::InvalidArgumentError("Unable rewrite url to new URL"); + } + + return *std::move(new_path); +} + } // namespace matching } // namespace Envoy diff --git a/source/extensions/url_template/url_template_matching.h b/source/extensions/url_template/url_template_matching.h index 2f5c78275af68..505e92a077956 100644 --- a/source/extensions/url_template/url_template_matching.h +++ b/source/extensions/url_template/url_template_matching.h @@ -9,6 +9,7 @@ #include "absl/status/statusor.h" #include "absl/strings/string_view.h" +#include "envoy/router/url_template.h" namespace Envoy { namespace matching { @@ -21,42 +22,62 @@ struct RewritePatternSegment { RewriteStringKind kind; }; -// Returns if the regex pattern matches the given url_pattern. -bool isPatternMatch(absl::string_view pattern, absl::string_view capture_regex); - -// Returns the regex pattern that is equivalent to the given url_pattern. -// Used in the config pipeline to translate user given url pattern to -// the safe regex Envoy can understand. Strips away any variable captures. -absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); - -// Helper function that parses the pattern and breaks it down to either -// literals or variable names. To be used by ParseRewritePattern(). -// Exposed here so that the validator for the rewrite pattern can also -// use it. -absl::StatusOr> -parseRewritePatternHelper(absl::string_view pattern); - -// Returns the parsed Url rewrite pattern to be used by -// RewriteURLTemplatePattern() |capture_regex| should -// be the regex generated by ConvertURLPatternSyntaxToRegex(). -absl::StatusOr -parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); - -// Returns the rewritten URL path based on the given parsed rewrite pattern. -// Used for template-based URL rewrite. -absl::StatusOr -RewriteURLTemplatePattern(absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::url_template::v3::RouteUrlRewritePattern& rewrite_pattern); - -// Returns if provided template match pattern is valid -bool isValidPathTemplateMatchPattern(const std::string& path_template_match); - -// Returns if provided rewrite pattern is valid -bool isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); - -// Returns if path_template and rewrite_template have valid variables -bool isValidSharedVariableSet(const std::string& path_template_rewrite, - absl::string_view capture_regex); +class UrlTemplatePredicate : public Router::UrlTemplatePredicate { +public: + explicit UrlTemplatePredicate(absl::string_view url_pattern, absl::string_view url_rewrite_pattern) + : url_pattern_(url_pattern), url_rewrite_pattern_(url_rewrite_pattern), + matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())) {} + + absl::string_view name() const override { return "envoy.url_template"; } + + // Returns if the regex pattern matches the given regex from constructor. + bool match(absl::string_view pattern) const; + + // Returns the regex pattern that is equivalent to the given url_pattern. + // Used in the config pipeline to translate user given url pattern to + // the safe regex Envoy can understand. Strips away any variable captures. + absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) const; + + // Helper function that parses the pattern and breaks it down to either + // literals or variable names. To be used by ParseRewritePattern(). + // Exposed here so that the validator for the rewrite pattern can also + // use it. + absl::StatusOr> parseRewritePatternHelper(absl::string_view pattern) const; + + // Returns the parsed Url rewrite pattern to be used by + // RewriteURLTemplatePattern() |capture_regex| should + // be the regex generated by ConvertURLPatternSyntaxToRegex(). + absl::StatusOr + parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) const; + + // Returns the rewritten URL path based on the given parsed rewrite pattern. + // Used for template-based URL rewrite. + absl::StatusOr rewriteURLTemplatePattern( + absl::string_view url, absl::string_view capture_regex, + const envoy::extensions::url_template::v3::RouteUrlRewritePattern& rewrite_pattern) const; + + // Returns if provided template match pattern is valid + bool isValidPathTemplateMatchPattern(const std::string& path_template_match); + + // Returns if provided rewrite pattern is valid + bool isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); + + // Returns if path_template and rewrite_template have valid variables + bool isValidSharedVariableSet(const std::string& path_template_rewrite, + absl::string_view capture_regex); + + absl::StatusOr rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const; + +public: + absl::string_view url_pattern_; + +private: + // move into library + absl::string_view url_rewrite_pattern_; + RE2 matching_pattern_regex_; + +}; } // namespace matching } // namespace Envoy diff --git a/test/extensions/url_template/BUILD b/test/extensions/url_template/BUILD new file mode 100644 index 0000000000000..2321f1f919dcd --- /dev/null +++ b/test/extensions/url_template/BUILD @@ -0,0 +1,33 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_package", +) +load( + "//test/extensions:extensions_build_system.bzl", + "envoy_extension_cc_test", +) + +licenses(["notice"]) # Apache 2 + +envoy_package() + +envoy_extension_cc_test( + name = "config_test", + srcs = ["config_test.cc"], + extension_names = ["envoy.url_template"], + deps = [ + "//source/common/stream_info:filter_state_lib", + "//source/extensions/url_template:config", + "//test/mocks/server:factory_context_mocks", + "@envoy_api//envoy/extensions/url_template/v3:pkg_cc_proto", + ], +) + +envoy_extension_cc_test( + name = "matcher_test", + srcs = ["matcher_test.cc"], + extension_names = ["envoy.url_template"], + deps = [ + "//source/extensions/url_template:url_template_matching", + ], +) diff --git a/test/extensions/url_template/config_test.cc b/test/extensions/url_template/config_test.cc new file mode 100644 index 0000000000000..b83f2650d601a --- /dev/null +++ b/test/extensions/url_template/config_test.cc @@ -0,0 +1,42 @@ +#include "envoy/registry/registry.h" +#include "envoy/router/url_template.h" + +#include "source/common/stream_info/filter_state_impl.h" +#include "source/extensions/url_template/config.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using namespace testing; + +namespace Envoy { +namespace Extensions { +namespace UrlTemplate { +namespace { + +class UrlTemplateConfigTest : public testing::Test { +protected: + UrlTemplateConfigTest() : filter_state_(StreamInfo::FilterState::LifeSpan::FilterChain) { + factory_ = Registry::FactoryRegistry::getFactory( + "envoy.url_template_predicates"); + config_ = factory_->createEmptyConfigProto(); + } + + StreamInfo::FilterStateImpl filter_state_; + Router::UrlTemplatePredicateFactory* factory_; + ProtobufTypes::MessagePtr config_; +}; + +TEST_F(UrlTemplateConfigTest, EmptyCreation) { + std::string current_route_name = "fake_current_route"; + // Create the predicate for the first time. + { + auto predicate = factory_->createUrlTemplatePredicate("url_pattern", "rewrite_pattern"); + ASSERT(predicate); + } +} + +} // namespace +} // namespace UrlTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/test/extensions/url_template/matcher_test.cc b/test/extensions/url_template/matcher_test.cc new file mode 100644 index 0000000000000..e6a2217a164a0 --- /dev/null +++ b/test/extensions/url_template/matcher_test.cc @@ -0,0 +1,34 @@ +#include "source/extensions/url_template/url_template_matching.h" + +#include "gtest/gtest.h" + +namespace Envoy { +namespace Extensions { +namespace UrlTemplate { + +TEST(StringMatcher, PatternMatch) { + matching::UrlTemplatePredicate matcher("/foo/{lang}/{country}", "rewrite"); + + EXPECT_TRUE(matcher.match("/foo/english/us")); + EXPECT_TRUE(matcher.match("/foo/spanish/spain")); + EXPECT_TRUE(matcher.match("/foo/french/france")); + + // with params + EXPECT_TRUE(matcher.match("/foo/english/us#fragment")); + EXPECT_TRUE(matcher.match("/foo/spanish/spain#fragment?param=val")); + EXPECT_TRUE(matcher.match("/foo/french/france?param=regex")); + + EXPECT_FALSE(matcher.match("/foo/english/us/goat")); + EXPECT_FALSE(matcher.match("/foo/goat")); + EXPECT_FALSE(matcher.match("/foo")); + EXPECT_FALSE(matcher.match("")); + + // with params + EXPECT_FALSE(matcher.match("/foo/english/us/goat#fragment?param=val")); + EXPECT_FALSE(matcher.match("/foo/goat?param=regex")); + EXPECT_FALSE(matcher.match("/foo?param=regex")); +} + +} // namespace UrlTemplate +} // namespace Extensions +} // namespace Envoy From 25ae25415e092b239fe7cf570198ca2d9dc55784 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 13 Jul 2022 20:25:22 +0000 Subject: [PATCH 011/238] Moved to extension and added some simple tests Signed-off-by: silverstar195 --- envoy/router/BUILD | 13 ++++ envoy/router/router.h | 21 +++++ envoy/router/url_template.h | 63 +++++++++++++++ source/common/http/async_client_impl.cc | 1 + source/common/http/async_client_impl.h | 4 + source/common/router/BUILD | 2 + source/common/router/config_impl.cc | 76 +++++++++++++++++-- source/common/router/config_impl.h | 44 ++++++++++- source/extensions/all_extensions.bzl | 2 + source/extensions/extensions_build_config.bzl | 2 +- source/extensions/extensions_metadata.yaml | 2 +- source/extensions/url_template/BUILD | 1 + source/extensions/url_template/config.cc | 10 +-- source/extensions/url_template/config.h | 19 ++--- .../url_template/url_template_matching.cc | 47 ++++++++---- .../url_template/url_template_matching.h | 40 +++++----- test/common/router/config_impl_test.cc | 75 ++++++++++++++++++ test/extensions/url_template/config_test.cc | 2 +- test/extensions/url_template/matcher_test.cc | 2 +- test/mocks/router/mocks.cc | 5 ++ test/mocks/router/mocks.h | 14 ++++ tools/extensions/extensions_schema.yaml | 1 + 22 files changed, 385 insertions(+), 61 deletions(-) create mode 100644 envoy/router/url_template.h diff --git a/envoy/router/BUILD b/envoy/router/BUILD index 1ec10b92c4291..0a46b0dd5d118 100644 --- a/envoy/router/BUILD +++ b/envoy/router/BUILD @@ -64,6 +64,7 @@ envoy_cc_library( external_deps = ["abseil_optional"], deps = [ ":internal_redirect_interface", + ":router_url_template_interface", "//envoy/access_log:access_log_interface", "//envoy/common:conn_pool_interface", "//envoy/common:matchers_interface", @@ -144,3 +145,15 @@ envoy_cc_library( "//envoy/server:factory_context_interface", ], ) + +envoy_cc_library( + name = "router_url_template_interface", + hdrs = ["url_template.h"], + deps = [ + "//envoy/config:typed_config_interface", + "//source/common/common:minimal_logger_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@envoy_api//envoy/config/core/v3:pkg_cc_proto", + ], +) diff --git a/envoy/router/router.h b/envoy/router/router.h index 30a675aa102af..4436cf61f4944 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -20,6 +20,7 @@ #include "envoy/http/hash_policy.h" #include "envoy/rds/config.h" #include "envoy/router/internal_redirect.h" +#include "envoy/router/url_template.h" #include "envoy/tcp/conn_pool.h" #include "envoy/tracing/http_tracer.h" #include "envoy/type/v3/percent.pb.h" @@ -297,6 +298,24 @@ class RetryPolicy { */ enum class RetryStatus { No, NoOverflow, NoRetryLimitExceeded, Yes }; +class PatternTemplatePolicy { +public: + virtual ~PatternTemplatePolicy() = default; + + /** + * @return whether internal redirect is enabled on this route. + */ + virtual bool enabled() const PURE; + + /** + * Creates the target route predicates. This should really be called only once for each upstream + * redirect response. Creating the predicates lazily to avoid wasting CPU cycles on non-redirect + * responses, which should be the most common case. + * @return a vector of newly constructed InternalRedirectPredicate instances. + */ + virtual PatternTemplatePredicateSharedPtr predicate() const PURE; +}; + /** * InternalRedirectPolicy from the route configuration. */ @@ -915,6 +934,8 @@ class RouteEntry : public ResponseEntry { */ virtual const InternalRedirectPolicy& internalRedirectPolicy() const PURE; + virtual const PatternTemplatePolicy& patternTemplatePolicy() const PURE; + /** * @return uint32_t any route cap on bytes which should be buffered for shadowing or retries. * This is an upper bound so does not necessarily reflect the bytes which will be buffered diff --git a/envoy/router/url_template.h b/envoy/router/url_template.h new file mode 100644 index 0000000000000..d70d89e082044 --- /dev/null +++ b/envoy/router/url_template.h @@ -0,0 +1,63 @@ +#pragma once + +#include "envoy/config/typed_config.h" +#include "source/common/common/logger.h" + +#include "absl/strings/string_view.h" +#include "absl/status/statusor.h" + +namespace Envoy { +namespace Router { + +/** + * Used to decide if an internal redirect is allowed to be followed based on the target route. + * Subclassing Logger::Loggable so that implementations can log details. + */ +class PatternTemplatePredicate : Logger::Loggable { +public: + PatternTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) : + url_pattern_(url_pattern), + url_rewrite_pattern_(url_rewrite_pattern) {}; + + PatternTemplatePredicate() = default; + + virtual ~PatternTemplatePredicate() = default; + + virtual absl::string_view name() const PURE; + virtual std::string category() const PURE; + + virtual bool match(absl::string_view pattern) const PURE; + + virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const PURE; + + virtual absl::Status is_valid_match_pattern(std::string match_pattern) const PURE; + virtual absl::Status is_valid_rewrite_pattern(std::string match_pattern, std::string rewrite_pattern) const PURE; + + const std::string url_pattern_; + const std::string url_rewrite_pattern_; +}; + +using PatternTemplatePredicateSharedPtr = std::shared_ptr; + +/** + * Factory for UrlTemplatePredicateFactory. + */ +class PatternTemplatePredicateFactory : public Envoy::Config::TypedFactory { +public: + virtual ~PatternTemplatePredicateFactory() = default; + + /** + * @param config contains the proto stored in TypedExtensionConfig.typed_config for the predicate. + * @param current_route_name stores the route name of the route where the predicate is installed. + * @return an InternalRedirectPredicate. The given current_route_name is useful for predicates + * that need to create per-route FilterState. + */ + virtual PatternTemplatePredicateSharedPtr + createUrlTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) PURE; + + std::string category() const override { return "envoy.url_template"; } +}; + +} // namespace Router +} // namespace Envoy \ No newline at end of file diff --git a/source/common/http/async_client_impl.cc b/source/common/http/async_client_impl.cc index ab2f9941c1edd..b867f4c273ad8 100644 --- a/source/common/http/async_client_impl.cc +++ b/source/common/http/async_client_impl.cc @@ -20,6 +20,7 @@ const std::vector> const AsyncStreamImpl::NullHedgePolicy AsyncStreamImpl::RouteEntryImpl::hedge_policy_; const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::RouteEntryImpl::rate_limit_policy_; const Router::InternalRedirectPolicyImpl AsyncStreamImpl::RouteEntryImpl::internal_redirect_policy_; +const Router::PatternTemplatePolicyImpl AsyncStreamImpl::RouteEntryImpl::pattern_template_policy_; const std::vector AsyncStreamImpl::RouteEntryImpl::shadow_policies_; const AsyncStreamImpl::NullVirtualHost AsyncStreamImpl::RouteEntryImpl::virtual_host_; const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::NullVirtualHost::rate_limit_policy_; diff --git a/source/common/http/async_client_impl.h b/source/common/http/async_client_impl.h index 6e13c150e3091..9d3b7814d0939 100644 --- a/source/common/http/async_client_impl.h +++ b/source/common/http/async_client_impl.h @@ -231,6 +231,9 @@ class AsyncStreamImpl : public AsyncClient::Stream, const Router::InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } + const Router::PatternTemplatePolicy& patternTemplatePolicy() const override { + return pattern_template_policy_; + } uint32_t retryShadowBufferLimit() const override { return std::numeric_limits::max(); } @@ -294,6 +297,7 @@ class AsyncStreamImpl : public AsyncClient::Stream, static const NullHedgePolicy hedge_policy_; static const NullRateLimitPolicy rate_limit_policy_; static const Router::InternalRedirectPolicyImpl internal_redirect_policy_; + static const Router::PatternTemplatePolicyImpl pattern_template_policy_; static const std::vector shadow_policies_; static const NullVirtualHost virtual_host_; static const std::multimap opaque_config_; diff --git a/source/common/router/BUILD b/source/common/router/BUILD index 674bb5a71455b..feb462839ed98 100644 --- a/source/common/router/BUILD +++ b/source/common/router/BUILD @@ -70,6 +70,8 @@ envoy_cc_library( "//source/common/tracing:http_tracer_lib", "//source/common/upstream:retry_factory_lib", "//source/extensions/early_data:default_early_data_policy_lib", + "//source/extensions/url_template:config", + "//source/extensions/url_template:url_template_matching", "@envoy_api//envoy/config/common/matcher/v3:pkg_cc_proto", "@envoy_api//envoy/config/core/v3:pkg_cc_proto", "@envoy_api//envoy/config/route/v3:pkg_cc_proto", diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 783f79946ef4e..0fa483bc3a80d 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -48,6 +48,9 @@ #include "source/common/tracing/http_tracer_impl.h" #include "source/common/upstream/retry_factory.h" #include "source/extensions/early_data/default_early_data_policy.h" +#include "source/extensions/url_template/url_template_matching.h" +#include "source/extensions/url_template/config.h" + #include "absl/strings/match.h" @@ -354,6 +357,21 @@ std::vector InternalRedirectPolicyImpl::pred return predicates; } +PatternTemplatePolicyImpl::PatternTemplatePolicyImpl() : enabled_(false){}; + +PatternTemplatePolicyImpl::PatternTemplatePolicyImpl(std::string url_pattern, + std::string url_rewrite_pattern) + : url_pattern_(url_pattern), url_rewrite_pattern_(url_rewrite_pattern), enabled_(true) { + absl::string_view name = "envoy.url_template.pattern_template_predicates"; + auto* factory = Registry::FactoryRegistry::getFactory(name); + ASSERT(factory); // factory not found + predicate_factory_ = factory; +} + +PatternTemplatePredicateSharedPtr PatternTemplatePolicyImpl::predicate() const { + return predicate_factory_->createUrlTemplatePredicate(url_pattern_, url_rewrite_pattern_); +} + absl::flat_hash_set InternalRedirectPolicyImpl::buildRedirectResponseCodes( const envoy::config::route::v3::InternalRedirectPolicy& policy_config) const { if (policy_config.redirect_response_codes_size() == 0) { @@ -474,6 +492,8 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, ProtobufMessage::ValidationVisitor& validator) : case_sensitive_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.match(), case_sensitive, true)), prefix_rewrite_(route.route().prefix_rewrite()), + pattern_template_policy_(buildPatternTemplatePolicy(route.match().path_template(), + route.route().path_template_rewrite())), host_rewrite_(route.route().host_rewrite_literal()), vhost_(vhost), auto_host_rewrite_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.route(), auto_host_rewrite, false)), auto_host_rewrite_header_(!route.route().host_rewrite_header().empty() @@ -811,7 +831,8 @@ void RouteEntryImplBase::finalizeRequestHeaders(Http::RequestHeaderMap& headers, // Handle path rewrite absl::optional container; - if (!getPathRewrite(headers, container).empty() || regex_rewrite_ != nullptr) { + if (!getPathRewrite(headers, container).empty() || regex_rewrite_ != nullptr || + pattern_template_policy_.enabled()) { rewritePathHeader(headers, insert_envoy_original_path); } } @@ -947,6 +968,12 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa } } + // complete pattern rewrite + if (pattern_template_policy_.enabled()) { + auto just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); + return *std::move(pattern_template_policy_.predicate()->rewritePattern(just_path, matched_path)); + } + // There are no rewrites configured. return absl::optional(); } @@ -1138,6 +1165,43 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( return InternalRedirectPolicyImpl(policy_config, validator, current_route_name); } +PatternTemplatePolicyImpl +RouteEntryImplBase::buildPatternTemplatePolicy(std::string path_template, + std::string path_template_rewrite) const { + // match + rewrite + if (!path_template.empty() && !path_template_rewrite.empty()) { + PatternTemplatePolicyImpl policy = + PatternTemplatePolicyImpl(path_template, path_template_rewrite); + + if (!policy.predicate()->is_valid_match_pattern(path_template).ok()) { + throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); + } + + if (!policy.predicate()->is_valid_rewrite_pattern(path_template, path_template_rewrite).ok()) { + throw EnvoyException( + fmt::format("mismatch between path_template_match {} and pattern_rewrite {}", + path_template, path_template_rewrite)); + } + + return policy; + } + + // only match + if (!path_template.empty()) { + PatternTemplatePolicyImpl policy = + PatternTemplatePolicyImpl(path_template, path_template_rewrite); + + if (!policy.predicate()->is_valid_match_pattern(path_template).ok()) { + throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); + } + + return policy; + } + + // no match + no rewrite + return PatternTemplatePolicyImpl(); +} + DecoratorConstPtr RouteEntryImplBase::parseDecorator(const envoy::config::route::v3::Route& route) { DecoratorConstPtr ret; if (route.has_decorator()) { @@ -1380,25 +1444,23 @@ PathTemplateRouteEntryImpl::PathTemplateRouteEntryImpl( const OptionalHttpFilters& optional_http_filters, Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) - : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - path_template_(route.match().path_template()), - path_matcher_(Matchers::PathMatcher::createPattern(path_template_, !case_sensitive_)) {} + : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator) {} void PathTemplateRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { - finalizePathHeader(headers, path_template_, insert_envoy_original_path); + finalizePathHeader(headers, pattern_template_policy_.predicate()->url_pattern_, insert_envoy_original_path); } absl::optional PathTemplateRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { - return currentUrlPathAfterRewriteWithMatchedPath(headers, path_template_); + return currentUrlPathAfterRewriteWithMatchedPath(headers, pattern_template_policy_.predicate()->url_pattern_); } RouteConstSharedPtr PathTemplateRouteEntryImpl::matches(const Http::RequestHeaderMap& headers, const StreamInfo::StreamInfo& stream_info, uint64_t random_value) const { if (RouteEntryImplBase::matchRoute(headers, stream_info, random_value) && - path_matcher_->match(headers.getPathValue())) { + pattern_template_policy_.predicate()->match(headers.getPathValue())) { return clusterEntry(headers, random_value); } return nullptr; diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 71c95296b60fa..9aed8170bd407 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -34,6 +34,8 @@ #include "source/common/router/tls_context_match_criteria_impl.h" #include "source/common/stats/symbol_table.h" +#include "source/extensions/url_template/url_template_matching.h" + #include "absl/container/node_hash_map.h" #include "absl/types/optional.h" @@ -453,6 +455,31 @@ class RouteTracingImpl : public RouteTracing { Tracing::CustomTagMap custom_tags_; }; +/** + * Implementation of InternalRedirectPolicy that reads from the proto + * InternalRedirectPolicy of the RouteAction. + */ +class PatternTemplatePolicyImpl : public PatternTemplatePolicy { +public: + // Constructor that enables internal redirect with policy_config controlling the configurable + // behaviors. + PatternTemplatePolicyImpl(std::string url_pattern, std::string url_rewrite_pattern); + + // Default constructor that disables internal redirect. + PatternTemplatePolicyImpl(); + + bool enabled() const override { return enabled_; } + + PatternTemplatePredicateSharedPtr predicate() const override; + + const std::string url_pattern_; + const std::string url_rewrite_pattern_; + +private: + const bool enabled_; + PatternTemplatePredicateFactory* predicate_factory_; +}; + /** * Implementation of InternalRedirectPolicy that reads from the proto * InternalRedirectPolicy of the RouteAction. @@ -564,6 +591,9 @@ class RouteEntryImplBase : public RouteEntry, const InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } + const PatternTemplatePolicy& patternTemplatePolicy() const override { + return pattern_template_policy_; + } uint32_t retryShadowBufferLimit() const override { return retry_shadow_buffer_limit_; } const std::vector& shadowPolicies() const override { return shadow_policies_; } const VirtualCluster* virtualCluster(const Http::HeaderMap& headers) const override { @@ -677,6 +707,9 @@ class RouteEntryImplBase : public RouteEntry, const InternalRedirectPolicy& internalRedirectPolicy() const override { return parent_->internalRedirectPolicy(); } + const PatternTemplatePolicy& patternTemplatePolicy() const override { + return parent_->patternTemplatePolicy(); + } uint32_t retryShadowBufferLimit() const override { return parent_->retryShadowBufferLimit(); } const std::vector& shadowPolicies() const override { return parent_->shadowPolicies(); @@ -842,6 +875,7 @@ class RouteEntryImplBase : public RouteEntry, const std::string prefix_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_redirect_; + const PatternTemplatePolicyImpl pattern_template_policy_; std::string regex_rewrite_substitution_; std::string regex_rewrite_redirect_substitution_; const std::string host_rewrite_; @@ -923,6 +957,9 @@ class RouteEntryImplBase : public RouteEntry, ProtobufMessage::ValidationVisitor& validator, absl::string_view current_route_name) const; + PatternTemplatePolicyImpl + buildPatternTemplatePolicy(std::string path_template, std::string path_template_rewrite) const; + RouteConstSharedPtr pickClusterViaClusterHeader(const Http::LowerCaseString& cluster_header_name, const Http::HeaderMap& headers) const; @@ -1012,7 +1049,7 @@ class PathTemplateRouteEntryImpl : public RouteEntryImplBase { ProtobufMessage::ValidationVisitor& validator); // Router::PathMatchCriterion - const std::string& matcher() const override { return path_template_; } + const std::string& matcher() const override { return match_pattern_; } PathMatchType matchType() const override { return PathMatchType::Pattern; } // Router::Matchable @@ -1028,9 +1065,8 @@ class PathTemplateRouteEntryImpl : public RouteEntryImplBase { absl::optional currentUrlPathAfterRewrite(const Http::RequestHeaderMap& headers) const override; -private: - const std::string path_template_; - const Matchers::PathMatcherConstSharedPtr path_matcher_; + private: + const std::string match_pattern_; }; /** diff --git a/source/extensions/all_extensions.bzl b/source/extensions/all_extensions.bzl index 0e6b28a0d9fb7..fb340840ae8ef 100644 --- a/source/extensions/all_extensions.bzl +++ b/source/extensions/all_extensions.bzl @@ -9,6 +9,7 @@ _required_extensions = { "envoy.transport_sockets.tls": "//source/extensions/transport_sockets/tls:config", "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", + "envoy.url_template.pattern_template_predicates": "//source/extensions/url_template:config", } # Return the extension cc_library target after select @@ -34,6 +35,7 @@ _core_extensions = [ "envoy.transport_sockets.raw_buffer", "envoy.network.dns_resolver.cares", "envoy.network.dns_resolver.apple", + "envoy.url_template.pattern_template_predicates", ] # Return all core extensions to be compiled into Envoy. diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index ad0f6aacf9649..fcd7fadc3ee88 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -59,7 +59,7 @@ EXTENSIONS = { # # Pattern Matcher # - "envoy.url_template": "//source/extensions/url_template:config", + "envoy.url_template.pattern_template_predicates": "//source/extensions/url_template:config", # # Generic Inputs diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index d6598562efb83..1d42f347588af 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -734,7 +734,7 @@ envoy.matching.input_matchers.ip: - envoy.matching.input_matchers security_posture: robust_to_untrusted_downstream_and_upstream status: stable -envoy.url_template: +envoy.url_template.pattern_template_predicates: categories: - envoy.url_template security_posture: robust_to_untrusted_downstream_and_upstream diff --git a/source/extensions/url_template/BUILD b/source/extensions/url_template/BUILD index 58afef3dc35a7..d1f794f3a38dc 100644 --- a/source/extensions/url_template/BUILD +++ b/source/extensions/url_template/BUILD @@ -79,6 +79,7 @@ envoy_cc_extension( name = "config", srcs = ["config.cc"], hdrs = ["config.h"], + visibility = ["//visibility:public"], deps = [ ":url_template_matching", "//envoy/registry", diff --git a/source/extensions/url_template/config.cc b/source/extensions/url_template/config.cc index b048bde658d65..4dbb5a38be1bb 100644 --- a/source/extensions/url_template/config.cc +++ b/source/extensions/url_template/config.cc @@ -2,14 +2,10 @@ #include "envoy/registry/registry.h" #include "envoy/router/url_template.h" -// TODO change includes namespace Envoy { -namespace Extensions { -namespace UrlTemplate { +namespace matching { -REGISTER_FACTORY(UrlTemplatePredicateFactory, Router::UrlTemplatePredicateFactory); - -} // namespace UrlTemplate -} // namespace Extensions +REGISTER_FACTORY(PatternTemplatePredicateFactory, Router::PatternTemplatePredicateFactory); +} // namespace matching } // namespace Envoy \ No newline at end of file diff --git a/source/extensions/url_template/config.h b/source/extensions/url_template/config.h index c1e075bffe0b4..dfab51c93d536 100644 --- a/source/extensions/url_template/config.h +++ b/source/extensions/url_template/config.h @@ -6,24 +6,25 @@ #include "source/extensions/url_template/url_template_matching.h" namespace Envoy { -namespace Extensions { -namespace UrlTemplate { +namespace matching { -class UrlTemplatePredicateFactory : public Router::UrlTemplatePredicateFactory { +class PatternTemplatePredicateFactory : public Router::PatternTemplatePredicateFactory { public: - Router::UrlTemplatePredicateSharedPtr + Router::PatternTemplatePredicateSharedPtr createUrlTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) override { - return std::make_shared(url_pattern, url_rewrite_pattern); + return std::make_shared(url_pattern, url_rewrite_pattern); } - std::string name() const override { return "envoy.url_template_predicates"; } - ProtobufTypes::MessagePtr createEmptyConfigProto() override { // may not be used to investigate return std::make_unique(); } + + std::string name() const override { + return "envoy.url_template.pattern_template_predicates"; + } + }; -} // namespace UrlTemplate -} // namespace Extensions +} // namespace matching } // namespace Envoy \ No newline at end of file diff --git a/source/extensions/url_template/url_template_matching.cc b/source/extensions/url_template/url_template_matching.cc index d36cb56d3747e..c7e186874c7f5 100644 --- a/source/extensions/url_template/url_template_matching.cc +++ b/source/extensions/url_template/url_template_matching.cc @@ -27,12 +27,12 @@ using matching::url_template_matching_internal::ParsedUrlPattern; inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.data(), text.size()}; } -bool UrlTemplatePredicate::match(absl::string_view pattern) const { +bool PatternTemplatePredicate::match(absl::string_view pattern) const { return RE2::FullMatch(ToStringPiece(Http::PathUtil::removeQueryAndFragment(pattern)), matching_pattern_regex_); } absl::StatusOr -UrlTemplatePredicate::convertURLPatternSyntaxToRegex(absl::string_view url_pattern) const { +PatternTemplatePredicate::convertURLPatternSyntaxToRegex(absl::string_view url_pattern) const { absl::StatusOr status = url_template_matching_internal::parseURLPatternSyntax(url_pattern); @@ -44,7 +44,7 @@ UrlTemplatePredicate::convertURLPatternSyntaxToRegex(absl::string_view url_patte } absl::StatusOr> -UrlTemplatePredicate::parseRewritePatternHelper(absl::string_view pattern) const { +PatternTemplatePredicate::parseRewritePatternHelper(absl::string_view pattern) const { std::vector result; // Don't allow contiguous '/' patterns. @@ -88,7 +88,7 @@ UrlTemplatePredicate::parseRewritePatternHelper(absl::string_view pattern) const } absl::StatusOr -UrlTemplatePredicate::parseRewritePattern(absl::string_view pattern, +PatternTemplatePredicate::parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) const { envoy::extensions::url_template::v3::RouteUrlRewritePattern parsed_pattern; RE2 regex = RE2(ToStringPiece(capture_regex)); @@ -122,7 +122,7 @@ UrlTemplatePredicate::parseRewritePattern(absl::string_view pattern, return parsed_pattern; } -absl::StatusOr UrlTemplatePredicate::rewriteURLTemplatePattern( +absl::StatusOr PatternTemplatePredicate::rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, const envoy::extensions::url_template::v3::RouteUrlRewritePattern& rewrite_pattern) const { RE2 regex = RE2(ToStringPiece(capture_regex)); @@ -154,20 +154,20 @@ absl::StatusOr UrlTemplatePredicate::rewriteURLTemplatePattern( return rewritten_url; } -bool UrlTemplatePredicate::isValidPathTemplateMatchPattern(const std::string& path_template_match) { - return convertURLPatternSyntaxToRegex(path_template_match).ok(); +absl::Status PatternTemplatePredicate::isValidPathTemplateMatchPattern(const std::string& path_template_match) const { + return convertURLPatternSyntaxToRegex(path_template_match).status(); } -bool UrlTemplatePredicate::isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { - return parseRewritePatternHelper(path_template_rewrite).ok(); +absl::Status PatternTemplatePredicate::isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) const { + return parseRewritePatternHelper(path_template_rewrite).status(); } -bool UrlTemplatePredicate::isValidSharedVariableSet(const std::string& path_template_rewrite, - absl::string_view capture_regex) { - return parseRewritePattern(path_template_rewrite, capture_regex).ok(); +absl::Status PatternTemplatePredicate::isValidSharedVariableSet(const std::string& path_template_rewrite, + std::string& capture_regex) const { + return parseRewritePattern(path_template_rewrite, capture_regex).status(); } -absl::StatusOr UrlTemplatePredicate::rewritePattern(absl::string_view current_pattern, absl::string_view matched_path) const { +absl::StatusOr PatternTemplatePredicate::rewritePattern(absl::string_view current_pattern, absl::string_view matched_path) const { absl::StatusOr regex_pattern = convertURLPatternSyntaxToRegex(matched_path); if (!regex_pattern.ok()) { return absl::InvalidArgumentError("Unable to parse url pattern regex"); @@ -194,5 +194,26 @@ absl::StatusOr UrlTemplatePredicate::rewritePattern(absl::string_vi return *std::move(new_path); } +absl::Status PatternTemplatePredicate::is_valid_match_pattern(std::string match_pattern) const { + return isValidPathTemplateMatchPattern(match_pattern); +}; + +absl::Status PatternTemplatePredicate::is_valid_rewrite_pattern(std::string match_pattern, std::string rewrite_pattern) const { + absl::StatusOr status = + convertURLPatternSyntaxToRegex(match_pattern); + if (!status.ok()) { + return absl::InvalidArgumentError(fmt::format("path_template {} is invalid", match_pattern)); + } + + std::string path_template_match_regex = *std::move(status); + if (path_template_match_regex.empty() || + !isValidSharedVariableSet(rewrite_pattern, path_template_match_regex).ok()) { + return absl::InvalidArgumentError(fmt::format("mismatch between path_template_match {} and pattern_rewrite {}", + match_pattern, rewrite_pattern)); + } + + return absl::OkStatus(); +}; + } // namespace matching } // namespace Envoy diff --git a/source/extensions/url_template/url_template_matching.h b/source/extensions/url_template/url_template_matching.h index 505e92a077956..0faa4a0420f08 100644 --- a/source/extensions/url_template/url_template_matching.h +++ b/source/extensions/url_template/url_template_matching.h @@ -7,6 +7,7 @@ #include "source/extensions/url_template/url_template_matching_internal.h" + #include "absl/status/statusor.h" #include "absl/strings/string_view.h" #include "envoy/router/url_template.h" @@ -22,16 +23,28 @@ struct RewritePatternSegment { RewriteStringKind kind; }; -class UrlTemplatePredicate : public Router::UrlTemplatePredicate { + +class PatternTemplatePredicate : public Router::PatternTemplatePredicate { public: - explicit UrlTemplatePredicate(absl::string_view url_pattern, absl::string_view url_rewrite_pattern) - : url_pattern_(url_pattern), url_rewrite_pattern_(url_rewrite_pattern), + explicit PatternTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) + : Router::PatternTemplatePredicate(url_pattern, url_rewrite_pattern), matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())) {} + PatternTemplatePredicate() = default; - absl::string_view name() const override { return "envoy.url_template"; } + absl::string_view name() const override { return "envoy.url_template.pattern_template_predicates"; } + std::string category() const override { return "envoy.url_template"; } // Returns if the regex pattern matches the given regex from constructor. - bool match(absl::string_view pattern) const; + bool match(absl::string_view pattern) const override; + + absl::Status is_valid_match_pattern(std::string match_pattern) const override; + + absl::Status is_valid_rewrite_pattern(std::string match_pattern, std::string rewrite_pattern) const override; + + absl::StatusOr rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const override; + +private: // Returns the regex pattern that is equivalent to the given url_pattern. // Used in the config pipeline to translate user given url pattern to @@ -57,25 +70,18 @@ class UrlTemplatePredicate : public Router::UrlTemplatePredicate { const envoy::extensions::url_template::v3::RouteUrlRewritePattern& rewrite_pattern) const; // Returns if provided template match pattern is valid - bool isValidPathTemplateMatchPattern(const std::string& path_template_match); + absl::Status isValidPathTemplateMatchPattern(const std::string& path_template_match) const; // Returns if provided rewrite pattern is valid - bool isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); + absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) const; // Returns if path_template and rewrite_template have valid variables - bool isValidSharedVariableSet(const std::string& path_template_rewrite, - absl::string_view capture_regex); - - absl::StatusOr rewritePattern(absl::string_view current_pattern, - absl::string_view matched_path) const; - -public: - absl::string_view url_pattern_; + absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, + std::string& capture_regex) const; private: // move into library - absl::string_view url_rewrite_pattern_; - RE2 matching_pattern_regex_; + RE2 matching_pattern_regex_{nullptr}; }; diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index b883c4131061c..eecf8ba9e3ccf 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -8832,6 +8832,81 @@ TEST_F(RouteConfigurationV2, InternalRedirectIsDisabledWhenNotSpecifiedInRouteAc EXPECT_FALSE(internal_redirect_policy.enabled()); } +TEST_F(RouteConfigurationV2, TemplatePatternIsDisabledWhenNotSpecifiedInRouteAction) { + const std::string yaml = R"EOF( +virtual_hosts: + - name: regex + domains: [idle.lyft.com] + routes: + - match: + safe_regex: + regex: "/regex" + route: + cluster: some-cluster + )EOF"; + + factory_context_.cluster_manager_.initializeClusters({"some-cluster"}, {}); + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + Http::TestRequestHeaderMapImpl headers = + genRedirectHeaders("idle.lyft.com", "/regex", true, false); + const auto& pattern_template_policy = + config.route(headers, 0)->routeEntry()->patternTemplatePolicy(); + EXPECT_FALSE(pattern_template_policy.enabled()); +} + +TEST_F(RouteConfigurationV2, TemplatePatternIsFilledFromConfigInRouteAction) { + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/bar/{country}/{lang}" + route: + path_template_rewrite: "/bar/{lang}/{country}" + cluster: some-cluster + )EOF"; + + factory_context_.cluster_manager_.initializeClusters({"some-cluster"}, {}); + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + Http::TestRequestHeaderMapImpl headers = genHeaders("path.prefix.com", "/bar/one/two", "GET"); + + const auto& pattern_template_policy = + config.route(headers, 0)->routeEntry()->patternTemplatePolicy(); + + EXPECT_TRUE(pattern_template_policy.enabled()); + EXPECT_EQ(pattern_template_policy.predicate()->url_pattern_, "/bar/{country}/{lang}"); + EXPECT_EQ(pattern_template_policy.predicate()->url_rewrite_pattern_, "/bar/{lang}/{country}"); +} + +TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { + + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{lang}/{state}" + case_sensitive: false + route: { cluster: path-pattern-cluster} + )EOF"; + + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster", "default-cluster"}, + {}); + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + + // Pattern matches + EXPECT_EQ("path-pattern-cluster", + config.route(genHeaders("path.prefix.com", "/rest/english/wa", "GET"), 0) + ->routeEntry() + ->clusterName()); + EXPECT_EQ("path-pattern-cluster", + config.route(genHeaders("path.prefix.com", "/rest/spanish/mexico", "GET"), 0) + ->routeEntry() + ->clusterName()); +} + TEST_F(RouteConfigurationV2, DefaultInternalRedirectPolicyIsSensible) { const std::string yaml = R"EOF( virtual_hosts: diff --git a/test/extensions/url_template/config_test.cc b/test/extensions/url_template/config_test.cc index b83f2650d601a..7c80a105cb744 100644 --- a/test/extensions/url_template/config_test.cc +++ b/test/extensions/url_template/config_test.cc @@ -31,7 +31,7 @@ TEST_F(UrlTemplateConfigTest, EmptyCreation) { std::string current_route_name = "fake_current_route"; // Create the predicate for the first time. { - auto predicate = factory_->createUrlTemplatePredicate("url_pattern", "rewrite_pattern"); + auto predicate = factory_->createUrlTemplatePredicate("/url_pattern/{TEST}", "rewrite_pattern"); ASSERT(predicate); } } diff --git a/test/extensions/url_template/matcher_test.cc b/test/extensions/url_template/matcher_test.cc index e6a2217a164a0..0b0709a3a0ade 100644 --- a/test/extensions/url_template/matcher_test.cc +++ b/test/extensions/url_template/matcher_test.cc @@ -6,7 +6,7 @@ namespace Envoy { namespace Extensions { namespace UrlTemplate { -TEST(StringMatcher, PatternMatch) { +TEST(UrlTemplate, RouteMatcher) { matching::UrlTemplatePredicate matcher("/foo/{lang}/{country}", "rewrite"); EXPECT_TRUE(matcher.match("/foo/english/us")); diff --git a/test/mocks/router/mocks.cc b/test/mocks/router/mocks.cc index 72762b45ec577..7f0a8a08d42c3 100644 --- a/test/mocks/router/mocks.cc +++ b/test/mocks/router/mocks.cc @@ -28,6 +28,10 @@ MockInternalRedirectPolicy::MockInternalRedirectPolicy() { ON_CALL(*this, enabled()).WillByDefault(Return(false)); } +MockPatternTemplatePolicy::MockPatternTemplatePolicy() { + ON_CALL(*this, enabled()).WillByDefault(Return(false)); +} + MockRetryState::MockRetryState() = default; void MockRetryState::expectHeadersRetry() { @@ -100,6 +104,7 @@ MockRouteEntry::MockRouteEntry() { ON_CALL(*this, rateLimitPolicy()).WillByDefault(ReturnRef(rate_limit_policy_)); ON_CALL(*this, retryPolicy()).WillByDefault(ReturnRef(retry_policy_)); ON_CALL(*this, internalRedirectPolicy()).WillByDefault(ReturnRef(internal_redirect_policy_)); + ON_CALL(*this, patternTemplatePolicy()).WillByDefault(ReturnRef(pattern_template_policy_)); ON_CALL(*this, retryShadowBufferLimit()) .WillByDefault(Return(std::numeric_limits::max())); ON_CALL(*this, shadowPolicies()).WillByDefault(ReturnRef(shadow_policies_)); diff --git a/test/mocks/router/mocks.h b/test/mocks/router/mocks.h index 2e0b4be9643f5..ef54cd80d2321 100644 --- a/test/mocks/router/mocks.h +++ b/test/mocks/router/mocks.h @@ -164,6 +164,18 @@ class MockInternalRedirectPredicate : public InternalRedirectPredicate { MOCK_METHOD(absl::string_view, name, (), (const)); }; +class MockPatternTemplatePolicy : public PatternTemplatePolicy { +public: + MockPatternTemplatePolicy(); + MOCK_METHOD(bool, enabled, (), (const)); + MOCK_METHOD(PatternTemplatePredicateSharedPtr, predicate, (), (const)); +}; + +class MockPatternTemplatePredicate : public PatternTemplatePredicate { +public: + MOCK_METHOD(absl::string_view, name, (), (const)); +}; + class MockRetryState : public RetryState { public: MockRetryState(); @@ -376,6 +388,7 @@ class MockRouteEntry : public RouteEntry { MOCK_METHOD(const RateLimitPolicy&, rateLimitPolicy, (), (const)); MOCK_METHOD(const RetryPolicy&, retryPolicy, (), (const)); MOCK_METHOD(const InternalRedirectPolicy&, internalRedirectPolicy, (), (const)); + MOCK_METHOD(const PatternTemplatePolicy&, patternTemplatePolicy, (), (const)); MOCK_METHOD(uint32_t, retryShadowBufferLimit, (), (const)); MOCK_METHOD(const std::vector&, shadowPolicies, (), (const)); MOCK_METHOD(std::chrono::milliseconds, timeout, (), (const)); @@ -413,6 +426,7 @@ class MockRouteEntry : public RouteEntry { TestVirtualCluster virtual_cluster_; TestRetryPolicy retry_policy_; testing::NiceMock internal_redirect_policy_; + testing::NiceMock pattern_template_policy_; TestHedgePolicy hedge_policy_; testing::NiceMock rate_limit_policy_; std::vector shadow_policies_; diff --git a/tools/extensions/extensions_schema.yaml b/tools/extensions/extensions_schema.yaml index b85d3f1db07d8..a1bf328f370d3 100644 --- a/tools/extensions/extensions_schema.yaml +++ b/tools/extensions/extensions_schema.yaml @@ -73,6 +73,7 @@ categories: - envoy.http.header_validators - envoy.http.stateful_header_formatters - envoy.internal_redirect_predicates +- envoy.url_template - envoy.io_socket - envoy.http.original_ip_detection - envoy.matching.common_inputs From ea4353bc210162852e2e95a5f3ef9c9c57a228c2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 13 Jul 2022 20:26:04 +0000 Subject: [PATCH 012/238] Added to proto Signed-off-by: silverstar195 --- .../url_template/v3/route_url_rewrite_pattern.proto | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto b/api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto index b60139af37994..e58e123373580 100644 --- a/api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto +++ b/api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto @@ -7,13 +7,13 @@ import "udpa/annotations/status.proto"; option java_package = "io.envoyproxy.envoy.extensions.matching.pattern_matcher.v3"; option java_outer_classname = "RouteUrlRewritePatternProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/matching/pattern_matcher/v3;rewritepatternv3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/url_template/v3;urltemplatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Pattern matcher] // [#extension: envoy.matching.pattern_matcher] -// TBD +// Holds the segments for rewriting urls base on pattern templates message RouteUrlRewritePattern { message RewriteSegment { oneof segment_type { From 3aebc125e46d6449db32cc8c02e8c106a8f0a615 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 14 Jul 2022 15:51:45 +0000 Subject: [PATCH 013/238] All tests added and passing Signed-off-by: silverstar195 --- envoy/router/url_template.h | 3 - source/common/router/config_impl.cc | 32 +- .../url_template/url_template_matching.cc | 46 ++- .../url_template/url_template_matching.h | 26 +- test/common/router/config_impl_test.cc | 368 +++++++++++++++++- 5 files changed, 426 insertions(+), 49 deletions(-) diff --git a/envoy/router/url_template.h b/envoy/router/url_template.h index d70d89e082044..33ec50dbda83b 100644 --- a/envoy/router/url_template.h +++ b/envoy/router/url_template.h @@ -31,9 +31,6 @@ class PatternTemplatePredicate : Logger::Loggable { virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, absl::string_view matched_path) const PURE; - virtual absl::Status is_valid_match_pattern(std::string match_pattern) const PURE; - virtual absl::Status is_valid_rewrite_pattern(std::string match_pattern, std::string rewrite_pattern) const PURE; - const std::string url_pattern_; const std::string url_rewrite_pattern_; }; diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 0fa483bc3a80d..54b3712df81c1 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -659,14 +659,26 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, } if (route.route().has_regex_rewrite()) { - if (!prefix_rewrite_.empty()) { - throw EnvoyException("Cannot specify both prefix_rewrite and regex_rewrite"); + if (!prefix_rewrite_.empty() || pattern_template_policy_.enabled()) { + throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_template_rewrite"); } auto rewrite_spec = route.route().regex_rewrite(); regex_rewrite_ = Regex::Utility::parseRegex(rewrite_spec.pattern()); regex_rewrite_substitution_ = rewrite_spec.substitution(); } + if (pattern_template_policy_.enabled()) { + if (!prefix_rewrite_.empty() || route.route().has_regex_rewrite()) { + throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_template_rewrite"); + } + } + + if (!prefix_rewrite_.empty()) { + if (route.route().has_regex_rewrite() || pattern_template_policy_.enabled()) { + throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_template_rewrite"); + } + } + if (route.redirect().has_regex_rewrite()) { ASSERT(prefix_rewrite_redirect_.empty()); auto rewrite_spec = route.redirect().regex_rewrite(); @@ -1170,32 +1182,28 @@ RouteEntryImplBase::buildPatternTemplatePolicy(std::string path_template, std::string path_template_rewrite) const { // match + rewrite if (!path_template.empty() && !path_template_rewrite.empty()) { - PatternTemplatePolicyImpl policy = - PatternTemplatePolicyImpl(path_template, path_template_rewrite); - if (!policy.predicate()->is_valid_match_pattern(path_template).ok()) { + if (!matching::PatternTemplatePredicate::is_valid_match_pattern(path_template).ok()) { throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); } - if (!policy.predicate()->is_valid_rewrite_pattern(path_template, path_template_rewrite).ok()) { + if (!matching::PatternTemplatePredicate::is_valid_rewrite_pattern(path_template, path_template_rewrite).ok()) { throw EnvoyException( - fmt::format("mismatch between path_template_match {} and pattern_rewrite {}", + fmt::format("mismatch between path_template {} and path_template_rewrite {}", path_template, path_template_rewrite)); } - return policy; + return PatternTemplatePolicyImpl(path_template, path_template_rewrite);; } // only match if (!path_template.empty()) { - PatternTemplatePolicyImpl policy = - PatternTemplatePolicyImpl(path_template, path_template_rewrite); - if (!policy.predicate()->is_valid_match_pattern(path_template).ok()) { + if (!matching::PatternTemplatePredicate::is_valid_match_pattern(path_template).ok()) { throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); } - return policy; + return PatternTemplatePolicyImpl(path_template, path_template_rewrite);; } // no match + no rewrite diff --git a/source/extensions/url_template/url_template_matching.cc b/source/extensions/url_template/url_template_matching.cc index c7e186874c7f5..292124532a4f9 100644 --- a/source/extensions/url_template/url_template_matching.cc +++ b/source/extensions/url_template/url_template_matching.cc @@ -32,7 +32,7 @@ bool PatternTemplatePredicate::match(absl::string_view pattern) const { } absl::StatusOr -PatternTemplatePredicate::convertURLPatternSyntaxToRegex(absl::string_view url_pattern) const { +PatternTemplatePredicate::convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { absl::StatusOr status = url_template_matching_internal::parseURLPatternSyntax(url_pattern); @@ -44,7 +44,7 @@ PatternTemplatePredicate::convertURLPatternSyntaxToRegex(absl::string_view url_p } absl::StatusOr> -PatternTemplatePredicate::parseRewritePatternHelper(absl::string_view pattern) const { +PatternTemplatePredicate::parseRewritePatternHelper(absl::string_view pattern) { std::vector result; // Don't allow contiguous '/' patterns. @@ -89,7 +89,7 @@ PatternTemplatePredicate::parseRewritePatternHelper(absl::string_view pattern) c absl::StatusOr PatternTemplatePredicate::parseRewritePattern(absl::string_view pattern, - absl::string_view capture_regex) const { + absl::string_view capture_regex) { envoy::extensions::url_template::v3::RouteUrlRewritePattern parsed_pattern; RE2 regex = RE2(ToStringPiece(capture_regex)); if (!regex.ok()) { @@ -154,16 +154,16 @@ absl::StatusOr PatternTemplatePredicate::rewriteURLTemplatePattern( return rewritten_url; } -absl::Status PatternTemplatePredicate::isValidPathTemplateMatchPattern(const std::string& path_template_match) const { +absl::Status PatternTemplatePredicate::isValidPathTemplateMatchPattern(const std::string& path_template_match) { return convertURLPatternSyntaxToRegex(path_template_match).status(); } -absl::Status PatternTemplatePredicate::isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) const { +absl::Status PatternTemplatePredicate::isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { return parseRewritePatternHelper(path_template_rewrite).status(); } absl::Status PatternTemplatePredicate::isValidSharedVariableSet(const std::string& path_template_rewrite, - std::string& capture_regex) const { + std::string& capture_regex) { return parseRewritePattern(path_template_rewrite, capture_regex).status(); } @@ -194,25 +194,31 @@ absl::StatusOr PatternTemplatePredicate::rewritePattern(absl::strin return *std::move(new_path); } -absl::Status PatternTemplatePredicate::is_valid_match_pattern(std::string match_pattern) const { +absl::Status PatternTemplatePredicate::is_valid_match_pattern(std::string match_pattern) { return isValidPathTemplateMatchPattern(match_pattern); }; -absl::Status PatternTemplatePredicate::is_valid_rewrite_pattern(std::string match_pattern, std::string rewrite_pattern) const { - absl::StatusOr status = - convertURLPatternSyntaxToRegex(match_pattern); - if (!status.ok()) { - return absl::InvalidArgumentError(fmt::format("path_template {} is invalid", match_pattern)); - } +absl::Status PatternTemplatePredicate::is_valid_rewrite_pattern(std::string match_pattern, + std::string rewrite_pattern) { - std::string path_template_match_regex = *std::move(status); - if (path_template_match_regex.empty() || - !isValidSharedVariableSet(rewrite_pattern, path_template_match_regex).ok()) { - return absl::InvalidArgumentError(fmt::format("mismatch between path_template_match {} and pattern_rewrite {}", - match_pattern, rewrite_pattern)); - } + if (!PatternTemplatePredicate::isValidPathTemplateRewritePattern(rewrite_pattern).ok()) { + return absl::InvalidArgumentError(fmt::format("path_template_rewrite {} is invalid", match_pattern)); + } + + absl::StatusOr converted_pattern = convertURLPatternSyntaxToRegex(match_pattern); + if (!converted_pattern.ok()) { + return absl::InvalidArgumentError(fmt::format("path_template {} is invalid", match_pattern)); + } + + std::string path_template_match_regex = *std::move(converted_pattern); + if (path_template_match_regex.empty() || + !isValidSharedVariableSet(rewrite_pattern, path_template_match_regex).ok()) { + return absl::InvalidArgumentError( + fmt::format("mismatch between path_template {} and path_template_rewrite {}", match_pattern, + rewrite_pattern)); + } - return absl::OkStatus(); + return absl::OkStatus(); }; } // namespace matching diff --git a/source/extensions/url_template/url_template_matching.h b/source/extensions/url_template/url_template_matching.h index 0faa4a0420f08..db0f2a45b3eb4 100644 --- a/source/extensions/url_template/url_template_matching.h +++ b/source/extensions/url_template/url_template_matching.h @@ -28,7 +28,7 @@ class PatternTemplatePredicate : public Router::PatternTemplatePredicate { public: explicit PatternTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) : Router::PatternTemplatePredicate(url_pattern, url_rewrite_pattern), - matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())) {} + matching_pattern_regex_(RE2(PatternTemplatePredicate::convertURLPatternSyntaxToRegex(url_pattern).value())) {} PatternTemplatePredicate() = default; absl::string_view name() const override { return "envoy.url_template.pattern_template_predicates"; } @@ -37,31 +37,31 @@ class PatternTemplatePredicate : public Router::PatternTemplatePredicate { // Returns if the regex pattern matches the given regex from constructor. bool match(absl::string_view pattern) const override; - absl::Status is_valid_match_pattern(std::string match_pattern) const override; - - absl::Status is_valid_rewrite_pattern(std::string match_pattern, std::string rewrite_pattern) const override; - absl::StatusOr rewritePattern(absl::string_view current_pattern, absl::string_view matched_path) const override; + static absl::Status is_valid_match_pattern(std::string match_pattern); + + static absl::Status is_valid_rewrite_pattern(std::string match_pattern, std::string rewrite_pattern); + private: // Returns the regex pattern that is equivalent to the given url_pattern. // Used in the config pipeline to translate user given url pattern to // the safe regex Envoy can understand. Strips away any variable captures. - absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) const; + static absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); // Helper function that parses the pattern and breaks it down to either // literals or variable names. To be used by ParseRewritePattern(). // Exposed here so that the validator for the rewrite pattern can also // use it. - absl::StatusOr> parseRewritePatternHelper(absl::string_view pattern) const; + static absl::StatusOr> parseRewritePatternHelper(absl::string_view pattern); // Returns the parsed Url rewrite pattern to be used by // RewriteURLTemplatePattern() |capture_regex| should // be the regex generated by ConvertURLPatternSyntaxToRegex(). - absl::StatusOr - parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) const; + static absl::StatusOr + parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); // Returns the rewritten URL path based on the given parsed rewrite pattern. // Used for template-based URL rewrite. @@ -70,14 +70,14 @@ class PatternTemplatePredicate : public Router::PatternTemplatePredicate { const envoy::extensions::url_template::v3::RouteUrlRewritePattern& rewrite_pattern) const; // Returns if provided template match pattern is valid - absl::Status isValidPathTemplateMatchPattern(const std::string& path_template_match) const; + static absl::Status isValidPathTemplateMatchPattern(const std::string& path_template_match); // Returns if provided rewrite pattern is valid - absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) const; + static absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); // Returns if path_template and rewrite_template have valid variables - absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, - std::string& capture_regex) const; + static absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, + std::string& capture_regex); private: // move into library diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index eecf8ba9e3ccf..fb0b5c5acd51f 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -5078,7 +5078,7 @@ TEST_F(RouteMatcherTest, TestPrefixAndRegexRewrites) { EXPECT_THROW_WITH_MESSAGE( TestConfigImpl(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException, - "Cannot specify both prefix_rewrite and regex_rewrite"); + "Specify only one of prefix_rewrite, regex_rewrite or path_template_rewrite"); } TEST_F(RouteMatcherTest, TestPatternRewriteConfigLoad) { @@ -8907,6 +8907,372 @@ TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { ->clusterName()); } +TEST_F(RouteMatcherTest, MixedPathPatternMatch) { + + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{lang}/{state}" + case_sensitive: false + route: { cluster: path-pattern-cluster-one} + - match: + path_template: "/boo/{go}/{fly}/{bat}" + case_sensitive: false + route: { cluster: path-pattern-cluster-two} + - match: + path_template: "/foo/boo/{go}/{fly}/{bat}/{sno}" + case_sensitive: false + route: { cluster: path-pattern-cluster-three} + )EOF"; + + factory_context_.cluster_manager_.initializeClusters( + {"path-pattern-cluster-one", "path-pattern-cluster-two", "path-pattern-cluster-three"}, {}); + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + + // Pattern matches + EXPECT_EQ("path-pattern-cluster-one", + config.route(genHeaders("path.prefix.com", "/rest/english/wa", "GET"), 0) + ->routeEntry() + ->clusterName()); + EXPECT_EQ("path-pattern-cluster-one", + config.route(genHeaders("path.prefix.com", "/rest/spanish/mexico", "GET"), 0) + ->routeEntry() + ->clusterName()); + + EXPECT_EQ("path-pattern-cluster-two", + config.route(genHeaders("path.prefix.com", "/boo/go/fly/bat", "GET"), 0) + ->routeEntry() + ->clusterName()); + EXPECT_EQ("path-pattern-cluster-two", + config.route(genHeaders("path.prefix.com", "/boo/snow/flew/cone", "GET"), 0) + ->routeEntry() + ->clusterName()); + + EXPECT_EQ("path-pattern-cluster-three", + config.route(genHeaders("path.prefix.com", "/foo/boo/hat/bat/bat/sat", "GET"), 0) + ->routeEntry() + ->clusterName()); + EXPECT_EQ( + "path-pattern-cluster-three", + config.route(genHeaders("path.prefix.com", "/foo/boo/spanish/mexico/lisk/fisl", "GET"), 0) + ->routeEntry() + ->clusterName()); +} + +TEST_F(RouteMatcherTest, PatternMatchRewriteSimple) { + + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{one}/{two}" + case_sensitive: false + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/rest/{two}/{one}" + )EOF"; + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + + Http::TestRequestHeaderMapImpl headers = genHeaders("path.prefix.com", "/rest/one/two", "GET"); + const RouteEntry* route = config.route(headers, 0)->routeEntry(); + EXPECT_EQ("/rest/two/one", route->currentUrlPathAfterRewrite(headers)); + route->finalizeRequestHeaders(headers, stream_info, true); + EXPECT_EQ("/rest/two/one", headers.get_(Http::Headers::get().Path)); + EXPECT_EQ("path.prefix.com", headers.get_(Http::Headers::get().Host)); +} + +TEST_F(RouteMatcherTest, PatternMatchRewriteSimpleTwo) { + + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{one=*}/{two}" + case_sensitive: false + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/{two}/{one}" + )EOF"; + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + + Http::TestRequestHeaderMapImpl headers = genHeaders("path.prefix.com", "/rest/one/two", "GET"); + const RouteEntry* route = config.route(headers, 0)->routeEntry(); + EXPECT_EQ("/two/one", route->currentUrlPathAfterRewrite(headers)); + route->finalizeRequestHeaders(headers, stream_info, true); + EXPECT_EQ("/two/one", headers.get_(Http::Headers::get().Path)); + EXPECT_EQ("path.prefix.com", headers.get_(Http::Headers::get().Host)); +} + +TEST_F(RouteMatcherTest, PatternMatchRewriteCaseSensitive) { + + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{one}/{two}" + case_sensitive: true + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/{two}/{one}" + - match: + path_template: "/REST/{one}/{two}" + case_sensitive: true + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/TEST/{one}" + )EOF"; + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + + Http::TestRequestHeaderMapImpl headers = genHeaders("path.prefix.com", "/rest/one/two", "GET"); + const RouteEntry* route = config.route(headers, 0)->routeEntry(); + EXPECT_EQ("/two/one", route->currentUrlPathAfterRewrite(headers)); + route->finalizeRequestHeaders(headers, stream_info, true); + EXPECT_EQ("/two/one", headers.get_(Http::Headers::get().Path)); + EXPECT_EQ("path.prefix.com", headers.get_(Http::Headers::get().Host)); + + headers = genHeaders("path.prefix.com", "/REST/one/two", "GET"); + route = config.route(headers, 0)->routeEntry(); + EXPECT_EQ("/TEST/one", route->currentUrlPathAfterRewrite(headers)); + route->finalizeRequestHeaders(headers, stream_info, true); + EXPECT_EQ("/TEST/one", headers.get_(Http::Headers::get().Path)); + EXPECT_EQ("path.prefix.com", headers.get_(Http::Headers::get().Host)); +} + +TEST_F(RouteMatcherTest, PatternMatchConfigMissingBracket) { + + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{one/{two}" + case_sensitive: false + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/{two}/{one}" + )EOF"; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + + EXPECT_THROW_WITH_MESSAGE( + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), + EnvoyException, "path_template /rest/{one/{two} is invalid"); +} + +TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{one}/{two}" + case_sensitive: false + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/rest/{one}/{two}/{missing}" + )EOF"; + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + + EXPECT_THROW_WITH_MESSAGE( + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), + EnvoyException, + "mismatch between path_template /rest/{one}/{two} and path_template_rewrite " + "/rest/{one}/{two}/{missing}"); +} + +TEST_F(RouteMatcherTest, PatternMatchInvalidVariableName) { + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{on==e}/{two}" + case_sensitive: false + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/rest/{one}/{two}/{missing}" + )EOF"; + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + + EXPECT_THROW_WITH_MESSAGE( + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), + EnvoyException, "path_template /rest/{on==e}/{two} is invalid"); +} + +TEST_F(RouteMatcherTest, PatternMatchWildcardUnnamedVariable) { + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/*/{two}" + case_sensitive: false + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/{two}" + )EOF"; + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + + Http::TestRequestHeaderMapImpl headers = genHeaders("path.prefix.com", "/rest/one/two", "GET"); + const RouteEntry* route = config.route(headers, 0)->routeEntry(); + EXPECT_EQ("/two", route->currentUrlPathAfterRewrite(headers)); + route->finalizeRequestHeaders(headers, stream_info, true); + EXPECT_EQ("/two", headers.get_(Http::Headers::get().Path)); + EXPECT_EQ("path.prefix.com", headers.get_(Http::Headers::get().Host)); +} + +TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariable) { + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{one}/**" + case_sensitive: false + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/{one}" + )EOF"; + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + + Http::TestRequestHeaderMapImpl headers = + genHeaders("path.prefix.com", "/rest/one/two/three/four", "GET"); + const RouteEntry* route = config.route(headers, 0)->routeEntry(); + EXPECT_EQ("/one", route->currentUrlPathAfterRewrite(headers)); + route->finalizeRequestHeaders(headers, stream_info, true); + EXPECT_EQ("/one", headers.get_(Http::Headers::get().Path)); + EXPECT_EQ("path.prefix.com", headers.get_(Http::Headers::get().Host)); +} + +TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariableNamed) { + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{one=*}/{last=**}" + case_sensitive: false + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/{last}" + )EOF"; + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + + Http::TestRequestHeaderMapImpl headers = + genHeaders("path.prefix.com", "/rest/one/two/three/four", "GET"); + const RouteEntry* route = config.route(headers, 0)->routeEntry(); + EXPECT_EQ("/two/three/four", route->currentUrlPathAfterRewrite(headers)); + route->finalizeRequestHeaders(headers, stream_info, true); + EXPECT_EQ("/two/three/four", headers.get_(Http::Headers::get().Path)); + EXPECT_EQ("path.prefix.com", headers.get_(Http::Headers::get().Host)); +} + +TEST_F(RouteMatcherTest, PatternMatchWildcardMiddleVariableNamed) { + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{one}/{middle=videos/*}/end" + case_sensitive: false + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/{middle}" + )EOF"; + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + + Http::TestRequestHeaderMapImpl headers = + genHeaders("path.prefix.com", "/rest/one/videos/three/end", "GET"); + const RouteEntry* route = config.route(headers, 0)->routeEntry(); + EXPECT_EQ("/videos/three", route->currentUrlPathAfterRewrite(headers)); + route->finalizeRequestHeaders(headers, stream_info, true); + EXPECT_EQ("/videos/three", headers.get_(Http::Headers::get().Path)); + EXPECT_EQ("path.prefix.com", headers.get_(Http::Headers::get().Host)); +} + +TEST_F(RouteMatcherTest, PatternMatchCaseSensitiveVariableNames) { + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{one}/{One}/end" + case_sensitive: false + route: + cluster: "path-pattern-cluster-one" + path_template_rewrite: "/{One}/{one}" + )EOF"; + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); + + Http::TestRequestHeaderMapImpl headers = + genHeaders("path.prefix.com", "/rest/lower/upper/end", "GET"); + const RouteEntry* route = config.route(headers, 0)->routeEntry(); + EXPECT_EQ("/upper/lower", route->currentUrlPathAfterRewrite(headers)); + route->finalizeRequestHeaders(headers, stream_info, true); + EXPECT_EQ("/upper/lower", headers.get_(Http::Headers::get().Path)); + EXPECT_EQ("path.prefix.com", headers.get_(Http::Headers::get().Host)); +} + +TEST_F(RouteMatcherTest, PatternMatchCaseTooManyVariableNames) { + const std::string yaml = R"EOF( +virtual_hosts: + - name: path_pattern + domains: ["*"] + routes: + - match: + path_template: "/rest/{one}/{two}/{three}/{four}/{five}/{six}" + case_sensitive: false + route: + cluster: "path-pattern-cluster-one" + )EOF"; + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); + + EXPECT_THROW_WITH_MESSAGE( + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), + EnvoyException, + "path_template /rest/{one}/{two}/{three}/{four}/{five}/{six} is invalid"); +} + TEST_F(RouteConfigurationV2, DefaultInternalRedirectPolicyIsSensible) { const std::string yaml = R"EOF( virtual_hosts: From a6d8c43eb2d302631a9d31f84a70ac5fd47d7088 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 14 Jul 2022 19:00:48 +0000 Subject: [PATCH 014/238] Isolate pattern_template extension and rename to pattern_template Signed-off-by: silverstar195 --- api/BUILD | 1 + .../extensions/pattern_template/v3/BUILD | 12 + .../v3/pattern_template_rewrite.proto | 32 ++ envoy/router/BUILD | 13 + envoy/router/pattern_template.h | 55 ++ source/extensions/extensions_build_config.bzl | 5 + source/extensions/extensions_metadata.yaml | 5 + source/extensions/pattern_template/BUILD | 89 ++++ source/extensions/pattern_template/config.cc | 11 + source/extensions/pattern_template/config.h | 29 ++ .../pattern_template_matching.cc | 228 +++++++++ .../pattern_template_matching.h | 90 ++++ .../pattern_template_matching_internal.cc | 383 ++++++++++++++ .../pattern_template_matching_internal.h | 95 ++++ ...pattern_template_matching_internal_test.cc | 468 ++++++++++++++++++ .../pattern_template_matching_test.cc | 335 +++++++++++++ test/extensions/pattern_template/BUILD | 33 ++ .../pattern_template/config_test.cc | 42 ++ .../pattern_template/matcher_test.cc | 34 ++ tools/extensions/extensions_schema.yaml | 1 + 20 files changed, 1961 insertions(+) create mode 100644 api/envoy/extensions/pattern_template/v3/BUILD create mode 100644 api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto create mode 100644 envoy/router/pattern_template.h create mode 100644 source/extensions/pattern_template/BUILD create mode 100644 source/extensions/pattern_template/config.cc create mode 100644 source/extensions/pattern_template/config.h create mode 100644 source/extensions/pattern_template/pattern_template_matching.cc create mode 100644 source/extensions/pattern_template/pattern_template_matching.h create mode 100644 source/extensions/pattern_template/pattern_template_matching_internal.cc create mode 100644 source/extensions/pattern_template/pattern_template_matching_internal.h create mode 100644 source/extensions/pattern_template/pattern_template_matching_internal_test.cc create mode 100644 source/extensions/pattern_template/pattern_template_matching_test.cc create mode 100644 test/extensions/pattern_template/BUILD create mode 100644 test/extensions/pattern_template/config_test.cc create mode 100644 test/extensions/pattern_template/matcher_test.cc diff --git a/api/BUILD b/api/BUILD index 7099a7abee5da..9747ad2812864 100644 --- a/api/BUILD +++ b/api/BUILD @@ -239,6 +239,7 @@ proto_library( "//envoy/extensions/network/dns_resolver/apple/v3:pkg", "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", + "//envoy/extensions/pattern_template/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", diff --git a/api/envoy/extensions/pattern_template/v3/BUILD b/api/envoy/extensions/pattern_template/v3/BUILD new file mode 100644 index 0000000000000..1c1a6f6b44235 --- /dev/null +++ b/api/envoy/extensions/pattern_template/v3/BUILD @@ -0,0 +1,12 @@ +# DO NOT EDIT. This file is generated by tools/proto_format/proto_sync.py. + +load("@envoy_api//bazel:api_build_system.bzl", "api_proto_package") + +licenses(["notice"]) # Apache 2 + +api_proto_package( + deps = [ + "//envoy/config/core/v3:pkg", + "@com_github_cncf_udpa//udpa/annotations:pkg", + ], +) diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto new file mode 100644 index 0000000000000..60acf6f6a881b --- /dev/null +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto @@ -0,0 +1,32 @@ +syntax = "proto3"; + +package envoy.extensions.pattern_template.v3; + +import "udpa/annotations/status.proto"; + +option java_package = "io.envoyproxy.envoy.extensions.pattern_template.v3"; +option java_outer_classname = "PatternTemplateRewrite"; +option java_multiple_files = true; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;patterntemplaterewritev3"; +option (udpa.annotations.file_status).package_version_status = ACTIVE; + +// [#protodoc-title: Pattern matcher] +// [#extension: envoy.pattern_template] + +// Holds the segments for rewriting urls base on pattern templates +message PatternTemplateRewrite { + message RewriteSegment { + oneof segment_type { + // Represents a segment of the rewritten URL, including any path segments, + // slash and prefix. + string literal = 1; + + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int32 var_index = 2; + } + } + + repeated RewriteSegment segments = 3; +} \ No newline at end of file diff --git a/envoy/router/BUILD b/envoy/router/BUILD index 1ec10b92c4291..09839e4febe8b 100644 --- a/envoy/router/BUILD +++ b/envoy/router/BUILD @@ -64,6 +64,7 @@ envoy_cc_library( external_deps = ["abseil_optional"], deps = [ ":internal_redirect_interface", + ":router_pattern_template_interface", "//envoy/access_log:access_log_interface", "//envoy/common:conn_pool_interface", "//envoy/common:matchers_interface", @@ -144,3 +145,15 @@ envoy_cc_library( "//envoy/server:factory_context_interface", ], ) + +envoy_cc_library( + name = "router_pattern_template_interface", + hdrs = ["pattern_template.h"], + deps = [ + "//envoy/config:typed_config_interface", + "//source/common/common:minimal_logger_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@envoy_api//envoy/config/core/v3:pkg_cc_proto", + ], +) diff --git a/envoy/router/pattern_template.h b/envoy/router/pattern_template.h new file mode 100644 index 0000000000000..b00cba1360938 --- /dev/null +++ b/envoy/router/pattern_template.h @@ -0,0 +1,55 @@ +#pragma once + +#include "envoy/config/typed_config.h" +#include "source/common/common/logger.h" + +#include "absl/strings/string_view.h" +#include "absl/status/statusor.h" + +namespace Envoy { +namespace Router { + +/** + * Used to decide if an internal redirect is allowed to be followed based on the target route. + * Subclassing Logger::Loggable so that implementations can log details. + */ +class PatternTemplatePredicate : Logger::Loggable { +public: + PatternTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) : + url_pattern_(url_pattern), + url_rewrite_pattern_(url_rewrite_pattern) {}; + + PatternTemplatePredicate() = default; + + virtual ~PatternTemplatePredicate() = default; + + virtual absl::string_view name() const PURE; + virtual std::string category() const PURE; + + virtual bool match(absl::string_view pattern) const PURE; + + virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const PURE; + + const std::string url_pattern_; + const std::string url_rewrite_pattern_; +}; + +using PatternTemplatePredicateSharedPtr = std::shared_ptr; + +/** + * Factory for PatternTemplatePredicate. + */ +class PatternTemplatePredicateFactory : public Envoy::Config::TypedFactory { +public: + virtual ~PatternTemplatePredicateFactory() = default; + + + virtual PatternTemplatePredicateSharedPtr + createUrlTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) PURE; + + std::string category() const override { return "envoy.pattern_template"; } +}; + +} // namespace Router +} // namespace Envoy \ No newline at end of file diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index dc71b3db22a93..255c4b53cddd9 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -348,6 +348,11 @@ EXTENSIONS = { "envoy.matching.custom_matchers.trie_matcher": "//source/extensions/common/matcher:trie_matcher_lib", + # + # Pattern Template Matcher + # + "envoy.pattern_template.pattern_template_predicates": "//source/extensions/pattern_template:config", + # # Header Validators # diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index 9c24adf503959..ba9574c092152 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -734,6 +734,11 @@ envoy.matching.input_matchers.ip: - envoy.matching.input_matchers security_posture: robust_to_untrusted_downstream_and_upstream status: stable +envoy.pattern_template.pattern_template_predicate: + categories: + - envoy.pattern_template + security_posture: robust_to_untrusted_downstream_and_upstream + status: stable envoy.quic.proof_source.filter_chain: categories: - envoy.quic.proof_source diff --git a/source/extensions/pattern_template/BUILD b/source/extensions/pattern_template/BUILD new file mode 100644 index 0000000000000..39a1279614d7f --- /dev/null +++ b/source/extensions/pattern_template/BUILD @@ -0,0 +1,89 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_extension", + "envoy_cc_library", + "envoy_cc_test", + "envoy_extension_package", +) + +licenses(["notice"]) # Apache 2 + +# Wildcard & Pattern Matching + +envoy_extension_package() + +envoy_cc_library( + name = "pattern_template_matching", + srcs = ["pattern_template_matching.cc"], + hdrs = ["pattern_template_matching.h"], + visibility = [ + "//source/common/router:__subpackages__", + "//source/extensions/pattern_template:__subpackages__", + "//test/extensions/pattern_template:__subpackages__", + ], + deps = [ + ":pattern_template_matching_internal_cc", + "//envoy/router:router_pattern_template_interface", + "//source/common/http:path_utility_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_googlesource_code_re2//:re2", + "@envoy_api//envoy/extensions/pattern_template/v3:pkg_cc_proto", + ], +) + +envoy_cc_library( + name = "pattern_template_matching_internal_cc", + srcs = ["pattern_template_matching_internal.cc"], + hdrs = ["pattern_template_matching_internal.h"], + deps = [ + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/functional:function_ref", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + "@com_google_absl//absl/types:variant", + "@com_googlesource_code_re2//:re2", + ], +) + +envoy_cc_test( + name = "pattern_template_matching_test", + srcs = ["pattern_template_matching_test.cc"], + deps = [ + ":pattern_template_matching", + "//test/test_common:status_utility_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + ], +) + +envoy_cc_test( + name = "pattern_template_matching_internal_test", + srcs = ["pattern_template_matching_internal_test.cc"], + deps = [ + ":pattern_template_matching_internal_cc", + "//test/test_common:status_utility_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_googlesource_code_re2//:re2", + ], +) + +envoy_cc_extension( + name = "config", + srcs = ["config.cc"], + hdrs = ["config.h"], + visibility = ["//visibility:public"], + deps = [ + ":pattern_template_matching", + "//envoy/registry", + "//envoy/router:router_pattern_template_interface", + "@envoy_api//envoy/extensions/pattern_template/v3:pkg_cc_proto", + ], +) diff --git a/source/extensions/pattern_template/config.cc b/source/extensions/pattern_template/config.cc new file mode 100644 index 0000000000000..eaba166ec1139 --- /dev/null +++ b/source/extensions/pattern_template/config.cc @@ -0,0 +1,11 @@ +#include "source/extensions/pattern_template/config.h" + +#include "envoy/registry/registry.h" +#include "envoy/router/pattern_template.h" + +namespace Envoy { +namespace PatternTemplate { + +REGISTER_FACTORY(PatternTemplatePredicateFactory, Router::PatternTemplatePredicateFactory); +} // namespace matching +} // namespace Envoy \ No newline at end of file diff --git a/source/extensions/pattern_template/config.h b/source/extensions/pattern_template/config.h new file mode 100644 index 0000000000000..98b700b1f39e7 --- /dev/null +++ b/source/extensions/pattern_template/config.h @@ -0,0 +1,29 @@ +#pragma once + +#include "envoy/extensions/pattern_template/v3/pattern_template_rewrite.pb.h" +#include "envoy/router/pattern_template.h" + +#include "source/extensions/pattern_template/pattern_template_matching.h" + +namespace Envoy { +namespace PatternTemplate { + +class PatternTemplatePredicateFactory : public Router::PatternTemplatePredicateFactory { +public: + Router::PatternTemplatePredicateSharedPtr + createUrlTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) override { + return std::make_shared(url_pattern, url_rewrite_pattern); + } + + ProtobufTypes::MessagePtr createEmptyConfigProto() override { + return std::make_unique(); + } + + std::string name() const override { + return "envoy.pattern_template.pattern_template_predicate"; + } + +}; + +} // namespace matching +} // namespace Envoy \ No newline at end of file diff --git a/source/extensions/pattern_template/pattern_template_matching.cc b/source/extensions/pattern_template/pattern_template_matching.cc new file mode 100644 index 0000000000000..653975ed8b977 --- /dev/null +++ b/source/extensions/pattern_template/pattern_template_matching.cc @@ -0,0 +1,228 @@ +#include "source/extensions/pattern_template/pattern_template_matching.h" + +#include +#include +#include +#include + +#include "envoy/extensions/pattern_template/v3/pattern_template_rewrite.pb.h" + +#include "source/extensions/pattern_template/pattern_template_matching_internal.h" +#include "source/common/http/path_utility.h" + +#include "absl/status/statusor.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "re2/re2.h" + +namespace Envoy { +namespace PatternTemplate { + +using PatternTemplateInternal::ParsedUrlPattern; + +#ifndef SWIG +// Silence warnings about missing initializers for members of LazyRE2. +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.data(), text.size()}; } + +absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { + + absl::StatusOr status = + PatternTemplateInternal::parseURLPatternSyntax(url_pattern); + if (!status.ok()) { + return status.status(); + } + struct ParsedUrlPattern pattern = *std::move(status); + return PatternTemplateInternal::toRegexPattern(pattern); +} + +absl::StatusOr> +parseRewritePatternHelper(absl::string_view pattern) { + std::vector result; + + // Don't allow contiguous '/' patterns. + static const LazyRE2 invalid_regex = {"^.*//.*$"}; + if (RE2::FullMatch(ToStringPiece(pattern), *invalid_regex)) { + return absl::InvalidArgumentError("Invalid rewrite literal pattern"); + } + + // The pattern should start with a '/' and thus the first segment should + // always be a literal. + if (pattern.empty() || pattern[0] != '/') { + return absl::InvalidArgumentError("Invalid rewrite variable placement"); + } + while (!pattern.empty()) { + std::vector segments1 = absl::StrSplit(pattern, absl::MaxSplits('{', 1)); + if (!segments1[0].empty()) { + if (!PatternTemplateInternal::isValidRewriteLiteral(segments1[0])) { + return absl::InvalidArgumentError("Invalid rewrite literal pattern"); + } + result.emplace_back(segments1[0], RewriteStringKind::kLiteral); + } + + if (segments1.size() < 2) { + // No more variable replacement, done. + break; + } + + std::vector segments2 = + absl::StrSplit(segments1[1], absl::MaxSplits('}', 1)); + if (segments2.size() < 2) { + return absl::InvalidArgumentError("Unmatched variable bracket"); + } + pattern = segments2[1]; + + if (!PatternTemplateInternal::isValidIndent(segments2[0])) { + return absl::InvalidArgumentError("Invalid variable name"); + } + result.emplace_back(segments2[0], RewriteStringKind::kVariable); + } + return result; +} + +absl::StatusOr +parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { + envoy::extensions::pattern_template::v3::PatternTemplateRewrite parsed_pattern; + RE2 regex = RE2(ToStringPiece(capture_regex)); + if (!regex.ok()) { + return absl::InternalError(regex.error()); + } + + absl::StatusOr> status = parseRewritePatternHelper(pattern); + if (!status.ok()) { + return status.status(); + } + std::vector processed_pattern = *std::move(status); + + const std::map& capture_index_map = regex.NamedCapturingGroups(); + + for (const auto& [str, kind] : processed_pattern) { + switch (kind) { + case RewriteStringKind::kLiteral: + parsed_pattern.add_segments()->set_literal(std::string(str)); + break; + case RewriteStringKind::kVariable: + auto it = capture_index_map.find(std::string(str)); + if (it == capture_index_map.end()) { + return absl::InvalidArgumentError("Nonexisting variable name"); + } + parsed_pattern.add_segments()->set_var_index(it->second); + break; + } + } + + return parsed_pattern; +} + +absl::Status isValidPathTemplateMatchPattern(const std::string& path_template_match) { + return convertURLPatternSyntaxToRegex(path_template_match).status(); +} + +absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { + return parseRewritePatternHelper(path_template_rewrite).status(); +} + +absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, + std::string& capture_regex) { + return parseRewritePattern(path_template_rewrite, capture_regex).status(); +} + +absl::Status is_valid_match_pattern(std::string match_pattern) { + return isValidPathTemplateMatchPattern(match_pattern); +}; + +absl::Status PatternTemplatePredicate::is_valid_rewrite_pattern(std::string match_pattern, + std::string rewrite_pattern) { + + if (!isValidPathTemplateRewritePattern(rewrite_pattern).ok()) { + return absl::InvalidArgumentError( + fmt::format("path_template_rewrite {} is invalid", match_pattern)); + } + + absl::StatusOr converted_pattern = convertURLPatternSyntaxToRegex(match_pattern); + if (!converted_pattern.ok()) { + return absl::InvalidArgumentError(fmt::format("path_template {} is invalid", match_pattern)); + } + + std::string path_template_match_regex = *std::move(converted_pattern); + if (path_template_match_regex.empty() || + !isValidSharedVariableSet(rewrite_pattern, path_template_match_regex).ok()) { + return absl::InvalidArgumentError( + fmt::format("mismatch between path_template {} and path_template_rewrite {}", match_pattern, + rewrite_pattern)); + } + + return absl::OkStatus(); +}; + +bool PatternTemplatePredicate::match(absl::string_view pattern) const { + return RE2::FullMatch(ToStringPiece(Http::PathUtil::removeQueryAndFragment(pattern)), + matching_pattern_regex_); +} + +absl::StatusOr +PatternTemplatePredicate::rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const { + absl::StatusOr regex_pattern = convertURLPatternSyntaxToRegex(matched_path); + if (!regex_pattern.ok()) { + return absl::InvalidArgumentError("Unable to parse url pattern regex"); + } + std::string regex_pattern_str = *std::move(regex_pattern); + + absl::StatusOr rewrite_pattern = + parseRewritePattern(url_rewrite_pattern_, regex_pattern_str); + + if (!rewrite_pattern.ok()) { + return absl::InvalidArgumentError("Unable to parse url rewrite pattern"); + } + + envoy::extensions::pattern_template::v3::PatternTemplateRewrite rewrite_pattern_proto = + *std::move(rewrite_pattern); + + absl::StatusOr new_path = + rewriteURLTemplatePattern(current_pattern, regex_pattern_str, rewrite_pattern_proto); + + if (!new_path.ok()) { + return absl::InvalidArgumentError("Unable rewrite url to new URL"); + } + + return *std::move(new_path); +} + +absl::StatusOr PatternTemplatePredicate::rewriteURLTemplatePattern( + absl::string_view url, absl::string_view capture_regex, + const envoy::extensions::pattern_template::v3::PatternTemplateRewrite& rewrite_pattern) const { + RE2 regex = RE2(ToStringPiece(capture_regex)); + if (!regex.ok()) { + return absl::InternalError(regex.error()); + } + + // First capture is the whole matched regex pattern. + int capture_num = regex.NumberOfCapturingGroups() + 1; + std::vector captures(capture_num); + if (!regex.Match(ToStringPiece(url), /*startpos=*/0, /*endpos=*/url.size(), RE2::ANCHOR_BOTH, + captures.data(), captures.size())) { + return absl::InvalidArgumentError("Pattern not match"); + } + + std::string rewritten_url; + + for (const envoy::extensions::pattern_template::v3::PatternTemplateRewrite::RewriteSegment& + segment : rewrite_pattern.segments()) { + if (segment.has_literal()) { + absl::StrAppend(&rewritten_url, segment.literal()); + } else if (segment.has_var_index()) { + if (segment.var_index() < 1 || segment.var_index() >= capture_num) { + return absl::InvalidArgumentError("Invalid variable index"); + } + absl::StrAppend(&rewritten_url, absl::string_view(captures[segment.var_index()].as_string())); + } + } + + return rewritten_url; +} + +} // namespace PatternTemplate +} // namespace Envoy diff --git a/source/extensions/pattern_template/pattern_template_matching.h b/source/extensions/pattern_template/pattern_template_matching.h new file mode 100644 index 0000000000000..22bf678c1cd72 --- /dev/null +++ b/source/extensions/pattern_template/pattern_template_matching.h @@ -0,0 +1,90 @@ +#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCHING_H +#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCHING_H + +#include + +#include "envoy/extensions/pattern_template/v3/pattern_template_rewrite.pb.h" + +#include "source/extensions/pattern_template/pattern_template_matching_internal.h" + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "envoy/router/pattern_template.h" + +namespace Envoy { +namespace PatternTemplate { + +enum class RewriteStringKind { kVariable, kLiteral }; + +struct RewritePatternSegment { + RewritePatternSegment(absl::string_view str, RewriteStringKind kind) : str(str), kind(kind) {} + absl::string_view str; + RewriteStringKind kind; +}; + +// Returns the regex pattern that is equivalent to the given url_pattern. +// Used in the config pipeline to translate user given url pattern to +// the safe regex Envoy can understand. Strips away any variable captures. +absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); + +// Helper function that parses the pattern and breaks it down to either +// literals or variable names. To be used by ParseRewritePattern(). +// Exposed here so that the validator for the rewrite pattern can also +// use it. +absl::StatusOr> +parseRewritePatternHelper(absl::string_view pattern); + +// Returns the parsed Url rewrite pattern to be used by +// RewriteURLTemplatePattern() |capture_regex| should +// be the regex generated by ConvertURLPatternSyntaxToRegex(). +absl::StatusOr +parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); + +// Returns if provided template match pattern is valid +absl::Status isValidPathTemplateMatchPattern(const std::string& path_template_match); + +// Returns if provided rewrite pattern is valid +absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); + +// Returns if path_template and rewrite_template have valid variables +absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, + std::string& capture_regex); + +// Holds actions to validate, match, and rewrite template pattern based urls. +class PatternTemplatePredicate : public Router::PatternTemplatePredicate { +public: + explicit PatternTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) + : Router::PatternTemplatePredicate(url_pattern, url_rewrite_pattern), + matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())) {} + PatternTemplatePredicate() = default; + + absl::string_view name() const override { + return "envoy.pattern_template.pattern_template_predicates"; + } + std::string category() const override { return "envoy.pattern_template"; } + + // Returns if the regex pattern matches the given regex from constructor. + bool match(absl::string_view pattern) const override; + + absl::StatusOr rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const override; + + static absl::Status is_valid_match_pattern(std::string match_pattern); + + static absl::Status is_valid_rewrite_pattern(std::string match_pattern, + std::string rewrite_pattern); + +private: + // Returns the rewritten URL path based on the given parsed rewrite pattern. + // Used for template-based URL rewrite. + absl::StatusOr rewriteURLTemplatePattern( + absl::string_view url, absl::string_view capture_regex, + const envoy::extensions::pattern_template::v3::PatternTemplateRewrite& rewrite_pattern) const; + + RE2 matching_pattern_regex_{nullptr}; +}; + +} // namespace PatternTemplate +} // namespace Envoy + +#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCHING_H diff --git a/source/extensions/pattern_template/pattern_template_matching_internal.cc b/source/extensions/pattern_template/pattern_template_matching_internal.cc new file mode 100644 index 0000000000000..b987674480260 --- /dev/null +++ b/source/extensions/pattern_template/pattern_template_matching_internal.cc @@ -0,0 +1,383 @@ +#include "source/extensions/pattern_template/pattern_template_matching_internal.h" + +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/flags/flag.h" +#include "absl/functional/function_ref.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_replace.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "absl/types/variant.h" +#include "re2/re2.h" + +namespace Envoy { +namespace PatternTemplate { + +namespace PatternTemplateInternal { + +namespace { + +#ifndef SWIG +// Silence warnings about missing initializers for members of LazyRE2. +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +unsigned long pattern_matching_max_variables_per_url = 5; +unsigned long pattern_matching_max_variable_name_len = 16; +unsigned long pattern_matching_min_variable_name_len = 1; + +// Valid pchar from https://datatracker.ietf.org/doc/html/rfc3986#appendix-A +constexpr absl::string_view kLiteral = "a-zA-Z0-9-._~" // Unreserved + "%" // pct-encoded + "!$&'()+,;" // sub-delims excluding *= + ":@"; + +// Default operator used for the variable when none specified. +constexpr Operator kDefaultVariableOperator = Operator::kPathGlob; + +// Visitor for displaying debug info of a ParsedSegment/Variable.var_match. +struct ToStringVisitor { + template std::string operator()(const T& val) const; +}; + +// Formatter used to allow joining variants together with StrJoin. +struct ToStringFormatter { + template void operator()(std::string* out, const T& t) const { + absl::StrAppend(out, absl::visit(ToStringVisitor(), t)); + } +}; + +// Visitor for converting a ParsedSegment variant to the regex. +struct ToRegexPatternVisitor { + template std::string operator()(const T& val) const { return toRegexPattern(val); } +}; + +// Formatter used to allow joining variants together with StrJoin. +struct ToRegexPatternFormatter { + template void operator()(std::string* out, const T& t) const { + absl::StrAppend(out, absl::visit(ToRegexPatternVisitor(), t)); + } +}; + +std::string toString(const Literal val) { return std::string(val); } + +std::string toString(const Operator val) { + switch (val) { + case Operator::kPathGlob: + return "*"; + case Operator::kTextGlob: + return "**"; + } +} + +std::string toString(const Variable val) { + if (val.var_match.empty()) { + return absl::StrCat("{", val.var_name, "}"); + } + + return absl::StrCat("{", val.var_name, "=", + absl::StrJoin(val.var_match, "/", ToStringFormatter()), "}"); +} + +template std::string ToStringVisitor::operator()(const T& val) const { + return toString(val); +} + +template +absl::StatusOr AlsoUpdatePattern( + absl::FunctionRef>(absl::string_view)> consume_func, + absl::string_view* patt) { + + absl::StatusOr> status = consume_func(*patt); + if (!status.ok()) { + return status.status(); + } + ParsedResult result = *std::move(status); + + *patt = result.unconsumed_pattern; + return result.parsed_value; +} + +} // namespace + +std::string Variable::DebugString() const { return toString(*this); } + +std::string ParsedUrlPattern::DebugString() const { + return absl::StrCat("/", absl::StrJoin(parsed_segments, "/", ToStringFormatter()), + suffix.value_or("")); +} + +bool isValidLiteral(absl::string_view pattern) { + static const std::string* kValidLiteralRegex = + new std::string(absl::StrCat("^[", kLiteral, "]+$")); + static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; + return RE2::FullMatch(toStringPiece(pattern), *literal_regex); +} + +bool isValidRewriteLiteral(absl::string_view pattern) { + static const std::string* kValidLiteralRegex = + new std::string(absl::StrCat("^[", kLiteral, "/]+$")); + static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; + return RE2::FullMatch(toStringPiece(pattern), *literal_regex); +} + +bool isValidIndent(absl::string_view pattern) { + static const LazyRE2 ident_regex = {"^[a-zA-Z][a-zA-Z0-9_]*$"}; + return RE2::FullMatch(toStringPiece(pattern), *ident_regex); +} + +absl::StatusOr> consumeLiteral(absl::string_view pattern) { + absl::string_view lit = + std::vector(absl::StrSplit(pattern, absl::MaxSplits('/', 1)))[0]; + absl::string_view unconsumed_pattern = pattern.substr(lit.size()); + if (!isValidLiteral(lit)) { + return absl::InvalidArgumentError("Invalid literal"); + } + return ParsedResult(lit, unconsumed_pattern); +} + +absl::StatusOr> consumeOperator(absl::string_view pattern) { + if (absl::StartsWith(pattern, "**")) { + return ParsedResult(Operator::kTextGlob, pattern.substr(2)); + } + if (absl::StartsWith(pattern, "*")) { + return ParsedResult(Operator::kPathGlob, pattern.substr(1)); + } + return absl::InvalidArgumentError("Invalid Operator"); +} + +absl::StatusOr> consumeVariable(absl::string_view pattern) { + // Locate the variable pattern to parse. + if (pattern.size() < 2 || (pattern)[0] != '{') { + return absl::InvalidArgumentError("Invalid variable"); + } + std::vector parts = absl::StrSplit(pattern.substr(1), absl::MaxSplits('}', 1)); + if (parts.size() != 2) { + return absl::InvalidArgumentError("Unmatched variable bracket"); + } + absl::string_view unconsumed_pattern = parts[1]; + + // Parse the actual variable pattern, starting with the variable name. + std::vector var_parts = absl::StrSplit(parts[0], absl::MaxSplits('=', 1)); + if (!isValidIndent(var_parts[0])) { + return absl::InvalidArgumentError("Invalid variable name"); + } + Variable var = Variable(var_parts[0], {}); + + // Parse the variable match pattern (if any). + if (var_parts.size() < 2) { + return ParsedResult(var, unconsumed_pattern); + } + absl::string_view var_patt = var_parts[1]; + if (var_patt.empty()) { + return absl::InvalidArgumentError("Empty variable match"); + } + while (!var_patt.empty()) { + absl::variant var_match; + if (var_patt[0] == '*') { + + absl::StatusOr status = AlsoUpdatePattern(consumeOperator, &var_patt); + if (!status.ok()) { + return status.status(); + } + var_match = *std::move(status); + + } else { + + absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &var_patt); + if (!status.ok()) { + return status.status(); + } + var_match = *std::move(status); + } + var.var_match.push_back(var_match); + if (!var_patt.empty()) { + if (var_patt[0] != '/' || var_patt.size() == 1) { + return absl::InvalidArgumentError("Invalid variable match"); + } + var_patt = var_patt.substr(1); + } + } + + return ParsedResult(var, unconsumed_pattern); +} + +absl::StatusOr> +gatherCaptureNames(struct ParsedUrlPattern pattern) { + absl::flat_hash_set captured_variables; + + for (const ParsedSegment& segment : pattern.parsed_segments) { + if (!absl::holds_alternative(segment)) { + continue; + } + if (captured_variables.size() >= pattern_matching_max_variables_per_url) { + return absl::InvalidArgumentError("Exceeded variable count limit"); + } + absl::string_view var_name = absl::get(segment).var_name; + + if (var_name.size() < pattern_matching_min_variable_name_len || + var_name.size() > pattern_matching_max_variable_name_len) { + return absl::InvalidArgumentError("Invalid variable length"); + } + if (captured_variables.contains(var_name)) { + return absl::InvalidArgumentError("Repeated variable name"); + } + captured_variables.emplace(var_name); + } + + return captured_variables; +} + +absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { + bool seen_text_glob = false; + for (const ParsedSegment& segment : pattern.parsed_segments) { + if (absl::holds_alternative(segment)) { + if (seen_text_glob) { + return absl::InvalidArgumentError("Glob after text glob."); + } + seen_text_glob = (absl::get(segment) == Operator::kTextGlob); + } else if (absl::holds_alternative(segment)) { + const Variable& var = absl::get(segment); + if (var.var_match.empty()) { + if (seen_text_glob) { + // A variable with no explicit matcher is treated as a path glob. + return absl::InvalidArgumentError("Implicit variable path glob after text glob."); + } + } else { + for (const absl::variant& var_seg : var.var_match) { + if (!absl::holds_alternative(var_seg)) { + continue; + } + if (seen_text_glob) { + return absl::InvalidArgumentError("Glob after text glob."); + } + seen_text_glob = (absl::get(var_seg) == Operator::kTextGlob); + } + } + } + } + return absl::OkStatus(); +} + +absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern) { + struct ParsedUrlPattern parsed_pattern; + + static const LazyRE2 printable_regex = {"^/[[:graph:]]*$"}; + if (!RE2::FullMatch(toStringPiece(url_pattern), *printable_regex)) { + + return absl::InvalidArgumentError("Invalid pattern"); + } + + // Consume the leading '/' + url_pattern = url_pattern.substr(1); + + // Do the initial lexical parsing. + while (!url_pattern.empty()) { + ParsedSegment segment; + if (url_pattern[0] == '*') { + + absl::StatusOr status = AlsoUpdatePattern(consumeOperator, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } else if (url_pattern[0] == '{') { + + absl::StatusOr status = AlsoUpdatePattern(consumeVariable, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } else { + + absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } + parsed_pattern.parsed_segments.push_back(segment); + + // Deal with trailing '/' or suffix. + if (!url_pattern.empty()) { + if (url_pattern == "/") { + // Single trailing '/' at the end, mark this with empty literal. + parsed_pattern.parsed_segments.emplace_back(""); + break; + } else if (url_pattern[0] == '/') { + // Have '/' followed by more text, consume the '/'. + url_pattern = url_pattern.substr(1); + } else { + // Not followed by '/', treat as suffix. + + absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &url_pattern); + if (!status.ok()) { + return status.status(); + } + parsed_pattern.suffix = *std::move(status); + if (!url_pattern.empty()) { + // Suffix didn't consume whole remaining pattern ('/' in url_pattern). + return absl::InvalidArgumentError("Prefix match not supported."); + } + break; + } + } + } + absl::StatusOr> status = + gatherCaptureNames(parsed_pattern); + if (!status.ok()) { + return status.status(); + } + parsed_pattern.captured_variables = *std::move(status); + + absl::Status validate_status = validateNoOperatorAfterTextGlob(parsed_pattern); + if (!validate_status.ok()) { + return validate_status; + } + + return parsed_pattern; +} + +std::string toRegexPattern(absl::string_view pattern) { + return absl::StrReplaceAll( + pattern, {{"$", "\\$"}, {"(", "\\("}, {")", "\\)"}, {"+", "\\+"}, {".", "\\."}}); +} + +std::string toRegexPattern(Operator pattern) { + static const std::string* kPathGlobRegex = new std::string(absl::StrCat("[", kLiteral, "]+")); + static const std::string* kTextGlobRegex = new std::string(absl::StrCat("[", kLiteral, "/]*")); + switch (pattern) { + case Operator::kPathGlob: // "*" + return *kPathGlobRegex; + case Operator::kTextGlob: // "**" + return *kTextGlobRegex; + } +} + +std::string toRegexPattern(const Variable& pattern) { + return absl::StrCat("(?P<", pattern.var_name, ">", + pattern.var_match.empty() + ? toRegexPattern(kDefaultVariableOperator) + : absl::StrJoin(pattern.var_match, "/", ToRegexPatternFormatter()), + ")"); +} + +std::string toRegexPattern(const struct ParsedUrlPattern& pattern) { + return absl::StrCat("/", absl::StrJoin(pattern.parsed_segments, "/", ToRegexPatternFormatter()), + toRegexPattern(pattern.suffix.value_or(""))); +} + +} // namespace PatternTemplateInternal + +} // namespace PatternTemplate +} // namespace Envoy diff --git a/source/extensions/pattern_template/pattern_template_matching_internal.h b/source/extensions/pattern_template/pattern_template_matching_internal.h new file mode 100644 index 0000000000000..be4c27090a0ba --- /dev/null +++ b/source/extensions/pattern_template/pattern_template_matching_internal.h @@ -0,0 +1,95 @@ +#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_MATCHING_INTERNAL_H +#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_MATCHING_INTERNAL_H + +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "absl/types/optional.h" +#include "absl/types/variant.h" +#include "re2/re2.h" + +namespace Envoy { +namespace PatternTemplate { + +namespace PatternTemplateInternal { + +using Literal = absl::string_view; +enum class Operator { kPathGlob, kTextGlob }; + +struct RewriteSegment { + // Represents a segment of the rewritten URL, including any path segments, + // slash and prefix. + absl::string_view literal; + + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int var_index; +}; + +struct Variable { + absl::string_view var_name; + std::vector> var_match; + + Variable(absl::string_view name, std::vector> match) + : var_name(name), var_match(match) {} + + std::string DebugString() const; +}; + +using ParsedSegment = absl::variant; + +struct ParsedUrlPattern { + std::vector parsed_segments; + absl::optional suffix; + absl::flat_hash_set captured_variables; + + std::string DebugString() const; +}; + +bool isValidLiteral(absl::string_view pattern); + +bool isValidRewriteLiteral(absl::string_view pattern); + +bool isValidIndent(absl::string_view pattern); + +// Used by the following Consume{Literal.Operator,Variable} functions +// in the return value. The functions would take the given pattern, +// parse what it can into |parsed_value| and return the unconsumed +// portion of the pattern in |unconsumed_pattern|. +template struct ParsedResult { + ParsedResult(T val, absl::string_view pattern) : parsed_value(val), unconsumed_pattern(pattern) {} + + T parsed_value; + absl::string_view unconsumed_pattern; +}; + +absl::StatusOr> consumeLiteral(absl::string_view pattern); + +absl::StatusOr> consumeOperator(absl::string_view pattern); + +absl::StatusOr> consumeVariable(absl::string_view pattern); + +absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern); + +std::string toRegexPattern(Literal pattern); + +std::string toRegexPattern(Operator pattern); + +std::string toRegexPattern(const Variable& pattern); + +std::string toRegexPattern(const struct ParsedUrlPattern& pattern); + +inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } + +} // namespace PatternTemplateInternal + +} // namespace PatternTemplate +} // namespace Envoy + +#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_MATCHING_INTERNAL_H diff --git a/source/extensions/pattern_template/pattern_template_matching_internal_test.cc b/source/extensions/pattern_template/pattern_template_matching_internal_test.cc new file mode 100644 index 0000000000000..38a0b3081bfee --- /dev/null +++ b/source/extensions/pattern_template/pattern_template_matching_internal_test.cc @@ -0,0 +1,468 @@ +#include +#include +#include +#include +#include +#include + +#include "source/common/common/assert.h" +#include "source/extensions/pattern_template/pattern_template_matching_internal.h" +#include "source/common/protobuf/protobuf.h" + +#include "test/test_common/logging.h" +#include "test/test_common/status_utility.h" +#include "test/test_common/utility.h" + +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "gtest/gtest.h" +#include "re2/re2.h" + +namespace Envoy { +namespace PatternTemplate { + +namespace PatternTemplateInternal { + +namespace { + +using ::Envoy::StatusHelpers::StatusIs; + +TEST(InternalParsing, ParsedUrlDebugString) { + ParsedUrlPattern patt1 = { + { + "abc", + "def", + Operator::kPathGlob, + Variable("var", {Operator::kPathGlob, "ghi", Operator::kTextGlob}), + }, + ".test", + {}, + }; + EXPECT_EQ(patt1.DebugString(), "/abc/def/*/{var=*/ghi/**}.test"); + + ParsedUrlPattern patt2 = {{ + Variable("var", {}), + }, + "", + {}}; + EXPECT_EQ(patt2.DebugString(), "/{var}"); +} + +TEST(InternalParsing, isValidLiteralWorks) { + EXPECT_TRUE(isValidLiteral("123abcABC")); + EXPECT_TRUE(isValidLiteral("._~-")); + EXPECT_TRUE(isValidLiteral("-._~%20!$&'()+,;:@")); + EXPECT_FALSE(isValidLiteral("`~!@#$%^&()-_+;:,<.>'\"\\| ")); + EXPECT_FALSE(isValidLiteral("abc/")); + EXPECT_FALSE(isValidLiteral("ab*c")); + EXPECT_FALSE(isValidLiteral("a**c")); + EXPECT_FALSE(isValidLiteral("a=c")); + EXPECT_FALSE(isValidLiteral("?abc")); + EXPECT_FALSE(isValidLiteral("?a=c")); + EXPECT_FALSE(isValidLiteral("{abc")); + EXPECT_FALSE(isValidLiteral("abc}")); + EXPECT_FALSE(isValidLiteral("{abc}")); +} + +TEST(InternalParsing, isValidRewriteLiteralWorks) { + EXPECT_TRUE(isValidRewriteLiteral("123abcABC")); + EXPECT_TRUE(isValidRewriteLiteral("abc/")); + EXPECT_TRUE(isValidRewriteLiteral("abc/def")); + EXPECT_TRUE(isValidRewriteLiteral("/abc.def")); + EXPECT_TRUE(isValidRewriteLiteral("._~-")); + EXPECT_TRUE(isValidRewriteLiteral("-._~%20!$&'()+,;:@")); + EXPECT_FALSE(isValidRewriteLiteral("`~!@#$%^&()-_+;:,<.>'\"| ")); + EXPECT_FALSE(isValidRewriteLiteral("ab}c")); + EXPECT_FALSE(isValidRewriteLiteral("ab{c")); + EXPECT_FALSE(isValidRewriteLiteral("a=c")); + EXPECT_FALSE(isValidRewriteLiteral("?a=c")); +} + +TEST(InternalParsing, isValidIdentWorks) { + EXPECT_TRUE(isValidIndent("abc")); + EXPECT_TRUE(isValidIndent("ABC_def_123")); + EXPECT_TRUE(isValidIndent("a1")); + EXPECT_TRUE(isValidIndent("T")); + EXPECT_FALSE(isValidIndent("123")); + EXPECT_FALSE(isValidIndent("__undefined__")); + EXPECT_FALSE(isValidIndent("abc-def")); + EXPECT_FALSE(isValidIndent("abc=def")); + EXPECT_FALSE(isValidIndent("abc def")); + EXPECT_FALSE(isValidIndent("a!!!")); +} + +TEST(InternalParsing, ConsumeLiteralWorks) { + std::string pattern = "abc/123"; + + absl::StatusOr> result = consumeLiteral(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, "abc"); + EXPECT_EQ(result->unconsumed_pattern, "/123"); +} + +TEST(InternalParsing, ConsumeTextGlob) { + std::string pattern = "***abc/123"; + + absl::StatusOr> result = consumeOperator(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, Operator::kTextGlob); + EXPECT_EQ(result->unconsumed_pattern, "*abc/123"); +} + +TEST(InternalParsing, ConsumePathGlob) { + std::string pattern = "*/123"; + + absl::StatusOr> result = consumeOperator(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, Operator::kPathGlob); + EXPECT_EQ(result->unconsumed_pattern, "/123"); +} + +class ConsumeVariableSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ConsumeVariableSuccessTestSuite, ConsumeVariableSuccess, + testing::Values("{var=*}", "{Var}", "{v1=**}", "{v_1=*/abc/**}", + "{v3=abc}", "{v=123/*/*}", "{var=abc/*/def}")); + +TEST_P(ConsumeVariableSuccess, ConsumeVariableSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + absl::StatusOr> result = consumeVariable(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value.DebugString(), pattern); + EXPECT_TRUE(result->unconsumed_pattern.empty()); +} + +class ConsumeVariableFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ConsumeVariableFailureTestSuite, ConsumeVariableFailure, + testing::Values("{var", "{=abc}", "{_var=*}", "{1v}", "{1v=abc}", + "{var=***}", "{v-a-r}", "{var=*/abc?q=1}", "{var=abc/a*}", + "{var=*def/abc}", "{var=}", "{var=abc=def}", + "{rc=||||(A+yl/}")); + +TEST_P(ConsumeVariableFailure, ConsumeVariableFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(consumeVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class parseURLPatternSyntaxSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P( + parseURLPatternSyntaxSuccessTestSuite, parseURLPatternSyntaxSuccess, + testing::Values("/**.m3u8", "/**.mpd", "/*_suf", "/{path=**}.m3u8", "/{foo}/**.ts", + "/media/*.m4s", "/media/{contentId=*}/**", "/media/*", "/api/*/*/**", + "/api/*/v1/**", "/api/*/v1/*", "/{version=api/*}/*", "/api/*/*/", + "/api/*/1234/", "/api/*/{resource=*}/{method=*}", + "/api/*/{resource=*}/{method=**}", "/v1/**", "/media/{country}/{lang=*}/**", + "/{foo}/{bar}/{fo}/{fum}/*", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", + "/media/{id=*}/*", "/media/{contentId=**}", + "/api/{version}/projects/{project}/locations/{location}/{resource}/" + "{name}", + "/api/{version=*}/{url=**}", "/api/{VERSION}/{version}/{verSION}", + "/api/1234/abcd", "/media/abcd/%10%20%30/{v1=*/%10%20}_suffix", "/")); + +TEST_P(parseURLPatternSyntaxSuccess, parseURLPatternSyntaxSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + absl::StatusOr parsed_patt = parseURLPatternSyntax(pattern); + ASSERT_OK(parsed_patt); + EXPECT_EQ(parsed_patt->DebugString(), pattern); +} + +class parseURLPatternSyntaxFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P( + parseURLPatternSyntaxFailureTestSuite, parseURLPatternSyntaxFailure, + testing::Values("/api/v*/1234", "/api/{version=v*}/1234", "/api/v{versionNum=*}/1234", + "/api/{version=*beta}/1234", "/media/eff456/ll-sd-out.{ext}", + "/media/eff456/ll-sd-out.{ext=*}", "/media/eff456/ll-sd-out.**", + "/media/{country=**}/{lang=*}/**", "/media/**/*/**", "/link/{id=*}/asset*", + "/link/{id=*}/{asset=asset*}", "/media/{id=/*}/*", "/media/{contentId=/**}", + "/api/{version}/{version}", "/api/{version.major}/{version.minor}", + "/media/***", "/media/*{*}*", "/media/{*}/", "/media/*/index?a=2", "media", + "/\001\002\003\004\005\006\007", "/*(/**", "/**/{var}", + "/{var1}/{var2}/{var3}/{var4}/{var5}/{var6}", "/{=*}", + "/{var12345678901234=*}")); + +TEST_P(parseURLPatternSyntaxFailure, parseURLPatternSyntaxFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(parseURLPatternSyntax(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(InternalRegexGen, LiteralEscapes) { + EXPECT_EQ(toRegexPattern("abcABC123/-._~%20!$&'()+,;:@"), + "abcABC123/-\\._~%20!\\$&'\\(\\)\\+,;:@"); +} + +TEST(InternalRegexGen, LiteralMatches) { + absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; + + EXPECT_TRUE(RE2::FullMatch(toStringPiece(kPattern), toRegexPattern(kPattern))); +} + +TEST(InternalRegexGen, LiteralMatchesInNamedCapture) { + absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; + + RE2 regex = RE2(absl::StrCat("(?P", toRegexPattern(kPattern), ")")); + ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); + + // Full matched string + capture groups + std::vector captures(2); + ASSERT_TRUE(regex.Match(toStringPiece(kPattern), /*startpos=*/0, /*endpos=*/kPattern.size(), + RE2::ANCHOR_BOTH, captures.data(), captures.size())); + + // Index 0 would be the full text of the matched string. + EXPECT_EQ(toStringPiece(kPattern), captures[0]); + // Get the pattern matched with the named capture group. + EXPECT_EQ(toStringPiece(kPattern), captures.at(regex.NamedCapturingGroups().at("var"))); +} + +TEST(InternalRegexGen, LiteralOnlyMatchesItself) { + constexpr absl::string_view kChars = "abcABC123/-._~%20!$&'()+,;:@"; + + for (const char c : kChars) { + std::string s = {'z', c, 'z'}; + EXPECT_TRUE(RE2::FullMatch(s, toRegexPattern(s))); + EXPECT_FALSE(RE2::FullMatch("zzz", toRegexPattern(s))); + } +} + +TEST(InternalRegexGen, RegexLikePatternIsMatchedLiterally) { + EXPECT_TRUE(RE2::FullMatch("(abc)", toRegexPattern("(abc)"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); + + EXPECT_TRUE(RE2::FullMatch("(abc)+", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("abcabc", toRegexPattern("(abc)+"))); + + EXPECT_TRUE(RE2::FullMatch(".+", toRegexPattern(".+"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern(".+"))); + + EXPECT_TRUE(RE2::FullMatch("a+", toRegexPattern("a+"))); + EXPECT_FALSE(RE2::FullMatch("aa", toRegexPattern("a+"))); +} + +TEST(InternalRegexGen, DollarSignMatchesIfself) { + EXPECT_TRUE(RE2::FullMatch("abc$", toRegexPattern("abc$"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("abc$"))); +} + +TEST(InternalRegexGen, OperatorRegexPattern) { + EXPECT_EQ(toRegexPattern(Operator::kPathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); + EXPECT_EQ(toRegexPattern(Operator::kTextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); +} + +TEST(InternalRegexGen, PathGlobRegex) { + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kPathGlob))); +} + +TEST(InternalRegexGen, TextGlobRegex) { + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kTextGlob))); + EXPECT_TRUE(RE2::FullMatch("", toRegexPattern(Operator::kTextGlob))); + EXPECT_TRUE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kTextGlob))); +} + +TEST(InternalRegexGen, VariableRegexPattern) { + EXPECT_EQ(toRegexPattern(Variable("var1", {})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"); + EXPECT_EQ(toRegexPattern(Variable("var2", {Operator::kPathGlob, "abc", Operator::kTextGlob})), + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+/abc/" + "[a-zA-Z0-9-._~%!$&'()+,;:@/]*)"); +} + +TEST(InternalRegexGen, VariableRegexDefaultMatch) { + absl::StatusOr> var = consumeVariable("{var}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc", toRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc"); +} + +TEST(InternalRegexGen, VariableRegexDefaultNotMatch) { + absl::StatusOr> var = consumeVariable("{var}"); + ASSERT_OK(var); + + EXPECT_FALSE(RE2::FullMatch("abc/def", toRegexPattern(var->parsed_value))); +} + +TEST(InternalRegexGen, VariableRegexSegmentsMatch) { + absl::StatusOr> var = consumeVariable("{var=abc/*/def}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc/123/def"); +} + +TEST(InternalRegexGen, VariableRegexTextGlobMatch) { + absl::StatusOr> var = consumeVariable("{var=**/def}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc/123/def"); +} + +TEST(InternalRegexGen, VariableRegexNamedCapture) { + re2::StringPiece kPattern = "abc"; + absl::StatusOr> var = consumeVariable("{var=*}"); + ASSERT_OK(var); + + RE2 regex = RE2(toRegexPattern(var->parsed_value)); + ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); + + // Full matched string + capture groups + std::vector captures(2); + ASSERT_TRUE(regex.Match(kPattern, /*startpos=*/0, /*endpos=*/kPattern.size(), RE2::ANCHOR_BOTH, + captures.data(), captures.size())); + + // Index 0 would be the full text of the matched string. + EXPECT_EQ(kPattern, captures[0]); + // Get the pattern matched with the named capture group. + EXPECT_EQ(kPattern, captures.at(regex.NamedCapturingGroups().at("var"))); +} + +TEST(InternalRegexGen, ParsedURLPatternToRegex) { + absl::StatusOr pattern = + parseURLPatternSyntax("/abc/*/{var1}/def/{var2=*/ghi/**}.jkl"); + ASSERT_OK(pattern); + + std::string var1_capture; + std::string var2_capture; + EXPECT_TRUE(RE2::FullMatch("/abc/123/456/def/789/ghi/%20/($).jkl", + toRegexPattern(pattern.value()), &var1_capture, &var2_capture)); + EXPECT_EQ(var1_capture, "456"); + EXPECT_EQ(var2_capture, "789/ghi/%20/($)"); +} + +struct GenPatternTestCase { + GenPatternTestCase(std::string request_path, std::string url_pattern, + std::vector> capture_pairs) + : path(request_path), pattern(url_pattern), captures(capture_pairs) {} + std::string path; + std::string pattern; + std::vector> captures; +}; + +class GenPatternRegexWithMatch : public testing::TestWithParam { +protected: + const std::string& request_path() const { return GetParam().path; } + const std::string& url_pattern() const { return GetParam().pattern; } + std::vector> const var_values() { + return GetParam().captures; + } +}; + +INSTANTIATE_TEST_SUITE_P( + GenPatternRegexWithMatchTestSuite, GenPatternRegexWithMatch, + testing::Values( + GenPatternTestCase("/media/1234/manifest.m3u8", "/**.m3u8", {}), + GenPatternTestCase("/manifest.mpd", "/**.mpd", {}), + GenPatternTestCase("/media/1234/manifest.m3u8", "/{path=**}.m3u8", + {{"path", "media/1234/manifest"}}), + GenPatternTestCase("/foo/12314341/format/123/hls/segment_0000000001.ts", "/{foo}/**.ts", + {{"foo", "foo"}}), + GenPatternTestCase("/media/eff456/ll-sd-out.js", "/media/{contentId=*}/**", + {{"contentId", "eff456"}}), + GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/*/**", {}), + GenPatternTestCase("/api/v1/1234", "/{version=api/*}/*", {{"version", "api/v1"}}), + GenPatternTestCase("/api/v1/1234/", "/api/*/*/", {}), + GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=**}", + {{"resource", "1234"}, {"method", "broadcasts/get"}}), + GenPatternTestCase("/v1/broadcasts/12345/live", "/v1/**", {}), + GenPatternTestCase("/media/us/en/12334/subtitle_enUS_00101.vtt", + "/media/{country}/{lang=*}/**", {{"country", "us"}, {"lang", "en"}}), + GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo}/{bar}/{fo}/{fum}/*", + {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), + GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", + {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), + GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{id=*}/**", {{"id", "1234"}}), + GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{contentId=**}", + {{"contentId", "1234/hls/1001011.m3u8"}}), + GenPatternTestCase("/api/v1/projects/my-project/locations/global/edgeCacheOrigins/foo", + "/api/{version}/projects/{project}/locations/{location}/{resource}/" + "{name}", + {{"version", "v1"}, + {"project", "my-project"}, + {"location", "global"}, + {"resource", "edgeCacheOrigins"}, + {"name", "foo"}}), + GenPatternTestCase("/api/v1/foo/bar/baz/", "/api/{version=*}/{url=**}", + {{"version", "v1"}, {"url", "foo/bar/baz/"}}), + GenPatternTestCase("/api/v1/v2/v3", "/api/{VERSION}/{version}/{verSION}", + {{"VERSION", "v1"}, {"version", "v2"}, {"verSION", "v3"}}))); + +TEST_P(GenPatternRegexWithMatch, WithCapture) { + absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); + ASSERT_OK(pattern); + + RE2 regex = RE2(toRegexPattern(pattern.value())); + ASSERT_TRUE(regex.ok()) << regex.error(); + ASSERT_EQ(regex.NumberOfCapturingGroups(), var_values().size()); + + int capture_num = regex.NumberOfCapturingGroups() + 1; + std::vector captures(capture_num); + ASSERT_TRUE(regex.Match(request_path(), /*startpos=*/0, + /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, captures.data(), + captures.size())); + + EXPECT_EQ(captures[0], toStringPiece(request_path())); + + for (const auto& [name, value] : var_values()) { + int capture_index = regex.NamedCapturingGroups().at(name); + ASSERT_GE(capture_index, 0); + EXPECT_EQ(captures.at(capture_index), value); + } +} + +class GenPatternRegexWithoutMatch + : public testing::TestWithParam> { +protected: + const std::string& request_path() const { return std::get<0>(GetParam()); } + const std::string& url_pattern() const { return std::get<1>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWithoutMatch, + testing::ValuesIn(std::vector>( + {{"/media/12345/f/123/s00002.m4s", "/media/*.m4s"}, + {"/media/eff456/ll-sd-out.js", "/media/*"}, + {"/api/v1/1234/", "/api/*/v1/*"}, + {"/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=*}"}, + {"/api/v1/1234/", "/api/*/v1/**"}, + {"/api/*/1234/", "/api/*/1234/"}}))); + +TEST_P(GenPatternRegexWithoutMatch, WithCapture) { + absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); + ASSERT_OK(pattern); + + RE2 regex = RE2(toRegexPattern(pattern.value())); + ASSERT_TRUE(regex.ok()) << regex.error(); + + EXPECT_FALSE(regex.Match(request_path(), /*startpos=*/0, + /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, nullptr, 0)); +} + +} // namespace +} // namespace url_template_matching_internal +} // namespace matching +} // namespace Envoy diff --git a/source/extensions/pattern_template/pattern_template_matching_test.cc b/source/extensions/pattern_template/pattern_template_matching_test.cc new file mode 100644 index 0000000000000..27524e2f8775d --- /dev/null +++ b/source/extensions/pattern_template/pattern_template_matching_test.cc @@ -0,0 +1,335 @@ +#include +#include +#include + +#include "source/common/common/assert.h" +#include "source/extensions/pattern_template/pattern_template_matching.h" +#include "source/extensions/pattern_template/pattern_template_matching_internal.h" +#include "source/common/protobuf/protobuf.h" + +#include "test/test_common/logging.h" +#include "test/test_common/status_utility.h" +#include "test/test_common/utility.h" + +#include "gtest/gtest.h" + +namespace Envoy { +namespace PatternTemplate { + +namespace { + +using ::Envoy::StatusHelpers::IsOkAndHolds; +using ::Envoy::StatusHelpers::StatusIs; + +// Capture regex for /{var1}/{var2}/{var3}/{var4}/{var5} +static constexpr absl::string_view kCaptureRegex = "/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"; +static constexpr absl::string_view kMatchUrl = "/val1/val2/val3/val4/val5"; + +TEST(ConvertURLPattern, ValidPattern) { + EXPECT_THAT(convertURLPatternSyntaxToRegex("/abc"), IsOkAndHolds("/abc")); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/**.mpd"), + IsOkAndHolds("/[a-zA-Z0-9-._~%!$&'()+,;:@/]*\\.mpd")); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/*/{resource=*}/{method=**}"), + IsOkAndHolds("/api/[a-zA-Z0-9-._~%!$&'()+,;:@]+/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@/]*)")); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/{VERSION}/{version}/{verSION}"), + IsOkAndHolds("/api/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)")); +} + +TEST(ConvertURLPattern, InvalidPattern) { + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/v*/1234"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/media/**/*/**"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/\001\002\003\004\005\006\007"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/{var12345678901234=*}"), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class ParseRewriteHelperSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperSuccessTestSuite, ParseRewriteHelperSuccess, + testing::Values("/{var1}", "/{var1}{var2}", "/{var1}-{var2}", + "/abc/{var1}/def", "/{var1}/abd/{var2}", + "/abc-def-{var1}/a/{var1}")); + +TEST_P(ParseRewriteHelperSuccess, ParseRewriteHelperSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_OK(parseRewritePatternHelper(pattern)); +} + +class ParseRewriteHelperFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperFailureTestSuite, ParseRewriteHelperFailure, + testing::Values("{var1}", "/{{var1}}", "/}va1{", "var1}", + "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); + +TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(parseRewritePatternHelper(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class ParseRewriteSuccess : public testing::TestWithParam> { +protected: + const std::string& rewrite_pattern() const { return std::get<0>(GetParam()); } + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern expected_proto() const { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern expected_proto; + Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); + return expected_proto; + } +}; + +TEST(ParseRewrite, InvalidRegex) { + EXPECT_THAT(parseRewritePattern("/{var1}", "+[abc"), StatusIs(absl::StatusCode::kInternal)); +} + +INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, + testing::ValuesIn(std::vector>({ + {"/static", R"EOF(segments: {literal: "/static"} )EOF"}, + {"/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1)EOF"}, + {"/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1)EOF"}, + {"/{var1}/{var1}/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1)EOF"}, + {"/{var3}/{var1}/{var2}", R"EOF(segments + - literal: "/" + - var_index: 3 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 2)EOF"}, + {"/{var3}/abc/def/{var2}.suffix", R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/abc/def/" + - var_index: 2 + - literal: ".suffix")EOF"}, + {"/abc/{var1}/{var2}/def", R"EOF(segments + - literal: "/abc/" + - var_index: 1 + - literal: "/" + - var_index: 2 + - literal: "/def")EOF"}, + {"/{var1}{var2}", R"EOF(segments + - literal: "/" + - var_index: 1 + - ar_index: 2)EOF"}, + {"/{var1}-{var2}/bucket-{var3}.suffix", R"EOF(segments + - literal: "/" + - var_index: 1 + - literal: "-" + - var_index: 2 + - literal: "/bucket-" + - var_index: 3 + - literal: ".suffix")EOF"}, + }))); + +TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { + absl::StatusOr rewrite = + parseRewritePattern(rewrite_pattern(), kCaptureRegex); + ASSERT_OK(rewrite); + // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); +} + +class ParseRewriteFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteFailureTestSuite, ParseRewriteFailure, + testing::Values("{var1}", "/{var6}", "/{{var1}}", "/}va1{", "var1}", + "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); + +TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(parseRewritePattern(pattern, kCaptureRegex), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class RewriteUrlTemplateSuccess + : public testing::TestWithParam> { +protected: + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto() const { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern proto; + Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); + return proto; + } + const std::string& expected_rewritten_url() const { return std::get<1>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateSuccess, + testing::ValuesIn(std::vector>( + {{R"EOF(segments: { literal: "/static" })EOF", "/static"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1)EOF", + "/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1)EOF", + "/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1)EOF", + "/val1/val1/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 2)EOF", + "/val3/val1/val2"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/abc/def/" + - var_index: 2 + - literal: ".suffix")EOF", + "/val3/abc/def/val2.suffix"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - var_index: 2 + - literal: "." + - var_index: 1)EOF", + "/val3val2.val1"}, + {R"EOF(segments: + - literal: "/abc/" + - var_index: 1 + - literal: "/" + - var_index: 5 + - literal: "/def")EOF", + "/abc/val1/val5/def"}}))); + +TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { + absl::StatusOr rewritten_url = + RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto()); + ASSERT_OK(rewritten_url); + EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); +} + +TEST(RewriteUrlTemplateFailure, BadRegex) { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 1 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "+/bad_regex", rewrite_proto), + StatusIs(absl::StatusCode::kInternal)); +} + +TEST(RewriteUrlTemplateFailure, RegexNoMatch) { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 1 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "/no_match_regex", rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 0 + )EOF"; + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 6 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class URLPatternMatchAndRewrite + : public testing::TestWithParam< + std::tuple> { +protected: + const std::string& url_pattern() const { return std::get<0>(GetParam()); } + const std::string& rewrite_pattern() const { return std::get<1>(GetParam()); } + const std::string& match_url() const { return std::get<2>(GetParam()); } + const std::string& expected_rewritten_url() const { return std::get<3>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P( + URLPatternMatchAndRewriteTestSuite, URLPatternMatchAndRewrite, + testing::ValuesIn(std::vector>( + {{"/api/users/{id}/{path=**}", "/users/{id}/{path}", "/api/users/21334/profile.json", + "/users/21334/profile.json"}, + {"/videos/*/{id}/{format}/{rendition}/{segment=**}.ts", + "/{id}/{format}/{rendition}/{segment}.ts", "/videos/lib/132939/hls/13/segment_00001.ts", + "/132939/hls/13/segment_00001.ts"}, + {"/region/{region}/bucket/{name}/{method=**}", "/{region}/bucket-{name}/{method}", + "/region/eu/bucket/prod-storage/object.pdf", "/eu/bucket-prod-storage/object.pdf"}, + {"/region/{region}/bucket/{name}/{method=**}", "/{region}{name}/{method}", + "/region/eu/bucket/prod-storage/object.pdf", "/euprod-storage/object.pdf"}}))); + +TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { + absl::StatusOr regex = convertURLPatternSyntaxToRegex(url_pattern()); + ASSERT_OK(regex); + + absl::StatusOr rewrite_proto = + parseRewritePattern(rewrite_pattern(), regex.value()); + ASSERT_OK(rewrite_proto); + + absl::StatusOr rewritten_url = + RewriteURLTemplatePattern(match_url(), regex.value(), rewrite_proto.value()); + ASSERT_OK(rewritten_url); + + EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); +} + +} // namespace + +} // namespace matching +} // namespace Envoy diff --git a/test/extensions/pattern_template/BUILD b/test/extensions/pattern_template/BUILD new file mode 100644 index 0000000000000..482abd2423976 --- /dev/null +++ b/test/extensions/pattern_template/BUILD @@ -0,0 +1,33 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_package", +) +load( + "//test/extensions:extensions_build_system.bzl", + "envoy_extension_cc_test", +) + +licenses(["notice"]) # Apache 2 + +envoy_package() + +envoy_extension_cc_test( + name = "config_test", + srcs = ["config_test.cc"], + extension_names = ["envoy.url_template"], + deps = [ + "//source/common/stream_info:filter_state_lib", + "//source/extensions/pattern_template:config", + "//test/mocks/server:factory_context_mocks", + "@envoy_api//envoy/extensions/pattern_template/v3:pkg_cc_proto", + ], +) + +envoy_extension_cc_test( + name = "matcher_test", + srcs = ["matcher_test.cc"], + extension_names = ["envoy.url_template"], + deps = [ + "//source/extensions/pattern_template:pattern_template_matching", + ], +) diff --git a/test/extensions/pattern_template/config_test.cc b/test/extensions/pattern_template/config_test.cc new file mode 100644 index 0000000000000..9a64226568811 --- /dev/null +++ b/test/extensions/pattern_template/config_test.cc @@ -0,0 +1,42 @@ +#include "envoy/registry/registry.h" +#include "envoy/router/pattern_template.h" + +#include "source/common/stream_info/filter_state_impl.h" +#include "source/extensions/pattern_template/config.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using namespace testing; + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace { + +class PatternTemplateConfigTest : public testing::Test { +protected: + PatternTemplateConfigTest() : filter_state_(StreamInfo::FilterState::LifeSpan::FilterChain) { + factory_ = Registry::FactoryRegistry::getFactory( + "envoy.pattern_template.pattern_template_predicate"); + config_ = factory_->createEmptyConfigProto(); + } + + StreamInfo::FilterStateImpl filter_state_; + Router::PatternTemplatePredicateFactory* factory_; + ProtobufTypes::MessagePtr config_; +}; + +TEST_F(PatternTemplateConfigTest, EmptyCreation) { + std::string current_route_name = "fake_current_route"; + // Create the predicate for the first time. + { + auto predicate = factory_->createPatternTemplatePredicate("/url_pattern/{TEST}", "rewrite_pattern"); + ASSERT(predicate); + } +} + +} // namespace +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/test/extensions/pattern_template/matcher_test.cc b/test/extensions/pattern_template/matcher_test.cc new file mode 100644 index 0000000000000..ae5404b230db1 --- /dev/null +++ b/test/extensions/pattern_template/matcher_test.cc @@ -0,0 +1,34 @@ +#include "source/extensions/pattern_template/pattern_template_matching.h" + +#include "gtest/gtest.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +TEST(PatternTemplate, RouteMatcher) { + matching::UrlTemplatePredicate matcher("/foo/{lang}/{country}", "rewrite"); + + EXPECT_TRUE(matcher.match("/foo/english/us")); + EXPECT_TRUE(matcher.match("/foo/spanish/spain")); + EXPECT_TRUE(matcher.match("/foo/french/france")); + + // with params + EXPECT_TRUE(matcher.match("/foo/english/us#fragment")); + EXPECT_TRUE(matcher.match("/foo/spanish/spain#fragment?param=val")); + EXPECT_TRUE(matcher.match("/foo/french/france?param=regex")); + + EXPECT_FALSE(matcher.match("/foo/english/us/goat")); + EXPECT_FALSE(matcher.match("/foo/goat")); + EXPECT_FALSE(matcher.match("/foo")); + EXPECT_FALSE(matcher.match("")); + + // with params + EXPECT_FALSE(matcher.match("/foo/english/us/goat#fragment?param=val")); + EXPECT_FALSE(matcher.match("/foo/goat?param=regex")); + EXPECT_FALSE(matcher.match("/foo?param=regex")); +} + +} // namespace UrlTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/tools/extensions/extensions_schema.yaml b/tools/extensions/extensions_schema.yaml index b85d3f1db07d8..4d2083c0cdcf5 100644 --- a/tools/extensions/extensions_schema.yaml +++ b/tools/extensions/extensions_schema.yaml @@ -73,6 +73,7 @@ categories: - envoy.http.header_validators - envoy.http.stateful_header_formatters - envoy.internal_redirect_predicates +- envoy.pattern_template - envoy.io_socket - envoy.http.original_ip_detection - envoy.matching.common_inputs From 9aa0d4a8d08667a37980cb566d4634c9e31a56fd Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 14 Jul 2022 19:16:24 +0000 Subject: [PATCH 015/238] Document change Signed-off-by: silverstar195 --- envoy/router/pattern_template.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/envoy/router/pattern_template.h b/envoy/router/pattern_template.h index b00cba1360938..30b11d310bc6d 100644 --- a/envoy/router/pattern_template.h +++ b/envoy/router/pattern_template.h @@ -10,7 +10,7 @@ namespace Envoy { namespace Router { /** - * Used to decide if an internal redirect is allowed to be followed based on the target route. + * Used to decide if pattern template match or rewrite is needed based on the target route. * Subclassing Logger::Loggable so that implementations can log details. */ class PatternTemplatePredicate : Logger::Loggable { From 316ff04c3f85586a9fd71e18ebb2b1b8015719a2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 14 Jul 2022 19:18:43 +0000 Subject: [PATCH 016/238] Document change Signed-off-by: silverstar195 --- source/extensions/extensions_build_config.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 255c4b53cddd9..9b83803b49cfc 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -349,7 +349,7 @@ EXTENSIONS = { "envoy.matching.custom_matchers.trie_matcher": "//source/extensions/common/matcher:trie_matcher_lib", # - # Pattern Template Matcher + # Pattern Template # "envoy.pattern_template.pattern_template_predicates": "//source/extensions/pattern_template:config", From f7670a1342e76e53fe111a63e7345c58c2314e22 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 14 Jul 2022 20:16:39 +0000 Subject: [PATCH 017/238] Style and lint fixes Signed-off-by: silverstar195 --- api/envoy/extensions/pattern_template/v3/BUILD | 5 +---- .../pattern_template/v3/pattern_template_rewrite.proto | 6 +++--- api/versioning/BUILD | 1 + envoy/router/BUILD | 1 - .../pattern_template_matching_internal_test.cc | 10 +++++----- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/api/envoy/extensions/pattern_template/v3/BUILD b/api/envoy/extensions/pattern_template/v3/BUILD index 1c1a6f6b44235..ee92fb652582e 100644 --- a/api/envoy/extensions/pattern_template/v3/BUILD +++ b/api/envoy/extensions/pattern_template/v3/BUILD @@ -5,8 +5,5 @@ load("@envoy_api//bazel:api_build_system.bzl", "api_proto_package") licenses(["notice"]) # Apache 2 api_proto_package( - deps = [ - "//envoy/config/core/v3:pkg", - "@com_github_cncf_udpa//udpa/annotations:pkg", - ], + deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], ) diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto index 60acf6f6a881b..731a8d39555c4 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto @@ -5,9 +5,9 @@ package envoy.extensions.pattern_template.v3; import "udpa/annotations/status.proto"; option java_package = "io.envoyproxy.envoy.extensions.pattern_template.v3"; -option java_outer_classname = "PatternTemplateRewrite"; +option java_outer_classname = "PatternTemplateRewriteProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;patterntemplaterewritev3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;pattern_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Pattern matcher] @@ -29,4 +29,4 @@ message PatternTemplateRewrite { } repeated RewriteSegment segments = 3; -} \ No newline at end of file +} diff --git a/api/versioning/BUILD b/api/versioning/BUILD index e394bb9198234..069cf8fb9bc4e 100644 --- a/api/versioning/BUILD +++ b/api/versioning/BUILD @@ -182,6 +182,7 @@ proto_library( "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", + "//envoy/extensions/pattern_template/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", diff --git a/envoy/router/BUILD b/envoy/router/BUILD index 09839e4febe8b..ffa3c7ae20a8c 100644 --- a/envoy/router/BUILD +++ b/envoy/router/BUILD @@ -154,6 +154,5 @@ envoy_cc_library( "//source/common/common:minimal_logger_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", - "@envoy_api//envoy/config/core/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/pattern_template/pattern_template_matching_internal_test.cc b/source/extensions/pattern_template/pattern_template_matching_internal_test.cc index 38a0b3081bfee..446bc042f7498 100644 --- a/source/extensions/pattern_template/pattern_template_matching_internal_test.cc +++ b/source/extensions/pattern_template/pattern_template_matching_internal_test.cc @@ -6,8 +6,8 @@ #include #include "source/common/common/assert.h" -#include "source/extensions/pattern_template/pattern_template_matching_internal.h" #include "source/common/protobuf/protobuf.h" +#include "source/extensions/pattern_template/pattern_template_matching_internal.h" #include "test/test_common/logging.h" #include "test/test_common/status_utility.h" @@ -64,7 +64,7 @@ TEST(InternalParsing, isValidLiteralWorks) { EXPECT_FALSE(isValidLiteral("{abc}")); } -TEST(InternalParsing, isValidRewriteLiteralWorks) { +TEST(InternalParsing, IsValidRewriteLiteralWorks) { EXPECT_TRUE(isValidRewriteLiteral("123abcABC")); EXPECT_TRUE(isValidRewriteLiteral("abc/")); EXPECT_TRUE(isValidRewriteLiteral("abc/def")); @@ -78,7 +78,7 @@ TEST(InternalParsing, isValidRewriteLiteralWorks) { EXPECT_FALSE(isValidRewriteLiteral("?a=c")); } -TEST(InternalParsing, isValidIdentWorks) { +TEST(InternalParsing, IsValidIndentWorks) { EXPECT_TRUE(isValidIndent("abc")); EXPECT_TRUE(isValidIndent("ABC_def_123")); EXPECT_TRUE(isValidIndent("a1")); @@ -463,6 +463,6 @@ TEST_P(GenPatternRegexWithoutMatch, WithCapture) { } } // namespace -} // namespace url_template_matching_internal -} // namespace matching +} // namespace PatternTemplateInternal +} // namespace PatternTemplate } // namespace Envoy From 029e9ee6cebed2033d8d07cc8c2f9313b24db9ac Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 14 Jul 2022 20:19:37 +0000 Subject: [PATCH 018/238] Document change Signed-off-by: silverstar195 --- envoy/router/pattern_template.h | 13 ++++++------- source/extensions/pattern_template/config.cc | 2 +- source/extensions/pattern_template/config.h | 7 ++----- .../pattern_template/pattern_template_matching.cc | 2 +- .../pattern_template/pattern_template_matching.h | 4 ++-- .../pattern_template_matching_internal_test.cc | 6 +++--- .../pattern_template_matching_test.cc | 4 ++-- test/extensions/pattern_template/BUILD | 1 - test/extensions/pattern_template/config_test.cc | 3 ++- test/extensions/pattern_template/matcher_test.cc | 2 +- 10 files changed, 20 insertions(+), 24 deletions(-) diff --git a/envoy/router/pattern_template.h b/envoy/router/pattern_template.h index 30b11d310bc6d..e3f2dce511733 100644 --- a/envoy/router/pattern_template.h +++ b/envoy/router/pattern_template.h @@ -1,10 +1,11 @@ #pragma once #include "envoy/config/typed_config.h" + #include "source/common/common/logger.h" -#include "absl/strings/string_view.h" #include "absl/status/statusor.h" +#include "absl/strings/string_view.h" namespace Envoy { namespace Router { @@ -15,9 +16,8 @@ namespace Router { */ class PatternTemplatePredicate : Logger::Loggable { public: - PatternTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) : - url_pattern_(url_pattern), - url_rewrite_pattern_(url_rewrite_pattern) {}; + PatternTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) + : url_pattern_(url_pattern), url_rewrite_pattern_(url_rewrite_pattern){}; PatternTemplatePredicate() = default; @@ -29,7 +29,7 @@ class PatternTemplatePredicate : Logger::Loggable { virtual bool match(absl::string_view pattern) const PURE; virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, - absl::string_view matched_path) const PURE; + absl::string_view matched_path) const PURE; const std::string url_pattern_; const std::string url_rewrite_pattern_; @@ -40,11 +40,10 @@ using PatternTemplatePredicateSharedPtr = std::shared_ptr(); } - std::string name() const override { - return "envoy.pattern_template.pattern_template_predicate"; - } - + std::string name() const override { return "envoy.pattern_template.pattern_template_predicate"; } }; -} // namespace matching +} // namespace PatternTemplate } // namespace Envoy \ No newline at end of file diff --git a/source/extensions/pattern_template/pattern_template_matching.cc b/source/extensions/pattern_template/pattern_template_matching.cc index 653975ed8b977..5ad755990f44f 100644 --- a/source/extensions/pattern_template/pattern_template_matching.cc +++ b/source/extensions/pattern_template/pattern_template_matching.cc @@ -7,8 +7,8 @@ #include "envoy/extensions/pattern_template/v3/pattern_template_rewrite.pb.h" -#include "source/extensions/pattern_template/pattern_template_matching_internal.h" #include "source/common/http/path_utility.h" +#include "source/extensions/pattern_template/pattern_template_matching_internal.h" #include "absl/status/statusor.h" #include "absl/strings/str_split.h" diff --git a/source/extensions/pattern_template/pattern_template_matching.h b/source/extensions/pattern_template/pattern_template_matching.h index 22bf678c1cd72..14fa338c46877 100644 --- a/source/extensions/pattern_template/pattern_template_matching.h +++ b/source/extensions/pattern_template/pattern_template_matching.h @@ -4,12 +4,12 @@ #include #include "envoy/extensions/pattern_template/v3/pattern_template_rewrite.pb.h" +#include "envoy/router/pattern_template.h" #include "source/extensions/pattern_template/pattern_template_matching_internal.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" -#include "envoy/router/pattern_template.h" namespace Envoy { namespace PatternTemplate { @@ -80,7 +80,7 @@ class PatternTemplatePredicate : public Router::PatternTemplatePredicate { absl::StatusOr rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, const envoy::extensions::pattern_template::v3::PatternTemplateRewrite& rewrite_pattern) const; - + RE2 matching_pattern_regex_{nullptr}; }; diff --git a/source/extensions/pattern_template/pattern_template_matching_internal_test.cc b/source/extensions/pattern_template/pattern_template_matching_internal_test.cc index 446bc042f7498..daee2d7038b2e 100644 --- a/source/extensions/pattern_template/pattern_template_matching_internal_test.cc +++ b/source/extensions/pattern_template/pattern_template_matching_internal_test.cc @@ -169,7 +169,7 @@ INSTANTIATE_TEST_SUITE_P( "/api/{version=*}/{url=**}", "/api/{VERSION}/{version}/{verSION}", "/api/1234/abcd", "/media/abcd/%10%20%30/{v1=*/%10%20}_suffix", "/")); -TEST_P(parseURLPatternSyntaxSuccess, parseURLPatternSyntaxSuccessTest) { +TEST_P(ParseURLPatternSyntaxSuccess, ParseURLPatternSyntaxSuccessTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); @@ -181,7 +181,7 @@ TEST_P(parseURLPatternSyntaxSuccess, parseURLPatternSyntaxSuccessTest) { class parseURLPatternSyntaxFailure : public testing::TestWithParam {}; INSTANTIATE_TEST_SUITE_P( - parseURLPatternSyntaxFailureTestSuite, parseURLPatternSyntaxFailure, + ParseURLPatternSyntaxFailureTestSuite, ParseURLPatternSyntaxFailure, testing::Values("/api/v*/1234", "/api/{version=v*}/1234", "/api/v{versionNum=*}/1234", "/api/{version=*beta}/1234", "/media/eff456/ll-sd-out.{ext}", "/media/eff456/ll-sd-out.{ext=*}", "/media/eff456/ll-sd-out.**", @@ -193,7 +193,7 @@ INSTANTIATE_TEST_SUITE_P( "/{var1}/{var2}/{var3}/{var4}/{var5}/{var6}", "/{=*}", "/{var12345678901234=*}")); -TEST_P(parseURLPatternSyntaxFailure, parseURLPatternSyntaxFailureTest) { +TEST_P(ParseURLPatternSyntaxFailure, ParseURLPatternSyntaxFailureTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); diff --git a/source/extensions/pattern_template/pattern_template_matching_test.cc b/source/extensions/pattern_template/pattern_template_matching_test.cc index 27524e2f8775d..a847d2f471385 100644 --- a/source/extensions/pattern_template/pattern_template_matching_test.cc +++ b/source/extensions/pattern_template/pattern_template_matching_test.cc @@ -3,9 +3,9 @@ #include #include "source/common/common/assert.h" +#include "source/common/protobuf/protobuf.h" #include "source/extensions/pattern_template/pattern_template_matching.h" #include "source/extensions/pattern_template/pattern_template_matching_internal.h" -#include "source/common/protobuf/protobuf.h" #include "test/test_common/logging.h" #include "test/test_common/status_utility.h" @@ -331,5 +331,5 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { } // namespace -} // namespace matching +} // namespace PatternTemplate } // namespace Envoy diff --git a/test/extensions/pattern_template/BUILD b/test/extensions/pattern_template/BUILD index 482abd2423976..e22aa8a0648c5 100644 --- a/test/extensions/pattern_template/BUILD +++ b/test/extensions/pattern_template/BUILD @@ -19,7 +19,6 @@ envoy_extension_cc_test( "//source/common/stream_info:filter_state_lib", "//source/extensions/pattern_template:config", "//test/mocks/server:factory_context_mocks", - "@envoy_api//envoy/extensions/pattern_template/v3:pkg_cc_proto", ], ) diff --git a/test/extensions/pattern_template/config_test.cc b/test/extensions/pattern_template/config_test.cc index 9a64226568811..de34898d901f8 100644 --- a/test/extensions/pattern_template/config_test.cc +++ b/test/extensions/pattern_template/config_test.cc @@ -31,7 +31,8 @@ TEST_F(PatternTemplateConfigTest, EmptyCreation) { std::string current_route_name = "fake_current_route"; // Create the predicate for the first time. { - auto predicate = factory_->createPatternTemplatePredicate("/url_pattern/{TEST}", "rewrite_pattern"); + auto predicate = + factory_->createPatternTemplatePredicate("/url_pattern/{TEST}", "rewrite_pattern"); ASSERT(predicate); } } diff --git a/test/extensions/pattern_template/matcher_test.cc b/test/extensions/pattern_template/matcher_test.cc index ae5404b230db1..d468b5757b9fb 100644 --- a/test/extensions/pattern_template/matcher_test.cc +++ b/test/extensions/pattern_template/matcher_test.cc @@ -29,6 +29,6 @@ TEST(PatternTemplate, RouteMatcher) { EXPECT_FALSE(matcher.match("/foo?param=regex")); } -} // namespace UrlTemplate +} // namespace PatternTemplate } // namespace Extensions } // namespace Envoy From e14d4d64940be4ff5a2f999c270df103a42e12bc Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 15 Jul 2022 15:07:42 +0000 Subject: [PATCH 019/238] Adding TypedExtensionConfig Signed-off-by: silverstar195 --- .../config/route/v3/route_components.proto | 72 +------------------ .../v3/pattern_template_match.proto | 43 +++++++++++ .../v3/pattern_template_rewrite.proto | 45 +++++++++++- 3 files changed, 89 insertions(+), 71 deletions(-) create mode 100644 api/envoy/extensions/pattern_template/v3/pattern_template_match.proto diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index f8fd729555d1d..58b3eee17dd52 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -572,34 +572,7 @@ message RouteMatch { // Expect the value to not contain ``?`` or ``#`` and not to end in ``/`` string path_separated_prefix = 14 [(validate.rules).string = {pattern: "^[^?#]+[^?#/]$"}]; - // If specified, the route is a template match rule meaning that the - // ``:path`` header (without the query string) must match the given - // ``path_template`` pattern. - // - // Path template matching types: - // - // * ``*`` : Matches a single path component, up to the next path separator: / - // - // * ``**`` : Matches zero or more path segments. If present, must be the last operator. - // - // * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. - // - // * ``{name=videos/*}`` : A named variable matching more than one path segment. - // The path component matching videos/* is captured as the named variable. - // - // * ``{name=**}`` : A named variable matching zero or more path segments. - // - // - // For example: - // - // * ``/videos/*/*/*.m4s`` would match ``videos/123414/hls/1080p5000_00001.m4s`` - // - // * ``/videos/{file}`` would match ``/videos/1080p5000_00001.m4s`` - // - // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` - // [#not-implemented-hide:] - string path_template = 15 - [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; + core.v3.TypedExtensionConfig path_match_policy = 15; } // Indicates that prefix/path matching should be case sensitive. The default @@ -1101,48 +1074,7 @@ message RouteAction { // ``/aaa/yyy/bbb``. type.matcher.v3.RegexMatchAndSubstitute regex_rewrite = 32; - // Indicates that during forwarding, portions of the path that match the - // pattern should be rewritten, even allowing the substitution of variables - // from the match pattern into the new path as specified by the rewrite template. - // This is useful to allow application paths to be - // rewritten in a way that is aware of segments with variable content like - // identifiers. The router filter will place the original path as it was - // before the rewrite into the :ref:`x-envoy-original-path - // ` header. - // - // Only one of :ref:`prefix_rewrite `, - // :ref:`regex_rewrite `, - // or *path_template_rewrite* may be specified. - // - // Template pattern matching types: - // - // * ``*`` : Matches a single path component, up to the next path separator: / - // - // * ``**`` : Matches zero or more path segments. If present, must be the last operator. - // - // * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. - // - // * ``{name=videos/*}`` : A named variable matching more than one path segment. - // The path component matching videos/* is captured as the named variable. - // - // * ``{name=**}`` : A named variable matching zero or more path segments. - // - // Only named matches can be used to perform rewrites. - // - // Examples using path_template_rewrite: - // - // * The pattern ``/{one}/{two}`` paired with a substitution string of ``/{two}/{one}`` would - // transform ``/cat/dog`` into ``/dog/cat``. - // - // * The pattern ``/videos/{language=lang/*}/*`` paired with a substitution string of - // ``/{language}`` would transform ``/videos/lang/en/video.m4s`` into ``lang/en``. - // - // * The path pattern ``/content/{format}/{lang}/{id}/{file}.vtt`` paired with a substitution - // string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` - // into ``/en-us/hls/en_193913.vtt``. - // [#not-implemented-hide:] - string path_template_rewrite = 41 - [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; + core.v3.TypedExtensionConfig path_rewrite_policy = 41; oneof host_rewrite_specifier { // Indicates that during forwarding, the host header will be swapped with diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto new file mode 100644 index 0000000000000..ef8fd56ff122d --- /dev/null +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto @@ -0,0 +1,43 @@ +syntax = "proto3"; + +package envoy.extensions.pattern_template.v3; + +import "udpa/annotations/status.proto"; + +option java_package = "io.envoyproxy.envoy.extensions.pattern_template.v3"; +option java_outer_classname = "PatternTemplateRewriteProto"; +option java_multiple_files = true; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;pattern_templatev3"; +option (udpa.annotations.file_status).package_version_status = ACTIVE; + +// [#protodoc-title: Pattern Template Matcher] +// [#extension: envoy.pattern_template] +// If specified, the route is a template match rule meaning that the +// ``:path`` header (without the query string) must match the given +// ``path_template`` pattern. +// +// Path template matching types: +// +// * ``*`` : Matches a single path component, up to the next path separator: / +// +// * ``**`` : Matches zero or more path segments. If present, must be the last operator. +// +// * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. +// +// * ``{name=videos/*}`` : A named variable matching more than one path segment. +// The path component matching videos/* is captured as the named variable. +// +// * ``{name=**}`` : A named variable matching zero or more path segments. +// +// +// For example: +// +// * ``/videos/*/*/*.m4s`` would match ``videos/123414/hls/1080p5000_00001.m4s`` +// +// * ``/videos/{file}`` would match ``/videos/1080p5000_00001.m4s`` +// +// * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` +message PatternTemplateMatchConfig { + string path_template = 1 + [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; +} \ No newline at end of file diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto index 731a8d39555c4..7db10d0f16582 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto @@ -10,8 +10,51 @@ option java_multiple_files = true; option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;pattern_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; -// [#protodoc-title: Pattern matcher] +// [#protodoc-title: Pattern Template Matcher] // [#extension: envoy.pattern_template] + // Indicates that during forwarding, portions of the path that match the + // pattern should be rewritten, even allowing the substitution of variables + // from the match pattern into the new path as specified by the rewrite template. + // This is useful to allow application paths to be + // rewritten in a way that is aware of segments with variable content like + // identifiers. The router filter will place the original path as it was + // before the rewrite into the :ref:`x-envoy-original-path + // ` header. + // + // Only one of :ref:`prefix_rewrite `, + // :ref:`regex_rewrite `, + // or *path_template_rewrite* may be specified. + // + // Template pattern matching types: + // + // * ``*`` : Matches a single path component, up to the next path separator: / + // + // * ``**`` : Matches zero or more path segments. If present, must be the last operator. + // + // * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. + // + // * ``{name=videos/*}`` : A named variable matching more than one path segment. + // The path component matching videos/* is captured as the named variable. + // + // * ``{name=**}`` : A named variable matching zero or more path segments. + // + // Only named matches can be used to perform rewrites. + // + // Examples using path_template_rewrite: + // + // * The pattern ``/{one}/{two}`` paired with a substitution string of ``/{two}/{one}`` would + // transform ``/cat/dog`` into ``/dog/cat``. + // + // * The pattern ``/videos/{language=lang/*}/*`` paired with a substitution string of + // ``/{language}`` would transform ``/videos/lang/en/video.m4s`` into ``lang/en``. + // + // * The path pattern ``/content/{format}/{lang}/{id}/{file}.vtt`` paired with a substitution + // string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` + // into ``/en-us/hls/en_193913.vtt``. +message PatternTemplateRewriteConfig { + string path_template_rewrite = 1 + [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; +} // Holds the segments for rewriting urls base on pattern templates message PatternTemplateRewrite { From 48460994f0c6831ac71d600ad08ef42b907530ea Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 14:21:22 +0000 Subject: [PATCH 020/238] Incremental Change Signed-off-by: silverstar195 --- .../config/route/v3/route_components.proto | 161 ++++++------------ .../v3/pattern_template_match.proto | 43 +++++ .../v3/pattern_template_rewrite.proto | 75 ++++++++ source/common/router/config_impl.cc | 62 +++---- 4 files changed, 202 insertions(+), 139 deletions(-) create mode 100644 api/envoy/extensions/pattern_template/v3/pattern_template_match.proto create mode 100644 api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index 2a72c2f546515..58b3eee17dd52 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -91,11 +91,11 @@ message VirtualHost { // The list of routes that will be matched, in order, for incoming requests. // The first route that matches will be used. - // Only one of this and `matcher` can be specified. + // Only one of this and ``matcher`` can be specified. repeated Route routes = 3; // [#next-major-version: This should be included in a oneof with routes wrapped in a message.] - // The match tree to use when resolving route actions for incoming requests. Only one of this and `routes` + // The match tree to use when resolving route actions for incoming requests. Only one of this and ``routes`` // can be specified. xds.type.matcher.v3.Matcher matcher = 21 [(xds.annotations.v3.field_status).work_in_progress = true]; @@ -145,11 +145,15 @@ message VirtualHost { // Indicates that the virtual host has a CORS policy. CorsPolicy cors = 8; - // The per_filter_config field can be used to provide virtual host-specific - // configurations for filters. The key should match the filter name, such as - // *envoy.filters.http.buffer* for the HTTP buffer filter. Use of this field is filter - // specific; see the :ref:`HTTP filter documentation ` - // for if and how it is utilized. + // The per_filter_config field can be used to provide virtual host-specific configurations for filters. + // The key should match the :ref:`filter config name + // `. + // The canonical filter name (e.g., *envoy.filters.http.buffer* for the HTTP buffer filter) can also + // be used for the backwards compatibility. If there is no entry referred by the filter config name, the + // entry referred by the canonical filter name will be provided to the filters as fallback. + // + // Use of this field is filter specific; + // see the :ref:`HTTP filter documentation ` for if and how it is utilized. // [#comment: An entry's value may be wrapped in a // :ref:`FilterConfig` // message to specify additional options.] @@ -269,11 +273,15 @@ message Route { // Decorator for the matched route. Decorator decorator = 5; - // The typed_per_filter_config field can be used to provide route-specific - // configurations for filters. The key should match the filter name, such as - // *envoy.filters.http.buffer* for the HTTP buffer filter. Use of this field is filter - // specific; see the :ref:`HTTP filter documentation ` for - // if and how it is utilized. + // The per_filter_config field can be used to provide route-specific configurations for filters. + // The key should match the :ref:`filter config name + // `. + // The canonical filter name (e.g., *envoy.filters.http.buffer* for the HTTP buffer filter) can also + // be used for the backwards compatibility. If there is no entry referred by the filter config name, the + // entry referred by the canonical filter name will be provided to the filters as fallback. + // + // Use of this field is filter specific; + // see the :ref:`HTTP filter documentation ` for if and how it is utilized. // [#comment: An entry's value may be wrapped in a // :ref:`FilterConfig` // message to specify additional options.] @@ -381,7 +389,7 @@ message WeightedCluster { // An integer between 0 and :ref:`total_weight // `. When a request matches the route, // the choice of an upstream cluster is determined by its weight. The sum of weights across all - // entries in the clusters array must add up to the total_weight, which defaults to 100. + // entries in the clusters array must add up to the total_weight, if total_weight is greater than 0. google.protobuf.UInt32Value weight = 2; // Optional endpoint metadata match criteria used by the subset load balancer. Only endpoints in @@ -423,11 +431,16 @@ message WeightedCluster { items {string {well_known_regex: HTTP_HEADER_NAME strict: false}} }]; - // The per_filter_config field can be used to provide weighted cluster-specific - // configurations for filters. The key should match the filter name, such as - // *envoy.filters.http.buffer* for the HTTP buffer filter. Use of this field is filter - // specific; see the :ref:`HTTP filter documentation ` - // for if and how it is utilized. + // The per_filter_config field can be used to provide weighted cluster-specific configurations + // for filters. + // The key should match the :ref:`filter config name + // `. + // The canonical filter name (e.g., *envoy.filters.http.buffer* for the HTTP buffer filter) can also + // be used for the backwards compatibility. If there is no entry referred by the filter config name, the + // entry referred by the canonical filter name will be provided to the filters as fallback. + // + // Use of this field is filter specific; + // see the :ref:`HTTP filter documentation ` for if and how it is utilized. // [#comment: An entry's value may be wrapped in a // :ref:`FilterConfig` // message to specify additional options.] @@ -445,8 +458,8 @@ message WeightedCluster { repeated ClusterWeight clusters = 1 [(validate.rules).repeated = {min_items: 1}]; // Specifies the total weight across all clusters. The sum of all cluster weights must equal this - // value, which must be greater than 0. Defaults to 100. - google.protobuf.UInt32Value total_weight = 3 [(validate.rules).uint32 = {gte: 1}]; + // value, if this is greater than 0. + google.protobuf.UInt32Value total_weight = 3; // Specifies the runtime key prefix that should be used to construct the // runtime keys associated with each cluster. When the *runtime_key_prefix* is @@ -559,34 +572,7 @@ message RouteMatch { // Expect the value to not contain ``?`` or ``#`` and not to end in ``/`` string path_separated_prefix = 14 [(validate.rules).string = {pattern: "^[^?#]+[^?#/]$"}]; - // If specified, the route is a template match rule meaning that the - // ``:path`` header (without the query string) must match the given - // ``path_template`` pattern. - // - // Path template matching types: - // - // * ``*`` : Matches a single path component, up to the next path separator: / - // - // * ``**`` : Matches zero or more path segments. If present, must be the last operator. - // - // * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. - // - // * ``{name=videos/*}`` : A named variable matching more than one path segment. - // The path component matching videos/* is captured as the named variable. - // - // * ``{name=**}`` : A named variable matching zero or more path segments. - // - // - // For example: - // - // * ``/videos/*/*/*.m4s`` would match ``videos/123414/hls/1080p5000_00001.m4s`` - // - // * ``/videos/{file}`` would match ``/videos/1080p5000_00001.m4s`` - // - // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` - // [#not-implemented-hide:] - string path_template = 15 - [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; + core.v3.TypedExtensionConfig path_match_policy = 15; } // Indicates that prefix/path matching should be case sensitive. The default @@ -769,7 +755,7 @@ message RouteAction { // If not specified, all requests to the target cluster will be mirrored. // - // If specified, this field takes precedence over the `runtime_key` field and requests must also + // If specified, this field takes precedence over the ``runtime_key`` field and requests must also // fall under the percentage of matches indicated by this field. // // For some fraction N/D, a random number in the range [0,D) is selected. If the @@ -954,10 +940,10 @@ message RouteAction { // If present, and the request contains a `grpc-timeout header // `_, use that value as the // *max_stream_duration*, but limit the applied timeout to the maximum value specified here. - // If set to 0, the `grpc-timeout` header is used without modification. + // If set to 0, the ``grpc-timeout`` header is used without modification. google.protobuf.Duration grpc_timeout_header_max = 2; - // If present, Envoy will adjust the timeout provided by the `grpc-timeout` header by + // If present, Envoy will adjust the timeout provided by the ``grpc-timeout`` header by // subtracting the provided duration from the header. This is useful for allowing Envoy to set // its global timeout to be less than that of the deadline imposed by the calling client, which // makes it more likely that Envoy will handle the timeout instead of having the call canceled @@ -1088,48 +1074,7 @@ message RouteAction { // ``/aaa/yyy/bbb``. type.matcher.v3.RegexMatchAndSubstitute regex_rewrite = 32; - // Indicates that during forwarding, portions of the path that match the - // pattern should be rewritten, even allowing the substitution of variables - // from the match pattern into the new path as specified by the rewrite template. - // This is useful to allow application paths to be - // rewritten in a way that is aware of segments with variable content like - // identifiers. The router filter will place the original path as it was - // before the rewrite into the :ref:`x-envoy-original-path - // ` header. - // - // Only one of :ref:`prefix_rewrite `, - // :ref:`regex_rewrite `, - // or *path_template_rewrite* may be specified. - // - // Template pattern matching types: - // - // * ``*`` : Matches a single path component, up to the next path separator: / - // - // * ``**`` : Matches zero or more path segments. If present, must be the last operator. - // - // * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. - // - // * ``{name=videos/*}`` : A named variable matching more than one path segment. - // The path component matching videos/* is captured as the named variable. - // - // * ``{name=**}`` : A named variable matching zero or more path segments. - // - // Only named matches can be used to perform rewrites. - // - // Examples using path_template_rewrite: - // - // * The pattern ``/{one}/{two}`` paired with a substitution string of ``/{two}/{one}`` would - // transform ``/cat/dog`` into ``/dog/cat``. - // - // * The pattern ``/videos/{language=lang/*}/*`` paired with a substitution string of - // ``/{language}`` would transform ``/videos/lang/en/video.m4s`` into ``lang/en``. - // - // * The path pattern ``/content/{format}/{lang}/{id}/{file}.vtt`` paired with a substitution - // string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` - // into ``/en-us/hls/en_193913.vtt``. - // [#not-implemented-hide:] - string path_template_rewrite = 41 - [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; + core.v3.TypedExtensionConfig path_rewrite_policy = 41; oneof host_rewrite_specifier { // Indicates that during forwarding, the host header will be swapped with @@ -1186,7 +1131,7 @@ message RouteAction { // regex: "^/(.+)/.+$" // substitution: \1 // - // Would rewrite the host header to `envoyproxy.io` given the path `/envoyproxy.io/some/path`. + // Would rewrite the host header to ``envoyproxy.io`` given the path ``/envoyproxy.io/some/path``. type.matcher.v3.RegexMatchAndSubstitute host_rewrite_path_regex = 35; } @@ -1297,7 +1242,7 @@ message RouteAction { // or its default value (infinity) instead of // :ref:`timeout `, but limit the applied timeout // to the maximum value specified here. If configured as 0, the maximum allowed timeout for - // gRPC requests is infinity. If not configured at all, the `grpc-timeout` header is not used + // gRPC requests is infinity. If not configured at all, the ``grpc-timeout`` header is not used // and gRPC requests time out like any other requests using // :ref:`timeout ` or its default. // This can be used to prevent unexpected upstream request timeouts due to potentially long @@ -1315,7 +1260,7 @@ message RouteAction { [deprecated = true, (envoy.annotations.deprecated_at_minor_version) = "3.0"]; // Deprecated by :ref:`grpc_timeout_header_offset `. - // If present, Envoy will adjust the timeout provided by the `grpc-timeout` header by subtracting + // If present, Envoy will adjust the timeout provided by the ``grpc-timeout`` header by subtracting // the provided duration from the header. This is useful in allowing Envoy to set its global // timeout to be less than that of the deadline imposed by the calling client, which makes it more // likely that Envoy will handle the timeout instead of having the call canceled by the client. @@ -1418,8 +1363,8 @@ message RetryPolicy { }]; // Specifies the maximum interval between retries. This parameter is optional, but must be - // greater than or equal to the `base_interval` if set. The default is 10 times the - // `base_interval`. See :ref:`config_http_filters_router_x-envoy-max-retries` for a discussion + // greater than or equal to the ``base_interval`` if set. The default is 10 times the + // ``base_interval``. See :ref:`config_http_filters_router_x-envoy-max-retries` for a discussion // of Envoy's back-off algorithm. google.protobuf.Duration max_interval = 2 [(validate.rules).duration = {gt {}}]; } @@ -1564,7 +1509,7 @@ message RetryPolicy { // Specifies parameters that control exponential retry back off. This parameter is optional, in which case the // default base interval is 25 milliseconds or, if set, the current value of the - // `upstream.base_retry_backoff_ms` runtime parameter. The default maximum interval is 10 times + // ``upstream.base_retry_backoff_ms`` runtime parameter. The default maximum interval is 10 times // the base interval. The documentation for :ref:`config_http_filters_router_x-envoy-max-retries` // describes Envoy's back-off algorithm. RetryBackOff retry_back_off = 8; @@ -1574,7 +1519,7 @@ message RetryPolicy { // return a response header like ``Retry-After`` or ``X-RateLimit-Reset`` to // provide feedback to the client on how long to wait before retrying. If // configured, this back-off strategy will be used instead of the - // default exponential back off strategy (configured using `retry_back_off`) + // default exponential back off strategy (configured using ``retry_back_off``) // whenever a response includes the matching headers. RateLimitedRetryBackOff rate_limited_retry_back_off = 11; @@ -1641,10 +1586,10 @@ message RedirectAction { } // When the scheme redirection take place, the following rules apply: - // 1. If the source URI scheme is `http` and the port is explicitly - // set to `:80`, the port will be removed after the redirection - // 2. If the source URI scheme is `https` and the port is explicitly - // set to `:443`, the port will be removed after the redirection + // 1. If the source URI scheme is ``http`` and the port is explicitly + // set to ``:80``, the port will be removed after the redirection + // 2. If the source URI scheme is ``https`` and the port is explicitly + // set to ``:443``, the port will be removed after the redirection oneof scheme_rewrite_specifier { // The scheme portion of the URL will be swapped with "https". bool https_redirect = 4; @@ -1827,7 +1772,7 @@ message VirtualCluster { reserved "pattern", "method"; // Specifies a list of header matchers to use for matching requests. Each specified header must - // match. The pseudo-headers `:path` and `:method` can be used to match the request path and + // match. The pseudo-headers ``:path`` and ``:method`` can be used to match the request path and // method, respectively. repeated HeaderMatcher headers = 4; @@ -1925,14 +1870,14 @@ message RateLimit { message MaskedRemoteAddress { // Length of prefix mask len for IPv4 (e.g. 0, 32). // Defaults to 32 when unset. - // For example, trusted address from x-forwarded-for is `192.168.1.1`, + // For example, trusted address from x-forwarded-for is ``192.168.1.1``, // the descriptor entry is ("masked_remote_address", "192.168.1.1/32"); // if mask len is 24, the descriptor entry is ("masked_remote_address", "192.168.1.0/24"). google.protobuf.UInt32Value v4_prefix_mask_len = 1 [(validate.rules).uint32 = {lte: 32}]; // Length of prefix mask len for IPv6 (e.g. 0, 128). // Defaults to 128 when unset. - // For example, trusted address from x-forwarded-for is `2001:abcd:ef01:2345:6789:abcd:ef01:234`, + // For example, trusted address from x-forwarded-for is ``2001:abcd:ef01:2345:6789:abcd:ef01:234``, // the descriptor entry is ("masked_remote_address", "2001:abcd:ef01:2345:6789:abcd:ef01:234/128"); // if mask len is 64, the descriptor entry is ("masked_remote_address", "2001:abcd:ef01:2345::/64"). google.protobuf.UInt32Value v6_prefix_mask_len = 2 [(validate.rules).uint32 = {lte: 128}]; @@ -1964,7 +1909,7 @@ message RateLimit { option (udpa.annotations.versioning).previous_message_type = "envoy.api.v2.route.RateLimit.Action.HeaderValueMatch"; - // The key to use in the descriptor entry. Defaults to `header_match`. + // The key to use in the descriptor entry. Defaults to ``header_match``. string descriptor_key = 4; // The value to use in the descriptor entry. diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto new file mode 100644 index 0000000000000..ef8fd56ff122d --- /dev/null +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto @@ -0,0 +1,43 @@ +syntax = "proto3"; + +package envoy.extensions.pattern_template.v3; + +import "udpa/annotations/status.proto"; + +option java_package = "io.envoyproxy.envoy.extensions.pattern_template.v3"; +option java_outer_classname = "PatternTemplateRewriteProto"; +option java_multiple_files = true; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;pattern_templatev3"; +option (udpa.annotations.file_status).package_version_status = ACTIVE; + +// [#protodoc-title: Pattern Template Matcher] +// [#extension: envoy.pattern_template] +// If specified, the route is a template match rule meaning that the +// ``:path`` header (without the query string) must match the given +// ``path_template`` pattern. +// +// Path template matching types: +// +// * ``*`` : Matches a single path component, up to the next path separator: / +// +// * ``**`` : Matches zero or more path segments. If present, must be the last operator. +// +// * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. +// +// * ``{name=videos/*}`` : A named variable matching more than one path segment. +// The path component matching videos/* is captured as the named variable. +// +// * ``{name=**}`` : A named variable matching zero or more path segments. +// +// +// For example: +// +// * ``/videos/*/*/*.m4s`` would match ``videos/123414/hls/1080p5000_00001.m4s`` +// +// * ``/videos/{file}`` would match ``/videos/1080p5000_00001.m4s`` +// +// * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` +message PatternTemplateMatchConfig { + string path_template = 1 + [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; +} \ No newline at end of file diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto new file mode 100644 index 0000000000000..7db10d0f16582 --- /dev/null +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto @@ -0,0 +1,75 @@ +syntax = "proto3"; + +package envoy.extensions.pattern_template.v3; + +import "udpa/annotations/status.proto"; + +option java_package = "io.envoyproxy.envoy.extensions.pattern_template.v3"; +option java_outer_classname = "PatternTemplateRewriteProto"; +option java_multiple_files = true; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;pattern_templatev3"; +option (udpa.annotations.file_status).package_version_status = ACTIVE; + +// [#protodoc-title: Pattern Template Matcher] +// [#extension: envoy.pattern_template] + // Indicates that during forwarding, portions of the path that match the + // pattern should be rewritten, even allowing the substitution of variables + // from the match pattern into the new path as specified by the rewrite template. + // This is useful to allow application paths to be + // rewritten in a way that is aware of segments with variable content like + // identifiers. The router filter will place the original path as it was + // before the rewrite into the :ref:`x-envoy-original-path + // ` header. + // + // Only one of :ref:`prefix_rewrite `, + // :ref:`regex_rewrite `, + // or *path_template_rewrite* may be specified. + // + // Template pattern matching types: + // + // * ``*`` : Matches a single path component, up to the next path separator: / + // + // * ``**`` : Matches zero or more path segments. If present, must be the last operator. + // + // * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. + // + // * ``{name=videos/*}`` : A named variable matching more than one path segment. + // The path component matching videos/* is captured as the named variable. + // + // * ``{name=**}`` : A named variable matching zero or more path segments. + // + // Only named matches can be used to perform rewrites. + // + // Examples using path_template_rewrite: + // + // * The pattern ``/{one}/{two}`` paired with a substitution string of ``/{two}/{one}`` would + // transform ``/cat/dog`` into ``/dog/cat``. + // + // * The pattern ``/videos/{language=lang/*}/*`` paired with a substitution string of + // ``/{language}`` would transform ``/videos/lang/en/video.m4s`` into ``lang/en``. + // + // * The path pattern ``/content/{format}/{lang}/{id}/{file}.vtt`` paired with a substitution + // string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` + // into ``/en-us/hls/en_193913.vtt``. +message PatternTemplateRewriteConfig { + string path_template_rewrite = 1 + [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; +} + +// Holds the segments for rewriting urls base on pattern templates +message PatternTemplateRewrite { + message RewriteSegment { + oneof segment_type { + // Represents a segment of the rewritten URL, including any path segments, + // slash and prefix. + string literal = 1; + + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int32 var_index = 2; + } + } + + repeated RewriteSegment segments = 3; +} diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 54b3712df81c1..f3a574fff13de 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -103,8 +103,8 @@ RouteEntryImplBaseConstSharedPtr createAndValidateRoute( vhost, route_config, optional_http_filters, factory_context, validator); break; } - case envoy::config::route::v3::RouteMatch::PathSpecifierCase::kPathTemplate: { - route = std::make_shared(vhost, route_config, optional_http_filters, + case envoy::config::route::v3::RouteMatch::PathSpecifierCase::kPathMatchPolicy: { + route = std::make_shared(vhost, route_config, optional_http_filters, factory_context, validator); break; } @@ -357,14 +357,20 @@ std::vector InternalRedirectPolicyImpl::pred return predicates; } -PatternTemplatePolicyImpl::PatternTemplatePolicyImpl() : enabled_(false){}; +PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; -PatternTemplatePolicyImpl::PatternTemplatePolicyImpl(std::string url_pattern, - std::string url_rewrite_pattern) +PathMatchPolicyImpl::PathMatchPolicyImpl(const ProtobufWkt::Any& typed_config) : url_pattern_(url_pattern), url_rewrite_pattern_(url_rewrite_pattern), enabled_(true) { + + // create matching predicate + if (!matching::PatternTemplatePredicate::is_valid_match_pattern(path_template).ok()) { + throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); + } absl::string_view name = "envoy.url_template.pattern_template_predicates"; auto* factory = Registry::FactoryRegistry::getFactory(name); ASSERT(factory); // factory not found + ProtobufTypes::MessagePtr proto_config = factory->createEmptyRouteConfigProto(); + Envoy::Config::Utility::translateOpaqueConfig(typed_config, validator, *proto_config); predicate_factory_ = factory; } @@ -492,8 +498,8 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, ProtobufMessage::ValidationVisitor& validator) : case_sensitive_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.match(), case_sensitive, true)), prefix_rewrite_(route.route().prefix_rewrite()), - pattern_template_policy_(buildPatternTemplatePolicy(route.match().path_template(), - route.route().path_template_rewrite())), + path_match_policy_(buildPathMatchPolicy(route.match())), + path_rewrite_policy_(buildPathRewritePolicy(route.route())), host_rewrite_(route.route().host_rewrite_literal()), vhost_(vhost), auto_host_rewrite_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.route(), auto_host_rewrite, false)), auto_host_rewrite_header_(!route.route().host_rewrite_header().empty() @@ -1176,37 +1182,31 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( } return InternalRedirectPolicyImpl(policy_config, validator, current_route_name); } - -PatternTemplatePolicyImpl -RouteEntryImplBase::buildPatternTemplatePolicy(std::string path_template, - std::string path_template_rewrite) const { - // match + rewrite - if (!path_template.empty() && !path_template_rewrite.empty()) { - - if (!matching::PatternTemplatePredicate::is_valid_match_pattern(path_template).ok()) { - throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); - } - - if (!matching::PatternTemplatePredicate::is_valid_rewrite_pattern(path_template, path_template_rewrite).ok()) { +PathRewritePolicyImpl +RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::RouteAction route) const { + if (route.has_path_rewrite_policy()) { + if (!matching::PatternTemplatePredicate::is_valid_rewrite_pattern(path_template, + path_template_rewrite) + .ok()) { throw EnvoyException( fmt::format("mismatch between path_template {} and path_template_rewrite {}", path_template, path_template_rewrite)); } - - return PatternTemplatePolicyImpl(path_template, path_template_rewrite);; + return PathRewritePolicyImpl(route.path_match_policy()); } + return PathRewritePolicyImpl(path_template, path_template_rewrite);; + } +} - // only match - if (!path_template.empty()) { - +PatternTemplatePolicyImpl +RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::RouteMatch route) const { + // match + rewrite + if (route.has_path_match_policy()) { if (!matching::PatternTemplatePredicate::is_valid_match_pattern(path_template).ok()) { throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); } - - return PatternTemplatePolicyImpl(path_template, path_template_rewrite);; + return PathMatchPolicyImpl(route.path_match_policy()); } - - // no match + no rewrite return PatternTemplatePolicyImpl(); } @@ -1447,19 +1447,19 @@ void RouteEntryImplBase::WeightedClusterEntry::traversePerFilterConfig( } } -PathTemplateRouteEntryImpl::PathTemplateRouteEntryImpl( +PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( const VirtualHostImpl& vhost, const envoy::config::route::v3::Route& route, const OptionalHttpFilters& optional_http_filters, Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator) {} -void PathTemplateRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, +void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { finalizePathHeader(headers, pattern_template_policy_.predicate()->url_pattern_, insert_envoy_original_path); } -absl::optional PathTemplateRouteEntryImpl::currentUrlPathAfterRewrite( +absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { return currentUrlPathAfterRewriteWithMatchedPath(headers, pattern_template_policy_.predicate()->url_pattern_); } From e3cb859302efefef33ff1244bb55ca3bb50b9171 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 15:08:50 +0000 Subject: [PATCH 021/238] Added import Signed-off-by: silverstar195 --- .../extensions/pattern_template/v3/pattern_template_match.proto | 1 + .../pattern_template/v3/pattern_template_rewrite.proto | 1 + 2 files changed, 2 insertions(+) diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto index ef8fd56ff122d..f649b07dee979 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto @@ -3,6 +3,7 @@ syntax = "proto3"; package envoy.extensions.pattern_template.v3; import "udpa/annotations/status.proto"; +import "validate/validate.proto"; option java_package = "io.envoyproxy.envoy.extensions.pattern_template.v3"; option java_outer_classname = "PatternTemplateRewriteProto"; diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto index 7db10d0f16582..b06063a814c68 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto @@ -3,6 +3,7 @@ syntax = "proto3"; package envoy.extensions.pattern_template.v3; import "udpa/annotations/status.proto"; +import "validate/validate.proto"; option java_package = "io.envoyproxy.envoy.extensions.pattern_template.v3"; option java_outer_classname = "PatternTemplateRewriteProto"; From db9aa982e866e692e91dd6a779a55ca49eac0997 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 15:25:55 +0000 Subject: [PATCH 022/238] Style and formatting Signed-off-by: silverstar195 --- .../v3/pattern_template_match.proto | 4 +- .../v3/pattern_template_rewrite.proto | 78 +++++++++---------- envoy/router/pattern_template.h | 2 +- 3 files changed, 41 insertions(+), 43 deletions(-) diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto index f649b07dee979..8a53fa7aaaff1 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto @@ -39,6 +39,4 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` message PatternTemplateMatchConfig { - string path_template = 1 - [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; -} \ No newline at end of file + string path_template = 1 [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto index b06063a814c68..75eb9393e8334 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto @@ -13,45 +13,45 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Pattern Template Matcher] // [#extension: envoy.pattern_template] - // Indicates that during forwarding, portions of the path that match the - // pattern should be rewritten, even allowing the substitution of variables - // from the match pattern into the new path as specified by the rewrite template. - // This is useful to allow application paths to be - // rewritten in a way that is aware of segments with variable content like - // identifiers. The router filter will place the original path as it was - // before the rewrite into the :ref:`x-envoy-original-path - // ` header. - // - // Only one of :ref:`prefix_rewrite `, - // :ref:`regex_rewrite `, - // or *path_template_rewrite* may be specified. - // - // Template pattern matching types: - // - // * ``*`` : Matches a single path component, up to the next path separator: / - // - // * ``**`` : Matches zero or more path segments. If present, must be the last operator. - // - // * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. - // - // * ``{name=videos/*}`` : A named variable matching more than one path segment. - // The path component matching videos/* is captured as the named variable. - // - // * ``{name=**}`` : A named variable matching zero or more path segments. - // - // Only named matches can be used to perform rewrites. - // - // Examples using path_template_rewrite: - // - // * The pattern ``/{one}/{two}`` paired with a substitution string of ``/{two}/{one}`` would - // transform ``/cat/dog`` into ``/dog/cat``. - // - // * The pattern ``/videos/{language=lang/*}/*`` paired with a substitution string of - // ``/{language}`` would transform ``/videos/lang/en/video.m4s`` into ``lang/en``. - // - // * The path pattern ``/content/{format}/{lang}/{id}/{file}.vtt`` paired with a substitution - // string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` - // into ``/en-us/hls/en_193913.vtt``. +// Indicates that during forwarding, portions of the path that match the +// pattern should be rewritten, even allowing the substitution of variables +// from the match pattern into the new path as specified by the rewrite template. +// This is useful to allow application paths to be +// rewritten in a way that is aware of segments with variable content like +// identifiers. The router filter will place the original path as it was +// before the rewrite into the :ref:`x-envoy-original-path +// ` header. +// +// Only one of :ref:`prefix_rewrite `, +// :ref:`regex_rewrite `, +// or *path_template_rewrite* may be specified. +// +// Template pattern matching types: +// +// * ``*`` : Matches a single path component, up to the next path separator: / +// +// * ``**`` : Matches zero or more path segments. If present, must be the last operator. +// +// * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. +// +// * ``{name=videos/*}`` : A named variable matching more than one path segment. +// The path component matching videos/* is captured as the named variable. +// +// * ``{name=**}`` : A named variable matching zero or more path segments. +// +// Only named matches can be used to perform rewrites. +// +// Examples using path_template_rewrite: +// +// * The pattern ``/{one}/{two}`` paired with a substitution string of ``/{two}/{one}`` would +// transform ``/cat/dog`` into ``/dog/cat``. +// +// * The pattern ``/videos/{language=lang/*}/*`` paired with a substitution string of +// ``/{language}`` would transform ``/videos/lang/en/video.m4s`` into ``lang/en``. +// +// * The path pattern ``/content/{format}/{lang}/{id}/{file}.vtt`` paired with a substitution +// string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` +// into ``/en-us/hls/en_193913.vtt``. message PatternTemplateRewriteConfig { string path_template_rewrite = 1 [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; diff --git a/envoy/router/pattern_template.h b/envoy/router/pattern_template.h index e3f2dce511733..06c62d19ae301 100644 --- a/envoy/router/pattern_template.h +++ b/envoy/router/pattern_template.h @@ -51,4 +51,4 @@ class PatternTemplatePredicateFactory : public Envoy::Config::TypedFactory { }; } // namespace Router -} // namespace Envoy \ No newline at end of file +} // namespace Envoy From 03fc99b887ca1b02dd01c982b11b6988389bb2ac Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 15:31:08 +0000 Subject: [PATCH 023/238] Spelling Signed-off-by: silverstar195 --- source/extensions/extensions_build_config.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 5c68b39c0e4fb..1d194fdf59ee6 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -353,7 +353,7 @@ EXTENSIONS = { # # Pattern Template # - "envoy.pattern_template.pattern_template_predicates": "//source/extensions/pattern_template:config", + "envoy.pattern_template.pattern_template_predicate": "//source/extensions/pattern_template:config", # # Header Validators From b4a840b9964e03fe260158ffc9fe0a32d67c2298 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 15:40:35 +0000 Subject: [PATCH 024/238] Added import Signed-off-by: silverstar195 --- .../extensions/pattern_template/v3/pattern_template_match.proto | 1 + 1 file changed, 1 insertion(+) diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto index 8a53fa7aaaff1..2b58853a8cfd3 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto @@ -40,3 +40,4 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` message PatternTemplateMatchConfig { string path_template = 1 [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; +} From cf26d41081612c46613ecf0382e696dac51a6fec Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 16:26:14 +0000 Subject: [PATCH 025/238] Style Signed-off-by: silverstar195 --- source/extensions/pattern_template/config.cc | 2 +- source/extensions/pattern_template/config.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/extensions/pattern_template/config.cc b/source/extensions/pattern_template/config.cc index 2cb76fdf33af7..30350661fa487 100644 --- a/source/extensions/pattern_template/config.cc +++ b/source/extensions/pattern_template/config.cc @@ -8,4 +8,4 @@ namespace PatternTemplate { REGISTER_FACTORY(PatternTemplatePredicateFactory, Router::PatternTemplatePredicateFactory); } // namespace PatternTemplate -} // namespace Envoy \ No newline at end of file +} // namespace Envoy diff --git a/source/extensions/pattern_template/config.h b/source/extensions/pattern_template/config.h index a46ce50aeabbf..fbdf4d9e185c3 100644 --- a/source/extensions/pattern_template/config.h +++ b/source/extensions/pattern_template/config.h @@ -23,4 +23,4 @@ class PatternTemplatePredicateFactory : public Router::PatternTemplatePredicateF }; } // namespace PatternTemplate -} // namespace Envoy \ No newline at end of file +} // namespace Envoy From f493c54d20b21ec1ce0784e3bc7e2f421246dec2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 16:27:42 +0000 Subject: [PATCH 026/238] Style Signed-off-by: silverstar195 --- .../extensions/pattern_template/v3/pattern_template_match.proto | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto index 2b58853a8cfd3..81aec7fa5a0d3 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto @@ -6,7 +6,7 @@ import "udpa/annotations/status.proto"; import "validate/validate.proto"; option java_package = "io.envoyproxy.envoy.extensions.pattern_template.v3"; -option java_outer_classname = "PatternTemplateRewriteProto"; +option java_outer_classname = "PatternTemplateMatchProto"; option java_multiple_files = true; option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;pattern_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; From 3543c89a67aed113a0342d070c52a07ccf836551 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 17:04:14 +0000 Subject: [PATCH 027/238] Added string words Signed-off-by: silverstar195 --- tools/spelling/spelling_dictionary.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tools/spelling/spelling_dictionary.txt b/tools/spelling/spelling_dictionary.txt index 514377b792afb..043f2b1758015 100644 --- a/tools/spelling/spelling_dictionary.txt +++ b/tools/spelling/spelling_dictionary.txt @@ -30,7 +30,12 @@ Bdecoded Bencoded GiB Repick +SION TRA +abc +abcd +abd +ar btree CAS CB @@ -150,9 +155,13 @@ FQDN FREEBIND FUZZER FUZZERS +delims dereferencing differentially dnsresolvers +endpos +fo +ghi guarddog GC GCC @@ -219,6 +228,10 @@ LEDS LEV LF LHS +hls +jkl +js +lang libsxg LLVM LPT @@ -262,6 +275,7 @@ Oauth OCSP OD ODCDS +mpd oghttp OID OK @@ -965,10 +979,12 @@ parsers passphrase passthrough pathname +patt pausable pausedness pcall pcap +pchar pclose performant pfctl @@ -1174,6 +1190,7 @@ srtt ssize stackdriver stacktrace +startpos starttls startup stateful @@ -1220,6 +1237,7 @@ subtrees subtype subtypes subzone +suf superclass superset svc @@ -1311,6 +1329,7 @@ username usr util utils +va valgrind validator validators @@ -1320,6 +1339,7 @@ variadic varint vec vectorize +ver verifier verifiers versa @@ -1330,6 +1350,7 @@ vip virtualhost virtualize vptr +vtt wakeup wakeups wamr From 910f0a17fd49c8931b9cadc6995ed8d5076a4520 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 17:26:04 +0000 Subject: [PATCH 028/238] Change extension name Signed-off-by: silverstar195 --- source/extensions/extensions_build_config.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 1d194fdf59ee6..28afa7b4ad1f6 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -353,7 +353,7 @@ EXTENSIONS = { # # Pattern Template # - "envoy.pattern_template.pattern_template_predicate": "//source/extensions/pattern_template:config", + "envoy.pattern_template": "//source/extensions/pattern_template:config", # # Header Validators From 75a8e66f4e00a8151fc1827aa82eb854652b5ec6 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 17:45:00 +0000 Subject: [PATCH 029/238] Change import Signed-off-by: silverstar195 --- source/extensions/extensions_build_config.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 28afa7b4ad1f6..1d194fdf59ee6 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -353,7 +353,7 @@ EXTENSIONS = { # # Pattern Template # - "envoy.pattern_template": "//source/extensions/pattern_template:config", + "envoy.pattern_template.pattern_template_predicate": "//source/extensions/pattern_template:config", # # Header Validators From be8991e061855cc8610164c715bae19a495f6ae7 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 17:54:19 +0000 Subject: [PATCH 030/238] Changed extension name Signed-off-by: silverstar195 --- source/extensions/pattern_template/pattern_template_matching.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/extensions/pattern_template/pattern_template_matching.h b/source/extensions/pattern_template/pattern_template_matching.h index 14fa338c46877..bf4def9694519 100644 --- a/source/extensions/pattern_template/pattern_template_matching.h +++ b/source/extensions/pattern_template/pattern_template_matching.h @@ -59,7 +59,7 @@ class PatternTemplatePredicate : public Router::PatternTemplatePredicate { PatternTemplatePredicate() = default; absl::string_view name() const override { - return "envoy.pattern_template.pattern_template_predicates"; + return "envoy.pattern_template.pattern_template_predicate"; } std::string category() const override { return "envoy.pattern_template"; } From c6b511d1747397128ab2fba48d2ee620329e02a1 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 18:12:27 +0000 Subject: [PATCH 031/238] Added import Signed-off-by: silverstar195 --- .../extensions/pattern_template/v3/pattern_template_match.proto | 2 +- .../pattern_template/v3/pattern_template_rewrite.proto | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto index 81aec7fa5a0d3..79e72f8d6a6ae 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto @@ -12,7 +12,7 @@ option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pat option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Pattern Template Matcher] -// [#extension: envoy.pattern_template] +// [#extension: envoy.pattern_template.pattern_template_predicate] // If specified, the route is a template match rule meaning that the // ``:path`` header (without the query string) must match the given // ``path_template`` pattern. diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto index 75eb9393e8334..1fce0ebb8954f 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto @@ -12,7 +12,7 @@ option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pat option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Pattern Template Matcher] -// [#extension: envoy.pattern_template] +// [#extension: envoy.pattern_template.pattern_template_predicate] // Indicates that during forwarding, portions of the path that match the // pattern should be rewritten, even allowing the substitution of variables // from the match pattern into the new path as specified by the rewrite template. From 7c4ec0a4e92afac47accedab4ccac8d60184deb9 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 18 Jul 2022 18:46:36 +0000 Subject: [PATCH 032/238] Changed title Signed-off-by: silverstar195 --- .../extensions/pattern_template/v3/pattern_template_match.proto | 1 - .../pattern_template/v3/pattern_template_rewrite.proto | 1 - 2 files changed, 2 deletions(-) diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto index 79e72f8d6a6ae..d60716f31f430 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto @@ -11,7 +11,6 @@ option java_multiple_files = true; option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;pattern_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; -// [#protodoc-title: Pattern Template Matcher] // [#extension: envoy.pattern_template.pattern_template_predicate] // If specified, the route is a template match rule meaning that the // ``:path`` header (without the query string) must match the given diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto index 1fce0ebb8954f..65e2e39b680f6 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto @@ -11,7 +11,6 @@ option java_multiple_files = true; option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;pattern_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; -// [#protodoc-title: Pattern Template Matcher] // [#extension: envoy.pattern_template.pattern_template_predicate] // Indicates that during forwarding, portions of the path that match the // pattern should be rewritten, even allowing the substitution of variables From 5ee9933f0499f726271b7e1623207fc22ef857f6 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 20 Jul 2022 14:28:49 +0000 Subject: [PATCH 033/238] Separate extension into two Signed-off-by: silverstar195 --- api/BUILD | 3 +- .../pattern_template/{ => match}/v3/BUILD | 0 .../v3/pattern_template_match.proto | 6 +- .../pattern_template/rewrite/v3/BUILD | 9 ++ .../v3/pattern_template_rewrite.proto | 6 +- envoy/router/pattern_template.h | 57 ++++++--- source/extensions/extensions_build_config.bzl | 3 +- source/extensions/extensions_metadata.yaml | 7 +- .../filters/http/jwt_authn/matcher.cc | 2 +- source/extensions/pattern_template/BUILD | 42 +++---- source/extensions/pattern_template/config.cc | 11 -- source/extensions/pattern_template/config.h | 26 ---- .../extensions/pattern_template/match/BUILD | 39 ++++++ .../pattern_template/match/config.cc | 16 +++ .../pattern_template/match/config.h | 34 +++++ .../match/pattern_template_match.cc | 29 +++++ .../match/pattern_template_match.h | 42 +++++++ ...mplate_matching.cc => pattern_template.cc} | 103 ++-------------- .../pattern_template/pattern_template.h | 62 ++++++++++ ...ternal.cc => pattern_template_internal.cc} | 5 +- ...internal.h => pattern_template_internal.h} | 11 +- ...t.cc => pattern_template_internal_test.cc} | 4 +- .../pattern_template_matching.h | 90 -------------- ...ching_test.cc => pattern_template_test.cc} | 6 +- .../extensions/pattern_template/rewrite/BUILD | 39 ++++++ .../pattern_template/rewrite/config.cc | 16 +++ .../pattern_template/rewrite/config.h | 31 +++++ .../rewrite/pattern_template_rewrite.cc | 116 ++++++++++++++++++ .../rewrite/pattern_template_rewrite.h | 52 ++++++++ test/extensions/pattern_template/BUILD | 15 ++- 30 files changed, 592 insertions(+), 290 deletions(-) rename api/envoy/extensions/pattern_template/{ => match}/v3/BUILD (100%) rename api/envoy/extensions/pattern_template/{ => match}/v3/pattern_template_match.proto (92%) create mode 100644 api/envoy/extensions/pattern_template/rewrite/v3/BUILD rename api/envoy/extensions/pattern_template/{ => rewrite}/v3/pattern_template_rewrite.proto (95%) delete mode 100644 source/extensions/pattern_template/config.cc delete mode 100644 source/extensions/pattern_template/config.h create mode 100644 source/extensions/pattern_template/match/BUILD create mode 100644 source/extensions/pattern_template/match/config.cc create mode 100644 source/extensions/pattern_template/match/config.h create mode 100644 source/extensions/pattern_template/match/pattern_template_match.cc create mode 100644 source/extensions/pattern_template/match/pattern_template_match.h rename source/extensions/pattern_template/{pattern_template_matching.cc => pattern_template.cc} (51%) create mode 100644 source/extensions/pattern_template/pattern_template.h rename source/extensions/pattern_template/{pattern_template_matching_internal.cc => pattern_template_internal.cc} (99%) rename source/extensions/pattern_template/{pattern_template_matching_internal.h => pattern_template_internal.h} (94%) rename source/extensions/pattern_template/{pattern_template_matching_internal_test.cc => pattern_template_internal_test.cc} (99%) delete mode 100644 source/extensions/pattern_template/pattern_template_matching.h rename source/extensions/pattern_template/{pattern_template_matching_test.cc => pattern_template_test.cc} (98%) create mode 100644 source/extensions/pattern_template/rewrite/BUILD create mode 100644 source/extensions/pattern_template/rewrite/config.cc create mode 100644 source/extensions/pattern_template/rewrite/config.h create mode 100644 source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc create mode 100644 source/extensions/pattern_template/rewrite/pattern_template_rewrite.h diff --git a/api/BUILD b/api/BUILD index 4d00a4efdb4ad..611430e42be74 100644 --- a/api/BUILD +++ b/api/BUILD @@ -240,7 +240,8 @@ proto_library( "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", - "//envoy/extensions/pattern_template/v3:pkg", + "//envoy/extensions/pattern_template/match/v3:pkg", + "//envoy/extensions/pattern_template/rewrite/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", diff --git a/api/envoy/extensions/pattern_template/v3/BUILD b/api/envoy/extensions/pattern_template/match/v3/BUILD similarity index 100% rename from api/envoy/extensions/pattern_template/v3/BUILD rename to api/envoy/extensions/pattern_template/match/v3/BUILD diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto similarity index 92% rename from api/envoy/extensions/pattern_template/v3/pattern_template_match.proto rename to api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto index d60716f31f430..f2e771458b34c 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto @@ -1,14 +1,14 @@ syntax = "proto3"; -package envoy.extensions.pattern_template.v3; +package envoy.extensions.pattern_template.match.v3; import "udpa/annotations/status.proto"; import "validate/validate.proto"; -option java_package = "io.envoyproxy.envoy.extensions.pattern_template.v3"; +option java_package = "io.envoyproxy.envoy.extensions.pattern_template.match.v3"; option java_outer_classname = "PatternTemplateMatchProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;pattern_templatev3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/match/v3;pattern_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#extension: envoy.pattern_template.pattern_template_predicate] diff --git a/api/envoy/extensions/pattern_template/rewrite/v3/BUILD b/api/envoy/extensions/pattern_template/rewrite/v3/BUILD new file mode 100644 index 0000000000000..ee92fb652582e --- /dev/null +++ b/api/envoy/extensions/pattern_template/rewrite/v3/BUILD @@ -0,0 +1,9 @@ +# DO NOT EDIT. This file is generated by tools/proto_format/proto_sync.py. + +load("@envoy_api//bazel:api_build_system.bzl", "api_proto_package") + +licenses(["notice"]) # Apache 2 + +api_proto_package( + deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], +) diff --git a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto similarity index 95% rename from api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto rename to api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto index 65e2e39b680f6..9a8429400f89f 100644 --- a/api/envoy/extensions/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto @@ -1,14 +1,14 @@ syntax = "proto3"; -package envoy.extensions.pattern_template.v3; +package envoy.extensions.pattern_template.rewrite.v3; import "udpa/annotations/status.proto"; import "validate/validate.proto"; -option java_package = "io.envoyproxy.envoy.extensions.pattern_template.v3"; +option java_package = "io.envoyproxy.envoy.extensions.pattern_template.rewrite.v3"; option java_outer_classname = "PatternTemplateRewriteProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/v3;pattern_templatev3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/rewrite/v3;pattern_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#extension: envoy.pattern_template.pattern_template_predicate] diff --git a/envoy/router/pattern_template.h b/envoy/router/pattern_template.h index 06c62d19ae301..f77359aaba4ce 100644 --- a/envoy/router/pattern_template.h +++ b/envoy/router/pattern_template.h @@ -11,41 +11,60 @@ namespace Envoy { namespace Router { /** - * Used to decide if pattern template match or rewrite is needed based on the target route. + * Used to decide if pattern template match is needed based on the target route. * Subclassing Logger::Loggable so that implementations can log details. */ -class PatternTemplatePredicate : Logger::Loggable { +class PatternTemplateMatchPredicate : Logger::Loggable { public: - PatternTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) - : url_pattern_(url_pattern), url_rewrite_pattern_(url_rewrite_pattern){}; - - PatternTemplatePredicate() = default; - - virtual ~PatternTemplatePredicate() = default; + PatternTemplateMatchPredicate() = default; + virtual ~PatternTemplateMatchPredicate() = default; virtual absl::string_view name() const PURE; - virtual std::string category() const PURE; virtual bool match(absl::string_view pattern) const PURE; +}; - virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, - absl::string_view matched_path) const PURE; +using PatternTemplateMatchPredicateSharedPtr = std::shared_ptr; + +/** + * Factory for PatternTemplateMatchPredicate. + */ +class PatternTemplateMatchPredicateFactory : public Envoy::Config::TypedFactory { +public: + virtual ~PatternTemplateMatchPredicateFactory() = default; + + virtual PatternTemplateMatchPredicateSharedPtr + createUrlTemplateMatchPredicate(std::string url_pattern) PURE; + + std::string category() const override { return "envoy.pattern_template"; } +}; + +/** + * Used to decide if pattern template rewrite is needed based on the target route. + * Subclassing Logger::Loggable so that implementations can log details. + */ +class PatternTemplateRewritePredicate : Logger::Loggable { +public: + PatternTemplateRewritePredicate() = default; + virtual ~PatternTemplateRewritePredicate() = default; - const std::string url_pattern_; - const std::string url_rewrite_pattern_; + virtual absl::string_view name() const PURE; + + virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const PURE; }; -using PatternTemplatePredicateSharedPtr = std::shared_ptr; +using PatternTemplateRewritePredicateSharedPtr = std::shared_ptr; /** - * Factory for PatternTemplatePredicate. + * Factory for PatternRewriteTemplatePredicate. */ -class PatternTemplatePredicateFactory : public Envoy::Config::TypedFactory { +class PatternTemplateRewritePredicateFactory : public Envoy::Config::TypedFactory { public: - virtual ~PatternTemplatePredicateFactory() = default; + virtual ~PatternTemplateRewritePredicateFactory() override = default; - virtual PatternTemplatePredicateSharedPtr - createUrlTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) PURE; + virtual PatternTemplateRewritePredicateSharedPtr + createUrlTemplateRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) PURE; std::string category() const override { return "envoy.pattern_template"; } }; diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 1d194fdf59ee6..9f6f47c4f03ce 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -353,7 +353,8 @@ EXTENSIONS = { # # Pattern Template # - "envoy.pattern_template.pattern_template_predicate": "//source/extensions/pattern_template:config", + "envoy.pattern_template.pattern_template_match_predicate": "//source/extensions/pattern_template/match:config", + "envoy.pattern_template.pattern_template_rewrite_predicate": "//source/extensions/pattern_template/rewrite:config", # # Header Validators diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index 3982158f380e4..a1906cb5e659b 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -746,11 +746,16 @@ envoy.matching.matchers.ip: status: stable type_urls: - envoy.extensions.matching.input_matchers.ip.v3.Ip -envoy.pattern_template.pattern_template_predicate: +envoy.pattern_template.pattern_template_match_predicate: categories: - envoy.pattern_template security_posture: robust_to_untrusted_downstream_and_upstream status: stable +envoy.pattern_template.pattern_template_rewrite_predicate: + categories: + - envoy.pattern_template + security_posture: robust_to_untrusted_downstream_and_upstream + status: stable envoy.quic.proof_source.filter_chain: categories: - envoy.quic.proof_source diff --git a/source/extensions/filters/http/jwt_authn/matcher.cc b/source/extensions/filters/http/jwt_authn/matcher.cc index f717f7fa044e6..d36f27483b677 100644 --- a/source/extensions/filters/http/jwt_authn/matcher.cc +++ b/source/extensions/filters/http/jwt_authn/matcher.cc @@ -194,7 +194,7 @@ MatcherConstPtr Matcher::create(const RequirementRule& rule) { return std::make_unique(rule); case RouteMatch::PathSpecifierCase::kPathSeparatedPrefix: return std::make_unique(rule); - case RouteMatch::PathSpecifierCase::kPathTemplate: + case RouteMatch::PathSpecifierCase::kPathMatchPolicy: // TODO(silverstar194): Implement matcher for template based match break; case RouteMatch::PathSpecifierCase::PATH_SPECIFIER_NOT_SET: diff --git a/source/extensions/pattern_template/BUILD b/source/extensions/pattern_template/BUILD index 39a1279614d7f..09a6ec9a7824f 100644 --- a/source/extensions/pattern_template/BUILD +++ b/source/extensions/pattern_template/BUILD @@ -13,30 +13,31 @@ licenses(["notice"]) # Apache 2 envoy_extension_package() envoy_cc_library( - name = "pattern_template_matching", - srcs = ["pattern_template_matching.cc"], - hdrs = ["pattern_template_matching.h"], + name = "pattern_template_lib", + srcs = ["pattern_template.cc"], + hdrs = ["pattern_template.h"], visibility = [ "//source/common/router:__subpackages__", "//source/extensions/pattern_template:__subpackages__", "//test/extensions/pattern_template:__subpackages__", ], deps = [ - ":pattern_template_matching_internal_cc", + ":pattern_template_internal_cc", "//envoy/router:router_pattern_template_interface", "//source/common/http:path_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_googlesource_code_re2//:re2", - "@envoy_api//envoy/extensions/pattern_template/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/pattern_template/match/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/pattern_template/rewrite/v3:pkg_cc_proto", ], ) envoy_cc_library( - name = "pattern_template_matching_internal_cc", - srcs = ["pattern_template_matching_internal.cc"], - hdrs = ["pattern_template_matching_internal.h"], + name = "pattern_template_internal_cc", + srcs = ["pattern_template_internal.cc"], + hdrs = ["pattern_template_internal.h"], deps = [ "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/flags:flag", @@ -51,10 +52,10 @@ envoy_cc_library( ) envoy_cc_test( - name = "pattern_template_matching_test", - srcs = ["pattern_template_matching_test.cc"], + name = "pattern_template_test", + srcs = ["pattern_template_test.cc"], deps = [ - ":pattern_template_matching", + ":pattern_template", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", @@ -63,10 +64,10 @@ envoy_cc_test( ) envoy_cc_test( - name = "pattern_template_matching_internal_test", - srcs = ["pattern_template_matching_internal_test.cc"], + name = "pattern_template_internal_test", + srcs = ["pattern_template_internal_test.cc"], deps = [ - ":pattern_template_matching_internal_cc", + ":pattern_template_internal_cc", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", @@ -74,16 +75,3 @@ envoy_cc_test( "@com_googlesource_code_re2//:re2", ], ) - -envoy_cc_extension( - name = "config", - srcs = ["config.cc"], - hdrs = ["config.h"], - visibility = ["//visibility:public"], - deps = [ - ":pattern_template_matching", - "//envoy/registry", - "//envoy/router:router_pattern_template_interface", - "@envoy_api//envoy/extensions/pattern_template/v3:pkg_cc_proto", - ], -) diff --git a/source/extensions/pattern_template/config.cc b/source/extensions/pattern_template/config.cc deleted file mode 100644 index 30350661fa487..0000000000000 --- a/source/extensions/pattern_template/config.cc +++ /dev/null @@ -1,11 +0,0 @@ -#include "source/extensions/pattern_template/config.h" - -#include "envoy/registry/registry.h" -#include "envoy/router/pattern_template.h" - -namespace Envoy { -namespace PatternTemplate { - -REGISTER_FACTORY(PatternTemplatePredicateFactory, Router::PatternTemplatePredicateFactory); -} // namespace PatternTemplate -} // namespace Envoy diff --git a/source/extensions/pattern_template/config.h b/source/extensions/pattern_template/config.h deleted file mode 100644 index fbdf4d9e185c3..0000000000000 --- a/source/extensions/pattern_template/config.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include "envoy/extensions/pattern_template/v3/pattern_template_rewrite.pb.h" -#include "envoy/router/pattern_template.h" - -#include "source/extensions/pattern_template/pattern_template_matching.h" - -namespace Envoy { -namespace PatternTemplate { - -class PatternTemplatePredicateFactory : public Router::PatternTemplatePredicateFactory { -public: - Router::PatternTemplatePredicateSharedPtr - createUrlTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) override { - return std::make_shared(url_pattern, url_rewrite_pattern); - } - - ProtobufTypes::MessagePtr createEmptyConfigProto() override { - return std::make_unique(); - } - - std::string name() const override { return "envoy.pattern_template.pattern_template_predicate"; } -}; - -} // namespace PatternTemplate -} // namespace Envoy diff --git a/source/extensions/pattern_template/match/BUILD b/source/extensions/pattern_template/match/BUILD new file mode 100644 index 0000000000000..072f805165493 --- /dev/null +++ b/source/extensions/pattern_template/match/BUILD @@ -0,0 +1,39 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_extension", + "envoy_cc_library", + "envoy_cc_test", + "envoy_extension_package", +) + +licenses(["notice"]) # Apache 2 + +# Wildcard & Pattern Matching + +envoy_extension_package() + +envoy_cc_library( + name = "pattern_template_match_lib", + srcs = ["pattern_template_match.cc"], + hdrs = ["pattern_template_match.h"], + visibility = ["//visibility:public"], + deps = [ + "//envoy/router:router_pattern_template_interface", + "//source/extensions/pattern_template:pattern_template_lib", + "@envoy_api//envoy/extensions/pattern_template/match/v3:pkg_cc_proto", + ], +) + +envoy_cc_extension( + name = "config", + srcs = ["config.cc"], + hdrs = ["config.h"], + visibility = ["//visibility:public"], + deps = [ + ":pattern_template_match_lib", + "//envoy/registry", + "//envoy/router:router_pattern_template_interface", + "//source/extensions/pattern_template:pattern_template_lib", + "@envoy_api//envoy/extensions/pattern_template/match/v3:pkg_cc_proto", + ], +) diff --git a/source/extensions/pattern_template/match/config.cc b/source/extensions/pattern_template/match/config.cc new file mode 100644 index 0000000000000..c0c6f588efb8f --- /dev/null +++ b/source/extensions/pattern_template/match/config.cc @@ -0,0 +1,16 @@ +#include "source/extensions/pattern_template/match/config.h" + +#include "envoy/registry/registry.h" +#include "envoy/router/pattern_template.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Match { + +REGISTER_FACTORY(PatternTemplateMatchPredicateFactory, + Router::PatternTemplateMatchPredicateFactory); +} // namespace Match +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/pattern_template/match/config.h b/source/extensions/pattern_template/match/config.h new file mode 100644 index 0000000000000..082de8c213e4b --- /dev/null +++ b/source/extensions/pattern_template/match/config.h @@ -0,0 +1,34 @@ +#pragma once + +#include "envoy/extensions/pattern_template/match/v3/pattern_template_match.pb.h" +#include "envoy/router/pattern_template.h" + +#include "source/extensions/pattern_template/match/pattern_template_match.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Match { + +class PatternTemplateMatchPredicateFactory : public Router::PatternTemplateMatchPredicateFactory { +public: + PatternTemplateMatchPredicateFactory(){}; + ~PatternTemplateMatchPredicateFactory(){}; + + Router::PatternTemplateMatchPredicateSharedPtr + createUrlTemplateMatchPredicate(std::string url_pattern) override { + return std::make_shared(url_pattern); + } + + ProtobufTypes::MessagePtr createEmptyConfigProto() override { + return std::make_unique(); + } + + std::string name() const override { return "envoy.pattern_template.pattern_template_match_predicate"; } + std::string category() const override { return "envoy.pattern_template"; } +}; + +} // namespace Match +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/pattern_template/match/pattern_template_match.cc b/source/extensions/pattern_template/match/pattern_template_match.cc new file mode 100644 index 0000000000000..fb3f675a6d665 --- /dev/null +++ b/source/extensions/pattern_template/match/pattern_template_match.cc @@ -0,0 +1,29 @@ +#include "source/extensions/pattern_template/pattern_template.h" + +#include +#include +#include +#include + +#include "source/common/http/path_utility.h" +#include "source/extensions/pattern_template/match/pattern_template_match.h" + +#include "absl/status/statusor.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "re2/re2.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Match { + +bool PatternTemplateMatchPredicate::match(absl::string_view pattern) const { + return RE2::FullMatch(PatternTemplateInternal::toStringPiece(Http::PathUtil::removeQueryAndFragment(pattern)), + matching_pattern_regex_); +} + +} // namespace Match +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/pattern_template/match/pattern_template_match.h b/source/extensions/pattern_template/match/pattern_template_match.h new file mode 100644 index 0000000000000..0431945358e96 --- /dev/null +++ b/source/extensions/pattern_template/match/pattern_template_match.h @@ -0,0 +1,42 @@ +#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H +#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H + +#include + +#include "envoy/extensions/pattern_template/match/v3/pattern_template_match.pb.h" +#include "envoy/router/pattern_template.h" + +#include "source/extensions/pattern_template/pattern_template.h" + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Match { + +class PatternTemplateMatchPredicate : public Router::PatternTemplateMatchPredicate { +public: + explicit PatternTemplateMatchPredicate(std::string url_pattern) + : matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())) {} + + PatternTemplateMatchPredicate(){}; + ~PatternTemplateMatchPredicate(){}; + + absl::string_view name() const override { + return "envoy.pattern_template.pattern_template_match_predicate"; + } + + bool match(absl::string_view pattern) const override; + + private: + RE2 matching_pattern_regex_{nullptr}; +}; + +} // namespace Match +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy + +#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H diff --git a/source/extensions/pattern_template/pattern_template_matching.cc b/source/extensions/pattern_template/pattern_template.cc similarity index 51% rename from source/extensions/pattern_template/pattern_template_matching.cc rename to source/extensions/pattern_template/pattern_template.cc index 5ad755990f44f..a8b216e8b344e 100644 --- a/source/extensions/pattern_template/pattern_template_matching.cc +++ b/source/extensions/pattern_template/pattern_template.cc @@ -1,14 +1,14 @@ -#include "source/extensions/pattern_template/pattern_template_matching.h" +#include "source/extensions/pattern_template/pattern_template.h" #include #include #include #include -#include "envoy/extensions/pattern_template/v3/pattern_template_rewrite.pb.h" +#include "envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.pb.h" #include "source/common/http/path_utility.h" -#include "source/extensions/pattern_template/pattern_template_matching_internal.h" +#include "source/extensions/pattern_template/pattern_template_internal.h" #include "absl/status/statusor.h" #include "absl/strings/str_split.h" @@ -16,6 +16,7 @@ #include "re2/re2.h" namespace Envoy { +namespace Extensions { namespace PatternTemplate { using PatternTemplateInternal::ParsedUrlPattern; @@ -82,9 +83,9 @@ parseRewritePatternHelper(absl::string_view pattern) { return result; } -absl::StatusOr +absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { - envoy::extensions::pattern_template::v3::PatternTemplateRewrite parsed_pattern; + envoy::extensions::pattern_template::rewrite::v3::PatternTemplateRewrite parsed_pattern; RE2 regex = RE2(ToStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); @@ -133,96 +134,6 @@ absl::Status is_valid_match_pattern(std::string match_pattern) { return isValidPathTemplateMatchPattern(match_pattern); }; -absl::Status PatternTemplatePredicate::is_valid_rewrite_pattern(std::string match_pattern, - std::string rewrite_pattern) { - - if (!isValidPathTemplateRewritePattern(rewrite_pattern).ok()) { - return absl::InvalidArgumentError( - fmt::format("path_template_rewrite {} is invalid", match_pattern)); - } - - absl::StatusOr converted_pattern = convertURLPatternSyntaxToRegex(match_pattern); - if (!converted_pattern.ok()) { - return absl::InvalidArgumentError(fmt::format("path_template {} is invalid", match_pattern)); - } - - std::string path_template_match_regex = *std::move(converted_pattern); - if (path_template_match_regex.empty() || - !isValidSharedVariableSet(rewrite_pattern, path_template_match_regex).ok()) { - return absl::InvalidArgumentError( - fmt::format("mismatch between path_template {} and path_template_rewrite {}", match_pattern, - rewrite_pattern)); - } - - return absl::OkStatus(); -}; - -bool PatternTemplatePredicate::match(absl::string_view pattern) const { - return RE2::FullMatch(ToStringPiece(Http::PathUtil::removeQueryAndFragment(pattern)), - matching_pattern_regex_); -} - -absl::StatusOr -PatternTemplatePredicate::rewritePattern(absl::string_view current_pattern, - absl::string_view matched_path) const { - absl::StatusOr regex_pattern = convertURLPatternSyntaxToRegex(matched_path); - if (!regex_pattern.ok()) { - return absl::InvalidArgumentError("Unable to parse url pattern regex"); - } - std::string regex_pattern_str = *std::move(regex_pattern); - - absl::StatusOr rewrite_pattern = - parseRewritePattern(url_rewrite_pattern_, regex_pattern_str); - - if (!rewrite_pattern.ok()) { - return absl::InvalidArgumentError("Unable to parse url rewrite pattern"); - } - - envoy::extensions::pattern_template::v3::PatternTemplateRewrite rewrite_pattern_proto = - *std::move(rewrite_pattern); - - absl::StatusOr new_path = - rewriteURLTemplatePattern(current_pattern, regex_pattern_str, rewrite_pattern_proto); - - if (!new_path.ok()) { - return absl::InvalidArgumentError("Unable rewrite url to new URL"); - } - - return *std::move(new_path); -} - -absl::StatusOr PatternTemplatePredicate::rewriteURLTemplatePattern( - absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::pattern_template::v3::PatternTemplateRewrite& rewrite_pattern) const { - RE2 regex = RE2(ToStringPiece(capture_regex)); - if (!regex.ok()) { - return absl::InternalError(regex.error()); - } - - // First capture is the whole matched regex pattern. - int capture_num = regex.NumberOfCapturingGroups() + 1; - std::vector captures(capture_num); - if (!regex.Match(ToStringPiece(url), /*startpos=*/0, /*endpos=*/url.size(), RE2::ANCHOR_BOTH, - captures.data(), captures.size())) { - return absl::InvalidArgumentError("Pattern not match"); - } - - std::string rewritten_url; - - for (const envoy::extensions::pattern_template::v3::PatternTemplateRewrite::RewriteSegment& - segment : rewrite_pattern.segments()) { - if (segment.has_literal()) { - absl::StrAppend(&rewritten_url, segment.literal()); - } else if (segment.has_var_index()) { - if (segment.var_index() < 1 || segment.var_index() >= capture_num) { - return absl::InvalidArgumentError("Invalid variable index"); - } - absl::StrAppend(&rewritten_url, absl::string_view(captures[segment.var_index()].as_string())); - } - } - - return rewritten_url; -} - } // namespace PatternTemplate +} // namespace Extensions } // namespace Envoy diff --git a/source/extensions/pattern_template/pattern_template.h b/source/extensions/pattern_template/pattern_template.h new file mode 100644 index 0000000000000..288a8bc515bb0 --- /dev/null +++ b/source/extensions/pattern_template/pattern_template.h @@ -0,0 +1,62 @@ +#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H +#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H + +#include + +#include "envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.pb.h" +#include "envoy/router/pattern_template.h" + +#include "source/extensions/pattern_template/pattern_template_internal.h" + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +enum class RewriteStringKind { kVariable, kLiteral }; + +struct RewritePatternSegment { + RewritePatternSegment(absl::string_view str, RewriteStringKind kind) : str(str), kind(kind) {} + absl::string_view str; + RewriteStringKind kind; +}; + +absl::Status is_valid_match_pattern(std::string match_pattern); + +// Returns the regex pattern that is equivalent to the given url_pattern. +// Used in the config pipeline to translate user given url pattern to +// the safe regex Envoy can understand. Strips away any variable captures. +absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); + +// Helper function that parses the pattern and breaks it down to either +// literals or variable names. To be used by ParseRewritePattern(). +// Exposed here so that the validator for the rewrite pattern can also +// use it. +absl::StatusOr> +parseRewritePatternHelper(absl::string_view pattern); + +// Returns the parsed Url rewrite pattern to be used by +// RewriteURLTemplatePattern() |capture_regex| should +// be the regex generated by ConvertURLPatternSyntaxToRegex(). +absl::StatusOr +parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); + +// Returns if provided template match pattern is valid +absl::Status isValidPathTemplateMatchPattern(const std::string& path_template_match); + +// Returns if provided rewrite pattern is valid +absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); + +// Returns if path_template and rewrite_template have valid variables +absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, + std::string& capture_regex); + +absl::Status isValidMatchPattern(std::string match_pattern); + +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy + +#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H diff --git a/source/extensions/pattern_template/pattern_template_matching_internal.cc b/source/extensions/pattern_template/pattern_template_internal.cc similarity index 99% rename from source/extensions/pattern_template/pattern_template_matching_internal.cc rename to source/extensions/pattern_template/pattern_template_internal.cc index b987674480260..90d79d3e593b5 100644 --- a/source/extensions/pattern_template/pattern_template_matching_internal.cc +++ b/source/extensions/pattern_template/pattern_template_internal.cc @@ -1,4 +1,4 @@ -#include "source/extensions/pattern_template/pattern_template_matching_internal.h" +#include "source/extensions/pattern_template/pattern_template_internal.h" #include #include @@ -21,6 +21,7 @@ #include "re2/re2.h" namespace Envoy { +namespace Extensions { namespace PatternTemplate { namespace PatternTemplateInternal { @@ -378,6 +379,6 @@ std::string toRegexPattern(const struct ParsedUrlPattern& pattern) { } } // namespace PatternTemplateInternal - } // namespace PatternTemplate +} // namespace Extensions } // namespace Envoy diff --git a/source/extensions/pattern_template/pattern_template_matching_internal.h b/source/extensions/pattern_template/pattern_template_internal.h similarity index 94% rename from source/extensions/pattern_template/pattern_template_matching_internal.h rename to source/extensions/pattern_template/pattern_template_internal.h index be4c27090a0ba..795cb14a91cce 100644 --- a/source/extensions/pattern_template/pattern_template_matching_internal.h +++ b/source/extensions/pattern_template/pattern_template_internal.h @@ -1,5 +1,5 @@ -#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_MATCHING_INTERNAL_H -#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_MATCHING_INTERNAL_H +#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H +#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H #include #include @@ -8,12 +8,15 @@ #include "absl/container/flat_hash_set.h" #include "absl/status/statusor.h" +#include "absl/status/status.h" +#include "absl/types/variant.h" #include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "absl/types/variant.h" #include "re2/re2.h" namespace Envoy { +namespace Extensions { namespace PatternTemplate { namespace PatternTemplateInternal { @@ -88,8 +91,8 @@ std::string toRegexPattern(const struct ParsedUrlPattern& pattern); inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } } // namespace PatternTemplateInternal - } // namespace PatternTemplate +} // namespace Extensions } // namespace Envoy -#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_MATCHING_INTERNAL_H +#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H diff --git a/source/extensions/pattern_template/pattern_template_matching_internal_test.cc b/source/extensions/pattern_template/pattern_template_internal_test.cc similarity index 99% rename from source/extensions/pattern_template/pattern_template_matching_internal_test.cc rename to source/extensions/pattern_template/pattern_template_internal_test.cc index daee2d7038b2e..c9bb23faf199a 100644 --- a/source/extensions/pattern_template/pattern_template_matching_internal_test.cc +++ b/source/extensions/pattern_template/pattern_template_internal_test.cc @@ -7,7 +7,7 @@ #include "source/common/common/assert.h" #include "source/common/protobuf/protobuf.h" -#include "source/extensions/pattern_template/pattern_template_matching_internal.h" +#include "source/extensions/pattern_template/pattern_template_internal.h" #include "test/test_common/logging.h" #include "test/test_common/status_utility.h" @@ -19,6 +19,7 @@ #include "re2/re2.h" namespace Envoy { +namespace Extensions { namespace PatternTemplate { namespace PatternTemplateInternal { @@ -465,4 +466,5 @@ TEST_P(GenPatternRegexWithoutMatch, WithCapture) { } // namespace } // namespace PatternTemplateInternal } // namespace PatternTemplate +} // namespace Extensions } // namespace Envoy diff --git a/source/extensions/pattern_template/pattern_template_matching.h b/source/extensions/pattern_template/pattern_template_matching.h deleted file mode 100644 index bf4def9694519..0000000000000 --- a/source/extensions/pattern_template/pattern_template_matching.h +++ /dev/null @@ -1,90 +0,0 @@ -#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCHING_H -#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCHING_H - -#include - -#include "envoy/extensions/pattern_template/v3/pattern_template_rewrite.pb.h" -#include "envoy/router/pattern_template.h" - -#include "source/extensions/pattern_template/pattern_template_matching_internal.h" - -#include "absl/status/statusor.h" -#include "absl/strings/string_view.h" - -namespace Envoy { -namespace PatternTemplate { - -enum class RewriteStringKind { kVariable, kLiteral }; - -struct RewritePatternSegment { - RewritePatternSegment(absl::string_view str, RewriteStringKind kind) : str(str), kind(kind) {} - absl::string_view str; - RewriteStringKind kind; -}; - -// Returns the regex pattern that is equivalent to the given url_pattern. -// Used in the config pipeline to translate user given url pattern to -// the safe regex Envoy can understand. Strips away any variable captures. -absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); - -// Helper function that parses the pattern and breaks it down to either -// literals or variable names. To be used by ParseRewritePattern(). -// Exposed here so that the validator for the rewrite pattern can also -// use it. -absl::StatusOr> -parseRewritePatternHelper(absl::string_view pattern); - -// Returns the parsed Url rewrite pattern to be used by -// RewriteURLTemplatePattern() |capture_regex| should -// be the regex generated by ConvertURLPatternSyntaxToRegex(). -absl::StatusOr -parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); - -// Returns if provided template match pattern is valid -absl::Status isValidPathTemplateMatchPattern(const std::string& path_template_match); - -// Returns if provided rewrite pattern is valid -absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); - -// Returns if path_template and rewrite_template have valid variables -absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, - std::string& capture_regex); - -// Holds actions to validate, match, and rewrite template pattern based urls. -class PatternTemplatePredicate : public Router::PatternTemplatePredicate { -public: - explicit PatternTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) - : Router::PatternTemplatePredicate(url_pattern, url_rewrite_pattern), - matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())) {} - PatternTemplatePredicate() = default; - - absl::string_view name() const override { - return "envoy.pattern_template.pattern_template_predicate"; - } - std::string category() const override { return "envoy.pattern_template"; } - - // Returns if the regex pattern matches the given regex from constructor. - bool match(absl::string_view pattern) const override; - - absl::StatusOr rewritePattern(absl::string_view current_pattern, - absl::string_view matched_path) const override; - - static absl::Status is_valid_match_pattern(std::string match_pattern); - - static absl::Status is_valid_rewrite_pattern(std::string match_pattern, - std::string rewrite_pattern); - -private: - // Returns the rewritten URL path based on the given parsed rewrite pattern. - // Used for template-based URL rewrite. - absl::StatusOr rewriteURLTemplatePattern( - absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::pattern_template::v3::PatternTemplateRewrite& rewrite_pattern) const; - - RE2 matching_pattern_regex_{nullptr}; -}; - -} // namespace PatternTemplate -} // namespace Envoy - -#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCHING_H diff --git a/source/extensions/pattern_template/pattern_template_matching_test.cc b/source/extensions/pattern_template/pattern_template_test.cc similarity index 98% rename from source/extensions/pattern_template/pattern_template_matching_test.cc rename to source/extensions/pattern_template/pattern_template_test.cc index a847d2f471385..dc97c4469bebe 100644 --- a/source/extensions/pattern_template/pattern_template_matching_test.cc +++ b/source/extensions/pattern_template/pattern_template_test.cc @@ -4,8 +4,8 @@ #include "source/common/common/assert.h" #include "source/common/protobuf/protobuf.h" -#include "source/extensions/pattern_template/pattern_template_matching.h" -#include "source/extensions/pattern_template/pattern_template_matching_internal.h" +#include "source/extensions/pattern_template/pattern_template.h" +#include "source/extensions/pattern_template/pattern_template_internal.h" #include "test/test_common/logging.h" #include "test/test_common/status_utility.h" @@ -14,6 +14,7 @@ #include "gtest/gtest.h" namespace Envoy { +namespace Extensions { namespace PatternTemplate { namespace { @@ -332,4 +333,5 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { } // namespace } // namespace PatternTemplate +} // namespace Extensions } // namespace Envoy diff --git a/source/extensions/pattern_template/rewrite/BUILD b/source/extensions/pattern_template/rewrite/BUILD new file mode 100644 index 0000000000000..ebe9ed01674a8 --- /dev/null +++ b/source/extensions/pattern_template/rewrite/BUILD @@ -0,0 +1,39 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_extension", + "envoy_cc_library", + "envoy_cc_test", + "envoy_extension_package", +) + +licenses(["notice"]) # Apache 2 + +# Wildcard & Pattern Matching + +envoy_extension_package() + +envoy_cc_library( + name = "pattern_template_rewrite_lib", + srcs = ["pattern_template_rewrite.cc"], + hdrs = ["pattern_template_rewrite.h"], + visibility = ["//visibility:public"], + deps = [ + "//envoy/router:router_pattern_template_interface", + "//source/extensions/pattern_template:pattern_template_lib", + "@envoy_api//envoy/extensions/pattern_template/rewrite/v3:pkg_cc_proto", + ], +) + +envoy_cc_extension( + name = "config", + srcs = ["config.cc"], + hdrs = ["config.h"], + visibility = ["//visibility:public"], + deps = [ + ":pattern_template_rewrite_lib", + "//envoy/registry", + "//envoy/router:router_pattern_template_interface", + "//source/extensions/pattern_template:pattern_template_lib", + "@envoy_api//envoy/extensions/pattern_template/rewrite/v3:pkg_cc_proto", + ], +) diff --git a/source/extensions/pattern_template/rewrite/config.cc b/source/extensions/pattern_template/rewrite/config.cc new file mode 100644 index 0000000000000..73f0f8f4cc8bb --- /dev/null +++ b/source/extensions/pattern_template/rewrite/config.cc @@ -0,0 +1,16 @@ +#include "source/extensions/pattern_template/rewrite/config.h" + +#include "envoy/registry/registry.h" +#include "envoy/router/pattern_template.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Rewrite { + +REGISTER_FACTORY(PatternTemplateRewritePredicateFactory, + Router::PatternTemplateRewritePredicateFactory); +} // namespace Rewrite +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/pattern_template/rewrite/config.h b/source/extensions/pattern_template/rewrite/config.h new file mode 100644 index 0000000000000..89a618074f1f0 --- /dev/null +++ b/source/extensions/pattern_template/rewrite/config.h @@ -0,0 +1,31 @@ +#pragma once + +#include "envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.pb.h" +#include "envoy/router/pattern_template.h" + +#include "source/extensions/pattern_template/rewrite/pattern_template_rewrite.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Rewrite { + +class PatternTemplateRewritePredicateFactory : public Router::PatternTemplateRewritePredicateFactory { +public: + Router::PatternTemplateRewritePredicateSharedPtr + createUrlTemplateRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) override { + return std::make_shared(url_pattern, url_rewrite_pattern); + } + + ProtobufTypes::MessagePtr createEmptyConfigProto() override { + return std::make_unique(); + } + + std::string name() const override { return "envoy.pattern_template.pattern_template_rewrite_predicate"; } + +}; + +} // namespace Rewrite +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc new file mode 100644 index 0000000000000..af10387d33226 --- /dev/null +++ b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc @@ -0,0 +1,116 @@ +#include "source/extensions/pattern_template/rewrite/pattern_template_rewrite.h" + +#include +#include +#include +#include + +#include "envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.pb.h" + +#include "source/common/http/path_utility.h" +#include "source/extensions/pattern_template/pattern_template_internal.h" + +#include "absl/status/statusor.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "re2/re2.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Rewrite { + +#ifndef SWIG +// Silence warnings about missing initializers for members of LazyRE2. +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +absl::Status PatternTemplateRewritePredicate::is_valid_rewrite_pattern(std::string match_pattern, + std::string rewrite_pattern) { + + if (!isValidPathTemplateRewritePattern(rewrite_pattern).ok()) { + return absl::InvalidArgumentError( + fmt::format("path_template_rewrite {} is invalid", match_pattern)); + } + + absl::StatusOr converted_pattern = convertURLPatternSyntaxToRegex(match_pattern); + if (!converted_pattern.ok()) { + return absl::InvalidArgumentError(fmt::format("path_template {} is invalid", match_pattern)); + } + + std::string path_template_match_regex = *std::move(converted_pattern); + if (path_template_match_regex.empty() || + !isValidSharedVariableSet(rewrite_pattern, path_template_match_regex).ok()) { + return absl::InvalidArgumentError( + fmt::format("mismatch between path_template {} and path_template_rewrite {}", match_pattern, + rewrite_pattern)); + } + + return absl::OkStatus(); +}; + +absl::StatusOr +PatternTemplateRewritePredicate::rewritePattern(absl::string_view current_pattern, absl::string_view matched_path) const { + absl::StatusOr regex_pattern = convertURLPatternSyntaxToRegex(matched_path); + if (!regex_pattern.ok()) { + return absl::InvalidArgumentError("Unable to parse url pattern regex"); + } + std::string regex_pattern_str = *std::move(regex_pattern); + + absl::StatusOr rewrite_pattern = + parseRewritePattern(url_rewrite_pattern_, regex_pattern_str); + + if (!rewrite_pattern.ok()) { + return absl::InvalidArgumentError("Unable to parse url rewrite pattern"); + } + + envoy::extensions::pattern_template::rewrite::v3::PatternTemplateRewrite rewrite_pattern_proto = + *std::move(rewrite_pattern); + + absl::StatusOr new_path = + rewriteURLTemplatePattern(current_pattern, regex_pattern_str, rewrite_pattern_proto); + + if (!new_path.ok()) { + return absl::InvalidArgumentError("Unable rewrite url to new URL"); + } + + return *std::move(new_path); +} + +absl::StatusOr PatternTemplateRewritePredicate::rewriteURLTemplatePattern( + absl::string_view url, absl::string_view capture_regex, + const envoy::extensions::pattern_template::rewrite::v3::PatternTemplateRewrite& rewrite_pattern) const { + RE2 regex = RE2(PatternTemplateInternal::toStringPiece(capture_regex)); + if (!regex.ok()) { + return absl::InternalError(regex.error()); + } + + // First capture is the whole matched regex pattern. + int capture_num = regex.NumberOfCapturingGroups() + 1; + std::vector captures(capture_num); + if (!regex.Match(PatternTemplateInternal::toStringPiece(url), /*startpos=*/0, /*endpos=*/url.size(), RE2::ANCHOR_BOTH, + captures.data(), captures.size())) { + return absl::InvalidArgumentError("Pattern not match"); + } + + std::string rewritten_url; + + for (const envoy::extensions::pattern_template::rewrite::v3::PatternTemplateRewrite::RewriteSegment& + segment : rewrite_pattern.segments()) { + if (segment.has_literal()) { + absl::StrAppend(&rewritten_url, segment.literal()); + } else if (segment.has_var_index()) { + if (segment.var_index() < 1 || segment.var_index() >= capture_num) { + return absl::InvalidArgumentError("Invalid variable index"); + } + absl::StrAppend(&rewritten_url, absl::string_view(captures[segment.var_index()].as_string())); + } + } + + return rewritten_url; +} + +} // namespace Rewrite +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h new file mode 100644 index 0000000000000..18739166eff79 --- /dev/null +++ b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h @@ -0,0 +1,52 @@ +#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_REWRITE_H +#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_REWRITE_H + +#include + +#include "envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.pb.h" +#include "envoy/router/pattern_template.h" + +#include "source/extensions/pattern_template/pattern_template.h" + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Rewrite { + +class PatternTemplateRewritePredicate : public Router::PatternTemplateRewritePredicate { +public: + explicit PatternTemplateRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) + : matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())), + url_rewrite_pattern_(url_rewrite_pattern) {} + + absl::string_view name() const override { + return "envoy.pattern_template.pattern_template_rewrite_predicate"; + } + + absl::StatusOr rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const override; + + static absl::Status is_valid_rewrite_pattern(std::string match_pattern, + std::string rewrite_pattern); + +private: + // Returns the rewritten URL path based on the given parsed rewrite pattern. + // Used for template-based URL rewrite. + absl::StatusOr rewriteURLTemplatePattern( + absl::string_view url, absl::string_view capture_regex, + const envoy::extensions::pattern_template::rewrite::v3::PatternTemplateRewrite& + rewrite_pattern) const; + + RE2 matching_pattern_regex_{nullptr}; + std::string url_rewrite_pattern_{nullptr}; +}; + +} // namespace Rewrite +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy + +#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_REWRITE_H diff --git a/test/extensions/pattern_template/BUILD b/test/extensions/pattern_template/BUILD index e22aa8a0648c5..c85932c574253 100644 --- a/test/extensions/pattern_template/BUILD +++ b/test/extensions/pattern_template/BUILD @@ -12,8 +12,19 @@ licenses(["notice"]) # Apache 2 envoy_package() envoy_extension_cc_test( - name = "config_test", - srcs = ["config_test.cc"], + name = "match_config_test", + srcs = ["match_config_test.cc"], + extension_names = ["envoy.url_template"], + deps = [ + "//source/common/stream_info:filter_state_lib", + "//source/extensions/pattern_template:config", + "//test/mocks/server:factory_context_mocks", + ], +) + +envoy_extension_cc_test( + name = "rewrite_config_test", + srcs = ["rewrite_config_test.cc"], extension_names = ["envoy.url_template"], deps = [ "//source/common/stream_info:filter_state_lib", From 75beba0daa129ac7c4b0c296d6c9e9e8d28596ed Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 20 Jul 2022 15:22:17 +0000 Subject: [PATCH 034/238] Cleaned up code Signed-off-by: silverstar195 --- api/versioning/BUILD | 3 +- .../extensions/pattern_template/match/BUILD | 1 - .../pattern_template/match/config.h | 3 -- .../match/pattern_template_match.h | 3 -- .../pattern_template/pattern_template.cc | 6 +-- .../pattern_template/pattern_template.h | 5 --- .../extensions/pattern_template/rewrite/BUILD | 3 +- .../rewrite/pattern_template_rewrite.cc | 2 +- .../rewrite/pattern_template_rewrite.h | 2 +- test/extensions/pattern_template/BUILD | 19 ++------ .../{config_test.cc => match_config_test.cc} | 14 +++--- .../pattern_template/matcher_test.cc | 34 --------------- .../pattern_template/rewrite_config_test.cc | 43 +++++++++++++++++++ 13 files changed, 60 insertions(+), 78 deletions(-) rename test/extensions/pattern_template/{config_test.cc => match_config_test.cc} (64%) delete mode 100644 test/extensions/pattern_template/matcher_test.cc create mode 100644 test/extensions/pattern_template/rewrite_config_test.cc diff --git a/api/versioning/BUILD b/api/versioning/BUILD index 069cf8fb9bc4e..67056b06f065f 100644 --- a/api/versioning/BUILD +++ b/api/versioning/BUILD @@ -182,7 +182,8 @@ proto_library( "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", - "//envoy/extensions/pattern_template/v3:pkg", + "//envoy/extensions/pattern_template/match/v3:pkg", + "//envoy/extensions/pattern_template/rewrite/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", diff --git a/source/extensions/pattern_template/match/BUILD b/source/extensions/pattern_template/match/BUILD index 072f805165493..f8b83ba3ae7fe 100644 --- a/source/extensions/pattern_template/match/BUILD +++ b/source/extensions/pattern_template/match/BUILD @@ -16,7 +16,6 @@ envoy_cc_library( name = "pattern_template_match_lib", srcs = ["pattern_template_match.cc"], hdrs = ["pattern_template_match.h"], - visibility = ["//visibility:public"], deps = [ "//envoy/router:router_pattern_template_interface", "//source/extensions/pattern_template:pattern_template_lib", diff --git a/source/extensions/pattern_template/match/config.h b/source/extensions/pattern_template/match/config.h index 082de8c213e4b..b4e2ae46201c6 100644 --- a/source/extensions/pattern_template/match/config.h +++ b/source/extensions/pattern_template/match/config.h @@ -12,9 +12,6 @@ namespace Match { class PatternTemplateMatchPredicateFactory : public Router::PatternTemplateMatchPredicateFactory { public: - PatternTemplateMatchPredicateFactory(){}; - ~PatternTemplateMatchPredicateFactory(){}; - Router::PatternTemplateMatchPredicateSharedPtr createUrlTemplateMatchPredicate(std::string url_pattern) override { return std::make_shared(url_pattern); diff --git a/source/extensions/pattern_template/match/pattern_template_match.h b/source/extensions/pattern_template/match/pattern_template_match.h index 0431945358e96..455829fd6d1e3 100644 --- a/source/extensions/pattern_template/match/pattern_template_match.h +++ b/source/extensions/pattern_template/match/pattern_template_match.h @@ -21,9 +21,6 @@ class PatternTemplateMatchPredicate : public Router::PatternTemplateMatchPredica explicit PatternTemplateMatchPredicate(std::string url_pattern) : matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())) {} - PatternTemplateMatchPredicate(){}; - ~PatternTemplateMatchPredicate(){}; - absl::string_view name() const override { return "envoy.pattern_template.pattern_template_match_predicate"; } diff --git a/source/extensions/pattern_template/pattern_template.cc b/source/extensions/pattern_template/pattern_template.cc index a8b216e8b344e..a94da25bffb58 100644 --- a/source/extensions/pattern_template/pattern_template.cc +++ b/source/extensions/pattern_template/pattern_template.cc @@ -117,7 +117,7 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) return parsed_pattern; } -absl::Status isValidPathTemplateMatchPattern(const std::string& path_template_match) { +absl::Status isValidMatchPattern(const std::string& path_template_match) { return convertURLPatternSyntaxToRegex(path_template_match).status(); } @@ -130,10 +130,6 @@ absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, return parseRewritePattern(path_template_rewrite, capture_regex).status(); } -absl::Status is_valid_match_pattern(std::string match_pattern) { - return isValidPathTemplateMatchPattern(match_pattern); -}; - } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy diff --git a/source/extensions/pattern_template/pattern_template.h b/source/extensions/pattern_template/pattern_template.h index 288a8bc515bb0..79ecc4ea52bc6 100644 --- a/source/extensions/pattern_template/pattern_template.h +++ b/source/extensions/pattern_template/pattern_template.h @@ -23,8 +23,6 @@ struct RewritePatternSegment { RewriteStringKind kind; }; -absl::Status is_valid_match_pattern(std::string match_pattern); - // Returns the regex pattern that is equivalent to the given url_pattern. // Used in the config pipeline to translate user given url pattern to // the safe regex Envoy can understand. Strips away any variable captures. @@ -43,9 +41,6 @@ parseRewritePatternHelper(absl::string_view pattern); absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); -// Returns if provided template match pattern is valid -absl::Status isValidPathTemplateMatchPattern(const std::string& path_template_match); - // Returns if provided rewrite pattern is valid absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); diff --git a/source/extensions/pattern_template/rewrite/BUILD b/source/extensions/pattern_template/rewrite/BUILD index ebe9ed01674a8..dec264ae33dc8 100644 --- a/source/extensions/pattern_template/rewrite/BUILD +++ b/source/extensions/pattern_template/rewrite/BUILD @@ -8,7 +8,7 @@ load( licenses(["notice"]) # Apache 2 -# Wildcard & Pattern Matching +# Wildcard & Pattern Rewrites envoy_extension_package() @@ -16,7 +16,6 @@ envoy_cc_library( name = "pattern_template_rewrite_lib", srcs = ["pattern_template_rewrite.cc"], hdrs = ["pattern_template_rewrite.h"], - visibility = ["//visibility:public"], deps = [ "//envoy/router:router_pattern_template_interface", "//source/extensions/pattern_template:pattern_template_lib", diff --git a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc index af10387d33226..1ec678adad924 100644 --- a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc +++ b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc @@ -25,7 +25,7 @@ namespace Rewrite { #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif -absl::Status PatternTemplateRewritePredicate::is_valid_rewrite_pattern(std::string match_pattern, +absl::Status PatternTemplateRewritePredicate::isValidRewritePattern(std::string match_pattern, std::string rewrite_pattern) { if (!isValidPathTemplateRewritePattern(rewrite_pattern).ok()) { diff --git a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h index 18739166eff79..a1c966c6e6c43 100644 --- a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h +++ b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h @@ -29,7 +29,7 @@ class PatternTemplateRewritePredicate : public Router::PatternTemplateRewritePre absl::StatusOr rewritePattern(absl::string_view current_pattern, absl::string_view matched_path) const override; - static absl::Status is_valid_rewrite_pattern(std::string match_pattern, + static absl::Status isValidRewritePattern(std::string match_pattern, std::string rewrite_pattern); private: diff --git a/test/extensions/pattern_template/BUILD b/test/extensions/pattern_template/BUILD index c85932c574253..0e6e4583d487e 100644 --- a/test/extensions/pattern_template/BUILD +++ b/test/extensions/pattern_template/BUILD @@ -14,10 +14,9 @@ envoy_package() envoy_extension_cc_test( name = "match_config_test", srcs = ["match_config_test.cc"], - extension_names = ["envoy.url_template"], + extension_names = ["envoy.pattern_template"], deps = [ - "//source/common/stream_info:filter_state_lib", - "//source/extensions/pattern_template:config", + "//source/extensions/pattern_template/match:match:pattern_template_match_lib", "//test/mocks/server:factory_context_mocks", ], ) @@ -25,19 +24,9 @@ envoy_extension_cc_test( envoy_extension_cc_test( name = "rewrite_config_test", srcs = ["rewrite_config_test.cc"], - extension_names = ["envoy.url_template"], + extension_names = ["envoy.pattern_template"], deps = [ - "//source/common/stream_info:filter_state_lib", - "//source/extensions/pattern_template:config", + "//source/extensions/pattern_template/rewrite:pattern_template_rewrite_lib", "//test/mocks/server:factory_context_mocks", ], ) - -envoy_extension_cc_test( - name = "matcher_test", - srcs = ["matcher_test.cc"], - extension_names = ["envoy.url_template"], - deps = [ - "//source/extensions/pattern_template:pattern_template_matching", - ], -) diff --git a/test/extensions/pattern_template/config_test.cc b/test/extensions/pattern_template/match_config_test.cc similarity index 64% rename from test/extensions/pattern_template/config_test.cc rename to test/extensions/pattern_template/match_config_test.cc index de34898d901f8..e300237f43356 100644 --- a/test/extensions/pattern_template/config_test.cc +++ b/test/extensions/pattern_template/match_config_test.cc @@ -2,7 +2,7 @@ #include "envoy/router/pattern_template.h" #include "source/common/stream_info/filter_state_impl.h" -#include "source/extensions/pattern_template/config.h" +#include "source/extensions/pattern_template/match/config.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -14,11 +14,11 @@ namespace Extensions { namespace PatternTemplate { namespace { -class PatternTemplateConfigTest : public testing::Test { +class PatternTemplateMatchConfigTest : public testing::Test { protected: - PatternTemplateConfigTest() : filter_state_(StreamInfo::FilterState::LifeSpan::FilterChain) { - factory_ = Registry::FactoryRegistry::getFactory( - "envoy.pattern_template.pattern_template_predicate"); + PatternTemplateMatchConfigTest() : filter_state_(StreamInfo::FilterState::LifeSpan::FilterChain) { + factory_ = Registry::FactoryRegistry::getFactory( + "envoy.pattern_template.pattern_template_match_predicate"); config_ = factory_->createEmptyConfigProto(); } @@ -27,12 +27,12 @@ class PatternTemplateConfigTest : public testing::Test { ProtobufTypes::MessagePtr config_; }; -TEST_F(PatternTemplateConfigTest, EmptyCreation) { +TEST_F(PatternTemplateMatchConfigTest, EmptyCreation) { std::string current_route_name = "fake_current_route"; // Create the predicate for the first time. { auto predicate = - factory_->createPatternTemplatePredicate("/url_pattern/{TEST}", "rewrite_pattern"); + factory_->createUrlTemplateMatchPredicate("/url_pattern/{TEST}"); ASSERT(predicate); } } diff --git a/test/extensions/pattern_template/matcher_test.cc b/test/extensions/pattern_template/matcher_test.cc deleted file mode 100644 index d468b5757b9fb..0000000000000 --- a/test/extensions/pattern_template/matcher_test.cc +++ /dev/null @@ -1,34 +0,0 @@ -#include "source/extensions/pattern_template/pattern_template_matching.h" - -#include "gtest/gtest.h" - -namespace Envoy { -namespace Extensions { -namespace PatternTemplate { - -TEST(PatternTemplate, RouteMatcher) { - matching::UrlTemplatePredicate matcher("/foo/{lang}/{country}", "rewrite"); - - EXPECT_TRUE(matcher.match("/foo/english/us")); - EXPECT_TRUE(matcher.match("/foo/spanish/spain")); - EXPECT_TRUE(matcher.match("/foo/french/france")); - - // with params - EXPECT_TRUE(matcher.match("/foo/english/us#fragment")); - EXPECT_TRUE(matcher.match("/foo/spanish/spain#fragment?param=val")); - EXPECT_TRUE(matcher.match("/foo/french/france?param=regex")); - - EXPECT_FALSE(matcher.match("/foo/english/us/goat")); - EXPECT_FALSE(matcher.match("/foo/goat")); - EXPECT_FALSE(matcher.match("/foo")); - EXPECT_FALSE(matcher.match("")); - - // with params - EXPECT_FALSE(matcher.match("/foo/english/us/goat#fragment?param=val")); - EXPECT_FALSE(matcher.match("/foo/goat?param=regex")); - EXPECT_FALSE(matcher.match("/foo?param=regex")); -} - -} // namespace PatternTemplate -} // namespace Extensions -} // namespace Envoy diff --git a/test/extensions/pattern_template/rewrite_config_test.cc b/test/extensions/pattern_template/rewrite_config_test.cc new file mode 100644 index 0000000000000..6dd0a036fe29d --- /dev/null +++ b/test/extensions/pattern_template/rewrite_config_test.cc @@ -0,0 +1,43 @@ +#include "envoy/registry/registry.h" +#include "envoy/router/pattern_template.h" + +#include "source/common/stream_info/filter_state_impl.h" +#include "source/extensions/pattern_template/rewrite/config.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using namespace testing; + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace { + +class PatternTemplateRewriteConfigTest : public testing::Test { +protected: + PatternTemplateRewriteConfigTest() : filter_state_(StreamInfo::FilterState::LifeSpan::FilterChain) { + factory_ = Registry::FactoryRegistry::getFactory( + "envoy.pattern_template.pattern_template_rewrite_predicate"); + config_ = factory_->createEmptyConfigProto(); + } + + StreamInfo::FilterStateImpl filter_state_; + Router::PatternTemplatePredicateFactory* factory_; + ProtobufTypes::MessagePtr config_; +}; + +TEST_F(PatternTemplateRewriteConfigTest, EmptyCreation) { + std::string current_route_name = "fake_current_route"; + // Create the predicate for the first time. + { + auto predicate = + factory_->createUrlTemplateRewritePredicate("/url_pattern/{TEST}", "rewrite_pattern"); + ASSERT(predicate); + } +} + +} // namespace +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy From dc8bae17467d1fd8a63e1c1b3b010ca0cf7b8eb7 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 20 Jul 2022 16:52:08 +0000 Subject: [PATCH 035/238] Proto changes for pattern template Signed-off-by: silverstar195 --- api/BUILD | 4 + .../config/route/v3/route_components.proto | 294 ++++++++---------- .../pattern_template/match/v3/BUILD | 9 + .../match/v3/pattern_template_match.proto | 42 +++ .../pattern_template/rewrite/v3/BUILD | 9 + .../rewrite/v3/pattern_template_rewrite.proto | 75 +++++ api/versioning/BUILD | 6 + 7 files changed, 280 insertions(+), 159 deletions(-) create mode 100644 api/envoy/extensions/pattern_template/match/v3/BUILD create mode 100644 api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto create mode 100644 api/envoy/extensions/pattern_template/rewrite/v3/BUILD create mode 100644 api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto diff --git a/api/BUILD b/api/BUILD index 7099a7abee5da..611430e42be74 100644 --- a/api/BUILD +++ b/api/BUILD @@ -238,7 +238,10 @@ proto_library( "//envoy/extensions/matching/input_matchers/ip/v3:pkg", "//envoy/extensions/network/dns_resolver/apple/v3:pkg", "//envoy/extensions/network/dns_resolver/cares/v3:pkg", + "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", + "//envoy/extensions/pattern_template/match/v3:pkg", + "//envoy/extensions/pattern_template/rewrite/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", @@ -254,6 +257,7 @@ proto_library( "//envoy/extensions/stat_sinks/graphite_statsd/v3:pkg", "//envoy/extensions/stat_sinks/wasm/v3:pkg", "//envoy/extensions/transport_sockets/alts/v3:pkg", + "//envoy/extensions/transport_sockets/http_11_proxy/v3:pkg", "//envoy/extensions/transport_sockets/internal_upstream/v3:pkg", "//envoy/extensions/transport_sockets/proxy_protocol/v3:pkg", "//envoy/extensions/transport_sockets/quic/v3:pkg", diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index 2a72c2f546515..4315c291c774b 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -91,11 +91,11 @@ message VirtualHost { // The list of routes that will be matched, in order, for incoming requests. // The first route that matches will be used. - // Only one of this and `matcher` can be specified. + // Only one of this and ``matcher`` can be specified. repeated Route routes = 3; // [#next-major-version: This should be included in a oneof with routes wrapped in a message.] - // The match tree to use when resolving route actions for incoming requests. Only one of this and `routes` + // The match tree to use when resolving route actions for incoming requests. Only one of this and ``routes`` // can be specified. xds.type.matcher.v3.Matcher matcher = 21 [(xds.annotations.v3.field_status).work_in_progress = true]; @@ -145,11 +145,15 @@ message VirtualHost { // Indicates that the virtual host has a CORS policy. CorsPolicy cors = 8; - // The per_filter_config field can be used to provide virtual host-specific - // configurations for filters. The key should match the filter name, such as - // *envoy.filters.http.buffer* for the HTTP buffer filter. Use of this field is filter - // specific; see the :ref:`HTTP filter documentation ` - // for if and how it is utilized. + // The per_filter_config field can be used to provide virtual host-specific configurations for filters. + // The key should match the :ref:`filter config name + // `. + // The canonical filter name (e.g., ``envoy.filters.http.buffer`` for the HTTP buffer filter) can also + // be used for the backwards compatibility. If there is no entry referred by the filter config name, the + // entry referred by the canonical filter name will be provided to the filters as fallback. + // + // Use of this field is filter specific; + // see the :ref:`HTTP filter documentation ` for if and how it is utilized. // [#comment: An entry's value may be wrapped in a // :ref:`FilterConfig` // message to specify additional options.] @@ -263,17 +267,21 @@ message Route { // about the route. It can be used for configuration, stats, and logging. // The metadata should go under the filter namespace that will need it. // For instance, if the metadata is intended for the Router filter, - // the filter name should be specified as *envoy.filters.http.router*. + // the filter name should be specified as ``envoy.filters.http.router``. core.v3.Metadata metadata = 4; // Decorator for the matched route. Decorator decorator = 5; - // The typed_per_filter_config field can be used to provide route-specific - // configurations for filters. The key should match the filter name, such as - // *envoy.filters.http.buffer* for the HTTP buffer filter. Use of this field is filter - // specific; see the :ref:`HTTP filter documentation ` for - // if and how it is utilized. + // The per_filter_config field can be used to provide route-specific configurations for filters. + // The key should match the :ref:`filter config name + // `. + // The canonical filter name (e.g., ``envoy.filters.http.buffer`` for the HTTP buffer filter) can also + // be used for the backwards compatibility. If there is no entry referred by the filter config name, the + // entry referred by the canonical filter name will be provided to the filters as fallback. + // + // Use of this field is filter specific; + // see the :ref:`HTTP filter documentation ` for if and how it is utilized. // [#comment: An entry's value may be wrapped in a // :ref:`FilterConfig` // message to specify additional options.] @@ -352,13 +360,13 @@ message WeightedCluster { reserved "per_filter_config"; - // Only one of *name* and *cluster_header* may be specified. + // Only one of ``name`` and ``cluster_header`` may be specified. // [#next-major-version: Need to add back the validation rule: (validate.rules).string = {min_len: 1}] // Name of the upstream cluster. The cluster must exist in the // :ref:`cluster manager configuration `. string name = 1 [(udpa.annotations.field_migrate).oneof_promotion = "cluster_specifier"]; - // Only one of *name* and *cluster_header* may be specified. + // Only one of ``name`` and ``cluster_header`` may be specified. // [#next-major-version: Need to add back the validation rule: (validate.rules).string = {min_len: 1 }] // Envoy will determine the cluster to route to by reading the value of the // HTTP header named by cluster_header from the request headers. If the @@ -367,8 +375,8 @@ message WeightedCluster { // // .. attention:: // - // Internally, Envoy always uses the HTTP/2 *:authority* header to represent the HTTP/1 - // *Host* header. Thus, if attempting to match on *Host*, match on *:authority* instead. + // Internally, Envoy always uses the HTTP/2 ``:authority`` header to represent the HTTP/1 + // ``Host`` header. Thus, if attempting to match on ``Host``, match on ``:authority`` instead. // // .. note:: // @@ -381,14 +389,14 @@ message WeightedCluster { // An integer between 0 and :ref:`total_weight // `. When a request matches the route, // the choice of an upstream cluster is determined by its weight. The sum of weights across all - // entries in the clusters array must add up to the total_weight, which defaults to 100. + // entries in the clusters array must add up to the total_weight, if total_weight is greater than 0. google.protobuf.UInt32Value weight = 2; // Optional endpoint metadata match criteria used by the subset load balancer. Only endpoints in // the upstream cluster with metadata matching what is set in this field will be considered for // load balancing. Note that this will be merged with what's provided in // :ref:`RouteAction.metadata_match `, with - // values here taking precedence. The filter name should be specified as *envoy.lb*. + // values here taking precedence. The filter name should be specified as ``envoy.lb``. core.v3.Metadata metadata_match = 3; // Specifies a list of headers to be added to requests when this cluster is selected @@ -423,11 +431,16 @@ message WeightedCluster { items {string {well_known_regex: HTTP_HEADER_NAME strict: false}} }]; - // The per_filter_config field can be used to provide weighted cluster-specific - // configurations for filters. The key should match the filter name, such as - // *envoy.filters.http.buffer* for the HTTP buffer filter. Use of this field is filter - // specific; see the :ref:`HTTP filter documentation ` - // for if and how it is utilized. + // The per_filter_config field can be used to provide weighted cluster-specific configurations + // for filters. + // The key should match the :ref:`filter config name + // `. + // The canonical filter name (e.g., ``envoy.filters.http.buffer`` for the HTTP buffer filter) can also + // be used for the backwards compatibility. If there is no entry referred by the filter config name, the + // entry referred by the canonical filter name will be provided to the filters as fallback. + // + // Use of this field is filter specific; + // see the :ref:`HTTP filter documentation ` for if and how it is utilized. // [#comment: An entry's value may be wrapped in a // :ref:`FilterConfig` // message to specify additional options.] @@ -445,14 +458,14 @@ message WeightedCluster { repeated ClusterWeight clusters = 1 [(validate.rules).repeated = {min_items: 1}]; // Specifies the total weight across all clusters. The sum of all cluster weights must equal this - // value, which must be greater than 0. Defaults to 100. - google.protobuf.UInt32Value total_weight = 3 [(validate.rules).uint32 = {gte: 1}]; + // value, if this is greater than 0. + google.protobuf.UInt32Value total_weight = 3; // Specifies the runtime key prefix that should be used to construct the - // runtime keys associated with each cluster. When the *runtime_key_prefix* is + // runtime keys associated with each cluster. When the ``runtime_key_prefix`` is // specified, the router will look for weights associated with each upstream - // cluster under the key *runtime_key_prefix* + "." + *cluster[i].name* where - // *cluster[i]* denotes an entry in the clusters array field. If the runtime + // cluster under the key ``runtime_key_prefix`` + ``.`` + ``cluster[i].name`` where + // ``cluster[i]`` denotes an entry in the clusters array field. If the runtime // key for the cluster does not exist, the value specified in the // configuration file will be used as the default weight. See the :ref:`runtime documentation // ` for how key names map to the underlying implementation. @@ -516,17 +529,17 @@ message RouteMatch { option (validate.required) = true; // If specified, the route is a prefix rule meaning that the prefix must - // match the beginning of the *:path* header. + // match the beginning of the ``:path`` header. string prefix = 1; // If specified, the route is an exact path rule meaning that the path must - // exactly match the *:path* header once the query string is removed. + // exactly match the ``:path`` header once the query string is removed. string path = 2; // If specified, the route is a regular expression rule meaning that the - // regex must match the *:path* header once the query string is removed. The entire path + // regex must match the ``:path`` header once the query string is removed. The entire path // (without the query string) must match the regex. The rule will not match if only a - // subsequence of the *:path* header matches the regex. + // subsequence of the ``:path`` header matches the regex. // // [#next-major-version: In the v3 API we should redo how path specification works such // that we utilize StringMatcher, and additionally have consistent options around whether we @@ -559,34 +572,7 @@ message RouteMatch { // Expect the value to not contain ``?`` or ``#`` and not to end in ``/`` string path_separated_prefix = 14 [(validate.rules).string = {pattern: "^[^?#]+[^?#/]$"}]; - // If specified, the route is a template match rule meaning that the - // ``:path`` header (without the query string) must match the given - // ``path_template`` pattern. - // - // Path template matching types: - // - // * ``*`` : Matches a single path component, up to the next path separator: / - // - // * ``**`` : Matches zero or more path segments. If present, must be the last operator. - // - // * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. - // - // * ``{name=videos/*}`` : A named variable matching more than one path segment. - // The path component matching videos/* is captured as the named variable. - // - // * ``{name=**}`` : A named variable matching zero or more path segments. - // - // - // For example: - // - // * ``/videos/*/*/*.m4s`` would match ``videos/123414/hls/1080p5000_00001.m4s`` - // - // * ``/videos/{file}`` would match ``/videos/1080p5000_00001.m4s`` - // - // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` - // [#not-implemented-hide:] - string path_template = 15 - [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; + core.v3.TypedExtensionConfig path_match_policy = 15; } // Indicates that prefix/path matching should be case sensitive. The default @@ -619,9 +605,9 @@ message RouteMatch { repeated HeaderMatcher headers = 6; // Specifies a set of URL query parameters on which the route should - // match. The router will check the query string from the *path* header + // match. The router will check the query string from the ``path`` header // against all the specified query parameters. If the number of specified - // query parameters is nonzero, they all must match the *path* header's + // query parameters is nonzero, they all must match the ``path`` header's // query string for a match to occur. // // .. note:: @@ -663,16 +649,16 @@ message CorsPolicy { // string matchers match. repeated type.matcher.v3.StringMatcher allow_origin_string_match = 11; - // Specifies the content for the *access-control-allow-methods* header. + // Specifies the content for the ``access-control-allow-methods`` header. string allow_methods = 2; - // Specifies the content for the *access-control-allow-headers* header. + // Specifies the content for the ``access-control-allow-headers`` header. string allow_headers = 3; - // Specifies the content for the *access-control-expose-headers* header. + // Specifies the content for the ``access-control-expose-headers`` header. string expose_headers = 4; - // Specifies the content for the *access-control-max-age* header. + // Specifies the content for the ``access-control-max-age`` header. string max_age = 5; // Specifies whether the resource allows credentials. @@ -697,7 +683,7 @@ message CorsPolicy { // // If :ref:`runtime_key ` is specified, // Envoy will lookup the runtime key to get the percentage of requests for which it will evaluate - // and track the request's *Origin* to determine if it's valid but will not enforce any policies. + // and track the request's ``Origin`` to determine if it's valid but will not enforce any policies. core.v3.RuntimeFractionalPercent shadow_enabled = 10; } @@ -711,6 +697,9 @@ message RouteAction { // HTTP status code - 404 Not Found. NOT_FOUND = 1; + + // HTTP status code - 500 Internal Server Error. + INTERNAL_SERVER_ERROR = 2; } // Configures :ref:`internal redirect ` behavior. @@ -727,8 +716,8 @@ message RouteAction { // respond before returning the response from the primary cluster. All normal statistics are // collected for the shadow cluster making this feature useful for testing. // - // During shadowing, the host/authority header is altered such that *-shadow* is appended. This is - // useful for logging. For example, *cluster1* becomes *cluster1-shadow*. + // During shadowing, the host/authority header is altered such that ``-shadow`` is appended. This is + // useful for logging. For example, ``cluster1`` becomes ``cluster1-shadow``. // // .. note:: // @@ -742,13 +731,13 @@ message RouteAction { reserved "runtime_key"; - // Only one of *cluster* and *cluster_header* can be specified. + // Only one of ``cluster`` and ``cluster_header`` can be specified. // [#next-major-version: Need to add back the validation rule: (validate.rules).string = {min_len: 1}] // Specifies the cluster that requests will be mirrored to. The cluster must // exist in the cluster manager configuration. string cluster = 1 [(udpa.annotations.field_migrate).oneof_promotion = "cluster_specifier"]; - // Only one of *cluster* and *cluster_header* can be specified. + // Only one of ``cluster`` and ``cluster_header`` can be specified. // Envoy will determine the cluster to route to by reading the value of the // HTTP header named by cluster_header from the request headers. Only the first value in header is used, // and no shadow request will happen if the value is not found in headers. Envoy will not wait for @@ -756,8 +745,8 @@ message RouteAction { // // .. attention:: // - // Internally, Envoy always uses the HTTP/2 *:authority* header to represent the HTTP/1 - // *Host* header. Thus, if attempting to match on *Host*, match on *:authority* instead. + // Internally, Envoy always uses the HTTP/2 ``:authority`` header to represent the HTTP/1 + // ``Host`` header. Thus, if attempting to match on ``Host``, match on ``:authority`` instead. // // .. note:: // @@ -769,7 +758,7 @@ message RouteAction { // If not specified, all requests to the target cluster will be mirrored. // - // If specified, this field takes precedence over the `runtime_key` field and requests must also + // If specified, this field takes precedence over the ``runtime_key`` field and requests must also // fall under the percentage of matches indicated by this field. // // For some fraction N/D, a random number in the range [0,D) is selected. If the @@ -953,11 +942,11 @@ message RouteAction { // If present, and the request contains a `grpc-timeout header // `_, use that value as the - // *max_stream_duration*, but limit the applied timeout to the maximum value specified here. - // If set to 0, the `grpc-timeout` header is used without modification. + // ``max_stream_duration``, but limit the applied timeout to the maximum value specified here. + // If set to 0, the ``grpc-timeout`` header is used without modification. google.protobuf.Duration grpc_timeout_header_max = 2; - // If present, Envoy will adjust the timeout provided by the `grpc-timeout` header by + // If present, Envoy will adjust the timeout provided by the ``grpc-timeout`` header by // subtracting the provided duration from the header. This is useful for allowing Envoy to set // its global timeout to be less than that of the deadline imposed by the calling client, which // makes it more likely that Envoy will handle the timeout instead of having the call canceled @@ -984,8 +973,8 @@ message RouteAction { // // .. attention:: // - // Internally, Envoy always uses the HTTP/2 *:authority* header to represent the HTTP/1 - // *Host* header. Thus, if attempting to match on *Host*, match on *:authority* instead. + // Internally, Envoy always uses the HTTP/2 ``:authority`` header to represent the HTTP/1 + // ``Host`` header. Thus, if attempting to match on ``Host``, match on ``:authority`` instead. // // .. note:: // @@ -1020,7 +1009,7 @@ message RouteAction { // in the upstream cluster with metadata matching what's set in this field will be considered // for load balancing. If using :ref:`weighted_clusters // `, metadata will be merged, with values - // provided there taking precedence. The filter name should be specified as *envoy.lb*. + // provided there taking precedence. The filter name should be specified as ``envoy.lb``. core.v3.Metadata metadata_match = 4; // Indicates that during forwarding, the matched prefix (or path) should be @@ -1031,14 +1020,14 @@ message RouteAction { // // Only one of :ref:`regex_rewrite ` // [#comment:TODO(silverstar194) add the following once path_template_rewrite is implemented: :ref:`path_template_rewrite `] - // or *prefix_rewrite* may be specified. + // or ``prefix_rewrite`` may be specified. // // .. attention:: // // Pay careful attention to the use of trailing slashes in the // :ref:`route's match ` prefix value. // Stripping a prefix from a path requires multiple Routes to handle all cases. For example, - // rewriting */prefix* to */* and */prefix/etc* to */etc* cannot be done in a single + // rewriting ``/prefix`` to ``/`` and ``/prefix/etc`` to ``/etc`` cannot be done in a single // :ref:`Route `, as shown by the below config entries: // // .. code-block:: yaml @@ -1052,8 +1041,8 @@ message RouteAction { // route: // prefix_rewrite: "/" // - // Having above entries in the config, requests to */prefix* will be stripped to */*, while - // requests to */prefix/etc* will be stripped to */etc*. + // Having above entries in the config, requests to ``/prefix`` will be stripped to ``/``, while + // requests to ``/prefix/etc`` will be stripped to ``/etc``. string prefix_rewrite = 5 [(validate.rules).string = {well_known_regex: HTTP_HEADER_VALUE strict: false}]; @@ -1068,7 +1057,7 @@ message RouteAction { // // Only one of :ref:`prefix_rewrite ` // [#comment:TODO(silverstar194) add the following once path_template_rewrite is implemented: :ref:`path_template_rewrite `,] - // or *regex_rewrite* may be specified. + // or ``regex_rewrite`` may be specified. // // Examples using Google's `RE2 `_ engine: // @@ -1088,48 +1077,7 @@ message RouteAction { // ``/aaa/yyy/bbb``. type.matcher.v3.RegexMatchAndSubstitute regex_rewrite = 32; - // Indicates that during forwarding, portions of the path that match the - // pattern should be rewritten, even allowing the substitution of variables - // from the match pattern into the new path as specified by the rewrite template. - // This is useful to allow application paths to be - // rewritten in a way that is aware of segments with variable content like - // identifiers. The router filter will place the original path as it was - // before the rewrite into the :ref:`x-envoy-original-path - // ` header. - // - // Only one of :ref:`prefix_rewrite `, - // :ref:`regex_rewrite `, - // or *path_template_rewrite* may be specified. - // - // Template pattern matching types: - // - // * ``*`` : Matches a single path component, up to the next path separator: / - // - // * ``**`` : Matches zero or more path segments. If present, must be the last operator. - // - // * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. - // - // * ``{name=videos/*}`` : A named variable matching more than one path segment. - // The path component matching videos/* is captured as the named variable. - // - // * ``{name=**}`` : A named variable matching zero or more path segments. - // - // Only named matches can be used to perform rewrites. - // - // Examples using path_template_rewrite: - // - // * The pattern ``/{one}/{two}`` paired with a substitution string of ``/{two}/{one}`` would - // transform ``/cat/dog`` into ``/dog/cat``. - // - // * The pattern ``/videos/{language=lang/*}/*`` paired with a substitution string of - // ``/{language}`` would transform ``/videos/lang/en/video.m4s`` into ``lang/en``. - // - // * The path pattern ``/content/{format}/{lang}/{id}/{file}.vtt`` paired with a substitution - // string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` - // into ``/en-us/hls/en_193913.vtt``. - // [#not-implemented-hide:] - string path_template_rewrite = 41 - [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; + core.v3.TypedExtensionConfig path_rewrite_policy = 41; oneof host_rewrite_specifier { // Indicates that during forwarding, the host header will be swapped with @@ -1143,7 +1091,7 @@ message RouteAction { // Indicates that during forwarding, the host header will be swapped with // the hostname of the upstream host chosen by the cluster manager. This // option is applicable only when the destination cluster for a route is of - // type *strict_dns* or *logical_dns*. Setting this to true with other cluster types + // type ``strict_dns`` or ``logical_dns``. Setting this to true with other cluster types // has no effect. Using this option will append the // :ref:`config_http_conn_man_headers_x-forwarded-host` header if // :ref:`append_x_forwarded_host ` @@ -1186,7 +1134,7 @@ message RouteAction { // regex: "^/(.+)/.+$" // substitution: \1 // - // Would rewrite the host header to `envoyproxy.io` given the path `/envoyproxy.io/some/path`. + // Would rewrite the host header to ``envoyproxy.io`` given the path ``/envoyproxy.io/some/path``. type.matcher.v3.RegexMatchAndSubstitute host_rewrite_path_regex = 35; } @@ -1297,7 +1245,7 @@ message RouteAction { // or its default value (infinity) instead of // :ref:`timeout `, but limit the applied timeout // to the maximum value specified here. If configured as 0, the maximum allowed timeout for - // gRPC requests is infinity. If not configured at all, the `grpc-timeout` header is not used + // gRPC requests is infinity. If not configured at all, the ``grpc-timeout`` header is not used // and gRPC requests time out like any other requests using // :ref:`timeout ` or its default. // This can be used to prevent unexpected upstream request timeouts due to potentially long @@ -1315,7 +1263,7 @@ message RouteAction { [deprecated = true, (envoy.annotations.deprecated_at_minor_version) = "3.0"]; // Deprecated by :ref:`grpc_timeout_header_offset `. - // If present, Envoy will adjust the timeout provided by the `grpc-timeout` header by subtracting + // If present, Envoy will adjust the timeout provided by the ``grpc-timeout`` header by subtracting // the provided duration from the header. This is useful in allowing Envoy to set its global // timeout to be less than that of the deadline imposed by the calling client, which makes it more // likely that Envoy will handle the timeout instead of having the call canceled by the client. @@ -1418,8 +1366,8 @@ message RetryPolicy { }]; // Specifies the maximum interval between retries. This parameter is optional, but must be - // greater than or equal to the `base_interval` if set. The default is 10 times the - // `base_interval`. See :ref:`config_http_filters_router_x-envoy-max-retries` for a discussion + // greater than or equal to the ``base_interval`` if set. The default is 10 times the + // ``base_interval``. See :ref:`config_http_filters_router_x-envoy-max-retries` for a discussion // of Envoy's back-off algorithm. google.protobuf.Duration max_interval = 2 [(validate.rules).duration = {gt {}}]; } @@ -1564,7 +1512,7 @@ message RetryPolicy { // Specifies parameters that control exponential retry back off. This parameter is optional, in which case the // default base interval is 25 milliseconds or, if set, the current value of the - // `upstream.base_retry_backoff_ms` runtime parameter. The default maximum interval is 10 times + // ``upstream.base_retry_backoff_ms`` runtime parameter. The default maximum interval is 10 times // the base interval. The documentation for :ref:`config_http_filters_router_x-envoy-max-retries` // describes Envoy's back-off algorithm. RetryBackOff retry_back_off = 8; @@ -1574,7 +1522,7 @@ message RetryPolicy { // return a response header like ``Retry-After`` or ``X-RateLimit-Reset`` to // provide feedback to the client on how long to wait before retrying. If // configured, this back-off strategy will be used instead of the - // default exponential back off strategy (configured using `retry_back_off`) + // default exponential back off strategy (configured using ``retry_back_off``) // whenever a response includes the matching headers. RateLimitedRetryBackOff rate_limited_retry_back_off = 11; @@ -1641,10 +1589,10 @@ message RedirectAction { } // When the scheme redirection take place, the following rules apply: - // 1. If the source URI scheme is `http` and the port is explicitly - // set to `:80`, the port will be removed after the redirection - // 2. If the source URI scheme is `https` and the port is explicitly - // set to `:443`, the port will be removed after the redirection + // 1. If the source URI scheme is ``http`` and the port is explicitly + // set to ``:80``, the port will be removed after the redirection + // 2. If the source URI scheme is ``https`` and the port is explicitly + // set to ``:443``, the port will be removed after the redirection oneof scheme_rewrite_specifier { // The scheme portion of the URL will be swapped with "https". bool https_redirect = 4; @@ -1738,7 +1686,7 @@ message DirectResponseAction { // // .. note:: // - // Headers can be specified using *response_headers_to_add* in the enclosing + // Headers can be specified using ``response_headers_to_add`` in the enclosing // :ref:`envoy_v3_api_msg_config.route.v3.Route`, :ref:`envoy_v3_api_msg_config.route.v3.RouteConfiguration` or // :ref:`envoy_v3_api_msg_config.route.v3.VirtualHost`. core.v3.DataSource body = 2; @@ -1827,7 +1775,7 @@ message VirtualCluster { reserved "pattern", "method"; // Specifies a list of header matchers to use for matching requests. Each specified header must - // match. The pseudo-headers `:path` and `:method` can be used to match the request path and + // match. The pseudo-headers ``:path`` and ``:method`` can be used to match the request path and // method, respectively. repeated HeaderMatcher headers = 4; @@ -1881,7 +1829,7 @@ message RateLimit { } // The following descriptor entry is appended when a header contains a key that matches the - // *header_name*: + // ``header_name``: // // .. code-block:: cpp // @@ -1925,14 +1873,14 @@ message RateLimit { message MaskedRemoteAddress { // Length of prefix mask len for IPv4 (e.g. 0, 32). // Defaults to 32 when unset. - // For example, trusted address from x-forwarded-for is `192.168.1.1`, + // For example, trusted address from x-forwarded-for is ``192.168.1.1``, // the descriptor entry is ("masked_remote_address", "192.168.1.1/32"); // if mask len is 24, the descriptor entry is ("masked_remote_address", "192.168.1.0/24"). google.protobuf.UInt32Value v4_prefix_mask_len = 1 [(validate.rules).uint32 = {lte: 32}]; // Length of prefix mask len for IPv6 (e.g. 0, 128). // Defaults to 128 when unset. - // For example, trusted address from x-forwarded-for is `2001:abcd:ef01:2345:6789:abcd:ef01:234`, + // For example, trusted address from x-forwarded-for is ``2001:abcd:ef01:2345:6789:abcd:ef01:234``, // the descriptor entry is ("masked_remote_address", "2001:abcd:ef01:2345:6789:abcd:ef01:234/128"); // if mask len is 64, the descriptor entry is ("masked_remote_address", "2001:abcd:ef01:2345::/64"). google.protobuf.UInt32Value v6_prefix_mask_len = 2 [(validate.rules).uint32 = {lte: 128}]; @@ -1964,7 +1912,7 @@ message RateLimit { option (udpa.annotations.versioning).previous_message_type = "envoy.api.v2.route.RateLimit.Action.HeaderValueMatch"; - // The key to use in the descriptor entry. Defaults to `header_match`. + // The key to use in the descriptor entry. Defaults to ``header_match``. string descriptor_key = 4; // The value to use in the descriptor entry. @@ -2001,7 +1949,7 @@ message RateLimit { // only happen if the value in the dynamic metadata is of type string. type.metadata.v3.MetadataKey metadata_key = 2 [(validate.rules).message = {required: true}]; - // An optional value to use if *metadata_key* is empty. If not set and + // An optional value to use if ``metadata_key`` is empty. If not set and // no value is present under the metadata_key then no descriptor is generated. string default_value = 3; } @@ -2027,7 +1975,7 @@ message RateLimit { // only happen if the value in the metadata is of type string. type.metadata.v3.MetadataKey metadata_key = 2 [(validate.rules).message = {required: true}]; - // An optional value to use if *metadata_key* is empty. If not set and + // An optional value to use if ``metadata_key`` is empty. If not set and // no value is present under the metadata_key then no descriptor is generated. string default_value = 3; @@ -2131,12 +2079,12 @@ message RateLimit { // .. attention:: // -// Internally, Envoy always uses the HTTP/2 *:authority* header to represent the HTTP/1 *Host* -// header. Thus, if attempting to match on *Host*, match on *:authority* instead. +// Internally, Envoy always uses the HTTP/2 ``:authority`` header to represent the HTTP/1 ``Host`` +// header. Thus, if attempting to match on ``Host``, match on ``:authority`` instead. // // .. attention:: // -// To route on HTTP method, use the special HTTP/2 *:method* header. This works for both +// To route on HTTP method, use the special HTTP/2 ``:method`` header. This works for both // HTTP/1 and HTTP/2 as Envoy normalizes headers. E.g., // // .. code-block:: json @@ -2153,7 +2101,7 @@ message RateLimit { // value. // // [#next-major-version: HeaderMatcher should be refactored to use StringMatcher.] -// [#next-free-field: 14] +// [#next-free-field: 15] message HeaderMatcher { option (udpa.annotations.versioning).previous_message_type = "envoy.api.v2.route.HeaderMatcher"; @@ -2188,8 +2136,8 @@ message HeaderMatcher { // // Examples: // - // * For range [-10,0), route will match for header value -1, but not for 0, "somestring", 10.9, - // "-1somestring" + // * For range [-10,0), route will match for header value -1, but not for 0, ``somestring``, 10.9, + // ``-1somestring`` type.v3.Int64Range range_match = 6; // If specified as true, header match will be performed based on whether the header is in the @@ -2202,7 +2150,7 @@ message HeaderMatcher { // // Examples: // - // * The prefix *abcd* matches the value *abcdxyz*, but not for *abcxyz*. + // * The prefix ``abcd`` matches the value ``abcdxyz``, but not for ``abcxyz``. string prefix_match = 9 [ deprecated = true, (validate.rules).string = {min_len: 1}, @@ -2215,7 +2163,7 @@ message HeaderMatcher { // // Examples: // - // * The suffix *abcd* matches the value *xyzabcd*, but not for *xyzbcd*. + // * The suffix ``abcd`` matches the value ``xyzabcd``, but not for ``xyzbcd``. string suffix_match = 10 [ deprecated = true, (validate.rules).string = {min_len: 1}, @@ -2229,7 +2177,7 @@ message HeaderMatcher { // // Examples: // - // * The value *abcd* matches the value *xyzabcdpqr*, but not for *xyzbcdpqr*. + // * The value ``abcd`` matches the value ``xyzabcdpqr``, but not for ``xyzbcdpqr``. string contains_match = 12 [ deprecated = true, (validate.rules).string = {min_len: 1}, @@ -2244,9 +2192,37 @@ message HeaderMatcher { // // Examples: // - // * The regex ``\d{3}`` does not match the value *1234*, so it will match when inverted. + // * The regex ``\d{3}`` does not match the value ``1234``, so it will match when inverted. // * The range [-10,0) will match the value -1, so it will not match when inverted. bool invert_match = 8; + + // If specified, for any header match rule, if the header match rule specified header + // does not exist, this header value will be treated as empty. Defaults to false. + // + // Examples: + // + // * The header match rule specified header "header1" to range match of [0, 10], + // :ref:`invert_match ` + // is set to true and :ref:`treat_missing_header_as_empty ` + // is set to true; The "header1" header is not present. The match rule will + // treat the "header1" as an empty header. The empty header does not match the range, + // so it will match when inverted. + // * The header match rule specified header "header2" to range match of [0, 10], + // :ref:`invert_match ` + // is set to true and :ref:`treat_missing_header_as_empty ` + // is set to false; The "header2" header is not present and the header + // matcher rule for "header2" will be ignored so it will not match. + // * The header match rule specified header "header3" to a string regex match + // ``^$`` which means an empty string, and + // :ref:`treat_missing_header_as_empty ` + // is set to true; The "header3" header is not present. + // The match rule will treat the "header3" header as an empty header so it will match. + // * The header match rule specified header "header4" to a string regex match + // ``^$`` which means an empty string, and + // :ref:`treat_missing_header_as_empty ` + // is set to false; The "header4" header is not present. + // The match rule for "header4" will be ignored so it will not match. + bool treat_missing_header_as_empty = 14; } // Query parameter matching treats the query string of a request's :path header @@ -2261,7 +2237,7 @@ message QueryParameterMatcher { reserved "value", "regex"; // Specifies the name of a key that must be present in the requested - // *path*'s query string. + // ``path``'s query string. string name = 1 [(validate.rules).string = {min_len: 1 max_bytes: 1024}]; oneof query_parameter_match_specifier { diff --git a/api/envoy/extensions/pattern_template/match/v3/BUILD b/api/envoy/extensions/pattern_template/match/v3/BUILD new file mode 100644 index 0000000000000..ee92fb652582e --- /dev/null +++ b/api/envoy/extensions/pattern_template/match/v3/BUILD @@ -0,0 +1,9 @@ +# DO NOT EDIT. This file is generated by tools/proto_format/proto_sync.py. + +load("@envoy_api//bazel:api_build_system.bzl", "api_proto_package") + +licenses(["notice"]) # Apache 2 + +api_proto_package( + deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], +) diff --git a/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto new file mode 100644 index 0000000000000..f2e771458b34c --- /dev/null +++ b/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto @@ -0,0 +1,42 @@ +syntax = "proto3"; + +package envoy.extensions.pattern_template.match.v3; + +import "udpa/annotations/status.proto"; +import "validate/validate.proto"; + +option java_package = "io.envoyproxy.envoy.extensions.pattern_template.match.v3"; +option java_outer_classname = "PatternTemplateMatchProto"; +option java_multiple_files = true; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/match/v3;pattern_templatev3"; +option (udpa.annotations.file_status).package_version_status = ACTIVE; + +// [#extension: envoy.pattern_template.pattern_template_predicate] +// If specified, the route is a template match rule meaning that the +// ``:path`` header (without the query string) must match the given +// ``path_template`` pattern. +// +// Path template matching types: +// +// * ``*`` : Matches a single path component, up to the next path separator: / +// +// * ``**`` : Matches zero or more path segments. If present, must be the last operator. +// +// * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. +// +// * ``{name=videos/*}`` : A named variable matching more than one path segment. +// The path component matching videos/* is captured as the named variable. +// +// * ``{name=**}`` : A named variable matching zero or more path segments. +// +// +// For example: +// +// * ``/videos/*/*/*.m4s`` would match ``videos/123414/hls/1080p5000_00001.m4s`` +// +// * ``/videos/{file}`` would match ``/videos/1080p5000_00001.m4s`` +// +// * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` +message PatternTemplateMatchConfig { + string path_template = 1 [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; +} diff --git a/api/envoy/extensions/pattern_template/rewrite/v3/BUILD b/api/envoy/extensions/pattern_template/rewrite/v3/BUILD new file mode 100644 index 0000000000000..ee92fb652582e --- /dev/null +++ b/api/envoy/extensions/pattern_template/rewrite/v3/BUILD @@ -0,0 +1,9 @@ +# DO NOT EDIT. This file is generated by tools/proto_format/proto_sync.py. + +load("@envoy_api//bazel:api_build_system.bzl", "api_proto_package") + +licenses(["notice"]) # Apache 2 + +api_proto_package( + deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], +) diff --git a/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto new file mode 100644 index 0000000000000..9a8429400f89f --- /dev/null +++ b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto @@ -0,0 +1,75 @@ +syntax = "proto3"; + +package envoy.extensions.pattern_template.rewrite.v3; + +import "udpa/annotations/status.proto"; +import "validate/validate.proto"; + +option java_package = "io.envoyproxy.envoy.extensions.pattern_template.rewrite.v3"; +option java_outer_classname = "PatternTemplateRewriteProto"; +option java_multiple_files = true; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/rewrite/v3;pattern_templatev3"; +option (udpa.annotations.file_status).package_version_status = ACTIVE; + +// [#extension: envoy.pattern_template.pattern_template_predicate] +// Indicates that during forwarding, portions of the path that match the +// pattern should be rewritten, even allowing the substitution of variables +// from the match pattern into the new path as specified by the rewrite template. +// This is useful to allow application paths to be +// rewritten in a way that is aware of segments with variable content like +// identifiers. The router filter will place the original path as it was +// before the rewrite into the :ref:`x-envoy-original-path +// ` header. +// +// Only one of :ref:`prefix_rewrite `, +// :ref:`regex_rewrite `, +// or *path_template_rewrite* may be specified. +// +// Template pattern matching types: +// +// * ``*`` : Matches a single path component, up to the next path separator: / +// +// * ``**`` : Matches zero or more path segments. If present, must be the last operator. +// +// * ``{name} or {name=*}`` : A named variable matching one path segment up to the next path separator: /. +// +// * ``{name=videos/*}`` : A named variable matching more than one path segment. +// The path component matching videos/* is captured as the named variable. +// +// * ``{name=**}`` : A named variable matching zero or more path segments. +// +// Only named matches can be used to perform rewrites. +// +// Examples using path_template_rewrite: +// +// * The pattern ``/{one}/{two}`` paired with a substitution string of ``/{two}/{one}`` would +// transform ``/cat/dog`` into ``/dog/cat``. +// +// * The pattern ``/videos/{language=lang/*}/*`` paired with a substitution string of +// ``/{language}`` would transform ``/videos/lang/en/video.m4s`` into ``lang/en``. +// +// * The path pattern ``/content/{format}/{lang}/{id}/{file}.vtt`` paired with a substitution +// string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` +// into ``/en-us/hls/en_193913.vtt``. +message PatternTemplateRewriteConfig { + string path_template_rewrite = 1 + [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; +} + +// Holds the segments for rewriting urls base on pattern templates +message PatternTemplateRewrite { + message RewriteSegment { + oneof segment_type { + // Represents a segment of the rewritten URL, including any path segments, + // slash and prefix. + string literal = 1; + + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int32 var_index = 2; + } + } + + repeated RewriteSegment segments = 3; +} diff --git a/api/versioning/BUILD b/api/versioning/BUILD index fd85c4e0c18f5..67056b06f065f 100644 --- a/api/versioning/BUILD +++ b/api/versioning/BUILD @@ -118,6 +118,7 @@ proto_library( "//envoy/extensions/filters/http/oauth2/v3:pkg", "//envoy/extensions/filters/http/on_demand/v3:pkg", "//envoy/extensions/filters/http/original_src/v3:pkg", + "//envoy/extensions/filters/http/rate_limit_quota/v3:pkg", "//envoy/extensions/filters/http/ratelimit/v3:pkg", "//envoy/extensions/filters/http/rbac/v3:pkg", "//envoy/extensions/filters/http/router/v3:pkg", @@ -179,7 +180,10 @@ proto_library( "//envoy/extensions/matching/input_matchers/ip/v3:pkg", "//envoy/extensions/network/dns_resolver/apple/v3:pkg", "//envoy/extensions/network/dns_resolver/cares/v3:pkg", + "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", + "//envoy/extensions/pattern_template/match/v3:pkg", + "//envoy/extensions/pattern_template/rewrite/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", @@ -195,6 +199,7 @@ proto_library( "//envoy/extensions/stat_sinks/graphite_statsd/v3:pkg", "//envoy/extensions/stat_sinks/wasm/v3:pkg", "//envoy/extensions/transport_sockets/alts/v3:pkg", + "//envoy/extensions/transport_sockets/http_11_proxy/v3:pkg", "//envoy/extensions/transport_sockets/internal_upstream/v3:pkg", "//envoy/extensions/transport_sockets/proxy_protocol/v3:pkg", "//envoy/extensions/transport_sockets/quic/v3:pkg", @@ -224,6 +229,7 @@ proto_library( "//envoy/service/listener/v3:pkg", "//envoy/service/load_stats/v3:pkg", "//envoy/service/metrics/v3:pkg", + "//envoy/service/rate_limit_quota/v3:pkg", "//envoy/service/ratelimit/v3:pkg", "//envoy/service/route/v3:pkg", "//envoy/service/runtime/v3:pkg", From 2dadf3ddd403bbe1b9981cbb14417a0b11e9f04c Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 20 Jul 2022 17:03:06 +0000 Subject: [PATCH 036/238] Proto changes for pattern template Signed-off-by: silverstar195 --- api/BUILD | 2 - .../config/route/v3/route_components.proto | 222 +++++++----------- 2 files changed, 89 insertions(+), 135 deletions(-) diff --git a/api/BUILD b/api/BUILD index 611430e42be74..74d444b3c20d0 100644 --- a/api/BUILD +++ b/api/BUILD @@ -238,7 +238,6 @@ proto_library( "//envoy/extensions/matching/input_matchers/ip/v3:pkg", "//envoy/extensions/network/dns_resolver/apple/v3:pkg", "//envoy/extensions/network/dns_resolver/cares/v3:pkg", - "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", "//envoy/extensions/pattern_template/match/v3:pkg", "//envoy/extensions/pattern_template/rewrite/v3:pkg", @@ -257,7 +256,6 @@ proto_library( "//envoy/extensions/stat_sinks/graphite_statsd/v3:pkg", "//envoy/extensions/stat_sinks/wasm/v3:pkg", "//envoy/extensions/transport_sockets/alts/v3:pkg", - "//envoy/extensions/transport_sockets/http_11_proxy/v3:pkg", "//envoy/extensions/transport_sockets/internal_upstream/v3:pkg", "//envoy/extensions/transport_sockets/proxy_protocol/v3:pkg", "//envoy/extensions/transport_sockets/quic/v3:pkg", diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index 4315c291c774b..e07be6d1d3dc5 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -91,11 +91,11 @@ message VirtualHost { // The list of routes that will be matched, in order, for incoming requests. // The first route that matches will be used. - // Only one of this and ``matcher`` can be specified. + // Only one of this and `matcher` can be specified. repeated Route routes = 3; // [#next-major-version: This should be included in a oneof with routes wrapped in a message.] - // The match tree to use when resolving route actions for incoming requests. Only one of this and ``routes`` + // The match tree to use when resolving route actions for incoming requests. Only one of this and `routes` // can be specified. xds.type.matcher.v3.Matcher matcher = 21 [(xds.annotations.v3.field_status).work_in_progress = true]; @@ -145,15 +145,11 @@ message VirtualHost { // Indicates that the virtual host has a CORS policy. CorsPolicy cors = 8; - // The per_filter_config field can be used to provide virtual host-specific configurations for filters. - // The key should match the :ref:`filter config name - // `. - // The canonical filter name (e.g., ``envoy.filters.http.buffer`` for the HTTP buffer filter) can also - // be used for the backwards compatibility. If there is no entry referred by the filter config name, the - // entry referred by the canonical filter name will be provided to the filters as fallback. - // - // Use of this field is filter specific; - // see the :ref:`HTTP filter documentation ` for if and how it is utilized. + // The per_filter_config field can be used to provide virtual host-specific + // configurations for filters. The key should match the filter name, such as + // *envoy.filters.http.buffer* for the HTTP buffer filter. Use of this field is filter + // specific; see the :ref:`HTTP filter documentation ` + // for if and how it is utilized. // [#comment: An entry's value may be wrapped in a // :ref:`FilterConfig` // message to specify additional options.] @@ -267,21 +263,17 @@ message Route { // about the route. It can be used for configuration, stats, and logging. // The metadata should go under the filter namespace that will need it. // For instance, if the metadata is intended for the Router filter, - // the filter name should be specified as ``envoy.filters.http.router``. + // the filter name should be specified as *envoy.filters.http.router*. core.v3.Metadata metadata = 4; // Decorator for the matched route. Decorator decorator = 5; - // The per_filter_config field can be used to provide route-specific configurations for filters. - // The key should match the :ref:`filter config name - // `. - // The canonical filter name (e.g., ``envoy.filters.http.buffer`` for the HTTP buffer filter) can also - // be used for the backwards compatibility. If there is no entry referred by the filter config name, the - // entry referred by the canonical filter name will be provided to the filters as fallback. - // - // Use of this field is filter specific; - // see the :ref:`HTTP filter documentation ` for if and how it is utilized. + // The typed_per_filter_config field can be used to provide route-specific + // configurations for filters. The key should match the filter name, such as + // *envoy.filters.http.buffer* for the HTTP buffer filter. Use of this field is filter + // specific; see the :ref:`HTTP filter documentation ` for + // if and how it is utilized. // [#comment: An entry's value may be wrapped in a // :ref:`FilterConfig` // message to specify additional options.] @@ -360,13 +352,13 @@ message WeightedCluster { reserved "per_filter_config"; - // Only one of ``name`` and ``cluster_header`` may be specified. + // Only one of *name* and *cluster_header* may be specified. // [#next-major-version: Need to add back the validation rule: (validate.rules).string = {min_len: 1}] // Name of the upstream cluster. The cluster must exist in the // :ref:`cluster manager configuration `. string name = 1 [(udpa.annotations.field_migrate).oneof_promotion = "cluster_specifier"]; - // Only one of ``name`` and ``cluster_header`` may be specified. + // Only one of *name* and *cluster_header* may be specified. // [#next-major-version: Need to add back the validation rule: (validate.rules).string = {min_len: 1 }] // Envoy will determine the cluster to route to by reading the value of the // HTTP header named by cluster_header from the request headers. If the @@ -375,8 +367,8 @@ message WeightedCluster { // // .. attention:: // - // Internally, Envoy always uses the HTTP/2 ``:authority`` header to represent the HTTP/1 - // ``Host`` header. Thus, if attempting to match on ``Host``, match on ``:authority`` instead. + // Internally, Envoy always uses the HTTP/2 *:authority* header to represent the HTTP/1 + // *Host* header. Thus, if attempting to match on *Host*, match on *:authority* instead. // // .. note:: // @@ -389,14 +381,14 @@ message WeightedCluster { // An integer between 0 and :ref:`total_weight // `. When a request matches the route, // the choice of an upstream cluster is determined by its weight. The sum of weights across all - // entries in the clusters array must add up to the total_weight, if total_weight is greater than 0. + // entries in the clusters array must add up to the total_weight, which defaults to 100. google.protobuf.UInt32Value weight = 2; // Optional endpoint metadata match criteria used by the subset load balancer. Only endpoints in // the upstream cluster with metadata matching what is set in this field will be considered for // load balancing. Note that this will be merged with what's provided in // :ref:`RouteAction.metadata_match `, with - // values here taking precedence. The filter name should be specified as ``envoy.lb``. + // values here taking precedence. The filter name should be specified as *envoy.lb*. core.v3.Metadata metadata_match = 3; // Specifies a list of headers to be added to requests when this cluster is selected @@ -431,16 +423,11 @@ message WeightedCluster { items {string {well_known_regex: HTTP_HEADER_NAME strict: false}} }]; - // The per_filter_config field can be used to provide weighted cluster-specific configurations - // for filters. - // The key should match the :ref:`filter config name - // `. - // The canonical filter name (e.g., ``envoy.filters.http.buffer`` for the HTTP buffer filter) can also - // be used for the backwards compatibility. If there is no entry referred by the filter config name, the - // entry referred by the canonical filter name will be provided to the filters as fallback. - // - // Use of this field is filter specific; - // see the :ref:`HTTP filter documentation ` for if and how it is utilized. + // The per_filter_config field can be used to provide weighted cluster-specific + // configurations for filters. The key should match the filter name, such as + // *envoy.filters.http.buffer* for the HTTP buffer filter. Use of this field is filter + // specific; see the :ref:`HTTP filter documentation ` + // for if and how it is utilized. // [#comment: An entry's value may be wrapped in a // :ref:`FilterConfig` // message to specify additional options.] @@ -458,14 +445,14 @@ message WeightedCluster { repeated ClusterWeight clusters = 1 [(validate.rules).repeated = {min_items: 1}]; // Specifies the total weight across all clusters. The sum of all cluster weights must equal this - // value, if this is greater than 0. - google.protobuf.UInt32Value total_weight = 3; + // value, which must be greater than 0. Defaults to 100. + google.protobuf.UInt32Value total_weight = 3 [(validate.rules).uint32 = {gte: 1}]; // Specifies the runtime key prefix that should be used to construct the - // runtime keys associated with each cluster. When the ``runtime_key_prefix`` is + // runtime keys associated with each cluster. When the *runtime_key_prefix* is // specified, the router will look for weights associated with each upstream - // cluster under the key ``runtime_key_prefix`` + ``.`` + ``cluster[i].name`` where - // ``cluster[i]`` denotes an entry in the clusters array field. If the runtime + // cluster under the key *runtime_key_prefix* + "." + *cluster[i].name* where + // *cluster[i]* denotes an entry in the clusters array field. If the runtime // key for the cluster does not exist, the value specified in the // configuration file will be used as the default weight. See the :ref:`runtime documentation // ` for how key names map to the underlying implementation. @@ -529,17 +516,17 @@ message RouteMatch { option (validate.required) = true; // If specified, the route is a prefix rule meaning that the prefix must - // match the beginning of the ``:path`` header. + // match the beginning of the *:path* header. string prefix = 1; // If specified, the route is an exact path rule meaning that the path must - // exactly match the ``:path`` header once the query string is removed. + // exactly match the *:path* header once the query string is removed. string path = 2; // If specified, the route is a regular expression rule meaning that the - // regex must match the ``:path`` header once the query string is removed. The entire path + // regex must match the *:path* header once the query string is removed. The entire path // (without the query string) must match the regex. The rule will not match if only a - // subsequence of the ``:path`` header matches the regex. + // subsequence of the *:path* header matches the regex. // // [#next-major-version: In the v3 API we should redo how path specification works such // that we utilize StringMatcher, and additionally have consistent options around whether we @@ -605,9 +592,9 @@ message RouteMatch { repeated HeaderMatcher headers = 6; // Specifies a set of URL query parameters on which the route should - // match. The router will check the query string from the ``path`` header + // match. The router will check the query string from the *path* header // against all the specified query parameters. If the number of specified - // query parameters is nonzero, they all must match the ``path`` header's + // query parameters is nonzero, they all must match the *path* header's // query string for a match to occur. // // .. note:: @@ -649,16 +636,16 @@ message CorsPolicy { // string matchers match. repeated type.matcher.v3.StringMatcher allow_origin_string_match = 11; - // Specifies the content for the ``access-control-allow-methods`` header. + // Specifies the content for the *access-control-allow-methods* header. string allow_methods = 2; - // Specifies the content for the ``access-control-allow-headers`` header. + // Specifies the content for the *access-control-allow-headers* header. string allow_headers = 3; - // Specifies the content for the ``access-control-expose-headers`` header. + // Specifies the content for the *access-control-expose-headers* header. string expose_headers = 4; - // Specifies the content for the ``access-control-max-age`` header. + // Specifies the content for the *access-control-max-age* header. string max_age = 5; // Specifies whether the resource allows credentials. @@ -683,7 +670,7 @@ message CorsPolicy { // // If :ref:`runtime_key ` is specified, // Envoy will lookup the runtime key to get the percentage of requests for which it will evaluate - // and track the request's ``Origin`` to determine if it's valid but will not enforce any policies. + // and track the request's *Origin* to determine if it's valid but will not enforce any policies. core.v3.RuntimeFractionalPercent shadow_enabled = 10; } @@ -697,9 +684,6 @@ message RouteAction { // HTTP status code - 404 Not Found. NOT_FOUND = 1; - - // HTTP status code - 500 Internal Server Error. - INTERNAL_SERVER_ERROR = 2; } // Configures :ref:`internal redirect ` behavior. @@ -716,8 +700,8 @@ message RouteAction { // respond before returning the response from the primary cluster. All normal statistics are // collected for the shadow cluster making this feature useful for testing. // - // During shadowing, the host/authority header is altered such that ``-shadow`` is appended. This is - // useful for logging. For example, ``cluster1`` becomes ``cluster1-shadow``. + // During shadowing, the host/authority header is altered such that *-shadow* is appended. This is + // useful for logging. For example, *cluster1* becomes *cluster1-shadow*. // // .. note:: // @@ -731,13 +715,13 @@ message RouteAction { reserved "runtime_key"; - // Only one of ``cluster`` and ``cluster_header`` can be specified. + // Only one of *cluster* and *cluster_header* can be specified. // [#next-major-version: Need to add back the validation rule: (validate.rules).string = {min_len: 1}] // Specifies the cluster that requests will be mirrored to. The cluster must // exist in the cluster manager configuration. string cluster = 1 [(udpa.annotations.field_migrate).oneof_promotion = "cluster_specifier"]; - // Only one of ``cluster`` and ``cluster_header`` can be specified. + // Only one of *cluster* and *cluster_header* can be specified. // Envoy will determine the cluster to route to by reading the value of the // HTTP header named by cluster_header from the request headers. Only the first value in header is used, // and no shadow request will happen if the value is not found in headers. Envoy will not wait for @@ -745,8 +729,8 @@ message RouteAction { // // .. attention:: // - // Internally, Envoy always uses the HTTP/2 ``:authority`` header to represent the HTTP/1 - // ``Host`` header. Thus, if attempting to match on ``Host``, match on ``:authority`` instead. + // Internally, Envoy always uses the HTTP/2 *:authority* header to represent the HTTP/1 + // *Host* header. Thus, if attempting to match on *Host*, match on *:authority* instead. // // .. note:: // @@ -758,7 +742,7 @@ message RouteAction { // If not specified, all requests to the target cluster will be mirrored. // - // If specified, this field takes precedence over the ``runtime_key`` field and requests must also + // If specified, this field takes precedence over the `runtime_key` field and requests must also // fall under the percentage of matches indicated by this field. // // For some fraction N/D, a random number in the range [0,D) is selected. If the @@ -942,11 +926,11 @@ message RouteAction { // If present, and the request contains a `grpc-timeout header // `_, use that value as the - // ``max_stream_duration``, but limit the applied timeout to the maximum value specified here. - // If set to 0, the ``grpc-timeout`` header is used without modification. + // *max_stream_duration*, but limit the applied timeout to the maximum value specified here. + // If set to 0, the `grpc-timeout` header is used without modification. google.protobuf.Duration grpc_timeout_header_max = 2; - // If present, Envoy will adjust the timeout provided by the ``grpc-timeout`` header by + // If present, Envoy will adjust the timeout provided by the `grpc-timeout` header by // subtracting the provided duration from the header. This is useful for allowing Envoy to set // its global timeout to be less than that of the deadline imposed by the calling client, which // makes it more likely that Envoy will handle the timeout instead of having the call canceled @@ -973,8 +957,8 @@ message RouteAction { // // .. attention:: // - // Internally, Envoy always uses the HTTP/2 ``:authority`` header to represent the HTTP/1 - // ``Host`` header. Thus, if attempting to match on ``Host``, match on ``:authority`` instead. + // Internally, Envoy always uses the HTTP/2 *:authority* header to represent the HTTP/1 + // *Host* header. Thus, if attempting to match on *Host*, match on *:authority* instead. // // .. note:: // @@ -1009,7 +993,7 @@ message RouteAction { // in the upstream cluster with metadata matching what's set in this field will be considered // for load balancing. If using :ref:`weighted_clusters // `, metadata will be merged, with values - // provided there taking precedence. The filter name should be specified as ``envoy.lb``. + // provided there taking precedence. The filter name should be specified as *envoy.lb*. core.v3.Metadata metadata_match = 4; // Indicates that during forwarding, the matched prefix (or path) should be @@ -1020,14 +1004,14 @@ message RouteAction { // // Only one of :ref:`regex_rewrite ` // [#comment:TODO(silverstar194) add the following once path_template_rewrite is implemented: :ref:`path_template_rewrite `] - // or ``prefix_rewrite`` may be specified. + // or *prefix_rewrite* may be specified. // // .. attention:: // // Pay careful attention to the use of trailing slashes in the // :ref:`route's match ` prefix value. // Stripping a prefix from a path requires multiple Routes to handle all cases. For example, - // rewriting ``/prefix`` to ``/`` and ``/prefix/etc`` to ``/etc`` cannot be done in a single + // rewriting */prefix* to */* and */prefix/etc* to */etc* cannot be done in a single // :ref:`Route `, as shown by the below config entries: // // .. code-block:: yaml @@ -1041,8 +1025,8 @@ message RouteAction { // route: // prefix_rewrite: "/" // - // Having above entries in the config, requests to ``/prefix`` will be stripped to ``/``, while - // requests to ``/prefix/etc`` will be stripped to ``/etc``. + // Having above entries in the config, requests to */prefix* will be stripped to */*, while + // requests to */prefix/etc* will be stripped to */etc*. string prefix_rewrite = 5 [(validate.rules).string = {well_known_regex: HTTP_HEADER_VALUE strict: false}]; @@ -1057,7 +1041,7 @@ message RouteAction { // // Only one of :ref:`prefix_rewrite ` // [#comment:TODO(silverstar194) add the following once path_template_rewrite is implemented: :ref:`path_template_rewrite `,] - // or ``regex_rewrite`` may be specified. + // or *regex_rewrite* may be specified. // // Examples using Google's `RE2 `_ engine: // @@ -1091,7 +1075,7 @@ message RouteAction { // Indicates that during forwarding, the host header will be swapped with // the hostname of the upstream host chosen by the cluster manager. This // option is applicable only when the destination cluster for a route is of - // type ``strict_dns`` or ``logical_dns``. Setting this to true with other cluster types + // type *strict_dns* or *logical_dns*. Setting this to true with other cluster types // has no effect. Using this option will append the // :ref:`config_http_conn_man_headers_x-forwarded-host` header if // :ref:`append_x_forwarded_host ` @@ -1134,7 +1118,7 @@ message RouteAction { // regex: "^/(.+)/.+$" // substitution: \1 // - // Would rewrite the host header to ``envoyproxy.io`` given the path ``/envoyproxy.io/some/path``. + // Would rewrite the host header to `envoyproxy.io` given the path `/envoyproxy.io/some/path`. type.matcher.v3.RegexMatchAndSubstitute host_rewrite_path_regex = 35; } @@ -1245,7 +1229,7 @@ message RouteAction { // or its default value (infinity) instead of // :ref:`timeout `, but limit the applied timeout // to the maximum value specified here. If configured as 0, the maximum allowed timeout for - // gRPC requests is infinity. If not configured at all, the ``grpc-timeout`` header is not used + // gRPC requests is infinity. If not configured at all, the `grpc-timeout` header is not used // and gRPC requests time out like any other requests using // :ref:`timeout ` or its default. // This can be used to prevent unexpected upstream request timeouts due to potentially long @@ -1263,7 +1247,7 @@ message RouteAction { [deprecated = true, (envoy.annotations.deprecated_at_minor_version) = "3.0"]; // Deprecated by :ref:`grpc_timeout_header_offset `. - // If present, Envoy will adjust the timeout provided by the ``grpc-timeout`` header by subtracting + // If present, Envoy will adjust the timeout provided by the `grpc-timeout` header by subtracting // the provided duration from the header. This is useful in allowing Envoy to set its global // timeout to be less than that of the deadline imposed by the calling client, which makes it more // likely that Envoy will handle the timeout instead of having the call canceled by the client. @@ -1366,8 +1350,8 @@ message RetryPolicy { }]; // Specifies the maximum interval between retries. This parameter is optional, but must be - // greater than or equal to the ``base_interval`` if set. The default is 10 times the - // ``base_interval``. See :ref:`config_http_filters_router_x-envoy-max-retries` for a discussion + // greater than or equal to the `base_interval` if set. The default is 10 times the + // `base_interval`. See :ref:`config_http_filters_router_x-envoy-max-retries` for a discussion // of Envoy's back-off algorithm. google.protobuf.Duration max_interval = 2 [(validate.rules).duration = {gt {}}]; } @@ -1512,7 +1496,7 @@ message RetryPolicy { // Specifies parameters that control exponential retry back off. This parameter is optional, in which case the // default base interval is 25 milliseconds or, if set, the current value of the - // ``upstream.base_retry_backoff_ms`` runtime parameter. The default maximum interval is 10 times + // `upstream.base_retry_backoff_ms` runtime parameter. The default maximum interval is 10 times // the base interval. The documentation for :ref:`config_http_filters_router_x-envoy-max-retries` // describes Envoy's back-off algorithm. RetryBackOff retry_back_off = 8; @@ -1522,7 +1506,7 @@ message RetryPolicy { // return a response header like ``Retry-After`` or ``X-RateLimit-Reset`` to // provide feedback to the client on how long to wait before retrying. If // configured, this back-off strategy will be used instead of the - // default exponential back off strategy (configured using ``retry_back_off``) + // default exponential back off strategy (configured using `retry_back_off`) // whenever a response includes the matching headers. RateLimitedRetryBackOff rate_limited_retry_back_off = 11; @@ -1589,10 +1573,10 @@ message RedirectAction { } // When the scheme redirection take place, the following rules apply: - // 1. If the source URI scheme is ``http`` and the port is explicitly - // set to ``:80``, the port will be removed after the redirection - // 2. If the source URI scheme is ``https`` and the port is explicitly - // set to ``:443``, the port will be removed after the redirection + // 1. If the source URI scheme is `http` and the port is explicitly + // set to `:80`, the port will be removed after the redirection + // 2. If the source URI scheme is `https` and the port is explicitly + // set to `:443`, the port will be removed after the redirection oneof scheme_rewrite_specifier { // The scheme portion of the URL will be swapped with "https". bool https_redirect = 4; @@ -1686,7 +1670,7 @@ message DirectResponseAction { // // .. note:: // - // Headers can be specified using ``response_headers_to_add`` in the enclosing + // Headers can be specified using *response_headers_to_add* in the enclosing // :ref:`envoy_v3_api_msg_config.route.v3.Route`, :ref:`envoy_v3_api_msg_config.route.v3.RouteConfiguration` or // :ref:`envoy_v3_api_msg_config.route.v3.VirtualHost`. core.v3.DataSource body = 2; @@ -1775,7 +1759,7 @@ message VirtualCluster { reserved "pattern", "method"; // Specifies a list of header matchers to use for matching requests. Each specified header must - // match. The pseudo-headers ``:path`` and ``:method`` can be used to match the request path and + // match. The pseudo-headers `:path` and `:method` can be used to match the request path and // method, respectively. repeated HeaderMatcher headers = 4; @@ -1829,7 +1813,7 @@ message RateLimit { } // The following descriptor entry is appended when a header contains a key that matches the - // ``header_name``: + // *header_name*: // // .. code-block:: cpp // @@ -1873,14 +1857,14 @@ message RateLimit { message MaskedRemoteAddress { // Length of prefix mask len for IPv4 (e.g. 0, 32). // Defaults to 32 when unset. - // For example, trusted address from x-forwarded-for is ``192.168.1.1``, + // For example, trusted address from x-forwarded-for is `192.168.1.1`, // the descriptor entry is ("masked_remote_address", "192.168.1.1/32"); // if mask len is 24, the descriptor entry is ("masked_remote_address", "192.168.1.0/24"). google.protobuf.UInt32Value v4_prefix_mask_len = 1 [(validate.rules).uint32 = {lte: 32}]; // Length of prefix mask len for IPv6 (e.g. 0, 128). // Defaults to 128 when unset. - // For example, trusted address from x-forwarded-for is ``2001:abcd:ef01:2345:6789:abcd:ef01:234``, + // For example, trusted address from x-forwarded-for is `2001:abcd:ef01:2345:6789:abcd:ef01:234`, // the descriptor entry is ("masked_remote_address", "2001:abcd:ef01:2345:6789:abcd:ef01:234/128"); // if mask len is 64, the descriptor entry is ("masked_remote_address", "2001:abcd:ef01:2345::/64"). google.protobuf.UInt32Value v6_prefix_mask_len = 2 [(validate.rules).uint32 = {lte: 128}]; @@ -1912,7 +1896,7 @@ message RateLimit { option (udpa.annotations.versioning).previous_message_type = "envoy.api.v2.route.RateLimit.Action.HeaderValueMatch"; - // The key to use in the descriptor entry. Defaults to ``header_match``. + // The key to use in the descriptor entry. Defaults to `header_match`. string descriptor_key = 4; // The value to use in the descriptor entry. @@ -1949,7 +1933,7 @@ message RateLimit { // only happen if the value in the dynamic metadata is of type string. type.metadata.v3.MetadataKey metadata_key = 2 [(validate.rules).message = {required: true}]; - // An optional value to use if ``metadata_key`` is empty. If not set and + // An optional value to use if *metadata_key* is empty. If not set and // no value is present under the metadata_key then no descriptor is generated. string default_value = 3; } @@ -1975,7 +1959,7 @@ message RateLimit { // only happen if the value in the metadata is of type string. type.metadata.v3.MetadataKey metadata_key = 2 [(validate.rules).message = {required: true}]; - // An optional value to use if ``metadata_key`` is empty. If not set and + // An optional value to use if *metadata_key* is empty. If not set and // no value is present under the metadata_key then no descriptor is generated. string default_value = 3; @@ -2079,12 +2063,12 @@ message RateLimit { // .. attention:: // -// Internally, Envoy always uses the HTTP/2 ``:authority`` header to represent the HTTP/1 ``Host`` -// header. Thus, if attempting to match on ``Host``, match on ``:authority`` instead. +// Internally, Envoy always uses the HTTP/2 *:authority* header to represent the HTTP/1 *Host* +// header. Thus, if attempting to match on *Host*, match on *:authority* instead. // // .. attention:: // -// To route on HTTP method, use the special HTTP/2 ``:method`` header. This works for both +// To route on HTTP method, use the special HTTP/2 *:method* header. This works for both // HTTP/1 and HTTP/2 as Envoy normalizes headers. E.g., // // .. code-block:: json @@ -2101,7 +2085,7 @@ message RateLimit { // value. // // [#next-major-version: HeaderMatcher should be refactored to use StringMatcher.] -// [#next-free-field: 15] +// [#next-free-field: 14] message HeaderMatcher { option (udpa.annotations.versioning).previous_message_type = "envoy.api.v2.route.HeaderMatcher"; @@ -2136,8 +2120,8 @@ message HeaderMatcher { // // Examples: // - // * For range [-10,0), route will match for header value -1, but not for 0, ``somestring``, 10.9, - // ``-1somestring`` + // * For range [-10,0), route will match for header value -1, but not for 0, "somestring", 10.9, + // "-1somestring" type.v3.Int64Range range_match = 6; // If specified as true, header match will be performed based on whether the header is in the @@ -2150,7 +2134,7 @@ message HeaderMatcher { // // Examples: // - // * The prefix ``abcd`` matches the value ``abcdxyz``, but not for ``abcxyz``. + // * The prefix *abcd* matches the value *abcdxyz*, but not for *abcxyz*. string prefix_match = 9 [ deprecated = true, (validate.rules).string = {min_len: 1}, @@ -2163,7 +2147,7 @@ message HeaderMatcher { // // Examples: // - // * The suffix ``abcd`` matches the value ``xyzabcd``, but not for ``xyzbcd``. + // * The suffix *abcd* matches the value *xyzabcd*, but not for *xyzbcd*. string suffix_match = 10 [ deprecated = true, (validate.rules).string = {min_len: 1}, @@ -2177,7 +2161,7 @@ message HeaderMatcher { // // Examples: // - // * The value ``abcd`` matches the value ``xyzabcdpqr``, but not for ``xyzbcdpqr``. + // * The value *abcd* matches the value *xyzabcdpqr*, but not for *xyzbcdpqr*. string contains_match = 12 [ deprecated = true, (validate.rules).string = {min_len: 1}, @@ -2192,37 +2176,9 @@ message HeaderMatcher { // // Examples: // - // * The regex ``\d{3}`` does not match the value ``1234``, so it will match when inverted. + // * The regex ``\d{3}`` does not match the value *1234*, so it will match when inverted. // * The range [-10,0) will match the value -1, so it will not match when inverted. bool invert_match = 8; - - // If specified, for any header match rule, if the header match rule specified header - // does not exist, this header value will be treated as empty. Defaults to false. - // - // Examples: - // - // * The header match rule specified header "header1" to range match of [0, 10], - // :ref:`invert_match ` - // is set to true and :ref:`treat_missing_header_as_empty ` - // is set to true; The "header1" header is not present. The match rule will - // treat the "header1" as an empty header. The empty header does not match the range, - // so it will match when inverted. - // * The header match rule specified header "header2" to range match of [0, 10], - // :ref:`invert_match ` - // is set to true and :ref:`treat_missing_header_as_empty ` - // is set to false; The "header2" header is not present and the header - // matcher rule for "header2" will be ignored so it will not match. - // * The header match rule specified header "header3" to a string regex match - // ``^$`` which means an empty string, and - // :ref:`treat_missing_header_as_empty ` - // is set to true; The "header3" header is not present. - // The match rule will treat the "header3" header as an empty header so it will match. - // * The header match rule specified header "header4" to a string regex match - // ``^$`` which means an empty string, and - // :ref:`treat_missing_header_as_empty ` - // is set to false; The "header4" header is not present. - // The match rule for "header4" will be ignored so it will not match. - bool treat_missing_header_as_empty = 14; } // Query parameter matching treats the query string of a request's :path header @@ -2237,7 +2193,7 @@ message QueryParameterMatcher { reserved "value", "regex"; // Specifies the name of a key that must be present in the requested - // ``path``'s query string. + // *path*'s query string. string name = 1 [(validate.rules).string = {min_len: 1 max_bytes: 1024}]; oneof query_parameter_match_specifier { From cb86b2c381b38c3c0d386ad378d86301955fb36e Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 20 Jul 2022 17:04:30 +0000 Subject: [PATCH 037/238] Proto changes for pattern template Signed-off-by: silverstar195 --- api/versioning/BUILD | 4 ---- 1 file changed, 4 deletions(-) diff --git a/api/versioning/BUILD b/api/versioning/BUILD index 67056b06f065f..8d1413d76205e 100644 --- a/api/versioning/BUILD +++ b/api/versioning/BUILD @@ -118,7 +118,6 @@ proto_library( "//envoy/extensions/filters/http/oauth2/v3:pkg", "//envoy/extensions/filters/http/on_demand/v3:pkg", "//envoy/extensions/filters/http/original_src/v3:pkg", - "//envoy/extensions/filters/http/rate_limit_quota/v3:pkg", "//envoy/extensions/filters/http/ratelimit/v3:pkg", "//envoy/extensions/filters/http/rbac/v3:pkg", "//envoy/extensions/filters/http/router/v3:pkg", @@ -180,7 +179,6 @@ proto_library( "//envoy/extensions/matching/input_matchers/ip/v3:pkg", "//envoy/extensions/network/dns_resolver/apple/v3:pkg", "//envoy/extensions/network/dns_resolver/cares/v3:pkg", - "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", "//envoy/extensions/pattern_template/match/v3:pkg", "//envoy/extensions/pattern_template/rewrite/v3:pkg", @@ -199,7 +197,6 @@ proto_library( "//envoy/extensions/stat_sinks/graphite_statsd/v3:pkg", "//envoy/extensions/stat_sinks/wasm/v3:pkg", "//envoy/extensions/transport_sockets/alts/v3:pkg", - "//envoy/extensions/transport_sockets/http_11_proxy/v3:pkg", "//envoy/extensions/transport_sockets/internal_upstream/v3:pkg", "//envoy/extensions/transport_sockets/proxy_protocol/v3:pkg", "//envoy/extensions/transport_sockets/quic/v3:pkg", @@ -229,7 +226,6 @@ proto_library( "//envoy/service/listener/v3:pkg", "//envoy/service/load_stats/v3:pkg", "//envoy/service/metrics/v3:pkg", - "//envoy/service/rate_limit_quota/v3:pkg", "//envoy/service/ratelimit/v3:pkg", "//envoy/service/route/v3:pkg", "//envoy/service/runtime/v3:pkg", From 19906510f7a479bf47e5bc77870c12a603eab141 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 20 Jul 2022 19:41:55 +0000 Subject: [PATCH 038/238] docs: Cleanup more literals in API docs (#22174) Signed-off-by: Ryan Northey Signed-off-by: silverstar195 # Conflicts: # api/envoy/config/route/v3/route_components.proto --- api/envoy/config/route/v3/route_components.proto | 6 +++--- .../pattern_template/match/v3/pattern_template_match.proto | 6 ++++-- .../rewrite/v3/pattern_template_rewrite.proto | 7 +++++-- source/common/router/config_impl.cc | 5 +++-- source/extensions/filters/http/jwt_authn/matcher.cc | 2 +- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index 4315c291c774b..da26aebefd1b8 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -1018,8 +1018,8 @@ message RouteAction { // place the original path before rewrite into the :ref:`x-envoy-original-path // ` header. // - // Only one of :ref:`regex_rewrite ` - // [#comment:TODO(silverstar194) add the following once path_template_rewrite is implemented: :ref:`path_template_rewrite `] + // Only one of :ref:`regex_rewrite `, + // :ref:`path_rewrite_policy ` // or ``prefix_rewrite`` may be specified. // // .. attention:: @@ -1056,7 +1056,7 @@ message RouteAction { // ` header. // // Only one of :ref:`prefix_rewrite ` - // [#comment:TODO(silverstar194) add the following once path_template_rewrite is implemented: :ref:`path_template_rewrite `,] + // :ref:`path_rewrite_policy ` // or ``regex_rewrite`` may be specified. // // Examples using Google's `RE2 `_ engine: diff --git a/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto index f2e771458b34c..d7e4c702e2ddb 100644 --- a/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto +++ b/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto @@ -8,10 +8,11 @@ import "validate/validate.proto"; option java_package = "io.envoyproxy.envoy.extensions.pattern_template.match.v3"; option java_outer_classname = "PatternTemplateMatchProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/match/v3;pattern_templatev3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/match/v3;matchv3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; -// [#extension: envoy.pattern_template.pattern_template_predicate] +// [#protodoc-title: Pattern Template Match Config] + // If specified, the route is a template match rule meaning that the // ``:path`` header (without the query string) must match the given // ``path_template`` pattern. @@ -37,6 +38,7 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // * ``/videos/{file}`` would match ``/videos/1080p5000_00001.m4s`` // // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` +// [#not-implemented-hide:] message PatternTemplateMatchConfig { string path_template = 1 [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; } diff --git a/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto index 9a8429400f89f..ccffb0c72d0ed 100644 --- a/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto @@ -8,10 +8,11 @@ import "validate/validate.proto"; option java_package = "io.envoyproxy.envoy.extensions.pattern_template.rewrite.v3"; option java_outer_classname = "PatternTemplateRewriteProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/rewrite/v3;pattern_templatev3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/rewrite/v3;rewritev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; -// [#extension: envoy.pattern_template.pattern_template_predicate] +// [#protodoc-title: Pattern Template Rewrite Config] + // Indicates that during forwarding, portions of the path that match the // pattern should be rewritten, even allowing the substitution of variables // from the match pattern into the new path as specified by the rewrite template. @@ -51,12 +52,14 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // * The path pattern ``/content/{format}/{lang}/{id}/{file}.vtt`` paired with a substitution // string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` // into ``/en-us/hls/en_193913.vtt``. +// [#not-implemented-hide:] message PatternTemplateRewriteConfig { string path_template_rewrite = 1 [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; } // Holds the segments for rewriting urls base on pattern templates +// [#not-implemented-hide:] message PatternTemplateRewrite { message RewriteSegment { oneof segment_type { diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 47e4cec240b19..1e8206e8e47f0 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -100,7 +100,7 @@ RouteEntryImplBaseConstSharedPtr createAndValidateRoute( vhost, route_config, optional_http_filters, factory_context, validator); break; } - case envoy::config::route::v3::RouteMatch::PathSpecifierCase::kPathTemplate: { + case envoy::config::route::v3::RouteMatch::PathSpecifierCase::kPathMatchPolicy: { route = std::make_shared(vhost, route_config, optional_http_filters, factory_context, validator); break; @@ -1382,7 +1382,8 @@ PathTemplateRouteEntryImpl::PathTemplateRouteEntryImpl( Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - path_template_(route.match().path_template()), + //TODO(silverstar194) Implement path temnplate matcher + path_template_(""), path_matcher_(Matchers::PathMatcher::createPattern(path_template_, !case_sensitive_)) {} void PathTemplateRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, diff --git a/source/extensions/filters/http/jwt_authn/matcher.cc b/source/extensions/filters/http/jwt_authn/matcher.cc index f717f7fa044e6..d36f27483b677 100644 --- a/source/extensions/filters/http/jwt_authn/matcher.cc +++ b/source/extensions/filters/http/jwt_authn/matcher.cc @@ -194,7 +194,7 @@ MatcherConstPtr Matcher::create(const RequirementRule& rule) { return std::make_unique(rule); case RouteMatch::PathSpecifierCase::kPathSeparatedPrefix: return std::make_unique(rule); - case RouteMatch::PathSpecifierCase::kPathTemplate: + case RouteMatch::PathSpecifierCase::kPathMatchPolicy: // TODO(silverstar194): Implement matcher for template based match break; case RouteMatch::PathSpecifierCase::PATH_SPECIFIER_NOT_SET: From 403349ac652f2b1bdef1a393b76c1f6d565b58ff Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 20 Jul 2022 19:51:12 +0000 Subject: [PATCH 039/238] Add space Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 1e8206e8e47f0..2020b56315204 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1382,7 +1382,7 @@ PathTemplateRouteEntryImpl::PathTemplateRouteEntryImpl( Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - //TODO(silverstar194) Implement path temnplate matcher + // TODO(silverstar194) Implement path temnplate matcher path_template_(""), path_matcher_(Matchers::PathMatcher::createPattern(path_template_, !case_sensitive_)) {} From e7d3877130f20734493df3fd32132900c9ed27b3 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 20 Jul 2022 22:54:08 +0000 Subject: [PATCH 040/238] Spelling Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 2020b56315204..2a173fd3ebee9 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1382,7 +1382,7 @@ PathTemplateRouteEntryImpl::PathTemplateRouteEntryImpl( Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - // TODO(silverstar194) Implement path temnplate matcher + // TODO(silverstar194) Implement path template matcher path_template_(""), path_matcher_(Matchers::PathMatcher::createPattern(path_template_, !case_sensitive_)) {} From 37c854af7a14a9ad222dd183f30251b7b69f9b3f Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 13:35:36 +0000 Subject: [PATCH 041/238] Small style and grammar changes Signed-off-by: silverstar195 --- api/envoy/config/route/v3/route_components.proto | 6 +++--- .../pattern_template/match/v3/pattern_template_match.proto | 2 +- .../rewrite/v3/pattern_template_rewrite.proto | 6 +++--- source/common/router/config_impl.cc | 6 ++++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index da26aebefd1b8..a12f0612d04af 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -1055,9 +1055,9 @@ message RouteAction { // before the rewrite into the :ref:`x-envoy-original-path // ` header. // - // Only one of :ref:`prefix_rewrite ` - // :ref:`path_rewrite_policy ` - // or ``regex_rewrite`` may be specified. + // Only one of ``regex_rewrite``, :ref:`prefix_rewrite `, + // or :ref:`path_rewrite_policy ` + // may be specified. // // Examples using Google's `RE2 `_ engine: // diff --git a/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto b/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto index d7e4c702e2ddb..2f3db17bbcfa9 100644 --- a/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto +++ b/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto @@ -40,5 +40,5 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` // [#not-implemented-hide:] message PatternTemplateMatchConfig { - string path_template = 1 [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; + string path_template = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; } diff --git a/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto index ccffb0c72d0ed..623c6767813b6 100644 --- a/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto @@ -55,10 +55,10 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#not-implemented-hide:] message PatternTemplateRewriteConfig { string path_template_rewrite = 1 - [(validate.rules).string = {min_len: 1 max_len: 256 ignore_empty: true}]; + [(validate.rules).string = {min_len: 1 max_len: 256}]; } -// Holds the segments for rewriting urls base on pattern templates +// Holds the segments for rewriting urls based on pattern templates // [#not-implemented-hide:] message PatternTemplateRewrite { message RewriteSegment { @@ -67,7 +67,7 @@ message PatternTemplateRewrite { // slash and prefix. string literal = 1; - // Represents an index into the RE2 capture which value should be used + // Indexes into the RE2 capture for the value to be used // to construct the rewritten URL. Note that the index should be greater // than 0 as 0 index into the whole match RE2 pattern. int32 var_index = 2; diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 2a173fd3ebee9..8ef4ea1677be0 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1382,9 +1382,11 @@ PathTemplateRouteEntryImpl::PathTemplateRouteEntryImpl( Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - // TODO(silverstar194) Implement path template matcher path_template_(""), - path_matcher_(Matchers::PathMatcher::createPattern(path_template_, !case_sensitive_)) {} + path_matcher_(Matchers::PathMatcher::createPattern(path_template_, !case_sensitive_)) { + // TODO(silverstar194) Implement path template matcher + throw absl::UnimplementedError("Path template matcher not implemented"); +} void PathTemplateRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { From 41658a0e1db1cea25d02156147f83157fbb04584 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 13:43:42 +0000 Subject: [PATCH 042/238] Change spacing Signed-off-by: silverstar195 --- .../pattern_template/rewrite/v3/pattern_template_rewrite.proto | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto index 623c6767813b6..696f29b768b2e 100644 --- a/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto @@ -54,8 +54,7 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // into ``/en-us/hls/en_193913.vtt``. // [#not-implemented-hide:] message PatternTemplateRewriteConfig { - string path_template_rewrite = 1 - [(validate.rules).string = {min_len: 1 max_len: 256}]; + string path_template_rewrite = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; } // Holds the segments for rewriting urls based on pattern templates From 66d25b2db9d83480412ad3ce4b5024a2d9a510b3 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 14:00:38 +0000 Subject: [PATCH 043/238] Remove proto Signed-off-by: silverstar195 --- .../rewrite/v3/pattern_template_rewrite.proto | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto index 696f29b768b2e..a196e18a5586e 100644 --- a/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto @@ -56,22 +56,3 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; message PatternTemplateRewriteConfig { string path_template_rewrite = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; } - -// Holds the segments for rewriting urls based on pattern templates -// [#not-implemented-hide:] -message PatternTemplateRewrite { - message RewriteSegment { - oneof segment_type { - // Represents a segment of the rewritten URL, including any path segments, - // slash and prefix. - string literal = 1; - - // Indexes into the RE2 capture for the value to be used - // to construct the rewritten URL. Note that the index should be greater - // than 0 as 0 index into the whole match RE2 pattern. - int32 var_index = 2; - } - } - - repeated RewriteSegment segments = 3; -} From 97d5b4869cc1a4bca2f4c9a3351c5885b9a795a9 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 14:11:47 +0000 Subject: [PATCH 044/238] Removed old test Signed-off-by: silverstar195 --- test/common/router/config_impl_test.cc | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index efd89c4da8a84..6d8473fef8933 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -5102,24 +5102,6 @@ TEST_F(RouteMatcherTest, TestPrefixAndRegexRewrites) { "Cannot specify both prefix_rewrite and regex_rewrite"); } -TEST_F(RouteMatcherTest, TestPatternRewriteConfigLoad) { - const std::string yaml = R"EOF( -virtual_hosts: -- name: path_template_rewrite - domains: ["*"] - routes: - - match: - path_template: "/bar/{country}/{lang}" - route: - path_template_rewrite: "/bar/{lang}/{country}" - cluster: www2 - )EOF"; - - NiceMock stream_info; - factory_context_.cluster_manager_.initializeClusters({"www2"}, {}); - TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); -} - TEST_F(RouteMatcherTest, TestDomainMatchOrderConfig) { const std::string yaml = R"EOF( virtual_hosts: From 239ef6aa8ab977bb40a517fe34559cda2a4eda49 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 15:06:58 +0000 Subject: [PATCH 045/238] Moved proto inside extension Signed-off-by: silverstar195 --- source/extensions/pattern_template/BUILD | 11 +++++++ .../pattern_template/pattern_template.cc | 6 ++-- .../pattern_template/pattern_template.h | 5 +-- .../pattern_template_rewrite_segments.proto | 31 +++++++++++++++++++ .../rewrite/pattern_template_rewrite.cc | 10 +++--- .../rewrite/pattern_template_rewrite.h | 3 +- 6 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 source/extensions/pattern_template/proto/pattern_template_rewrite_segments.proto diff --git a/source/extensions/pattern_template/BUILD b/source/extensions/pattern_template/BUILD index 09a6ec9a7824f..fb5803523b7aa 100644 --- a/source/extensions/pattern_template/BUILD +++ b/source/extensions/pattern_template/BUILD @@ -4,6 +4,7 @@ load( "envoy_cc_library", "envoy_cc_test", "envoy_extension_package", + "envoy_proto_library", ) licenses(["notice"]) # Apache 2 @@ -12,6 +13,12 @@ licenses(["notice"]) # Apache 2 envoy_extension_package() +envoy_proto_library( + name = "pattern_template_rewrite_segements", + srcs = ["proto/pattern_template_rewrite_segments.proto"], + deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], +) + envoy_cc_library( name = "pattern_template_lib", srcs = ["pattern_template.cc"], @@ -23,6 +30,7 @@ envoy_cc_library( ], deps = [ ":pattern_template_internal_cc", + ":pattern_template_rewrite_segements_cc_proto", "//envoy/router:router_pattern_template_interface", "//source/common/http:path_utility_lib", "@com_google_absl//absl/status", @@ -39,6 +47,7 @@ envoy_cc_library( srcs = ["pattern_template_internal.cc"], hdrs = ["pattern_template_internal.h"], deps = [ + ":pattern_template_rewrite_segements_cc_proto", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/flags:flag", "@com_google_absl//absl/functional:function_ref", @@ -56,6 +65,7 @@ envoy_cc_test( srcs = ["pattern_template_test.cc"], deps = [ ":pattern_template", + ":pattern_template_rewrite_segements_cc_proto", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", @@ -68,6 +78,7 @@ envoy_cc_test( srcs = ["pattern_template_internal_test.cc"], deps = [ ":pattern_template_internal_cc", + ":pattern_template_rewrite_segements_cc_proto", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", diff --git a/source/extensions/pattern_template/pattern_template.cc b/source/extensions/pattern_template/pattern_template.cc index a94da25bffb58..033d14173b9e0 100644 --- a/source/extensions/pattern_template/pattern_template.cc +++ b/source/extensions/pattern_template/pattern_template.cc @@ -5,7 +5,7 @@ #include #include -#include "envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.pb.h" +#include "source/extensions/pattern_template/proto/pattern_template_rewrite_segments.pb.h" #include "source/common/http/path_utility.h" #include "source/extensions/pattern_template/pattern_template_internal.h" @@ -83,9 +83,9 @@ parseRewritePatternHelper(absl::string_view pattern) { return result; } -absl::StatusOr +absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { - envoy::extensions::pattern_template::rewrite::v3::PatternTemplateRewrite parsed_pattern; + envoy::extensions::pattern_template::PatternTemplateRewriteSegments parsed_pattern; RE2 regex = RE2(ToStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); diff --git a/source/extensions/pattern_template/pattern_template.h b/source/extensions/pattern_template/pattern_template.h index 79ecc4ea52bc6..432c948078107 100644 --- a/source/extensions/pattern_template/pattern_template.h +++ b/source/extensions/pattern_template/pattern_template.h @@ -3,7 +3,8 @@ #include -#include "envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.pb.h" +#include "source/extensions/pattern_template/proto/pattern_template_rewrite_segments.pb.h" + #include "envoy/router/pattern_template.h" #include "source/extensions/pattern_template/pattern_template_internal.h" @@ -38,7 +39,7 @@ parseRewritePatternHelper(absl::string_view pattern); // Returns the parsed Url rewrite pattern to be used by // RewriteURLTemplatePattern() |capture_regex| should // be the regex generated by ConvertURLPatternSyntaxToRegex(). -absl::StatusOr +absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); // Returns if provided rewrite pattern is valid diff --git a/source/extensions/pattern_template/proto/pattern_template_rewrite_segments.proto b/source/extensions/pattern_template/proto/pattern_template_rewrite_segments.proto new file mode 100644 index 0000000000000..0cd054e9dbbd8 --- /dev/null +++ b/source/extensions/pattern_template/proto/pattern_template_rewrite_segments.proto @@ -0,0 +1,31 @@ +syntax = "proto3"; + +package envoy.extensions.pattern_template; + +import "udpa/annotations/status.proto"; + +option java_package = "io.envoyproxy.envoy.extensions.pattern_template"; +option java_outer_classname = "PatternTemplateRewrite"; +option java_multiple_files = true; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/pattern_template"; +option (udpa.annotations.file_status).package_version_status = ACTIVE; + +// [#protodoc-title: Pattern Template Rewrite] + +// Holds the segments for rewriting urls base on pattern templates +message PatternTemplateRewriteSegments { + message RewriteSegment { + oneof segment_type { + // Represents a segment of the rewritten URL, including any path segments, + // slash and prefix. + string literal = 1; + + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int32 var_index = 2; + } + } + + repeated RewriteSegment segments = 3; +} diff --git a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc index 1ec678adad924..6bf9f36fd211c 100644 --- a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc +++ b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc @@ -5,7 +5,7 @@ #include #include -#include "envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.pb.h" +#include "source/extensions/pattern_template/proto/pattern_template_rewrite_segments.pb.h" #include "source/common/http/path_utility.h" #include "source/extensions/pattern_template/pattern_template_internal.h" @@ -57,14 +57,14 @@ PatternTemplateRewritePredicate::rewritePattern(absl::string_view current_patter } std::string regex_pattern_str = *std::move(regex_pattern); - absl::StatusOr rewrite_pattern = + absl::StatusOr rewrite_pattern = parseRewritePattern(url_rewrite_pattern_, regex_pattern_str); if (!rewrite_pattern.ok()) { return absl::InvalidArgumentError("Unable to parse url rewrite pattern"); } - envoy::extensions::pattern_template::rewrite::v3::PatternTemplateRewrite rewrite_pattern_proto = + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_pattern_proto = *std::move(rewrite_pattern); absl::StatusOr new_path = @@ -79,7 +79,7 @@ PatternTemplateRewritePredicate::rewritePattern(absl::string_view current_patter absl::StatusOr PatternTemplateRewritePredicate::rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::pattern_template::rewrite::v3::PatternTemplateRewrite& rewrite_pattern) const { + const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern) const { RE2 regex = RE2(PatternTemplateInternal::toStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); @@ -95,7 +95,7 @@ absl::StatusOr PatternTemplateRewritePredicate::rewriteURLTemplateP std::string rewritten_url; - for (const envoy::extensions::pattern_template::rewrite::v3::PatternTemplateRewrite::RewriteSegment& + for (const envoy::extensions::pattern_template::PatternTemplateRewriteSegments::RewriteSegment& segment : rewrite_pattern.segments()) { if (segment.has_literal()) { absl::StrAppend(&rewritten_url, segment.literal()); diff --git a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h index a1c966c6e6c43..203e9ce465fbd 100644 --- a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h +++ b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h @@ -3,7 +3,6 @@ #include -#include "envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.pb.h" #include "envoy/router/pattern_template.h" #include "source/extensions/pattern_template/pattern_template.h" @@ -37,7 +36,7 @@ class PatternTemplateRewritePredicate : public Router::PatternTemplateRewritePre // Used for template-based URL rewrite. absl::StatusOr rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::pattern_template::rewrite::v3::PatternTemplateRewrite& + const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern) const; RE2 matching_pattern_regex_{nullptr}; From 662047ad2e5bf6a029a458fe724f98d01cd3db33 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 15:57:16 +0000 Subject: [PATCH 046/238] Start towards generalizing pattern_match -> path_match Signed-off-by: silverstar195 --- envoy/router/path_match_policy.h | 73 +++++++++++++++++++ envoy/router/pattern_template.h | 73 ------------------- envoy/router/router.h | 12 +-- envoy/router/url_template.h | 60 --------------- source/common/router/BUILD | 6 +- source/common/router/config_impl.cc | 48 ++++++------ source/common/router/config_impl.h | 34 ++++----- source/extensions/pattern_template/BUILD | 14 +--- .../extensions/pattern_template/match/BUILD | 3 + .../extensions/pattern_template/proto/BUILD | 17 +++++ .../extensions/pattern_template/rewrite/BUILD | 3 + test/mocks/router/mocks.h | 8 +- 12 files changed, 157 insertions(+), 194 deletions(-) create mode 100644 envoy/router/path_match_policy.h delete mode 100644 envoy/router/pattern_template.h delete mode 100644 envoy/router/url_template.h create mode 100644 source/extensions/pattern_template/proto/BUILD diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h new file mode 100644 index 0000000000000..43cc41c356690 --- /dev/null +++ b/envoy/router/path_match_policy.h @@ -0,0 +1,73 @@ +#pragma once + +#include "envoy/config/typed_config.h" + +#include "source/common/common/logger.h" + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" + +namespace Envoy { +namespace Router { + +/** + * Used to decide if path match is needed based on the target route. + * Subclassing Logger::Loggable so that implementations can log details. + */ +class PathMatchPredicate : Logger::Loggable { +public: + PathMatchPredicate() = default; + virtual ~PathMatchPredicate() = default; + + virtual absl::string_view name() const PURE; + + virtual bool match(absl::string_view pattern) const PURE; +}; + +using PathMatchPredicateSharedPtr = std::shared_ptr; + +/** + * Factory for PatternTemplateMatchPredicate. + */ +class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { +public: + virtual ~PathMatchPredicateFactory() = default; + + virtual PathMatchPredicateFactory + createPathMatchPredicate(std::string url_pattern) PURE; + + std::string category() const override { return "envoy.pattern_template"; } +}; + +/** + * Used to decide if pattern template rewrite is needed based on the target route. + * Subclassing Logger::Loggable so that implementations can log details. + */ +class PathRewritePredicate : Logger::Loggable { +public: + PathRewritePredicate() = default; + virtual ~PathRewritePredicate() = default; + + virtual absl::string_view name() const PURE; + + virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const PURE; +}; + +using PathRewritePredicateSharedPtr = std::shared_ptr; + +/** + * Factory for PatternRewriteTemplatePredicate. + */ +class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { +public: + virtual ~PathRewritePredicateFactory() override = default; + + virtual PathRewritePredicateSharedPtr + createPathRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) PURE; + + std::string category() const override { return "envoy.pattern_template"; } +}; + +} // namespace Router +} // namespace Envoy diff --git a/envoy/router/pattern_template.h b/envoy/router/pattern_template.h deleted file mode 100644 index f77359aaba4ce..0000000000000 --- a/envoy/router/pattern_template.h +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once - -#include "envoy/config/typed_config.h" - -#include "source/common/common/logger.h" - -#include "absl/status/statusor.h" -#include "absl/strings/string_view.h" - -namespace Envoy { -namespace Router { - -/** - * Used to decide if pattern template match is needed based on the target route. - * Subclassing Logger::Loggable so that implementations can log details. - */ -class PatternTemplateMatchPredicate : Logger::Loggable { -public: - PatternTemplateMatchPredicate() = default; - virtual ~PatternTemplateMatchPredicate() = default; - - virtual absl::string_view name() const PURE; - - virtual bool match(absl::string_view pattern) const PURE; -}; - -using PatternTemplateMatchPredicateSharedPtr = std::shared_ptr; - -/** - * Factory for PatternTemplateMatchPredicate. - */ -class PatternTemplateMatchPredicateFactory : public Envoy::Config::TypedFactory { -public: - virtual ~PatternTemplateMatchPredicateFactory() = default; - - virtual PatternTemplateMatchPredicateSharedPtr - createUrlTemplateMatchPredicate(std::string url_pattern) PURE; - - std::string category() const override { return "envoy.pattern_template"; } -}; - -/** - * Used to decide if pattern template rewrite is needed based on the target route. - * Subclassing Logger::Loggable so that implementations can log details. - */ -class PatternTemplateRewritePredicate : Logger::Loggable { -public: - PatternTemplateRewritePredicate() = default; - virtual ~PatternTemplateRewritePredicate() = default; - - virtual absl::string_view name() const PURE; - - virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, - absl::string_view matched_path) const PURE; -}; - -using PatternTemplateRewritePredicateSharedPtr = std::shared_ptr; - -/** - * Factory for PatternRewriteTemplatePredicate. - */ -class PatternTemplateRewritePredicateFactory : public Envoy::Config::TypedFactory { -public: - virtual ~PatternTemplateRewritePredicateFactory() override = default; - - virtual PatternTemplateRewritePredicateSharedPtr - createUrlTemplateRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) PURE; - - std::string category() const override { return "envoy.pattern_template"; } -}; - -} // namespace Router -} // namespace Envoy diff --git a/envoy/router/router.h b/envoy/router/router.h index 4436cf61f4944..ee0b464b8f65d 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -20,7 +20,7 @@ #include "envoy/http/hash_policy.h" #include "envoy/rds/config.h" #include "envoy/router/internal_redirect.h" -#include "envoy/router/url_template.h" +#include "envoy/router/path_match_policy.h" #include "envoy/tcp/conn_pool.h" #include "envoy/tracing/http_tracer.h" #include "envoy/type/v3/percent.pb.h" @@ -298,9 +298,9 @@ class RetryPolicy { */ enum class RetryStatus { No, NoOverflow, NoRetryLimitExceeded, Yes }; -class PatternTemplatePolicy { +class PathMatchPolicyImpl { public: - virtual ~PatternTemplatePolicy() = default; + virtual ~PathMatchPolicyImpl() = default; /** * @return whether internal redirect is enabled on this route. @@ -313,7 +313,7 @@ class PatternTemplatePolicy { * responses, which should be the most common case. * @return a vector of newly constructed InternalRedirectPredicate instances. */ - virtual PatternTemplatePredicateSharedPtr predicate() const PURE; + virtual PathMatchPredicateSharedPtr predicate() const PURE; }; /** @@ -782,7 +782,7 @@ enum class PathMatchType { Exact, Regex, PathSeparatedPrefix, - Pattern, + Policy, }; /** @@ -934,7 +934,7 @@ class RouteEntry : public ResponseEntry { */ virtual const InternalRedirectPolicy& internalRedirectPolicy() const PURE; - virtual const PatternTemplatePolicy& patternTemplatePolicy() const PURE; + virtual const PathMatchPolicy& pathMatchPolicy() const PURE; /** * @return uint32_t any route cap on bytes which should be buffered for shadowing or retries. diff --git a/envoy/router/url_template.h b/envoy/router/url_template.h deleted file mode 100644 index 33ec50dbda83b..0000000000000 --- a/envoy/router/url_template.h +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once - -#include "envoy/config/typed_config.h" -#include "source/common/common/logger.h" - -#include "absl/strings/string_view.h" -#include "absl/status/statusor.h" - -namespace Envoy { -namespace Router { - -/** - * Used to decide if an internal redirect is allowed to be followed based on the target route. - * Subclassing Logger::Loggable so that implementations can log details. - */ -class PatternTemplatePredicate : Logger::Loggable { -public: - PatternTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) : - url_pattern_(url_pattern), - url_rewrite_pattern_(url_rewrite_pattern) {}; - - PatternTemplatePredicate() = default; - - virtual ~PatternTemplatePredicate() = default; - - virtual absl::string_view name() const PURE; - virtual std::string category() const PURE; - - virtual bool match(absl::string_view pattern) const PURE; - - virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, - absl::string_view matched_path) const PURE; - - const std::string url_pattern_; - const std::string url_rewrite_pattern_; -}; - -using PatternTemplatePredicateSharedPtr = std::shared_ptr; - -/** - * Factory for UrlTemplatePredicateFactory. - */ -class PatternTemplatePredicateFactory : public Envoy::Config::TypedFactory { -public: - virtual ~PatternTemplatePredicateFactory() = default; - - /** - * @param config contains the proto stored in TypedExtensionConfig.typed_config for the predicate. - * @param current_route_name stores the route name of the route where the predicate is installed. - * @return an InternalRedirectPredicate. The given current_route_name is useful for predicates - * that need to create per-route FilterState. - */ - virtual PatternTemplatePredicateSharedPtr - createUrlTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) PURE; - - std::string category() const override { return "envoy.url_template"; } -}; - -} // namespace Router -} // namespace Envoy \ No newline at end of file diff --git a/source/common/router/BUILD b/source/common/router/BUILD index 91a220845ad0e..e06e78ea104b4 100644 --- a/source/common/router/BUILD +++ b/source/common/router/BUILD @@ -70,8 +70,10 @@ envoy_cc_library( "//source/common/tracing:http_tracer_lib", "//source/common/upstream:retry_factory_lib", "//source/extensions/early_data:default_early_data_policy_lib", - "//source/extensions/url_template:config", - "//source/extensions/url_template:url_template_matching", + "//source/extensions/pattern_template/match:config", + "//source/extensions/pattern_template/match:pattern_template_match_lib", + "//source/extensions/pattern_template/rewrite:config", + "//source/extensions/pattern_template/rewrite:pattern_template_rewrite_lib", "@envoy_api//envoy/config/common/matcher/v3:pkg_cc_proto", "@envoy_api//envoy/config/core/v3:pkg_cc_proto", "@envoy_api//envoy/config/route/v3:pkg_cc_proto", diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 4a0594dacacf9..db1893d07d0cf 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -48,8 +48,10 @@ #include "source/common/tracing/http_tracer_impl.h" #include "source/common/upstream/retry_factory.h" #include "source/extensions/early_data/default_early_data_policy.h" -#include "source/extensions/url_template/url_template_matching.h" -#include "source/extensions/url_template/config.h" +#include "source/extensions/pattern_template/match/pattern_template_match.h" +#include "source/extensions/pattern_template/match/config.h" +#include "source/extensions/pattern_template/rewrite/pattern_template_rewrite.h" +#include "source/extensions/pattern_template/rewrite/config.h" #include "absl/strings/match.h" @@ -362,20 +364,22 @@ PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; PathMatchPolicyImpl::PathMatchPolicyImpl(const ProtobufWkt::Any& typed_config) : url_pattern_(url_pattern), url_rewrite_pattern_(url_rewrite_pattern), enabled_(true) { - // create matching predicate - if (!matching::PatternTemplatePredicate::is_valid_match_pattern(path_template).ok()) { - throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); - } absl::string_view name = "envoy.url_template.pattern_template_predicates"; - auto* factory = Registry::FactoryRegistry::getFactory(name); + auto* factory = Registry::FactoryRegistry::getFactory(name); ASSERT(factory); // factory not found ProtobufTypes::MessagePtr proto_config = factory->createEmptyRouteConfigProto(); Envoy::Config::Utility::translateOpaqueConfig(typed_config, validator, *proto_config); + + // validate with match input factory + if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { + throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); + } + predicate_factory_ = factory; } -PatternTemplatePredicateSharedPtr PatternTemplatePolicyImpl::predicate() const { - return predicate_factory_->createUrlTemplatePredicate(url_pattern_, url_rewrite_pattern_); +PathMatchPredicateSharedPtr PathMatchPolicyImpl::predicate() const { + return predicate_factory_->createPathMatchPredicate(url_pattern_, url_rewrite_pattern_); } absl::flat_hash_set InternalRedirectPolicyImpl::buildRedirectResponseCodes( @@ -666,7 +670,7 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, } if (route.route().has_regex_rewrite()) { - if (!prefix_rewrite_.empty() || pattern_template_policy_.enabled()) { + if (!prefix_rewrite_.empty() || path_match_policy_.enabled()) { throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_template_rewrite"); } auto rewrite_spec = route.route().regex_rewrite(); @@ -674,14 +678,14 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, regex_rewrite_substitution_ = rewrite_spec.substitution(); } - if (pattern_template_policy_.enabled()) { + if (path_match_policy_.enabled()) { if (!prefix_rewrite_.empty() || route.route().has_regex_rewrite()) { throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_template_rewrite"); } } if (!prefix_rewrite_.empty()) { - if (route.route().has_regex_rewrite() || pattern_template_policy_.enabled()) { + if (route.route().has_regex_rewrite() || path_match_policy_.enabled()) { throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_template_rewrite"); } } @@ -851,7 +855,7 @@ void RouteEntryImplBase::finalizeRequestHeaders(Http::RequestHeaderMap& headers, // Handle path rewrite absl::optional container; if (!getPathRewrite(headers, container).empty() || regex_rewrite_ != nullptr || - pattern_template_policy_.enabled()) { + path_match_policy_.enabled()) { rewritePathHeader(headers, insert_envoy_original_path); } } @@ -988,9 +992,9 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa } // complete pattern rewrite - if (pattern_template_policy_.enabled()) { + if (path_match_policy_.enabled()) { auto just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); - return *std::move(pattern_template_policy_.predicate()->rewritePattern(just_path, matched_path)); + return *std::move(path_match_policy_.predicate()->rewritePattern(just_path, matched_path)); } // There are no rewrites configured. @@ -1186,7 +1190,7 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( PathRewritePolicyImpl RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::RouteAction route) const { if (route.has_path_rewrite_policy()) { - if (!matching::PatternTemplatePredicate::is_valid_rewrite_pattern(path_template, + if (!matching::PatternTemplateMatchPredicate::is_valid_rewrite_pattern(path_template, path_template_rewrite) .ok()) { throw EnvoyException( @@ -1199,16 +1203,16 @@ RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::RouteAction } } -PatternTemplatePolicyImpl +PathMatchPolicyImpl RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::RouteMatch route) const { // match + rewrite if (route.has_path_match_policy()) { - if (!matching::PatternTemplatePredicate::is_valid_match_pattern(path_template).ok()) { + if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); } return PathMatchPolicyImpl(route.path_match_policy()); } - return PatternTemplatePolicyImpl(); + return PathMatchPolicyImpl(); } DecoratorConstPtr RouteEntryImplBase::parseDecorator(const envoy::config::route::v3::Route& route) { @@ -1457,19 +1461,19 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { - finalizePathHeader(headers, pattern_template_policy_.predicate()->url_pattern_, insert_envoy_original_path); + finalizePathHeader(headers, path_match_policy_.predicate()->url_pattern_, insert_envoy_original_path); } absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { - return currentUrlPathAfterRewriteWithMatchedPath(headers, pattern_template_policy_.predicate()->url_pattern_); + return currentUrlPathAfterRewriteWithMatchedPath(headers, path_match_policy_.predicate()->url_pattern_); } RouteConstSharedPtr PathTemplateRouteEntryImpl::matches(const Http::RequestHeaderMap& headers, const StreamInfo::StreamInfo& stream_info, uint64_t random_value) const { if (RouteEntryImplBase::matchRoute(headers, stream_info, random_value) && - pattern_template_policy_.predicate()->match(headers.getPathValue())) { + path_match_policy_.predicate()->match(headers.getPathValue())) { return clusterEntry(headers, random_value); } return nullptr; diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index e22f3ca694027..37a76569af9a3 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -34,7 +34,7 @@ #include "source/common/router/tls_context_match_criteria_impl.h" #include "source/common/stats/symbol_table.h" -#include "source/extensions/url_template/url_template_matching.h" +#include "source/extensions/pattern_template/match/pattern_template_match.h" #include "absl/container/node_hash_map.h" #include "absl/types/optional.h" @@ -459,25 +459,25 @@ class RouteTracingImpl : public RouteTracing { * Implementation of InternalRedirectPolicy that reads from the proto * InternalRedirectPolicy of the RouteAction. */ -class PatternTemplatePolicyImpl : public PatternTemplatePolicy { +class PathRewritePolicyImpl : public PathRewritePolicy { public: // Constructor that enables internal redirect with policy_config controlling the configurable // behaviors. - PatternTemplatePolicyImpl(std::string url_pattern, std::string url_rewrite_pattern); + PathRewritePolicyImpl(std::string url_pattern, std::string url_rewrite_pattern); // Default constructor that disables internal redirect. - PatternTemplatePolicyImpl(); + PathRewritePolicyImpl(); bool enabled() const override { return enabled_; } - PatternTemplatePredicateSharedPtr predicate() const override; + PatnRewritePredicateSharedPtr predicate() const override; const std::string url_pattern_; const std::string url_rewrite_pattern_; private: const bool enabled_; - PatternTemplatePredicateFactory* predicate_factory_; + PathRewritePredicateFactory* predicate_factory_; }; /** @@ -591,8 +591,8 @@ class RouteEntryImplBase : public RouteEntry, const InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } - const PatternTemplatePolicy& patternTemplatePolicy() const override { - return pattern_template_policy_; + const PathMatchPolicy& pathMatchPolicy() const override { + return pattern_template_match_policy_; } uint32_t retryShadowBufferLimit() const override { return retry_shadow_buffer_limit_; } const std::vector& shadowPolicies() const override { return shadow_policies_; } @@ -707,8 +707,8 @@ class RouteEntryImplBase : public RouteEntry, const InternalRedirectPolicy& internalRedirectPolicy() const override { return parent_->internalRedirectPolicy(); } - const PatternTemplatePolicy& patternTemplatePolicy() const override { - return parent_->patternTemplatePolicy(); + const PathMatchPolicy& pathMatchPolicy() const override { + return parent_->pathMatchPolicy(); } uint32_t retryShadowBufferLimit() const override { return parent_->retryShadowBufferLimit(); } const std::vector& shadowPolicies() const override { @@ -875,7 +875,7 @@ class RouteEntryImplBase : public RouteEntry, const std::string prefix_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_redirect_; - const PatternTemplatePolicyImpl pattern_template_policy_; + const PathMatchPolicyImpl path_match_policy_; std::string regex_rewrite_substitution_; std::string regex_rewrite_redirect_substitution_; const std::string host_rewrite_; @@ -957,8 +957,8 @@ class RouteEntryImplBase : public RouteEntry, ProtobufMessage::ValidationVisitor& validator, absl::string_view current_route_name) const; - PatternTemplatePolicyImpl - buildPatternTemplatePolicy(std::string path_template, std::string path_template_rewrite) const; + PathMatchPolicyImpl + buildPathMatchPolicy(std::string path_template, std::string path_template_rewrite) const; RouteConstSharedPtr pickClusterViaClusterHeader(const Http::LowerCaseString& cluster_header_name, const Http::HeaderMap& headers) const; @@ -1038,11 +1038,11 @@ class RouteEntryImplBase : public RouteEntry, }; /** - * Route entry implementation for pattern path match routing. + * Route entry implementation for path match policy based routing. */ -class PathTemplateRouteEntryImpl : public RouteEntryImplBase { +class PathMatchPolicyRouteEntryImpl : public RouteEntryImplBase { public: - PathTemplateRouteEntryImpl(const VirtualHostImpl& vhost, + PathMatchPolicyRouteEntryImpl(const VirtualHostImpl& vhost, const envoy::config::route::v3::Route& route, const OptionalHttpFilters& optional_http_filters, Server::Configuration::ServerFactoryContext& factory_context, @@ -1050,7 +1050,7 @@ class PathTemplateRouteEntryImpl : public RouteEntryImplBase { // Router::PathMatchCriterion const std::string& matcher() const override { return match_pattern_; } - PathMatchType matchType() const override { return PathMatchType::Pattern; } + PathMatchType matchType() const override { return PathMatchType::Policy; } // Router::Matchable RouteConstSharedPtr matches(const Http::RequestHeaderMap& headers, diff --git a/source/extensions/pattern_template/BUILD b/source/extensions/pattern_template/BUILD index fb5803523b7aa..3005a60d42018 100644 --- a/source/extensions/pattern_template/BUILD +++ b/source/extensions/pattern_template/BUILD @@ -13,12 +13,6 @@ licenses(["notice"]) # Apache 2 envoy_extension_package() -envoy_proto_library( - name = "pattern_template_rewrite_segements", - srcs = ["proto/pattern_template_rewrite_segments.proto"], - deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], -) - envoy_cc_library( name = "pattern_template_lib", srcs = ["pattern_template.cc"], @@ -30,9 +24,9 @@ envoy_cc_library( ], deps = [ ":pattern_template_internal_cc", - ":pattern_template_rewrite_segements_cc_proto", "//envoy/router:router_pattern_template_interface", "//source/common/http:path_utility_lib", + "//source/extensions/pattern_template/proto:pattern_template_rewrite_segements_cc_proto", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", @@ -47,7 +41,7 @@ envoy_cc_library( srcs = ["pattern_template_internal.cc"], hdrs = ["pattern_template_internal.h"], deps = [ - ":pattern_template_rewrite_segements_cc_proto", + "//source/extensions/pattern_template/proto:pattern_template_rewrite_segements_cc_proto", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/flags:flag", "@com_google_absl//absl/functional:function_ref", @@ -65,7 +59,7 @@ envoy_cc_test( srcs = ["pattern_template_test.cc"], deps = [ ":pattern_template", - ":pattern_template_rewrite_segements_cc_proto", + "//source/extensions/pattern_template/proto:pattern_template_rewrite_segements_cc_proto", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", @@ -78,7 +72,7 @@ envoy_cc_test( srcs = ["pattern_template_internal_test.cc"], deps = [ ":pattern_template_internal_cc", - ":pattern_template_rewrite_segements_cc_proto", + "//source/extensions/pattern_template/proto:pattern_template_rewrite_segements_cc_proto", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", diff --git a/source/extensions/pattern_template/match/BUILD b/source/extensions/pattern_template/match/BUILD index f8b83ba3ae7fe..f9d91b740062f 100644 --- a/source/extensions/pattern_template/match/BUILD +++ b/source/extensions/pattern_template/match/BUILD @@ -16,6 +16,9 @@ envoy_cc_library( name = "pattern_template_match_lib", srcs = ["pattern_template_match.cc"], hdrs = ["pattern_template_match.h"], + visibility = [ + "//source/common/router:__subpackages__", + ], deps = [ "//envoy/router:router_pattern_template_interface", "//source/extensions/pattern_template:pattern_template_lib", diff --git a/source/extensions/pattern_template/proto/BUILD b/source/extensions/pattern_template/proto/BUILD new file mode 100644 index 0000000000000..15daa9ec85d32 --- /dev/null +++ b/source/extensions/pattern_template/proto/BUILD @@ -0,0 +1,17 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_extension_package", + "envoy_proto_library", +) + +licenses(["notice"]) # Apache 2 + +# Wildcard & Pattern Matching Proto + +envoy_extension_package() + +envoy_proto_library( + name = "pattern_template_rewrite_segements", + srcs = ["pattern_template_rewrite_segments.proto"], + deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], +) diff --git a/source/extensions/pattern_template/rewrite/BUILD b/source/extensions/pattern_template/rewrite/BUILD index dec264ae33dc8..8545742c30232 100644 --- a/source/extensions/pattern_template/rewrite/BUILD +++ b/source/extensions/pattern_template/rewrite/BUILD @@ -16,6 +16,9 @@ envoy_cc_library( name = "pattern_template_rewrite_lib", srcs = ["pattern_template_rewrite.cc"], hdrs = ["pattern_template_rewrite.h"], + visibility = [ + "//source/common/router:__subpackages__", + ], deps = [ "//envoy/router:router_pattern_template_interface", "//source/extensions/pattern_template:pattern_template_lib", diff --git a/test/mocks/router/mocks.h b/test/mocks/router/mocks.h index ef54cd80d2321..57b00916df132 100644 --- a/test/mocks/router/mocks.h +++ b/test/mocks/router/mocks.h @@ -164,14 +164,14 @@ class MockInternalRedirectPredicate : public InternalRedirectPredicate { MOCK_METHOD(absl::string_view, name, (), (const)); }; -class MockPatternTemplatePolicy : public PatternTemplatePolicy { +class MockPatternTemplateMatchPolicy : public PatternTemplateMatchPolicy { public: - MockPatternTemplatePolicy(); + MockPatternTemplateMatchPolicy(); MOCK_METHOD(bool, enabled, (), (const)); - MOCK_METHOD(PatternTemplatePredicateSharedPtr, predicate, (), (const)); + MOCK_METHOD(PatternTemplatePredicateMatchSharedPtr, predicate, (), (const)); }; -class MockPatternTemplatePredicate : public PatternTemplatePredicate { +class MockPatternTemplateMatchPredicate : public PatternTemplateMatchPredicate { public: MOCK_METHOD(absl::string_view, name, (), (const)); }; From 93fe32dc3b569772402c19c7a50e3671621503f2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 17:30:15 +0000 Subject: [PATCH 047/238] Add code coverage Signed-off-by: silverstar195 --- test/common/router/config_impl_test.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index 6d8473fef8933..7db9c0258fc90 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -5081,6 +5081,26 @@ TEST_F(RouteMatcherTest, TestInvalidCharactersInPrefixRewriteRedirect) { "RedirectActionValidationError.PrefixRewrite:.*value does not match regex pattern"); } +TEST_F(RouteMatcherTest, TestPathMatchPolicy) { + const std::string yaml = R"EOF( +virtual_hosts: +- name: www2 + domains: ["bar.*"] + routes: + - match: + path_match_policy: {} + route: + prefix_rewrite: / + regex_rewrite: + pattern: + regex: foo + substitution: bar + cluster: www2 + )EOF"; + + EXPECT_THROW(TestConfigImpl(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException); +} + TEST_F(RouteMatcherTest, TestPrefixAndRegexRewrites) { const std::string yaml = R"EOF( virtual_hosts: From 53560e35e8e4e15450a4e1d5b82c50e2ea1dd068 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 18:25:37 +0000 Subject: [PATCH 048/238] Add code coverage Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 14 +++++--------- source/common/router/config_impl.h | 6 +----- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 8ef4ea1677be0..0716b30c8902b 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1381,30 +1381,26 @@ PathTemplateRouteEntryImpl::PathTemplateRouteEntryImpl( const OptionalHttpFilters& optional_http_filters, Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) - : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - path_template_(""), - path_matcher_(Matchers::PathMatcher::createPattern(path_template_, !case_sensitive_)) { + : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator) { // TODO(silverstar194) Implement path template matcher throw absl::UnimplementedError("Path template matcher not implemented"); } void PathTemplateRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { - finalizePathHeader(headers, path_template_, insert_envoy_original_path); + finalizePathHeader(headers, nullptr, insert_envoy_original_path); } absl::optional PathTemplateRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { - return currentUrlPathAfterRewriteWithMatchedPath(headers, path_template_); + return currentUrlPathAfterRewriteWithMatchedPath(headers, nullptr); } RouteConstSharedPtr PathTemplateRouteEntryImpl::matches(const Http::RequestHeaderMap& headers, const StreamInfo::StreamInfo& stream_info, uint64_t random_value) const { - if (RouteEntryImplBase::matchRoute(headers, stream_info, random_value) && - path_matcher_->match(headers.getPathValue())) { - return clusterEntry(headers, random_value); - } + throw absl::UnimplementedError("Path template matcher not implemented"); + RouteEntryImplBase::matchRoute(headers, stream_info, random_value); return nullptr; } diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index e879743221c19..009df242c4341 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -1012,7 +1012,7 @@ class PathTemplateRouteEntryImpl : public RouteEntryImplBase { ProtobufMessage::ValidationVisitor& validator); // Router::PathMatchCriterion - const std::string& matcher() const override { return path_template_; } + const std::string& matcher() const override { throw absl::UnimplementedError("Path template matcher not implemented"); } PathMatchType matchType() const override { return PathMatchType::Pattern; } // Router::Matchable @@ -1027,10 +1027,6 @@ class PathTemplateRouteEntryImpl : public RouteEntryImplBase { // Router::RouteEntry absl::optional currentUrlPathAfterRewrite(const Http::RequestHeaderMap& headers) const override; - -private: - const std::string path_template_; - const Matchers::PathMatcherConstSharedPtr path_matcher_; }; /** From 32a522290c5a70c6895efe4a48bc569ecd94813e Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 18:29:34 +0000 Subject: [PATCH 049/238] Revert test Signed-off-by: silverstar195 --- test/common/router/config_impl_test.cc | 30 ++++++++++++-------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index 7db9c0258fc90..efd89c4da8a84 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -5081,14 +5081,13 @@ TEST_F(RouteMatcherTest, TestInvalidCharactersInPrefixRewriteRedirect) { "RedirectActionValidationError.PrefixRewrite:.*value does not match regex pattern"); } -TEST_F(RouteMatcherTest, TestPathMatchPolicy) { +TEST_F(RouteMatcherTest, TestPrefixAndRegexRewrites) { const std::string yaml = R"EOF( virtual_hosts: - name: www2 domains: ["bar.*"] routes: - - match: - path_match_policy: {} + - match: { prefix: "/foo" } route: prefix_rewrite: / regex_rewrite: @@ -5098,28 +5097,27 @@ TEST_F(RouteMatcherTest, TestPathMatchPolicy) { cluster: www2 )EOF"; - EXPECT_THROW(TestConfigImpl(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException); + EXPECT_THROW_WITH_MESSAGE( + TestConfigImpl(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException, + "Cannot specify both prefix_rewrite and regex_rewrite"); } -TEST_F(RouteMatcherTest, TestPrefixAndRegexRewrites) { +TEST_F(RouteMatcherTest, TestPatternRewriteConfigLoad) { const std::string yaml = R"EOF( virtual_hosts: -- name: www2 - domains: ["bar.*"] +- name: path_template_rewrite + domains: ["*"] routes: - - match: { prefix: "/foo" } + - match: + path_template: "/bar/{country}/{lang}" route: - prefix_rewrite: / - regex_rewrite: - pattern: - regex: foo - substitution: bar + path_template_rewrite: "/bar/{lang}/{country}" cluster: www2 )EOF"; - EXPECT_THROW_WITH_MESSAGE( - TestConfigImpl(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException, - "Cannot specify both prefix_rewrite and regex_rewrite"); + NiceMock stream_info; + factory_context_.cluster_manager_.initializeClusters({"www2"}, {}); + TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); } TEST_F(RouteMatcherTest, TestDomainMatchOrderConfig) { From 40e84b4c763856453deedd297e803f96638efc4e Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 18:41:14 +0000 Subject: [PATCH 050/238] Reformat Signed-off-by: silverstar195 --- source/common/router/config_impl.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 009df242c4341..f2fc47c365b56 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -1012,7 +1012,10 @@ class PathTemplateRouteEntryImpl : public RouteEntryImplBase { ProtobufMessage::ValidationVisitor& validator); // Router::PathMatchCriterion - const std::string& matcher() const override { throw absl::UnimplementedError("Path template matcher not implemented"); } + const std::string& matcher() const override { + throw absl::UnimplementedError("Path template matcher not implemented"); + } + PathMatchType matchType() const override { return PathMatchType::Pattern; } // Router::Matchable From 94aa0e76ba4b37d0f38d5e1d33a61a1df6220f75 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 18:43:33 +0000 Subject: [PATCH 051/238] Reformat Signed-off-by: silverstar195 --- source/common/router/config_impl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index f2fc47c365b56..0467acd4c3525 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -1015,7 +1015,7 @@ class PathTemplateRouteEntryImpl : public RouteEntryImplBase { const std::string& matcher() const override { throw absl::UnimplementedError("Path template matcher not implemented"); } - + PathMatchType matchType() const override { return PathMatchType::Pattern; } // Router::Matchable From c943a59b12b6cb9291297c752521ecafd61fb2b4 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 19:24:28 +0000 Subject: [PATCH 052/238] Progress towards two extensions Signed-off-by: silverstar195 --- envoy/router/BUILD | 17 +- envoy/router/path_match_policy.h | 36 +-- envoy/router/path_rewrite_policy.h | 44 +++ envoy/router/router.h | 4 +- source/common/http/async_client_impl.cc | 2 +- source/common/http/async_client_impl.h | 6 +- source/common/router/BUILD | 2 + source/common/router/config_impl.cc | 12 +- source/common/router/config_impl.h | 2 + source/extensions/all_extensions.bzl | 2 - source/extensions/extensions_build_config.bzl | 274 ++++++++---------- source/extensions/extensions_metadata.yaml | 8 +- source/extensions/pattern_template/BUILD | 2 +- .../extensions/pattern_template/match/BUILD | 4 +- .../pattern_template/match/config.cc | 4 +- .../pattern_template/match/config.h | 8 +- .../match/pattern_template_match.h | 4 +- .../pattern_template/pattern_template.h | 2 +- .../extensions/pattern_template/rewrite/BUILD | 4 +- .../pattern_template/rewrite/config.cc | 4 +- .../pattern_template/rewrite/config.h | 8 +- .../rewrite/pattern_template_rewrite.h | 4 +- 22 files changed, 229 insertions(+), 224 deletions(-) create mode 100644 envoy/router/path_rewrite_policy.h diff --git a/envoy/router/BUILD b/envoy/router/BUILD index ffa3c7ae20a8c..9ea9cbb91ee83 100644 --- a/envoy/router/BUILD +++ b/envoy/router/BUILD @@ -64,7 +64,7 @@ envoy_cc_library( external_deps = ["abseil_optional"], deps = [ ":internal_redirect_interface", - ":router_pattern_template_interface", + ":router_path_match_policy_interface", "//envoy/access_log:access_log_interface", "//envoy/common:conn_pool_interface", "//envoy/common:matchers_interface", @@ -147,8 +147,19 @@ envoy_cc_library( ) envoy_cc_library( - name = "router_pattern_template_interface", - hdrs = ["pattern_template.h"], + name = "router_path_match_policy_interface", + hdrs = ["path_match_policy.h"], + deps = [ + "//envoy/config:typed_config_interface", + "//source/common/common:minimal_logger_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + ], +) + +envoy_cc_library( + name = "router_path_rewrite_policy_interface", + hdrs = ["path_rewrite_policy.h"], deps = [ "//envoy/config:typed_config_interface", "//source/common/common:minimal_logger_lib", diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index 43cc41c356690..465c65a0c5138 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -27,46 +27,16 @@ class PathMatchPredicate : Logger::Loggable { using PathMatchPredicateSharedPtr = std::shared_ptr; /** - * Factory for PatternTemplateMatchPredicate. + * Factory for PathMatchPredicateFactory. */ class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { public: virtual ~PathMatchPredicateFactory() = default; - virtual PathMatchPredicateFactory + virtual PathMatchPredicateSharedPtr createPathMatchPredicate(std::string url_pattern) PURE; - std::string category() const override { return "envoy.pattern_template"; } -}; - -/** - * Used to decide if pattern template rewrite is needed based on the target route. - * Subclassing Logger::Loggable so that implementations can log details. - */ -class PathRewritePredicate : Logger::Loggable { -public: - PathRewritePredicate() = default; - virtual ~PathRewritePredicate() = default; - - virtual absl::string_view name() const PURE; - - virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, - absl::string_view matched_path) const PURE; -}; - -using PathRewritePredicateSharedPtr = std::shared_ptr; - -/** - * Factory for PatternRewriteTemplatePredicate. - */ -class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { -public: - virtual ~PathRewritePredicateFactory() override = default; - - virtual PathRewritePredicateSharedPtr - createPathRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) PURE; - - std::string category() const override { return "envoy.pattern_template"; } + std::string category() const override { return "envoy.path_match_policy"; } }; } // namespace Router diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h new file mode 100644 index 0000000000000..e5c25b4a659a2 --- /dev/null +++ b/envoy/router/path_rewrite_policy.h @@ -0,0 +1,44 @@ +#pragma once + +#include "envoy/config/typed_config.h" + +#include "source/common/common/logger.h" + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" + +namespace Envoy { +namespace Router { + +/** + * Used to decide if pattern template rewrite is needed based on the target route. + * Subclassing Logger::Loggable so that implementations can log details. + */ +class PathRewritePredicate : Logger::Loggable { +public: + PathRewritePredicate() = default; + virtual ~PathRewritePredicate() = default; + + virtual absl::string_view name() const PURE; + + virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const PURE; +}; + +using PathRewritePredicateSharedPtr = std::shared_ptr; + +/** + * Factory for PatternRewriteTemplatePredicate. + */ +class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { +public: + virtual ~PathRewritePredicateFactory() override = default; + + virtual PathRewritePredicateSharedPtr + createPathRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) PURE; + + std::string category() const override { return "envoy.path_rewrite_policy"; } +}; + +} // namespace Router +} // namespace Envoy diff --git a/envoy/router/router.h b/envoy/router/router.h index ee0b464b8f65d..baaa20f38aef9 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -298,9 +298,9 @@ class RetryPolicy { */ enum class RetryStatus { No, NoOverflow, NoRetryLimitExceeded, Yes }; -class PathMatchPolicyImpl { +class PathMatchPolicy { public: - virtual ~PathMatchPolicyImpl() = default; + virtual ~PathMatchPolicy() = default; /** * @return whether internal redirect is enabled on this route. diff --git a/source/common/http/async_client_impl.cc b/source/common/http/async_client_impl.cc index b867f4c273ad8..d179d98dfaf42 100644 --- a/source/common/http/async_client_impl.cc +++ b/source/common/http/async_client_impl.cc @@ -20,7 +20,7 @@ const std::vector> const AsyncStreamImpl::NullHedgePolicy AsyncStreamImpl::RouteEntryImpl::hedge_policy_; const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::RouteEntryImpl::rate_limit_policy_; const Router::InternalRedirectPolicyImpl AsyncStreamImpl::RouteEntryImpl::internal_redirect_policy_; -const Router::PatternTemplatePolicyImpl AsyncStreamImpl::RouteEntryImpl::pattern_template_policy_; +const Router::PathMatchPolicyImpl AsyncStreamImpl::RouteEntryImpl::path_match_policy_; const std::vector AsyncStreamImpl::RouteEntryImpl::shadow_policies_; const AsyncStreamImpl::NullVirtualHost AsyncStreamImpl::RouteEntryImpl::virtual_host_; const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::NullVirtualHost::rate_limit_policy_; diff --git a/source/common/http/async_client_impl.h b/source/common/http/async_client_impl.h index 9d3b7814d0939..66034a22d3616 100644 --- a/source/common/http/async_client_impl.h +++ b/source/common/http/async_client_impl.h @@ -231,8 +231,8 @@ class AsyncStreamImpl : public AsyncClient::Stream, const Router::InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } - const Router::PatternTemplatePolicy& patternTemplatePolicy() const override { - return pattern_template_policy_; + const Router::PathMatchPolicy& pathMatchPolicy() const override { + return path_match_policy_; } uint32_t retryShadowBufferLimit() const override { return std::numeric_limits::max(); @@ -297,7 +297,7 @@ class AsyncStreamImpl : public AsyncClient::Stream, static const NullHedgePolicy hedge_policy_; static const NullRateLimitPolicy rate_limit_policy_; static const Router::InternalRedirectPolicyImpl internal_redirect_policy_; - static const Router::PatternTemplatePolicyImpl pattern_template_policy_; + static const Router::PathMatchPolicyImpl path_match_policy_; static const std::vector shadow_policies_; static const NullVirtualHost virtual_host_; static const std::multimap opaque_config_; diff --git a/source/common/router/BUILD b/source/common/router/BUILD index e06e78ea104b4..2d34c2b9b0efe 100644 --- a/source/common/router/BUILD +++ b/source/common/router/BUILD @@ -46,6 +46,8 @@ envoy_cc_library( "//envoy/http:header_map_interface", "//envoy/router:cluster_specifier_plugin_interface", "//envoy/router:router_interface", + "//envoy/router:router_path_match_policy_interface", + "//envoy/router:router_path_rewrite_policy_interface", "//envoy/runtime:runtime_interface", "//envoy/server:filter_config_interface", # TODO(rodaine): break dependency on server "//envoy/upstream:cluster_manager_interface", diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index db1893d07d0cf..24cf8e3d3e589 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -372,7 +372,7 @@ PathMatchPolicyImpl::PathMatchPolicyImpl(const ProtobufWkt::Any& typed_config) // validate with match input factory if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { - throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); + throw EnvoyException(fmt::format("path_match_policy {} is invalid", path_template)); } predicate_factory_ = factory; @@ -671,7 +671,7 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, if (route.route().has_regex_rewrite()) { if (!prefix_rewrite_.empty() || path_match_policy_.enabled()) { - throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_template_rewrite"); + throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); } auto rewrite_spec = route.route().regex_rewrite(); regex_rewrite_ = Regex::Utility::parseRegex(rewrite_spec.pattern()); @@ -680,13 +680,13 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, if (path_match_policy_.enabled()) { if (!prefix_rewrite_.empty() || route.route().has_regex_rewrite()) { - throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_template_rewrite"); + throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); } } if (!prefix_rewrite_.empty()) { if (route.route().has_regex_rewrite() || path_match_policy_.enabled()) { - throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_template_rewrite"); + throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); } } @@ -1194,7 +1194,7 @@ RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::RouteAction path_template_rewrite) .ok()) { throw EnvoyException( - fmt::format("mismatch between path_template {} and path_template_rewrite {}", + fmt::format("mismatch between path_match_policy {} and path_rewrite_policy {}", path_template, path_template_rewrite)); } return PathRewritePolicyImpl(route.path_match_policy()); @@ -1208,7 +1208,7 @@ RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::RouteMatch ro // match + rewrite if (route.has_path_match_policy()) { if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { - throw EnvoyException(fmt::format("path_template {} is invalid", path_template)); + throw EnvoyException(fmt::format("path_match_policy {} is invalid", path_template)); } return PathMatchPolicyImpl(route.path_match_policy()); } diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 37a76569af9a3..87078cbf5b910 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -27,6 +27,8 @@ #include "source/common/http/header_utility.h" #include "source/common/matcher/matcher.h" #include "source/common/router/config_utility.h" +#include "source/common/router/path_match_policy.h" +#include "source/common/router/path_rewrite_policy.h" #include "source/common/router/header_formatter.h" #include "source/common/router/header_parser.h" #include "source/common/router/metadatamatchcriteria_impl.h" diff --git a/source/extensions/all_extensions.bzl b/source/extensions/all_extensions.bzl index 6785b726f4ef1..9102e9f9ee628 100644 --- a/source/extensions/all_extensions.bzl +++ b/source/extensions/all_extensions.bzl @@ -9,7 +9,6 @@ _required_extensions = { "envoy.transport_sockets.tls": "//source/extensions/transport_sockets/tls:config", "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", - "envoy.url_template.pattern_template_predicates": "//source/extensions/url_template:config", } # Return the extension cc_library target after select @@ -35,7 +34,6 @@ _core_extensions = [ "envoy.transport_sockets.raw_buffer", "envoy.network.dns_resolver.cares", "envoy.network.dns_resolver.apple", - "envoy.url_template.pattern_template_predicates", ] # Return all core extensions to be compiled into Envoy. diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index e143d94b7e981..2e5ab87f8aacc 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -3,23 +3,21 @@ EXTENSIONS = { # # Access loggers # - - "envoy.access_loggers.file": "//source/extensions/access_loggers/file:config", - "envoy.access_loggers.extension_filters.cel": "//source/extensions/access_loggers/filters/cel:config", - "envoy.access_loggers.http_grpc": "//source/extensions/access_loggers/grpc:http_config", - "envoy.access_loggers.tcp_grpc": "//source/extensions/access_loggers/grpc:tcp_config", - "envoy.access_loggers.open_telemetry": "//source/extensions/access_loggers/open_telemetry:config", - "envoy.access_loggers.stdout": "//source/extensions/access_loggers/stream:config", - "envoy.access_loggers.stderr": "//source/extensions/access_loggers/stream:config", - "envoy.access_loggers.wasm": "//source/extensions/access_loggers/wasm:config", + "envoy.access_loggers.file": "//source/extensions/access_loggers/file:config", + "envoy.access_loggers.extension_filters.cel": "//source/extensions/access_loggers/filters/cel:config", + "envoy.access_loggers.http_grpc": "//source/extensions/access_loggers/grpc:http_config", + "envoy.access_loggers.tcp_grpc": "//source/extensions/access_loggers/grpc:tcp_config", + "envoy.access_loggers.open_telemetry": "//source/extensions/access_loggers/open_telemetry:config", + "envoy.access_loggers.stdout": "//source/extensions/access_loggers/stream:config", + "envoy.access_loggers.stderr": "//source/extensions/access_loggers/stream:config", + "envoy.access_loggers.wasm": "//source/extensions/access_loggers/wasm:config", # # Clusters # - - "envoy.clusters.aggregate": "//source/extensions/clusters/aggregate:cluster", - "envoy.clusters.dynamic_forward_proxy": "//source/extensions/clusters/dynamic_forward_proxy:cluster", - "envoy.clusters.redis": "//source/extensions/clusters/redis:redis_cluster", + "envoy.clusters.aggregate": "//source/extensions/clusters/aggregate:cluster", + "envoy.clusters.dynamic_forward_proxy": "//source/extensions/clusters/dynamic_forward_proxy:cluster", + "envoy.clusters.redis": "//source/extensions/clusters/redis:redis_cluster", # # Compression @@ -34,88 +32,81 @@ EXTENSIONS = { # # Config validators # - - "envoy.config.validators.minimum_clusters": "//source/extensions/config/validators/minimum_clusters:config", + "envoy.config.validators.minimum_clusters": "//source/extensions/config/validators/minimum_clusters:config", # # gRPC Credentials Plugins # - - "envoy.grpc_credentials.file_based_metadata": "//source/extensions/grpc_credentials/file_based_metadata:config", - "envoy.grpc_credentials.aws_iam": "//source/extensions/grpc_credentials/aws_iam:config", + "envoy.grpc_credentials.file_based_metadata": "//source/extensions/grpc_credentials/file_based_metadata:config", + "envoy.grpc_credentials.aws_iam": "//source/extensions/grpc_credentials/aws_iam:config", # # WASM # - - "envoy.bootstrap.wasm": "//source/extensions/bootstrap/wasm:config", + "envoy.bootstrap.wasm": "//source/extensions/bootstrap/wasm:config", # # Health checkers # - - "envoy.health_checkers.redis": "//source/extensions/health_checkers/redis:config", + "envoy.health_checkers.redis": "//source/extensions/health_checkers/redis:config", # # Input Matchers # - - "envoy.matching.matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", - "envoy.matching.matchers.ip": "//source/extensions/matching/input_matchers/ip:config", + "envoy.matching.matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", + "envoy.matching.matchers.ip": "//source/extensions/matching/input_matchers/ip:config", # # Generic Inputs # - - "envoy.matching.common_inputs.environment_variable": "//source/extensions/matching/common_inputs/environment_variable:config", + "envoy.matching.common_inputs.environment_variable": "//source/extensions/matching/common_inputs/environment_variable:config", # # HTTP filters # - - "envoy.filters.http.adaptive_concurrency": "//source/extensions/filters/http/adaptive_concurrency:config", - "envoy.filters.http.admission_control": "//source/extensions/filters/http/admission_control:config", - "envoy.filters.http.alternate_protocols_cache": "//source/extensions/filters/http/alternate_protocols_cache:config", - "envoy.filters.http.aws_lambda": "//source/extensions/filters/http/aws_lambda:config", - "envoy.filters.http.aws_request_signing": "//source/extensions/filters/http/aws_request_signing:config", - "envoy.filters.http.bandwidth_limit": "//source/extensions/filters/http/bandwidth_limit:config", - "envoy.filters.http.buffer": "//source/extensions/filters/http/buffer:config", - "envoy.filters.http.cache": "//source/extensions/filters/http/cache:config", - "envoy.filters.http.cdn_loop": "//source/extensions/filters/http/cdn_loop:config", - "envoy.filters.http.compressor": "//source/extensions/filters/http/compressor:config", - "envoy.filters.http.cors": "//source/extensions/filters/http/cors:config", - "envoy.filters.http.composite": "//source/extensions/filters/http/composite:config", - "envoy.filters.http.csrf": "//source/extensions/filters/http/csrf:config", - "envoy.filters.http.decompressor": "//source/extensions/filters/http/decompressor:config", - "envoy.filters.http.dynamic_forward_proxy": "//source/extensions/filters/http/dynamic_forward_proxy:config", - "envoy.filters.http.ext_authz": "//source/extensions/filters/http/ext_authz:config", - "envoy.filters.http.ext_proc": "//source/extensions/filters/http/ext_proc:config", - "envoy.filters.http.fault": "//source/extensions/filters/http/fault:config", - "envoy.filters.http.file_system_buffer": "//source/extensions/filters/http/file_system_buffer:config", - "envoy.filters.http.gcp_authn": "//source/extensions/filters/http/gcp_authn:config", - "envoy.filters.http.grpc_http1_bridge": "//source/extensions/filters/http/grpc_http1_bridge:config", - "envoy.filters.http.grpc_http1_reverse_bridge": "//source/extensions/filters/http/grpc_http1_reverse_bridge:config", - "envoy.filters.http.grpc_json_transcoder": "//source/extensions/filters/http/grpc_json_transcoder:config", - "envoy.filters.http.grpc_stats": "//source/extensions/filters/http/grpc_stats:config", - "envoy.filters.http.grpc_web": "//source/extensions/filters/http/grpc_web:config", - "envoy.filters.http.header_to_metadata": "//source/extensions/filters/http/header_to_metadata:config", - "envoy.filters.http.health_check": "//source/extensions/filters/http/health_check:config", - "envoy.filters.http.ip_tagging": "//source/extensions/filters/http/ip_tagging:config", - "envoy.filters.http.jwt_authn": "//source/extensions/filters/http/jwt_authn:config", + "envoy.filters.http.adaptive_concurrency": "//source/extensions/filters/http/adaptive_concurrency:config", + "envoy.filters.http.admission_control": "//source/extensions/filters/http/admission_control:config", + "envoy.filters.http.alternate_protocols_cache": "//source/extensions/filters/http/alternate_protocols_cache:config", + "envoy.filters.http.aws_lambda": "//source/extensions/filters/http/aws_lambda:config", + "envoy.filters.http.aws_request_signing": "//source/extensions/filters/http/aws_request_signing:config", + "envoy.filters.http.bandwidth_limit": "//source/extensions/filters/http/bandwidth_limit:config", + "envoy.filters.http.buffer": "//source/extensions/filters/http/buffer:config", + "envoy.filters.http.cache": "//source/extensions/filters/http/cache:config", + "envoy.filters.http.cdn_loop": "//source/extensions/filters/http/cdn_loop:config", + "envoy.filters.http.compressor": "//source/extensions/filters/http/compressor:config", + "envoy.filters.http.cors": "//source/extensions/filters/http/cors:config", + "envoy.filters.http.composite": "//source/extensions/filters/http/composite:config", + "envoy.filters.http.csrf": "//source/extensions/filters/http/csrf:config", + "envoy.filters.http.decompressor": "//source/extensions/filters/http/decompressor:config", + "envoy.filters.http.dynamic_forward_proxy": "//source/extensions/filters/http/dynamic_forward_proxy:config", + "envoy.filters.http.ext_authz": "//source/extensions/filters/http/ext_authz:config", + "envoy.filters.http.ext_proc": "//source/extensions/filters/http/ext_proc:config", + "envoy.filters.http.fault": "//source/extensions/filters/http/fault:config", + "envoy.filters.http.file_system_buffer": "//source/extensions/filters/http/file_system_buffer:config", + "envoy.filters.http.gcp_authn": "//source/extensions/filters/http/gcp_authn:config", + "envoy.filters.http.grpc_http1_bridge": "//source/extensions/filters/http/grpc_http1_bridge:config", + "envoy.filters.http.grpc_http1_reverse_bridge": "//source/extensions/filters/http/grpc_http1_reverse_bridge:config", + "envoy.filters.http.grpc_json_transcoder": "//source/extensions/filters/http/grpc_json_transcoder:config", + "envoy.filters.http.grpc_stats": "//source/extensions/filters/http/grpc_stats:config", + "envoy.filters.http.grpc_web": "//source/extensions/filters/http/grpc_web:config", + "envoy.filters.http.header_to_metadata": "//source/extensions/filters/http/header_to_metadata:config", + "envoy.filters.http.health_check": "//source/extensions/filters/http/health_check:config", + "envoy.filters.http.ip_tagging": "//source/extensions/filters/http/ip_tagging:config", + "envoy.filters.http.jwt_authn": "//source/extensions/filters/http/jwt_authn:config", # Disabled by default - "envoy.filters.http.kill_request": "//source/extensions/filters/http/kill_request:kill_request_config", - "envoy.filters.http.local_ratelimit": "//source/extensions/filters/http/local_ratelimit:config", - "envoy.filters.http.lua": "//source/extensions/filters/http/lua:config", - "envoy.filters.http.oauth2": "//source/extensions/filters/http/oauth2:config", - "envoy.filters.http.on_demand": "//source/extensions/filters/http/on_demand:config", - "envoy.filters.http.original_src": "//source/extensions/filters/http/original_src:config", - "envoy.filters.http.ratelimit": "//source/extensions/filters/http/ratelimit:config", - "envoy.filters.http.rbac": "//source/extensions/filters/http/rbac:config", - "envoy.filters.http.router": "//source/extensions/filters/http/router:config", - "envoy.filters.http.set_metadata": "//source/extensions/filters/http/set_metadata:config", - "envoy.filters.http.tap": "//source/extensions/filters/http/tap:config", - "envoy.filters.http.wasm": "//source/extensions/filters/http/wasm:config", - "envoy.filters.http.stateful_session": "//source/extensions/filters/http/stateful_session:config", + "envoy.filters.http.kill_request": "//source/extensions/filters/http/kill_request:kill_request_config", + "envoy.filters.http.local_ratelimit": "//source/extensions/filters/http/local_ratelimit:config", + "envoy.filters.http.lua": "//source/extensions/filters/http/lua:config", + "envoy.filters.http.oauth2": "//source/extensions/filters/http/oauth2:config", + "envoy.filters.http.on_demand": "//source/extensions/filters/http/on_demand:config", + "envoy.filters.http.original_src": "//source/extensions/filters/http/original_src:config", + "envoy.filters.http.ratelimit": "//source/extensions/filters/http/ratelimit:config", + "envoy.filters.http.rbac": "//source/extensions/filters/http/rbac:config", + "envoy.filters.http.router": "//source/extensions/filters/http/router:config", + "envoy.filters.http.set_metadata": "//source/extensions/filters/http/set_metadata:config", + "envoy.filters.http.tap": "//source/extensions/filters/http/tap:config", + "envoy.filters.http.wasm": "//source/extensions/filters/http/wasm:config", + "envoy.filters.http.stateful_session": "//source/extensions/filters/http/stateful_session:config", # # Listener filters @@ -133,132 +124,119 @@ EXTENSIONS = { # # Network filters # - - "envoy.filters.network.connection_limit": "//source/extensions/filters/network/connection_limit:config", - "envoy.filters.network.direct_response": "//source/extensions/filters/network/direct_response:config", - "envoy.filters.network.dubbo_proxy": "//source/extensions/filters/network/dubbo_proxy:config", - "envoy.filters.network.echo": "//source/extensions/filters/network/echo:config", - "envoy.filters.network.ext_authz": "//source/extensions/filters/network/ext_authz:config", - "envoy.filters.network.http_connection_manager": "//source/extensions/filters/network/http_connection_manager:config", - "envoy.filters.network.local_ratelimit": "//source/extensions/filters/network/local_ratelimit:config", - "envoy.filters.network.mongo_proxy": "//source/extensions/filters/network/mongo_proxy:config", - "envoy.filters.network.ratelimit": "//source/extensions/filters/network/ratelimit:config", - "envoy.filters.network.rbac": "//source/extensions/filters/network/rbac:config", - "envoy.filters.network.redis_proxy": "//source/extensions/filters/network/redis_proxy:config", - "envoy.filters.network.tcp_proxy": "//source/extensions/filters/network/tcp_proxy:config", - "envoy.filters.network.thrift_proxy": "//source/extensions/filters/network/thrift_proxy:config", - "envoy.filters.network.sni_cluster": "//source/extensions/filters/network/sni_cluster:config", - "envoy.filters.network.sni_dynamic_forward_proxy": "//source/extensions/filters/network/sni_dynamic_forward_proxy:config", - "envoy.filters.network.wasm": "//source/extensions/filters/network/wasm:config", - "envoy.filters.network.zookeeper_proxy": "//source/extensions/filters/network/zookeeper_proxy:config", + "envoy.filters.network.connection_limit": "//source/extensions/filters/network/connection_limit:config", + "envoy.filters.network.direct_response": "//source/extensions/filters/network/direct_response:config", + "envoy.filters.network.dubbo_proxy": "//source/extensions/filters/network/dubbo_proxy:config", + "envoy.filters.network.echo": "//source/extensions/filters/network/echo:config", + "envoy.filters.network.ext_authz": "//source/extensions/filters/network/ext_authz:config", + "envoy.filters.network.http_connection_manager": "//source/extensions/filters/network/http_connection_manager:config", + "envoy.filters.network.local_ratelimit": "//source/extensions/filters/network/local_ratelimit:config", + "envoy.filters.network.mongo_proxy": "//source/extensions/filters/network/mongo_proxy:config", + "envoy.filters.network.ratelimit": "//source/extensions/filters/network/ratelimit:config", + "envoy.filters.network.rbac": "//source/extensions/filters/network/rbac:config", + "envoy.filters.network.redis_proxy": "//source/extensions/filters/network/redis_proxy:config", + "envoy.filters.network.tcp_proxy": "//source/extensions/filters/network/tcp_proxy:config", + "envoy.filters.network.thrift_proxy": "//source/extensions/filters/network/thrift_proxy:config", + "envoy.filters.network.sni_cluster": "//source/extensions/filters/network/sni_cluster:config", + "envoy.filters.network.sni_dynamic_forward_proxy": "//source/extensions/filters/network/sni_dynamic_forward_proxy:config", + "envoy.filters.network.wasm": "//source/extensions/filters/network/wasm:config", + "envoy.filters.network.zookeeper_proxy": "//source/extensions/filters/network/zookeeper_proxy:config", # # UDP filters # - - "envoy.filters.udp.dns_filter": "//source/extensions/filters/udp/dns_filter:config", - "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", + "envoy.filters.udp.dns_filter": "//source/extensions/filters/udp/dns_filter:config", + "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", # # Resource monitors # - - "envoy.resource_monitors.fixed_heap": "//source/extensions/resource_monitors/fixed_heap:config", - "envoy.resource_monitors.injected_resource": "//source/extensions/resource_monitors/injected_resource:config", + "envoy.resource_monitors.fixed_heap": "//source/extensions/resource_monitors/fixed_heap:config", + "envoy.resource_monitors.injected_resource": "//source/extensions/resource_monitors/injected_resource:config", # # Stat sinks # - - "envoy.stat_sinks.dog_statsd": "//source/extensions/stat_sinks/dog_statsd:config", - "envoy.stat_sinks.graphite_statsd": "//source/extensions/stat_sinks/graphite_statsd:config", - "envoy.stat_sinks.hystrix": "//source/extensions/stat_sinks/hystrix:config", - "envoy.stat_sinks.metrics_service": "//source/extensions/stat_sinks/metrics_service:config", - "envoy.stat_sinks.statsd": "//source/extensions/stat_sinks/statsd:config", - "envoy.stat_sinks.wasm": "//source/extensions/stat_sinks/wasm:config", + "envoy.stat_sinks.dog_statsd": "//source/extensions/stat_sinks/dog_statsd:config", + "envoy.stat_sinks.graphite_statsd": "//source/extensions/stat_sinks/graphite_statsd:config", + "envoy.stat_sinks.hystrix": "//source/extensions/stat_sinks/hystrix:config", + "envoy.stat_sinks.metrics_service": "//source/extensions/stat_sinks/metrics_service:config", + "envoy.stat_sinks.statsd": "//source/extensions/stat_sinks/statsd:config", + "envoy.stat_sinks.wasm": "//source/extensions/stat_sinks/wasm:config", # # Thrift filters # - - "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", - "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", - "envoy.filters.thrift.rate_limit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", + "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", + "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", + "envoy.filters.thrift.rate_limit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", # # Tracers # - - "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", - "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", - "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", - "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", - "envoy.tracers.xray": "//source/extensions/tracers/xray:config", - "envoy.tracers.skywalking": "//source/extensions/tracers/skywalking:config", - "envoy.tracers.opentelemetry": "//source/extensions/tracers/opentelemetry:config", + "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", + "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", + "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", + "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", + "envoy.tracers.xray": "//source/extensions/tracers/xray:config", + "envoy.tracers.skywalking": "//source/extensions/tracers/skywalking:config", + "envoy.tracers.opentelemetry": "//source/extensions/tracers/opentelemetry:config", # # Transport sockets # - - "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", - "envoy.transport_sockets.http_11_proxy": "//source/extensions/transport_sockets/http_11_proxy:upstream_config", - "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", - "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", - "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", - "envoy.transport_sockets.starttls": "//source/extensions/transport_sockets/starttls:config", - "envoy.transport_sockets.tcp_stats": "//source/extensions/transport_sockets/tcp_stats:config", - "envoy.transport_sockets.internal_upstream": "//source/extensions/transport_sockets/internal_upstream:config", + "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", + "envoy.transport_sockets.http_11_proxy": "//source/extensions/transport_sockets/http_11_proxy:upstream_config", + "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", + "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", + "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", + "envoy.transport_sockets.starttls": "//source/extensions/transport_sockets/starttls:config", + "envoy.transport_sockets.tcp_stats": "//source/extensions/transport_sockets/tcp_stats:config", + "envoy.transport_sockets.internal_upstream": "//source/extensions/transport_sockets/internal_upstream:config", # # Retry host predicates # - - "envoy.retry_host_predicates.previous_hosts": "//source/extensions/retry/host/previous_hosts:config", - "envoy.retry_host_predicates.omit_canary_hosts": "//source/extensions/retry/host/omit_canary_hosts:config", - "envoy.retry_host_predicates.omit_host_metadata": "//source/extensions/retry/host/omit_host_metadata:config", + "envoy.retry_host_predicates.previous_hosts": "//source/extensions/retry/host/previous_hosts:config", + "envoy.retry_host_predicates.omit_canary_hosts": "//source/extensions/retry/host/omit_canary_hosts:config", + "envoy.retry_host_predicates.omit_host_metadata": "//source/extensions/retry/host/omit_host_metadata:config", # # Retry priorities # - - "envoy.retry_priorities.previous_priorities": "//source/extensions/retry/priority/previous_priorities:config", + "envoy.retry_priorities.previous_priorities": "//source/extensions/retry/priority/previous_priorities:config", # # CacheFilter plugins # - "envoy.extensions.http.cache.simple": "//source/extensions/filters/http/cache/simple_http_cache:config", + "envoy.extensions.http.cache.simple": "//source/extensions/filters/http/cache/simple_http_cache:config", # # Internal redirect predicates # - "envoy.internal_redirect_predicates.allow_listed_routes": "//source/extensions/internal_redirect/allow_listed_routes:config", - "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", - "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", + "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", + "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", # # Http Upstreams (excepting envoy.upstreams.http.generic which is hard-coded into the build so not registered here) # - - "envoy.upstreams.http.http": "//source/extensions/upstreams/http/http:config", - "envoy.upstreams.http.tcp": "//source/extensions/upstreams/http/tcp:config", + "envoy.upstreams.http.http": "//source/extensions/upstreams/http/http:config", + "envoy.upstreams.http.tcp": "//source/extensions/upstreams/http/tcp:config", # # Watchdog actions # - - "envoy.watchdog.profile_action": "//source/extensions/watchdog/profile_action:config", + "envoy.watchdog.profile_action": "//source/extensions/watchdog/profile_action:config", # # WebAssembly runtimes # - - "envoy.wasm.runtime.null": "//source/extensions/wasm_runtime/null:config", - "envoy.wasm.runtime.v8": "//source/extensions/wasm_runtime/v8:config", - "envoy.wasm.runtime.wamr": "//source/extensions/wasm_runtime/wamr:config", - "envoy.wasm.runtime.wavm": "//source/extensions/wasm_runtime/wavm:config", - "envoy.wasm.runtime.wasmtime": "//source/extensions/wasm_runtime/wasmtime:config", + "envoy.wasm.runtime.null": "//source/extensions/wasm_runtime/null:config", + "envoy.wasm.runtime.v8": "//source/extensions/wasm_runtime/v8:config", + "envoy.wasm.runtime.wamr": "//source/extensions/wasm_runtime/wamr:config", + "envoy.wasm.runtime.wavm": "//source/extensions/wasm_runtime/wavm:config", + "envoy.wasm.runtime.wasmtime": "//source/extensions/wasm_runtime/wasmtime:config", # # Rate limit descriptors @@ -325,11 +303,11 @@ EXTENSIONS = { # # c-ares DNS resolver extension is recommended to be enabled to maintain the legacy DNS resolving behavior. - "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", + "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", # apple DNS resolver extension is only needed in MacOS build plus one want to use apple library for DNS resolving. - "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", + "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", # getaddrinfo DNS resolver extension can be used when the system resolver is desired (e.g., Android) - "envoy.network.dns_resolver.getaddrinfo": "//source/extensions/network/dns_resolver/getaddrinfo:config", + "envoy.network.dns_resolver.getaddrinfo": "//source/extensions/network/dns_resolver/getaddrinfo:config", # # Custom matchers @@ -339,8 +317,8 @@ EXTENSIONS = { # # Pattern Template # - "envoy.pattern_template.pattern_template_match_predicate": "//source/extensions/pattern_template/match:config", - "envoy.pattern_template.pattern_template_rewrite_predicate": "//source/extensions/pattern_template/rewrite:config", + "envoy.path_match_policy.pattern_template_match_predicate": "//source/extensions/pattern_template/match:config", + "envoy.path_rewrite_policy.pattern_template_rewrite_predicate": "//source/extensions/pattern_template/rewrite:config", # # Header Validators diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index 2523c7e663eef..9063e5a2d2b1b 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -746,14 +746,14 @@ envoy.matching.matchers.ip: status: stable type_urls: - envoy.extensions.matching.input_matchers.ip.v3.Ip -envoy.pattern_template.pattern_template_match_predicate: +envoy.path_match_policy.pattern_template_match_predicate: categories: - - envoy.pattern_template + - envoy.path_match_policy security_posture: robust_to_untrusted_downstream_and_upstream status: stable -envoy.pattern_template.pattern_template_rewrite_predicate: +envoy.path_rewrite_policy.pattern_template_rewrite_predicate: categories: - - envoy.pattern_template + - envoy.path_rewrite_policy security_posture: robust_to_untrusted_downstream_and_upstream status: stable envoy.quic.proof_source.filter_chain: diff --git a/source/extensions/pattern_template/BUILD b/source/extensions/pattern_template/BUILD index 3005a60d42018..e954c40872c33 100644 --- a/source/extensions/pattern_template/BUILD +++ b/source/extensions/pattern_template/BUILD @@ -24,7 +24,7 @@ envoy_cc_library( ], deps = [ ":pattern_template_internal_cc", - "//envoy/router:router_pattern_template_interface", + "//envoy/router:router_path_match_policy_interface", "//source/common/http:path_utility_lib", "//source/extensions/pattern_template/proto:pattern_template_rewrite_segements_cc_proto", "@com_google_absl//absl/status", diff --git a/source/extensions/pattern_template/match/BUILD b/source/extensions/pattern_template/match/BUILD index f9d91b740062f..70b93aa68b73b 100644 --- a/source/extensions/pattern_template/match/BUILD +++ b/source/extensions/pattern_template/match/BUILD @@ -20,7 +20,7 @@ envoy_cc_library( "//source/common/router:__subpackages__", ], deps = [ - "//envoy/router:router_pattern_template_interface", + "//envoy/router:router_path_match_policy_interface", "//source/extensions/pattern_template:pattern_template_lib", "@envoy_api//envoy/extensions/pattern_template/match/v3:pkg_cc_proto", ], @@ -34,7 +34,7 @@ envoy_cc_extension( deps = [ ":pattern_template_match_lib", "//envoy/registry", - "//envoy/router:router_pattern_template_interface", + "//envoy/router:router_path_match_policy_interface", "//source/extensions/pattern_template:pattern_template_lib", "@envoy_api//envoy/extensions/pattern_template/match/v3:pkg_cc_proto", ], diff --git a/source/extensions/pattern_template/match/config.cc b/source/extensions/pattern_template/match/config.cc index c0c6f588efb8f..f988811c1c0fc 100644 --- a/source/extensions/pattern_template/match/config.cc +++ b/source/extensions/pattern_template/match/config.cc @@ -1,7 +1,7 @@ #include "source/extensions/pattern_template/match/config.h" #include "envoy/registry/registry.h" -#include "envoy/router/pattern_template.h" +#include "envoy/router/path_match_policy.h" namespace Envoy { namespace Extensions { @@ -9,7 +9,7 @@ namespace PatternTemplate { namespace Match { REGISTER_FACTORY(PatternTemplateMatchPredicateFactory, - Router::PatternTemplateMatchPredicateFactory); + Router::PathMatchPredicateFactory); } // namespace Match } // namespace PatternTemplate } // namespace Extensions diff --git a/source/extensions/pattern_template/match/config.h b/source/extensions/pattern_template/match/config.h index b4e2ae46201c6..9ea2a482c0f35 100644 --- a/source/extensions/pattern_template/match/config.h +++ b/source/extensions/pattern_template/match/config.h @@ -1,7 +1,7 @@ #pragma once #include "envoy/extensions/pattern_template/match/v3/pattern_template_match.pb.h" -#include "envoy/router/pattern_template.h" +#include "envoy/router/path_match_policy.h" #include "source/extensions/pattern_template/match/pattern_template_match.h" @@ -10,10 +10,10 @@ namespace Extensions { namespace PatternTemplate { namespace Match { -class PatternTemplateMatchPredicateFactory : public Router::PatternTemplateMatchPredicateFactory { +class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFactory { public: - Router::PatternTemplateMatchPredicateSharedPtr - createUrlTemplateMatchPredicate(std::string url_pattern) override { + Router::PathMatchPredicateSharedPtr + createPathMatchPredicate(std::string url_pattern) override { return std::make_shared(url_pattern); } diff --git a/source/extensions/pattern_template/match/pattern_template_match.h b/source/extensions/pattern_template/match/pattern_template_match.h index 455829fd6d1e3..21cdb37d64156 100644 --- a/source/extensions/pattern_template/match/pattern_template_match.h +++ b/source/extensions/pattern_template/match/pattern_template_match.h @@ -4,7 +4,7 @@ #include #include "envoy/extensions/pattern_template/match/v3/pattern_template_match.pb.h" -#include "envoy/router/pattern_template.h" +#include "envoy/router/path_match_policy.h" #include "source/extensions/pattern_template/pattern_template.h" @@ -16,7 +16,7 @@ namespace Extensions { namespace PatternTemplate { namespace Match { -class PatternTemplateMatchPredicate : public Router::PatternTemplateMatchPredicate { +class PatternTemplateMatchPredicate : public Router::PathMatchPredicate { public: explicit PatternTemplateMatchPredicate(std::string url_pattern) : matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())) {} diff --git a/source/extensions/pattern_template/pattern_template.h b/source/extensions/pattern_template/pattern_template.h index 432c948078107..1df2bd9937cfb 100644 --- a/source/extensions/pattern_template/pattern_template.h +++ b/source/extensions/pattern_template/pattern_template.h @@ -5,7 +5,7 @@ #include "source/extensions/pattern_template/proto/pattern_template_rewrite_segments.pb.h" -#include "envoy/router/pattern_template.h" +#include "envoy/router/path_match_policy.h" #include "source/extensions/pattern_template/pattern_template_internal.h" diff --git a/source/extensions/pattern_template/rewrite/BUILD b/source/extensions/pattern_template/rewrite/BUILD index 8545742c30232..2dbde5a933c12 100644 --- a/source/extensions/pattern_template/rewrite/BUILD +++ b/source/extensions/pattern_template/rewrite/BUILD @@ -20,7 +20,7 @@ envoy_cc_library( "//source/common/router:__subpackages__", ], deps = [ - "//envoy/router:router_pattern_template_interface", + "//envoy/router:router_path_rewrite_policy_interface", "//source/extensions/pattern_template:pattern_template_lib", "@envoy_api//envoy/extensions/pattern_template/rewrite/v3:pkg_cc_proto", ], @@ -34,7 +34,7 @@ envoy_cc_extension( deps = [ ":pattern_template_rewrite_lib", "//envoy/registry", - "//envoy/router:router_pattern_template_interface", + "//envoy/router:router_path_rewrite_policy_interface", "//source/extensions/pattern_template:pattern_template_lib", "@envoy_api//envoy/extensions/pattern_template/rewrite/v3:pkg_cc_proto", ], diff --git a/source/extensions/pattern_template/rewrite/config.cc b/source/extensions/pattern_template/rewrite/config.cc index 73f0f8f4cc8bb..ed52e4db888b5 100644 --- a/source/extensions/pattern_template/rewrite/config.cc +++ b/source/extensions/pattern_template/rewrite/config.cc @@ -1,7 +1,7 @@ #include "source/extensions/pattern_template/rewrite/config.h" #include "envoy/registry/registry.h" -#include "envoy/router/pattern_template.h" +#include "envoy/router/path_rewrite_policy.h" namespace Envoy { namespace Extensions { @@ -9,7 +9,7 @@ namespace PatternTemplate { namespace Rewrite { REGISTER_FACTORY(PatternTemplateRewritePredicateFactory, - Router::PatternTemplateRewritePredicateFactory); + Router::PathRewritePredicateFactory); } // namespace Rewrite } // namespace PatternTemplate } // namespace Extensions diff --git a/source/extensions/pattern_template/rewrite/config.h b/source/extensions/pattern_template/rewrite/config.h index 89a618074f1f0..8461e8056614b 100644 --- a/source/extensions/pattern_template/rewrite/config.h +++ b/source/extensions/pattern_template/rewrite/config.h @@ -1,7 +1,7 @@ #pragma once #include "envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.pb.h" -#include "envoy/router/pattern_template.h" +#include "envoy/router/path_rewrite_policy.h" #include "source/extensions/pattern_template/rewrite/pattern_template_rewrite.h" @@ -10,10 +10,10 @@ namespace Extensions { namespace PatternTemplate { namespace Rewrite { -class PatternTemplateRewritePredicateFactory : public Router::PatternTemplateRewritePredicateFactory { +class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredicateFactory { public: - Router::PatternTemplateRewritePredicateSharedPtr - createUrlTemplateRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) override { + Router::PathRewritePredicateSharedPtr + createPathRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) override { return std::make_shared(url_pattern, url_rewrite_pattern); } diff --git a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h index 203e9ce465fbd..8d06a387fa3cf 100644 --- a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h +++ b/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h @@ -3,7 +3,7 @@ #include -#include "envoy/router/pattern_template.h" +#include "envoy/router/path_rewrite_policy.h" #include "source/extensions/pattern_template/pattern_template.h" @@ -15,7 +15,7 @@ namespace Extensions { namespace PatternTemplate { namespace Rewrite { -class PatternTemplateRewritePredicate : public Router::PatternTemplateRewritePredicate { +class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { public: explicit PatternTemplateRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) : matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())), From b99921587dff2bf081863ec9cfd7d1589743e969 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 19:29:47 +0000 Subject: [PATCH 053/238] Progress towards two extensions Signed-off-by: silverstar195 --- source/common/router/config_impl.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 0467acd4c3525..930011bd48d97 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -1013,7 +1013,7 @@ class PathTemplateRouteEntryImpl : public RouteEntryImplBase { // Router::PathMatchCriterion const std::string& matcher() const override { - throw absl::UnimplementedError("Path template matcher not implemented"); + return url_pattern_; } PathMatchType matchType() const override { return PathMatchType::Pattern; } @@ -1030,6 +1030,9 @@ class PathTemplateRouteEntryImpl : public RouteEntryImplBase { // Router::RouteEntry absl::optional currentUrlPathAfterRewrite(const Http::RequestHeaderMap& headers) const override; + +private: + std::string url_pattern_{nullptr}; }; /** From 12045aec5bed7b7165647fc24b9108de56274fe2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 19:33:57 +0000 Subject: [PATCH 054/238] Remove old test Signed-off-by: silverstar195 --- test/common/router/config_impl_test.cc | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index efd89c4da8a84..6d8473fef8933 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -5102,24 +5102,6 @@ TEST_F(RouteMatcherTest, TestPrefixAndRegexRewrites) { "Cannot specify both prefix_rewrite and regex_rewrite"); } -TEST_F(RouteMatcherTest, TestPatternRewriteConfigLoad) { - const std::string yaml = R"EOF( -virtual_hosts: -- name: path_template_rewrite - domains: ["*"] - routes: - - match: - path_template: "/bar/{country}/{lang}" - route: - path_template_rewrite: "/bar/{lang}/{country}" - cluster: www2 - )EOF"; - - NiceMock stream_info; - factory_context_.cluster_manager_.initializeClusters({"www2"}, {}); - TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); -} - TEST_F(RouteMatcherTest, TestDomainMatchOrderConfig) { const std::string yaml = R"EOF( virtual_hosts: From ec3c28bca409afd82ee46fd8b84a8416f0c7fc71 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 19:50:14 +0000 Subject: [PATCH 055/238] Changes towards two extensions Signed-off-by: silverstar195 --- envoy/router/BUILD | 1 + envoy/router/router.h | 19 +++++++++++ source/common/router/BUILD | 2 -- source/common/router/config_impl.h | 33 ++++++++++++++++--- .../pattern_template/match/config.h | 4 +-- .../pattern_template/rewrite/config.h | 4 +-- 6 files changed, 52 insertions(+), 11 deletions(-) diff --git a/envoy/router/BUILD b/envoy/router/BUILD index 9ea9cbb91ee83..0b2f8365ad1fb 100644 --- a/envoy/router/BUILD +++ b/envoy/router/BUILD @@ -65,6 +65,7 @@ envoy_cc_library( deps = [ ":internal_redirect_interface", ":router_path_match_policy_interface", + ":router_path_rewrite_policy_interface", "//envoy/access_log:access_log_interface", "//envoy/common:conn_pool_interface", "//envoy/common:matchers_interface", diff --git a/envoy/router/router.h b/envoy/router/router.h index baaa20f38aef9..4358bf4d25971 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -21,6 +21,7 @@ #include "envoy/rds/config.h" #include "envoy/router/internal_redirect.h" #include "envoy/router/path_match_policy.h" +#include "envoy/router/path_rewrite_policy.h" #include "envoy/tcp/conn_pool.h" #include "envoy/tracing/http_tracer.h" #include "envoy/type/v3/percent.pb.h" @@ -316,6 +317,24 @@ class PathMatchPolicy { virtual PathMatchPredicateSharedPtr predicate() const PURE; }; +class PathRewritePolicy { +public: + virtual ~PathRewritePolicy() = default; + + /** + * @return whether internal redirect is enabled on this route. + */ + virtual bool enabled() const PURE; + + /** + * Creates the target route predicates. This should really be called only once for each upstream + * redirect response. Creating the predicates lazily to avoid wasting CPU cycles on non-redirect + * responses, which should be the most common case. + * @return a vector of newly constructed InternalRedirectPredicate instances. + */ + virtual PathRewritePredicateSharedPtr predicate() const PURE; +}; + /** * InternalRedirectPolicy from the route configuration. */ diff --git a/source/common/router/BUILD b/source/common/router/BUILD index 2d34c2b9b0efe..e06e78ea104b4 100644 --- a/source/common/router/BUILD +++ b/source/common/router/BUILD @@ -46,8 +46,6 @@ envoy_cc_library( "//envoy/http:header_map_interface", "//envoy/router:cluster_specifier_plugin_interface", "//envoy/router:router_interface", - "//envoy/router:router_path_match_policy_interface", - "//envoy/router:router_path_rewrite_policy_interface", "//envoy/runtime:runtime_interface", "//envoy/server:filter_config_interface", # TODO(rodaine): break dependency on server "//envoy/upstream:cluster_manager_interface", diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 87078cbf5b910..e61bc7a83c9c6 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -27,8 +27,6 @@ #include "source/common/http/header_utility.h" #include "source/common/matcher/matcher.h" #include "source/common/router/config_utility.h" -#include "source/common/router/path_match_policy.h" -#include "source/common/router/path_rewrite_policy.h" #include "source/common/router/header_formatter.h" #include "source/common/router/header_parser.h" #include "source/common/router/metadatamatchcriteria_impl.h" @@ -457,6 +455,31 @@ class RouteTracingImpl : public RouteTracing { Tracing::CustomTagMap custom_tags_; }; +/** + * Implementation of InternalRedirectPolicy that reads from the proto + * InternalRedirectPolicy of the RouteAction. + */ +class PathMatchPolicyImpl : public PathMatchPolicy { +public: + // Constructor that enables internal redirect with policy_config controlling the configurable + // behaviors. + PathMatchPolicyImpl(std::string url_pattern); + + // Default constructor that disables internal redirect. + PathMatchPolicyImpl(); + + bool enabled() const override { return enabled_; } + + PathMatchPredicateSharedPtr predicate() const override; + + const std::string url_pattern_; + +private: + const bool enabled_; + PathMatchPredicateFactory* predicate_factory_; +}; + + /** * Implementation of InternalRedirectPolicy that reads from the proto * InternalRedirectPolicy of the RouteAction. @@ -472,7 +495,7 @@ class PathRewritePolicyImpl : public PathRewritePolicy { bool enabled() const override { return enabled_; } - PatnRewritePredicateSharedPtr predicate() const override; + PathRewritePredicateSharedPtr predicate() const override; const std::string url_pattern_; const std::string url_rewrite_pattern_; @@ -593,7 +616,7 @@ class RouteEntryImplBase : public RouteEntry, const InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } - const PathMatchPolicy& pathMatchPolicy() const override { + const PathMatchPolicyImpl& pathMatchPolicy() const override { return pattern_template_match_policy_; } uint32_t retryShadowBufferLimit() const override { return retry_shadow_buffer_limit_; } @@ -709,7 +732,7 @@ class RouteEntryImplBase : public RouteEntry, const InternalRedirectPolicy& internalRedirectPolicy() const override { return parent_->internalRedirectPolicy(); } - const PathMatchPolicy& pathMatchPolicy() const override { + const PathMatchPolicyImpl& pathMatchPolicy() const override { return parent_->pathMatchPolicy(); } uint32_t retryShadowBufferLimit() const override { return parent_->retryShadowBufferLimit(); } diff --git a/source/extensions/pattern_template/match/config.h b/source/extensions/pattern_template/match/config.h index 9ea2a482c0f35..5735e7419f4b5 100644 --- a/source/extensions/pattern_template/match/config.h +++ b/source/extensions/pattern_template/match/config.h @@ -21,8 +21,8 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa return std::make_unique(); } - std::string name() const override { return "envoy.pattern_template.pattern_template_match_predicate"; } - std::string category() const override { return "envoy.pattern_template"; } + std::string name() const override { return "envoy.path_match_policy.pattern_template_match_predicate"; } + std::string category() const override { return "envoy.path_match_policy"; } }; } // namespace Match diff --git a/source/extensions/pattern_template/rewrite/config.h b/source/extensions/pattern_template/rewrite/config.h index 8461e8056614b..6d22b20c7f307 100644 --- a/source/extensions/pattern_template/rewrite/config.h +++ b/source/extensions/pattern_template/rewrite/config.h @@ -21,8 +21,8 @@ class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredica return std::make_unique(); } - std::string name() const override { return "envoy.pattern_template.pattern_template_rewrite_predicate"; } - + std::string name() const override { return "envoy.path_rewrite_policy.pattern_template_rewrite_predicate"; } + std::string category() const override { return "envoy.path_rewrite_policy"; } }; } // namespace Rewrite From 0eff4184ad011de9e404b439fd23349c2bd13386 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 21 Jul 2022 19:51:00 +0000 Subject: [PATCH 056/238] Format Signed-off-by: silverstar195 --- source/common/router/config_impl.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 930011bd48d97..de325e6087b34 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -1012,9 +1012,7 @@ class PathTemplateRouteEntryImpl : public RouteEntryImplBase { ProtobufMessage::ValidationVisitor& validator); // Router::PathMatchCriterion - const std::string& matcher() const override { - return url_pattern_; - } + const std::string& matcher() const override { return url_pattern_; } PathMatchType matchType() const override { return PathMatchType::Pattern; } From 38b9220482c8a71b194da3582950da37c09f3349 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 22 Jul 2022 14:35:39 +0000 Subject: [PATCH 057/238] Added comments Signed-off-by: silverstar195 --- api/envoy/config/route/v3/route_components.proto | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index a12f0612d04af..9c03a25f832ab 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -572,6 +572,7 @@ message RouteMatch { // Expect the value to not contain ``?`` or ``#`` and not to end in ``/`` string path_separated_prefix = 14 [(validate.rules).string = {pattern: "^[^?#]+[^?#/]$"}]; + // [#comment: TODO(silverstar195): Hook into extension once added] core.v3.TypedExtensionConfig path_match_policy = 15; } @@ -1077,6 +1078,7 @@ message RouteAction { // ``/aaa/yyy/bbb``. type.matcher.v3.RegexMatchAndSubstitute regex_rewrite = 32; + // [#comment: TODO(silverstar195): Hook into extension once added] core.v3.TypedExtensionConfig path_rewrite_policy = 41; oneof host_rewrite_specifier { From 2865e4b53470ddf869310ec9727c11199b188cd4 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 22 Jul 2022 22:06:59 +0000 Subject: [PATCH 058/238] Small changes Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 33 ++++++++++++++++++++++------- source/common/router/config_impl.h | 9 ++++++-- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 24cf8e3d3e589..0c3f6345a6730 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -106,7 +106,7 @@ RouteEntryImplBaseConstSharedPtr createAndValidateRoute( break; } case envoy::config::route::v3::RouteMatch::PathSpecifierCase::kPathMatchPolicy: { - route = std::make_shared(vhost, route_config, optional_http_filters, + route = std::make_shared(vhost, route_config, optional_http_filters, factory_context, validator); break; } @@ -361,13 +361,14 @@ std::vector InternalRedirectPolicyImpl::pred PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; -PathMatchPolicyImpl::PathMatchPolicyImpl(const ProtobufWkt::Any& typed_config) - : url_pattern_(url_pattern), url_rewrite_pattern_(url_rewrite_pattern), enabled_(true) { +PathMatchPolicyImpl::PathMatchPolicyImpl(const ProtobufWkt::Any& typed_config, + ProtobufMessage::ValidationVisitor& validator) + : enabled_(true) { absl::string_view name = "envoy.url_template.pattern_template_predicates"; - auto* factory = Registry::FactoryRegistry::getFactory(name); + auto* factory = Registry::FactoryRegistry::getFactory(name); ASSERT(factory); // factory not found - ProtobufTypes::MessagePtr proto_config = factory->createEmptyRouteConfigProto(); + ProtobufTypes::MessagePtr proto_config = factory->createEmptyConfigProto(); Envoy::Config::Utility::translateOpaqueConfig(typed_config, validator, *proto_config); // validate with match input factory @@ -502,8 +503,8 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, ProtobufMessage::ValidationVisitor& validator) : case_sensitive_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.match(), case_sensitive, true)), prefix_rewrite_(route.route().prefix_rewrite()), - path_match_policy_(buildPathMatchPolicy(route.match())), - path_rewrite_policy_(buildPathRewritePolicy(route.route())), + path_match_policy_(buildPathMatchPolicy(route.match(), validator)), + path_rewrite_policy_(buildPathRewritePolicy(route.route(), validator)), host_rewrite_(route.route().host_rewrite_literal()), vhost_(vhost), auto_host_rewrite_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.route(), auto_host_rewrite, false)), auto_host_rewrite_header_(!route.route().host_rewrite_header().empty() @@ -1203,8 +1204,24 @@ RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::RouteAction } } +// TODO +PathRewritePolicyImpl +RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::RouteAction route, + ProtobufMessage::ValidationVisitor& validator) const { + // match + rewrite + if (route.has_path_match_policy()) { + if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { + throw EnvoyException(fmt::format("path_match_policy {} is invalid", path_template)); + } + return PathMatchPolicyImpl(route.path_match_policy()); + } + return PathMatchPolicyImpl(); +} + +// TODO PathMatchPolicyImpl -RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::RouteMatch route) const { +RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::RouteAction route, + ProtobufMessage::ValidationVisitor& validator) const { // match + rewrite if (route.has_path_match_policy()) { if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index e61bc7a83c9c6..3239787b6a74f 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -617,7 +617,7 @@ class RouteEntryImplBase : public RouteEntry, return internal_redirect_policy_; } const PathMatchPolicyImpl& pathMatchPolicy() const override { - return pattern_template_match_policy_; + return path_match_policy_; } uint32_t retryShadowBufferLimit() const override { return retry_shadow_buffer_limit_; } const std::vector& shadowPolicies() const override { return shadow_policies_; } @@ -983,7 +983,12 @@ class RouteEntryImplBase : public RouteEntry, absl::string_view current_route_name) const; PathMatchPolicyImpl - buildPathMatchPolicy(std::string path_template, std::string path_template_rewrite) const; + buildPathMatchPolicy(envoy::config::route::v3::Route route, + ProtobufMessage::ValidationVisitor& validator) const; + + PathMatchPolicyImpl + buildPathRewritePolicy(envoy::config::route::v3::RouteAction route_action, + ProtobufMessage::ValidationVisitor& validator) const; RouteConstSharedPtr pickClusterViaClusterHeader(const Http::LowerCaseString& cluster_header_name, const Http::HeaderMap& headers) const; From a4d912490ffb5a032d7d224cde65d4ad9e48ff2b Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 22 Jul 2022 22:09:51 +0000 Subject: [PATCH 059/238] Small changes Signed-off-by: silverstar195 --- api/envoy/config/route/v3/route_components.proto | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index 9c03a25f832ab..75d145d1f4a91 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -1021,7 +1021,7 @@ message RouteAction { // // Only one of :ref:`regex_rewrite `, // :ref:`path_rewrite_policy ` - // or ``prefix_rewrite`` may be specified. + // or :ref:`prefix_rewrite ` may be specified. // // .. attention:: // @@ -1056,7 +1056,7 @@ message RouteAction { // before the rewrite into the :ref:`x-envoy-original-path // ` header. // - // Only one of ``regex_rewrite``, :ref:`prefix_rewrite `, + // Only one of :ref:`regex_rewrite `, :ref:`prefix_rewrite `, // or :ref:`path_rewrite_policy ` // may be specified. // From cbb117db7d07b590908f001affc2944abcd349f2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 25 Jul 2022 14:08:13 +0000 Subject: [PATCH 060/238] Change extension format Signed-off-by: silverstar195 --- .../extensions/{pattern_template => path}/match/v3/BUILD | 0 .../match/v3/pattern_template_match.proto | 4 ++-- .../extensions/{pattern_template => path}/rewrite/v3/BUILD | 0 .../rewrite/v3/pattern_template_rewrite.proto | 6 +++--- 4 files changed, 5 insertions(+), 5 deletions(-) rename api/envoy/extensions/{pattern_template => path}/match/v3/BUILD (100%) rename api/envoy/extensions/{pattern_template => path}/match/v3/pattern_template_match.proto (92%) rename api/envoy/extensions/{pattern_template => path}/rewrite/v3/BUILD (100%) rename api/envoy/extensions/{pattern_template => path}/rewrite/v3/pattern_template_rewrite.proto (92%) diff --git a/api/envoy/extensions/pattern_template/match/v3/BUILD b/api/envoy/extensions/path/match/v3/BUILD similarity index 100% rename from api/envoy/extensions/pattern_template/match/v3/BUILD rename to api/envoy/extensions/path/match/v3/BUILD diff --git a/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto b/api/envoy/extensions/path/match/v3/pattern_template_match.proto similarity index 92% rename from api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto rename to api/envoy/extensions/path/match/v3/pattern_template_match.proto index 2f3db17bbcfa9..8a2d36c6cab1d 100644 --- a/api/envoy/extensions/pattern_template/match/v3/pattern_template_match.proto +++ b/api/envoy/extensions/path/match/v3/pattern_template_match.proto @@ -1,6 +1,6 @@ syntax = "proto3"; -package envoy.extensions.pattern_template.match.v3; +package envoy.extensions.path.match.pattern_template.v3; import "udpa/annotations/status.proto"; import "validate/validate.proto"; @@ -8,7 +8,7 @@ import "validate/validate.proto"; option java_package = "io.envoyproxy.envoy.extensions.pattern_template.match.v3"; option java_outer_classname = "PatternTemplateMatchProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/match/v3;matchv3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/path/match/pattern_template/v3;matchpattern_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Pattern Template Match Config] diff --git a/api/envoy/extensions/pattern_template/rewrite/v3/BUILD b/api/envoy/extensions/path/rewrite/v3/BUILD similarity index 100% rename from api/envoy/extensions/pattern_template/rewrite/v3/BUILD rename to api/envoy/extensions/path/rewrite/v3/BUILD diff --git a/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto b/api/envoy/extensions/path/rewrite/v3/pattern_template_rewrite.proto similarity index 92% rename from api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto rename to api/envoy/extensions/path/rewrite/v3/pattern_template_rewrite.proto index a196e18a5586e..1c444af358ca0 100644 --- a/api/envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/path/rewrite/v3/pattern_template_rewrite.proto @@ -1,14 +1,14 @@ syntax = "proto3"; -package envoy.extensions.pattern_template.rewrite.v3; +package envoy.extensions.path.rewrite.pattern_template.v3; import "udpa/annotations/status.proto"; import "validate/validate.proto"; -option java_package = "io.envoyproxy.envoy.extensions.pattern_template.rewrite.v3"; +option java_package = "io.envoyproxy.envoy.extensions.path.rewrite.pattern_template.v3"; option java_outer_classname = "PatternTemplateRewriteProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/rewrite/v3;rewritev3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/path/rewrite/pattern_template/v3;rewritepattern_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Pattern Template Rewrite Config] From 9714c7ae6b95ff4902e33c07bb8559af6437c1c2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 25 Jul 2022 14:24:10 +0000 Subject: [PATCH 061/238] Change extension format BUILDS Signed-off-by: silverstar195 --- api/BUILD | 4 ++-- api/versioning/BUILD | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/api/BUILD b/api/BUILD index 7b6e3fe7d344f..f363fbca13504 100644 --- a/api/BUILD +++ b/api/BUILD @@ -241,8 +241,8 @@ proto_library( "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", - "//envoy/extensions/pattern_template/match/v3:pkg", - "//envoy/extensions/pattern_template/rewrite/v3:pkg", + "//envoy/extensions/path/match/v3:pkg", + "//envoy/extensions/path/rewrite/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", diff --git a/api/versioning/BUILD b/api/versioning/BUILD index f751c85e940ce..8874ccf835eaa 100644 --- a/api/versioning/BUILD +++ b/api/versioning/BUILD @@ -183,8 +183,8 @@ proto_library( "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", - "//envoy/extensions/pattern_template/match/v3:pkg", - "//envoy/extensions/pattern_template/rewrite/v3:pkg", + "//envoy/extensions/path/match/v3:pkg", + "//envoy/extensions/path/rewrite/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", From ebd39a30741062d046957bc610ccd7a435856f2e Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 25 Jul 2022 14:35:39 +0000 Subject: [PATCH 062/238] Fix package name Signed-off-by: silverstar195 --- api/envoy/extensions/path/match/v3/pattern_template_match.proto | 2 +- .../extensions/path/rewrite/v3/pattern_template_rewrite.proto | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/envoy/extensions/path/match/v3/pattern_template_match.proto b/api/envoy/extensions/path/match/v3/pattern_template_match.proto index 8a2d36c6cab1d..b3563f10b70de 100644 --- a/api/envoy/extensions/path/match/v3/pattern_template_match.proto +++ b/api/envoy/extensions/path/match/v3/pattern_template_match.proto @@ -8,7 +8,7 @@ import "validate/validate.proto"; option java_package = "io.envoyproxy.envoy.extensions.pattern_template.match.v3"; option java_outer_classname = "PatternTemplateMatchProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/path/match/pattern_template/v3;matchpattern_templatev3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/path/match/pattern_template/v3;pattern_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Pattern Template Match Config] diff --git a/api/envoy/extensions/path/rewrite/v3/pattern_template_rewrite.proto b/api/envoy/extensions/path/rewrite/v3/pattern_template_rewrite.proto index 1c444af358ca0..2d5cebe876120 100644 --- a/api/envoy/extensions/path/rewrite/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/path/rewrite/v3/pattern_template_rewrite.proto @@ -8,7 +8,7 @@ import "validate/validate.proto"; option java_package = "io.envoyproxy.envoy.extensions.path.rewrite.pattern_template.v3"; option java_outer_classname = "PatternTemplateRewriteProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/path/rewrite/pattern_template/v3;rewritepattern_templatev3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/path/rewrite/pattern_template/v3;pattern_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Pattern Template Rewrite Config] From 1967be1d83681efb3d5a1a836200d75f7e76ca7f Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 25 Jul 2022 14:52:02 +0000 Subject: [PATCH 063/238] Fix build paths Signed-off-by: silverstar195 --- api/BUILD | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/BUILD b/api/BUILD index f363fbca13504..a8a01964eff9f 100644 --- a/api/BUILD +++ b/api/BUILD @@ -241,8 +241,8 @@ proto_library( "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", - "//envoy/extensions/path/match/v3:pkg", - "//envoy/extensions/path/rewrite/v3:pkg", + "//envoy/extensions/path/match/pattern_template/v3:pkg", + "//envoy/extensions/path/rewrite/pattern_template/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", From b9f6c707a5b1798dee3946f20cf43a6c398f084a Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 25 Jul 2022 14:57:54 +0000 Subject: [PATCH 064/238] Fix build paths Signed-off-by: silverstar195 --- api/BUILD | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/BUILD b/api/BUILD index a8a01964eff9f..f363fbca13504 100644 --- a/api/BUILD +++ b/api/BUILD @@ -241,8 +241,8 @@ proto_library( "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", - "//envoy/extensions/path/match/pattern_template/v3:pkg", - "//envoy/extensions/path/rewrite/pattern_template/v3:pkg", + "//envoy/extensions/path/match/v3:pkg", + "//envoy/extensions/path/rewrite/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", From fb72d9637369354b234331b6ea6d2f334de5b5fa Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 25 Jul 2022 15:09:10 +0000 Subject: [PATCH 065/238] Fix build paths Signed-off-by: silverstar195 --- api/envoy/extensions/path/match/v3/pattern_template_match.proto | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/envoy/extensions/path/match/v3/pattern_template_match.proto b/api/envoy/extensions/path/match/v3/pattern_template_match.proto index b3563f10b70de..24e6d96c44c98 100644 --- a/api/envoy/extensions/path/match/v3/pattern_template_match.proto +++ b/api/envoy/extensions/path/match/v3/pattern_template_match.proto @@ -5,7 +5,7 @@ package envoy.extensions.path.match.pattern_template.v3; import "udpa/annotations/status.proto"; import "validate/validate.proto"; -option java_package = "io.envoyproxy.envoy.extensions.pattern_template.match.v3"; +option java_package = "io.envoyproxy.envoy.extensions.path.match.pattern_template.v3"; option java_outer_classname = "PatternTemplateMatchProto"; option java_multiple_files = true; option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/path/match/pattern_template/v3;pattern_templatev3"; From 6277e915943ff16b22c54f29a454b876109200c9 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 25 Jul 2022 19:04:03 +0000 Subject: [PATCH 066/238] Change extension paths Signed-off-by: silverstar195 --- source/extensions/extensions_build_config.bzl | 349 ++++++++++-------- .../{pattern_template => path}/match/BUILD | 0 .../match/config.cc | 0 .../{pattern_template => path}/match/config.h | 0 .../match/pattern_template_match.cc | 0 .../match/pattern_template_match.h | 0 .../{ => path}/pattern_template/BUILD | 0 .../pattern_template/pattern_template.cc | 0 .../pattern_template/pattern_template.h | 0 .../pattern_template_internal.cc | 0 .../pattern_template_internal.h | 0 .../pattern_template_internal_test.cc | 0 .../pattern_template/pattern_template_test.cc | 0 .../{ => path}/pattern_template/proto/BUILD | 0 .../pattern_template_rewrite_segments.proto | 0 .../{pattern_template => path}/rewrite/BUILD | 0 .../rewrite/config.cc | 0 .../rewrite/config.h | 0 .../rewrite/pattern_template_rewrite.cc | 0 .../rewrite/pattern_template_rewrite.h | 0 20 files changed, 190 insertions(+), 159 deletions(-) rename source/extensions/{pattern_template => path}/match/BUILD (100%) rename source/extensions/{pattern_template => path}/match/config.cc (100%) rename source/extensions/{pattern_template => path}/match/config.h (100%) rename source/extensions/{pattern_template => path}/match/pattern_template_match.cc (100%) rename source/extensions/{pattern_template => path}/match/pattern_template_match.h (100%) rename source/extensions/{ => path}/pattern_template/BUILD (100%) rename source/extensions/{ => path}/pattern_template/pattern_template.cc (100%) rename source/extensions/{ => path}/pattern_template/pattern_template.h (100%) rename source/extensions/{ => path}/pattern_template/pattern_template_internal.cc (100%) rename source/extensions/{ => path}/pattern_template/pattern_template_internal.h (100%) rename source/extensions/{ => path}/pattern_template/pattern_template_internal_test.cc (100%) rename source/extensions/{ => path}/pattern_template/pattern_template_test.cc (100%) rename source/extensions/{ => path}/pattern_template/proto/BUILD (100%) rename source/extensions/{ => path}/pattern_template/proto/pattern_template_rewrite_segments.proto (100%) rename source/extensions/{pattern_template => path}/rewrite/BUILD (100%) rename source/extensions/{pattern_template => path}/rewrite/config.cc (100%) rename source/extensions/{pattern_template => path}/rewrite/config.h (100%) rename source/extensions/{pattern_template => path}/rewrite/pattern_template_rewrite.cc (100%) rename source/extensions/{pattern_template => path}/rewrite/pattern_template_rewrite.h (100%) diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 2e5ab87f8aacc..197e061b3b79e 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -3,332 +3,363 @@ EXTENSIONS = { # # Access loggers # - "envoy.access_loggers.file": "//source/extensions/access_loggers/file:config", - "envoy.access_loggers.extension_filters.cel": "//source/extensions/access_loggers/filters/cel:config", - "envoy.access_loggers.http_grpc": "//source/extensions/access_loggers/grpc:http_config", - "envoy.access_loggers.tcp_grpc": "//source/extensions/access_loggers/grpc:tcp_config", - "envoy.access_loggers.open_telemetry": "//source/extensions/access_loggers/open_telemetry:config", - "envoy.access_loggers.stdout": "//source/extensions/access_loggers/stream:config", - "envoy.access_loggers.stderr": "//source/extensions/access_loggers/stream:config", - "envoy.access_loggers.wasm": "//source/extensions/access_loggers/wasm:config", + + "envoy.access_loggers.file": "//source/extensions/access_loggers/file:config", + "envoy.access_loggers.extension_filters.cel": "//source/extensions/access_loggers/filters/cel:config", + "envoy.access_loggers.http_grpc": "//source/extensions/access_loggers/grpc:http_config", + "envoy.access_loggers.tcp_grpc": "//source/extensions/access_loggers/grpc:tcp_config", + "envoy.access_loggers.open_telemetry": "//source/extensions/access_loggers/open_telemetry:config", + "envoy.access_loggers.stdout": "//source/extensions/access_loggers/stream:config", + "envoy.access_loggers.stderr": "//source/extensions/access_loggers/stream:config", + "envoy.access_loggers.wasm": "//source/extensions/access_loggers/wasm:config", # # Clusters # - "envoy.clusters.aggregate": "//source/extensions/clusters/aggregate:cluster", - "envoy.clusters.dynamic_forward_proxy": "//source/extensions/clusters/dynamic_forward_proxy:cluster", - "envoy.clusters.redis": "//source/extensions/clusters/redis:redis_cluster", + + "envoy.clusters.aggregate": "//source/extensions/clusters/aggregate:cluster", + "envoy.clusters.dynamic_forward_proxy": "//source/extensions/clusters/dynamic_forward_proxy:cluster", + "envoy.clusters.redis": "//source/extensions/clusters/redis:redis_cluster", # # Compression # - "envoy.compression.gzip.compressor": "//source/extensions/compression/gzip/compressor:config", - "envoy.compression.gzip.decompressor": "//source/extensions/compression/gzip/decompressor:config", - "envoy.compression.brotli.compressor": "//source/extensions/compression/brotli/compressor:config", - "envoy.compression.brotli.decompressor": "//source/extensions/compression/brotli/decompressor:config", - "envoy.compression.zstd.compressor": "//source/extensions/compression/zstd/compressor:config", - "envoy.compression.zstd.decompressor": "//source/extensions/compression/zstd/decompressor:config", + + "envoy.compression.gzip.compressor": "//source/extensions/compression/gzip/compressor:config", + "envoy.compression.gzip.decompressor": "//source/extensions/compression/gzip/decompressor:config", + "envoy.compression.brotli.compressor": "//source/extensions/compression/brotli/compressor:config", + "envoy.compression.brotli.decompressor": "//source/extensions/compression/brotli/decompressor:config", + "envoy.compression.zstd.compressor": "//source/extensions/compression/zstd/compressor:config", + "envoy.compression.zstd.decompressor": "//source/extensions/compression/zstd/decompressor:config", # # Config validators # - "envoy.config.validators.minimum_clusters": "//source/extensions/config/validators/minimum_clusters:config", + + "envoy.config.validators.minimum_clusters_validator": "//source/extensions/config/validators/minimum_clusters:config", # # gRPC Credentials Plugins # - "envoy.grpc_credentials.file_based_metadata": "//source/extensions/grpc_credentials/file_based_metadata:config", - "envoy.grpc_credentials.aws_iam": "//source/extensions/grpc_credentials/aws_iam:config", + + "envoy.grpc_credentials.file_based_metadata": "//source/extensions/grpc_credentials/file_based_metadata:config", + "envoy.grpc_credentials.aws_iam": "//source/extensions/grpc_credentials/aws_iam:config", # # WASM # - "envoy.bootstrap.wasm": "//source/extensions/bootstrap/wasm:config", + + "envoy.bootstrap.wasm": "//source/extensions/bootstrap/wasm:config", # # Health checkers # - "envoy.health_checkers.redis": "//source/extensions/health_checkers/redis:config", + + "envoy.health_checkers.redis": "//source/extensions/health_checkers/redis:config", # # Input Matchers # - "envoy.matching.matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", - "envoy.matching.matchers.ip": "//source/extensions/matching/input_matchers/ip:config", + + "envoy.matching.matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", + "envoy.matching.matchers.ip": "//source/extensions/matching/input_matchers/ip:config", # # Generic Inputs # - "envoy.matching.common_inputs.environment_variable": "//source/extensions/matching/common_inputs/environment_variable:config", + + "envoy.matching.common_inputs.environment_variable": "//source/extensions/matching/common_inputs/environment_variable:config", # # HTTP filters # - "envoy.filters.http.adaptive_concurrency": "//source/extensions/filters/http/adaptive_concurrency:config", - "envoy.filters.http.admission_control": "//source/extensions/filters/http/admission_control:config", - "envoy.filters.http.alternate_protocols_cache": "//source/extensions/filters/http/alternate_protocols_cache:config", - "envoy.filters.http.aws_lambda": "//source/extensions/filters/http/aws_lambda:config", - "envoy.filters.http.aws_request_signing": "//source/extensions/filters/http/aws_request_signing:config", - "envoy.filters.http.bandwidth_limit": "//source/extensions/filters/http/bandwidth_limit:config", - "envoy.filters.http.buffer": "//source/extensions/filters/http/buffer:config", - "envoy.filters.http.cache": "//source/extensions/filters/http/cache:config", - "envoy.filters.http.cdn_loop": "//source/extensions/filters/http/cdn_loop:config", - "envoy.filters.http.compressor": "//source/extensions/filters/http/compressor:config", - "envoy.filters.http.cors": "//source/extensions/filters/http/cors:config", - "envoy.filters.http.composite": "//source/extensions/filters/http/composite:config", - "envoy.filters.http.csrf": "//source/extensions/filters/http/csrf:config", - "envoy.filters.http.decompressor": "//source/extensions/filters/http/decompressor:config", - "envoy.filters.http.dynamic_forward_proxy": "//source/extensions/filters/http/dynamic_forward_proxy:config", - "envoy.filters.http.ext_authz": "//source/extensions/filters/http/ext_authz:config", - "envoy.filters.http.ext_proc": "//source/extensions/filters/http/ext_proc:config", - "envoy.filters.http.fault": "//source/extensions/filters/http/fault:config", - "envoy.filters.http.file_system_buffer": "//source/extensions/filters/http/file_system_buffer:config", - "envoy.filters.http.gcp_authn": "//source/extensions/filters/http/gcp_authn:config", - "envoy.filters.http.grpc_http1_bridge": "//source/extensions/filters/http/grpc_http1_bridge:config", - "envoy.filters.http.grpc_http1_reverse_bridge": "//source/extensions/filters/http/grpc_http1_reverse_bridge:config", - "envoy.filters.http.grpc_json_transcoder": "//source/extensions/filters/http/grpc_json_transcoder:config", - "envoy.filters.http.grpc_stats": "//source/extensions/filters/http/grpc_stats:config", - "envoy.filters.http.grpc_web": "//source/extensions/filters/http/grpc_web:config", - "envoy.filters.http.header_to_metadata": "//source/extensions/filters/http/header_to_metadata:config", - "envoy.filters.http.health_check": "//source/extensions/filters/http/health_check:config", - "envoy.filters.http.ip_tagging": "//source/extensions/filters/http/ip_tagging:config", - "envoy.filters.http.jwt_authn": "//source/extensions/filters/http/jwt_authn:config", + + "envoy.filters.http.adaptive_concurrency": "//source/extensions/filters/http/adaptive_concurrency:config", + "envoy.filters.http.admission_control": "//source/extensions/filters/http/admission_control:config", + "envoy.filters.http.alternate_protocols_cache": "//source/extensions/filters/http/alternate_protocols_cache:config", + "envoy.filters.http.aws_lambda": "//source/extensions/filters/http/aws_lambda:config", + "envoy.filters.http.aws_request_signing": "//source/extensions/filters/http/aws_request_signing:config", + "envoy.filters.http.bandwidth_limit": "//source/extensions/filters/http/bandwidth_limit:config", + "envoy.filters.http.buffer": "//source/extensions/filters/http/buffer:config", + "envoy.filters.http.cache": "//source/extensions/filters/http/cache:config", + "envoy.filters.http.cdn_loop": "//source/extensions/filters/http/cdn_loop:config", + "envoy.filters.http.compressor": "//source/extensions/filters/http/compressor:config", + "envoy.filters.http.cors": "//source/extensions/filters/http/cors:config", + "envoy.filters.http.composite": "//source/extensions/filters/http/composite:config", + "envoy.filters.http.csrf": "//source/extensions/filters/http/csrf:config", + "envoy.filters.http.decompressor": "//source/extensions/filters/http/decompressor:config", + "envoy.filters.http.dynamic_forward_proxy": "//source/extensions/filters/http/dynamic_forward_proxy:config", + "envoy.filters.http.ext_authz": "//source/extensions/filters/http/ext_authz:config", + "envoy.filters.http.ext_proc": "//source/extensions/filters/http/ext_proc:config", + "envoy.filters.http.fault": "//source/extensions/filters/http/fault:config", + "envoy.filters.http.file_system_buffer": "//source/extensions/filters/http/file_system_buffer:config", + "envoy.filters.http.gcp_authn": "//source/extensions/filters/http/gcp_authn:config", + "envoy.filters.http.grpc_http1_bridge": "//source/extensions/filters/http/grpc_http1_bridge:config", + "envoy.filters.http.grpc_http1_reverse_bridge": "//source/extensions/filters/http/grpc_http1_reverse_bridge:config", + "envoy.filters.http.grpc_json_transcoder": "//source/extensions/filters/http/grpc_json_transcoder:config", + "envoy.filters.http.grpc_stats": "//source/extensions/filters/http/grpc_stats:config", + "envoy.filters.http.grpc_web": "//source/extensions/filters/http/grpc_web:config", + "envoy.filters.http.header_to_metadata": "//source/extensions/filters/http/header_to_metadata:config", + "envoy.filters.http.health_check": "//source/extensions/filters/http/health_check:config", + "envoy.filters.http.ip_tagging": "//source/extensions/filters/http/ip_tagging:config", + "envoy.filters.http.jwt_authn": "//source/extensions/filters/http/jwt_authn:config", # Disabled by default - "envoy.filters.http.kill_request": "//source/extensions/filters/http/kill_request:kill_request_config", - "envoy.filters.http.local_ratelimit": "//source/extensions/filters/http/local_ratelimit:config", - "envoy.filters.http.lua": "//source/extensions/filters/http/lua:config", - "envoy.filters.http.oauth2": "//source/extensions/filters/http/oauth2:config", - "envoy.filters.http.on_demand": "//source/extensions/filters/http/on_demand:config", - "envoy.filters.http.original_src": "//source/extensions/filters/http/original_src:config", - "envoy.filters.http.ratelimit": "//source/extensions/filters/http/ratelimit:config", - "envoy.filters.http.rbac": "//source/extensions/filters/http/rbac:config", - "envoy.filters.http.router": "//source/extensions/filters/http/router:config", - "envoy.filters.http.set_metadata": "//source/extensions/filters/http/set_metadata:config", - "envoy.filters.http.tap": "//source/extensions/filters/http/tap:config", - "envoy.filters.http.wasm": "//source/extensions/filters/http/wasm:config", - "envoy.filters.http.stateful_session": "//source/extensions/filters/http/stateful_session:config", + "envoy.filters.http.kill_request": "//source/extensions/filters/http/kill_request:kill_request_config", + "envoy.filters.http.local_ratelimit": "//source/extensions/filters/http/local_ratelimit:config", + "envoy.filters.http.lua": "//source/extensions/filters/http/lua:config", + "envoy.filters.http.oauth2": "//source/extensions/filters/http/oauth2:config", + "envoy.filters.http.on_demand": "//source/extensions/filters/http/on_demand:config", + "envoy.filters.http.original_src": "//source/extensions/filters/http/original_src:config", + "envoy.filters.http.ratelimit": "//source/extensions/filters/http/ratelimit:config", + "envoy.filters.http.rbac": "//source/extensions/filters/http/rbac:config", + "envoy.filters.http.router": "//source/extensions/filters/http/router:config", + "envoy.filters.http.set_metadata": "//source/extensions/filters/http/set_metadata:config", + "envoy.filters.http.tap": "//source/extensions/filters/http/tap:config", + "envoy.filters.http.wasm": "//source/extensions/filters/http/wasm:config", + "envoy.filters.http.stateful_session": "//source/extensions/filters/http/stateful_session:config", # # Listener filters # - "envoy.filters.listener.http_inspector": "//source/extensions/filters/listener/http_inspector:config", + + "envoy.filters.listener.http_inspector": "//source/extensions/filters/listener/http_inspector:config", # NOTE: The original_dst filter is implicitly loaded if original_dst functionality is # configured on the listener. Do not remove it in that case or configs will fail to load. - "envoy.filters.listener.original_dst": "//source/extensions/filters/listener/original_dst:config", - "envoy.filters.listener.original_src": "//source/extensions/filters/listener/original_src:config", + "envoy.filters.listener.original_dst": "//source/extensions/filters/listener/original_dst:config", + "envoy.filters.listener.original_src": "//source/extensions/filters/listener/original_src:config", # NOTE: The proxy_protocol filter is implicitly loaded if proxy_protocol functionality is # configured on the listener. Do not remove it in that case or configs will fail to load. - "envoy.filters.listener.proxy_protocol": "//source/extensions/filters/listener/proxy_protocol:config", - "envoy.filters.listener.tls_inspector": "//source/extensions/filters/listener/tls_inspector:config", + "envoy.filters.listener.proxy_protocol": "//source/extensions/filters/listener/proxy_protocol:config", + "envoy.filters.listener.tls_inspector": "//source/extensions/filters/listener/tls_inspector:config", # # Network filters # - "envoy.filters.network.connection_limit": "//source/extensions/filters/network/connection_limit:config", - "envoy.filters.network.direct_response": "//source/extensions/filters/network/direct_response:config", - "envoy.filters.network.dubbo_proxy": "//source/extensions/filters/network/dubbo_proxy:config", - "envoy.filters.network.echo": "//source/extensions/filters/network/echo:config", - "envoy.filters.network.ext_authz": "//source/extensions/filters/network/ext_authz:config", - "envoy.filters.network.http_connection_manager": "//source/extensions/filters/network/http_connection_manager:config", - "envoy.filters.network.local_ratelimit": "//source/extensions/filters/network/local_ratelimit:config", - "envoy.filters.network.mongo_proxy": "//source/extensions/filters/network/mongo_proxy:config", - "envoy.filters.network.ratelimit": "//source/extensions/filters/network/ratelimit:config", - "envoy.filters.network.rbac": "//source/extensions/filters/network/rbac:config", - "envoy.filters.network.redis_proxy": "//source/extensions/filters/network/redis_proxy:config", - "envoy.filters.network.tcp_proxy": "//source/extensions/filters/network/tcp_proxy:config", - "envoy.filters.network.thrift_proxy": "//source/extensions/filters/network/thrift_proxy:config", - "envoy.filters.network.sni_cluster": "//source/extensions/filters/network/sni_cluster:config", - "envoy.filters.network.sni_dynamic_forward_proxy": "//source/extensions/filters/network/sni_dynamic_forward_proxy:config", - "envoy.filters.network.wasm": "//source/extensions/filters/network/wasm:config", - "envoy.filters.network.zookeeper_proxy": "//source/extensions/filters/network/zookeeper_proxy:config", + + "envoy.filters.network.connection_limit": "//source/extensions/filters/network/connection_limit:config", + "envoy.filters.network.direct_response": "//source/extensions/filters/network/direct_response:config", + "envoy.filters.network.dubbo_proxy": "//source/extensions/filters/network/dubbo_proxy:config", + "envoy.filters.network.echo": "//source/extensions/filters/network/echo:config", + "envoy.filters.network.ext_authz": "//source/extensions/filters/network/ext_authz:config", + "envoy.filters.network.http_connection_manager": "//source/extensions/filters/network/http_connection_manager:config", + "envoy.filters.network.local_ratelimit": "//source/extensions/filters/network/local_ratelimit:config", + "envoy.filters.network.mongo_proxy": "//source/extensions/filters/network/mongo_proxy:config", + "envoy.filters.network.ratelimit": "//source/extensions/filters/network/ratelimit:config", + "envoy.filters.network.rbac": "//source/extensions/filters/network/rbac:config", + "envoy.filters.network.redis_proxy": "//source/extensions/filters/network/redis_proxy:config", + "envoy.filters.network.tcp_proxy": "//source/extensions/filters/network/tcp_proxy:config", + "envoy.filters.network.thrift_proxy": "//source/extensions/filters/network/thrift_proxy:config", + "envoy.filters.network.sni_cluster": "//source/extensions/filters/network/sni_cluster:config", + "envoy.filters.network.sni_dynamic_forward_proxy": "//source/extensions/filters/network/sni_dynamic_forward_proxy:config", + "envoy.filters.network.wasm": "//source/extensions/filters/network/wasm:config", + "envoy.filters.network.zookeeper_proxy": "//source/extensions/filters/network/zookeeper_proxy:config", # # UDP filters # - "envoy.filters.udp.dns_filter": "//source/extensions/filters/udp/dns_filter:config", - "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", + + "envoy.filters.udp.dns_filter": "//source/extensions/filters/udp/dns_filter:config", + "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", # # Resource monitors # - "envoy.resource_monitors.fixed_heap": "//source/extensions/resource_monitors/fixed_heap:config", - "envoy.resource_monitors.injected_resource": "//source/extensions/resource_monitors/injected_resource:config", + + "envoy.resource_monitors.fixed_heap": "//source/extensions/resource_monitors/fixed_heap:config", + "envoy.resource_monitors.injected_resource": "//source/extensions/resource_monitors/injected_resource:config", # # Stat sinks # - "envoy.stat_sinks.dog_statsd": "//source/extensions/stat_sinks/dog_statsd:config", - "envoy.stat_sinks.graphite_statsd": "//source/extensions/stat_sinks/graphite_statsd:config", - "envoy.stat_sinks.hystrix": "//source/extensions/stat_sinks/hystrix:config", - "envoy.stat_sinks.metrics_service": "//source/extensions/stat_sinks/metrics_service:config", - "envoy.stat_sinks.statsd": "//source/extensions/stat_sinks/statsd:config", - "envoy.stat_sinks.wasm": "//source/extensions/stat_sinks/wasm:config", + + "envoy.stat_sinks.dog_statsd": "//source/extensions/stat_sinks/dog_statsd:config", + "envoy.stat_sinks.graphite_statsd": "//source/extensions/stat_sinks/graphite_statsd:config", + "envoy.stat_sinks.hystrix": "//source/extensions/stat_sinks/hystrix:config", + "envoy.stat_sinks.metrics_service": "//source/extensions/stat_sinks/metrics_service:config", + "envoy.stat_sinks.statsd": "//source/extensions/stat_sinks/statsd:config", + "envoy.stat_sinks.wasm": "//source/extensions/stat_sinks/wasm:config", # # Thrift filters # - "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", - "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", - "envoy.filters.thrift.rate_limit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", + + "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", + "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", + "envoy.filters.thrift.rate_limit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", # # Tracers # - "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", - "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", - "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", - "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", - "envoy.tracers.xray": "//source/extensions/tracers/xray:config", - "envoy.tracers.skywalking": "//source/extensions/tracers/skywalking:config", - "envoy.tracers.opentelemetry": "//source/extensions/tracers/opentelemetry:config", + + "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", + "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", + "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", + "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", + "envoy.tracers.xray": "//source/extensions/tracers/xray:config", + "envoy.tracers.skywalking": "//source/extensions/tracers/skywalking:config", + "envoy.tracers.opentelemetry": "//source/extensions/tracers/opentelemetry:config", # # Transport sockets # - "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", - "envoy.transport_sockets.http_11_proxy": "//source/extensions/transport_sockets/http_11_proxy:upstream_config", - "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", - "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", - "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", - "envoy.transport_sockets.starttls": "//source/extensions/transport_sockets/starttls:config", - "envoy.transport_sockets.tcp_stats": "//source/extensions/transport_sockets/tcp_stats:config", - "envoy.transport_sockets.internal_upstream": "//source/extensions/transport_sockets/internal_upstream:config", + + "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", + "envoy.transport_sockets.http_11_proxy": "//source/extensions/transport_sockets/http_11_proxy:upstream_config", + "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", + "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", + "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", + "envoy.transport_sockets.starttls": "//source/extensions/transport_sockets/starttls:config", + "envoy.transport_sockets.tcp_stats": "//source/extensions/transport_sockets/tcp_stats:config", + "envoy.transport_sockets.internal_upstream": "//source/extensions/transport_sockets/internal_upstream:config", # # Retry host predicates # - "envoy.retry_host_predicates.previous_hosts": "//source/extensions/retry/host/previous_hosts:config", - "envoy.retry_host_predicates.omit_canary_hosts": "//source/extensions/retry/host/omit_canary_hosts:config", - "envoy.retry_host_predicates.omit_host_metadata": "//source/extensions/retry/host/omit_host_metadata:config", + + "envoy.retry_host_predicates.previous_hosts": "//source/extensions/retry/host/previous_hosts:config", + "envoy.retry_host_predicates.omit_canary_hosts": "//source/extensions/retry/host/omit_canary_hosts:config", + "envoy.retry_host_predicates.omit_host_metadata": "//source/extensions/retry/host/omit_host_metadata:config", # # Retry priorities # - "envoy.retry_priorities.previous_priorities": "//source/extensions/retry/priority/previous_priorities:config", + + "envoy.retry_priorities.previous_priorities": "//source/extensions/retry/priority/previous_priorities:config", # # CacheFilter plugins # - "envoy.extensions.http.cache.simple": "//source/extensions/filters/http/cache/simple_http_cache:config", + "envoy.extensions.http.cache.simple": "//source/extensions/filters/http/cache/simple_http_cache:config", # # Internal redirect predicates # + "envoy.internal_redirect_predicates.allow_listed_routes": "//source/extensions/internal_redirect/allow_listed_routes:config", - "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", - "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", + "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", + "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", # # Http Upstreams (excepting envoy.upstreams.http.generic which is hard-coded into the build so not registered here) # - "envoy.upstreams.http.http": "//source/extensions/upstreams/http/http:config", - "envoy.upstreams.http.tcp": "//source/extensions/upstreams/http/tcp:config", + + "envoy.upstreams.http.http": "//source/extensions/upstreams/http/http:config", + "envoy.upstreams.http.tcp": "//source/extensions/upstreams/http/tcp:config", # # Watchdog actions # - "envoy.watchdog.profile_action": "//source/extensions/watchdog/profile_action:config", + + "envoy.watchdog.profile_action": "//source/extensions/watchdog/profile_action:config", # # WebAssembly runtimes # - "envoy.wasm.runtime.null": "//source/extensions/wasm_runtime/null:config", - "envoy.wasm.runtime.v8": "//source/extensions/wasm_runtime/v8:config", - "envoy.wasm.runtime.wamr": "//source/extensions/wasm_runtime/wamr:config", - "envoy.wasm.runtime.wavm": "//source/extensions/wasm_runtime/wavm:config", - "envoy.wasm.runtime.wasmtime": "//source/extensions/wasm_runtime/wasmtime:config", + + "envoy.wasm.runtime.null": "//source/extensions/wasm_runtime/null:config", + "envoy.wasm.runtime.v8": "//source/extensions/wasm_runtime/v8:config", + "envoy.wasm.runtime.wamr": "//source/extensions/wasm_runtime/wamr:config", + "envoy.wasm.runtime.wavm": "//source/extensions/wasm_runtime/wavm:config", + "envoy.wasm.runtime.wasmtime": "//source/extensions/wasm_runtime/wasmtime:config", # # Rate limit descriptors # - "envoy.rate_limit_descriptors.expr": "//source/extensions/rate_limit_descriptors/expr:config", + + "envoy.rate_limit_descriptors.expr": "//source/extensions/rate_limit_descriptors/expr:config", # # IO socket # - "envoy.io_socket.user_space": "//source/extensions/io_socket/user_space:config", - "envoy.bootstrap.internal_listener": "//source/extensions/bootstrap/internal_listener:config", + + "envoy.io_socket.user_space": "//source/extensions/io_socket/user_space:config", + "envoy.bootstrap.internal_listener": "//source/extensions/bootstrap/internal_listener:config", # # TLS peer certification validators # - "envoy.tls.cert_validator.spiffe": "//source/extensions/transport_sockets/tls/cert_validator/spiffe:config", + + "envoy.tls.cert_validator.spiffe": "//source/extensions/transport_sockets/tls/cert_validator/spiffe:config", # # HTTP header formatters # - "envoy.http.stateful_header_formatters.preserve_case": "//source/extensions/http/header_formatters/preserve_case:config", + + "envoy.http.stateful_header_formatters.preserve_case": "//source/extensions/http/header_formatters/preserve_case:config", # # Original IP detection # - "envoy.http.original_ip_detection.custom_header": "//source/extensions/http/original_ip_detection/custom_header:config", - "envoy.http.original_ip_detection.xff": "//source/extensions/http/original_ip_detection/xff:config", + + "envoy.http.original_ip_detection.custom_header": "//source/extensions/http/original_ip_detection/custom_header:config", + "envoy.http.original_ip_detection.xff": "//source/extensions/http/original_ip_detection/xff:config", # # Stateful session # - "envoy.http.stateful_session.cookie": "//source/extensions/http/stateful_session/cookie:config", + + "envoy.http.stateful_session.cookie": "//source/extensions/http/stateful_session/cookie:config", # # QUIC extensions # - "envoy.quic.crypto_stream.server.quiche": "//source/extensions/quic/crypto_stream:envoy_quic_default_crypto_server_stream", - "envoy.quic.proof_source.filter_chain": "//source/extensions/quic/proof_source:envoy_quic_default_proof_source", + + "envoy.quic.crypto_stream.server.quiche": "//source/extensions/quic/crypto_stream:envoy_quic_default_crypto_server_stream", + "envoy.quic.proof_source.filter_chain": "//source/extensions/quic/proof_source:envoy_quic_default_proof_source", # # UDP packet writers # - "envoy.udp_packet_writer.default": "//source/extensions/udp_packet_writer/default:config", - "envoy.udp_packet_writer.gso": "//source/extensions/udp_packet_writer/gso:config", + "envoy.udp_packet_writer.default": "//source/extensions/udp_packet_writer/default:config", + "envoy.udp_packet_writer.gso": "//source/extensions/udp_packet_writer/gso:config", # # Formatter # - "envoy.formatter.metadata": "//source/extensions/formatter/metadata:config", - "envoy.formatter.req_without_query": "//source/extensions/formatter/req_without_query:config", + + "envoy.formatter.metadata": "//source/extensions/formatter/metadata:config", + "envoy.formatter.req_without_query": "//source/extensions/formatter/req_without_query:config", # # Key value store # - "envoy.key_value.file_based": "//source/extensions/key_value/file_based:config_lib", + + "envoy.key_value.file_based": "//source/extensions/key_value/file_based:config_lib", # # RBAC matchers # - "envoy.rbac.matchers.upstream_ip_port": "//source/extensions/filters/common/rbac/matchers:upstream_ip_port_lib", + + "envoy.rbac.matchers.upstream_ip_port": "//source/extensions/filters/common/rbac/matchers:upstream_ip_port_lib", # # DNS Resolver # # c-ares DNS resolver extension is recommended to be enabled to maintain the legacy DNS resolving behavior. - "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", + "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", # apple DNS resolver extension is only needed in MacOS build plus one want to use apple library for DNS resolving. - "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", + "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", # getaddrinfo DNS resolver extension can be used when the system resolver is desired (e.g., Android) - "envoy.network.dns_resolver.getaddrinfo": "//source/extensions/network/dns_resolver/getaddrinfo:config", + "envoy.network.dns_resolver.getaddrinfo": "//source/extensions/network/dns_resolver/getaddrinfo:config", # # Custom matchers # - "envoy.matching.custom_matchers.trie_matcher": "//source/extensions/common/matcher:trie_matcher_lib", - # - # Pattern Template - # - "envoy.path_match_policy.pattern_template_match_predicate": "//source/extensions/pattern_template/match:config", - "envoy.path_rewrite_policy.pattern_template_rewrite_predicate": "//source/extensions/pattern_template/rewrite:config", + "envoy.matching.custom_matchers.trie_matcher": "//source/extensions/common/matcher:trie_matcher_lib", # # Header Validators # - "envoy.http.header_validators.envoy_default": "//source/extensions/http/header_validators/envoy_default:config", + + "envoy.http.header_validators.envoy_default": "//source/extensions/http/header_validators/envoy_default:config", # # Early Data option # - "envoy.route.early_data_policy.default": "//source/extensions/early_data:default_early_data_policy_lib", + + "envoy.route.early_data_policy.default": "//source/extensions/early_data:default_early_data_policy_lib", } # These can be changed to ["//visibility:public"], for downstream builds which diff --git a/source/extensions/pattern_template/match/BUILD b/source/extensions/path/match/BUILD similarity index 100% rename from source/extensions/pattern_template/match/BUILD rename to source/extensions/path/match/BUILD diff --git a/source/extensions/pattern_template/match/config.cc b/source/extensions/path/match/config.cc similarity index 100% rename from source/extensions/pattern_template/match/config.cc rename to source/extensions/path/match/config.cc diff --git a/source/extensions/pattern_template/match/config.h b/source/extensions/path/match/config.h similarity index 100% rename from source/extensions/pattern_template/match/config.h rename to source/extensions/path/match/config.h diff --git a/source/extensions/pattern_template/match/pattern_template_match.cc b/source/extensions/path/match/pattern_template_match.cc similarity index 100% rename from source/extensions/pattern_template/match/pattern_template_match.cc rename to source/extensions/path/match/pattern_template_match.cc diff --git a/source/extensions/pattern_template/match/pattern_template_match.h b/source/extensions/path/match/pattern_template_match.h similarity index 100% rename from source/extensions/pattern_template/match/pattern_template_match.h rename to source/extensions/path/match/pattern_template_match.h diff --git a/source/extensions/pattern_template/BUILD b/source/extensions/path/pattern_template/BUILD similarity index 100% rename from source/extensions/pattern_template/BUILD rename to source/extensions/path/pattern_template/BUILD diff --git a/source/extensions/pattern_template/pattern_template.cc b/source/extensions/path/pattern_template/pattern_template.cc similarity index 100% rename from source/extensions/pattern_template/pattern_template.cc rename to source/extensions/path/pattern_template/pattern_template.cc diff --git a/source/extensions/pattern_template/pattern_template.h b/source/extensions/path/pattern_template/pattern_template.h similarity index 100% rename from source/extensions/pattern_template/pattern_template.h rename to source/extensions/path/pattern_template/pattern_template.h diff --git a/source/extensions/pattern_template/pattern_template_internal.cc b/source/extensions/path/pattern_template/pattern_template_internal.cc similarity index 100% rename from source/extensions/pattern_template/pattern_template_internal.cc rename to source/extensions/path/pattern_template/pattern_template_internal.cc diff --git a/source/extensions/pattern_template/pattern_template_internal.h b/source/extensions/path/pattern_template/pattern_template_internal.h similarity index 100% rename from source/extensions/pattern_template/pattern_template_internal.h rename to source/extensions/path/pattern_template/pattern_template_internal.h diff --git a/source/extensions/pattern_template/pattern_template_internal_test.cc b/source/extensions/path/pattern_template/pattern_template_internal_test.cc similarity index 100% rename from source/extensions/pattern_template/pattern_template_internal_test.cc rename to source/extensions/path/pattern_template/pattern_template_internal_test.cc diff --git a/source/extensions/pattern_template/pattern_template_test.cc b/source/extensions/path/pattern_template/pattern_template_test.cc similarity index 100% rename from source/extensions/pattern_template/pattern_template_test.cc rename to source/extensions/path/pattern_template/pattern_template_test.cc diff --git a/source/extensions/pattern_template/proto/BUILD b/source/extensions/path/pattern_template/proto/BUILD similarity index 100% rename from source/extensions/pattern_template/proto/BUILD rename to source/extensions/path/pattern_template/proto/BUILD diff --git a/source/extensions/pattern_template/proto/pattern_template_rewrite_segments.proto b/source/extensions/path/pattern_template/proto/pattern_template_rewrite_segments.proto similarity index 100% rename from source/extensions/pattern_template/proto/pattern_template_rewrite_segments.proto rename to source/extensions/path/pattern_template/proto/pattern_template_rewrite_segments.proto diff --git a/source/extensions/pattern_template/rewrite/BUILD b/source/extensions/path/rewrite/BUILD similarity index 100% rename from source/extensions/pattern_template/rewrite/BUILD rename to source/extensions/path/rewrite/BUILD diff --git a/source/extensions/pattern_template/rewrite/config.cc b/source/extensions/path/rewrite/config.cc similarity index 100% rename from source/extensions/pattern_template/rewrite/config.cc rename to source/extensions/path/rewrite/config.cc diff --git a/source/extensions/pattern_template/rewrite/config.h b/source/extensions/path/rewrite/config.h similarity index 100% rename from source/extensions/pattern_template/rewrite/config.h rename to source/extensions/path/rewrite/config.h diff --git a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template_rewrite.cc similarity index 100% rename from source/extensions/pattern_template/rewrite/pattern_template_rewrite.cc rename to source/extensions/path/rewrite/pattern_template_rewrite.cc diff --git a/source/extensions/pattern_template/rewrite/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template_rewrite.h similarity index 100% rename from source/extensions/pattern_template/rewrite/pattern_template_rewrite.h rename to source/extensions/path/rewrite/pattern_template_rewrite.h From a7280383b5452b11c282fa44eca9a085d6a756d2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 25 Jul 2022 19:14:00 +0000 Subject: [PATCH 067/238] Work to merge Signed-off-by: silverstar195 --- source/extensions/path/match/{ => pattern_template}/BUILD | 0 source/extensions/path/match/{ => pattern_template}/config.cc | 0 source/extensions/path/match/{ => pattern_template}/config.h | 0 .../path/match/{ => pattern_template}/pattern_template_match.cc | 0 .../path/match/{ => pattern_template}/pattern_template_match.h | 0 .../path/{pattern_template => pattern_template_lib}/BUILD | 0 .../pattern_template.cc | 0 .../{pattern_template => pattern_template_lib}/pattern_template.h | 0 .../pattern_template_internal.cc | 0 .../pattern_template_internal.h | 0 .../pattern_template_internal_test.cc | 0 .../pattern_template_test.cc | 0 .../path/{pattern_template => pattern_template_lib}/proto/BUILD | 0 source/extensions/path/rewrite/{ => pattern_template}/BUILD | 0 source/extensions/path/rewrite/{ => pattern_template}/config.cc | 0 source/extensions/path/rewrite/{ => pattern_template}/config.h | 0 .../rewrite/{ => pattern_template}/pattern_template_rewrite.cc | 0 .../rewrite/{ => pattern_template}/pattern_template_rewrite.h | 0 18 files changed, 0 insertions(+), 0 deletions(-) rename source/extensions/path/match/{ => pattern_template}/BUILD (100%) rename source/extensions/path/match/{ => pattern_template}/config.cc (100%) rename source/extensions/path/match/{ => pattern_template}/config.h (100%) rename source/extensions/path/match/{ => pattern_template}/pattern_template_match.cc (100%) rename source/extensions/path/match/{ => pattern_template}/pattern_template_match.h (100%) rename source/extensions/path/{pattern_template => pattern_template_lib}/BUILD (100%) rename source/extensions/path/{pattern_template => pattern_template_lib}/pattern_template.cc (100%) rename source/extensions/path/{pattern_template => pattern_template_lib}/pattern_template.h (100%) rename source/extensions/path/{pattern_template => pattern_template_lib}/pattern_template_internal.cc (100%) rename source/extensions/path/{pattern_template => pattern_template_lib}/pattern_template_internal.h (100%) rename source/extensions/path/{pattern_template => pattern_template_lib}/pattern_template_internal_test.cc (100%) rename source/extensions/path/{pattern_template => pattern_template_lib}/pattern_template_test.cc (100%) rename source/extensions/path/{pattern_template => pattern_template_lib}/proto/BUILD (100%) rename source/extensions/path/rewrite/{ => pattern_template}/BUILD (100%) rename source/extensions/path/rewrite/{ => pattern_template}/config.cc (100%) rename source/extensions/path/rewrite/{ => pattern_template}/config.h (100%) rename source/extensions/path/rewrite/{ => pattern_template}/pattern_template_rewrite.cc (100%) rename source/extensions/path/rewrite/{ => pattern_template}/pattern_template_rewrite.h (100%) diff --git a/source/extensions/path/match/BUILD b/source/extensions/path/match/pattern_template/BUILD similarity index 100% rename from source/extensions/path/match/BUILD rename to source/extensions/path/match/pattern_template/BUILD diff --git a/source/extensions/path/match/config.cc b/source/extensions/path/match/pattern_template/config.cc similarity index 100% rename from source/extensions/path/match/config.cc rename to source/extensions/path/match/pattern_template/config.cc diff --git a/source/extensions/path/match/config.h b/source/extensions/path/match/pattern_template/config.h similarity index 100% rename from source/extensions/path/match/config.h rename to source/extensions/path/match/pattern_template/config.h diff --git a/source/extensions/path/match/pattern_template_match.cc b/source/extensions/path/match/pattern_template/pattern_template_match.cc similarity index 100% rename from source/extensions/path/match/pattern_template_match.cc rename to source/extensions/path/match/pattern_template/pattern_template_match.cc diff --git a/source/extensions/path/match/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h similarity index 100% rename from source/extensions/path/match/pattern_template_match.h rename to source/extensions/path/match/pattern_template/pattern_template_match.h diff --git a/source/extensions/path/pattern_template/BUILD b/source/extensions/path/pattern_template_lib/BUILD similarity index 100% rename from source/extensions/path/pattern_template/BUILD rename to source/extensions/path/pattern_template_lib/BUILD diff --git a/source/extensions/path/pattern_template/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc similarity index 100% rename from source/extensions/path/pattern_template/pattern_template.cc rename to source/extensions/path/pattern_template_lib/pattern_template.cc diff --git a/source/extensions/path/pattern_template/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h similarity index 100% rename from source/extensions/path/pattern_template/pattern_template.h rename to source/extensions/path/pattern_template_lib/pattern_template.h diff --git a/source/extensions/path/pattern_template/pattern_template_internal.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc similarity index 100% rename from source/extensions/path/pattern_template/pattern_template_internal.cc rename to source/extensions/path/pattern_template_lib/pattern_template_internal.cc diff --git a/source/extensions/path/pattern_template/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h similarity index 100% rename from source/extensions/path/pattern_template/pattern_template_internal.h rename to source/extensions/path/pattern_template_lib/pattern_template_internal.h diff --git a/source/extensions/path/pattern_template/pattern_template_internal_test.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc similarity index 100% rename from source/extensions/path/pattern_template/pattern_template_internal_test.cc rename to source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc diff --git a/source/extensions/path/pattern_template/pattern_template_test.cc b/source/extensions/path/pattern_template_lib/pattern_template_test.cc similarity index 100% rename from source/extensions/path/pattern_template/pattern_template_test.cc rename to source/extensions/path/pattern_template_lib/pattern_template_test.cc diff --git a/source/extensions/path/pattern_template/proto/BUILD b/source/extensions/path/pattern_template_lib/proto/BUILD similarity index 100% rename from source/extensions/path/pattern_template/proto/BUILD rename to source/extensions/path/pattern_template_lib/proto/BUILD diff --git a/source/extensions/path/rewrite/BUILD b/source/extensions/path/rewrite/pattern_template/BUILD similarity index 100% rename from source/extensions/path/rewrite/BUILD rename to source/extensions/path/rewrite/pattern_template/BUILD diff --git a/source/extensions/path/rewrite/config.cc b/source/extensions/path/rewrite/pattern_template/config.cc similarity index 100% rename from source/extensions/path/rewrite/config.cc rename to source/extensions/path/rewrite/pattern_template/config.cc diff --git a/source/extensions/path/rewrite/config.h b/source/extensions/path/rewrite/pattern_template/config.h similarity index 100% rename from source/extensions/path/rewrite/config.h rename to source/extensions/path/rewrite/pattern_template/config.h diff --git a/source/extensions/path/rewrite/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc similarity index 100% rename from source/extensions/path/rewrite/pattern_template_rewrite.cc rename to source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc diff --git a/source/extensions/path/rewrite/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h similarity index 100% rename from source/extensions/path/rewrite/pattern_template_rewrite.h rename to source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h From 6d1611d01d259334c92468b746df1878bee66832 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 25 Jul 2022 19:26:45 +0000 Subject: [PATCH 068/238] Work to merge Signed-off-by: silverstar195 --- source/extensions/url_template/BUILD | 89 ---- source/extensions/url_template/config.cc | 11 - source/extensions/url_template/config.h | 30 -- .../url_template/url_template_matching.cc | 225 --------- .../url_template/url_template_matching.h | 91 ---- .../url_template_matching_internal.cc | 383 -------------- .../url_template_matching_internal.h | 95 ---- .../url_template_matching_internal_test.cc | 468 ------------------ .../url_template_matching_test.cc | 335 ------------- 9 files changed, 1727 deletions(-) delete mode 100644 source/extensions/url_template/BUILD delete mode 100644 source/extensions/url_template/config.cc delete mode 100644 source/extensions/url_template/config.h delete mode 100644 source/extensions/url_template/url_template_matching.cc delete mode 100644 source/extensions/url_template/url_template_matching.h delete mode 100644 source/extensions/url_template/url_template_matching_internal.cc delete mode 100644 source/extensions/url_template/url_template_matching_internal.h delete mode 100644 source/extensions/url_template/url_template_matching_internal_test.cc delete mode 100644 source/extensions/url_template/url_template_matching_test.cc diff --git a/source/extensions/url_template/BUILD b/source/extensions/url_template/BUILD deleted file mode 100644 index d1f794f3a38dc..0000000000000 --- a/source/extensions/url_template/BUILD +++ /dev/null @@ -1,89 +0,0 @@ -load( - "//bazel:envoy_build_system.bzl", - "envoy_cc_extension", - "envoy_cc_library", - "envoy_cc_test", - "envoy_extension_package", -) - -licenses(["notice"]) # Apache 2 - -# Wildcard & Pattern Matching - -envoy_extension_package() - -envoy_cc_library( - name = "url_template_matching", - srcs = ["url_template_matching.cc"], - hdrs = ["url_template_matching.h"], - visibility = [ - "//source/common/router:__subpackages__", - "//source/extensions/url_template:__subpackages__", - "//test/extensions/url_template:__subpackages__", - ], - deps = [ - ":url_template_matching_internal_cc", - "//envoy/router:router_url_template_interface", - "//source/common/http:path_utility_lib", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - "@com_googlesource_code_re2//:re2", - "@envoy_api//envoy/extensions/url_template/v3:pkg_cc_proto", - ], -) - -envoy_cc_library( - name = "url_template_matching_internal_cc", - srcs = ["url_template_matching_internal.cc"], - hdrs = ["url_template_matching_internal.h"], - deps = [ - "@com_google_absl//absl/container:flat_hash_set", - "@com_google_absl//absl/flags:flag", - "@com_google_absl//absl/functional:function_ref", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/types:optional", - "@com_google_absl//absl/types:variant", - "@com_googlesource_code_re2//:re2", - ], -) - -envoy_cc_test( - name = "url_template_matching_test", - srcs = ["url_template_matching_test.cc"], - deps = [ - ":url_template_matching", - "//test/test_common:status_utility_lib", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - ], -) - -envoy_cc_test( - name = "url_template_matching_internal_test", - srcs = ["url_template_matching_internal_test.cc"], - deps = [ - ":url_template_matching_internal_cc", - "//test/test_common:status_utility_lib", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - "@com_googlesource_code_re2//:re2", - ], -) - -envoy_cc_extension( - name = "config", - srcs = ["config.cc"], - hdrs = ["config.h"], - visibility = ["//visibility:public"], - deps = [ - ":url_template_matching", - "//envoy/registry", - "//envoy/router:router_url_template_interface", - "@envoy_api//envoy/extensions/url_template/v3:pkg_cc_proto", - ], -) diff --git a/source/extensions/url_template/config.cc b/source/extensions/url_template/config.cc deleted file mode 100644 index 4dbb5a38be1bb..0000000000000 --- a/source/extensions/url_template/config.cc +++ /dev/null @@ -1,11 +0,0 @@ -#include "source/extensions/url_template/config.h" - -#include "envoy/registry/registry.h" -#include "envoy/router/url_template.h" - -namespace Envoy { -namespace matching { - -REGISTER_FACTORY(PatternTemplatePredicateFactory, Router::PatternTemplatePredicateFactory); -} // namespace matching -} // namespace Envoy \ No newline at end of file diff --git a/source/extensions/url_template/config.h b/source/extensions/url_template/config.h deleted file mode 100644 index dfab51c93d536..0000000000000 --- a/source/extensions/url_template/config.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include "envoy/extensions/url_template/v3/route_url_rewrite_pattern.pb.h" -#include "envoy/router/url_template.h" - -#include "source/extensions/url_template/url_template_matching.h" - -namespace Envoy { -namespace matching { - -class PatternTemplatePredicateFactory : public Router::PatternTemplatePredicateFactory { -public: - Router::PatternTemplatePredicateSharedPtr - createUrlTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) override { - return std::make_shared(url_pattern, url_rewrite_pattern); - } - - ProtobufTypes::MessagePtr createEmptyConfigProto() override { - // may not be used to investigate - return std::make_unique(); - } - - std::string name() const override { - return "envoy.url_template.pattern_template_predicates"; - } - -}; - -} // namespace matching -} // namespace Envoy \ No newline at end of file diff --git a/source/extensions/url_template/url_template_matching.cc b/source/extensions/url_template/url_template_matching.cc deleted file mode 100644 index 292124532a4f9..0000000000000 --- a/source/extensions/url_template/url_template_matching.cc +++ /dev/null @@ -1,225 +0,0 @@ -#include "source/extensions/url_template/url_template_matching.h" - -#include -#include -#include -#include - -#include "envoy/extensions/url_template/v3/route_url_rewrite_pattern.pb.h" - -#include "source/extensions/url_template/url_template_matching_internal.h" -#include "source/common/http/path_utility.h" - -#include "absl/status/statusor.h" -#include "absl/strings/str_split.h" -#include "absl/strings/string_view.h" -#include "re2/re2.h" - -namespace Envoy { -namespace matching { - -using matching::url_template_matching_internal::ParsedUrlPattern; - -#ifndef SWIG -// Silence warnings about missing initializers for members of LazyRE2. -#pragma GCC diagnostic ignored "-Wmissing-field-initializers" -#endif - -inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.data(), text.size()}; } - -bool PatternTemplatePredicate::match(absl::string_view pattern) const { - return RE2::FullMatch(ToStringPiece(Http::PathUtil::removeQueryAndFragment(pattern)), matching_pattern_regex_); -} - -absl::StatusOr -PatternTemplatePredicate::convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { - - absl::StatusOr status = - url_template_matching_internal::parseURLPatternSyntax(url_pattern); - if (!status.ok()) { - return status.status(); - } - struct ParsedUrlPattern pattern = *std::move(status); - return url_template_matching_internal::toRegexPattern(pattern); -} - -absl::StatusOr> -PatternTemplatePredicate::parseRewritePatternHelper(absl::string_view pattern) { - std::vector result; - - // Don't allow contiguous '/' patterns. - static const LazyRE2 invalid_regex = {"^.*//.*$"}; - if (RE2::FullMatch(ToStringPiece(pattern), *invalid_regex)) { - return absl::InvalidArgumentError("Invalid rewrite literal pattern"); - } - - // The pattern should start with a '/' and thus the first segment should - // always be a literal. - if (pattern.empty() || pattern[0] != '/') { - return absl::InvalidArgumentError("Invalid rewrite variable placement"); - } - while (!pattern.empty()) { - std::vector segments1 = absl::StrSplit(pattern, absl::MaxSplits('{', 1)); - if (!segments1[0].empty()) { - if (!url_template_matching_internal::isValidRewriteLiteral(segments1[0])) { - return absl::InvalidArgumentError("Invalid rewrite literal pattern"); - } - result.emplace_back(segments1[0], RewriteStringKind::kLiteral); - } - - if (segments1.size() < 2) { - // No more variable replacement, done. - break; - } - - std::vector segments2 = - absl::StrSplit(segments1[1], absl::MaxSplits('}', 1)); - if (segments2.size() < 2) { - return absl::InvalidArgumentError("Unmatched variable bracket"); - } - pattern = segments2[1]; - - if (!url_template_matching_internal::isValidIndent(segments2[0])) { - return absl::InvalidArgumentError("Invalid variable name"); - } - result.emplace_back(segments2[0], RewriteStringKind::kVariable); - } - return result; -} - -absl::StatusOr -PatternTemplatePredicate::parseRewritePattern(absl::string_view pattern, - absl::string_view capture_regex) { - envoy::extensions::url_template::v3::RouteUrlRewritePattern parsed_pattern; - RE2 regex = RE2(ToStringPiece(capture_regex)); - if (!regex.ok()) { - return absl::InternalError(regex.error()); - } - - absl::StatusOr> status = parseRewritePatternHelper(pattern); - if (!status.ok()) { - return status.status(); - } - std::vector processed_pattern = *std::move(status); - - const std::map& capture_index_map = regex.NamedCapturingGroups(); - - for (const auto& [str, kind] : processed_pattern) { - switch (kind) { - case RewriteStringKind::kLiteral: - parsed_pattern.add_segments()->set_literal(std::string(str)); - break; - case RewriteStringKind::kVariable: - auto it = capture_index_map.find(std::string(str)); - if (it == capture_index_map.end()) { - return absl::InvalidArgumentError("Nonexisting variable name"); - } - parsed_pattern.add_segments()->set_var_index(it->second); - break; - } - } - - return parsed_pattern; -} - -absl::StatusOr PatternTemplatePredicate::rewriteURLTemplatePattern( - absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::url_template::v3::RouteUrlRewritePattern& rewrite_pattern) const { - RE2 regex = RE2(ToStringPiece(capture_regex)); - if (!regex.ok()) { - return absl::InternalError(regex.error()); - } - // First capture is the whole matched regex pattern. - int capture_num = regex.NumberOfCapturingGroups() + 1; - std::vector captures(capture_num); - if (!regex.Match(ToStringPiece(url), /*startpos=*/0, /*endpos=*/url.size(), RE2::ANCHOR_BOTH, - captures.data(), captures.size())) { - return absl::InvalidArgumentError("Pattern not match"); - } - - std::string rewritten_url; - - for (const envoy::extensions::url_template::v3::RouteUrlRewritePattern::RewriteSegment& segment : - rewrite_pattern.segments()) { - if (segment.has_literal()) { - absl::StrAppend(&rewritten_url, segment.literal()); - } else if (segment.has_var_index()) { - if (segment.var_index() < 1 || segment.var_index() >= capture_num) { - return absl::InvalidArgumentError("Invalid variable index"); - } - absl::StrAppend(&rewritten_url, absl::string_view(captures[segment.var_index()].as_string())); - } - } - - return rewritten_url; -} - -absl::Status PatternTemplatePredicate::isValidPathTemplateMatchPattern(const std::string& path_template_match) { - return convertURLPatternSyntaxToRegex(path_template_match).status(); -} - -absl::Status PatternTemplatePredicate::isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { - return parseRewritePatternHelper(path_template_rewrite).status(); -} - -absl::Status PatternTemplatePredicate::isValidSharedVariableSet(const std::string& path_template_rewrite, - std::string& capture_regex) { - return parseRewritePattern(path_template_rewrite, capture_regex).status(); -} - -absl::StatusOr PatternTemplatePredicate::rewritePattern(absl::string_view current_pattern, absl::string_view matched_path) const { - absl::StatusOr regex_pattern = convertURLPatternSyntaxToRegex(matched_path); - if (!regex_pattern.ok()) { - return absl::InvalidArgumentError("Unable to parse url pattern regex"); - } - std::string regex_pattern_str = *std::move(regex_pattern); - - absl::StatusOr rewrite_pattern = - parseRewritePattern(url_rewrite_pattern_, regex_pattern_str); - - if (!rewrite_pattern.ok()) { - return absl::InvalidArgumentError("Unable to parse url rewrite pattern"); - } - - envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_pattern_proto = - *std::move(rewrite_pattern); - - absl::StatusOr new_path = - rewriteURLTemplatePattern(current_pattern, regex_pattern_str, rewrite_pattern_proto); - - if (!new_path.ok()) { - return absl::InvalidArgumentError("Unable rewrite url to new URL"); - } - - return *std::move(new_path); -} - -absl::Status PatternTemplatePredicate::is_valid_match_pattern(std::string match_pattern) { - return isValidPathTemplateMatchPattern(match_pattern); -}; - -absl::Status PatternTemplatePredicate::is_valid_rewrite_pattern(std::string match_pattern, - std::string rewrite_pattern) { - - if (!PatternTemplatePredicate::isValidPathTemplateRewritePattern(rewrite_pattern).ok()) { - return absl::InvalidArgumentError(fmt::format("path_template_rewrite {} is invalid", match_pattern)); - } - - absl::StatusOr converted_pattern = convertURLPatternSyntaxToRegex(match_pattern); - if (!converted_pattern.ok()) { - return absl::InvalidArgumentError(fmt::format("path_template {} is invalid", match_pattern)); - } - - std::string path_template_match_regex = *std::move(converted_pattern); - if (path_template_match_regex.empty() || - !isValidSharedVariableSet(rewrite_pattern, path_template_match_regex).ok()) { - return absl::InvalidArgumentError( - fmt::format("mismatch between path_template {} and path_template_rewrite {}", match_pattern, - rewrite_pattern)); - } - - return absl::OkStatus(); -}; - -} // namespace matching -} // namespace Envoy diff --git a/source/extensions/url_template/url_template_matching.h b/source/extensions/url_template/url_template_matching.h deleted file mode 100644 index db0f2a45b3eb4..0000000000000 --- a/source/extensions/url_template/url_template_matching.h +++ /dev/null @@ -1,91 +0,0 @@ -#ifndef SOURCE_EXTENSIONS_URL_TEMPLATE_URL_TEMPLATE_MATCHING_H -#define SOURCE_EXTENSIONS_URL_TEMPLATE_URL_TEMPLATE_MATCHING_H - -#include - -#include "envoy/extensions/url_template/v3/route_url_rewrite_pattern.pb.h" - -#include "source/extensions/url_template/url_template_matching_internal.h" - - -#include "absl/status/statusor.h" -#include "absl/strings/string_view.h" -#include "envoy/router/url_template.h" - -namespace Envoy { -namespace matching { - -enum class RewriteStringKind { kVariable, kLiteral }; - -struct RewritePatternSegment { - RewritePatternSegment(absl::string_view str, RewriteStringKind kind) : str(str), kind(kind) {} - absl::string_view str; - RewriteStringKind kind; -}; - - -class PatternTemplatePredicate : public Router::PatternTemplatePredicate { -public: - explicit PatternTemplatePredicate(std::string url_pattern, std::string url_rewrite_pattern) - : Router::PatternTemplatePredicate(url_pattern, url_rewrite_pattern), - matching_pattern_regex_(RE2(PatternTemplatePredicate::convertURLPatternSyntaxToRegex(url_pattern).value())) {} - PatternTemplatePredicate() = default; - - absl::string_view name() const override { return "envoy.url_template.pattern_template_predicates"; } - std::string category() const override { return "envoy.url_template"; } - - // Returns if the regex pattern matches the given regex from constructor. - bool match(absl::string_view pattern) const override; - - absl::StatusOr rewritePattern(absl::string_view current_pattern, - absl::string_view matched_path) const override; - - static absl::Status is_valid_match_pattern(std::string match_pattern); - - static absl::Status is_valid_rewrite_pattern(std::string match_pattern, std::string rewrite_pattern); - -private: - - // Returns the regex pattern that is equivalent to the given url_pattern. - // Used in the config pipeline to translate user given url pattern to - // the safe regex Envoy can understand. Strips away any variable captures. - static absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); - - // Helper function that parses the pattern and breaks it down to either - // literals or variable names. To be used by ParseRewritePattern(). - // Exposed here so that the validator for the rewrite pattern can also - // use it. - static absl::StatusOr> parseRewritePatternHelper(absl::string_view pattern); - - // Returns the parsed Url rewrite pattern to be used by - // RewriteURLTemplatePattern() |capture_regex| should - // be the regex generated by ConvertURLPatternSyntaxToRegex(). - static absl::StatusOr - parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); - - // Returns the rewritten URL path based on the given parsed rewrite pattern. - // Used for template-based URL rewrite. - absl::StatusOr rewriteURLTemplatePattern( - absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::url_template::v3::RouteUrlRewritePattern& rewrite_pattern) const; - - // Returns if provided template match pattern is valid - static absl::Status isValidPathTemplateMatchPattern(const std::string& path_template_match); - - // Returns if provided rewrite pattern is valid - static absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); - - // Returns if path_template and rewrite_template have valid variables - static absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, - std::string& capture_regex); - -private: - // move into library - RE2 matching_pattern_regex_{nullptr}; - -}; - -} // namespace matching -} // namespace Envoy - -#endif // SOURCE_EXTENSIONS_URL_TEMPLATE_URL_TEMPLATE_MATCHING_H diff --git a/source/extensions/url_template/url_template_matching_internal.cc b/source/extensions/url_template/url_template_matching_internal.cc deleted file mode 100644 index 62b4999071da3..0000000000000 --- a/source/extensions/url_template/url_template_matching_internal.cc +++ /dev/null @@ -1,383 +0,0 @@ -#include "source/extensions/url_template/url_template_matching_internal.h" - -#include -#include -#include -#include -#include - -#include "absl/container/flat_hash_set.h" -#include "absl/flags/flag.h" -#include "absl/functional/function_ref.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/match.h" -#include "absl/strings/str_cat.h" -#include "absl/strings/str_join.h" -#include "absl/strings/str_replace.h" -#include "absl/strings/str_split.h" -#include "absl/strings/string_view.h" -#include "absl/types/variant.h" -#include "re2/re2.h" - -namespace Envoy { -namespace matching { - -namespace url_template_matching_internal { - -namespace { - -#ifndef SWIG -// Silence warnings about missing initializers for members of LazyRE2. -#pragma GCC diagnostic ignored "-Wmissing-field-initializers" -#endif - -unsigned long pattern_matching_max_variables_per_url = 5; -unsigned long pattern_matching_max_variable_name_len = 16; -unsigned long pattern_matching_min_variable_name_len = 1; - -// Valid pchar from https://datatracker.ietf.org/doc/html/rfc3986#appendix-A -constexpr absl::string_view kLiteral = "a-zA-Z0-9-._~" // Unreserved - "%" // pct-encoded - "!$&'()+,;" // sub-delims excluding *= - ":@"; - -// Default operator used for the variable when none specified. -constexpr Operator kDefaultVariableOperator = Operator::kPathGlob; - -// Visitor for displaying debug info of a ParsedSegment/Variable.var_match. -struct ToStringVisitor { - template std::string operator()(const T& val) const; -}; - -// Formatter used to allow joining variants together with StrJoin. -struct ToStringFormatter { - template void operator()(std::string* out, const T& t) const { - absl::StrAppend(out, absl::visit(ToStringVisitor(), t)); - } -}; - -// Visitor for converting a ParsedSegment variant to the regex. -struct ToRegexPatternVisitor { - template std::string operator()(const T& val) const { return toRegexPattern(val); } -}; - -// Formatter used to allow joining variants together with StrJoin. -struct ToRegexPatternFormatter { - template void operator()(std::string* out, const T& t) const { - absl::StrAppend(out, absl::visit(ToRegexPatternVisitor(), t)); - } -}; - -std::string toString(const Literal val) { return std::string(val); } - -std::string toString(const Operator val) { - switch (val) { - case Operator::kPathGlob: - return "*"; - case Operator::kTextGlob: - return "**"; - } -} - -std::string toString(const Variable val) { - if (val.var_match.empty()) { - return absl::StrCat("{", val.var_name, "}"); - } - - return absl::StrCat("{", val.var_name, "=", - absl::StrJoin(val.var_match, "/", ToStringFormatter()), "}"); -} - -template std::string ToStringVisitor::operator()(const T& val) const { - return toString(val); -} - -template -absl::StatusOr AlsoUpdatePattern( - absl::FunctionRef>(absl::string_view)> consume_func, - absl::string_view* patt) { - - absl::StatusOr> status = consume_func(*patt); - if (!status.ok()) { - return status.status(); - } - ParsedResult result = *std::move(status); - - *patt = result.unconsumed_pattern; - return result.parsed_value; -} - -} // namespace - -std::string Variable::DebugString() const { return toString(*this); } - -std::string ParsedUrlPattern::DebugString() const { - return absl::StrCat("/", absl::StrJoin(parsed_segments, "/", ToStringFormatter()), - suffix.value_or("")); -} - -bool isValidLiteral(absl::string_view pattern) { - static const std::string* kValidLiteralRegex = - new std::string(absl::StrCat("^[", kLiteral, "]+$")); - static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; - return RE2::FullMatch(toStringPiece(pattern), *literal_regex); -} - -bool isValidRewriteLiteral(absl::string_view pattern) { - static const std::string* kValidLiteralRegex = - new std::string(absl::StrCat("^[", kLiteral, "/]+$")); - static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; - return RE2::FullMatch(toStringPiece(pattern), *literal_regex); -} - -bool isValidIndent(absl::string_view pattern) { - static const LazyRE2 ident_regex = {"^[a-zA-Z][a-zA-Z0-9_]*$"}; - return RE2::FullMatch(toStringPiece(pattern), *ident_regex); -} - -absl::StatusOr> consumeLiteral(absl::string_view pattern) { - absl::string_view lit = - std::vector(absl::StrSplit(pattern, absl::MaxSplits('/', 1)))[0]; - absl::string_view unconsumed_pattern = pattern.substr(lit.size()); - if (!isValidLiteral(lit)) { - return absl::InvalidArgumentError("Invalid literal"); - } - return ParsedResult(lit, unconsumed_pattern); -} - -absl::StatusOr> consumeOperator(absl::string_view pattern) { - if (absl::StartsWith(pattern, "**")) { - return ParsedResult(Operator::kTextGlob, pattern.substr(2)); - } - if (absl::StartsWith(pattern, "*")) { - return ParsedResult(Operator::kPathGlob, pattern.substr(1)); - } - return absl::InvalidArgumentError("Invalid Operator"); -} - -absl::StatusOr> consumeVariable(absl::string_view pattern) { - // Locate the variable pattern to parse. - if (pattern.size() < 2 || (pattern)[0] != '{') { - return absl::InvalidArgumentError("Invalid variable"); - } - std::vector parts = absl::StrSplit(pattern.substr(1), absl::MaxSplits('}', 1)); - if (parts.size() != 2) { - return absl::InvalidArgumentError("Unmatched variable bracket"); - } - absl::string_view unconsumed_pattern = parts[1]; - - // Parse the actual variable pattern, starting with the variable name. - std::vector var_parts = absl::StrSplit(parts[0], absl::MaxSplits('=', 1)); - if (!isValidIndent(var_parts[0])) { - return absl::InvalidArgumentError("Invalid variable name"); - } - Variable var = Variable(var_parts[0], {}); - - // Parse the variable match pattern (if any). - if (var_parts.size() < 2) { - return ParsedResult(var, unconsumed_pattern); - } - absl::string_view var_patt = var_parts[1]; - if (var_patt.empty()) { - return absl::InvalidArgumentError("Empty variable match"); - } - while (!var_patt.empty()) { - absl::variant var_match; - if (var_patt[0] == '*') { - - absl::StatusOr status = AlsoUpdatePattern(consumeOperator, &var_patt); - if (!status.ok()) { - return status.status(); - } - var_match = *std::move(status); - - } else { - - absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &var_patt); - if (!status.ok()) { - return status.status(); - } - var_match = *std::move(status); - } - var.var_match.push_back(var_match); - if (!var_patt.empty()) { - if (var_patt[0] != '/' || var_patt.size() == 1) { - return absl::InvalidArgumentError("Invalid variable match"); - } - var_patt = var_patt.substr(1); - } - } - - return ParsedResult(var, unconsumed_pattern); -} - -absl::StatusOr> -gatherCaptureNames(struct ParsedUrlPattern pattern) { - absl::flat_hash_set captured_variables; - - for (const ParsedSegment& segment : pattern.parsed_segments) { - if (!absl::holds_alternative(segment)) { - continue; - } - if (captured_variables.size() >= pattern_matching_max_variables_per_url) { - return absl::InvalidArgumentError("Exceeded variable count limit"); - } - absl::string_view var_name = absl::get(segment).var_name; - - if (var_name.size() < pattern_matching_min_variable_name_len || - var_name.size() > pattern_matching_max_variable_name_len) { - return absl::InvalidArgumentError("Invalid variable length"); - } - if (captured_variables.contains(var_name)) { - return absl::InvalidArgumentError("Repeated variable name"); - } - captured_variables.emplace(var_name); - } - - return captured_variables; -} - -absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { - bool seen_text_glob = false; - for (const ParsedSegment& segment : pattern.parsed_segments) { - if (absl::holds_alternative(segment)) { - if (seen_text_glob) { - return absl::InvalidArgumentError("Glob after text glob."); - } - seen_text_glob = (absl::get(segment) == Operator::kTextGlob); - } else if (absl::holds_alternative(segment)) { - const Variable& var = absl::get(segment); - if (var.var_match.empty()) { - if (seen_text_glob) { - // A variable with no explicit matcher is treated as a path glob. - return absl::InvalidArgumentError("Implicit variable path glob after text glob."); - } - } else { - for (const absl::variant& var_seg : var.var_match) { - if (!absl::holds_alternative(var_seg)) { - continue; - } - if (seen_text_glob) { - return absl::InvalidArgumentError("Glob after text glob."); - } - seen_text_glob = (absl::get(var_seg) == Operator::kTextGlob); - } - } - } - } - return absl::OkStatus(); -} - -absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern) { - struct ParsedUrlPattern parsed_pattern; - - static const LazyRE2 printable_regex = {"^/[[:graph:]]*$"}; - if (!RE2::FullMatch(toStringPiece(url_pattern), *printable_regex)) { - - return absl::InvalidArgumentError("Invalid pattern"); - } - - // Consume the leading '/' - url_pattern = url_pattern.substr(1); - - // Do the initial lexical parsing. - while (!url_pattern.empty()) { - ParsedSegment segment; - if (url_pattern[0] == '*') { - - absl::StatusOr status = AlsoUpdatePattern(consumeOperator, &url_pattern); - if (!status.ok()) { - return status.status(); - } - segment = *std::move(status); - } else if (url_pattern[0] == '{') { - - absl::StatusOr status = AlsoUpdatePattern(consumeVariable, &url_pattern); - if (!status.ok()) { - return status.status(); - } - segment = *std::move(status); - } else { - - absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &url_pattern); - if (!status.ok()) { - return status.status(); - } - segment = *std::move(status); - } - parsed_pattern.parsed_segments.push_back(segment); - - // Deal with trailing '/' or suffix. - if (!url_pattern.empty()) { - if (url_pattern == "/") { - // Single trailing '/' at the end, mark this with empty literal. - parsed_pattern.parsed_segments.emplace_back(""); - break; - } else if (url_pattern[0] == '/') { - // Have '/' followed by more text, consume the '/'. - url_pattern = url_pattern.substr(1); - } else { - // Not followed by '/', treat as suffix. - - absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &url_pattern); - if (!status.ok()) { - return status.status(); - } - parsed_pattern.suffix = *std::move(status); - if (!url_pattern.empty()) { - // Suffix didn't consume whole remaining pattern ('/' in url_pattern). - return absl::InvalidArgumentError("Prefix match not supported."); - } - break; - } - } - } - absl::StatusOr> status = - gatherCaptureNames(parsed_pattern); - if (!status.ok()) { - return status.status(); - } - parsed_pattern.captured_variables = *std::move(status); - - absl::Status validate_status = validateNoOperatorAfterTextGlob(parsed_pattern); - if (!validate_status.ok()) { - return validate_status; - } - - return parsed_pattern; -} - -std::string toRegexPattern(absl::string_view pattern) { - return absl::StrReplaceAll( - pattern, {{"$", "\\$"}, {"(", "\\("}, {")", "\\)"}, {"+", "\\+"}, {".", "\\."}}); -} - -std::string toRegexPattern(Operator pattern) { - static const std::string* kPathGlobRegex = new std::string(absl::StrCat("[", kLiteral, "]+")); - static const std::string* kTextGlobRegex = new std::string(absl::StrCat("[", kLiteral, "/]*")); - switch (pattern) { - case Operator::kPathGlob: // "*" - return *kPathGlobRegex; - case Operator::kTextGlob: // "**" - return *kTextGlobRegex; - } -} - -std::string toRegexPattern(const Variable& pattern) { - return absl::StrCat("(?P<", pattern.var_name, ">", - pattern.var_match.empty() - ? toRegexPattern(kDefaultVariableOperator) - : absl::StrJoin(pattern.var_match, "/", ToRegexPatternFormatter()), - ")"); -} - -std::string toRegexPattern(const struct ParsedUrlPattern& pattern) { - return absl::StrCat("/", absl::StrJoin(pattern.parsed_segments, "/", ToRegexPatternFormatter()), - toRegexPattern(pattern.suffix.value_or(""))); -} - -} // namespace url_template_matching_internal - -} // namespace matching -} // namespace Envoy diff --git a/source/extensions/url_template/url_template_matching_internal.h b/source/extensions/url_template/url_template_matching_internal.h deleted file mode 100644 index af8476a4a8db7..0000000000000 --- a/source/extensions/url_template/url_template_matching_internal.h +++ /dev/null @@ -1,95 +0,0 @@ -#ifndef SOURCE_EXTENSIONS_URL_TEMPLATE_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H -#define SOURCE_EXTENSIONS_URL_TEMPLATE_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H - -#include -#include -#include -#include - -#include "absl/container/flat_hash_set.h" -#include "absl/status/statusor.h" -#include "absl/strings/string_view.h" -#include "absl/types/optional.h" -#include "absl/types/variant.h" -#include "re2/re2.h" - -namespace Envoy { -namespace matching { - -namespace url_template_matching_internal { - -using Literal = absl::string_view; -enum class Operator { kPathGlob, kTextGlob }; - -struct RewriteSegment { - // Represents a segment of the rewritten URL, including any path segments, - // slash and prefix. - absl::string_view literal; - - // Represents an index into the RE2 capture which value should be used - // to construct the rewritten URL. Note that the index should be greater - // than 0 as 0 index into the whole match RE2 pattern. - int var_index; -}; - -struct Variable { - absl::string_view var_name; - std::vector> var_match; - - Variable(absl::string_view name, std::vector> match) - : var_name(name), var_match(match) {} - - std::string DebugString() const; -}; - -using ParsedSegment = absl::variant; - -struct ParsedUrlPattern { - std::vector parsed_segments; - absl::optional suffix; - absl::flat_hash_set captured_variables; - - std::string DebugString() const; -}; - -bool isValidLiteral(absl::string_view pattern); - -bool isValidRewriteLiteral(absl::string_view pattern); - -bool isValidIndent(absl::string_view pattern); - -// Used by the following Consume{Literal.Operator,Variable} functions -// in the return value. The functions would take the given pattern, -// parse what it can into |parsed_value| and return the unconsumed -// portion of the pattern in |unconsumed_pattern|. -template struct ParsedResult { - ParsedResult(T val, absl::string_view pattern) : parsed_value(val), unconsumed_pattern(pattern) {} - - T parsed_value; - absl::string_view unconsumed_pattern; -}; - -absl::StatusOr> consumeLiteral(absl::string_view pattern); - -absl::StatusOr> consumeOperator(absl::string_view pattern); - -absl::StatusOr> consumeVariable(absl::string_view pattern); - -absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern); - -std::string toRegexPattern(Literal pattern); - -std::string toRegexPattern(Operator pattern); - -std::string toRegexPattern(const Variable& pattern); - -std::string toRegexPattern(const struct ParsedUrlPattern& pattern); - -inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } - -} // namespace url_template_matching_internal - -} // namespace matching -} // namespace Envoy - -#endif // SOURCE_EXTENSIONS_URL_TEMPLATE_MATCHING_URL_TEMPLATE_MATCHING_INTERNAL_H diff --git a/source/extensions/url_template/url_template_matching_internal_test.cc b/source/extensions/url_template/url_template_matching_internal_test.cc deleted file mode 100644 index 496088fff22ab..0000000000000 --- a/source/extensions/url_template/url_template_matching_internal_test.cc +++ /dev/null @@ -1,468 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "source/common/common/assert.h" -#include "source/extensions/url_template/url_template_matching_internal.h" -#include "source/common/protobuf/protobuf.h" - -#include "test/test_common/logging.h" -#include "test/test_common/status_utility.h" -#include "test/test_common/utility.h" - -#include "absl/strings/str_cat.h" -#include "absl/strings/string_view.h" -#include "gtest/gtest.h" -#include "re2/re2.h" - -namespace Envoy { -namespace matching { - -namespace url_template_matching_internal { - -namespace { - -using ::Envoy::StatusHelpers::StatusIs; - -TEST(InternalParsing, ParsedUrlDebugString) { - ParsedUrlPattern patt1 = { - { - "abc", - "def", - Operator::kPathGlob, - Variable("var", {Operator::kPathGlob, "ghi", Operator::kTextGlob}), - }, - ".test", - {}, - }; - EXPECT_EQ(patt1.DebugString(), "/abc/def/*/{var=*/ghi/**}.test"); - - ParsedUrlPattern patt2 = {{ - Variable("var", {}), - }, - "", - {}}; - EXPECT_EQ(patt2.DebugString(), "/{var}"); -} - -TEST(InternalParsing, isValidLiteralWorks) { - EXPECT_TRUE(isValidLiteral("123abcABC")); - EXPECT_TRUE(isValidLiteral("._~-")); - EXPECT_TRUE(isValidLiteral("-._~%20!$&'()+,;:@")); - EXPECT_FALSE(isValidLiteral("`~!@#$%^&()-_+;:,<.>'\"\\| ")); - EXPECT_FALSE(isValidLiteral("abc/")); - EXPECT_FALSE(isValidLiteral("ab*c")); - EXPECT_FALSE(isValidLiteral("a**c")); - EXPECT_FALSE(isValidLiteral("a=c")); - EXPECT_FALSE(isValidLiteral("?abc")); - EXPECT_FALSE(isValidLiteral("?a=c")); - EXPECT_FALSE(isValidLiteral("{abc")); - EXPECT_FALSE(isValidLiteral("abc}")); - EXPECT_FALSE(isValidLiteral("{abc}")); -} - -TEST(InternalParsing, isValidRewriteLiteralWorks) { - EXPECT_TRUE(isValidRewriteLiteral("123abcABC")); - EXPECT_TRUE(isValidRewriteLiteral("abc/")); - EXPECT_TRUE(isValidRewriteLiteral("abc/def")); - EXPECT_TRUE(isValidRewriteLiteral("/abc.def")); - EXPECT_TRUE(isValidRewriteLiteral("._~-")); - EXPECT_TRUE(isValidRewriteLiteral("-._~%20!$&'()+,;:@")); - EXPECT_FALSE(isValidRewriteLiteral("`~!@#$%^&()-_+;:,<.>'\"| ")); - EXPECT_FALSE(isValidRewriteLiteral("ab}c")); - EXPECT_FALSE(isValidRewriteLiteral("ab{c")); - EXPECT_FALSE(isValidRewriteLiteral("a=c")); - EXPECT_FALSE(isValidRewriteLiteral("?a=c")); -} - -TEST(InternalParsing, isValidIdentWorks) { - EXPECT_TRUE(isValidIndent("abc")); - EXPECT_TRUE(isValidIndent("ABC_def_123")); - EXPECT_TRUE(isValidIndent("a1")); - EXPECT_TRUE(isValidIndent("T")); - EXPECT_FALSE(isValidIndent("123")); - EXPECT_FALSE(isValidIndent("__undefined__")); - EXPECT_FALSE(isValidIndent("abc-def")); - EXPECT_FALSE(isValidIndent("abc=def")); - EXPECT_FALSE(isValidIndent("abc def")); - EXPECT_FALSE(isValidIndent("a!!!")); -} - -TEST(InternalParsing, ConsumeLiteralWorks) { - std::string pattern = "abc/123"; - - absl::StatusOr> result = consumeLiteral(pattern); - - ASSERT_OK(result); - EXPECT_EQ(result->parsed_value, "abc"); - EXPECT_EQ(result->unconsumed_pattern, "/123"); -} - -TEST(InternalParsing, ConsumeTextGlob) { - std::string pattern = "***abc/123"; - - absl::StatusOr> result = consumeOperator(pattern); - - ASSERT_OK(result); - EXPECT_EQ(result->parsed_value, Operator::kTextGlob); - EXPECT_EQ(result->unconsumed_pattern, "*abc/123"); -} - -TEST(InternalParsing, ConsumePathGlob) { - std::string pattern = "*/123"; - - absl::StatusOr> result = consumeOperator(pattern); - - ASSERT_OK(result); - EXPECT_EQ(result->parsed_value, Operator::kPathGlob); - EXPECT_EQ(result->unconsumed_pattern, "/123"); -} - -class ConsumeVariableSuccess : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P(ConsumeVariableSuccessTestSuite, ConsumeVariableSuccess, - testing::Values("{var=*}", "{Var}", "{v1=**}", "{v_1=*/abc/**}", - "{v3=abc}", "{v=123/*/*}", "{var=abc/*/def}")); - -TEST_P(ConsumeVariableSuccess, ConsumeVariableSuccessTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - absl::StatusOr> result = consumeVariable(pattern); - - ASSERT_OK(result); - EXPECT_EQ(result->parsed_value.DebugString(), pattern); - EXPECT_TRUE(result->unconsumed_pattern.empty()); -} - -class ConsumeVariableFailure : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P(ConsumeVariableFailureTestSuite, ConsumeVariableFailure, - testing::Values("{var", "{=abc}", "{_var=*}", "{1v}", "{1v=abc}", - "{var=***}", "{v-a-r}", "{var=*/abc?q=1}", "{var=abc/a*}", - "{var=*def/abc}", "{var=}", "{var=abc=def}", - "{rc=||||(A+yl/}")); - -TEST_P(ConsumeVariableFailure, ConsumeVariableFailureTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - EXPECT_THAT(consumeVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); -} - -class parseURLPatternSyntaxSuccess : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P( - parseURLPatternSyntaxSuccessTestSuite, parseURLPatternSyntaxSuccess, - testing::Values("/**.m3u8", "/**.mpd", "/*_suf", "/{path=**}.m3u8", "/{foo}/**.ts", - "/media/*.m4s", "/media/{contentId=*}/**", "/media/*", "/api/*/*/**", - "/api/*/v1/**", "/api/*/v1/*", "/{version=api/*}/*", "/api/*/*/", - "/api/*/1234/", "/api/*/{resource=*}/{method=*}", - "/api/*/{resource=*}/{method=**}", "/v1/**", "/media/{country}/{lang=*}/**", - "/{foo}/{bar}/{fo}/{fum}/*", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", - "/media/{id=*}/*", "/media/{contentId=**}", - "/api/{version}/projects/{project}/locations/{location}/{resource}/" - "{name}", - "/api/{version=*}/{url=**}", "/api/{VERSION}/{version}/{verSION}", - "/api/1234/abcd", "/media/abcd/%10%20%30/{v1=*/%10%20}_suffix", "/")); - -TEST_P(parseURLPatternSyntaxSuccess, parseURLPatternSyntaxSuccessTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - absl::StatusOr parsed_patt = parseURLPatternSyntax(pattern); - ASSERT_OK(parsed_patt); - EXPECT_EQ(parsed_patt->DebugString(), pattern); -} - -class parseURLPatternSyntaxFailure : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P( - parseURLPatternSyntaxFailureTestSuite, parseURLPatternSyntaxFailure, - testing::Values("/api/v*/1234", "/api/{version=v*}/1234", "/api/v{versionNum=*}/1234", - "/api/{version=*beta}/1234", "/media/eff456/ll-sd-out.{ext}", - "/media/eff456/ll-sd-out.{ext=*}", "/media/eff456/ll-sd-out.**", - "/media/{country=**}/{lang=*}/**", "/media/**/*/**", "/link/{id=*}/asset*", - "/link/{id=*}/{asset=asset*}", "/media/{id=/*}/*", "/media/{contentId=/**}", - "/api/{version}/{version}", "/api/{version.major}/{version.minor}", - "/media/***", "/media/*{*}*", "/media/{*}/", "/media/*/index?a=2", "media", - "/\001\002\003\004\005\006\007", "/*(/**", "/**/{var}", - "/{var1}/{var2}/{var3}/{var4}/{var5}/{var6}", "/{=*}", - "/{var12345678901234=*}")); - -TEST_P(parseURLPatternSyntaxFailure, parseURLPatternSyntaxFailureTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - EXPECT_THAT(parseURLPatternSyntax(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); -} - -TEST(InternalRegexGen, LiteralEscapes) { - EXPECT_EQ(toRegexPattern("abcABC123/-._~%20!$&'()+,;:@"), - "abcABC123/-\\._~%20!\\$&'\\(\\)\\+,;:@"); -} - -TEST(InternalRegexGen, LiteralMatches) { - absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; - - EXPECT_TRUE(RE2::FullMatch(toStringPiece(kPattern), toRegexPattern(kPattern))); -} - -TEST(InternalRegexGen, LiteralMatchesInNamedCapture) { - absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; - - RE2 regex = RE2(absl::StrCat("(?P", toRegexPattern(kPattern), ")")); - ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); - - // Full matched string + capture groups - std::vector captures(2); - ASSERT_TRUE(regex.Match(toStringPiece(kPattern), /*startpos=*/0, /*endpos=*/kPattern.size(), - RE2::ANCHOR_BOTH, captures.data(), captures.size())); - - // Index 0 would be the full text of the matched string. - EXPECT_EQ(toStringPiece(kPattern), captures[0]); - // Get the pattern matched with the named capture group. - EXPECT_EQ(toStringPiece(kPattern), captures.at(regex.NamedCapturingGroups().at("var"))); -} - -TEST(InternalRegexGen, LiteralOnlyMatchesItself) { - constexpr absl::string_view kChars = "abcABC123/-._~%20!$&'()+,;:@"; - - for (const char c : kChars) { - std::string s = {'z', c, 'z'}; - EXPECT_TRUE(RE2::FullMatch(s, toRegexPattern(s))); - EXPECT_FALSE(RE2::FullMatch("zzz", toRegexPattern(s))); - } -} - -TEST(InternalRegexGen, RegexLikePatternIsMatchedLiterally) { - EXPECT_TRUE(RE2::FullMatch("(abc)", toRegexPattern("(abc)"))); - EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); - - EXPECT_TRUE(RE2::FullMatch("(abc)+", toRegexPattern("(abc)+"))); - EXPECT_FALSE(RE2::FullMatch("", toRegexPattern("(abc)+"))); - EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); - EXPECT_FALSE(RE2::FullMatch("abcabc", toRegexPattern("(abc)+"))); - - EXPECT_TRUE(RE2::FullMatch(".+", toRegexPattern(".+"))); - EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern(".+"))); - - EXPECT_TRUE(RE2::FullMatch("a+", toRegexPattern("a+"))); - EXPECT_FALSE(RE2::FullMatch("aa", toRegexPattern("a+"))); -} - -TEST(InternalRegexGen, DollarSignMatchesIfself) { - EXPECT_TRUE(RE2::FullMatch("abc$", toRegexPattern("abc$"))); - EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("abc$"))); -} - -TEST(InternalRegexGen, OperatorRegexPattern) { - EXPECT_EQ(toRegexPattern(Operator::kPathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); - EXPECT_EQ(toRegexPattern(Operator::kTextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); -} - -TEST(InternalRegexGen, PathGlobRegex) { - EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kPathGlob))); -} - -TEST(InternalRegexGen, TextGlobRegex) { - EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kTextGlob))); - EXPECT_TRUE(RE2::FullMatch("", toRegexPattern(Operator::kTextGlob))); - EXPECT_TRUE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kTextGlob))); -} - -TEST(InternalRegexGen, VariableRegexPattern) { - EXPECT_EQ(toRegexPattern(Variable("var1", {})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"); - EXPECT_EQ(toRegexPattern(Variable("var2", {Operator::kPathGlob, "abc", Operator::kTextGlob})), - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+/abc/" - "[a-zA-Z0-9-._~%!$&'()+,;:@/]*)"); -} - -TEST(InternalRegexGen, VariableRegexDefaultMatch) { - absl::StatusOr> var = consumeVariable("{var}"); - ASSERT_OK(var); - - std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc", toRegexPattern(var->parsed_value), &capture)); - EXPECT_EQ(capture, "abc"); -} - -TEST(InternalRegexGen, VariableRegexDefaultNotMatch) { - absl::StatusOr> var = consumeVariable("{var}"); - ASSERT_OK(var); - - EXPECT_FALSE(RE2::FullMatch("abc/def", toRegexPattern(var->parsed_value))); -} - -TEST(InternalRegexGen, VariableRegexSegmentsMatch) { - absl::StatusOr> var = consumeVariable("{var=abc/*/def}"); - ASSERT_OK(var); - - std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); - EXPECT_EQ(capture, "abc/123/def"); -} - -TEST(InternalRegexGen, VariableRegexTextGlobMatch) { - absl::StatusOr> var = consumeVariable("{var=**/def}"); - ASSERT_OK(var); - - std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); - EXPECT_EQ(capture, "abc/123/def"); -} - -TEST(InternalRegexGen, VariableRegexNamedCapture) { - re2::StringPiece kPattern = "abc"; - absl::StatusOr> var = consumeVariable("{var=*}"); - ASSERT_OK(var); - - RE2 regex = RE2(toRegexPattern(var->parsed_value)); - ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); - - // Full matched string + capture groups - std::vector captures(2); - ASSERT_TRUE(regex.Match(kPattern, /*startpos=*/0, /*endpos=*/kPattern.size(), RE2::ANCHOR_BOTH, - captures.data(), captures.size())); - - // Index 0 would be the full text of the matched string. - EXPECT_EQ(kPattern, captures[0]); - // Get the pattern matched with the named capture group. - EXPECT_EQ(kPattern, captures.at(regex.NamedCapturingGroups().at("var"))); -} - -TEST(InternalRegexGen, ParsedURLPatternToRegex) { - absl::StatusOr pattern = - parseURLPatternSyntax("/abc/*/{var1}/def/{var2=*/ghi/**}.jkl"); - ASSERT_OK(pattern); - - std::string var1_capture; - std::string var2_capture; - EXPECT_TRUE(RE2::FullMatch("/abc/123/456/def/789/ghi/%20/($).jkl", - toRegexPattern(pattern.value()), &var1_capture, &var2_capture)); - EXPECT_EQ(var1_capture, "456"); - EXPECT_EQ(var2_capture, "789/ghi/%20/($)"); -} - -struct GenPatternTestCase { - GenPatternTestCase(std::string request_path, std::string url_pattern, - std::vector> capture_pairs) - : path(request_path), pattern(url_pattern), captures(capture_pairs) {} - std::string path; - std::string pattern; - std::vector> captures; -}; - -class GenPatternRegexWithMatch : public testing::TestWithParam { -protected: - const std::string& request_path() const { return GetParam().path; } - const std::string& url_pattern() const { return GetParam().pattern; } - std::vector> const var_values() { - return GetParam().captures; - } -}; - -INSTANTIATE_TEST_SUITE_P( - GenPatternRegexWithMatchTestSuite, GenPatternRegexWithMatch, - testing::Values( - GenPatternTestCase("/media/1234/manifest.m3u8", "/**.m3u8", {}), - GenPatternTestCase("/manifest.mpd", "/**.mpd", {}), - GenPatternTestCase("/media/1234/manifest.m3u8", "/{path=**}.m3u8", - {{"path", "media/1234/manifest"}}), - GenPatternTestCase("/foo/12314341/format/123/hls/segment_0000000001.ts", "/{foo}/**.ts", - {{"foo", "foo"}}), - GenPatternTestCase("/media/eff456/ll-sd-out.js", "/media/{contentId=*}/**", - {{"contentId", "eff456"}}), - GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/*/**", {}), - GenPatternTestCase("/api/v1/1234", "/{version=api/*}/*", {{"version", "api/v1"}}), - GenPatternTestCase("/api/v1/1234/", "/api/*/*/", {}), - GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=**}", - {{"resource", "1234"}, {"method", "broadcasts/get"}}), - GenPatternTestCase("/v1/broadcasts/12345/live", "/v1/**", {}), - GenPatternTestCase("/media/us/en/12334/subtitle_enUS_00101.vtt", - "/media/{country}/{lang=*}/**", {{"country", "us"}, {"lang", "en"}}), - GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo}/{bar}/{fo}/{fum}/*", - {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), - GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", - {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), - GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{id=*}/**", {{"id", "1234"}}), - GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{contentId=**}", - {{"contentId", "1234/hls/1001011.m3u8"}}), - GenPatternTestCase("/api/v1/projects/my-project/locations/global/edgeCacheOrigins/foo", - "/api/{version}/projects/{project}/locations/{location}/{resource}/" - "{name}", - {{"version", "v1"}, - {"project", "my-project"}, - {"location", "global"}, - {"resource", "edgeCacheOrigins"}, - {"name", "foo"}}), - GenPatternTestCase("/api/v1/foo/bar/baz/", "/api/{version=*}/{url=**}", - {{"version", "v1"}, {"url", "foo/bar/baz/"}}), - GenPatternTestCase("/api/v1/v2/v3", "/api/{VERSION}/{version}/{verSION}", - {{"VERSION", "v1"}, {"version", "v2"}, {"verSION", "v3"}}))); - -TEST_P(GenPatternRegexWithMatch, WithCapture) { - absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); - ASSERT_OK(pattern); - - RE2 regex = RE2(toRegexPattern(pattern.value())); - ASSERT_TRUE(regex.ok()) << regex.error(); - ASSERT_EQ(regex.NumberOfCapturingGroups(), var_values().size()); - - int capture_num = regex.NumberOfCapturingGroups() + 1; - std::vector captures(capture_num); - ASSERT_TRUE(regex.Match(request_path(), /*startpos=*/0, - /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, captures.data(), - captures.size())); - - EXPECT_EQ(captures[0], toStringPiece(request_path())); - - for (const auto& [name, value] : var_values()) { - int capture_index = regex.NamedCapturingGroups().at(name); - ASSERT_GE(capture_index, 0); - EXPECT_EQ(captures.at(capture_index), value); - } -} - -class GenPatternRegexWithoutMatch - : public testing::TestWithParam> { -protected: - const std::string& request_path() const { return std::get<0>(GetParam()); } - const std::string& url_pattern() const { return std::get<1>(GetParam()); } -}; - -INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWithoutMatch, - testing::ValuesIn(std::vector>( - {{"/media/12345/f/123/s00002.m4s", "/media/*.m4s"}, - {"/media/eff456/ll-sd-out.js", "/media/*"}, - {"/api/v1/1234/", "/api/*/v1/*"}, - {"/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=*}"}, - {"/api/v1/1234/", "/api/*/v1/**"}, - {"/api/*/1234/", "/api/*/1234/"}}))); - -TEST_P(GenPatternRegexWithoutMatch, WithCapture) { - absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); - ASSERT_OK(pattern); - - RE2 regex = RE2(toRegexPattern(pattern.value())); - ASSERT_TRUE(regex.ok()) << regex.error(); - - EXPECT_FALSE(regex.Match(request_path(), /*startpos=*/0, - /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, nullptr, 0)); -} - -} // namespace -} // namespace url_template_matching_internal -} // namespace matching -} // namespace Envoy diff --git a/source/extensions/url_template/url_template_matching_test.cc b/source/extensions/url_template/url_template_matching_test.cc deleted file mode 100644 index e90be3d0cb705..0000000000000 --- a/source/extensions/url_template/url_template_matching_test.cc +++ /dev/null @@ -1,335 +0,0 @@ -#include -#include -#include - -#include "source/common/common/assert.h" -#include "source/extensions/url_template/url_template_matching.h" -#include "source/extensions/url_template/url_template_matching_internal.h" -#include "source/common/protobuf/protobuf.h" - -#include "test/test_common/logging.h" -#include "test/test_common/status_utility.h" -#include "test/test_common/utility.h" - -#include "gtest/gtest.h" - -namespace Envoy { -namespace matching { - -namespace { - -using ::Envoy::StatusHelpers::IsOkAndHolds; -using ::Envoy::StatusHelpers::StatusIs; - -// Capture regex for /{var1}/{var2}/{var3}/{var4}/{var5} -static constexpr absl::string_view kCaptureRegex = "/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"; -static constexpr absl::string_view kMatchUrl = "/val1/val2/val3/val4/val5"; - -TEST(ConvertURLPattern, ValidPattern) { - EXPECT_THAT(convertURLPatternSyntaxToRegex("/abc"), IsOkAndHolds("/abc")); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/**.mpd"), - IsOkAndHolds("/[a-zA-Z0-9-._~%!$&'()+,;:@/]*\\.mpd")); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/*/{resource=*}/{method=**}"), - IsOkAndHolds("/api/[a-zA-Z0-9-._~%!$&'()+,;:@]+/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@/]*)")); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/{VERSION}/{version}/{verSION}"), - IsOkAndHolds("/api/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)")); -} - -TEST(ConvertURLPattern, InvalidPattern) { - EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/v*/1234"), - StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/media/**/*/**"), - StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/\001\002\003\004\005\006\007"), - StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/{var12345678901234=*}"), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -class ParseRewriteHelperSuccess : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperSuccessTestSuite, ParseRewriteHelperSuccess, - testing::Values("/{var1}", "/{var1}{var2}", "/{var1}-{var2}", - "/abc/{var1}/def", "/{var1}/abd/{var2}", - "/abc-def-{var1}/a/{var1}")); - -TEST_P(ParseRewriteHelperSuccess, ParseRewriteHelperSuccessTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - EXPECT_OK(parseRewritePatternHelper(pattern)); -} - -class ParseRewriteHelperFailure : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperFailureTestSuite, ParseRewriteHelperFailure, - testing::Values("{var1}", "/{{var1}}", "/}va1{", "var1}", - "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); - -TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - EXPECT_THAT(parseRewritePatternHelper(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); -} - -class ParseRewriteSuccess : public testing::TestWithParam> { -protected: - const std::string& rewrite_pattern() const { return std::get<0>(GetParam()); } - envoy::extensions::url_template::v3::RouteUrlRewritePattern expected_proto() const { - envoy::extensions::url_template::v3::RouteUrlRewritePattern expected_proto; - Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); - return expected_proto; - } -}; - -TEST(ParseRewrite, InvalidRegex) { - EXPECT_THAT(parseRewritePattern("/{var1}", "+[abc"), StatusIs(absl::StatusCode::kInternal)); -} - -INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, - testing::ValuesIn(std::vector>({ - {"/static", R"EOF(segments: {literal: "/static"} )EOF"}, - {"/{var1}", R"EOF(segments: - - literal: "/" - - var_index: 1)EOF"}, - {"/{var1}", R"EOF(segments: - - literal: "/" - - var_index: 1)EOF"}, - {"/{var1}/{var1}/{var1}", R"EOF(segments: - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 1)EOF"}, - {"/{var3}/{var1}/{var2}", R"EOF(segments - - literal: "/" - - var_index: 3 - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 2)EOF"}, - {"/{var3}/abc/def/{var2}.suffix", R"EOF(segments: - - literal: "/" - - var_index: 3 - - literal: "/abc/def/" - - var_index: 2 - - literal: ".suffix")EOF"}, - {"/abc/{var1}/{var2}/def", R"EOF(segments - - literal: "/abc/" - - var_index: 1 - - literal: "/" - - var_index: 2 - - literal: "/def")EOF"}, - {"/{var1}{var2}", R"EOF(segments - - literal: "/" - - var_index: 1 - - ar_index: 2)EOF"}, - {"/{var1}-{var2}/bucket-{var3}.suffix", R"EOF(segments - - literal: "/" - - var_index: 1 - - literal: "-" - - var_index: 2 - - literal: "/bucket-" - - var_index: 3 - - literal: ".suffix")EOF"}, - }))); - -TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { - absl::StatusOr rewrite = - parseRewritePattern(rewrite_pattern(), kCaptureRegex); - ASSERT_OK(rewrite); - // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); -} - -class ParseRewriteFailure : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P(ParseRewriteFailureTestSuite, ParseRewriteFailure, - testing::Values("{var1}", "/{var6}", "/{{var1}}", "/}va1{", "var1}", - "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); - -TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - EXPECT_THAT(parseRewritePattern(pattern, kCaptureRegex), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -class RewriteUrlTemplateSuccess - : public testing::TestWithParam> { -protected: - envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_proto() const { - envoy::extensions::url_template::v3::RouteUrlRewritePattern proto; - Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); - return proto; - } - const std::string& expected_rewritten_url() const { return std::get<1>(GetParam()); } -}; - -INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateSuccess, - testing::ValuesIn(std::vector>( - {{R"EOF(segments: { literal: "/static" })EOF", "/static"}, - {R"EOF(segments: - - literal: "/" - - var_index: 1)EOF", - "/val1"}, - {R"EOF(segments: - - literal: "/" - - var_index: 1)EOF", - "/val1"}, - {R"EOF(segments: - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 1)EOF", - "/val1/val1/val1"}, - {R"EOF(segments: - - literal: "/" - - var_index: 3 - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 2)EOF", - "/val3/val1/val2"}, - {R"EOF(segments: - - literal: "/" - - var_index: 3 - - literal: "/abc/def/" - - var_index: 2 - - literal: ".suffix")EOF", - "/val3/abc/def/val2.suffix"}, - {R"EOF(segments: - - literal: "/" - - var_index: 3 - - var_index: 2 - - literal: "." - - var_index: 1)EOF", - "/val3val2.val1"}, - {R"EOF(segments: - - literal: "/abc/" - - var_index: 1 - - literal: "/" - - var_index: 5 - - literal: "/def")EOF", - "/abc/val1/val5/def"}}))); - -TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { - absl::StatusOr rewritten_url = - RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto()); - ASSERT_OK(rewritten_url); - EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); -} - -TEST(RewriteUrlTemplateFailure, BadRegex) { - envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- var_index: 1 - )EOF"; - - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "+/bad_regex", rewrite_proto), - StatusIs(absl::StatusCode::kInternal)); -} - -TEST(RewriteUrlTemplateFailure, RegexNoMatch) { - envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- var_index: 1 - )EOF"; - - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "/no_match_regex", rewrite_proto), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { - envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- var_index: 0 - )EOF"; - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { - envoy::extensions::url_template::v3::RouteUrlRewritePattern rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- var_index: 6 - )EOF"; - - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -class URLPatternMatchAndRewrite - : public testing::TestWithParam< - std::tuple> { -protected: - const std::string& url_pattern() const { return std::get<0>(GetParam()); } - const std::string& rewrite_pattern() const { return std::get<1>(GetParam()); } - const std::string& match_url() const { return std::get<2>(GetParam()); } - const std::string& expected_rewritten_url() const { return std::get<3>(GetParam()); } -}; - -INSTANTIATE_TEST_SUITE_P( - URLPatternMatchAndRewriteTestSuite, URLPatternMatchAndRewrite, - testing::ValuesIn(std::vector>( - {{"/api/users/{id}/{path=**}", "/users/{id}/{path}", "/api/users/21334/profile.json", - "/users/21334/profile.json"}, - {"/videos/*/{id}/{format}/{rendition}/{segment=**}.ts", - "/{id}/{format}/{rendition}/{segment}.ts", "/videos/lib/132939/hls/13/segment_00001.ts", - "/132939/hls/13/segment_00001.ts"}, - {"/region/{region}/bucket/{name}/{method=**}", "/{region}/bucket-{name}/{method}", - "/region/eu/bucket/prod-storage/object.pdf", "/eu/bucket-prod-storage/object.pdf"}, - {"/region/{region}/bucket/{name}/{method=**}", "/{region}{name}/{method}", - "/region/eu/bucket/prod-storage/object.pdf", "/euprod-storage/object.pdf"}}))); - -TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { - absl::StatusOr regex = convertURLPatternSyntaxToRegex(url_pattern()); - ASSERT_OK(regex); - - absl::StatusOr rewrite_proto = - parseRewritePattern(rewrite_pattern(), regex.value()); - ASSERT_OK(rewrite_proto); - - absl::StatusOr rewritten_url = - RewriteURLTemplatePattern(match_url(), regex.value(), rewrite_proto.value()); - ASSERT_OK(rewritten_url); - - EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); -} - -} // namespace - -} // namespace matching -} // namespace Envoy From eda37b8c900b864fb73f0857dcab1969b9e4b76b Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 25 Jul 2022 19:28:26 +0000 Subject: [PATCH 069/238] Work to merge Signed-off-by: silverstar195 --- test/extensions/url_template/BUILD | 33 --------------- test/extensions/url_template/config_test.cc | 42 -------------------- test/extensions/url_template/matcher_test.cc | 34 ---------------- 3 files changed, 109 deletions(-) delete mode 100644 test/extensions/url_template/BUILD delete mode 100644 test/extensions/url_template/config_test.cc delete mode 100644 test/extensions/url_template/matcher_test.cc diff --git a/test/extensions/url_template/BUILD b/test/extensions/url_template/BUILD deleted file mode 100644 index 2321f1f919dcd..0000000000000 --- a/test/extensions/url_template/BUILD +++ /dev/null @@ -1,33 +0,0 @@ -load( - "//bazel:envoy_build_system.bzl", - "envoy_package", -) -load( - "//test/extensions:extensions_build_system.bzl", - "envoy_extension_cc_test", -) - -licenses(["notice"]) # Apache 2 - -envoy_package() - -envoy_extension_cc_test( - name = "config_test", - srcs = ["config_test.cc"], - extension_names = ["envoy.url_template"], - deps = [ - "//source/common/stream_info:filter_state_lib", - "//source/extensions/url_template:config", - "//test/mocks/server:factory_context_mocks", - "@envoy_api//envoy/extensions/url_template/v3:pkg_cc_proto", - ], -) - -envoy_extension_cc_test( - name = "matcher_test", - srcs = ["matcher_test.cc"], - extension_names = ["envoy.url_template"], - deps = [ - "//source/extensions/url_template:url_template_matching", - ], -) diff --git a/test/extensions/url_template/config_test.cc b/test/extensions/url_template/config_test.cc deleted file mode 100644 index 7c80a105cb744..0000000000000 --- a/test/extensions/url_template/config_test.cc +++ /dev/null @@ -1,42 +0,0 @@ -#include "envoy/registry/registry.h" -#include "envoy/router/url_template.h" - -#include "source/common/stream_info/filter_state_impl.h" -#include "source/extensions/url_template/config.h" - -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -using namespace testing; - -namespace Envoy { -namespace Extensions { -namespace UrlTemplate { -namespace { - -class UrlTemplateConfigTest : public testing::Test { -protected: - UrlTemplateConfigTest() : filter_state_(StreamInfo::FilterState::LifeSpan::FilterChain) { - factory_ = Registry::FactoryRegistry::getFactory( - "envoy.url_template_predicates"); - config_ = factory_->createEmptyConfigProto(); - } - - StreamInfo::FilterStateImpl filter_state_; - Router::UrlTemplatePredicateFactory* factory_; - ProtobufTypes::MessagePtr config_; -}; - -TEST_F(UrlTemplateConfigTest, EmptyCreation) { - std::string current_route_name = "fake_current_route"; - // Create the predicate for the first time. - { - auto predicate = factory_->createUrlTemplatePredicate("/url_pattern/{TEST}", "rewrite_pattern"); - ASSERT(predicate); - } -} - -} // namespace -} // namespace UrlTemplate -} // namespace Extensions -} // namespace Envoy diff --git a/test/extensions/url_template/matcher_test.cc b/test/extensions/url_template/matcher_test.cc deleted file mode 100644 index 0b0709a3a0ade..0000000000000 --- a/test/extensions/url_template/matcher_test.cc +++ /dev/null @@ -1,34 +0,0 @@ -#include "source/extensions/url_template/url_template_matching.h" - -#include "gtest/gtest.h" - -namespace Envoy { -namespace Extensions { -namespace UrlTemplate { - -TEST(UrlTemplate, RouteMatcher) { - matching::UrlTemplatePredicate matcher("/foo/{lang}/{country}", "rewrite"); - - EXPECT_TRUE(matcher.match("/foo/english/us")); - EXPECT_TRUE(matcher.match("/foo/spanish/spain")); - EXPECT_TRUE(matcher.match("/foo/french/france")); - - // with params - EXPECT_TRUE(matcher.match("/foo/english/us#fragment")); - EXPECT_TRUE(matcher.match("/foo/spanish/spain#fragment?param=val")); - EXPECT_TRUE(matcher.match("/foo/french/france?param=regex")); - - EXPECT_FALSE(matcher.match("/foo/english/us/goat")); - EXPECT_FALSE(matcher.match("/foo/goat")); - EXPECT_FALSE(matcher.match("/foo")); - EXPECT_FALSE(matcher.match("")); - - // with params - EXPECT_FALSE(matcher.match("/foo/english/us/goat#fragment?param=val")); - EXPECT_FALSE(matcher.match("/foo/goat?param=regex")); - EXPECT_FALSE(matcher.match("/foo?param=regex")); -} - -} // namespace UrlTemplate -} // namespace Extensions -} // namespace Envoy From f4c353a4e18e03116174cf18901556c4d0e612a1 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 26 Jul 2022 14:11:38 +0000 Subject: [PATCH 070/238] Add progress Signed-off-by: silverstar195 --- .../config/route/v3/route_components.proto | 10 ++- envoy/router/path_match_policy.h | 3 +- envoy/router/path_rewrite_policy.h | 2 +- source/common/router/BUILD | 8 +- source/common/router/config_impl.cc | 77 ++++++++----------- source/common/router/config_impl.h | 15 ++-- .../path/match/pattern_template/BUILD | 8 +- .../path/match/pattern_template/config.cc | 2 +- .../path/match/pattern_template/config.h | 10 +-- .../pattern_template_match.cc | 3 +- .../pattern_template/pattern_template_match.h | 12 +-- .../path/pattern_template_lib/BUILD | 16 ++-- .../pattern_template_lib/pattern_template.cc | 6 +- .../pattern_template_lib/pattern_template.h | 10 +-- .../pattern_template_internal.cc | 2 +- .../pattern_template_internal.h | 6 +- .../pattern_template_internal_test.cc | 2 +- .../pattern_template_test.cc | 4 +- .../pattern_template_rewrite_segments.proto | 0 .../path/rewrite/pattern_template/BUILD | 8 +- .../path/rewrite/pattern_template/config.cc | 2 +- .../path/rewrite/pattern_template/config.h | 8 +- .../pattern_template_rewrite.cc | 6 +- .../pattern_template_rewrite.h | 8 +- 24 files changed, 108 insertions(+), 120 deletions(-) rename source/extensions/path/{pattern_template => pattern_template_lib}/proto/pattern_template_rewrite_segments.proto (100%) diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index 75d145d1f4a91..b4aec2a218101 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -572,6 +572,7 @@ message RouteMatch { // Expect the value to not contain ``?`` or ``#`` and not to end in ``/`` string path_separated_prefix = 14 [(validate.rules).string = {pattern: "^[^?#]+[^?#/]$"}]; + // [#not-implemented-hide:] // [#comment: TODO(silverstar195): Hook into extension once added] core.v3.TypedExtensionConfig path_match_policy = 15; } @@ -1019,8 +1020,8 @@ message RouteAction { // place the original path before rewrite into the :ref:`x-envoy-original-path // ` header. // - // Only one of :ref:`regex_rewrite `, - // :ref:`path_rewrite_policy ` + // Only one of :ref:`regex_rewrite ' + // [#comment:TODO(silverstar194) add the following once path_template_rewrite is implemented: :ref:`path_rewrite_policy `] // or :ref:`prefix_rewrite ` may be specified. // // .. attention:: @@ -1056,8 +1057,8 @@ message RouteAction { // before the rewrite into the :ref:`x-envoy-original-path // ` header. // - // Only one of :ref:`regex_rewrite `, :ref:`prefix_rewrite `, - // or :ref:`path_rewrite_policy ` + // Only one of :ref:`regex_rewrite ` or :ref:`prefix_rewrite ` + // [#comment:TODO(silverstar194) add the following once implemented: :ref:`path_rewrite_policy `] // may be specified. // // Examples using Google's `RE2 `_ engine: @@ -1078,6 +1079,7 @@ message RouteAction { // ``/aaa/yyy/bbb``. type.matcher.v3.RegexMatchAndSubstitute regex_rewrite = 32; + // [#not-implemented-hide:] // [#comment: TODO(silverstar195): Hook into extension once added] core.v3.TypedExtensionConfig path_rewrite_policy = 41; diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index 465c65a0c5138..8e14c5716e8f2 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -32,9 +32,8 @@ using PathMatchPredicateSharedPtr = std::shared_ptr; class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { public: virtual ~PathMatchPredicateFactory() = default; - virtual PathMatchPredicateSharedPtr - createPathMatchPredicate(std::string url_pattern) PURE; + createPathMatchPredicate(const Protobuf::Message& config, std::string url_pattern) PURE; std::string category() const override { return "envoy.path_match_policy"; } }; diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index e5c25b4a659a2..396635243e01f 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -35,7 +35,7 @@ class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { virtual ~PathRewritePredicateFactory() override = default; virtual PathRewritePredicateSharedPtr - createPathRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) PURE; + createPathRewritePredicate(const Protobuf::Message& config, std::string url_pattern); std::string category() const override { return "envoy.path_rewrite_policy"; } }; diff --git a/source/common/router/BUILD b/source/common/router/BUILD index e06e78ea104b4..c73db31576787 100644 --- a/source/common/router/BUILD +++ b/source/common/router/BUILD @@ -70,10 +70,10 @@ envoy_cc_library( "//source/common/tracing:http_tracer_lib", "//source/common/upstream:retry_factory_lib", "//source/extensions/early_data:default_early_data_policy_lib", - "//source/extensions/pattern_template/match:config", - "//source/extensions/pattern_template/match:pattern_template_match_lib", - "//source/extensions/pattern_template/rewrite:config", - "//source/extensions/pattern_template/rewrite:pattern_template_rewrite_lib", + "//source/extensions/path/match/pattern_template:config", + "//source/extensions/path/match/pattern_template:pattern_template_match_lib", + "//source/extensions/path/rewrite/pattern_template:config", + "//source/extensions/path/rewrite/pattern_template:pattern_template_rewrite_lib", "@envoy_api//envoy/config/common/matcher/v3:pkg_cc_proto", "@envoy_api//envoy/config/core/v3:pkg_cc_proto", "@envoy_api//envoy/config/route/v3:pkg_cc_proto", diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 0c3f6345a6730..5059d8fefc36c 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -48,10 +48,10 @@ #include "source/common/tracing/http_tracer_impl.h" #include "source/common/upstream/retry_factory.h" #include "source/extensions/early_data/default_early_data_policy.h" -#include "source/extensions/pattern_template/match/pattern_template_match.h" -#include "source/extensions/pattern_template/match/config.h" -#include "source/extensions/pattern_template/rewrite/pattern_template_rewrite.h" -#include "source/extensions/pattern_template/rewrite/config.h" +#include "source/extensions/path/match/pattern_template/pattern_template_match.h" +#include "source/extensions/path/match/pattern_template/config.h" +#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" +#include "source/extensions/path/rewrite/pattern_template/config.h" #include "absl/strings/match.h" @@ -361,26 +361,22 @@ std::vector InternalRedirectPolicyImpl::pred PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; -PathMatchPolicyImpl::PathMatchPolicyImpl(const ProtobufWkt::Any& typed_config, +PathMatchPolicyImpl::PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, std::string url_pattern, ProtobufMessage::ValidationVisitor& validator) - : enabled_(true) { + : url_pattern_(url_pattern), enabled_(true) { - absl::string_view name = "envoy.url_template.pattern_template_predicates"; + absl::string_view name = "envoy.path_match_policy.pattern_template_match_predicate"; auto* factory = Registry::FactoryRegistry::getFactory(name); ASSERT(factory); // factory not found ProtobufTypes::MessagePtr proto_config = factory->createEmptyConfigProto(); - Envoy::Config::Utility::translateOpaqueConfig(typed_config, validator, *proto_config); - - // validate with match input factory - if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { - throw EnvoyException(fmt::format("path_match_policy {} is invalid", path_template)); - } + Envoy::Config::Utility::translateOpaqueConfig(typed_config.typed_config(), validator, *proto_config); predicate_factory_ = factory; + predicate_config_ = std::move(proto_config); } PathMatchPredicateSharedPtr PathMatchPolicyImpl::predicate() const { - return predicate_factory_->createPathMatchPredicate(url_pattern_, url_rewrite_pattern_); + return predicate_factory_->createPathMatchPredicate(*predicate_config_, url_pattern_); } absl::flat_hash_set InternalRedirectPolicyImpl::buildRedirectResponseCodes( @@ -503,8 +499,8 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, ProtobufMessage::ValidationVisitor& validator) : case_sensitive_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.match(), case_sensitive, true)), prefix_rewrite_(route.route().prefix_rewrite()), - path_match_policy_(buildPathMatchPolicy(route.match(), validator)), - path_rewrite_policy_(buildPathRewritePolicy(route.route(), validator)), + path_match_policy_(buildPathMatchPolicy(route, validator)), + path_rewrite_policy_(buildPathRewritePolicy(route, validator)), host_rewrite_(route.route().host_rewrite_literal()), vhost_(vhost), auto_host_rewrite_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.route(), auto_host_rewrite, false)), auto_host_rewrite_header_(!route.route().host_rewrite_header().empty() @@ -683,6 +679,18 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, if (!prefix_rewrite_.empty() || route.route().has_regex_rewrite()) { throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); } + + // TODO: validate path_match_policy + if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { + throw EnvoyException(fmt::format("path_match_policy {} is invalid", path_template)); + } + } + + if (path_rewrite_policy_.enabled()) { + // TODO: validate path_rewrite_policy + if (!matching::PatternTemplateMatchPredicate::is_valid_match_rewrite(path_template).ok()) { + throw EnvoyException(fmt::format("path_rewrite_policy {} is invalid", path_template)); + } } if (!prefix_rewrite_.empty()) { @@ -1189,46 +1197,23 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( return InternalRedirectPolicyImpl(policy_config, validator, current_route_name); } PathRewritePolicyImpl -RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::RouteAction route) const { +RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::Route route, + ProtobufMessage::ValidationVisitor& validator) const { if (route.has_path_rewrite_policy()) { - if (!matching::PatternTemplateMatchPredicate::is_valid_rewrite_pattern(path_template, - path_template_rewrite) - .ok()) { - throw EnvoyException( - fmt::format("mismatch between path_match_policy {} and path_rewrite_policy {}", - path_template, path_template_rewrite)); - } - return PathRewritePolicyImpl(route.path_match_policy()); + return PathRewritePolicyImpl(route.path_rewrite_policy(), validator, route.name()); } - return PathRewritePolicyImpl(path_template, path_template_rewrite);; + return PathRewritePolicyImpl(); } } -// TODO -PathRewritePolicyImpl -RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::RouteAction route, - ProtobufMessage::ValidationVisitor& validator) const { - // match + rewrite - if (route.has_path_match_policy()) { - if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { - throw EnvoyException(fmt::format("path_match_policy {} is invalid", path_template)); - } - return PathMatchPolicyImpl(route.path_match_policy()); - } - return PathMatchPolicyImpl(); -} - // TODO PathMatchPolicyImpl -RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::RouteAction route, +RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { - // match + rewrite if (route.has_path_match_policy()) { - if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { - throw EnvoyException(fmt::format("path_match_policy {} is invalid", path_template)); - } - return PathMatchPolicyImpl(route.path_match_policy()); + return PathMatchPolicyImpl(route.path_match_policy(), validator, route.name()); } + return PathMatchPolicyImpl(); } diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 3239787b6a74f..a8fde149e9749 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -34,7 +34,7 @@ #include "source/common/router/tls_context_match_criteria_impl.h" #include "source/common/stats/symbol_table.h" -#include "source/extensions/pattern_template/match/pattern_template_match.h" +#include "source/extensions/path/match/pattern_template/pattern_template_match.h" #include "absl/container/node_hash_map.h" #include "absl/types/optional.h" @@ -461,9 +461,8 @@ class RouteTracingImpl : public RouteTracing { */ class PathMatchPolicyImpl : public PathMatchPolicy { public: - // Constructor that enables internal redirect with policy_config controlling the configurable - // behaviors. - PathMatchPolicyImpl(std::string url_pattern); + PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, std::string url_pattern, + ProtobufMessage::ValidationVisitor& validator); // Default constructor that disables internal redirect. PathMatchPolicyImpl(); @@ -476,6 +475,7 @@ class PathMatchPolicyImpl : public PathMatchPolicy { private: const bool enabled_; + ProtobufTypes::MessagePtr predicate_config_; PathMatchPredicateFactory* predicate_factory_; }; @@ -486,6 +486,8 @@ class PathMatchPolicyImpl : public PathMatchPolicy { */ class PathRewritePolicyImpl : public PathRewritePolicy { public: + PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, + ProtobufMessage::ValidationVisitor& validator, std::string url_pattern,, std::string url_rewrite_pattern); // Constructor that enables internal redirect with policy_config controlling the configurable // behaviors. PathRewritePolicyImpl(std::string url_pattern, std::string url_rewrite_pattern); @@ -901,6 +903,7 @@ class RouteEntryImplBase : public RouteEntry, Regex::CompiledMatcherPtr regex_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_redirect_; const PathMatchPolicyImpl path_match_policy_; + const PathRewritePolicyImpl path_rewrite_policy_; std::string regex_rewrite_substitution_; std::string regex_rewrite_redirect_substitution_; const std::string host_rewrite_; @@ -986,8 +989,8 @@ class RouteEntryImplBase : public RouteEntry, buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const; - PathMatchPolicyImpl - buildPathRewritePolicy(envoy::config::route::v3::RouteAction route_action, + PathRewritePolicyImpl + buildPathRewritePolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const; RouteConstSharedPtr pickClusterViaClusterHeader(const Http::LowerCaseString& cluster_header_name, diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD index 70b93aa68b73b..31039a640b208 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/pattern_template/BUILD @@ -21,8 +21,8 @@ envoy_cc_library( ], deps = [ "//envoy/router:router_path_match_policy_interface", - "//source/extensions/pattern_template:pattern_template_lib", - "@envoy_api//envoy/extensions/pattern_template/match/v3:pkg_cc_proto", + "//source/extensions/path/pattern_template_lib:pattern_template_lib", + "@envoy_api//envoy/extensions/path/match/v3:pkg_cc_proto", ], ) @@ -35,7 +35,7 @@ envoy_cc_extension( ":pattern_template_match_lib", "//envoy/registry", "//envoy/router:router_path_match_policy_interface", - "//source/extensions/pattern_template:pattern_template_lib", - "@envoy_api//envoy/extensions/pattern_template/match/v3:pkg_cc_proto", + "//source/extensions/path/pattern_template_lib:pattern_template_lib", + "@envoy_api//envoy/extensions/path/match/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/match/pattern_template/config.cc b/source/extensions/path/match/pattern_template/config.cc index f988811c1c0fc..11e5c954f8c81 100644 --- a/source/extensions/path/match/pattern_template/config.cc +++ b/source/extensions/path/match/pattern_template/config.cc @@ -1,4 +1,4 @@ -#include "source/extensions/pattern_template/match/config.h" +#include "source/extensions/path/match/pattern_template/config.h" #include "envoy/registry/registry.h" #include "envoy/router/path_match_policy.h" diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 5735e7419f4b5..3a8c46298850c 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -1,9 +1,9 @@ #pragma once -#include "envoy/extensions/pattern_template/match/v3/pattern_template_match.pb.h" +#include "envoy/extensions/path/match/v3/pattern_template_match.pb.h" #include "envoy/router/path_match_policy.h" -#include "source/extensions/pattern_template/match/pattern_template_match.h" +#include "source/extensions/path/match/pattern_template/pattern_template_match.h" namespace Envoy { namespace Extensions { @@ -13,12 +13,12 @@ namespace Match { class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFactory { public: Router::PathMatchPredicateSharedPtr - createPathMatchPredicate(std::string url_pattern) override { - return std::make_shared(url_pattern); + createPathMatchPredicate(const Protobuf::Message& config, std::string url_pattern) override { + return std::make_shared(config, url_pattern); } ProtobufTypes::MessagePtr createEmptyConfigProto() override { - return std::make_unique(); + return std::make_unique(); } std::string name() const override { return "envoy.path_match_policy.pattern_template_match_predicate"; } diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.cc b/source/extensions/path/match/pattern_template/pattern_template_match.cc index fb3f675a6d665..ba9f5acc23d1d 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.cc +++ b/source/extensions/path/match/pattern_template/pattern_template_match.cc @@ -1,4 +1,4 @@ -#include "source/extensions/pattern_template/pattern_template.h" +#include "source/extensions/path/match/pattern_template/pattern_template_match.h" #include #include @@ -6,7 +6,6 @@ #include #include "source/common/http/path_utility.h" -#include "source/extensions/pattern_template/match/pattern_template_match.h" #include "absl/status/statusor.h" #include "absl/strings/str_split.h" diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index 21cdb37d64156..c0eb8b3f7da78 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -1,12 +1,12 @@ -#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H -#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H +#ifndef SOURCE_EXTENSIONS_PATH_MATCH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H +#define SOURCE_EXTENSIONS_PATH_MATCH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H #include -#include "envoy/extensions/pattern_template/match/v3/pattern_template_match.pb.h" +#include "envoy/extensions/path/match/v3/pattern_template_match.pb.h" #include "envoy/router/path_match_policy.h" -#include "source/extensions/pattern_template/pattern_template.h" +#include "source/extensions/path/pattern_template_lib/pattern_template.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" @@ -18,7 +18,7 @@ namespace Match { class PatternTemplateMatchPredicate : public Router::PathMatchPredicate { public: - explicit PatternTemplateMatchPredicate(std::string url_pattern) + explicit PatternTemplateMatchPredicate(const Protobuf::Message&, std::string url_pattern) : matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())) {} absl::string_view name() const override { @@ -36,4 +36,4 @@ class PatternTemplateMatchPredicate : public Router::PathMatchPredicate { } // namespace Extensions } // namespace Envoy -#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H +#endif // SOURCE_EXTENSIONS_PATH_MATCH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H diff --git a/source/extensions/path/pattern_template_lib/BUILD b/source/extensions/path/pattern_template_lib/BUILD index e954c40872c33..7ef6cf20b1b40 100644 --- a/source/extensions/path/pattern_template_lib/BUILD +++ b/source/extensions/path/pattern_template_lib/BUILD @@ -19,20 +19,20 @@ envoy_cc_library( hdrs = ["pattern_template.h"], visibility = [ "//source/common/router:__subpackages__", - "//source/extensions/pattern_template:__subpackages__", - "//test/extensions/pattern_template:__subpackages__", + "//source/extensions/path:__subpackages__", + "//test/extensions/path:__subpackages__", ], deps = [ ":pattern_template_internal_cc", "//envoy/router:router_path_match_policy_interface", "//source/common/http:path_utility_lib", - "//source/extensions/pattern_template/proto:pattern_template_rewrite_segements_cc_proto", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_googlesource_code_re2//:re2", - "@envoy_api//envoy/extensions/pattern_template/match/v3:pkg_cc_proto", - "@envoy_api//envoy/extensions/pattern_template/rewrite/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/match/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/rewrite/v3:pkg_cc_proto", ], ) @@ -41,7 +41,7 @@ envoy_cc_library( srcs = ["pattern_template_internal.cc"], hdrs = ["pattern_template_internal.h"], deps = [ - "//source/extensions/pattern_template/proto:pattern_template_rewrite_segements_cc_proto", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/flags:flag", "@com_google_absl//absl/functional:function_ref", @@ -59,7 +59,7 @@ envoy_cc_test( srcs = ["pattern_template_test.cc"], deps = [ ":pattern_template", - "//source/extensions/pattern_template/proto:pattern_template_rewrite_segements_cc_proto", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", @@ -72,7 +72,7 @@ envoy_cc_test( srcs = ["pattern_template_internal_test.cc"], deps = [ ":pattern_template_internal_cc", - "//source/extensions/pattern_template/proto:pattern_template_rewrite_segements_cc_proto", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc index 033d14173b9e0..9d9237d52fb7c 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -1,14 +1,14 @@ -#include "source/extensions/pattern_template/pattern_template.h" +#include "source/extensions/path/pattern_template_lib/pattern_template.h" #include #include #include #include -#include "source/extensions/pattern_template/proto/pattern_template_rewrite_segments.pb.h" +#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" #include "source/common/http/path_utility.h" -#include "source/extensions/pattern_template/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" #include "absl/status/statusor.h" #include "absl/strings/str_split.h" diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 1df2bd9937cfb..cbb72340e2138 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -1,13 +1,13 @@ -#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H -#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H +#ifndef SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H +#define SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H #include -#include "source/extensions/pattern_template/proto/pattern_template_rewrite_segments.pb.h" +#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" #include "envoy/router/path_match_policy.h" -#include "source/extensions/pattern_template/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" @@ -55,4 +55,4 @@ absl::Status isValidMatchPattern(std::string match_pattern); } // namespace Extensions } // namespace Envoy -#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H +#endif // SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc index 90d79d3e593b5..889eb5834fdfd 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc @@ -1,4 +1,4 @@ -#include "source/extensions/pattern_template/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" #include #include diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h index 795cb14a91cce..f53e07b507080 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.h +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.h @@ -1,5 +1,5 @@ -#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H -#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H +#ifndef SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H +#define SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H #include #include @@ -95,4 +95,4 @@ inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.dat } // namespace Extensions } // namespace Envoy -#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H +#endif // SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc index c9bb23faf199a..55c0124b62ef3 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc @@ -7,7 +7,7 @@ #include "source/common/common/assert.h" #include "source/common/protobuf/protobuf.h" -#include "source/extensions/pattern_template/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" #include "test/test_common/logging.h" #include "test/test_common/status_utility.h" diff --git a/source/extensions/path/pattern_template_lib/pattern_template_test.cc b/source/extensions/path/pattern_template_lib/pattern_template_test.cc index dc97c4469bebe..fe1cd1dc6105b 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_test.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template_test.cc @@ -4,8 +4,8 @@ #include "source/common/common/assert.h" #include "source/common/protobuf/protobuf.h" -#include "source/extensions/pattern_template/pattern_template.h" -#include "source/extensions/pattern_template/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/pattern_template.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" #include "test/test_common/logging.h" #include "test/test_common/status_utility.h" diff --git a/source/extensions/path/pattern_template/proto/pattern_template_rewrite_segments.proto b/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto similarity index 100% rename from source/extensions/path/pattern_template/proto/pattern_template_rewrite_segments.proto rename to source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto diff --git a/source/extensions/path/rewrite/pattern_template/BUILD b/source/extensions/path/rewrite/pattern_template/BUILD index 2dbde5a933c12..d692a6f93ccc8 100644 --- a/source/extensions/path/rewrite/pattern_template/BUILD +++ b/source/extensions/path/rewrite/pattern_template/BUILD @@ -21,8 +21,8 @@ envoy_cc_library( ], deps = [ "//envoy/router:router_path_rewrite_policy_interface", - "//source/extensions/pattern_template:pattern_template_lib", - "@envoy_api//envoy/extensions/pattern_template/rewrite/v3:pkg_cc_proto", + "//source/extensions/path/pattern_template_lib:pattern_template_lib", + "@envoy_api//envoy/extensions/path/rewrite/v3:pkg_cc_proto", ], ) @@ -35,7 +35,7 @@ envoy_cc_extension( ":pattern_template_rewrite_lib", "//envoy/registry", "//envoy/router:router_path_rewrite_policy_interface", - "//source/extensions/pattern_template:pattern_template_lib", - "@envoy_api//envoy/extensions/pattern_template/rewrite/v3:pkg_cc_proto", + "//source/extensions/path/pattern_template_lib:pattern_template_lib", + "@envoy_api//envoy/extensions/path/rewrite/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/rewrite/pattern_template/config.cc b/source/extensions/path/rewrite/pattern_template/config.cc index ed52e4db888b5..81293711457d9 100644 --- a/source/extensions/path/rewrite/pattern_template/config.cc +++ b/source/extensions/path/rewrite/pattern_template/config.cc @@ -1,4 +1,4 @@ -#include "source/extensions/pattern_template/rewrite/config.h" +#include "source/extensions/path/rewrite/pattern_template/config.h" #include "envoy/registry/registry.h" #include "envoy/router/path_rewrite_policy.h" diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 6d22b20c7f307..58c93d7b56c48 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -1,9 +1,9 @@ #pragma once -#include "envoy/extensions/pattern_template/rewrite/v3/pattern_template_rewrite.pb.h" +#include "envoy/extensions/path/rewrite/v3/pattern_template_rewrite.pb.h" #include "envoy/router/path_rewrite_policy.h" -#include "source/extensions/pattern_template/rewrite/pattern_template_rewrite.h" +#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" namespace Envoy { namespace Extensions { @@ -13,12 +13,12 @@ namespace Rewrite { class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredicateFactory { public: Router::PathRewritePredicateSharedPtr - createPathRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) override { + createPathRewritePredicate(const Protobuf::Message&, std::string url_pattern, std::string url_rewrite_pattern) override { return std::make_shared(url_pattern, url_rewrite_pattern); } ProtobufTypes::MessagePtr createEmptyConfigProto() override { - return std::make_unique(); + return std::make_unique(); } std::string name() const override { return "envoy.path_rewrite_policy.pattern_template_rewrite_predicate"; } diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index 6bf9f36fd211c..188cd54283fa7 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -1,14 +1,14 @@ -#include "source/extensions/pattern_template/rewrite/pattern_template_rewrite.h" +#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" #include #include #include #include -#include "source/extensions/pattern_template/proto/pattern_template_rewrite_segments.pb.h" +#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" #include "source/common/http/path_utility.h" -#include "source/extensions/pattern_template/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" #include "absl/status/statusor.h" #include "absl/strings/str_split.h" diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 8d06a387fa3cf..1192d8114b0cf 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -1,11 +1,11 @@ -#ifndef SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_REWRITE_H -#define SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_REWRITE_H +#ifndef SOURCE_EXTENSIONS_PATH_REWRITE_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H +#define SOURCE_EXTENSIONS_PATH_REWRITE_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H #include #include "envoy/router/path_rewrite_policy.h" -#include "source/extensions/pattern_template/pattern_template.h" +#include "source/extensions/path/pattern_template_lib/pattern_template.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" @@ -48,4 +48,4 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { } // namespace Extensions } // namespace Envoy -#endif // SOURCE_EXTENSIONS_PATTERN_TEMPLATE_PATTERN_TEMPLATE_REWRITE_H +#endif // SOURCE_EXTENSIONS_PATH_REWRITE_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H From 659898d314b88832604cef66fbfcc329e743faa8 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 26 Jul 2022 14:15:40 +0000 Subject: [PATCH 071/238] Add comments Signed-off-by: silverstar195 --- api/envoy/config/route/v3/route_components.proto | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index 75d145d1f4a91..27853c04d05d8 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -572,6 +572,7 @@ message RouteMatch { // Expect the value to not contain ``?`` or ``#`` and not to end in ``/`` string path_separated_prefix = 14 [(validate.rules).string = {pattern: "^[^?#]+[^?#/]$"}]; + // [#not-implemented-hide:] // [#comment: TODO(silverstar195): Hook into extension once added] core.v3.TypedExtensionConfig path_match_policy = 15; } @@ -1019,9 +1020,9 @@ message RouteAction { // place the original path before rewrite into the :ref:`x-envoy-original-path // ` header. // - // Only one of :ref:`regex_rewrite `, - // :ref:`path_rewrite_policy ` - // or :ref:`prefix_rewrite ` may be specified. + // Only one of :ref:`regex_rewrite ` or + // [#comment:TODO(silverstar194) add the following once implemented: :ref:`path_rewrite_policy `] + // :ref:`prefix_rewrite ` may be specified. // // .. attention:: // @@ -1056,8 +1057,9 @@ message RouteAction { // before the rewrite into the :ref:`x-envoy-original-path // ` header. // - // Only one of :ref:`regex_rewrite `, :ref:`prefix_rewrite `, - // or :ref:`path_rewrite_policy ` + // Only one of :ref:`regex_rewrite ` + // or :ref:`prefix_rewrite ` + // [#comment:TODO(silverstar194) add the following once implemented: :ref:`path_rewrite_policy `] // may be specified. // // Examples using Google's `RE2 `_ engine: @@ -1078,6 +1080,7 @@ message RouteAction { // ``/aaa/yyy/bbb``. type.matcher.v3.RegexMatchAndSubstitute regex_rewrite = 32; + // [#not-implemented-hide:] // [#comment: TODO(silverstar195): Hook into extension once added] core.v3.TypedExtensionConfig path_rewrite_policy = 41; From 989642ff46f4bbe0a7196f1f8e2e83c4930af976 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 26 Jul 2022 14:24:54 +0000 Subject: [PATCH 072/238] Changed path Signed-off-by: silverstar195 --- api/BUILD | 4 ++-- .../extensions/path/match/{ => pattern_template}/v3/BUILD | 0 .../{ => pattern_template}/v3/pattern_template_match.proto | 0 .../extensions/path/rewrite/{ => pattern_template}/v3/BUILD | 0 .../{ => pattern_template}/v3/pattern_template_rewrite.proto | 0 api/versioning/BUILD | 4 ++-- 6 files changed, 4 insertions(+), 4 deletions(-) rename api/envoy/extensions/path/match/{ => pattern_template}/v3/BUILD (100%) rename api/envoy/extensions/path/match/{ => pattern_template}/v3/pattern_template_match.proto (100%) rename api/envoy/extensions/path/rewrite/{ => pattern_template}/v3/BUILD (100%) rename api/envoy/extensions/path/rewrite/{ => pattern_template}/v3/pattern_template_rewrite.proto (100%) diff --git a/api/BUILD b/api/BUILD index f363fbca13504..a8a01964eff9f 100644 --- a/api/BUILD +++ b/api/BUILD @@ -241,8 +241,8 @@ proto_library( "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", - "//envoy/extensions/path/match/v3:pkg", - "//envoy/extensions/path/rewrite/v3:pkg", + "//envoy/extensions/path/match/pattern_template/v3:pkg", + "//envoy/extensions/path/rewrite/pattern_template/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", diff --git a/api/envoy/extensions/path/match/v3/BUILD b/api/envoy/extensions/path/match/pattern_template/v3/BUILD similarity index 100% rename from api/envoy/extensions/path/match/v3/BUILD rename to api/envoy/extensions/path/match/pattern_template/v3/BUILD diff --git a/api/envoy/extensions/path/match/v3/pattern_template_match.proto b/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto similarity index 100% rename from api/envoy/extensions/path/match/v3/pattern_template_match.proto rename to api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto diff --git a/api/envoy/extensions/path/rewrite/v3/BUILD b/api/envoy/extensions/path/rewrite/pattern_template/v3/BUILD similarity index 100% rename from api/envoy/extensions/path/rewrite/v3/BUILD rename to api/envoy/extensions/path/rewrite/pattern_template/v3/BUILD diff --git a/api/envoy/extensions/path/rewrite/v3/pattern_template_rewrite.proto b/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto similarity index 100% rename from api/envoy/extensions/path/rewrite/v3/pattern_template_rewrite.proto rename to api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto diff --git a/api/versioning/BUILD b/api/versioning/BUILD index 8874ccf835eaa..04ef2593f5397 100644 --- a/api/versioning/BUILD +++ b/api/versioning/BUILD @@ -183,8 +183,8 @@ proto_library( "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", - "//envoy/extensions/path/match/v3:pkg", - "//envoy/extensions/path/rewrite/v3:pkg", + "//envoy/extensions/path/match/pattern_template/v3:pkg", + "//envoy/extensions/path/rewrite/pattern_template/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", From b1495262ecf926b00eafff7259e2642efecc0f51 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 27 Jul 2022 13:11:10 +0000 Subject: [PATCH 073/238] Merge in main Signed-off-by: silverstar195 --- api/envoy/extensions/url_template/v3/BUILD | 12 ------- .../v3/route_url_rewrite_pattern.proto | 32 ------------------- 2 files changed, 44 deletions(-) delete mode 100644 api/envoy/extensions/url_template/v3/BUILD delete mode 100644 api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto diff --git a/api/envoy/extensions/url_template/v3/BUILD b/api/envoy/extensions/url_template/v3/BUILD deleted file mode 100644 index 1c1a6f6b44235..0000000000000 --- a/api/envoy/extensions/url_template/v3/BUILD +++ /dev/null @@ -1,12 +0,0 @@ -# DO NOT EDIT. This file is generated by tools/proto_format/proto_sync.py. - -load("@envoy_api//bazel:api_build_system.bzl", "api_proto_package") - -licenses(["notice"]) # Apache 2 - -api_proto_package( - deps = [ - "//envoy/config/core/v3:pkg", - "@com_github_cncf_udpa//udpa/annotations:pkg", - ], -) diff --git a/api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto b/api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto deleted file mode 100644 index e58e123373580..0000000000000 --- a/api/envoy/extensions/url_template/v3/route_url_rewrite_pattern.proto +++ /dev/null @@ -1,32 +0,0 @@ -syntax = "proto3"; - -package envoy.extensions.url_template.v3; - -import "udpa/annotations/status.proto"; - -option java_package = "io.envoyproxy.envoy.extensions.matching.pattern_matcher.v3"; -option java_outer_classname = "RouteUrlRewritePatternProto"; -option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/url_template/v3;urltemplatev3"; -option (udpa.annotations.file_status).package_version_status = ACTIVE; - -// [#protodoc-title: Pattern matcher] -// [#extension: envoy.matching.pattern_matcher] - -// Holds the segments for rewriting urls base on pattern templates -message RouteUrlRewritePattern { - message RewriteSegment { - oneof segment_type { - // Represents a segment of the rewritten URL, including any path segments, - // slash and prefix. - string literal = 1; - - // Represents an index into the RE2 capture which value should be used - // to construct the rewritten URL. Note that the index should be greater - // than 0 as 0 index into the whole match RE2 pattern. - int32 var_index = 2; - } - } - - repeated RewriteSegment segments = 3; -} \ No newline at end of file From f25cbc022cbfafdee4f591f641996379911ec664 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 27 Jul 2022 14:39:16 +0000 Subject: [PATCH 074/238] Simple changes Signed-off-by: silverstar195 --- api/BUILD | 1 - source/common/router/BUILD | 4 -- source/common/router/config_impl.cc | 56 ++++++++++++------- source/common/router/config_impl.h | 21 +++---- .../path/match/pattern_template/BUILD | 6 +- .../path/match/pattern_template/config.h | 2 +- .../pattern_template/pattern_template_match.h | 2 +- .../path/pattern_template_lib/BUILD | 4 +- .../path/rewrite/pattern_template/BUILD | 4 +- .../path/rewrite/pattern_template/config.h | 6 +- 10 files changed, 54 insertions(+), 52 deletions(-) diff --git a/api/BUILD b/api/BUILD index 4c42dcdd659c8..a8a01964eff9f 100644 --- a/api/BUILD +++ b/api/BUILD @@ -274,7 +274,6 @@ proto_library( "//envoy/extensions/upstreams/http/tcp/v3:pkg", "//envoy/extensions/upstreams/http/v3:pkg", "//envoy/extensions/upstreams/tcp/generic/v3:pkg", - "//envoy/extensions/url_template/v3:pkg", "//envoy/extensions/wasm/v3:pkg", "//envoy/extensions/watchdog/profile_action/v3:pkg", "//envoy/service/accesslog/v3:pkg", diff --git a/source/common/router/BUILD b/source/common/router/BUILD index c73db31576787..3d04806fd7328 100644 --- a/source/common/router/BUILD +++ b/source/common/router/BUILD @@ -70,10 +70,6 @@ envoy_cc_library( "//source/common/tracing:http_tracer_lib", "//source/common/upstream:retry_factory_lib", "//source/extensions/early_data:default_early_data_policy_lib", - "//source/extensions/path/match/pattern_template:config", - "//source/extensions/path/match/pattern_template:pattern_template_match_lib", - "//source/extensions/path/rewrite/pattern_template:config", - "//source/extensions/path/rewrite/pattern_template:pattern_template_rewrite_lib", "@envoy_api//envoy/config/common/matcher/v3:pkg_cc_proto", "@envoy_api//envoy/config/core/v3:pkg_cc_proto", "@envoy_api//envoy/config/route/v3:pkg_cc_proto", diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 5059d8fefc36c..72f3f82bbb2b6 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -48,11 +48,6 @@ #include "source/common/tracing/http_tracer_impl.h" #include "source/common/upstream/retry_factory.h" #include "source/extensions/early_data/default_early_data_policy.h" -#include "source/extensions/path/match/pattern_template/pattern_template_match.h" -#include "source/extensions/path/match/pattern_template/config.h" -#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" -#include "source/extensions/path/rewrite/pattern_template/config.h" - #include "absl/strings/match.h" @@ -358,11 +353,31 @@ std::vector InternalRedirectPolicyImpl::pred } return predicates; } +PathRewritePolicyImpl::PathRewritePolicyImpl() : enabled_(false){}; + +PathRewritePolicyImpl::PathRewritePolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, + ProtobufMessage::ValidationVisitor& validator, std::string curr_url) + : curr_url_(curr_url), enabled_(true) { + + absl::string_view name = "envoy.path_match_policy.pattern_template_rewrite_predicate"; + auto* factory = Registry::FactoryRegistry::getFactory(name); + ASSERT(factory); // factory not found + ProtobufTypes::MessagePtr proto_config = factory->createEmptyConfigProto(); + Envoy::Config::Utility::translateOpaqueConfig(typed_config.typed_config(), validator, *proto_config); + + predicate_factory_ = factory; + predicate_config_ = std::move(proto_config); +} + +PathRewritePredicateSharedPtr PathRewritePolicyImpl::predicate() const { + return predicate_factory_->createPathRewritePredicate(*predicate_config_, curr_url_); +} PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; -PathMatchPolicyImpl::PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, std::string url_pattern, - ProtobufMessage::ValidationVisitor& validator) +PathMatchPolicyImpl::PathMatchPolicyImpl( + const envoy::config::core::v3::TypedExtensionConfig typed_config, + ProtobufMessage::ValidationVisitor& validator, std::string url_pattern) : url_pattern_(url_pattern), enabled_(true) { absl::string_view name = "envoy.path_match_policy.pattern_template_match_predicate"; @@ -681,16 +696,16 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, } // TODO: validate path_match_policy - if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { - throw EnvoyException(fmt::format("path_match_policy {} is invalid", path_template)); - } +// if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { +// throw EnvoyException(fmt::format("path_match_policy {} is invalid", path_template)); +// } } if (path_rewrite_policy_.enabled()) { // TODO: validate path_rewrite_policy - if (!matching::PatternTemplateMatchPredicate::is_valid_match_rewrite(path_template).ok()) { - throw EnvoyException(fmt::format("path_rewrite_policy {} is invalid", path_template)); - } +// if (!matching::PatternTemplateMatchPredicate::is_valid_match_rewrite(path_template).ok()) { +// throw EnvoyException(fmt::format("path_rewrite_policy {} is invalid", path_template)); +// } } if (!prefix_rewrite_.empty()) { @@ -1001,9 +1016,9 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa } // complete pattern rewrite - if (path_match_policy_.enabled()) { + if (path_rewrite_policy_.enabled()) { auto just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); - return *std::move(path_match_policy_.predicate()->rewritePattern(just_path, matched_path)); + return *std::move(path_rewrite_policy_.predicate()->rewritePattern(just_path, matched_path)); } // There are no rewrites configured. @@ -1199,19 +1214,18 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( PathRewritePolicyImpl RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { - if (route.has_path_rewrite_policy()) { - return PathRewritePolicyImpl(route.path_rewrite_policy(), validator, route.name()); + if (route.route().has_path_rewrite_policy()) { + return PathRewritePolicyImpl(route.route().path_rewrite_policy(), validator, route.name()); } return PathRewritePolicyImpl(); } } // TODO -PathMatchPolicyImpl -RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::Route route, +PathMatchPolicyImpl RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { - if (route.has_path_match_policy()) { - return PathMatchPolicyImpl(route.path_match_policy(), validator, route.name()); + if (route.match().has_path_match_policy()) { + return PathMatchPolicyImpl(route.match().path_match_policy(), validator, route.name()); } return PathMatchPolicyImpl(); diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index ad2f96a5818ae..efc8a6427202b 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -34,8 +34,6 @@ #include "source/common/router/tls_context_match_criteria_impl.h" #include "source/common/stats/symbol_table.h" -#include "source/extensions/path/match/pattern_template/pattern_template_match.h" - #include "absl/container/node_hash_map.h" #include "absl/types/optional.h" @@ -456,13 +454,12 @@ class RouteTracingImpl : public RouteTracing { }; /** - * Implementation of InternalRedirectPolicy that reads from the proto - * InternalRedirectPolicy of the RouteAction. + * Implementation of PathMatchPolicy that reads from the proto + * PathMatchPolicy of the RouteAction. */ class PathMatchPolicyImpl : public PathMatchPolicy { public: - PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, std::string url_pattern, - ProtobufMessage::ValidationVisitor& validator); + PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, ProtobufMessage::ValidationVisitor& validator, std::string url_pattern); // Default constructor that disables internal redirect. PathMatchPolicyImpl(); @@ -479,18 +476,14 @@ class PathMatchPolicyImpl : public PathMatchPolicy { PathMatchPredicateFactory* predicate_factory_; }; - /** * Implementation of InternalRedirectPolicy that reads from the proto * InternalRedirectPolicy of the RouteAction. */ class PathRewritePolicyImpl : public PathRewritePolicy { public: - PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string url_pattern,, std::string url_rewrite_pattern); - // Constructor that enables internal redirect with policy_config controlling the configurable - // behaviors. - PathRewritePolicyImpl(std::string url_pattern, std::string url_rewrite_pattern); + PathRewritePolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, + ProtobufMessage::ValidationVisitor& validator, std::string curr_url); // Default constructor that disables internal redirect. PathRewritePolicyImpl(); @@ -499,11 +492,11 @@ class PathRewritePolicyImpl : public PathRewritePolicy { PathRewritePredicateSharedPtr predicate() const override; - const std::string url_pattern_; - const std::string url_rewrite_pattern_; + const std::string curr_url_; private: const bool enabled_; + ProtobufTypes::MessagePtr predicate_config_; PathRewritePredicateFactory* predicate_factory_; }; diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD index 31039a640b208..4c53f40c88e2f 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/pattern_template/BUILD @@ -22,7 +22,7 @@ envoy_cc_library( deps = [ "//envoy/router:router_path_match_policy_interface", "//source/extensions/path/pattern_template_lib:pattern_template_lib", - "@envoy_api//envoy/extensions/path/match/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", ], ) @@ -35,7 +35,7 @@ envoy_cc_extension( ":pattern_template_match_lib", "//envoy/registry", "//envoy/router:router_path_match_policy_interface", - "//source/extensions/path/pattern_template_lib:pattern_template_lib", - "@envoy_api//envoy/extensions/path/match/v3:pkg_cc_proto", + "//source/extensions/path/pattern_template_lib", + "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 3a8c46298850c..5146668f38468 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -1,6 +1,6 @@ #pragma once -#include "envoy/extensions/path/match/v3/pattern_template_match.pb.h" +#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" #include "envoy/router/path_match_policy.h" #include "source/extensions/path/match/pattern_template/pattern_template_match.h" diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index c0eb8b3f7da78..9267c035a9184 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -3,7 +3,7 @@ #include -#include "envoy/extensions/path/match/v3/pattern_template_match.pb.h" +#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" #include "envoy/router/path_match_policy.h" #include "source/extensions/path/pattern_template_lib/pattern_template.h" diff --git a/source/extensions/path/pattern_template_lib/BUILD b/source/extensions/path/pattern_template_lib/BUILD index 7ef6cf20b1b40..06819f84c52ef 100644 --- a/source/extensions/path/pattern_template_lib/BUILD +++ b/source/extensions/path/pattern_template_lib/BUILD @@ -31,8 +31,8 @@ envoy_cc_library( "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_googlesource_code_re2//:re2", - "@envoy_api//envoy/extensions/path/match/v3:pkg_cc_proto", - "@envoy_api//envoy/extensions/path/rewrite/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/rewrite/pattern_template/BUILD b/source/extensions/path/rewrite/pattern_template/BUILD index d692a6f93ccc8..cf526a8bdda15 100644 --- a/source/extensions/path/rewrite/pattern_template/BUILD +++ b/source/extensions/path/rewrite/pattern_template/BUILD @@ -22,7 +22,7 @@ envoy_cc_library( deps = [ "//envoy/router:router_path_rewrite_policy_interface", "//source/extensions/path/pattern_template_lib:pattern_template_lib", - "@envoy_api//envoy/extensions/path/rewrite/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", ], ) @@ -36,6 +36,6 @@ envoy_cc_extension( "//envoy/registry", "//envoy/router:router_path_rewrite_policy_interface", "//source/extensions/path/pattern_template_lib:pattern_template_lib", - "@envoy_api//envoy/extensions/path/rewrite/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 58c93d7b56c48..9d55478232517 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -1,6 +1,6 @@ #pragma once -#include "envoy/extensions/path/rewrite/v3/pattern_template_rewrite.pb.h" +#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" #include "envoy/router/path_rewrite_policy.h" #include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" @@ -13,8 +13,8 @@ namespace Rewrite { class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredicateFactory { public: Router::PathRewritePredicateSharedPtr - createPathRewritePredicate(const Protobuf::Message&, std::string url_pattern, std::string url_rewrite_pattern) override { - return std::make_shared(url_pattern, url_rewrite_pattern); + createPathRewritePredicate(const Protobuf::Message&, std::string url_pattern) override { + return std::make_shared(url_pattern, ""); } ProtobufTypes::MessagePtr createEmptyConfigProto() override { From c1baccc4fc2314ef052be270e94982fbe427a9e4 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 28 Jul 2022 19:25:10 +0000 Subject: [PATCH 075/238] Add and pass tests Signed-off-by: silverstar195 --- envoy/router/path_match_policy.h | 7 +- envoy/router/path_rewrite_policy.h | 10 +- envoy/router/router.h | 2 + source/common/http/async_client_impl.cc | 1 + source/common/http/async_client_impl.h | 4 + source/common/router/BUILD | 2 + source/common/router/config_impl.cc | 59 +++-- source/common/router/config_impl.h | 26 ++- source/extensions/extensions_build_config.bzl | 7 + source/extensions/extensions_metadata.yaml | 8 +- .../path/match/pattern_template/BUILD | 4 +- .../path/match/pattern_template/config.h | 17 +- .../pattern_template_match.cc | 7 +- .../pattern_template/pattern_template_match.h | 19 +- .../pattern_template_lib/pattern_template.cc | 13 +- .../pattern_template_lib/pattern_template.h | 4 +- .../path/rewrite/pattern_template/BUILD | 6 +- .../path/rewrite/pattern_template/config.h | 13 +- .../pattern_template_rewrite.h | 26 ++- test/common/router/config_impl_test.cc | 210 ++++++++++++++---- .../{pattern_template => path}/BUILD | 8 +- .../match_config_test.cc | 13 +- .../rewrite_config_test.cc | 0 test/mocks/router/mocks.cc | 8 +- test/mocks/router/mocks.h | 26 ++- 25 files changed, 367 insertions(+), 133 deletions(-) rename test/extensions/{pattern_template => path}/BUILD (66%) rename test/extensions/{pattern_template => path}/match_config_test.cc (61%) rename test/extensions/{pattern_template => path}/rewrite_config_test.cc (100%) diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index 8e14c5716e8f2..f0f002fc44b34 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -22,6 +22,8 @@ class PathMatchPredicate : Logger::Loggable { virtual absl::string_view name() const PURE; virtual bool match(absl::string_view pattern) const PURE; + + virtual std::string pattern() const PURE; }; using PathMatchPredicateSharedPtr = std::shared_ptr; @@ -32,10 +34,11 @@ using PathMatchPredicateSharedPtr = std::shared_ptr; class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { public: virtual ~PathMatchPredicateFactory() = default; + virtual PathMatchPredicateSharedPtr - createPathMatchPredicate(const Protobuf::Message& config, std::string url_pattern) PURE; + createPathMatchPredicate(const Protobuf::Message& config) PURE; - std::string category() const override { return "envoy.path_match_policy"; } + std::string category() const override { return "envoy.path.match"; } }; } // namespace Router diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index 396635243e01f..4b1e4d2765fd0 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -23,6 +23,9 @@ class PathRewritePredicate : Logger::Loggable { virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, absl::string_view matched_path) const PURE; + + virtual std::string pattern() const PURE; + }; using PathRewritePredicateSharedPtr = std::shared_ptr; @@ -35,9 +38,12 @@ class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { virtual ~PathRewritePredicateFactory() override = default; virtual PathRewritePredicateSharedPtr - createPathRewritePredicate(const Protobuf::Message& config, std::string url_pattern); + createPathRewritePredicate(const Protobuf::Message& rewrite_config) PURE; + + virtual ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; - std::string category() const override { return "envoy.path_rewrite_policy"; } + virtual std::string name() const override PURE; + virtual std::string category() const override PURE; }; } // namespace Router diff --git a/envoy/router/router.h b/envoy/router/router.h index 4358bf4d25971..8004482fe6062 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -955,6 +955,8 @@ class RouteEntry : public ResponseEntry { virtual const PathMatchPolicy& pathMatchPolicy() const PURE; + virtual const PathRewritePolicy& pathRewritePolicy() const PURE; + /** * @return uint32_t any route cap on bytes which should be buffered for shadowing or retries. * This is an upper bound so does not necessarily reflect the bytes which will be buffered diff --git a/source/common/http/async_client_impl.cc b/source/common/http/async_client_impl.cc index d179d98dfaf42..6199ccb7c7fec 100644 --- a/source/common/http/async_client_impl.cc +++ b/source/common/http/async_client_impl.cc @@ -21,6 +21,7 @@ const AsyncStreamImpl::NullHedgePolicy AsyncStreamImpl::RouteEntryImpl::hedge_po const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::RouteEntryImpl::rate_limit_policy_; const Router::InternalRedirectPolicyImpl AsyncStreamImpl::RouteEntryImpl::internal_redirect_policy_; const Router::PathMatchPolicyImpl AsyncStreamImpl::RouteEntryImpl::path_match_policy_; +const Router::PathRewritePolicyImpl AsyncStreamImpl::RouteEntryImpl::path_rewrite_policy_; const std::vector AsyncStreamImpl::RouteEntryImpl::shadow_policies_; const AsyncStreamImpl::NullVirtualHost AsyncStreamImpl::RouteEntryImpl::virtual_host_; const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::NullVirtualHost::rate_limit_policy_; diff --git a/source/common/http/async_client_impl.h b/source/common/http/async_client_impl.h index 66034a22d3616..48ad78f73939c 100644 --- a/source/common/http/async_client_impl.h +++ b/source/common/http/async_client_impl.h @@ -234,6 +234,9 @@ class AsyncStreamImpl : public AsyncClient::Stream, const Router::PathMatchPolicy& pathMatchPolicy() const override { return path_match_policy_; } + const Router::PathRewritePolicy& pathRewritePolicy() const override { + return path_rewrite_policy_; + } uint32_t retryShadowBufferLimit() const override { return std::numeric_limits::max(); } @@ -298,6 +301,7 @@ class AsyncStreamImpl : public AsyncClient::Stream, static const NullRateLimitPolicy rate_limit_policy_; static const Router::InternalRedirectPolicyImpl internal_redirect_policy_; static const Router::PathMatchPolicyImpl path_match_policy_; + static const Router::PathRewritePolicyImpl path_rewrite_policy_; static const std::vector shadow_policies_; static const NullVirtualHost virtual_host_; static const std::multimap opaque_config_; diff --git a/source/common/router/BUILD b/source/common/router/BUILD index 3d04806fd7328..dbf50ccf2e813 100644 --- a/source/common/router/BUILD +++ b/source/common/router/BUILD @@ -70,6 +70,8 @@ envoy_cc_library( "//source/common/tracing:http_tracer_lib", "//source/common/upstream:retry_factory_lib", "//source/extensions/early_data:default_early_data_policy_lib", + "//source/extensions/path/match/pattern_template:config", + "//source/extensions/path/rewrite/pattern_template:config", "@envoy_api//envoy/config/common/matcher/v3:pkg_cc_proto", "@envoy_api//envoy/config/core/v3:pkg_cc_proto", "@envoy_api//envoy/config/route/v3:pkg_cc_proto", diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 72f3f82bbb2b6..31b9d2c66e79a 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -48,6 +48,8 @@ #include "source/common/tracing/http_tracer_impl.h" #include "source/common/upstream/retry_factory.h" #include "source/extensions/early_data/default_early_data_policy.h" +#include "source/extensions/path/match/pattern_template/pattern_template_match.h" +#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" #include "absl/strings/match.h" @@ -356,31 +358,32 @@ std::vector InternalRedirectPolicyImpl::pred PathRewritePolicyImpl::PathRewritePolicyImpl() : enabled_(false){}; PathRewritePolicyImpl::PathRewritePolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string curr_url) - : curr_url_(curr_url), enabled_(true) { + ProtobufMessage::ValidationVisitor& validator, std::string route_url) + : route_url_(route_url), enabled_(true) { - absl::string_view name = "envoy.path_match_policy.pattern_template_rewrite_predicate"; + absl::string_view name = "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate"; auto* factory = Registry::FactoryRegistry::getFactory(name); ASSERT(factory); // factory not found + ProtobufTypes::MessagePtr proto_config = factory->createEmptyConfigProto(); Envoy::Config::Utility::translateOpaqueConfig(typed_config.typed_config(), validator, *proto_config); predicate_factory_ = factory; - predicate_config_ = std::move(proto_config); + rewrite_predicate_config_ = std::move(proto_config); } PathRewritePredicateSharedPtr PathRewritePolicyImpl::predicate() const { - return predicate_factory_->createPathRewritePredicate(*predicate_config_, curr_url_); + return predicate_factory_->createPathRewritePredicate(*rewrite_predicate_config_); } PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; PathMatchPolicyImpl::PathMatchPolicyImpl( const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string url_pattern) - : url_pattern_(url_pattern), enabled_(true) { + ProtobufMessage::ValidationVisitor& validator, std::string route_url) + : route_url_(route_url), enabled_(true) { - absl::string_view name = "envoy.path_match_policy.pattern_template_match_predicate"; + absl::string_view name = "envoy.path.match.pattern_template.v3.pattern_template_match_predicate"; auto* factory = Registry::FactoryRegistry::getFactory(name); ASSERT(factory); // factory not found ProtobufTypes::MessagePtr proto_config = factory->createEmptyConfigProto(); @@ -391,7 +394,7 @@ PathMatchPolicyImpl::PathMatchPolicyImpl( } PathMatchPredicateSharedPtr PathMatchPolicyImpl::predicate() const { - return predicate_factory_->createPathMatchPredicate(*predicate_config_, url_pattern_); + return predicate_factory_->createPathMatchPredicate(*predicate_config_); } absl::flat_hash_set InternalRedirectPolicyImpl::buildRedirectResponseCodes( @@ -694,18 +697,33 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, if (!prefix_rewrite_.empty() || route.route().has_regex_rewrite()) { throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); } + } - // TODO: validate path_match_policy -// if (!matching::PatternTemplateMatchPredicate::is_valid_match_pattern(path_template).ok()) { -// throw EnvoyException(fmt::format("path_match_policy {} is invalid", path_template)); -// } + if (path_match_policy_.enabled()) { + // TODO: validate path_match_policy is of kind the pattern template + if (!Extensions::PatternTemplate::isValidMatchPattern(path_match_policy_.predicate()->pattern()) + .ok()) { + throw EnvoyException(fmt::format("path_match_policy {} is invalid", + path_match_policy_.predicate()->pattern())); + } } if (path_rewrite_policy_.enabled()) { - // TODO: validate path_rewrite_policy -// if (!matching::PatternTemplateMatchPredicate::is_valid_match_rewrite(path_template).ok()) { -// throw EnvoyException(fmt::format("path_rewrite_policy {} is invalid", path_template)); -// } + // TODO: validate path_rewrite_policy is of kind the pattern template + if (!Extensions::PatternTemplate::isValidPathTemplateRewritePattern( + path_rewrite_policy_.predicate()->pattern()) + .ok()) { + throw EnvoyException(fmt::format("path_rewrite_policy {} is invalid", + path_match_policy_.predicate()->pattern())); + } + + if (!Extensions::PatternTemplate::isValidSharedVariableSet( + path_rewrite_policy_.predicate()->pattern(), path_match_policy_.predicate()->pattern()) + .ok()) { + throw EnvoyException(fmt::format( + "mismatch between path_match_policy {} and path_rewrite_policy {} is invalid", + path_match_policy_.predicate()->pattern(), path_rewrite_policy_.predicate()->pattern())); + } } if (!prefix_rewrite_.empty()) { @@ -1218,7 +1236,6 @@ RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::Route route return PathRewritePolicyImpl(route.route().path_rewrite_policy(), validator, route.name()); } return PathRewritePolicyImpl(); - } } // TODO @@ -1477,15 +1494,15 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { - finalizePathHeader(headers, path_match_policy_.predicate()->url_pattern_, insert_envoy_original_path); + finalizePathHeader(headers, path_match_policy_.predicate()->pattern(), insert_envoy_original_path); } absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { - return currentUrlPathAfterRewriteWithMatchedPath(headers, path_match_policy_.predicate()->url_pattern_); + return currentUrlPathAfterRewriteWithMatchedPath(headers, path_match_policy_.predicate()->pattern()); } -RouteConstSharedPtr PathTemplateRouteEntryImpl::matches(const Http::RequestHeaderMap& headers, +RouteConstSharedPtr PathMatchPolicyRouteEntryImpl::matches(const Http::RequestHeaderMap& headers, const StreamInfo::StreamInfo& stream_info, uint64_t random_value) const { if (RouteEntryImplBase::matchRoute(headers, stream_info, random_value) && diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index efc8a6427202b..f66e80a27cd3d 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -459,7 +459,8 @@ class RouteTracingImpl : public RouteTracing { */ class PathMatchPolicyImpl : public PathMatchPolicy { public: - PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, ProtobufMessage::ValidationVisitor& validator, std::string url_pattern); + PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, + ProtobufMessage::ValidationVisitor& validator, std::string route_url); // Default constructor that disables internal redirect. PathMatchPolicyImpl(); @@ -468,11 +469,13 @@ class PathMatchPolicyImpl : public PathMatchPolicy { PathMatchPredicateSharedPtr predicate() const override; - const std::string url_pattern_; + std::string route_url() const { return route_url_; } + + ProtobufTypes::MessagePtr predicate_config_; private: + const std::string route_url_; const bool enabled_; - ProtobufTypes::MessagePtr predicate_config_; PathMatchPredicateFactory* predicate_factory_; }; @@ -483,7 +486,7 @@ class PathMatchPolicyImpl : public PathMatchPolicy { class PathRewritePolicyImpl : public PathRewritePolicy { public: PathRewritePolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string curr_url); + ProtobufMessage::ValidationVisitor& validator, std::string route_url); // Default constructor that disables internal redirect. PathRewritePolicyImpl(); @@ -492,11 +495,10 @@ class PathRewritePolicyImpl : public PathRewritePolicy { PathRewritePredicateSharedPtr predicate() const override; - const std::string curr_url_; - private: + const std::string route_url_; const bool enabled_; - ProtobufTypes::MessagePtr predicate_config_; + ProtobufTypes::MessagePtr rewrite_predicate_config_; PathRewritePredicateFactory* predicate_factory_; }; @@ -611,9 +613,10 @@ class RouteEntryImplBase : public RouteEntry, const InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } - const PathMatchPolicyImpl& pathMatchPolicy() const override { - return path_match_policy_; - } + + const PathMatchPolicyImpl& pathMatchPolicy() const override { return path_match_policy_; } + const PathRewritePolicyImpl& pathRewritePolicy() const override { return path_rewrite_policy_; } + uint32_t retryShadowBufferLimit() const override { return retry_shadow_buffer_limit_; } const std::vector& shadowPolicies() const override { return shadow_policies_; } const VirtualCluster* virtualCluster(const Http::HeaderMap& headers) const override { @@ -730,6 +733,9 @@ class RouteEntryImplBase : public RouteEntry, const PathMatchPolicyImpl& pathMatchPolicy() const override { return parent_->pathMatchPolicy(); } + const PathRewritePolicyImpl& pathRewritePolicy() const override { + return parent_->pathRewritePolicy(); + } uint32_t retryShadowBufferLimit() const override { return parent_->retryShadowBufferLimit(); } const std::vector& shadowPolicies() const override { return parent_->shadowPolicies(); diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 197e061b3b79e..fdb2b520e936a 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -239,6 +239,13 @@ EXTENSIONS = { "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", + # + # Path match and rewrite + # + + "envoy.path.match.pattern_template.v3.pattern_template_match_predicate": "//source/extensions/path/match/pattern_template:config", + "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate": "//source/extensions/path/rewrite/pattern_template:config", + # # Http Upstreams (excepting envoy.upstreams.http.generic which is hard-coded into the build so not registered here) # diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index 9063e5a2d2b1b..bfe858438f594 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -746,14 +746,14 @@ envoy.matching.matchers.ip: status: stable type_urls: - envoy.extensions.matching.input_matchers.ip.v3.Ip -envoy.path_match_policy.pattern_template_match_predicate: +envoy.path.match.pattern_template.v3.pattern_template_match_predicate: categories: - - envoy.path_match_policy + - envoy.path.match security_posture: robust_to_untrusted_downstream_and_upstream status: stable -envoy.path_rewrite_policy.pattern_template_rewrite_predicate: +envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate: categories: - - envoy.path_rewrite_policy + - envoy.path.rewrite security_posture: robust_to_untrusted_downstream_and_upstream status: stable envoy.quic.proof_source.filter_chain: diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD index 4c53f40c88e2f..d290e1ac07b1e 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/pattern_template/BUILD @@ -21,6 +21,8 @@ envoy_cc_library( ], deps = [ "//envoy/router:router_path_match_policy_interface", + "//source/common/protobuf", + "//source/common/protobuf:utility_lib", "//source/extensions/path/pattern_template_lib:pattern_template_lib", "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", ], @@ -35,7 +37,7 @@ envoy_cc_extension( ":pattern_template_match_lib", "//envoy/registry", "//envoy/router:router_path_match_policy_interface", - "//source/extensions/path/pattern_template_lib", + "//source/extensions/path/pattern_template_lib:pattern_template_lib", "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 5146668f38468..4b5b4497d7841 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -1,10 +1,14 @@ #pragma once #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" +#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" #include "envoy/router/path_match_policy.h" #include "source/extensions/path/match/pattern_template/pattern_template_match.h" +#include "source/common/protobuf/message_validator_impl.h" +#include "source/common/protobuf/utility.h" + namespace Envoy { namespace Extensions { namespace PatternTemplate { @@ -13,16 +17,21 @@ namespace Match { class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFactory { public: Router::PathMatchPredicateSharedPtr - createPathMatchPredicate(const Protobuf::Message& config, std::string url_pattern) override { - return std::make_shared(config, url_pattern); + createPathMatchPredicate(const Protobuf::Message& config) override { + //TODO: Get pattern from config + auto path_match_config = + MessageUtil::downcastAndValidate( + config, ProtobufMessage::getStrictValidationVisitor()); + return std::make_shared(path_match_config); } ProtobufTypes::MessagePtr createEmptyConfigProto() override { return std::make_unique(); } - std::string name() const override { return "envoy.path_match_policy.pattern_template_match_predicate"; } - std::string category() const override { return "envoy.path_match_policy"; } + std::string name() const override { return "envoy.path.match.pattern_template.v3.pattern_template_match_predicate"; } + std::string category() const override { return "envoy.path.match"; } }; } // namespace Match diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.cc b/source/extensions/path/match/pattern_template/pattern_template_match.cc index ba9f5acc23d1d..2266c4d8bd76d 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.cc +++ b/source/extensions/path/match/pattern_template/pattern_template_match.cc @@ -18,8 +18,13 @@ namespace PatternTemplate { namespace Match { bool PatternTemplateMatchPredicate::match(absl::string_view pattern) const { + RE2 matching_pattern_regex = RE2(convertURLPatternSyntaxToRegex(path_template_).value()); return RE2::FullMatch(PatternTemplateInternal::toStringPiece(Http::PathUtil::removeQueryAndFragment(pattern)), - matching_pattern_regex_); + matching_pattern_regex); +} + +std::string PatternTemplateMatchPredicate::pattern() const { + return path_template_; } } // namespace Match diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index 9267c035a9184..de1298531c333 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -18,17 +18,20 @@ namespace Match { class PatternTemplateMatchPredicate : public Router::PathMatchPredicate { public: - explicit PatternTemplateMatchPredicate(const Protobuf::Message&, std::string url_pattern) - : matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())) {} + explicit PatternTemplateMatchPredicate( + const envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig& + config) + : path_template_(config.path_template()){} - absl::string_view name() const override { - return "envoy.pattern_template.pattern_template_match_predicate"; - } + absl::string_view name() const override { + return "envoy.path.match.pattern_template.v3.pattern_template_match_predicate"; + } - bool match(absl::string_view pattern) const override; + bool match(absl::string_view pattern) const override; + std::string pattern() const override; - private: - RE2 matching_pattern_regex_{nullptr}; +private: + const std::string path_template_; }; } // namespace Match diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc index 9d9237d52fb7c..e3dc29cf855fb 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -117,7 +117,7 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) return parsed_pattern; } -absl::Status isValidMatchPattern(const std::string& path_template_match) { +absl::Status isValidMatchPattern(const std::string path_template_match) { return convertURLPatternSyntaxToRegex(path_template_match).status(); } @@ -126,8 +126,15 @@ absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_ } absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, - std::string& capture_regex) { - return parseRewritePattern(path_template_rewrite, capture_regex).status(); + const std::string& capture_regex) { + + absl::StatusOr status = + convertURLPatternSyntaxToRegex(capture_regex).value(); + if (!status.ok()) { + return status.status(); + } + + return parseRewritePattern(path_template_rewrite, *std::move(status)).status(); } } // namespace PatternTemplate diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index cbb72340e2138..7879ec4813659 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -47,9 +47,9 @@ absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_ // Returns if path_template and rewrite_template have valid variables absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, - std::string& capture_regex); + const std::string& capture_regex); -absl::Status isValidMatchPattern(std::string match_pattern); +absl::Status isValidMatchPattern(const std::string match_pattern); } // namespace PatternTemplate } // namespace Extensions diff --git a/source/extensions/path/rewrite/pattern_template/BUILD b/source/extensions/path/rewrite/pattern_template/BUILD index cf526a8bdda15..34f7cd6698aa1 100644 --- a/source/extensions/path/rewrite/pattern_template/BUILD +++ b/source/extensions/path/rewrite/pattern_template/BUILD @@ -21,7 +21,9 @@ envoy_cc_library( ], deps = [ "//envoy/router:router_path_rewrite_policy_interface", - "//source/extensions/path/pattern_template_lib:pattern_template_lib", + "//source/common/protobuf", + "//source/common/protobuf:utility_lib", + "//source/extensions/path/pattern_template_lib", "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", ], ) @@ -35,7 +37,7 @@ envoy_cc_extension( ":pattern_template_rewrite_lib", "//envoy/registry", "//envoy/router:router_path_rewrite_policy_interface", - "//source/extensions/path/pattern_template_lib:pattern_template_lib", + "//source/extensions/path/pattern_template_lib", "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 9d55478232517..ee3c40a7c460a 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -1,6 +1,7 @@ #pragma once #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" +#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" #include "envoy/router/path_rewrite_policy.h" #include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" @@ -13,16 +14,20 @@ namespace Rewrite { class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredicateFactory { public: Router::PathRewritePredicateSharedPtr - createPathRewritePredicate(const Protobuf::Message&, std::string url_pattern) override { - return std::make_shared(url_pattern, ""); + createPathRewritePredicate(const Protobuf::Message& rewrite_config) override { + auto cast_rewrite_config = + MessageUtil::downcastAndValidate( + rewrite_config, ProtobufMessage::getStrictValidationVisitor()); + return std::make_shared(cast_rewrite_config); } ProtobufTypes::MessagePtr createEmptyConfigProto() override { return std::make_unique(); } - std::string name() const override { return "envoy.path_rewrite_policy.pattern_template_rewrite_predicate"; } - std::string category() const override { return "envoy.path_rewrite_policy"; } + std::string name() const override { return "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate";} + std::string category() const override { return "envoy.path.rewrite"; } }; } // namespace Rewrite diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 1192d8114b0cf..a012029646500 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -6,7 +6,14 @@ #include "envoy/router/path_rewrite_policy.h" #include "source/extensions/path/pattern_template_lib/pattern_template.h" +#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" +#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" +#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" +#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" + +#include "source/common/protobuf/message_validator_impl.h" +#include "source/common/protobuf/utility.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" @@ -17,29 +24,30 @@ namespace Rewrite { class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { public: - explicit PatternTemplateRewritePredicate(std::string url_pattern, std::string url_rewrite_pattern) - : matching_pattern_regex_(RE2(convertURLPatternSyntaxToRegex(url_pattern).value())), - url_rewrite_pattern_(url_rewrite_pattern) {} + explicit PatternTemplateRewritePredicate( + const envoy::extensions::path::rewrite::pattern_template::v3::PatternTemplateRewriteConfig& + rewrite_config) + : url_rewrite_pattern_(rewrite_config.path_template_rewrite()) {} absl::string_view name() const override { - return "envoy.pattern_template.pattern_template_rewrite_predicate"; + return "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate"; } + std::string pattern() const override { return url_rewrite_pattern_; } + absl::StatusOr rewritePattern(absl::string_view current_pattern, absl::string_view matched_path) const override; - static absl::Status isValidRewritePattern(std::string match_pattern, - std::string rewrite_pattern); + static absl::Status isValidRewritePattern(std::string match_pattern, std::string rewrite_pattern); private: // Returns the rewritten URL path based on the given parsed rewrite pattern. // Used for template-based URL rewrite. absl::StatusOr rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& - rewrite_pattern) const; + const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern) + const; - RE2 matching_pattern_regex_{nullptr}; std::string url_rewrite_pattern_{nullptr}; }; diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index d3419fb3b48ea..e0fdffe99366a 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -5099,7 +5099,7 @@ TEST_F(RouteMatcherTest, TestPrefixAndRegexRewrites) { EXPECT_THROW_WITH_MESSAGE( TestConfigImpl(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException, - "Specify only one of prefix_rewrite, regex_rewrite or path_template_rewrite"); + "Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); } TEST_F(RouteMatcherTest, TestDomainMatchOrderConfig) { @@ -8832,7 +8832,7 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsDisabledWhenNotSpecifiedInRouteAct Http::TestRequestHeaderMapImpl headers = genRedirectHeaders("idle.lyft.com", "/regex", true, false); const auto& pattern_template_policy = - config.route(headers, 0)->routeEntry()->patternTemplatePolicy(); + config.route(headers, 0)->routeEntry()->pathMatchPolicy(); EXPECT_FALSE(pattern_template_policy.enabled()); } @@ -8843,22 +8843,32 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsFilledFromConfigInRouteAction) { domains: ["*"] routes: - match: - path_template: "/bar/{country}/{lang}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/bar/{country}/{lang}" route: - path_template_rewrite: "/bar/{lang}/{country}" cluster: some-cluster + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/bar/{lang}/{country}" + )EOF"; factory_context_.cluster_manager_.initializeClusters({"some-cluster"}, {}); TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); Http::TestRequestHeaderMapImpl headers = genHeaders("path.prefix.com", "/bar/one/two", "GET"); - const auto& pattern_template_policy = - config.route(headers, 0)->routeEntry()->patternTemplatePolicy(); + const auto& pattern_match_policy = config.route(headers, 0)->routeEntry()->pathMatchPolicy(); + EXPECT_TRUE(pattern_match_policy.enabled()); + EXPECT_EQ(pattern_match_policy.predicate()->pattern(), "/bar/{country}/{lang}"); - EXPECT_TRUE(pattern_template_policy.enabled()); - EXPECT_EQ(pattern_template_policy.predicate()->url_pattern_, "/bar/{country}/{lang}"); - EXPECT_EQ(pattern_template_policy.predicate()->url_rewrite_pattern_, "/bar/{lang}/{country}"); + const auto& pattern_rewrite_policy = config.route(headers, 0)->routeEntry()->pathRewritePolicy(); + EXPECT_TRUE(pattern_rewrite_policy.enabled()); + EXPECT_EQ(pattern_rewrite_policy.predicate()->pattern(), "/bar/{lang}/{country}"); } TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { @@ -8869,7 +8879,11 @@ TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { domains: ["*"] routes: - match: - path_template: "/rest/{lang}/{state}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{lang}/{state}" case_sensitive: false route: { cluster: path-pattern-cluster} )EOF"; @@ -8897,15 +8911,27 @@ TEST_F(RouteMatcherTest, MixedPathPatternMatch) { domains: ["*"] routes: - match: - path_template: "/rest/{lang}/{state}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{lang}/{state}" case_sensitive: false route: { cluster: path-pattern-cluster-one} - match: - path_template: "/boo/{go}/{fly}/{bat}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/boo/{go}/{fly}/{bat}" case_sensitive: false route: { cluster: path-pattern-cluster-two} - match: - path_template: "/foo/boo/{go}/{fly}/{bat}/{sno}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/foo/boo/{go}/{fly}/{bat}/{sno}" case_sensitive: false route: { cluster: path-pattern-cluster-three} )EOF"; @@ -8952,11 +8978,19 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimple) { domains: ["*"] routes: - match: - path_template: "/rest/{one}/{two}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{one}/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/rest/{two}/{one}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/rest/{two}/{one}" )EOF"; NiceMock stream_info; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); @@ -8978,11 +9012,19 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimpleTwo) { domains: ["*"] routes: - match: - path_template: "/rest/{one=*}/{two}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{one=*}/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/{two}/{one}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/{two}/{one}" )EOF"; NiceMock stream_info; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); @@ -9004,17 +9046,33 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteCaseSensitive) { domains: ["*"] routes: - match: - path_template: "/rest/{one}/{two}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{one}/{two}" case_sensitive: true route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/{two}/{one}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/{two}/{one}" - match: - path_template: "/REST/{one}/{two}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/REST/{one}/{two}" case_sensitive: true route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/TEST/{one}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/TEST/{one}" )EOF"; NiceMock stream_info; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); @@ -9043,17 +9101,25 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingBracket) { domains: ["*"] routes: - match: - path_template: "/rest/{one/{two}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{one/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/{two}/{one}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/{two}/{one}" )EOF"; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); EXPECT_THROW_WITH_MESSAGE( TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), - EnvoyException, "path_template /rest/{one/{two} is invalid"); + EnvoyException, "path_match_policy /rest/{one/{two} is invalid"); } TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { @@ -9063,11 +9129,19 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { domains: ["*"] routes: - match: - path_template: "/rest/{one}/{two}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{one}/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/rest/{one}/{two}/{missing}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/rest/{one}/{two}/{missing}" )EOF"; NiceMock stream_info; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); @@ -9075,7 +9149,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { EXPECT_THROW_WITH_MESSAGE( TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException, - "mismatch between path_template /rest/{one}/{two} and path_template_rewrite " + "mismatch between path_match_policy /rest/{one}/{two} and path_rewrite_policy" "/rest/{one}/{two}/{missing}"); } @@ -9086,18 +9160,26 @@ TEST_F(RouteMatcherTest, PatternMatchInvalidVariableName) { domains: ["*"] routes: - match: - path_template: "/rest/{on==e}/{two}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{on==e}/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/rest/{one}/{two}/{missing}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/rest/{one}/{two}/{missing}" )EOF"; NiceMock stream_info; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); EXPECT_THROW_WITH_MESSAGE( TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), - EnvoyException, "path_template /rest/{on==e}/{two} is invalid"); + EnvoyException, "path_match_policy /rest/{on==e}/{two} is invalid"); } TEST_F(RouteMatcherTest, PatternMatchWildcardUnnamedVariable) { @@ -9107,11 +9189,19 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardUnnamedVariable) { domains: ["*"] routes: - match: - path_template: "/rest/*/{two}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/*/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/{two}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/{two}" )EOF"; NiceMock stream_info; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); @@ -9133,11 +9223,19 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariable) { domains: ["*"] routes: - match: - path_template: "/rest/{one}/**" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{one}/**" case_sensitive: false route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/{one}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/{one}" )EOF"; NiceMock stream_info; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); @@ -9160,11 +9258,19 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariableNamed) { domains: ["*"] routes: - match: - path_template: "/rest/{one=*}/{last=**}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{one=*}/{last=**}" case_sensitive: false route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/{last}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/{last}" )EOF"; NiceMock stream_info; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); @@ -9187,11 +9293,19 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardMiddleVariableNamed) { domains: ["*"] routes: - match: - path_template: "/rest/{one}/{middle=videos/*}/end" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{one}/{middle=videos/*}/end" case_sensitive: false route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/{middle}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/{middle}" )EOF"; NiceMock stream_info; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); @@ -9214,11 +9328,19 @@ TEST_F(RouteMatcherTest, PatternMatchCaseSensitiveVariableNames) { domains: ["*"] routes: - match: - path_template: "/rest/{one}/{One}/end" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{one}/{One}/end" case_sensitive: false route: cluster: "path-pattern-cluster-one" - path_template_rewrite: "/{One}/{one}" + path_rewrite_policy: + name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/{One}/{one}" )EOF"; NiceMock stream_info; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); @@ -9241,7 +9363,11 @@ TEST_F(RouteMatcherTest, PatternMatchCaseTooManyVariableNames) { domains: ["*"] routes: - match: - path_template: "/rest/{one}/{two}/{three}/{four}/{five}/{six}" + path_match_policy: + name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/rest/{one}/{two}/{three}/{four}/{five}/{six}" case_sensitive: false route: cluster: "path-pattern-cluster-one" @@ -9252,7 +9378,7 @@ TEST_F(RouteMatcherTest, PatternMatchCaseTooManyVariableNames) { EXPECT_THROW_WITH_MESSAGE( TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException, - "path_template /rest/{one}/{two}/{three}/{four}/{five}/{six} is invalid"); + "path_match_policy /rest/{one}/{two}/{three}/{four}/{five}/{six} is invalid"); } TEST_F(RouteConfigurationV2, DefaultInternalRedirectPolicyIsSensible) { diff --git a/test/extensions/pattern_template/BUILD b/test/extensions/path/BUILD similarity index 66% rename from test/extensions/pattern_template/BUILD rename to test/extensions/path/BUILD index 0e6e4583d487e..ef0cd3652e046 100644 --- a/test/extensions/pattern_template/BUILD +++ b/test/extensions/path/BUILD @@ -14,9 +14,9 @@ envoy_package() envoy_extension_cc_test( name = "match_config_test", srcs = ["match_config_test.cc"], - extension_names = ["envoy.pattern_template"], + extension_names = ["envoy.path.match"], deps = [ - "//source/extensions/pattern_template/match:match:pattern_template_match_lib", + "//source/extensions/path/match/pattern_template:match:pattern_template_match_lib", "//test/mocks/server:factory_context_mocks", ], ) @@ -24,9 +24,9 @@ envoy_extension_cc_test( envoy_extension_cc_test( name = "rewrite_config_test", srcs = ["rewrite_config_test.cc"], - extension_names = ["envoy.pattern_template"], + extension_names = ["envoy.path.rewrite"], deps = [ - "//source/extensions/pattern_template/rewrite:pattern_template_rewrite_lib", + "//source/extensions/path/rewrite/pattern_template/:pattern_template_rewrite_lib", "//test/mocks/server:factory_context_mocks", ], ) diff --git a/test/extensions/pattern_template/match_config_test.cc b/test/extensions/path/match_config_test.cc similarity index 61% rename from test/extensions/pattern_template/match_config_test.cc rename to test/extensions/path/match_config_test.cc index e300237f43356..58f0a4061721d 100644 --- a/test/extensions/pattern_template/match_config_test.cc +++ b/test/extensions/path/match_config_test.cc @@ -14,12 +14,13 @@ namespace Extensions { namespace PatternTemplate { namespace { -class PatternTemplateMatchConfigTest : public testing::Test { +class PathMatchPredicateFactoryConfigTest : public testing::Test { protected: - PatternTemplateMatchConfigTest() : filter_state_(StreamInfo::FilterState::LifeSpan::FilterChain) { - factory_ = Registry::FactoryRegistry::getFactory( - "envoy.pattern_template.pattern_template_match_predicate"); + PathMatchPredicateFactoryConfigTest() : filter_state_(StreamInfo::FilterState::LifeSpan::FilterChain) { + factory_ = Registry::FactoryRegistry::getFactory( + "envoy.path.match.pattern_template.v3.pattern_template_match_predicate"); config_ = factory_->createEmptyConfigProto(); + ASSERT(config_); } StreamInfo::FilterStateImpl filter_state_; @@ -27,12 +28,12 @@ class PatternTemplateMatchConfigTest : public testing::Test { ProtobufTypes::MessagePtr config_; }; -TEST_F(PatternTemplateMatchConfigTest, EmptyCreation) { +TEST_F(PathMatchPredicateFactoryConfigTest, EmptyCreation) { std::string current_route_name = "fake_current_route"; // Create the predicate for the first time. { auto predicate = - factory_->createUrlTemplateMatchPredicate("/url_pattern/{TEST}"); + factory_->createPathMatchPredicate(config_, "/url_pattern/{TEST}"); ASSERT(predicate); } } diff --git a/test/extensions/pattern_template/rewrite_config_test.cc b/test/extensions/path/rewrite_config_test.cc similarity index 100% rename from test/extensions/pattern_template/rewrite_config_test.cc rename to test/extensions/path/rewrite_config_test.cc diff --git a/test/mocks/router/mocks.cc b/test/mocks/router/mocks.cc index 7f0a8a08d42c3..74b27d634ec30 100644 --- a/test/mocks/router/mocks.cc +++ b/test/mocks/router/mocks.cc @@ -28,7 +28,11 @@ MockInternalRedirectPolicy::MockInternalRedirectPolicy() { ON_CALL(*this, enabled()).WillByDefault(Return(false)); } -MockPatternTemplatePolicy::MockPatternTemplatePolicy() { +MockPathMatchPolicy::MockPathMatchPolicy() { + ON_CALL(*this, enabled()).WillByDefault(Return(false)); +} + +MockPathRewritePolicy::MockPathRewritePolicy() { ON_CALL(*this, enabled()).WillByDefault(Return(false)); } @@ -104,7 +108,7 @@ MockRouteEntry::MockRouteEntry() { ON_CALL(*this, rateLimitPolicy()).WillByDefault(ReturnRef(rate_limit_policy_)); ON_CALL(*this, retryPolicy()).WillByDefault(ReturnRef(retry_policy_)); ON_CALL(*this, internalRedirectPolicy()).WillByDefault(ReturnRef(internal_redirect_policy_)); - ON_CALL(*this, patternTemplatePolicy()).WillByDefault(ReturnRef(pattern_template_policy_)); + ON_CALL(*this, pathMatchPolicy()).WillByDefault(ReturnRef(path_match_policy_)); ON_CALL(*this, retryShadowBufferLimit()) .WillByDefault(Return(std::numeric_limits::max())); ON_CALL(*this, shadowPolicies()).WillByDefault(ReturnRef(shadow_policies_)); diff --git a/test/mocks/router/mocks.h b/test/mocks/router/mocks.h index 57b00916df132..edae1ba6e0e1b 100644 --- a/test/mocks/router/mocks.h +++ b/test/mocks/router/mocks.h @@ -164,14 +164,26 @@ class MockInternalRedirectPredicate : public InternalRedirectPredicate { MOCK_METHOD(absl::string_view, name, (), (const)); }; -class MockPatternTemplateMatchPolicy : public PatternTemplateMatchPolicy { +class MockPathRewritePolicy : public PathRewritePolicy { public: - MockPatternTemplateMatchPolicy(); + MockPathRewritePolicy(); MOCK_METHOD(bool, enabled, (), (const)); - MOCK_METHOD(PatternTemplatePredicateMatchSharedPtr, predicate, (), (const)); + MOCK_METHOD(PathRewritePredicateSharedPtr, predicate, (), (const)); }; -class MockPatternTemplateMatchPredicate : public PatternTemplateMatchPredicate { +class MockPathRewritePredicate : public PathRewritePredicate { +public: + MOCK_METHOD(absl::string_view, name, (), (const)); +}; + +class MockPathMatchPolicy : public PathMatchPolicy { +public: + MockPathMatchPolicy(); + MOCK_METHOD(bool, enabled, (), (const)); + MOCK_METHOD(PathMatchPredicateSharedPtr, predicate, (), (const)); +}; + +class MockPathMatchPredicate : public PathMatchPredicate { public: MOCK_METHOD(absl::string_view, name, (), (const)); }; @@ -388,7 +400,8 @@ class MockRouteEntry : public RouteEntry { MOCK_METHOD(const RateLimitPolicy&, rateLimitPolicy, (), (const)); MOCK_METHOD(const RetryPolicy&, retryPolicy, (), (const)); MOCK_METHOD(const InternalRedirectPolicy&, internalRedirectPolicy, (), (const)); - MOCK_METHOD(const PatternTemplatePolicy&, patternTemplatePolicy, (), (const)); + MOCK_METHOD(const PathMatchPolicy&, pathMatchPolicy, (), (const)); + MOCK_METHOD(const PathRewritePolicy&, pathRewritePolicy, (), (const)); MOCK_METHOD(uint32_t, retryShadowBufferLimit, (), (const)); MOCK_METHOD(const std::vector&, shadowPolicies, (), (const)); MOCK_METHOD(std::chrono::milliseconds, timeout, (), (const)); @@ -426,7 +439,8 @@ class MockRouteEntry : public RouteEntry { TestVirtualCluster virtual_cluster_; TestRetryPolicy retry_policy_; testing::NiceMock internal_redirect_policy_; - testing::NiceMock pattern_template_policy_; + testing::NiceMock path_match_policy_; + testing::NiceMock path_rewrite_policy_; TestHedgePolicy hedge_policy_; testing::NiceMock rate_limit_policy_; std::vector shadow_policies_; From 6ac59da0e07fe67aa45aa8ed4972983569caf939 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 28 Jul 2022 19:28:02 +0000 Subject: [PATCH 076/238] Small change to test Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 31b9d2c66e79a..03dbf3230d330 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -721,7 +721,7 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, path_rewrite_policy_.predicate()->pattern(), path_match_policy_.predicate()->pattern()) .ok()) { throw EnvoyException(fmt::format( - "mismatch between path_match_policy {} and path_rewrite_policy {} is invalid", + "mismatch between path_match_policy {} and path_rewrite_policy {}", path_match_policy_.predicate()->pattern(), path_rewrite_policy_.predicate()->pattern())); } } From 6d3160bd99ead84708d56d06495b099b5ad91a0d Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 28 Jul 2022 19:32:44 +0000 Subject: [PATCH 077/238] Small change to test Signed-off-by: silverstar195 --- test/common/router/config_impl_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index e0fdffe99366a..00a31acb5399b 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -9149,7 +9149,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { EXPECT_THROW_WITH_MESSAGE( TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException, - "mismatch between path_match_policy /rest/{one}/{two} and path_rewrite_policy" + "mismatch between path_match_policy /rest/{one}/{two} and path_rewrite_policy " "/rest/{one}/{two}/{missing}"); } From 3a1890597d730e8b6ed0b682679aae45ac4eeff3 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 28 Jul 2022 20:09:25 +0000 Subject: [PATCH 078/238] Format and clean up code Signed-off-by: silverstar195 --- envoy/router/BUILD | 24 +++++++ envoy/router/path_match_policy.h | 64 +++++++++++++++++ envoy/router/path_rewrite_policy.h | 70 +++++++++++++++++++ envoy/router/router.h | 58 ++++++++++++++- source/extensions/extensions_build_config.bzl | 22 ++++-- .../path/match/pattern_template/BUILD | 41 +++++++++++ .../path/match/pattern_template/config.cc | 15 ++++ .../path/match/pattern_template/config.h | 40 +++++++++++ .../path/rewrite/pattern_template/BUILD | 41 +++++++++++ .../path/rewrite/pattern_template/config.cc | 15 ++++ .../path/rewrite/pattern_template/config.h | 37 ++++++++++ tools/extensions/extensions_schema.yaml | 2 + 12 files changed, 421 insertions(+), 8 deletions(-) create mode 100644 envoy/router/path_match_policy.h create mode 100644 envoy/router/path_rewrite_policy.h create mode 100644 source/extensions/path/match/pattern_template/BUILD create mode 100644 source/extensions/path/match/pattern_template/config.cc create mode 100644 source/extensions/path/match/pattern_template/config.h create mode 100644 source/extensions/path/rewrite/pattern_template/BUILD create mode 100644 source/extensions/path/rewrite/pattern_template/config.cc create mode 100644 source/extensions/path/rewrite/pattern_template/config.h diff --git a/envoy/router/BUILD b/envoy/router/BUILD index 1ec10b92c4291..0b2f8365ad1fb 100644 --- a/envoy/router/BUILD +++ b/envoy/router/BUILD @@ -64,6 +64,8 @@ envoy_cc_library( external_deps = ["abseil_optional"], deps = [ ":internal_redirect_interface", + ":router_path_match_policy_interface", + ":router_path_rewrite_policy_interface", "//envoy/access_log:access_log_interface", "//envoy/common:conn_pool_interface", "//envoy/common:matchers_interface", @@ -144,3 +146,25 @@ envoy_cc_library( "//envoy/server:factory_context_interface", ], ) + +envoy_cc_library( + name = "router_path_match_policy_interface", + hdrs = ["path_match_policy.h"], + deps = [ + "//envoy/config:typed_config_interface", + "//source/common/common:minimal_logger_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + ], +) + +envoy_cc_library( + name = "router_path_rewrite_policy_interface", + hdrs = ["path_rewrite_policy.h"], + deps = [ + "//envoy/config:typed_config_interface", + "//source/common/common:minimal_logger_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + ], +) diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h new file mode 100644 index 0000000000000..28f4cad1205fb --- /dev/null +++ b/envoy/router/path_match_policy.h @@ -0,0 +1,64 @@ +#pragma once + +#include "envoy/config/typed_config.h" + +#include "source/common/common/logger.h" + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" + +namespace Envoy { +namespace Router { + +/** + * Used to decide if path match is needed based on the target route. + * Subclassing Logger::Loggable so that implementations can log details. + */ +class PathMatchPredicate : Logger::Loggable { +public: + PathMatchPredicate() = default; + virtual ~PathMatchPredicate() = default; + + /** + * @return the name of the current predicate. + */ + virtual absl::string_view name() const PURE; + + /** + * Used to determine if the current url matches the predicate pattern. + * + * @param pattern current url of route + * @return vaild is route url matches the precidate pattern. + */ + virtual bool match(absl::string_view pattern) const PURE; + + /** + * @return the match pattern of the predicate. + */ + virtual std::string pattern() const PURE; +}; + +using PathMatchPredicateSharedPtr = std::shared_ptr; + +/** + * Factory for PathMatchPredicateFactory. + */ +class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { +public: + virtual ~PathMatchPredicateFactory() = default; + + /** + * @param config contains the proto stored in TypedExtensionConfig.typed_config for the predicate. + * @return an PathMatchPredicateSharedPtr. + */ + virtual PathMatchPredicateSharedPtr + createPathMatchPredicate(const Protobuf::Message& config) PURE; + + /** + * @return the category of the rewrite pattern predicate to be created. + */ + std::string category() const override { return "envoy.path.match"; } +}; + +} // namespace Router +} // namespace Envoy diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h new file mode 100644 index 0000000000000..b26ad6ca10734 --- /dev/null +++ b/envoy/router/path_rewrite_policy.h @@ -0,0 +1,70 @@ +#pragma once + +#include "envoy/config/typed_config.h" + +#include "source/common/common/logger.h" + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" + +namespace Envoy { +namespace Router { + +/** + * Used to decide if pattern template rewrite is needed based on the target route. + * Subclassing Logger::Loggable so that implementations can log details. + */ +class PathRewritePredicate : Logger::Loggable { +public: + PathRewritePredicate() = default; + virtual ~PathRewritePredicate() = default; + + /** + * @return the name of the current predicate. + */ + virtual absl::string_view name() const PURE; + + /** + * Used to rewrite the current url to the specified output. Can return a failure in case rewrite + * is not successful. + * + * @param current_pattern current url of route + * @param matched_path pattern to rewrite the url to + * @return the name of the rewrite pattern current predicate. + */ + virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const PURE; + + /** + * @return the rewrite pattern of the predicate. + */ + virtual std::string pattern() const PURE; +}; + +using PathRewritePredicateSharedPtr = std::shared_ptr; + +/** + * Factory for PatternRewriteTemplatePredicate. + */ +class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { +public: + virtual ~PathRewritePredicateFactory() override = default; + + virtual PathRewritePredicateSharedPtr + createPathRewritePredicate(const Protobuf::Message& rewrite_config) PURE; + + virtual ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; + + /** + * @return the name of the rewrite pattern predicate to be created. + */ + virtual std::string name() const override PURE; + + /** + * @return the category of the rewrite pattern predicate to be created. + */ + virtual std::string category() const override PURE; +}; + +} // namespace Router +} // namespace Envoy diff --git a/envoy/router/router.h b/envoy/router/router.h index 30a675aa102af..954aa79be5ab6 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -20,6 +20,8 @@ #include "envoy/http/hash_policy.h" #include "envoy/rds/config.h" #include "envoy/router/internal_redirect.h" +#include "envoy/router/path_match_policy.h" +#include "envoy/router/path_rewrite_policy.h" #include "envoy/tcp/conn_pool.h" #include "envoy/tracing/http_tracer.h" #include "envoy/type/v3/percent.pb.h" @@ -297,6 +299,48 @@ class RetryPolicy { */ enum class RetryStatus { No, NoOverflow, NoRetryLimitExceeded, Yes }; +/** + * PathMatchPolicy from the route configuration. + */ +class PathMatchPolicy { +public: + virtual ~PathMatchPolicy() = default; + + /** + * @return whether path match based on custom policy is enabled on this route. + */ + virtual bool enabled() const PURE; + + /** + * Creates the target route predicates. This should really be called only once for each upstream + * match. Creating the predicate lazily to avoid wasting CPU cycles on non-matching + * responses, which should be the most common case. + * @return a newly constructed PathMatchPredicate instance. + */ + virtual PathMatchPredicateSharedPtr predicate() const PURE; +}; + +/** + * PathRewritePolicy from the route configuration. + */ +class PathRewritePolicy { +public: + virtual ~PathRewritePolicy() = default; + + /** + * @return whether path rewrite based on custom policy is enabled on this route. + */ + virtual bool enabled() const PURE; + + /** + * Creates the target route predicates. This should really be called only once for each upstream + * rewrite. Creating the predicate lazily to avoid wasting CPU cycles on non-rewrite + * responses, which should be the most common case. + * @return a newly constructed PathRewritePredicate instances. + */ + virtual PathRewritePredicateSharedPtr predicate() const PURE; +}; + /** * InternalRedirectPolicy from the route configuration. */ @@ -763,7 +807,7 @@ enum class PathMatchType { Exact, Regex, PathSeparatedPrefix, - Pattern, + Policy, }; /** @@ -915,6 +959,18 @@ class RouteEntry : public ResponseEntry { */ virtual const InternalRedirectPolicy& internalRedirectPolicy() const PURE; + /** + * @return const PathMatchPolicy& the path match policy for the route. All routes + * have a path match policy even if it is not enabled. + */ + virtual const PathMatchPolicy& pathMatchPolicy() const PURE; + + /** + * @return const PathRewritePolicy& the path rewrite policy for the route. All routes + * have a path rewrite policy even if it is not enabled. + */ + virtual const PathRewritePolicy& pathRewritePolicy() const PURE; + /** * @return uint32_t any route cap on bytes which should be buffered for shadowing or retries. * This is an upper bound so does not necessarily reflect the bytes which will be buffered diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index dc71b3db22a93..40d0e668bd811 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -36,7 +36,7 @@ EXTENSIONS = { # Config validators # - "envoy.config.validators.minimum_clusters": "//source/extensions/config/validators/minimum_clusters:config", + "envoy.config.validators.minimum_clusters_validator": "//source/extensions/config/validators/minimum_clusters:config", # # gRPC Credentials Plugins @@ -61,8 +61,8 @@ EXTENSIONS = { # Input Matchers # - "envoy.matching.input_matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", - "envoy.matching.input_matchers.ip": "//source/extensions/matching/input_matchers/ip:config", + "envoy.matching.matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", + "envoy.matching.matchers.ip": "//source/extensions/matching/input_matchers/ip:config", # # Generic Inputs @@ -158,7 +158,7 @@ EXTENSIONS = { # UDP filters # - "envoy.filters.udp_listener.dns_filter": "//source/extensions/filters/udp/dns_filter:config", + "envoy.filters.udp.dns_filter": "//source/extensions/filters/udp/dns_filter:config", "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", # @@ -185,14 +185,13 @@ EXTENSIONS = { "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", - "envoy.filters.thrift.ratelimit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", + "envoy.filters.thrift.rate_limit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", # # Tracers # "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", - "envoy.tracers.lightstep": "//source/extensions/tracers/lightstep:config", "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", @@ -205,6 +204,7 @@ EXTENSIONS = { # "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", + "envoy.transport_sockets.http_11_proxy": "//source/extensions/transport_sockets/http_11_proxy:upstream_config", "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", @@ -239,6 +239,13 @@ EXTENSIONS = { "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", + # + # Path Pattern Match and Path Pattern Rewrite + # + + "envoy.path.match.pattern_template.v3.pattern_template_match_predicate": "//source/extensions/path/match/pattern_template:config", + "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate": "//source/extensions/path/rewrite/pattern_template:config", + # # Http Upstreams (excepting envoy.upstreams.http.generic which is hard-coded into the build so not registered here) # @@ -338,9 +345,10 @@ EXTENSIONS = { # c-ares DNS resolver extension is recommended to be enabled to maintain the legacy DNS resolving behavior. "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", - # apple DNS resolver extension is only needed in MacOS build plus one want to use apple library for DNS resolving. "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", + # getaddrinfo DNS resolver extension can be used when the system resolver is desired (e.g., Android) + "envoy.network.dns_resolver.getaddrinfo": "//source/extensions/network/dns_resolver/getaddrinfo:config", # # Custom matchers diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD new file mode 100644 index 0000000000000..c67c47cf19bb1 --- /dev/null +++ b/source/extensions/path/match/pattern_template/BUILD @@ -0,0 +1,41 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_extension", + "envoy_cc_library", + "envoy_cc_test", + "envoy_extension_package", +) + +licenses(["notice"]) # Apache 2 + +# Wildcard & Pattern Matching + +envoy_extension_package() + +envoy_cc_library( + name = "pattern_template_match_lib", + srcs = ["pattern_template_match.cc"], + hdrs = ["pattern_template_match.h"], + visibility = [ + "//source/common/router:__subpackages__", + ], + deps = [ + "//envoy/router:router_path_match_policy_interface", + "//source/common/protobuf", + "//source/common/protobuf:utility_lib", + "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", + ], +) + +envoy_cc_extension( + name = "config", + srcs = ["config.cc"], + hdrs = ["config.h"], + visibility = ["//visibility:public"], + deps = [ + ":pattern_template_match_lib", + "//envoy/registry", + "//envoy/router:router_path_match_policy_interface", + "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", + ], +) diff --git a/source/extensions/path/match/pattern_template/config.cc b/source/extensions/path/match/pattern_template/config.cc new file mode 100644 index 0000000000000..4295b98fcdbef --- /dev/null +++ b/source/extensions/path/match/pattern_template/config.cc @@ -0,0 +1,15 @@ +#include "source/extensions/path/match/pattern_template/config.h" + +#include "envoy/registry/registry.h" +#include "envoy/router/path_match_policy.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Match { + +REGISTER_FACTORY(PatternTemplateMatchPredicateFactory, Router::PathMatchPredicateFactory); +} // namespace Match +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h new file mode 100644 index 0000000000000..4dfb8d853843e --- /dev/null +++ b/source/extensions/path/match/pattern_template/config.h @@ -0,0 +1,40 @@ +#pragma once + +#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" +#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" +#include "envoy/router/path_match_policy.h" + +#include "source/extensions/path/match/pattern_template/pattern_template_match.h" + +#include "source/common/protobuf/message_validator_impl.h" +#include "source/common/protobuf/utility.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Match { + +class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFactory { +public: + Router::PathMatchPredicateSharedPtr + createPathMatchPredicate(const Protobuf::Message& config) override { + // TODO(silverstar195): Implement createPathMatchPredicate + return nullptr; + } + + ProtobufTypes::MessagePtr createEmptyConfigProto() override { + return std::make_unique< + envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig>(); + } + + std::string name() const override { + return "envoy.path.match.pattern_template.v3.pattern_template_match_predicate"; + } + + std::string category() const override { return "envoy.path.match"; } +}; + +} // namespace Match +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/path/rewrite/pattern_template/BUILD b/source/extensions/path/rewrite/pattern_template/BUILD new file mode 100644 index 0000000000000..c3620122a3967 --- /dev/null +++ b/source/extensions/path/rewrite/pattern_template/BUILD @@ -0,0 +1,41 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_extension", + "envoy_cc_library", + "envoy_cc_test", + "envoy_extension_package", +) + +licenses(["notice"]) # Apache 2 + +# Wildcard & Pattern Rewrites + +envoy_extension_package() + +envoy_cc_library( + name = "pattern_template_rewrite_lib", + srcs = ["pattern_template_rewrite.cc"], + hdrs = ["pattern_template_rewrite.h"], + visibility = [ + "//source/common/router:__subpackages__", + ], + deps = [ + "//envoy/router:router_path_rewrite_policy_interface", + "//source/common/protobuf", + "//source/common/protobuf:utility_lib", + "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", + ], +) + +envoy_cc_extension( + name = "config", + srcs = ["config.cc"], + hdrs = ["config.h"], + visibility = ["//visibility:public"], + deps = [ + ":pattern_template_rewrite_lib", + "//envoy/registry", + "//envoy/router:router_path_rewrite_policy_interface", + "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", + ], +) diff --git a/source/extensions/path/rewrite/pattern_template/config.cc b/source/extensions/path/rewrite/pattern_template/config.cc new file mode 100644 index 0000000000000..a813e1f46567f --- /dev/null +++ b/source/extensions/path/rewrite/pattern_template/config.cc @@ -0,0 +1,15 @@ +#include "source/extensions/path/rewrite/pattern_template/config.h" + +#include "envoy/registry/registry.h" +#include "envoy/router/path_rewrite_policy.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Rewrite { + +REGISTER_FACTORY(PatternTemplateRewritePredicateFactory, Router::PathRewritePredicateFactory); +} // namespace Rewrite +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h new file mode 100644 index 0000000000000..98cd89a55896c --- /dev/null +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -0,0 +1,37 @@ +#pragma once + +#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" +#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" +#include "envoy/router/path_rewrite_policy.h" + +#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Rewrite { + +class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredicateFactory { +public: + Router::PathRewritePredicateSharedPtr + createPathRewritePredicate(const Protobuf::Message& rewrite_config) override { + // TODO(silverstar195): Implement createPathRewritePredicate + return nullptr; + } + + ProtobufTypes::MessagePtr createEmptyConfigProto() override { + return std::make_unique< + envoy::extensions::path::rewrite::pattern_template::v3::PatternTemplateRewriteConfig>(); + } + + std::string name() const override { + return "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate"; + } + + std::string category() const override { return "envoy.path.rewrite"; } +}; + +} // namespace Rewrite +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/tools/extensions/extensions_schema.yaml b/tools/extensions/extensions_schema.yaml index b85d3f1db07d8..8b40fcdde9f9b 100644 --- a/tools/extensions/extensions_schema.yaml +++ b/tools/extensions/extensions_schema.yaml @@ -22,6 +22,7 @@ builtin: - envoy.matching.inputs.uri_san - envoy.matching.inputs.dns_san - envoy.matching.inputs.subject +- envoy.regex_engines.google_re2 # All Envoy extensions must be tagged with their security hardening stance with # respect to downstream and upstream data plane threats. These are verbose @@ -73,6 +74,7 @@ categories: - envoy.http.header_validators - envoy.http.stateful_header_formatters - envoy.internal_redirect_predicates +- envoy.pattern_template - envoy.io_socket - envoy.http.original_ip_detection - envoy.matching.common_inputs From fbeca1af22b198ef9fbe4cfd98c8ac66159fadd5 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 28 Jul 2022 20:13:21 +0000 Subject: [PATCH 079/238] Clean up Signed-off-by: silverstar195 --- source/extensions/extensions_build_config.bzl | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 40d0e668bd811..2115fe1457153 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -36,7 +36,7 @@ EXTENSIONS = { # Config validators # - "envoy.config.validators.minimum_clusters_validator": "//source/extensions/config/validators/minimum_clusters:config", + "envoy.config.validators.minimum_clusters": "//source/extensions/config/validators/minimum_clusters:config", # # gRPC Credentials Plugins @@ -61,8 +61,8 @@ EXTENSIONS = { # Input Matchers # - "envoy.matching.matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", - "envoy.matching.matchers.ip": "//source/extensions/matching/input_matchers/ip:config", + "envoy.matching.input_matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", + "envoy.matching.input_matchers.ip": "//source/extensions/matching/input_matchers/ip:config", # # Generic Inputs @@ -158,7 +158,7 @@ EXTENSIONS = { # UDP filters # - "envoy.filters.udp.dns_filter": "//source/extensions/filters/udp/dns_filter:config", + "envoy.filters.udp_listener.dns_filter": "//source/extensions/filters/udp/dns_filter:config", "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", # @@ -185,13 +185,14 @@ EXTENSIONS = { "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", - "envoy.filters.thrift.rate_limit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", + "envoy.filters.thrift.ratelimit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", # # Tracers # "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", + "envoy.tracers.lightstep": "//source/extensions/tracers/lightstep:config", "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", @@ -204,7 +205,6 @@ EXTENSIONS = { # "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", - "envoy.transport_sockets.http_11_proxy": "//source/extensions/transport_sockets/http_11_proxy:upstream_config", "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", @@ -345,10 +345,9 @@ EXTENSIONS = { # c-ares DNS resolver extension is recommended to be enabled to maintain the legacy DNS resolving behavior. "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", + # apple DNS resolver extension is only needed in MacOS build plus one want to use apple library for DNS resolving. "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", - # getaddrinfo DNS resolver extension can be used when the system resolver is desired (e.g., Android) - "envoy.network.dns_resolver.getaddrinfo": "//source/extensions/network/dns_resolver/getaddrinfo:config", # # Custom matchers From fa7d61bff8c6a715cf26e0a633020feee942a843 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 28 Jul 2022 20:18:07 +0000 Subject: [PATCH 080/238] Clean up Signed-off-by: silverstar195 --- envoy/router/path_match_policy.h | 2 +- envoy/router/path_rewrite_policy.h | 2 +- envoy/router/router.h | 14 +------------- tools/extensions/extensions_schema.yaml | 1 - 4 files changed, 3 insertions(+), 16 deletions(-) diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index 28f4cad1205fb..0e4bc57bff2cd 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -22,7 +22,7 @@ class PathMatchPredicate : Logger::Loggable { /** * @return the name of the current predicate. */ - virtual absl::string_view name() const PURE; + virtual std::string name() const PURE; /** * Used to determine if the current url matches the predicate pattern. diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index b26ad6ca10734..7108a9f5d960b 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -22,7 +22,7 @@ class PathRewritePredicate : Logger::Loggable { /** * @return the name of the current predicate. */ - virtual absl::string_view name() const PURE; + virtual std::string_view name() const PURE; /** * Used to rewrite the current url to the specified output. Can return a failure in case rewrite diff --git a/envoy/router/router.h b/envoy/router/router.h index 954aa79be5ab6..0871c0b34224b 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -958,19 +958,7 @@ class RouteEntry : public ResponseEntry { * simply proxied as normal responses. */ virtual const InternalRedirectPolicy& internalRedirectPolicy() const PURE; - - /** - * @return const PathMatchPolicy& the path match policy for the route. All routes - * have a path match policy even if it is not enabled. - */ - virtual const PathMatchPolicy& pathMatchPolicy() const PURE; - - /** - * @return const PathRewritePolicy& the path rewrite policy for the route. All routes - * have a path rewrite policy even if it is not enabled. - */ - virtual const PathRewritePolicy& pathRewritePolicy() const PURE; - + /** * @return uint32_t any route cap on bytes which should be buffered for shadowing or retries. * This is an upper bound so does not necessarily reflect the bytes which will be buffered diff --git a/tools/extensions/extensions_schema.yaml b/tools/extensions/extensions_schema.yaml index 8b40fcdde9f9b..4d2083c0cdcf5 100644 --- a/tools/extensions/extensions_schema.yaml +++ b/tools/extensions/extensions_schema.yaml @@ -22,7 +22,6 @@ builtin: - envoy.matching.inputs.uri_san - envoy.matching.inputs.dns_san - envoy.matching.inputs.subject -- envoy.regex_engines.google_re2 # All Envoy extensions must be tagged with their security hardening stance with # respect to downstream and upstream data plane threats. These are verbose From dea34c2da32fe89b78f9cecceda3e40b475300fd Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 28 Jul 2022 20:24:18 +0000 Subject: [PATCH 081/238] Clean up Signed-off-by: silverstar195 --- envoy/router/router.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/envoy/router/router.h b/envoy/router/router.h index 0871c0b34224b..5a86b83411bb4 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -958,7 +958,7 @@ class RouteEntry : public ResponseEntry { * simply proxied as normal responses. */ virtual const InternalRedirectPolicy& internalRedirectPolicy() const PURE; - + /** * @return uint32_t any route cap on bytes which should be buffered for shadowing or retries. * This is an upper bound so does not necessarily reflect the bytes which will be buffered From 854b14a83da00d5953094febd4c830e7676ed8bb Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 28 Jul 2022 20:40:17 +0000 Subject: [PATCH 082/238] Clean up Signed-off-by: silverstar195 --- source/extensions/path/match/pattern_template/BUILD | 2 -- source/extensions/path/match/pattern_template/config.h | 2 -- source/extensions/path/rewrite/pattern_template/BUILD | 2 -- source/extensions/path/rewrite/pattern_template/config.h | 2 -- 4 files changed, 8 deletions(-) diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD index c67c47cf19bb1..ea24393b7a70e 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/pattern_template/BUILD @@ -2,7 +2,6 @@ load( "//bazel:envoy_build_system.bzl", "envoy_cc_extension", "envoy_cc_library", - "envoy_cc_test", "envoy_extension_package", ) @@ -23,7 +22,6 @@ envoy_cc_library( "//envoy/router:router_path_match_policy_interface", "//source/common/protobuf", "//source/common/protobuf:utility_lib", - "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 4dfb8d853843e..917d799f6f594 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -4,8 +4,6 @@ #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" #include "envoy/router/path_match_policy.h" -#include "source/extensions/path/match/pattern_template/pattern_template_match.h" - #include "source/common/protobuf/message_validator_impl.h" #include "source/common/protobuf/utility.h" diff --git a/source/extensions/path/rewrite/pattern_template/BUILD b/source/extensions/path/rewrite/pattern_template/BUILD index c3620122a3967..c2813e268c93c 100644 --- a/source/extensions/path/rewrite/pattern_template/BUILD +++ b/source/extensions/path/rewrite/pattern_template/BUILD @@ -2,7 +2,6 @@ load( "//bazel:envoy_build_system.bzl", "envoy_cc_extension", "envoy_cc_library", - "envoy_cc_test", "envoy_extension_package", ) @@ -23,7 +22,6 @@ envoy_cc_library( "//envoy/router:router_path_rewrite_policy_interface", "//source/common/protobuf", "//source/common/protobuf:utility_lib", - "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 98cd89a55896c..7285be8f6b518 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -4,8 +4,6 @@ #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" #include "envoy/router/path_rewrite_policy.h" -#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" - namespace Envoy { namespace Extensions { namespace PatternTemplate { From d1573270cbbd848924792accf55504a848ca2eab Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 28 Jul 2022 20:42:39 +0000 Subject: [PATCH 083/238] Clean up Signed-off-by: silverstar195 --- .../extensions/path/match/pattern_template/BUILD | 15 --------------- .../path/rewrite/pattern_template/BUILD | 15 --------------- 2 files changed, 30 deletions(-) diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD index ea24393b7a70e..8321eab1802f0 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/pattern_template/BUILD @@ -11,27 +11,12 @@ licenses(["notice"]) # Apache 2 envoy_extension_package() -envoy_cc_library( - name = "pattern_template_match_lib", - srcs = ["pattern_template_match.cc"], - hdrs = ["pattern_template_match.h"], - visibility = [ - "//source/common/router:__subpackages__", - ], - deps = [ - "//envoy/router:router_path_match_policy_interface", - "//source/common/protobuf", - "//source/common/protobuf:utility_lib", - ], -) - envoy_cc_extension( name = "config", srcs = ["config.cc"], hdrs = ["config.h"], visibility = ["//visibility:public"], deps = [ - ":pattern_template_match_lib", "//envoy/registry", "//envoy/router:router_path_match_policy_interface", "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", diff --git a/source/extensions/path/rewrite/pattern_template/BUILD b/source/extensions/path/rewrite/pattern_template/BUILD index c2813e268c93c..fb64816312ce4 100644 --- a/source/extensions/path/rewrite/pattern_template/BUILD +++ b/source/extensions/path/rewrite/pattern_template/BUILD @@ -11,27 +11,12 @@ licenses(["notice"]) # Apache 2 envoy_extension_package() -envoy_cc_library( - name = "pattern_template_rewrite_lib", - srcs = ["pattern_template_rewrite.cc"], - hdrs = ["pattern_template_rewrite.h"], - visibility = [ - "//source/common/router:__subpackages__", - ], - deps = [ - "//envoy/router:router_path_rewrite_policy_interface", - "//source/common/protobuf", - "//source/common/protobuf:utility_lib", - ], -) - envoy_cc_extension( name = "config", srcs = ["config.cc"], hdrs = ["config.h"], visibility = ["//visibility:public"], deps = [ - ":pattern_template_rewrite_lib", "//envoy/registry", "//envoy/router:router_path_rewrite_policy_interface", "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", From 437eae7b33c18159296ba151190778e2d731b776 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 28 Jul 2022 20:45:25 +0000 Subject: [PATCH 084/238] Clean up Signed-off-by: silverstar195 --- source/common/router/config_impl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index de325e6087b34..e6435855f4b2c 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -1014,7 +1014,7 @@ class PathTemplateRouteEntryImpl : public RouteEntryImplBase { // Router::PathMatchCriterion const std::string& matcher() const override { return url_pattern_; } - PathMatchType matchType() const override { return PathMatchType::Pattern; } + PathMatchType matchType() const override { return PathMatchType::Policy; } // Router::Matchable RouteConstSharedPtr matches(const Http::RequestHeaderMap& headers, From 7cad22b3980665f6dd7994aa4a01800c5a318ccd Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 29 Jul 2022 14:39:32 +0000 Subject: [PATCH 085/238] To string_view Signed-off-by: silverstar195 --- envoy/router/path_match_policy.h | 6 +++--- envoy/router/path_rewrite_policy.h | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index 0e4bc57bff2cd..9f3506fe5f0e0 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -22,7 +22,7 @@ class PathMatchPredicate : Logger::Loggable { /** * @return the name of the current predicate. */ - virtual std::string name() const PURE; + virtual absl::string_view name() const PURE; /** * Used to determine if the current url matches the predicate pattern. @@ -35,7 +35,7 @@ class PathMatchPredicate : Logger::Loggable { /** * @return the match pattern of the predicate. */ - virtual std::string pattern() const PURE; + virtual absl::string_view pattern() const PURE; }; using PathMatchPredicateSharedPtr = std::shared_ptr; @@ -57,7 +57,7 @@ class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { /** * @return the category of the rewrite pattern predicate to be created. */ - std::string category() const override { return "envoy.path.match"; } + absl::string_view category() const override { return "envoy.path.match"; } }; } // namespace Router diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index 7108a9f5d960b..87b0946da3beb 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -22,7 +22,7 @@ class PathRewritePredicate : Logger::Loggable { /** * @return the name of the current predicate. */ - virtual std::string_view name() const PURE; + virtual absl::string_view name() const PURE; /** * Used to rewrite the current url to the specified output. Can return a failure in case rewrite @@ -38,7 +38,7 @@ class PathRewritePredicate : Logger::Loggable { /** * @return the rewrite pattern of the predicate. */ - virtual std::string pattern() const PURE; + virtual absl::string_view pattern() const PURE; }; using PathRewritePredicateSharedPtr = std::shared_ptr; @@ -58,12 +58,12 @@ class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { /** * @return the name of the rewrite pattern predicate to be created. */ - virtual std::string name() const override PURE; + virtual absl::string_view name() const override PURE; /** * @return the category of the rewrite pattern predicate to be created. */ - virtual std::string category() const override PURE; + virtual absl::string_view category() const override PURE; }; } // namespace Router From 386d5d2991421b064255bf25784259d20902dd54 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 29 Jul 2022 14:41:17 +0000 Subject: [PATCH 086/238] To string_view Signed-off-by: silverstar195 --- source/extensions/path/match/pattern_template/config.h | 4 ++-- source/extensions/path/rewrite/pattern_template/config.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 917d799f6f594..f0e97473d345f 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -25,11 +25,11 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig>(); } - std::string name() const override { + absl::string_view name() const override { return "envoy.path.match.pattern_template.v3.pattern_template_match_predicate"; } - std::string category() const override { return "envoy.path.match"; } + absl::string_view category() const override { return "envoy.path.match"; } }; } // namespace Match diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 7285be8f6b518..1f44e7705ce65 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -22,11 +22,11 @@ class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredica envoy::extensions::path::rewrite::pattern_template::v3::PatternTemplateRewriteConfig>(); } - std::string name() const override { + absl::string_view name() const override { return "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate"; } - std::string category() const override { return "envoy.path.rewrite"; } + absl::string_view category() const override { return "envoy.path.rewrite"; } }; } // namespace Rewrite From abbadb0aebec539b2d479d54da23f0986ada81ef Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 29 Jul 2022 14:59:25 +0000 Subject: [PATCH 087/238] Small changes Signed-off-by: silverstar195 --- CODEOWNERS | 4 ++++ envoy/router/path_match_policy.h | 4 ++-- envoy/router/path_rewrite_policy.h | 4 ++-- source/extensions/path/match/pattern_template/config.h | 10 ++++++---- .../extensions/path/rewrite/pattern_template/config.h | 10 +++++----- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index a9aa45317bfb8..53843e279cddb 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -265,6 +265,10 @@ extensions/filters/http/oauth2 @derekargueta @snowp /*/extensions/filters/http/header_to_metadata @zuercher @JuniorHsu # zookeeper /*/extensions/filters/network/zookeeper_proxy @snowp @JuniorHsu +# path match by pattern +/*/extensions/path/match/pattern_template @silverstar194 +# path rewrite by pattern +/*/extensions/path/rewrite/pattern_template @silverstar194 # Intentionally exempt (treated as core code) /*/extensions/filters/common @UNOWNED @UNOWNED diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index 9f3506fe5f0e0..18fc2f04fea9d 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -22,7 +22,7 @@ class PathMatchPredicate : Logger::Loggable { /** * @return the name of the current predicate. */ - virtual absl::string_view name() const PURE; + virtual std::string name() const PURE; /** * Used to determine if the current url matches the predicate pattern. @@ -57,7 +57,7 @@ class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { /** * @return the category of the rewrite pattern predicate to be created. */ - absl::string_view category() const override { return "envoy.path.match"; } + std::string category() const override { return "envoy.path.match"; } }; } // namespace Router diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index 87b0946da3beb..69ae8bf0ff9a8 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -58,12 +58,12 @@ class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { /** * @return the name of the rewrite pattern predicate to be created. */ - virtual absl::string_view name() const override PURE; + virtual std::string name() const override PURE; /** * @return the category of the rewrite pattern predicate to be created. */ - virtual absl::string_view category() const override PURE; + virtual std::string category() const override PURE; }; } // namespace Router diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index f0e97473d345f..8f5c97a9fff44 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -16,8 +16,10 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa public: Router::PathMatchPredicateSharedPtr createPathMatchPredicate(const Protobuf::Message& config) override { - // TODO(silverstar195): Implement createPathMatchPredicate - return nullptr; + // TODO(silverstar194): Implement createPathMatchPredicate + const Protobuf::Message& temp = config; + config = temp; + throw absl::UnimplementedError("createPathMatchPredicate not implemented"); } ProtobufTypes::MessagePtr createEmptyConfigProto() override { @@ -25,11 +27,11 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig>(); } - absl::string_view name() const override { + std::string name() const override { return "envoy.path.match.pattern_template.v3.pattern_template_match_predicate"; } - absl::string_view category() const override { return "envoy.path.match"; } + std::string category() const override { return "envoy.path.match"; } }; } // namespace Match diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 1f44e7705ce65..8c83a9025abe6 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -12,9 +12,9 @@ namespace Rewrite { class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredicateFactory { public: Router::PathRewritePredicateSharedPtr - createPathRewritePredicate(const Protobuf::Message& rewrite_config) override { - // TODO(silverstar195): Implement createPathRewritePredicate - return nullptr; + createPathRewritePredicate(const Protobuf::Message& config) override { + // TODO(silverstar194): Implement createPathRewritePredicate + throw absl::UnimplementedError("createPathRewritePredicate not implemented"); } ProtobufTypes::MessagePtr createEmptyConfigProto() override { @@ -22,11 +22,11 @@ class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredica envoy::extensions::path::rewrite::pattern_template::v3::PatternTemplateRewriteConfig>(); } - absl::string_view name() const override { + std::string name() const override { return "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate"; } - absl::string_view category() const override { return "envoy.path.rewrite"; } + std::string category() const override { return "envoy.path.rewrite"; } }; } // namespace Rewrite From 93a0a2c64a56de7e9e17f3e32d536d65e848056c Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 29 Jul 2022 16:16:17 +0000 Subject: [PATCH 088/238] Small changes Signed-off-by: silverstar195 --- source/extensions/path/match/pattern_template/BUILD | 1 - source/extensions/path/match/pattern_template/config.h | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD index 8321eab1802f0..27fcb28d1eeb6 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/pattern_template/BUILD @@ -1,7 +1,6 @@ load( "//bazel:envoy_build_system.bzl", "envoy_cc_extension", - "envoy_cc_library", "envoy_extension_package", ) diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 8f5c97a9fff44..d442e8938a8f2 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -17,9 +17,9 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa Router::PathMatchPredicateSharedPtr createPathMatchPredicate(const Protobuf::Message& config) override { // TODO(silverstar194): Implement createPathMatchPredicate - const Protobuf::Message& temp = config; - config = temp; - throw absl::UnimplementedError("createPathMatchPredicate not implemented"); + const Protobuf::Message& temp = config; + config = temp; + throw absl::UnimplementedError("createPathMatchPredicate not implemented"); } ProtobufTypes::MessagePtr createEmptyConfigProto() override { From b6504c7569b80029de414b5fb49cb550106265e4 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 29 Jul 2022 16:16:50 +0000 Subject: [PATCH 089/238] Small changes Signed-off-by: silverstar195 --- source/extensions/path/rewrite/pattern_template/BUILD | 1 - source/extensions/path/rewrite/pattern_template/config.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/source/extensions/path/rewrite/pattern_template/BUILD b/source/extensions/path/rewrite/pattern_template/BUILD index fb64816312ce4..a02988de27a8a 100644 --- a/source/extensions/path/rewrite/pattern_template/BUILD +++ b/source/extensions/path/rewrite/pattern_template/BUILD @@ -1,7 +1,6 @@ load( "//bazel:envoy_build_system.bzl", "envoy_cc_extension", - "envoy_cc_library", "envoy_extension_package", ) diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 8c83a9025abe6..5a197ac1b171d 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -14,7 +14,7 @@ class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredica Router::PathRewritePredicateSharedPtr createPathRewritePredicate(const Protobuf::Message& config) override { // TODO(silverstar194): Implement createPathRewritePredicate - throw absl::UnimplementedError("createPathRewritePredicate not implemented"); + throw absl::UnimplementedError("createPathRewritePredicate not implemented"); } ProtobufTypes::MessagePtr createEmptyConfigProto() override { From 1366abd4d6df6adae13ef546a0003ada1a25648f Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 29 Jul 2022 17:59:15 +0000 Subject: [PATCH 090/238] Add pattern lib Signed-off-by: silverstar195 --- .../path/pattern_template_lib/BUILD | 78 +++ .../pattern_template_lib/pattern_template.cc | 140 ++++++ .../pattern_template_lib/pattern_template.h | 57 +++ .../pattern_template_internal.cc | 384 ++++++++++++++ .../pattern_template_internal.h | 97 ++++ .../pattern_template_internal_test.cc | 470 ++++++++++++++++++ .../pattern_template_test.cc | 337 +++++++++++++ 7 files changed, 1563 insertions(+) create mode 100644 source/extensions/path/pattern_template_lib/BUILD create mode 100644 source/extensions/path/pattern_template_lib/pattern_template.cc create mode 100644 source/extensions/path/pattern_template_lib/pattern_template.h create mode 100644 source/extensions/path/pattern_template_lib/pattern_template_internal.cc create mode 100644 source/extensions/path/pattern_template_lib/pattern_template_internal.h create mode 100644 source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc create mode 100644 source/extensions/path/pattern_template_lib/pattern_template_test.cc diff --git a/source/extensions/path/pattern_template_lib/BUILD b/source/extensions/path/pattern_template_lib/BUILD new file mode 100644 index 0000000000000..3a6cb161fe08e --- /dev/null +++ b/source/extensions/path/pattern_template_lib/BUILD @@ -0,0 +1,78 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_library", + "envoy_cc_test", + "envoy_extension_package", +) + +licenses(["notice"]) # Apache 2 + +# Wildcard & Pattern Matching + +envoy_extension_package() + +envoy_cc_library( + name = "pattern_template_lib", + srcs = ["pattern_template.cc"], + hdrs = ["pattern_template.h"], + visibility = [ + "//source/common/router:__subpackages__", + "//source/extensions/path:__subpackages__", + "//test/extensions/path:__subpackages__", + ], + deps = [ + ":pattern_template_internal_cc", + "//envoy/router:router_path_match_policy_interface", + "//source/common/http:path_utility_lib", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_googlesource_code_re2//:re2", + ], +) + +envoy_cc_library( + name = "pattern_template_internal_cc", + srcs = ["pattern_template_internal.cc"], + hdrs = ["pattern_template_internal.h"], + deps = [ + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/functional:function_ref", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + "@com_google_absl//absl/types:variant", + "@com_googlesource_code_re2//:re2", + ], +) + +envoy_cc_test( + name = "pattern_template_test", + srcs = ["pattern_template_test.cc"], + deps = [ + ":pattern_template", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "//test/test_common:status_utility_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + ], +) + +envoy_cc_test( + name = "pattern_template_internal_test", + srcs = ["pattern_template_internal_test.cc"], + deps = [ + ":pattern_template_internal_cc", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "//test/test_common:status_utility_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_googlesource_code_re2//:re2", + ], +) diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc new file mode 100644 index 0000000000000..17b754ffd1679 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -0,0 +1,140 @@ +#include "source/extensions/path/pattern_template_lib/pattern_template.h" + +#include +#include +#include +#include + +#include "source/common/http/path_utility.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" + +#include "absl/status/statusor.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "re2/re2.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +using PatternTemplateInternal::ParsedUrlPattern; + +#ifndef SWIG +// Silence warnings about missing initializers for members of LazyRE2. +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.data(), text.size()}; } + +absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { + + absl::StatusOr status = + PatternTemplateInternal::parseURLPatternSyntax(url_pattern); + if (!status.ok()) { + return status.status(); + } + struct ParsedUrlPattern pattern = *std::move(status); + return PatternTemplateInternal::toRegexPattern(pattern); +} + +absl::StatusOr> +parseRewritePatternHelper(absl::string_view pattern) { + std::vector result; + + // Don't allow contiguous '/' patterns. + static const LazyRE2 invalid_regex = {"^.*//.*$"}; + if (RE2::FullMatch(ToStringPiece(pattern), *invalid_regex)) { + return absl::InvalidArgumentError("Invalid rewrite literal pattern"); + } + + // The pattern should start with a '/' and thus the first segment should + // always be a literal. + if (pattern.empty() || pattern[0] != '/') { + return absl::InvalidArgumentError("Invalid rewrite variable placement"); + } + while (!pattern.empty()) { + std::vector segments1 = absl::StrSplit(pattern, absl::MaxSplits('{', 1)); + if (!segments1[0].empty()) { + if (!PatternTemplateInternal::isValidRewriteLiteral(segments1[0])) { + return absl::InvalidArgumentError("Invalid rewrite literal pattern"); + } + result.emplace_back(segments1[0], RewriteStringKind::kLiteral); + } + + if (segments1.size() < 2) { + // No more variable replacement, done. + break; + } + + std::vector segments2 = + absl::StrSplit(segments1[1], absl::MaxSplits('}', 1)); + if (segments2.size() < 2) { + return absl::InvalidArgumentError("Unmatched variable bracket"); + } + pattern = segments2[1]; + + if (!PatternTemplateInternal::isValidIndent(segments2[0])) { + return absl::InvalidArgumentError("Invalid variable name"); + } + result.emplace_back(segments2[0], RewriteStringKind::kVariable); + } + return result; +} + +absl::StatusOr +parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { + envoy::extensions::pattern_template::PatternTemplateRewriteSegments parsed_pattern; + RE2 regex = RE2(ToStringPiece(capture_regex)); + if (!regex.ok()) { + return absl::InternalError(regex.error()); + } + + absl::StatusOr> status = parseRewritePatternHelper(pattern); + if (!status.ok()) { + return status.status(); + } + std::vector processed_pattern = *std::move(status); + + const std::map& capture_index_map = regex.NamedCapturingGroups(); + + for (const auto& [str, kind] : processed_pattern) { + switch (kind) { + case RewriteStringKind::kLiteral: + parsed_pattern.add_segments()->set_literal(std::string(str)); + break; + case RewriteStringKind::kVariable: + auto it = capture_index_map.find(std::string(str)); + if (it == capture_index_map.end()) { + return absl::InvalidArgumentError("Nonexisting variable name"); + } + parsed_pattern.add_segments()->set_var_index(it->second); + break; + } + } + + return parsed_pattern; +} + +absl::Status isValidMatchPattern(const std::string path_template_match) { + return convertURLPatternSyntaxToRegex(path_template_match).status(); +} + +absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { + return parseRewritePatternHelper(path_template_rewrite).status(); +} + +absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, + const std::string& capture_regex) { + + absl::StatusOr status = convertURLPatternSyntaxToRegex(capture_regex).value(); + if (!status.ok()) { + return status.status(); + } + + return parseRewritePattern(path_template_rewrite, *std::move(status)).status(); +} + +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h new file mode 100644 index 0000000000000..05d3034f53073 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -0,0 +1,57 @@ +#ifndef SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H +#define SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H + +#include + +#include "envoy/router/path_match_policy.h" + +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +enum class RewriteStringKind { kVariable, kLiteral }; + +struct RewritePatternSegment { + RewritePatternSegment(absl::string_view str, RewriteStringKind kind) : str(str), kind(kind) {} + absl::string_view str; + RewriteStringKind kind; +}; + +// Returns the regex pattern that is equivalent to the given url_pattern. +// Used in the config pipeline to translate user given url pattern to +// the safe regex Envoy can understand. Strips away any variable captures. +absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); + +// Helper function that parses the pattern and breaks it down to either +// literals or variable names. To be used by ParseRewritePattern(). +// Exposed here so that the validator for the rewrite pattern can also +// use it. +absl::StatusOr> +parseRewritePatternHelper(absl::string_view pattern); + +// Returns the parsed Url rewrite pattern to be used by +// RewriteURLTemplatePattern() |capture_regex| should +// be the regex generated by ConvertURLPatternSyntaxToRegex(). +absl::StatusOr +parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); + +// Returns if provided rewrite pattern is valid +absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); + +// Returns if path_template and rewrite_template have valid variables +absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, + const std::string& capture_regex); + +absl::Status isValidMatchPattern(const std::string match_pattern); + +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy + +#endif // SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc new file mode 100644 index 0000000000000..889eb5834fdfd --- /dev/null +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc @@ -0,0 +1,384 @@ +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" + +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/flags/flag.h" +#include "absl/functional/function_ref.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_replace.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "absl/types/variant.h" +#include "re2/re2.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +namespace PatternTemplateInternal { + +namespace { + +#ifndef SWIG +// Silence warnings about missing initializers for members of LazyRE2. +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +unsigned long pattern_matching_max_variables_per_url = 5; +unsigned long pattern_matching_max_variable_name_len = 16; +unsigned long pattern_matching_min_variable_name_len = 1; + +// Valid pchar from https://datatracker.ietf.org/doc/html/rfc3986#appendix-A +constexpr absl::string_view kLiteral = "a-zA-Z0-9-._~" // Unreserved + "%" // pct-encoded + "!$&'()+,;" // sub-delims excluding *= + ":@"; + +// Default operator used for the variable when none specified. +constexpr Operator kDefaultVariableOperator = Operator::kPathGlob; + +// Visitor for displaying debug info of a ParsedSegment/Variable.var_match. +struct ToStringVisitor { + template std::string operator()(const T& val) const; +}; + +// Formatter used to allow joining variants together with StrJoin. +struct ToStringFormatter { + template void operator()(std::string* out, const T& t) const { + absl::StrAppend(out, absl::visit(ToStringVisitor(), t)); + } +}; + +// Visitor for converting a ParsedSegment variant to the regex. +struct ToRegexPatternVisitor { + template std::string operator()(const T& val) const { return toRegexPattern(val); } +}; + +// Formatter used to allow joining variants together with StrJoin. +struct ToRegexPatternFormatter { + template void operator()(std::string* out, const T& t) const { + absl::StrAppend(out, absl::visit(ToRegexPatternVisitor(), t)); + } +}; + +std::string toString(const Literal val) { return std::string(val); } + +std::string toString(const Operator val) { + switch (val) { + case Operator::kPathGlob: + return "*"; + case Operator::kTextGlob: + return "**"; + } +} + +std::string toString(const Variable val) { + if (val.var_match.empty()) { + return absl::StrCat("{", val.var_name, "}"); + } + + return absl::StrCat("{", val.var_name, "=", + absl::StrJoin(val.var_match, "/", ToStringFormatter()), "}"); +} + +template std::string ToStringVisitor::operator()(const T& val) const { + return toString(val); +} + +template +absl::StatusOr AlsoUpdatePattern( + absl::FunctionRef>(absl::string_view)> consume_func, + absl::string_view* patt) { + + absl::StatusOr> status = consume_func(*patt); + if (!status.ok()) { + return status.status(); + } + ParsedResult result = *std::move(status); + + *patt = result.unconsumed_pattern; + return result.parsed_value; +} + +} // namespace + +std::string Variable::DebugString() const { return toString(*this); } + +std::string ParsedUrlPattern::DebugString() const { + return absl::StrCat("/", absl::StrJoin(parsed_segments, "/", ToStringFormatter()), + suffix.value_or("")); +} + +bool isValidLiteral(absl::string_view pattern) { + static const std::string* kValidLiteralRegex = + new std::string(absl::StrCat("^[", kLiteral, "]+$")); + static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; + return RE2::FullMatch(toStringPiece(pattern), *literal_regex); +} + +bool isValidRewriteLiteral(absl::string_view pattern) { + static const std::string* kValidLiteralRegex = + new std::string(absl::StrCat("^[", kLiteral, "/]+$")); + static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; + return RE2::FullMatch(toStringPiece(pattern), *literal_regex); +} + +bool isValidIndent(absl::string_view pattern) { + static const LazyRE2 ident_regex = {"^[a-zA-Z][a-zA-Z0-9_]*$"}; + return RE2::FullMatch(toStringPiece(pattern), *ident_regex); +} + +absl::StatusOr> consumeLiteral(absl::string_view pattern) { + absl::string_view lit = + std::vector(absl::StrSplit(pattern, absl::MaxSplits('/', 1)))[0]; + absl::string_view unconsumed_pattern = pattern.substr(lit.size()); + if (!isValidLiteral(lit)) { + return absl::InvalidArgumentError("Invalid literal"); + } + return ParsedResult(lit, unconsumed_pattern); +} + +absl::StatusOr> consumeOperator(absl::string_view pattern) { + if (absl::StartsWith(pattern, "**")) { + return ParsedResult(Operator::kTextGlob, pattern.substr(2)); + } + if (absl::StartsWith(pattern, "*")) { + return ParsedResult(Operator::kPathGlob, pattern.substr(1)); + } + return absl::InvalidArgumentError("Invalid Operator"); +} + +absl::StatusOr> consumeVariable(absl::string_view pattern) { + // Locate the variable pattern to parse. + if (pattern.size() < 2 || (pattern)[0] != '{') { + return absl::InvalidArgumentError("Invalid variable"); + } + std::vector parts = absl::StrSplit(pattern.substr(1), absl::MaxSplits('}', 1)); + if (parts.size() != 2) { + return absl::InvalidArgumentError("Unmatched variable bracket"); + } + absl::string_view unconsumed_pattern = parts[1]; + + // Parse the actual variable pattern, starting with the variable name. + std::vector var_parts = absl::StrSplit(parts[0], absl::MaxSplits('=', 1)); + if (!isValidIndent(var_parts[0])) { + return absl::InvalidArgumentError("Invalid variable name"); + } + Variable var = Variable(var_parts[0], {}); + + // Parse the variable match pattern (if any). + if (var_parts.size() < 2) { + return ParsedResult(var, unconsumed_pattern); + } + absl::string_view var_patt = var_parts[1]; + if (var_patt.empty()) { + return absl::InvalidArgumentError("Empty variable match"); + } + while (!var_patt.empty()) { + absl::variant var_match; + if (var_patt[0] == '*') { + + absl::StatusOr status = AlsoUpdatePattern(consumeOperator, &var_patt); + if (!status.ok()) { + return status.status(); + } + var_match = *std::move(status); + + } else { + + absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &var_patt); + if (!status.ok()) { + return status.status(); + } + var_match = *std::move(status); + } + var.var_match.push_back(var_match); + if (!var_patt.empty()) { + if (var_patt[0] != '/' || var_patt.size() == 1) { + return absl::InvalidArgumentError("Invalid variable match"); + } + var_patt = var_patt.substr(1); + } + } + + return ParsedResult(var, unconsumed_pattern); +} + +absl::StatusOr> +gatherCaptureNames(struct ParsedUrlPattern pattern) { + absl::flat_hash_set captured_variables; + + for (const ParsedSegment& segment : pattern.parsed_segments) { + if (!absl::holds_alternative(segment)) { + continue; + } + if (captured_variables.size() >= pattern_matching_max_variables_per_url) { + return absl::InvalidArgumentError("Exceeded variable count limit"); + } + absl::string_view var_name = absl::get(segment).var_name; + + if (var_name.size() < pattern_matching_min_variable_name_len || + var_name.size() > pattern_matching_max_variable_name_len) { + return absl::InvalidArgumentError("Invalid variable length"); + } + if (captured_variables.contains(var_name)) { + return absl::InvalidArgumentError("Repeated variable name"); + } + captured_variables.emplace(var_name); + } + + return captured_variables; +} + +absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { + bool seen_text_glob = false; + for (const ParsedSegment& segment : pattern.parsed_segments) { + if (absl::holds_alternative(segment)) { + if (seen_text_glob) { + return absl::InvalidArgumentError("Glob after text glob."); + } + seen_text_glob = (absl::get(segment) == Operator::kTextGlob); + } else if (absl::holds_alternative(segment)) { + const Variable& var = absl::get(segment); + if (var.var_match.empty()) { + if (seen_text_glob) { + // A variable with no explicit matcher is treated as a path glob. + return absl::InvalidArgumentError("Implicit variable path glob after text glob."); + } + } else { + for (const absl::variant& var_seg : var.var_match) { + if (!absl::holds_alternative(var_seg)) { + continue; + } + if (seen_text_glob) { + return absl::InvalidArgumentError("Glob after text glob."); + } + seen_text_glob = (absl::get(var_seg) == Operator::kTextGlob); + } + } + } + } + return absl::OkStatus(); +} + +absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern) { + struct ParsedUrlPattern parsed_pattern; + + static const LazyRE2 printable_regex = {"^/[[:graph:]]*$"}; + if (!RE2::FullMatch(toStringPiece(url_pattern), *printable_regex)) { + + return absl::InvalidArgumentError("Invalid pattern"); + } + + // Consume the leading '/' + url_pattern = url_pattern.substr(1); + + // Do the initial lexical parsing. + while (!url_pattern.empty()) { + ParsedSegment segment; + if (url_pattern[0] == '*') { + + absl::StatusOr status = AlsoUpdatePattern(consumeOperator, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } else if (url_pattern[0] == '{') { + + absl::StatusOr status = AlsoUpdatePattern(consumeVariable, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } else { + + absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } + parsed_pattern.parsed_segments.push_back(segment); + + // Deal with trailing '/' or suffix. + if (!url_pattern.empty()) { + if (url_pattern == "/") { + // Single trailing '/' at the end, mark this with empty literal. + parsed_pattern.parsed_segments.emplace_back(""); + break; + } else if (url_pattern[0] == '/') { + // Have '/' followed by more text, consume the '/'. + url_pattern = url_pattern.substr(1); + } else { + // Not followed by '/', treat as suffix. + + absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &url_pattern); + if (!status.ok()) { + return status.status(); + } + parsed_pattern.suffix = *std::move(status); + if (!url_pattern.empty()) { + // Suffix didn't consume whole remaining pattern ('/' in url_pattern). + return absl::InvalidArgumentError("Prefix match not supported."); + } + break; + } + } + } + absl::StatusOr> status = + gatherCaptureNames(parsed_pattern); + if (!status.ok()) { + return status.status(); + } + parsed_pattern.captured_variables = *std::move(status); + + absl::Status validate_status = validateNoOperatorAfterTextGlob(parsed_pattern); + if (!validate_status.ok()) { + return validate_status; + } + + return parsed_pattern; +} + +std::string toRegexPattern(absl::string_view pattern) { + return absl::StrReplaceAll( + pattern, {{"$", "\\$"}, {"(", "\\("}, {")", "\\)"}, {"+", "\\+"}, {".", "\\."}}); +} + +std::string toRegexPattern(Operator pattern) { + static const std::string* kPathGlobRegex = new std::string(absl::StrCat("[", kLiteral, "]+")); + static const std::string* kTextGlobRegex = new std::string(absl::StrCat("[", kLiteral, "/]*")); + switch (pattern) { + case Operator::kPathGlob: // "*" + return *kPathGlobRegex; + case Operator::kTextGlob: // "**" + return *kTextGlobRegex; + } +} + +std::string toRegexPattern(const Variable& pattern) { + return absl::StrCat("(?P<", pattern.var_name, ">", + pattern.var_match.empty() + ? toRegexPattern(kDefaultVariableOperator) + : absl::StrJoin(pattern.var_match, "/", ToRegexPatternFormatter()), + ")"); +} + +std::string toRegexPattern(const struct ParsedUrlPattern& pattern) { + return absl::StrCat("/", absl::StrJoin(pattern.parsed_segments, "/", ToRegexPatternFormatter()), + toRegexPattern(pattern.suffix.value_or(""))); +} + +} // namespace PatternTemplateInternal +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h new file mode 100644 index 0000000000000..061ef6952da87 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.h @@ -0,0 +1,97 @@ +#ifndef SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H +#define SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H + +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "absl/types/optional.h" +#include "absl/types/variant.h" +#include "re2/re2.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +namespace PatternTemplateInternal { + +using Literal = absl::string_view; +enum class Operator { kPathGlob, kTextGlob }; + +struct RewriteSegment { + // Represents a segment of the rewritten URL, including any path segments, + // slash and prefix. + absl::string_view literal; + + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int var_index; +}; + +struct Variable { + absl::string_view var_name; + std::vector> var_match; + + Variable(absl::string_view name, std::vector> match) + : var_name(name), var_match(match) {} + + std::string DebugString() const; +}; + +using ParsedSegment = absl::variant; + +struct ParsedUrlPattern { + std::vector parsed_segments; + absl::optional suffix; + absl::flat_hash_set captured_variables; + + std::string DebugString() const; +}; + +bool isValidLiteral(absl::string_view pattern); + +bool isValidRewriteLiteral(absl::string_view pattern); + +bool isValidIndent(absl::string_view pattern); + +// Used by the following Consume{Literal.Operator,Variable} functions +// in the return value. The functions would take the given pattern, +// parse what it can into |parsed_value| and return the unconsumed +// portion of the pattern in |unconsumed_pattern|. +template struct ParsedResult { + ParsedResult(T val, absl::string_view pattern) : parsed_value(val), unconsumed_pattern(pattern) {} + + T parsed_value; + absl::string_view unconsumed_pattern; +}; + +absl::StatusOr> consumeLiteral(absl::string_view pattern); + +absl::StatusOr> consumeOperator(absl::string_view pattern); + +absl::StatusOr> consumeVariable(absl::string_view pattern); + +absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern); + +std::string toRegexPattern(Literal pattern); + +std::string toRegexPattern(Operator pattern); + +std::string toRegexPattern(const Variable& pattern); + +std::string toRegexPattern(const struct ParsedUrlPattern& pattern); + +inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } + +} // namespace PatternTemplateInternal +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy + +#endif // SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc new file mode 100644 index 0000000000000..55c0124b62ef3 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc @@ -0,0 +1,470 @@ +#include +#include +#include +#include +#include +#include + +#include "source/common/common/assert.h" +#include "source/common/protobuf/protobuf.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" + +#include "test/test_common/logging.h" +#include "test/test_common/status_utility.h" +#include "test/test_common/utility.h" + +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "gtest/gtest.h" +#include "re2/re2.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +namespace PatternTemplateInternal { + +namespace { + +using ::Envoy::StatusHelpers::StatusIs; + +TEST(InternalParsing, ParsedUrlDebugString) { + ParsedUrlPattern patt1 = { + { + "abc", + "def", + Operator::kPathGlob, + Variable("var", {Operator::kPathGlob, "ghi", Operator::kTextGlob}), + }, + ".test", + {}, + }; + EXPECT_EQ(patt1.DebugString(), "/abc/def/*/{var=*/ghi/**}.test"); + + ParsedUrlPattern patt2 = {{ + Variable("var", {}), + }, + "", + {}}; + EXPECT_EQ(patt2.DebugString(), "/{var}"); +} + +TEST(InternalParsing, isValidLiteralWorks) { + EXPECT_TRUE(isValidLiteral("123abcABC")); + EXPECT_TRUE(isValidLiteral("._~-")); + EXPECT_TRUE(isValidLiteral("-._~%20!$&'()+,;:@")); + EXPECT_FALSE(isValidLiteral("`~!@#$%^&()-_+;:,<.>'\"\\| ")); + EXPECT_FALSE(isValidLiteral("abc/")); + EXPECT_FALSE(isValidLiteral("ab*c")); + EXPECT_FALSE(isValidLiteral("a**c")); + EXPECT_FALSE(isValidLiteral("a=c")); + EXPECT_FALSE(isValidLiteral("?abc")); + EXPECT_FALSE(isValidLiteral("?a=c")); + EXPECT_FALSE(isValidLiteral("{abc")); + EXPECT_FALSE(isValidLiteral("abc}")); + EXPECT_FALSE(isValidLiteral("{abc}")); +} + +TEST(InternalParsing, IsValidRewriteLiteralWorks) { + EXPECT_TRUE(isValidRewriteLiteral("123abcABC")); + EXPECT_TRUE(isValidRewriteLiteral("abc/")); + EXPECT_TRUE(isValidRewriteLiteral("abc/def")); + EXPECT_TRUE(isValidRewriteLiteral("/abc.def")); + EXPECT_TRUE(isValidRewriteLiteral("._~-")); + EXPECT_TRUE(isValidRewriteLiteral("-._~%20!$&'()+,;:@")); + EXPECT_FALSE(isValidRewriteLiteral("`~!@#$%^&()-_+;:,<.>'\"| ")); + EXPECT_FALSE(isValidRewriteLiteral("ab}c")); + EXPECT_FALSE(isValidRewriteLiteral("ab{c")); + EXPECT_FALSE(isValidRewriteLiteral("a=c")); + EXPECT_FALSE(isValidRewriteLiteral("?a=c")); +} + +TEST(InternalParsing, IsValidIndentWorks) { + EXPECT_TRUE(isValidIndent("abc")); + EXPECT_TRUE(isValidIndent("ABC_def_123")); + EXPECT_TRUE(isValidIndent("a1")); + EXPECT_TRUE(isValidIndent("T")); + EXPECT_FALSE(isValidIndent("123")); + EXPECT_FALSE(isValidIndent("__undefined__")); + EXPECT_FALSE(isValidIndent("abc-def")); + EXPECT_FALSE(isValidIndent("abc=def")); + EXPECT_FALSE(isValidIndent("abc def")); + EXPECT_FALSE(isValidIndent("a!!!")); +} + +TEST(InternalParsing, ConsumeLiteralWorks) { + std::string pattern = "abc/123"; + + absl::StatusOr> result = consumeLiteral(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, "abc"); + EXPECT_EQ(result->unconsumed_pattern, "/123"); +} + +TEST(InternalParsing, ConsumeTextGlob) { + std::string pattern = "***abc/123"; + + absl::StatusOr> result = consumeOperator(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, Operator::kTextGlob); + EXPECT_EQ(result->unconsumed_pattern, "*abc/123"); +} + +TEST(InternalParsing, ConsumePathGlob) { + std::string pattern = "*/123"; + + absl::StatusOr> result = consumeOperator(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, Operator::kPathGlob); + EXPECT_EQ(result->unconsumed_pattern, "/123"); +} + +class ConsumeVariableSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ConsumeVariableSuccessTestSuite, ConsumeVariableSuccess, + testing::Values("{var=*}", "{Var}", "{v1=**}", "{v_1=*/abc/**}", + "{v3=abc}", "{v=123/*/*}", "{var=abc/*/def}")); + +TEST_P(ConsumeVariableSuccess, ConsumeVariableSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + absl::StatusOr> result = consumeVariable(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value.DebugString(), pattern); + EXPECT_TRUE(result->unconsumed_pattern.empty()); +} + +class ConsumeVariableFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ConsumeVariableFailureTestSuite, ConsumeVariableFailure, + testing::Values("{var", "{=abc}", "{_var=*}", "{1v}", "{1v=abc}", + "{var=***}", "{v-a-r}", "{var=*/abc?q=1}", "{var=abc/a*}", + "{var=*def/abc}", "{var=}", "{var=abc=def}", + "{rc=||||(A+yl/}")); + +TEST_P(ConsumeVariableFailure, ConsumeVariableFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(consumeVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class parseURLPatternSyntaxSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P( + parseURLPatternSyntaxSuccessTestSuite, parseURLPatternSyntaxSuccess, + testing::Values("/**.m3u8", "/**.mpd", "/*_suf", "/{path=**}.m3u8", "/{foo}/**.ts", + "/media/*.m4s", "/media/{contentId=*}/**", "/media/*", "/api/*/*/**", + "/api/*/v1/**", "/api/*/v1/*", "/{version=api/*}/*", "/api/*/*/", + "/api/*/1234/", "/api/*/{resource=*}/{method=*}", + "/api/*/{resource=*}/{method=**}", "/v1/**", "/media/{country}/{lang=*}/**", + "/{foo}/{bar}/{fo}/{fum}/*", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", + "/media/{id=*}/*", "/media/{contentId=**}", + "/api/{version}/projects/{project}/locations/{location}/{resource}/" + "{name}", + "/api/{version=*}/{url=**}", "/api/{VERSION}/{version}/{verSION}", + "/api/1234/abcd", "/media/abcd/%10%20%30/{v1=*/%10%20}_suffix", "/")); + +TEST_P(ParseURLPatternSyntaxSuccess, ParseURLPatternSyntaxSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + absl::StatusOr parsed_patt = parseURLPatternSyntax(pattern); + ASSERT_OK(parsed_patt); + EXPECT_EQ(parsed_patt->DebugString(), pattern); +} + +class parseURLPatternSyntaxFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P( + ParseURLPatternSyntaxFailureTestSuite, ParseURLPatternSyntaxFailure, + testing::Values("/api/v*/1234", "/api/{version=v*}/1234", "/api/v{versionNum=*}/1234", + "/api/{version=*beta}/1234", "/media/eff456/ll-sd-out.{ext}", + "/media/eff456/ll-sd-out.{ext=*}", "/media/eff456/ll-sd-out.**", + "/media/{country=**}/{lang=*}/**", "/media/**/*/**", "/link/{id=*}/asset*", + "/link/{id=*}/{asset=asset*}", "/media/{id=/*}/*", "/media/{contentId=/**}", + "/api/{version}/{version}", "/api/{version.major}/{version.minor}", + "/media/***", "/media/*{*}*", "/media/{*}/", "/media/*/index?a=2", "media", + "/\001\002\003\004\005\006\007", "/*(/**", "/**/{var}", + "/{var1}/{var2}/{var3}/{var4}/{var5}/{var6}", "/{=*}", + "/{var12345678901234=*}")); + +TEST_P(ParseURLPatternSyntaxFailure, ParseURLPatternSyntaxFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(parseURLPatternSyntax(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(InternalRegexGen, LiteralEscapes) { + EXPECT_EQ(toRegexPattern("abcABC123/-._~%20!$&'()+,;:@"), + "abcABC123/-\\._~%20!\\$&'\\(\\)\\+,;:@"); +} + +TEST(InternalRegexGen, LiteralMatches) { + absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; + + EXPECT_TRUE(RE2::FullMatch(toStringPiece(kPattern), toRegexPattern(kPattern))); +} + +TEST(InternalRegexGen, LiteralMatchesInNamedCapture) { + absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; + + RE2 regex = RE2(absl::StrCat("(?P", toRegexPattern(kPattern), ")")); + ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); + + // Full matched string + capture groups + std::vector captures(2); + ASSERT_TRUE(regex.Match(toStringPiece(kPattern), /*startpos=*/0, /*endpos=*/kPattern.size(), + RE2::ANCHOR_BOTH, captures.data(), captures.size())); + + // Index 0 would be the full text of the matched string. + EXPECT_EQ(toStringPiece(kPattern), captures[0]); + // Get the pattern matched with the named capture group. + EXPECT_EQ(toStringPiece(kPattern), captures.at(regex.NamedCapturingGroups().at("var"))); +} + +TEST(InternalRegexGen, LiteralOnlyMatchesItself) { + constexpr absl::string_view kChars = "abcABC123/-._~%20!$&'()+,;:@"; + + for (const char c : kChars) { + std::string s = {'z', c, 'z'}; + EXPECT_TRUE(RE2::FullMatch(s, toRegexPattern(s))); + EXPECT_FALSE(RE2::FullMatch("zzz", toRegexPattern(s))); + } +} + +TEST(InternalRegexGen, RegexLikePatternIsMatchedLiterally) { + EXPECT_TRUE(RE2::FullMatch("(abc)", toRegexPattern("(abc)"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); + + EXPECT_TRUE(RE2::FullMatch("(abc)+", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("abcabc", toRegexPattern("(abc)+"))); + + EXPECT_TRUE(RE2::FullMatch(".+", toRegexPattern(".+"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern(".+"))); + + EXPECT_TRUE(RE2::FullMatch("a+", toRegexPattern("a+"))); + EXPECT_FALSE(RE2::FullMatch("aa", toRegexPattern("a+"))); +} + +TEST(InternalRegexGen, DollarSignMatchesIfself) { + EXPECT_TRUE(RE2::FullMatch("abc$", toRegexPattern("abc$"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("abc$"))); +} + +TEST(InternalRegexGen, OperatorRegexPattern) { + EXPECT_EQ(toRegexPattern(Operator::kPathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); + EXPECT_EQ(toRegexPattern(Operator::kTextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); +} + +TEST(InternalRegexGen, PathGlobRegex) { + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kPathGlob))); +} + +TEST(InternalRegexGen, TextGlobRegex) { + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kTextGlob))); + EXPECT_TRUE(RE2::FullMatch("", toRegexPattern(Operator::kTextGlob))); + EXPECT_TRUE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kTextGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kTextGlob))); +} + +TEST(InternalRegexGen, VariableRegexPattern) { + EXPECT_EQ(toRegexPattern(Variable("var1", {})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"); + EXPECT_EQ(toRegexPattern(Variable("var2", {Operator::kPathGlob, "abc", Operator::kTextGlob})), + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+/abc/" + "[a-zA-Z0-9-._~%!$&'()+,;:@/]*)"); +} + +TEST(InternalRegexGen, VariableRegexDefaultMatch) { + absl::StatusOr> var = consumeVariable("{var}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc", toRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc"); +} + +TEST(InternalRegexGen, VariableRegexDefaultNotMatch) { + absl::StatusOr> var = consumeVariable("{var}"); + ASSERT_OK(var); + + EXPECT_FALSE(RE2::FullMatch("abc/def", toRegexPattern(var->parsed_value))); +} + +TEST(InternalRegexGen, VariableRegexSegmentsMatch) { + absl::StatusOr> var = consumeVariable("{var=abc/*/def}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc/123/def"); +} + +TEST(InternalRegexGen, VariableRegexTextGlobMatch) { + absl::StatusOr> var = consumeVariable("{var=**/def}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc/123/def"); +} + +TEST(InternalRegexGen, VariableRegexNamedCapture) { + re2::StringPiece kPattern = "abc"; + absl::StatusOr> var = consumeVariable("{var=*}"); + ASSERT_OK(var); + + RE2 regex = RE2(toRegexPattern(var->parsed_value)); + ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); + + // Full matched string + capture groups + std::vector captures(2); + ASSERT_TRUE(regex.Match(kPattern, /*startpos=*/0, /*endpos=*/kPattern.size(), RE2::ANCHOR_BOTH, + captures.data(), captures.size())); + + // Index 0 would be the full text of the matched string. + EXPECT_EQ(kPattern, captures[0]); + // Get the pattern matched with the named capture group. + EXPECT_EQ(kPattern, captures.at(regex.NamedCapturingGroups().at("var"))); +} + +TEST(InternalRegexGen, ParsedURLPatternToRegex) { + absl::StatusOr pattern = + parseURLPatternSyntax("/abc/*/{var1}/def/{var2=*/ghi/**}.jkl"); + ASSERT_OK(pattern); + + std::string var1_capture; + std::string var2_capture; + EXPECT_TRUE(RE2::FullMatch("/abc/123/456/def/789/ghi/%20/($).jkl", + toRegexPattern(pattern.value()), &var1_capture, &var2_capture)); + EXPECT_EQ(var1_capture, "456"); + EXPECT_EQ(var2_capture, "789/ghi/%20/($)"); +} + +struct GenPatternTestCase { + GenPatternTestCase(std::string request_path, std::string url_pattern, + std::vector> capture_pairs) + : path(request_path), pattern(url_pattern), captures(capture_pairs) {} + std::string path; + std::string pattern; + std::vector> captures; +}; + +class GenPatternRegexWithMatch : public testing::TestWithParam { +protected: + const std::string& request_path() const { return GetParam().path; } + const std::string& url_pattern() const { return GetParam().pattern; } + std::vector> const var_values() { + return GetParam().captures; + } +}; + +INSTANTIATE_TEST_SUITE_P( + GenPatternRegexWithMatchTestSuite, GenPatternRegexWithMatch, + testing::Values( + GenPatternTestCase("/media/1234/manifest.m3u8", "/**.m3u8", {}), + GenPatternTestCase("/manifest.mpd", "/**.mpd", {}), + GenPatternTestCase("/media/1234/manifest.m3u8", "/{path=**}.m3u8", + {{"path", "media/1234/manifest"}}), + GenPatternTestCase("/foo/12314341/format/123/hls/segment_0000000001.ts", "/{foo}/**.ts", + {{"foo", "foo"}}), + GenPatternTestCase("/media/eff456/ll-sd-out.js", "/media/{contentId=*}/**", + {{"contentId", "eff456"}}), + GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/*/**", {}), + GenPatternTestCase("/api/v1/1234", "/{version=api/*}/*", {{"version", "api/v1"}}), + GenPatternTestCase("/api/v1/1234/", "/api/*/*/", {}), + GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=**}", + {{"resource", "1234"}, {"method", "broadcasts/get"}}), + GenPatternTestCase("/v1/broadcasts/12345/live", "/v1/**", {}), + GenPatternTestCase("/media/us/en/12334/subtitle_enUS_00101.vtt", + "/media/{country}/{lang=*}/**", {{"country", "us"}, {"lang", "en"}}), + GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo}/{bar}/{fo}/{fum}/*", + {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), + GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", + {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), + GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{id=*}/**", {{"id", "1234"}}), + GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{contentId=**}", + {{"contentId", "1234/hls/1001011.m3u8"}}), + GenPatternTestCase("/api/v1/projects/my-project/locations/global/edgeCacheOrigins/foo", + "/api/{version}/projects/{project}/locations/{location}/{resource}/" + "{name}", + {{"version", "v1"}, + {"project", "my-project"}, + {"location", "global"}, + {"resource", "edgeCacheOrigins"}, + {"name", "foo"}}), + GenPatternTestCase("/api/v1/foo/bar/baz/", "/api/{version=*}/{url=**}", + {{"version", "v1"}, {"url", "foo/bar/baz/"}}), + GenPatternTestCase("/api/v1/v2/v3", "/api/{VERSION}/{version}/{verSION}", + {{"VERSION", "v1"}, {"version", "v2"}, {"verSION", "v3"}}))); + +TEST_P(GenPatternRegexWithMatch, WithCapture) { + absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); + ASSERT_OK(pattern); + + RE2 regex = RE2(toRegexPattern(pattern.value())); + ASSERT_TRUE(regex.ok()) << regex.error(); + ASSERT_EQ(regex.NumberOfCapturingGroups(), var_values().size()); + + int capture_num = regex.NumberOfCapturingGroups() + 1; + std::vector captures(capture_num); + ASSERT_TRUE(regex.Match(request_path(), /*startpos=*/0, + /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, captures.data(), + captures.size())); + + EXPECT_EQ(captures[0], toStringPiece(request_path())); + + for (const auto& [name, value] : var_values()) { + int capture_index = regex.NamedCapturingGroups().at(name); + ASSERT_GE(capture_index, 0); + EXPECT_EQ(captures.at(capture_index), value); + } +} + +class GenPatternRegexWithoutMatch + : public testing::TestWithParam> { +protected: + const std::string& request_path() const { return std::get<0>(GetParam()); } + const std::string& url_pattern() const { return std::get<1>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWithoutMatch, + testing::ValuesIn(std::vector>( + {{"/media/12345/f/123/s00002.m4s", "/media/*.m4s"}, + {"/media/eff456/ll-sd-out.js", "/media/*"}, + {"/api/v1/1234/", "/api/*/v1/*"}, + {"/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=*}"}, + {"/api/v1/1234/", "/api/*/v1/**"}, + {"/api/*/1234/", "/api/*/1234/"}}))); + +TEST_P(GenPatternRegexWithoutMatch, WithCapture) { + absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); + ASSERT_OK(pattern); + + RE2 regex = RE2(toRegexPattern(pattern.value())); + ASSERT_TRUE(regex.ok()) << regex.error(); + + EXPECT_FALSE(regex.Match(request_path(), /*startpos=*/0, + /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, nullptr, 0)); +} + +} // namespace +} // namespace PatternTemplateInternal +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template_test.cc b/source/extensions/path/pattern_template_lib/pattern_template_test.cc new file mode 100644 index 0000000000000..fe1cd1dc6105b --- /dev/null +++ b/source/extensions/path/pattern_template_lib/pattern_template_test.cc @@ -0,0 +1,337 @@ +#include +#include +#include + +#include "source/common/common/assert.h" +#include "source/common/protobuf/protobuf.h" +#include "source/extensions/path/pattern_template_lib/pattern_template.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" + +#include "test/test_common/logging.h" +#include "test/test_common/status_utility.h" +#include "test/test_common/utility.h" + +#include "gtest/gtest.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +namespace { + +using ::Envoy::StatusHelpers::IsOkAndHolds; +using ::Envoy::StatusHelpers::StatusIs; + +// Capture regex for /{var1}/{var2}/{var3}/{var4}/{var5} +static constexpr absl::string_view kCaptureRegex = "/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"; +static constexpr absl::string_view kMatchUrl = "/val1/val2/val3/val4/val5"; + +TEST(ConvertURLPattern, ValidPattern) { + EXPECT_THAT(convertURLPatternSyntaxToRegex("/abc"), IsOkAndHolds("/abc")); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/**.mpd"), + IsOkAndHolds("/[a-zA-Z0-9-._~%!$&'()+,;:@/]*\\.mpd")); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/*/{resource=*}/{method=**}"), + IsOkAndHolds("/api/[a-zA-Z0-9-._~%!$&'()+,;:@]+/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@/]*)")); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/{VERSION}/{version}/{verSION}"), + IsOkAndHolds("/api/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)")); +} + +TEST(ConvertURLPattern, InvalidPattern) { + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/v*/1234"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/media/**/*/**"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/\001\002\003\004\005\006\007"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/{var12345678901234=*}"), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class ParseRewriteHelperSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperSuccessTestSuite, ParseRewriteHelperSuccess, + testing::Values("/{var1}", "/{var1}{var2}", "/{var1}-{var2}", + "/abc/{var1}/def", "/{var1}/abd/{var2}", + "/abc-def-{var1}/a/{var1}")); + +TEST_P(ParseRewriteHelperSuccess, ParseRewriteHelperSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_OK(parseRewritePatternHelper(pattern)); +} + +class ParseRewriteHelperFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperFailureTestSuite, ParseRewriteHelperFailure, + testing::Values("{var1}", "/{{var1}}", "/}va1{", "var1}", + "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); + +TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(parseRewritePatternHelper(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class ParseRewriteSuccess : public testing::TestWithParam> { +protected: + const std::string& rewrite_pattern() const { return std::get<0>(GetParam()); } + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern expected_proto() const { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern expected_proto; + Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); + return expected_proto; + } +}; + +TEST(ParseRewrite, InvalidRegex) { + EXPECT_THAT(parseRewritePattern("/{var1}", "+[abc"), StatusIs(absl::StatusCode::kInternal)); +} + +INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, + testing::ValuesIn(std::vector>({ + {"/static", R"EOF(segments: {literal: "/static"} )EOF"}, + {"/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1)EOF"}, + {"/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1)EOF"}, + {"/{var1}/{var1}/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1)EOF"}, + {"/{var3}/{var1}/{var2}", R"EOF(segments + - literal: "/" + - var_index: 3 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 2)EOF"}, + {"/{var3}/abc/def/{var2}.suffix", R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/abc/def/" + - var_index: 2 + - literal: ".suffix")EOF"}, + {"/abc/{var1}/{var2}/def", R"EOF(segments + - literal: "/abc/" + - var_index: 1 + - literal: "/" + - var_index: 2 + - literal: "/def")EOF"}, + {"/{var1}{var2}", R"EOF(segments + - literal: "/" + - var_index: 1 + - ar_index: 2)EOF"}, + {"/{var1}-{var2}/bucket-{var3}.suffix", R"EOF(segments + - literal: "/" + - var_index: 1 + - literal: "-" + - var_index: 2 + - literal: "/bucket-" + - var_index: 3 + - literal: ".suffix")EOF"}, + }))); + +TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { + absl::StatusOr rewrite = + parseRewritePattern(rewrite_pattern(), kCaptureRegex); + ASSERT_OK(rewrite); + // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); +} + +class ParseRewriteFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteFailureTestSuite, ParseRewriteFailure, + testing::Values("{var1}", "/{var6}", "/{{var1}}", "/}va1{", "var1}", + "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); + +TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(parseRewritePattern(pattern, kCaptureRegex), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class RewriteUrlTemplateSuccess + : public testing::TestWithParam> { +protected: + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto() const { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern proto; + Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); + return proto; + } + const std::string& expected_rewritten_url() const { return std::get<1>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateSuccess, + testing::ValuesIn(std::vector>( + {{R"EOF(segments: { literal: "/static" })EOF", "/static"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1)EOF", + "/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1)EOF", + "/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1)EOF", + "/val1/val1/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 2)EOF", + "/val3/val1/val2"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/abc/def/" + - var_index: 2 + - literal: ".suffix")EOF", + "/val3/abc/def/val2.suffix"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - var_index: 2 + - literal: "." + - var_index: 1)EOF", + "/val3val2.val1"}, + {R"EOF(segments: + - literal: "/abc/" + - var_index: 1 + - literal: "/" + - var_index: 5 + - literal: "/def")EOF", + "/abc/val1/val5/def"}}))); + +TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { + absl::StatusOr rewritten_url = + RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto()); + ASSERT_OK(rewritten_url); + EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); +} + +TEST(RewriteUrlTemplateFailure, BadRegex) { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 1 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "+/bad_regex", rewrite_proto), + StatusIs(absl::StatusCode::kInternal)); +} + +TEST(RewriteUrlTemplateFailure, RegexNoMatch) { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 1 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "/no_match_regex", rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 0 + )EOF"; + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { + envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 6 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class URLPatternMatchAndRewrite + : public testing::TestWithParam< + std::tuple> { +protected: + const std::string& url_pattern() const { return std::get<0>(GetParam()); } + const std::string& rewrite_pattern() const { return std::get<1>(GetParam()); } + const std::string& match_url() const { return std::get<2>(GetParam()); } + const std::string& expected_rewritten_url() const { return std::get<3>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P( + URLPatternMatchAndRewriteTestSuite, URLPatternMatchAndRewrite, + testing::ValuesIn(std::vector>( + {{"/api/users/{id}/{path=**}", "/users/{id}/{path}", "/api/users/21334/profile.json", + "/users/21334/profile.json"}, + {"/videos/*/{id}/{format}/{rendition}/{segment=**}.ts", + "/{id}/{format}/{rendition}/{segment}.ts", "/videos/lib/132939/hls/13/segment_00001.ts", + "/132939/hls/13/segment_00001.ts"}, + {"/region/{region}/bucket/{name}/{method=**}", "/{region}/bucket-{name}/{method}", + "/region/eu/bucket/prod-storage/object.pdf", "/eu/bucket-prod-storage/object.pdf"}, + {"/region/{region}/bucket/{name}/{method=**}", "/{region}{name}/{method}", + "/region/eu/bucket/prod-storage/object.pdf", "/euprod-storage/object.pdf"}}))); + +TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { + absl::StatusOr regex = convertURLPatternSyntaxToRegex(url_pattern()); + ASSERT_OK(regex); + + absl::StatusOr rewrite_proto = + parseRewritePattern(rewrite_pattern(), regex.value()); + ASSERT_OK(rewrite_proto); + + absl::StatusOr rewritten_url = + RewriteURLTemplatePattern(match_url(), regex.value(), rewrite_proto.value()); + ASSERT_OK(rewritten_url); + + EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); +} + +} // namespace + +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy From 792a8a6d973a12363ad3ccfece8983a6866b4274 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 29 Jul 2022 19:05:40 +0000 Subject: [PATCH 091/238] Add pattern lib tests Signed-off-by: silverstar195 --- .../path/pattern_template_lib/BUILD | 29 -------------- .../pattern_template_lib/pattern_template.cc | 35 ++++++++++++++++- .../pattern_template_lib/pattern_template.h | 5 ++- .../path/pattern_template_lib/proto/BUILD | 17 +++++++++ .../pattern_template_rewrite_segments.proto | 31 +++++++++++++++ test/extensions/path/BUILD | 1 + .../path/pattern_template_lib/BUILD | 38 +++++++++++++++++++ .../pattern_template_internal_test.cc | 6 +-- .../pattern_template_test.cc | 34 ++++++++--------- 9 files changed, 143 insertions(+), 53 deletions(-) create mode 100644 source/extensions/path/pattern_template_lib/proto/BUILD create mode 100644 source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto create mode 100644 test/extensions/path/BUILD create mode 100644 test/extensions/path/pattern_template_lib/BUILD rename {source => test}/extensions/path/pattern_template_lib/pattern_template_internal_test.cc (99%) rename {source => test}/extensions/path/pattern_template_lib/pattern_template_test.cc (90%) diff --git a/source/extensions/path/pattern_template_lib/BUILD b/source/extensions/path/pattern_template_lib/BUILD index 3a6cb161fe08e..dfb084bbc72e2 100644 --- a/source/extensions/path/pattern_template_lib/BUILD +++ b/source/extensions/path/pattern_template_lib/BUILD @@ -1,7 +1,6 @@ load( "//bazel:envoy_build_system.bzl", "envoy_cc_library", - "envoy_cc_test", "envoy_extension_package", ) @@ -22,7 +21,6 @@ envoy_cc_library( ], deps = [ ":pattern_template_internal_cc", - "//envoy/router:router_path_match_policy_interface", "//source/common/http:path_utility_lib", "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", "@com_google_absl//absl/status", @@ -49,30 +47,3 @@ envoy_cc_library( "@com_googlesource_code_re2//:re2", ], ) - -envoy_cc_test( - name = "pattern_template_test", - srcs = ["pattern_template_test.cc"], - deps = [ - ":pattern_template", - "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", - "//test/test_common:status_utility_lib", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - ], -) - -envoy_cc_test( - name = "pattern_template_internal_test", - srcs = ["pattern_template_internal_test.cc"], - deps = [ - ":pattern_template_internal_cc", - "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", - "//test/test_common:status_utility_lib", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - "@com_googlesource_code_re2//:re2", - ], -) diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc index 17b754ffd1679..24a1a13d27a61 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -126,15 +126,46 @@ absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_ absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, const std::string& capture_regex) { - absl::StatusOr status = convertURLPatternSyntaxToRegex(capture_regex).value(); if (!status.ok()) { return status.status(); } - return parseRewritePattern(path_template_rewrite, *std::move(status)).status(); } +absl::StatusOr rewriteURLTemplatePattern( + absl::string_view url, absl::string_view capture_regex, + const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern) { + RE2 regex = RE2(PatternTemplateInternal::toStringPiece(capture_regex)); + if (!regex.ok()) { + return absl::InternalError(regex.error()); + } + + // First capture is the whole matched regex pattern. + int capture_num = regex.NumberOfCapturingGroups() + 1; + std::vector captures(capture_num); + if (!regex.Match(PatternTemplateInternal::toStringPiece(url), /*startpos=*/0, + /*endpos=*/url.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { + return absl::InvalidArgumentError("Pattern not match"); + } + + std::string rewritten_url; + + for (const envoy::extensions::pattern_template::PatternTemplateRewriteSegments::RewriteSegment& + segment : rewrite_pattern.segments()) { + if (segment.has_literal()) { + absl::StrAppend(&rewritten_url, segment.literal()); + } else if (segment.has_var_index()) { + if (segment.var_index() < 1 || segment.var_index() >= capture_num) { + return absl::InvalidArgumentError("Invalid variable index"); + } + absl::StrAppend(&rewritten_url, absl::string_view(captures[segment.var_index()].as_string())); + } + } + + return rewritten_url; +} + } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 05d3034f53073..36fdf03b34462 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -3,8 +3,6 @@ #include -#include "envoy/router/path_match_policy.h" - #include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" #include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" @@ -50,6 +48,9 @@ absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, absl::Status isValidMatchPattern(const std::string match_pattern); +absl::StatusOr rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, + const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern); + } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/proto/BUILD b/source/extensions/path/pattern_template_lib/proto/BUILD new file mode 100644 index 0000000000000..15daa9ec85d32 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/proto/BUILD @@ -0,0 +1,17 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_extension_package", + "envoy_proto_library", +) + +licenses(["notice"]) # Apache 2 + +# Wildcard & Pattern Matching Proto + +envoy_extension_package() + +envoy_proto_library( + name = "pattern_template_rewrite_segements", + srcs = ["pattern_template_rewrite_segments.proto"], + deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], +) diff --git a/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto b/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto new file mode 100644 index 0000000000000..0cd054e9dbbd8 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto @@ -0,0 +1,31 @@ +syntax = "proto3"; + +package envoy.extensions.pattern_template; + +import "udpa/annotations/status.proto"; + +option java_package = "io.envoyproxy.envoy.extensions.pattern_template"; +option java_outer_classname = "PatternTemplateRewrite"; +option java_multiple_files = true; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/pattern_template"; +option (udpa.annotations.file_status).package_version_status = ACTIVE; + +// [#protodoc-title: Pattern Template Rewrite] + +// Holds the segments for rewriting urls base on pattern templates +message PatternTemplateRewriteSegments { + message RewriteSegment { + oneof segment_type { + // Represents a segment of the rewritten URL, including any path segments, + // slash and prefix. + string literal = 1; + + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int32 var_index = 2; + } + } + + repeated RewriteSegment segments = 3; +} diff --git a/test/extensions/path/BUILD b/test/extensions/path/BUILD new file mode 100644 index 0000000000000..779d1695d3b7c --- /dev/null +++ b/test/extensions/path/BUILD @@ -0,0 +1 @@ +licenses(["notice"]) # Apache 2 diff --git a/test/extensions/path/pattern_template_lib/BUILD b/test/extensions/path/pattern_template_lib/BUILD new file mode 100644 index 0000000000000..a72b9195196d0 --- /dev/null +++ b/test/extensions/path/pattern_template_lib/BUILD @@ -0,0 +1,38 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_test", + "envoy_package", +) + +licenses(["notice"]) # Apache 2 + +envoy_package() + +# Wildcard & Pattern Matching + +envoy_cc_test( + name = "pattern_template_test", + srcs = ["pattern_template_test.cc"], + deps = [ + "//source/extensions/path/pattern_template_lib:pattern_template_lib", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "//test/test_common:status_utility_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + ], +) + +envoy_cc_test( + name = "pattern_template_internal_test", + srcs = ["pattern_template_internal_test.cc"], + deps = [ + "//source/extensions/path/pattern_template_lib:pattern_template_internal_cc", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "//test/test_common:status_utility_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_googlesource_code_re2//:re2", + ], +) diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc similarity index 99% rename from source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc rename to test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc index 55c0124b62ef3..116dbae70f826 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc @@ -154,10 +154,10 @@ TEST_P(ConsumeVariableFailure, ConsumeVariableFailureTest) { EXPECT_THAT(consumeVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); } -class parseURLPatternSyntaxSuccess : public testing::TestWithParam {}; +class ParseURLPatternSyntaxSuccess : public testing::TestWithParam {}; INSTANTIATE_TEST_SUITE_P( - parseURLPatternSyntaxSuccessTestSuite, parseURLPatternSyntaxSuccess, + ParseURLPatternSyntaxSuccessTestSuite, ParseURLPatternSyntaxSuccess, testing::Values("/**.m3u8", "/**.mpd", "/*_suf", "/{path=**}.m3u8", "/{foo}/**.ts", "/media/*.m4s", "/media/{contentId=*}/**", "/media/*", "/api/*/*/**", "/api/*/v1/**", "/api/*/v1/*", "/{version=api/*}/*", "/api/*/*/", @@ -179,7 +179,7 @@ TEST_P(ParseURLPatternSyntaxSuccess, ParseURLPatternSyntaxSuccessTest) { EXPECT_EQ(parsed_patt->DebugString(), pattern); } -class parseURLPatternSyntaxFailure : public testing::TestWithParam {}; +class ParseURLPatternSyntaxFailure : public testing::TestWithParam {}; INSTANTIATE_TEST_SUITE_P( ParseURLPatternSyntaxFailureTestSuite, ParseURLPatternSyntaxFailure, diff --git a/source/extensions/path/pattern_template_lib/pattern_template_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_test.cc similarity index 90% rename from source/extensions/path/pattern_template_lib/pattern_template_test.cc rename to test/extensions/path/pattern_template_lib/pattern_template_test.cc index fe1cd1dc6105b..500de1611033c 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_test.cc @@ -85,8 +85,8 @@ TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { class ParseRewriteSuccess : public testing::TestWithParam> { protected: const std::string& rewrite_pattern() const { return std::get<0>(GetParam()); } - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern expected_proto() const { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern expected_proto; + envoy::extensions::pattern_template::PatternTemplateRewriteSegments expected_proto() const { + envoy::extensions::pattern_template::PatternTemplateRewriteSegments expected_proto; Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); return expected_proto; } @@ -146,7 +146,7 @@ INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, }))); TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { - absl::StatusOr rewrite = + absl::StatusOr rewrite = parseRewritePattern(rewrite_pattern(), kCaptureRegex); ASSERT_OK(rewrite); // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); @@ -169,8 +169,8 @@ TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { class RewriteUrlTemplateSuccess : public testing::TestWithParam> { protected: - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto() const { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern proto; + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto() const { + envoy::extensions::pattern_template::PatternTemplateRewriteSegments proto; Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); return proto; } @@ -228,13 +228,13 @@ INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateS TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { absl::StatusOr rewritten_url = - RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto()); + rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto()); ASSERT_OK(rewritten_url); EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); } TEST(RewriteUrlTemplateFailure, BadRegex) { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -244,12 +244,12 @@ TEST(RewriteUrlTemplateFailure, BadRegex) { Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "+/bad_regex", rewrite_proto), + EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, "+/bad_regex", rewrite_proto), StatusIs(absl::StatusCode::kInternal)); } TEST(RewriteUrlTemplateFailure, RegexNoMatch) { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -259,12 +259,12 @@ TEST(RewriteUrlTemplateFailure, RegexNoMatch) { Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "/no_match_regex", rewrite_proto), + EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, "/no_match_regex", rewrite_proto), StatusIs(absl::StatusCode::kInvalidArgument)); } TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -273,12 +273,12 @@ TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { )EOF"; Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), StatusIs(absl::StatusCode::kInvalidArgument)); } TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -288,7 +288,7 @@ TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), StatusIs(absl::StatusCode::kInvalidArgument)); } @@ -319,12 +319,12 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { absl::StatusOr regex = convertURLPatternSyntaxToRegex(url_pattern()); ASSERT_OK(regex); - absl::StatusOr rewrite_proto = - parseRewritePattern(rewrite_pattern(), regex.value()); + absl::StatusOr + rewrite_proto = parseRewritePattern(rewrite_pattern(), regex.value()); ASSERT_OK(rewrite_proto); absl::StatusOr rewritten_url = - RewriteURLTemplatePattern(match_url(), regex.value(), rewrite_proto.value()); + rewriteURLTemplatePattern(match_url(), regex.value(), rewrite_proto.value()); ASSERT_OK(rewritten_url); EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); From 34764c05f613fa166ccf4160481e08da474391bc Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 1 Aug 2022 14:40:16 +0000 Subject: [PATCH 092/238] Small formatting issues Signed-off-by: silverstar195 --- source/extensions/path/pattern_template_lib/pattern_template.h | 3 ++- test/extensions/path/pattern_template_lib/BUILD | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 36fdf03b34462..390b0651a276c 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -48,7 +48,8 @@ absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, absl::Status isValidMatchPattern(const std::string match_pattern); -absl::StatusOr rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, +absl::StatusOr rewriteURLTemplatePattern( + absl::string_view url, absl::string_view capture_regex, const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern); } // namespace PatternTemplate diff --git a/test/extensions/path/pattern_template_lib/BUILD b/test/extensions/path/pattern_template_lib/BUILD index a72b9195196d0..dcb9aa1326df5 100644 --- a/test/extensions/path/pattern_template_lib/BUILD +++ b/test/extensions/path/pattern_template_lib/BUILD @@ -14,7 +14,7 @@ envoy_cc_test( name = "pattern_template_test", srcs = ["pattern_template_test.cc"], deps = [ - "//source/extensions/path/pattern_template_lib:pattern_template_lib", + "//source/extensions/path/pattern_template_lib", "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", From aea500792d6d0924ed8e601dc7b8746890d02fa3 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 1 Aug 2022 15:05:41 +0000 Subject: [PATCH 093/238] Small formatting issues Signed-off-by: silverstar195 --- tools/spelling/spelling_dictionary.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tools/spelling/spelling_dictionary.txt b/tools/spelling/spelling_dictionary.txt index aa4aeae03eaf7..0d6f591b0cd09 100644 --- a/tools/spelling/spelling_dictionary.txt +++ b/tools/spelling/spelling_dictionary.txt @@ -29,7 +29,12 @@ BPF Bdecoded Bencoded Repick +SION TRA +abc +abcd +abd +ar btree CAS CB @@ -149,9 +154,13 @@ FQDN FREEBIND FUZZER FUZZERS +delims dereferencing differentially dnsresolvers +endpos +fo +ghi guarddog GC GCC @@ -218,6 +227,10 @@ LEDS LEV LF LHS +hls +jkl +js +lang libsxg LLVM LPT @@ -261,6 +274,7 @@ Oauth OCSP OD ODCDS +mpd oghttp OID OK @@ -963,10 +977,12 @@ parsers passphrase passthrough pathname +patt pausable pausedness pcall pcap +pchar pclose performant pfctl @@ -1172,6 +1188,7 @@ srtt ssize stackdriver stacktrace +startpos starttls startup stateful @@ -1218,6 +1235,7 @@ subtrees subtype subtypes subzone +suf superclass superset svc @@ -1309,6 +1327,7 @@ username usr util utils +va valgrind validator validators @@ -1318,6 +1337,7 @@ variadic varint vec vectorize +ver verifier verifiers versa @@ -1328,6 +1348,7 @@ vip virtualhost virtualize vptr +vtt wakeup wakeups wamr From 0e78b0376abe52f39c4e28e31cd6eec83d677cc4 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 1 Aug 2022 15:19:58 +0000 Subject: [PATCH 094/238] Remove proto title Signed-off-by: silverstar195 --- .../proto/pattern_template_rewrite_segments.proto | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto b/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto index 0cd054e9dbbd8..14ae540825171 100644 --- a/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto +++ b/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto @@ -10,8 +10,6 @@ option java_multiple_files = true; option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/pattern_template"; option (udpa.annotations.file_status).package_version_status = ACTIVE; -// [#protodoc-title: Pattern Template Rewrite] - // Holds the segments for rewriting urls base on pattern templates message PatternTemplateRewriteSegments { message RewriteSegment { From 75b3a0c61c7b870a87a3125f5c77d3d24d7a7d6a Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 1 Aug 2022 15:22:23 +0000 Subject: [PATCH 095/238] Add owners Signed-off-by: silverstar195 --- CODEOWNERS | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CODEOWNERS b/CODEOWNERS index ca989c164e4b0..b76ac78dc3c08 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -257,6 +257,10 @@ extensions/filters/http/oauth2 @derekargueta @snowp # Thrift /*/extensions/filters/network/thrift_proxy @zuercher @JuniorHsu +# URL Pattern Match and Rewrite +/*/extensions/path/pattern_template_lib @silverstar195 +/*/extensions/path/pattern_template_lib/proto @silverstar195 + # Intentionally exempt (treated as core code) /*/extensions/filters/common @UNOWNED @UNOWNED /*/extensions/filters/http/common @UNOWNED @UNOWNED From bd7081c0ab568d09539910b843cb326c98bfaa56 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 1 Aug 2022 15:23:05 +0000 Subject: [PATCH 096/238] Add owners Signed-off-by: silverstar195 --- CODEOWNERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index b76ac78dc3c08..dd79335bc37d6 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -258,8 +258,8 @@ extensions/filters/http/oauth2 @derekargueta @snowp /*/extensions/filters/network/thrift_proxy @zuercher @JuniorHsu # URL Pattern Match and Rewrite -/*/extensions/path/pattern_template_lib @silverstar195 -/*/extensions/path/pattern_template_lib/proto @silverstar195 +/*/extensions/path/pattern_template_lib @silverstar194 +/*/extensions/path/pattern_template_lib/proto @silverstar194 # Intentionally exempt (treated as core code) /*/extensions/filters/common @UNOWNED @UNOWNED From 94d146c2a4177584135c40b5e1e138932d6f7a7a Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 1 Aug 2022 15:41:40 +0000 Subject: [PATCH 097/238] Changed owner to alyssawilk Signed-off-by: silverstar195 --- CODEOWNERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index dd79335bc37d6..23a72c0322021 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -258,8 +258,8 @@ extensions/filters/http/oauth2 @derekargueta @snowp /*/extensions/filters/network/thrift_proxy @zuercher @JuniorHsu # URL Pattern Match and Rewrite -/*/extensions/path/pattern_template_lib @silverstar194 -/*/extensions/path/pattern_template_lib/proto @silverstar194 +/*/extensions/path/pattern_template_lib @alyssawilk +/*/extensions/path/pattern_template_lib/proto @alyssawilk # Intentionally exempt (treated as core code) /*/extensions/filters/common @UNOWNED @UNOWNED From 42fb15c62b1701f69a40a43dd9eae1715eaefa95 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 1 Aug 2022 17:54:42 +0000 Subject: [PATCH 098/238] Grammer changes Signed-off-by: silverstar195 --- CODEOWNERS | 4 ++-- envoy/router/path_match_policy.h | 6 +++--- envoy/router/path_rewrite_policy.h | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index 53843e279cddb..08fdcd36053eb 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -266,9 +266,9 @@ extensions/filters/http/oauth2 @derekargueta @snowp # zookeeper /*/extensions/filters/network/zookeeper_proxy @snowp @JuniorHsu # path match by pattern -/*/extensions/path/match/pattern_template @silverstar194 +/*/extensions/path/match/pattern_template @alyssawilk # path rewrite by pattern -/*/extensions/path/rewrite/pattern_template @silverstar194 +/*/extensions/path/rewrite/pattern_template @alyssawilk # Intentionally exempt (treated as core code) /*/extensions/filters/common @UNOWNED @UNOWNED diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index 18fc2f04fea9d..b48200bb06ad3 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -27,10 +27,10 @@ class PathMatchPredicate : Logger::Loggable { /** * Used to determine if the current url matches the predicate pattern. * - * @param pattern current url of route - * @return vaild is route url matches the precidate pattern. + * @param url current url of route + * @return valid if route url matches the predicate pattern. */ - virtual bool match(absl::string_view pattern) const PURE; + virtual bool match(absl::string_view url) const PURE; /** * @return the match pattern of the predicate. diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index 69ae8bf0ff9a8..18b47f83dad0f 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -28,11 +28,11 @@ class PathRewritePredicate : Logger::Loggable { * Used to rewrite the current url to the specified output. Can return a failure in case rewrite * is not successful. * - * @param current_pattern current url of route + * @param url current url of route * @param matched_path pattern to rewrite the url to * @return the name of the rewrite pattern current predicate. */ - virtual absl::StatusOr rewritePattern(absl::string_view current_pattern, + virtual absl::StatusOr rewritePattern(absl::string_view url, absl::string_view matched_path) const PURE; /** From c280b13e501b98be875aaea7deac7925091ff7dc Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 1 Aug 2022 18:23:22 +0000 Subject: [PATCH 099/238] Grammer changes Signed-off-by: silverstar195 --- CODEOWNERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index 08fdcd36053eb..415ff022f26dc 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -266,9 +266,9 @@ extensions/filters/http/oauth2 @derekargueta @snowp # zookeeper /*/extensions/filters/network/zookeeper_proxy @snowp @JuniorHsu # path match by pattern -/*/extensions/path/match/pattern_template @alyssawilk +/*/extensions/path/match/pattern_template @alyssawilk @yanjunxiang # path rewrite by pattern -/*/extensions/path/rewrite/pattern_template @alyssawilk +/*/extensions/path/rewrite/pattern_template @alyssawilk @yanjunxiang # Intentionally exempt (treated as core code) /*/extensions/filters/common @UNOWNED @UNOWNED From 989959abc02b9931719c2d51c34ddc86ce80dc17 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 1 Aug 2022 18:24:41 +0000 Subject: [PATCH 100/238] Grammer changes Signed-off-by: silverstar195 --- CODEOWNERS | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index 23a72c0322021..4ce303524c535 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -257,9 +257,9 @@ extensions/filters/http/oauth2 @derekargueta @snowp # Thrift /*/extensions/filters/network/thrift_proxy @zuercher @JuniorHsu -# URL Pattern Match and Rewrite -/*/extensions/path/pattern_template_lib @alyssawilk -/*/extensions/path/pattern_template_lib/proto @alyssawilk +# URL Pattern Match and Rewrite Library +/*/extensions/path/pattern_template_lib @alyssawilk @yanjunxiang +/*/extensions/path/pattern_template_lib/proto @alyssawilk @yanjunxiang # Intentionally exempt (treated as core code) /*/extensions/filters/common @UNOWNED @UNOWNED From 0dae2f157ebc273e6584fa9ed41ae5f7bbe5e4e1 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 1 Aug 2022 18:28:07 +0000 Subject: [PATCH 101/238] Grammer changes Signed-off-by: silverstar195 --- CODEOWNERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index 4ce303524c535..734d4aec5f409 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -258,8 +258,8 @@ extensions/filters/http/oauth2 @derekargueta @snowp /*/extensions/filters/network/thrift_proxy @zuercher @JuniorHsu # URL Pattern Match and Rewrite Library -/*/extensions/path/pattern_template_lib @alyssawilk @yanjunxiang -/*/extensions/path/pattern_template_lib/proto @alyssawilk @yanjunxiang +/*/extensions/path/pattern_template_lib @alyssawilk @yanjunxiang-google +/*/extensions/path/pattern_template_lib/proto @alyssawilk @yanjunxiang-google # Intentionally exempt (treated as core code) /*/extensions/filters/common @UNOWNED @UNOWNED From 5bcebeb472d2ebf5b4e73ac3dafe14f062a4a67c Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 1 Aug 2022 18:29:04 +0000 Subject: [PATCH 102/238] Grammar changes Signed-off-by: silverstar195 --- CODEOWNERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index 415ff022f26dc..365a2f0dc4c09 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -266,9 +266,9 @@ extensions/filters/http/oauth2 @derekargueta @snowp # zookeeper /*/extensions/filters/network/zookeeper_proxy @snowp @JuniorHsu # path match by pattern -/*/extensions/path/match/pattern_template @alyssawilk @yanjunxiang +/*/extensions/path/match/pattern_template @alyssawilk @yanjunxiang-google # path rewrite by pattern -/*/extensions/path/rewrite/pattern_template @alyssawilk @yanjunxiang +/*/extensions/path/rewrite/pattern_template @alyssawilk @yanjunxiang-google # Intentionally exempt (treated as core code) /*/extensions/filters/common @UNOWNED @UNOWNED From 0825db3228c0042d2d167b86a998ef4414807837 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 14:51:01 +0000 Subject: [PATCH 103/238] Checkpoint changes Signed-off-by: silverstar195 --- source/extensions/extensions_build_config.bzl | 1 - source/extensions/path/match/pattern_template/BUILD | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 40d0e668bd811..1f9d56959afbe 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -242,7 +242,6 @@ EXTENSIONS = { # # Path Pattern Match and Path Pattern Rewrite # - "envoy.path.match.pattern_template.v3.pattern_template_match_predicate": "//source/extensions/path/match/pattern_template:config", "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate": "//source/extensions/path/rewrite/pattern_template:config", diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD index d290e1ac07b1e..c9c4b39ea1c38 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/pattern_template/BUILD @@ -23,7 +23,7 @@ envoy_cc_library( "//envoy/router:router_path_match_policy_interface", "//source/common/protobuf", "//source/common/protobuf:utility_lib", - "//source/extensions/path/pattern_template_lib:pattern_template_lib", + "//source/extensions/path/pattern_template_lib", "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", ], ) @@ -37,7 +37,7 @@ envoy_cc_extension( ":pattern_template_match_lib", "//envoy/registry", "//envoy/router:router_path_match_policy_interface", - "//source/extensions/path/pattern_template_lib:pattern_template_lib", + "//source/extensions/path/pattern_template_lib", "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", ], ) From 00848b18457bb22d4a4495b1325762402f14f410 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 18:47:09 +0000 Subject: [PATCH 104/238] Checkpoint changes Signed-off-by: silverstar195 --- envoy/router/path_match_policy.h | 19 +- envoy/router/path_rewrite_policy.h | 12 +- envoy/router/router.h | 36 -- source/common/router/config_impl.cc | 98 ++-- source/common/router/config_impl.h | 7 +- source/extensions/extensions_build_config.bzl | 4 +- source/extensions/extensions_metadata.yaml | 4 +- .../path/match/pattern_template/config.h | 8 +- .../pattern_template/pattern_template_match.h | 8 +- .../pattern_template_internal_test.cc | 470 ------------------ .../pattern_template_test.cc | 337 ------------- .../path/rewrite/pattern_template/config.h | 15 +- .../pattern_template_rewrite.h | 8 +- 13 files changed, 103 insertions(+), 923 deletions(-) delete mode 100644 source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc delete mode 100644 source/extensions/path/pattern_template_lib/pattern_template_test.cc diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index b48200bb06ad3..962a3b52df8c7 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -19,11 +19,6 @@ class PathMatchPredicate : Logger::Loggable { PathMatchPredicate() = default; virtual ~PathMatchPredicate() = default; - /** - * @return the name of the current predicate. - */ - virtual std::string name() const PURE; - /** * Used to determine if the current url matches the predicate pattern. * @@ -35,7 +30,12 @@ class PathMatchPredicate : Logger::Loggable { /** * @return the match pattern of the predicate. */ - virtual absl::string_view pattern() const PURE; + virtual std::string pattern() const PURE; + + /** + * @return the name of the current predicate. + */ + virtual absl::string_view name() const PURE; }; using PathMatchPredicateSharedPtr = std::shared_ptr; @@ -55,7 +55,12 @@ class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { createPathMatchPredicate(const Protobuf::Message& config) PURE; /** - * @return the category of the rewrite pattern predicate to be created. + * @return the name of the match pattern predicate to be created. + */ + virtual std::string name() const override PURE; + + /** + * @return the category of the match pattern predicate to be created. */ std::string category() const override { return "envoy.path.match"; } }; diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index 18b47f83dad0f..f8689ea564ada 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -19,11 +19,6 @@ class PathRewritePredicate : Logger::Loggable { PathRewritePredicate() = default; virtual ~PathRewritePredicate() = default; - /** - * @return the name of the current predicate. - */ - virtual absl::string_view name() const PURE; - /** * Used to rewrite the current url to the specified output. Can return a failure in case rewrite * is not successful. @@ -38,7 +33,12 @@ class PathRewritePredicate : Logger::Loggable { /** * @return the rewrite pattern of the predicate. */ - virtual absl::string_view pattern() const PURE; + virtual std::string pattern() const PURE; + + /** + * @return the name of the rewrite predicate. + */ + virtual absl::string_view name() const PURE; }; using PathRewritePredicateSharedPtr = std::shared_ptr; diff --git a/envoy/router/router.h b/envoy/router/router.h index ec1503153e805..f381505191c37 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -299,42 +299,6 @@ class RetryPolicy { */ enum class RetryStatus { No, NoOverflow, NoRetryLimitExceeded, Yes }; -class PathMatchPolicy { -public: - virtual ~PathMatchPolicy() = default; - - /** - * @return whether internal redirect is enabled on this route. - */ - virtual bool enabled() const PURE; - - /** - * Creates the target route predicates. This should really be called only once for each upstream - * redirect response. Creating the predicates lazily to avoid wasting CPU cycles on non-redirect - * responses, which should be the most common case. - * @return a vector of newly constructed InternalRedirectPredicate instances. - */ - virtual PathMatchPredicateSharedPtr predicate() const PURE; -}; - -class PathRewritePolicy { -public: - virtual ~PathRewritePolicy() = default; - - /** - * @return whether internal redirect is enabled on this route. - */ - virtual bool enabled() const PURE; - - /** - * Creates the target route predicates. This should really be called only once for each upstream - * redirect response. Creating the predicates lazily to avoid wasting CPU cycles on non-redirect - * responses, which should be the most common case. - * @return a vector of newly constructed InternalRedirectPredicate instances. - */ - virtual PathRewritePredicateSharedPtr predicate() const PURE; -}; - /** * PathMatchPolicy from the route configuration. */ diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 03dbf3230d330..35e67907cac03 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -361,19 +361,21 @@ PathRewritePolicyImpl::PathRewritePolicyImpl(const envoy::config::core::v3::Type ProtobufMessage::ValidationVisitor& validator, std::string route_url) : route_url_(route_url), enabled_(true) { - absl::string_view name = "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate"; - auto* factory = Registry::FactoryRegistry::getFactory(name); - ASSERT(factory); // factory not found + predicate_factory_ = &Envoy::Config::Utility::getAndCheckFactory(typed_config); + ASSERT(predicate_factory_); // factory not found - ProtobufTypes::MessagePtr proto_config = factory->createEmptyConfigProto(); - Envoy::Config::Utility::translateOpaqueConfig(typed_config.typed_config(), validator, *proto_config); + predicate_config_ = Envoy::Config::Utility::translateAnyToFactoryConfig( + typed_config.typed_config(), validator, *predicate_factory_); + ASSERT(predicate_config_); // config translation failed - predicate_factory_ = factory; - rewrite_predicate_config_ = std::move(proto_config); + // Validate config format and inputs when creating factory. + // As the validation and create are nearly 1:1 store for later use. + // Predicate is only ever created once. + predicate_ = predicate_factory_->createPathRewritePredicate(*predicate_config_); } PathRewritePredicateSharedPtr PathRewritePolicyImpl::predicate() const { - return predicate_factory_->createPathRewritePredicate(*rewrite_predicate_config_); + return predicate_; } PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; @@ -383,18 +385,22 @@ PathMatchPolicyImpl::PathMatchPolicyImpl( ProtobufMessage::ValidationVisitor& validator, std::string route_url) : route_url_(route_url), enabled_(true) { - absl::string_view name = "envoy.path.match.pattern_template.v3.pattern_template_match_predicate"; - auto* factory = Registry::FactoryRegistry::getFactory(name); - ASSERT(factory); // factory not found - ProtobufTypes::MessagePtr proto_config = factory->createEmptyConfigProto(); - Envoy::Config::Utility::translateOpaqueConfig(typed_config.typed_config(), validator, *proto_config); + predicate_factory_ = + &Envoy::Config::Utility::getAndCheckFactory(typed_config); + ASSERT(predicate_factory_); // factory not found - predicate_factory_ = factory; - predicate_config_ = std::move(proto_config); + predicate_config_ = Envoy::Config::Utility::translateAnyToFactoryConfig( + typed_config.typed_config(), validator, *predicate_factory_); + ASSERT(predicate_config_); // config translation failed + + // Validate config format and inputs when creating factory. + // As the validation and create are nearly 1:1 store for later use. + // Predicate is only ever created once. + predicate_ = predicate_factory_->createPathMatchPredicate(*predicate_config_); } PathMatchPredicateSharedPtr PathMatchPolicyImpl::predicate() const { - return predicate_factory_->createPathMatchPredicate(*predicate_config_); + return predicate_; } absl::flat_hash_set InternalRedirectPolicyImpl::buildRedirectResponseCodes( @@ -684,54 +690,56 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, } } - if (route.route().has_regex_rewrite()) { - if (!prefix_rewrite_.empty() || path_match_policy_.enabled()) { - throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); + if (path_rewrite_policy_.enabled()) { + if (!prefix_rewrite_.empty() || route.route().has_regex_rewrite()) { + throw EnvoyException( + "Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); } - auto rewrite_spec = route.route().regex_rewrite(); - regex_rewrite_ = Regex::Utility::parseRegex(rewrite_spec.pattern()); - regex_rewrite_substitution_ = rewrite_spec.substitution(); } - if (path_match_policy_.enabled()) { - if (!prefix_rewrite_.empty() || route.route().has_regex_rewrite()) { - throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); + if (!prefix_rewrite_.empty()) { + if (route.route().has_regex_rewrite() || path_rewrite_policy_.enabled()) { + throw EnvoyException( + "Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); } } - if (path_match_policy_.enabled()) { - // TODO: validate path_match_policy is of kind the pattern template - if (!Extensions::PatternTemplate::isValidMatchPattern(path_match_policy_.predicate()->pattern()) - .ok()) { - throw EnvoyException(fmt::format("path_match_policy {} is invalid", - path_match_policy_.predicate()->pattern())); + if(route.route().has_regex_rewrite()) { + if (!prefix_rewrite_.empty() || path_rewrite_policy_.enabled()) { + throw EnvoyException( + "Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); } } - if (path_rewrite_policy_.enabled()) { - // TODO: validate path_rewrite_policy is of kind the pattern template - if (!Extensions::PatternTemplate::isValidPathTemplateRewritePattern( - path_rewrite_policy_.predicate()->pattern()) - .ok()) { - throw EnvoyException(fmt::format("path_rewrite_policy {} is invalid", - path_match_policy_.predicate()->pattern())); + if (route.route().has_regex_rewrite()) { + auto rewrite_spec = route.route().regex_rewrite(); + regex_rewrite_ = Regex::Utility::parseRegex(rewrite_spec.pattern()); + regex_rewrite_substitution_ = rewrite_spec.substitution(); + } + + // Check if pattern rewrite is enabled that the pattern matching is also enabled. + // This is needed to match up variable values. + if (path_rewrite_policy_.enabled() && + path_rewrite_policy_.predicate()->name() == Extensions::PatternTemplate::Rewrite::NAME) { + if (!path_match_policy_.enabled() || + path_match_policy_.predicate()->name() != Extensions::PatternTemplate::Match::NAME) { + throw EnvoyException(fmt::format("unable to use {} extension without {} extension", + Extensions::PatternTemplate::Rewrite::NAME, + Extensions::PatternTemplate::Match::NAME)); } + } + // Validation between extensions as they share rewrite pattern variables. + if (path_rewrite_policy_.enabled() && path_match_policy_.enabled()) { if (!Extensions::PatternTemplate::isValidSharedVariableSet( path_rewrite_policy_.predicate()->pattern(), path_match_policy_.predicate()->pattern()) .ok()) { throw EnvoyException(fmt::format( - "mismatch between path_match_policy {} and path_rewrite_policy {}", + "mismatch between variables in path_match_policy {} and path_rewrite_policy {}", path_match_policy_.predicate()->pattern(), path_rewrite_policy_.predicate()->pattern())); } } - if (!prefix_rewrite_.empty()) { - if (route.route().has_regex_rewrite() || path_match_policy_.enabled()) { - throw EnvoyException("Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); - } - } - if (route.redirect().has_regex_rewrite()) { ASSERT(prefix_rewrite_redirect_.empty()); auto rewrite_spec = route.redirect().regex_rewrite(); diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index f66e80a27cd3d..f1b1a6f9a089e 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -471,12 +471,12 @@ class PathMatchPolicyImpl : public PathMatchPolicy { std::string route_url() const { return route_url_; } - ProtobufTypes::MessagePtr predicate_config_; - private: const std::string route_url_; const bool enabled_; + ProtobufTypes::MessagePtr predicate_config_; PathMatchPredicateFactory* predicate_factory_; + PathMatchPredicateSharedPtr predicate_; }; /** @@ -498,8 +498,9 @@ class PathRewritePolicyImpl : public PathRewritePolicy { private: const std::string route_url_; const bool enabled_; - ProtobufTypes::MessagePtr rewrite_predicate_config_; + ProtobufTypes::MessagePtr predicate_config_; PathRewritePredicateFactory* predicate_factory_; + PathRewritePredicateSharedPtr predicate_; }; /** diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 1f9d56959afbe..098b9948f8e82 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -242,8 +242,8 @@ EXTENSIONS = { # # Path Pattern Match and Path Pattern Rewrite # - "envoy.path.match.pattern_template.v3.pattern_template_match_predicate": "//source/extensions/path/match/pattern_template:config", - "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate": "//source/extensions/path/rewrite/pattern_template:config", + "envoy.path.match.pattern_template.pattern_template_match_predicate": "//source/extensions/path/match/pattern_template:config", + "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate": "//source/extensions/path/rewrite/pattern_template:config", # # Http Upstreams (excepting envoy.upstreams.http.generic which is hard-coded into the build so not registered here) diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index bfe858438f594..9ba210d6608ba 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -746,12 +746,12 @@ envoy.matching.matchers.ip: status: stable type_urls: - envoy.extensions.matching.input_matchers.ip.v3.Ip -envoy.path.match.pattern_template.v3.pattern_template_match_predicate: +envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig: categories: - envoy.path.match security_posture: robust_to_untrusted_downstream_and_upstream status: stable -envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate: +envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig: categories: - envoy.path.rewrite security_posture: robust_to_untrusted_downstream_and_upstream diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 4b5b4497d7841..69f04e764b0ed 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -18,11 +18,15 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa public: Router::PathMatchPredicateSharedPtr createPathMatchPredicate(const Protobuf::Message& config) override { - //TODO: Get pattern from config auto path_match_config = MessageUtil::downcastAndValidate( config, ProtobufMessage::getStrictValidationVisitor()); + + if (!PatternTemplate::isValidMatchPattern(path_match_config.path_template()).ok()) { + throw EnvoyException(fmt::format("path_match_policy.path_template {} is invalid", + path_match_config.path_template())); + } return std::make_shared(path_match_config); } @@ -30,7 +34,7 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa return std::make_unique(); } - std::string name() const override { return "envoy.path.match.pattern_template.v3.pattern_template_match_predicate"; } + std::string name() const override { return "envoy.path.match.pattern_template.pattern_template_match_predicate"; } std::string category() const override { return "envoy.path.match"; } }; diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index de1298531c333..834e6d747256a 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -16,6 +16,8 @@ namespace Extensions { namespace PatternTemplate { namespace Match { +const absl::string_view NAME = "envoy.path.match.pattern_template.pattern_template_match_predicate"; + class PatternTemplateMatchPredicate : public Router::PathMatchPredicate { public: explicit PatternTemplateMatchPredicate( @@ -23,12 +25,10 @@ class PatternTemplateMatchPredicate : public Router::PathMatchPredicate { config) : path_template_(config.path_template()){} - absl::string_view name() const override { - return "envoy.path.match.pattern_template.v3.pattern_template_match_predicate"; - } - bool match(absl::string_view pattern) const override; + std::string pattern() const override; + absl::string_view name() const override { return NAME; } private: const std::string path_template_; diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc deleted file mode 100644 index 55c0124b62ef3..0000000000000 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal_test.cc +++ /dev/null @@ -1,470 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "source/common/common/assert.h" -#include "source/common/protobuf/protobuf.h" -#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" - -#include "test/test_common/logging.h" -#include "test/test_common/status_utility.h" -#include "test/test_common/utility.h" - -#include "absl/strings/str_cat.h" -#include "absl/strings/string_view.h" -#include "gtest/gtest.h" -#include "re2/re2.h" - -namespace Envoy { -namespace Extensions { -namespace PatternTemplate { - -namespace PatternTemplateInternal { - -namespace { - -using ::Envoy::StatusHelpers::StatusIs; - -TEST(InternalParsing, ParsedUrlDebugString) { - ParsedUrlPattern patt1 = { - { - "abc", - "def", - Operator::kPathGlob, - Variable("var", {Operator::kPathGlob, "ghi", Operator::kTextGlob}), - }, - ".test", - {}, - }; - EXPECT_EQ(patt1.DebugString(), "/abc/def/*/{var=*/ghi/**}.test"); - - ParsedUrlPattern patt2 = {{ - Variable("var", {}), - }, - "", - {}}; - EXPECT_EQ(patt2.DebugString(), "/{var}"); -} - -TEST(InternalParsing, isValidLiteralWorks) { - EXPECT_TRUE(isValidLiteral("123abcABC")); - EXPECT_TRUE(isValidLiteral("._~-")); - EXPECT_TRUE(isValidLiteral("-._~%20!$&'()+,;:@")); - EXPECT_FALSE(isValidLiteral("`~!@#$%^&()-_+;:,<.>'\"\\| ")); - EXPECT_FALSE(isValidLiteral("abc/")); - EXPECT_FALSE(isValidLiteral("ab*c")); - EXPECT_FALSE(isValidLiteral("a**c")); - EXPECT_FALSE(isValidLiteral("a=c")); - EXPECT_FALSE(isValidLiteral("?abc")); - EXPECT_FALSE(isValidLiteral("?a=c")); - EXPECT_FALSE(isValidLiteral("{abc")); - EXPECT_FALSE(isValidLiteral("abc}")); - EXPECT_FALSE(isValidLiteral("{abc}")); -} - -TEST(InternalParsing, IsValidRewriteLiteralWorks) { - EXPECT_TRUE(isValidRewriteLiteral("123abcABC")); - EXPECT_TRUE(isValidRewriteLiteral("abc/")); - EXPECT_TRUE(isValidRewriteLiteral("abc/def")); - EXPECT_TRUE(isValidRewriteLiteral("/abc.def")); - EXPECT_TRUE(isValidRewriteLiteral("._~-")); - EXPECT_TRUE(isValidRewriteLiteral("-._~%20!$&'()+,;:@")); - EXPECT_FALSE(isValidRewriteLiteral("`~!@#$%^&()-_+;:,<.>'\"| ")); - EXPECT_FALSE(isValidRewriteLiteral("ab}c")); - EXPECT_FALSE(isValidRewriteLiteral("ab{c")); - EXPECT_FALSE(isValidRewriteLiteral("a=c")); - EXPECT_FALSE(isValidRewriteLiteral("?a=c")); -} - -TEST(InternalParsing, IsValidIndentWorks) { - EXPECT_TRUE(isValidIndent("abc")); - EXPECT_TRUE(isValidIndent("ABC_def_123")); - EXPECT_TRUE(isValidIndent("a1")); - EXPECT_TRUE(isValidIndent("T")); - EXPECT_FALSE(isValidIndent("123")); - EXPECT_FALSE(isValidIndent("__undefined__")); - EXPECT_FALSE(isValidIndent("abc-def")); - EXPECT_FALSE(isValidIndent("abc=def")); - EXPECT_FALSE(isValidIndent("abc def")); - EXPECT_FALSE(isValidIndent("a!!!")); -} - -TEST(InternalParsing, ConsumeLiteralWorks) { - std::string pattern = "abc/123"; - - absl::StatusOr> result = consumeLiteral(pattern); - - ASSERT_OK(result); - EXPECT_EQ(result->parsed_value, "abc"); - EXPECT_EQ(result->unconsumed_pattern, "/123"); -} - -TEST(InternalParsing, ConsumeTextGlob) { - std::string pattern = "***abc/123"; - - absl::StatusOr> result = consumeOperator(pattern); - - ASSERT_OK(result); - EXPECT_EQ(result->parsed_value, Operator::kTextGlob); - EXPECT_EQ(result->unconsumed_pattern, "*abc/123"); -} - -TEST(InternalParsing, ConsumePathGlob) { - std::string pattern = "*/123"; - - absl::StatusOr> result = consumeOperator(pattern); - - ASSERT_OK(result); - EXPECT_EQ(result->parsed_value, Operator::kPathGlob); - EXPECT_EQ(result->unconsumed_pattern, "/123"); -} - -class ConsumeVariableSuccess : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P(ConsumeVariableSuccessTestSuite, ConsumeVariableSuccess, - testing::Values("{var=*}", "{Var}", "{v1=**}", "{v_1=*/abc/**}", - "{v3=abc}", "{v=123/*/*}", "{var=abc/*/def}")); - -TEST_P(ConsumeVariableSuccess, ConsumeVariableSuccessTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - absl::StatusOr> result = consumeVariable(pattern); - - ASSERT_OK(result); - EXPECT_EQ(result->parsed_value.DebugString(), pattern); - EXPECT_TRUE(result->unconsumed_pattern.empty()); -} - -class ConsumeVariableFailure : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P(ConsumeVariableFailureTestSuite, ConsumeVariableFailure, - testing::Values("{var", "{=abc}", "{_var=*}", "{1v}", "{1v=abc}", - "{var=***}", "{v-a-r}", "{var=*/abc?q=1}", "{var=abc/a*}", - "{var=*def/abc}", "{var=}", "{var=abc=def}", - "{rc=||||(A+yl/}")); - -TEST_P(ConsumeVariableFailure, ConsumeVariableFailureTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - EXPECT_THAT(consumeVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); -} - -class parseURLPatternSyntaxSuccess : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P( - parseURLPatternSyntaxSuccessTestSuite, parseURLPatternSyntaxSuccess, - testing::Values("/**.m3u8", "/**.mpd", "/*_suf", "/{path=**}.m3u8", "/{foo}/**.ts", - "/media/*.m4s", "/media/{contentId=*}/**", "/media/*", "/api/*/*/**", - "/api/*/v1/**", "/api/*/v1/*", "/{version=api/*}/*", "/api/*/*/", - "/api/*/1234/", "/api/*/{resource=*}/{method=*}", - "/api/*/{resource=*}/{method=**}", "/v1/**", "/media/{country}/{lang=*}/**", - "/{foo}/{bar}/{fo}/{fum}/*", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", - "/media/{id=*}/*", "/media/{contentId=**}", - "/api/{version}/projects/{project}/locations/{location}/{resource}/" - "{name}", - "/api/{version=*}/{url=**}", "/api/{VERSION}/{version}/{verSION}", - "/api/1234/abcd", "/media/abcd/%10%20%30/{v1=*/%10%20}_suffix", "/")); - -TEST_P(ParseURLPatternSyntaxSuccess, ParseURLPatternSyntaxSuccessTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - absl::StatusOr parsed_patt = parseURLPatternSyntax(pattern); - ASSERT_OK(parsed_patt); - EXPECT_EQ(parsed_patt->DebugString(), pattern); -} - -class parseURLPatternSyntaxFailure : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P( - ParseURLPatternSyntaxFailureTestSuite, ParseURLPatternSyntaxFailure, - testing::Values("/api/v*/1234", "/api/{version=v*}/1234", "/api/v{versionNum=*}/1234", - "/api/{version=*beta}/1234", "/media/eff456/ll-sd-out.{ext}", - "/media/eff456/ll-sd-out.{ext=*}", "/media/eff456/ll-sd-out.**", - "/media/{country=**}/{lang=*}/**", "/media/**/*/**", "/link/{id=*}/asset*", - "/link/{id=*}/{asset=asset*}", "/media/{id=/*}/*", "/media/{contentId=/**}", - "/api/{version}/{version}", "/api/{version.major}/{version.minor}", - "/media/***", "/media/*{*}*", "/media/{*}/", "/media/*/index?a=2", "media", - "/\001\002\003\004\005\006\007", "/*(/**", "/**/{var}", - "/{var1}/{var2}/{var3}/{var4}/{var5}/{var6}", "/{=*}", - "/{var12345678901234=*}")); - -TEST_P(ParseURLPatternSyntaxFailure, ParseURLPatternSyntaxFailureTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - EXPECT_THAT(parseURLPatternSyntax(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); -} - -TEST(InternalRegexGen, LiteralEscapes) { - EXPECT_EQ(toRegexPattern("abcABC123/-._~%20!$&'()+,;:@"), - "abcABC123/-\\._~%20!\\$&'\\(\\)\\+,;:@"); -} - -TEST(InternalRegexGen, LiteralMatches) { - absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; - - EXPECT_TRUE(RE2::FullMatch(toStringPiece(kPattern), toRegexPattern(kPattern))); -} - -TEST(InternalRegexGen, LiteralMatchesInNamedCapture) { - absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; - - RE2 regex = RE2(absl::StrCat("(?P", toRegexPattern(kPattern), ")")); - ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); - - // Full matched string + capture groups - std::vector captures(2); - ASSERT_TRUE(regex.Match(toStringPiece(kPattern), /*startpos=*/0, /*endpos=*/kPattern.size(), - RE2::ANCHOR_BOTH, captures.data(), captures.size())); - - // Index 0 would be the full text of the matched string. - EXPECT_EQ(toStringPiece(kPattern), captures[0]); - // Get the pattern matched with the named capture group. - EXPECT_EQ(toStringPiece(kPattern), captures.at(regex.NamedCapturingGroups().at("var"))); -} - -TEST(InternalRegexGen, LiteralOnlyMatchesItself) { - constexpr absl::string_view kChars = "abcABC123/-._~%20!$&'()+,;:@"; - - for (const char c : kChars) { - std::string s = {'z', c, 'z'}; - EXPECT_TRUE(RE2::FullMatch(s, toRegexPattern(s))); - EXPECT_FALSE(RE2::FullMatch("zzz", toRegexPattern(s))); - } -} - -TEST(InternalRegexGen, RegexLikePatternIsMatchedLiterally) { - EXPECT_TRUE(RE2::FullMatch("(abc)", toRegexPattern("(abc)"))); - EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); - - EXPECT_TRUE(RE2::FullMatch("(abc)+", toRegexPattern("(abc)+"))); - EXPECT_FALSE(RE2::FullMatch("", toRegexPattern("(abc)+"))); - EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); - EXPECT_FALSE(RE2::FullMatch("abcabc", toRegexPattern("(abc)+"))); - - EXPECT_TRUE(RE2::FullMatch(".+", toRegexPattern(".+"))); - EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern(".+"))); - - EXPECT_TRUE(RE2::FullMatch("a+", toRegexPattern("a+"))); - EXPECT_FALSE(RE2::FullMatch("aa", toRegexPattern("a+"))); -} - -TEST(InternalRegexGen, DollarSignMatchesIfself) { - EXPECT_TRUE(RE2::FullMatch("abc$", toRegexPattern("abc$"))); - EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("abc$"))); -} - -TEST(InternalRegexGen, OperatorRegexPattern) { - EXPECT_EQ(toRegexPattern(Operator::kPathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); - EXPECT_EQ(toRegexPattern(Operator::kTextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); -} - -TEST(InternalRegexGen, PathGlobRegex) { - EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kPathGlob))); -} - -TEST(InternalRegexGen, TextGlobRegex) { - EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kTextGlob))); - EXPECT_TRUE(RE2::FullMatch("", toRegexPattern(Operator::kTextGlob))); - EXPECT_TRUE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kTextGlob))); -} - -TEST(InternalRegexGen, VariableRegexPattern) { - EXPECT_EQ(toRegexPattern(Variable("var1", {})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"); - EXPECT_EQ(toRegexPattern(Variable("var2", {Operator::kPathGlob, "abc", Operator::kTextGlob})), - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+/abc/" - "[a-zA-Z0-9-._~%!$&'()+,;:@/]*)"); -} - -TEST(InternalRegexGen, VariableRegexDefaultMatch) { - absl::StatusOr> var = consumeVariable("{var}"); - ASSERT_OK(var); - - std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc", toRegexPattern(var->parsed_value), &capture)); - EXPECT_EQ(capture, "abc"); -} - -TEST(InternalRegexGen, VariableRegexDefaultNotMatch) { - absl::StatusOr> var = consumeVariable("{var}"); - ASSERT_OK(var); - - EXPECT_FALSE(RE2::FullMatch("abc/def", toRegexPattern(var->parsed_value))); -} - -TEST(InternalRegexGen, VariableRegexSegmentsMatch) { - absl::StatusOr> var = consumeVariable("{var=abc/*/def}"); - ASSERT_OK(var); - - std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); - EXPECT_EQ(capture, "abc/123/def"); -} - -TEST(InternalRegexGen, VariableRegexTextGlobMatch) { - absl::StatusOr> var = consumeVariable("{var=**/def}"); - ASSERT_OK(var); - - std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); - EXPECT_EQ(capture, "abc/123/def"); -} - -TEST(InternalRegexGen, VariableRegexNamedCapture) { - re2::StringPiece kPattern = "abc"; - absl::StatusOr> var = consumeVariable("{var=*}"); - ASSERT_OK(var); - - RE2 regex = RE2(toRegexPattern(var->parsed_value)); - ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); - - // Full matched string + capture groups - std::vector captures(2); - ASSERT_TRUE(regex.Match(kPattern, /*startpos=*/0, /*endpos=*/kPattern.size(), RE2::ANCHOR_BOTH, - captures.data(), captures.size())); - - // Index 0 would be the full text of the matched string. - EXPECT_EQ(kPattern, captures[0]); - // Get the pattern matched with the named capture group. - EXPECT_EQ(kPattern, captures.at(regex.NamedCapturingGroups().at("var"))); -} - -TEST(InternalRegexGen, ParsedURLPatternToRegex) { - absl::StatusOr pattern = - parseURLPatternSyntax("/abc/*/{var1}/def/{var2=*/ghi/**}.jkl"); - ASSERT_OK(pattern); - - std::string var1_capture; - std::string var2_capture; - EXPECT_TRUE(RE2::FullMatch("/abc/123/456/def/789/ghi/%20/($).jkl", - toRegexPattern(pattern.value()), &var1_capture, &var2_capture)); - EXPECT_EQ(var1_capture, "456"); - EXPECT_EQ(var2_capture, "789/ghi/%20/($)"); -} - -struct GenPatternTestCase { - GenPatternTestCase(std::string request_path, std::string url_pattern, - std::vector> capture_pairs) - : path(request_path), pattern(url_pattern), captures(capture_pairs) {} - std::string path; - std::string pattern; - std::vector> captures; -}; - -class GenPatternRegexWithMatch : public testing::TestWithParam { -protected: - const std::string& request_path() const { return GetParam().path; } - const std::string& url_pattern() const { return GetParam().pattern; } - std::vector> const var_values() { - return GetParam().captures; - } -}; - -INSTANTIATE_TEST_SUITE_P( - GenPatternRegexWithMatchTestSuite, GenPatternRegexWithMatch, - testing::Values( - GenPatternTestCase("/media/1234/manifest.m3u8", "/**.m3u8", {}), - GenPatternTestCase("/manifest.mpd", "/**.mpd", {}), - GenPatternTestCase("/media/1234/manifest.m3u8", "/{path=**}.m3u8", - {{"path", "media/1234/manifest"}}), - GenPatternTestCase("/foo/12314341/format/123/hls/segment_0000000001.ts", "/{foo}/**.ts", - {{"foo", "foo"}}), - GenPatternTestCase("/media/eff456/ll-sd-out.js", "/media/{contentId=*}/**", - {{"contentId", "eff456"}}), - GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/*/**", {}), - GenPatternTestCase("/api/v1/1234", "/{version=api/*}/*", {{"version", "api/v1"}}), - GenPatternTestCase("/api/v1/1234/", "/api/*/*/", {}), - GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=**}", - {{"resource", "1234"}, {"method", "broadcasts/get"}}), - GenPatternTestCase("/v1/broadcasts/12345/live", "/v1/**", {}), - GenPatternTestCase("/media/us/en/12334/subtitle_enUS_00101.vtt", - "/media/{country}/{lang=*}/**", {{"country", "us"}, {"lang", "en"}}), - GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo}/{bar}/{fo}/{fum}/*", - {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), - GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", - {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), - GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{id=*}/**", {{"id", "1234"}}), - GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{contentId=**}", - {{"contentId", "1234/hls/1001011.m3u8"}}), - GenPatternTestCase("/api/v1/projects/my-project/locations/global/edgeCacheOrigins/foo", - "/api/{version}/projects/{project}/locations/{location}/{resource}/" - "{name}", - {{"version", "v1"}, - {"project", "my-project"}, - {"location", "global"}, - {"resource", "edgeCacheOrigins"}, - {"name", "foo"}}), - GenPatternTestCase("/api/v1/foo/bar/baz/", "/api/{version=*}/{url=**}", - {{"version", "v1"}, {"url", "foo/bar/baz/"}}), - GenPatternTestCase("/api/v1/v2/v3", "/api/{VERSION}/{version}/{verSION}", - {{"VERSION", "v1"}, {"version", "v2"}, {"verSION", "v3"}}))); - -TEST_P(GenPatternRegexWithMatch, WithCapture) { - absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); - ASSERT_OK(pattern); - - RE2 regex = RE2(toRegexPattern(pattern.value())); - ASSERT_TRUE(regex.ok()) << regex.error(); - ASSERT_EQ(regex.NumberOfCapturingGroups(), var_values().size()); - - int capture_num = regex.NumberOfCapturingGroups() + 1; - std::vector captures(capture_num); - ASSERT_TRUE(regex.Match(request_path(), /*startpos=*/0, - /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, captures.data(), - captures.size())); - - EXPECT_EQ(captures[0], toStringPiece(request_path())); - - for (const auto& [name, value] : var_values()) { - int capture_index = regex.NamedCapturingGroups().at(name); - ASSERT_GE(capture_index, 0); - EXPECT_EQ(captures.at(capture_index), value); - } -} - -class GenPatternRegexWithoutMatch - : public testing::TestWithParam> { -protected: - const std::string& request_path() const { return std::get<0>(GetParam()); } - const std::string& url_pattern() const { return std::get<1>(GetParam()); } -}; - -INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWithoutMatch, - testing::ValuesIn(std::vector>( - {{"/media/12345/f/123/s00002.m4s", "/media/*.m4s"}, - {"/media/eff456/ll-sd-out.js", "/media/*"}, - {"/api/v1/1234/", "/api/*/v1/*"}, - {"/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=*}"}, - {"/api/v1/1234/", "/api/*/v1/**"}, - {"/api/*/1234/", "/api/*/1234/"}}))); - -TEST_P(GenPatternRegexWithoutMatch, WithCapture) { - absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); - ASSERT_OK(pattern); - - RE2 regex = RE2(toRegexPattern(pattern.value())); - ASSERT_TRUE(regex.ok()) << regex.error(); - - EXPECT_FALSE(regex.Match(request_path(), /*startpos=*/0, - /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, nullptr, 0)); -} - -} // namespace -} // namespace PatternTemplateInternal -} // namespace PatternTemplate -} // namespace Extensions -} // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template_test.cc b/source/extensions/path/pattern_template_lib/pattern_template_test.cc deleted file mode 100644 index fe1cd1dc6105b..0000000000000 --- a/source/extensions/path/pattern_template_lib/pattern_template_test.cc +++ /dev/null @@ -1,337 +0,0 @@ -#include -#include -#include - -#include "source/common/common/assert.h" -#include "source/common/protobuf/protobuf.h" -#include "source/extensions/path/pattern_template_lib/pattern_template.h" -#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" - -#include "test/test_common/logging.h" -#include "test/test_common/status_utility.h" -#include "test/test_common/utility.h" - -#include "gtest/gtest.h" - -namespace Envoy { -namespace Extensions { -namespace PatternTemplate { - -namespace { - -using ::Envoy::StatusHelpers::IsOkAndHolds; -using ::Envoy::StatusHelpers::StatusIs; - -// Capture regex for /{var1}/{var2}/{var3}/{var4}/{var5} -static constexpr absl::string_view kCaptureRegex = "/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"; -static constexpr absl::string_view kMatchUrl = "/val1/val2/val3/val4/val5"; - -TEST(ConvertURLPattern, ValidPattern) { - EXPECT_THAT(convertURLPatternSyntaxToRegex("/abc"), IsOkAndHolds("/abc")); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/**.mpd"), - IsOkAndHolds("/[a-zA-Z0-9-._~%!$&'()+,;:@/]*\\.mpd")); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/*/{resource=*}/{method=**}"), - IsOkAndHolds("/api/[a-zA-Z0-9-._~%!$&'()+,;:@]+/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@/]*)")); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/{VERSION}/{version}/{verSION}"), - IsOkAndHolds("/api/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" - "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)")); -} - -TEST(ConvertURLPattern, InvalidPattern) { - EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/v*/1234"), - StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/media/**/*/**"), - StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/\001\002\003\004\005\006\007"), - StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/{var12345678901234=*}"), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -class ParseRewriteHelperSuccess : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperSuccessTestSuite, ParseRewriteHelperSuccess, - testing::Values("/{var1}", "/{var1}{var2}", "/{var1}-{var2}", - "/abc/{var1}/def", "/{var1}/abd/{var2}", - "/abc-def-{var1}/a/{var1}")); - -TEST_P(ParseRewriteHelperSuccess, ParseRewriteHelperSuccessTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - EXPECT_OK(parseRewritePatternHelper(pattern)); -} - -class ParseRewriteHelperFailure : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperFailureTestSuite, ParseRewriteHelperFailure, - testing::Values("{var1}", "/{{var1}}", "/}va1{", "var1}", - "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); - -TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - EXPECT_THAT(parseRewritePatternHelper(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); -} - -class ParseRewriteSuccess : public testing::TestWithParam> { -protected: - const std::string& rewrite_pattern() const { return std::get<0>(GetParam()); } - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern expected_proto() const { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern expected_proto; - Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); - return expected_proto; - } -}; - -TEST(ParseRewrite, InvalidRegex) { - EXPECT_THAT(parseRewritePattern("/{var1}", "+[abc"), StatusIs(absl::StatusCode::kInternal)); -} - -INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, - testing::ValuesIn(std::vector>({ - {"/static", R"EOF(segments: {literal: "/static"} )EOF"}, - {"/{var1}", R"EOF(segments: - - literal: "/" - - var_index: 1)EOF"}, - {"/{var1}", R"EOF(segments: - - literal: "/" - - var_index: 1)EOF"}, - {"/{var1}/{var1}/{var1}", R"EOF(segments: - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 1)EOF"}, - {"/{var3}/{var1}/{var2}", R"EOF(segments - - literal: "/" - - var_index: 3 - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 2)EOF"}, - {"/{var3}/abc/def/{var2}.suffix", R"EOF(segments: - - literal: "/" - - var_index: 3 - - literal: "/abc/def/" - - var_index: 2 - - literal: ".suffix")EOF"}, - {"/abc/{var1}/{var2}/def", R"EOF(segments - - literal: "/abc/" - - var_index: 1 - - literal: "/" - - var_index: 2 - - literal: "/def")EOF"}, - {"/{var1}{var2}", R"EOF(segments - - literal: "/" - - var_index: 1 - - ar_index: 2)EOF"}, - {"/{var1}-{var2}/bucket-{var3}.suffix", R"EOF(segments - - literal: "/" - - var_index: 1 - - literal: "-" - - var_index: 2 - - literal: "/bucket-" - - var_index: 3 - - literal: ".suffix")EOF"}, - }))); - -TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { - absl::StatusOr rewrite = - parseRewritePattern(rewrite_pattern(), kCaptureRegex); - ASSERT_OK(rewrite); - // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); -} - -class ParseRewriteFailure : public testing::TestWithParam {}; - -INSTANTIATE_TEST_SUITE_P(ParseRewriteFailureTestSuite, ParseRewriteFailure, - testing::Values("{var1}", "/{var6}", "/{{var1}}", "/}va1{", "var1}", - "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); - -TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { - std::string pattern = GetParam(); - SCOPED_TRACE(pattern); - - EXPECT_THAT(parseRewritePattern(pattern, kCaptureRegex), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -class RewriteUrlTemplateSuccess - : public testing::TestWithParam> { -protected: - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto() const { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern proto; - Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); - return proto; - } - const std::string& expected_rewritten_url() const { return std::get<1>(GetParam()); } -}; - -INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateSuccess, - testing::ValuesIn(std::vector>( - {{R"EOF(segments: { literal: "/static" })EOF", "/static"}, - {R"EOF(segments: - - literal: "/" - - var_index: 1)EOF", - "/val1"}, - {R"EOF(segments: - - literal: "/" - - var_index: 1)EOF", - "/val1"}, - {R"EOF(segments: - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 1)EOF", - "/val1/val1/val1"}, - {R"EOF(segments: - - literal: "/" - - var_index: 3 - - literal: "/" - - var_index: 1 - - literal: "/" - - var_index: 2)EOF", - "/val3/val1/val2"}, - {R"EOF(segments: - - literal: "/" - - var_index: 3 - - literal: "/abc/def/" - - var_index: 2 - - literal: ".suffix")EOF", - "/val3/abc/def/val2.suffix"}, - {R"EOF(segments: - - literal: "/" - - var_index: 3 - - var_index: 2 - - literal: "." - - var_index: 1)EOF", - "/val3val2.val1"}, - {R"EOF(segments: - - literal: "/abc/" - - var_index: 1 - - literal: "/" - - var_index: 5 - - literal: "/def")EOF", - "/abc/val1/val5/def"}}))); - -TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { - absl::StatusOr rewritten_url = - RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto()); - ASSERT_OK(rewritten_url); - EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); -} - -TEST(RewriteUrlTemplateFailure, BadRegex) { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- var_index: 1 - )EOF"; - - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "+/bad_regex", rewrite_proto), - StatusIs(absl::StatusCode::kInternal)); -} - -TEST(RewriteUrlTemplateFailure, RegexNoMatch) { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- var_index: 1 - )EOF"; - - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, "/no_match_regex", rewrite_proto), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- var_index: 0 - )EOF"; - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { - envoy::extensions::pattern_template::v3::RouteUrlRewritePattern rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- var_index: 6 - )EOF"; - - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - - EXPECT_THAT(RewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -class URLPatternMatchAndRewrite - : public testing::TestWithParam< - std::tuple> { -protected: - const std::string& url_pattern() const { return std::get<0>(GetParam()); } - const std::string& rewrite_pattern() const { return std::get<1>(GetParam()); } - const std::string& match_url() const { return std::get<2>(GetParam()); } - const std::string& expected_rewritten_url() const { return std::get<3>(GetParam()); } -}; - -INSTANTIATE_TEST_SUITE_P( - URLPatternMatchAndRewriteTestSuite, URLPatternMatchAndRewrite, - testing::ValuesIn(std::vector>( - {{"/api/users/{id}/{path=**}", "/users/{id}/{path}", "/api/users/21334/profile.json", - "/users/21334/profile.json"}, - {"/videos/*/{id}/{format}/{rendition}/{segment=**}.ts", - "/{id}/{format}/{rendition}/{segment}.ts", "/videos/lib/132939/hls/13/segment_00001.ts", - "/132939/hls/13/segment_00001.ts"}, - {"/region/{region}/bucket/{name}/{method=**}", "/{region}/bucket-{name}/{method}", - "/region/eu/bucket/prod-storage/object.pdf", "/eu/bucket-prod-storage/object.pdf"}, - {"/region/{region}/bucket/{name}/{method=**}", "/{region}{name}/{method}", - "/region/eu/bucket/prod-storage/object.pdf", "/euprod-storage/object.pdf"}}))); - -TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { - absl::StatusOr regex = convertURLPatternSyntaxToRegex(url_pattern()); - ASSERT_OK(regex); - - absl::StatusOr rewrite_proto = - parseRewritePattern(rewrite_pattern(), regex.value()); - ASSERT_OK(rewrite_proto); - - absl::StatusOr rewritten_url = - RewriteURLTemplatePattern(match_url(), regex.value(), rewrite_proto.value()); - ASSERT_OK(rewritten_url); - - EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); -} - -} // namespace - -} // namespace PatternTemplate -} // namespace Extensions -} // namespace Envoy diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index ee3c40a7c460a..f311355e5a9df 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -15,18 +15,23 @@ class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredica public: Router::PathRewritePredicateSharedPtr createPathRewritePredicate(const Protobuf::Message& rewrite_config) override { - auto cast_rewrite_config = - MessageUtil::downcastAndValidate( + auto path_rewrite_config = + MessageUtil::downcastAndValidate( rewrite_config, ProtobufMessage::getStrictValidationVisitor()); - return std::make_shared(cast_rewrite_config); + + if (!PatternTemplate::isValidPathTemplateRewritePattern(path_rewrite_config.path_template_rewrite()).ok()) { + throw EnvoyException(fmt::format("path_rewrite_policy.path_template_rewrite {} is invalid", + path_rewrite_config.path_template_rewrite())); + } + return std::make_shared(path_rewrite_config); } ProtobufTypes::MessagePtr createEmptyConfigProto() override { return std::make_unique(); } - std::string name() const override { return "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate";} + std::string name() const override { return "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate";} std::string category() const override { return "envoy.path.rewrite"; } }; diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index a012029646500..642c875d806c8 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -22,6 +22,8 @@ namespace Extensions { namespace PatternTemplate { namespace Rewrite { +const absl::string_view NAME = "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"; + class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { public: explicit PatternTemplateRewritePredicate( @@ -29,10 +31,6 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { rewrite_config) : url_rewrite_pattern_(rewrite_config.path_template_rewrite()) {} - absl::string_view name() const override { - return "envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate"; - } - std::string pattern() const override { return url_rewrite_pattern_; } absl::StatusOr rewritePattern(absl::string_view current_pattern, @@ -40,6 +38,8 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { static absl::Status isValidRewritePattern(std::string match_pattern, std::string rewrite_pattern); + absl::string_view name() const override { return NAME;} + private: // Returns the rewritten URL path based on the given parsed rewrite pattern. // Used for template-based URL rewrite. From fc9f2eaad3650632c80ac9a47205c7cf681df8e2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 18:47:25 +0000 Subject: [PATCH 105/238] Checkpoint changes Signed-off-by: silverstar195 --- test/common/router/config_impl_test.cc | 70 +++++++++++++------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index 00a31acb5399b..b5b1c4ab24800 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -8844,14 +8844,14 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsFilledFromConfigInRouteAction) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/bar/{country}/{lang}" route: cluster: some-cluster path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" @@ -8880,7 +8880,7 @@ TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{lang}/{state}" @@ -8912,7 +8912,7 @@ TEST_F(RouteMatcherTest, MixedPathPatternMatch) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{lang}/{state}" @@ -8920,7 +8920,7 @@ TEST_F(RouteMatcherTest, MixedPathPatternMatch) { route: { cluster: path-pattern-cluster-one} - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/boo/{go}/{fly}/{bat}" @@ -8928,7 +8928,7 @@ TEST_F(RouteMatcherTest, MixedPathPatternMatch) { route: { cluster: path-pattern-cluster-two} - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/foo/boo/{go}/{fly}/{bat}/{sno}" @@ -8979,7 +8979,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimple) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{two}" @@ -8987,7 +8987,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimple) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/rest/{two}/{one}" @@ -9013,7 +9013,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimpleTwo) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one=*}/{two}" @@ -9021,7 +9021,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimpleTwo) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{two}/{one}" @@ -9047,7 +9047,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteCaseSensitive) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{two}" @@ -9055,13 +9055,13 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteCaseSensitive) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{two}/{one}" - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/REST/{one}/{two}" @@ -9069,7 +9069,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteCaseSensitive) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/TEST/{one}" @@ -9102,7 +9102,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingBracket) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one/{two}" @@ -9110,7 +9110,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingBracket) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{two}/{one}" @@ -9119,7 +9119,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingBracket) { EXPECT_THROW_WITH_MESSAGE( TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), - EnvoyException, "path_match_policy /rest/{one/{two} is invalid"); + EnvoyException, "path_match_policy.path_template /rest/{one/{two} is invalid"); } TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { @@ -9130,7 +9130,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{two}" @@ -9138,7 +9138,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/rest/{one}/{two}/{missing}" @@ -9149,7 +9149,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { EXPECT_THROW_WITH_MESSAGE( TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException, - "mismatch between path_match_policy /rest/{one}/{two} and path_rewrite_policy " + "mismatch between variables in path_match_policy /rest/{one}/{two} and path_rewrite_policy " "/rest/{one}/{two}/{missing}"); } @@ -9161,7 +9161,7 @@ TEST_F(RouteMatcherTest, PatternMatchInvalidVariableName) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{on==e}/{two}" @@ -9169,7 +9169,7 @@ TEST_F(RouteMatcherTest, PatternMatchInvalidVariableName) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/rest/{one}/{two}/{missing}" @@ -9179,7 +9179,7 @@ TEST_F(RouteMatcherTest, PatternMatchInvalidVariableName) { EXPECT_THROW_WITH_MESSAGE( TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), - EnvoyException, "path_match_policy /rest/{on==e}/{two} is invalid"); + EnvoyException, "path_match_policy.path_template /rest/{on==e}/{two} is invalid"); } TEST_F(RouteMatcherTest, PatternMatchWildcardUnnamedVariable) { @@ -9190,7 +9190,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardUnnamedVariable) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/*/{two}" @@ -9198,7 +9198,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardUnnamedVariable) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{two}" @@ -9224,7 +9224,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariable) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/**" @@ -9232,7 +9232,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariable) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{one}" @@ -9259,7 +9259,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariableNamed) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one=*}/{last=**}" @@ -9267,7 +9267,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariableNamed) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{last}" @@ -9294,7 +9294,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardMiddleVariableNamed) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{middle=videos/*}/end" @@ -9302,7 +9302,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardMiddleVariableNamed) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{middle}" @@ -9329,7 +9329,7 @@ TEST_F(RouteMatcherTest, PatternMatchCaseSensitiveVariableNames) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{One}/end" @@ -9337,7 +9337,7 @@ TEST_F(RouteMatcherTest, PatternMatchCaseSensitiveVariableNames) { route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.v3.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{One}/{one}" @@ -9364,7 +9364,7 @@ TEST_F(RouteMatcherTest, PatternMatchCaseTooManyVariableNames) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.v3.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_match_predicate typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{two}/{three}/{four}/{five}/{six}" @@ -9378,7 +9378,7 @@ TEST_F(RouteMatcherTest, PatternMatchCaseTooManyVariableNames) { EXPECT_THROW_WITH_MESSAGE( TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException, - "path_match_policy /rest/{one}/{two}/{three}/{four}/{five}/{six} is invalid"); + "path_match_policy.path_template /rest/{one}/{two}/{three}/{four}/{five}/{six} is invalid"); } TEST_F(RouteConfigurationV2, DefaultInternalRedirectPolicyIsSensible) { From 3d8cc7b28024dbe99add81cf8d0d45fa71338523 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 18:50:00 +0000 Subject: [PATCH 106/238] Remove unused tests Signed-off-by: silverstar195 --- test/extensions/path/match_config_test.cc | 44 --------------------- test/extensions/path/rewrite_config_test.cc | 43 -------------------- 2 files changed, 87 deletions(-) delete mode 100644 test/extensions/path/match_config_test.cc delete mode 100644 test/extensions/path/rewrite_config_test.cc diff --git a/test/extensions/path/match_config_test.cc b/test/extensions/path/match_config_test.cc deleted file mode 100644 index 58f0a4061721d..0000000000000 --- a/test/extensions/path/match_config_test.cc +++ /dev/null @@ -1,44 +0,0 @@ -#include "envoy/registry/registry.h" -#include "envoy/router/pattern_template.h" - -#include "source/common/stream_info/filter_state_impl.h" -#include "source/extensions/pattern_template/match/config.h" - -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -using namespace testing; - -namespace Envoy { -namespace Extensions { -namespace PatternTemplate { -namespace { - -class PathMatchPredicateFactoryConfigTest : public testing::Test { -protected: - PathMatchPredicateFactoryConfigTest() : filter_state_(StreamInfo::FilterState::LifeSpan::FilterChain) { - factory_ = Registry::FactoryRegistry::getFactory( - "envoy.path.match.pattern_template.v3.pattern_template_match_predicate"); - config_ = factory_->createEmptyConfigProto(); - ASSERT(config_); - } - - StreamInfo::FilterStateImpl filter_state_; - Router::PatternTemplatePredicateFactory* factory_; - ProtobufTypes::MessagePtr config_; -}; - -TEST_F(PathMatchPredicateFactoryConfigTest, EmptyCreation) { - std::string current_route_name = "fake_current_route"; - // Create the predicate for the first time. - { - auto predicate = - factory_->createPathMatchPredicate(config_, "/url_pattern/{TEST}"); - ASSERT(predicate); - } -} - -} // namespace -} // namespace PatternTemplate -} // namespace Extensions -} // namespace Envoy diff --git a/test/extensions/path/rewrite_config_test.cc b/test/extensions/path/rewrite_config_test.cc deleted file mode 100644 index 6dd0a036fe29d..0000000000000 --- a/test/extensions/path/rewrite_config_test.cc +++ /dev/null @@ -1,43 +0,0 @@ -#include "envoy/registry/registry.h" -#include "envoy/router/pattern_template.h" - -#include "source/common/stream_info/filter_state_impl.h" -#include "source/extensions/pattern_template/rewrite/config.h" - -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -using namespace testing; - -namespace Envoy { -namespace Extensions { -namespace PatternTemplate { -namespace { - -class PatternTemplateRewriteConfigTest : public testing::Test { -protected: - PatternTemplateRewriteConfigTest() : filter_state_(StreamInfo::FilterState::LifeSpan::FilterChain) { - factory_ = Registry::FactoryRegistry::getFactory( - "envoy.pattern_template.pattern_template_rewrite_predicate"); - config_ = factory_->createEmptyConfigProto(); - } - - StreamInfo::FilterStateImpl filter_state_; - Router::PatternTemplatePredicateFactory* factory_; - ProtobufTypes::MessagePtr config_; -}; - -TEST_F(PatternTemplateRewriteConfigTest, EmptyCreation) { - std::string current_route_name = "fake_current_route"; - // Create the predicate for the first time. - { - auto predicate = - factory_->createUrlTemplateRewritePredicate("/url_pattern/{TEST}", "rewrite_pattern"); - ASSERT(predicate); - } -} - -} // namespace -} // namespace PatternTemplate -} // namespace Extensions -} // namespace Envoy From a974d0909595acb033977656196568d74821a38c Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 19:03:48 +0000 Subject: [PATCH 107/238] Casing changes Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 4 +-- source/common/router/config_impl.h | 4 +-- .../pattern_template_lib/pattern_template.cc | 14 ++++---- .../pattern_template_lib/pattern_template.h | 2 +- .../pattern_template_internal.cc | 36 +++++++++---------- .../pattern_template_internal.h | 6 ++-- .../pattern_template_internal_test.cc | 28 +++++++-------- .../pattern_template_test.cc | 20 +++++------ 8 files changed, 57 insertions(+), 57 deletions(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 35e67907cac03..c918d3f625dd5 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -358,8 +358,8 @@ std::vector InternalRedirectPolicyImpl::pred PathRewritePolicyImpl::PathRewritePolicyImpl() : enabled_(false){}; PathRewritePolicyImpl::PathRewritePolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string route_url) - : route_url_(route_url), enabled_(true) { + ProtobufMessage::ValidationVisitor& validator, std::string path) + : path_(path), enabled_(true) { predicate_factory_ = &Envoy::Config::Utility::getAndCheckFactory(typed_config); ASSERT(predicate_factory_); // factory not found diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index f1b1a6f9a089e..a978a97765c0f 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -469,10 +469,10 @@ class PathMatchPolicyImpl : public PathMatchPolicy { PathMatchPredicateSharedPtr predicate() const override; - std::string route_url() const { return route_url_; } + std::string path() const { return path_; } private: - const std::string route_url_; + const std::string path_; const bool enabled_; ProtobufTypes::MessagePtr predicate_config_; PathMatchPredicateFactory* predicate_factory_; diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc index 24a1a13d27a61..860920b8f89c7 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -25,7 +25,7 @@ using PatternTemplateInternal::ParsedUrlPattern; #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif -inline re2::StringPiece ToStringPiece(absl::string_view text) { return {text.data(), text.size()}; } +inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { @@ -44,7 +44,7 @@ parseRewritePatternHelper(absl::string_view pattern) { // Don't allow contiguous '/' patterns. static const LazyRE2 invalid_regex = {"^.*//.*$"}; - if (RE2::FullMatch(ToStringPiece(pattern), *invalid_regex)) { + if (RE2::FullMatch(toStringPiece(pattern), *invalid_regex)) { return absl::InvalidArgumentError("Invalid rewrite literal pattern"); } @@ -59,7 +59,7 @@ parseRewritePatternHelper(absl::string_view pattern) { if (!PatternTemplateInternal::isValidRewriteLiteral(segments1[0])) { return absl::InvalidArgumentError("Invalid rewrite literal pattern"); } - result.emplace_back(segments1[0], RewriteStringKind::kLiteral); + result.emplace_back(segments1[0], RewriteStringKind::KLiteral); } if (segments1.size() < 2) { @@ -77,7 +77,7 @@ parseRewritePatternHelper(absl::string_view pattern) { if (!PatternTemplateInternal::isValidIndent(segments2[0])) { return absl::InvalidArgumentError("Invalid variable name"); } - result.emplace_back(segments2[0], RewriteStringKind::kVariable); + result.emplace_back(segments2[0], RewriteStringKind::KVariable); } return result; } @@ -85,7 +85,7 @@ parseRewritePatternHelper(absl::string_view pattern) { absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { envoy::extensions::pattern_template::PatternTemplateRewriteSegments parsed_pattern; - RE2 regex = RE2(ToStringPiece(capture_regex)); + RE2 regex = RE2(toStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); } @@ -100,10 +100,10 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) for (const auto& [str, kind] : processed_pattern) { switch (kind) { - case RewriteStringKind::kLiteral: + case RewriteStringKind::KLiteral: parsed_pattern.add_segments()->set_literal(std::string(str)); break; - case RewriteStringKind::kVariable: + case RewriteStringKind::KVariable: auto it = capture_index_map.find(std::string(str)); if (it == capture_index_map.end()) { return absl::InvalidArgumentError("Nonexisting variable name"); diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 8da6f1ea2731d..a32d4cb882335 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -18,7 +18,7 @@ namespace Envoy { namespace Extensions { namespace PatternTemplate { -enum class RewriteStringKind { kVariable, kLiteral }; +enum class RewriteStringKind { KVariable, KLiteral }; struct RewritePatternSegment { RewritePatternSegment(absl::string_view str, RewriteStringKind kind) : str(str), kind(kind) {} diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc index 889eb5834fdfd..756964335f344 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc @@ -44,7 +44,7 @@ constexpr absl::string_view kLiteral = "a-zA-Z0-9-._~" // Unreserved ":@"; // Default operator used for the variable when none specified. -constexpr Operator kDefaultVariableOperator = Operator::kPathGlob; +constexpr Operator kDefaultVariableOperator = Operator::KPathGlob; // Visitor for displaying debug info of a ParsedSegment/Variable.var_match. struct ToStringVisitor { @@ -74,9 +74,9 @@ std::string toString(const Literal val) { return std::string(val); } std::string toString(const Operator val) { switch (val) { - case Operator::kPathGlob: + case Operator::KPathGlob: return "*"; - case Operator::kTextGlob: + case Operator::KTextGlob: return "**"; } } @@ -95,7 +95,7 @@ template std::string ToStringVisitor::operator()(const T& val) cons } template -absl::StatusOr AlsoUpdatePattern( +absl::StatusOr alsoUpdatePattern( absl::FunctionRef>(absl::string_view)> consume_func, absl::string_view* patt) { @@ -111,9 +111,9 @@ absl::StatusOr AlsoUpdatePattern( } // namespace -std::string Variable::DebugString() const { return toString(*this); } +std::string Variable::debugString() const { return toString(*this); } -std::string ParsedUrlPattern::DebugString() const { +std::string ParsedUrlPattern::debugString() const { return absl::StrCat("/", absl::StrJoin(parsed_segments, "/", ToStringFormatter()), suffix.value_or("")); } @@ -149,10 +149,10 @@ absl::StatusOr> consumeLiteral(absl::string_view pattern) absl::StatusOr> consumeOperator(absl::string_view pattern) { if (absl::StartsWith(pattern, "**")) { - return ParsedResult(Operator::kTextGlob, pattern.substr(2)); + return ParsedResult(Operator::KTextGlob, pattern.substr(2)); } if (absl::StartsWith(pattern, "*")) { - return ParsedResult(Operator::kPathGlob, pattern.substr(1)); + return ParsedResult(Operator::KPathGlob, pattern.substr(1)); } return absl::InvalidArgumentError("Invalid Operator"); } @@ -187,7 +187,7 @@ absl::StatusOr> consumeVariable(absl::string_view pattern absl::variant var_match; if (var_patt[0] == '*') { - absl::StatusOr status = AlsoUpdatePattern(consumeOperator, &var_patt); + absl::StatusOr status = alsoUpdatePattern(consumeOperator, &var_patt); if (!status.ok()) { return status.status(); } @@ -195,7 +195,7 @@ absl::StatusOr> consumeVariable(absl::string_view pattern } else { - absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &var_patt); + absl::StatusOr status = alsoUpdatePattern(consumeLiteral, &var_patt); if (!status.ok()) { return status.status(); } @@ -246,7 +246,7 @@ absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { if (seen_text_glob) { return absl::InvalidArgumentError("Glob after text glob."); } - seen_text_glob = (absl::get(segment) == Operator::kTextGlob); + seen_text_glob = (absl::get(segment) == Operator::KTextGlob); } else if (absl::holds_alternative(segment)) { const Variable& var = absl::get(segment); if (var.var_match.empty()) { @@ -262,7 +262,7 @@ absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { if (seen_text_glob) { return absl::InvalidArgumentError("Glob after text glob."); } - seen_text_glob = (absl::get(var_seg) == Operator::kTextGlob); + seen_text_glob = (absl::get(var_seg) == Operator::KTextGlob); } } } @@ -287,21 +287,21 @@ absl::StatusOr parseURLPatternSyntax(absl::string_view url_pat ParsedSegment segment; if (url_pattern[0] == '*') { - absl::StatusOr status = AlsoUpdatePattern(consumeOperator, &url_pattern); + absl::StatusOr status = alsoUpdatePattern(consumeOperator, &url_pattern); if (!status.ok()) { return status.status(); } segment = *std::move(status); } else if (url_pattern[0] == '{') { - absl::StatusOr status = AlsoUpdatePattern(consumeVariable, &url_pattern); + absl::StatusOr status = alsoUpdatePattern(consumeVariable, &url_pattern); if (!status.ok()) { return status.status(); } segment = *std::move(status); } else { - absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &url_pattern); + absl::StatusOr status = alsoUpdatePattern(consumeLiteral, &url_pattern); if (!status.ok()) { return status.status(); } @@ -321,7 +321,7 @@ absl::StatusOr parseURLPatternSyntax(absl::string_view url_pat } else { // Not followed by '/', treat as suffix. - absl::StatusOr status = AlsoUpdatePattern(consumeLiteral, &url_pattern); + absl::StatusOr status = alsoUpdatePattern(consumeLiteral, &url_pattern); if (!status.ok()) { return status.status(); } @@ -358,9 +358,9 @@ std::string toRegexPattern(Operator pattern) { static const std::string* kPathGlobRegex = new std::string(absl::StrCat("[", kLiteral, "]+")); static const std::string* kTextGlobRegex = new std::string(absl::StrCat("[", kLiteral, "/]*")); switch (pattern) { - case Operator::kPathGlob: // "*" + case Operator::KPathGlob: // "*" return *kPathGlobRegex; - case Operator::kTextGlob: // "**" + case Operator::KTextGlob: // "**" return *kTextGlobRegex; } } diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h index 9ded8b4fa1515..d8666883a3583 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.h +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.h @@ -22,7 +22,7 @@ namespace PatternTemplate { namespace PatternTemplateInternal { using Literal = absl::string_view; -enum class Operator { kPathGlob, kTextGlob }; +enum class Operator { KPathGlob, KTextGlob }; struct RewriteSegment { // Represents a segment of the rewritten URL, including any path segments, @@ -42,7 +42,7 @@ struct Variable { Variable(absl::string_view name, std::vector> match) : var_name(name), var_match(match) {} - std::string DebugString() const; + std::string debugString() const; }; using ParsedSegment = absl::variant; @@ -52,7 +52,7 @@ struct ParsedUrlPattern { absl::optional suffix; absl::flat_hash_set captured_variables; - std::string DebugString() const; + std::string debugString() const; }; bool isValidLiteral(absl::string_view pattern); diff --git a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc index 116dbae70f826..a5a19c4c70c61 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc @@ -367,9 +367,9 @@ struct GenPatternTestCase { class GenPatternRegexWithMatch : public testing::TestWithParam { protected: - const std::string& request_path() const { return GetParam().path; } - const std::string& url_pattern() const { return GetParam().pattern; } - std::vector> const var_values() { + const std::string& requestPath() const { return GetParam().path; } + const std::string& urlPattern() const { return GetParam().pattern; } + std::vector> const varValues() { return GetParam().captures; } }; @@ -414,22 +414,22 @@ INSTANTIATE_TEST_SUITE_P( {{"VERSION", "v1"}, {"version", "v2"}, {"verSION", "v3"}}))); TEST_P(GenPatternRegexWithMatch, WithCapture) { - absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); + absl::StatusOr pattern = parseURLPatternSyntax(urlPattern()); ASSERT_OK(pattern); RE2 regex = RE2(toRegexPattern(pattern.value())); ASSERT_TRUE(regex.ok()) << regex.error(); - ASSERT_EQ(regex.NumberOfCapturingGroups(), var_values().size()); + ASSERT_EQ(regex.NumberOfCapturingGroups(), varValues().size()); int capture_num = regex.NumberOfCapturingGroups() + 1; std::vector captures(capture_num); - ASSERT_TRUE(regex.Match(request_path(), /*startpos=*/0, - /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, captures.data(), + ASSERT_TRUE(regex.Match(requestPath(), /*startpos=*/0, + /*endpos=*/requestPath().size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())); - EXPECT_EQ(captures[0], toStringPiece(request_path())); + EXPECT_EQ(captures[0], toStringPiece(requestPath())); - for (const auto& [name, value] : var_values()) { + for (const auto& [name, value] : varValues()) { int capture_index = regex.NamedCapturingGroups().at(name); ASSERT_GE(capture_index, 0); EXPECT_EQ(captures.at(capture_index), value); @@ -439,8 +439,8 @@ TEST_P(GenPatternRegexWithMatch, WithCapture) { class GenPatternRegexWithoutMatch : public testing::TestWithParam> { protected: - const std::string& request_path() const { return std::get<0>(GetParam()); } - const std::string& url_pattern() const { return std::get<1>(GetParam()); } + const std::string& requestPath() const { return std::get<0>(GetParam()); } + const std::string& urlPattern() const { return std::get<1>(GetParam()); } }; INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWithoutMatch, @@ -453,14 +453,14 @@ INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWi {"/api/*/1234/", "/api/*/1234/"}}))); TEST_P(GenPatternRegexWithoutMatch, WithCapture) { - absl::StatusOr pattern = parseURLPatternSyntax(url_pattern()); + absl::StatusOr pattern = parseURLPatternSyntax(urlPattern()); ASSERT_OK(pattern); RE2 regex = RE2(toRegexPattern(pattern.value())); ASSERT_TRUE(regex.ok()) << regex.error(); - EXPECT_FALSE(regex.Match(request_path(), /*startpos=*/0, - /*endpos=*/request_path().size(), RE2::ANCHOR_BOTH, nullptr, 0)); + EXPECT_FALSE(regex.Match(requestPath(), /*startpos=*/0, + /*endpos=*/requestPath().size(), RE2::ANCHOR_BOTH, nullptr, 0)); } } // namespace diff --git a/test/extensions/path/pattern_template_lib/pattern_template_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_test.cc index 500de1611033c..57b7097fd12df 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_test.cc @@ -84,8 +84,8 @@ TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { class ParseRewriteSuccess : public testing::TestWithParam> { protected: - const std::string& rewrite_pattern() const { return std::get<0>(GetParam()); } - envoy::extensions::pattern_template::PatternTemplateRewriteSegments expected_proto() const { + const std::string& rewritePattern() const { return std::get<0>(GetParam()); } + envoy::extensions::pattern_template::PatternTemplateRewriteSegments expectedProto() const { envoy::extensions::pattern_template::PatternTemplateRewriteSegments expected_proto; Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); return expected_proto; @@ -147,7 +147,7 @@ INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { absl::StatusOr rewrite = - parseRewritePattern(rewrite_pattern(), kCaptureRegex); + parseRewritePattern(rewritePattern(), kCaptureRegex); ASSERT_OK(rewrite); // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); } @@ -174,7 +174,7 @@ class RewriteUrlTemplateSuccess Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); return proto; } - const std::string& expected_rewritten_url() const { return std::get<1>(GetParam()); } + const std::string& expectedRewrittenUrl() const { return std::get<1>(GetParam()); } }; INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateSuccess, @@ -230,7 +230,7 @@ TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { absl::StatusOr rewritten_url = rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto()); ASSERT_OK(rewritten_url); - EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); + EXPECT_EQ(rewritten_url.value(), expectedRewrittenUrl()); } TEST(RewriteUrlTemplateFailure, BadRegex) { @@ -297,9 +297,9 @@ class URLPatternMatchAndRewrite std::tuple> { protected: const std::string& url_pattern() const { return std::get<0>(GetParam()); } - const std::string& rewrite_pattern() const { return std::get<1>(GetParam()); } - const std::string& match_url() const { return std::get<2>(GetParam()); } - const std::string& expected_rewritten_url() const { return std::get<3>(GetParam()); } + const std::string& rewritePattern() const { return std::get<1>(GetParam()); } + const std::string& matchUrl() const { return std::get<2>(GetParam()); } + const std::string& expectedRewrittenUrl() const { return std::get<3>(GetParam()); } }; INSTANTIATE_TEST_SUITE_P( @@ -320,11 +320,11 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { ASSERT_OK(regex); absl::StatusOr - rewrite_proto = parseRewritePattern(rewrite_pattern(), regex.value()); + rewrite_proto = parseRewritePattern(rewritePattern(), regex.value()); ASSERT_OK(rewrite_proto); absl::StatusOr rewritten_url = - rewriteURLTemplatePattern(match_url(), regex.value(), rewrite_proto.value()); + rewriteURLTemplatePattern(matchUrl(), regex.value(), rewrite_proto.value()); ASSERT_OK(rewritten_url); EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); From 1f57b54c1e404d4be4c0b2accdaa8a1434e87c00 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 19:16:14 +0000 Subject: [PATCH 108/238] Run format Signed-off-by: silverstar195 --- source/common/http/async_client_impl.h | 4 +-- source/common/router/config_impl.cc | 8 ----- source/common/router/config_impl.h | 31 +++++++++---------- .../path/match/pattern_template/BUILD | 1 - .../path/match/pattern_template/config.cc | 3 +- .../path/match/pattern_template/config.h | 17 +++++----- .../pattern_template_match.cc | 9 +++--- .../pattern_template/pattern_template_match.h | 2 +- .../path/pattern_template_lib/BUILD | 2 -- .../pattern_template_lib/pattern_template.h | 5 +-- .../pattern_template_internal.h | 3 +- .../path/rewrite/pattern_template/BUILD | 2 +- .../path/rewrite/pattern_template/config.cc | 3 +- .../path/rewrite/pattern_template/config.h | 13 +++++--- .../pattern_template_rewrite.cc | 19 ++++++------ .../pattern_template_rewrite.h | 16 +++++----- test/common/router/config_impl_test.cc | 3 +- .../pattern_template_internal_test.cc | 4 +-- tools/extensions/extensions_schema.yaml | 2 ++ 19 files changed, 66 insertions(+), 81 deletions(-) diff --git a/source/common/http/async_client_impl.h b/source/common/http/async_client_impl.h index 48ad78f73939c..41841f25719da 100644 --- a/source/common/http/async_client_impl.h +++ b/source/common/http/async_client_impl.h @@ -231,9 +231,7 @@ class AsyncStreamImpl : public AsyncClient::Stream, const Router::InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } - const Router::PathMatchPolicy& pathMatchPolicy() const override { - return path_match_policy_; - } + const Router::PathMatchPolicy& pathMatchPolicy() const override { return path_match_policy_; } const Router::PathRewritePolicy& pathRewritePolicy() const override { return path_rewrite_policy_; } diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index db8b47f7fff21..bee09e58f4cdc 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1505,22 +1505,14 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { -<<<<<<< HEAD finalizePathHeader(headers, path_match_policy_.predicate()->pattern(), insert_envoy_original_path); -======= - finalizePathHeader(headers, "", insert_envoy_original_path); ->>>>>>> upstream/main } absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { -<<<<<<< HEAD return currentUrlPathAfterRewriteWithMatchedPath(headers, path_match_policy_.predicate()->pattern()); -======= - return currentUrlPathAfterRewriteWithMatchedPath(headers, ""); ->>>>>>> upstream/main } RouteConstSharedPtr diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 285a68f6722cb..5b87080b5fc34 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -463,8 +463,8 @@ class RouteTracingImpl : public RouteTracing { */ class PathMatchPolicyImpl : public PathMatchPolicy { public: - PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string route_url); + PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, + ProtobufMessage::ValidationVisitor& validator, std::string route_url); // Default constructor that disables internal redirect. PathMatchPolicyImpl(); @@ -489,8 +489,8 @@ class PathMatchPolicyImpl : public PathMatchPolicy { */ class PathRewritePolicyImpl : public PathRewritePolicy { public: - PathRewritePolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string route_url); + PathRewritePolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, + ProtobufMessage::ValidationVisitor& validator, std::string route_url); // Default constructor that disables internal redirect. PathRewritePolicyImpl(); @@ -989,13 +989,11 @@ class RouteEntryImplBase : public RouteEntry, ProtobufMessage::ValidationVisitor& validator, absl::string_view current_route_name) const; - PathMatchPolicyImpl - buildPathMatchPolicy(envoy::config::route::v3::Route route, - ProtobufMessage::ValidationVisitor& validator) const; + PathMatchPolicyImpl buildPathMatchPolicy(envoy::config::route::v3::Route route, + ProtobufMessage::ValidationVisitor& validator) const; - PathRewritePolicyImpl - buildPathRewritePolicy(envoy::config::route::v3::Route route, - ProtobufMessage::ValidationVisitor& validator) const; + PathRewritePolicyImpl buildPathRewritePolicy(envoy::config::route::v3::Route route, + ProtobufMessage::ValidationVisitor& validator) const; RouteConstSharedPtr pickClusterViaClusterHeader(const Http::LowerCaseString& cluster_header_name, const Http::HeaderMap& headers) const; @@ -1080,10 +1078,10 @@ class RouteEntryImplBase : public RouteEntry, class PathMatchPolicyRouteEntryImpl : public RouteEntryImplBase { public: PathMatchPolicyRouteEntryImpl(const VirtualHostImpl& vhost, - const envoy::config::route::v3::Route& route, - const OptionalHttpFilters& optional_http_filters, - Server::Configuration::ServerFactoryContext& factory_context, - ProtobufMessage::ValidationVisitor& validator); + const envoy::config::route::v3::Route& route, + const OptionalHttpFilters& optional_http_filters, + Server::Configuration::ServerFactoryContext& factory_context, + ProtobufMessage::ValidationVisitor& validator); // Router::PathMatchCriterion const std::string& matcher() const override { return match_pattern_; } @@ -1101,8 +1099,9 @@ class PathMatchPolicyRouteEntryImpl : public RouteEntryImplBase { // Router::RouteEntry absl::optional currentUrlPathAfterRewrite(const Http::RequestHeaderMap& headers) const override; - private: - const std::string match_pattern_; + +private: + const std::string match_pattern_; }; /** diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD index c9c4b39ea1c38..39e9df419bfe5 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/pattern_template/BUILD @@ -2,7 +2,6 @@ load( "//bazel:envoy_build_system.bzl", "envoy_cc_extension", "envoy_cc_library", - "envoy_cc_test", "envoy_extension_package", ) diff --git a/source/extensions/path/match/pattern_template/config.cc b/source/extensions/path/match/pattern_template/config.cc index 11e5c954f8c81..4295b98fcdbef 100644 --- a/source/extensions/path/match/pattern_template/config.cc +++ b/source/extensions/path/match/pattern_template/config.cc @@ -8,8 +8,7 @@ namespace Extensions { namespace PatternTemplate { namespace Match { -REGISTER_FACTORY(PatternTemplateMatchPredicateFactory, - Router::PathMatchPredicateFactory); +REGISTER_FACTORY(PatternTemplateMatchPredicateFactory, Router::PathMatchPredicateFactory); } // namespace Match } // namespace PatternTemplate } // namespace Extensions diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 69f04e764b0ed..b8007789d8fdc 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -4,10 +4,9 @@ #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" #include "envoy/router/path_match_policy.h" -#include "source/extensions/path/match/pattern_template/pattern_template_match.h" - #include "source/common/protobuf/message_validator_impl.h" #include "source/common/protobuf/utility.h" +#include "source/extensions/path/match/pattern_template/pattern_template_match.h" namespace Envoy { namespace Extensions { @@ -18,10 +17,9 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa public: Router::PathMatchPredicateSharedPtr createPathMatchPredicate(const Protobuf::Message& config) override { - auto path_match_config = - MessageUtil::downcastAndValidate( - config, ProtobufMessage::getStrictValidationVisitor()); + auto path_match_config = MessageUtil::downcastAndValidate< + const envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig&>( + config, ProtobufMessage::getStrictValidationVisitor()); if (!PatternTemplate::isValidMatchPattern(path_match_config.path_template()).ok()) { throw EnvoyException(fmt::format("path_match_policy.path_template {} is invalid", @@ -31,10 +29,13 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa } ProtobufTypes::MessagePtr createEmptyConfigProto() override { - return std::make_unique(); + return std::make_unique< + envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig>(); } - std::string name() const override { return "envoy.path.match.pattern_template.pattern_template_match_predicate"; } + std::string name() const override { + return "envoy.path.match.pattern_template.pattern_template_match_predicate"; + } std::string category() const override { return "envoy.path.match"; } }; diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.cc b/source/extensions/path/match/pattern_template/pattern_template_match.cc index 2266c4d8bd76d..30e62bc7dcde5 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.cc +++ b/source/extensions/path/match/pattern_template/pattern_template_match.cc @@ -19,13 +19,12 @@ namespace Match { bool PatternTemplateMatchPredicate::match(absl::string_view pattern) const { RE2 matching_pattern_regex = RE2(convertURLPatternSyntaxToRegex(path_template_).value()); - return RE2::FullMatch(PatternTemplateInternal::toStringPiece(Http::PathUtil::removeQueryAndFragment(pattern)), - matching_pattern_regex); + return RE2::FullMatch( + PatternTemplateInternal::toStringPiece(Http::PathUtil::removeQueryAndFragment(pattern)), + matching_pattern_regex); } -std::string PatternTemplateMatchPredicate::pattern() const { - return path_template_; -} +std::string PatternTemplateMatchPredicate::pattern() const { return path_template_; } } // namespace Match } // namespace PatternTemplate diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index 834e6d747256a..b9edb414e0bb9 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -23,7 +23,7 @@ class PatternTemplateMatchPredicate : public Router::PathMatchPredicate { explicit PatternTemplateMatchPredicate( const envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig& config) - : path_template_(config.path_template()){} + : path_template_(config.path_template()) {} bool match(absl::string_view pattern) const override; diff --git a/source/extensions/path/pattern_template_lib/BUILD b/source/extensions/path/pattern_template_lib/BUILD index ef09a3fccb111..f949bfdc87e34 100644 --- a/source/extensions/path/pattern_template_lib/BUILD +++ b/source/extensions/path/pattern_template_lib/BUILD @@ -28,8 +28,6 @@ envoy_cc_library( "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_googlesource_code_re2//:re2", - "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", - "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index a32d4cb882335..6298585c78242 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -3,13 +3,10 @@ #include -#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" -#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" -#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" - #include "envoy/router/path_match_policy.h" #include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h index d8666883a3583..4a982a54611d3 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.h +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.h @@ -7,9 +7,8 @@ #include #include "absl/container/flat_hash_set.h" -#include "absl/status/statusor.h" -#include "absl/types/variant.h" #include "absl/status/status.h" +#include "absl/status/statusor.h" #include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "absl/types/variant.h" diff --git a/source/extensions/path/rewrite/pattern_template/BUILD b/source/extensions/path/rewrite/pattern_template/BUILD index 34f7cd6698aa1..db7cb6830b591 100644 --- a/source/extensions/path/rewrite/pattern_template/BUILD +++ b/source/extensions/path/rewrite/pattern_template/BUILD @@ -2,7 +2,6 @@ load( "//bazel:envoy_build_system.bzl", "envoy_cc_extension", "envoy_cc_library", - "envoy_cc_test", "envoy_extension_package", ) @@ -24,6 +23,7 @@ envoy_cc_library( "//source/common/protobuf", "//source/common/protobuf:utility_lib", "//source/extensions/path/pattern_template_lib", + "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/rewrite/pattern_template/config.cc b/source/extensions/path/rewrite/pattern_template/config.cc index 81293711457d9..a813e1f46567f 100644 --- a/source/extensions/path/rewrite/pattern_template/config.cc +++ b/source/extensions/path/rewrite/pattern_template/config.cc @@ -8,8 +8,7 @@ namespace Extensions { namespace PatternTemplate { namespace Rewrite { -REGISTER_FACTORY(PatternTemplateRewritePredicateFactory, - Router::PathRewritePredicateFactory); +REGISTER_FACTORY(PatternTemplateRewritePredicateFactory, Router::PathRewritePredicateFactory); } // namespace Rewrite } // namespace PatternTemplate } // namespace Extensions diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index f311355e5a9df..9597e3dee81d3 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -15,12 +15,14 @@ class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredica public: Router::PathRewritePredicateSharedPtr createPathRewritePredicate(const Protobuf::Message& rewrite_config) override { - auto path_rewrite_config = + auto path_rewrite_config = MessageUtil::downcastAndValidate( rewrite_config, ProtobufMessage::getStrictValidationVisitor()); - if (!PatternTemplate::isValidPathTemplateRewritePattern(path_rewrite_config.path_template_rewrite()).ok()) { + if (!PatternTemplate::isValidPathTemplateRewritePattern( + path_rewrite_config.path_template_rewrite()) + .ok()) { throw EnvoyException(fmt::format("path_rewrite_policy.path_template_rewrite {} is invalid", path_rewrite_config.path_template_rewrite())); } @@ -28,10 +30,13 @@ class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredica } ProtobufTypes::MessagePtr createEmptyConfigProto() override { - return std::make_unique(); + return std::make_unique< + envoy::extensions::path::rewrite::pattern_template::v3::PatternTemplateRewriteConfig>(); } - std::string name() const override { return "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate";} + std::string name() const override { + return "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"; + } std::string category() const override { return "envoy.path.rewrite"; } }; diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index 188cd54283fa7..665e116914aaf 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -5,10 +5,9 @@ #include #include -#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" - #include "source/common/http/path_utility.h" #include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" #include "absl/status/statusor.h" #include "absl/strings/str_split.h" @@ -26,7 +25,7 @@ namespace Rewrite { #endif absl::Status PatternTemplateRewritePredicate::isValidRewritePattern(std::string match_pattern, - std::string rewrite_pattern) { + std::string rewrite_pattern) { if (!isValidPathTemplateRewritePattern(rewrite_pattern).ok()) { return absl::InvalidArgumentError( @@ -50,15 +49,16 @@ absl::Status PatternTemplateRewritePredicate::isValidRewritePattern(std::string }; absl::StatusOr -PatternTemplateRewritePredicate::rewritePattern(absl::string_view current_pattern, absl::string_view matched_path) const { +PatternTemplateRewritePredicate::rewritePattern(absl::string_view current_pattern, + absl::string_view matched_path) const { absl::StatusOr regex_pattern = convertURLPatternSyntaxToRegex(matched_path); if (!regex_pattern.ok()) { return absl::InvalidArgumentError("Unable to parse url pattern regex"); } std::string regex_pattern_str = *std::move(regex_pattern); - absl::StatusOr rewrite_pattern = - parseRewritePattern(url_rewrite_pattern_, regex_pattern_str); + absl::StatusOr + rewrite_pattern = parseRewritePattern(url_rewrite_pattern_, regex_pattern_str); if (!rewrite_pattern.ok()) { return absl::InvalidArgumentError("Unable to parse url rewrite pattern"); @@ -79,7 +79,8 @@ PatternTemplateRewritePredicate::rewritePattern(absl::string_view current_patter absl::StatusOr PatternTemplateRewritePredicate::rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern) const { + const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern) + const { RE2 regex = RE2(PatternTemplateInternal::toStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); @@ -88,8 +89,8 @@ absl::StatusOr PatternTemplateRewritePredicate::rewriteURLTemplateP // First capture is the whole matched regex pattern. int capture_num = regex.NumberOfCapturingGroups() + 1; std::vector captures(capture_num); - if (!regex.Match(PatternTemplateInternal::toStringPiece(url), /*startpos=*/0, /*endpos=*/url.size(), RE2::ANCHOR_BOTH, - captures.data(), captures.size())) { + if (!regex.Match(PatternTemplateInternal::toStringPiece(url), /*startpos=*/0, + /*endpos=*/url.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { return absl::InvalidArgumentError("Pattern not match"); } diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 642c875d806c8..a575078801b6b 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -3,17 +3,16 @@ #include -#include "envoy/router/path_rewrite_policy.h" - -#include "source/extensions/path/pattern_template_lib/pattern_template.h" -#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" -#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" - #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" +#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" +#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" +#include "envoy/router/path_rewrite_policy.h" #include "source/common/protobuf/message_validator_impl.h" #include "source/common/protobuf/utility.h" +#include "source/extensions/path/pattern_template_lib/pattern_template.h" + #include "absl/status/statusor.h" #include "absl/strings/string_view.h" @@ -22,7 +21,8 @@ namespace Extensions { namespace PatternTemplate { namespace Rewrite { -const absl::string_view NAME = "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"; +const absl::string_view NAME = + "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"; class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { public: @@ -38,7 +38,7 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { static absl::Status isValidRewritePattern(std::string match_pattern, std::string rewrite_pattern); - absl::string_view name() const override { return NAME;} + absl::string_view name() const override { return NAME; } private: // Returns the rewritten URL path based on the given parsed rewrite pattern. diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index 23010bb8b74eb..1dfd28645ffa3 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -8790,8 +8790,7 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsDisabledWhenNotSpecifiedInRouteAct TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); Http::TestRequestHeaderMapImpl headers = genRedirectHeaders("idle.lyft.com", "/regex", true, false); - const auto& pattern_template_policy = - config.route(headers, 0)->routeEntry()->pathMatchPolicy(); + const auto& pattern_template_policy = config.route(headers, 0)->routeEntry()->pathMatchPolicy(); EXPECT_FALSE(pattern_template_policy.enabled()); } diff --git a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc index a5a19c4c70c61..70019f858b166 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc @@ -369,9 +369,7 @@ class GenPatternRegexWithMatch : public testing::TestWithParam> const varValues() { - return GetParam().captures; - } + std::vector> const varValues() { return GetParam().captures; } }; INSTANTIATE_TEST_SUITE_P( diff --git a/tools/extensions/extensions_schema.yaml b/tools/extensions/extensions_schema.yaml index 8b40fcdde9f9b..ebe36a39cdd95 100644 --- a/tools/extensions/extensions_schema.yaml +++ b/tools/extensions/extensions_schema.yaml @@ -108,6 +108,8 @@ categories: - envoy.matching.http.input - envoy.matching.network.input - envoy.matching.network.custom_matchers +- envoy.path.rewrite +- envoy.path.match status_values: - name: stable From 1e6ca9d66ea2c4030b5f2f9cf3dab99589ef77d4 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 19:26:26 +0000 Subject: [PATCH 109/238] Changed extensions_metadata.yaml Signed-off-by: silverstar195 --- source/extensions/extensions_metadata.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index 9ba210d6608ba..8fda5a33ed86d 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -746,12 +746,12 @@ envoy.matching.matchers.ip: status: stable type_urls: - envoy.extensions.matching.input_matchers.ip.v3.Ip -envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig: +envoy.path.match.pattern_template.pattern_template_match_predicate: categories: - envoy.path.match security_posture: robust_to_untrusted_downstream_and_upstream status: stable -envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig: +envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate: categories: - envoy.path.rewrite security_posture: robust_to_untrusted_downstream_and_upstream From 6eacf89d4f14ae8983b7d88864dfe10d74955d52 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 20:05:10 +0000 Subject: [PATCH 110/238] Format and refactor Signed-off-by: silverstar195 --- envoy/router/path_match_policy.h | 2 +- envoy/router/path_rewrite_policy.h | 2 +- source/common/router/config_impl.cc | 26 ++++++++++++++----- source/common/router/config_impl.h | 4 ++- .../path/match/pattern_template/config.h | 7 ++--- .../path/rewrite/pattern_template/config.h | 7 ++--- 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index 962a3b52df8c7..b94eea9583089 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -51,7 +51,7 @@ class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { * @param config contains the proto stored in TypedExtensionConfig.typed_config for the predicate. * @return an PathMatchPredicateSharedPtr. */ - virtual PathMatchPredicateSharedPtr + virtual absl::StatusOr createPathMatchPredicate(const Protobuf::Message& config) PURE; /** diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index f8689ea564ada..1fa67a338b336 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -50,7 +50,7 @@ class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { public: virtual ~PathRewritePredicateFactory() override = default; - virtual PathRewritePredicateSharedPtr + virtual absl::StatusOr createPathRewritePredicate(const Protobuf::Message& rewrite_config) PURE; virtual ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index bee09e58f4cdc..9f6ee76a1702f 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -359,8 +359,8 @@ PathRewritePolicyImpl::PathRewritePolicyImpl() : enabled_(false){}; PathRewritePolicyImpl::PathRewritePolicyImpl( const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string path) - : path_(path), enabled_(true) { + ProtobufMessage::ValidationVisitor& validator, std::string route_url) + : route_url_(route_url), enabled_(true) { predicate_factory_ = &Envoy::Config::Utility::getAndCheckFactory(typed_config); @@ -373,7 +373,14 @@ PathRewritePolicyImpl::PathRewritePolicyImpl( // Validate config format and inputs when creating factory. // As the validation and create are nearly 1:1 store for later use. // Predicate is only ever created once. - predicate_ = predicate_factory_->createPathRewritePredicate(*predicate_config_); + absl::StatusOr config_or_error = + predicate_factory_->createPathRewritePredicate(*predicate_config_); + + if (!config_or_error.ok()) { + throw EnvoyException(std::string(config_or_error.status().message())); + } + + predicate_ = config_or_error.value(); } PathRewritePredicateSharedPtr PathRewritePolicyImpl::predicate() const { return predicate_; } @@ -382,8 +389,8 @@ PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; PathMatchPolicyImpl::PathMatchPolicyImpl( const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string route_url) - : route_url_(route_url), enabled_(true) { + ProtobufMessage::ValidationVisitor& validator, std::string path) + : path_(path), enabled_(true) { predicate_factory_ = &Envoy::Config::Utility::getAndCheckFactory(typed_config); @@ -396,7 +403,14 @@ PathMatchPolicyImpl::PathMatchPolicyImpl( // Validate config format and inputs when creating factory. // As the validation and create are nearly 1:1 store for later use. // Predicate is only ever created once. - predicate_ = predicate_factory_->createPathMatchPredicate(*predicate_config_); + absl::StatusOr config_or_error = + predicate_factory_->createPathMatchPredicate(*predicate_config_); + + if (!config_or_error.ok()) { + throw EnvoyException(std::string(config_or_error.status().message())); + } + + predicate_ = config_or_error.value(); } PathMatchPredicateSharedPtr PathMatchPolicyImpl::predicate() const { return predicate_; } diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 5b87080b5fc34..c5c18cd295975 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -464,7 +464,7 @@ class RouteTracingImpl : public RouteTracing { class PathMatchPolicyImpl : public PathMatchPolicy { public: PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string route_url); + ProtobufMessage::ValidationVisitor& validator, std::string path); // Default constructor that disables internal redirect. PathMatchPolicyImpl(); @@ -499,6 +499,8 @@ class PathRewritePolicyImpl : public PathRewritePolicy { PathRewritePredicateSharedPtr predicate() const override; + std::string routeUrl_() const { return route_url_; } + private: const std::string route_url_; const bool enabled_; diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index b8007789d8fdc..1815c4e807040 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -15,16 +15,17 @@ namespace Match { class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFactory { public: - Router::PathMatchPredicateSharedPtr + absl::StatusOr createPathMatchPredicate(const Protobuf::Message& config) override { auto path_match_config = MessageUtil::downcastAndValidate< const envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig&>( config, ProtobufMessage::getStrictValidationVisitor()); if (!PatternTemplate::isValidMatchPattern(path_match_config.path_template()).ok()) { - throw EnvoyException(fmt::format("path_match_policy.path_template {} is invalid", - path_match_config.path_template())); + return absl::InvalidArgumentError(fmt::format("path_match_policy.path_template {} is invalid", + path_match_config.path_template())); } + return std::make_shared(path_match_config); } diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 9597e3dee81d3..91b325ed54f07 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -13,7 +13,7 @@ namespace Rewrite { class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredicateFactory { public: - Router::PathRewritePredicateSharedPtr + absl::StatusOr createPathRewritePredicate(const Protobuf::Message& rewrite_config) override { auto path_rewrite_config = MessageUtil::downcastAndValidate(path_rewrite_config); } From f101cbf3ae95028cef7b3917bd9c072ae219c351 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 20:24:13 +0000 Subject: [PATCH 111/238] Format and refactor Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 1 - tools/extensions/extensions_schema.yaml | 1 - 2 files changed, 2 deletions(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 9f6ee76a1702f..121c8297aa1e5 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1262,7 +1262,6 @@ RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::Route route return PathRewritePolicyImpl(); } -// TODO PathMatchPolicyImpl RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { diff --git a/tools/extensions/extensions_schema.yaml b/tools/extensions/extensions_schema.yaml index ebe36a39cdd95..d2606dcb0ca25 100644 --- a/tools/extensions/extensions_schema.yaml +++ b/tools/extensions/extensions_schema.yaml @@ -74,7 +74,6 @@ categories: - envoy.http.header_validators - envoy.http.stateful_header_formatters - envoy.internal_redirect_predicates -- envoy.pattern_template - envoy.io_socket - envoy.http.original_ip_detection - envoy.matching.common_inputs From 598e5c909c223b73539a6b5c65246c0c543e517c Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 20:28:50 +0000 Subject: [PATCH 112/238] Added to exclude Signed-off-by: silverstar195 --- tools/code_format/config.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index 15006d31f87e6..91a6f8a744524 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -295,3 +295,5 @@ visibility_excludes: - source/extensions/transport_sockets/common/BUILD - source/extensions/udp_packet_writer/default/BUILD - source/extensions/udp_packet_writer/gso/BUILD +- source/extensions/path/match/pattern_template/BUILD +- source/extensions/path/rewrite/pattern_template/BUILD From 4c00b5245db38db083c2ec3e4193345bd3b0397a Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 20:35:15 +0000 Subject: [PATCH 113/238] Added to exclude Signed-off-by: silverstar195 --- tools/code_format/config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index 91a6f8a744524..7c68ab2d1cc4c 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -297,3 +297,4 @@ visibility_excludes: - source/extensions/udp_packet_writer/gso/BUILD - source/extensions/path/match/pattern_template/BUILD - source/extensions/path/rewrite/pattern_template/BUILD +- source/extensions/path/pattern_template_lib/BUILD \ No newline at end of file From 561d69613854cfc54e5d299da50e5da297778586 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 20:39:46 +0000 Subject: [PATCH 114/238] Added to exclude Signed-off-by: silverstar195 --- tools/code_format/config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index 7c68ab2d1cc4c..dfcd313d99010 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -297,4 +297,4 @@ visibility_excludes: - source/extensions/udp_packet_writer/gso/BUILD - source/extensions/path/match/pattern_template/BUILD - source/extensions/path/rewrite/pattern_template/BUILD -- source/extensions/path/pattern_template_lib/BUILD \ No newline at end of file +- source/extensions/path/pattern_template_lib/BUILD From 9226da3700f0e42340fac983ea807039cab192a1 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 20:43:12 +0000 Subject: [PATCH 115/238] Add extension label Signed-off-by: silverstar195 --- .../path/match/pattern_template/v3/pattern_template_match.proto | 1 + .../rewrite/pattern_template/v3/pattern_template_rewrite.proto | 1 + 2 files changed, 2 insertions(+) diff --git a/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto index 24e6d96c44c98..364e8ea64c1be 100644 --- a/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto @@ -38,6 +38,7 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // * ``/videos/{file}`` would match ``/videos/1080p5000_00001.m4s`` // // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` +// [#extension: envoy.path.match.pattern_template.pattern_template_match_predicate] // [#not-implemented-hide:] message PatternTemplateMatchConfig { string path_template = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; diff --git a/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto index 2d5cebe876120..21a305541be2a 100644 --- a/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto @@ -53,6 +53,7 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` // into ``/en-us/hls/en_193913.vtt``. // [#not-implemented-hide:] +// [#extension: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate] message PatternTemplateRewriteConfig { string path_template_rewrite = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; } From 63e8be8c6b47f7091bd9b015acf2cd38243b142a Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 4 Aug 2022 21:21:58 +0000 Subject: [PATCH 116/238] Changed doc strings and name Signed-off-by: silverstar195 --- envoy/router/path_rewrite_policy.h | 4 ++-- source/common/router/config_impl.cc | 3 +-- source/common/router/config_impl.h | 4 ++-- .../path/rewrite/pattern_template/pattern_template_rewrite.cc | 2 +- .../path/rewrite/pattern_template/pattern_template_rewrite.h | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index 1fa67a338b336..de75eb4183dcf 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -25,9 +25,9 @@ class PathRewritePredicate : Logger::Loggable { * * @param url current url of route * @param matched_path pattern to rewrite the url to - * @return the name of the rewrite pattern current predicate. + * @return the rewritten url. */ - virtual absl::StatusOr rewritePattern(absl::string_view url, + virtual absl::StatusOr rewriteUrl(absl::string_view url, absl::string_view matched_path) const PURE; /** diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 121c8297aa1e5..63f46d0601526 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1057,10 +1057,9 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa } } - // complete pattern rewrite if (path_rewrite_policy_.enabled()) { auto just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); - return *std::move(path_rewrite_policy_.predicate()->rewritePattern(just_path, matched_path)); + return *std::move(path_rewrite_policy_.predicate()->rewriteUrl(just_path, matched_path)); } // There are no rewrites configured. diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index c5c18cd295975..b0e8e66c736d9 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -484,8 +484,8 @@ class PathMatchPolicyImpl : public PathMatchPolicy { }; /** - * Implementation of InternalRedirectPolicy that reads from the proto - * InternalRedirectPolicy of the RouteAction. + * Implementation of PathRewritePolicyImpl that reads from the proto + * PathRewritePolicyImpl of the RouteAction. */ class PathRewritePolicyImpl : public PathRewritePolicy { public: diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index 665e116914aaf..fd8f1dac4db00 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -49,7 +49,7 @@ absl::Status PatternTemplateRewritePredicate::isValidRewritePattern(std::string }; absl::StatusOr -PatternTemplateRewritePredicate::rewritePattern(absl::string_view current_pattern, +PatternTemplateRewritePredicate::rewriteUrl(absl::string_view current_pattern, absl::string_view matched_path) const { absl::StatusOr regex_pattern = convertURLPatternSyntaxToRegex(matched_path); if (!regex_pattern.ok()) { diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index a575078801b6b..cd7fd7be53355 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -33,7 +33,7 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { std::string pattern() const override { return url_rewrite_pattern_; } - absl::StatusOr rewritePattern(absl::string_view current_pattern, + absl::StatusOr rewriteUrl(absl::string_view current_pattern, absl::string_view matched_path) const override; static absl::Status isValidRewritePattern(std::string match_pattern, std::string rewrite_pattern); From 655d05422f38f55b2ef288f0c0b44312a8cd1819 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 5 Aug 2022 16:14:27 +0000 Subject: [PATCH 117/238] Changed doc strings and name Signed-off-by: silverstar195 --- source/extensions/extensions_metadata.yaml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index 8fda5a33ed86d..129b63da7ab82 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -749,13 +749,19 @@ envoy.matching.matchers.ip: envoy.path.match.pattern_template.pattern_template_match_predicate: categories: - envoy.path.match - security_posture: robust_to_untrusted_downstream_and_upstream + security_posture: unknown status: stable + undocumented: true + type_urls: + - envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate: categories: - envoy.path.rewrite - security_posture: robust_to_untrusted_downstream_and_upstream + security_posture: unknown status: stable + undocumented: true + type_urls: + - envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig envoy.quic.proof_source.filter_chain: categories: - envoy.quic.proof_source From 35f452dd7cf2567d5a3bbfc183e72df9e09661f2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 5 Aug 2022 18:30:49 +0000 Subject: [PATCH 118/238] Spacing Signed-off-by: silverstar195 --- envoy/router/path_rewrite_policy.h | 2 +- .../path/rewrite/pattern_template/pattern_template_rewrite.cc | 2 +- .../path/rewrite/pattern_template/pattern_template_rewrite.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index de75eb4183dcf..fa79acece88f8 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -28,7 +28,7 @@ class PathRewritePredicate : Logger::Loggable { * @return the rewritten url. */ virtual absl::StatusOr rewriteUrl(absl::string_view url, - absl::string_view matched_path) const PURE; + absl::string_view matched_path) const PURE; /** * @return the rewrite pattern of the predicate. diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index fd8f1dac4db00..5c3740a28a68b 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -50,7 +50,7 @@ absl::Status PatternTemplateRewritePredicate::isValidRewritePattern(std::string absl::StatusOr PatternTemplateRewritePredicate::rewriteUrl(absl::string_view current_pattern, - absl::string_view matched_path) const { + absl::string_view matched_path) const { absl::StatusOr regex_pattern = convertURLPatternSyntaxToRegex(matched_path); if (!regex_pattern.ok()) { return absl::InvalidArgumentError("Unable to parse url pattern regex"); diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index cd7fd7be53355..67d02fbd46a5a 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -34,7 +34,7 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { std::string pattern() const override { return url_rewrite_pattern_; } absl::StatusOr rewriteUrl(absl::string_view current_pattern, - absl::string_view matched_path) const override; + absl::string_view matched_path) const override; static absl::Status isValidRewritePattern(std::string match_pattern, std::string rewrite_pattern); From d993ae44e6c79332a311716edfb0bb9dbebfcf51 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 5 Aug 2022 18:42:11 +0000 Subject: [PATCH 119/238] Ordering Signed-off-by: silverstar195 --- tools/code_format/check_format.py | 14 +++++++++----- tools/code_format/config.yaml | 2 ++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index 38cfce07b55ed..b73f8d36886d1 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -7,17 +7,20 @@ import os import pathlib import re -import subprocess +import shutil import stat +import subprocess import sys import traceback -import shutil -from functools import cached_property -from typing import Callable, Dict, List, Pattern, Tuple - # The way this script is currently used (ie no bazel) it relies on system deps. # As `pyyaml` is present in `envoy-build-ubuntu` it should be safe to use here. import yaml +from functools import cached_property +from typing import Callable +from typing import Dict +from typing import List +from typing import Pattern +from typing import Tuple import paths @@ -1079,6 +1082,7 @@ def check_visibility(error_messages): try: output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() if output: + error_messages.append(command) error_messages.append( "This change appears to add visibility rules. Please get senior maintainer " "approval to add an exemption to check_visibility tools/code_format/check_format.py" diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index dfcd313d99010..18a27e204df7d 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -298,3 +298,5 @@ visibility_excludes: - source/extensions/path/match/pattern_template/BUILD - source/extensions/path/rewrite/pattern_template/BUILD - source/extensions/path/pattern_template_lib/BUILD +- source/extensions/path/pattern_template_lib/proto/BUILD +- test/extensions/path/pattern_template_lib/BUILD From b0cd6b2b2dc2c338fea6b94646686ea32994c40c Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 5 Aug 2022 18:46:14 +0000 Subject: [PATCH 120/238] Add output Signed-off-by: silverstar195 --- tools/code_format/check_format.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index b73f8d36886d1..2e913d17665af 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -1082,6 +1082,7 @@ def check_visibility(error_messages): try: output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() if output: + error_messages.append(output) error_messages.append(command) error_messages.append( "This change appears to add visibility rules. Please get senior maintainer " From d897f248ae67e1593eb974c971393ad61752a00a Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 5 Aug 2022 19:03:17 +0000 Subject: [PATCH 121/238] Edit script Signed-off-by: silverstar195 --- tools/code_format/check_format.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index 2e913d17665af..dde0e0da93a86 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -1077,8 +1077,8 @@ def normalize_path(path): def check_visibility(error_messages): command = ( - "git diff $(tools/git/last_github_commit.sh) -- source/extensions/* %s |grep '+.*visibility ='" - % [f"':(exclude){c}' " for c in format_checker.config["visibility_excludes"]]) + "git diff $(tools/git/last_github_commit.sh) -- source/extensions/* %s | grep '+.*visibility ='" + % ''.join([f"':(exclude){c}' " for c in format_checker.config["visibility_excludes"]])) try: output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() if output: From bb2c540faa6b11796e0059c4d41e423908fc7102 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 5 Aug 2022 19:07:51 +0000 Subject: [PATCH 122/238] Remove unneeded files Signed-off-by: silverstar195 --- tools/code_format/config.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index 18a27e204df7d..dfcd313d99010 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -298,5 +298,3 @@ visibility_excludes: - source/extensions/path/match/pattern_template/BUILD - source/extensions/path/rewrite/pattern_template/BUILD - source/extensions/path/pattern_template_lib/BUILD -- source/extensions/path/pattern_template_lib/proto/BUILD -- test/extensions/path/pattern_template_lib/BUILD From f37852d39e7fafcc6270759497ab351384fdbee3 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 5 Aug 2022 19:57:29 +0000 Subject: [PATCH 123/238] Add stubs Signed-off-by: silverstar195 --- source/common/router/delegating_route_impl.cc | 8 ++++++++ source/common/router/delegating_route_impl.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/source/common/router/delegating_route_impl.cc b/source/common/router/delegating_route_impl.cc index 3b03674f64b15..6864272261568 100644 --- a/source/common/router/delegating_route_impl.cc +++ b/source/common/router/delegating_route_impl.cc @@ -76,6 +76,14 @@ const RetryPolicy& DelegatingRouteEntry::retryPolicy() const { return base_route_->routeEntry()->retryPolicy(); } +const PathMatchPolicy& DelegatingRouteEntry::pathMatchPolicy() const { + return base_route_->routeEntry()->pathMatchPolicy(); +} + +const PathRewritePolicy& DelegatingRouteEntry::pathRewritePolicy() const { + return base_route_->routeEntry()->pathRewritePolicy(); +} + const InternalRedirectPolicy& DelegatingRouteEntry::internalRedirectPolicy() const { return base_route_->routeEntry()->internalRedirectPolicy(); } diff --git a/source/common/router/delegating_route_impl.h b/source/common/router/delegating_route_impl.h index 3ec429d4047b0..2fb75d7d3ba8f 100644 --- a/source/common/router/delegating_route_impl.h +++ b/source/common/router/delegating_route_impl.h @@ -84,6 +84,8 @@ class DelegatingRouteEntry : public Router::RouteEntry { Upstream::ResourcePriority priority() const override; const RateLimitPolicy& rateLimitPolicy() const override; const RetryPolicy& retryPolicy() const override; + const Router::PathMatchPolicy& pathMatchPolicy() const override; + const Router::PathRewritePolicy& pathRewritePolicy() const override; const InternalRedirectPolicy& internalRedirectPolicy() const override; uint32_t retryShadowBufferLimit() const override; const std::vector& shadowPolicies() const override; From d44dbb8d8194fb72f0e7a37361b8abf99947c55a Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 8 Aug 2022 14:54:56 +0000 Subject: [PATCH 124/238] Addressed some comments Signed-off-by: silverstar195 --- envoy/router/path_match_policy.h | 2 +- envoy/router/path_rewrite_policy.h | 4 ++-- envoy/router/router.h | 12 ++++-------- source/common/router/config_impl.cc | 9 +++------ .../extensions/path/match/pattern_template/config.h | 2 +- .../match/pattern_template/pattern_template_match.h | 5 +---- .../path/pattern_template_lib/pattern_template.h | 5 +---- .../pattern_template_lib/pattern_template_internal.h | 5 +---- .../path/rewrite/pattern_template/config.h | 1 - .../pattern_template/pattern_template_rewrite.h | 5 +---- 10 files changed, 15 insertions(+), 35 deletions(-) diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index b94eea9583089..baf3892e6d049 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -11,7 +11,7 @@ namespace Envoy { namespace Router { /** - * Used to decide if path match is needed based on the target route. + * Decides if the target route path is matching the provided pattern. * Subclassing Logger::Loggable so that implementations can log details. */ class PathMatchPredicate : Logger::Loggable { diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index fa79acece88f8..46d267392f290 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -11,7 +11,7 @@ namespace Envoy { namespace Router { /** - * Used to decide if pattern template rewrite is needed based on the target route. + * Decides if the target route path is matching the provided pattern. * Subclassing Logger::Loggable so that implementations can log details. */ class PathRewritePredicate : Logger::Loggable { @@ -63,7 +63,7 @@ class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { /** * @return the category of the rewrite pattern predicate to be created. */ - virtual std::string category() const override PURE; + std::string category() const override { return "envoy.path.rewrite"; } }; } // namespace Router diff --git a/envoy/router/router.h b/envoy/router/router.h index 5904405545e8d..8e8a670de40d3 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -317,10 +317,8 @@ class PathMatchPolicy { virtual bool enabled() const PURE; /** - * Creates the target route predicates. This should really be called only once for each upstream - * match. Creating the predicate lazily to avoid wasting CPU cycles on non-matching - * responses, which should be the most common case. - * @return a newly constructed PathMatchPredicate instance. + * Returns the stored target route predicate. + * @return a PathMatchPredicate instance. */ virtual PathMatchPredicateSharedPtr predicate() const PURE; }; @@ -338,10 +336,8 @@ class PathRewritePolicy { virtual bool enabled() const PURE; /** - * Creates the target route predicates. This should really be called only once for each upstream - * rewrite. Creating the predicate lazily to avoid wasting CPU cycles on non-rewrite - * responses, which should be the most common case. - * @return a newly constructed PathRewritePredicate instances. + * Returns the stored target route predicate. + * @return a PathRewritePredicate instance. */ virtual PathRewritePredicateSharedPtr predicate() const PURE; }; diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 63f46d0601526..c430c9b61d286 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -368,7 +368,6 @@ PathRewritePolicyImpl::PathRewritePolicyImpl( predicate_config_ = Envoy::Config::Utility::translateAnyToFactoryConfig( typed_config.typed_config(), validator, *predicate_factory_); - ASSERT(predicate_config_); // config translation failed // Validate config format and inputs when creating factory. // As the validation and create are nearly 1:1 store for later use. @@ -398,7 +397,6 @@ PathMatchPolicyImpl::PathMatchPolicyImpl( predicate_config_ = Envoy::Config::Utility::translateAnyToFactoryConfig( typed_config.typed_config(), validator, *predicate_factory_); - ASSERT(predicate_config_); // config translation failed // Validate config format and inputs when creating factory. // As the validation and create are nearly 1:1 store for later use. @@ -743,10 +741,8 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, Extensions::PatternTemplate::Rewrite::NAME, Extensions::PatternTemplate::Match::NAME)); } - } - // Validation between extensions as they share rewrite pattern variables. - if (path_rewrite_policy_.enabled() && path_match_policy_.enabled()) { + // Validation between extensions as they share rewrite pattern variables. if (!Extensions::PatternTemplate::isValidSharedVariableSet( path_rewrite_policy_.predicate()->pattern(), path_match_policy_.predicate()->pattern()) .ok()) { @@ -1513,7 +1509,8 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( const OptionalHttpFilters& optional_http_filters, Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) - : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator) {} + : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), + match_pattern_(path_match_policy_.predicate()->pattern()) {}; void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 1815c4e807040..46d34d1ea74c0 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -37,7 +37,7 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa std::string name() const override { return "envoy.path.match.pattern_template.pattern_template_match_predicate"; } - std::string category() const override { return "envoy.path.match"; } + }; } // namespace Match diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index b9edb414e0bb9..748cc44cc3841 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -1,5 +1,4 @@ -#ifndef SOURCE_EXTENSIONS_PATH_MATCH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H -#define SOURCE_EXTENSIONS_PATH_MATCH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H +#pragma once #include @@ -38,5 +37,3 @@ class PatternTemplateMatchPredicate : public Router::PathMatchPredicate { } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy - -#endif // SOURCE_EXTENSIONS_PATH_MATCH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_MATCH_H diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 6298585c78242..7b1df3b89f5fc 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -1,5 +1,4 @@ -#ifndef SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H -#define SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H +#pragma once #include @@ -57,5 +56,3 @@ absl::StatusOr rewriteURLTemplatePattern( } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy - -#endif // SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h index 4a982a54611d3..06f91ab3d9d8b 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.h +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.h @@ -1,5 +1,4 @@ -#ifndef SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H -#define SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H +#pragma once #include #include @@ -93,5 +92,3 @@ inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.dat } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy - -#endif // SOURCE_EXTENSIONS_PATH_PATTERN_TEMPLATE_MATCHING_PATTERN_TEMPLATE_INTERNAL_H diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 91b325ed54f07..9861ff0bf0fec 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -38,7 +38,6 @@ class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredica std::string name() const override { return "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"; } - std::string category() const override { return "envoy.path.rewrite"; } }; } // namespace Rewrite diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 67d02fbd46a5a..b951ec9b058e6 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -1,5 +1,4 @@ -#ifndef SOURCE_EXTENSIONS_PATH_REWRITE_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H -#define SOURCE_EXTENSIONS_PATH_REWRITE_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H +#pragma once #include @@ -55,5 +54,3 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy - -#endif // SOURCE_EXTENSIONS_PATH_REWRITE_PATTERN_TEMPLATE_PATTERN_TEMPLATE_H From 1020d4a2e49ef1e739299ec8744b050b96404946 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 8 Aug 2022 15:04:11 +0000 Subject: [PATCH 125/238] Handled error path Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index c430c9b61d286..70e214f78672e 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1055,7 +1055,14 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa if (path_rewrite_policy_.enabled()) { auto just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); - return *std::move(path_rewrite_policy_.predicate()->rewriteUrl(just_path, matched_path)); + + absl::StatusOr new_path = path_rewrite_policy_.predicate()->rewriteUrl(just_path, matched_path); + + // if rewrite fails return old path. + if(!new_path.ok()) { + return *std::move(just_path); + } + return *std::move(new_path); } // There are no rewrites configured. From 5821840be50bd6c8b3023e74ff584aadccf38b15 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 8 Aug 2022 15:29:51 +0000 Subject: [PATCH 126/238] Format Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 10 ++++++---- source/extensions/path/match/pattern_template/config.h | 1 - 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 70e214f78672e..5233d1c1c2226 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1056,12 +1056,14 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa if (path_rewrite_policy_.enabled()) { auto just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); - absl::StatusOr new_path = path_rewrite_policy_.predicate()->rewriteUrl(just_path, matched_path); + absl::StatusOr new_path = + path_rewrite_policy_.predicate()->rewriteUrl(just_path, matched_path); // if rewrite fails return old path. - if(!new_path.ok()) { - return *std::move(just_path); + if (!new_path.ok()) { + return std::string(headers.getPathValue()); } + return *std::move(new_path); } @@ -1517,7 +1519,7 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - match_pattern_(path_match_policy_.predicate()->pattern()) {}; + match_pattern_(path_match_policy_.predicate()->pattern()){}; void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 46d34d1ea74c0..fca213be90733 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -37,7 +37,6 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa std::string name() const override { return "envoy.path.match.pattern_template.pattern_template_match_predicate"; } - }; } // namespace Match From 662fa3533576589b199540f345d8fab1d5c40f18 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 8 Aug 2022 16:05:58 +0000 Subject: [PATCH 127/238] Format Signed-off-by: silverstar195 --- source/common/router/config_impl.h | 2 +- .../pattern_template_internal.cc | 3 ++ .../pattern_template_internal_test.cc | 46 +++++++++---------- .../pattern_template_test.cc | 2 +- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index b0e8e66c736d9..0b4a85f32399f 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -499,7 +499,7 @@ class PathRewritePolicyImpl : public PathRewritePolicy { PathRewritePredicateSharedPtr predicate() const override; - std::string routeUrl_() const { return route_url_; } + std::string routeUrl() const { return route_url_; } private: const std::string route_url_; diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc index 756964335f344..181b5c88f89aa 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc @@ -79,6 +79,7 @@ std::string toString(const Operator val) { case Operator::KTextGlob: return "**"; } + return ""; } std::string toString(const Variable val) { @@ -88,6 +89,7 @@ std::string toString(const Variable val) { return absl::StrCat("{", val.var_name, "=", absl::StrJoin(val.var_match, "/", ToStringFormatter()), "}"); + return ""; } template std::string ToStringVisitor::operator()(const T& val) const { @@ -363,6 +365,7 @@ std::string toRegexPattern(Operator pattern) { case Operator::KTextGlob: // "**" return *kTextGlobRegex; } + return ""; } std::string toRegexPattern(const Variable& pattern) { diff --git a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc index 70019f858b166..d98809bbb2559 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc @@ -33,20 +33,20 @@ TEST(InternalParsing, ParsedUrlDebugString) { { "abc", "def", - Operator::kPathGlob, - Variable("var", {Operator::kPathGlob, "ghi", Operator::kTextGlob}), + Operator::KPathGlob, + Variable("var", {Operator::KPathGlob, "ghi", Operator::KTextGlob}), }, ".test", {}, }; - EXPECT_EQ(patt1.DebugString(), "/abc/def/*/{var=*/ghi/**}.test"); + EXPECT_EQ(patt1.debugString(), "/abc/def/*/{var=*/ghi/**}.test"); ParsedUrlPattern patt2 = {{ Variable("var", {}), }, "", {}}; - EXPECT_EQ(patt2.DebugString(), "/{var}"); + EXPECT_EQ(patt2.debugString(), "/{var}"); } TEST(InternalParsing, isValidLiteralWorks) { @@ -108,7 +108,7 @@ TEST(InternalParsing, ConsumeTextGlob) { absl::StatusOr> result = consumeOperator(pattern); ASSERT_OK(result); - EXPECT_EQ(result->parsed_value, Operator::kTextGlob); + EXPECT_EQ(result->parsed_value, Operator::KTextGlob); EXPECT_EQ(result->unconsumed_pattern, "*abc/123"); } @@ -118,7 +118,7 @@ TEST(InternalParsing, ConsumePathGlob) { absl::StatusOr> result = consumeOperator(pattern); ASSERT_OK(result); - EXPECT_EQ(result->parsed_value, Operator::kPathGlob); + EXPECT_EQ(result->parsed_value, Operator::KPathGlob); EXPECT_EQ(result->unconsumed_pattern, "/123"); } @@ -135,7 +135,7 @@ TEST_P(ConsumeVariableSuccess, ConsumeVariableSuccessTest) { absl::StatusOr> result = consumeVariable(pattern); ASSERT_OK(result); - EXPECT_EQ(result->parsed_value.DebugString(), pattern); + EXPECT_EQ(result->parsed_value.debugString(), pattern); EXPECT_TRUE(result->unconsumed_pattern.empty()); } @@ -176,7 +176,7 @@ TEST_P(ParseURLPatternSyntaxSuccess, ParseURLPatternSyntaxSuccessTest) { absl::StatusOr parsed_patt = parseURLPatternSyntax(pattern); ASSERT_OK(parsed_patt); - EXPECT_EQ(parsed_patt->DebugString(), pattern); + EXPECT_EQ(parsed_patt->debugString(), pattern); } class ParseURLPatternSyntaxFailure : public testing::TestWithParam {}; @@ -261,31 +261,31 @@ TEST(InternalRegexGen, DollarSignMatchesIfself) { } TEST(InternalRegexGen, OperatorRegexPattern) { - EXPECT_EQ(toRegexPattern(Operator::kPathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); - EXPECT_EQ(toRegexPattern(Operator::kTextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); + EXPECT_EQ(toRegexPattern(Operator::KPathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); + EXPECT_EQ(toRegexPattern(Operator::KTextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); } TEST(InternalRegexGen, PathGlobRegex) { - EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kPathGlob))); - EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kPathGlob))); + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::KPathGlob))); + EXPECT_FALSE(RE2::FullMatch("", toRegexPattern(Operator::KPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc/123", toRegexPattern(Operator::KPathGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::KPathGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::KPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::KPathGlob))); } TEST(InternalRegexGen, TextGlobRegex) { - EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::kTextGlob))); - EXPECT_TRUE(RE2::FullMatch("", toRegexPattern(Operator::kTextGlob))); - EXPECT_TRUE(RE2::FullMatch("abc/123", toRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::kTextGlob))); - EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::kTextGlob))); + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::KTextGlob))); + EXPECT_TRUE(RE2::FullMatch("", toRegexPattern(Operator::KTextGlob))); + EXPECT_TRUE(RE2::FullMatch("abc/123", toRegexPattern(Operator::KTextGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::KTextGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::KTextGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::KTextGlob))); } TEST(InternalRegexGen, VariableRegexPattern) { EXPECT_EQ(toRegexPattern(Variable("var1", {})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"); - EXPECT_EQ(toRegexPattern(Variable("var2", {Operator::kPathGlob, "abc", Operator::kTextGlob})), + EXPECT_EQ(toRegexPattern(Variable("var2", {Operator::KPathGlob, "abc", Operator::KTextGlob})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+/abc/" "[a-zA-Z0-9-._~%!$&'()+,;:@/]*)"); } diff --git a/test/extensions/path/pattern_template_lib/pattern_template_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_test.cc index 57b7097fd12df..bfa47233e5b8a 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_test.cc @@ -327,7 +327,7 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { rewriteURLTemplatePattern(matchUrl(), regex.value(), rewrite_proto.value()); ASSERT_OK(rewritten_url); - EXPECT_EQ(rewritten_url.value(), expected_rewritten_url()); + EXPECT_EQ(rewritten_url.value(), expectedRewrittenUrl()); } } // namespace From 697c1456444c538ff8734bd196da8467ce5d83dd Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 8 Aug 2022 18:54:41 +0000 Subject: [PATCH 128/238] Format Signed-off-by: silverstar195 --- envoy/router/path_match_policy.h | 8 +++++--- envoy/router/path_rewrite_policy.h | 10 +++++++--- source/common/router/config_impl.cc | 12 ++++++------ .../pattern_template_lib/pattern_template_test.cc | 8 ++++---- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index baf3892e6d049..6d3f154ee6e75 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -45,19 +45,21 @@ using PathMatchPredicateSharedPtr = std::shared_ptr; */ class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { public: - virtual ~PathMatchPredicateFactory() = default; + ~PathMatchPredicateFactory() override = default; /** - * @param config contains the proto stored in TypedExtensionConfig.typed_config for the predicate. + * @param config contains the proto stored in TypedExtensionConfig for the predicate. * @return an PathMatchPredicateSharedPtr. */ virtual absl::StatusOr createPathMatchPredicate(const Protobuf::Message& config) PURE; + ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; + /** * @return the name of the match pattern predicate to be created. */ - virtual std::string name() const override PURE; + std::string name() const override PURE; /** * @return the category of the match pattern predicate to be created. diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index 46d267392f290..1f0c353cf5b1c 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -48,17 +48,21 @@ using PathRewritePredicateSharedPtr = std::shared_ptr; */ class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { public: - virtual ~PathRewritePredicateFactory() override = default; + ~PathRewritePredicateFactory() override = default; + /** + * @param rewrite_config contains the proto stored in TypedExtensionConfig for the predicate. + * @return an PathRewritePredicateSharedPtr. + */ virtual absl::StatusOr createPathRewritePredicate(const Protobuf::Message& rewrite_config) PURE; - virtual ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; + ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; /** * @return the name of the rewrite pattern predicate to be created. */ - virtual std::string name() const override PURE; + std::string name() const override PURE; /** * @return the category of the rewrite pattern predicate to be created. diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 5233d1c1c2226..7fb3863bfbca0 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1068,7 +1068,7 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa } // There are no rewrites configured. - return absl::optional(); + return {}; } absl::string_view RouteEntryImplBase::processRequestHost(const Http::RequestHeaderMap& headers, @@ -1255,25 +1255,25 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( if (route_config.has_max_internal_redirects()) { *policy_config.mutable_max_internal_redirects() = route_config.max_internal_redirects(); } - return InternalRedirectPolicyImpl(policy_config, validator, current_route_name); + return InternalRedirectPolicyImpl{policy_config, validator, current_route_name}; } PathRewritePolicyImpl RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { if (route.route().has_path_rewrite_policy()) { - return PathRewritePolicyImpl(route.route().path_rewrite_policy(), validator, route.name()); + return PathRewritePolicyImpl{route.route().path_rewrite_policy(), validator, route.name()}; } - return PathRewritePolicyImpl(); + return {}; } PathMatchPolicyImpl RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { if (route.match().has_path_match_policy()) { - return PathMatchPolicyImpl(route.match().path_match_policy(), validator, route.name()); + return PathMatchPolicyImpl{route.match().path_match_policy(), validator, route.name()}; } - return PathMatchPolicyImpl(); + return {}; } DecoratorConstPtr RouteEntryImplBase::parseDecorator(const envoy::config::route::v3::Route& route) { diff --git a/test/extensions/path/pattern_template_lib/pattern_template_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_test.cc index bfa47233e5b8a..7f9fea6c54a0f 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_test.cc @@ -169,7 +169,7 @@ TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { class RewriteUrlTemplateSuccess : public testing::TestWithParam> { protected: - envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto() const { + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewriteProto() const { envoy::extensions::pattern_template::PatternTemplateRewriteSegments proto; Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); return proto; @@ -228,7 +228,7 @@ INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateS TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { absl::StatusOr rewritten_url = - rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto()); + rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewriteProto()); ASSERT_OK(rewritten_url); EXPECT_EQ(rewritten_url.value(), expectedRewrittenUrl()); } @@ -296,7 +296,7 @@ class URLPatternMatchAndRewrite : public testing::TestWithParam< std::tuple> { protected: - const std::string& url_pattern() const { return std::get<0>(GetParam()); } + const std::string& urlPattern() const { return std::get<0>(GetParam()); } const std::string& rewritePattern() const { return std::get<1>(GetParam()); } const std::string& matchUrl() const { return std::get<2>(GetParam()); } const std::string& expectedRewrittenUrl() const { return std::get<3>(GetParam()); } @@ -316,7 +316,7 @@ INSTANTIATE_TEST_SUITE_P( "/region/eu/bucket/prod-storage/object.pdf", "/euprod-storage/object.pdf"}}))); TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { - absl::StatusOr regex = convertURLPatternSyntaxToRegex(url_pattern()); + absl::StatusOr regex = convertURLPatternSyntaxToRegex(urlPattern()); ASSERT_OK(regex); absl::StatusOr From 54473ad7dc7530207927a61bda533498b898e82d Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 9 Aug 2022 16:03:47 +0000 Subject: [PATCH 129/238] Format Signed-off-by: silverstar195 --- envoy/router/path_match_policy.h | 1 + 1 file changed, 1 insertion(+) diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index 6d3f154ee6e75..b4c0a558b45b6 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -56,6 +56,7 @@ class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; + absl::Statyus isCompatibleMatchPolicy /** * @return the name of the match pattern predicate to be created. */ From 0320a5e973f640946bc7bd10422e2fc2f6ac1052 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 9 Aug 2022 17:45:18 +0000 Subject: [PATCH 130/238] Reformat validations Signed-off-by: silverstar195 --- envoy/router/path_match_policy.h | 1 - envoy/router/path_rewrite_policy.h | 10 +++++ source/common/router/config_impl.cc | 45 +++++++------------ .../path/match/pattern_template/BUILD | 1 + .../path/rewrite/pattern_template/BUILD | 2 + .../pattern_template_rewrite.cc | 20 +++++++++ .../pattern_template_rewrite.h | 4 ++ 7 files changed, 52 insertions(+), 31 deletions(-) diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index b4c0a558b45b6..6d3f154ee6e75 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -56,7 +56,6 @@ class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; - absl::Statyus isCompatibleMatchPolicy /** * @return the name of the match pattern predicate to be created. */ diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index 1f0c353cf5b1c..e491ecf200872 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -1,6 +1,7 @@ #pragma once #include "envoy/config/typed_config.h" +#include "envoy/router/path_match_policy.h" #include "source/common/common/logger.h" @@ -19,6 +20,15 @@ class PathRewritePredicate : Logger::Loggable { PathRewritePredicate() = default; virtual ~PathRewritePredicate() = default; + /** + * Used to determine if the match policy is compatible. + * + * @param match_policy current path match policy for route + * @return valid if current path match policy is acceptable + */ + virtual absl::Status + isCompatibleMatchPolicy(PathMatchPredicateSharedPtr path_match_policy) const PURE; + /** * Used to rewrite the current url to the specified output. Can return a failure in case rewrite * is not successful. diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 7fb3863bfbca0..b1edadfa904ce 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -704,25 +704,22 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, } } + int num_rewrite_polices = 0; if (path_rewrite_policy_.enabled()) { - if (!prefix_rewrite_.empty() || route.route().has_regex_rewrite()) { - throw EnvoyException( - "Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); - } + ++num_rewrite_polices; } if (!prefix_rewrite_.empty()) { - if (route.route().has_regex_rewrite() || path_rewrite_policy_.enabled()) { - throw EnvoyException( - "Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); - } + ++num_rewrite_polices; } if (route.route().has_regex_rewrite()) { - if (!prefix_rewrite_.empty() || path_rewrite_policy_.enabled()) { - throw EnvoyException( - "Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); - } + ++num_rewrite_polices; + } + + if (num_rewrite_polices > 1) { + throw EnvoyException( + "Specify only one of prefix_rewrite, regex_rewrite or path_rewrite_policy"); } if (route.route().has_regex_rewrite()) { @@ -731,24 +728,12 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, regex_rewrite_substitution_ = rewrite_spec.substitution(); } - // Check if pattern rewrite is enabled that the pattern matching is also enabled. - // This is needed to match up variable values. - if (path_rewrite_policy_.enabled() && - path_rewrite_policy_.predicate()->name() == Extensions::PatternTemplate::Rewrite::NAME) { - if (!path_match_policy_.enabled() || - path_match_policy_.predicate()->name() != Extensions::PatternTemplate::Match::NAME) { - throw EnvoyException(fmt::format("unable to use {} extension without {} extension", - Extensions::PatternTemplate::Rewrite::NAME, - Extensions::PatternTemplate::Match::NAME)); - } - - // Validation between extensions as they share rewrite pattern variables. - if (!Extensions::PatternTemplate::isValidSharedVariableSet( - path_rewrite_policy_.predicate()->pattern(), path_match_policy_.predicate()->pattern()) - .ok()) { - throw EnvoyException(fmt::format( - "mismatch between variables in path_match_policy {} and path_rewrite_policy {}", - path_match_policy_.predicate()->pattern(), path_rewrite_policy_.predicate()->pattern())); + // validate rewrite policy is valid with the provided match policy + if (path_rewrite_policy_.enabled()) { + absl::Status compatible_polices = + path_rewrite_policy_.predicate()->isCompatibleMatchPolicy(path_match_policy_.predicate()); + if (!compatible_polices.ok()) { + throw EnvoyException(std::string(compatible_polices.message())); } } diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD index 39e9df419bfe5..8134b65a98aa9 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/pattern_template/BUILD @@ -17,6 +17,7 @@ envoy_cc_library( hdrs = ["pattern_template_match.h"], visibility = [ "//source/common/router:__subpackages__", + "//source/extensions/path/rewrite/pattern_template:__subpackages__", ], deps = [ "//envoy/router:router_path_match_policy_interface", diff --git a/source/extensions/path/rewrite/pattern_template/BUILD b/source/extensions/path/rewrite/pattern_template/BUILD index db7cb6830b591..5ecf07dd7bca7 100644 --- a/source/extensions/path/rewrite/pattern_template/BUILD +++ b/source/extensions/path/rewrite/pattern_template/BUILD @@ -19,9 +19,11 @@ envoy_cc_library( "//source/common/router:__subpackages__", ], deps = [ + "//envoy/router:router_path_match_policy_interface", "//envoy/router:router_path_rewrite_policy_interface", "//source/common/protobuf", "//source/common/protobuf:utility_lib", + "//source/extensions/path/match/pattern_template:pattern_template_match_lib", "//source/extensions/path/pattern_template_lib", "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index 5c3740a28a68b..71acd34adf966 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -6,6 +6,7 @@ #include #include "source/common/http/path_utility.h" +#include "source/extensions/path/match/pattern_template/pattern_template_match.h" #include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" #include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" @@ -24,6 +25,25 @@ namespace Rewrite { #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif +absl::Status PatternTemplateRewritePredicate::isCompatibleMatchPolicy( + Router::PathMatchPredicateSharedPtr path_match_predicate) const { + if (path_match_predicate->name() != Extensions::PatternTemplate::Match::NAME) { + return absl::InvalidArgumentError(fmt::format("unable to use {} extension without {} extension", + Extensions::PatternTemplate::Rewrite::NAME, + Extensions::PatternTemplate::Match::NAME)); + } + + // This is needed to match up variable values. + // Validation between extensions as they share rewrite pattern variables. + if (!isValidSharedVariableSet(url_rewrite_pattern_, path_match_predicate->pattern()).ok()) { + return absl::InvalidArgumentError( + fmt::format("mismatch between variables in path_match_policy {} and path_rewrite_policy {}", + path_match_predicate->pattern(), url_rewrite_pattern_)); + } + + return absl::OkStatus(); +} + absl::Status PatternTemplateRewritePredicate::isValidRewritePattern(std::string match_pattern, std::string rewrite_pattern) { diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index b951ec9b058e6..4fc0a1b146abc 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -6,6 +6,7 @@ #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" +#include "envoy/router/path_match_policy.h" #include "envoy/router/path_rewrite_policy.h" #include "source/common/protobuf/message_validator_impl.h" @@ -35,6 +36,9 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { absl::StatusOr rewriteUrl(absl::string_view current_pattern, absl::string_view matched_path) const override; + absl::Status + isCompatibleMatchPolicy(Router::PathMatchPredicateSharedPtr match_policy) const override; + static absl::Status isValidRewritePattern(std::string match_pattern, std::string rewrite_pattern); absl::string_view name() const override { return NAME; } From 433b9b654c65ced5ae54985317c7345e8fc96b53 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 9 Aug 2022 18:23:43 +0000 Subject: [PATCH 131/238] Remove unneeded factory class member Signed-off-by: silverstar195 --- envoy/router/path_rewrite_policy.h | 4 ++-- source/common/router/config_impl.cc | 19 ++++++++----------- source/common/router/config_impl.h | 2 -- .../pattern_template_rewrite.cc | 4 ++-- .../pattern_template_rewrite.h | 4 ++-- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index e491ecf200872..fce04c2d8b607 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -26,8 +26,8 @@ class PathRewritePredicate : Logger::Loggable { * @param match_policy current path match policy for route * @return valid if current path match policy is acceptable */ - virtual absl::Status - isCompatibleMatchPolicy(PathMatchPredicateSharedPtr path_match_policy) const PURE; + virtual absl::Status isCompatibleMatchPolicy(PathMatchPredicateSharedPtr path_match_policy, + bool active_policy) const PURE; /** * Used to rewrite the current url to the specified output. Can return a failure in case rewrite diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index b1edadfa904ce..bc3c92cb0ade6 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -362,18 +362,17 @@ PathRewritePolicyImpl::PathRewritePolicyImpl( ProtobufMessage::ValidationVisitor& validator, std::string route_url) : route_url_(route_url), enabled_(true) { - predicate_factory_ = + const auto& predicate_factory = &Envoy::Config::Utility::getAndCheckFactory(typed_config); - ASSERT(predicate_factory_); // factory not found predicate_config_ = Envoy::Config::Utility::translateAnyToFactoryConfig( - typed_config.typed_config(), validator, *predicate_factory_); + typed_config.typed_config(), validator, *predicate_factory); // Validate config format and inputs when creating factory. // As the validation and create are nearly 1:1 store for later use. // Predicate is only ever created once. absl::StatusOr config_or_error = - predicate_factory_->createPathRewritePredicate(*predicate_config_); + predicate_factory->createPathRewritePredicate(*predicate_config_); if (!config_or_error.ok()) { throw EnvoyException(std::string(config_or_error.status().message())); @@ -391,18 +390,17 @@ PathMatchPolicyImpl::PathMatchPolicyImpl( ProtobufMessage::ValidationVisitor& validator, std::string path) : path_(path), enabled_(true) { - predicate_factory_ = + const auto& predicate_factory = &Envoy::Config::Utility::getAndCheckFactory(typed_config); - ASSERT(predicate_factory_); // factory not found predicate_config_ = Envoy::Config::Utility::translateAnyToFactoryConfig( - typed_config.typed_config(), validator, *predicate_factory_); + typed_config.typed_config(), validator, *predicate_factory); // Validate config format and inputs when creating factory. // As the validation and create are nearly 1:1 store for later use. // Predicate is only ever created once. absl::StatusOr config_or_error = - predicate_factory_->createPathMatchPredicate(*predicate_config_); + predicate_factory->createPathMatchPredicate(*predicate_config_); if (!config_or_error.ok()) { throw EnvoyException(std::string(config_or_error.status().message())); @@ -728,10 +726,9 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, regex_rewrite_substitution_ = rewrite_spec.substitution(); } - // validate rewrite policy is valid with the provided match policy if (path_rewrite_policy_.enabled()) { - absl::Status compatible_polices = - path_rewrite_policy_.predicate()->isCompatibleMatchPolicy(path_match_policy_.predicate()); + absl::Status compatible_polices = path_rewrite_policy_.predicate()->isCompatibleMatchPolicy( + path_match_policy_.predicate(), path_match_policy_.enabled()); if (!compatible_polices.ok()) { throw EnvoyException(std::string(compatible_polices.message())); } diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 0b4a85f32399f..a44351364ba97 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -479,7 +479,6 @@ class PathMatchPolicyImpl : public PathMatchPolicy { const std::string path_; const bool enabled_; ProtobufTypes::MessagePtr predicate_config_; - PathMatchPredicateFactory* predicate_factory_; PathMatchPredicateSharedPtr predicate_; }; @@ -505,7 +504,6 @@ class PathRewritePolicyImpl : public PathRewritePolicy { const std::string route_url_; const bool enabled_; ProtobufTypes::MessagePtr predicate_config_; - PathRewritePredicateFactory* predicate_factory_; PathRewritePredicateSharedPtr predicate_; }; diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index 71acd34adf966..feb90b98b09fa 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -26,8 +26,8 @@ namespace Rewrite { #endif absl::Status PatternTemplateRewritePredicate::isCompatibleMatchPolicy( - Router::PathMatchPredicateSharedPtr path_match_predicate) const { - if (path_match_predicate->name() != Extensions::PatternTemplate::Match::NAME) { + Router::PathMatchPredicateSharedPtr path_match_predicate, bool active_policy) const { + if (!active_policy || path_match_predicate->name() != Extensions::PatternTemplate::Match::NAME) { return absl::InvalidArgumentError(fmt::format("unable to use {} extension without {} extension", Extensions::PatternTemplate::Rewrite::NAME, Extensions::PatternTemplate::Match::NAME)); diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 4fc0a1b146abc..3923bbb7667d8 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -36,8 +36,8 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { absl::StatusOr rewriteUrl(absl::string_view current_pattern, absl::string_view matched_path) const override; - absl::Status - isCompatibleMatchPolicy(Router::PathMatchPredicateSharedPtr match_policy) const override; + absl::Status isCompatibleMatchPolicy(Router::PathMatchPredicateSharedPtr match_policy, + bool active_policy) const override; static absl::Status isValidRewritePattern(std::string match_pattern, std::string rewrite_pattern); From 16d88c24f3773beb4b741ba42c05f2771baa5498 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 10 Aug 2022 17:10:19 +0000 Subject: [PATCH 132/238] Add config tests Signed-off-by: silverstar195 --- .../path/match/pattern_template/BUILD | 31 ++++++ .../match/pattern_template/config_test.cc | 102 +++++++++++++++++ .../match/pattern_template/library_test.cc | 14 +++ .../path/rewrite/pattern_template/BUILD | 31 ++++++ .../rewrite/pattern_template/config_test.cc | 103 ++++++++++++++++++ .../rewrite/pattern_template/library_test.cc | 13 +++ 6 files changed, 294 insertions(+) create mode 100644 test/extensions/path/match/pattern_template/BUILD create mode 100644 test/extensions/path/match/pattern_template/config_test.cc create mode 100644 test/extensions/path/match/pattern_template/library_test.cc create mode 100644 test/extensions/path/rewrite/pattern_template/BUILD create mode 100644 test/extensions/path/rewrite/pattern_template/config_test.cc create mode 100644 test/extensions/path/rewrite/pattern_template/library_test.cc diff --git a/test/extensions/path/match/pattern_template/BUILD b/test/extensions/path/match/pattern_template/BUILD new file mode 100644 index 0000000000000..52fec127047c2 --- /dev/null +++ b/test/extensions/path/match/pattern_template/BUILD @@ -0,0 +1,31 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_package", +) +load( + "//test/extensions:extensions_build_system.bzl", + "envoy_extension_cc_test", +) + +licenses(["notice"]) # Apache 2 + +envoy_package() + +envoy_extension_cc_test( + name = "config_test", + srcs = ["config_test.cc"], + extension_names = ["envoy.path.match.pattern_template.pattern_template_match_predicate"], + deps = [ + "//source/extensions/path/match/pattern_template:config", + "//test/mocks/server:factory_context_mocks", + ], +) + +envoy_extension_cc_test( + name = "library_test", + srcs = ["library_test.cc"], + extension_names = ["envoy.path.match.pattern_template.pattern_template_match_predicate"], + deps = [ + "//source/extensions/path/match/pattern_template:pattern_template_match_lib", + ], +) diff --git a/test/extensions/path/match/pattern_template/config_test.cc b/test/extensions/path/match/pattern_template/config_test.cc new file mode 100644 index 0000000000000..ce198dd638239 --- /dev/null +++ b/test/extensions/path/match/pattern_template/config_test.cc @@ -0,0 +1,102 @@ +#include "source/common/config/utility.h" +#include "source/extensions/path/match/pattern_template/config.h" + +#include "test/mocks/server/factory_context.h" +#include "test/test_common/environment.h" + +#include "gtest/gtest.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Match { + + +TEST(ConfigTest, TestEmptyConfig) { + const std::string yaml_string = R"EOF( + name: envoy.path.match.pattern_template.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/bar/{lang}/{country}" +)EOF"; + + envoy::config::core::v3::TypedExtensionConfig config; + TestUtility::loadFromYaml(yaml_string, config); + + const auto& factory = + &Envoy::Config::Utility::getAndCheckFactory(config); + + EXPECT_NE(nullptr, factory); + EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); + + auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( + config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); + + EXPECT_NE(nullptr, message); + + ProtobufTypes::MessagePtr empty_config = factory->createEmptyConfigProto(); + + EXPECT_NE(nullptr, empty_config); +} + +TEST(ConfigTest, InvalidConfigSetup) { + const std::string yaml_string = R"EOF( + name: envoy.path.match.pattern_template.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/bar/{lang}/{country" +)EOF"; + + envoy::config::core::v3::TypedExtensionConfig config; + TestUtility::loadFromYaml(yaml_string, config); + + const auto& factory = + &Envoy::Config::Utility::getAndCheckFactory(config); + + EXPECT_NE(nullptr, factory); + EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); + + auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( + config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); + + EXPECT_NE(nullptr, message); + + absl::StatusOr config_or_error = + factory->createPathMatchPredicate(*message); + + EXPECT_FALSE(config_or_error.ok()); + EXPECT_EQ(config_or_error.status().message(), "path_match_policy.path_template /bar/{lang}/{country is invalid"); +} + +TEST(ConfigTest, TestConfigSetup) { + const std::string yaml_string = R"EOF( + name: envoy.path.match.pattern_template.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/bar/{lang}/{country}" +)EOF"; + + envoy::config::core::v3::TypedExtensionConfig config; + TestUtility::loadFromYaml(yaml_string, config); + + const auto& factory = + &Envoy::Config::Utility::getAndCheckFactory(config); + + EXPECT_NE(nullptr, factory); + EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); + + auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( + config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); + + EXPECT_NE(nullptr, message); + + absl::StatusOr config_or_error = + factory->createPathMatchPredicate(*message); + + EXPECT_TRUE(config_or_error.ok()); +} + +} // namespace Rewrite +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/test/extensions/path/match/pattern_template/library_test.cc b/test/extensions/path/match/pattern_template/library_test.cc new file mode 100644 index 0000000000000..49f8eec42daee --- /dev/null +++ b/test/extensions/path/match/pattern_template/library_test.cc @@ -0,0 +1,14 @@ +#include "source/extensions/matching/common_inputs/environment_variable/input.h" + +#include "gtest/gtest.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Rewrite { + + +} // namespace Rewrite +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/test/extensions/path/rewrite/pattern_template/BUILD b/test/extensions/path/rewrite/pattern_template/BUILD new file mode 100644 index 0000000000000..b7cd35e324aa6 --- /dev/null +++ b/test/extensions/path/rewrite/pattern_template/BUILD @@ -0,0 +1,31 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_package", +) +load( + "//test/extensions:extensions_build_system.bzl", + "envoy_extension_cc_test", +) + +licenses(["notice"]) # Apache 2 + +envoy_package() + +envoy_extension_cc_test( + name = "config_test", + srcs = ["config_test.cc"], + extension_names = ["envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"], + deps = [ + "//source/extensions/path/rewrite/pattern_template:config", + "//test/mocks/server:factory_context_mocks", + ], +) + +envoy_extension_cc_test( + name = "library_test", + srcs = ["library_test.cc"], + extension_names = ["envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"], + deps = [ + "//source/extensions/path/rewrite/pattern_template:pattern_template_rewrite_lib", + ], +) diff --git a/test/extensions/path/rewrite/pattern_template/config_test.cc b/test/extensions/path/rewrite/pattern_template/config_test.cc new file mode 100644 index 0000000000000..d9f04087df1d0 --- /dev/null +++ b/test/extensions/path/rewrite/pattern_template/config_test.cc @@ -0,0 +1,103 @@ +#include "source/common/config/utility.h" +#include "source/extensions/path/rewrite/pattern_template/config.h" + +#include "test/mocks/server/factory_context.h" +#include "test/test_common/environment.h" + +#include "gtest/gtest.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Rewrite { + + +TEST(ConfigTest, TestEmptyConfig) { + const std::string yaml_string = R"EOF( + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/bar/{lang}/{country}" +)EOF"; + + envoy::config::core::v3::TypedExtensionConfig config; + TestUtility::loadFromYaml(yaml_string, config); + + const auto& factory = + &Envoy::Config::Utility::getAndCheckFactory(config); + + EXPECT_NE(nullptr, factory); + EXPECT_EQ(factory->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + + auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( + config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); + + EXPECT_NE(nullptr, message); + + ProtobufTypes::MessagePtr empty_config = factory->createEmptyConfigProto(); + + EXPECT_NE(nullptr, empty_config); +} + +TEST(ConfigTest, InvalidConfigSetup) { + const std::string yaml_string = R"EOF( + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/bar/{lang}/{country" +)EOF"; + + envoy::config::core::v3::TypedExtensionConfig config; + TestUtility::loadFromYaml(yaml_string, config); + + const auto& factory = + &Envoy::Config::Utility::getAndCheckFactory(config); + + EXPECT_NE(nullptr, factory); + EXPECT_EQ(factory->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + + auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( + config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); + + EXPECT_NE(nullptr, message); + + absl::StatusOr config_or_error = + factory->createPathRewritePredicate(*message); + + EXPECT_FALSE(config_or_error.ok()); + EXPECT_EQ(config_or_error.status().message(), "path_rewrite_policy.path_template_rewrite /bar/{lang}/{country is invalid"); +} + + +TEST(ConfigTest, TestConfigSetup) { + const std::string yaml_string = R"EOF( + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/bar/{lang}/{country}" +)EOF"; + + envoy::config::core::v3::TypedExtensionConfig config; + TestUtility::loadFromYaml(yaml_string, config); + + const auto& factory = + &Envoy::Config::Utility::getAndCheckFactory(config); + + EXPECT_NE(nullptr, factory); + EXPECT_EQ(factory->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + + auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( + config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); + + EXPECT_NE(nullptr, message); + + absl::StatusOr config_or_error = + factory->createPathRewritePredicate(*message); + + EXPECT_TRUE(config_or_error.ok()); +} + +} // namespace Rewrite +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc new file mode 100644 index 0000000000000..3e1806401a38e --- /dev/null +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -0,0 +1,13 @@ +#include "source/extensions/matching/common_inputs/environment_variable/input.h" + +#include "gtest/gtest.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { +namespace Rewrite { + +} // namespace Rewrite +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy From 7e5a7850761d465203180fb7e3b67343fcdd9d8c Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 10 Aug 2022 18:30:41 +0000 Subject: [PATCH 133/238] Add library tests Signed-off-by: silverstar195 --- .../path/match/pattern_template/BUILD | 1 + .../path/rewrite/pattern_template/BUILD | 1 + .../path/match/pattern_template/BUILD | 2 + .../match/pattern_template/library_test.cc | 41 +++++++++++++-- .../path/rewrite/pattern_template/BUILD | 2 + .../rewrite/pattern_template/library_test.cc | 51 ++++++++++++++++++- 6 files changed, 94 insertions(+), 4 deletions(-) diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD index 8134b65a98aa9..53bfe3f0565f1 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/pattern_template/BUILD @@ -18,6 +18,7 @@ envoy_cc_library( visibility = [ "//source/common/router:__subpackages__", "//source/extensions/path/rewrite/pattern_template:__subpackages__", + "//test/extensions/path/match/pattern_template:__subpackages__", ], deps = [ "//envoy/router:router_path_match_policy_interface", diff --git a/source/extensions/path/rewrite/pattern_template/BUILD b/source/extensions/path/rewrite/pattern_template/BUILD index 5ecf07dd7bca7..80c83ab6c428b 100644 --- a/source/extensions/path/rewrite/pattern_template/BUILD +++ b/source/extensions/path/rewrite/pattern_template/BUILD @@ -17,6 +17,7 @@ envoy_cc_library( hdrs = ["pattern_template_rewrite.h"], visibility = [ "//source/common/router:__subpackages__", + "//test/extensions/path/rewrite/pattern_template:__subpackages__", ], deps = [ "//envoy/router:router_path_match_policy_interface", diff --git a/test/extensions/path/match/pattern_template/BUILD b/test/extensions/path/match/pattern_template/BUILD index 52fec127047c2..927bfb4739670 100644 --- a/test/extensions/path/match/pattern_template/BUILD +++ b/test/extensions/path/match/pattern_template/BUILD @@ -26,6 +26,8 @@ envoy_extension_cc_test( srcs = ["library_test.cc"], extension_names = ["envoy.path.match.pattern_template.pattern_template_match_predicate"], deps = [ + "//source/extensions/path/match/pattern_template:config", "//source/extensions/path/match/pattern_template:pattern_template_match_lib", + "//test/mocks/server:factory_context_mocks", ], ) diff --git a/test/extensions/path/match/pattern_template/library_test.cc b/test/extensions/path/match/pattern_template/library_test.cc index 49f8eec42daee..3fb795557c58c 100644 --- a/test/extensions/path/match/pattern_template/library_test.cc +++ b/test/extensions/path/match/pattern_template/library_test.cc @@ -1,14 +1,49 @@ -#include "source/extensions/matching/common_inputs/environment_variable/input.h" +#include "source/extensions/path/match/pattern_template/pattern_template_match.h" +#include "source/common/config/utility.h" +#include "source/extensions/path/match/pattern_template/config.h" + +#include "test/mocks/server/factory_context.h" +#include "test/test_common/environment.h" #include "gtest/gtest.h" namespace Envoy { namespace Extensions { namespace PatternTemplate { -namespace Rewrite { +namespace Match { + +Router::PathMatchPredicateSharedPtr createMatchPredicateFromYaml(std::string yaml_string) { + envoy::config::core::v3::TypedExtensionConfig config; + TestUtility::loadFromYaml(yaml_string, config); + + const auto& factory = + &Envoy::Config::Utility::getAndCheckFactory(config); + + auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( + config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); + + absl::StatusOr config_or_error = + factory->createPathMatchPredicate(*message); + + return config_or_error.value(); +} + +TEST(MatchTest, BasicUsage) { + const std::string yaml_string = R"EOF( + name: envoy.path.match.pattern_template.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/bar/{lang}/{country}" +)EOF"; + + Router::PathMatchPredicateSharedPtr predicate = createMatchPredicateFromYaml(yaml_string); + EXPECT_EQ(predicate->pattern(), "/bar/{lang}/{country}"); + EXPECT_EQ(predicate->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); + EXPECT_TRUE(predicate->match("/bar/en/us")); +} -} // namespace Rewrite +} // namespace Match } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy diff --git a/test/extensions/path/rewrite/pattern_template/BUILD b/test/extensions/path/rewrite/pattern_template/BUILD index b7cd35e324aa6..99d744c49d29b 100644 --- a/test/extensions/path/rewrite/pattern_template/BUILD +++ b/test/extensions/path/rewrite/pattern_template/BUILD @@ -26,6 +26,8 @@ envoy_extension_cc_test( srcs = ["library_test.cc"], extension_names = ["envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"], deps = [ + "//source/extensions/path/rewrite/pattern_template:config", "//source/extensions/path/rewrite/pattern_template:pattern_template_rewrite_lib", + "//test/mocks/server:factory_context_mocks", ], ) diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc index 3e1806401a38e..86ed8f0684bf2 100644 --- a/test/extensions/path/rewrite/pattern_template/library_test.cc +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -1,5 +1,10 @@ -#include "source/extensions/matching/common_inputs/environment_variable/input.h" +#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" +#include "source/common/config/utility.h" +#include "source/extensions/path/rewrite/pattern_template/config.h" + +#include "test/mocks/server/factory_context.h" +#include "test/test_common/environment.h" #include "gtest/gtest.h" namespace Envoy { @@ -7,6 +12,50 @@ namespace Extensions { namespace PatternTemplate { namespace Rewrite { +Router::PathRewritePredicateSharedPtr createRewritePredicateFromYaml(std::string yaml_string) { + envoy::config::core::v3::TypedExtensionConfig config; + TestUtility::loadFromYaml(yaml_string, config); + + const auto& factory = + &Envoy::Config::Utility::getAndCheckFactory(config); + + auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( + config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); + + absl::StatusOr config_or_error = + factory->createPathRewritePredicate(*message); + + return config_or_error.value(); +} + +TEST(RewriteTest, BasicSetup) { + const std::string yaml_string = R"EOF( + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/bar/{lang}/{country}" +)EOF"; + + Router::PathRewritePredicateSharedPtr predicate = createRewritePredicateFromYaml(yaml_string); + EXPECT_EQ(predicate->pattern(), "/bar/{lang}/{country}"); + EXPECT_EQ(predicate->name(), + "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); +} + +TEST(RewriteTest, BasicUsage) { + const std::string yaml_string = R"EOF( + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/bar/{lang}/{country}" +)EOF"; + + Router::PathRewritePredicateSharedPtr predicate = createRewritePredicateFromYaml(yaml_string); + EXPECT_EQ(predicate->rewriteUrl("/bar/en/usa", "/bar/{country}/{lang}").value(), "/bar/usa/en"); + EXPECT_EQ(predicate->name(), + "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); +} + } // namespace Rewrite } // namespace PatternTemplate } // namespace Extensions From c35f5994f031e3084e904b5b6a0a324faf392b29 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 10 Aug 2022 19:09:32 +0000 Subject: [PATCH 134/238] Format tests Signed-off-by: silverstar195 --- .../path/match/pattern_template/config_test.cc | 6 +++--- .../path/match/pattern_template/library_test.cc | 7 ++++--- .../path/rewrite/pattern_template/config_test.cc | 14 ++++++++------ .../path/rewrite/pattern_template/library_test.cc | 4 ++-- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/test/extensions/path/match/pattern_template/config_test.cc b/test/extensions/path/match/pattern_template/config_test.cc index ce198dd638239..6413bbc88c5d5 100644 --- a/test/extensions/path/match/pattern_template/config_test.cc +++ b/test/extensions/path/match/pattern_template/config_test.cc @@ -11,7 +11,6 @@ namespace Extensions { namespace PatternTemplate { namespace Match { - TEST(ConfigTest, TestEmptyConfig) { const std::string yaml_string = R"EOF( name: envoy.path.match.pattern_template.pattern_template_match_predicate @@ -65,7 +64,8 @@ TEST(ConfigTest, InvalidConfigSetup) { factory->createPathMatchPredicate(*message); EXPECT_FALSE(config_or_error.ok()); - EXPECT_EQ(config_or_error.status().message(), "path_match_policy.path_template /bar/{lang}/{country is invalid"); + EXPECT_EQ(config_or_error.status().message(), + "path_match_policy.path_template /bar/{lang}/{country is invalid"); } TEST(ConfigTest, TestConfigSetup) { @@ -96,7 +96,7 @@ TEST(ConfigTest, TestConfigSetup) { EXPECT_TRUE(config_or_error.ok()); } -} // namespace Rewrite +} // namespace Match } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy diff --git a/test/extensions/path/match/pattern_template/library_test.cc b/test/extensions/path/match/pattern_template/library_test.cc index 3fb795557c58c..b2f35840dafb4 100644 --- a/test/extensions/path/match/pattern_template/library_test.cc +++ b/test/extensions/path/match/pattern_template/library_test.cc @@ -1,10 +1,10 @@ -#include "source/extensions/path/match/pattern_template/pattern_template_match.h" - #include "source/common/config/utility.h" #include "source/extensions/path/match/pattern_template/config.h" +#include "source/extensions/path/match/pattern_template/pattern_template_match.h" #include "test/mocks/server/factory_context.h" #include "test/test_common/environment.h" + #include "gtest/gtest.h" namespace Envoy { @@ -38,7 +38,8 @@ TEST(MatchTest, BasicUsage) { Router::PathMatchPredicateSharedPtr predicate = createMatchPredicateFromYaml(yaml_string); EXPECT_EQ(predicate->pattern(), "/bar/{lang}/{country}"); - EXPECT_EQ(predicate->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); + EXPECT_EQ(predicate->name(), + "envoy.path.match.pattern_template.pattern_template_match_predicate"); EXPECT_TRUE(predicate->match("/bar/en/us")); } diff --git a/test/extensions/path/rewrite/pattern_template/config_test.cc b/test/extensions/path/rewrite/pattern_template/config_test.cc index d9f04087df1d0..e41bbc34aa9de 100644 --- a/test/extensions/path/rewrite/pattern_template/config_test.cc +++ b/test/extensions/path/rewrite/pattern_template/config_test.cc @@ -11,7 +11,6 @@ namespace Extensions { namespace PatternTemplate { namespace Rewrite { - TEST(ConfigTest, TestEmptyConfig) { const std::string yaml_string = R"EOF( name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate @@ -27,7 +26,8 @@ TEST(ConfigTest, TestEmptyConfig) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + EXPECT_EQ(factory->name(), + "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -54,7 +54,8 @@ TEST(ConfigTest, InvalidConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + EXPECT_EQ(factory->name(), + "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -65,10 +66,10 @@ TEST(ConfigTest, InvalidConfigSetup) { factory->createPathRewritePredicate(*message); EXPECT_FALSE(config_or_error.ok()); - EXPECT_EQ(config_or_error.status().message(), "path_rewrite_policy.path_template_rewrite /bar/{lang}/{country is invalid"); + EXPECT_EQ(config_or_error.status().message(), + "path_rewrite_policy.path_template_rewrite /bar/{lang}/{country is invalid"); } - TEST(ConfigTest, TestConfigSetup) { const std::string yaml_string = R"EOF( name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate @@ -84,7 +85,8 @@ TEST(ConfigTest, TestConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + EXPECT_EQ(factory->name(), + "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc index 86ed8f0684bf2..cb20eb634aa6f 100644 --- a/test/extensions/path/rewrite/pattern_template/library_test.cc +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -1,10 +1,10 @@ -#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" - #include "source/common/config/utility.h" #include "source/extensions/path/rewrite/pattern_template/config.h" +#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" #include "test/mocks/server/factory_context.h" #include "test/test_common/environment.h" + #include "gtest/gtest.h" namespace Envoy { From 9fae620d1629b706d6bb267c78522c53fd766c29 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 11 Aug 2022 14:04:22 +0000 Subject: [PATCH 135/238] Address some comments Signed-off-by: silverstar195 --- envoy/router/path_match_policy.h | 4 +- envoy/router/path_rewrite_policy.h | 4 +- .../path/match/pattern_template/config.cc | 1 + .../pattern_template_lib/pattern_template.h | 5 +- .../pattern_template_internal.cc | 12 ++-- .../pattern_template_internal.h | 63 +++++++++++++++++-- .../path/rewrite/pattern_template/config.cc | 1 + .../pattern_template_rewrite.cc | 24 ------- .../pattern_template_rewrite.h | 2 - 9 files changed, 72 insertions(+), 44 deletions(-) diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match_policy.h index 6d3f154ee6e75..d0e3e23f73e1d 100644 --- a/envoy/router/path_match_policy.h +++ b/envoy/router/path_match_policy.h @@ -11,7 +11,7 @@ namespace Envoy { namespace Router { /** - * Decides if the target route path is matching the provided pattern. + * Decides if the target route path matches the provided pattern. * Subclassing Logger::Loggable so that implementations can log details. */ class PathMatchPredicate : Logger::Loggable { @@ -23,7 +23,7 @@ class PathMatchPredicate : Logger::Loggable { * Used to determine if the current url matches the predicate pattern. * * @param url current url of route - * @return valid if route url matches the predicate pattern. + * @return true if route url matches the predicate pattern. */ virtual bool match(absl::string_view url) const PURE; diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index fce04c2d8b607..2e921bb1d9d29 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -12,7 +12,7 @@ namespace Envoy { namespace Router { /** - * Decides if the target route path is matching the provided pattern. + * Creates the new route path based on the provided rewrite pattern. * Subclassing Logger::Loggable so that implementations can log details. */ class PathRewritePredicate : Logger::Loggable { @@ -24,7 +24,7 @@ class PathRewritePredicate : Logger::Loggable { * Used to determine if the match policy is compatible. * * @param match_policy current path match policy for route - * @return valid if current path match policy is acceptable + * @return true if current path match policy is acceptable */ virtual absl::Status isCompatibleMatchPolicy(PathMatchPredicateSharedPtr path_match_policy, bool active_policy) const PURE; diff --git a/source/extensions/path/match/pattern_template/config.cc b/source/extensions/path/match/pattern_template/config.cc index 4295b98fcdbef..c425c4c6ba6c1 100644 --- a/source/extensions/path/match/pattern_template/config.cc +++ b/source/extensions/path/match/pattern_template/config.cc @@ -9,6 +9,7 @@ namespace PatternTemplate { namespace Match { REGISTER_FACTORY(PatternTemplateMatchPredicateFactory, Router::PathMatchPredicateFactory); + } // namespace Match } // namespace PatternTemplate } // namespace Extensions diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 7b1df3b89f5fc..593f1fe3f5a40 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -17,8 +17,9 @@ namespace PatternTemplate { enum class RewriteStringKind { KVariable, KLiteral }; struct RewritePatternSegment { - RewritePatternSegment(absl::string_view str, RewriteStringKind kind) : str(str), kind(kind) {} - absl::string_view str; + RewritePatternSegment(absl::string_view segment_value, RewriteStringKind kind) + : segment_value(segment_value), kind(kind) {} + absl::string_view segment_value; RewriteStringKind kind; }; diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc index 181b5c88f89aa..945741885cea9 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc @@ -33,9 +33,9 @@ namespace { #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif -unsigned long pattern_matching_max_variables_per_url = 5; -unsigned long pattern_matching_max_variable_name_len = 16; -unsigned long pattern_matching_min_variable_name_len = 1; +constexpr unsigned long kPatternMatchingMaxVariablesPerUrl = 5; +constexpr unsigned long kPatternMatchingMaxVariableNameLen = 16; +constexpr unsigned long kPatternMatchingMinVariableNameLen = 1; // Valid pchar from https://datatracker.ietf.org/doc/html/rfc3986#appendix-A constexpr absl::string_view kLiteral = "a-zA-Z0-9-._~" // Unreserved @@ -223,13 +223,13 @@ gatherCaptureNames(struct ParsedUrlPattern pattern) { if (!absl::holds_alternative(segment)) { continue; } - if (captured_variables.size() >= pattern_matching_max_variables_per_url) { + if (captured_variables.size() >= kPatternMatchingMaxVariablesPerUrl) { return absl::InvalidArgumentError("Exceeded variable count limit"); } absl::string_view var_name = absl::get(segment).var_name; - if (var_name.size() < pattern_matching_min_variable_name_len || - var_name.size() > pattern_matching_max_variable_name_len) { + if (var_name.size() < kPatternMatchingMinVariableNameLen || + var_name.size() > kPatternMatchingMaxVariableNameLen) { return absl::InvalidArgumentError("Invalid variable length"); } if (captured_variables.contains(var_name)) { diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h index 06f91ab3d9d8b..0277bf54698c4 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.h +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.h @@ -20,11 +20,18 @@ namespace PatternTemplate { namespace PatternTemplateInternal { using Literal = absl::string_view; + +/** + * Determines what operations to use on the input pattern segment + */ enum class Operator { KPathGlob, KTextGlob }; +/** + * Represents a segment of the rewritten URL, including any path segments, + * slash and prefix. + */ struct RewriteSegment { - // Represents a segment of the rewritten URL, including any path segments, - // slash and prefix. + absl::string_view literal; // Represents an index into the RE2 capture which value should be used @@ -33,6 +40,9 @@ struct RewriteSegment { int var_index; }; +/** + * Represents a pattern variable. Variables are included in both path match and rewrite paths. + */ struct Variable { absl::string_view var_name; std::vector> var_match; @@ -45,6 +55,9 @@ struct Variable { using ParsedSegment = absl::variant; +/** + * Represents the parsed path including literals and variables. + */ struct ParsedUrlPattern { std::vector parsed_segments; absl::optional suffix; @@ -53,16 +66,27 @@ struct ParsedUrlPattern { std::string debugString() const; }; +/** + * Check if literal is valid + */ bool isValidLiteral(absl::string_view pattern); +/** + * Check if rewrite literal is valid + */ bool isValidRewriteLiteral(absl::string_view pattern); +/** + * Check if indent is valid + */ bool isValidIndent(absl::string_view pattern); -// Used by the following Consume{Literal.Operator,Variable} functions -// in the return value. The functions would take the given pattern, -// parse what it can into |parsed_value| and return the unconsumed -// portion of the pattern in |unconsumed_pattern|. +/** + * Used by the following Consume{Literal.Operator,Variable} functions + * in the return value. The functions would take the given pattern, + * parse what it can into |parsed_value| and return the unconsumed + * portion of the pattern in |unconsumed_pattern|. + */ template struct ParsedResult { ParsedResult(T val, absl::string_view pattern) : parsed_value(val), unconsumed_pattern(pattern) {} @@ -70,22 +94,49 @@ template struct ParsedResult { absl::string_view unconsumed_pattern; }; +/** + * Converts input pattern to ParsedResult + */ absl::StatusOr> consumeLiteral(absl::string_view pattern); +/** + * Converts input pattern to ParsedResult + */ absl::StatusOr> consumeOperator(absl::string_view pattern); +/** + * Converts input pattern to ParsedResult + */ absl::StatusOr> consumeVariable(absl::string_view pattern); +/** + * Converts input pattern to ParsedUrlPattern + */ absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern); +/** + * Converts Literal to std::string + */ std::string toRegexPattern(Literal pattern); +/** + * Converts Operator to std::string + */ std::string toRegexPattern(Operator pattern); +/** + * Converts Variable to std::string + */ std::string toRegexPattern(const Variable& pattern); +/** + * Converts ParsedUrlPattern to std::string + */ std::string toRegexPattern(const struct ParsedUrlPattern& pattern); +/** + * Converts string_view to be used in re2 + */ inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } } // namespace PatternTemplateInternal diff --git a/source/extensions/path/rewrite/pattern_template/config.cc b/source/extensions/path/rewrite/pattern_template/config.cc index a813e1f46567f..894589f741763 100644 --- a/source/extensions/path/rewrite/pattern_template/config.cc +++ b/source/extensions/path/rewrite/pattern_template/config.cc @@ -9,6 +9,7 @@ namespace PatternTemplate { namespace Rewrite { REGISTER_FACTORY(PatternTemplateRewritePredicateFactory, Router::PathRewritePredicateFactory); + } // namespace Rewrite } // namespace PatternTemplate } // namespace Extensions diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index feb90b98b09fa..bf0759050bde2 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -44,30 +44,6 @@ absl::Status PatternTemplateRewritePredicate::isCompatibleMatchPolicy( return absl::OkStatus(); } -absl::Status PatternTemplateRewritePredicate::isValidRewritePattern(std::string match_pattern, - std::string rewrite_pattern) { - - if (!isValidPathTemplateRewritePattern(rewrite_pattern).ok()) { - return absl::InvalidArgumentError( - fmt::format("path_template_rewrite {} is invalid", match_pattern)); - } - - absl::StatusOr converted_pattern = convertURLPatternSyntaxToRegex(match_pattern); - if (!converted_pattern.ok()) { - return absl::InvalidArgumentError(fmt::format("path_template {} is invalid", match_pattern)); - } - - std::string path_template_match_regex = *std::move(converted_pattern); - if (path_template_match_regex.empty() || - !isValidSharedVariableSet(rewrite_pattern, path_template_match_regex).ok()) { - return absl::InvalidArgumentError( - fmt::format("mismatch between path_template {} and path_template_rewrite {}", match_pattern, - rewrite_pattern)); - } - - return absl::OkStatus(); -}; - absl::StatusOr PatternTemplateRewritePredicate::rewriteUrl(absl::string_view current_pattern, absl::string_view matched_path) const { diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 3923bbb7667d8..7e22c83c94950 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -39,8 +39,6 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { absl::Status isCompatibleMatchPolicy(Router::PathMatchPredicateSharedPtr match_policy, bool active_policy) const override; - static absl::Status isValidRewritePattern(std::string match_pattern, std::string rewrite_pattern); - absl::string_view name() const override { return NAME; } private: From 5d1b98e9cc167af08a83f4d471db6aa704ee2a18 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 11 Aug 2022 14:51:27 +0000 Subject: [PATCH 136/238] Adding more tests Signed-off-by: silverstar195 --- .../path/match/pattern_template/BUILD | 1 + .../path/rewrite/pattern_template/BUILD | 2 + .../rewrite/pattern_template/library_test.cc | 94 +++++++++++++++++++ 3 files changed, 97 insertions(+) diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/pattern_template/BUILD index 53bfe3f0565f1..ccec05223385f 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/pattern_template/BUILD @@ -19,6 +19,7 @@ envoy_cc_library( "//source/common/router:__subpackages__", "//source/extensions/path/rewrite/pattern_template:__subpackages__", "//test/extensions/path/match/pattern_template:__subpackages__", + "//test/extensions/path/rewrite/pattern_template:__subpackages__", ], deps = [ "//envoy/router:router_path_match_policy_interface", diff --git a/test/extensions/path/rewrite/pattern_template/BUILD b/test/extensions/path/rewrite/pattern_template/BUILD index 99d744c49d29b..1defff4c52998 100644 --- a/test/extensions/path/rewrite/pattern_template/BUILD +++ b/test/extensions/path/rewrite/pattern_template/BUILD @@ -26,6 +26,8 @@ envoy_extension_cc_test( srcs = ["library_test.cc"], extension_names = ["envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"], deps = [ + "//source/extensions/path/match/pattern_template:config", + "//source/extensions/path/match/pattern_template:pattern_template_match_lib", "//source/extensions/path/rewrite/pattern_template:config", "//source/extensions/path/rewrite/pattern_template:pattern_template_rewrite_lib", "//test/mocks/server:factory_context_mocks", diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc index cb20eb634aa6f..daffd1138c240 100644 --- a/test/extensions/path/rewrite/pattern_template/library_test.cc +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -1,4 +1,5 @@ #include "source/common/config/utility.h" +#include "source/extensions/path/match/pattern_template/pattern_template_match.h" #include "source/extensions/path/rewrite/pattern_template/config.h" #include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" @@ -12,6 +13,22 @@ namespace Extensions { namespace PatternTemplate { namespace Rewrite { +Router::PathMatchPredicateSharedPtr createMatchPredicateFromYaml(std::string yaml_string) { + envoy::config::core::v3::TypedExtensionConfig config; + TestUtility::loadFromYaml(yaml_string, config); + + const auto& factory = + &Envoy::Config::Utility::getAndCheckFactory(config); + + auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( + config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); + + absl::StatusOr config_or_error = + factory->createPathMatchPredicate(*message); + + return config_or_error.value(); +} + Router::PathRewritePredicateSharedPtr createRewritePredicateFromYaml(std::string yaml_string) { envoy::config::core::v3::TypedExtensionConfig config; TestUtility::loadFromYaml(yaml_string, config); @@ -56,6 +73,83 @@ TEST(RewriteTest, BasicUsage) { "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); } +TEST(RewriteTest, MatchPatternValidation) { + const std::string rewrite_yaml_string = R"EOF( + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/foo/{lang}/{country}" +)EOF"; + + const std::string match_yaml_string = R"EOF( + name: envoy.path.match.pattern_template.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/bar/{lang}/{country}" +)EOF"; + + Router::PathRewritePredicateSharedPtr rewrite_predicate = + createRewritePredicateFromYaml(rewrite_yaml_string); + Router::PathMatchPredicateSharedPtr match_predicate = + createMatchPredicateFromYaml(match_yaml_string); + + EXPECT_TRUE(rewrite_predicate->isCompatibleMatchPolicy(match_predicate, true).ok()); +} + +TEST(RewriteTest, MatchPatternInactive) { + const std::string rewrite_yaml_string = R"EOF( + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/foo/{lang}/{country}" +)EOF"; + + const std::string match_yaml_string = R"EOF( + name: envoy.path.match.pattern_template.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/bar/{lang}/{country}" +)EOF"; + + Router::PathRewritePredicateSharedPtr rewrite_predicate = + createRewritePredicateFromYaml(rewrite_yaml_string); + Router::PathMatchPredicateSharedPtr match_predicate = + createMatchPredicateFromYaml(match_yaml_string); + + absl::Status error = rewrite_predicate->isCompatibleMatchPolicy(match_predicate, false); + EXPECT_FALSE(error.ok()); + EXPECT_EQ(error.message(), + "unable to use envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate " + "extension without envoy.path.match.pattern_template.pattern_template_match_predicate " + "extension"); +} + +TEST(RewriteTest, MatchPatternMismatchedVars) { + const std::string rewrite_yaml_string = R"EOF( + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/foo/{lang}/{missing}" +)EOF"; + + const std::string match_yaml_string = R"EOF( + name: envoy.path.match.pattern_template.pattern_template_match_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + path_template: "/bar/{lang}/{country}" +)EOF"; + + Router::PathRewritePredicateSharedPtr rewrite_predicate = + createRewritePredicateFromYaml(rewrite_yaml_string); + Router::PathMatchPredicateSharedPtr match_predicate = + createMatchPredicateFromYaml(match_yaml_string); + + absl::Status error = rewrite_predicate->isCompatibleMatchPolicy(match_predicate, true); + EXPECT_FALSE(error.ok()); + EXPECT_EQ(error.message(), "mismatch between variables in path_match_policy " + "/bar/{lang}/{country} and path_rewrite_policy /foo/{lang}/{missing}"); +} + } // namespace Rewrite } // namespace PatternTemplate } // namespace Extensions From c98799a4dfa6a5643f6aa543a90fab62b15f6e6f Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 12 Aug 2022 13:37:06 +0000 Subject: [PATCH 137/238] Refactor internal method and add tests Signed-off-by: silverstar195 --- .../pattern_template_rewrite.cc | 35 +++++-------------- .../pattern_template_rewrite.h | 7 ---- .../rewrite/pattern_template/config_test.cc | 30 ++++++++++++++++ .../rewrite/pattern_template/library_test.cc | 14 ++++++++ 4 files changed, 53 insertions(+), 33 deletions(-) diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index bf0759050bde2..3cac40057f640 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -60,24 +60,8 @@ PatternTemplateRewritePredicate::rewriteUrl(absl::string_view current_pattern, return absl::InvalidArgumentError("Unable to parse url rewrite pattern"); } - envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_pattern_proto = - *std::move(rewrite_pattern); - - absl::StatusOr new_path = - rewriteURLTemplatePattern(current_pattern, regex_pattern_str, rewrite_pattern_proto); - - if (!new_path.ok()) { - return absl::InvalidArgumentError("Unable rewrite url to new URL"); - } - - return *std::move(new_path); -} - -absl::StatusOr PatternTemplateRewritePredicate::rewriteURLTemplatePattern( - absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern) - const { - RE2 regex = RE2(PatternTemplateInternal::toStringPiece(capture_regex)); + const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern_proto = *std::move(rewrite_pattern); + RE2 regex = RE2(PatternTemplateInternal::toStringPiece(regex_pattern_str)); if (!regex.ok()) { return absl::InternalError(regex.error()); } @@ -85,26 +69,25 @@ absl::StatusOr PatternTemplateRewritePredicate::rewriteURLTemplateP // First capture is the whole matched regex pattern. int capture_num = regex.NumberOfCapturingGroups() + 1; std::vector captures(capture_num); - if (!regex.Match(PatternTemplateInternal::toStringPiece(url), /*startpos=*/0, - /*endpos=*/url.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { + if (!regex.Match(PatternTemplateInternal::toStringPiece(current_pattern), /*startpos=*/0, + /*endpos=*/current_pattern.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { return absl::InvalidArgumentError("Pattern not match"); } - std::string rewritten_url; - + std::string new_path; for (const envoy::extensions::pattern_template::PatternTemplateRewriteSegments::RewriteSegment& - segment : rewrite_pattern.segments()) { + segment : rewrite_pattern_proto.segments()) { if (segment.has_literal()) { - absl::StrAppend(&rewritten_url, segment.literal()); + absl::StrAppend(&new_path, segment.literal()); } else if (segment.has_var_index()) { if (segment.var_index() < 1 || segment.var_index() >= capture_num) { return absl::InvalidArgumentError("Invalid variable index"); } - absl::StrAppend(&rewritten_url, absl::string_view(captures[segment.var_index()].as_string())); + absl::StrAppend(&new_path, absl::string_view(captures[segment.var_index()].as_string())); } } - return rewritten_url; + return absl::StatusOr(new_path); } } // namespace Rewrite diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 7e22c83c94950..f98335a814652 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -42,13 +42,6 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { absl::string_view name() const override { return NAME; } private: - // Returns the rewritten URL path based on the given parsed rewrite pattern. - // Used for template-based URL rewrite. - absl::StatusOr rewriteURLTemplatePattern( - absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern) - const; - std::string url_rewrite_pattern_{nullptr}; }; diff --git a/test/extensions/path/rewrite/pattern_template/config_test.cc b/test/extensions/path/rewrite/pattern_template/config_test.cc index e41bbc34aa9de..068fd5e60790a 100644 --- a/test/extensions/path/rewrite/pattern_template/config_test.cc +++ b/test/extensions/path/rewrite/pattern_template/config_test.cc @@ -99,6 +99,36 @@ TEST(ConfigTest, TestConfigSetup) { EXPECT_TRUE(config_or_error.ok()); } +TEST(ConfigTest, TestInvalidConfigSetup) { + const std::string yaml_string = R"EOF( + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/bar/{lang}/country}" +)EOF"; + + envoy::config::core::v3::TypedExtensionConfig config; + TestUtility::loadFromYaml(yaml_string, config); + + const auto& factory = + &Envoy::Config::Utility::getAndCheckFactory(config); + + EXPECT_NE(nullptr, factory); + EXPECT_EQ(factory->name(), + "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + + auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( + config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); + + EXPECT_NE(nullptr, message); + + absl::StatusOr config_or_error = + factory->createPathRewritePredicate(*message); + + EXPECT_FALSE(config_or_error.ok()); + EXPECT_EQ(config_or_error.message(), "path_rewrite_policy.path_template_rewrite /bar/{lang}/country} is invalid""); +} + } // namespace Rewrite } // namespace PatternTemplate } // namespace Extensions diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc index daffd1138c240..af3d43a2d74a7 100644 --- a/test/extensions/path/rewrite/pattern_template/library_test.cc +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -73,6 +73,20 @@ TEST(RewriteTest, BasicUsage) { "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); } +TEST(RewriteTest, RewriteInvalidRegex) { + const std::string yaml_string = R"EOF( + name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + path_template_rewrite: "/bar/{lang}/{country}" +)EOF"; + + Router::PathRewritePredicateSharedPtr predicate = createRewritePredicateFromYaml(yaml_string); + absl::StatusOr rewrite_or_error = predicate->rewriteUrl("/bar/en/usa", "/bar/invalid}/{lang}"); + EXPECT_FALSE(rewrite_or_error.ok()); + EXPECT_EQ(rewrite_or_error.status().message(), "Unable to parse url pattern regex"); +} + TEST(RewriteTest, MatchPatternValidation) { const std::string rewrite_yaml_string = R"EOF( name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate From c65868c3f3f96f2860e1f12d1ec14ceefd4628c4 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 12 Aug 2022 14:02:07 +0000 Subject: [PATCH 138/238] Small changes for comments Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 12 ++++++------ source/common/router/config_impl.h | 10 ++-------- .../path/pattern_template_lib/pattern_template.cc | 5 ++--- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index bc3c92cb0ade6..d963f5a9b361c 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -359,8 +359,8 @@ PathRewritePolicyImpl::PathRewritePolicyImpl() : enabled_(false){}; PathRewritePolicyImpl::PathRewritePolicyImpl( const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string route_url) - : route_url_(route_url), enabled_(true) { + ProtobufMessage::ValidationVisitor& validator) + : enabled_(true) { const auto& predicate_factory = &Envoy::Config::Utility::getAndCheckFactory(typed_config); @@ -387,8 +387,8 @@ PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; PathMatchPolicyImpl::PathMatchPolicyImpl( const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string path) - : path_(path), enabled_(true) { + ProtobufMessage::ValidationVisitor& validator) + : enabled_(true) { const auto& predicate_factory = &Envoy::Config::Utility::getAndCheckFactory(typed_config); @@ -1243,7 +1243,7 @@ PathRewritePolicyImpl RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { if (route.route().has_path_rewrite_policy()) { - return PathRewritePolicyImpl{route.route().path_rewrite_policy(), validator, route.name()}; + return PathRewritePolicyImpl{route.route().path_rewrite_policy(), validator}; } return {}; } @@ -1252,7 +1252,7 @@ PathMatchPolicyImpl RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { if (route.match().has_path_match_policy()) { - return PathMatchPolicyImpl{route.match().path_match_policy(), validator, route.name()}; + return PathMatchPolicyImpl{route.match().path_match_policy(), validator}; } return {}; diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index a44351364ba97..421209abbf8f4 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -464,7 +464,7 @@ class RouteTracingImpl : public RouteTracing { class PathMatchPolicyImpl : public PathMatchPolicy { public: PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string path); + ProtobufMessage::ValidationVisitor& validator); // Default constructor that disables internal redirect. PathMatchPolicyImpl(); @@ -473,10 +473,7 @@ class PathMatchPolicyImpl : public PathMatchPolicy { PathMatchPredicateSharedPtr predicate() const override; - std::string path() const { return path_; } - private: - const std::string path_; const bool enabled_; ProtobufTypes::MessagePtr predicate_config_; PathMatchPredicateSharedPtr predicate_; @@ -489,7 +486,7 @@ class PathMatchPolicyImpl : public PathMatchPolicy { class PathRewritePolicyImpl : public PathRewritePolicy { public: PathRewritePolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator, std::string route_url); + ProtobufMessage::ValidationVisitor& validator); // Default constructor that disables internal redirect. PathRewritePolicyImpl(); @@ -498,10 +495,7 @@ class PathRewritePolicyImpl : public PathRewritePolicy { PathRewritePredicateSharedPtr predicate() const override; - std::string routeUrl() const { return route_url_; } - private: - const std::string route_url_; const bool enabled_; ProtobufTypes::MessagePtr predicate_config_; PathRewritePredicateSharedPtr predicate_; diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc index 860920b8f89c7..89ee12fa1a1e0 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -34,8 +34,7 @@ absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url if (!status.ok()) { return status.status(); } - struct ParsedUrlPattern pattern = *std::move(status); - return PatternTemplateInternal::toRegexPattern(pattern); + return PatternTemplateInternal::toRegexPattern(*status); } absl::StatusOr> @@ -146,7 +145,7 @@ absl::StatusOr rewriteURLTemplatePattern( std::vector captures(capture_num); if (!regex.Match(PatternTemplateInternal::toStringPiece(url), /*startpos=*/0, /*endpos=*/url.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { - return absl::InvalidArgumentError("Pattern not match"); + return absl::InvalidArgumentError("Pattern does not match"); } std::string rewritten_url; From 8d9f034f88e6fb7fdddabb057a027ad3a6b0cc49 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 12 Aug 2022 14:39:47 +0000 Subject: [PATCH 139/238] Small reorder Signed-off-by: silverstar195 --- .../path/pattern_template_lib/pattern_template.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc index 89ee12fa1a1e0..c40620b618005 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -41,17 +41,18 @@ absl::StatusOr> parseRewritePatternHelper(absl::string_view pattern) { std::vector result; - // Don't allow contiguous '/' patterns. - static const LazyRE2 invalid_regex = {"^.*//.*$"}; - if (RE2::FullMatch(toStringPiece(pattern), *invalid_regex)) { - return absl::InvalidArgumentError("Invalid rewrite literal pattern"); - } - // The pattern should start with a '/' and thus the first segment should // always be a literal. if (pattern.empty() || pattern[0] != '/') { return absl::InvalidArgumentError("Invalid rewrite variable placement"); } + + // Don't allow contiguous '/' patterns. + static const LazyRE2 invalid_regex = {"^.*//.*$"}; + if (RE2::FullMatch(toStringPiece(pattern), *invalid_regex)) { + return absl::InvalidArgumentError("Invalid rewrite literal"); + } + while (!pattern.empty()) { std::vector segments1 = absl::StrSplit(pattern, absl::MaxSplits('{', 1)); if (!segments1[0].empty()) { @@ -149,7 +150,6 @@ absl::StatusOr rewriteURLTemplatePattern( } std::string rewritten_url; - for (const envoy::extensions::pattern_template::PatternTemplateRewriteSegments::RewriteSegment& segment : rewrite_pattern.segments()) { if (segment.has_literal()) { From 081cf8e65d5642c95d4dd84d7799ac389be1f3e6 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 12 Aug 2022 14:43:38 +0000 Subject: [PATCH 140/238] Added comment Signed-off-by: silverstar195 --- envoy/router/path_rewrite_policy.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite_policy.h index 2e921bb1d9d29..3189ae9072a34 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite_policy.h @@ -23,7 +23,8 @@ class PathRewritePredicate : Logger::Loggable { /** * Used to determine if the match policy is compatible. * - * @param match_policy current path match policy for route + * @param path_match_policy current path match policy for route + * @param active_policy true if user provided policy * @return true if current path match policy is acceptable */ virtual absl::Status isCompatibleMatchPolicy(PathMatchPredicateSharedPtr path_match_policy, From 8ff74df256d410c27f548508f765bfcd1629c687 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 12 Aug 2022 15:05:41 +0000 Subject: [PATCH 141/238] Added commit Signed-off-by: silverstar195 --- test/extensions/path/rewrite/pattern_template/library_test.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc index af3d43a2d74a7..07d2d189cd101 100644 --- a/test/extensions/path/rewrite/pattern_template/library_test.cc +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -82,7 +82,8 @@ TEST(RewriteTest, RewriteInvalidRegex) { )EOF"; Router::PathRewritePredicateSharedPtr predicate = createRewritePredicateFromYaml(yaml_string); - absl::StatusOr rewrite_or_error = predicate->rewriteUrl("/bar/en/usa", "/bar/invalid}/{lang}"); + absl::StatusOr rewrite_or_error = + predicate->rewriteUrl("/bar/en/usa", "/bar/invalid}/{lang}"); EXPECT_FALSE(rewrite_or_error.ok()); EXPECT_EQ(rewrite_or_error.status().message(), "Unable to parse url pattern regex"); } From b11c7e3cee619ae42559e6d13b6da8d3dacebfe1 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 12 Aug 2022 15:05:52 +0000 Subject: [PATCH 142/238] Added commit Signed-off-by: silverstar195 --- .../rewrite/pattern_template/pattern_template_rewrite.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index 3cac40057f640..ad8dbab0cfff2 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -60,8 +60,9 @@ PatternTemplateRewritePredicate::rewriteUrl(absl::string_view current_pattern, return absl::InvalidArgumentError("Unable to parse url rewrite pattern"); } - const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern_proto = *std::move(rewrite_pattern); - RE2 regex = RE2(PatternTemplateInternal::toStringPiece(regex_pattern_str)); + const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern_proto = + *std::move(rewrite_pattern); + RE2 regex = RE2(PatternTemplateInternal::toStringPiece(regex_pattern_str)); if (!regex.ok()) { return absl::InternalError(regex.error()); } @@ -70,7 +71,8 @@ PatternTemplateRewritePredicate::rewriteUrl(absl::string_view current_pattern, int capture_num = regex.NumberOfCapturingGroups() + 1; std::vector captures(capture_num); if (!regex.Match(PatternTemplateInternal::toStringPiece(current_pattern), /*startpos=*/0, - /*endpos=*/current_pattern.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { + /*endpos=*/current_pattern.size(), RE2::ANCHOR_BOTH, captures.data(), + captures.size())) { return absl::InvalidArgumentError("Pattern not match"); } From da94ae2c382e56d125b32a38bc460d433e9ef12d Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 12 Aug 2022 15:21:08 +0000 Subject: [PATCH 143/238] Rename path_match_policy and path_rewrite_policy Signed-off-by: silverstar195 --- envoy/router/BUILD | 4 ++-- envoy/router/{path_match_policy.h => path_match.h} | 0 envoy/router/{path_rewrite_policy.h => path_rewrite.h} | 2 +- envoy/router/router.h | 4 ++-- source/extensions/path/match/pattern_template/config.cc | 2 +- source/extensions/path/match/pattern_template/config.h | 2 +- .../path/match/pattern_template/pattern_template_match.h | 2 +- .../extensions/path/pattern_template_lib/pattern_template.h | 2 +- source/extensions/path/rewrite/pattern_template/config.cc | 2 +- source/extensions/path/rewrite/pattern_template/config.h | 2 +- .../path/rewrite/pattern_template/pattern_template_rewrite.h | 4 ++-- 11 files changed, 13 insertions(+), 13 deletions(-) rename envoy/router/{path_match_policy.h => path_match.h} (100%) rename envoy/router/{path_rewrite_policy.h => path_rewrite.h} (98%) diff --git a/envoy/router/BUILD b/envoy/router/BUILD index 0b2f8365ad1fb..4c37e0f3397b8 100644 --- a/envoy/router/BUILD +++ b/envoy/router/BUILD @@ -149,7 +149,7 @@ envoy_cc_library( envoy_cc_library( name = "router_path_match_policy_interface", - hdrs = ["path_match_policy.h"], + hdrs = ["path_match.h"], deps = [ "//envoy/config:typed_config_interface", "//source/common/common:minimal_logger_lib", @@ -160,7 +160,7 @@ envoy_cc_library( envoy_cc_library( name = "router_path_rewrite_policy_interface", - hdrs = ["path_rewrite_policy.h"], + hdrs = ["path_rewrite.h"], deps = [ "//envoy/config:typed_config_interface", "//source/common/common:minimal_logger_lib", diff --git a/envoy/router/path_match_policy.h b/envoy/router/path_match.h similarity index 100% rename from envoy/router/path_match_policy.h rename to envoy/router/path_match.h diff --git a/envoy/router/path_rewrite_policy.h b/envoy/router/path_rewrite.h similarity index 98% rename from envoy/router/path_rewrite_policy.h rename to envoy/router/path_rewrite.h index 3189ae9072a34..5210c217d3e88 100644 --- a/envoy/router/path_rewrite_policy.h +++ b/envoy/router/path_rewrite.h @@ -1,7 +1,7 @@ #pragma once #include "envoy/config/typed_config.h" -#include "envoy/router/path_match_policy.h" +#include "envoy/router/path_match.h" #include "source/common/common/logger.h" diff --git a/envoy/router/router.h b/envoy/router/router.h index 8e8a670de40d3..4d987fac9628d 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -20,8 +20,8 @@ #include "envoy/http/hash_policy.h" #include "envoy/rds/config.h" #include "envoy/router/internal_redirect.h" -#include "envoy/router/path_match_policy.h" -#include "envoy/router/path_rewrite_policy.h" +#include "envoy/router/path_match.h" +#include "envoy/router/path_rewrite.h" #include "envoy/tcp/conn_pool.h" #include "envoy/tracing/http_tracer.h" #include "envoy/type/v3/percent.pb.h" diff --git a/source/extensions/path/match/pattern_template/config.cc b/source/extensions/path/match/pattern_template/config.cc index c425c4c6ba6c1..d89f1841f1a16 100644 --- a/source/extensions/path/match/pattern_template/config.cc +++ b/source/extensions/path/match/pattern_template/config.cc @@ -1,7 +1,7 @@ #include "source/extensions/path/match/pattern_template/config.h" #include "envoy/registry/registry.h" -#include "envoy/router/path_match_policy.h" +#include "envoy/router/path_match.h" namespace Envoy { namespace Extensions { diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index fca213be90733..8a316309146ad 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -2,7 +2,7 @@ #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" -#include "envoy/router/path_match_policy.h" +#include "envoy/router/path_match.h" #include "source/common/protobuf/message_validator_impl.h" #include "source/common/protobuf/utility.h" diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index 748cc44cc3841..9c32af4f042c8 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -3,7 +3,7 @@ #include #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" -#include "envoy/router/path_match_policy.h" +#include "envoy/router/path_match.h" #include "source/extensions/path/pattern_template_lib/pattern_template.h" diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 593f1fe3f5a40..263fc7cabe02d 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -2,7 +2,7 @@ #include -#include "envoy/router/path_match_policy.h" +#include "envoy/router/path_match.h" #include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" #include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" diff --git a/source/extensions/path/rewrite/pattern_template/config.cc b/source/extensions/path/rewrite/pattern_template/config.cc index 894589f741763..762d1da56098a 100644 --- a/source/extensions/path/rewrite/pattern_template/config.cc +++ b/source/extensions/path/rewrite/pattern_template/config.cc @@ -1,7 +1,7 @@ #include "source/extensions/path/rewrite/pattern_template/config.h" #include "envoy/registry/registry.h" -#include "envoy/router/path_rewrite_policy.h" +#include "envoy/router/path_rewrite.h" namespace Envoy { namespace Extensions { diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 9861ff0bf0fec..491cac7e87760 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -2,7 +2,7 @@ #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" -#include "envoy/router/path_rewrite_policy.h" +#include "envoy/router/path_rewrite.h" #include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index f98335a814652..13afcd7555b23 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -6,8 +6,8 @@ #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" -#include "envoy/router/path_match_policy.h" -#include "envoy/router/path_rewrite_policy.h" +#include "envoy/router/path_match.h" +#include "envoy/router/path_rewrite.h" #include "source/common/protobuf/message_validator_impl.h" #include "source/common/protobuf/utility.h" From 531444165d932061ee78b1d5305072b63a21b066 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 12 Aug 2022 17:07:56 +0000 Subject: [PATCH 144/238] Formating Signed-off-by: silverstar195 --- test/extensions/path/rewrite/pattern_template/config_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/path/rewrite/pattern_template/config_test.cc b/test/extensions/path/rewrite/pattern_template/config_test.cc index 068fd5e60790a..eea90bb2fb5b1 100644 --- a/test/extensions/path/rewrite/pattern_template/config_test.cc +++ b/test/extensions/path/rewrite/pattern_template/config_test.cc @@ -126,7 +126,7 @@ TEST(ConfigTest, TestInvalidConfigSetup) { factory->createPathRewritePredicate(*message); EXPECT_FALSE(config_or_error.ok()); - EXPECT_EQ(config_or_error.message(), "path_rewrite_policy.path_template_rewrite /bar/{lang}/country} is invalid""); + EXPECT_EQ(config_or_error.status().message(), "path_rewrite_policy.path_template_rewrite /bar/{lang}/country} is invalid"); } } // namespace Rewrite From 606932da6cccbbdc54f7033e9ee7285ac70116ad Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 13:52:48 +0000 Subject: [PATCH 145/238] added config_test Signed-off-by: silverstar195 --- test/extensions/path/rewrite/pattern_template/config_test.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/extensions/path/rewrite/pattern_template/config_test.cc b/test/extensions/path/rewrite/pattern_template/config_test.cc index eea90bb2fb5b1..fea978a77fd87 100644 --- a/test/extensions/path/rewrite/pattern_template/config_test.cc +++ b/test/extensions/path/rewrite/pattern_template/config_test.cc @@ -126,7 +126,8 @@ TEST(ConfigTest, TestInvalidConfigSetup) { factory->createPathRewritePredicate(*message); EXPECT_FALSE(config_or_error.ok()); - EXPECT_EQ(config_or_error.status().message(), "path_rewrite_policy.path_template_rewrite /bar/{lang}/country} is invalid"); + EXPECT_EQ(config_or_error.status().message(), + "path_rewrite_policy.path_template_rewrite /bar/{lang}/country} is invalid"); } } // namespace Rewrite From 7770a357589e105c1cd6628676d54555b046282e Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 14:52:55 +0000 Subject: [PATCH 146/238] Match and Rewrite Library Signed-off-by: silverstar195 --- .../path/pattern_template_lib/BUILD | 49 ++ .../pattern_template_lib/pattern_template.cc | 170 +++++++ .../pattern_template_lib/pattern_template.h | 57 +++ .../pattern_template_internal.cc | 387 +++++++++++++++ .../pattern_template_internal.h | 145 ++++++ .../path/pattern_template_lib/proto/BUILD | 17 + .../pattern_template_rewrite_segments.proto | 29 ++ .../path/pattern_template_lib/BUILD | 38 ++ .../pattern_template_internal_test.cc | 468 ++++++++++++++++++ .../pattern_template_test.cc | 337 +++++++++++++ 10 files changed, 1697 insertions(+) create mode 100644 source/extensions/path/pattern_template_lib/BUILD create mode 100644 source/extensions/path/pattern_template_lib/pattern_template.cc create mode 100644 source/extensions/path/pattern_template_lib/pattern_template.h create mode 100644 source/extensions/path/pattern_template_lib/pattern_template_internal.cc create mode 100644 source/extensions/path/pattern_template_lib/pattern_template_internal.h create mode 100644 source/extensions/path/pattern_template_lib/proto/BUILD create mode 100644 source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto create mode 100644 test/extensions/path/pattern_template_lib/BUILD create mode 100644 test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc create mode 100644 test/extensions/path/pattern_template_lib/pattern_template_test.cc diff --git a/source/extensions/path/pattern_template_lib/BUILD b/source/extensions/path/pattern_template_lib/BUILD new file mode 100644 index 0000000000000..dfb084bbc72e2 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/BUILD @@ -0,0 +1,49 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_library", + "envoy_extension_package", +) + +licenses(["notice"]) # Apache 2 + +# Wildcard & Pattern Matching + +envoy_extension_package() + +envoy_cc_library( + name = "pattern_template_lib", + srcs = ["pattern_template.cc"], + hdrs = ["pattern_template.h"], + visibility = [ + "//source/common/router:__subpackages__", + "//source/extensions/path:__subpackages__", + "//test/extensions/path:__subpackages__", + ], + deps = [ + ":pattern_template_internal_cc", + "//source/common/http:path_utility_lib", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_googlesource_code_re2//:re2", + ], +) + +envoy_cc_library( + name = "pattern_template_internal_cc", + srcs = ["pattern_template_internal.cc"], + hdrs = ["pattern_template_internal.h"], + deps = [ + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/functional:function_ref", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", + "@com_google_absl//absl/types:variant", + "@com_googlesource_code_re2//:re2", + ], +) diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc new file mode 100644 index 0000000000000..c40620b618005 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -0,0 +1,170 @@ +#include "source/extensions/path/pattern_template_lib/pattern_template.h" + +#include +#include +#include +#include + +#include "source/common/http/path_utility.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" + +#include "absl/status/statusor.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "re2/re2.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +using PatternTemplateInternal::ParsedUrlPattern; + +#ifndef SWIG +// Silence warnings about missing initializers for members of LazyRE2. +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } + +absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { + + absl::StatusOr status = + PatternTemplateInternal::parseURLPatternSyntax(url_pattern); + if (!status.ok()) { + return status.status(); + } + return PatternTemplateInternal::toRegexPattern(*status); +} + +absl::StatusOr> +parseRewritePatternHelper(absl::string_view pattern) { + std::vector result; + + // The pattern should start with a '/' and thus the first segment should + // always be a literal. + if (pattern.empty() || pattern[0] != '/') { + return absl::InvalidArgumentError("Invalid rewrite variable placement"); + } + + // Don't allow contiguous '/' patterns. + static const LazyRE2 invalid_regex = {"^.*//.*$"}; + if (RE2::FullMatch(toStringPiece(pattern), *invalid_regex)) { + return absl::InvalidArgumentError("Invalid rewrite literal"); + } + + while (!pattern.empty()) { + std::vector segments1 = absl::StrSplit(pattern, absl::MaxSplits('{', 1)); + if (!segments1[0].empty()) { + if (!PatternTemplateInternal::isValidRewriteLiteral(segments1[0])) { + return absl::InvalidArgumentError("Invalid rewrite literal pattern"); + } + result.emplace_back(segments1[0], RewriteStringKind::KLiteral); + } + + if (segments1.size() < 2) { + // No more variable replacement, done. + break; + } + + std::vector segments2 = + absl::StrSplit(segments1[1], absl::MaxSplits('}', 1)); + if (segments2.size() < 2) { + return absl::InvalidArgumentError("Unmatched variable bracket"); + } + pattern = segments2[1]; + + if (!PatternTemplateInternal::isValidIndent(segments2[0])) { + return absl::InvalidArgumentError("Invalid variable name"); + } + result.emplace_back(segments2[0], RewriteStringKind::KVariable); + } + return result; +} + +absl::StatusOr +parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { + envoy::extensions::pattern_template::PatternTemplateRewriteSegments parsed_pattern; + RE2 regex = RE2(toStringPiece(capture_regex)); + if (!regex.ok()) { + return absl::InternalError(regex.error()); + } + + absl::StatusOr> status = parseRewritePatternHelper(pattern); + if (!status.ok()) { + return status.status(); + } + std::vector processed_pattern = *std::move(status); + + const std::map& capture_index_map = regex.NamedCapturingGroups(); + + for (const auto& [str, kind] : processed_pattern) { + switch (kind) { + case RewriteStringKind::KLiteral: + parsed_pattern.add_segments()->set_literal(std::string(str)); + break; + case RewriteStringKind::KVariable: + auto it = capture_index_map.find(std::string(str)); + if (it == capture_index_map.end()) { + return absl::InvalidArgumentError("Nonexisting variable name"); + } + parsed_pattern.add_segments()->set_var_index(it->second); + break; + } + } + + return parsed_pattern; +} + +absl::Status isValidMatchPattern(const std::string path_template_match) { + return convertURLPatternSyntaxToRegex(path_template_match).status(); +} + +absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { + return parseRewritePatternHelper(path_template_rewrite).status(); +} + +absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, + const std::string& capture_regex) { + absl::StatusOr status = convertURLPatternSyntaxToRegex(capture_regex).value(); + if (!status.ok()) { + return status.status(); + } + return parseRewritePattern(path_template_rewrite, *std::move(status)).status(); +} + +absl::StatusOr rewriteURLTemplatePattern( + absl::string_view url, absl::string_view capture_regex, + const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern) { + RE2 regex = RE2(PatternTemplateInternal::toStringPiece(capture_regex)); + if (!regex.ok()) { + return absl::InternalError(regex.error()); + } + + // First capture is the whole matched regex pattern. + int capture_num = regex.NumberOfCapturingGroups() + 1; + std::vector captures(capture_num); + if (!regex.Match(PatternTemplateInternal::toStringPiece(url), /*startpos=*/0, + /*endpos=*/url.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { + return absl::InvalidArgumentError("Pattern does not match"); + } + + std::string rewritten_url; + for (const envoy::extensions::pattern_template::PatternTemplateRewriteSegments::RewriteSegment& + segment : rewrite_pattern.segments()) { + if (segment.has_literal()) { + absl::StrAppend(&rewritten_url, segment.literal()); + } else if (segment.has_var_index()) { + if (segment.var_index() < 1 || segment.var_index() >= capture_num) { + return absl::InvalidArgumentError("Invalid variable index"); + } + absl::StrAppend(&rewritten_url, absl::string_view(captures[segment.var_index()].as_string())); + } + } + + return rewritten_url; +} + +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h new file mode 100644 index 0000000000000..82753ade6ac67 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -0,0 +1,57 @@ +#pragma once + +#include + +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" +#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" + +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +enum class RewriteStringKind { KVariable, KLiteral }; + +struct RewritePatternSegment { + RewritePatternSegment(absl::string_view segment_value, RewriteStringKind kind) + : segment_value(segment_value), kind(kind) {} + absl::string_view segment_value; + RewriteStringKind kind; +}; + +// Returns the regex pattern that is equivalent to the given url_pattern. +// Used in the config pipeline to translate user given url pattern to +// the safe regex Envoy can understand. Strips away any variable captures. +absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); + +// Helper function that parses the pattern and breaks it down to either +// literals or variable names. To be used by ParseRewritePattern(). +// Exposed here so that the validator for the rewrite pattern can also +// use it. +absl::StatusOr> +parseRewritePatternHelper(absl::string_view pattern); + +// Returns the parsed Url rewrite pattern to be used by +// RewriteURLTemplatePattern() |capture_regex| should +// be the regex generated by ConvertURLPatternSyntaxToRegex(). +absl::StatusOr +parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); + +// Returns if provided rewrite pattern is valid +absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); + +// Returns if path_template and rewrite_template have valid variables +absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, + const std::string& capture_regex); + +absl::Status isValidMatchPattern(const std::string match_pattern); + +absl::StatusOr rewriteURLTemplatePattern( + absl::string_view url, absl::string_view capture_regex, + const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern); + +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc new file mode 100644 index 0000000000000..945741885cea9 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc @@ -0,0 +1,387 @@ +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" + +#include +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/flags/flag.h" +#include "absl/functional/function_ref.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_replace.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" +#include "absl/types/variant.h" +#include "re2/re2.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +namespace PatternTemplateInternal { + +namespace { + +#ifndef SWIG +// Silence warnings about missing initializers for members of LazyRE2. +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +constexpr unsigned long kPatternMatchingMaxVariablesPerUrl = 5; +constexpr unsigned long kPatternMatchingMaxVariableNameLen = 16; +constexpr unsigned long kPatternMatchingMinVariableNameLen = 1; + +// Valid pchar from https://datatracker.ietf.org/doc/html/rfc3986#appendix-A +constexpr absl::string_view kLiteral = "a-zA-Z0-9-._~" // Unreserved + "%" // pct-encoded + "!$&'()+,;" // sub-delims excluding *= + ":@"; + +// Default operator used for the variable when none specified. +constexpr Operator kDefaultVariableOperator = Operator::KPathGlob; + +// Visitor for displaying debug info of a ParsedSegment/Variable.var_match. +struct ToStringVisitor { + template std::string operator()(const T& val) const; +}; + +// Formatter used to allow joining variants together with StrJoin. +struct ToStringFormatter { + template void operator()(std::string* out, const T& t) const { + absl::StrAppend(out, absl::visit(ToStringVisitor(), t)); + } +}; + +// Visitor for converting a ParsedSegment variant to the regex. +struct ToRegexPatternVisitor { + template std::string operator()(const T& val) const { return toRegexPattern(val); } +}; + +// Formatter used to allow joining variants together with StrJoin. +struct ToRegexPatternFormatter { + template void operator()(std::string* out, const T& t) const { + absl::StrAppend(out, absl::visit(ToRegexPatternVisitor(), t)); + } +}; + +std::string toString(const Literal val) { return std::string(val); } + +std::string toString(const Operator val) { + switch (val) { + case Operator::KPathGlob: + return "*"; + case Operator::KTextGlob: + return "**"; + } + return ""; +} + +std::string toString(const Variable val) { + if (val.var_match.empty()) { + return absl::StrCat("{", val.var_name, "}"); + } + + return absl::StrCat("{", val.var_name, "=", + absl::StrJoin(val.var_match, "/", ToStringFormatter()), "}"); + return ""; +} + +template std::string ToStringVisitor::operator()(const T& val) const { + return toString(val); +} + +template +absl::StatusOr alsoUpdatePattern( + absl::FunctionRef>(absl::string_view)> consume_func, + absl::string_view* patt) { + + absl::StatusOr> status = consume_func(*patt); + if (!status.ok()) { + return status.status(); + } + ParsedResult result = *std::move(status); + + *patt = result.unconsumed_pattern; + return result.parsed_value; +} + +} // namespace + +std::string Variable::debugString() const { return toString(*this); } + +std::string ParsedUrlPattern::debugString() const { + return absl::StrCat("/", absl::StrJoin(parsed_segments, "/", ToStringFormatter()), + suffix.value_or("")); +} + +bool isValidLiteral(absl::string_view pattern) { + static const std::string* kValidLiteralRegex = + new std::string(absl::StrCat("^[", kLiteral, "]+$")); + static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; + return RE2::FullMatch(toStringPiece(pattern), *literal_regex); +} + +bool isValidRewriteLiteral(absl::string_view pattern) { + static const std::string* kValidLiteralRegex = + new std::string(absl::StrCat("^[", kLiteral, "/]+$")); + static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; + return RE2::FullMatch(toStringPiece(pattern), *literal_regex); +} + +bool isValidIndent(absl::string_view pattern) { + static const LazyRE2 ident_regex = {"^[a-zA-Z][a-zA-Z0-9_]*$"}; + return RE2::FullMatch(toStringPiece(pattern), *ident_regex); +} + +absl::StatusOr> consumeLiteral(absl::string_view pattern) { + absl::string_view lit = + std::vector(absl::StrSplit(pattern, absl::MaxSplits('/', 1)))[0]; + absl::string_view unconsumed_pattern = pattern.substr(lit.size()); + if (!isValidLiteral(lit)) { + return absl::InvalidArgumentError("Invalid literal"); + } + return ParsedResult(lit, unconsumed_pattern); +} + +absl::StatusOr> consumeOperator(absl::string_view pattern) { + if (absl::StartsWith(pattern, "**")) { + return ParsedResult(Operator::KTextGlob, pattern.substr(2)); + } + if (absl::StartsWith(pattern, "*")) { + return ParsedResult(Operator::KPathGlob, pattern.substr(1)); + } + return absl::InvalidArgumentError("Invalid Operator"); +} + +absl::StatusOr> consumeVariable(absl::string_view pattern) { + // Locate the variable pattern to parse. + if (pattern.size() < 2 || (pattern)[0] != '{') { + return absl::InvalidArgumentError("Invalid variable"); + } + std::vector parts = absl::StrSplit(pattern.substr(1), absl::MaxSplits('}', 1)); + if (parts.size() != 2) { + return absl::InvalidArgumentError("Unmatched variable bracket"); + } + absl::string_view unconsumed_pattern = parts[1]; + + // Parse the actual variable pattern, starting with the variable name. + std::vector var_parts = absl::StrSplit(parts[0], absl::MaxSplits('=', 1)); + if (!isValidIndent(var_parts[0])) { + return absl::InvalidArgumentError("Invalid variable name"); + } + Variable var = Variable(var_parts[0], {}); + + // Parse the variable match pattern (if any). + if (var_parts.size() < 2) { + return ParsedResult(var, unconsumed_pattern); + } + absl::string_view var_patt = var_parts[1]; + if (var_patt.empty()) { + return absl::InvalidArgumentError("Empty variable match"); + } + while (!var_patt.empty()) { + absl::variant var_match; + if (var_patt[0] == '*') { + + absl::StatusOr status = alsoUpdatePattern(consumeOperator, &var_patt); + if (!status.ok()) { + return status.status(); + } + var_match = *std::move(status); + + } else { + + absl::StatusOr status = alsoUpdatePattern(consumeLiteral, &var_patt); + if (!status.ok()) { + return status.status(); + } + var_match = *std::move(status); + } + var.var_match.push_back(var_match); + if (!var_patt.empty()) { + if (var_patt[0] != '/' || var_patt.size() == 1) { + return absl::InvalidArgumentError("Invalid variable match"); + } + var_patt = var_patt.substr(1); + } + } + + return ParsedResult(var, unconsumed_pattern); +} + +absl::StatusOr> +gatherCaptureNames(struct ParsedUrlPattern pattern) { + absl::flat_hash_set captured_variables; + + for (const ParsedSegment& segment : pattern.parsed_segments) { + if (!absl::holds_alternative(segment)) { + continue; + } + if (captured_variables.size() >= kPatternMatchingMaxVariablesPerUrl) { + return absl::InvalidArgumentError("Exceeded variable count limit"); + } + absl::string_view var_name = absl::get(segment).var_name; + + if (var_name.size() < kPatternMatchingMinVariableNameLen || + var_name.size() > kPatternMatchingMaxVariableNameLen) { + return absl::InvalidArgumentError("Invalid variable length"); + } + if (captured_variables.contains(var_name)) { + return absl::InvalidArgumentError("Repeated variable name"); + } + captured_variables.emplace(var_name); + } + + return captured_variables; +} + +absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { + bool seen_text_glob = false; + for (const ParsedSegment& segment : pattern.parsed_segments) { + if (absl::holds_alternative(segment)) { + if (seen_text_glob) { + return absl::InvalidArgumentError("Glob after text glob."); + } + seen_text_glob = (absl::get(segment) == Operator::KTextGlob); + } else if (absl::holds_alternative(segment)) { + const Variable& var = absl::get(segment); + if (var.var_match.empty()) { + if (seen_text_glob) { + // A variable with no explicit matcher is treated as a path glob. + return absl::InvalidArgumentError("Implicit variable path glob after text glob."); + } + } else { + for (const absl::variant& var_seg : var.var_match) { + if (!absl::holds_alternative(var_seg)) { + continue; + } + if (seen_text_glob) { + return absl::InvalidArgumentError("Glob after text glob."); + } + seen_text_glob = (absl::get(var_seg) == Operator::KTextGlob); + } + } + } + } + return absl::OkStatus(); +} + +absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern) { + struct ParsedUrlPattern parsed_pattern; + + static const LazyRE2 printable_regex = {"^/[[:graph:]]*$"}; + if (!RE2::FullMatch(toStringPiece(url_pattern), *printable_regex)) { + + return absl::InvalidArgumentError("Invalid pattern"); + } + + // Consume the leading '/' + url_pattern = url_pattern.substr(1); + + // Do the initial lexical parsing. + while (!url_pattern.empty()) { + ParsedSegment segment; + if (url_pattern[0] == '*') { + + absl::StatusOr status = alsoUpdatePattern(consumeOperator, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } else if (url_pattern[0] == '{') { + + absl::StatusOr status = alsoUpdatePattern(consumeVariable, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } else { + + absl::StatusOr status = alsoUpdatePattern(consumeLiteral, &url_pattern); + if (!status.ok()) { + return status.status(); + } + segment = *std::move(status); + } + parsed_pattern.parsed_segments.push_back(segment); + + // Deal with trailing '/' or suffix. + if (!url_pattern.empty()) { + if (url_pattern == "/") { + // Single trailing '/' at the end, mark this with empty literal. + parsed_pattern.parsed_segments.emplace_back(""); + break; + } else if (url_pattern[0] == '/') { + // Have '/' followed by more text, consume the '/'. + url_pattern = url_pattern.substr(1); + } else { + // Not followed by '/', treat as suffix. + + absl::StatusOr status = alsoUpdatePattern(consumeLiteral, &url_pattern); + if (!status.ok()) { + return status.status(); + } + parsed_pattern.suffix = *std::move(status); + if (!url_pattern.empty()) { + // Suffix didn't consume whole remaining pattern ('/' in url_pattern). + return absl::InvalidArgumentError("Prefix match not supported."); + } + break; + } + } + } + absl::StatusOr> status = + gatherCaptureNames(parsed_pattern); + if (!status.ok()) { + return status.status(); + } + parsed_pattern.captured_variables = *std::move(status); + + absl::Status validate_status = validateNoOperatorAfterTextGlob(parsed_pattern); + if (!validate_status.ok()) { + return validate_status; + } + + return parsed_pattern; +} + +std::string toRegexPattern(absl::string_view pattern) { + return absl::StrReplaceAll( + pattern, {{"$", "\\$"}, {"(", "\\("}, {")", "\\)"}, {"+", "\\+"}, {".", "\\."}}); +} + +std::string toRegexPattern(Operator pattern) { + static const std::string* kPathGlobRegex = new std::string(absl::StrCat("[", kLiteral, "]+")); + static const std::string* kTextGlobRegex = new std::string(absl::StrCat("[", kLiteral, "/]*")); + switch (pattern) { + case Operator::KPathGlob: // "*" + return *kPathGlobRegex; + case Operator::KTextGlob: // "**" + return *kTextGlobRegex; + } + return ""; +} + +std::string toRegexPattern(const Variable& pattern) { + return absl::StrCat("(?P<", pattern.var_name, ">", + pattern.var_match.empty() + ? toRegexPattern(kDefaultVariableOperator) + : absl::StrJoin(pattern.var_match, "/", ToRegexPatternFormatter()), + ")"); +} + +std::string toRegexPattern(const struct ParsedUrlPattern& pattern) { + return absl::StrCat("/", absl::StrJoin(pattern.parsed_segments, "/", ToRegexPatternFormatter()), + toRegexPattern(pattern.suffix.value_or(""))); +} + +} // namespace PatternTemplateInternal +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h new file mode 100644 index 0000000000000..0277bf54698c4 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.h @@ -0,0 +1,145 @@ +#pragma once + +#include +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" +#include "absl/types/optional.h" +#include "absl/types/variant.h" +#include "re2/re2.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +namespace PatternTemplateInternal { + +using Literal = absl::string_view; + +/** + * Determines what operations to use on the input pattern segment + */ +enum class Operator { KPathGlob, KTextGlob }; + +/** + * Represents a segment of the rewritten URL, including any path segments, + * slash and prefix. + */ +struct RewriteSegment { + + absl::string_view literal; + + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int var_index; +}; + +/** + * Represents a pattern variable. Variables are included in both path match and rewrite paths. + */ +struct Variable { + absl::string_view var_name; + std::vector> var_match; + + Variable(absl::string_view name, std::vector> match) + : var_name(name), var_match(match) {} + + std::string debugString() const; +}; + +using ParsedSegment = absl::variant; + +/** + * Represents the parsed path including literals and variables. + */ +struct ParsedUrlPattern { + std::vector parsed_segments; + absl::optional suffix; + absl::flat_hash_set captured_variables; + + std::string debugString() const; +}; + +/** + * Check if literal is valid + */ +bool isValidLiteral(absl::string_view pattern); + +/** + * Check if rewrite literal is valid + */ +bool isValidRewriteLiteral(absl::string_view pattern); + +/** + * Check if indent is valid + */ +bool isValidIndent(absl::string_view pattern); + +/** + * Used by the following Consume{Literal.Operator,Variable} functions + * in the return value. The functions would take the given pattern, + * parse what it can into |parsed_value| and return the unconsumed + * portion of the pattern in |unconsumed_pattern|. + */ +template struct ParsedResult { + ParsedResult(T val, absl::string_view pattern) : parsed_value(val), unconsumed_pattern(pattern) {} + + T parsed_value; + absl::string_view unconsumed_pattern; +}; + +/** + * Converts input pattern to ParsedResult + */ +absl::StatusOr> consumeLiteral(absl::string_view pattern); + +/** + * Converts input pattern to ParsedResult + */ +absl::StatusOr> consumeOperator(absl::string_view pattern); + +/** + * Converts input pattern to ParsedResult + */ +absl::StatusOr> consumeVariable(absl::string_view pattern); + +/** + * Converts input pattern to ParsedUrlPattern + */ +absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern); + +/** + * Converts Literal to std::string + */ +std::string toRegexPattern(Literal pattern); + +/** + * Converts Operator to std::string + */ +std::string toRegexPattern(Operator pattern); + +/** + * Converts Variable to std::string + */ +std::string toRegexPattern(const Variable& pattern); + +/** + * Converts ParsedUrlPattern to std::string + */ +std::string toRegexPattern(const struct ParsedUrlPattern& pattern); + +/** + * Converts string_view to be used in re2 + */ +inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } + +} // namespace PatternTemplateInternal +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/proto/BUILD b/source/extensions/path/pattern_template_lib/proto/BUILD new file mode 100644 index 0000000000000..15daa9ec85d32 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/proto/BUILD @@ -0,0 +1,17 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_extension_package", + "envoy_proto_library", +) + +licenses(["notice"]) # Apache 2 + +# Wildcard & Pattern Matching Proto + +envoy_extension_package() + +envoy_proto_library( + name = "pattern_template_rewrite_segements", + srcs = ["pattern_template_rewrite_segments.proto"], + deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], +) diff --git a/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto b/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto new file mode 100644 index 0000000000000..14ae540825171 --- /dev/null +++ b/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto @@ -0,0 +1,29 @@ +syntax = "proto3"; + +package envoy.extensions.pattern_template; + +import "udpa/annotations/status.proto"; + +option java_package = "io.envoyproxy.envoy.extensions.pattern_template"; +option java_outer_classname = "PatternTemplateRewrite"; +option java_multiple_files = true; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/pattern_template"; +option (udpa.annotations.file_status).package_version_status = ACTIVE; + +// Holds the segments for rewriting urls base on pattern templates +message PatternTemplateRewriteSegments { + message RewriteSegment { + oneof segment_type { + // Represents a segment of the rewritten URL, including any path segments, + // slash and prefix. + string literal = 1; + + // Represents an index into the RE2 capture which value should be used + // to construct the rewritten URL. Note that the index should be greater + // than 0 as 0 index into the whole match RE2 pattern. + int32 var_index = 2; + } + } + + repeated RewriteSegment segments = 3; +} diff --git a/test/extensions/path/pattern_template_lib/BUILD b/test/extensions/path/pattern_template_lib/BUILD new file mode 100644 index 0000000000000..dcb9aa1326df5 --- /dev/null +++ b/test/extensions/path/pattern_template_lib/BUILD @@ -0,0 +1,38 @@ +load( + "//bazel:envoy_build_system.bzl", + "envoy_cc_test", + "envoy_package", +) + +licenses(["notice"]) # Apache 2 + +envoy_package() + +# Wildcard & Pattern Matching + +envoy_cc_test( + name = "pattern_template_test", + srcs = ["pattern_template_test.cc"], + deps = [ + "//source/extensions/path/pattern_template_lib", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "//test/test_common:status_utility_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + ], +) + +envoy_cc_test( + name = "pattern_template_internal_test", + srcs = ["pattern_template_internal_test.cc"], + deps = [ + "//source/extensions/path/pattern_template_lib:pattern_template_internal_cc", + "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "//test/test_common:status_utility_lib", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_googlesource_code_re2//:re2", + ], +) diff --git a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc new file mode 100644 index 0000000000000..d98809bbb2559 --- /dev/null +++ b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc @@ -0,0 +1,468 @@ +#include +#include +#include +#include +#include +#include + +#include "source/common/common/assert.h" +#include "source/common/protobuf/protobuf.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" + +#include "test/test_common/logging.h" +#include "test/test_common/status_utility.h" +#include "test/test_common/utility.h" + +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "gtest/gtest.h" +#include "re2/re2.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +namespace PatternTemplateInternal { + +namespace { + +using ::Envoy::StatusHelpers::StatusIs; + +TEST(InternalParsing, ParsedUrlDebugString) { + ParsedUrlPattern patt1 = { + { + "abc", + "def", + Operator::KPathGlob, + Variable("var", {Operator::KPathGlob, "ghi", Operator::KTextGlob}), + }, + ".test", + {}, + }; + EXPECT_EQ(patt1.debugString(), "/abc/def/*/{var=*/ghi/**}.test"); + + ParsedUrlPattern patt2 = {{ + Variable("var", {}), + }, + "", + {}}; + EXPECT_EQ(patt2.debugString(), "/{var}"); +} + +TEST(InternalParsing, isValidLiteralWorks) { + EXPECT_TRUE(isValidLiteral("123abcABC")); + EXPECT_TRUE(isValidLiteral("._~-")); + EXPECT_TRUE(isValidLiteral("-._~%20!$&'()+,;:@")); + EXPECT_FALSE(isValidLiteral("`~!@#$%^&()-_+;:,<.>'\"\\| ")); + EXPECT_FALSE(isValidLiteral("abc/")); + EXPECT_FALSE(isValidLiteral("ab*c")); + EXPECT_FALSE(isValidLiteral("a**c")); + EXPECT_FALSE(isValidLiteral("a=c")); + EXPECT_FALSE(isValidLiteral("?abc")); + EXPECT_FALSE(isValidLiteral("?a=c")); + EXPECT_FALSE(isValidLiteral("{abc")); + EXPECT_FALSE(isValidLiteral("abc}")); + EXPECT_FALSE(isValidLiteral("{abc}")); +} + +TEST(InternalParsing, IsValidRewriteLiteralWorks) { + EXPECT_TRUE(isValidRewriteLiteral("123abcABC")); + EXPECT_TRUE(isValidRewriteLiteral("abc/")); + EXPECT_TRUE(isValidRewriteLiteral("abc/def")); + EXPECT_TRUE(isValidRewriteLiteral("/abc.def")); + EXPECT_TRUE(isValidRewriteLiteral("._~-")); + EXPECT_TRUE(isValidRewriteLiteral("-._~%20!$&'()+,;:@")); + EXPECT_FALSE(isValidRewriteLiteral("`~!@#$%^&()-_+;:,<.>'\"| ")); + EXPECT_FALSE(isValidRewriteLiteral("ab}c")); + EXPECT_FALSE(isValidRewriteLiteral("ab{c")); + EXPECT_FALSE(isValidRewriteLiteral("a=c")); + EXPECT_FALSE(isValidRewriteLiteral("?a=c")); +} + +TEST(InternalParsing, IsValidIndentWorks) { + EXPECT_TRUE(isValidIndent("abc")); + EXPECT_TRUE(isValidIndent("ABC_def_123")); + EXPECT_TRUE(isValidIndent("a1")); + EXPECT_TRUE(isValidIndent("T")); + EXPECT_FALSE(isValidIndent("123")); + EXPECT_FALSE(isValidIndent("__undefined__")); + EXPECT_FALSE(isValidIndent("abc-def")); + EXPECT_FALSE(isValidIndent("abc=def")); + EXPECT_FALSE(isValidIndent("abc def")); + EXPECT_FALSE(isValidIndent("a!!!")); +} + +TEST(InternalParsing, ConsumeLiteralWorks) { + std::string pattern = "abc/123"; + + absl::StatusOr> result = consumeLiteral(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, "abc"); + EXPECT_EQ(result->unconsumed_pattern, "/123"); +} + +TEST(InternalParsing, ConsumeTextGlob) { + std::string pattern = "***abc/123"; + + absl::StatusOr> result = consumeOperator(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, Operator::KTextGlob); + EXPECT_EQ(result->unconsumed_pattern, "*abc/123"); +} + +TEST(InternalParsing, ConsumePathGlob) { + std::string pattern = "*/123"; + + absl::StatusOr> result = consumeOperator(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value, Operator::KPathGlob); + EXPECT_EQ(result->unconsumed_pattern, "/123"); +} + +class ConsumeVariableSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ConsumeVariableSuccessTestSuite, ConsumeVariableSuccess, + testing::Values("{var=*}", "{Var}", "{v1=**}", "{v_1=*/abc/**}", + "{v3=abc}", "{v=123/*/*}", "{var=abc/*/def}")); + +TEST_P(ConsumeVariableSuccess, ConsumeVariableSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + absl::StatusOr> result = consumeVariable(pattern); + + ASSERT_OK(result); + EXPECT_EQ(result->parsed_value.debugString(), pattern); + EXPECT_TRUE(result->unconsumed_pattern.empty()); +} + +class ConsumeVariableFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ConsumeVariableFailureTestSuite, ConsumeVariableFailure, + testing::Values("{var", "{=abc}", "{_var=*}", "{1v}", "{1v=abc}", + "{var=***}", "{v-a-r}", "{var=*/abc?q=1}", "{var=abc/a*}", + "{var=*def/abc}", "{var=}", "{var=abc=def}", + "{rc=||||(A+yl/}")); + +TEST_P(ConsumeVariableFailure, ConsumeVariableFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(consumeVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class ParseURLPatternSyntaxSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P( + ParseURLPatternSyntaxSuccessTestSuite, ParseURLPatternSyntaxSuccess, + testing::Values("/**.m3u8", "/**.mpd", "/*_suf", "/{path=**}.m3u8", "/{foo}/**.ts", + "/media/*.m4s", "/media/{contentId=*}/**", "/media/*", "/api/*/*/**", + "/api/*/v1/**", "/api/*/v1/*", "/{version=api/*}/*", "/api/*/*/", + "/api/*/1234/", "/api/*/{resource=*}/{method=*}", + "/api/*/{resource=*}/{method=**}", "/v1/**", "/media/{country}/{lang=*}/**", + "/{foo}/{bar}/{fo}/{fum}/*", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", + "/media/{id=*}/*", "/media/{contentId=**}", + "/api/{version}/projects/{project}/locations/{location}/{resource}/" + "{name}", + "/api/{version=*}/{url=**}", "/api/{VERSION}/{version}/{verSION}", + "/api/1234/abcd", "/media/abcd/%10%20%30/{v1=*/%10%20}_suffix", "/")); + +TEST_P(ParseURLPatternSyntaxSuccess, ParseURLPatternSyntaxSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + absl::StatusOr parsed_patt = parseURLPatternSyntax(pattern); + ASSERT_OK(parsed_patt); + EXPECT_EQ(parsed_patt->debugString(), pattern); +} + +class ParseURLPatternSyntaxFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P( + ParseURLPatternSyntaxFailureTestSuite, ParseURLPatternSyntaxFailure, + testing::Values("/api/v*/1234", "/api/{version=v*}/1234", "/api/v{versionNum=*}/1234", + "/api/{version=*beta}/1234", "/media/eff456/ll-sd-out.{ext}", + "/media/eff456/ll-sd-out.{ext=*}", "/media/eff456/ll-sd-out.**", + "/media/{country=**}/{lang=*}/**", "/media/**/*/**", "/link/{id=*}/asset*", + "/link/{id=*}/{asset=asset*}", "/media/{id=/*}/*", "/media/{contentId=/**}", + "/api/{version}/{version}", "/api/{version.major}/{version.minor}", + "/media/***", "/media/*{*}*", "/media/{*}/", "/media/*/index?a=2", "media", + "/\001\002\003\004\005\006\007", "/*(/**", "/**/{var}", + "/{var1}/{var2}/{var3}/{var4}/{var5}/{var6}", "/{=*}", + "/{var12345678901234=*}")); + +TEST_P(ParseURLPatternSyntaxFailure, ParseURLPatternSyntaxFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(parseURLPatternSyntax(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(InternalRegexGen, LiteralEscapes) { + EXPECT_EQ(toRegexPattern("abcABC123/-._~%20!$&'()+,;:@"), + "abcABC123/-\\._~%20!\\$&'\\(\\)\\+,;:@"); +} + +TEST(InternalRegexGen, LiteralMatches) { + absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; + + EXPECT_TRUE(RE2::FullMatch(toStringPiece(kPattern), toRegexPattern(kPattern))); +} + +TEST(InternalRegexGen, LiteralMatchesInNamedCapture) { + absl::string_view kPattern = "abcABC123/-._~%20!$&'()+,;:@"; + + RE2 regex = RE2(absl::StrCat("(?P", toRegexPattern(kPattern), ")")); + ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); + + // Full matched string + capture groups + std::vector captures(2); + ASSERT_TRUE(regex.Match(toStringPiece(kPattern), /*startpos=*/0, /*endpos=*/kPattern.size(), + RE2::ANCHOR_BOTH, captures.data(), captures.size())); + + // Index 0 would be the full text of the matched string. + EXPECT_EQ(toStringPiece(kPattern), captures[0]); + // Get the pattern matched with the named capture group. + EXPECT_EQ(toStringPiece(kPattern), captures.at(regex.NamedCapturingGroups().at("var"))); +} + +TEST(InternalRegexGen, LiteralOnlyMatchesItself) { + constexpr absl::string_view kChars = "abcABC123/-._~%20!$&'()+,;:@"; + + for (const char c : kChars) { + std::string s = {'z', c, 'z'}; + EXPECT_TRUE(RE2::FullMatch(s, toRegexPattern(s))); + EXPECT_FALSE(RE2::FullMatch("zzz", toRegexPattern(s))); + } +} + +TEST(InternalRegexGen, RegexLikePatternIsMatchedLiterally) { + EXPECT_TRUE(RE2::FullMatch("(abc)", toRegexPattern("(abc)"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); + + EXPECT_TRUE(RE2::FullMatch("(abc)+", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("(abc)+"))); + EXPECT_FALSE(RE2::FullMatch("abcabc", toRegexPattern("(abc)+"))); + + EXPECT_TRUE(RE2::FullMatch(".+", toRegexPattern(".+"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern(".+"))); + + EXPECT_TRUE(RE2::FullMatch("a+", toRegexPattern("a+"))); + EXPECT_FALSE(RE2::FullMatch("aa", toRegexPattern("a+"))); +} + +TEST(InternalRegexGen, DollarSignMatchesIfself) { + EXPECT_TRUE(RE2::FullMatch("abc$", toRegexPattern("abc$"))); + EXPECT_FALSE(RE2::FullMatch("abc", toRegexPattern("abc$"))); +} + +TEST(InternalRegexGen, OperatorRegexPattern) { + EXPECT_EQ(toRegexPattern(Operator::KPathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); + EXPECT_EQ(toRegexPattern(Operator::KTextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); +} + +TEST(InternalRegexGen, PathGlobRegex) { + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::KPathGlob))); + EXPECT_FALSE(RE2::FullMatch("", toRegexPattern(Operator::KPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc/123", toRegexPattern(Operator::KPathGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::KPathGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::KPathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::KPathGlob))); +} + +TEST(InternalRegexGen, TextGlobRegex) { + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::KTextGlob))); + EXPECT_TRUE(RE2::FullMatch("", toRegexPattern(Operator::KTextGlob))); + EXPECT_TRUE(RE2::FullMatch("abc/123", toRegexPattern(Operator::KTextGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::KTextGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::KTextGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::KTextGlob))); +} + +TEST(InternalRegexGen, VariableRegexPattern) { + EXPECT_EQ(toRegexPattern(Variable("var1", {})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"); + EXPECT_EQ(toRegexPattern(Variable("var2", {Operator::KPathGlob, "abc", Operator::KTextGlob})), + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+/abc/" + "[a-zA-Z0-9-._~%!$&'()+,;:@/]*)"); +} + +TEST(InternalRegexGen, VariableRegexDefaultMatch) { + absl::StatusOr> var = consumeVariable("{var}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc", toRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc"); +} + +TEST(InternalRegexGen, VariableRegexDefaultNotMatch) { + absl::StatusOr> var = consumeVariable("{var}"); + ASSERT_OK(var); + + EXPECT_FALSE(RE2::FullMatch("abc/def", toRegexPattern(var->parsed_value))); +} + +TEST(InternalRegexGen, VariableRegexSegmentsMatch) { + absl::StatusOr> var = consumeVariable("{var=abc/*/def}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc/123/def"); +} + +TEST(InternalRegexGen, VariableRegexTextGlobMatch) { + absl::StatusOr> var = consumeVariable("{var=**/def}"); + ASSERT_OK(var); + + std::string capture; + EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); + EXPECT_EQ(capture, "abc/123/def"); +} + +TEST(InternalRegexGen, VariableRegexNamedCapture) { + re2::StringPiece kPattern = "abc"; + absl::StatusOr> var = consumeVariable("{var=*}"); + ASSERT_OK(var); + + RE2 regex = RE2(toRegexPattern(var->parsed_value)); + ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); + + // Full matched string + capture groups + std::vector captures(2); + ASSERT_TRUE(regex.Match(kPattern, /*startpos=*/0, /*endpos=*/kPattern.size(), RE2::ANCHOR_BOTH, + captures.data(), captures.size())); + + // Index 0 would be the full text of the matched string. + EXPECT_EQ(kPattern, captures[0]); + // Get the pattern matched with the named capture group. + EXPECT_EQ(kPattern, captures.at(regex.NamedCapturingGroups().at("var"))); +} + +TEST(InternalRegexGen, ParsedURLPatternToRegex) { + absl::StatusOr pattern = + parseURLPatternSyntax("/abc/*/{var1}/def/{var2=*/ghi/**}.jkl"); + ASSERT_OK(pattern); + + std::string var1_capture; + std::string var2_capture; + EXPECT_TRUE(RE2::FullMatch("/abc/123/456/def/789/ghi/%20/($).jkl", + toRegexPattern(pattern.value()), &var1_capture, &var2_capture)); + EXPECT_EQ(var1_capture, "456"); + EXPECT_EQ(var2_capture, "789/ghi/%20/($)"); +} + +struct GenPatternTestCase { + GenPatternTestCase(std::string request_path, std::string url_pattern, + std::vector> capture_pairs) + : path(request_path), pattern(url_pattern), captures(capture_pairs) {} + std::string path; + std::string pattern; + std::vector> captures; +}; + +class GenPatternRegexWithMatch : public testing::TestWithParam { +protected: + const std::string& requestPath() const { return GetParam().path; } + const std::string& urlPattern() const { return GetParam().pattern; } + std::vector> const varValues() { return GetParam().captures; } +}; + +INSTANTIATE_TEST_SUITE_P( + GenPatternRegexWithMatchTestSuite, GenPatternRegexWithMatch, + testing::Values( + GenPatternTestCase("/media/1234/manifest.m3u8", "/**.m3u8", {}), + GenPatternTestCase("/manifest.mpd", "/**.mpd", {}), + GenPatternTestCase("/media/1234/manifest.m3u8", "/{path=**}.m3u8", + {{"path", "media/1234/manifest"}}), + GenPatternTestCase("/foo/12314341/format/123/hls/segment_0000000001.ts", "/{foo}/**.ts", + {{"foo", "foo"}}), + GenPatternTestCase("/media/eff456/ll-sd-out.js", "/media/{contentId=*}/**", + {{"contentId", "eff456"}}), + GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/*/**", {}), + GenPatternTestCase("/api/v1/1234", "/{version=api/*}/*", {{"version", "api/v1"}}), + GenPatternTestCase("/api/v1/1234/", "/api/*/*/", {}), + GenPatternTestCase("/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=**}", + {{"resource", "1234"}, {"method", "broadcasts/get"}}), + GenPatternTestCase("/v1/broadcasts/12345/live", "/v1/**", {}), + GenPatternTestCase("/media/us/en/12334/subtitle_enUS_00101.vtt", + "/media/{country}/{lang=*}/**", {{"country", "us"}, {"lang", "en"}}), + GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo}/{bar}/{fo}/{fum}/*", + {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), + GenPatternTestCase("/foo/bar/fo/fum/123", "/{foo=*}/{bar=*}/{fo=*}/{fum=*}/*", + {{"foo", "foo"}, {"bar", "bar"}, {"fo", "fo"}, {"fum", "fum"}}), + GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{id=*}/**", {{"id", "1234"}}), + GenPatternTestCase("/media/1234/hls/1001011.m3u8", "/media/{contentId=**}", + {{"contentId", "1234/hls/1001011.m3u8"}}), + GenPatternTestCase("/api/v1/projects/my-project/locations/global/edgeCacheOrigins/foo", + "/api/{version}/projects/{project}/locations/{location}/{resource}/" + "{name}", + {{"version", "v1"}, + {"project", "my-project"}, + {"location", "global"}, + {"resource", "edgeCacheOrigins"}, + {"name", "foo"}}), + GenPatternTestCase("/api/v1/foo/bar/baz/", "/api/{version=*}/{url=**}", + {{"version", "v1"}, {"url", "foo/bar/baz/"}}), + GenPatternTestCase("/api/v1/v2/v3", "/api/{VERSION}/{version}/{verSION}", + {{"VERSION", "v1"}, {"version", "v2"}, {"verSION", "v3"}}))); + +TEST_P(GenPatternRegexWithMatch, WithCapture) { + absl::StatusOr pattern = parseURLPatternSyntax(urlPattern()); + ASSERT_OK(pattern); + + RE2 regex = RE2(toRegexPattern(pattern.value())); + ASSERT_TRUE(regex.ok()) << regex.error(); + ASSERT_EQ(regex.NumberOfCapturingGroups(), varValues().size()); + + int capture_num = regex.NumberOfCapturingGroups() + 1; + std::vector captures(capture_num); + ASSERT_TRUE(regex.Match(requestPath(), /*startpos=*/0, + /*endpos=*/requestPath().size(), RE2::ANCHOR_BOTH, captures.data(), + captures.size())); + + EXPECT_EQ(captures[0], toStringPiece(requestPath())); + + for (const auto& [name, value] : varValues()) { + int capture_index = regex.NamedCapturingGroups().at(name); + ASSERT_GE(capture_index, 0); + EXPECT_EQ(captures.at(capture_index), value); + } +} + +class GenPatternRegexWithoutMatch + : public testing::TestWithParam> { +protected: + const std::string& requestPath() const { return std::get<0>(GetParam()); } + const std::string& urlPattern() const { return std::get<1>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWithoutMatch, + testing::ValuesIn(std::vector>( + {{"/media/12345/f/123/s00002.m4s", "/media/*.m4s"}, + {"/media/eff456/ll-sd-out.js", "/media/*"}, + {"/api/v1/1234/", "/api/*/v1/*"}, + {"/api/v1/1234/broadcasts/get", "/api/*/{resource=*}/{method=*}"}, + {"/api/v1/1234/", "/api/*/v1/**"}, + {"/api/*/1234/", "/api/*/1234/"}}))); + +TEST_P(GenPatternRegexWithoutMatch, WithCapture) { + absl::StatusOr pattern = parseURLPatternSyntax(urlPattern()); + ASSERT_OK(pattern); + + RE2 regex = RE2(toRegexPattern(pattern.value())); + ASSERT_TRUE(regex.ok()) << regex.error(); + + EXPECT_FALSE(regex.Match(requestPath(), /*startpos=*/0, + /*endpos=*/requestPath().size(), RE2::ANCHOR_BOTH, nullptr, 0)); +} + +} // namespace +} // namespace PatternTemplateInternal +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy diff --git a/test/extensions/path/pattern_template_lib/pattern_template_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_test.cc new file mode 100644 index 0000000000000..7f9fea6c54a0f --- /dev/null +++ b/test/extensions/path/pattern_template_lib/pattern_template_test.cc @@ -0,0 +1,337 @@ +#include +#include +#include + +#include "source/common/common/assert.h" +#include "source/common/protobuf/protobuf.h" +#include "source/extensions/path/pattern_template_lib/pattern_template.h" +#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" + +#include "test/test_common/logging.h" +#include "test/test_common/status_utility.h" +#include "test/test_common/utility.h" + +#include "gtest/gtest.h" + +namespace Envoy { +namespace Extensions { +namespace PatternTemplate { + +namespace { + +using ::Envoy::StatusHelpers::IsOkAndHolds; +using ::Envoy::StatusHelpers::StatusIs; + +// Capture regex for /{var1}/{var2}/{var3}/{var4}/{var5} +static constexpr absl::string_view kCaptureRegex = "/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"; +static constexpr absl::string_view kMatchUrl = "/val1/val2/val3/val4/val5"; + +TEST(ConvertURLPattern, ValidPattern) { + EXPECT_THAT(convertURLPatternSyntaxToRegex("/abc"), IsOkAndHolds("/abc")); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/**.mpd"), + IsOkAndHolds("/[a-zA-Z0-9-._~%!$&'()+,;:@/]*\\.mpd")); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/*/{resource=*}/{method=**}"), + IsOkAndHolds("/api/[a-zA-Z0-9-._~%!$&'()+,;:@]+/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@/]*)")); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/{VERSION}/{version}/{verSION}"), + IsOkAndHolds("/api/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" + "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)")); +} + +TEST(ConvertURLPattern, InvalidPattern) { + EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/v*/1234"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/media/**/*/**"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/\001\002\003\004\005\006\007"), + StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/{var12345678901234=*}"), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class ParseRewriteHelperSuccess : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperSuccessTestSuite, ParseRewriteHelperSuccess, + testing::Values("/{var1}", "/{var1}{var2}", "/{var1}-{var2}", + "/abc/{var1}/def", "/{var1}/abd/{var2}", + "/abc-def-{var1}/a/{var1}")); + +TEST_P(ParseRewriteHelperSuccess, ParseRewriteHelperSuccessTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_OK(parseRewritePatternHelper(pattern)); +} + +class ParseRewriteHelperFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteHelperFailureTestSuite, ParseRewriteHelperFailure, + testing::Values("{var1}", "/{{var1}}", "/}va1{", "var1}", + "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); + +TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(parseRewritePatternHelper(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class ParseRewriteSuccess : public testing::TestWithParam> { +protected: + const std::string& rewritePattern() const { return std::get<0>(GetParam()); } + envoy::extensions::pattern_template::PatternTemplateRewriteSegments expectedProto() const { + envoy::extensions::pattern_template::PatternTemplateRewriteSegments expected_proto; + Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); + return expected_proto; + } +}; + +TEST(ParseRewrite, InvalidRegex) { + EXPECT_THAT(parseRewritePattern("/{var1}", "+[abc"), StatusIs(absl::StatusCode::kInternal)); +} + +INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, + testing::ValuesIn(std::vector>({ + {"/static", R"EOF(segments: {literal: "/static"} )EOF"}, + {"/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1)EOF"}, + {"/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1)EOF"}, + {"/{var1}/{var1}/{var1}", R"EOF(segments: + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1)EOF"}, + {"/{var3}/{var1}/{var2}", R"EOF(segments + - literal: "/" + - var_index: 3 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 2)EOF"}, + {"/{var3}/abc/def/{var2}.suffix", R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/abc/def/" + - var_index: 2 + - literal: ".suffix")EOF"}, + {"/abc/{var1}/{var2}/def", R"EOF(segments + - literal: "/abc/" + - var_index: 1 + - literal: "/" + - var_index: 2 + - literal: "/def")EOF"}, + {"/{var1}{var2}", R"EOF(segments + - literal: "/" + - var_index: 1 + - ar_index: 2)EOF"}, + {"/{var1}-{var2}/bucket-{var3}.suffix", R"EOF(segments + - literal: "/" + - var_index: 1 + - literal: "-" + - var_index: 2 + - literal: "/bucket-" + - var_index: 3 + - literal: ".suffix")EOF"}, + }))); + +TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { + absl::StatusOr rewrite = + parseRewritePattern(rewritePattern(), kCaptureRegex); + ASSERT_OK(rewrite); + // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); +} + +class ParseRewriteFailure : public testing::TestWithParam {}; + +INSTANTIATE_TEST_SUITE_P(ParseRewriteFailureTestSuite, ParseRewriteFailure, + testing::Values("{var1}", "/{var6}", "/{{var1}}", "/}va1{", "var1}", + "/{var1}?abc=123", "", "/{var1/var2}", "/{}", "/a//b")); + +TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { + std::string pattern = GetParam(); + SCOPED_TRACE(pattern); + + EXPECT_THAT(parseRewritePattern(pattern, kCaptureRegex), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class RewriteUrlTemplateSuccess + : public testing::TestWithParam> { +protected: + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewriteProto() const { + envoy::extensions::pattern_template::PatternTemplateRewriteSegments proto; + Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); + return proto; + } + const std::string& expectedRewrittenUrl() const { return std::get<1>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateSuccess, + testing::ValuesIn(std::vector>( + {{R"EOF(segments: { literal: "/static" })EOF", "/static"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1)EOF", + "/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1)EOF", + "/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 1)EOF", + "/val1/val1/val1"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/" + - var_index: 1 + - literal: "/" + - var_index: 2)EOF", + "/val3/val1/val2"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - literal: "/abc/def/" + - var_index: 2 + - literal: ".suffix")EOF", + "/val3/abc/def/val2.suffix"}, + {R"EOF(segments: + - literal: "/" + - var_index: 3 + - var_index: 2 + - literal: "." + - var_index: 1)EOF", + "/val3val2.val1"}, + {R"EOF(segments: + - literal: "/abc/" + - var_index: 1 + - literal: "/" + - var_index: 5 + - literal: "/def")EOF", + "/abc/val1/val5/def"}}))); + +TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { + absl::StatusOr rewritten_url = + rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewriteProto()); + ASSERT_OK(rewritten_url); + EXPECT_EQ(rewritten_url.value(), expectedRewrittenUrl()); +} + +TEST(RewriteUrlTemplateFailure, BadRegex) { + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 1 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, "+/bad_regex", rewrite_proto), + StatusIs(absl::StatusCode::kInternal)); +} + +TEST(RewriteUrlTemplateFailure, RegexNoMatch) { + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 1 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, "/no_match_regex", rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 0 + )EOF"; + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { + envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; + + const std::string yaml = R"EOF( +segments: +- literal: "/" +- var_index: 6 + )EOF"; + + Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + + EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + StatusIs(absl::StatusCode::kInvalidArgument)); +} + +class URLPatternMatchAndRewrite + : public testing::TestWithParam< + std::tuple> { +protected: + const std::string& urlPattern() const { return std::get<0>(GetParam()); } + const std::string& rewritePattern() const { return std::get<1>(GetParam()); } + const std::string& matchUrl() const { return std::get<2>(GetParam()); } + const std::string& expectedRewrittenUrl() const { return std::get<3>(GetParam()); } +}; + +INSTANTIATE_TEST_SUITE_P( + URLPatternMatchAndRewriteTestSuite, URLPatternMatchAndRewrite, + testing::ValuesIn(std::vector>( + {{"/api/users/{id}/{path=**}", "/users/{id}/{path}", "/api/users/21334/profile.json", + "/users/21334/profile.json"}, + {"/videos/*/{id}/{format}/{rendition}/{segment=**}.ts", + "/{id}/{format}/{rendition}/{segment}.ts", "/videos/lib/132939/hls/13/segment_00001.ts", + "/132939/hls/13/segment_00001.ts"}, + {"/region/{region}/bucket/{name}/{method=**}", "/{region}/bucket-{name}/{method}", + "/region/eu/bucket/prod-storage/object.pdf", "/eu/bucket-prod-storage/object.pdf"}, + {"/region/{region}/bucket/{name}/{method=**}", "/{region}{name}/{method}", + "/region/eu/bucket/prod-storage/object.pdf", "/euprod-storage/object.pdf"}}))); + +TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { + absl::StatusOr regex = convertURLPatternSyntaxToRegex(urlPattern()); + ASSERT_OK(regex); + + absl::StatusOr + rewrite_proto = parseRewritePattern(rewritePattern(), regex.value()); + ASSERT_OK(rewrite_proto); + + absl::StatusOr rewritten_url = + rewriteURLTemplatePattern(matchUrl(), regex.value(), rewrite_proto.value()); + ASSERT_OK(rewritten_url); + + EXPECT_EQ(rewritten_url.value(), expectedRewrittenUrl()); +} + +} // namespace + +} // namespace PatternTemplate +} // namespace Extensions +} // namespace Envoy From cb5656691fb7506dc2c80ce9adecdb1fcc2c1322 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 15:06:25 +0000 Subject: [PATCH 147/238] Add formatting Signed-off-by: silverstar195 --- tools/code_format/check_format.py | 14 ++++++++------ tools/spelling/spelling_dictionary.txt | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index 9163b2d858be3..69fd9d577764c 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 import argparse -import common import functools import logging import multiprocessing @@ -9,14 +8,16 @@ import os.path import pathlib import re -import subprocess +import shutil import stat +import subprocess import sys import traceback -import shutil -import paths from functools import cached_property +import common +import paths + EXCLUDED_PREFIXES = ( "./.", "./generated/", "./thirdparty/", "./build", "./bazel-", "./tools/dev/src", "./source/extensions/extensions_build_config.bzl", "./contrib/contrib_build_config.bzl", @@ -1149,10 +1150,11 @@ def check_visibility(error_messages): "':(exclude)source/extensions/filters/network/common/BUILD' " "':(exclude)source/extensions/transport_sockets/common/BUILD' " "':(exclude)source/extensions/udp_packet_writer/default/BUILD' " - "':(exclude)source/extensions/udp_packet_writer/gso/BUILD' ") + "':(exclude)source/extensions/udp_packet_writer/gso/BUILD' " + "':(exclude)source/extensions/path/match/pattern_template/BUILD' ") command = ( "git diff $(tools/git/last_github_commit.sh) -- source/extensions/* %s |grep '+.*visibility ='" - % exclude_list) + % ''.join(exclude_list)) try: output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() if output: diff --git a/tools/spelling/spelling_dictionary.txt b/tools/spelling/spelling_dictionary.txt index 0812de5511b82..7ec5fc282445b 100644 --- a/tools/spelling/spelling_dictionary.txt +++ b/tools/spelling/spelling_dictionary.txt @@ -30,7 +30,12 @@ Bdecoded Bencoded GiB Repick +SION TRA +abc +abcd +abd +ar btree CAS CB @@ -151,9 +156,13 @@ FQDN FREEBIND FUZZER FUZZERS +delims dereferencing differentially dnsresolvers +endpos +fo +ghi guarddog GC GCC @@ -220,6 +229,9 @@ LEDS LEV LF LHS +hls +jkl +lang libsxg LLVM LPT @@ -263,6 +275,7 @@ Oauth OCSP OD ODCDS +mpd oghttp OID OK @@ -967,10 +980,12 @@ parsers passphrase passthrough pathname +patt pausable pausedness pcall pcap +pchar pclose performant pfctl @@ -1176,6 +1191,7 @@ srtt ssize stackdriver stacktrace +startpos starttls startup stateful @@ -1222,6 +1238,7 @@ subtrees subtype subtypes subzone +suf superclass superset svc @@ -1313,6 +1330,7 @@ username usr util utils +va valgrind validator validators @@ -1322,6 +1340,7 @@ variadic varint vec vectorize +ver verifier verifiers versa @@ -1332,6 +1351,7 @@ vip virtualhost virtualize vptr +vtt wakeup wakeups wamr From 2b847805ef728c0eeb17d7847cc7ed6c9d85d60b Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 15:14:15 +0000 Subject: [PATCH 148/238] Add formatting Signed-off-by: silverstar195 --- tools/code_format/check_format.py | 17 ++++++++++------- tools/code_format/config.yaml | 1 + 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index ebea2813f61ae..bfa680c57cbb6 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -1,24 +1,27 @@ #!/usr/bin/env python3 import argparse -import common import functools import logging import multiprocessing import os import pathlib import re -import subprocess +import shutil import stat +import subprocess import sys import traceback -import shutil -from functools import cached_property -from typing import Callable, Dict, List, Pattern, Tuple, Union - # The way this script is currently used (ie no bazel) it relies on system deps. # As `pyyaml` is present in `envoy-build-ubuntu` it should be safe to use here. import yaml +from functools import cached_property +from typing import Callable +from typing import Dict +from typing import List +from typing import Pattern +from typing import Tuple +from typing import Union import paths @@ -1077,7 +1080,7 @@ def normalize_path(path): def check_visibility(error_messages): command = ( "git diff $(tools/git/last_github_commit.sh) -- source/extensions/* %s |grep '+.*visibility ='" - % [f"':(exclude){c}' " for c in format_checker.config["visibility_excludes"]]) + % ''.join([f"':(exclude){c}' " for c in format_checker.config["visibility_excludes"]])) try: output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() if output: diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index 15006d31f87e6..172100ea3e4ff 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -295,3 +295,4 @@ visibility_excludes: - source/extensions/transport_sockets/common/BUILD - source/extensions/udp_packet_writer/default/BUILD - source/extensions/udp_packet_writer/gso/BUILD +- source/extensions/path/pattern_template_lib/BUILD From 3ac78ba9e7f3879e28085d1c8bf8547f5293eeb8 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 15:20:28 +0000 Subject: [PATCH 149/238] Add code owners Signed-off-by: silverstar195 --- CODEOWNERS | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CODEOWNERS b/CODEOWNERS index 9cdcad71a8d80..86dc82b8859f5 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -271,6 +271,10 @@ extensions/filters/http/oauth2 @derekargueta @snowp /*/extensions/filters/http/common @UNOWNED @UNOWNED /*/extensions/filters/network/common @UNOWNED @UNOWNED +# URL Pattern Match and Rewrite Library +/*/extensions/path/pattern_template_lib @alyssawilk @yanjunxiang-google +/*/extensions/path/pattern_template_lib/proto @alyssawilk @yanjunxiang-google + # Contrib /contrib/exe/ @mattklein123 @lizan /contrib/client_ssl_auth/ @UNOWNED @UNOWNED From 1574273a45660214bf2548b07e1b7ae8e8bf3029 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 17:40:45 +0000 Subject: [PATCH 150/238] Changes for comments Signed-off-by: silverstar195 --- .../pattern_template_lib/pattern_template.cc | 35 +++++----- .../pattern_template_lib/pattern_template.h | 24 +++---- .../pattern_template_internal.cc | 64 +++++++++---------- .../pattern_template_internal.h | 33 +++++----- .../pattern_template_internal_test.cc | 22 +++---- .../pattern_template_test.cc | 4 +- 6 files changed, 91 insertions(+), 91 deletions(-) diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc index c40620b618005..6a2de41bf4b11 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -18,27 +18,24 @@ namespace Envoy { namespace Extensions { namespace PatternTemplate { -using PatternTemplateInternal::ParsedUrlPattern; +using Internal::ParsedUrlPattern; #ifndef SWIG // Silence warnings about missing initializers for members of LazyRE2. #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif -inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } - absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { - absl::StatusOr status = - PatternTemplateInternal::parseURLPatternSyntax(url_pattern); + Internal::parseURLPatternSyntax(url_pattern); if (!status.ok()) { return status.status(); } - return PatternTemplateInternal::toRegexPattern(*status); + return Internal::toRegexPattern(*status); } absl::StatusOr> -parseRewritePatternHelper(absl::string_view pattern) { +parseRewritePattern(absl::string_view pattern) { std::vector result; // The pattern should start with a '/' and thus the first segment should @@ -49,17 +46,17 @@ parseRewritePatternHelper(absl::string_view pattern) { // Don't allow contiguous '/' patterns. static const LazyRE2 invalid_regex = {"^.*//.*$"}; - if (RE2::FullMatch(toStringPiece(pattern), *invalid_regex)) { + if (RE2::FullMatch(Internal::toStringPiece(pattern), *invalid_regex)) { return absl::InvalidArgumentError("Invalid rewrite literal"); } while (!pattern.empty()) { std::vector segments1 = absl::StrSplit(pattern, absl::MaxSplits('{', 1)); if (!segments1[0].empty()) { - if (!PatternTemplateInternal::isValidRewriteLiteral(segments1[0])) { + if (!Internal::isValidRewriteLiteral(segments1[0])) { return absl::InvalidArgumentError("Invalid rewrite literal pattern"); } - result.emplace_back(segments1[0], RewriteStringKind::KLiteral); + result.emplace_back(segments1[0], RewriteStringKind::Literal); } if (segments1.size() < 2) { @@ -74,10 +71,10 @@ parseRewritePatternHelper(absl::string_view pattern) { } pattern = segments2[1]; - if (!PatternTemplateInternal::isValidIndent(segments2[0])) { + if (!Internal::isValidVariableName(segments2[0])) { return absl::InvalidArgumentError("Invalid variable name"); } - result.emplace_back(segments2[0], RewriteStringKind::KVariable); + result.emplace_back(segments2[0], RewriteStringKind::Variable); } return result; } @@ -85,12 +82,12 @@ parseRewritePatternHelper(absl::string_view pattern) { absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { envoy::extensions::pattern_template::PatternTemplateRewriteSegments parsed_pattern; - RE2 regex = RE2(toStringPiece(capture_regex)); + RE2 regex = RE2(Internal::toStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); } - absl::StatusOr> status = parseRewritePatternHelper(pattern); + absl::StatusOr> status = parseRewritePattern(pattern); if (!status.ok()) { return status.status(); } @@ -100,10 +97,10 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) for (const auto& [str, kind] : processed_pattern) { switch (kind) { - case RewriteStringKind::KLiteral: + case RewriteStringKind::Literal: parsed_pattern.add_segments()->set_literal(std::string(str)); break; - case RewriteStringKind::KVariable: + case RewriteStringKind::Variable: auto it = capture_index_map.find(std::string(str)); if (it == capture_index_map.end()) { return absl::InvalidArgumentError("Nonexisting variable name"); @@ -121,7 +118,7 @@ absl::Status isValidMatchPattern(const std::string path_template_match) { } absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { - return parseRewritePatternHelper(path_template_rewrite).status(); + return parseRewritePattern(path_template_rewrite).status(); } absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, @@ -136,7 +133,7 @@ absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, absl::StatusOr rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern) { - RE2 regex = RE2(PatternTemplateInternal::toStringPiece(capture_regex)); + RE2 regex = RE2(Internal::toStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); } @@ -144,7 +141,7 @@ absl::StatusOr rewriteURLTemplatePattern( // First capture is the whole matched regex pattern. int capture_num = regex.NumberOfCapturingGroups() + 1; std::vector captures(capture_num); - if (!regex.Match(PatternTemplateInternal::toStringPiece(url), /*startpos=*/0, + if (!regex.Match(Internal::toStringPiece(url), /*startpos=*/0, /*endpos=*/url.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { return absl::InvalidArgumentError("Pattern does not match"); } diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 82753ade6ac67..7a1142e0f0ac4 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -12,13 +12,13 @@ namespace Envoy { namespace Extensions { namespace PatternTemplate { -enum class RewriteStringKind { KVariable, KLiteral }; +enum class RewriteStringKind { Variable, Literal }; struct RewritePatternSegment { - RewritePatternSegment(absl::string_view segment_value, RewriteStringKind kind) - : segment_value(segment_value), kind(kind) {} - absl::string_view segment_value; - RewriteStringKind kind; + RewritePatternSegment(absl::string_view value, RewriteStringKind kind) + : value_(value), kind_(kind) {} + absl::string_view value_; + RewriteStringKind kind_; }; // Returns the regex pattern that is equivalent to the given url_pattern. @@ -26,12 +26,10 @@ struct RewritePatternSegment { // the safe regex Envoy can understand. Strips away any variable captures. absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); -// Helper function that parses the pattern and breaks it down to either -// literals or variable names. To be used by ParseRewritePattern(). -// Exposed here so that the validator for the rewrite pattern can also -// use it. +// Parses the specified pattern into a sequence of segments (which are +// either literals or variable names). absl::StatusOr> -parseRewritePatternHelper(absl::string_view pattern); +parseRewritePattern(absl::string_view pattern); // Returns the parsed Url rewrite pattern to be used by // RewriteURLTemplatePattern() |capture_regex| should @@ -42,7 +40,11 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); // Returns if provided rewrite pattern is valid absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); -// Returns if path_template and rewrite_template have valid variables +// Returns if path_template and rewrite_template have valid variables. +// Every variable in rewrite MUST be present in match +// For example: +// Match: /foo/{bar}/{var} and Rewrite: /goo/{var} is vaild +// Match: /foo/{bar} and Rewrite: /goo/{bar}/{var} is invaild. Match is missing {var}. absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, const std::string& capture_regex); diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc index 945741885cea9..4996ef60e8e23 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc @@ -24,7 +24,7 @@ namespace Envoy { namespace Extensions { namespace PatternTemplate { -namespace PatternTemplateInternal { +namespace Internal { namespace { @@ -83,12 +83,12 @@ std::string toString(const Operator val) { } std::string toString(const Variable val) { - if (val.var_match.empty()) { - return absl::StrCat("{", val.var_name, "}"); + if (val.var_match_.empty()) { + return absl::StrCat("{", val.var_name_, "}"); } - return absl::StrCat("{", val.var_name, "=", - absl::StrJoin(val.var_match, "/", ToStringFormatter()), "}"); + return absl::StrCat("{", val.var_name_, "=", + absl::StrJoin(val.var_match_, "/", ToStringFormatter()), "}"); return ""; } @@ -107,8 +107,8 @@ absl::StatusOr alsoUpdatePattern( } ParsedResult result = *std::move(status); - *patt = result.unconsumed_pattern; - return result.parsed_value; + *patt = result.unconsumed_pattern_; + return result.parsed_value_; } } // namespace @@ -116,27 +116,27 @@ absl::StatusOr alsoUpdatePattern( std::string Variable::debugString() const { return toString(*this); } std::string ParsedUrlPattern::debugString() const { - return absl::StrCat("/", absl::StrJoin(parsed_segments, "/", ToStringFormatter()), - suffix.value_or("")); + return absl::StrCat("/", absl::StrJoin(parsed_segments_, "/", ToStringFormatter()), + suffix_.value_or("")); } -bool isValidLiteral(absl::string_view pattern) { +bool isValidLiteral(absl::string_view literal) { static const std::string* kValidLiteralRegex = new std::string(absl::StrCat("^[", kLiteral, "]+$")); static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; - return RE2::FullMatch(toStringPiece(pattern), *literal_regex); + return RE2::FullMatch(toStringPiece(literal), *literal_regex); } -bool isValidRewriteLiteral(absl::string_view pattern) { +bool isValidRewriteLiteral(absl::string_view literal) { static const std::string* kValidLiteralRegex = new std::string(absl::StrCat("^[", kLiteral, "/]+$")); static const LazyRE2 literal_regex = {kValidLiteralRegex->data()}; - return RE2::FullMatch(toStringPiece(pattern), *literal_regex); + return RE2::FullMatch(toStringPiece(literal), *literal_regex); } -bool isValidIndent(absl::string_view pattern) { +bool isValidVariableName(absl::string_view indent) { static const LazyRE2 ident_regex = {"^[a-zA-Z][a-zA-Z0-9_]*$"}; - return RE2::FullMatch(toStringPiece(pattern), *ident_regex); + return RE2::FullMatch(toStringPiece(indent), *ident_regex); } absl::StatusOr> consumeLiteral(absl::string_view pattern) { @@ -172,7 +172,7 @@ absl::StatusOr> consumeVariable(absl::string_view pattern // Parse the actual variable pattern, starting with the variable name. std::vector var_parts = absl::StrSplit(parts[0], absl::MaxSplits('=', 1)); - if (!isValidIndent(var_parts[0])) { + if (!isValidVariableName(var_parts[0])) { return absl::InvalidArgumentError("Invalid variable name"); } Variable var = Variable(var_parts[0], {}); @@ -203,7 +203,7 @@ absl::StatusOr> consumeVariable(absl::string_view pattern } var_match = *std::move(status); } - var.var_match.push_back(var_match); + var.var_match_.push_back(var_match); if (!var_patt.empty()) { if (var_patt[0] != '/' || var_patt.size() == 1) { return absl::InvalidArgumentError("Invalid variable match"); @@ -219,14 +219,14 @@ absl::StatusOr> gatherCaptureNames(struct ParsedUrlPattern pattern) { absl::flat_hash_set captured_variables; - for (const ParsedSegment& segment : pattern.parsed_segments) { + for (const ParsedSegment& segment : pattern.parsed_segments_) { if (!absl::holds_alternative(segment)) { continue; } if (captured_variables.size() >= kPatternMatchingMaxVariablesPerUrl) { return absl::InvalidArgumentError("Exceeded variable count limit"); } - absl::string_view var_name = absl::get(segment).var_name; + absl::string_view var_name = absl::get(segment).var_name_; if (var_name.size() < kPatternMatchingMinVariableNameLen || var_name.size() > kPatternMatchingMaxVariableNameLen) { @@ -243,7 +243,7 @@ gatherCaptureNames(struct ParsedUrlPattern pattern) { absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { bool seen_text_glob = false; - for (const ParsedSegment& segment : pattern.parsed_segments) { + for (const ParsedSegment& segment : pattern.parsed_segments_) { if (absl::holds_alternative(segment)) { if (seen_text_glob) { return absl::InvalidArgumentError("Glob after text glob."); @@ -251,13 +251,13 @@ absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { seen_text_glob = (absl::get(segment) == Operator::KTextGlob); } else if (absl::holds_alternative(segment)) { const Variable& var = absl::get(segment); - if (var.var_match.empty()) { + if (var.var_match_.empty()) { if (seen_text_glob) { // A variable with no explicit matcher is treated as a path glob. return absl::InvalidArgumentError("Implicit variable path glob after text glob."); } } else { - for (const absl::variant& var_seg : var.var_match) { + for (const absl::variant& var_seg : var.var_match_) { if (!absl::holds_alternative(var_seg)) { continue; } @@ -309,13 +309,13 @@ absl::StatusOr parseURLPatternSyntax(absl::string_view url_pat } segment = *std::move(status); } - parsed_pattern.parsed_segments.push_back(segment); + parsed_pattern.parsed_segments_.push_back(segment); // Deal with trailing '/' or suffix. if (!url_pattern.empty()) { if (url_pattern == "/") { // Single trailing '/' at the end, mark this with empty literal. - parsed_pattern.parsed_segments.emplace_back(""); + parsed_pattern.parsed_segments_.emplace_back(""); break; } else if (url_pattern[0] == '/') { // Have '/' followed by more text, consume the '/'. @@ -327,7 +327,7 @@ absl::StatusOr parseURLPatternSyntax(absl::string_view url_pat if (!status.ok()) { return status.status(); } - parsed_pattern.suffix = *std::move(status); + parsed_pattern.suffix_ = *std::move(status); if (!url_pattern.empty()) { // Suffix didn't consume whole remaining pattern ('/' in url_pattern). return absl::InvalidArgumentError("Prefix match not supported."); @@ -341,7 +341,7 @@ absl::StatusOr parseURLPatternSyntax(absl::string_view url_pat if (!status.ok()) { return status.status(); } - parsed_pattern.captured_variables = *std::move(status); + parsed_pattern.captured_variables_ = *std::move(status); absl::Status validate_status = validateNoOperatorAfterTextGlob(parsed_pattern); if (!validate_status.ok()) { @@ -369,19 +369,19 @@ std::string toRegexPattern(Operator pattern) { } std::string toRegexPattern(const Variable& pattern) { - return absl::StrCat("(?P<", pattern.var_name, ">", - pattern.var_match.empty() + return absl::StrCat("(?P<", pattern.var_name_, ">", + pattern.var_match_.empty() ? toRegexPattern(kDefaultVariableOperator) - : absl::StrJoin(pattern.var_match, "/", ToRegexPatternFormatter()), + : absl::StrJoin(pattern.var_match_, "/", ToRegexPatternFormatter()), ")"); } std::string toRegexPattern(const struct ParsedUrlPattern& pattern) { - return absl::StrCat("/", absl::StrJoin(pattern.parsed_segments, "/", ToRegexPatternFormatter()), - toRegexPattern(pattern.suffix.value_or(""))); + return absl::StrCat("/", absl::StrJoin(pattern.parsed_segments_, "/", ToRegexPatternFormatter()), + toRegexPattern(pattern.suffix_.value_or(""))); } -} // namespace PatternTemplateInternal +} // namespace Internal } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h index 0277bf54698c4..3e3c213761eba 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.h +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.h @@ -17,7 +17,7 @@ namespace Envoy { namespace Extensions { namespace PatternTemplate { -namespace PatternTemplateInternal { +namespace Internal { using Literal = absl::string_view; @@ -32,23 +32,24 @@ enum class Operator { KPathGlob, KTextGlob }; */ struct RewriteSegment { - absl::string_view literal; + // A string that will be concatenated for rewrite. + absl::string_view literal_; // Represents an index into the RE2 capture which value should be used // to construct the rewritten URL. Note that the index should be greater // than 0 as 0 index into the whole match RE2 pattern. - int var_index; + int capture_index_; }; /** * Represents a pattern variable. Variables are included in both path match and rewrite paths. */ struct Variable { - absl::string_view var_name; - std::vector> var_match; + absl::string_view var_name_; + std::vector> var_match_; Variable(absl::string_view name, std::vector> match) - : var_name(name), var_match(match) {} + : var_name_(name), var_match_(match) {} std::string debugString() const; }; @@ -59,9 +60,9 @@ using ParsedSegment = absl::variant; * Represents the parsed path including literals and variables. */ struct ParsedUrlPattern { - std::vector parsed_segments; - absl::optional suffix; - absl::flat_hash_set captured_variables; + std::vector parsed_segments_; + absl::optional suffix_; + absl::flat_hash_set captured_variables_; std::string debugString() const; }; @@ -69,17 +70,17 @@ struct ParsedUrlPattern { /** * Check if literal is valid */ -bool isValidLiteral(absl::string_view pattern); +bool isValidLiteral(absl::string_view literal); /** * Check if rewrite literal is valid */ -bool isValidRewriteLiteral(absl::string_view pattern); +bool isValidRewriteLiteral(absl::string_view literal); /** * Check if indent is valid */ -bool isValidIndent(absl::string_view pattern); +bool isValidVariableName(absl::string_view indent); /** * Used by the following Consume{Literal.Operator,Variable} functions @@ -88,10 +89,10 @@ bool isValidIndent(absl::string_view pattern); * portion of the pattern in |unconsumed_pattern|. */ template struct ParsedResult { - ParsedResult(T val, absl::string_view pattern) : parsed_value(val), unconsumed_pattern(pattern) {} + ParsedResult(T val, absl::string_view pattern) : parsed_value_(val), unconsumed_pattern_(pattern) {} - T parsed_value; - absl::string_view unconsumed_pattern; + T parsed_value_; + absl::string_view unconsumed_pattern_; }; /** @@ -139,7 +140,7 @@ std::string toRegexPattern(const struct ParsedUrlPattern& pattern); */ inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } -} // namespace PatternTemplateInternal +} // namespace Internal } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy diff --git a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc index d98809bbb2559..255bd8dd482fc 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc @@ -79,17 +79,17 @@ TEST(InternalParsing, IsValidRewriteLiteralWorks) { EXPECT_FALSE(isValidRewriteLiteral("?a=c")); } -TEST(InternalParsing, IsValidIndentWorks) { - EXPECT_TRUE(isValidIndent("abc")); - EXPECT_TRUE(isValidIndent("ABC_def_123")); - EXPECT_TRUE(isValidIndent("a1")); - EXPECT_TRUE(isValidIndent("T")); - EXPECT_FALSE(isValidIndent("123")); - EXPECT_FALSE(isValidIndent("__undefined__")); - EXPECT_FALSE(isValidIndent("abc-def")); - EXPECT_FALSE(isValidIndent("abc=def")); - EXPECT_FALSE(isValidIndent("abc def")); - EXPECT_FALSE(isValidIndent("a!!!")); +TEST(InternalParsing, IsValidVariableNameWorks) { + EXPECT_TRUE(isValidVariableName("abc")); + EXPECT_TRUE(isValidVariableName("ABC_def_123")); + EXPECT_TRUE(isValidVariableName("a1")); + EXPECT_TRUE(isValidVariableName("T")); + EXPECT_FALSE(isValidVariableName("123")); + EXPECT_FALSE(isValidVariableName("__undefined__")); + EXPECT_FALSE(isValidVariableName("abc-def")); + EXPECT_FALSE(isValidVariableName("abc=def")); + EXPECT_FALSE(isValidVariableName("abc def")); + EXPECT_FALSE(isValidVariableName("a!!!")); } TEST(InternalParsing, ConsumeLiteralWorks) { diff --git a/test/extensions/path/pattern_template_lib/pattern_template_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_test.cc index 7f9fea6c54a0f..67f3f1c1b9f5b 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_test.cc @@ -66,7 +66,7 @@ TEST_P(ParseRewriteHelperSuccess, ParseRewriteHelperSuccessTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - EXPECT_OK(parseRewritePatternHelper(pattern)); + EXPECT_OK(parseRewritePattern(pattern)); } class ParseRewriteHelperFailure : public testing::TestWithParam {}; @@ -79,7 +79,7 @@ TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - EXPECT_THAT(parseRewritePatternHelper(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(parseRewritePattern(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); } class ParseRewriteSuccess : public testing::TestWithParam> { From d2cf6f77e0941bb8ee8fa85f7497b486d34f275b Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 18:41:53 +0000 Subject: [PATCH 151/238] Add some tests Signed-off-by: silverstar195 --- .../pattern_template_internal_test.cc | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc index 255bd8dd482fc..a8fddde8b4a20 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc @@ -22,7 +22,7 @@ namespace Envoy { namespace Extensions { namespace PatternTemplate { -namespace PatternTemplateInternal { +namespace Internal { namespace { @@ -98,8 +98,8 @@ TEST(InternalParsing, ConsumeLiteralWorks) { absl::StatusOr> result = consumeLiteral(pattern); ASSERT_OK(result); - EXPECT_EQ(result->parsed_value, "abc"); - EXPECT_EQ(result->unconsumed_pattern, "/123"); + EXPECT_EQ(result->parsed_value_, "abc"); + EXPECT_EQ(result->unconsumed_pattern_, "/123"); } TEST(InternalParsing, ConsumeTextGlob) { @@ -108,8 +108,8 @@ TEST(InternalParsing, ConsumeTextGlob) { absl::StatusOr> result = consumeOperator(pattern); ASSERT_OK(result); - EXPECT_EQ(result->parsed_value, Operator::KTextGlob); - EXPECT_EQ(result->unconsumed_pattern, "*abc/123"); + EXPECT_EQ(result->parsed_value_, Operator::KTextGlob); + EXPECT_EQ(result->unconsumed_pattern_, "*abc/123"); } TEST(InternalParsing, ConsumePathGlob) { @@ -118,8 +118,8 @@ TEST(InternalParsing, ConsumePathGlob) { absl::StatusOr> result = consumeOperator(pattern); ASSERT_OK(result); - EXPECT_EQ(result->parsed_value, Operator::KPathGlob); - EXPECT_EQ(result->unconsumed_pattern, "/123"); + EXPECT_EQ(result->parsed_value_, Operator::KPathGlob); + EXPECT_EQ(result->unconsumed_pattern_, "/123"); } class ConsumeVariableSuccess : public testing::TestWithParam {}; @@ -135,8 +135,8 @@ TEST_P(ConsumeVariableSuccess, ConsumeVariableSuccessTest) { absl::StatusOr> result = consumeVariable(pattern); ASSERT_OK(result); - EXPECT_EQ(result->parsed_value.debugString(), pattern); - EXPECT_TRUE(result->unconsumed_pattern.empty()); + EXPECT_EQ(result->parsed_value_.debugString(), pattern); + EXPECT_TRUE(result->unconsumed_pattern_.empty()); } class ConsumeVariableFailure : public testing::TestWithParam {}; @@ -295,7 +295,7 @@ TEST(InternalRegexGen, VariableRegexDefaultMatch) { ASSERT_OK(var); std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc", toRegexPattern(var->parsed_value), &capture)); + EXPECT_TRUE(RE2::FullMatch("abc", toRegexPattern(var->parsed_value_), &capture)); EXPECT_EQ(capture, "abc"); } @@ -303,7 +303,7 @@ TEST(InternalRegexGen, VariableRegexDefaultNotMatch) { absl::StatusOr> var = consumeVariable("{var}"); ASSERT_OK(var); - EXPECT_FALSE(RE2::FullMatch("abc/def", toRegexPattern(var->parsed_value))); + EXPECT_FALSE(RE2::FullMatch("abc/def", toRegexPattern(var->parsed_value_))); } TEST(InternalRegexGen, VariableRegexSegmentsMatch) { @@ -311,7 +311,7 @@ TEST(InternalRegexGen, VariableRegexSegmentsMatch) { ASSERT_OK(var); std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); + EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value_), &capture)); EXPECT_EQ(capture, "abc/123/def"); } @@ -320,7 +320,7 @@ TEST(InternalRegexGen, VariableRegexTextGlobMatch) { ASSERT_OK(var); std::string capture; - EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value), &capture)); + EXPECT_TRUE(RE2::FullMatch("abc/123/def", toRegexPattern(var->parsed_value_), &capture)); EXPECT_EQ(capture, "abc/123/def"); } @@ -329,7 +329,7 @@ TEST(InternalRegexGen, VariableRegexNamedCapture) { absl::StatusOr> var = consumeVariable("{var=*}"); ASSERT_OK(var); - RE2 regex = RE2(toRegexPattern(var->parsed_value)); + RE2 regex = RE2(toRegexPattern(var->parsed_value_)); ASSERT_EQ(regex.NumberOfCapturingGroups(), 1); // Full matched string + capture groups From 5525bc8efa6ab86cfb293925fe0618d89eda214f Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 19:05:31 +0000 Subject: [PATCH 152/238] Add coverage threshold Signed-off-by: silverstar195 --- test/per_file_coverage.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/per_file_coverage.sh b/test/per_file_coverage.sh index d0e7f1a48d20d..d06b8bc6975fc 100755 --- a/test/per_file_coverage.sh +++ b/test/per_file_coverage.sh @@ -63,6 +63,7 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/filters/network/sni_dynamic_forward_proxy:95.2" "source/extensions/filters/network/thrift_proxy/router:96.4" "source/extensions/filters/network/wasm:95.7" +"source/extensions/path/pattern_template_lib:93.2" "source/extensions/filters/udp:96.4" "source/extensions/filters/udp/dns_filter:96.1" "source/extensions/health_checkers:95.7" From e930ee91d56c75cc6cf73ee3d84bb622010ef1c8 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 19:12:56 +0000 Subject: [PATCH 153/238] Add coverage threshold Signed-off-by: silverstar195 --- test/per_file_coverage.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/per_file_coverage.sh b/test/per_file_coverage.sh index d0e7f1a48d20d..d1354376ab202 100755 --- a/test/per_file_coverage.sh +++ b/test/per_file_coverage.sh @@ -70,6 +70,7 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/io_socket:96.2" "source/extensions/io_socket/user_space:96.2" "source/extensions/network/dns_resolver/getaddrinfo:96.3" +"source/extensions/path/rewrite/pattern_template:89.1" "source/extensions/rate_limit_descriptors:95.5" "source/extensions/rate_limit_descriptors/expr:95.5" "source/extensions/stat_sinks/common:96.4" From 200fc40c088c177a11e7df9ee8b09491d61a7789 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 19:16:00 +0000 Subject: [PATCH 154/238] Add coverage threshold Signed-off-by: silverstar195 --- .../path/pattern_template_lib/pattern_template_internal_test.cc | 2 +- test/per_file_coverage.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc index a8fddde8b4a20..4b52e13ee2bd4 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc @@ -462,7 +462,7 @@ TEST_P(GenPatternRegexWithoutMatch, WithCapture) { } } // namespace -} // namespace PatternTemplateInternal +} // namespace Internal } // namespace PatternTemplate } // namespace Extensions } // namespace Envoy diff --git a/test/per_file_coverage.sh b/test/per_file_coverage.sh index d06b8bc6975fc..3283dc9ebc9bf 100755 --- a/test/per_file_coverage.sh +++ b/test/per_file_coverage.sh @@ -63,7 +63,6 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/filters/network/sni_dynamic_forward_proxy:95.2" "source/extensions/filters/network/thrift_proxy/router:96.4" "source/extensions/filters/network/wasm:95.7" -"source/extensions/path/pattern_template_lib:93.2" "source/extensions/filters/udp:96.4" "source/extensions/filters/udp/dns_filter:96.1" "source/extensions/health_checkers:95.7" @@ -71,6 +70,7 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/io_socket:96.2" "source/extensions/io_socket/user_space:96.2" "source/extensions/network/dns_resolver/getaddrinfo:96.3" +"source/extensions/path/pattern_template_lib:93.2" "source/extensions/rate_limit_descriptors:95.5" "source/extensions/rate_limit_descriptors/expr:95.5" "source/extensions/stat_sinks/common:96.4" From 7be7de65be6f611f7163a9ae1d7a04d968caa708 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 19:18:47 +0000 Subject: [PATCH 155/238] Spelling Signed-off-by: silverstar195 --- .../extensions/path/pattern_template_lib/pattern_template.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 7a1142e0f0ac4..1df8529701eb0 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -43,8 +43,8 @@ absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_ // Returns if path_template and rewrite_template have valid variables. // Every variable in rewrite MUST be present in match // For example: -// Match: /foo/{bar}/{var} and Rewrite: /goo/{var} is vaild -// Match: /foo/{bar} and Rewrite: /goo/{bar}/{var} is invaild. Match is missing {var}. +// Match: /foo/{bar}/{var} and Rewrite: /goo/{var} is valid +// Match: /foo/{bar} and Rewrite: /goo/{bar}/{var} is invalid. Match is missing {var}. absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, const std::string& capture_regex); From af6402c43bea1cc429c67bb669a65cf32dbce644 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 19:27:25 +0000 Subject: [PATCH 156/238] Formating Signed-off-by: silverstar195 --- .../path/pattern_template_lib/pattern_template.cc | 6 ++---- .../extensions/path/pattern_template_lib/pattern_template.h | 3 +-- .../path/pattern_template_lib/pattern_template_internal.h | 3 ++- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc index 6a2de41bf4b11..631fe9fd285d4 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -26,16 +26,14 @@ using Internal::ParsedUrlPattern; #endif absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { - absl::StatusOr status = - Internal::parseURLPatternSyntax(url_pattern); + absl::StatusOr status = Internal::parseURLPatternSyntax(url_pattern); if (!status.ok()) { return status.status(); } return Internal::toRegexPattern(*status); } -absl::StatusOr> -parseRewritePattern(absl::string_view pattern) { +absl::StatusOr> parseRewritePattern(absl::string_view pattern) { std::vector result; // The pattern should start with a '/' and thus the first segment should diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 1df8529701eb0..22b4064c8453e 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -28,8 +28,7 @@ absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url // Parses the specified pattern into a sequence of segments (which are // either literals or variable names). -absl::StatusOr> -parseRewritePattern(absl::string_view pattern); +absl::StatusOr> parseRewritePattern(absl::string_view pattern); // Returns the parsed Url rewrite pattern to be used by // RewriteURLTemplatePattern() |capture_regex| should diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h index 3e3c213761eba..b7ea84e92f4a2 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.h +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.h @@ -89,7 +89,8 @@ bool isValidVariableName(absl::string_view indent); * portion of the pattern in |unconsumed_pattern|. */ template struct ParsedResult { - ParsedResult(T val, absl::string_view pattern) : parsed_value_(val), unconsumed_pattern_(pattern) {} + ParsedResult(T val, absl::string_view pattern) + : parsed_value_(val), unconsumed_pattern_(pattern) {} T parsed_value_; absl::string_view unconsumed_pattern_; From a036811639dc5d175133e3ba2cc241a084281481 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 21:09:08 +0000 Subject: [PATCH 157/238] Formating Signed-off-by: silverstar195 --- test/per_file_coverage.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/per_file_coverage.sh b/test/per_file_coverage.sh index 3283dc9ebc9bf..170ba5a920df0 100755 --- a/test/per_file_coverage.sh +++ b/test/per_file_coverage.sh @@ -71,6 +71,7 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/io_socket/user_space:96.2" "source/extensions/network/dns_resolver/getaddrinfo:96.3" "source/extensions/path/pattern_template_lib:93.2" +"source/extensions/path:93.2" "source/extensions/rate_limit_descriptors:95.5" "source/extensions/rate_limit_descriptors/expr:95.5" "source/extensions/stat_sinks/common:96.4" From 796d861d598e6b18b7c692f405a75725ed36fde9 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 21:11:43 +0000 Subject: [PATCH 158/238] Format Signed-off-by: silverstar195 --- .../path/rewrite/pattern_template/pattern_template_rewrite.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index ad8dbab0cfff2..ea5abf8434f7f 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -89,7 +89,7 @@ PatternTemplateRewritePredicate::rewriteUrl(absl::string_view current_pattern, } } - return absl::StatusOr(new_path); + return absl::StatusOr{new_path}; } } // namespace Rewrite From 1133f8fe333c56cb83e05f6b7b19a6ddee9392b1 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 15 Aug 2022 21:13:26 +0000 Subject: [PATCH 159/238] Coverage Signed-off-by: silverstar195 --- test/per_file_coverage.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/per_file_coverage.sh b/test/per_file_coverage.sh index d1354376ab202..f09846ef6c6f8 100755 --- a/test/per_file_coverage.sh +++ b/test/per_file_coverage.sh @@ -70,6 +70,7 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/io_socket:96.2" "source/extensions/io_socket/user_space:96.2" "source/extensions/network/dns_resolver/getaddrinfo:96.3" +"source/extensions/path/rewrite:89.1" "source/extensions/path/rewrite/pattern_template:89.1" "source/extensions/rate_limit_descriptors:95.5" "source/extensions/rate_limit_descriptors/expr:95.5" From ef6ab4f1c0e901f933cf8762de0e7579a7b8d52b Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 14:17:37 +0000 Subject: [PATCH 160/238] Addresses comments Signed-off-by: silverstar195 --- .../path/pattern_template_lib/pattern_template.h | 13 ++++++++----- .../pattern_template_internal.h | 3 +++ tools/code_format/config.yaml | 1 - 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 22b4064c8453e..0d36f2bc3498e 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -30,25 +30,28 @@ absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url // either literals or variable names). absl::StatusOr> parseRewritePattern(absl::string_view pattern); -// Returns the parsed Url rewrite pattern to be used by -// RewriteURLTemplatePattern() |capture_regex| should -// be the regex generated by ConvertURLPatternSyntaxToRegex(). +// Returns the parsed Url rewrite pattern and processes variables to be used by +// RewriteURLTemplatePattern() and in rewrite. +// |capture_regex| should be the regex generated by ConvertURLPatternSyntaxToRegex(). absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); // Returns if provided rewrite pattern is valid absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite); -// Returns if path_template and rewrite_template have valid variables. -// Every variable in rewrite MUST be present in match +// Returns if path_template_rewrite and capture_regex have valid variables. +// Every variable in rewrite MUST be present in match. // For example: // Match: /foo/{bar}/{var} and Rewrite: /goo/{var} is valid // Match: /foo/{bar} and Rewrite: /goo/{bar}/{var} is invalid. Match is missing {var}. absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, const std::string& capture_regex); +// Returns is the match_pattern is valid. +// Validation attempts to parse pattern into literals and variables. absl::Status isValidMatchPattern(const std::string match_pattern); +// Concatenates literals and extracts variable values to form the final rewritten url. absl::StatusOr rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern); diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h index b7ea84e92f4a2..c4f0442d5f26b 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.h +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.h @@ -19,6 +19,9 @@ namespace PatternTemplate { namespace Internal { +/** + * String to be concatenated in rewritten url + */ using Literal = absl::string_view; /** diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index 172100ea3e4ff..15006d31f87e6 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -295,4 +295,3 @@ visibility_excludes: - source/extensions/transport_sockets/common/BUILD - source/extensions/udp_packet_writer/default/BUILD - source/extensions/udp_packet_writer/gso/BUILD -- source/extensions/path/pattern_template_lib/BUILD From 5438e13fbad910ad96d543e2f1c27db457d4543b Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 15:59:17 +0000 Subject: [PATCH 161/238] Added more tests Signed-off-by: silverstar195 --- .../pattern_template_internal.cc | 1 - .../pattern_template_internal.h | 15 --------- .../pattern_template_internal_test.cc | 9 +++++- .../pattern_template_test.cc | 32 +++++++++++++++++++ test/per_file_coverage.sh | 2 -- tools/code_format/config.yaml | 1 + 6 files changed, 41 insertions(+), 19 deletions(-) diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc index 4996ef60e8e23..28f26108bcd0c 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.cc @@ -89,7 +89,6 @@ std::string toString(const Variable val) { return absl::StrCat("{", val.var_name_, "=", absl::StrJoin(val.var_match_, "/", ToStringFormatter()), "}"); - return ""; } template std::string ToStringVisitor::operator()(const T& val) const { diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/pattern_template_lib/pattern_template_internal.h index c4f0442d5f26b..c3024f7c1e1ef 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.h +++ b/source/extensions/path/pattern_template_lib/pattern_template_internal.h @@ -29,21 +29,6 @@ using Literal = absl::string_view; */ enum class Operator { KPathGlob, KTextGlob }; -/** - * Represents a segment of the rewritten URL, including any path segments, - * slash and prefix. - */ -struct RewriteSegment { - - // A string that will be concatenated for rewrite. - absl::string_view literal_; - - // Represents an index into the RE2 capture which value should be used - // to construct the rewritten URL. Note that the index should be greater - // than 0 as 0 index into the whole match RE2 pattern. - int capture_index_; -}; - /** * Represents a pattern variable. Variables are included in both path match and rewrite paths. */ diff --git a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc index 4b52e13ee2bd4..c77d4d3d632d5 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc @@ -112,6 +112,13 @@ TEST(InternalParsing, ConsumeTextGlob) { EXPECT_EQ(result->unconsumed_pattern_, "*abc/123"); } +TEST(InternalParsing, ConsumeInvalidOperator) { + std::string pattern = "/"; + + absl::StatusOr> result = consumeOperator(pattern); + EXPECT_THAT(result, StatusIs(absl::StatusCode::kInvalidArgument)); +} + TEST(InternalParsing, ConsumePathGlob) { std::string pattern = "*/123"; @@ -145,7 +152,7 @@ INSTANTIATE_TEST_SUITE_P(ConsumeVariableFailureTestSuite, ConsumeVariableFailure testing::Values("{var", "{=abc}", "{_var=*}", "{1v}", "{1v=abc}", "{var=***}", "{v-a-r}", "{var=*/abc?q=1}", "{var=abc/a*}", "{var=*def/abc}", "{var=}", "{var=abc=def}", - "{rc=||||(A+yl/}")); + "{rc=||||(A+yl/}", "/")); TEST_P(ConsumeVariableFailure, ConsumeVariableFailureTest) { std::string pattern = GetParam(); diff --git a/test/extensions/path/pattern_template_lib/pattern_template_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_test.cc index 67f3f1c1b9f5b..1570d51b395a0 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_test.cc @@ -53,6 +53,8 @@ TEST(ConvertURLPattern, InvalidPattern) { StatusIs(absl::StatusCode::kInvalidArgument)); EXPECT_THAT(convertURLPatternSyntaxToRegex("/{var12345678901234=*}"), StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(convertURLPatternSyntaxToRegex("/{var12345678901234=*"), + StatusIs(absl::StatusCode::kInvalidArgument)); } class ParseRewriteHelperSuccess : public testing::TestWithParam {}; @@ -330,6 +332,36 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { EXPECT_EQ(rewritten_url.value(), expectedRewrittenUrl()); } +TEST_P(URLPatternMatchAndRewrite, isValidMatchPattern) { + EXPECT_TRUE(isValidMatchPattern("/foo/bar/{goo}").ok()); + EXPECT_TRUE(isValidMatchPattern("/foo/bar/{goo}/{doo}").ok()); + EXPECT_TRUE(isValidMatchPattern("/{hoo}/bar/{goo}").ok()); + + EXPECT_FALSE(isValidMatchPattern("/foo//bar/{goo}").ok()); + EXPECT_FALSE(isValidMatchPattern("//bar/{goo}").ok()); + EXPECT_FALSE(isValidMatchPattern("/foo/bar/{goo}}").ok()); +} + +TEST_P(URLPatternMatchAndRewrite, isValidPathTemplateRewritePattern) { + EXPECT_TRUE(isValidPathTemplateRewritePattern("/foo/bar/{goo}").ok()); + EXPECT_TRUE(isValidPathTemplateRewritePattern("/foo/bar/{goo}/{doo}").ok()); + EXPECT_TRUE(isValidPathTemplateRewritePattern("/{hoo}/bar/{goo}").ok()); + + EXPECT_FALSE(isValidMatchPattern("/foo//bar/{goo}").ok()); + EXPECT_FALSE(isValidMatchPattern("/foo//bar/{goo}").ok()); + EXPECT_FALSE(isValidMatchPattern("/foo/bar/{goo}}").ok()); +} + +TEST_P(URLPatternMatchAndRewrite, isValidSharedVariableSet) { + EXPECT_TRUE(isValidSharedVariableSet("/foo/bar/{goo}", "/foo/bar/{goo}").ok()); + EXPECT_TRUE(isValidSharedVariableSet("/foo/bar/{goo}/{doo}", "/foo/bar/{doo}/{goo}").ok()); + EXPECT_TRUE(isValidSharedVariableSet("/bar/{goo}", "/bar/{goo}").ok()); + + EXPECT_FALSE(isValidSharedVariableSet("/foo/bar/{goo}/{goo}", "/foo/{bar}").ok()); + EXPECT_FALSE(isValidSharedVariableSet("/foo/{goo}", "/foo/bar/").ok()); + EXPECT_FALSE(isValidSharedVariableSet("/foo/bar/{goo}", "/{foo}").ok()); +} + } // namespace } // namespace PatternTemplate diff --git a/test/per_file_coverage.sh b/test/per_file_coverage.sh index 170ba5a920df0..d0e7f1a48d20d 100755 --- a/test/per_file_coverage.sh +++ b/test/per_file_coverage.sh @@ -70,8 +70,6 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/io_socket:96.2" "source/extensions/io_socket/user_space:96.2" "source/extensions/network/dns_resolver/getaddrinfo:96.3" -"source/extensions/path/pattern_template_lib:93.2" -"source/extensions/path:93.2" "source/extensions/rate_limit_descriptors:95.5" "source/extensions/rate_limit_descriptors/expr:95.5" "source/extensions/stat_sinks/common:96.4" diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index 15006d31f87e6..172100ea3e4ff 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -295,3 +295,4 @@ visibility_excludes: - source/extensions/transport_sockets/common/BUILD - source/extensions/udp_packet_writer/default/BUILD - source/extensions/udp_packet_writer/gso/BUILD +- source/extensions/path/pattern_template_lib/BUILD From fa2780162e020f707c252addf84d0bfb78dfd015 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 16:03:14 +0000 Subject: [PATCH 162/238] Change casing Signed-off-by: silverstar195 --- .../path/pattern_template_lib/pattern_template_test.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/extensions/path/pattern_template_lib/pattern_template_test.cc b/test/extensions/path/pattern_template_lib/pattern_template_test.cc index 1570d51b395a0..317b8b1e5209b 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_test.cc +++ b/test/extensions/path/pattern_template_lib/pattern_template_test.cc @@ -332,7 +332,7 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { EXPECT_EQ(rewritten_url.value(), expectedRewrittenUrl()); } -TEST_P(URLPatternMatchAndRewrite, isValidMatchPattern) { +TEST_P(URLPatternMatchAndRewrite, IsValidMatchPattern) { EXPECT_TRUE(isValidMatchPattern("/foo/bar/{goo}").ok()); EXPECT_TRUE(isValidMatchPattern("/foo/bar/{goo}/{doo}").ok()); EXPECT_TRUE(isValidMatchPattern("/{hoo}/bar/{goo}").ok()); @@ -342,7 +342,7 @@ TEST_P(URLPatternMatchAndRewrite, isValidMatchPattern) { EXPECT_FALSE(isValidMatchPattern("/foo/bar/{goo}}").ok()); } -TEST_P(URLPatternMatchAndRewrite, isValidPathTemplateRewritePattern) { +TEST_P(URLPatternMatchAndRewrite, IsValidPathTemplateRewritePattern) { EXPECT_TRUE(isValidPathTemplateRewritePattern("/foo/bar/{goo}").ok()); EXPECT_TRUE(isValidPathTemplateRewritePattern("/foo/bar/{goo}/{doo}").ok()); EXPECT_TRUE(isValidPathTemplateRewritePattern("/{hoo}/bar/{goo}").ok()); @@ -352,7 +352,7 @@ TEST_P(URLPatternMatchAndRewrite, isValidPathTemplateRewritePattern) { EXPECT_FALSE(isValidMatchPattern("/foo/bar/{goo}}").ok()); } -TEST_P(URLPatternMatchAndRewrite, isValidSharedVariableSet) { +TEST_P(URLPatternMatchAndRewrite, IsValidSharedVariableSet) { EXPECT_TRUE(isValidSharedVariableSet("/foo/bar/{goo}", "/foo/bar/{goo}").ok()); EXPECT_TRUE(isValidSharedVariableSet("/foo/bar/{goo}/{doo}", "/foo/bar/{doo}/{goo}").ok()); EXPECT_TRUE(isValidSharedVariableSet("/bar/{goo}", "/bar/{goo}").ok()); From 68d22dc50be40bb24a7416f6f876a1aedadc421e Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 16:52:49 +0000 Subject: [PATCH 163/238] Merge master Signed-off-by: silverstar195 --- tools/code_format/check_format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index bfa680c57cbb6..12fe50a557e66 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -1080,7 +1080,7 @@ def normalize_path(path): def check_visibility(error_messages): command = ( "git diff $(tools/git/last_github_commit.sh) -- source/extensions/* %s |grep '+.*visibility ='" - % ''.join([f"':(exclude){c}' " for c in format_checker.config["visibility_excludes"]])) + % [f"':(exclude){c}' " for c in format_checker.config["visibility_excludes"]]) try: output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() if output: From 516a086b0e03f5a0d32fad7cd13419070aba7e9b Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 16:53:19 +0000 Subject: [PATCH 164/238] Unformat Signed-off-by: silverstar195 --- tools/code_format/check_format.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index 12fe50a557e66..6b7517327d51a 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -7,21 +7,17 @@ import os import pathlib import re -import shutil -import stat import subprocess +import stat import sys import traceback +import shutil +from functools import cached_property +from typing import Callable, Dict, List, Pattern, Tuple, Union + # The way this script is currently used (ie no bazel) it relies on system deps. # As `pyyaml` is present in `envoy-build-ubuntu` it should be safe to use here. import yaml -from functools import cached_property -from typing import Callable -from typing import Dict -from typing import List -from typing import Pattern -from typing import Tuple -from typing import Union import paths From 73cf21528d75a7416252d07c5facb0b464ec3c99 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 17:04:16 +0000 Subject: [PATCH 165/238] Change over to string_view Signed-off-by: silverstar195 --- envoy/router/path_match.h | 2 +- envoy/router/path_rewrite.h | 2 +- .../path/match/pattern_template/pattern_template_match.cc | 2 +- .../path/match/pattern_template/pattern_template_match.h | 2 +- .../path/pattern_template_lib/pattern_template.cc | 8 ++++---- .../path/pattern_template_lib/pattern_template.h | 8 ++++---- .../rewrite/pattern_template/pattern_template_rewrite.h | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/envoy/router/path_match.h b/envoy/router/path_match.h index d0e3e23f73e1d..dcd7f041303b3 100644 --- a/envoy/router/path_match.h +++ b/envoy/router/path_match.h @@ -30,7 +30,7 @@ class PathMatchPredicate : Logger::Loggable { /** * @return the match pattern of the predicate. */ - virtual std::string pattern() const PURE; + virtual absl::string_view pattern() const PURE; /** * @return the name of the current predicate. diff --git a/envoy/router/path_rewrite.h b/envoy/router/path_rewrite.h index 5210c217d3e88..a8c612f23df59 100644 --- a/envoy/router/path_rewrite.h +++ b/envoy/router/path_rewrite.h @@ -44,7 +44,7 @@ class PathRewritePredicate : Logger::Loggable { /** * @return the rewrite pattern of the predicate. */ - virtual std::string pattern() const PURE; + virtual absl::string_view pattern() const PURE; /** * @return the name of the rewrite predicate. diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.cc b/source/extensions/path/match/pattern_template/pattern_template_match.cc index 30e62bc7dcde5..405b08abb4ec9 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.cc +++ b/source/extensions/path/match/pattern_template/pattern_template_match.cc @@ -24,7 +24,7 @@ bool PatternTemplateMatchPredicate::match(absl::string_view pattern) const { matching_pattern_regex); } -std::string PatternTemplateMatchPredicate::pattern() const { return path_template_; } +absl::string_view PatternTemplateMatchPredicate::pattern() const { return path_template_; } } // namespace Match } // namespace PatternTemplate diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index 9c32af4f042c8..11074a845379a 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -26,7 +26,7 @@ class PatternTemplateMatchPredicate : public Router::PathMatchPredicate { bool match(absl::string_view pattern) const override; - std::string pattern() const override; + absl::string_view pattern() const override; absl::string_view name() const override { return NAME; } private: diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc index c40620b618005..388e2347f906b 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -116,16 +116,16 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) return parsed_pattern; } -absl::Status isValidMatchPattern(const std::string path_template_match) { +absl::Status isValidMatchPattern(absl::string_view path_template_match) { return convertURLPatternSyntaxToRegex(path_template_match).status(); } -absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { +absl::Status isValidPathTemplateRewritePattern(absl::string_view path_template_rewrite) { return parseRewritePatternHelper(path_template_rewrite).status(); } -absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, - const std::string& capture_regex) { +absl::Status isValidSharedVariableSet(absl::string_view path_template_rewrite, + absl::string_view capture_regex) { absl::StatusOr status = convertURLPatternSyntaxToRegex(capture_regex).value(); if (!status.ok()) { return status.status(); diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 263fc7cabe02d..1358417305d76 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -42,13 +42,13 @@ absl::StatusOr rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 13afcd7555b23..492e2f50998db 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -31,7 +31,7 @@ class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { rewrite_config) : url_rewrite_pattern_(rewrite_config.path_template_rewrite()) {} - std::string pattern() const override { return url_rewrite_pattern_; } + absl::string_view pattern() const override { return url_rewrite_pattern_; } absl::StatusOr rewriteUrl(absl::string_view current_pattern, absl::string_view matched_path) const override; From a658592bab9dae690aaa2af5cf5a66a0cd329812 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 17:14:48 +0000 Subject: [PATCH 166/238] Move to string_view Signed-off-by: silverstar195 --- .../path/pattern_template_lib/pattern_template.cc | 8 ++++---- .../path/pattern_template_lib/pattern_template.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/pattern_template_lib/pattern_template.cc index 631fe9fd285d4..1c23170324181 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.cc +++ b/source/extensions/path/pattern_template_lib/pattern_template.cc @@ -111,16 +111,16 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) return parsed_pattern; } -absl::Status isValidMatchPattern(const std::string path_template_match) { +absl::Status isValidMatchPattern(absl::string_view path_template_match) { return convertURLPatternSyntaxToRegex(path_template_match).status(); } -absl::Status isValidPathTemplateRewritePattern(const std::string& path_template_rewrite) { +absl::Status isValidPathTemplateRewritePattern(absl::string_view path_template_rewrite) { return parseRewritePattern(path_template_rewrite).status(); } -absl::Status isValidSharedVariableSet(const std::string& path_template_rewrite, - const std::string& capture_regex) { +absl::Status isValidSharedVariableSet(absl::string_view path_template_rewrite, + absl::string_view capture_regex) { absl::StatusOr status = convertURLPatternSyntaxToRegex(capture_regex).value(); if (!status.ok()) { return status.status(); diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 0d36f2bc3498e..019a731ff0414 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -37,19 +37,19 @@ absl::StatusOr rewriteURLTemplatePattern( From d09feca2285a5c0b64203a8923de502cd93859bc Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 17:22:13 +0000 Subject: [PATCH 167/238] Change factory Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index d963f5a9b361c..7726b1a6f4a83 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -362,17 +362,17 @@ PathRewritePolicyImpl::PathRewritePolicyImpl( ProtobufMessage::ValidationVisitor& validator) : enabled_(true) { - const auto& predicate_factory = - &Envoy::Config::Utility::getAndCheckFactory(typed_config); + auto& predicate_factory = + Envoy::Config::Utility::getAndCheckFactory(typed_config); predicate_config_ = Envoy::Config::Utility::translateAnyToFactoryConfig( - typed_config.typed_config(), validator, *predicate_factory); + typed_config.typed_config(), validator, predicate_factory); // Validate config format and inputs when creating factory. // As the validation and create are nearly 1:1 store for later use. // Predicate is only ever created once. absl::StatusOr config_or_error = - predicate_factory->createPathRewritePredicate(*predicate_config_); + predicate_factory.createPathRewritePredicate(*predicate_config_); if (!config_or_error.ok()) { throw EnvoyException(std::string(config_or_error.status().message())); @@ -390,17 +390,17 @@ PathMatchPolicyImpl::PathMatchPolicyImpl( ProtobufMessage::ValidationVisitor& validator) : enabled_(true) { - const auto& predicate_factory = - &Envoy::Config::Utility::getAndCheckFactory(typed_config); + auto& predicate_factory = + Envoy::Config::Utility::getAndCheckFactory(typed_config); predicate_config_ = Envoy::Config::Utility::translateAnyToFactoryConfig( - typed_config.typed_config(), validator, *predicate_factory); + typed_config.typed_config(), validator, predicate_factory); // Validate config format and inputs when creating factory. // As the validation and create are nearly 1:1 store for later use. // Predicate is only ever created once. absl::StatusOr config_or_error = - predicate_factory->createPathMatchPredicate(*predicate_config_); + predicate_factory.createPathMatchPredicate(*predicate_config_); if (!config_or_error.ok()) { throw EnvoyException(std::string(config_or_error.status().message())); From 4824c3012b9296a8db61f734ba42221438154862 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 19:37:43 +0000 Subject: [PATCH 168/238] Various comments Signed-off-by: silverstar195 --- envoy/router/path_match.h | 18 ++--- envoy/router/path_rewrite.h | 36 +++++----- envoy/router/router.h | 20 ++++-- source/common/router/config_impl.cc | 65 ++++++++----------- source/common/router/config_impl.h | 18 ++--- .../path/match/pattern_template/config.cc | 2 +- .../path/match/pattern_template/config.h | 9 +-- .../pattern_template_match.cc | 6 +- .../pattern_template/pattern_template_match.h | 18 ++++- .../pattern_template_lib/pattern_template.h | 2 +- .../path/rewrite/pattern_template/config.cc | 2 +- .../path/rewrite/pattern_template/config.h | 9 +-- .../pattern_template_rewrite.cc | 14 ++-- .../pattern_template_rewrite.h | 19 ++++-- test/common/router/config_impl_test.cc | 4 +- test/mocks/router/mocks.h | 8 +-- 16 files changed, 137 insertions(+), 113 deletions(-) diff --git a/envoy/router/path_match.h b/envoy/router/path_match.h index dcd7f041303b3..a675e65b32be8 100644 --- a/envoy/router/path_match.h +++ b/envoy/router/path_match.h @@ -14,10 +14,10 @@ namespace Router { * Decides if the target route path matches the provided pattern. * Subclassing Logger::Loggable so that implementations can log details. */ -class PathMatchPredicate : Logger::Loggable { +class PathMatcher : Logger::Loggable { public: - PathMatchPredicate() = default; - virtual ~PathMatchPredicate() = default; + PathMatcher() = default; + virtual ~PathMatcher() = default; /** * Used to determine if the current url matches the predicate pattern. @@ -25,7 +25,7 @@ class PathMatchPredicate : Logger::Loggable { * @param url current url of route * @return true if route url matches the predicate pattern. */ - virtual bool match(absl::string_view url) const PURE; + virtual bool match(absl::string_view path) const PURE; /** * @return the match pattern of the predicate. @@ -38,21 +38,21 @@ class PathMatchPredicate : Logger::Loggable { virtual absl::string_view name() const PURE; }; -using PathMatchPredicateSharedPtr = std::shared_ptr; +using PathMatcherSharedPtr = std::shared_ptr; /** * Factory for PathMatchPredicateFactory. */ -class PathMatchPredicateFactory : public Envoy::Config::TypedFactory { +class PathMatcherFactory : public Envoy::Config::TypedFactory { public: - ~PathMatchPredicateFactory() override = default; + ~PathMatcherFactory() override = default; /** * @param config contains the proto stored in TypedExtensionConfig for the predicate. * @return an PathMatchPredicateSharedPtr. */ - virtual absl::StatusOr - createPathMatchPredicate(const Protobuf::Message& config) PURE; + virtual absl::StatusOr + createPathMatcher(const Protobuf::Message& config) PURE; ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; diff --git a/envoy/router/path_rewrite.h b/envoy/router/path_rewrite.h index a8c612f23df59..a603a18d86df0 100644 --- a/envoy/router/path_rewrite.h +++ b/envoy/router/path_rewrite.h @@ -15,19 +15,19 @@ namespace Router { * Creates the new route path based on the provided rewrite pattern. * Subclassing Logger::Loggable so that implementations can log details. */ -class PathRewritePredicate : Logger::Loggable { +class PathRewriter : Logger::Loggable { public: - PathRewritePredicate() = default; - virtual ~PathRewritePredicate() = default; + PathRewriter() = default; + virtual ~PathRewriter() = default; /** - * Used to determine if the match policy is compatible. + * Used to determine if the matcher policy is compatible. * * @param path_match_policy current path match policy for route * @param active_policy true if user provided policy * @return true if current path match policy is acceptable */ - virtual absl::Status isCompatibleMatchPolicy(PathMatchPredicateSharedPtr path_match_policy, + virtual absl::Status isCompatibleMatchPolicy(PathMatcherSharedPtr path_match_policy, bool active_policy) const PURE; /** @@ -38,45 +38,45 @@ class PathRewritePredicate : Logger::Loggable { * @param matched_path pattern to rewrite the url to * @return the rewritten url. */ - virtual absl::StatusOr rewriteUrl(absl::string_view url, + virtual absl::StatusOr rewriteUrl(absl::string_view current_pattern, absl::string_view matched_path) const PURE; /** - * @return the rewrite pattern of the predicate. + * @return the rewrite pattern. */ virtual absl::string_view pattern() const PURE; /** - * @return the name of the rewrite predicate. + * @return the name. */ virtual absl::string_view name() const PURE; }; -using PathRewritePredicateSharedPtr = std::shared_ptr; +using PathRewriterSharedPtr = std::shared_ptr; /** - * Factory for PatternRewriteTemplatePredicate. + * Factory for PathRewrite. */ -class PathRewritePredicateFactory : public Envoy::Config::TypedFactory { +class PathRewriterFactory : public Envoy::Config::TypedFactory { public: - ~PathRewritePredicateFactory() override = default; + ~PathRewriterFactory() override = default; /** - * @param rewrite_config contains the proto stored in TypedExtensionConfig for the predicate. - * @return an PathRewritePredicateSharedPtr. + * @param rewrite_config contains the proto stored in TypedExtensionConfig. + * @return an PathRewriterSharedPtr. */ - virtual absl::StatusOr - createPathRewritePredicate(const Protobuf::Message& rewrite_config) PURE; + virtual absl::StatusOr + createPathRewriter(const Protobuf::Message& rewrite_config) PURE; ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; /** - * @return the name of the rewrite pattern predicate to be created. + * @return the name of the rewrite pattern to be created. */ std::string name() const override PURE; /** - * @return the category of the rewrite pattern predicate to be created. + * @return the category of the rewrite pattern to be created. */ std::string category() const override { return "envoy.path.rewrite"; } }; diff --git a/envoy/router/router.h b/envoy/router/router.h index 4d987fac9628d..8738558b5906b 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -317,14 +317,14 @@ class PathMatchPolicy { virtual bool enabled() const PURE; /** - * Returns the stored target route predicate. - * @return a PathMatchPredicate instance. + * Returns the stored target route PathMatcher. + * @return a PathMatcher instance. */ - virtual PathMatchPredicateSharedPtr predicate() const PURE; + virtual PathMatcherSharedPtr path_matcher() const PURE; }; /** - * PathRewritePolicy from the route configuration. + * PathRewriterPolicy from the route configuration. */ class PathRewritePolicy { public: @@ -336,10 +336,10 @@ class PathRewritePolicy { virtual bool enabled() const PURE; /** - * Returns the stored target route predicate. - * @return a PathRewritePredicate instance. + * Returns the stored target route PathRewriter. + * @return a PathRewriter instance. */ - virtual PathRewritePredicateSharedPtr predicate() const PURE; + virtual PathRewriterSharedPtr path_rewriter() const PURE; }; /** @@ -960,8 +960,14 @@ class RouteEntry : public ResponseEntry { */ virtual const InternalRedirectPolicy& internalRedirectPolicy() const PURE; + /** + * @return const PathMatchPolicy& the path match policy for the route. + */ virtual const PathMatchPolicy& pathMatchPolicy() const PURE; + /** + * @return const PathRewritePolicy& the path match rewrite for the route. + */ virtual const PathRewritePolicy& pathRewritePolicy() const PURE; /** diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 7726b1a6f4a83..bc5cacea63860 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -355,6 +355,7 @@ std::vector InternalRedirectPolicyImpl::pred } return predicates; } + PathRewritePolicyImpl::PathRewritePolicyImpl() : enabled_(false){}; PathRewritePolicyImpl::PathRewritePolicyImpl( @@ -362,26 +363,21 @@ PathRewritePolicyImpl::PathRewritePolicyImpl( ProtobufMessage::ValidationVisitor& validator) : enabled_(true) { - auto& predicate_factory = - Envoy::Config::Utility::getAndCheckFactory(typed_config); + auto& factory = Envoy::Config::Utility::getAndCheckFactory(typed_config); - predicate_config_ = Envoy::Config::Utility::translateAnyToFactoryConfig( - typed_config.typed_config(), validator, predicate_factory); + config_ = Envoy::Config::Utility::translateAnyToFactoryConfig(typed_config.typed_config(), + validator, factory); - // Validate config format and inputs when creating factory. - // As the validation and create are nearly 1:1 store for later use. - // Predicate is only ever created once. - absl::StatusOr config_or_error = - predicate_factory.createPathRewritePredicate(*predicate_config_); + absl::StatusOr rewriter = factory.createPathRewriter(*config_); - if (!config_or_error.ok()) { - throw EnvoyException(std::string(config_or_error.status().message())); + if (!rewriter.ok()) { + throw EnvoyException(std::string(rewriter.status().message())); } - predicate_ = config_or_error.value(); + rewriter_ = rewriter.value(); } -PathRewritePredicateSharedPtr PathRewritePolicyImpl::predicate() const { return predicate_; } +PathRewriterSharedPtr PathRewritePolicyImpl::path_rewriter() const { return rewriter_; } PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; @@ -390,26 +386,21 @@ PathMatchPolicyImpl::PathMatchPolicyImpl( ProtobufMessage::ValidationVisitor& validator) : enabled_(true) { - auto& predicate_factory = - Envoy::Config::Utility::getAndCheckFactory(typed_config); + auto& factory = Envoy::Config::Utility::getAndCheckFactory(typed_config); - predicate_config_ = Envoy::Config::Utility::translateAnyToFactoryConfig( - typed_config.typed_config(), validator, predicate_factory); + config_ = Envoy::Config::Utility::translateAnyToFactoryConfig(typed_config.typed_config(), + validator, factory); - // Validate config format and inputs when creating factory. - // As the validation and create are nearly 1:1 store for later use. - // Predicate is only ever created once. - absl::StatusOr config_or_error = - predicate_factory.createPathMatchPredicate(*predicate_config_); + absl::StatusOr matcher = factory.createPathMatcher(*config_); - if (!config_or_error.ok()) { - throw EnvoyException(std::string(config_or_error.status().message())); + if (!matcher.ok()) { + throw EnvoyException(std::string(matcher.status().message())); } - predicate_ = config_or_error.value(); + path_matcher_ = matcher.value(); } -PathMatchPredicateSharedPtr PathMatchPolicyImpl::predicate() const { return predicate_; } +PathMatcherSharedPtr PathMatchPolicyImpl::path_matcher() const { return path_matcher_; } absl::flat_hash_set InternalRedirectPolicyImpl::buildRedirectResponseCodes( const envoy::config::route::v3::InternalRedirectPolicy& policy_config) const { @@ -727,10 +718,10 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, } if (path_rewrite_policy_.enabled()) { - absl::Status compatible_polices = path_rewrite_policy_.predicate()->isCompatibleMatchPolicy( - path_match_policy_.predicate(), path_match_policy_.enabled()); - if (!compatible_polices.ok()) { - throw EnvoyException(std::string(compatible_polices.message())); + absl::Status compatible_status = path_rewrite_policy_.path_rewriter()->isCompatibleMatchPolicy( + path_match_policy_.path_matcher(), path_match_policy_.enabled()); + if (!compatible_status.ok()) { + throw EnvoyException(std::string(compatible_status.message())); } } @@ -899,7 +890,7 @@ void RouteEntryImplBase::finalizeRequestHeaders(Http::RequestHeaderMap& headers, // Handle path rewrite absl::optional container; if (!getPathRewrite(headers, container).empty() || regex_rewrite_ != nullptr || - path_match_policy_.enabled()) { + path_rewrite_policy_.enabled()) { rewritePathHeader(headers, insert_envoy_original_path); } } @@ -1036,10 +1027,10 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa } if (path_rewrite_policy_.enabled()) { - auto just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); + absl::string_view just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); absl::StatusOr new_path = - path_rewrite_policy_.predicate()->rewriteUrl(just_path, matched_path); + path_rewrite_policy_.path_rewriter()->rewriteUrl(just_path, matched_path); // if rewrite fails return old path. if (!new_path.ok()) { @@ -1501,18 +1492,18 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - match_pattern_(path_match_policy_.predicate()->pattern()){}; + match_pattern_(path_match_policy_.path_matcher()->pattern()){}; void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { - finalizePathHeader(headers, path_match_policy_.predicate()->pattern(), + finalizePathHeader(headers, path_match_policy_.path_matcher()->pattern(), insert_envoy_original_path); } absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { return currentUrlPathAfterRewriteWithMatchedPath(headers, - path_match_policy_.predicate()->pattern()); + path_match_policy_.path_matcher()->pattern()); } RouteConstSharedPtr @@ -1520,7 +1511,7 @@ PathMatchPolicyRouteEntryImpl::matches(const Http::RequestHeaderMap& headers, const StreamInfo::StreamInfo& stream_info, uint64_t random_value) const { if (RouteEntryImplBase::matchRoute(headers, stream_info, random_value) && - path_match_policy_.predicate()->match(headers.getPathValue())) { + path_match_policy_.path_matcher()->match(headers.getPathValue())) { return clusterEntry(headers, random_value); } return nullptr; diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 421209abbf8f4..33b1934aeec65 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -466,17 +466,18 @@ class PathMatchPolicyImpl : public PathMatchPolicy { PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, ProtobufMessage::ValidationVisitor& validator); - // Default constructor that disables internal redirect. + // Constructs a disabled PathMatchPolicyImpl PathMatchPolicyImpl(); + // Router::PathMatchPolicy bool enabled() const override { return enabled_; } - PathMatchPredicateSharedPtr predicate() const override; + PathMatcherSharedPtr path_matcher() const override; private: const bool enabled_; - ProtobufTypes::MessagePtr predicate_config_; - PathMatchPredicateSharedPtr predicate_; + ProtobufTypes::MessagePtr config_; + PathMatcherSharedPtr path_matcher_; }; /** @@ -488,17 +489,18 @@ class PathRewritePolicyImpl : public PathRewritePolicy { PathRewritePolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, ProtobufMessage::ValidationVisitor& validator); - // Default constructor that disables internal redirect. + // Constructs a disabled PathRewritePolicyImpl PathRewritePolicyImpl(); + // Router::PathRewritePolicy bool enabled() const override { return enabled_; } - PathRewritePredicateSharedPtr predicate() const override; + PathRewriterSharedPtr path_rewriter() const override; private: const bool enabled_; - ProtobufTypes::MessagePtr predicate_config_; - PathRewritePredicateSharedPtr predicate_; + ProtobufTypes::MessagePtr config_; + PathRewriterSharedPtr rewriter_; }; /** diff --git a/source/extensions/path/match/pattern_template/config.cc b/source/extensions/path/match/pattern_template/config.cc index d89f1841f1a16..995ea4cb2aa64 100644 --- a/source/extensions/path/match/pattern_template/config.cc +++ b/source/extensions/path/match/pattern_template/config.cc @@ -8,7 +8,7 @@ namespace Extensions { namespace PatternTemplate { namespace Match { -REGISTER_FACTORY(PatternTemplateMatchPredicateFactory, Router::PathMatchPredicateFactory); +REGISTER_FACTORY(PatternTemplateMatcherFactory, Router::PathMatcherFactory); } // namespace Match } // namespace PatternTemplate diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 8a316309146ad..3cd7c4d061d61 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -13,10 +13,10 @@ namespace Extensions { namespace PatternTemplate { namespace Match { -class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFactory { +class PatternTemplateMatcherFactory : public Router::PathMatcherFactory { public: - absl::StatusOr - createPathMatchPredicate(const Protobuf::Message& config) override { + absl::StatusOr + createPathMatcher(const Protobuf::Message& config) override { auto path_match_config = MessageUtil::downcastAndValidate< const envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig&>( config, ProtobufMessage::getStrictValidationVisitor()); @@ -26,9 +26,10 @@ class PatternTemplateMatchPredicateFactory : public Router::PathMatchPredicateFa path_match_config.path_template())); } - return std::make_shared(path_match_config); + return std::make_shared(path_match_config); } + // Router::PathMatcherFactory ProtobufTypes::MessagePtr createEmptyConfigProto() override { return std::make_unique< envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig>(); diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.cc b/source/extensions/path/match/pattern_template/pattern_template_match.cc index 405b08abb4ec9..65a4b9546b03e 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.cc +++ b/source/extensions/path/match/pattern_template/pattern_template_match.cc @@ -17,14 +17,14 @@ namespace Extensions { namespace PatternTemplate { namespace Match { -bool PatternTemplateMatchPredicate::match(absl::string_view pattern) const { +bool PatternTemplateMatcher::match(absl::string_view path) const { RE2 matching_pattern_regex = RE2(convertURLPatternSyntaxToRegex(path_template_).value()); return RE2::FullMatch( - PatternTemplateInternal::toStringPiece(Http::PathUtil::removeQueryAndFragment(pattern)), + PatternTemplateInternal::toStringPiece(Http::PathUtil::removeQueryAndFragment(path)), matching_pattern_regex); } -absl::string_view PatternTemplateMatchPredicate::pattern() const { return path_template_; } +absl::string_view PatternTemplateMatcher::pattern() const { return path_template_; } } // namespace Match } // namespace PatternTemplate diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index 11074a845379a..3c3053fc64dcb 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -17,16 +17,28 @@ namespace Match { const absl::string_view NAME = "envoy.path.match.pattern_template.pattern_template_match_predicate"; -class PatternTemplateMatchPredicate : public Router::PathMatchPredicate { +/** + * PatternTemplateMatcher allows matching based on pattern templates. + * Examples of several pattern templates types are below: + * Wildcard: /foo/bar/** + * Will match any path that starts with /foo/bar and any number of following segments + * Variable: /foo/bar/{var} + * Will match any path that starts with /foo/bar and has one additional segment + * Literal: /foo/bar/goo + * Will match only a path of /foo/bar/goo + */ +class PatternTemplateMatcher : public Router::PathMatcher { public: - explicit PatternTemplateMatchPredicate( + explicit PatternTemplateMatcher( const envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig& config) : path_template_(config.path_template()) {} - bool match(absl::string_view pattern) const override; + // Router::PathMatcher + bool match(absl::string_view path) const override; absl::string_view pattern() const override; + absl::string_view name() const override { return NAME; } private: diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/pattern_template_lib/pattern_template.h index 1358417305d76..acbacd50b949f 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/pattern_template_lib/pattern_template.h @@ -46,7 +46,7 @@ absl::Status isValidPathTemplateRewritePattern(absl::string_view path_template_r // Returns if path_template and rewrite_template have valid variables absl::Status isValidSharedVariableSet(absl::string_view path_template_rewrite, - absl::string_view capture_regex); + absl::string_view capture_regex); absl::Status isValidMatchPattern(absl::string_view match_pattern); diff --git a/source/extensions/path/rewrite/pattern_template/config.cc b/source/extensions/path/rewrite/pattern_template/config.cc index 762d1da56098a..3df438c415603 100644 --- a/source/extensions/path/rewrite/pattern_template/config.cc +++ b/source/extensions/path/rewrite/pattern_template/config.cc @@ -8,7 +8,7 @@ namespace Extensions { namespace PatternTemplate { namespace Rewrite { -REGISTER_FACTORY(PatternTemplateRewritePredicateFactory, Router::PathRewritePredicateFactory); +REGISTER_FACTORY(PatternTemplateRewriterFactory, Router::PathRewriterFactory); } // namespace Rewrite } // namespace PatternTemplate diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 491cac7e87760..612def14418f1 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -11,10 +11,10 @@ namespace Extensions { namespace PatternTemplate { namespace Rewrite { -class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredicateFactory { +class PatternTemplateRewriterFactory : public Router::PathRewriterFactory { public: - absl::StatusOr - createPathRewritePredicate(const Protobuf::Message& rewrite_config) override { + absl::StatusOr + createPathRewriter(const Protobuf::Message& rewrite_config) override { auto path_rewrite_config = MessageUtil::downcastAndValidate( @@ -27,9 +27,10 @@ class PatternTemplateRewritePredicateFactory : public Router::PathRewritePredica fmt::format("path_rewrite_policy.path_template_rewrite {} is invalid", path_rewrite_config.path_template_rewrite())); } - return std::make_shared(path_rewrite_config); + return std::make_shared(path_rewrite_config); } + // Router::PathRewriterFactory ProtobufTypes::MessagePtr createEmptyConfigProto() override { return std::make_unique< envoy::extensions::path::rewrite::pattern_template::v3::PatternTemplateRewriteConfig>(); diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index ea5abf8434f7f..9e81936ba603c 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -25,8 +25,9 @@ namespace Rewrite { #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif -absl::Status PatternTemplateRewritePredicate::isCompatibleMatchPolicy( - Router::PathMatchPredicateSharedPtr path_match_predicate, bool active_policy) const { +absl::Status +PatternTemplateRewriter::isCompatibleMatchPolicy(Router::PathMatcherSharedPtr path_match_predicate, + bool active_policy) const { if (!active_policy || path_match_predicate->name() != Extensions::PatternTemplate::Match::NAME) { return absl::InvalidArgumentError(fmt::format("unable to use {} extension without {} extension", Extensions::PatternTemplate::Rewrite::NAME, @@ -45,8 +46,8 @@ absl::Status PatternTemplateRewritePredicate::isCompatibleMatchPolicy( } absl::StatusOr -PatternTemplateRewritePredicate::rewriteUrl(absl::string_view current_pattern, - absl::string_view matched_path) const { +PatternTemplateRewriter::rewriteUrl(absl::string_view pattern, + absl::string_view matched_path) const { absl::StatusOr regex_pattern = convertURLPatternSyntaxToRegex(matched_path); if (!regex_pattern.ok()) { return absl::InvalidArgumentError("Unable to parse url pattern regex"); @@ -70,9 +71,8 @@ PatternTemplateRewritePredicate::rewriteUrl(absl::string_view current_pattern, // First capture is the whole matched regex pattern. int capture_num = regex.NumberOfCapturingGroups() + 1; std::vector captures(capture_num); - if (!regex.Match(PatternTemplateInternal::toStringPiece(current_pattern), /*startpos=*/0, - /*endpos=*/current_pattern.size(), RE2::ANCHOR_BOTH, captures.data(), - captures.size())) { + if (!regex.Match(PatternTemplateInternal::toStringPiece(pattern), /*startpos=*/0, + /*endpos=*/pattern.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { return absl::InvalidArgumentError("Pattern not match"); } diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 492e2f50998db..f36a681853bba 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -24,19 +24,30 @@ namespace Rewrite { const absl::string_view NAME = "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"; -class PatternTemplateRewritePredicate : public Router::PathRewritePredicate { +/** + * PatternTemplateRewriter allows rewriting paths based on match pattern variables provided + * in PatternTemplateMatcher. + * + * Example: + * PatternTemplateMatcher = /foo/bar/{var} + * PatternTemplateRewriter = /foo/{var} + * Will replace segment of path with value of {var} + * e.g. /foo/bar/cat -> /foo/cat + */ +class PatternTemplateRewriter : public Router::PathRewriter { public: - explicit PatternTemplateRewritePredicate( + explicit PatternTemplateRewriter( const envoy::extensions::path::rewrite::pattern_template::v3::PatternTemplateRewriteConfig& rewrite_config) : url_rewrite_pattern_(rewrite_config.path_template_rewrite()) {} + // Router::PathRewriter absl::string_view pattern() const override { return url_rewrite_pattern_; } - absl::StatusOr rewriteUrl(absl::string_view current_pattern, + absl::StatusOr rewriteUrl(absl::string_view pattern, absl::string_view matched_path) const override; - absl::Status isCompatibleMatchPolicy(Router::PathMatchPredicateSharedPtr match_policy, + absl::Status isCompatibleMatchPolicy(Router::PathMatcherSharedPtr match_policy, bool active_policy) const override; absl::string_view name() const override { return NAME; } diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index 1dfd28645ffa3..e3d3ada173e99 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -8822,11 +8822,11 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsFilledFromConfigInRouteAction) { const auto& pattern_match_policy = config.route(headers, 0)->routeEntry()->pathMatchPolicy(); EXPECT_TRUE(pattern_match_policy.enabled()); - EXPECT_EQ(pattern_match_policy.predicate()->pattern(), "/bar/{country}/{lang}"); + EXPECT_EQ(pattern_match_policy.path_matcher()->pattern(), "/bar/{country}/{lang}"); const auto& pattern_rewrite_policy = config.route(headers, 0)->routeEntry()->pathRewritePolicy(); EXPECT_TRUE(pattern_rewrite_policy.enabled()); - EXPECT_EQ(pattern_rewrite_policy.predicate()->pattern(), "/bar/{lang}/{country}"); + EXPECT_EQ(pattern_rewrite_policy.path_rewriter()->pattern(), "/bar/{lang}/{country}"); } TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { diff --git a/test/mocks/router/mocks.h b/test/mocks/router/mocks.h index 6d349d01b2aba..b8246039d7bd8 100644 --- a/test/mocks/router/mocks.h +++ b/test/mocks/router/mocks.h @@ -172,10 +172,10 @@ class MockPathRewritePolicy : public PathRewritePolicy { public: MockPathRewritePolicy(); MOCK_METHOD(bool, enabled, (), (const)); - MOCK_METHOD(PathRewritePredicateSharedPtr, predicate, (), (const)); + MOCK_METHOD(PathRewriterSharedPtr, path_rewriter, (), (const)); }; -class MockPathRewritePredicate : public PathRewritePredicate { +class MockPathRewriter : public PathRewriter { public: MOCK_METHOD(absl::string_view, name, (), (const)); }; @@ -184,10 +184,10 @@ class MockPathMatchPolicy : public PathMatchPolicy { public: MockPathMatchPolicy(); MOCK_METHOD(bool, enabled, (), (const)); - MOCK_METHOD(PathMatchPredicateSharedPtr, predicate, (), (const)); + MOCK_METHOD(PathMatcherSharedPtr, path_matcher, (), (const)); }; -class MockPathMatchPredicate : public PathMatchPredicate { +class MockPathMatcher : public PathMatcher { public: MOCK_METHOD(absl::string_view, name, (), (const)); }; From 776b32dfba889a30c6f00bb5824bf05fbea6f43b Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 19:41:29 +0000 Subject: [PATCH 169/238] Add exclude fix Signed-off-by: silverstar195 --- tools/code_format/check_format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index 6b7517327d51a..b0c49ffa0611b 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -1076,7 +1076,7 @@ def normalize_path(path): def check_visibility(error_messages): command = ( "git diff $(tools/git/last_github_commit.sh) -- source/extensions/* %s |grep '+.*visibility ='" - % [f"':(exclude){c}' " for c in format_checker.config["visibility_excludes"]]) + % "".join([f"':(exclude){c}' " for c in format_checker.config["visibility_excludes"]])) try: output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() if output: From cecbeba8492015abd49c4d93a0090de032c346bc Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 19:59:01 +0000 Subject: [PATCH 170/238] Remove doc Signed-off-by: silverstar195 --- .../path/match/pattern_template/pattern_template_match.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index 3c3053fc64dcb..44bbd10b34434 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -20,8 +20,6 @@ const absl::string_view NAME = "envoy.path.match.pattern_template.pattern_templa /** * PatternTemplateMatcher allows matching based on pattern templates. * Examples of several pattern templates types are below: - * Wildcard: /foo/bar/** - * Will match any path that starts with /foo/bar and any number of following segments * Variable: /foo/bar/{var} * Will match any path that starts with /foo/bar and has one additional segment * Literal: /foo/bar/goo From 3bc293edfa7fc344e8512e5b6901e211c5e8cd49 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 16 Aug 2022 20:36:12 +0000 Subject: [PATCH 171/238] Moved over some tests Signed-off-by: silverstar195 --- .../match/pattern_template/config_test.cc | 14 +++---- .../match/pattern_template/library_test.cc | 10 ++--- .../rewrite/pattern_template/config_test.cc | 20 +++++----- .../rewrite/pattern_template/library_test.cc | 40 +++++++++---------- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/test/extensions/path/match/pattern_template/config_test.cc b/test/extensions/path/match/pattern_template/config_test.cc index 6413bbc88c5d5..b94198ad7b2a5 100644 --- a/test/extensions/path/match/pattern_template/config_test.cc +++ b/test/extensions/path/match/pattern_template/config_test.cc @@ -23,7 +23,7 @@ TEST(ConfigTest, TestEmptyConfig) { TestUtility::loadFromYaml(yaml_string, config); const auto& factory = - &Envoy::Config::Utility::getAndCheckFactory(config); + &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); @@ -50,7 +50,7 @@ TEST(ConfigTest, InvalidConfigSetup) { TestUtility::loadFromYaml(yaml_string, config); const auto& factory = - &Envoy::Config::Utility::getAndCheckFactory(config); + &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); @@ -60,8 +60,8 @@ TEST(ConfigTest, InvalidConfigSetup) { EXPECT_NE(nullptr, message); - absl::StatusOr config_or_error = - factory->createPathMatchPredicate(*message); + absl::StatusOr config_or_error = + factory->createPathMatcher(*message); EXPECT_FALSE(config_or_error.ok()); EXPECT_EQ(config_or_error.status().message(), @@ -80,7 +80,7 @@ TEST(ConfigTest, TestConfigSetup) { TestUtility::loadFromYaml(yaml_string, config); const auto& factory = - &Envoy::Config::Utility::getAndCheckFactory(config); + &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); @@ -90,8 +90,8 @@ TEST(ConfigTest, TestConfigSetup) { EXPECT_NE(nullptr, message); - absl::StatusOr config_or_error = - factory->createPathMatchPredicate(*message); + absl::StatusOr config_or_error = + factory->createPathMatcher(*message); EXPECT_TRUE(config_or_error.ok()); } diff --git a/test/extensions/path/match/pattern_template/library_test.cc b/test/extensions/path/match/pattern_template/library_test.cc index b2f35840dafb4..eb5b41bbf18a1 100644 --- a/test/extensions/path/match/pattern_template/library_test.cc +++ b/test/extensions/path/match/pattern_template/library_test.cc @@ -12,18 +12,18 @@ namespace Extensions { namespace PatternTemplate { namespace Match { -Router::PathMatchPredicateSharedPtr createMatchPredicateFromYaml(std::string yaml_string) { +Router::PathMatcherSharedPtr createMatchPredicateFromYaml(std::string yaml_string) { envoy::config::core::v3::TypedExtensionConfig config; TestUtility::loadFromYaml(yaml_string, config); const auto& factory = - &Envoy::Config::Utility::getAndCheckFactory(config); + &Envoy::Config::Utility::getAndCheckFactory(config); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); - absl::StatusOr config_or_error = - factory->createPathMatchPredicate(*message); + absl::StatusOr config_or_error = + factory->createPathMatcher(*message); return config_or_error.value(); } @@ -36,7 +36,7 @@ TEST(MatchTest, BasicUsage) { path_template: "/bar/{lang}/{country}" )EOF"; - Router::PathMatchPredicateSharedPtr predicate = createMatchPredicateFromYaml(yaml_string); + Router::PathMatcherSharedPtr predicate = createMatcherFromYaml(yaml_string); EXPECT_EQ(predicate->pattern(), "/bar/{lang}/{country}"); EXPECT_EQ(predicate->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); diff --git a/test/extensions/path/rewrite/pattern_template/config_test.cc b/test/extensions/path/rewrite/pattern_template/config_test.cc index fea978a77fd87..5e5bb6ba7786a 100644 --- a/test/extensions/path/rewrite/pattern_template/config_test.cc +++ b/test/extensions/path/rewrite/pattern_template/config_test.cc @@ -23,7 +23,7 @@ TEST(ConfigTest, TestEmptyConfig) { TestUtility::loadFromYaml(yaml_string, config); const auto& factory = - &Envoy::Config::Utility::getAndCheckFactory(config); + &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); EXPECT_EQ(factory->name(), @@ -51,7 +51,7 @@ TEST(ConfigTest, InvalidConfigSetup) { TestUtility::loadFromYaml(yaml_string, config); const auto& factory = - &Envoy::Config::Utility::getAndCheckFactory(config); + &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); EXPECT_EQ(factory->name(), @@ -62,8 +62,8 @@ TEST(ConfigTest, InvalidConfigSetup) { EXPECT_NE(nullptr, message); - absl::StatusOr config_or_error = - factory->createPathRewritePredicate(*message); + absl::StatusOr config_or_error = + factory->createPathRewriter(*message); EXPECT_FALSE(config_or_error.ok()); EXPECT_EQ(config_or_error.status().message(), @@ -82,7 +82,7 @@ TEST(ConfigTest, TestConfigSetup) { TestUtility::loadFromYaml(yaml_string, config); const auto& factory = - &Envoy::Config::Utility::getAndCheckFactory(config); + &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); EXPECT_EQ(factory->name(), @@ -93,8 +93,8 @@ TEST(ConfigTest, TestConfigSetup) { EXPECT_NE(nullptr, message); - absl::StatusOr config_or_error = - factory->createPathRewritePredicate(*message); + absl::StatusOr config_or_error = + factory->createPathRewriter(*message); EXPECT_TRUE(config_or_error.ok()); } @@ -111,7 +111,7 @@ TEST(ConfigTest, TestInvalidConfigSetup) { TestUtility::loadFromYaml(yaml_string, config); const auto& factory = - &Envoy::Config::Utility::getAndCheckFactory(config); + &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); EXPECT_EQ(factory->name(), @@ -122,8 +122,8 @@ TEST(ConfigTest, TestInvalidConfigSetup) { EXPECT_NE(nullptr, message); - absl::StatusOr config_or_error = - factory->createPathRewritePredicate(*message); + absl::StatusOr config_or_error = + factory->createPathRewriter(*message); EXPECT_FALSE(config_or_error.ok()); EXPECT_EQ(config_or_error.status().message(), diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc index 07d2d189cd101..c71b74c80f904 100644 --- a/test/extensions/path/rewrite/pattern_template/library_test.cc +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -13,34 +13,34 @@ namespace Extensions { namespace PatternTemplate { namespace Rewrite { -Router::PathMatchPredicateSharedPtr createMatchPredicateFromYaml(std::string yaml_string) { +Router::PathMatcherSharedPtr createMatchPredicateFromYaml(std::string yaml_string) { envoy::config::core::v3::TypedExtensionConfig config; TestUtility::loadFromYaml(yaml_string, config); const auto& factory = - &Envoy::Config::Utility::getAndCheckFactory(config); + &Envoy::Config::Utility::getAndCheckFactory(config); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); - absl::StatusOr config_or_error = - factory->createPathMatchPredicate(*message); + absl::StatusOr config_or_error = + factory->createPathMatcher(*message); return config_or_error.value(); } -Router::PathRewritePredicateSharedPtr createRewritePredicateFromYaml(std::string yaml_string) { +Router::PathRewriterSharedPtr createRewritePredicateFromYaml(std::string yaml_string) { envoy::config::core::v3::TypedExtensionConfig config; TestUtility::loadFromYaml(yaml_string, config); const auto& factory = - &Envoy::Config::Utility::getAndCheckFactory(config); + &Envoy::Config::Utility::getAndCheckFactory(config); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); - absl::StatusOr config_or_error = - factory->createPathRewritePredicate(*message); + absl::StatusOr config_or_error = + factory->createPathRewriter(*message); return config_or_error.value(); } @@ -53,7 +53,7 @@ TEST(RewriteTest, BasicSetup) { path_template_rewrite: "/bar/{lang}/{country}" )EOF"; - Router::PathRewritePredicateSharedPtr predicate = createRewritePredicateFromYaml(yaml_string); + Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); EXPECT_EQ(predicate->pattern(), "/bar/{lang}/{country}"); EXPECT_EQ(predicate->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); @@ -67,7 +67,7 @@ TEST(RewriteTest, BasicUsage) { path_template_rewrite: "/bar/{lang}/{country}" )EOF"; - Router::PathRewritePredicateSharedPtr predicate = createRewritePredicateFromYaml(yaml_string); + Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); EXPECT_EQ(predicate->rewriteUrl("/bar/en/usa", "/bar/{country}/{lang}").value(), "/bar/usa/en"); EXPECT_EQ(predicate->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); @@ -81,7 +81,7 @@ TEST(RewriteTest, RewriteInvalidRegex) { path_template_rewrite: "/bar/{lang}/{country}" )EOF"; - Router::PathRewritePredicateSharedPtr predicate = createRewritePredicateFromYaml(yaml_string); + Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); absl::StatusOr rewrite_or_error = predicate->rewriteUrl("/bar/en/usa", "/bar/invalid}/{lang}"); EXPECT_FALSE(rewrite_or_error.ok()); @@ -103,9 +103,9 @@ TEST(RewriteTest, MatchPatternValidation) { path_template: "/bar/{lang}/{country}" )EOF"; - Router::PathRewritePredicateSharedPtr rewrite_predicate = - createRewritePredicateFromYaml(rewrite_yaml_string); - Router::PathMatchPredicateSharedPtr match_predicate = + Router::PathRewriterSharedPtr rewrite_predicate = + createRewriterFromYaml(rewrite_yaml_string); + Router::PathMatchrharedPtr match_predicate = createMatchPredicateFromYaml(match_yaml_string); EXPECT_TRUE(rewrite_predicate->isCompatibleMatchPolicy(match_predicate, true).ok()); @@ -126,9 +126,9 @@ TEST(RewriteTest, MatchPatternInactive) { path_template: "/bar/{lang}/{country}" )EOF"; - Router::PathRewritePredicateSharedPtr rewrite_predicate = - createRewritePredicateFromYaml(rewrite_yaml_string); - Router::PathMatchPredicateSharedPtr match_predicate = + Router::PathRewriterSharedPtr rewrite_predicate = + createRewriterFromYaml(rewrite_yaml_string); + Router::PathMatcherSharedPtr match_predicate = createMatchPredicateFromYaml(match_yaml_string); absl::Status error = rewrite_predicate->isCompatibleMatchPolicy(match_predicate, false); @@ -154,9 +154,9 @@ TEST(RewriteTest, MatchPatternMismatchedVars) { path_template: "/bar/{lang}/{country}" )EOF"; - Router::PathRewritePredicateSharedPtr rewrite_predicate = - createRewritePredicateFromYaml(rewrite_yaml_string); - Router::PathMatchPredicateSharedPtr match_predicate = + Router::PathRewriterSharedPtr rewrite_predicate = + createRewriterFromYaml(rewrite_yaml_string); + Router::PathMatcherSharedPtr match_predicate = createMatchPredicateFromYaml(match_yaml_string); absl::Status error = rewrite_predicate->isCompatibleMatchPolicy(match_predicate, true); From 0f39b1cc4179cab58a4fa82af1fc95894e73beec Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 17 Aug 2022 19:27:27 +0000 Subject: [PATCH 172/238] Renamed Signed-off-by: silverstar195 --- .../BUILD | 18 ++++++------ .../proto/BUILD | 4 +-- .../uri_template_rewrite_segments.proto} | 12 ++++---- .../uri_template.cc} | 18 ++++++------ .../uri_template.h} | 17 +++++++---- .../uri_template_internal.cc} | 6 ++-- .../uri_template_internal.h} | 4 +-- .../BUILD | 18 ++++++------ .../uri_template_internal_test.cc} | 6 ++-- .../uri_template_test.cc} | 28 +++++++++---------- tools/code_format/config.yaml | 2 +- 11 files changed, 69 insertions(+), 64 deletions(-) rename source/extensions/path/{pattern_template_lib => uri_template_lib}/BUILD (69%) rename source/extensions/path/{pattern_template_lib => uri_template_lib}/proto/BUILD (73%) rename source/extensions/path/{pattern_template_lib/proto/pattern_template_rewrite_segments.proto => uri_template_lib/proto/uri_template_rewrite_segments.proto} (68%) rename source/extensions/path/{pattern_template_lib/pattern_template.cc => uri_template_lib/uri_template.cc} (88%) rename source/extensions/path/{pattern_template_lib/pattern_template.h => uri_template_lib/uri_template.h} (80%) rename source/extensions/path/{pattern_template_lib/pattern_template_internal.cc => uri_template_lib/uri_template_internal.cc} (98%) rename source/extensions/path/{pattern_template_lib/pattern_template_internal.h => uri_template_lib/uri_template_internal.h} (98%) rename test/extensions/path/{pattern_template_lib => uri_template_lib}/BUILD (52%) rename test/extensions/path/{pattern_template_lib/pattern_template_internal_test.cc => uri_template_lib/uri_template_internal_test.cc} (99%) rename test/extensions/path/{pattern_template_lib/pattern_template_test.cc => uri_template_lib/uri_template_test.cc} (93%) diff --git a/source/extensions/path/pattern_template_lib/BUILD b/source/extensions/path/uri_template_lib/BUILD similarity index 69% rename from source/extensions/path/pattern_template_lib/BUILD rename to source/extensions/path/uri_template_lib/BUILD index dfb084bbc72e2..2a3035758b86d 100644 --- a/source/extensions/path/pattern_template_lib/BUILD +++ b/source/extensions/path/uri_template_lib/BUILD @@ -11,18 +11,18 @@ licenses(["notice"]) # Apache 2 envoy_extension_package() envoy_cc_library( - name = "pattern_template_lib", - srcs = ["pattern_template.cc"], - hdrs = ["pattern_template.h"], + name = "uri_template_lib", + srcs = ["uri_template.cc"], + hdrs = ["uri_template.h"], visibility = [ "//source/common/router:__subpackages__", "//source/extensions/path:__subpackages__", "//test/extensions/path:__subpackages__", ], deps = [ - ":pattern_template_internal_cc", + ":uri_template_internal_cc", "//source/common/http:path_utility_lib", - "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "//source/extensions/path/uri_template_lib/proto:uri_template_rewrite_segements_cc_proto", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", @@ -31,11 +31,11 @@ envoy_cc_library( ) envoy_cc_library( - name = "pattern_template_internal_cc", - srcs = ["pattern_template_internal.cc"], - hdrs = ["pattern_template_internal.h"], + name = "uri_template_internal_cc", + srcs = ["uri_template_internal.cc"], + hdrs = ["uri_template_internal.h"], deps = [ - "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "//source/extensions/path/uri_template_lib/proto:uri_template_rewrite_segements_cc_proto", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/flags:flag", "@com_google_absl//absl/functional:function_ref", diff --git a/source/extensions/path/pattern_template_lib/proto/BUILD b/source/extensions/path/uri_template_lib/proto/BUILD similarity index 73% rename from source/extensions/path/pattern_template_lib/proto/BUILD rename to source/extensions/path/uri_template_lib/proto/BUILD index 15daa9ec85d32..3f32198593f57 100644 --- a/source/extensions/path/pattern_template_lib/proto/BUILD +++ b/source/extensions/path/uri_template_lib/proto/BUILD @@ -11,7 +11,7 @@ licenses(["notice"]) # Apache 2 envoy_extension_package() envoy_proto_library( - name = "pattern_template_rewrite_segements", - srcs = ["pattern_template_rewrite_segments.proto"], + name = "uri_template_rewrite_segements", + srcs = ["uri_template_rewrite_segments.proto"], deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], ) diff --git a/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto b/source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.proto similarity index 68% rename from source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto rename to source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.proto index 14ae540825171..323f5250de855 100644 --- a/source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.proto +++ b/source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.proto @@ -1,17 +1,17 @@ syntax = "proto3"; -package envoy.extensions.pattern_template; +package envoy.extensions.uri_template; import "udpa/annotations/status.proto"; -option java_package = "io.envoyproxy.envoy.extensions.pattern_template"; -option java_outer_classname = "PatternTemplateRewrite"; +option java_package = "io.envoyproxy.envoy.extensions.uri_template"; +option java_outer_classname = "UriTemplateRewrite"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pattern_template/pattern_template"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/url_template/url_template"; option (udpa.annotations.file_status).package_version_status = ACTIVE; -// Holds the segments for rewriting urls base on pattern templates -message PatternTemplateRewriteSegments { +// Holds the segments for rewriting urls base on uri pattern templates +message UriTemplateRewriteSegments { message RewriteSegment { oneof segment_type { // Represents a segment of the rewritten URL, including any path segments, diff --git a/source/extensions/path/pattern_template_lib/pattern_template.cc b/source/extensions/path/uri_template_lib/uri_template.cc similarity index 88% rename from source/extensions/path/pattern_template_lib/pattern_template.cc rename to source/extensions/path/uri_template_lib/uri_template.cc index 1c23170324181..b30182226300c 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.cc +++ b/source/extensions/path/uri_template_lib/uri_template.cc @@ -1,4 +1,4 @@ -#include "source/extensions/path/pattern_template_lib/pattern_template.h" +#include "source/extensions/path/uri_template_lib/uri_template.h" #include #include @@ -6,8 +6,8 @@ #include #include "source/common/http/path_utility.h" -#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" -#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" +#include "source/extensions/path/uri_template_lib/uri_template_internal.h" +#include "source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.pb.h" #include "absl/status/statusor.h" #include "absl/strings/str_split.h" @@ -16,7 +16,7 @@ namespace Envoy { namespace Extensions { -namespace PatternTemplate { +namespace UriTemplate { using Internal::ParsedUrlPattern; @@ -77,9 +77,9 @@ absl::StatusOr> parseRewritePattern(absl::str return result; } -absl::StatusOr +absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { - envoy::extensions::pattern_template::PatternTemplateRewriteSegments parsed_pattern; + envoy::extensions::uri_template::UriTemplateRewriteSegments parsed_pattern; RE2 regex = RE2(Internal::toStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); @@ -130,7 +130,7 @@ absl::Status isValidSharedVariableSet(absl::string_view path_template_rewrite, absl::StatusOr rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern) { + const envoy::extensions::uri_template::UriTemplateRewriteSegments& rewrite_pattern) { RE2 regex = RE2(Internal::toStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); @@ -145,7 +145,7 @@ absl::StatusOr rewriteURLTemplatePattern( } std::string rewritten_url; - for (const envoy::extensions::pattern_template::PatternTemplateRewriteSegments::RewriteSegment& + for (const envoy::extensions::uri_template::UriTemplateRewriteSegments::RewriteSegment& segment : rewrite_pattern.segments()) { if (segment.has_literal()) { absl::StrAppend(&rewritten_url, segment.literal()); @@ -160,6 +160,6 @@ absl::StatusOr rewriteURLTemplatePattern( return rewritten_url; } -} // namespace PatternTemplate +} // namespace UriTemplate } // namespace Extensions } // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template.h b/source/extensions/path/uri_template_lib/uri_template.h similarity index 80% rename from source/extensions/path/pattern_template_lib/pattern_template.h rename to source/extensions/path/uri_template_lib/uri_template.h index 019a731ff0414..7dc5fb43d8532 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -2,15 +2,15 @@ #include -#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" -#include "source/extensions/path/pattern_template_lib/proto/pattern_template_rewrite_segments.pb.h" +#include "source/extensions/path/uri_template_lib/uri_template_internal.h" +#include "source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.pb.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" namespace Envoy { namespace Extensions { -namespace PatternTemplate { +namespace UriTemplate { enum class RewriteStringKind { Variable, Literal }; @@ -33,7 +33,7 @@ absl::StatusOr> parseRewritePattern(absl::str // Returns the parsed Url rewrite pattern and processes variables to be used by // RewriteURLTemplatePattern() and in rewrite. // |capture_regex| should be the regex generated by ConvertURLPatternSyntaxToRegex(). -absl::StatusOr +absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); // Returns if provided rewrite pattern is valid @@ -52,10 +52,15 @@ absl::Status isValidSharedVariableSet(absl::string_view path_template_rewrite, absl::Status isValidMatchPattern(absl::string_view match_pattern); // Concatenates literals and extracts variable values to form the final rewritten url. +// For example: +// rewrite_pattern: [var_index=2, literal="cat"] +// url: "/bar/var" +// capture_regex: "(1)/(2)" +// Rewrite would result in rewrite of "/var/cat". absl::StatusOr rewriteURLTemplatePattern( absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::pattern_template::PatternTemplateRewriteSegments& rewrite_pattern); + const envoy::extensions::uri_template::UriTemplateRewriteSegments& rewrite_pattern); -} // namespace PatternTemplate +} // namespace UriTemplate } // namespace Extensions } // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc b/source/extensions/path/uri_template_lib/uri_template_internal.cc similarity index 98% rename from source/extensions/path/pattern_template_lib/pattern_template_internal.cc rename to source/extensions/path/uri_template_lib/uri_template_internal.cc index 28f26108bcd0c..8109e602d6873 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.cc +++ b/source/extensions/path/uri_template_lib/uri_template_internal.cc @@ -1,4 +1,4 @@ -#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" +#include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include #include @@ -22,7 +22,7 @@ namespace Envoy { namespace Extensions { -namespace PatternTemplate { +namespace UriTemplate { namespace Internal { @@ -381,6 +381,6 @@ std::string toRegexPattern(const struct ParsedUrlPattern& pattern) { } } // namespace Internal -} // namespace PatternTemplate +} // namespace UriTemplate } // namespace Extensions } // namespace Envoy diff --git a/source/extensions/path/pattern_template_lib/pattern_template_internal.h b/source/extensions/path/uri_template_lib/uri_template_internal.h similarity index 98% rename from source/extensions/path/pattern_template_lib/pattern_template_internal.h rename to source/extensions/path/uri_template_lib/uri_template_internal.h index c3024f7c1e1ef..f8984d7a3457c 100644 --- a/source/extensions/path/pattern_template_lib/pattern_template_internal.h +++ b/source/extensions/path/uri_template_lib/uri_template_internal.h @@ -15,7 +15,7 @@ namespace Envoy { namespace Extensions { -namespace PatternTemplate { +namespace UriTemplate { namespace Internal { @@ -130,6 +130,6 @@ std::string toRegexPattern(const struct ParsedUrlPattern& pattern); inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } } // namespace Internal -} // namespace PatternTemplate +} // namespace UriTemplate } // namespace Extensions } // namespace Envoy diff --git a/test/extensions/path/pattern_template_lib/BUILD b/test/extensions/path/uri_template_lib/BUILD similarity index 52% rename from test/extensions/path/pattern_template_lib/BUILD rename to test/extensions/path/uri_template_lib/BUILD index dcb9aa1326df5..4c175eaa40333 100644 --- a/test/extensions/path/pattern_template_lib/BUILD +++ b/test/extensions/path/uri_template_lib/BUILD @@ -8,14 +8,14 @@ licenses(["notice"]) # Apache 2 envoy_package() -# Wildcard & Pattern Matching +# Wildcard & Uri Pattern Matching envoy_cc_test( - name = "pattern_template_test", - srcs = ["pattern_template_test.cc"], + name = "uri_template_test", + srcs = ["uri_template_test.cc"], deps = [ - "//source/extensions/path/pattern_template_lib", - "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "//source/extensions/path/uri_template_lib", + "//source/extensions/path/uri_template_lib/proto:uri_template_rewrite_segements_cc_proto", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", @@ -24,11 +24,11 @@ envoy_cc_test( ) envoy_cc_test( - name = "pattern_template_internal_test", - srcs = ["pattern_template_internal_test.cc"], + name = "uri_template_internal_test", + srcs = ["uri_template_internal_test.cc"], deps = [ - "//source/extensions/path/pattern_template_lib:pattern_template_internal_cc", - "//source/extensions/path/pattern_template_lib/proto:pattern_template_rewrite_segements_cc_proto", + "//source/extensions/path/uri_template_lib:uri_template_internal_cc", + "//source/extensions/path/uri_template_lib/proto:uri_template_rewrite_segements_cc_proto", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", diff --git a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc similarity index 99% rename from test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc rename to test/extensions/path/uri_template_lib/uri_template_internal_test.cc index c77d4d3d632d5..ad86a55b7b730 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_internal_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc @@ -7,7 +7,7 @@ #include "source/common/common/assert.h" #include "source/common/protobuf/protobuf.h" -#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" +#include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "test/test_common/logging.h" #include "test/test_common/status_utility.h" @@ -20,7 +20,7 @@ namespace Envoy { namespace Extensions { -namespace PatternTemplate { +namespace UriTemplate { namespace Internal { @@ -470,6 +470,6 @@ TEST_P(GenPatternRegexWithoutMatch, WithCapture) { } // namespace } // namespace Internal -} // namespace PatternTemplate +} // namespace UriTemplate } // namespace Extensions } // namespace Envoy diff --git a/test/extensions/path/pattern_template_lib/pattern_template_test.cc b/test/extensions/path/uri_template_lib/uri_template_test.cc similarity index 93% rename from test/extensions/path/pattern_template_lib/pattern_template_test.cc rename to test/extensions/path/uri_template_lib/uri_template_test.cc index 317b8b1e5209b..d08840531cccc 100644 --- a/test/extensions/path/pattern_template_lib/pattern_template_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_test.cc @@ -4,8 +4,8 @@ #include "source/common/common/assert.h" #include "source/common/protobuf/protobuf.h" -#include "source/extensions/path/pattern_template_lib/pattern_template.h" -#include "source/extensions/path/pattern_template_lib/pattern_template_internal.h" +#include "source/extensions/path/uri_template_lib/uri_template.h" +#include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "test/test_common/logging.h" #include "test/test_common/status_utility.h" @@ -15,7 +15,7 @@ namespace Envoy { namespace Extensions { -namespace PatternTemplate { +namespace UriTemplate { namespace { @@ -87,8 +87,8 @@ TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { class ParseRewriteSuccess : public testing::TestWithParam> { protected: const std::string& rewritePattern() const { return std::get<0>(GetParam()); } - envoy::extensions::pattern_template::PatternTemplateRewriteSegments expectedProto() const { - envoy::extensions::pattern_template::PatternTemplateRewriteSegments expected_proto; + envoy::extensions::uri_template::UriTemplateRewriteSegments expectedProto() const { + envoy::extensions::uri_template::UriTemplateRewriteSegments expected_proto; Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); return expected_proto; } @@ -148,7 +148,7 @@ INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, }))); TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { - absl::StatusOr rewrite = + absl::StatusOr rewrite = parseRewritePattern(rewritePattern(), kCaptureRegex); ASSERT_OK(rewrite); // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); @@ -171,8 +171,8 @@ TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { class RewriteUrlTemplateSuccess : public testing::TestWithParam> { protected: - envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewriteProto() const { - envoy::extensions::pattern_template::PatternTemplateRewriteSegments proto; + envoy::extensions::uri_template::UriTemplateRewriteSegments rewriteProto() const { + envoy::extensions::uri_template::UriTemplateRewriteSegments proto; Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); return proto; } @@ -236,7 +236,7 @@ TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { } TEST(RewriteUrlTemplateFailure, BadRegex) { - envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; + envoy::extensions::uri_template::UriTemplateRewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -251,7 +251,7 @@ TEST(RewriteUrlTemplateFailure, BadRegex) { } TEST(RewriteUrlTemplateFailure, RegexNoMatch) { - envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; + envoy::extensions::uri_template::UriTemplateRewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -266,7 +266,7 @@ TEST(RewriteUrlTemplateFailure, RegexNoMatch) { } TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { - envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; + envoy::extensions::uri_template::UriTemplateRewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -280,7 +280,7 @@ TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { } TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { - envoy::extensions::pattern_template::PatternTemplateRewriteSegments rewrite_proto; + envoy::extensions::uri_template::UriTemplateRewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -321,7 +321,7 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { absl::StatusOr regex = convertURLPatternSyntaxToRegex(urlPattern()); ASSERT_OK(regex); - absl::StatusOr + absl::StatusOr rewrite_proto = parseRewritePattern(rewritePattern(), regex.value()); ASSERT_OK(rewrite_proto); @@ -364,6 +364,6 @@ TEST_P(URLPatternMatchAndRewrite, IsValidSharedVariableSet) { } // namespace -} // namespace PatternTemplate +} // namespace UriTemplate } // namespace Extensions } // namespace Envoy diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index 172100ea3e4ff..101f6746c1da2 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -295,4 +295,4 @@ visibility_excludes: - source/extensions/transport_sockets/common/BUILD - source/extensions/udp_packet_writer/default/BUILD - source/extensions/udp_packet_writer/gso/BUILD -- source/extensions/path/pattern_template_lib/BUILD +- source/extensions/path/uri_template_lib/BUILD From 4e9dea087578493d6ba164496567d02bcfae49bc Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 17 Aug 2022 19:29:44 +0000 Subject: [PATCH 173/238] Renamed Signed-off-by: silverstar195 --- source/extensions/path/uri_template_lib/BUILD | 2 +- source/extensions/path/uri_template_lib/proto/BUILD | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/extensions/path/uri_template_lib/BUILD b/source/extensions/path/uri_template_lib/BUILD index 2a3035758b86d..715e0d911005c 100644 --- a/source/extensions/path/uri_template_lib/BUILD +++ b/source/extensions/path/uri_template_lib/BUILD @@ -6,7 +6,7 @@ load( licenses(["notice"]) # Apache 2 -# Wildcard & Pattern Matching +# Wildcard & Uri Pattern Matching envoy_extension_package() diff --git a/source/extensions/path/uri_template_lib/proto/BUILD b/source/extensions/path/uri_template_lib/proto/BUILD index 3f32198593f57..70c2114ccc1a5 100644 --- a/source/extensions/path/uri_template_lib/proto/BUILD +++ b/source/extensions/path/uri_template_lib/proto/BUILD @@ -6,7 +6,7 @@ load( licenses(["notice"]) # Apache 2 -# Wildcard & Pattern Matching Proto +# Wildcard & Uri Pattern Matching Proto envoy_extension_package() From 33552d1ebe67c9258cf71bf9a12f957ba6a69d8f Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 17 Aug 2022 19:36:31 +0000 Subject: [PATCH 174/238] Formating Signed-off-by: silverstar195 --- source/extensions/path/uri_template_lib/uri_template.cc | 6 +++--- source/extensions/path/uri_template_lib/uri_template.h | 2 +- test/extensions/path/uri_template_lib/uri_template_test.cc | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/source/extensions/path/uri_template_lib/uri_template.cc b/source/extensions/path/uri_template_lib/uri_template.cc index b30182226300c..38fec75f5b2a7 100644 --- a/source/extensions/path/uri_template_lib/uri_template.cc +++ b/source/extensions/path/uri_template_lib/uri_template.cc @@ -6,8 +6,8 @@ #include #include "source/common/http/path_utility.h" -#include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.pb.h" +#include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "absl/status/statusor.h" #include "absl/strings/str_split.h" @@ -145,8 +145,8 @@ absl::StatusOr rewriteURLTemplatePattern( } std::string rewritten_url; - for (const envoy::extensions::uri_template::UriTemplateRewriteSegments::RewriteSegment& - segment : rewrite_pattern.segments()) { + for (const envoy::extensions::uri_template::UriTemplateRewriteSegments::RewriteSegment& segment : + rewrite_pattern.segments()) { if (segment.has_literal()) { absl::StrAppend(&rewritten_url, segment.literal()); } else if (segment.has_var_index()) { diff --git a/source/extensions/path/uri_template_lib/uri_template.h b/source/extensions/path/uri_template_lib/uri_template.h index 7dc5fb43d8532..74e58f41974fd 100644 --- a/source/extensions/path/uri_template_lib/uri_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -2,8 +2,8 @@ #include -#include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.pb.h" +#include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" diff --git a/test/extensions/path/uri_template_lib/uri_template_test.cc b/test/extensions/path/uri_template_lib/uri_template_test.cc index d08840531cccc..1911c01d5d7da 100644 --- a/test/extensions/path/uri_template_lib/uri_template_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_test.cc @@ -321,8 +321,8 @@ TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { absl::StatusOr regex = convertURLPatternSyntaxToRegex(urlPattern()); ASSERT_OK(regex); - absl::StatusOr - rewrite_proto = parseRewritePattern(rewritePattern(), regex.value()); + absl::StatusOr rewrite_proto = + parseRewritePattern(rewritePattern(), regex.value()); ASSERT_OK(rewrite_proto); absl::StatusOr rewritten_url = From 5186ad69fd4df6d0b7d5ee118dc4eca8f378f243 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 18 Aug 2022 13:35:54 +0000 Subject: [PATCH 175/238] Updates from comments Signed-off-by: silverstar195 --- CODEOWNERS | 4 +- .../uri_template_lib/uri_template_internal.cc | 18 ++++----- .../uri_template_lib/uri_template_internal.h | 9 +++-- .../uri_template_internal_test.cc | 38 +++++++++---------- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index 86dc82b8859f5..0edafdef95fe3 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -272,8 +272,8 @@ extensions/filters/http/oauth2 @derekargueta @snowp /*/extensions/filters/network/common @UNOWNED @UNOWNED # URL Pattern Match and Rewrite Library -/*/extensions/path/pattern_template_lib @alyssawilk @yanjunxiang-google -/*/extensions/path/pattern_template_lib/proto @alyssawilk @yanjunxiang-google +/*/extensions/path/uri_template_lib @alyssawilk @yanjunxiang-google +/*/extensions/path/uri_template_lib/proto @alyssawilk @yanjunxiang-google # Contrib /contrib/exe/ @mattklein123 @lizan diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.cc b/source/extensions/path/uri_template_lib/uri_template_internal.cc index 8109e602d6873..e6b133eff870f 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.cc +++ b/source/extensions/path/uri_template_lib/uri_template_internal.cc @@ -44,7 +44,7 @@ constexpr absl::string_view kLiteral = "a-zA-Z0-9-._~" // Unreserved ":@"; // Default operator used for the variable when none specified. -constexpr Operator kDefaultVariableOperator = Operator::KPathGlob; +constexpr Operator kDefaultVariableOperator = Operator::PathGlob; // Visitor for displaying debug info of a ParsedSegment/Variable.var_match. struct ToStringVisitor { @@ -74,9 +74,9 @@ std::string toString(const Literal val) { return std::string(val); } std::string toString(const Operator val) { switch (val) { - case Operator::KPathGlob: + case Operator::PathGlob: return "*"; - case Operator::KTextGlob: + case Operator::TextGlob: return "**"; } return ""; @@ -150,10 +150,10 @@ absl::StatusOr> consumeLiteral(absl::string_view pattern) absl::StatusOr> consumeOperator(absl::string_view pattern) { if (absl::StartsWith(pattern, "**")) { - return ParsedResult(Operator::KTextGlob, pattern.substr(2)); + return ParsedResult(Operator::TextGlob, pattern.substr(2)); } if (absl::StartsWith(pattern, "*")) { - return ParsedResult(Operator::KPathGlob, pattern.substr(1)); + return ParsedResult(Operator::PathGlob, pattern.substr(1)); } return absl::InvalidArgumentError("Invalid Operator"); } @@ -247,7 +247,7 @@ absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { if (seen_text_glob) { return absl::InvalidArgumentError("Glob after text glob."); } - seen_text_glob = (absl::get(segment) == Operator::KTextGlob); + seen_text_glob = (absl::get(segment) == Operator::TextGlob); } else if (absl::holds_alternative(segment)) { const Variable& var = absl::get(segment); if (var.var_match_.empty()) { @@ -263,7 +263,7 @@ absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { if (seen_text_glob) { return absl::InvalidArgumentError("Glob after text glob."); } - seen_text_glob = (absl::get(var_seg) == Operator::KTextGlob); + seen_text_glob = (absl::get(var_seg) == Operator::TextGlob); } } } @@ -359,9 +359,9 @@ std::string toRegexPattern(Operator pattern) { static const std::string* kPathGlobRegex = new std::string(absl::StrCat("[", kLiteral, "]+")); static const std::string* kTextGlobRegex = new std::string(absl::StrCat("[", kLiteral, "/]*")); switch (pattern) { - case Operator::KPathGlob: // "*" + case Operator::PathGlob: // "*" return *kPathGlobRegex; - case Operator::KTextGlob: // "**" + case Operator::TextGlob: // "**" return *kTextGlobRegex; } return ""; diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.h b/source/extensions/path/uri_template_lib/uri_template_internal.h index f8984d7a3457c..48155db8dad1a 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.h +++ b/source/extensions/path/uri_template_lib/uri_template_internal.h @@ -27,7 +27,7 @@ using Literal = absl::string_view; /** * Determines what operations to use on the input pattern segment */ -enum class Operator { KPathGlob, KTextGlob }; +enum class Operator { PathGlob, TextGlob }; /** * Represents a pattern variable. Variables are included in both path match and rewrite paths. @@ -57,16 +57,18 @@ struct ParsedUrlPattern { /** * Check if literal is valid + * Literals cannot hold wildcards or curl brackets. */ bool isValidLiteral(absl::string_view literal); /** * Check if rewrite literal is valid + * Literals cannot hold wildcards or curl brackets. */ bool isValidRewriteLiteral(absl::string_view literal); /** - * Check if indent is valid + * Check if variable name is valid */ bool isValidVariableName(absl::string_view indent); @@ -100,7 +102,8 @@ absl::StatusOr> consumeOperator(absl::string_view pattern absl::StatusOr> consumeVariable(absl::string_view pattern); /** - * Converts input pattern to ParsedUrlPattern + * Converts input pattern to ParsedUrlPattern. + * ParsedUrlPattern hold prefix, string literals, and variables for rewrite. */ absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern); diff --git a/test/extensions/path/uri_template_lib/uri_template_internal_test.cc b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc index ad86a55b7b730..ea73d573e98b0 100644 --- a/test/extensions/path/uri_template_lib/uri_template_internal_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc @@ -33,8 +33,8 @@ TEST(InternalParsing, ParsedUrlDebugString) { { "abc", "def", - Operator::KPathGlob, - Variable("var", {Operator::KPathGlob, "ghi", Operator::KTextGlob}), + Operator::PathGlob, + Variable("var", {Operator::PathGlob, "ghi", Operator::TextGlob}), }, ".test", {}, @@ -108,7 +108,7 @@ TEST(InternalParsing, ConsumeTextGlob) { absl::StatusOr> result = consumeOperator(pattern); ASSERT_OK(result); - EXPECT_EQ(result->parsed_value_, Operator::KTextGlob); + EXPECT_EQ(result->parsed_value_, Operator::TextGlob); EXPECT_EQ(result->unconsumed_pattern_, "*abc/123"); } @@ -125,7 +125,7 @@ TEST(InternalParsing, ConsumePathGlob) { absl::StatusOr> result = consumeOperator(pattern); ASSERT_OK(result); - EXPECT_EQ(result->parsed_value_, Operator::KPathGlob); + EXPECT_EQ(result->parsed_value_, Operator::PathGlob); EXPECT_EQ(result->unconsumed_pattern_, "/123"); } @@ -268,31 +268,31 @@ TEST(InternalRegexGen, DollarSignMatchesIfself) { } TEST(InternalRegexGen, OperatorRegexPattern) { - EXPECT_EQ(toRegexPattern(Operator::KPathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); - EXPECT_EQ(toRegexPattern(Operator::KTextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); + EXPECT_EQ(toRegexPattern(Operator::PathGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@]+"); + EXPECT_EQ(toRegexPattern(Operator::TextGlob), "[a-zA-Z0-9-._~%!$&'()+,;:@/]*"); } TEST(InternalRegexGen, PathGlobRegex) { - EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::KPathGlob))); - EXPECT_FALSE(RE2::FullMatch("", toRegexPattern(Operator::KPathGlob))); - EXPECT_FALSE(RE2::FullMatch("abc/123", toRegexPattern(Operator::KPathGlob))); - EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::KPathGlob))); - EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::KPathGlob))); - EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::KPathGlob))); + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::PathGlob))); + EXPECT_FALSE(RE2::FullMatch("", toRegexPattern(Operator::PathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc/123", toRegexPattern(Operator::PathGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::PathGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::PathGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::PathGlob))); } TEST(InternalRegexGen, TextGlobRegex) { - EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::KTextGlob))); - EXPECT_TRUE(RE2::FullMatch("", toRegexPattern(Operator::KTextGlob))); - EXPECT_TRUE(RE2::FullMatch("abc/123", toRegexPattern(Operator::KTextGlob))); - EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::KTextGlob))); - EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::KTextGlob))); - EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::KTextGlob))); + EXPECT_TRUE(RE2::FullMatch("abc.123", toRegexPattern(Operator::TextGlob))); + EXPECT_TRUE(RE2::FullMatch("", toRegexPattern(Operator::TextGlob))); + EXPECT_TRUE(RE2::FullMatch("abc/123", toRegexPattern(Operator::TextGlob))); + EXPECT_FALSE(RE2::FullMatch("*", toRegexPattern(Operator::TextGlob))); + EXPECT_FALSE(RE2::FullMatch("**", toRegexPattern(Operator::TextGlob))); + EXPECT_FALSE(RE2::FullMatch("abc*123", toRegexPattern(Operator::TextGlob))); } TEST(InternalRegexGen, VariableRegexPattern) { EXPECT_EQ(toRegexPattern(Variable("var1", {})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"); - EXPECT_EQ(toRegexPattern(Variable("var2", {Operator::KPathGlob, "abc", Operator::KTextGlob})), + EXPECT_EQ(toRegexPattern(Variable("var2", {Operator::PathGlob, "abc", Operator::TextGlob})), "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+/abc/" "[a-zA-Z0-9-._~%!$&'()+,;:@/]*)"); } From b6ecdca507620f22158fe90c42b70c4f5977894e Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 18 Aug 2022 18:37:41 +0000 Subject: [PATCH 176/238] Rename extensions Signed-off-by: silverstar195 --- .../v3/pattern_template_match.proto | 2 +- .../v3/pattern_template_rewrite.proto | 2 +- source/extensions/extensions_build_config.bzl | 4 +-- source/extensions/extensions_metadata.yaml | 4 +-- .../path/match/pattern_template/config.h | 2 +- .../pattern_template/pattern_template_match.h | 2 +- .../path/rewrite/pattern_template/config.h | 2 +- .../pattern_template_rewrite.h | 2 +- .../path/match/pattern_template/BUILD | 4 +-- .../match/pattern_template/config_test.cc | 12 ++++----- .../match/pattern_template/library_test.cc | 4 +-- .../path/rewrite/pattern_template/BUILD | 4 +-- .../rewrite/pattern_template/config_test.cc | 16 ++++++------ .../rewrite/pattern_template/library_test.cc | 26 +++++++++---------- 14 files changed, 43 insertions(+), 43 deletions(-) diff --git a/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto index 364e8ea64c1be..d5bff9eb62ab6 100644 --- a/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto @@ -38,7 +38,7 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // * ``/videos/{file}`` would match ``/videos/1080p5000_00001.m4s`` // // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` -// [#extension: envoy.path.match.pattern_template.pattern_template_match_predicate] +// [#extension: envoy.path.match.pattern_template.pattern_template_matcher] // [#not-implemented-hide:] message PatternTemplateMatchConfig { string path_template = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; diff --git a/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto index 21a305541be2a..0019c4546092e 100644 --- a/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto @@ -53,7 +53,7 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` // into ``/en-us/hls/en_193913.vtt``. // [#not-implemented-hide:] -// [#extension: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate] +// [#extension: envoy.path.rewrite.pattern_template.pattern_template_rewriter] message PatternTemplateRewriteConfig { string path_template_rewrite = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; } diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 098b9948f8e82..47353b1744e16 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -242,8 +242,8 @@ EXTENSIONS = { # # Path Pattern Match and Path Pattern Rewrite # - "envoy.path.match.pattern_template.pattern_template_match_predicate": "//source/extensions/path/match/pattern_template:config", - "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate": "//source/extensions/path/rewrite/pattern_template:config", + "envoy.path.match.pattern_template.pattern_template_matcher": "//source/extensions/path/match/pattern_template:config", + "envoy.path.rewrite.pattern_template.pattern_template_rewriter": "//source/extensions/path/rewrite/pattern_template:config", # # Http Upstreams (excepting envoy.upstreams.http.generic which is hard-coded into the build so not registered here) diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index 20a23a76c4156..7b67f60f1ed78 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -746,7 +746,7 @@ envoy.matching.matchers.ip: status: stable type_urls: - envoy.extensions.matching.input_matchers.ip.v3.Ip -envoy.path.match.pattern_template.pattern_template_match_predicate: +envoy.path.match.pattern_template.pattern_template_matcher: categories: - envoy.path.match security_posture: unknown @@ -754,7 +754,7 @@ envoy.path.match.pattern_template.pattern_template_match_predicate: undocumented: true type_urls: - envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig -envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate: +envoy.path.rewrite.pattern_template.pattern_template_rewriter: categories: - envoy.path.rewrite security_posture: unknown diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index 3cd7c4d061d61..64f5d811e9cf2 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -36,7 +36,7 @@ class PatternTemplateMatcherFactory : public Router::PathMatcherFactory { } std::string name() const override { - return "envoy.path.match.pattern_template.pattern_template_match_predicate"; + return "envoy.path.match.pattern_template.pattern_template_matcher; } }; diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index 44bbd10b34434..cb916a9a7b2bb 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -15,7 +15,7 @@ namespace Extensions { namespace PatternTemplate { namespace Match { -const absl::string_view NAME = "envoy.path.match.pattern_template.pattern_template_match_predicate"; +const absl::string_view NAME = "envoy.path.match.pattern_template.pattern_template_matcher"; /** * PatternTemplateMatcher allows matching based on pattern templates. diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 612def14418f1..5ca693d61aae6 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -37,7 +37,7 @@ class PatternTemplateRewriterFactory : public Router::PathRewriterFactory { } std::string name() const override { - return "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"; + return "envoy.path.rewrite.pattern_template.pattern_template_rewriter"; } }; diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index f36a681853bba..305cb5d0ba770 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -22,7 +22,7 @@ namespace PatternTemplate { namespace Rewrite { const absl::string_view NAME = - "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"; + "envoy.path.rewrite.pattern_template.pattern_template_rewriter"; /** * PatternTemplateRewriter allows rewriting paths based on match pattern variables provided diff --git a/test/extensions/path/match/pattern_template/BUILD b/test/extensions/path/match/pattern_template/BUILD index 927bfb4739670..e1b121448a626 100644 --- a/test/extensions/path/match/pattern_template/BUILD +++ b/test/extensions/path/match/pattern_template/BUILD @@ -14,7 +14,7 @@ envoy_package() envoy_extension_cc_test( name = "config_test", srcs = ["config_test.cc"], - extension_names = ["envoy.path.match.pattern_template.pattern_template_match_predicate"], + extension_names = ["envoy.path.match.pattern_template.pattern_template_matcher"], deps = [ "//source/extensions/path/match/pattern_template:config", "//test/mocks/server:factory_context_mocks", @@ -24,7 +24,7 @@ envoy_extension_cc_test( envoy_extension_cc_test( name = "library_test", srcs = ["library_test.cc"], - extension_names = ["envoy.path.match.pattern_template.pattern_template_match_predicate"], + extension_names = ["envoy.path.match.pattern_template.pattern_template_matcher"], deps = [ "//source/extensions/path/match/pattern_template:config", "//source/extensions/path/match/pattern_template:pattern_template_match_lib", diff --git a/test/extensions/path/match/pattern_template/config_test.cc b/test/extensions/path/match/pattern_template/config_test.cc index b94198ad7b2a5..7e058e10a3c0f 100644 --- a/test/extensions/path/match/pattern_template/config_test.cc +++ b/test/extensions/path/match/pattern_template/config_test.cc @@ -13,7 +13,7 @@ namespace Match { TEST(ConfigTest, TestEmptyConfig) { const std::string yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_matcher typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" @@ -26,7 +26,7 @@ TEST(ConfigTest, TestEmptyConfig) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); + EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_matcher"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -40,7 +40,7 @@ TEST(ConfigTest, TestEmptyConfig) { TEST(ConfigTest, InvalidConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_matcher typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country" @@ -53,7 +53,7 @@ TEST(ConfigTest, InvalidConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); + EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_matcher"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -70,7 +70,7 @@ TEST(ConfigTest, InvalidConfigSetup) { TEST(ConfigTest, TestConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_matcher typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" @@ -83,7 +83,7 @@ TEST(ConfigTest, TestConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_match_predicate"); + EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_matcher"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); diff --git a/test/extensions/path/match/pattern_template/library_test.cc b/test/extensions/path/match/pattern_template/library_test.cc index eb5b41bbf18a1..17a84b71e7460 100644 --- a/test/extensions/path/match/pattern_template/library_test.cc +++ b/test/extensions/path/match/pattern_template/library_test.cc @@ -30,7 +30,7 @@ Router::PathMatcherSharedPtr createMatchPredicateFromYaml(std::string yaml_strin TEST(MatchTest, BasicUsage) { const std::string yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_matcher typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" @@ -39,7 +39,7 @@ TEST(MatchTest, BasicUsage) { Router::PathMatcherSharedPtr predicate = createMatcherFromYaml(yaml_string); EXPECT_EQ(predicate->pattern(), "/bar/{lang}/{country}"); EXPECT_EQ(predicate->name(), - "envoy.path.match.pattern_template.pattern_template_match_predicate"); + "envoy.path.match.pattern_template.pattern_template_matcher"); EXPECT_TRUE(predicate->match("/bar/en/us")); } diff --git a/test/extensions/path/rewrite/pattern_template/BUILD b/test/extensions/path/rewrite/pattern_template/BUILD index 1defff4c52998..c9af17dcc1fdd 100644 --- a/test/extensions/path/rewrite/pattern_template/BUILD +++ b/test/extensions/path/rewrite/pattern_template/BUILD @@ -14,7 +14,7 @@ envoy_package() envoy_extension_cc_test( name = "config_test", srcs = ["config_test.cc"], - extension_names = ["envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"], + extension_names = ["envoy.path.rewrite.pattern_template.pattern_template_rewriter"], deps = [ "//source/extensions/path/rewrite/pattern_template:config", "//test/mocks/server:factory_context_mocks", @@ -24,7 +24,7 @@ envoy_extension_cc_test( envoy_extension_cc_test( name = "library_test", srcs = ["library_test.cc"], - extension_names = ["envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"], + extension_names = ["envoy.path.rewrite.pattern_template.pattern_template_rewriter"], deps = [ "//source/extensions/path/match/pattern_template:config", "//source/extensions/path/match/pattern_template:pattern_template_match_lib", diff --git a/test/extensions/path/rewrite/pattern_template/config_test.cc b/test/extensions/path/rewrite/pattern_template/config_test.cc index 5e5bb6ba7786a..05c72f67bc2e3 100644 --- a/test/extensions/path/rewrite/pattern_template/config_test.cc +++ b/test/extensions/path/rewrite/pattern_template/config_test.cc @@ -13,7 +13,7 @@ namespace Rewrite { TEST(ConfigTest, TestEmptyConfig) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewriter typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" @@ -27,7 +27,7 @@ TEST(ConfigTest, TestEmptyConfig) { EXPECT_NE(nullptr, factory); EXPECT_EQ(factory->name(), - "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -41,7 +41,7 @@ TEST(ConfigTest, TestEmptyConfig) { TEST(ConfigTest, InvalidConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewritere typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country" @@ -55,7 +55,7 @@ TEST(ConfigTest, InvalidConfigSetup) { EXPECT_NE(nullptr, factory); EXPECT_EQ(factory->name(), - "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -72,7 +72,7 @@ TEST(ConfigTest, InvalidConfigSetup) { TEST(ConfigTest, TestConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewriter typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" @@ -86,7 +86,7 @@ TEST(ConfigTest, TestConfigSetup) { EXPECT_NE(nullptr, factory); EXPECT_EQ(factory->name(), - "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -101,7 +101,7 @@ TEST(ConfigTest, TestConfigSetup) { TEST(ConfigTest, TestInvalidConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewriter typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/country}" @@ -115,7 +115,7 @@ TEST(ConfigTest, TestInvalidConfigSetup) { EXPECT_NE(nullptr, factory); EXPECT_EQ(factory->name(), - "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc index c71b74c80f904..6d4abec27fa2d 100644 --- a/test/extensions/path/rewrite/pattern_template/library_test.cc +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -47,7 +47,7 @@ Router::PathRewriterSharedPtr createRewritePredicateFromYaml(std::string yaml_st TEST(RewriteTest, BasicSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewriter typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" @@ -56,12 +56,12 @@ TEST(RewriteTest, BasicSetup) { Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); EXPECT_EQ(predicate->pattern(), "/bar/{lang}/{country}"); EXPECT_EQ(predicate->name(), - "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); } TEST(RewriteTest, BasicUsage) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewriter typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" @@ -70,12 +70,12 @@ TEST(RewriteTest, BasicUsage) { Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); EXPECT_EQ(predicate->rewriteUrl("/bar/en/usa", "/bar/{country}/{lang}").value(), "/bar/usa/en"); EXPECT_EQ(predicate->name(), - "envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate"); + "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); } TEST(RewriteTest, RewriteInvalidRegex) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewriter typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" @@ -90,14 +90,14 @@ TEST(RewriteTest, RewriteInvalidRegex) { TEST(RewriteTest, MatchPatternValidation) { const std::string rewrite_yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewriter typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/foo/{lang}/{country}" )EOF"; const std::string match_yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_matcher typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" @@ -113,14 +113,14 @@ TEST(RewriteTest, MatchPatternValidation) { TEST(RewriteTest, MatchPatternInactive) { const std::string rewrite_yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewriter typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/foo/{lang}/{country}" )EOF"; const std::string match_yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_matcher typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" @@ -134,21 +134,21 @@ TEST(RewriteTest, MatchPatternInactive) { absl::Status error = rewrite_predicate->isCompatibleMatchPolicy(match_predicate, false); EXPECT_FALSE(error.ok()); EXPECT_EQ(error.message(), - "unable to use envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate " - "extension without envoy.path.match.pattern_template.pattern_template_match_predicate " + "unable to use envoy.path.rewrite.pattern_template.pattern_template_rewriter " + "extension without envoy.path.match.pattern_template.pattern_template_matcher " "extension"); } TEST(RewriteTest, MatchPatternMismatchedVars) { const std::string rewrite_yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.pattern_template.pattern_template_rewriter typed_config: "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/foo/{lang}/{missing}" )EOF"; const std::string match_yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.pattern_template.pattern_template_matcher typed_config: "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" From 613f15ac9dcf10e5ccad06f772cc1a6cceb2346f Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 18 Aug 2022 20:02:24 +0000 Subject: [PATCH 177/238] from main check_format Signed-off-by: silverstar195 --- tools/code_format/check_format.py | 536 +++++++++++++++++------------- 1 file changed, 304 insertions(+), 232 deletions(-) diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index 1a15eb255b0a9..9163b2d858be3 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -1,10 +1,12 @@ #!/usr/bin/env python3 import argparse +import common import functools import logging import multiprocessing import os +import os.path import pathlib import re import subprocess @@ -12,140 +14,233 @@ import sys import traceback import shutil +import paths from functools import cached_property -from typing import Callable, Dict, List, Pattern, Tuple, Union - -# The way this script is currently used (ie no bazel) it relies on system deps. -# As `pyyaml` is present in `envoy-build-ubuntu` it should be safe to use here. -import yaml -import paths +EXCLUDED_PREFIXES = ( + "./.", "./generated/", "./thirdparty/", "./build", "./bazel-", "./tools/dev/src", + "./source/extensions/extensions_build_config.bzl", "./contrib/contrib_build_config.bzl", + "./bazel/toolchains/configs/", "./tools/testdata/check_format/", "./tools/pyformat/", + "./third_party/", "./test/extensions/filters/http/wasm/test_data", + "./test/extensions/filters/network/wasm/test_data", + "./test/extensions/stats_sinks/wasm/test_data", "./test/extensions/bootstrap/wasm/test_data", + "./test/extensions/common/wasm/test_data", "./test/extensions/access_loggers/wasm/test_data", + "./source/extensions/common/wasm/ext", "./examples/wasm-cc", "./bazel/external/http_parser/") +SUFFIXES = ("BUILD", "WORKSPACE", ".bzl", ".cc", ".h", ".java", ".m", ".mm", ".proto") +PROTO_SUFFIX = (".proto") + +# Files in these paths can make reference to protobuf stuff directly +GOOGLE_PROTOBUF_ALLOWLIST = ( + "ci/prebuilt", "source/common/protobuf", "api/test", "test/extensions/bootstrap/wasm/test_data") +REPOSITORIES_BZL = "bazel/repositories.bzl" + +# Files matching these exact names can reference real-world time. These include the class +# definitions for real-world time, the construction of them in main(), and perf annotation. +# For now it includes the validation server but that really should be injected too. +REAL_TIME_ALLOWLIST = ( + "./source/common/common/utility.h", "./source/extensions/common/aws/utility.cc", + "./source/common/event/real_time_system.cc", "./source/common/event/real_time_system.h", + "./source/exe/main_common.cc", "./source/exe/main_common.h", + "./source/server/config_validation/server.cc", "./source/common/common/perf_annotation.h", + "./test/common/common/log_macros_test.cc", "./test/common/protobuf/utility_test.cc", + "./test/test_common/simulated_time_system.cc", "./test/test_common/simulated_time_system.h", + "./test/test_common/test_time.cc", "./test/test_common/test_time.h", + "./test/test_common/utility.cc", "./test/test_common/utility.h", + "./test/integration/integration.h", "./test/tools/wee8_compile/wee8_compile.cc") + +# Tests in these paths may make use of the Registry::RegisterFactory constructor or the +# REGISTER_FACTORY macro. Other locations should use the InjectFactory helper class to +# perform temporary registrations. +REGISTER_FACTORY_TEST_ALLOWLIST = ( + "./test/common/config/registry_test.cc", "./test/integration/clusters/", + "./test/integration/filters/", "./test/integration/load_balancers/", + "./test/extensions/transport_sockets/tls/integration/") + +# Files in these paths can use MessageLite::SerializeAsString +SERIALIZE_AS_STRING_ALLOWLIST = ( + "./source/common/protobuf/utility.cc", + "./source/extensions/filters/http/grpc_json_transcoder/json_transcoder_filter.cc", + "./test/common/protobuf/utility_test.cc", + "./test/common/grpc/codec_test.cc", + "./test/common/grpc/codec_fuzz_test.cc", + "./test/extensions/filters/common/expr/context_test.cc", + "./test/extensions/filters/http/common/fuzz/uber_filter.h", + "./test/extensions/bootstrap/wasm/test_data/speed_cpp.cc", + "./test/tools/router_check/router_check.cc", +) + +# Files in these paths can use Protobuf::util::JsonStringToMessage +JSON_STRING_TO_MESSAGE_ALLOWLIST = ( + "./source/common/protobuf/utility.cc", + "./test/extensions/bootstrap/wasm/test_data/speed_cpp.cc") + +# Histogram names which are allowed to be suffixed with the unit symbol, all of the pre-existing +# ones were grandfathered as part of PR #8484 for backwards compatibility. +HISTOGRAM_WITH_SI_SUFFIX_ALLOWLIST = ( + "cx_rtt_us", "cx_rtt_variance_us", "downstream_cx_length_ms", "downstream_cx_length_ms", + "initialization_time_ms", "loop_duration_us", "poll_delay_us", "request_time_ms", + "upstream_cx_connect_ms", "upstream_cx_length_ms") + +# Files in these paths can use std::regex +STD_REGEX_ALLOWLIST = ( + "./source/common/common/utility.cc", "./source/common/common/regex.h", + "./source/common/common/regex.cc", "./source/common/stats/tag_extractor_impl.h", + "./source/common/stats/tag_extractor_impl.cc", + "./source/common/formatter/substitution_formatter.cc", + "./contrib/squash/filters/http/source/squash_filter.h", + "./contrib/squash/filters/http/source/squash_filter.cc", "./source/server/admin/utils.h", + "./source/server/admin/utils.cc", "./source/server/admin/stats_params.h", + "./source/server/admin/stats_request.cc", "./source/server/admin/prometheus_stats.h", + "./source/server/admin/prometheus_stats.cc", "./tools/clang_tools/api_booster/main.cc", + "./tools/clang_tools/api_booster/proto_cxx_utils.cc", "./source/common/version/version.cc") + +# Only one C++ file should instantiate grpc_init +GRPC_INIT_ALLOWLIST = ("./source/common/grpc/google_grpc_context.cc") + +# Files that should not raise an error for using memcpy +MEMCPY_WHITELIST = ( + "./source/common/common/mem_block_builder.h", "./source/common/common/safe_memcpy.h") + +# These files should not throw exceptions. Add HTTP/1 when exceptions removed. +EXCEPTION_DENYLIST = ( + "./source/common/http/http2/codec_impl.h", "./source/common/http/http2/codec_impl.cc") + +# Files that are allowed to use try without main thread assertion. +RAW_TRY_ALLOWLIST = ( + "./source/common/common/regex.cc", "./source/common/common/thread.h", + "./source/common/network/utility.cc") + +# These are entire files that are allowed to use std::string_view vs. individual exclusions. Right +# now this is just WASM which makes use of std::string_view heavily so we need to convert to +# absl::string_view internally. Everywhere else should be using absl::string_view for additional +# safety. +STD_STRING_VIEW_ALLOWLIST = ( + "./source/extensions/common/wasm/context.h", + "./source/extensions/common/wasm/context.cc", + "./source/extensions/common/wasm/foreign.cc", + "./source/extensions/common/wasm/wasm.h", + "./source/extensions/common/wasm/wasm.cc", + "./source/extensions/common/wasm/wasm_vm.h", + "./source/extensions/common/wasm/wasm_vm.cc", + "./test/extensions/bootstrap/wasm/wasm_speed_test.cc", + "./test/extensions/bootstrap/wasm/wasm_test.cc", + "./test/extensions/common/wasm/wasm_test.cc", + "./test/extensions/stats_sinks/wasm/wasm_stat_sink_test.cc", + "./test/test_common/wasm_base.h", +) + +# Header files that can throw exceptions. These should be limited; the only +# valid situation identified so far is template functions used for config +# processing. +EXCEPTION_ALLOWLIST = ("./source/common/config/utility.h") + +# We want all URL references to exist in repository_locations.bzl files and have +# metadata that conforms to the schema in ./api/bazel/external_deps.bzl. Below +# we have some exceptions for either infrastructure files or places we fall +# short today (Rust). +# +# Please DO NOT extend this allow list without consulting +# @envoyproxy/dependency-shepherds. +BUILD_URLS_ALLOWLIST = ( + "./bazel/repository_locations.bzl", + "./bazel/external/cargo/crates.bzl", + "./api/bazel/repository_locations.bzl", + "./api/bazel/envoy_http_archive.bzl", +) + +CLANG_FORMAT_PATH = os.getenv("CLANG_FORMAT", "clang-format-14") +BUILDIFIER_PATH = paths.get_buildifier() +BUILDOZER_PATH = paths.get_buildozer() +ENVOY_BUILD_FIXER_PATH = os.path.join( + os.path.dirname(os.path.abspath(sys.argv[0])), "envoy_build_fixer.py") +HEADER_ORDER_PATH = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "header_order.py") +SUBDIR_SET = set(common.include_dir_order()) +INCLUDE_ANGLE = "#include <" +INCLUDE_ANGLE_LEN = len(INCLUDE_ANGLE) +X_ENVOY_USED_DIRECTLY_REGEX = re.compile(r'.*\"x-envoy-.*\".*') +DESIGNATED_INITIALIZER_REGEX = re.compile(r"\{\s*\.\w+\s*\=") +MANGLED_PROTOBUF_NAME_REGEX = re.compile(r"envoy::[a-z0-9_:]+::[A-Z][a-z]\w*_\w*_[A-Z]{2}") +HISTOGRAM_SI_SUFFIX_REGEX = re.compile(r"(?<=HISTOGRAM\()[a-zA-Z0-9_]+_(b|kb|mb|ns|us|ms|s)(?=,)") +TEST_NAME_STARTING_LOWER_CASE_REGEX = re.compile(r"TEST(_.\(.*,\s|\()[a-z].*\)\s\{") +EXTENSIONS_CODEOWNERS_REGEX = re.compile(r'.*(extensions[^@]*\s+)(@.*)') +CONTRIB_CODEOWNERS_REGEX = re.compile(r'(/contrib/[^@]*\s+)(@.*)') +COMMENT_REGEX = re.compile(r"//|\*") +DURATION_VALUE_REGEX = re.compile(r'\b[Dd]uration\(([0-9.]+)') +PROTO_VALIDATION_STRING = re.compile(r'\bmin_bytes\b') +OLD_MOCK_METHOD_REGEX = re.compile("MOCK_METHOD\d") +# C++17 feature, lacks sufficient support across various libraries / compilers. +FOR_EACH_N_REGEX = re.compile("for_each_n\(") +# Check for punctuation in a terminal ref clause, e.g. +# :ref:`panic mode. ` +DOT_MULTI_SPACE_REGEX = re.compile("\\. +") +FLAG_REGEX = re.compile("RUNTIME_GUARD\((.*)\);") + +# yapf: disable +PROTOBUF_TYPE_ERRORS = { + # Well-known types should be referenced from the ProtobufWkt namespace. + "Protobuf::Any": "ProtobufWkt::Any", + "Protobuf::Empty": "ProtobufWkt::Empty", + "Protobuf::ListValue": "ProtobufWkt::ListValue", + "Protobuf::NULL_VALUE": "ProtobufWkt::NULL_VALUE", + "Protobuf::StringValue": "ProtobufWkt::StringValue", + "Protobuf::Struct": "ProtobufWkt::Struct", + "Protobuf::Value": "ProtobufWkt::Value", + + # Other common mis-namespacing of protobuf types. + "ProtobufWkt::Map": "Protobuf::Map", + "ProtobufWkt::MapPair": "Protobuf::MapPair", + "ProtobufUtil::MessageDifferencer": "Protobuf::util::MessageDifferencer" +} +# yapf: enable + +LIBCXX_REPLACEMENTS = { + "absl::make_unique<": "std::make_unique<", +} + +CODE_CONVENTION_REPLACEMENTS = { + # We can't just remove Times(1) everywhere, since .Times(1).WillRepeatedly + # is a legitimate pattern. See + # https://github.com/google/googletest/blob/master/googlemock/docs/for_dummies.md#cardinalities-how-many-times-will-it-be-called + ".Times(1);": ";", + # These may miss some cases, due to line breaks, but should reduce the + # Times(1) noise. + ".Times(1).WillOnce": ".WillOnce", + ".Times(1).WillRepeatedly": ".WillOnce", + "Stats::ScopePtr": "Stats::ScopeSharedPtr", +} + +UNSORTED_FLAGS = { + "envoy.reloadable_features.activate_timers_next_event_loop", + "envoy.reloadable_features.grpc_json_transcoder_adhere_to_buffer_limits", +} logger = logging.getLogger(__name__) - -class FormatConfig: - """Provides a format config object based on parsed YAML config.""" - - def __init__(self, path: str) -> None: - self.path = path - - def __getitem__(self, k): - return self.config.__getitem__(k) - - @cached_property - def buildifier_path(self) -> str: - """Path to the buildifer binary.""" - return paths.get_buildifier() - - @cached_property - def buildozer_path(self) -> str: - """Path to the buildozer binary.""" - return paths.get_buildozer() - - @cached_property - def clang_format_path(self) -> str: - """Path to the clang-format binary.""" - return os.getenv("CLANG_FORMAT", "clang-format-14") - - @cached_property - def config(self) -> Dict: - """Parsed YAML config.""" - # TODO(phlax): Ensure the YAML is valid/well-formed.""" - return yaml.safe_load(pathlib.Path(self.path).read_text()) - - @cached_property - def dir_order(self) -> List[str]: - """Expected order of includes in code.""" - return self["dir_order"] - - @cached_property - def paths(self) -> Dict[str, Union[Tuple[str, ...], Dict[str, Tuple[str, ...]]]]: - """Mapping of named paths.""" - paths = self._normalize("paths", cb=lambda paths: tuple(f"./{p}" for p in paths)) - paths["build_fixer_py"] = self._build_fixer_path - paths["header_order_py"] = self._header_order_path - return paths - - @cached_property - def re(self) -> Dict[str, Pattern[str]]: - """Mapping of named regular expressions.""" - return {k: re.compile(v) for k, v in self["re"].items()} - - @cached_property - def re_multiline(self) -> Dict[str, Pattern[str]]: - """Mapping of named multi-line regular expressions.""" - return {k: re.compile(v, re.MULTILINE) for k, v in self["re_multiline"].items()} - - @cached_property - def replacements(self) -> Dict[str, str]: - """Mapping of subsitutions to be replaced in code.""" - return self["replacements"] - - @cached_property - def suffixes(self) -> Dict[str, Union[Tuple[str, ...], Dict[str, Tuple[str, ...]]]]: - """Mapping of named file suffixes for target files.""" - return self._normalize("suffixes") - - @property - def _build_fixer_path(self) -> str: - return os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "envoy_build_fixer.py") - - @property - def _header_order_path(self) -> str: - return os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "header_order.py") - - def _normalize( - self, - config_type: str, - cb: Callable = tuple) -> Dict[str, Union[Tuple[str, ...], Dict[str, Tuple[str, ...]]]]: - config = {} - for k, v in self[config_type].items(): - if isinstance(v, dict): - config[k] = {} - for key in ("include", "exclude"): - if key in v: - config[k][key] = cb(v[key]) - else: - config[k] = cb(v) - return config +LINE_NUMBER_RE = re.compile(r"^(\d+)[a|c|d]?\d*(?:,\d+[a|c|d]?\d*)?$") +VIRTUAL_INCLUDE_HEADERS_RE = re.compile(r"#include.*/_virtual_includes/") +OWNER_RE = re.compile('@\S+') +PROJECT_OWNERS_RE = re.compile(r'.*github.com.(.*)\)\)') class FormatChecker: def __init__(self, args): - self.args = args - self.config_path = args.config_path self.operation_type = args.operation_type self.target_path = args.target_path self.api_prefix = args.api_prefix self.envoy_build_rule_check = not args.skip_envoy_build_rule_check - self._include_dir_order = args.include_dir_order - - @cached_property - def build_fixer_check_excluded_paths(self): - return ( - tuple(self.args.build_fixer_check_excluded_paths) - + self.config.paths["build_fixer"]["exclude"]) - - @cached_property - def config(self) -> FormatConfig: - return FormatConfig(self.config_path) - - @cached_property - def include_dir_order(self): - return ",".join( - self._include_dir_order if self._include_dir_order else self.config["dir_order"]) - - @property - def namespace_check(self): - return self.args.namespace_check - - @cached_property - def namespace_check_excluded_paths(self): - return ( - tuple(self.args.namespace_check_excluded_paths) - + self.config.paths["namespace_check"]["exclude"]) + self.namespace_check = args.namespace_check + self.namespace_check_excluded_paths = args.namespace_check_excluded_paths + [ + "./tools/api_boost/testdata/", + "./tools/clang_tools/", + ] + self.build_fixer_check_excluded_paths = args.build_fixer_check_excluded_paths + [ + "./bazel/external/", + "./bazel/toolchains/", + "./bazel/BUILD", + "./tools/clang_tools", + ] + self.include_dir_order = args.include_dir_order @cached_property def namespace_re(self): @@ -215,12 +310,12 @@ def executable_by_others(self, executable): def check_tools(self): error_messages = [] - clang_format_abs_path = self.look_path(self.config.clang_format_path) + clang_format_abs_path = self.look_path(CLANG_FORMAT_PATH) if clang_format_abs_path: if not self.executable_by_others(clang_format_abs_path): error_messages.append( "command {} exists, but cannot be executed by other " - "users".format(self.config.clang_format_path)) + "users".format(CLANG_FORMAT_PATH)) else: error_messages.append( "Command {} not found. If you have clang-format in version 12.x.x " @@ -230,7 +325,7 @@ def check_tools(self): " export CLANG_FORMAT=clang-format-14.0.0\n" " export CLANG_FORMAT=/opt/bin/clang-format-14\n" " export CLANG_FORMAT=/usr/local/opt/llvm@14/bin/clang-format".format( - self.config.clang_format_path)) + CLANG_FORMAT_PATH)) def check_bazel_tool(name, path, var): bazel_tool_abs_path = self.look_path(path) @@ -255,8 +350,8 @@ def check_bazel_tool(name, path, var): " go get -u github.com/bazelbuild/buildtools/{}".format( path, name, var, var, name, name, name)) - check_bazel_tool('buildifier', self.config.buildifier_path, 'BUILDIFIER_BIN') - check_bazel_tool('buildozer', self.config.buildozer_path, 'BUILDOZER_BIN') + check_bazel_tool('buildifier', BUILDIFIER_PATH, 'BUILDIFIER_BIN') + check_bazel_tool('buildozer', BUILDOZER_PATH, 'BUILDOZER_BIN') return error_messages @@ -277,9 +372,8 @@ def check_namespace(self, file_path): # To avoid breaking the Lyft import, we just check for path inclusion here. def allow_listed_for_protobuf_deps(self, file_path): return ( - file_path.endswith(self.config.suffixes["proto"]) - or file_path.endswith(self.config.suffixes["repositories_bzl"]) - or any(file_path.startswith(path) for path in self.config.paths["protobuf"]["include"])) + file_path.endswith(PROTO_SUFFIX) or file_path.endswith(REPOSITORIES_BZL) + or any(path_segment in file_path for path_segment in GOOGLE_PROTOBUF_ALLOWLIST)) # Real-world time sources should not be instantiated in the source, except for a few # specific cases. They should be passed down from where they are instantied to where @@ -287,34 +381,31 @@ def allow_listed_for_protobuf_deps(self, file_path): def allow_listed_for_realtime(self, file_path): if file_path.endswith(".md"): return True - return file_path in self.config.paths["real_time"]["include"] + return file_path in REAL_TIME_ALLOWLIST def allow_listed_for_register_factory(self, file_path): if not file_path.startswith("./test/"): return True - return any( - file_path.startswith(prefix) - for prefix in self.config.paths["register_factory_test"]["include"]) + return any(file_path.startswith(prefix) for prefix in REGISTER_FACTORY_TEST_ALLOWLIST) def allow_listed_for_serialize_as_string(self, file_path): - return file_path in self.config.paths["serialize_as_string"]["include"] + return file_path in SERIALIZE_AS_STRING_ALLOWLIST def allow_listed_for_std_string_view(self, file_path): - return file_path in self.config.paths["std_string_view"]["include"] + return file_path in STD_STRING_VIEW_ALLOWLIST def allow_listed_for_json_string_to_message(self, file_path): - return file_path in self.config.paths["json_string_to_message"]["include"] + return file_path in JSON_STRING_TO_MESSAGE_ALLOWLIST def allow_listed_for_histogram_si_suffix(self, name): - return name in self.config.suffixes["histogram_with_si"]["include"] + return name in HISTOGRAM_WITH_SI_SUFFIX_ALLOWLIST def allow_listed_for_std_regex(self, file_path): - return file_path.startswith( - "./test") or file_path in self.config.paths["std_regex"]["include"] + return file_path.startswith("./test") or file_path in STD_REGEX_ALLOWLIST def allow_listed_for_grpc_init(self, file_path): - return file_path in self.config.paths["grpc_init"]["include"] + return file_path in GRPC_INIT_ALLOWLIST def allow_listed_for_unpack_to(self, file_path): return file_path.startswith("./test") or file_path in [ @@ -323,18 +414,17 @@ def allow_listed_for_unpack_to(self, file_path): def allow_listed_for_raw_try(self, file_path): # TODO(chaoqin-li1123): Exclude some important extensions from ALLOWLIST. - return file_path in self.config.paths["raw_try"]["include"] or file_path.startswith( - "./source/extensions") + return file_path in RAW_TRY_ALLOWLIST or file_path.startswith("./source/extensions") def deny_listed_for_exceptions(self, file_path): # Returns true when it is a non test header file or the file_path is in DENYLIST or # it is under tools/testdata subdirectory. - return (file_path.endswith('.h') and not file_path.startswith("./test/") and not file_path in self.config.paths["exception"]["include"]) or file_path in self.config.paths["exception"]["exclude"] \ + return (file_path.endswith('.h') and not file_path.startswith("./test/") and not file_path in EXCEPTION_ALLOWLIST) or file_path in EXCEPTION_DENYLIST \ or self.is_in_subdir(file_path, 'tools/testdata') def allow_listed_for_build_urls(self, file_path): - return file_path in self.config.paths["build_urls"]["include"] + return file_path in BUILD_URLS_ALLOWLIST def is_api_file(self, file_path): return file_path.startswith(self.api_prefix) @@ -363,28 +453,27 @@ def is_build_fixer_excluded_file(self, file_path): return False def has_invalid_angle_bracket_directory(self, line): - if not line.startswith(self.config["include_angle"]): + if not line.startswith(INCLUDE_ANGLE): return False - path = line[len(self.config["include_angle"]):] + path = line[INCLUDE_ANGLE_LEN:] slash = path.find("/") if slash == -1: return False subdir = path[0:slash] - return subdir in self.config.dir_order + return subdir in SUBDIR_SET # simple check that all flags are sorted. def check_runtime_flags(self, file_path, error_messages): previous_flag = "" for line_number, line in enumerate(self.read_lines(file_path)): if line.startswith("RUNTIME_GUARD"): - match = self.config.re["runtime_guard_flag"].match(line) + match = FLAG_REGEX.match(line) if not match: error_messages.append("%s does not look like a reloadable flag" % line) break if previous_flag: - if line < previous_flag and match.groups( - )[0] not in self.config["unsorted_flags"]: + if line < previous_flag and match.groups()[0] not in UNSORTED_FLAGS: error_messages.append( "%s and %s are out of order\n" % (line, previous_flag)) previous_flag = line @@ -411,23 +500,21 @@ def report_error(message): def fix_source_line(self, line, line_number): # Strip double space after '.' This may prove overenthusiastic and need to # be restricted to comments and metadata files but works for now. - line = self.config.re["dot_multi_space"].sub(". ", line) + line = re.sub(DOT_MULTI_SPACE_REGEX, ". ", line) if self.has_invalid_angle_bracket_directory(line): line = line.replace("<", '"').replace(">", '"') # Fix incorrect protobuf namespace references. - for invalid_construct, valid_construct in self.config.replacements[ - "protobuf_type_errors"].items(): + for invalid_construct, valid_construct in PROTOBUF_TYPE_ERRORS.items(): line = line.replace(invalid_construct, valid_construct) # Use recommended cpp stdlib - for invalid_construct, valid_construct in self.config.replacements["libcxx"].items(): + for invalid_construct, valid_construct in LIBCXX_REPLACEMENTS.items(): line = line.replace(invalid_construct, valid_construct) # Fix code conventions violations. - for invalid_construct, valid_construct in self.config.replacements["code_convention"].items( - ): + for invalid_construct, valid_construct in CODE_CONVENTION_REPLACEMENTS.items(): line = line.replace(invalid_construct, valid_construct) return line @@ -447,9 +534,6 @@ def has_cond_var_wait_for(self, line): return False return True - def is_api_proto(self, file_path): - return file_path.endswith(self.config.suffixes["proto"]) and self.is_api_file(file_path) - # Determines whether the filename is either in the specified subdirectory, or # at the top level. We consider files in the top level for the benefit of # the check_format testcases in tools/testdata/check_format. @@ -486,31 +570,29 @@ def check_source_line(self, line, file_path, report_error): if line.find(". ") != -1: report_error("over-enthusiastic spaces") if self.is_in_subdir(file_path, 'source', - 'include') and self.config.re["x_envoy_used_directly"].match(line): + 'include') and X_ENVOY_USED_DIRECTLY_REGEX.match(line): report_error( "Please do not use the raw literal x-envoy in source code. See Envoy::Http::PrefixValue." ) if self.has_invalid_angle_bracket_directory(line): report_error("envoy includes should not have angle brackets") - for invalid_construct, valid_construct in self.config.replacements[ - "protobuf_type_errors"].items(): + for invalid_construct, valid_construct in PROTOBUF_TYPE_ERRORS.items(): if invalid_construct in line: report_error( "incorrect protobuf type reference %s; " "should be %s" % (invalid_construct, valid_construct)) - for invalid_construct, valid_construct in self.config.replacements["libcxx"].items(): + for invalid_construct, valid_construct in LIBCXX_REPLACEMENTS.items(): if invalid_construct in line: report_error( "term %s should be replaced with standard library term %s" % (invalid_construct, valid_construct)) - for invalid_construct, valid_construct in self.config.replacements["code_convention"].items( - ): + for invalid_construct, valid_construct in CODE_CONVENTION_REPLACEMENTS.items(): if invalid_construct in line: report_error( "term %s should be replaced with preferred term %s" % (invalid_construct, valid_construct)) # Do not include the virtual_includes headers. - if self.config.re["virtual_include_headers"].search(line): + if VIRTUAL_INCLUDE_HEADERS_RE.search(line): report_error("Don't include the virtual includes headers.") # Some errors cannot be fixed automatically, and actionable, consistent, @@ -545,7 +627,7 @@ def check_source_line(self, line, file_path, report_error): "Don't use CondVar::waitFor(); use TimeSystem::waitFor() instead. If this " "already is TimeSystem::waitFor(), please name the TimeSystem variable " "time_system or time_system_ so the linter can understand.") - duration_arg = self.config.re["duration_value"].search(line) + duration_arg = DURATION_VALUE_REGEX.search(line) if duration_arg and duration_arg.group(1) != "0" and duration_arg.group(1) != "0.0": # Matching duration(int-const or float-const) other than zero report_error( @@ -632,7 +714,7 @@ def check_source_line(self, line, file_path, report_error): report_error( "Don't use __attribute__((packed)), use the PACKED_STRUCT macro defined " "in envoy/common/platform.h instead") - if self.config.re["designated_initializer"].search(line): + if DESIGNATED_INITIALIZER_REGEX.search(line): # Designated initializers are not part of the C++14 standard and are not supported # by MSVC report_error( @@ -645,13 +727,13 @@ def check_source_line(self, line, file_path, report_error): report_error("Don't use 'using testing::Test;, elaborate the type instead") if line.startswith("using testing::TestWithParams;"): report_error("Don't use 'using testing::Test;, elaborate the type instead") - if self.config.re["test_name_starting_lc"].search(line): + if TEST_NAME_STARTING_LOWER_CASE_REGEX.search(line): # Matches variants of TEST(), TEST_P(), TEST_F() etc. where the test name begins # with a lowercase letter. report_error("Test names should be CamelCase, starting with a capital letter") - if self.config.re["old_mock_method"].search(line): + if OLD_MOCK_METHOD_REGEX.search(line): report_error("The MOCK_METHODn() macros should not be used, use MOCK_METHOD() instead") - if self.config.re["for_each_n"].search(line): + if FOR_EACH_N_REGEX.search(line): report_error("std::for_each_n should not be used, use an alternative for loop instead") if not self.allow_listed_for_serialize_as_string(file_path) and "SerializeAsString" in line: @@ -675,10 +757,10 @@ def check_source_line(self, line, file_path, report_error): report_error( "Don't lookup stats by name at runtime; use StatName saved during construction") - if self.config.re["mangled_protobuf_name"].search(line): + if MANGLED_PROTOBUF_NAME_REGEX.search(line): report_error("Don't use mangled Protobuf names for enum constants") - hist_m = self.config.re["histogram_si_suffix"].search(line) + hist_m = HISTOGRAM_SI_SUFFIX_REGEX.search(line) if hist_m and not self.allow_listed_for_histogram_si_suffix(hist_m.group(0)): report_error( "Don't suffix histogram names with the unit symbol, " @@ -706,7 +788,7 @@ def check_source_line(self, line, file_path, report_error): "Don't call grpc_init() or grpc_shutdown() directly, instantiate " + "Grpc::GoogleGrpcContext. See #8282") - if not self.included_for_memcpy(file_path) and \ + if not self.whitelisted_for_memcpy(file_path) and \ not ("test/" in file_path) and \ ("memcpy(" in line) and \ not ("NOLINT(safe-memcpy)" in line): @@ -717,7 +799,7 @@ def check_source_line(self, line, file_path, report_error): if self.deny_listed_for_exceptions(file_path): # Skpping cases where 'throw' is a substring of a symbol like in "foothrowBar". if "throw" in line.split(): - comment_match = self.config.re["comment"].search(line) + comment_match = COMMENT_REGEX.search(line) if comment_match is None or comment_match.start(0) > line.find("throw"): report_error( "Don't introduce throws into exception-free files, use error " @@ -729,9 +811,9 @@ def check_source_line(self, line, file_path, report_error): + "Lua API (bad light userdata pointer) on ARM64 architecture. See " + "https://github.com/LuaJIT/LuaJIT/issues/450#issuecomment-433659873 for details.") - if file_path.endswith(self.config.suffixes["proto"]): + if file_path.endswith(PROTO_SUFFIX): exclude_path = ['v1', 'v2'] - result = self.config.re["proto_validation_string"].search(line) + result = PROTO_VALIDATION_STRING.search(line) if result is not None: if not any(x in file_path for x in exclude_path): report_error("min_bytes is DEPRECATED, Use min_len.") @@ -770,11 +852,10 @@ def fix_build_path(self, file_path): if not self.is_build_fixer_excluded_file(file_path) and not self.is_api_file( file_path) and not self.is_starlark_file(file_path) and not self.is_workspace_file( file_path): - if os.system("%s %s %s" % - (self.config.paths["build_fixer_py"], file_path, file_path)) != 0: + if os.system("%s %s %s" % (ENVOY_BUILD_FIXER_PATH, file_path, file_path)) != 0: error_messages += ["envoy_build_fixer rewrite failed for file: %s" % file_path] - if os.system("%s -lint=fix -mode=fix %s" % (self.config.buildifier_path, file_path)) != 0: + if os.system("%s -lint=fix -mode=fix %s" % (BUILDIFIER_PATH, file_path)) != 0: error_messages += ["buildifier rewrite failed for file: %s" % file_path] return error_messages @@ -784,8 +865,7 @@ def check_build_path(self, file_path): if not self.is_build_fixer_excluded_file(file_path) and not self.is_api_file( file_path) and not self.is_starlark_file(file_path) and not self.is_workspace_file( file_path): - command = "%s %s | diff %s -" % ( - self.config.paths["build_fixer_py"], file_path, file_path) + command = "%s %s | diff %s -" % (ENVOY_BUILD_FIXER_PATH, file_path, file_path) error_messages += self.execute_command( command, "envoy_build_fixer check failed", file_path) @@ -798,7 +878,7 @@ def check_build_path(self, file_path): if not found: error_messages += ["API build file does not provide api_proto_package()"] - command = "%s -mode=diff %s" % (self.config.buildifier_path, file_path) + command = "%s -mode=diff %s" % (BUILDIFIER_PATH, file_path) error_messages += self.execute_command(command, "buildifier check failed", file_path) error_messages += self.check_file_contents(file_path, self.check_build_line) return error_messages @@ -808,7 +888,7 @@ def fix_source_path(self, file_path): error_messages = [] - if not file_path.endswith(self.config.suffixes["proto"]): + if not file_path.endswith(PROTO_SUFFIX): error_messages += self.fix_header_order(file_path) error_messages += self.clang_format(file_path) return error_messages @@ -816,15 +896,14 @@ def fix_source_path(self, file_path): def check_source_path(self, file_path): error_messages = self.check_file_contents(file_path, self.check_source_line) - if not file_path.endswith(self.config.suffixes["proto"]): + if not file_path.endswith(PROTO_SUFFIX): error_messages += self.check_namespace(file_path) command = ( - "%s --include_dir_order %s --path %s | diff %s -" % ( - self.config.paths["header_order_py"], self.include_dir_order, file_path, - file_path)) + "%s --include_dir_order %s --path %s | diff %s -" % + (HEADER_ORDER_PATH, self.include_dir_order, file_path, file_path)) error_messages += self.execute_command( command, "header_order.py check failed", file_path) - command = ("%s %s | diff %s -" % (self.config.clang_format_path, file_path, file_path)) + command = ("%s %s | diff %s -" % (CLANG_FORMAT_PATH, file_path, file_path)) error_messages += self.execute_command(command, "clang-format check failed", file_path) return error_messages @@ -832,8 +911,7 @@ def check_source_path(self, file_path): # - "26,27c26" # - "12,13d13" # - "7a8,9" - def execute_command(self, command, error_message, file_path, regex=None): - regex = regex or self.config.re["line_number"] + def execute_command(self, command, error_message, file_path, regex=LINE_NUMBER_RE): try: output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() if output: @@ -851,13 +929,13 @@ def execute_command(self, command, error_message, file_path, regex=None): def fix_header_order(self, file_path): command = "%s --rewrite --include_dir_order %s --path %s" % ( - self.config.paths["header_order_py"], self.include_dir_order, file_path) + HEADER_ORDER_PATH, self.include_dir_order, file_path) if os.system(command) != 0: return ["header_order.py rewrite error: %s" % (file_path)] return [] def clang_format(self, file_path): - command = "%s -i %s" % (self.config.clang_format_path, file_path) + command = "%s -i %s" % (CLANG_FORMAT_PATH, file_path) if os.system(command) != 0: return ["clang-format rewrite error: %s" % (file_path)] return [] @@ -951,14 +1029,10 @@ def check_format_visitor(self, arg, dir_name, names, fail_on_diff=False): dir_name = normalize_path(dir_name) - # TODO(phlax): improve class/process handling - this is required because if these - # are not cached before the class is sent into the pool, it only caches them on the + # TODO(phlax): improve class/process handling - this is required because if it + # is not cached before the class is sent into the pool, it only caches on the # forked proc - self.build_fixer_check_excluded_paths - self.namespace_check_excluded_paths self.namespace_re - self.config.replacements - self.config.dir_order for file_name in names: result = pool.apply_async( @@ -974,8 +1048,8 @@ def check_error_messages(self, error_messages): return True return False - def included_for_memcpy(self, file_path): - return file_path in self.config.paths["memcpy"]["include"] + def whitelisted_for_memcpy(self, file_path): + return file_path in MEMCPY_WHITELIST def normalize_path(path): @@ -1003,10 +1077,6 @@ def normalize_path(path): nargs="?", default=".", help="specify the root directory for the script to recurse over. Default '.'.") - parser.add_argument( - "--config_path", - default="./tools/code_format/config.yaml", - help="specify the config path. Default './tools/code_format/config.yaml'.") parser.add_argument( "--fail_on_diff", action="store_true", @@ -1051,37 +1121,41 @@ def normalize_path(path): parser.add_argument( "--include_dir_order", type=str, - default="", + default=",".join(common.include_dir_order()), help="specify the header block include directory order.") args = parser.parse_args() - - format_checker = FormatChecker(args) - - excluded_prefixes = format_checker.config.paths["excluded"] if args.add_excluded_prefixes: - excluded_prefixes += tuple(args.add_excluded_prefixes) + EXCLUDED_PREFIXES += tuple(args.add_excluded_prefixes) + format_checker = FormatChecker(args) # Check whether all needed external tools are available. ct_error_messages = format_checker.check_tools() if format_checker.check_error_messages(ct_error_messages): sys.exit(1) - if not os.environ.get("CI"): - # TODO(phlax): Remove this after a month or so - logger.warning( - "Please note: `tools/code_format/check_format.py` no longer checks API `.proto` files, " - "please use `tools/proto_format/proto_format.sh` if you are making changes to the API files" - ) + # TODO(phlax): Remove this after a month or so + logger.warning( + "Please note: `tools/code_format/check_format.py` no longer checks API `.proto` files, " + "please use `tools/proto_format/proto_format.sh` if you are making changes to the API files" + ) def check_visibility(error_messages): + # https://github.com/envoyproxy/envoy/issues/20589 + # https://github.com/envoyproxy/envoy/issues/9953 + # PLEASE DO NOT ADD FILES TO THIS LIST WITHOUT SENIOR MAINTAINER APPROVAL + exclude_list = ( + "':(exclude)source/extensions/early_data/BUILD' " + "':(exclude)source/extensions/filters/http/buffer/BUILD' " + "':(exclude)source/extensions/filters/network/common/BUILD' " + "':(exclude)source/extensions/transport_sockets/common/BUILD' " + "':(exclude)source/extensions/udp_packet_writer/default/BUILD' " + "':(exclude)source/extensions/udp_packet_writer/gso/BUILD' ") command = ( - "git diff $(tools/git/last_github_commit.sh) -- source/extensions/* %s | grep '+.*visibility ='" - % ''.join([f"':(exclude){c}' " for c in format_checker.config["visibility_excludes"]])) + "git diff $(tools/git/last_github_commit.sh) -- source/extensions/* %s |grep '+.*visibility ='" + % exclude_list) try: output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() if output: - error_messages.append(output) - error_messages.append(command) error_messages.append( "This change appears to add visibility rules. Please get senior maintainer " "approval to add an exemption to check_visibility tools/code_format/check_format.py" @@ -1103,7 +1177,7 @@ def get_owners(): for line in f: if "Senior extension maintainers" in line: return maintainers - m = format_checker.config.re["maintainers"].search(line) + m = PROJECT_OWNERS_RE.search(line) if m is not None: maintainers.append("@" + m.group(1).lower()) @@ -1118,10 +1192,10 @@ def owned_directories(error_messages): for line in f: # If this line is of the form "extensions/... @owner1 @owner2" capture the directory # name and store it in the list of directories with documented owners. - m = format_checker.config.re["codeowners_extensions"].search(line) + m = EXTENSIONS_CODEOWNERS_REGEX.search(line) if m is not None and not line.startswith('#'): owned.append(m.group(1).strip()) - owners = format_checker.config.re["owner"].findall(m.group(2).strip()) + owners = OWNER_RE.findall(m.group(2).strip()) if len(owners) < 2: error_messages.append( "Extensions require at least 2 owners in CODEOWNERS:\n" @@ -1132,7 +1206,7 @@ def owned_directories(error_messages): "Extensions require at least one maintainer OWNER:\n" " {}".format(line)) - m = format_checker.config.re["codeowners_contrib"].search(line) + m = CONTRIB_CODEOWNERS_REGEX.search(line) if m is not None and not line.startswith('#'): stripped_path = m.group(1).strip() if not stripped_path.endswith('/'): @@ -1150,7 +1224,7 @@ def owned_directories(error_messages): continue owned.append(stripped_path) - owners = format_checker.config.re["owner"].findall(m.group(2).strip()) + owners = OWNER_RE.findall(m.group(2).strip()) if len(owners) < 2: error_messages.append( "Contrib extensions require at least 2 owners in CODEOWNERS:\n" @@ -1167,13 +1241,12 @@ def owned_directories(error_messages): check_visibility(error_messages) if os.path.isfile(args.target_path): - # All of our `excluded_prefixes` start with "./", but the provided + # All of our EXCLUDED_PREFIXES start with "./", but the provided # target path argument might not. Add it here if it is missing, # and use that normalized path for both lookup and `check_format`. normalized_target_path = normalize_path(args.target_path) if not normalized_target_path.startswith( - excluded_prefixes) and normalized_target_path.endswith( - format_checker.config.suffixes["included"]): + EXCLUDED_PREFIXES) and normalized_target_path.endswith(SUFFIXES): error_messages += format_checker.check_format(normalized_target_path) else: results = [] @@ -1187,10 +1260,9 @@ def pooled_check_format(path_predicate): for filename in files: file_path = os.path.join(root, filename) check_file = ( - path_predicate(filename) and not file_path.startswith(excluded_prefixes) - and file_path.endswith(format_checker.config.suffixes["included"]) and not ( - file_path.endswith(format_checker.config.suffixes["proto"]) - and root.startswith(args.api_prefix))) + path_predicate(filename) and not file_path.startswith(EXCLUDED_PREFIXES) + and file_path.endswith(SUFFIXES) and + not (file_path.endswith(PROTO_SUFFIX) and root.startswith(args.api_prefix))) if check_file: _files.append(filename) if not _files: From 225c1f0952946d3ef5e0bc55237b5b9e21d15655 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 19 Aug 2022 18:22:26 +0000 Subject: [PATCH 178/238] Changes per comments Signed-off-by: silverstar195 --- .../proto/uri_template_rewrite_segments.proto | 2 +- .../path/uri_template_lib/uri_template.cc | 31 +++-- .../path/uri_template_lib/uri_template.h | 16 +-- .../uri_template_lib/uri_template_internal.cc | 120 +++++++++--------- .../uri_template_lib/uri_template_internal.h | 71 ++++++----- .../uri_template_internal_test.cc | 50 ++++---- .../uri_template_lib/uri_template_test.cc | 8 +- 7 files changed, 153 insertions(+), 145 deletions(-) diff --git a/source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.proto b/source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.proto index 323f5250de855..4c4f8bd10789b 100644 --- a/source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.proto +++ b/source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.proto @@ -21,7 +21,7 @@ message UriTemplateRewriteSegments { // Represents an index into the RE2 capture which value should be used // to construct the rewritten URL. Note that the index should be greater // than 0 as 0 index into the whole match RE2 pattern. - int32 var_index = 2; + int32 capture_index = 2; } } diff --git a/source/extensions/path/uri_template_lib/uri_template.cc b/source/extensions/path/uri_template_lib/uri_template.cc index 38fec75f5b2a7..ad0a1289d220d 100644 --- a/source/extensions/path/uri_template_lib/uri_template.cc +++ b/source/extensions/path/uri_template_lib/uri_template.cc @@ -25,6 +25,11 @@ using Internal::ParsedUrlPattern; #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif +// Match pattern is used to match route. +// Example: /foo/{bar} matches /foo/cat + +// Rewrite pattern is used to rewrite the matched route. + absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { absl::StatusOr status = Internal::parseURLPatternSyntax(url_pattern); if (!status.ok()) { @@ -33,23 +38,23 @@ absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url return Internal::toRegexPattern(*status); } -absl::StatusOr> parseRewritePattern(absl::string_view pattern) { +absl::StatusOr> parseRewritePattern(absl::string_view url_pattern) { std::vector result; // The pattern should start with a '/' and thus the first segment should // always be a literal. - if (pattern.empty() || pattern[0] != '/') { + if (url_pattern.empty() || url_pattern[0] != '/') { return absl::InvalidArgumentError("Invalid rewrite variable placement"); } // Don't allow contiguous '/' patterns. static const LazyRE2 invalid_regex = {"^.*//.*$"}; - if (RE2::FullMatch(Internal::toStringPiece(pattern), *invalid_regex)) { + if (RE2::FullMatch(Internal::toStringPiece(url_pattern), *invalid_regex)) { return absl::InvalidArgumentError("Invalid rewrite literal"); } - while (!pattern.empty()) { - std::vector segments1 = absl::StrSplit(pattern, absl::MaxSplits('{', 1)); + while (!url_pattern.empty()) { + std::vector segments1 = absl::StrSplit(url_pattern, absl::MaxSplits('{', 1)); if (!segments1[0].empty()) { if (!Internal::isValidRewriteLiteral(segments1[0])) { return absl::InvalidArgumentError("Invalid rewrite literal pattern"); @@ -67,7 +72,7 @@ absl::StatusOr> parseRewritePattern(absl::str if (segments2.size() < 2) { return absl::InvalidArgumentError("Unmatched variable bracket"); } - pattern = segments2[1]; + url_pattern = segments2[1]; if (!Internal::isValidVariableName(segments2[0])) { return absl::InvalidArgumentError("Invalid variable name"); @@ -103,7 +108,7 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) if (it == capture_index_map.end()) { return absl::InvalidArgumentError("Nonexisting variable name"); } - parsed_pattern.add_segments()->set_var_index(it->second); + parsed_pattern.add_segments()->set_capture_index(it->second); break; } } @@ -115,17 +120,17 @@ absl::Status isValidMatchPattern(absl::string_view path_template_match) { return convertURLPatternSyntaxToRegex(path_template_match).status(); } -absl::Status isValidPathTemplateRewritePattern(absl::string_view path_template_rewrite) { +absl::Status isValidRewritePattern(absl::string_view path_template_rewrite) { return parseRewritePattern(path_template_rewrite).status(); } -absl::Status isValidSharedVariableSet(absl::string_view path_template_rewrite, +absl::Status isValidSharedVariableSet(absl::string_view pattern, absl::string_view capture_regex) { absl::StatusOr status = convertURLPatternSyntaxToRegex(capture_regex).value(); if (!status.ok()) { return status.status(); } - return parseRewritePattern(path_template_rewrite, *std::move(status)).status(); + return parseRewritePattern(pattern, *std::move(status)).status(); } absl::StatusOr rewriteURLTemplatePattern( @@ -149,11 +154,11 @@ absl::StatusOr rewriteURLTemplatePattern( rewrite_pattern.segments()) { if (segment.has_literal()) { absl::StrAppend(&rewritten_url, segment.literal()); - } else if (segment.has_var_index()) { - if (segment.var_index() < 1 || segment.var_index() >= capture_num) { + } else if (segment.capture_index()) { + if (segment.capture_index() < 1 || segment.capture_index() >= capture_num) { return absl::InvalidArgumentError("Invalid variable index"); } - absl::StrAppend(&rewritten_url, absl::string_view(captures[segment.var_index()].as_string())); + absl::StrAppend(&rewritten_url, absl::string_view(captures[segment.capture_index()].as_string())); } } diff --git a/source/extensions/path/uri_template_lib/uri_template.h b/source/extensions/path/uri_template_lib/uri_template.h index 74e58f41974fd..bc32aa107a19a 100644 --- a/source/extensions/path/uri_template_lib/uri_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -21,30 +21,26 @@ struct RewritePatternSegment { RewriteStringKind kind_; }; -// Returns the regex pattern that is equivalent to the given url_pattern. -// Used in the config pipeline to translate user given url pattern to -// the safe regex Envoy can understand. Strips away any variable captures. +// Returns the safe regex that Envoy understands that is equivalent to the given pattern. absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); // Parses the specified pattern into a sequence of segments (which are // either literals or variable names). -absl::StatusOr> parseRewritePattern(absl::string_view pattern); +absl::StatusOr> parseRewritePattern(absl::string_view url_pattern); -// Returns the parsed Url rewrite pattern and processes variables to be used by -// RewriteURLTemplatePattern() and in rewrite. -// |capture_regex| should be the regex generated by ConvertURLPatternSyntaxToRegex(). +// Returns the parsed Url rewrite pattern and processes variables. absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); // Returns if provided rewrite pattern is valid -absl::Status isValidPathTemplateRewritePattern(absl::string_view path_template_rewrite); +absl::Status isValidRewritePattern(absl::string_view path_template_rewrite); // Returns if path_template_rewrite and capture_regex have valid variables. // Every variable in rewrite MUST be present in match. // For example: -// Match: /foo/{bar}/{var} and Rewrite: /goo/{var} is valid +// Match: /foo/{bar}/{var} and Rewrite: /goo/{var} is valid. // Match: /foo/{bar} and Rewrite: /goo/{bar}/{var} is invalid. Match is missing {var}. -absl::Status isValidSharedVariableSet(absl::string_view path_template_rewrite, +absl::Status isValidSharedVariableSet(absl::string_view pattern, absl::string_view capture_regex); // Returns is the match_pattern is valid. diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.cc b/source/extensions/path/uri_template_lib/uri_template_internal.cc index e6b133eff870f..0e60318260177 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.cc +++ b/source/extensions/path/uri_template_lib/uri_template_internal.cc @@ -83,12 +83,12 @@ std::string toString(const Operator val) { } std::string toString(const Variable val) { - if (val.var_match_.empty()) { - return absl::StrCat("{", val.var_name_, "}"); + if (val.match_.empty()) { + return absl::StrCat("{", val.name_, "}"); } - return absl::StrCat("{", val.var_name_, "=", - absl::StrJoin(val.var_match_, "/", ToStringFormatter()), "}"); + return absl::StrCat("{", val.name_, "=", + absl::StrJoin(val.match_, "/", ToStringFormatter()), "}"); } template std::string ToStringVisitor::operator()(const T& val) const { @@ -97,16 +97,16 @@ template std::string ToStringVisitor::operator()(const T& val) cons template absl::StatusOr alsoUpdatePattern( - absl::FunctionRef>(absl::string_view)> consume_func, + absl::FunctionRef>(absl::string_view)> parse_func, absl::string_view* patt) { - absl::StatusOr> status = consume_func(*patt); + absl::StatusOr> status = parse_func(*patt); if (!status.ok()) { return status.status(); } ParsedResult result = *std::move(status); - *patt = result.unconsumed_pattern_; + *patt = result.unparsed_pattern_; return result.parsed_value_; } @@ -133,22 +133,22 @@ bool isValidRewriteLiteral(absl::string_view literal) { return RE2::FullMatch(toStringPiece(literal), *literal_regex); } -bool isValidVariableName(absl::string_view indent) { - static const LazyRE2 ident_regex = {"^[a-zA-Z][a-zA-Z0-9_]*$"}; - return RE2::FullMatch(toStringPiece(indent), *ident_regex); +bool isValidVariableName(absl::string_view variable) { + static const LazyRE2 variable_regex = {"^[a-zA-Z][a-zA-Z0-9_]*$"}; + return RE2::FullMatch(toStringPiece(variable), *variable_regex); } -absl::StatusOr> consumeLiteral(absl::string_view pattern) { - absl::string_view lit = +absl::StatusOr> parseLiteral(absl::string_view pattern) { + absl::string_view literal = std::vector(absl::StrSplit(pattern, absl::MaxSplits('/', 1)))[0]; - absl::string_view unconsumed_pattern = pattern.substr(lit.size()); - if (!isValidLiteral(lit)) { + absl::string_view unparsed_pattern = pattern.substr(literal.size()); + if (!isValidLiteral(literal)) { return absl::InvalidArgumentError("Invalid literal"); } - return ParsedResult(lit, unconsumed_pattern); + return ParsedResult(literal, unparsed_pattern); } -absl::StatusOr> consumeOperator(absl::string_view pattern) { +absl::StatusOr> parseOperator(absl::string_view pattern) { if (absl::StartsWith(pattern, "**")) { return ParsedResult(Operator::TextGlob, pattern.substr(2)); } @@ -158,7 +158,7 @@ absl::StatusOr> consumeOperator(absl::string_view pattern return absl::InvalidArgumentError("Invalid Operator"); } -absl::StatusOr> consumeVariable(absl::string_view pattern) { +absl::StatusOr> parseVariable(absl::string_view pattern) { // Locate the variable pattern to parse. if (pattern.size() < 2 || (pattern)[0] != '{') { return absl::InvalidArgumentError("Invalid variable"); @@ -167,55 +167,54 @@ absl::StatusOr> consumeVariable(absl::string_view pattern if (parts.size() != 2) { return absl::InvalidArgumentError("Unmatched variable bracket"); } - absl::string_view unconsumed_pattern = parts[1]; + absl::string_view unparsed_pattern = parts[1]; // Parse the actual variable pattern, starting with the variable name. - std::vector var_parts = absl::StrSplit(parts[0], absl::MaxSplits('=', 1)); - if (!isValidVariableName(var_parts[0])) { + std::vector variable_parts = absl::StrSplit(parts[0], absl::MaxSplits('=', 1)); + if (!isValidVariableName(variable_parts[0])) { return absl::InvalidArgumentError("Invalid variable name"); } - Variable var = Variable(var_parts[0], {}); + Variable var = Variable(variable_parts[0], {}); // Parse the variable match pattern (if any). - if (var_parts.size() < 2) { - return ParsedResult(var, unconsumed_pattern); + if (variable_parts.size() < 2) { + return ParsedResult(var, unparsed_pattern); } - absl::string_view var_patt = var_parts[1]; - if (var_patt.empty()) { + absl::string_view pattern_item = variable_parts[1]; + if (pattern_item.empty()) { return absl::InvalidArgumentError("Empty variable match"); } - while (!var_patt.empty()) { - absl::variant var_match; - if (var_patt[0] == '*') { + while (!pattern_item.empty()) { + absl::variant match; + if (pattern_item[0] == '*') { - absl::StatusOr status = alsoUpdatePattern(consumeOperator, &var_patt); + absl::StatusOr status = alsoUpdatePattern(parseOperator, &pattern_item); if (!status.ok()) { return status.status(); } - var_match = *std::move(status); + match = *std::move(status); } else { - - absl::StatusOr status = alsoUpdatePattern(consumeLiteral, &var_patt); + absl::StatusOr status = alsoUpdatePattern(parseLiteral, &pattern_item); if (!status.ok()) { return status.status(); } - var_match = *std::move(status); + match = *std::move(status); } - var.var_match_.push_back(var_match); - if (!var_patt.empty()) { - if (var_patt[0] != '/' || var_patt.size() == 1) { + var.match_.push_back(match); + if (!pattern_item.empty()) { + if (pattern_item[0] != '/' || pattern_item.size() == 1) { return absl::InvalidArgumentError("Invalid variable match"); } - var_patt = var_patt.substr(1); + pattern_item = pattern_item.substr(1); } } - return ParsedResult(var, unconsumed_pattern); + return ParsedResult(var, unparsed_pattern); } absl::StatusOr> -gatherCaptureNames(struct ParsedUrlPattern pattern) { +gatherCaptureNames(const struct ParsedUrlPattern& pattern) { absl::flat_hash_set captured_variables; for (const ParsedSegment& segment : pattern.parsed_segments_) { @@ -225,22 +224,22 @@ gatherCaptureNames(struct ParsedUrlPattern pattern) { if (captured_variables.size() >= kPatternMatchingMaxVariablesPerUrl) { return absl::InvalidArgumentError("Exceeded variable count limit"); } - absl::string_view var_name = absl::get(segment).var_name_; + absl::string_view name = absl::get(segment).name_; - if (var_name.size() < kPatternMatchingMinVariableNameLen || - var_name.size() > kPatternMatchingMaxVariableNameLen) { - return absl::InvalidArgumentError("Invalid variable length"); + if (name.size() < kPatternMatchingMinVariableNameLen || + name.size() > kPatternMatchingMaxVariableNameLen) { + return absl::InvalidArgumentError("Invalid variable name length"); } - if (captured_variables.contains(var_name)) { + if (captured_variables.contains(name)) { return absl::InvalidArgumentError("Repeated variable name"); } - captured_variables.emplace(var_name); + captured_variables.emplace(name); } return captured_variables; } -absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { +absl::Status validateNoOperatorAfterTextGlob(const struct ParsedUrlPattern& pattern) { bool seen_text_glob = false; for (const ParsedSegment& segment : pattern.parsed_segments_) { if (absl::holds_alternative(segment)) { @@ -250,13 +249,13 @@ absl::Status validateNoOperatorAfterTextGlob(struct ParsedUrlPattern pattern) { seen_text_glob = (absl::get(segment) == Operator::TextGlob); } else if (absl::holds_alternative(segment)) { const Variable& var = absl::get(segment); - if (var.var_match_.empty()) { + if (var.match_.empty()) { if (seen_text_glob) { // A variable with no explicit matcher is treated as a path glob. return absl::InvalidArgumentError("Implicit variable path glob after text glob."); } } else { - for (const absl::variant& var_seg : var.var_match_) { + for (const absl::variant& var_seg : var.match_) { if (!absl::holds_alternative(var_seg)) { continue; } @@ -276,33 +275,29 @@ absl::StatusOr parseURLPatternSyntax(absl::string_view url_pat static const LazyRE2 printable_regex = {"^/[[:graph:]]*$"}; if (!RE2::FullMatch(toStringPiece(url_pattern), *printable_regex)) { - return absl::InvalidArgumentError("Invalid pattern"); } - // Consume the leading '/' + // Parse the leading '/' url_pattern = url_pattern.substr(1); // Do the initial lexical parsing. while (!url_pattern.empty()) { ParsedSegment segment; if (url_pattern[0] == '*') { - - absl::StatusOr status = alsoUpdatePattern(consumeOperator, &url_pattern); + absl::StatusOr status = alsoUpdatePattern(parseOperator, &url_pattern); if (!status.ok()) { return status.status(); } segment = *std::move(status); } else if (url_pattern[0] == '{') { - - absl::StatusOr status = alsoUpdatePattern(consumeVariable, &url_pattern); + absl::StatusOr status = alsoUpdatePattern(parseVariable, &url_pattern); if (!status.ok()) { return status.status(); } segment = *std::move(status); } else { - - absl::StatusOr status = alsoUpdatePattern(consumeLiteral, &url_pattern); + absl::StatusOr status = alsoUpdatePattern(parseLiteral, &url_pattern); if (!status.ok()) { return status.status(); } @@ -317,18 +312,17 @@ absl::StatusOr parseURLPatternSyntax(absl::string_view url_pat parsed_pattern.parsed_segments_.emplace_back(""); break; } else if (url_pattern[0] == '/') { - // Have '/' followed by more text, consume the '/'. + // Have '/' followed by more text, parse the '/'. url_pattern = url_pattern.substr(1); } else { // Not followed by '/', treat as suffix. - - absl::StatusOr status = alsoUpdatePattern(consumeLiteral, &url_pattern); + absl::StatusOr status = alsoUpdatePattern(parseLiteral, &url_pattern); if (!status.ok()) { return status.status(); } parsed_pattern.suffix_ = *std::move(status); if (!url_pattern.empty()) { - // Suffix didn't consume whole remaining pattern ('/' in url_pattern). + // Suffix didn't parse whole remaining pattern ('/' in url_pattern). return absl::InvalidArgumentError("Prefix match not supported."); } break; @@ -368,10 +362,10 @@ std::string toRegexPattern(Operator pattern) { } std::string toRegexPattern(const Variable& pattern) { - return absl::StrCat("(?P<", pattern.var_name_, ">", - pattern.var_match_.empty() + return absl::StrCat("(?P<", pattern.name_, ">", + pattern.match_.empty() ? toRegexPattern(kDefaultVariableOperator) - : absl::StrJoin(pattern.var_match_, "/", ToRegexPatternFormatter()), + : absl::StrJoin(pattern.match_, "/", ToRegexPatternFormatter()), ")"); } diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.h b/source/extensions/path/uri_template_lib/uri_template_internal.h index 48155db8dad1a..38b470ef15a28 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.h +++ b/source/extensions/path/uri_template_lib/uri_template_internal.h @@ -20,12 +20,12 @@ namespace UriTemplate { namespace Internal { /** - * String to be concatenated in rewritten url + * String to be concatenated in rewritten url. */ using Literal = absl::string_view; /** - * Determines what operations to use on the input pattern segment + * Determines what operations to use on the input pattern segment. */ enum class Operator { PathGlob, TextGlob }; @@ -33,11 +33,13 @@ enum class Operator { PathGlob, TextGlob }; * Represents a pattern variable. Variables are included in both path match and rewrite paths. */ struct Variable { - absl::string_view var_name_; - std::vector> var_match_; + absl::string_view name_; + + // replacement value for the rewrite + std::vector> match_; Variable(absl::string_view name, std::vector> match) - : var_name_(name), var_match_(match) {} + : name_(name), match_(match) {} std::string debugString() const; }; @@ -45,10 +47,16 @@ struct Variable { using ParsedSegment = absl::variant; /** - * Represents the parsed path including literals and variables. + * Represents the parsed url including literals and variables. */ struct ParsedUrlPattern { std::vector parsed_segments_; + + // Suffix holds end of url that matched a wildcard + // For example: + // Pattern: /foo/bar/** + // Url: /foo/bar/some/more/stuff + // Suffix: some/more/stuff absl::optional suffix_; absl::flat_hash_set captured_variables_; @@ -56,82 +64,87 @@ struct ParsedUrlPattern { }; /** - * Check if literal is valid - * Literals cannot hold wildcards or curl brackets. + * Returns true if `literal` is valid for pattern match + * (does not contain wildcards or curly brackets). */ bool isValidLiteral(absl::string_view literal); /** - * Check if rewrite literal is valid - * Literals cannot hold wildcards or curl brackets. + * Returns true if `literal` is valid for pattern rewrite + * (does not contain wildcards or curly brackets). */ bool isValidRewriteLiteral(absl::string_view literal); /** - * Check if variable name is valid + * Check if variable name is valid. */ -bool isValidVariableName(absl::string_view indent); +bool isValidVariableName(absl::string_view variable); /** - * Used by the following Consume{Literal.Operator,Variable} functions + * Used by the following Parse{Literal.Operator,Variable} functions * in the return value. The functions would take the given pattern, - * parse what it can into |parsed_value| and return the unconsumed - * portion of the pattern in |unconsumed_pattern|. + * parse what it can into |parsed_value| and return the unparse + * portion of the pattern in |unparsed_pattern|. */ template struct ParsedResult { ParsedResult(T val, absl::string_view pattern) - : parsed_value_(val), unconsumed_pattern_(pattern) {} + : parsed_value_(val), unparsed_pattern_(pattern) {} T parsed_value_; - absl::string_view unconsumed_pattern_; + absl::string_view unparsed_pattern_; }; /** - * Converts input pattern to ParsedResult + * Parses a literal in the front of `pattern` and returns the literal and remaining pattern. */ -absl::StatusOr> consumeLiteral(absl::string_view pattern); +absl::StatusOr> parseLiteral(absl::string_view pattern); /** - * Converts input pattern to ParsedResult + * Converts input pattern to ParsedResult. */ -absl::StatusOr> consumeOperator(absl::string_view pattern); +absl::StatusOr> parseOperator(absl::string_view pattern); /** - * Converts input pattern to ParsedResult + * Parses a variable in the front of `pattern`. */ -absl::StatusOr> consumeVariable(absl::string_view pattern); +absl::StatusOr> parseVariable(absl::string_view pattern); /** - * Converts input pattern to ParsedUrlPattern. + * Converts input url to ParsedUrlPattern. * ParsedUrlPattern hold prefix, string literals, and variables for rewrite. */ absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern); /** - * Converts Literal to std::string + * Convert Literal to a regex string representation */ std::string toRegexPattern(Literal pattern); /** - * Converts Operator to std::string + * Converts Operator to a regex string representation */ std::string toRegexPattern(Operator pattern); /** - * Converts Variable to std::string + * Converts Variable to a regex string representation */ std::string toRegexPattern(const Variable& pattern); /** - * Converts ParsedUrlPattern to std::string + * Converts ParsedUrlPattern to a regex string representation. */ std::string toRegexPattern(const struct ParsedUrlPattern& pattern); /** - * Converts string_view to be used in re2 + * Converts string_view to be used in re2::string_piece. */ inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.data(), text.size()}; } +/** + * Checks end of pattern to ensure glob operator is last. + */ +absl::Status validateNoOperatorAfterTextGlob(const struct ParsedUrlPattern& pattern); + } // namespace Internal } // namespace UriTemplate } // namespace Extensions diff --git a/test/extensions/path/uri_template_lib/uri_template_internal_test.cc b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc index ea73d573e98b0..31061ed6a1bd2 100644 --- a/test/extensions/path/uri_template_lib/uri_template_internal_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc @@ -92,73 +92,73 @@ TEST(InternalParsing, IsValidVariableNameWorks) { EXPECT_FALSE(isValidVariableName("a!!!")); } -TEST(InternalParsing, ConsumeLiteralWorks) { +TEST(InternalParsing, ParseLiteralWorks) { std::string pattern = "abc/123"; - absl::StatusOr> result = consumeLiteral(pattern); + absl::StatusOr> result = parseLiteral(pattern); ASSERT_OK(result); EXPECT_EQ(result->parsed_value_, "abc"); - EXPECT_EQ(result->unconsumed_pattern_, "/123"); + EXPECT_EQ(result->unparsed_pattern_, "/123"); } -TEST(InternalParsing, ConsumeTextGlob) { +TEST(InternalParsing, ParseTextGlob) { std::string pattern = "***abc/123"; - absl::StatusOr> result = consumeOperator(pattern); + absl::StatusOr> result = parseOperator(pattern); ASSERT_OK(result); EXPECT_EQ(result->parsed_value_, Operator::TextGlob); - EXPECT_EQ(result->unconsumed_pattern_, "*abc/123"); + EXPECT_EQ(result->unparsed_pattern_, "*abc/123"); } -TEST(InternalParsing, ConsumeInvalidOperator) { +TEST(InternalParsing, ParsedInvalidOperator) { std::string pattern = "/"; - absl::StatusOr> result = consumeOperator(pattern); + absl::StatusOr> result = parseOperator(pattern); EXPECT_THAT(result, StatusIs(absl::StatusCode::kInvalidArgument)); } -TEST(InternalParsing, ConsumePathGlob) { +TEST(InternalParsing, ParsePathGlob) { std::string pattern = "*/123"; - absl::StatusOr> result = consumeOperator(pattern); + absl::StatusOr> result = parseOperator(pattern); ASSERT_OK(result); EXPECT_EQ(result->parsed_value_, Operator::PathGlob); - EXPECT_EQ(result->unconsumed_pattern_, "/123"); + EXPECT_EQ(result->unparsed_pattern_, "/123"); } -class ConsumeVariableSuccess : public testing::TestWithParam {}; +class ParseVariableSuccess : public testing::TestWithParam {}; -INSTANTIATE_TEST_SUITE_P(ConsumeVariableSuccessTestSuite, ConsumeVariableSuccess, +INSTANTIATE_TEST_SUITE_P(ParseVariableSuccessTestSuite, ParseVariableSuccess, testing::Values("{var=*}", "{Var}", "{v1=**}", "{v_1=*/abc/**}", "{v3=abc}", "{v=123/*/*}", "{var=abc/*/def}")); -TEST_P(ConsumeVariableSuccess, ConsumeVariableSuccessTest) { +TEST_P(ParseVariableSuccess, ParseVariableSuccessTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - absl::StatusOr> result = consumeVariable(pattern); + absl::StatusOr> result = parseVariable(pattern); ASSERT_OK(result); EXPECT_EQ(result->parsed_value_.debugString(), pattern); - EXPECT_TRUE(result->unconsumed_pattern_.empty()); + EXPECT_TRUE(result->unparsed_pattern_.empty()); } -class ConsumeVariableFailure : public testing::TestWithParam {}; +class ParseVariableFailure : public testing::TestWithParam {}; -INSTANTIATE_TEST_SUITE_P(ConsumeVariableFailureTestSuite, ConsumeVariableFailure, +INSTANTIATE_TEST_SUITE_P(ParseVariableFailureTestSuite, ParseVariableFailure, testing::Values("{var", "{=abc}", "{_var=*}", "{1v}", "{1v=abc}", "{var=***}", "{v-a-r}", "{var=*/abc?q=1}", "{var=abc/a*}", "{var=*def/abc}", "{var=}", "{var=abc=def}", "{rc=||||(A+yl/}", "/")); -TEST_P(ConsumeVariableFailure, ConsumeVariableFailureTest) { +TEST_P(ParseVariableFailure, ParseVariableFailureTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - EXPECT_THAT(consumeVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(parseVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); } class ParseURLPatternSyntaxSuccess : public testing::TestWithParam {}; @@ -298,7 +298,7 @@ TEST(InternalRegexGen, VariableRegexPattern) { } TEST(InternalRegexGen, VariableRegexDefaultMatch) { - absl::StatusOr> var = consumeVariable("{var}"); + absl::StatusOr> var = parseVariable("{var}"); ASSERT_OK(var); std::string capture; @@ -307,14 +307,14 @@ TEST(InternalRegexGen, VariableRegexDefaultMatch) { } TEST(InternalRegexGen, VariableRegexDefaultNotMatch) { - absl::StatusOr> var = consumeVariable("{var}"); + absl::StatusOr> var = parseVariable("{var}"); ASSERT_OK(var); EXPECT_FALSE(RE2::FullMatch("abc/def", toRegexPattern(var->parsed_value_))); } TEST(InternalRegexGen, VariableRegexSegmentsMatch) { - absl::StatusOr> var = consumeVariable("{var=abc/*/def}"); + absl::StatusOr> var = parseVariable("{var=abc/*/def}"); ASSERT_OK(var); std::string capture; @@ -323,7 +323,7 @@ TEST(InternalRegexGen, VariableRegexSegmentsMatch) { } TEST(InternalRegexGen, VariableRegexTextGlobMatch) { - absl::StatusOr> var = consumeVariable("{var=**/def}"); + absl::StatusOr> var = parseVariable("{var=**/def}"); ASSERT_OK(var); std::string capture; @@ -333,7 +333,7 @@ TEST(InternalRegexGen, VariableRegexTextGlobMatch) { TEST(InternalRegexGen, VariableRegexNamedCapture) { re2::StringPiece kPattern = "abc"; - absl::StatusOr> var = consumeVariable("{var=*}"); + absl::StatusOr> var = parseVariable("{var=*}"); ASSERT_OK(var); RE2 regex = RE2(toRegexPattern(var->parsed_value_)); diff --git a/test/extensions/path/uri_template_lib/uri_template_test.cc b/test/extensions/path/uri_template_lib/uri_template_test.cc index 1911c01d5d7da..4d87c43de50cc 100644 --- a/test/extensions/path/uri_template_lib/uri_template_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_test.cc @@ -342,10 +342,10 @@ TEST_P(URLPatternMatchAndRewrite, IsValidMatchPattern) { EXPECT_FALSE(isValidMatchPattern("/foo/bar/{goo}}").ok()); } -TEST_P(URLPatternMatchAndRewrite, IsValidPathTemplateRewritePattern) { - EXPECT_TRUE(isValidPathTemplateRewritePattern("/foo/bar/{goo}").ok()); - EXPECT_TRUE(isValidPathTemplateRewritePattern("/foo/bar/{goo}/{doo}").ok()); - EXPECT_TRUE(isValidPathTemplateRewritePattern("/{hoo}/bar/{goo}").ok()); +TEST_P(URLPatternMatchAndRewrite, IsValidRewritePattern) { + EXPECT_TRUE(isValidRewritePattern("/foo/bar/{goo}").ok()); + EXPECT_TRUE(isValidRewritePattern("/foo/bar/{goo}/{doo}").ok()); + EXPECT_TRUE(isValidRewritePattern("/{hoo}/bar/{goo}").ok()); EXPECT_FALSE(isValidMatchPattern("/foo//bar/{goo}").ok()); EXPECT_FALSE(isValidMatchPattern("/foo//bar/{goo}").ok()); From 849751e242c12249129d47fef13457d26f83c26d Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 19 Aug 2022 18:25:36 +0000 Subject: [PATCH 179/238] Formatting clang Signed-off-by: silverstar195 --- .../extensions/path/uri_template_lib/uri_template.cc | 9 +++++---- source/extensions/path/uri_template_lib/uri_template.h | 6 +++--- .../path/uri_template_lib/uri_template_internal.cc | 10 +++++----- .../path/uri_template_lib/uri_template_internal.h | 3 +-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/source/extensions/path/uri_template_lib/uri_template.cc b/source/extensions/path/uri_template_lib/uri_template.cc index ad0a1289d220d..2b437e8ed855e 100644 --- a/source/extensions/path/uri_template_lib/uri_template.cc +++ b/source/extensions/path/uri_template_lib/uri_template.cc @@ -38,7 +38,8 @@ absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url return Internal::toRegexPattern(*status); } -absl::StatusOr> parseRewritePattern(absl::string_view url_pattern) { +absl::StatusOr> +parseRewritePattern(absl::string_view url_pattern) { std::vector result; // The pattern should start with a '/' and thus the first segment should @@ -124,8 +125,7 @@ absl::Status isValidRewritePattern(absl::string_view path_template_rewrite) { return parseRewritePattern(path_template_rewrite).status(); } -absl::Status isValidSharedVariableSet(absl::string_view pattern, - absl::string_view capture_regex) { +absl::Status isValidSharedVariableSet(absl::string_view pattern, absl::string_view capture_regex) { absl::StatusOr status = convertURLPatternSyntaxToRegex(capture_regex).value(); if (!status.ok()) { return status.status(); @@ -158,7 +158,8 @@ absl::StatusOr rewriteURLTemplatePattern( if (segment.capture_index() < 1 || segment.capture_index() >= capture_num) { return absl::InvalidArgumentError("Invalid variable index"); } - absl::StrAppend(&rewritten_url, absl::string_view(captures[segment.capture_index()].as_string())); + absl::StrAppend(&rewritten_url, + absl::string_view(captures[segment.capture_index()].as_string())); } } diff --git a/source/extensions/path/uri_template_lib/uri_template.h b/source/extensions/path/uri_template_lib/uri_template.h index bc32aa107a19a..ff10470930c3f 100644 --- a/source/extensions/path/uri_template_lib/uri_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -26,7 +26,8 @@ absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url // Parses the specified pattern into a sequence of segments (which are // either literals or variable names). -absl::StatusOr> parseRewritePattern(absl::string_view url_pattern); +absl::StatusOr> +parseRewritePattern(absl::string_view url_pattern); // Returns the parsed Url rewrite pattern and processes variables. absl::StatusOr @@ -40,8 +41,7 @@ absl::Status isValidRewritePattern(absl::string_view path_template_rewrite); // For example: // Match: /foo/{bar}/{var} and Rewrite: /goo/{var} is valid. // Match: /foo/{bar} and Rewrite: /goo/{bar}/{var} is invalid. Match is missing {var}. -absl::Status isValidSharedVariableSet(absl::string_view pattern, - absl::string_view capture_regex); +absl::Status isValidSharedVariableSet(absl::string_view pattern, absl::string_view capture_regex); // Returns is the match_pattern is valid. // Validation attempts to parse pattern into literals and variables. diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.cc b/source/extensions/path/uri_template_lib/uri_template_internal.cc index 0e60318260177..1e00425b712e7 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.cc +++ b/source/extensions/path/uri_template_lib/uri_template_internal.cc @@ -87,8 +87,8 @@ std::string toString(const Variable val) { return absl::StrCat("{", val.name_, "}"); } - return absl::StrCat("{", val.name_, "=", - absl::StrJoin(val.match_, "/", ToStringFormatter()), "}"); + return absl::StrCat("{", val.name_, "=", absl::StrJoin(val.match_, "/", ToStringFormatter()), + "}"); } template std::string ToStringVisitor::operator()(const T& val) const { @@ -96,9 +96,9 @@ template std::string ToStringVisitor::operator()(const T& val) cons } template -absl::StatusOr alsoUpdatePattern( - absl::FunctionRef>(absl::string_view)> parse_func, - absl::string_view* patt) { +absl::StatusOr +alsoUpdatePattern(absl::FunctionRef>(absl::string_view)> parse_func, + absl::string_view* patt) { absl::StatusOr> status = parse_func(*patt); if (!status.ok()) { diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.h b/source/extensions/path/uri_template_lib/uri_template_internal.h index 38b470ef15a28..0a611028cbfdf 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.h +++ b/source/extensions/path/uri_template_lib/uri_template_internal.h @@ -87,8 +87,7 @@ bool isValidVariableName(absl::string_view variable); * portion of the pattern in |unparsed_pattern|. */ template struct ParsedResult { - ParsedResult(T val, absl::string_view pattern) - : parsed_value_(val), unparsed_pattern_(pattern) {} + ParsedResult(T val, absl::string_view pattern) : parsed_value_(val), unparsed_pattern_(pattern) {} T parsed_value_; absl::string_view unparsed_pattern_; From e83815bd4b43b8eb6daf967751e20b54e16cd82e Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 22 Aug 2022 14:31:45 +0000 Subject: [PATCH 180/238] Changed names Signed-off-by: silverstar195 --- .../uri_template_lib/uri_template_test.cc | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/test/extensions/path/uri_template_lib/uri_template_test.cc b/test/extensions/path/uri_template_lib/uri_template_test.cc index 4d87c43de50cc..25e4a5d66822a 100644 --- a/test/extensions/path/uri_template_lib/uri_template_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_test.cc @@ -103,47 +103,47 @@ INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, {"/static", R"EOF(segments: {literal: "/static"} )EOF"}, {"/{var1}", R"EOF(segments: - literal: "/" - - var_index: 1)EOF"}, + - capture_index: 1)EOF"}, {"/{var1}", R"EOF(segments: - literal: "/" - - var_index: 1)EOF"}, + - capture_index: 1)EOF"}, {"/{var1}/{var1}/{var1}", R"EOF(segments: - literal: "/" - - var_index: 1 + - capture_index: 1 - literal: "/" - - var_index: 1 + - capture_index: 1 - literal: "/" - - var_index: 1)EOF"}, + - capture_index: 1)EOF"}, {"/{var3}/{var1}/{var2}", R"EOF(segments - literal: "/" - - var_index: 3 + - capture_index: 3 - literal: "/" - - var_index: 1 + - capture_index: 1 - literal: "/" - - var_index: 2)EOF"}, + - capture_index: 2)EOF"}, {"/{var3}/abc/def/{var2}.suffix", R"EOF(segments: - literal: "/" - - var_index: 3 + - capture_index: 3 - literal: "/abc/def/" - - var_index: 2 + - capture_index: 2 - literal: ".suffix")EOF"}, {"/abc/{var1}/{var2}/def", R"EOF(segments - literal: "/abc/" - - var_index: 1 + - capture_index: 1 - literal: "/" - - var_index: 2 + - capture_index: 2 - literal: "/def")EOF"}, {"/{var1}{var2}", R"EOF(segments - literal: "/" - - var_index: 1 + - capture_index: 1 - ar_index: 2)EOF"}, {"/{var1}-{var2}/bucket-{var3}.suffix", R"EOF(segments - literal: "/" - - var_index: 1 + - capture_index: 1 - literal: "-" - - var_index: 2 + - capture_index: 2 - literal: "/bucket-" - - var_index: 3 + - capture_index: 3 - literal: ".suffix")EOF"}, }))); @@ -184,47 +184,47 @@ INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateS {{R"EOF(segments: { literal: "/static" })EOF", "/static"}, {R"EOF(segments: - literal: "/" - - var_index: 1)EOF", + - capture_index: 1)EOF", "/val1"}, {R"EOF(segments: - literal: "/" - - var_index: 1)EOF", + - capture_index: 1)EOF", "/val1"}, {R"EOF(segments: - literal: "/" - - var_index: 1 + - capture_index: 1 - literal: "/" - - var_index: 1 + - capture_index: 1 - literal: "/" - - var_index: 1)EOF", + - capture_index: 1)EOF", "/val1/val1/val1"}, {R"EOF(segments: - literal: "/" - - var_index: 3 + - capture_index: 3 - literal: "/" - - var_index: 1 + - capture_index: 1 - literal: "/" - - var_index: 2)EOF", + - capture_index: 2)EOF", "/val3/val1/val2"}, {R"EOF(segments: - literal: "/" - - var_index: 3 + - capture_index: 3 - literal: "/abc/def/" - - var_index: 2 + - capture_index: 2 - literal: ".suffix")EOF", "/val3/abc/def/val2.suffix"}, {R"EOF(segments: - literal: "/" - - var_index: 3 - - var_index: 2 + - capture_index: 3 + - capture_index: 2 - literal: "." - - var_index: 1)EOF", + - capture_index: 1)EOF", "/val3val2.val1"}, {R"EOF(segments: - literal: "/abc/" - - var_index: 1 + - capture_index: 1 - literal: "/" - - var_index: 5 + - capture_index: 5 - literal: "/def")EOF", "/abc/val1/val5/def"}}))); @@ -241,7 +241,7 @@ TEST(RewriteUrlTemplateFailure, BadRegex) { const std::string yaml = R"EOF( segments: - literal: "/" -- var_index: 1 +- capture_index: 1 )EOF"; Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); @@ -256,7 +256,7 @@ TEST(RewriteUrlTemplateFailure, RegexNoMatch) { const std::string yaml = R"EOF( segments: - literal: "/" -- var_index: 1 +- capture_index: 1 )EOF"; Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); @@ -271,7 +271,7 @@ TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { const std::string yaml = R"EOF( segments: - literal: "/" -- var_index: 0 +- capture_index: 0 )EOF"; Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); @@ -285,7 +285,7 @@ TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { const std::string yaml = R"EOF( segments: - literal: "/" -- var_index: 6 +- capture_index: 6 )EOF"; Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); From 595726fc41468c4db6655ce9fb555e5fc14c6b37 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 22 Aug 2022 15:06:30 +0000 Subject: [PATCH 181/238] Fix small bug Signed-off-by: silverstar195 --- source/extensions/path/uri_template_lib/uri_template.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/extensions/path/uri_template_lib/uri_template.cc b/source/extensions/path/uri_template_lib/uri_template.cc index 2b437e8ed855e..76d6fc048df90 100644 --- a/source/extensions/path/uri_template_lib/uri_template.cc +++ b/source/extensions/path/uri_template_lib/uri_template.cc @@ -154,7 +154,7 @@ absl::StatusOr rewriteURLTemplatePattern( rewrite_pattern.segments()) { if (segment.has_literal()) { absl::StrAppend(&rewritten_url, segment.literal()); - } else if (segment.capture_index()) { + } else if (segment.has_capture_index()) { if (segment.capture_index() < 1 || segment.capture_index() >= capture_num) { return absl::InvalidArgumentError("Invalid variable index"); } From f146a0ac36d4ed9e79ee19ff302852f346ca899a Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 23 Aug 2022 15:31:45 +0000 Subject: [PATCH 182/238] Changes for comments Signed-off-by: silverstar195 --- .../path/uri_template_lib/proto/BUILD | 2 +- ..._segments.proto => rewrite_segments.proto} | 2 +- .../path/uri_template_lib/uri_template.cc | 61 +++++----- .../path/uri_template_lib/uri_template.h | 77 ++++++++----- .../uri_template_lib/uri_template_internal.cc | 46 ++++---- .../uri_template_lib/uri_template_internal.h | 36 +++--- .../uri_template_internal_test.cc | 50 ++++----- .../uri_template_lib/uri_template_test.cc | 104 +++++++++--------- 8 files changed, 197 insertions(+), 181 deletions(-) rename source/extensions/path/uri_template_lib/proto/{uri_template_rewrite_segments.proto => rewrite_segments.proto} (96%) diff --git a/source/extensions/path/uri_template_lib/proto/BUILD b/source/extensions/path/uri_template_lib/proto/BUILD index 70c2114ccc1a5..d8ad5c3057d55 100644 --- a/source/extensions/path/uri_template_lib/proto/BUILD +++ b/source/extensions/path/uri_template_lib/proto/BUILD @@ -12,6 +12,6 @@ envoy_extension_package() envoy_proto_library( name = "uri_template_rewrite_segements", - srcs = ["uri_template_rewrite_segments.proto"], + srcs = ["rewrite_segments.proto"], deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], ) diff --git a/source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.proto b/source/extensions/path/uri_template_lib/proto/rewrite_segments.proto similarity index 96% rename from source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.proto rename to source/extensions/path/uri_template_lib/proto/rewrite_segments.proto index 4c4f8bd10789b..a11ea58a3a5a4 100644 --- a/source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.proto +++ b/source/extensions/path/uri_template_lib/proto/rewrite_segments.proto @@ -11,7 +11,7 @@ option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/url option (udpa.annotations.file_status).package_version_status = ACTIVE; // Holds the segments for rewriting urls base on uri pattern templates -message UriTemplateRewriteSegments { +message RewriteSegments { message RewriteSegment { oneof segment_type { // Represents a segment of the rewritten URL, including any path segments, diff --git a/source/extensions/path/uri_template_lib/uri_template.cc b/source/extensions/path/uri_template_lib/uri_template.cc index 76d6fc048df90..88a77e4fd7263 100644 --- a/source/extensions/path/uri_template_lib/uri_template.cc +++ b/source/extensions/path/uri_template_lib/uri_template.cc @@ -6,7 +6,7 @@ #include #include "source/common/http/path_utility.h" -#include "source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.pb.h" +#include "source/extensions/path/uri_template_lib/proto/rewrite_segments.pb.h" #include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "absl/status/statusor.h" @@ -18,44 +18,39 @@ namespace Envoy { namespace Extensions { namespace UriTemplate { -using Internal::ParsedUrlPattern; +using Internal::ParsedPathPattern; #ifndef SWIG // Silence warnings about missing initializers for members of LazyRE2. #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif -// Match pattern is used to match route. -// Example: /foo/{bar} matches /foo/cat - -// Rewrite pattern is used to rewrite the matched route. - -absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern) { - absl::StatusOr status = Internal::parseURLPatternSyntax(url_pattern); +absl::StatusOr convertPathPatternSyntaxToRegex(absl::string_view path_pattern) { + absl::StatusOr status = Internal::parsePathPatternSyntax(path_pattern); if (!status.ok()) { return status.status(); } return Internal::toRegexPattern(*status); } -absl::StatusOr> -parseRewritePattern(absl::string_view url_pattern) { - std::vector result; +absl::StatusOr> +parseRewritePattern(absl::string_view path_pattern) { + std::vector result; // The pattern should start with a '/' and thus the first segment should // always be a literal. - if (url_pattern.empty() || url_pattern[0] != '/') { + if (path_pattern.empty() || path_pattern[0] != '/') { return absl::InvalidArgumentError("Invalid rewrite variable placement"); } // Don't allow contiguous '/' patterns. static const LazyRE2 invalid_regex = {"^.*//.*$"}; - if (RE2::FullMatch(Internal::toStringPiece(url_pattern), *invalid_regex)) { + if (RE2::FullMatch(Internal::toStringPiece(path_pattern), *invalid_regex)) { return absl::InvalidArgumentError("Invalid rewrite literal"); } - while (!url_pattern.empty()) { - std::vector segments1 = absl::StrSplit(url_pattern, absl::MaxSplits('{', 1)); + while (!path_pattern.empty()) { + std::vector segments1 = absl::StrSplit(path_pattern, absl::MaxSplits('{', 1)); if (!segments1[0].empty()) { if (!Internal::isValidRewriteLiteral(segments1[0])) { return absl::InvalidArgumentError("Invalid rewrite literal pattern"); @@ -73,7 +68,7 @@ parseRewritePattern(absl::string_view url_pattern) { if (segments2.size() < 2) { return absl::InvalidArgumentError("Unmatched variable bracket"); } - url_pattern = segments2[1]; + path_pattern = segments2[1]; if (!Internal::isValidVariableName(segments2[0])) { return absl::InvalidArgumentError("Invalid variable name"); @@ -83,19 +78,19 @@ parseRewritePattern(absl::string_view url_pattern) { return result; } -absl::StatusOr +absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { - envoy::extensions::uri_template::UriTemplateRewriteSegments parsed_pattern; + envoy::extensions::uri_template::RewriteSegments parsed_pattern; RE2 regex = RE2(Internal::toStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); } - absl::StatusOr> status = parseRewritePattern(pattern); + absl::StatusOr> status = parseRewritePattern(pattern); if (!status.ok()) { return status.status(); } - std::vector processed_pattern = *std::move(status); + std::vector processed_pattern = *std::move(status); const std::map& capture_index_map = regex.NamedCapturingGroups(); @@ -118,7 +113,7 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) } absl::Status isValidMatchPattern(absl::string_view path_template_match) { - return convertURLPatternSyntaxToRegex(path_template_match).status(); + return convertPathPatternSyntaxToRegex(path_template_match).status(); } absl::Status isValidRewritePattern(absl::string_view path_template_rewrite) { @@ -126,16 +121,16 @@ absl::Status isValidRewritePattern(absl::string_view path_template_rewrite) { } absl::Status isValidSharedVariableSet(absl::string_view pattern, absl::string_view capture_regex) { - absl::StatusOr status = convertURLPatternSyntaxToRegex(capture_regex).value(); + absl::StatusOr status = convertPathPatternSyntaxToRegex(capture_regex).value(); if (!status.ok()) { return status.status(); } return parseRewritePattern(pattern, *std::move(status)).status(); } -absl::StatusOr rewriteURLTemplatePattern( - absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::uri_template::UriTemplateRewriteSegments& rewrite_pattern) { +absl::StatusOr rewritePathTemplatePattern( + absl::string_view path, absl::string_view capture_regex, + const envoy::extensions::uri_template::RewriteSegments& rewrite_pattern) { RE2 regex = RE2(Internal::toStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); @@ -144,26 +139,26 @@ absl::StatusOr rewriteURLTemplatePattern( // First capture is the whole matched regex pattern. int capture_num = regex.NumberOfCapturingGroups() + 1; std::vector captures(capture_num); - if (!regex.Match(Internal::toStringPiece(url), /*startpos=*/0, - /*endpos=*/url.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { + if (!regex.Match(Internal::toStringPiece(path), /*startpos=*/0, + /*endpos=*/path.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { return absl::InvalidArgumentError("Pattern does not match"); } - std::string rewritten_url; - for (const envoy::extensions::uri_template::UriTemplateRewriteSegments::RewriteSegment& segment : + std::string rewritten_path; + for (const envoy::extensions::uri_template::RewriteSegments::RewriteSegment& segment : rewrite_pattern.segments()) { if (segment.has_literal()) { - absl::StrAppend(&rewritten_url, segment.literal()); + absl::StrAppend(&rewritten_path, segment.literal()); } else if (segment.has_capture_index()) { if (segment.capture_index() < 1 || segment.capture_index() >= capture_num) { return absl::InvalidArgumentError("Invalid variable index"); } - absl::StrAppend(&rewritten_url, + absl::StrAppend(&rewritten_path, absl::string_view(captures[segment.capture_index()].as_string())); } } - return rewritten_url; + return rewritten_path; } } // namespace UriTemplate diff --git a/source/extensions/path/uri_template_lib/uri_template.h b/source/extensions/path/uri_template_lib/uri_template.h index ff10470930c3f..89a44434f445b 100644 --- a/source/extensions/path/uri_template_lib/uri_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -2,7 +2,7 @@ #include -#include "source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.pb.h" +#include "source/extensions/path/uri_template_lib/proto/rewrite_segments.pb.h" #include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "absl/status/statusor.h" @@ -12,50 +12,71 @@ namespace Envoy { namespace Extensions { namespace UriTemplate { +// This library provides functionality to rewrite paths based on templates (https://www.rfc-editor.org/rfc/rfc6570.html). +// Paths are matches against "match patterns" which capture matched tokens. For example: +// The match pattern "/foo/{bar}" will match "/foo/cat" and will capture "cat" in the variable "bar". +// +// A matched path can then be rewritten with a rewrite pattern. +// For example: rewrite pattern "/pat/hat/{bar}" would use the captured variable "bar" from above +// to rewrite "/foo/cat" into "/pat/hat/cat" + enum class RewriteStringKind { Variable, Literal }; -struct RewritePatternSegment { - RewritePatternSegment(absl::string_view value, RewriteStringKind kind) +struct ParsedSegment { + ParsedSegment(absl::string_view value, RewriteStringKind kind) : value_(value), kind_(kind) {} absl::string_view value_; RewriteStringKind kind_; }; -// Returns the safe regex that Envoy understands that is equivalent to the given pattern. -absl::StatusOr convertURLPatternSyntaxToRegex(absl::string_view url_pattern); +/** + * Returns the safe regex that Envoy understands that is equivalent to the given pattern. + */ +absl::StatusOr convertPathPatternSyntaxToRegex(absl::string_view path_pattern); -// Parses the specified pattern into a sequence of segments (which are -// either literals or variable names). -absl::StatusOr> -parseRewritePattern(absl::string_view url_pattern); +/** Parses the specified pattern into a sequence of segments (which are +/ * either literals or variable names). + **/ +absl::StatusOr> +parseRewritePattern(absl::string_view path_pattern); -// Returns the parsed Url rewrite pattern and processes variables. -absl::StatusOr +/** + * Returns the parsed path rewrite pattern and processes variables. + */ +absl::StatusOr parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); -// Returns if provided rewrite pattern is valid +/** + * Returns true if provided rewrite pattern is valid. + */ absl::Status isValidRewritePattern(absl::string_view path_template_rewrite); -// Returns if path_template_rewrite and capture_regex have valid variables. -// Every variable in rewrite MUST be present in match. -// For example: -// Match: /foo/{bar}/{var} and Rewrite: /goo/{var} is valid. -// Match: /foo/{bar} and Rewrite: /goo/{bar}/{var} is invalid. Match is missing {var}. +/** + * Returns true if path_template_rewrite and capture_regex have valid variables. + * Every variable in rewrite MUST be present in match. + * For example: + * Match: /foo/{bar}/{var} and Rewrite: /goo/{var} is valid. + * Match: /foo/{bar} and Rewrite: /goo/{bar}/{var} is invalid. Match is missing {var}. + */ absl::Status isValidSharedVariableSet(absl::string_view pattern, absl::string_view capture_regex); -// Returns is the match_pattern is valid. -// Validation attempts to parse pattern into literals and variables. +/** + * Returns true if the match_pattern is valid. + * Validation attempts to parse pattern into literals and variables. + */ absl::Status isValidMatchPattern(absl::string_view match_pattern); -// Concatenates literals and extracts variable values to form the final rewritten url. -// For example: -// rewrite_pattern: [var_index=2, literal="cat"] -// url: "/bar/var" -// capture_regex: "(1)/(2)" -// Rewrite would result in rewrite of "/var/cat". -absl::StatusOr rewriteURLTemplatePattern( - absl::string_view url, absl::string_view capture_regex, - const envoy::extensions::uri_template::UriTemplateRewriteSegments& rewrite_pattern); +/** + * Concatenates literals and extracts variable values to form the final rewritten path. + * For example: + * rewrite_pattern: [var_index=2, literal="cat"] + * path: "/bar/var" + * capture_regex: "(1)/(2)" + * Rewrite would result in rewrite of "/var/cat". + */ +absl::StatusOr rewritePathTemplatePattern( + absl::string_view path, absl::string_view capture_regex, + const envoy::extensions::uri_template::RewriteSegments& rewrite_pattern); } // namespace UriTemplate } // namespace Extensions diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.cc b/source/extensions/path/uri_template_lib/uri_template_internal.cc index 1e00425b712e7..0ce295e8efb62 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.cc +++ b/source/extensions/path/uri_template_lib/uri_template_internal.cc @@ -33,7 +33,7 @@ namespace { #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif -constexpr unsigned long kPatternMatchingMaxVariablesPerUrl = 5; +constexpr unsigned long kPatternMatchingMaxVariablesPerPath = 5; constexpr unsigned long kPatternMatchingMaxVariableNameLen = 16; constexpr unsigned long kPatternMatchingMinVariableNameLen = 1; @@ -114,7 +114,7 @@ alsoUpdatePattern(absl::FunctionRef>(absl::string std::string Variable::debugString() const { return toString(*this); } -std::string ParsedUrlPattern::debugString() const { +std::string ParsedPathPattern::debugString() const { return absl::StrCat("/", absl::StrJoin(parsed_segments_, "/", ToStringFormatter()), suffix_.value_or("")); } @@ -214,14 +214,14 @@ absl::StatusOr> parseVariable(absl::string_view pattern) } absl::StatusOr> -gatherCaptureNames(const struct ParsedUrlPattern& pattern) { +gatherCaptureNames(const struct ParsedPathPattern& pattern) { absl::flat_hash_set captured_variables; for (const ParsedSegment& segment : pattern.parsed_segments_) { if (!absl::holds_alternative(segment)) { continue; } - if (captured_variables.size() >= kPatternMatchingMaxVariablesPerUrl) { + if (captured_variables.size() >= kPatternMatchingMaxVariablesPerPath) { return absl::InvalidArgumentError("Exceeded variable count limit"); } absl::string_view name = absl::get(segment).name_; @@ -239,7 +239,7 @@ gatherCaptureNames(const struct ParsedUrlPattern& pattern) { return captured_variables; } -absl::Status validateNoOperatorAfterTextGlob(const struct ParsedUrlPattern& pattern) { +absl::Status validateNoOperatorAfterTextGlob(const struct ParsedPathPattern& pattern) { bool seen_text_glob = false; for (const ParsedSegment& segment : pattern.parsed_segments_) { if (absl::holds_alternative(segment)) { @@ -270,34 +270,34 @@ absl::Status validateNoOperatorAfterTextGlob(const struct ParsedUrlPattern& patt return absl::OkStatus(); } -absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern) { - struct ParsedUrlPattern parsed_pattern; +absl::StatusOr parsePathPatternSyntax(absl::string_view path) { + struct ParsedPathPattern parsed_pattern; static const LazyRE2 printable_regex = {"^/[[:graph:]]*$"}; - if (!RE2::FullMatch(toStringPiece(url_pattern), *printable_regex)) { + if (!RE2::FullMatch(toStringPiece(path), *printable_regex)) { return absl::InvalidArgumentError("Invalid pattern"); } // Parse the leading '/' - url_pattern = url_pattern.substr(1); + path = path.substr(1); // Do the initial lexical parsing. - while (!url_pattern.empty()) { + while (!path.empty()) { ParsedSegment segment; - if (url_pattern[0] == '*') { - absl::StatusOr status = alsoUpdatePattern(parseOperator, &url_pattern); + if (path[0] == '*') { + absl::StatusOr status = alsoUpdatePattern(parseOperator, &path); if (!status.ok()) { return status.status(); } segment = *std::move(status); - } else if (url_pattern[0] == '{') { - absl::StatusOr status = alsoUpdatePattern(parseVariable, &url_pattern); + } else if (path[0] == '{') { + absl::StatusOr status = alsoUpdatePattern(parseVariable, &path); if (!status.ok()) { return status.status(); } segment = *std::move(status); } else { - absl::StatusOr status = alsoUpdatePattern(parseLiteral, &url_pattern); + absl::StatusOr status = alsoUpdatePattern(parseLiteral, &path); if (!status.ok()) { return status.status(); } @@ -306,23 +306,23 @@ absl::StatusOr parseURLPatternSyntax(absl::string_view url_pat parsed_pattern.parsed_segments_.push_back(segment); // Deal with trailing '/' or suffix. - if (!url_pattern.empty()) { - if (url_pattern == "/") { + if (!path.empty()) { + if (path == "/") { // Single trailing '/' at the end, mark this with empty literal. parsed_pattern.parsed_segments_.emplace_back(""); break; - } else if (url_pattern[0] == '/') { + } else if (path[0] == '/') { // Have '/' followed by more text, parse the '/'. - url_pattern = url_pattern.substr(1); + path = path.substr(1); } else { // Not followed by '/', treat as suffix. - absl::StatusOr status = alsoUpdatePattern(parseLiteral, &url_pattern); + absl::StatusOr status = alsoUpdatePattern(parseLiteral, &path); if (!status.ok()) { return status.status(); } parsed_pattern.suffix_ = *std::move(status); - if (!url_pattern.empty()) { - // Suffix didn't parse whole remaining pattern ('/' in url_pattern). + if (!path.empty()) { + // Suffix didn't parse whole remaining pattern ('/' in path). return absl::InvalidArgumentError("Prefix match not supported."); } break; @@ -369,7 +369,7 @@ std::string toRegexPattern(const Variable& pattern) { ")"); } -std::string toRegexPattern(const struct ParsedUrlPattern& pattern) { +std::string toRegexPattern(const struct ParsedPathPattern& pattern) { return absl::StrCat("/", absl::StrJoin(pattern.parsed_segments_, "/", ToRegexPatternFormatter()), toRegexPattern(pattern.suffix_.value_or(""))); } diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.h b/source/extensions/path/uri_template_lib/uri_template_internal.h index 0a611028cbfdf..4ec35a9eb6eba 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.h +++ b/source/extensions/path/uri_template_lib/uri_template_internal.h @@ -20,7 +20,7 @@ namespace UriTemplate { namespace Internal { /** - * String to be concatenated in rewritten url. + * String to be concatenated in rewritten path. */ using Literal = absl::string_view; @@ -47,15 +47,15 @@ struct Variable { using ParsedSegment = absl::variant; /** - * Represents the parsed url including literals and variables. + * Represents the parsed path including literals and variables. */ -struct ParsedUrlPattern { +struct ParsedPathPattern { std::vector parsed_segments_; - // Suffix holds end of url that matched a wildcard + // Suffix holds end of path that matched a wildcard. // For example: // Pattern: /foo/bar/** - // Url: /foo/bar/some/more/stuff + // Path: /foo/bar/some/more/stuff // Suffix: some/more/stuff absl::optional suffix_; absl::flat_hash_set captured_variables_; @@ -64,19 +64,19 @@ struct ParsedUrlPattern { }; /** - * Returns true if `literal` is valid for pattern match + * Returns true if `literal` is valid for pattern match. * (does not contain wildcards or curly brackets). */ bool isValidLiteral(absl::string_view literal); /** - * Returns true if `literal` is valid for pattern rewrite + * Returns true if `literal` is valid for pattern rewrite. * (does not contain wildcards or curly brackets). */ bool isValidRewriteLiteral(absl::string_view literal); /** - * Check if variable name is valid. + * Return true if variable name is valid. */ bool isValidVariableName(absl::string_view variable); @@ -99,7 +99,7 @@ template struct ParsedResult { absl::StatusOr> parseLiteral(absl::string_view pattern); /** - * Converts input pattern to ParsedResult. + * Parses a operator in the front of `pattern` and returns the operator and remaining pattern. */ absl::StatusOr> parseOperator(absl::string_view pattern); @@ -109,30 +109,30 @@ absl::StatusOr> parseOperator(absl::string_view pattern); absl::StatusOr> parseVariable(absl::string_view pattern); /** - * Converts input url to ParsedUrlPattern. - * ParsedUrlPattern hold prefix, string literals, and variables for rewrite. + * Converts input path to ParsedPathPattern. + * ParsedPathPattern hold prefix, string literals, and variables for rewrite. */ -absl::StatusOr parseURLPatternSyntax(absl::string_view url_pattern); +absl::StatusOr parsePathPatternSyntax(absl::string_view path); /** - * Convert Literal to a regex string representation + * Convert Literal to a re2-compatible regular expression. */ std::string toRegexPattern(Literal pattern); /** - * Converts Operator to a regex string representation + * Converts Operator to a regex string representation. */ std::string toRegexPattern(Operator pattern); /** - * Converts Variable to a regex string representation + * Converts Variable to a regex string representation. */ std::string toRegexPattern(const Variable& pattern); /** - * Converts ParsedUrlPattern to a regex string representation. + * Converts ParsedPathPattern to a regex string representation. */ -std::string toRegexPattern(const struct ParsedUrlPattern& pattern); +std::string toRegexPattern(const struct ParsedPathPattern& pattern); /** * Converts string_view to be used in re2::string_piece. @@ -142,7 +142,7 @@ inline re2::StringPiece toStringPiece(absl::string_view text) { return {text.dat /** * Checks end of pattern to ensure glob operator is last. */ -absl::Status validateNoOperatorAfterTextGlob(const struct ParsedUrlPattern& pattern); +absl::Status validateNoOperatorAfterTextGlob(const struct ParsedPathPattern& pattern); } // namespace Internal } // namespace UriTemplate diff --git a/test/extensions/path/uri_template_lib/uri_template_internal_test.cc b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc index 31061ed6a1bd2..af90964dc9b46 100644 --- a/test/extensions/path/uri_template_lib/uri_template_internal_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc @@ -28,8 +28,8 @@ namespace { using ::Envoy::StatusHelpers::StatusIs; -TEST(InternalParsing, ParsedUrlDebugString) { - ParsedUrlPattern patt1 = { +TEST(InternalParsing, ParsedPathDebugString) { + ParsedPathPattern patt1 = { { "abc", "def", @@ -41,7 +41,7 @@ TEST(InternalParsing, ParsedUrlDebugString) { }; EXPECT_EQ(patt1.debugString(), "/abc/def/*/{var=*/ghi/**}.test"); - ParsedUrlPattern patt2 = {{ + ParsedPathPattern patt2 = {{ Variable("var", {}), }, "", @@ -161,10 +161,10 @@ TEST_P(ParseVariableFailure, ParseVariableFailureTest) { EXPECT_THAT(parseVariable(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); } -class ParseURLPatternSyntaxSuccess : public testing::TestWithParam {}; +class ParsePathPatternSyntaxSuccess : public testing::TestWithParam {}; INSTANTIATE_TEST_SUITE_P( - ParseURLPatternSyntaxSuccessTestSuite, ParseURLPatternSyntaxSuccess, + ParsePathPatternSyntaxSuccessTestSuite, ParsePathPatternSyntaxSuccess, testing::Values("/**.m3u8", "/**.mpd", "/*_suf", "/{path=**}.m3u8", "/{foo}/**.ts", "/media/*.m4s", "/media/{contentId=*}/**", "/media/*", "/api/*/*/**", "/api/*/v1/**", "/api/*/v1/*", "/{version=api/*}/*", "/api/*/*/", @@ -177,19 +177,19 @@ INSTANTIATE_TEST_SUITE_P( "/api/{version=*}/{url=**}", "/api/{VERSION}/{version}/{verSION}", "/api/1234/abcd", "/media/abcd/%10%20%30/{v1=*/%10%20}_suffix", "/")); -TEST_P(ParseURLPatternSyntaxSuccess, ParseURLPatternSyntaxSuccessTest) { +TEST_P(ParsePathPatternSyntaxSuccess, ParsePathPatternSyntaxSuccessTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - absl::StatusOr parsed_patt = parseURLPatternSyntax(pattern); + absl::StatusOr parsed_patt = parsePathPatternSyntax(pattern); ASSERT_OK(parsed_patt); EXPECT_EQ(parsed_patt->debugString(), pattern); } -class ParseURLPatternSyntaxFailure : public testing::TestWithParam {}; +class ParsePathPatternSyntaxFailure : public testing::TestWithParam {}; INSTANTIATE_TEST_SUITE_P( - ParseURLPatternSyntaxFailureTestSuite, ParseURLPatternSyntaxFailure, + ParsePathPatternSyntaxFailureTestSuite, ParsePathPatternSyntaxFailure, testing::Values("/api/v*/1234", "/api/{version=v*}/1234", "/api/v{versionNum=*}/1234", "/api/{version=*beta}/1234", "/media/eff456/ll-sd-out.{ext}", "/media/eff456/ll-sd-out.{ext=*}", "/media/eff456/ll-sd-out.**", @@ -201,11 +201,11 @@ INSTANTIATE_TEST_SUITE_P( "/{var1}/{var2}/{var3}/{var4}/{var5}/{var6}", "/{=*}", "/{var12345678901234=*}")); -TEST_P(ParseURLPatternSyntaxFailure, ParseURLPatternSyntaxFailureTest) { +TEST_P(ParsePathPatternSyntaxFailure, ParsePathPatternSyntaxFailureTest) { std::string pattern = GetParam(); SCOPED_TRACE(pattern); - EXPECT_THAT(parseURLPatternSyntax(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); + EXPECT_THAT(parsePathPatternSyntax(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); } TEST(InternalRegexGen, LiteralEscapes) { @@ -350,9 +350,9 @@ TEST(InternalRegexGen, VariableRegexNamedCapture) { EXPECT_EQ(kPattern, captures.at(regex.NamedCapturingGroups().at("var"))); } -TEST(InternalRegexGen, ParsedURLPatternToRegex) { - absl::StatusOr pattern = - parseURLPatternSyntax("/abc/*/{var1}/def/{var2=*/ghi/**}.jkl"); +TEST(InternalRegexGen, ParsedPathPatternToRegex) { + absl::StatusOr pattern = + parsePathPatternSyntax("/abc/*/{var1}/def/{var2=*/ghi/**}.jkl"); ASSERT_OK(pattern); std::string var1_capture; @@ -364,19 +364,19 @@ TEST(InternalRegexGen, ParsedURLPatternToRegex) { } struct GenPatternTestCase { - GenPatternTestCase(std::string request_path, std::string url_pattern, + GenPatternTestCase(std::string request_path, std::string path_pattern, std::vector> capture_pairs) - : path(request_path), pattern(url_pattern), captures(capture_pairs) {} - std::string path; - std::string pattern; - std::vector> captures; + : path_(request_path), path_pattern_(path_pattern), captures_(capture_pairs) {} + std::string path_; + std::string path_pattern_; + std::vector> captures_; }; class GenPatternRegexWithMatch : public testing::TestWithParam { protected: - const std::string& requestPath() const { return GetParam().path; } - const std::string& urlPattern() const { return GetParam().pattern; } - std::vector> const varValues() { return GetParam().captures; } + const std::string& requestPath() const { return GetParam().path_; } + const std::string& path_pattern() const { return GetParam().path_pattern_; } + std::vector> const varValues() { return GetParam().captures_; } }; INSTANTIATE_TEST_SUITE_P( @@ -419,7 +419,7 @@ INSTANTIATE_TEST_SUITE_P( {{"VERSION", "v1"}, {"version", "v2"}, {"verSION", "v3"}}))); TEST_P(GenPatternRegexWithMatch, WithCapture) { - absl::StatusOr pattern = parseURLPatternSyntax(urlPattern()); + absl::StatusOr pattern = parsePathPatternSyntax(path_pattern()); ASSERT_OK(pattern); RE2 regex = RE2(toRegexPattern(pattern.value())); @@ -445,7 +445,7 @@ class GenPatternRegexWithoutMatch : public testing::TestWithParam> { protected: const std::string& requestPath() const { return std::get<0>(GetParam()); } - const std::string& urlPattern() const { return std::get<1>(GetParam()); } + const std::string& path_pattern() const { return std::get<1>(GetParam()); } }; INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWithoutMatch, @@ -458,7 +458,7 @@ INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWi {"/api/*/1234/", "/api/*/1234/"}}))); TEST_P(GenPatternRegexWithoutMatch, WithCapture) { - absl::StatusOr pattern = parseURLPatternSyntax(urlPattern()); + absl::StatusOr pattern = parsePathPatternSyntax(path_pattern()); ASSERT_OK(pattern); RE2 regex = RE2(toRegexPattern(pattern.value())); diff --git a/test/extensions/path/uri_template_lib/uri_template_test.cc b/test/extensions/path/uri_template_lib/uri_template_test.cc index 25e4a5d66822a..4030f5e28ba5c 100644 --- a/test/extensions/path/uri_template_lib/uri_template_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_test.cc @@ -28,32 +28,32 @@ static constexpr absl::string_view kCaptureRegex = "/(?P[a-zA-Z0-9-._~%!$& "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"; -static constexpr absl::string_view kMatchUrl = "/val1/val2/val3/val4/val5"; +static constexpr absl::string_view kMatchPath = "/val1/val2/val3/val4/val5"; -TEST(ConvertURLPattern, ValidPattern) { - EXPECT_THAT(convertURLPatternSyntaxToRegex("/abc"), IsOkAndHolds("/abc")); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/**.mpd"), +TEST(ConvertPathPattern, ValidPattern) { + EXPECT_THAT(convertPathPatternSyntaxToRegex("/abc"), IsOkAndHolds("/abc")); + EXPECT_THAT(convertPathPatternSyntaxToRegex("/**.mpd"), IsOkAndHolds("/[a-zA-Z0-9-._~%!$&'()+,;:@/]*\\.mpd")); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/*/{resource=*}/{method=**}"), + EXPECT_THAT(convertPathPatternSyntaxToRegex("/api/*/{resource=*}/{method=**}"), IsOkAndHolds("/api/[a-zA-Z0-9-._~%!$&'()+,;:@]+/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@/]*)")); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/{VERSION}/{version}/{verSION}"), + EXPECT_THAT(convertPathPatternSyntaxToRegex("/api/{VERSION}/{version}/{verSION}"), IsOkAndHolds("/api/(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)")); } -TEST(ConvertURLPattern, InvalidPattern) { - EXPECT_THAT(convertURLPatternSyntaxToRegex("/api/v*/1234"), +TEST(ConvertPathPattern, InvalidPattern) { + EXPECT_THAT(convertPathPatternSyntaxToRegex("/api/v*/1234"), StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/media/**/*/**"), + EXPECT_THAT(convertPathPatternSyntaxToRegex("/media/**/*/**"), StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/\001\002\003\004\005\006\007"), + EXPECT_THAT(convertPathPatternSyntaxToRegex("/\001\002\003\004\005\006\007"), StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/{var12345678901234=*}"), + EXPECT_THAT(convertPathPatternSyntaxToRegex("/{var12345678901234=*}"), StatusIs(absl::StatusCode::kInvalidArgument)); - EXPECT_THAT(convertURLPatternSyntaxToRegex("/{var12345678901234=*"), + EXPECT_THAT(convertPathPatternSyntaxToRegex("/{var12345678901234=*"), StatusIs(absl::StatusCode::kInvalidArgument)); } @@ -87,8 +87,8 @@ TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { class ParseRewriteSuccess : public testing::TestWithParam> { protected: const std::string& rewritePattern() const { return std::get<0>(GetParam()); } - envoy::extensions::uri_template::UriTemplateRewriteSegments expectedProto() const { - envoy::extensions::uri_template::UriTemplateRewriteSegments expected_proto; + envoy::extensions::uri_template::RewriteSegments expectedProto() const { + envoy::extensions::uri_template::RewriteSegments expected_proto; Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); return expected_proto; } @@ -148,7 +148,7 @@ INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, }))); TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { - absl::StatusOr rewrite = + absl::StatusOr rewrite = parseRewritePattern(rewritePattern(), kCaptureRegex); ASSERT_OK(rewrite); // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); @@ -168,18 +168,18 @@ TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { StatusIs(absl::StatusCode::kInvalidArgument)); } -class RewriteUrlTemplateSuccess +class RewritePathTemplateSuccess : public testing::TestWithParam> { protected: - envoy::extensions::uri_template::UriTemplateRewriteSegments rewriteProto() const { - envoy::extensions::uri_template::UriTemplateRewriteSegments proto; + envoy::extensions::uri_template::RewriteSegments rewriteProto() const { + envoy::extensions::uri_template::RewriteSegments proto; Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); return proto; } - const std::string& expectedRewrittenUrl() const { return std::get<1>(GetParam()); } + const std::string& expectedRewrittenPath() const { return std::get<1>(GetParam()); } }; -INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateSuccess, +INSTANTIATE_TEST_SUITE_P(RewritePathTemplateSuccessTestSuite, RewritePathTemplateSuccess, testing::ValuesIn(std::vector>( {{R"EOF(segments: { literal: "/static" })EOF", "/static"}, {R"EOF(segments: @@ -228,15 +228,15 @@ INSTANTIATE_TEST_SUITE_P(RewriteUrlTemplateSuccessTestSuite, RewriteUrlTemplateS - literal: "/def")EOF", "/abc/val1/val5/def"}}))); -TEST_P(RewriteUrlTemplateSuccess, RewriteUrlTemplateSuccessTest) { - absl::StatusOr rewritten_url = - rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewriteProto()); - ASSERT_OK(rewritten_url); - EXPECT_EQ(rewritten_url.value(), expectedRewrittenUrl()); +TEST_P(RewritePathTemplateSuccess, RewritePathTemplateSuccessTest) { + absl::StatusOr rewritten_path = + rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewriteProto()); + ASSERT_OK(rewritten_path); + EXPECT_EQ(rewritten_path.value(), expectedRewrittenPath()); } -TEST(RewriteUrlTemplateFailure, BadRegex) { - envoy::extensions::uri_template::UriTemplateRewriteSegments rewrite_proto; +TEST(RewritePathTemplateFailure, BadRegex) { + envoy::extensions::uri_template::RewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -246,12 +246,12 @@ TEST(RewriteUrlTemplateFailure, BadRegex) { Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, "+/bad_regex", rewrite_proto), + EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, "+/bad_regex", rewrite_proto), StatusIs(absl::StatusCode::kInternal)); } -TEST(RewriteUrlTemplateFailure, RegexNoMatch) { - envoy::extensions::uri_template::UriTemplateRewriteSegments rewrite_proto; +TEST(RewritePathTemplateFailure, RegexNoMatch) { + envoy::extensions::uri_template::RewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -261,12 +261,12 @@ TEST(RewriteUrlTemplateFailure, RegexNoMatch) { Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, "/no_match_regex", rewrite_proto), + EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, "/no_match_regex", rewrite_proto), StatusIs(absl::StatusCode::kInvalidArgument)); } -TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { - envoy::extensions::uri_template::UriTemplateRewriteSegments rewrite_proto; +TEST(RewritePathTemplateFailure, RegexCaptureIndexZero) { + envoy::extensions::uri_template::RewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -275,12 +275,12 @@ TEST(RewriteUrlTemplateFailure, RegexCaptureIndexZero) { )EOF"; Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewrite_proto), StatusIs(absl::StatusCode::kInvalidArgument)); } -TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { - envoy::extensions::uri_template::UriTemplateRewriteSegments rewrite_proto; +TEST(RewritePathTemplateFailure, RegexCaptureIndexAboveMaxCapture) { + envoy::extensions::uri_template::RewriteSegments rewrite_proto; const std::string yaml = R"EOF( segments: @@ -290,22 +290,22 @@ TEST(RewriteUrlTemplateFailure, RegexCaptureIndexAboveMaxCapture) { Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); - EXPECT_THAT(rewriteURLTemplatePattern(kMatchUrl, kCaptureRegex, rewrite_proto), + EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewrite_proto), StatusIs(absl::StatusCode::kInvalidArgument)); } -class URLPatternMatchAndRewrite +class PathPatternMatchAndRewrite : public testing::TestWithParam< std::tuple> { protected: - const std::string& urlPattern() const { return std::get<0>(GetParam()); } + const std::string& pattern() const { return std::get<0>(GetParam()); } const std::string& rewritePattern() const { return std::get<1>(GetParam()); } - const std::string& matchUrl() const { return std::get<2>(GetParam()); } - const std::string& expectedRewrittenUrl() const { return std::get<3>(GetParam()); } + const std::string& matchPath() const { return std::get<2>(GetParam()); } + const std::string& expectedRewrittenPath() const { return std::get<3>(GetParam()); } }; INSTANTIATE_TEST_SUITE_P( - URLPatternMatchAndRewriteTestSuite, URLPatternMatchAndRewrite, + PathPatternMatchAndRewriteTestSuite, PathPatternMatchAndRewrite, testing::ValuesIn(std::vector>( {{"/api/users/{id}/{path=**}", "/users/{id}/{path}", "/api/users/21334/profile.json", "/users/21334/profile.json"}, @@ -317,22 +317,22 @@ INSTANTIATE_TEST_SUITE_P( {"/region/{region}/bucket/{name}/{method=**}", "/{region}{name}/{method}", "/region/eu/bucket/prod-storage/object.pdf", "/euprod-storage/object.pdf"}}))); -TEST_P(URLPatternMatchAndRewrite, URLPatternMatchAndRewriteTest) { - absl::StatusOr regex = convertURLPatternSyntaxToRegex(urlPattern()); +TEST_P(PathPatternMatchAndRewrite, PathPatternMatchAndRewriteTest) { + absl::StatusOr regex = convertPathPatternSyntaxToRegex(pattern()); ASSERT_OK(regex); - absl::StatusOr rewrite_proto = + absl::StatusOr rewrite_proto = parseRewritePattern(rewritePattern(), regex.value()); ASSERT_OK(rewrite_proto); - absl::StatusOr rewritten_url = - rewriteURLTemplatePattern(matchUrl(), regex.value(), rewrite_proto.value()); - ASSERT_OK(rewritten_url); + absl::StatusOr rewritten_path = + rewritePathTemplatePattern(matchPath(), regex.value(), rewrite_proto.value()); + ASSERT_OK(rewritten_path); - EXPECT_EQ(rewritten_url.value(), expectedRewrittenUrl()); + EXPECT_EQ(rewritten_path.value(), expectedRewrittenPath()); } -TEST_P(URLPatternMatchAndRewrite, IsValidMatchPattern) { +TEST_P(PathPatternMatchAndRewrite, IsValidMatchPattern) { EXPECT_TRUE(isValidMatchPattern("/foo/bar/{goo}").ok()); EXPECT_TRUE(isValidMatchPattern("/foo/bar/{goo}/{doo}").ok()); EXPECT_TRUE(isValidMatchPattern("/{hoo}/bar/{goo}").ok()); @@ -342,7 +342,7 @@ TEST_P(URLPatternMatchAndRewrite, IsValidMatchPattern) { EXPECT_FALSE(isValidMatchPattern("/foo/bar/{goo}}").ok()); } -TEST_P(URLPatternMatchAndRewrite, IsValidRewritePattern) { +TEST_P(PathPatternMatchAndRewrite, IsValidRewritePattern) { EXPECT_TRUE(isValidRewritePattern("/foo/bar/{goo}").ok()); EXPECT_TRUE(isValidRewritePattern("/foo/bar/{goo}/{doo}").ok()); EXPECT_TRUE(isValidRewritePattern("/{hoo}/bar/{goo}").ok()); @@ -352,7 +352,7 @@ TEST_P(URLPatternMatchAndRewrite, IsValidRewritePattern) { EXPECT_FALSE(isValidMatchPattern("/foo/bar/{goo}}").ok()); } -TEST_P(URLPatternMatchAndRewrite, IsValidSharedVariableSet) { +TEST_P(PathPatternMatchAndRewrite, IsValidSharedVariableSet) { EXPECT_TRUE(isValidSharedVariableSet("/foo/bar/{goo}", "/foo/bar/{goo}").ok()); EXPECT_TRUE(isValidSharedVariableSet("/foo/bar/{goo}/{doo}", "/foo/bar/{doo}/{goo}").ok()); EXPECT_TRUE(isValidSharedVariableSet("/bar/{goo}", "/bar/{goo}").ok()); From 1f7417e35ce9f6297438f0436972b715c0473edb Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 23 Aug 2022 15:36:46 +0000 Subject: [PATCH 183/238] Format Signed-off-by: silverstar195 --- .../path/uri_template_lib/uri_template.cc | 6 +++--- .../path/uri_template_lib/uri_template.h | 19 +++++++++---------- .../uri_template_internal_test.cc | 12 +++++++----- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/source/extensions/path/uri_template_lib/uri_template.cc b/source/extensions/path/uri_template_lib/uri_template.cc index 88a77e4fd7263..77c87ba461181 100644 --- a/source/extensions/path/uri_template_lib/uri_template.cc +++ b/source/extensions/path/uri_template_lib/uri_template.cc @@ -33,8 +33,7 @@ absl::StatusOr convertPathPatternSyntaxToRegex(absl::string_view pa return Internal::toRegexPattern(*status); } -absl::StatusOr> -parseRewritePattern(absl::string_view path_pattern) { +absl::StatusOr> parseRewritePattern(absl::string_view path_pattern) { std::vector result; // The pattern should start with a '/' and thus the first segment should @@ -50,7 +49,8 @@ parseRewritePattern(absl::string_view path_pattern) { } while (!path_pattern.empty()) { - std::vector segments1 = absl::StrSplit(path_pattern, absl::MaxSplits('{', 1)); + std::vector segments1 = + absl::StrSplit(path_pattern, absl::MaxSplits('{', 1)); if (!segments1[0].empty()) { if (!Internal::isValidRewriteLiteral(segments1[0])) { return absl::InvalidArgumentError("Invalid rewrite literal pattern"); diff --git a/source/extensions/path/uri_template_lib/uri_template.h b/source/extensions/path/uri_template_lib/uri_template.h index 89a44434f445b..0edd4c5b4051f 100644 --- a/source/extensions/path/uri_template_lib/uri_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -12,9 +12,10 @@ namespace Envoy { namespace Extensions { namespace UriTemplate { -// This library provides functionality to rewrite paths based on templates (https://www.rfc-editor.org/rfc/rfc6570.html). -// Paths are matches against "match patterns" which capture matched tokens. For example: -// The match pattern "/foo/{bar}" will match "/foo/cat" and will capture "cat" in the variable "bar". +// This library provides functionality to rewrite paths based on templates +// (https://www.rfc-editor.org/rfc/rfc6570.html). Paths are matches against "match patterns" which +// capture matched tokens. For example: The match pattern "/foo/{bar}" will match "/foo/cat" and +// will capture "cat" in the variable "bar". // // A matched path can then be rewritten with a rewrite pattern. // For example: rewrite pattern "/pat/hat/{bar}" would use the captured variable "bar" from above @@ -23,8 +24,7 @@ namespace UriTemplate { enum class RewriteStringKind { Variable, Literal }; struct ParsedSegment { - ParsedSegment(absl::string_view value, RewriteStringKind kind) - : value_(value), kind_(kind) {} + ParsedSegment(absl::string_view value, RewriteStringKind kind) : value_(value), kind_(kind) {} absl::string_view value_; RewriteStringKind kind_; }; @@ -37,8 +37,7 @@ absl::StatusOr convertPathPatternSyntaxToRegex(absl::string_view pa /** Parses the specified pattern into a sequence of segments (which are / * either literals or variable names). **/ -absl::StatusOr> -parseRewritePattern(absl::string_view path_pattern); +absl::StatusOr> parseRewritePattern(absl::string_view path_pattern); /** * Returns the parsed path rewrite pattern and processes variables. @@ -74,9 +73,9 @@ absl::Status isValidMatchPattern(absl::string_view match_pattern); * capture_regex: "(1)/(2)" * Rewrite would result in rewrite of "/var/cat". */ -absl::StatusOr rewritePathTemplatePattern( - absl::string_view path, absl::string_view capture_regex, - const envoy::extensions::uri_template::RewriteSegments& rewrite_pattern); +absl::StatusOr +rewritePathTemplatePattern(absl::string_view path, absl::string_view capture_regex, + const envoy::extensions::uri_template::RewriteSegments& rewrite_pattern); } // namespace UriTemplate } // namespace Extensions diff --git a/test/extensions/path/uri_template_lib/uri_template_internal_test.cc b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc index af90964dc9b46..bb7e891eb023d 100644 --- a/test/extensions/path/uri_template_lib/uri_template_internal_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc @@ -42,10 +42,10 @@ TEST(InternalParsing, ParsedPathDebugString) { EXPECT_EQ(patt1.debugString(), "/abc/def/*/{var=*/ghi/**}.test"); ParsedPathPattern patt2 = {{ - Variable("var", {}), - }, - "", - {}}; + Variable("var", {}), + }, + "", + {}}; EXPECT_EQ(patt2.debugString(), "/{var}"); } @@ -376,7 +376,9 @@ class GenPatternRegexWithMatch : public testing::TestWithParam> const varValues() { return GetParam().captures_; } + std::vector> const varValues() { + return GetParam().captures_; + } }; INSTANTIATE_TEST_SUITE_P( From b7d8910582590bcca64e6e590a282e18df5aa1d9 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 23 Aug 2022 19:52:14 +0000 Subject: [PATCH 184/238] Remove optional string Signed-off-by: silverstar195 --- .../extensions/path/uri_template_lib/uri_template_internal.cc | 4 ++-- .../extensions/path/uri_template_lib/uri_template_internal.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.cc b/source/extensions/path/uri_template_lib/uri_template_internal.cc index 0ce295e8efb62..913b661e4fec9 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.cc +++ b/source/extensions/path/uri_template_lib/uri_template_internal.cc @@ -116,7 +116,7 @@ std::string Variable::debugString() const { return toString(*this); } std::string ParsedPathPattern::debugString() const { return absl::StrCat("/", absl::StrJoin(parsed_segments_, "/", ToStringFormatter()), - suffix_.value_or("")); + suffix_); } bool isValidLiteral(absl::string_view literal) { @@ -371,7 +371,7 @@ std::string toRegexPattern(const Variable& pattern) { std::string toRegexPattern(const struct ParsedPathPattern& pattern) { return absl::StrCat("/", absl::StrJoin(pattern.parsed_segments_, "/", ToRegexPatternFormatter()), - toRegexPattern(pattern.suffix_.value_or(""))); + toRegexPattern(pattern.suffix_)); } } // namespace Internal diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.h b/source/extensions/path/uri_template_lib/uri_template_internal.h index 4ec35a9eb6eba..4b8561bf7b896 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.h +++ b/source/extensions/path/uri_template_lib/uri_template_internal.h @@ -57,7 +57,7 @@ struct ParsedPathPattern { // Pattern: /foo/bar/** // Path: /foo/bar/some/more/stuff // Suffix: some/more/stuff - absl::optional suffix_; + absl::string_view suffix_; absl::flat_hash_set captured_variables_; std::string debugString() const; From c386a92469b67dafa19df237468687d7d734f733 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 23 Aug 2022 20:38:48 +0000 Subject: [PATCH 185/238] Format Signed-off-by: silverstar195 --- .../extensions/path/uri_template_lib/uri_template_internal.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.cc b/source/extensions/path/uri_template_lib/uri_template_internal.cc index 913b661e4fec9..985be5973fa92 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.cc +++ b/source/extensions/path/uri_template_lib/uri_template_internal.cc @@ -115,8 +115,7 @@ alsoUpdatePattern(absl::FunctionRef>(absl::string std::string Variable::debugString() const { return toString(*this); } std::string ParsedPathPattern::debugString() const { - return absl::StrCat("/", absl::StrJoin(parsed_segments_, "/", ToStringFormatter()), - suffix_); + return absl::StrCat("/", absl::StrJoin(parsed_segments_, "/", ToStringFormatter()), suffix_); } bool isValidLiteral(absl::string_view literal) { From 35159826bd954221ae82b6645360c95aa92939da Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 24 Aug 2022 14:10:55 +0000 Subject: [PATCH 186/238] Small nits Signed-off-by: silverstar195 --- .../extensions/path/uri_template_lib/uri_template.h | 5 +++-- .../path/uri_template_lib/uri_template_internal.h | 11 +++++------ .../uri_template_lib/uri_template_internal_test.cc | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/source/extensions/path/uri_template_lib/uri_template.h b/source/extensions/path/uri_template_lib/uri_template.h index 0edd4c5b4051f..807bd3a567f5b 100644 --- a/source/extensions/path/uri_template_lib/uri_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -34,8 +34,9 @@ struct ParsedSegment { */ absl::StatusOr convertPathPatternSyntaxToRegex(absl::string_view path_pattern); -/** Parses the specified pattern into a sequence of segments (which are -/ * either literals or variable names). +/** + * Parses the specified pattern into a sequence of segments (which are + * either literals or variable names). **/ absl::StatusOr> parseRewritePattern(absl::string_view path_pattern); diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.h b/source/extensions/path/uri_template_lib/uri_template_internal.h index 4b8561bf7b896..5212bf1ede150 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.h +++ b/source/extensions/path/uri_template_lib/uri_template_internal.h @@ -33,15 +33,14 @@ enum class Operator { PathGlob, TextGlob }; * Represents a pattern variable. Variables are included in both path match and rewrite paths. */ struct Variable { - absl::string_view name_; - - // replacement value for the rewrite - std::vector> match_; - Variable(absl::string_view name, std::vector> match) : name_(name), match_(match) {} std::string debugString() const; + + absl::string_view name_; + // replacement value for the rewrite. + std::vector> match_; }; using ParsedSegment = absl::variant; @@ -65,7 +64,7 @@ struct ParsedPathPattern { /** * Returns true if `literal` is valid for pattern match. - * (does not contain wildcards or curly brackets). + * (does not contain wildcards, forwards slashes, or curly brackets). */ bool isValidLiteral(absl::string_view literal); diff --git a/test/extensions/path/uri_template_lib/uri_template_internal_test.cc b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc index bb7e891eb023d..b7d92e3dd4540 100644 --- a/test/extensions/path/uri_template_lib/uri_template_internal_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_internal_test.cc @@ -375,7 +375,7 @@ struct GenPatternTestCase { class GenPatternRegexWithMatch : public testing::TestWithParam { protected: const std::string& requestPath() const { return GetParam().path_; } - const std::string& path_pattern() const { return GetParam().path_pattern_; } + const std::string& pathPattern() const { return GetParam().path_pattern_; } std::vector> const varValues() { return GetParam().captures_; } @@ -421,7 +421,7 @@ INSTANTIATE_TEST_SUITE_P( {{"VERSION", "v1"}, {"version", "v2"}, {"verSION", "v3"}}))); TEST_P(GenPatternRegexWithMatch, WithCapture) { - absl::StatusOr pattern = parsePathPatternSyntax(path_pattern()); + absl::StatusOr pattern = parsePathPatternSyntax(pathPattern()); ASSERT_OK(pattern); RE2 regex = RE2(toRegexPattern(pattern.value())); @@ -447,7 +447,7 @@ class GenPatternRegexWithoutMatch : public testing::TestWithParam> { protected: const std::string& requestPath() const { return std::get<0>(GetParam()); } - const std::string& path_pattern() const { return std::get<1>(GetParam()); } + const std::string& pathPattern() const { return std::get<1>(GetParam()); } }; INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWithoutMatch, @@ -460,7 +460,7 @@ INSTANTIATE_TEST_SUITE_P(GenPatternRegexWithoutMatchTestSuite, GenPatternRegexWi {"/api/*/1234/", "/api/*/1234/"}}))); TEST_P(GenPatternRegexWithoutMatch, WithCapture) { - absl::StatusOr pattern = parsePathPatternSyntax(path_pattern()); + absl::StatusOr pattern = parsePathPatternSyntax(pathPattern()); ASSERT_OK(pattern); RE2 regex = RE2(toRegexPattern(pattern.value())); From 686e016d63ae2cd76f9c98b3080966c54e9fedbc Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 24 Aug 2022 18:28:32 +0000 Subject: [PATCH 187/238] Changed comment Signed-off-by: silverstar195 --- .../extensions/path/uri_template_lib/uri_template_internal.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.h b/source/extensions/path/uri_template_lib/uri_template_internal.h index 5212bf1ede150..94334f2d86f65 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.h +++ b/source/extensions/path/uri_template_lib/uri_template_internal.h @@ -39,7 +39,8 @@ struct Variable { std::string debugString() const; absl::string_view name_; - // replacement value for the rewrite. + + // The pattern which this variable matches. std::vector> match_; }; From 86fd526db07fd27cc44fe090917e964a880c2ac3 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 29 Aug 2022 18:14:13 +0000 Subject: [PATCH 188/238] Merge in branch Signed-off-by: silverstar195 --- .../pattern_template/pattern_template_match.cc | 2 +- .../path/rewrite/pattern_template/config.h | 2 +- .../pattern_template_rewrite.cc | 18 +++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.cc b/source/extensions/path/match/pattern_template/pattern_template_match.cc index 75a1fd7a6c452..76cf86697e3f5 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.cc +++ b/source/extensions/path/match/pattern_template/pattern_template_match.cc @@ -18,7 +18,7 @@ namespace UriTemplate { namespace Match { bool PatternTemplateMatcher::match(absl::string_view path) const { - RE2 matching_pattern_regex = RE2(convertURLPatternSyntaxToRegex(path_template_).value()); + RE2 matching_pattern_regex = RE2(convertPathPatternSyntaxToRegex(path_template_).value()); return RE2::FullMatch(Internal::toStringPiece(Http::PathUtil::removeQueryAndFragment(path)), matching_pattern_regex); } diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index add91665c62cb..a49123e4b3b23 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -20,7 +20,7 @@ class PatternTemplateRewriterFactory : public Router::PathRewriterFactory { v3::PatternTemplateRewriteConfig&>( rewrite_config, ProtobufMessage::getStrictValidationVisitor()); - if (!UriTemplate::isValidPathTemplateRewritePattern(path_rewrite_config.path_template_rewrite()) + if (!UriTemplate::isValidRewritePattern(path_rewrite_config.path_template_rewrite()) .ok()) { return absl::InvalidArgumentError( fmt::format("path_rewrite_policy.path_template_rewrite {} is invalid", diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index a65b607209c65..084f1f205daa3 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -7,7 +7,7 @@ #include "source/common/http/path_utility.h" #include "source/extensions/path/match/pattern_template/pattern_template_match.h" -#include "source/extensions/path/uri_template_lib/proto/uri_template_rewrite_segments.pb.h" +#include "source/extensions/path/uri_template_lib/proto/rewrite_segments.pb.h" #include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "absl/status/statusor.h" @@ -48,20 +48,20 @@ PatternTemplateRewriter::isCompatibleMatchPolicy(Router::PathMatcherSharedPtr pa absl::StatusOr PatternTemplateRewriter::rewriteUrl(absl::string_view pattern, absl::string_view matched_path) const { - absl::StatusOr regex_pattern = convertURLPatternSyntaxToRegex(matched_path); + absl::StatusOr regex_pattern = convertPathPatternSyntaxToRegex(matched_path); if (!regex_pattern.ok()) { return absl::InvalidArgumentError("Unable to parse url pattern regex"); } std::string regex_pattern_str = *std::move(regex_pattern); - absl::StatusOr rewrite_pattern = + absl::StatusOr rewrite_pattern = parseRewritePattern(url_rewrite_pattern_, regex_pattern_str); if (!rewrite_pattern.ok()) { return absl::InvalidArgumentError("Unable to parse url rewrite pattern"); } - const envoy::extensions::uri_template::UriTemplateRewriteSegments& rewrite_pattern_proto = + const envoy::extensions::uri_template::RewriteSegments& rewrite_pattern_proto = *std::move(rewrite_pattern); RE2 regex = RE2(Internal::toStringPiece(regex_pattern_str)); if (!regex.ok()) { @@ -77,19 +77,19 @@ PatternTemplateRewriter::rewriteUrl(absl::string_view pattern, } std::string new_path; - for (const envoy::extensions::uri_template::UriTemplateRewriteSegments::RewriteSegment& segment : + for (const envoy::extensions::uri_template::RewriteSegments::RewriteSegment& segment : rewrite_pattern_proto.segments()) { if (segment.has_literal()) { absl::StrAppend(&new_path, segment.literal()); - } else if (segment.has_var_index()) { - if (segment.var_index() < 1 || segment.var_index() >= capture_num) { + } else if (segment.has_capture_index()) { + if (segment.capture_index() < 1 || segment.capture_index() >= capture_num) { return absl::InvalidArgumentError("Invalid variable index"); } - absl::StrAppend(&new_path, absl::string_view(captures[segment.var_index()].as_string())); + absl::StrAppend(&new_path, absl::string_view(captures[segment.capture_index()].as_string())); } } - return absl::StatusOr{new_path}; + return {new_path}; } } // namespace Rewrite From d9e4d9164c0296960b5c9337edfc82194e6ec41d Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 29 Aug 2022 18:21:30 +0000 Subject: [PATCH 189/238] Revert check_format to main Signed-off-by: silverstar195 --- tools/code_format/check_format.py | 532 +++++++++++++----------------- 1 file changed, 229 insertions(+), 303 deletions(-) diff --git a/tools/code_format/check_format.py b/tools/code_format/check_format.py index 9163b2d858be3..b0c49ffa0611b 100755 --- a/tools/code_format/check_format.py +++ b/tools/code_format/check_format.py @@ -1,12 +1,10 @@ #!/usr/bin/env python3 import argparse -import common import functools import logging import multiprocessing import os -import os.path import pathlib import re import subprocess @@ -14,233 +12,140 @@ import sys import traceback import shutil -import paths from functools import cached_property +from typing import Callable, Dict, List, Pattern, Tuple, Union + +# The way this script is currently used (ie no bazel) it relies on system deps. +# As `pyyaml` is present in `envoy-build-ubuntu` it should be safe to use here. +import yaml -EXCLUDED_PREFIXES = ( - "./.", "./generated/", "./thirdparty/", "./build", "./bazel-", "./tools/dev/src", - "./source/extensions/extensions_build_config.bzl", "./contrib/contrib_build_config.bzl", - "./bazel/toolchains/configs/", "./tools/testdata/check_format/", "./tools/pyformat/", - "./third_party/", "./test/extensions/filters/http/wasm/test_data", - "./test/extensions/filters/network/wasm/test_data", - "./test/extensions/stats_sinks/wasm/test_data", "./test/extensions/bootstrap/wasm/test_data", - "./test/extensions/common/wasm/test_data", "./test/extensions/access_loggers/wasm/test_data", - "./source/extensions/common/wasm/ext", "./examples/wasm-cc", "./bazel/external/http_parser/") -SUFFIXES = ("BUILD", "WORKSPACE", ".bzl", ".cc", ".h", ".java", ".m", ".mm", ".proto") -PROTO_SUFFIX = (".proto") - -# Files in these paths can make reference to protobuf stuff directly -GOOGLE_PROTOBUF_ALLOWLIST = ( - "ci/prebuilt", "source/common/protobuf", "api/test", "test/extensions/bootstrap/wasm/test_data") -REPOSITORIES_BZL = "bazel/repositories.bzl" - -# Files matching these exact names can reference real-world time. These include the class -# definitions for real-world time, the construction of them in main(), and perf annotation. -# For now it includes the validation server but that really should be injected too. -REAL_TIME_ALLOWLIST = ( - "./source/common/common/utility.h", "./source/extensions/common/aws/utility.cc", - "./source/common/event/real_time_system.cc", "./source/common/event/real_time_system.h", - "./source/exe/main_common.cc", "./source/exe/main_common.h", - "./source/server/config_validation/server.cc", "./source/common/common/perf_annotation.h", - "./test/common/common/log_macros_test.cc", "./test/common/protobuf/utility_test.cc", - "./test/test_common/simulated_time_system.cc", "./test/test_common/simulated_time_system.h", - "./test/test_common/test_time.cc", "./test/test_common/test_time.h", - "./test/test_common/utility.cc", "./test/test_common/utility.h", - "./test/integration/integration.h", "./test/tools/wee8_compile/wee8_compile.cc") - -# Tests in these paths may make use of the Registry::RegisterFactory constructor or the -# REGISTER_FACTORY macro. Other locations should use the InjectFactory helper class to -# perform temporary registrations. -REGISTER_FACTORY_TEST_ALLOWLIST = ( - "./test/common/config/registry_test.cc", "./test/integration/clusters/", - "./test/integration/filters/", "./test/integration/load_balancers/", - "./test/extensions/transport_sockets/tls/integration/") - -# Files in these paths can use MessageLite::SerializeAsString -SERIALIZE_AS_STRING_ALLOWLIST = ( - "./source/common/protobuf/utility.cc", - "./source/extensions/filters/http/grpc_json_transcoder/json_transcoder_filter.cc", - "./test/common/protobuf/utility_test.cc", - "./test/common/grpc/codec_test.cc", - "./test/common/grpc/codec_fuzz_test.cc", - "./test/extensions/filters/common/expr/context_test.cc", - "./test/extensions/filters/http/common/fuzz/uber_filter.h", - "./test/extensions/bootstrap/wasm/test_data/speed_cpp.cc", - "./test/tools/router_check/router_check.cc", -) - -# Files in these paths can use Protobuf::util::JsonStringToMessage -JSON_STRING_TO_MESSAGE_ALLOWLIST = ( - "./source/common/protobuf/utility.cc", - "./test/extensions/bootstrap/wasm/test_data/speed_cpp.cc") - -# Histogram names which are allowed to be suffixed with the unit symbol, all of the pre-existing -# ones were grandfathered as part of PR #8484 for backwards compatibility. -HISTOGRAM_WITH_SI_SUFFIX_ALLOWLIST = ( - "cx_rtt_us", "cx_rtt_variance_us", "downstream_cx_length_ms", "downstream_cx_length_ms", - "initialization_time_ms", "loop_duration_us", "poll_delay_us", "request_time_ms", - "upstream_cx_connect_ms", "upstream_cx_length_ms") - -# Files in these paths can use std::regex -STD_REGEX_ALLOWLIST = ( - "./source/common/common/utility.cc", "./source/common/common/regex.h", - "./source/common/common/regex.cc", "./source/common/stats/tag_extractor_impl.h", - "./source/common/stats/tag_extractor_impl.cc", - "./source/common/formatter/substitution_formatter.cc", - "./contrib/squash/filters/http/source/squash_filter.h", - "./contrib/squash/filters/http/source/squash_filter.cc", "./source/server/admin/utils.h", - "./source/server/admin/utils.cc", "./source/server/admin/stats_params.h", - "./source/server/admin/stats_request.cc", "./source/server/admin/prometheus_stats.h", - "./source/server/admin/prometheus_stats.cc", "./tools/clang_tools/api_booster/main.cc", - "./tools/clang_tools/api_booster/proto_cxx_utils.cc", "./source/common/version/version.cc") - -# Only one C++ file should instantiate grpc_init -GRPC_INIT_ALLOWLIST = ("./source/common/grpc/google_grpc_context.cc") - -# Files that should not raise an error for using memcpy -MEMCPY_WHITELIST = ( - "./source/common/common/mem_block_builder.h", "./source/common/common/safe_memcpy.h") - -# These files should not throw exceptions. Add HTTP/1 when exceptions removed. -EXCEPTION_DENYLIST = ( - "./source/common/http/http2/codec_impl.h", "./source/common/http/http2/codec_impl.cc") - -# Files that are allowed to use try without main thread assertion. -RAW_TRY_ALLOWLIST = ( - "./source/common/common/regex.cc", "./source/common/common/thread.h", - "./source/common/network/utility.cc") - -# These are entire files that are allowed to use std::string_view vs. individual exclusions. Right -# now this is just WASM which makes use of std::string_view heavily so we need to convert to -# absl::string_view internally. Everywhere else should be using absl::string_view for additional -# safety. -STD_STRING_VIEW_ALLOWLIST = ( - "./source/extensions/common/wasm/context.h", - "./source/extensions/common/wasm/context.cc", - "./source/extensions/common/wasm/foreign.cc", - "./source/extensions/common/wasm/wasm.h", - "./source/extensions/common/wasm/wasm.cc", - "./source/extensions/common/wasm/wasm_vm.h", - "./source/extensions/common/wasm/wasm_vm.cc", - "./test/extensions/bootstrap/wasm/wasm_speed_test.cc", - "./test/extensions/bootstrap/wasm/wasm_test.cc", - "./test/extensions/common/wasm/wasm_test.cc", - "./test/extensions/stats_sinks/wasm/wasm_stat_sink_test.cc", - "./test/test_common/wasm_base.h", -) - -# Header files that can throw exceptions. These should be limited; the only -# valid situation identified so far is template functions used for config -# processing. -EXCEPTION_ALLOWLIST = ("./source/common/config/utility.h") - -# We want all URL references to exist in repository_locations.bzl files and have -# metadata that conforms to the schema in ./api/bazel/external_deps.bzl. Below -# we have some exceptions for either infrastructure files or places we fall -# short today (Rust). -# -# Please DO NOT extend this allow list without consulting -# @envoyproxy/dependency-shepherds. -BUILD_URLS_ALLOWLIST = ( - "./bazel/repository_locations.bzl", - "./bazel/external/cargo/crates.bzl", - "./api/bazel/repository_locations.bzl", - "./api/bazel/envoy_http_archive.bzl", -) - -CLANG_FORMAT_PATH = os.getenv("CLANG_FORMAT", "clang-format-14") -BUILDIFIER_PATH = paths.get_buildifier() -BUILDOZER_PATH = paths.get_buildozer() -ENVOY_BUILD_FIXER_PATH = os.path.join( - os.path.dirname(os.path.abspath(sys.argv[0])), "envoy_build_fixer.py") -HEADER_ORDER_PATH = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "header_order.py") -SUBDIR_SET = set(common.include_dir_order()) -INCLUDE_ANGLE = "#include <" -INCLUDE_ANGLE_LEN = len(INCLUDE_ANGLE) -X_ENVOY_USED_DIRECTLY_REGEX = re.compile(r'.*\"x-envoy-.*\".*') -DESIGNATED_INITIALIZER_REGEX = re.compile(r"\{\s*\.\w+\s*\=") -MANGLED_PROTOBUF_NAME_REGEX = re.compile(r"envoy::[a-z0-9_:]+::[A-Z][a-z]\w*_\w*_[A-Z]{2}") -HISTOGRAM_SI_SUFFIX_REGEX = re.compile(r"(?<=HISTOGRAM\()[a-zA-Z0-9_]+_(b|kb|mb|ns|us|ms|s)(?=,)") -TEST_NAME_STARTING_LOWER_CASE_REGEX = re.compile(r"TEST(_.\(.*,\s|\()[a-z].*\)\s\{") -EXTENSIONS_CODEOWNERS_REGEX = re.compile(r'.*(extensions[^@]*\s+)(@.*)') -CONTRIB_CODEOWNERS_REGEX = re.compile(r'(/contrib/[^@]*\s+)(@.*)') -COMMENT_REGEX = re.compile(r"//|\*") -DURATION_VALUE_REGEX = re.compile(r'\b[Dd]uration\(([0-9.]+)') -PROTO_VALIDATION_STRING = re.compile(r'\bmin_bytes\b') -OLD_MOCK_METHOD_REGEX = re.compile("MOCK_METHOD\d") -# C++17 feature, lacks sufficient support across various libraries / compilers. -FOR_EACH_N_REGEX = re.compile("for_each_n\(") -# Check for punctuation in a terminal ref clause, e.g. -# :ref:`panic mode. ` -DOT_MULTI_SPACE_REGEX = re.compile("\\. +") -FLAG_REGEX = re.compile("RUNTIME_GUARD\((.*)\);") - -# yapf: disable -PROTOBUF_TYPE_ERRORS = { - # Well-known types should be referenced from the ProtobufWkt namespace. - "Protobuf::Any": "ProtobufWkt::Any", - "Protobuf::Empty": "ProtobufWkt::Empty", - "Protobuf::ListValue": "ProtobufWkt::ListValue", - "Protobuf::NULL_VALUE": "ProtobufWkt::NULL_VALUE", - "Protobuf::StringValue": "ProtobufWkt::StringValue", - "Protobuf::Struct": "ProtobufWkt::Struct", - "Protobuf::Value": "ProtobufWkt::Value", - - # Other common mis-namespacing of protobuf types. - "ProtobufWkt::Map": "Protobuf::Map", - "ProtobufWkt::MapPair": "Protobuf::MapPair", - "ProtobufUtil::MessageDifferencer": "Protobuf::util::MessageDifferencer" -} -# yapf: enable - -LIBCXX_REPLACEMENTS = { - "absl::make_unique<": "std::make_unique<", -} - -CODE_CONVENTION_REPLACEMENTS = { - # We can't just remove Times(1) everywhere, since .Times(1).WillRepeatedly - # is a legitimate pattern. See - # https://github.com/google/googletest/blob/master/googlemock/docs/for_dummies.md#cardinalities-how-many-times-will-it-be-called - ".Times(1);": ";", - # These may miss some cases, due to line breaks, but should reduce the - # Times(1) noise. - ".Times(1).WillOnce": ".WillOnce", - ".Times(1).WillRepeatedly": ".WillOnce", - "Stats::ScopePtr": "Stats::ScopeSharedPtr", -} - -UNSORTED_FLAGS = { - "envoy.reloadable_features.activate_timers_next_event_loop", - "envoy.reloadable_features.grpc_json_transcoder_adhere_to_buffer_limits", -} +import paths logger = logging.getLogger(__name__) -LINE_NUMBER_RE = re.compile(r"^(\d+)[a|c|d]?\d*(?:,\d+[a|c|d]?\d*)?$") -VIRTUAL_INCLUDE_HEADERS_RE = re.compile(r"#include.*/_virtual_includes/") -OWNER_RE = re.compile('@\S+') -PROJECT_OWNERS_RE = re.compile(r'.*github.com.(.*)\)\)') + +class FormatConfig: + """Provides a format config object based on parsed YAML config.""" + + def __init__(self, path: str) -> None: + self.path = path + + def __getitem__(self, k): + return self.config.__getitem__(k) + + @cached_property + def buildifier_path(self) -> str: + """Path to the buildifer binary.""" + return paths.get_buildifier() + + @cached_property + def buildozer_path(self) -> str: + """Path to the buildozer binary.""" + return paths.get_buildozer() + + @cached_property + def clang_format_path(self) -> str: + """Path to the clang-format binary.""" + return os.getenv("CLANG_FORMAT", "clang-format-14") + + @cached_property + def config(self) -> Dict: + """Parsed YAML config.""" + # TODO(phlax): Ensure the YAML is valid/well-formed.""" + return yaml.safe_load(pathlib.Path(self.path).read_text()) + + @cached_property + def dir_order(self) -> List[str]: + """Expected order of includes in code.""" + return self["dir_order"] + + @cached_property + def paths(self) -> Dict[str, Union[Tuple[str, ...], Dict[str, Tuple[str, ...]]]]: + """Mapping of named paths.""" + paths = self._normalize("paths", cb=lambda paths: tuple(f"./{p}" for p in paths)) + paths["build_fixer_py"] = self._build_fixer_path + paths["header_order_py"] = self._header_order_path + return paths + + @cached_property + def re(self) -> Dict[str, Pattern[str]]: + """Mapping of named regular expressions.""" + return {k: re.compile(v) for k, v in self["re"].items()} + + @cached_property + def re_multiline(self) -> Dict[str, Pattern[str]]: + """Mapping of named multi-line regular expressions.""" + return {k: re.compile(v, re.MULTILINE) for k, v in self["re_multiline"].items()} + + @cached_property + def replacements(self) -> Dict[str, str]: + """Mapping of subsitutions to be replaced in code.""" + return self["replacements"] + + @cached_property + def suffixes(self) -> Dict[str, Union[Tuple[str, ...], Dict[str, Tuple[str, ...]]]]: + """Mapping of named file suffixes for target files.""" + return self._normalize("suffixes") + + @property + def _build_fixer_path(self) -> str: + return os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "envoy_build_fixer.py") + + @property + def _header_order_path(self) -> str: + return os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "header_order.py") + + def _normalize( + self, + config_type: str, + cb: Callable = tuple) -> Dict[str, Union[Tuple[str, ...], Dict[str, Tuple[str, ...]]]]: + config = {} + for k, v in self[config_type].items(): + if isinstance(v, dict): + config[k] = {} + for key in ("include", "exclude"): + if key in v: + config[k][key] = cb(v[key]) + else: + config[k] = cb(v) + return config class FormatChecker: def __init__(self, args): + self.args = args + self.config_path = args.config_path self.operation_type = args.operation_type self.target_path = args.target_path self.api_prefix = args.api_prefix self.envoy_build_rule_check = not args.skip_envoy_build_rule_check - self.namespace_check = args.namespace_check - self.namespace_check_excluded_paths = args.namespace_check_excluded_paths + [ - "./tools/api_boost/testdata/", - "./tools/clang_tools/", - ] - self.build_fixer_check_excluded_paths = args.build_fixer_check_excluded_paths + [ - "./bazel/external/", - "./bazel/toolchains/", - "./bazel/BUILD", - "./tools/clang_tools", - ] - self.include_dir_order = args.include_dir_order + self._include_dir_order = args.include_dir_order + + @cached_property + def build_fixer_check_excluded_paths(self): + return ( + tuple(self.args.build_fixer_check_excluded_paths) + + self.config.paths["build_fixer"]["exclude"]) + + @cached_property + def config(self) -> FormatConfig: + return FormatConfig(self.config_path) + + @cached_property + def include_dir_order(self): + return ",".join( + self._include_dir_order if self._include_dir_order else self.config["dir_order"]) + + @property + def namespace_check(self): + return self.args.namespace_check + + @cached_property + def namespace_check_excluded_paths(self): + return ( + tuple(self.args.namespace_check_excluded_paths) + + self.config.paths["namespace_check"]["exclude"]) @cached_property def namespace_re(self): @@ -310,12 +215,12 @@ def executable_by_others(self, executable): def check_tools(self): error_messages = [] - clang_format_abs_path = self.look_path(CLANG_FORMAT_PATH) + clang_format_abs_path = self.look_path(self.config.clang_format_path) if clang_format_abs_path: if not self.executable_by_others(clang_format_abs_path): error_messages.append( "command {} exists, but cannot be executed by other " - "users".format(CLANG_FORMAT_PATH)) + "users".format(self.config.clang_format_path)) else: error_messages.append( "Command {} not found. If you have clang-format in version 12.x.x " @@ -325,7 +230,7 @@ def check_tools(self): " export CLANG_FORMAT=clang-format-14.0.0\n" " export CLANG_FORMAT=/opt/bin/clang-format-14\n" " export CLANG_FORMAT=/usr/local/opt/llvm@14/bin/clang-format".format( - CLANG_FORMAT_PATH)) + self.config.clang_format_path)) def check_bazel_tool(name, path, var): bazel_tool_abs_path = self.look_path(path) @@ -350,8 +255,8 @@ def check_bazel_tool(name, path, var): " go get -u github.com/bazelbuild/buildtools/{}".format( path, name, var, var, name, name, name)) - check_bazel_tool('buildifier', BUILDIFIER_PATH, 'BUILDIFIER_BIN') - check_bazel_tool('buildozer', BUILDOZER_PATH, 'BUILDOZER_BIN') + check_bazel_tool('buildifier', self.config.buildifier_path, 'BUILDIFIER_BIN') + check_bazel_tool('buildozer', self.config.buildozer_path, 'BUILDOZER_BIN') return error_messages @@ -372,8 +277,9 @@ def check_namespace(self, file_path): # To avoid breaking the Lyft import, we just check for path inclusion here. def allow_listed_for_protobuf_deps(self, file_path): return ( - file_path.endswith(PROTO_SUFFIX) or file_path.endswith(REPOSITORIES_BZL) - or any(path_segment in file_path for path_segment in GOOGLE_PROTOBUF_ALLOWLIST)) + file_path.endswith(self.config.suffixes["proto"]) + or file_path.endswith(self.config.suffixes["repositories_bzl"]) + or any(file_path.startswith(path) for path in self.config.paths["protobuf"]["include"])) # Real-world time sources should not be instantiated in the source, except for a few # specific cases. They should be passed down from where they are instantied to where @@ -381,31 +287,34 @@ def allow_listed_for_protobuf_deps(self, file_path): def allow_listed_for_realtime(self, file_path): if file_path.endswith(".md"): return True - return file_path in REAL_TIME_ALLOWLIST + return file_path in self.config.paths["real_time"]["include"] def allow_listed_for_register_factory(self, file_path): if not file_path.startswith("./test/"): return True - return any(file_path.startswith(prefix) for prefix in REGISTER_FACTORY_TEST_ALLOWLIST) + return any( + file_path.startswith(prefix) + for prefix in self.config.paths["register_factory_test"]["include"]) def allow_listed_for_serialize_as_string(self, file_path): - return file_path in SERIALIZE_AS_STRING_ALLOWLIST + return file_path in self.config.paths["serialize_as_string"]["include"] def allow_listed_for_std_string_view(self, file_path): - return file_path in STD_STRING_VIEW_ALLOWLIST + return file_path in self.config.paths["std_string_view"]["include"] def allow_listed_for_json_string_to_message(self, file_path): - return file_path in JSON_STRING_TO_MESSAGE_ALLOWLIST + return file_path in self.config.paths["json_string_to_message"]["include"] def allow_listed_for_histogram_si_suffix(self, name): - return name in HISTOGRAM_WITH_SI_SUFFIX_ALLOWLIST + return name in self.config.suffixes["histogram_with_si"]["include"] def allow_listed_for_std_regex(self, file_path): - return file_path.startswith("./test") or file_path in STD_REGEX_ALLOWLIST + return file_path.startswith( + "./test") or file_path in self.config.paths["std_regex"]["include"] def allow_listed_for_grpc_init(self, file_path): - return file_path in GRPC_INIT_ALLOWLIST + return file_path in self.config.paths["grpc_init"]["include"] def allow_listed_for_unpack_to(self, file_path): return file_path.startswith("./test") or file_path in [ @@ -414,17 +323,18 @@ def allow_listed_for_unpack_to(self, file_path): def allow_listed_for_raw_try(self, file_path): # TODO(chaoqin-li1123): Exclude some important extensions from ALLOWLIST. - return file_path in RAW_TRY_ALLOWLIST or file_path.startswith("./source/extensions") + return file_path in self.config.paths["raw_try"]["include"] or file_path.startswith( + "./source/extensions") def deny_listed_for_exceptions(self, file_path): # Returns true when it is a non test header file or the file_path is in DENYLIST or # it is under tools/testdata subdirectory. - return (file_path.endswith('.h') and not file_path.startswith("./test/") and not file_path in EXCEPTION_ALLOWLIST) or file_path in EXCEPTION_DENYLIST \ + return (file_path.endswith('.h') and not file_path.startswith("./test/") and not file_path in self.config.paths["exception"]["include"]) or file_path in self.config.paths["exception"]["exclude"] \ or self.is_in_subdir(file_path, 'tools/testdata') def allow_listed_for_build_urls(self, file_path): - return file_path in BUILD_URLS_ALLOWLIST + return file_path in self.config.paths["build_urls"]["include"] def is_api_file(self, file_path): return file_path.startswith(self.api_prefix) @@ -453,27 +363,28 @@ def is_build_fixer_excluded_file(self, file_path): return False def has_invalid_angle_bracket_directory(self, line): - if not line.startswith(INCLUDE_ANGLE): + if not line.startswith(self.config["include_angle"]): return False - path = line[INCLUDE_ANGLE_LEN:] + path = line[len(self.config["include_angle"]):] slash = path.find("/") if slash == -1: return False subdir = path[0:slash] - return subdir in SUBDIR_SET + return subdir in self.config.dir_order # simple check that all flags are sorted. def check_runtime_flags(self, file_path, error_messages): previous_flag = "" for line_number, line in enumerate(self.read_lines(file_path)): if line.startswith("RUNTIME_GUARD"): - match = FLAG_REGEX.match(line) + match = self.config.re["runtime_guard_flag"].match(line) if not match: error_messages.append("%s does not look like a reloadable flag" % line) break if previous_flag: - if line < previous_flag and match.groups()[0] not in UNSORTED_FLAGS: + if line < previous_flag and match.groups( + )[0] not in self.config["unsorted_flags"]: error_messages.append( "%s and %s are out of order\n" % (line, previous_flag)) previous_flag = line @@ -500,21 +411,23 @@ def report_error(message): def fix_source_line(self, line, line_number): # Strip double space after '.' This may prove overenthusiastic and need to # be restricted to comments and metadata files but works for now. - line = re.sub(DOT_MULTI_SPACE_REGEX, ". ", line) + line = self.config.re["dot_multi_space"].sub(". ", line) if self.has_invalid_angle_bracket_directory(line): line = line.replace("<", '"').replace(">", '"') # Fix incorrect protobuf namespace references. - for invalid_construct, valid_construct in PROTOBUF_TYPE_ERRORS.items(): + for invalid_construct, valid_construct in self.config.replacements[ + "protobuf_type_errors"].items(): line = line.replace(invalid_construct, valid_construct) # Use recommended cpp stdlib - for invalid_construct, valid_construct in LIBCXX_REPLACEMENTS.items(): + for invalid_construct, valid_construct in self.config.replacements["libcxx"].items(): line = line.replace(invalid_construct, valid_construct) # Fix code conventions violations. - for invalid_construct, valid_construct in CODE_CONVENTION_REPLACEMENTS.items(): + for invalid_construct, valid_construct in self.config.replacements["code_convention"].items( + ): line = line.replace(invalid_construct, valid_construct) return line @@ -534,6 +447,9 @@ def has_cond_var_wait_for(self, line): return False return True + def is_api_proto(self, file_path): + return file_path.endswith(self.config.suffixes["proto"]) and self.is_api_file(file_path) + # Determines whether the filename is either in the specified subdirectory, or # at the top level. We consider files in the top level for the benefit of # the check_format testcases in tools/testdata/check_format. @@ -570,29 +486,31 @@ def check_source_line(self, line, file_path, report_error): if line.find(". ") != -1: report_error("over-enthusiastic spaces") if self.is_in_subdir(file_path, 'source', - 'include') and X_ENVOY_USED_DIRECTLY_REGEX.match(line): + 'include') and self.config.re["x_envoy_used_directly"].match(line): report_error( "Please do not use the raw literal x-envoy in source code. See Envoy::Http::PrefixValue." ) if self.has_invalid_angle_bracket_directory(line): report_error("envoy includes should not have angle brackets") - for invalid_construct, valid_construct in PROTOBUF_TYPE_ERRORS.items(): + for invalid_construct, valid_construct in self.config.replacements[ + "protobuf_type_errors"].items(): if invalid_construct in line: report_error( "incorrect protobuf type reference %s; " "should be %s" % (invalid_construct, valid_construct)) - for invalid_construct, valid_construct in LIBCXX_REPLACEMENTS.items(): + for invalid_construct, valid_construct in self.config.replacements["libcxx"].items(): if invalid_construct in line: report_error( "term %s should be replaced with standard library term %s" % (invalid_construct, valid_construct)) - for invalid_construct, valid_construct in CODE_CONVENTION_REPLACEMENTS.items(): + for invalid_construct, valid_construct in self.config.replacements["code_convention"].items( + ): if invalid_construct in line: report_error( "term %s should be replaced with preferred term %s" % (invalid_construct, valid_construct)) # Do not include the virtual_includes headers. - if VIRTUAL_INCLUDE_HEADERS_RE.search(line): + if self.config.re["virtual_include_headers"].search(line): report_error("Don't include the virtual includes headers.") # Some errors cannot be fixed automatically, and actionable, consistent, @@ -627,7 +545,7 @@ def check_source_line(self, line, file_path, report_error): "Don't use CondVar::waitFor(); use TimeSystem::waitFor() instead. If this " "already is TimeSystem::waitFor(), please name the TimeSystem variable " "time_system or time_system_ so the linter can understand.") - duration_arg = DURATION_VALUE_REGEX.search(line) + duration_arg = self.config.re["duration_value"].search(line) if duration_arg and duration_arg.group(1) != "0" and duration_arg.group(1) != "0.0": # Matching duration(int-const or float-const) other than zero report_error( @@ -714,7 +632,7 @@ def check_source_line(self, line, file_path, report_error): report_error( "Don't use __attribute__((packed)), use the PACKED_STRUCT macro defined " "in envoy/common/platform.h instead") - if DESIGNATED_INITIALIZER_REGEX.search(line): + if self.config.re["designated_initializer"].search(line): # Designated initializers are not part of the C++14 standard and are not supported # by MSVC report_error( @@ -727,13 +645,13 @@ def check_source_line(self, line, file_path, report_error): report_error("Don't use 'using testing::Test;, elaborate the type instead") if line.startswith("using testing::TestWithParams;"): report_error("Don't use 'using testing::Test;, elaborate the type instead") - if TEST_NAME_STARTING_LOWER_CASE_REGEX.search(line): + if self.config.re["test_name_starting_lc"].search(line): # Matches variants of TEST(), TEST_P(), TEST_F() etc. where the test name begins # with a lowercase letter. report_error("Test names should be CamelCase, starting with a capital letter") - if OLD_MOCK_METHOD_REGEX.search(line): + if self.config.re["old_mock_method"].search(line): report_error("The MOCK_METHODn() macros should not be used, use MOCK_METHOD() instead") - if FOR_EACH_N_REGEX.search(line): + if self.config.re["for_each_n"].search(line): report_error("std::for_each_n should not be used, use an alternative for loop instead") if not self.allow_listed_for_serialize_as_string(file_path) and "SerializeAsString" in line: @@ -757,10 +675,10 @@ def check_source_line(self, line, file_path, report_error): report_error( "Don't lookup stats by name at runtime; use StatName saved during construction") - if MANGLED_PROTOBUF_NAME_REGEX.search(line): + if self.config.re["mangled_protobuf_name"].search(line): report_error("Don't use mangled Protobuf names for enum constants") - hist_m = HISTOGRAM_SI_SUFFIX_REGEX.search(line) + hist_m = self.config.re["histogram_si_suffix"].search(line) if hist_m and not self.allow_listed_for_histogram_si_suffix(hist_m.group(0)): report_error( "Don't suffix histogram names with the unit symbol, " @@ -788,7 +706,7 @@ def check_source_line(self, line, file_path, report_error): "Don't call grpc_init() or grpc_shutdown() directly, instantiate " + "Grpc::GoogleGrpcContext. See #8282") - if not self.whitelisted_for_memcpy(file_path) and \ + if not self.included_for_memcpy(file_path) and \ not ("test/" in file_path) and \ ("memcpy(" in line) and \ not ("NOLINT(safe-memcpy)" in line): @@ -799,7 +717,7 @@ def check_source_line(self, line, file_path, report_error): if self.deny_listed_for_exceptions(file_path): # Skpping cases where 'throw' is a substring of a symbol like in "foothrowBar". if "throw" in line.split(): - comment_match = COMMENT_REGEX.search(line) + comment_match = self.config.re["comment"].search(line) if comment_match is None or comment_match.start(0) > line.find("throw"): report_error( "Don't introduce throws into exception-free files, use error " @@ -811,9 +729,9 @@ def check_source_line(self, line, file_path, report_error): + "Lua API (bad light userdata pointer) on ARM64 architecture. See " + "https://github.com/LuaJIT/LuaJIT/issues/450#issuecomment-433659873 for details.") - if file_path.endswith(PROTO_SUFFIX): + if file_path.endswith(self.config.suffixes["proto"]): exclude_path = ['v1', 'v2'] - result = PROTO_VALIDATION_STRING.search(line) + result = self.config.re["proto_validation_string"].search(line) if result is not None: if not any(x in file_path for x in exclude_path): report_error("min_bytes is DEPRECATED, Use min_len.") @@ -852,10 +770,11 @@ def fix_build_path(self, file_path): if not self.is_build_fixer_excluded_file(file_path) and not self.is_api_file( file_path) and not self.is_starlark_file(file_path) and not self.is_workspace_file( file_path): - if os.system("%s %s %s" % (ENVOY_BUILD_FIXER_PATH, file_path, file_path)) != 0: + if os.system("%s %s %s" % + (self.config.paths["build_fixer_py"], file_path, file_path)) != 0: error_messages += ["envoy_build_fixer rewrite failed for file: %s" % file_path] - if os.system("%s -lint=fix -mode=fix %s" % (BUILDIFIER_PATH, file_path)) != 0: + if os.system("%s -lint=fix -mode=fix %s" % (self.config.buildifier_path, file_path)) != 0: error_messages += ["buildifier rewrite failed for file: %s" % file_path] return error_messages @@ -865,7 +784,8 @@ def check_build_path(self, file_path): if not self.is_build_fixer_excluded_file(file_path) and not self.is_api_file( file_path) and not self.is_starlark_file(file_path) and not self.is_workspace_file( file_path): - command = "%s %s | diff %s -" % (ENVOY_BUILD_FIXER_PATH, file_path, file_path) + command = "%s %s | diff %s -" % ( + self.config.paths["build_fixer_py"], file_path, file_path) error_messages += self.execute_command( command, "envoy_build_fixer check failed", file_path) @@ -878,7 +798,7 @@ def check_build_path(self, file_path): if not found: error_messages += ["API build file does not provide api_proto_package()"] - command = "%s -mode=diff %s" % (BUILDIFIER_PATH, file_path) + command = "%s -mode=diff %s" % (self.config.buildifier_path, file_path) error_messages += self.execute_command(command, "buildifier check failed", file_path) error_messages += self.check_file_contents(file_path, self.check_build_line) return error_messages @@ -888,7 +808,7 @@ def fix_source_path(self, file_path): error_messages = [] - if not file_path.endswith(PROTO_SUFFIX): + if not file_path.endswith(self.config.suffixes["proto"]): error_messages += self.fix_header_order(file_path) error_messages += self.clang_format(file_path) return error_messages @@ -896,14 +816,15 @@ def fix_source_path(self, file_path): def check_source_path(self, file_path): error_messages = self.check_file_contents(file_path, self.check_source_line) - if not file_path.endswith(PROTO_SUFFIX): + if not file_path.endswith(self.config.suffixes["proto"]): error_messages += self.check_namespace(file_path) command = ( - "%s --include_dir_order %s --path %s | diff %s -" % - (HEADER_ORDER_PATH, self.include_dir_order, file_path, file_path)) + "%s --include_dir_order %s --path %s | diff %s -" % ( + self.config.paths["header_order_py"], self.include_dir_order, file_path, + file_path)) error_messages += self.execute_command( command, "header_order.py check failed", file_path) - command = ("%s %s | diff %s -" % (CLANG_FORMAT_PATH, file_path, file_path)) + command = ("%s %s | diff %s -" % (self.config.clang_format_path, file_path, file_path)) error_messages += self.execute_command(command, "clang-format check failed", file_path) return error_messages @@ -911,7 +832,8 @@ def check_source_path(self, file_path): # - "26,27c26" # - "12,13d13" # - "7a8,9" - def execute_command(self, command, error_message, file_path, regex=LINE_NUMBER_RE): + def execute_command(self, command, error_message, file_path, regex=None): + regex = regex or self.config.re["line_number"] try: output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() if output: @@ -929,13 +851,13 @@ def execute_command(self, command, error_message, file_path, regex=LINE_NUMBER_R def fix_header_order(self, file_path): command = "%s --rewrite --include_dir_order %s --path %s" % ( - HEADER_ORDER_PATH, self.include_dir_order, file_path) + self.config.paths["header_order_py"], self.include_dir_order, file_path) if os.system(command) != 0: return ["header_order.py rewrite error: %s" % (file_path)] return [] def clang_format(self, file_path): - command = "%s -i %s" % (CLANG_FORMAT_PATH, file_path) + command = "%s -i %s" % (self.config.clang_format_path, file_path) if os.system(command) != 0: return ["clang-format rewrite error: %s" % (file_path)] return [] @@ -1029,10 +951,14 @@ def check_format_visitor(self, arg, dir_name, names, fail_on_diff=False): dir_name = normalize_path(dir_name) - # TODO(phlax): improve class/process handling - this is required because if it - # is not cached before the class is sent into the pool, it only caches on the + # TODO(phlax): improve class/process handling - this is required because if these + # are not cached before the class is sent into the pool, it only caches them on the # forked proc + self.build_fixer_check_excluded_paths + self.namespace_check_excluded_paths self.namespace_re + self.config.replacements + self.config.dir_order for file_name in names: result = pool.apply_async( @@ -1048,8 +974,8 @@ def check_error_messages(self, error_messages): return True return False - def whitelisted_for_memcpy(self, file_path): - return file_path in MEMCPY_WHITELIST + def included_for_memcpy(self, file_path): + return file_path in self.config.paths["memcpy"]["include"] def normalize_path(path): @@ -1077,6 +1003,10 @@ def normalize_path(path): nargs="?", default=".", help="specify the root directory for the script to recurse over. Default '.'.") + parser.add_argument( + "--config_path", + default="./tools/code_format/config.yaml", + help="specify the config path. Default './tools/code_format/config.yaml'.") parser.add_argument( "--fail_on_diff", action="store_true", @@ -1121,38 +1051,32 @@ def normalize_path(path): parser.add_argument( "--include_dir_order", type=str, - default=",".join(common.include_dir_order()), + default="", help="specify the header block include directory order.") args = parser.parse_args() - if args.add_excluded_prefixes: - EXCLUDED_PREFIXES += tuple(args.add_excluded_prefixes) + format_checker = FormatChecker(args) + excluded_prefixes = format_checker.config.paths["excluded"] + if args.add_excluded_prefixes: + excluded_prefixes += tuple(args.add_excluded_prefixes) + # Check whether all needed external tools are available. ct_error_messages = format_checker.check_tools() if format_checker.check_error_messages(ct_error_messages): sys.exit(1) - # TODO(phlax): Remove this after a month or so - logger.warning( - "Please note: `tools/code_format/check_format.py` no longer checks API `.proto` files, " - "please use `tools/proto_format/proto_format.sh` if you are making changes to the API files" - ) + if not os.environ.get("CI"): + # TODO(phlax): Remove this after a month or so + logger.warning( + "Please note: `tools/code_format/check_format.py` no longer checks API `.proto` files, " + "please use `tools/proto_format/proto_format.sh` if you are making changes to the API files" + ) def check_visibility(error_messages): - # https://github.com/envoyproxy/envoy/issues/20589 - # https://github.com/envoyproxy/envoy/issues/9953 - # PLEASE DO NOT ADD FILES TO THIS LIST WITHOUT SENIOR MAINTAINER APPROVAL - exclude_list = ( - "':(exclude)source/extensions/early_data/BUILD' " - "':(exclude)source/extensions/filters/http/buffer/BUILD' " - "':(exclude)source/extensions/filters/network/common/BUILD' " - "':(exclude)source/extensions/transport_sockets/common/BUILD' " - "':(exclude)source/extensions/udp_packet_writer/default/BUILD' " - "':(exclude)source/extensions/udp_packet_writer/gso/BUILD' ") command = ( "git diff $(tools/git/last_github_commit.sh) -- source/extensions/* %s |grep '+.*visibility ='" - % exclude_list) + % "".join([f"':(exclude){c}' " for c in format_checker.config["visibility_excludes"]])) try: output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).strip() if output: @@ -1177,7 +1101,7 @@ def get_owners(): for line in f: if "Senior extension maintainers" in line: return maintainers - m = PROJECT_OWNERS_RE.search(line) + m = format_checker.config.re["maintainers"].search(line) if m is not None: maintainers.append("@" + m.group(1).lower()) @@ -1192,10 +1116,10 @@ def owned_directories(error_messages): for line in f: # If this line is of the form "extensions/... @owner1 @owner2" capture the directory # name and store it in the list of directories with documented owners. - m = EXTENSIONS_CODEOWNERS_REGEX.search(line) + m = format_checker.config.re["codeowners_extensions"].search(line) if m is not None and not line.startswith('#'): owned.append(m.group(1).strip()) - owners = OWNER_RE.findall(m.group(2).strip()) + owners = format_checker.config.re["owner"].findall(m.group(2).strip()) if len(owners) < 2: error_messages.append( "Extensions require at least 2 owners in CODEOWNERS:\n" @@ -1206,7 +1130,7 @@ def owned_directories(error_messages): "Extensions require at least one maintainer OWNER:\n" " {}".format(line)) - m = CONTRIB_CODEOWNERS_REGEX.search(line) + m = format_checker.config.re["codeowners_contrib"].search(line) if m is not None and not line.startswith('#'): stripped_path = m.group(1).strip() if not stripped_path.endswith('/'): @@ -1224,7 +1148,7 @@ def owned_directories(error_messages): continue owned.append(stripped_path) - owners = OWNER_RE.findall(m.group(2).strip()) + owners = format_checker.config.re["owner"].findall(m.group(2).strip()) if len(owners) < 2: error_messages.append( "Contrib extensions require at least 2 owners in CODEOWNERS:\n" @@ -1241,12 +1165,13 @@ def owned_directories(error_messages): check_visibility(error_messages) if os.path.isfile(args.target_path): - # All of our EXCLUDED_PREFIXES start with "./", but the provided + # All of our `excluded_prefixes` start with "./", but the provided # target path argument might not. Add it here if it is missing, # and use that normalized path for both lookup and `check_format`. normalized_target_path = normalize_path(args.target_path) if not normalized_target_path.startswith( - EXCLUDED_PREFIXES) and normalized_target_path.endswith(SUFFIXES): + excluded_prefixes) and normalized_target_path.endswith( + format_checker.config.suffixes["included"]): error_messages += format_checker.check_format(normalized_target_path) else: results = [] @@ -1260,9 +1185,10 @@ def pooled_check_format(path_predicate): for filename in files: file_path = os.path.join(root, filename) check_file = ( - path_predicate(filename) and not file_path.startswith(EXCLUDED_PREFIXES) - and file_path.endswith(SUFFIXES) and - not (file_path.endswith(PROTO_SUFFIX) and root.startswith(args.api_prefix))) + path_predicate(filename) and not file_path.startswith(excluded_prefixes) + and file_path.endswith(format_checker.config.suffixes["included"]) and not ( + file_path.endswith(format_checker.config.suffixes["proto"]) + and root.startswith(args.api_prefix))) if check_file: _files.append(filename) if not _files: From bbddb4d24c061a454434e9bc233c397520444004 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 29 Aug 2022 18:26:47 +0000 Subject: [PATCH 190/238] Format Signed-off-by: silverstar195 --- source/extensions/path/rewrite/pattern_template/config.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index a49123e4b3b23..43356a0976807 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -20,8 +20,7 @@ class PatternTemplateRewriterFactory : public Router::PathRewriterFactory { v3::PatternTemplateRewriteConfig&>( rewrite_config, ProtobufMessage::getStrictValidationVisitor()); - if (!UriTemplate::isValidRewritePattern(path_rewrite_config.path_template_rewrite()) - .ok()) { + if (!UriTemplate::isValidRewritePattern(path_rewrite_config.path_template_rewrite()).ok()) { return absl::InvalidArgumentError( fmt::format("path_rewrite_policy.path_template_rewrite {} is invalid", path_rewrite_config.path_template_rewrite())); From 522048a4de24889d5cb1162d56b2524f37501516 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 29 Aug 2022 20:10:29 +0000 Subject: [PATCH 191/238] Format Signed-off-by: silverstar195 --- envoy/router/router.h | 4 ++-- source/common/router/config_impl.cc | 18 +++++++++--------- source/common/router/config_impl.h | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/envoy/router/router.h b/envoy/router/router.h index a19f1bab5b05f..d9e81dc0f2cf9 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -320,7 +320,7 @@ class PathMatchPolicy { * Returns the stored target route PathMatcher. * @return a PathMatcher instance. */ - virtual PathMatcherSharedPtr path_matcher() const PURE; + virtual PathMatcherSharedPtr pathMatcher() const PURE; }; /** @@ -339,7 +339,7 @@ class PathRewritePolicy { * Returns the stored target route PathRewriter. * @return a PathRewriter instance. */ - virtual PathRewriterSharedPtr path_rewriter() const PURE; + virtual PathRewriterSharedPtr pathRewriter() const PURE; }; /** diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index bc5cacea63860..9d708725c8a3b 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -377,7 +377,7 @@ PathRewritePolicyImpl::PathRewritePolicyImpl( rewriter_ = rewriter.value(); } -PathRewriterSharedPtr PathRewritePolicyImpl::path_rewriter() const { return rewriter_; } +PathRewriterSharedPtr PathRewritePolicyImpl::pathRewriter() const { return rewriter_; } PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; @@ -400,7 +400,7 @@ PathMatchPolicyImpl::PathMatchPolicyImpl( path_matcher_ = matcher.value(); } -PathMatcherSharedPtr PathMatchPolicyImpl::path_matcher() const { return path_matcher_; } +PathMatcherSharedPtr PathMatchPolicyImpl::pathMatcher() const { return path_matcher_; } absl::flat_hash_set InternalRedirectPolicyImpl::buildRedirectResponseCodes( const envoy::config::route::v3::InternalRedirectPolicy& policy_config) const { @@ -718,8 +718,8 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, } if (path_rewrite_policy_.enabled()) { - absl::Status compatible_status = path_rewrite_policy_.path_rewriter()->isCompatibleMatchPolicy( - path_match_policy_.path_matcher(), path_match_policy_.enabled()); + absl::Status compatible_status = path_rewrite_policy_.pathRewriter()->isCompatibleMatchPolicy( + path_match_policy_.pathMatcher(), path_match_policy_.enabled()); if (!compatible_status.ok()) { throw EnvoyException(std::string(compatible_status.message())); } @@ -1030,7 +1030,7 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa absl::string_view just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); absl::StatusOr new_path = - path_rewrite_policy_.path_rewriter()->rewriteUrl(just_path, matched_path); + path_rewrite_policy_.pathRewriter()->rewriteUrl(just_path, matched_path); // if rewrite fails return old path. if (!new_path.ok()) { @@ -1492,18 +1492,18 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - match_pattern_(path_match_policy_.path_matcher()->pattern()){}; + match_pattern_(path_match_policy_.pathMatcher()->pattern()){}; void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { - finalizePathHeader(headers, path_match_policy_.path_matcher()->pattern(), + finalizePathHeader(headers, path_match_policy_.pathMatcher()->pattern(), insert_envoy_original_path); } absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { return currentUrlPathAfterRewriteWithMatchedPath(headers, - path_match_policy_.path_matcher()->pattern()); + path_match_policy_.pathMatcher()->pattern()); } RouteConstSharedPtr @@ -1511,7 +1511,7 @@ PathMatchPolicyRouteEntryImpl::matches(const Http::RequestHeaderMap& headers, const StreamInfo::StreamInfo& stream_info, uint64_t random_value) const { if (RouteEntryImplBase::matchRoute(headers, stream_info, random_value) && - path_match_policy_.path_matcher()->match(headers.getPathValue())) { + path_match_policy_.pathMatcher()->match(headers.getPathValue())) { return clusterEntry(headers, random_value); } return nullptr; diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 33b1934aeec65..f188712720566 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -472,7 +472,7 @@ class PathMatchPolicyImpl : public PathMatchPolicy { // Router::PathMatchPolicy bool enabled() const override { return enabled_; } - PathMatcherSharedPtr path_matcher() const override; + PathMatcherSharedPtr pathMatcher() const override; private: const bool enabled_; @@ -495,7 +495,7 @@ class PathRewritePolicyImpl : public PathRewritePolicy { // Router::PathRewritePolicy bool enabled() const override { return enabled_; } - PathRewriterSharedPtr path_rewriter() const override; + PathRewriterSharedPtr pathRewriter() const override; private: const bool enabled_; From d96998aeac544b644b248040512bf327c941b678 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 29 Aug 2022 20:48:45 +0000 Subject: [PATCH 192/238] Format Signed-off-by: silverstar195 --- test/mocks/router/mocks.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/mocks/router/mocks.h b/test/mocks/router/mocks.h index c875c0f0e7ab8..fed00408bcf8d 100644 --- a/test/mocks/router/mocks.h +++ b/test/mocks/router/mocks.h @@ -172,7 +172,7 @@ class MockPathRewritePolicy : public PathRewritePolicy { public: MockPathRewritePolicy(); MOCK_METHOD(bool, enabled, (), (const)); - MOCK_METHOD(PathRewriterSharedPtr, path_rewriter, (), (const)); + MOCK_METHOD(PathRewriterSharedPtr, pathRewriter, (), (const)); }; class MockPathRewriter : public PathRewriter { @@ -184,7 +184,7 @@ class MockPathMatchPolicy : public PathMatchPolicy { public: MockPathMatchPolicy(); MOCK_METHOD(bool, enabled, (), (const)); - MOCK_METHOD(PathMatcherSharedPtr, path_matcher, (), (const)); + MOCK_METHOD(PathMatcherSharedPtr, pathMatcher, (), (const)); }; class MockPathMatcher : public PathMatcher { From b87b2130283a9c9c4c52f507f958910f74e67fdd Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 29 Aug 2022 21:31:02 +0000 Subject: [PATCH 193/238] Format Signed-off-by: silverstar195 --- test/common/router/config_impl_test.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index e3d3ada173e99..54707b33ab8cc 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -8822,11 +8822,11 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsFilledFromConfigInRouteAction) { const auto& pattern_match_policy = config.route(headers, 0)->routeEntry()->pathMatchPolicy(); EXPECT_TRUE(pattern_match_policy.enabled()); - EXPECT_EQ(pattern_match_policy.path_matcher()->pattern(), "/bar/{country}/{lang}"); + EXPECT_EQ(pattern_match_policy.pathMatcher()->pattern(), "/bar/{country}/{lang}"); const auto& pattern_rewrite_policy = config.route(headers, 0)->routeEntry()->pathRewritePolicy(); EXPECT_TRUE(pattern_rewrite_policy.enabled()); - EXPECT_EQ(pattern_rewrite_policy.path_rewriter()->pattern(), "/bar/{lang}/{country}"); + EXPECT_EQ(pattern_rewrite_policy.pathRewriter()->pattern(), "/bar/{lang}/{country}"); } TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { From cb45a6053e20785f028f8219ea797673def73523 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 30 Aug 2022 14:27:52 +0000 Subject: [PATCH 194/238] nits Signed-off-by: silverstar195 --- source/extensions/path/uri_template_lib/uri_template.h | 2 +- .../path/uri_template_lib/uri_template_internal.h | 6 +++--- test/extensions/path/uri_template_lib/uri_template_test.cc | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/source/extensions/path/uri_template_lib/uri_template.h b/source/extensions/path/uri_template_lib/uri_template.h index 807bd3a567f5b..5064569bb867b 100644 --- a/source/extensions/path/uri_template_lib/uri_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -69,7 +69,7 @@ absl::Status isValidMatchPattern(absl::string_view match_pattern); /** * Concatenates literals and extracts variable values to form the final rewritten path. * For example: - * rewrite_pattern: [var_index=2, literal="cat"] + * rewrite_pattern: [capture_index=2, literal="cat"] * path: "/bar/var" * capture_regex: "(1)/(2)" * Rewrite would result in rewrite of "/var/cat". diff --git a/source/extensions/path/uri_template_lib/uri_template_internal.h b/source/extensions/path/uri_template_lib/uri_template_internal.h index 94334f2d86f65..b27bd4062a204 100644 --- a/source/extensions/path/uri_template_lib/uri_template_internal.h +++ b/source/extensions/path/uri_template_lib/uri_template_internal.h @@ -81,10 +81,10 @@ bool isValidRewriteLiteral(absl::string_view literal); bool isValidVariableName(absl::string_view variable); /** - * Used by the following Parse{Literal.Operator,Variable} functions + * Used by the following Parse{Literal,Operator,Variable} functions * in the return value. The functions would take the given pattern, - * parse what it can into |parsed_value| and return the unparse - * portion of the pattern in |unparsed_pattern|. + * parse what it can into |parsed_value| and return the unparsed + * portion of the pattern in |unparsed_pattern|. */ template struct ParsedResult { ParsedResult(T val, absl::string_view pattern) : parsed_value_(val), unparsed_pattern_(pattern) {} diff --git a/test/extensions/path/uri_template_lib/uri_template_test.cc b/test/extensions/path/uri_template_lib/uri_template_test.cc index 4030f5e28ba5c..f7438e537993b 100644 --- a/test/extensions/path/uri_template_lib/uri_template_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_test.cc @@ -151,7 +151,6 @@ TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { absl::StatusOr rewrite = parseRewritePattern(rewritePattern(), kCaptureRegex); ASSERT_OK(rewrite); - // EXPECT_THAT(rewrite.value(), testing::EqualsProto(expected_proto())); } class ParseRewriteFailure : public testing::TestWithParam {}; From 0d42c560f98e9fd50aa37f3b1c1e50e88d5b602b Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 30 Aug 2022 16:41:30 +0000 Subject: [PATCH 195/238] Change coverage Signed-off-by: silverstar195 --- test/per_file_coverage.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/per_file_coverage.sh b/test/per_file_coverage.sh index f09846ef6c6f8..59e0a4574891e 100755 --- a/test/per_file_coverage.sh +++ b/test/per_file_coverage.sh @@ -70,8 +70,8 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/io_socket:96.2" "source/extensions/io_socket/user_space:96.2" "source/extensions/network/dns_resolver/getaddrinfo:96.3" -"source/extensions/path/rewrite:89.1" -"source/extensions/path/rewrite/pattern_template:89.1" +"source/extensions/path/rewrite:88.1" +"source/extensions/path/rewrite/pattern_template:88.1" "source/extensions/rate_limit_descriptors:95.5" "source/extensions/rate_limit_descriptors/expr:95.5" "source/extensions/stat_sinks/common:96.4" From ea5ad5a31cafe17298244a8668d09d9e71b67440 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 31 Aug 2022 14:59:04 +0000 Subject: [PATCH 196/238] Rename instances left behind Signed-off-by: silverstar195 --- envoy/router/path_match.h | 16 ++++++++-------- envoy/router/path_rewrite.h | 18 +++++++++--------- envoy/router/router.h | 4 ++-- source/common/router/config_impl.cc | 2 +- .../pattern_template_rewrite.cc | 12 ++++++------ .../pattern_template_rewrite.h | 8 ++++---- .../rewrite/pattern_template/library_test.cc | 6 +++--- 7 files changed, 33 insertions(+), 33 deletions(-) diff --git a/envoy/router/path_match.h b/envoy/router/path_match.h index a675e65b32be8..cf95be1f9adc8 100644 --- a/envoy/router/path_match.h +++ b/envoy/router/path_match.h @@ -20,7 +20,7 @@ class PathMatcher : Logger::Loggable { virtual ~PathMatcher() = default; /** - * Used to determine if the current url matches the predicate pattern. + * Used to determine if the current path matches the pattern. * * @param url current url of route * @return true if route url matches the predicate pattern. @@ -28,12 +28,12 @@ class PathMatcher : Logger::Loggable { virtual bool match(absl::string_view path) const PURE; /** - * @return the match pattern of the predicate. + * @return the match pattern */ virtual absl::string_view pattern() const PURE; /** - * @return the name of the current predicate. + * @return the name of the path matcher. */ virtual absl::string_view name() const PURE; }; @@ -41,15 +41,15 @@ class PathMatcher : Logger::Loggable { using PathMatcherSharedPtr = std::shared_ptr; /** - * Factory for PathMatchPredicateFactory. + * Factory for PathMatcher. */ class PathMatcherFactory : public Envoy::Config::TypedFactory { public: ~PathMatcherFactory() override = default; /** - * @param config contains the proto stored in TypedExtensionConfig for the predicate. - * @return an PathMatchPredicateSharedPtr. + * @param config contains the proto stored in TypedExtensionConfig. + * @return an PathMatcherSharedPtr. */ virtual absl::StatusOr createPathMatcher(const Protobuf::Message& config) PURE; @@ -57,12 +57,12 @@ class PathMatcherFactory : public Envoy::Config::TypedFactory { ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; /** - * @return the name of the match pattern predicate to be created. + * @return the name of the path matcher to be created. */ std::string name() const override PURE; /** - * @return the category of the match pattern predicate to be created. + * @return the category of the path matcher to be created. */ std::string category() const override { return "envoy.path.match"; } }; diff --git a/envoy/router/path_rewrite.h b/envoy/router/path_rewrite.h index a603a18d86df0..7eed3c3358842 100644 --- a/envoy/router/path_rewrite.h +++ b/envoy/router/path_rewrite.h @@ -31,15 +31,15 @@ class PathRewriter : Logger::Loggable { bool active_policy) const PURE; /** - * Used to rewrite the current url to the specified output. Can return a failure in case rewrite + * Used to rewrite the current path to the specified output. Can return a failure in case rewrite * is not successful. * - * @param url current url of route - * @param matched_path pattern to rewrite the url to - * @return the rewritten url. + * @param path current path of route + * @param rewrite_pattern pattern to rewrite the path to + * @return the rewritten path. */ - virtual absl::StatusOr rewriteUrl(absl::string_view current_pattern, - absl::string_view matched_path) const PURE; + virtual absl::StatusOr rewritePath(absl::string_view path, + absl::string_view rewrite_pattern) const PURE; /** * @return the rewrite pattern. @@ -47,7 +47,7 @@ class PathRewriter : Logger::Loggable { virtual absl::string_view pattern() const PURE; /** - * @return the name. + * @return the name of the pattern rewriter. */ virtual absl::string_view name() const PURE; }; @@ -71,12 +71,12 @@ class PathRewriterFactory : public Envoy::Config::TypedFactory { ProtobufTypes::MessagePtr createEmptyConfigProto() override PURE; /** - * @return the name of the rewrite pattern to be created. + * @return the name of the pattern rewriter to be created. */ std::string name() const override PURE; /** - * @return the category of the rewrite pattern to be created. + * @return the category of the pattern rewriter to be created. */ std::string category() const override { return "envoy.path.rewrite"; } }; diff --git a/envoy/router/router.h b/envoy/router/router.h index d9e81dc0f2cf9..c9cc5131a460a 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -312,7 +312,7 @@ class PathMatchPolicy { virtual ~PathMatchPolicy() = default; /** - * @return whether path match based on custom policy is enabled on this route. + * @return whether path match policy is enabled on this route. */ virtual bool enabled() const PURE; @@ -331,7 +331,7 @@ class PathRewritePolicy { virtual ~PathRewritePolicy() = default; /** - * @return whether path rewrite based on custom policy is enabled on this route. + * @return whether path rewrite policy is enabled on this route. */ virtual bool enabled() const PURE; diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 9d708725c8a3b..79e78487b88e0 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1030,7 +1030,7 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa absl::string_view just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); absl::StatusOr new_path = - path_rewrite_policy_.pathRewriter()->rewriteUrl(just_path, matched_path); + path_rewrite_policy_.pathRewriter()->rewritePath(just_path, matched_path); // if rewrite fails return old path. if (!new_path.ok()) { diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index 084f1f205daa3..f1cd095a80f79 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -36,29 +36,29 @@ PatternTemplateRewriter::isCompatibleMatchPolicy(Router::PathMatcherSharedPtr pa // This is needed to match up variable values. // Validation between extensions as they share rewrite pattern variables. - if (!isValidSharedVariableSet(url_rewrite_pattern_, path_match_predicate->pattern()).ok()) { + if (!isValidSharedVariableSet(rewrite_pattern_, path_match_predicate->pattern()).ok()) { return absl::InvalidArgumentError( fmt::format("mismatch between variables in path_match_policy {} and path_rewrite_policy {}", - path_match_predicate->pattern(), url_rewrite_pattern_)); + path_match_predicate->pattern(), rewrite_pattern_)); } return absl::OkStatus(); } absl::StatusOr -PatternTemplateRewriter::rewriteUrl(absl::string_view pattern, +PatternTemplateRewriter::rewritePath(absl::string_view pattern, absl::string_view matched_path) const { absl::StatusOr regex_pattern = convertPathPatternSyntaxToRegex(matched_path); if (!regex_pattern.ok()) { - return absl::InvalidArgumentError("Unable to parse url pattern regex"); + return absl::InvalidArgumentError("Unable to parse matched_path"); } std::string regex_pattern_str = *std::move(regex_pattern); absl::StatusOr rewrite_pattern = - parseRewritePattern(url_rewrite_pattern_, regex_pattern_str); + parseRewritePattern(rewrite_pattern_, regex_pattern_str); if (!rewrite_pattern.ok()) { - return absl::InvalidArgumentError("Unable to parse url rewrite pattern"); + return absl::InvalidArgumentError("Unable to parse path rewrite pattern"); } const envoy::extensions::uri_template::RewriteSegments& rewrite_pattern_proto = diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 72d68b319a1a5..fcf2c3d3d8efa 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -38,12 +38,12 @@ class PatternTemplateRewriter : public Router::PathRewriter { explicit PatternTemplateRewriter( const envoy::extensions::path::rewrite::pattern_template::v3::PatternTemplateRewriteConfig& rewrite_config) - : url_rewrite_pattern_(rewrite_config.path_template_rewrite()) {} + : rewrite_pattern_(rewrite_config.path_template_rewrite()) {} // Router::PathRewriter - absl::string_view pattern() const override { return url_rewrite_pattern_; } + absl::string_view pattern() const override { return rewrite_pattern_; } - absl::StatusOr rewriteUrl(absl::string_view pattern, + absl::StatusOr rewritePath(absl::string_view pattern, absl::string_view matched_path) const override; absl::Status isCompatibleMatchPolicy(Router::PathMatcherSharedPtr match_policy, @@ -52,7 +52,7 @@ class PatternTemplateRewriter : public Router::PathRewriter { absl::string_view name() const override { return NAME; } private: - std::string url_rewrite_pattern_{nullptr}; + std::string rewrite_pattern_{nullptr}; }; } // namespace Rewrite diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc index d97b38f9c75b6..6674d9cddc6dc 100644 --- a/test/extensions/path/rewrite/pattern_template/library_test.cc +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -67,7 +67,7 @@ TEST(RewriteTest, BasicUsage) { )EOF"; Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); - EXPECT_EQ(predicate->rewriteUrl("/bar/en/usa", "/bar/{country}/{lang}").value(), "/bar/usa/en"); + EXPECT_EQ(predicate->rewritePath("/bar/en/usa", "/bar/{country}/{lang}").value(), "/bar/usa/en"); EXPECT_EQ(predicate->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); } @@ -81,9 +81,9 @@ TEST(RewriteTest, RewriteInvalidRegex) { Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); absl::StatusOr rewrite_or_error = - predicate->rewriteUrl("/bar/en/usa", "/bar/invalid}/{lang}"); + predicate->rewritePath("/bar/en/usa", "/bar/invalid}/{lang}"); EXPECT_FALSE(rewrite_or_error.ok()); - EXPECT_EQ(rewrite_or_error.status().message(), "Unable to parse url pattern regex"); + EXPECT_EQ(rewrite_or_error.status().message(), "Unable to parse matched_path"); } TEST(RewriteTest, MatchPatternValidation) { From d121c9c6f1a078528ecedaf71e5286dff9b593bf Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 31 Aug 2022 15:09:32 +0000 Subject: [PATCH 197/238] Format Signed-off-by: silverstar195 --- envoy/router/path_rewrite.h | 2 +- .../path/rewrite/pattern_template/pattern_template_rewrite.cc | 2 +- .../path/rewrite/pattern_template/pattern_template_rewrite.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/envoy/router/path_rewrite.h b/envoy/router/path_rewrite.h index 7eed3c3358842..34d4965ec7dd6 100644 --- a/envoy/router/path_rewrite.h +++ b/envoy/router/path_rewrite.h @@ -39,7 +39,7 @@ class PathRewriter : Logger::Loggable { * @return the rewritten path. */ virtual absl::StatusOr rewritePath(absl::string_view path, - absl::string_view rewrite_pattern) const PURE; + absl::string_view rewrite_pattern) const PURE; /** * @return the rewrite pattern. diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index f1cd095a80f79..6069d33e4dc45 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -47,7 +47,7 @@ PatternTemplateRewriter::isCompatibleMatchPolicy(Router::PathMatcherSharedPtr pa absl::StatusOr PatternTemplateRewriter::rewritePath(absl::string_view pattern, - absl::string_view matched_path) const { + absl::string_view matched_path) const { absl::StatusOr regex_pattern = convertPathPatternSyntaxToRegex(matched_path); if (!regex_pattern.ok()) { return absl::InvalidArgumentError("Unable to parse matched_path"); diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index fcf2c3d3d8efa..af90c0c2215bd 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -44,7 +44,7 @@ class PatternTemplateRewriter : public Router::PathRewriter { absl::string_view pattern() const override { return rewrite_pattern_; } absl::StatusOr rewritePath(absl::string_view pattern, - absl::string_view matched_path) const override; + absl::string_view matched_path) const override; absl::Status isCompatibleMatchPolicy(Router::PathMatcherSharedPtr match_policy, bool active_policy) const override; From bb707407d8ec72e81c148e243d41e18cf8550955 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 31 Aug 2022 17:23:57 +0000 Subject: [PATCH 198/238] Grammer Signed-off-by: silverstar195 --- envoy/router/path_match.h | 4 ++-- envoy/router/path_rewrite.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/envoy/router/path_match.h b/envoy/router/path_match.h index cf95be1f9adc8..d98e4e59f35b7 100644 --- a/envoy/router/path_match.h +++ b/envoy/router/path_match.h @@ -20,7 +20,7 @@ class PathMatcher : Logger::Loggable { virtual ~PathMatcher() = default; /** - * Used to determine if the current path matches the pattern. + * Determines if the current path matches the pattern. * * @param url current url of route * @return true if route url matches the predicate pattern. @@ -28,7 +28,7 @@ class PathMatcher : Logger::Loggable { virtual bool match(absl::string_view path) const PURE; /** - * @return the match pattern + * @return the match pattern. */ virtual absl::string_view pattern() const PURE; diff --git a/envoy/router/path_rewrite.h b/envoy/router/path_rewrite.h index 34d4965ec7dd6..48eda4ea68b9b 100644 --- a/envoy/router/path_rewrite.h +++ b/envoy/router/path_rewrite.h @@ -21,7 +21,7 @@ class PathRewriter : Logger::Loggable { virtual ~PathRewriter() = default; /** - * Used to determine if the matcher policy is compatible. + * Determines if the matcher policy is compatible. * * @param path_match_policy current path match policy for route * @param active_policy true if user provided policy @@ -31,7 +31,7 @@ class PathRewriter : Logger::Loggable { bool active_policy) const PURE; /** - * Used to rewrite the current path to the specified output. Can return a failure in case rewrite + * Rewrites the current path to the specified output. Return a failure in case rewrite * is not successful. * * @param path current path of route From a800753f72d219b96bae910c840c9f046a938234 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 31 Aug 2022 17:28:50 +0000 Subject: [PATCH 199/238] Add todo Signed-off-by: silverstar195 --- .../path/uri_template_lib/proto/rewrite_segments.proto | 1 + 1 file changed, 1 insertion(+) diff --git a/source/extensions/path/uri_template_lib/proto/rewrite_segments.proto b/source/extensions/path/uri_template_lib/proto/rewrite_segments.proto index a11ea58a3a5a4..3c942274c7156 100644 --- a/source/extensions/path/uri_template_lib/proto/rewrite_segments.proto +++ b/source/extensions/path/uri_template_lib/proto/rewrite_segments.proto @@ -10,6 +10,7 @@ option java_multiple_files = true; option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/url_template/url_template"; option (udpa.annotations.file_status).package_version_status = ACTIVE; +// TODO(silverstar194): Convert proto to struct // Holds the segments for rewriting urls base on uri pattern templates message RewriteSegments { message RewriteSegment { From f1528badaff2384eb270fe37c8a67ea5640d06df Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 31 Aug 2022 20:41:53 +0000 Subject: [PATCH 200/238] Add work for proto refactor todo Signed-off-by: silverstar195 --- source/extensions/path/uri_template_lib/BUILD | 2 - .../path/uri_template_lib/proto/BUILD | 17 -- .../proto/rewrite_segments.proto | 30 --- .../path/uri_template_lib/uri_template.cc | 33 ++- .../path/uri_template_lib/uri_template.h | 15 +- test/extensions/path/uri_template_lib/BUILD | 2 - .../uri_template_lib/uri_template_test.cc | 250 ++++++++---------- 7 files changed, 133 insertions(+), 216 deletions(-) delete mode 100644 source/extensions/path/uri_template_lib/proto/BUILD delete mode 100644 source/extensions/path/uri_template_lib/proto/rewrite_segments.proto diff --git a/source/extensions/path/uri_template_lib/BUILD b/source/extensions/path/uri_template_lib/BUILD index 715e0d911005c..dfea1c17ec2ce 100644 --- a/source/extensions/path/uri_template_lib/BUILD +++ b/source/extensions/path/uri_template_lib/BUILD @@ -22,7 +22,6 @@ envoy_cc_library( deps = [ ":uri_template_internal_cc", "//source/common/http:path_utility_lib", - "//source/extensions/path/uri_template_lib/proto:uri_template_rewrite_segements_cc_proto", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", @@ -35,7 +34,6 @@ envoy_cc_library( srcs = ["uri_template_internal.cc"], hdrs = ["uri_template_internal.h"], deps = [ - "//source/extensions/path/uri_template_lib/proto:uri_template_rewrite_segements_cc_proto", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/flags:flag", "@com_google_absl//absl/functional:function_ref", diff --git a/source/extensions/path/uri_template_lib/proto/BUILD b/source/extensions/path/uri_template_lib/proto/BUILD deleted file mode 100644 index d8ad5c3057d55..0000000000000 --- a/source/extensions/path/uri_template_lib/proto/BUILD +++ /dev/null @@ -1,17 +0,0 @@ -load( - "//bazel:envoy_build_system.bzl", - "envoy_extension_package", - "envoy_proto_library", -) - -licenses(["notice"]) # Apache 2 - -# Wildcard & Uri Pattern Matching Proto - -envoy_extension_package() - -envoy_proto_library( - name = "uri_template_rewrite_segements", - srcs = ["rewrite_segments.proto"], - deps = ["@com_github_cncf_udpa//udpa/annotations:pkg"], -) diff --git a/source/extensions/path/uri_template_lib/proto/rewrite_segments.proto b/source/extensions/path/uri_template_lib/proto/rewrite_segments.proto deleted file mode 100644 index 3c942274c7156..0000000000000 --- a/source/extensions/path/uri_template_lib/proto/rewrite_segments.proto +++ /dev/null @@ -1,30 +0,0 @@ -syntax = "proto3"; - -package envoy.extensions.uri_template; - -import "udpa/annotations/status.proto"; - -option java_package = "io.envoyproxy.envoy.extensions.uri_template"; -option java_outer_classname = "UriTemplateRewrite"; -option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/url_template/url_template"; -option (udpa.annotations.file_status).package_version_status = ACTIVE; - -// TODO(silverstar194): Convert proto to struct -// Holds the segments for rewriting urls base on uri pattern templates -message RewriteSegments { - message RewriteSegment { - oneof segment_type { - // Represents a segment of the rewritten URL, including any path segments, - // slash and prefix. - string literal = 1; - - // Represents an index into the RE2 capture which value should be used - // to construct the rewritten URL. Note that the index should be greater - // than 0 as 0 index into the whole match RE2 pattern. - int32 capture_index = 2; - } - } - - repeated RewriteSegment segments = 3; -} diff --git a/source/extensions/path/uri_template_lib/uri_template.cc b/source/extensions/path/uri_template_lib/uri_template.cc index 77c87ba461181..e59b9403352c0 100644 --- a/source/extensions/path/uri_template_lib/uri_template.cc +++ b/source/extensions/path/uri_template_lib/uri_template.cc @@ -6,7 +6,6 @@ #include #include "source/common/http/path_utility.h" -#include "source/extensions/path/uri_template_lib/proto/rewrite_segments.pb.h" #include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "absl/status/statusor.h" @@ -78,9 +77,9 @@ absl::StatusOr> parseRewritePattern(absl::string_view return result; } -absl::StatusOr -parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) { - envoy::extensions::uri_template::RewriteSegments parsed_pattern; +absl::StatusOr parseRewritePattern(absl::string_view pattern, + absl::string_view capture_regex) { + RewriteSegments parsed_pattern; RE2 regex = RE2(Internal::toStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); @@ -97,14 +96,14 @@ parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex) for (const auto& [str, kind] : processed_pattern) { switch (kind) { case RewriteStringKind::Literal: - parsed_pattern.add_segments()->set_literal(std::string(str)); + parsed_pattern.segments.push_back(absl::variant(std::string(str))); break; case RewriteStringKind::Variable: auto it = capture_index_map.find(std::string(str)); if (it == capture_index_map.end()) { return absl::InvalidArgumentError("Nonexisting variable name"); } - parsed_pattern.add_segments()->set_capture_index(it->second); + parsed_pattern.segments.push_back(absl::variant(it->second)); break; } } @@ -128,9 +127,9 @@ absl::Status isValidSharedVariableSet(absl::string_view pattern, absl::string_vi return parseRewritePattern(pattern, *std::move(status)).status(); } -absl::StatusOr rewritePathTemplatePattern( - absl::string_view path, absl::string_view capture_regex, - const envoy::extensions::uri_template::RewriteSegments& rewrite_pattern) { +absl::StatusOr rewritePathTemplatePattern(absl::string_view path, + absl::string_view capture_regex, + const RewriteSegments& rewrite_pattern) { RE2 regex = RE2(Internal::toStringPiece(capture_regex)); if (!regex.ok()) { return absl::InternalError(regex.error()); @@ -145,16 +144,16 @@ absl::StatusOr rewritePathTemplatePattern( } std::string rewritten_path; - for (const envoy::extensions::uri_template::RewriteSegments::RewriteSegment& segment : - rewrite_pattern.segments()) { - if (segment.has_literal()) { - absl::StrAppend(&rewritten_path, segment.literal()); - } else if (segment.has_capture_index()) { - if (segment.capture_index() < 1 || segment.capture_index() >= capture_num) { + for (absl::variant segment : rewrite_pattern.segments) { + auto* literal = absl::get_if(&segment); + auto* capture_index = absl::get_if(&segment); + if (literal != nullptr) { + absl::StrAppend(&rewritten_path, *literal); + } else if (capture_index != nullptr) { + if (*capture_index < 1 || *capture_index >= capture_num) { return absl::InvalidArgumentError("Invalid variable index"); } - absl::StrAppend(&rewritten_path, - absl::string_view(captures[segment.capture_index()].as_string())); + absl::StrAppend(&rewritten_path, absl::string_view(captures[*capture_index].as_string())); } } diff --git a/source/extensions/path/uri_template_lib/uri_template.h b/source/extensions/path/uri_template_lib/uri_template.h index 5064569bb867b..3c6714acae189 100644 --- a/source/extensions/path/uri_template_lib/uri_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -2,7 +2,6 @@ #include -#include "source/extensions/path/uri_template_lib/proto/rewrite_segments.pb.h" #include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "absl/status/statusor.h" @@ -29,6 +28,10 @@ struct ParsedSegment { RewriteStringKind kind_; }; +struct RewriteSegments { + std::vector> segments; +}; + /** * Returns the safe regex that Envoy understands that is equivalent to the given pattern. */ @@ -43,8 +46,8 @@ absl::StatusOr> parseRewritePattern(absl::string_view /** * Returns the parsed path rewrite pattern and processes variables. */ -absl::StatusOr -parseRewritePattern(absl::string_view pattern, absl::string_view capture_regex); +absl::StatusOr parseRewritePattern(absl::string_view pattern, + absl::string_view capture_regex); /** * Returns true if provided rewrite pattern is valid. @@ -74,9 +77,9 @@ absl::Status isValidMatchPattern(absl::string_view match_pattern); * capture_regex: "(1)/(2)" * Rewrite would result in rewrite of "/var/cat". */ -absl::StatusOr -rewritePathTemplatePattern(absl::string_view path, absl::string_view capture_regex, - const envoy::extensions::uri_template::RewriteSegments& rewrite_pattern); +absl::StatusOr rewritePathTemplatePattern(absl::string_view path, + absl::string_view capture_regex, + const RewriteSegments& rewrite_pattern); } // namespace UriTemplate } // namespace Extensions diff --git a/test/extensions/path/uri_template_lib/BUILD b/test/extensions/path/uri_template_lib/BUILD index 4c175eaa40333..87eda04edce71 100644 --- a/test/extensions/path/uri_template_lib/BUILD +++ b/test/extensions/path/uri_template_lib/BUILD @@ -15,7 +15,6 @@ envoy_cc_test( srcs = ["uri_template_test.cc"], deps = [ "//source/extensions/path/uri_template_lib", - "//source/extensions/path/uri_template_lib/proto:uri_template_rewrite_segements_cc_proto", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", @@ -28,7 +27,6 @@ envoy_cc_test( srcs = ["uri_template_internal_test.cc"], deps = [ "//source/extensions/path/uri_template_lib:uri_template_internal_cc", - "//source/extensions/path/uri_template_lib/proto:uri_template_rewrite_segements_cc_proto", "//test/test_common:status_utility_lib", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", diff --git a/test/extensions/path/uri_template_lib/uri_template_test.cc b/test/extensions/path/uri_template_lib/uri_template_test.cc index f7438e537993b..c8693c60fa016 100644 --- a/test/extensions/path/uri_template_lib/uri_template_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_test.cc @@ -84,12 +84,14 @@ TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { EXPECT_THAT(parseRewritePattern(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); } -class ParseRewriteSuccess : public testing::TestWithParam> { +class ParseRewriteSuccess + : public testing::TestWithParam< + std::pair>>> { protected: const std::string& rewritePattern() const { return std::get<0>(GetParam()); } - envoy::extensions::uri_template::RewriteSegments expectedProto() const { - envoy::extensions::uri_template::RewriteSegments expected_proto; - Envoy::TestUtility::loadFromYaml(std::get<1>(GetParam()), expected_proto); + RewriteSegments expectedProto() const { + RewriteSegments expected_proto; + expected_proto.segments = std::get<1>(GetParam()); return expected_proto; } }; @@ -98,58 +100,53 @@ TEST(ParseRewrite, InvalidRegex) { EXPECT_THAT(parseRewritePattern("/{var1}", "+[abc"), StatusIs(absl::StatusCode::kInternal)); } -INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, - testing::ValuesIn(std::vector>({ - {"/static", R"EOF(segments: {literal: "/static"} )EOF"}, - {"/{var1}", R"EOF(segments: - - literal: "/" - - capture_index: 1)EOF"}, - {"/{var1}", R"EOF(segments: - - literal: "/" - - capture_index: 1)EOF"}, - {"/{var1}/{var1}/{var1}", R"EOF(segments: - - literal: "/" - - capture_index: 1 - - literal: "/" - - capture_index: 1 - - literal: "/" - - capture_index: 1)EOF"}, - {"/{var3}/{var1}/{var2}", R"EOF(segments - - literal: "/" - - capture_index: 3 - - literal: "/" - - capture_index: 1 - - literal: "/" - - capture_index: 2)EOF"}, - {"/{var3}/abc/def/{var2}.suffix", R"EOF(segments: - - literal: "/" - - capture_index: 3 - - literal: "/abc/def/" - - capture_index: 2 - - literal: ".suffix")EOF"}, - {"/abc/{var1}/{var2}/def", R"EOF(segments - - literal: "/abc/" - - capture_index: 1 - - literal: "/" - - capture_index: 2 - - literal: "/def")EOF"}, - {"/{var1}{var2}", R"EOF(segments - - literal: "/" - - capture_index: 1 - - ar_index: 2)EOF"}, - {"/{var1}-{var2}/bucket-{var3}.suffix", R"EOF(segments - - literal: "/" - - capture_index: 1 - - literal: "-" - - capture_index: 2 - - literal: "/bucket-" - - capture_index: 3 - - literal: ".suffix")EOF"}, - }))); +INSTANTIATE_TEST_SUITE_P( + ParseRewriteSuccessTestSuite, ParseRewriteSuccess, + testing::ValuesIn( + std::vector>>>({ + {"/static", + std::vector>{ + absl::variant("/static")}}, + {"/{var1}", + std::vector>{absl::variant("/"), + absl::variant(1)}}, + {"/{var1}", + std::vector>{absl::variant("/"), + absl::variant(1)}}, + {"/{var1}/{var1}/{var1}", + std::vector>{ + absl::variant("/"), absl::variant(1), + absl::variant("/"), absl::variant(1), + absl::variant("/"), absl::variant(1)}}, + {"/{var3}/{var1}/{var2}", + std::vector>{ + absl::variant("/"), absl::variant(3), + absl::variant("/"), absl::variant(1), + absl::variant("/"), absl::variant(2)}}, + {"/{var3}/abc/def/{var2}.suffix", + std::vector>{ + absl::variant("/"), absl::variant(3), + absl::variant("/abc/def/"), absl::variant(2), + absl::variant(".suffix")}}, + {"/abc/{var1}/{var2}/def", + std::vector>{ + absl::variant("/abc/"), absl::variant(1), + absl::variant("/"), absl::variant(2), + absl::variant("/def")}}, + {"/{var1}/{var2}", + std::vector>{absl::variant("/"), + absl::variant(1), + absl::variant(2)}}, + {"/{var1}-{var2}/bucket-{var3}.suffix", + std::vector>{ + absl::variant("/"), absl::variant(1), + absl::variant("-"), absl::variant(2), + absl::variant("/bucket-"), absl::variant(3), + absl::variant(".suffix")}}, + }))); TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { - absl::StatusOr rewrite = - parseRewritePattern(rewritePattern(), kCaptureRegex); + absl::StatusOr rewrite = parseRewritePattern(rewritePattern(), kCaptureRegex); ASSERT_OK(rewrite); } @@ -168,64 +165,56 @@ TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { } class RewritePathTemplateSuccess - : public testing::TestWithParam> { + : public testing::TestWithParam< + std::pair>, std::string>> { protected: - envoy::extensions::uri_template::RewriteSegments rewriteProto() const { - envoy::extensions::uri_template::RewriteSegments proto; - Envoy::TestUtility::loadFromYaml(std::get<0>(GetParam()), proto); + RewriteSegments rewriteProto() const { + RewriteSegments proto; + proto.segments = std::get<0>(GetParam()); return proto; } + const std::string& expectedRewrittenPath() const { return std::get<1>(GetParam()); } }; -INSTANTIATE_TEST_SUITE_P(RewritePathTemplateSuccessTestSuite, RewritePathTemplateSuccess, - testing::ValuesIn(std::vector>( - {{R"EOF(segments: { literal: "/static" })EOF", "/static"}, - {R"EOF(segments: - - literal: "/" - - capture_index: 1)EOF", - "/val1"}, - {R"EOF(segments: - - literal: "/" - - capture_index: 1)EOF", - "/val1"}, - {R"EOF(segments: - - literal: "/" - - capture_index: 1 - - literal: "/" - - capture_index: 1 - - literal: "/" - - capture_index: 1)EOF", - "/val1/val1/val1"}, - {R"EOF(segments: - - literal: "/" - - capture_index: 3 - - literal: "/" - - capture_index: 1 - - literal: "/" - - capture_index: 2)EOF", - "/val3/val1/val2"}, - {R"EOF(segments: - - literal: "/" - - capture_index: 3 - - literal: "/abc/def/" - - capture_index: 2 - - literal: ".suffix")EOF", - "/val3/abc/def/val2.suffix"}, - {R"EOF(segments: - - literal: "/" - - capture_index: 3 - - capture_index: 2 - - literal: "." - - capture_index: 1)EOF", - "/val3val2.val1"}, - {R"EOF(segments: - - literal: "/abc/" - - capture_index: 1 - - literal: "/" - - capture_index: 5 - - literal: "/def")EOF", - "/abc/val1/val5/def"}}))); +INSTANTIATE_TEST_SUITE_P( + RewritePathTemplateSuccessTestSuite, RewritePathTemplateSuccess, + testing::ValuesIn( + std::vector>, std::string>>( + {{std::vector>{ + absl::variant("/static")}, + "/static"}, + {std::vector>{absl::variant("/"), + absl::variant(1)}, + "/val1"}, + {std::vector>{absl::variant("/"), + absl::variant(1)}, + "/val1"}, + {std::vector>{ + absl::variant("/"), absl::variant(1), + absl::variant("/"), absl::variant(1), + absl::variant("/"), absl::variant(1)}, + "/val1/val1/val1"}, + {std::vector>{ + absl::variant("/"), absl::variant(3), + absl::variant("/"), absl::variant(1), + absl::variant("/"), absl::variant(2)}, + "/val3/val1/val2"}, + {std::vector>{ + absl::variant("/"), absl::variant(3), + absl::variant("/abc/def/"), absl::variant(2), + absl::variant(".suffix")}, + "/val3/abc/def/val2.suffix"}, + {std::vector>{ + absl::variant("/"), absl::variant(3), + absl::variant(2), absl::variant("."), + absl::variant(1)}, + "/val3val2.val1"}, + {std::vector>{ + absl::variant("/abc/"), absl::variant(1), + absl::variant("/"), absl::variant(5), + absl::variant("/def")}, + "/abc/val1/val5/def"}}))); TEST_P(RewritePathTemplateSuccess, RewritePathTemplateSuccessTest) { absl::StatusOr rewritten_path = @@ -235,59 +224,36 @@ TEST_P(RewritePathTemplateSuccess, RewritePathTemplateSuccessTest) { } TEST(RewritePathTemplateFailure, BadRegex) { - envoy::extensions::uri_template::RewriteSegments rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- capture_index: 1 - )EOF"; - - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + RewriteSegments rewrite_proto; + rewrite_proto.segments = std::vector>{ + absl::variant("/static"), absl::variant(1)}; EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, "+/bad_regex", rewrite_proto), StatusIs(absl::StatusCode::kInternal)); } TEST(RewritePathTemplateFailure, RegexNoMatch) { - envoy::extensions::uri_template::RewriteSegments rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- capture_index: 1 - )EOF"; - - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + RewriteSegments rewrite_proto; + rewrite_proto.segments = std::vector>{ + absl::variant("/"), absl::variant(1)}; EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, "/no_match_regex", rewrite_proto), StatusIs(absl::StatusCode::kInvalidArgument)); } TEST(RewritePathTemplateFailure, RegexCaptureIndexZero) { - envoy::extensions::uri_template::RewriteSegments rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- capture_index: 0 - )EOF"; - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + RewriteSegments rewrite_proto; + rewrite_proto.segments = std::vector>{ + absl::variant("/"), absl::variant(0)}; EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewrite_proto), StatusIs(absl::StatusCode::kInvalidArgument)); } TEST(RewritePathTemplateFailure, RegexCaptureIndexAboveMaxCapture) { - envoy::extensions::uri_template::RewriteSegments rewrite_proto; - - const std::string yaml = R"EOF( -segments: -- literal: "/" -- capture_index: 6 - )EOF"; - - Envoy::TestUtility::loadFromYaml(yaml, rewrite_proto); + RewriteSegments rewrite_proto; + rewrite_proto.segments = std::vector>{ + absl::variant("/"), absl::variant(6)}; EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewrite_proto), StatusIs(absl::StatusCode::kInvalidArgument)); @@ -320,7 +286,7 @@ TEST_P(PathPatternMatchAndRewrite, PathPatternMatchAndRewriteTest) { absl::StatusOr regex = convertPathPatternSyntaxToRegex(pattern()); ASSERT_OK(regex); - absl::StatusOr rewrite_proto = + absl::StatusOr rewrite_proto = parseRewritePattern(rewritePattern(), regex.value()); ASSERT_OK(rewrite_proto); From c2b24480658150932470214e1a34256ccf8f0636 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 1 Sep 2022 14:35:11 +0000 Subject: [PATCH 201/238] Rename types Signed-off-by: silverstar195 --- .../path/uri_template_lib/uri_template.cc | 6 +- .../path/uri_template_lib/uri_template.h | 5 +- .../uri_template_lib/uri_template_test.cc | 171 ++++++------------ 3 files changed, 63 insertions(+), 119 deletions(-) diff --git a/source/extensions/path/uri_template_lib/uri_template.cc b/source/extensions/path/uri_template_lib/uri_template.cc index e59b9403352c0..0e2b5bf3bc9f6 100644 --- a/source/extensions/path/uri_template_lib/uri_template.cc +++ b/source/extensions/path/uri_template_lib/uri_template.cc @@ -96,14 +96,14 @@ absl::StatusOr parseRewritePattern(absl::string_view pattern, for (const auto& [str, kind] : processed_pattern) { switch (kind) { case RewriteStringKind::Literal: - parsed_pattern.segments.push_back(absl::variant(std::string(str))); + parsed_pattern.push_back(RewriteSegment(std::string(str))); break; case RewriteStringKind::Variable: auto it = capture_index_map.find(std::string(str)); if (it == capture_index_map.end()) { return absl::InvalidArgumentError("Nonexisting variable name"); } - parsed_pattern.segments.push_back(absl::variant(it->second)); + parsed_pattern.push_back(RewriteSegment(it->second)); break; } } @@ -144,7 +144,7 @@ absl::StatusOr rewritePathTemplatePattern(absl::string_view path, } std::string rewritten_path; - for (absl::variant segment : rewrite_pattern.segments) { + for (RewriteSegment segment : rewrite_pattern) { auto* literal = absl::get_if(&segment); auto* capture_index = absl::get_if(&segment); if (literal != nullptr) { diff --git a/source/extensions/path/uri_template_lib/uri_template.h b/source/extensions/path/uri_template_lib/uri_template.h index 3c6714acae189..2c36784c40522 100644 --- a/source/extensions/path/uri_template_lib/uri_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -28,9 +28,8 @@ struct ParsedSegment { RewriteStringKind kind_; }; -struct RewriteSegments { - std::vector> segments; -}; +using RewriteSegment = absl::variant; +using RewriteSegments = std::vector; /** * Returns the safe regex that Envoy understands that is equivalent to the given pattern. diff --git a/test/extensions/path/uri_template_lib/uri_template_test.cc b/test/extensions/path/uri_template_lib/uri_template_test.cc index c8693c60fa016..5ec4ad38dff67 100644 --- a/test/extensions/path/uri_template_lib/uri_template_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_test.cc @@ -86,14 +86,10 @@ TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { class ParseRewriteSuccess : public testing::TestWithParam< - std::pair>>> { + std::pair> { protected: const std::string& rewritePattern() const { return std::get<0>(GetParam()); } - RewriteSegments expectedProto() const { - RewriteSegments expected_proto; - expected_proto.segments = std::get<1>(GetParam()); - return expected_proto; - } + RewriteSegments expectedSegments() const { return std::get<1>(GetParam()); } }; TEST(ParseRewrite, InvalidRegex) { @@ -102,48 +98,29 @@ TEST(ParseRewrite, InvalidRegex) { INSTANTIATE_TEST_SUITE_P( ParseRewriteSuccessTestSuite, ParseRewriteSuccess, - testing::ValuesIn( - std::vector>>>({ - {"/static", - std::vector>{ - absl::variant("/static")}}, - {"/{var1}", - std::vector>{absl::variant("/"), - absl::variant(1)}}, - {"/{var1}", - std::vector>{absl::variant("/"), - absl::variant(1)}}, - {"/{var1}/{var1}/{var1}", - std::vector>{ - absl::variant("/"), absl::variant(1), - absl::variant("/"), absl::variant(1), - absl::variant("/"), absl::variant(1)}}, - {"/{var3}/{var1}/{var2}", - std::vector>{ - absl::variant("/"), absl::variant(3), - absl::variant("/"), absl::variant(1), - absl::variant("/"), absl::variant(2)}}, - {"/{var3}/abc/def/{var2}.suffix", - std::vector>{ - absl::variant("/"), absl::variant(3), - absl::variant("/abc/def/"), absl::variant(2), - absl::variant(".suffix")}}, - {"/abc/{var1}/{var2}/def", - std::vector>{ - absl::variant("/abc/"), absl::variant(1), - absl::variant("/"), absl::variant(2), - absl::variant("/def")}}, - {"/{var1}/{var2}", - std::vector>{absl::variant("/"), - absl::variant(1), - absl::variant(2)}}, - {"/{var1}-{var2}/bucket-{var3}.suffix", - std::vector>{ - absl::variant("/"), absl::variant(1), - absl::variant("-"), absl::variant(2), - absl::variant("/bucket-"), absl::variant(3), - absl::variant(".suffix")}}, - }))); + testing::ValuesIn(std::vector>({ + {"/static", RewriteSegments({RewriteSegment("/static")})}, + {"/{var1}", RewriteSegments({RewriteSegment("/"), RewriteSegment(1)})}, + {"/{var1}", RewriteSegments({RewriteSegment("/"), RewriteSegment(1)})}, + {"/{var1}/{var1}/{var1}", + RewriteSegments({RewriteSegment("/"), RewriteSegment(1), RewriteSegment("/"), + RewriteSegment(1), RewriteSegment("/"), RewriteSegment(1)})}, + {"/{var3}/{var1}/{var2}", + RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/"), + RewriteSegment(1), RewriteSegment("/"), RewriteSegment(2)})}, + {"/{var3}/abc/def/{var2}.suffix", + RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/abc/def/"), + RewriteSegment(2), RewriteSegment(".suffix")})}, + {"/abc/{var1}/{var2}/def", + RewriteSegments({RewriteSegment("/abc/"), RewriteSegment(1), RewriteSegment("/"), + RewriteSegment(2), RewriteSegment("def")})}, + {"/{var1}/{var2}", RewriteSegments({RewriteSegment("/"), RewriteSegment(1), + RewriteSegment(1), RewriteSegment(2)})}, + {"/{var1}-{var2}/bucket-{var3}.suffix", + RewriteSegments({RewriteSegment("/"), RewriteSegment(1), RewriteSegment("-"), + RewriteSegment(2), RewriteSegment("/bucket-"), RewriteSegment(3), + RewriteSegment(".suffix")})}, + }))); TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { absl::StatusOr rewrite = parseRewritePattern(rewritePattern(), kCaptureRegex); @@ -166,13 +143,9 @@ TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { class RewritePathTemplateSuccess : public testing::TestWithParam< - std::pair>, std::string>> { + std::pair> { protected: - RewriteSegments rewriteProto() const { - RewriteSegments proto; - proto.segments = std::get<0>(GetParam()); - return proto; - } + RewriteSegments rewriteSegments() const { return std::get<0>(GetParam()); } const std::string& expectedRewrittenPath() const { return std::get<1>(GetParam()); } }; @@ -180,82 +153,54 @@ class RewritePathTemplateSuccess INSTANTIATE_TEST_SUITE_P( RewritePathTemplateSuccessTestSuite, RewritePathTemplateSuccess, testing::ValuesIn( - std::vector>, std::string>>( - {{std::vector>{ - absl::variant("/static")}, - "/static"}, - {std::vector>{absl::variant("/"), - absl::variant(1)}, - "/val1"}, - {std::vector>{absl::variant("/"), - absl::variant(1)}, - "/val1"}, - {std::vector>{ - absl::variant("/"), absl::variant(1), - absl::variant("/"), absl::variant(1), - absl::variant("/"), absl::variant(1)}, - "/val1/val1/val1"}, - {std::vector>{ - absl::variant("/"), absl::variant(3), - absl::variant("/"), absl::variant(1), - absl::variant("/"), absl::variant(2)}, - "/val3/val1/val2"}, - {std::vector>{ - absl::variant("/"), absl::variant(3), - absl::variant("/abc/def/"), absl::variant(2), - absl::variant(".suffix")}, - "/val3/abc/def/val2.suffix"}, - {std::vector>{ - absl::variant("/"), absl::variant(3), - absl::variant(2), absl::variant("."), - absl::variant(1)}, - "/val3val2.val1"}, - {std::vector>{ - absl::variant("/abc/"), absl::variant(1), - absl::variant("/"), absl::variant(5), - absl::variant("/def")}, - "/abc/val1/val5/def"}}))); + std::vector>({ + {RewriteSegments({RewriteSegment("/static")}), "/static"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(1)}), "/val1"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(1)}), "/val1"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(1), RewriteSegment("/"), + RewriteSegment(1), RewriteSegment("/"), RewriteSegment(1)}), + "/val1/val1/val1"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/"), + RewriteSegment(1), RewriteSegment("/"), RewriteSegment(2)}), + "/val3/val1/val2"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/abc/def/"), + RewriteSegment(2), RewriteSegment(".suffix")}), + "/val3/abc/def/val2.suffix"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment(2), + RewriteSegment("."), RewriteSegment(1)}), + "/val3val2.val1"}, + {RewriteSegments({RewriteSegment("/abc/"), RewriteSegment(1), RewriteSegment("/"), + RewriteSegment(5), RewriteSegment("/def")}), + "/abc/val1/val5/def"}}))); TEST_P(RewritePathTemplateSuccess, RewritePathTemplateSuccessTest) { absl::StatusOr rewritten_path = - rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewriteProto()); + rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewriteSegments()); ASSERT_OK(rewritten_path); EXPECT_EQ(rewritten_path.value(), expectedRewrittenPath()); } TEST(RewritePathTemplateFailure, BadRegex) { - RewriteSegments rewrite_proto; - rewrite_proto.segments = std::vector>{ - absl::variant("/static"), absl::variant(1)}; - - EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, "+/bad_regex", rewrite_proto), + RewriteSegments rewrite_segments({RewriteSegment("static"), RewriteSegment(1)}); + EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, "+/bad_regex", rewrite_segments), StatusIs(absl::StatusCode::kInternal)); } TEST(RewritePathTemplateFailure, RegexNoMatch) { - RewriteSegments rewrite_proto; - rewrite_proto.segments = std::vector>{ - absl::variant("/"), absl::variant(1)}; - - EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, "/no_match_regex", rewrite_proto), + RewriteSegments rewrite_segments({RewriteSegment("/"), RewriteSegment(1)}); + EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, "/no_match_regex", rewrite_segments), StatusIs(absl::StatusCode::kInvalidArgument)); } TEST(RewritePathTemplateFailure, RegexCaptureIndexZero) { - RewriteSegments rewrite_proto; - rewrite_proto.segments = std::vector>{ - absl::variant("/"), absl::variant(0)}; - - EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewrite_proto), + RewriteSegments rewrite_segments({RewriteSegment("/"), RewriteSegment(0)}); + EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewrite_segments), StatusIs(absl::StatusCode::kInvalidArgument)); } TEST(RewritePathTemplateFailure, RegexCaptureIndexAboveMaxCapture) { - RewriteSegments rewrite_proto; - rewrite_proto.segments = std::vector>{ - absl::variant("/"), absl::variant(6)}; - - EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewrite_proto), + RewriteSegments rewrite_segments({RewriteSegment("/"), RewriteSegment(6)}); + EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewrite_segments), StatusIs(absl::StatusCode::kInvalidArgument)); } @@ -286,12 +231,12 @@ TEST_P(PathPatternMatchAndRewrite, PathPatternMatchAndRewriteTest) { absl::StatusOr regex = convertPathPatternSyntaxToRegex(pattern()); ASSERT_OK(regex); - absl::StatusOr rewrite_proto = + absl::StatusOr rewrite_segment = parseRewritePattern(rewritePattern(), regex.value()); - ASSERT_OK(rewrite_proto); + ASSERT_OK(rewrite_segment); absl::StatusOr rewritten_path = - rewritePathTemplatePattern(matchPath(), regex.value(), rewrite_proto.value()); + rewritePathTemplatePattern(matchPath(), regex.value(), rewrite_segment.value()); ASSERT_OK(rewritten_path); EXPECT_EQ(rewritten_path.value(), expectedRewrittenPath()); From b8482e64256d2a5688a53eabb6bbeb14aefd3c4d Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 1 Sep 2022 14:40:42 +0000 Subject: [PATCH 202/238] Format Signed-off-by: silverstar195 --- .../path/uri_template_lib/uri_template.h | 3 ++ .../uri_template_lib/uri_template_test.cc | 46 +++++++++---------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/source/extensions/path/uri_template_lib/uri_template.h b/source/extensions/path/uri_template_lib/uri_template.h index 2c36784c40522..e620da60f775d 100644 --- a/source/extensions/path/uri_template_lib/uri_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -28,7 +28,10 @@ struct ParsedSegment { RewriteStringKind kind_; }; +// Stores string literals and regex capture indexes for rewriting paths using RewriteSegment = absl::variant; + +// Stores all segments in left to right order for a path rewrite using RewriteSegments = std::vector; /** diff --git a/test/extensions/path/uri_template_lib/uri_template_test.cc b/test/extensions/path/uri_template_lib/uri_template_test.cc index 5ec4ad38dff67..69ad814643785 100644 --- a/test/extensions/path/uri_template_lib/uri_template_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_test.cc @@ -84,9 +84,7 @@ TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { EXPECT_THAT(parseRewritePattern(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); } -class ParseRewriteSuccess - : public testing::TestWithParam< - std::pair> { +class ParseRewriteSuccess : public testing::TestWithParam> { protected: const std::string& rewritePattern() const { return std::get<0>(GetParam()); } RewriteSegments expectedSegments() const { return std::get<1>(GetParam()); } @@ -142,8 +140,7 @@ TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { } class RewritePathTemplateSuccess - : public testing::TestWithParam< - std::pair> { + : public testing::TestWithParam> { protected: RewriteSegments rewriteSegments() const { return std::get<0>(GetParam()); } @@ -152,26 +149,25 @@ class RewritePathTemplateSuccess INSTANTIATE_TEST_SUITE_P( RewritePathTemplateSuccessTestSuite, RewritePathTemplateSuccess, - testing::ValuesIn( - std::vector>({ - {RewriteSegments({RewriteSegment("/static")}), "/static"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(1)}), "/val1"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(1)}), "/val1"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(1), RewriteSegment("/"), - RewriteSegment(1), RewriteSegment("/"), RewriteSegment(1)}), - "/val1/val1/val1"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/"), - RewriteSegment(1), RewriteSegment("/"), RewriteSegment(2)}), - "/val3/val1/val2"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/abc/def/"), - RewriteSegment(2), RewriteSegment(".suffix")}), - "/val3/abc/def/val2.suffix"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment(2), - RewriteSegment("."), RewriteSegment(1)}), - "/val3val2.val1"}, - {RewriteSegments({RewriteSegment("/abc/"), RewriteSegment(1), RewriteSegment("/"), - RewriteSegment(5), RewriteSegment("/def")}), - "/abc/val1/val5/def"}}))); + testing::ValuesIn(std::vector>( + {{RewriteSegments({RewriteSegment("/static")}), "/static"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(1)}), "/val1"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(1)}), "/val1"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(1), RewriteSegment("/"), + RewriteSegment(1), RewriteSegment("/"), RewriteSegment(1)}), + "/val1/val1/val1"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/"), + RewriteSegment(1), RewriteSegment("/"), RewriteSegment(2)}), + "/val3/val1/val2"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/abc/def/"), + RewriteSegment(2), RewriteSegment(".suffix")}), + "/val3/abc/def/val2.suffix"}, + {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment(2), + RewriteSegment("."), RewriteSegment(1)}), + "/val3val2.val1"}, + {RewriteSegments({RewriteSegment("/abc/"), RewriteSegment(1), RewriteSegment("/"), + RewriteSegment(5), RewriteSegment("/def")}), + "/abc/val1/val5/def"}}))); TEST_P(RewritePathTemplateSuccess, RewritePathTemplateSuccessTest) { absl::StatusOr rewritten_path = From 257702d3b051f3a85fb9829b78402825c1e546fe Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 1 Sep 2022 14:53:27 +0000 Subject: [PATCH 203/238] Clean up merge issues Signed-off-by: silverstar195 --- CODEOWNERS | 4 ---- .../match/pattern_template/v3/pattern_template_match.proto | 1 - .../pattern_template/v3/pattern_template_rewrite.proto | 1 - 3 files changed, 6 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index c4b306f63dbd4..41131b8ea666c 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -270,10 +270,6 @@ extensions/filters/http/oauth2 @derekargueta @snowp # path rewrite by pattern /*/extensions/path/rewrite/pattern_template @alyssawilk @yanjunxiang-google -# URL Pattern Match and Rewrite Library -/*/extensions/path/pattern_template_lib @alyssawilk @yanjunxiang-google -/*/extensions/path/pattern_template_lib/proto @alyssawilk @yanjunxiang-google - # Intentionally exempt (treated as core code) /*/extensions/filters/common @UNOWNED @UNOWNED /*/extensions/filters/http/common @UNOWNED @UNOWNED diff --git a/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto index d5bff9eb62ab6..730f574495e39 100644 --- a/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto @@ -39,7 +39,6 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` // [#extension: envoy.path.match.pattern_template.pattern_template_matcher] -// [#not-implemented-hide:] message PatternTemplateMatchConfig { string path_template = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; } diff --git a/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto index 0019c4546092e..cd835706b341d 100644 --- a/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto @@ -52,7 +52,6 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // * The path pattern ``/content/{format}/{lang}/{id}/{file}.vtt`` paired with a substitution // string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` // into ``/en-us/hls/en_193913.vtt``. -// [#not-implemented-hide:] // [#extension: envoy.path.rewrite.pattern_template.pattern_template_rewriter] message PatternTemplateRewriteConfig { string path_template_rewrite = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; From 6e03162c0806281cad242c327e564c77d991cc9d Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 1 Sep 2022 14:59:33 +0000 Subject: [PATCH 204/238] Change comment Signed-off-by: silverstar195 --- envoy/router/path_match.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/envoy/router/path_match.h b/envoy/router/path_match.h index d98e4e59f35b7..2d6cd09c860ba 100644 --- a/envoy/router/path_match.h +++ b/envoy/router/path_match.h @@ -22,8 +22,8 @@ class PathMatcher : Logger::Loggable { /** * Determines if the current path matches the pattern. * - * @param url current url of route - * @return true if route url matches the predicate pattern. + * @param path current path of the matchable route + * @return true if path matches the pattern. */ virtual bool match(absl::string_view path) const PURE; From 6173aaf7e3fc0d4094c483432b62dfe486147d36 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 1 Sep 2022 15:34:25 +0000 Subject: [PATCH 205/238] Remove unused method rewritePathTemplatePattern Signed-off-by: silverstar195 --- .../pattern_template_rewrite.cc | 20 ++-- .../pattern_template_rewrite.h | 8 ++ .../path/uri_template_lib/uri_template.cc | 33 ------ .../path/uri_template_lib/uri_template.h | 12 -- .../uri_template_lib/uri_template_test.cc | 103 ++---------------- 5 files changed, 25 insertions(+), 151 deletions(-) diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index 6069d33e4dc45..296f78fc73224 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -7,7 +7,6 @@ #include "source/common/http/path_utility.h" #include "source/extensions/path/match/pattern_template/pattern_template_match.h" -#include "source/extensions/path/uri_template_lib/proto/rewrite_segments.pb.h" #include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "absl/status/statusor.h" @@ -54,14 +53,14 @@ PatternTemplateRewriter::rewritePath(absl::string_view pattern, } std::string regex_pattern_str = *std::move(regex_pattern); - absl::StatusOr rewrite_pattern = + absl::StatusOr rewrite_pattern = parseRewritePattern(rewrite_pattern_, regex_pattern_str); if (!rewrite_pattern.ok()) { return absl::InvalidArgumentError("Unable to parse path rewrite pattern"); } - const envoy::extensions::uri_template::RewriteSegments& rewrite_pattern_proto = + const RewriteSegments& rewrite_pattern_segments = *std::move(rewrite_pattern); RE2 regex = RE2(Internal::toStringPiece(regex_pattern_str)); if (!regex.ok()) { @@ -77,15 +76,16 @@ PatternTemplateRewriter::rewritePath(absl::string_view pattern, } std::string new_path; - for (const envoy::extensions::uri_template::RewriteSegments::RewriteSegment& segment : - rewrite_pattern_proto.segments()) { - if (segment.has_literal()) { - absl::StrAppend(&new_path, segment.literal()); - } else if (segment.has_capture_index()) { - if (segment.capture_index() < 1 || segment.capture_index() >= capture_num) { + for (const RewriteSegment& segment : rewrite_pattern_segments) { + auto* literal = absl::get_if(&segment); + auto* capture_index = absl::get_if(&segment); + if (literal != nullptr) { + absl::StrAppend(&new_path, *literal); + } else if (capture_index != nullptr) { + if (*capture_index < 1 || *capture_index >= capture_num) { return absl::InvalidArgumentError("Invalid variable index"); } - absl::StrAppend(&new_path, absl::string_view(captures[segment.capture_index()].as_string())); + absl::StrAppend(&new_path, absl::string_view(captures[*capture_index].as_string())); } } diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index af90c0c2215bd..8c949ff7afec0 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -43,6 +43,14 @@ class PatternTemplateRewriter : public Router::PathRewriter { // Router::PathRewriter absl::string_view pattern() const override { return rewrite_pattern_; } + /** + * Concatenates literals and extracts variable values to form the final rewritten path. + * For example: + * rewrite_pattern: [capture_index=2, literal="cat"] + * path: "/bar/var" + * capture_regex: "(1)/(2)" + * Rewrite would result in rewrite of "/var/cat". + */ absl::StatusOr rewritePath(absl::string_view pattern, absl::string_view matched_path) const override; diff --git a/source/extensions/path/uri_template_lib/uri_template.cc b/source/extensions/path/uri_template_lib/uri_template.cc index 0e2b5bf3bc9f6..af9f3d3e1a594 100644 --- a/source/extensions/path/uri_template_lib/uri_template.cc +++ b/source/extensions/path/uri_template_lib/uri_template.cc @@ -127,39 +127,6 @@ absl::Status isValidSharedVariableSet(absl::string_view pattern, absl::string_vi return parseRewritePattern(pattern, *std::move(status)).status(); } -absl::StatusOr rewritePathTemplatePattern(absl::string_view path, - absl::string_view capture_regex, - const RewriteSegments& rewrite_pattern) { - RE2 regex = RE2(Internal::toStringPiece(capture_regex)); - if (!regex.ok()) { - return absl::InternalError(regex.error()); - } - - // First capture is the whole matched regex pattern. - int capture_num = regex.NumberOfCapturingGroups() + 1; - std::vector captures(capture_num); - if (!regex.Match(Internal::toStringPiece(path), /*startpos=*/0, - /*endpos=*/path.size(), RE2::ANCHOR_BOTH, captures.data(), captures.size())) { - return absl::InvalidArgumentError("Pattern does not match"); - } - - std::string rewritten_path; - for (RewriteSegment segment : rewrite_pattern) { - auto* literal = absl::get_if(&segment); - auto* capture_index = absl::get_if(&segment); - if (literal != nullptr) { - absl::StrAppend(&rewritten_path, *literal); - } else if (capture_index != nullptr) { - if (*capture_index < 1 || *capture_index >= capture_num) { - return absl::InvalidArgumentError("Invalid variable index"); - } - absl::StrAppend(&rewritten_path, absl::string_view(captures[*capture_index].as_string())); - } - } - - return rewritten_path; -} - } // namespace UriTemplate } // namespace Extensions } // namespace Envoy diff --git a/source/extensions/path/uri_template_lib/uri_template.h b/source/extensions/path/uri_template_lib/uri_template.h index e620da60f775d..6e50d04bc1555 100644 --- a/source/extensions/path/uri_template_lib/uri_template.h +++ b/source/extensions/path/uri_template_lib/uri_template.h @@ -71,18 +71,6 @@ absl::Status isValidSharedVariableSet(absl::string_view pattern, absl::string_vi */ absl::Status isValidMatchPattern(absl::string_view match_pattern); -/** - * Concatenates literals and extracts variable values to form the final rewritten path. - * For example: - * rewrite_pattern: [capture_index=2, literal="cat"] - * path: "/bar/var" - * capture_regex: "(1)/(2)" - * Rewrite would result in rewrite of "/var/cat". - */ -absl::StatusOr rewritePathTemplatePattern(absl::string_view path, - absl::string_view capture_regex, - const RewriteSegments& rewrite_pattern); - } // namespace UriTemplate } // namespace Extensions } // namespace Envoy diff --git a/test/extensions/path/uri_template_lib/uri_template_test.cc b/test/extensions/path/uri_template_lib/uri_template_test.cc index 69ad814643785..abebf4833a01e 100644 --- a/test/extensions/path/uri_template_lib/uri_template_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_test.cc @@ -28,7 +28,6 @@ static constexpr absl::string_view kCaptureRegex = "/(?P[a-zA-Z0-9-._~%!$& "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)/" "(?P[a-zA-Z0-9-._~%!$&'()+,;:@]+)"; -static constexpr absl::string_view kMatchPath = "/val1/val2/val3/val4/val5"; TEST(ConvertPathPattern, ValidPattern) { EXPECT_THAT(convertPathPatternSyntaxToRegex("/abc"), IsOkAndHolds("/abc")); @@ -84,41 +83,20 @@ TEST_P(ParseRewriteHelperFailure, ParseRewriteHelperFailureTest) { EXPECT_THAT(parseRewritePattern(pattern), StatusIs(absl::StatusCode::kInvalidArgument)); } -class ParseRewriteSuccess : public testing::TestWithParam> { +class ParseRewriteSuccess : public testing::TestWithParam { protected: - const std::string& rewritePattern() const { return std::get<0>(GetParam()); } - RewriteSegments expectedSegments() const { return std::get<1>(GetParam()); } + std::string rewritePattern() const { return GetParam(); } }; TEST(ParseRewrite, InvalidRegex) { EXPECT_THAT(parseRewritePattern("/{var1}", "+[abc"), StatusIs(absl::StatusCode::kInternal)); } -INSTANTIATE_TEST_SUITE_P( - ParseRewriteSuccessTestSuite, ParseRewriteSuccess, - testing::ValuesIn(std::vector>({ - {"/static", RewriteSegments({RewriteSegment("/static")})}, - {"/{var1}", RewriteSegments({RewriteSegment("/"), RewriteSegment(1)})}, - {"/{var1}", RewriteSegments({RewriteSegment("/"), RewriteSegment(1)})}, - {"/{var1}/{var1}/{var1}", - RewriteSegments({RewriteSegment("/"), RewriteSegment(1), RewriteSegment("/"), - RewriteSegment(1), RewriteSegment("/"), RewriteSegment(1)})}, - {"/{var3}/{var1}/{var2}", - RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/"), - RewriteSegment(1), RewriteSegment("/"), RewriteSegment(2)})}, - {"/{var3}/abc/def/{var2}.suffix", - RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/abc/def/"), - RewriteSegment(2), RewriteSegment(".suffix")})}, - {"/abc/{var1}/{var2}/def", - RewriteSegments({RewriteSegment("/abc/"), RewriteSegment(1), RewriteSegment("/"), - RewriteSegment(2), RewriteSegment("def")})}, - {"/{var1}/{var2}", RewriteSegments({RewriteSegment("/"), RewriteSegment(1), - RewriteSegment(1), RewriteSegment(2)})}, - {"/{var1}-{var2}/bucket-{var3}.suffix", - RewriteSegments({RewriteSegment("/"), RewriteSegment(1), RewriteSegment("-"), - RewriteSegment(2), RewriteSegment("/bucket-"), RewriteSegment(3), - RewriteSegment(".suffix")})}, - }))); +INSTANTIATE_TEST_SUITE_P(ParseRewriteSuccessTestSuite, ParseRewriteSuccess, + testing::Values("/static", "/{var1}", "/{var1}", "/{var1}/{var1}/{var1}", + "/{var3}/{var1}/{var2}", "/{var3}/abc/def/{var2}.suffix", + "/abc/{var1}/{var2}/def", "/{var1}/{var2}", + "/{var1}-{var2}/bucket-{var3}.suffix")); TEST_P(ParseRewriteSuccess, ParseRewriteSuccessTest) { absl::StatusOr rewrite = parseRewritePattern(rewritePattern(), kCaptureRegex); @@ -139,67 +117,6 @@ TEST_P(ParseRewriteFailure, ParseRewriteFailureTest) { StatusIs(absl::StatusCode::kInvalidArgument)); } -class RewritePathTemplateSuccess - : public testing::TestWithParam> { -protected: - RewriteSegments rewriteSegments() const { return std::get<0>(GetParam()); } - - const std::string& expectedRewrittenPath() const { return std::get<1>(GetParam()); } -}; - -INSTANTIATE_TEST_SUITE_P( - RewritePathTemplateSuccessTestSuite, RewritePathTemplateSuccess, - testing::ValuesIn(std::vector>( - {{RewriteSegments({RewriteSegment("/static")}), "/static"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(1)}), "/val1"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(1)}), "/val1"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(1), RewriteSegment("/"), - RewriteSegment(1), RewriteSegment("/"), RewriteSegment(1)}), - "/val1/val1/val1"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/"), - RewriteSegment(1), RewriteSegment("/"), RewriteSegment(2)}), - "/val3/val1/val2"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment("/abc/def/"), - RewriteSegment(2), RewriteSegment(".suffix")}), - "/val3/abc/def/val2.suffix"}, - {RewriteSegments({RewriteSegment("/"), RewriteSegment(3), RewriteSegment(2), - RewriteSegment("."), RewriteSegment(1)}), - "/val3val2.val1"}, - {RewriteSegments({RewriteSegment("/abc/"), RewriteSegment(1), RewriteSegment("/"), - RewriteSegment(5), RewriteSegment("/def")}), - "/abc/val1/val5/def"}}))); - -TEST_P(RewritePathTemplateSuccess, RewritePathTemplateSuccessTest) { - absl::StatusOr rewritten_path = - rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewriteSegments()); - ASSERT_OK(rewritten_path); - EXPECT_EQ(rewritten_path.value(), expectedRewrittenPath()); -} - -TEST(RewritePathTemplateFailure, BadRegex) { - RewriteSegments rewrite_segments({RewriteSegment("static"), RewriteSegment(1)}); - EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, "+/bad_regex", rewrite_segments), - StatusIs(absl::StatusCode::kInternal)); -} - -TEST(RewritePathTemplateFailure, RegexNoMatch) { - RewriteSegments rewrite_segments({RewriteSegment("/"), RewriteSegment(1)}); - EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, "/no_match_regex", rewrite_segments), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -TEST(RewritePathTemplateFailure, RegexCaptureIndexZero) { - RewriteSegments rewrite_segments({RewriteSegment("/"), RewriteSegment(0)}); - EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewrite_segments), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - -TEST(RewritePathTemplateFailure, RegexCaptureIndexAboveMaxCapture) { - RewriteSegments rewrite_segments({RewriteSegment("/"), RewriteSegment(6)}); - EXPECT_THAT(rewritePathTemplatePattern(kMatchPath, kCaptureRegex, rewrite_segments), - StatusIs(absl::StatusCode::kInvalidArgument)); -} - class PathPatternMatchAndRewrite : public testing::TestWithParam< std::tuple> { @@ -230,12 +147,6 @@ TEST_P(PathPatternMatchAndRewrite, PathPatternMatchAndRewriteTest) { absl::StatusOr rewrite_segment = parseRewritePattern(rewritePattern(), regex.value()); ASSERT_OK(rewrite_segment); - - absl::StatusOr rewritten_path = - rewritePathTemplatePattern(matchPath(), regex.value(), rewrite_segment.value()); - ASSERT_OK(rewritten_path); - - EXPECT_EQ(rewritten_path.value(), expectedRewrittenPath()); } TEST_P(PathPatternMatchAndRewrite, IsValidMatchPattern) { From b56cad5c0fa5639308da09ea456797a28a9d0cf2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 1 Sep 2022 15:41:52 +0000 Subject: [PATCH 206/238] Format Signed-off-by: silverstar195 --- .../pattern_template/pattern_template_rewrite.cc | 3 +-- .../pattern_template/pattern_template_rewrite.h | 14 +++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index 296f78fc73224..103f3b8687771 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -60,8 +60,7 @@ PatternTemplateRewriter::rewritePath(absl::string_view pattern, return absl::InvalidArgumentError("Unable to parse path rewrite pattern"); } - const RewriteSegments& rewrite_pattern_segments = - *std::move(rewrite_pattern); + const RewriteSegments& rewrite_pattern_segments = *std::move(rewrite_pattern); RE2 regex = RE2(Internal::toStringPiece(regex_pattern_str)); if (!regex.ok()) { return absl::InternalError(regex.error()); diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 8c949ff7afec0..0ba6cd62f653a 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -44,13 +44,13 @@ class PatternTemplateRewriter : public Router::PathRewriter { absl::string_view pattern() const override { return rewrite_pattern_; } /** - * Concatenates literals and extracts variable values to form the final rewritten path. - * For example: - * rewrite_pattern: [capture_index=2, literal="cat"] - * path: "/bar/var" - * capture_regex: "(1)/(2)" - * Rewrite would result in rewrite of "/var/cat". - */ + * Concatenates literals and extracts variable values to form the final rewritten path. + * For example: + * rewrite_pattern: [capture_index=2, literal="cat"] + * path: "/bar/var" + * capture_regex: "(1)/(2)" + * Rewrite would result in rewrite of "/var/cat". + */ absl::StatusOr rewritePath(absl::string_view pattern, absl::string_view matched_path) const override; From ce23b028d9e1539d9c4c64fcce5cd79335858113 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 1 Sep 2022 19:33:49 +0000 Subject: [PATCH 207/238] Merge remote-tracking branch 'origin/main' into router-filter-matching-lib-control-plane # Conflicts: # source/extensions/path/uri_template_lib/uri_template.cc # source/extensions/path/uri_template_lib/uri_template.h # test/extensions/path/uri_template_lib/uri_template_test.cc Signed-off-by: silverstar195 --- api/envoy/config/route/v3/route_components.proto | 2 ++ .../path/match/pattern_template/v3/pattern_template_match.proto | 2 +- .../rewrite/pattern_template/v3/pattern_template_rewrite.proto | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/api/envoy/config/route/v3/route_components.proto b/api/envoy/config/route/v3/route_components.proto index 7661c6f3a0ef8..01a466520c414 100644 --- a/api/envoy/config/route/v3/route_components.proto +++ b/api/envoy/config/route/v3/route_components.proto @@ -572,6 +572,7 @@ message RouteMatch { // Expect the value to not contain ``?`` or ``#`` and not to end in ``/`` string path_separated_prefix = 14 [(validate.rules).string = {pattern: "^[^?#]+[^?#/]$"}]; + // [#extension-category: envoy.path.match] core.v3.TypedExtensionConfig path_match_policy = 15; } @@ -1084,6 +1085,7 @@ message RouteAction { // ``/aaa/yyy/bbb``. type.matcher.v3.RegexMatchAndSubstitute regex_rewrite = 32; + // [#extension-category: envoy.path.rewrite] core.v3.TypedExtensionConfig path_rewrite_policy = 41; oneof host_rewrite_specifier { diff --git a/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto index 3387a1e79fee0..87515f1298098 100644 --- a/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto @@ -12,6 +12,7 @@ option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pat option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Pattern Template Match Config] +// [#extension: envoy.path.match.pattern_template.pattern_template_matcher] // If specified, the route is a template match rule meaning that the // ``:path`` header (without the query string) must match the given @@ -38,7 +39,6 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // * ``/videos/{file}`` would match ``/videos/1080p5000_00001.m4s`` // // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` - message PatternTemplateMatchConfig { string path_template = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; } diff --git a/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto index a61594c10292c..3cdef3d438d4e 100644 --- a/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto @@ -12,6 +12,7 @@ option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pat option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Pattern Template Rewrite Config] +// [#extension: envoy.path.rewrite.pattern_template.pattern_template_rewriter] // Indicates that during forwarding, portions of the path that match the // pattern should be rewritten, even allowing the substitution of variables From 69c7c25605433850dcf23aba411f7343e07f68c0 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 1 Sep 2022 19:49:39 +0000 Subject: [PATCH 208/238] Add link to docs Signed-off-by: silverstar195 --- docs/root/api-v3/config/config.rst | 3 +++ docs/root/api-v3/config/path/match/path_matcher.rst | 8 ++++++++ docs/root/api-v3/config/path/rewrite/path_rewriter.rst | 8 ++++++++ 3 files changed, 19 insertions(+) create mode 100644 docs/root/api-v3/config/path/match/path_matcher.rst create mode 100644 docs/root/api-v3/config/path/rewrite/path_rewriter.rst diff --git a/docs/root/api-v3/config/config.rst b/docs/root/api-v3/config/config.rst index 53ef30681645b..07465c083d86f 100644 --- a/docs/root/api-v3/config/config.rst +++ b/docs/root/api-v3/config/config.rst @@ -38,3 +38,6 @@ Extensions contrib/contrib rbac/matchers config_validators/config_validators + path/match/path_matcher + path/rewrite/path_rewriter + diff --git a/docs/root/api-v3/config/path/match/path_matcher.rst b/docs/root/api-v3/config/path/match/path_matcher.rst new file mode 100644 index 0000000000000..68800d0a68f2c --- /dev/null +++ b/docs/root/api-v3/config/path/match/path_matcher.rst @@ -0,0 +1,8 @@ +Path Matcher +================= + +.. toctree:: + :glob: + :maxdepth: 2 + + ../../../extensions/path/match/*/v3/* diff --git a/docs/root/api-v3/config/path/rewrite/path_rewriter.rst b/docs/root/api-v3/config/path/rewrite/path_rewriter.rst new file mode 100644 index 0000000000000..01e7d1086561f --- /dev/null +++ b/docs/root/api-v3/config/path/rewrite/path_rewriter.rst @@ -0,0 +1,8 @@ +Path Rewriter +================= + +.. toctree:: + :glob: + :maxdepth: 2 + + ../../../extensions/path/rewrite/*/v3/* From 443cdcfe4d2f27365256683d124e87c26c6be4e7 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 2 Sep 2022 12:57:42 +0000 Subject: [PATCH 209/238] Add link to docs Signed-off-by: silverstar195 --- test/per_file_coverage.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/per_file_coverage.sh b/test/per_file_coverage.sh index 0d0690d3676d4..da38225bcdc15 100755 --- a/test/per_file_coverage.sh +++ b/test/per_file_coverage.sh @@ -71,6 +71,7 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/io_socket/user_space:96.2" "source/extensions/network/dns_resolver/getaddrinfo:96.3" "source/extensions/path/rewrite:88.1" +"source/extensions/path/:96.1" "source/extensions/path/rewrite/pattern_template:88.1" "source/extensions/rate_limit_descriptors:95.5" "source/extensions/rate_limit_descriptors/expr:95.5" From a3f450dc594db6d917e2a315fc489c7bb0475677 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 2 Sep 2022 19:40:45 +0000 Subject: [PATCH 210/238] Partly moved code Signed-off-by: silverstar195 --- envoy/router/BUILD | 4 +-- envoy/router/{path_match.h => path_matcher.h} | 4 +-- .../{path_rewrite.h => path_rewriter.h} | 6 ++-- envoy/router/router.h | 22 ++++--------- source/common/http/async_client_impl.cc | 2 +- source/common/http/async_client_impl.h | 4 +-- source/common/router/config_impl.cc | 32 ++++++++----------- source/common/router/config_impl.h | 22 +++++-------- .../path/match/pattern_template/config.cc | 2 +- .../path/match/pattern_template/config.h | 2 +- .../pattern_template/pattern_template_match.h | 2 +- .../path/rewrite/pattern_template/config.cc | 2 +- .../path/rewrite/pattern_template/config.h | 2 +- .../pattern_template_rewrite.cc | 10 +++--- .../pattern_template_rewrite.h | 10 +++--- 15 files changed, 52 insertions(+), 74 deletions(-) rename envoy/router/{path_match.h => path_matcher.h} (93%) rename envoy/router/{path_rewrite.h => path_rewriter.h} (91%) diff --git a/envoy/router/BUILD b/envoy/router/BUILD index 4c37e0f3397b8..37ba1aea9e2c7 100644 --- a/envoy/router/BUILD +++ b/envoy/router/BUILD @@ -149,7 +149,7 @@ envoy_cc_library( envoy_cc_library( name = "router_path_match_policy_interface", - hdrs = ["path_match.h"], + hdrs = ["path_matcher.h"], deps = [ "//envoy/config:typed_config_interface", "//source/common/common:minimal_logger_lib", @@ -160,7 +160,7 @@ envoy_cc_library( envoy_cc_library( name = "router_path_rewrite_policy_interface", - hdrs = ["path_rewrite.h"], + hdrs = ["path_rewriter.h"], deps = [ "//envoy/config:typed_config_interface", "//source/common/common:minimal_logger_lib", diff --git a/envoy/router/path_match.h b/envoy/router/path_matcher.h similarity index 93% rename from envoy/router/path_match.h rename to envoy/router/path_matcher.h index 2d6cd09c860ba..7934ca1cfbc73 100644 --- a/envoy/router/path_match.h +++ b/envoy/router/path_matcher.h @@ -20,9 +20,9 @@ class PathMatcher : Logger::Loggable { virtual ~PathMatcher() = default; /** - * Determines if the current path matches the pattern. + * Returns true if path matches the pattern. * - * @param path current path of the matchable route + * @param path the path to be matched * @return true if path matches the pattern. */ virtual bool match(absl::string_view path) const PURE; diff --git a/envoy/router/path_rewrite.h b/envoy/router/path_rewriter.h similarity index 91% rename from envoy/router/path_rewrite.h rename to envoy/router/path_rewriter.h index 48eda4ea68b9b..1f1b0205c987c 100644 --- a/envoy/router/path_rewrite.h +++ b/envoy/router/path_rewriter.h @@ -1,7 +1,7 @@ #pragma once #include "envoy/config/typed_config.h" -#include "envoy/router/path_match.h" +#include "envoy/router/path_matcher.h" #include "source/common/common/logger.h" @@ -27,8 +27,8 @@ class PathRewriter : Logger::Loggable { * @param active_policy true if user provided policy * @return true if current path match policy is acceptable */ - virtual absl::Status isCompatibleMatchPolicy(PathMatcherSharedPtr path_match_policy, - bool active_policy) const PURE; + virtual absl::Status isCompatiblePathMatcher(PathMatcherSharedPtr path_matcher, + bool active_matcher) const PURE; /** * Rewrites the current path to the specified output. Return a failure in case rewrite diff --git a/envoy/router/router.h b/envoy/router/router.h index c9cc5131a460a..f9cc85b386ade 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -20,8 +20,8 @@ #include "envoy/http/hash_policy.h" #include "envoy/rds/config.h" #include "envoy/router/internal_redirect.h" -#include "envoy/router/path_match.h" -#include "envoy/router/path_rewrite.h" +#include "envoy/router/path_matcher.h" +#include "envoy/router/path_rewriter.h" #include "envoy/tcp/conn_pool.h" #include "envoy/tracing/http_tracer.h" #include "envoy/type/v3/percent.pb.h" @@ -311,11 +311,6 @@ class PathMatchPolicy { public: virtual ~PathMatchPolicy() = default; - /** - * @return whether path match policy is enabled on this route. - */ - virtual bool enabled() const PURE; - /** * Returns the stored target route PathMatcher. * @return a PathMatcher instance. @@ -330,11 +325,6 @@ class PathRewritePolicy { public: virtual ~PathRewritePolicy() = default; - /** - * @return whether path rewrite policy is enabled on this route. - */ - virtual bool enabled() const PURE; - /** * Returns the stored target route PathRewriter. * @return a PathRewriter instance. @@ -961,14 +951,14 @@ class RouteEntry : public ResponseEntry { virtual const InternalRedirectPolicy& internalRedirectPolicy() const PURE; /** - * @return const PathMatchPolicy& the path match policy for the route. + * @return const PathMatcherPolicy& the path match policy for the route. */ - virtual const PathMatchPolicy& pathMatchPolicy() const PURE; + virtual const std::unique_ptr pathMatchPolicy() const PURE; /** - * @return const PathRewritePolicy& the path match rewrite for the route. + * @return const PathRewritertPolicy& the path match rewrite for the route. */ - virtual const PathRewritePolicy& pathRewritePolicy() const PURE; + virtual const std::unique_ptr pathRewritePolicy() const PURE; /** * @return uint32_t any route cap on bytes which should be buffered for shadowing or retries. diff --git a/source/common/http/async_client_impl.cc b/source/common/http/async_client_impl.cc index 68dd1497794ea..523dd39d3d52e 100644 --- a/source/common/http/async_client_impl.cc +++ b/source/common/http/async_client_impl.cc @@ -20,7 +20,7 @@ const std::vector> const AsyncStreamImpl::NullHedgePolicy AsyncStreamImpl::RouteEntryImpl::hedge_policy_; const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::RouteEntryImpl::rate_limit_policy_; const Router::InternalRedirectPolicyImpl AsyncStreamImpl::RouteEntryImpl::internal_redirect_policy_; -const Router::PathMatchPolicyImpl AsyncStreamImpl::RouteEntryImpl::path_match_policy_; +const std::unique_ptr AsyncStreamImpl::RouteEntryImpl::path_match_policy_; const Router::PathRewritePolicyImpl AsyncStreamImpl::RouteEntryImpl::path_rewrite_policy_; const std::vector AsyncStreamImpl::RouteEntryImpl::shadow_policies_; const AsyncStreamImpl::NullVirtualHost AsyncStreamImpl::RouteEntryImpl::virtual_host_; diff --git a/source/common/http/async_client_impl.h b/source/common/http/async_client_impl.h index ec973f3082708..7058851513f7d 100644 --- a/source/common/http/async_client_impl.h +++ b/source/common/http/async_client_impl.h @@ -231,8 +231,8 @@ class AsyncStreamImpl : public AsyncClient::Stream, const Router::InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } - const Router::PathMatchPolicy& pathMatchPolicy() const override { return path_match_policy_; } - const Router::PathRewritePolicy& pathRewritePolicy() const override { + const std::unique_ptr pathMatchPolicy() const override { return path_match_policy_; } + const std::unique_ptr pathRewritePolicy() const override { return path_rewrite_policy_; } uint32_t retryShadowBufferLimit() const override { diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 17f99738b329d..708af7f00f1fb 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -360,12 +360,9 @@ std::vector InternalRedirectPolicyImpl::pred return predicates; } -PathRewritePolicyImpl::PathRewritePolicyImpl() : enabled_(false){}; - PathRewritePolicyImpl::PathRewritePolicyImpl( const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator) - : enabled_(true) { + ProtobufMessage::ValidationVisitor& validator) { auto& factory = Envoy::Config::Utility::getAndCheckFactory(typed_config); @@ -383,12 +380,9 @@ PathRewritePolicyImpl::PathRewritePolicyImpl( PathRewriterSharedPtr PathRewritePolicyImpl::pathRewriter() const { return rewriter_; } -PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; - -PathMatchPolicyImpl::PathMatchPolicyImpl( +PathMatchPolicy::PathMatchPolicy( const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator) - : enabled_(true) { + ProtobufMessage::ValidationVisitor& validator) { auto& factory = Envoy::Config::Utility::getAndCheckFactory(typed_config); @@ -404,7 +398,7 @@ PathMatchPolicyImpl::PathMatchPolicyImpl( path_matcher_ = matcher.value(); } -PathMatcherSharedPtr PathMatchPolicyImpl::pathMatcher() const { return path_matcher_; } +PathMatcherSharedPtr PathMatchPolicy::pathMatcher() const { return path_matcher_; } absl::flat_hash_set InternalRedirectPolicyImpl::buildRedirectResponseCodes( const envoy::config::route::v3::InternalRedirectPolicy& policy_config) const { @@ -721,9 +715,9 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, regex_rewrite_substitution_ = rewrite_spec.substitution(); } - if (path_rewrite_policy_.enabled()) { - absl::Status compatible_status = path_rewrite_policy_.pathRewriter()->isCompatibleMatchPolicy( - path_match_policy_.pathMatcher(), path_match_policy_.enabled()); + if (path_rewrite_policy_.has_value()) { + absl::Status compatible_status = path_rewrite_policy_.pathRewriter()->isCompatiblePathMatcher( + path_match_policy_.pathMatcher(), path_match_policy_.has_value()); if (!compatible_status.ok()) { throw EnvoyException(std::string(compatible_status.message())); } @@ -894,7 +888,7 @@ void RouteEntryImplBase::finalizeRequestHeaders(Http::RequestHeaderMap& headers, // Handle path rewrite absl::optional container; if (!getPathRewrite(headers, container).empty() || regex_rewrite_ != nullptr || - path_rewrite_policy_.enabled()) { + path_rewrite_policy_.has_value()) { rewritePathHeader(headers, insert_envoy_original_path); } } @@ -1030,7 +1024,7 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa } } - if (path_rewrite_policy_.enabled()) { + if (path_rewrite_policy_.has_value()) { absl::string_view just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); absl::StatusOr new_path = @@ -1234,20 +1228,20 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( } return InternalRedirectPolicyImpl{policy_config, validator, current_route_name}; } -PathRewritePolicyImpl +std::unique_ptr RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { if (route.match().has_path_match_policy()) { - return PathMatchPolicyImpl{route.match().path_match_policy(), validator}; + return {PathMatchPolicy{route.match().path_match_policy(), validator}}; } return {}; diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index f188712720566..d58e67b9f9488 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -470,12 +470,9 @@ class PathMatchPolicyImpl : public PathMatchPolicy { PathMatchPolicyImpl(); // Router::PathMatchPolicy - bool enabled() const override { return enabled_; } - PathMatcherSharedPtr pathMatcher() const override; private: - const bool enabled_; ProtobufTypes::MessagePtr config_; PathMatcherSharedPtr path_matcher_; }; @@ -493,12 +490,9 @@ class PathRewritePolicyImpl : public PathRewritePolicy { PathRewritePolicyImpl(); // Router::PathRewritePolicy - bool enabled() const override { return enabled_; } - PathRewriterSharedPtr pathRewriter() const override; private: - const bool enabled_; ProtobufTypes::MessagePtr config_; PathRewriterSharedPtr rewriter_; }; @@ -615,8 +609,8 @@ class RouteEntryImplBase : public RouteEntry, return internal_redirect_policy_; } - const PathMatchPolicyImpl& pathMatchPolicy() const override { return path_match_policy_; } - const PathRewritePolicyImpl& pathRewritePolicy() const override { return path_rewrite_policy_; } + const std::unique_ptr pathMatchPolicy() const override { return path_match_policy_; } + const std::unique_ptr pathRewritePolicy() const override { return path_rewrite_policy_; } uint32_t retryShadowBufferLimit() const override { return retry_shadow_buffer_limit_; } const std::vector& shadowPolicies() const override { return shadow_policies_; } @@ -731,10 +725,10 @@ class RouteEntryImplBase : public RouteEntry, const InternalRedirectPolicy& internalRedirectPolicy() const override { return parent_->internalRedirectPolicy(); } - const PathMatchPolicyImpl& pathMatchPolicy() const override { + const std::unique_ptr pathMatchPolicy() const override { return parent_->pathMatchPolicy(); } - const PathRewritePolicyImpl& pathRewritePolicy() const override { + const std::unique_ptr pathRewritePolicy() const override { return parent_->pathRewritePolicy(); } uint32_t retryShadowBufferLimit() const override { return parent_->retryShadowBufferLimit(); } @@ -902,8 +896,8 @@ class RouteEntryImplBase : public RouteEntry, const std::string prefix_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_redirect_; - const PathMatchPolicyImpl path_match_policy_; - const PathRewritePolicyImpl path_rewrite_policy_; + const std::unique_ptr path_match_policy_; + const std::unique_ptr path_rewrite_policy_; std::string regex_rewrite_substitution_; std::string regex_rewrite_redirect_substitution_; const std::string host_rewrite_; @@ -985,10 +979,10 @@ class RouteEntryImplBase : public RouteEntry, ProtobufMessage::ValidationVisitor& validator, absl::string_view current_route_name) const; - PathMatchPolicyImpl buildPathMatchPolicy(envoy::config::route::v3::Route route, + absl::optional buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const; - PathRewritePolicyImpl buildPathRewritePolicy(envoy::config::route::v3::Route route, + absl::optional buildPathRewritePolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const; RouteConstSharedPtr pickClusterViaClusterHeader(const Http::LowerCaseString& cluster_header_name, diff --git a/source/extensions/path/match/pattern_template/config.cc b/source/extensions/path/match/pattern_template/config.cc index f3a07a8710609..cc78366ced7c1 100644 --- a/source/extensions/path/match/pattern_template/config.cc +++ b/source/extensions/path/match/pattern_template/config.cc @@ -1,7 +1,7 @@ #include "source/extensions/path/match/pattern_template/config.h" #include "envoy/registry/registry.h" -#include "envoy/router/path_match.h" +#include "envoy/router/path_matcher.h" namespace Envoy { namespace Extensions { diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/pattern_template/config.h index d1ab35f78249a..6ee5c86a0a797 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/pattern_template/config.h @@ -2,7 +2,7 @@ #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" -#include "envoy/router/path_match.h" +#include "envoy/router/path_matcher.h" #include "source/common/protobuf/message_validator_impl.h" #include "source/common/protobuf/utility.h" diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/pattern_template/pattern_template_match.h index 0d81b66fadf88..8f201615dfc5c 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/pattern_template/pattern_template_match.h @@ -3,7 +3,7 @@ #include #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" -#include "envoy/router/path_match.h" +#include "envoy/router/path_matcher.h" #include "source/extensions/path/uri_template_lib/uri_template.h" diff --git a/source/extensions/path/rewrite/pattern_template/config.cc b/source/extensions/path/rewrite/pattern_template/config.cc index 41b270576a556..10c36835cee2b 100644 --- a/source/extensions/path/rewrite/pattern_template/config.cc +++ b/source/extensions/path/rewrite/pattern_template/config.cc @@ -1,7 +1,7 @@ #include "source/extensions/path/rewrite/pattern_template/config.h" #include "envoy/registry/registry.h" -#include "envoy/router/path_rewrite.h" +#include "envoy/router/path_rewriter.h" namespace Envoy { namespace Extensions { diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/pattern_template/config.h index 43356a0976807..91f53b4ffb8bc 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/pattern_template/config.h @@ -2,7 +2,7 @@ #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" -#include "envoy/router/path_rewrite.h" +#include "envoy/router/path_rewriter.h" #include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc index 103f3b8687771..c3d58604c50c1 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc @@ -25,9 +25,9 @@ namespace Rewrite { #endif absl::Status -PatternTemplateRewriter::isCompatibleMatchPolicy(Router::PathMatcherSharedPtr path_match_predicate, - bool active_policy) const { - if (!active_policy || path_match_predicate->name() != Extensions::UriTemplate::Match::NAME) { +PatternTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher, + bool active_matcher) const { + if (!active_matcher || path_matcher->name() != Extensions::UriTemplate::Match::NAME) { return absl::InvalidArgumentError(fmt::format("unable to use {} extension without {} extension", Extensions::UriTemplate::Rewrite::NAME, Extensions::UriTemplate::Match::NAME)); @@ -35,10 +35,10 @@ PatternTemplateRewriter::isCompatibleMatchPolicy(Router::PathMatcherSharedPtr pa // This is needed to match up variable values. // Validation between extensions as they share rewrite pattern variables. - if (!isValidSharedVariableSet(rewrite_pattern_, path_match_predicate->pattern()).ok()) { + if (!isValidSharedVariableSet(rewrite_pattern_, path_matcher->pattern()).ok()) { return absl::InvalidArgumentError( fmt::format("mismatch between variables in path_match_policy {} and path_rewrite_policy {}", - path_match_predicate->pattern(), rewrite_pattern_)); + path_matcher->pattern(), rewrite_pattern_)); } return absl::OkStatus(); diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h index 0ba6cd62f653a..ffb5ef2107065 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h @@ -6,8 +6,8 @@ #include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" #include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" -#include "envoy/router/path_match.h" -#include "envoy/router/path_rewrite.h" +#include "envoy/router/path_matcher.h" +#include "envoy/router/path_rewriter.h" #include "source/common/protobuf/message_validator_impl.h" #include "source/common/protobuf/utility.h" @@ -54,13 +54,13 @@ class PatternTemplateRewriter : public Router::PathRewriter { absl::StatusOr rewritePath(absl::string_view pattern, absl::string_view matched_path) const override; - absl::Status isCompatibleMatchPolicy(Router::PathMatcherSharedPtr match_policy, - bool active_policy) const override; + absl::Status isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher, + bool active_matcher) const override; absl::string_view name() const override { return NAME; } private: - std::string rewrite_pattern_{nullptr}; + std::string rewrite_pattern_; }; } // namespace Rewrite From 10b3b6aaacdefcad5ef2d4f327d0d1fa5f613642 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 2 Sep 2022 20:18:07 +0000 Subject: [PATCH 211/238] Roll about some changes Signed-off-by: silverstar195 --- envoy/router/router.h | 16 ++++++++++--- source/common/http/async_client_impl.cc | 2 +- source/common/http/async_client_impl.h | 4 ++-- source/common/router/config_impl.cc | 30 +++++++++++++++---------- source/common/router/config_impl.h | 22 +++++++++++------- 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/envoy/router/router.h b/envoy/router/router.h index f9cc85b386ade..97e00b0917a7f 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -311,6 +311,11 @@ class PathMatchPolicy { public: virtual ~PathMatchPolicy() = default; + /** + * @return whether path match policy is enabled on this route. + */ + virtual bool enabled() const PURE; + /** * Returns the stored target route PathMatcher. * @return a PathMatcher instance. @@ -325,6 +330,11 @@ class PathRewritePolicy { public: virtual ~PathRewritePolicy() = default; + /** + * @return whether path rewrite policy is enabled on this route. + */ + virtual bool enabled() const PURE; + /** * Returns the stored target route PathRewriter. * @return a PathRewriter instance. @@ -953,12 +963,12 @@ class RouteEntry : public ResponseEntry { /** * @return const PathMatcherPolicy& the path match policy for the route. */ - virtual const std::unique_ptr pathMatchPolicy() const PURE; + virtual const PathMatchPolicy& pathMatchPolicy() const PURE; /** - * @return const PathRewritertPolicy& the path match rewrite for the route. + * @return const PathRewriterPolicy& the path match rewrite for the route. */ - virtual const std::unique_ptr pathRewritePolicy() const PURE; + virtual const PathRewritePolicy& pathRewritePolicy() const PURE; /** * @return uint32_t any route cap on bytes which should be buffered for shadowing or retries. diff --git a/source/common/http/async_client_impl.cc b/source/common/http/async_client_impl.cc index 523dd39d3d52e..68dd1497794ea 100644 --- a/source/common/http/async_client_impl.cc +++ b/source/common/http/async_client_impl.cc @@ -20,7 +20,7 @@ const std::vector> const AsyncStreamImpl::NullHedgePolicy AsyncStreamImpl::RouteEntryImpl::hedge_policy_; const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::RouteEntryImpl::rate_limit_policy_; const Router::InternalRedirectPolicyImpl AsyncStreamImpl::RouteEntryImpl::internal_redirect_policy_; -const std::unique_ptr AsyncStreamImpl::RouteEntryImpl::path_match_policy_; +const Router::PathMatchPolicyImpl AsyncStreamImpl::RouteEntryImpl::path_match_policy_; const Router::PathRewritePolicyImpl AsyncStreamImpl::RouteEntryImpl::path_rewrite_policy_; const std::vector AsyncStreamImpl::RouteEntryImpl::shadow_policies_; const AsyncStreamImpl::NullVirtualHost AsyncStreamImpl::RouteEntryImpl::virtual_host_; diff --git a/source/common/http/async_client_impl.h b/source/common/http/async_client_impl.h index 7058851513f7d..ec973f3082708 100644 --- a/source/common/http/async_client_impl.h +++ b/source/common/http/async_client_impl.h @@ -231,8 +231,8 @@ class AsyncStreamImpl : public AsyncClient::Stream, const Router::InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } - const std::unique_ptr pathMatchPolicy() const override { return path_match_policy_; } - const std::unique_ptr pathRewritePolicy() const override { + const Router::PathMatchPolicy& pathMatchPolicy() const override { return path_match_policy_; } + const Router::PathRewritePolicy& pathRewritePolicy() const override { return path_rewrite_policy_; } uint32_t retryShadowBufferLimit() const override { diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 708af7f00f1fb..3c19d3b82fd5e 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -360,9 +360,12 @@ std::vector InternalRedirectPolicyImpl::pred return predicates; } +PathRewritePolicyImpl::PathRewritePolicyImpl() : enabled_(false){}; + PathRewritePolicyImpl::PathRewritePolicyImpl( const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator) { + ProtobufMessage::ValidationVisitor& validator) + : enabled_(true) { auto& factory = Envoy::Config::Utility::getAndCheckFactory(typed_config); @@ -380,9 +383,12 @@ PathRewritePolicyImpl::PathRewritePolicyImpl( PathRewriterSharedPtr PathRewritePolicyImpl::pathRewriter() const { return rewriter_; } -PathMatchPolicy::PathMatchPolicy( +PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; + +PathMatchPolicyImpl::PathMatchPolicyImpl( const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator) { + ProtobufMessage::ValidationVisitor& validator) + : enabled_(true) { auto& factory = Envoy::Config::Utility::getAndCheckFactory(typed_config); @@ -398,7 +404,7 @@ PathMatchPolicy::PathMatchPolicy( path_matcher_ = matcher.value(); } -PathMatcherSharedPtr PathMatchPolicy::pathMatcher() const { return path_matcher_; } +PathMatcherSharedPtr PathMatchPolicyImpl::pathMatcher() const { return path_matcher_; } absl::flat_hash_set InternalRedirectPolicyImpl::buildRedirectResponseCodes( const envoy::config::route::v3::InternalRedirectPolicy& policy_config) const { @@ -715,9 +721,9 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, regex_rewrite_substitution_ = rewrite_spec.substitution(); } - if (path_rewrite_policy_.has_value()) { + if (path_rewrite_policy_.enabled()) { absl::Status compatible_status = path_rewrite_policy_.pathRewriter()->isCompatiblePathMatcher( - path_match_policy_.pathMatcher(), path_match_policy_.has_value()); + path_match_policy_.pathMatcher(), path_match_policy_.enabled()); if (!compatible_status.ok()) { throw EnvoyException(std::string(compatible_status.message())); } @@ -888,7 +894,7 @@ void RouteEntryImplBase::finalizeRequestHeaders(Http::RequestHeaderMap& headers, // Handle path rewrite absl::optional container; if (!getPathRewrite(headers, container).empty() || regex_rewrite_ != nullptr || - path_rewrite_policy_.has_value()) { + path_rewrite_policy_.enabled()) { rewritePathHeader(headers, insert_envoy_original_path); } } @@ -1024,7 +1030,7 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa } } - if (path_rewrite_policy_.has_value()) { + if (path_rewrite_policy_.enabled()) { absl::string_view just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); absl::StatusOr new_path = @@ -1228,20 +1234,20 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( } return InternalRedirectPolicyImpl{policy_config, validator, current_route_name}; } -std::unique_ptr +PathMatchPolicyImpl RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { if (route.match().has_path_match_policy()) { - return {PathMatchPolicy{route.match().path_match_policy(), validator}}; + return PathMatchPolicyImpl{route.match().path_match_policy(), validator}; } return {}; diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index d58e67b9f9488..f188712720566 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -470,9 +470,12 @@ class PathMatchPolicyImpl : public PathMatchPolicy { PathMatchPolicyImpl(); // Router::PathMatchPolicy + bool enabled() const override { return enabled_; } + PathMatcherSharedPtr pathMatcher() const override; private: + const bool enabled_; ProtobufTypes::MessagePtr config_; PathMatcherSharedPtr path_matcher_; }; @@ -490,9 +493,12 @@ class PathRewritePolicyImpl : public PathRewritePolicy { PathRewritePolicyImpl(); // Router::PathRewritePolicy + bool enabled() const override { return enabled_; } + PathRewriterSharedPtr pathRewriter() const override; private: + const bool enabled_; ProtobufTypes::MessagePtr config_; PathRewriterSharedPtr rewriter_; }; @@ -609,8 +615,8 @@ class RouteEntryImplBase : public RouteEntry, return internal_redirect_policy_; } - const std::unique_ptr pathMatchPolicy() const override { return path_match_policy_; } - const std::unique_ptr pathRewritePolicy() const override { return path_rewrite_policy_; } + const PathMatchPolicyImpl& pathMatchPolicy() const override { return path_match_policy_; } + const PathRewritePolicyImpl& pathRewritePolicy() const override { return path_rewrite_policy_; } uint32_t retryShadowBufferLimit() const override { return retry_shadow_buffer_limit_; } const std::vector& shadowPolicies() const override { return shadow_policies_; } @@ -725,10 +731,10 @@ class RouteEntryImplBase : public RouteEntry, const InternalRedirectPolicy& internalRedirectPolicy() const override { return parent_->internalRedirectPolicy(); } - const std::unique_ptr pathMatchPolicy() const override { + const PathMatchPolicyImpl& pathMatchPolicy() const override { return parent_->pathMatchPolicy(); } - const std::unique_ptr pathRewritePolicy() const override { + const PathRewritePolicyImpl& pathRewritePolicy() const override { return parent_->pathRewritePolicy(); } uint32_t retryShadowBufferLimit() const override { return parent_->retryShadowBufferLimit(); } @@ -896,8 +902,8 @@ class RouteEntryImplBase : public RouteEntry, const std::string prefix_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_redirect_; - const std::unique_ptr path_match_policy_; - const std::unique_ptr path_rewrite_policy_; + const PathMatchPolicyImpl path_match_policy_; + const PathRewritePolicyImpl path_rewrite_policy_; std::string regex_rewrite_substitution_; std::string regex_rewrite_redirect_substitution_; const std::string host_rewrite_; @@ -979,10 +985,10 @@ class RouteEntryImplBase : public RouteEntry, ProtobufMessage::ValidationVisitor& validator, absl::string_view current_route_name) const; - absl::optional buildPathMatchPolicy(envoy::config::route::v3::Route route, + PathMatchPolicyImpl buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const; - absl::optional buildPathRewritePolicy(envoy::config::route::v3::Route route, + PathRewritePolicyImpl buildPathRewritePolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const; RouteConstSharedPtr pickClusterViaClusterHeader(const Http::LowerCaseString& cluster_header_name, From 43e02ef177d8c7057fa6299184f5239956c55869 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 2 Sep 2022 20:52:36 +0000 Subject: [PATCH 212/238] Progress towards renaming Signed-off-by: silverstar195 --- api/BUILD | 4 +- .../v3/BUILD | 0 .../v3/uri_template_match.proto} | 14 +- .../v3/BUILD | 0 .../v3/uri_template_rewrite.proto} | 14 +- api/versioning/BUILD | 4 +- envoy/router/path_matcher.h | 4 +- envoy/router/path_rewriter.h | 4 +- source/common/router/BUILD | 4 +- source/common/router/config_impl.cc | 10 +- source/common/router/config_impl.h | 4 +- source/extensions/extensions_build_config.bzl | 349 ++++++++---------- source/extensions/extensions_metadata.yaml | 8 +- .../{pattern_template => uri_template}/BUILD | 18 +- .../config.cc | 4 +- .../config.h | 18 +- .../uri_template_match.cc} | 6 +- .../uri_template_match.h} | 16 +- .../{pattern_template => uri_template}/BUILD | 18 +- .../config.cc | 4 +- .../config.h | 18 +- .../uri_template_rewrite.cc} | 12 +- .../uri_template_rewrite.h} | 26 +- test/common/router/config_impl_test.cc | 124 +++---- .../path/match/pattern_template/BUILD | 10 +- .../match/pattern_template/config_test.cc | 18 +- .../match/pattern_template/library_test.cc | 6 +- .../path/rewrite/pattern_template/BUILD | 14 +- .../rewrite/pattern_template/config_test.cc | 24 +- .../rewrite/pattern_template/library_test.cc | 44 +-- tools/code_format/config.yaml | 4 +- 31 files changed, 383 insertions(+), 420 deletions(-) rename api/envoy/extensions/path/match/{pattern_template => uri_template}/v3/BUILD (100%) rename api/envoy/extensions/path/match/{pattern_template/v3/pattern_template_match.proto => uri_template/v3/uri_template_match.proto} (79%) rename api/envoy/extensions/path/rewrite/{pattern_template => uri_template}/v3/BUILD (100%) rename api/envoy/extensions/path/rewrite/{pattern_template/v3/pattern_template_rewrite.proto => uri_template/v3/uri_template_rewrite.proto} (86%) rename source/extensions/path/match/{pattern_template => uri_template}/BUILD (60%) rename source/extensions/path/match/{pattern_template => uri_template}/config.cc (64%) rename source/extensions/path/match/{pattern_template => uri_template}/config.h (58%) rename source/extensions/path/match/{pattern_template/pattern_template_match.cc => uri_template/uri_template_match.cc} (74%) rename source/extensions/path/match/{pattern_template/pattern_template_match.h => uri_template/uri_template_match.h} (60%) rename source/extensions/path/rewrite/{pattern_template => uri_template}/BUILD (60%) rename source/extensions/path/rewrite/{pattern_template => uri_template}/config.cc (64%) rename source/extensions/path/rewrite/{pattern_template => uri_template}/config.h (58%) rename source/extensions/path/rewrite/{pattern_template/pattern_template_rewrite.cc => uri_template/uri_template_rewrite.cc} (88%) rename source/extensions/path/rewrite/{pattern_template/pattern_template_rewrite.h => uri_template/uri_template_rewrite.h} (61%) diff --git a/api/BUILD b/api/BUILD index 2b32baae989fc..e342bf4870831 100644 --- a/api/BUILD +++ b/api/BUILD @@ -242,8 +242,8 @@ proto_library( "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", - "//envoy/extensions/path/match/pattern_template/v3:pkg", - "//envoy/extensions/path/rewrite/pattern_template/v3:pkg", + "//envoy/extensions/path/match/uri_template/v3:pkg", + "//envoy/extensions/path/rewrite/uri_template/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", diff --git a/api/envoy/extensions/path/match/pattern_template/v3/BUILD b/api/envoy/extensions/path/match/uri_template/v3/BUILD similarity index 100% rename from api/envoy/extensions/path/match/pattern_template/v3/BUILD rename to api/envoy/extensions/path/match/uri_template/v3/BUILD diff --git a/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto b/api/envoy/extensions/path/match/uri_template/v3/uri_template_match.proto similarity index 79% rename from api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto rename to api/envoy/extensions/path/match/uri_template/v3/uri_template_match.proto index 87515f1298098..877954345b21e 100644 --- a/api/envoy/extensions/path/match/pattern_template/v3/pattern_template_match.proto +++ b/api/envoy/extensions/path/match/uri_template/v3/uri_template_match.proto @@ -1,18 +1,18 @@ syntax = "proto3"; -package envoy.extensions.path.match.pattern_template.v3; +package envoy.extensions.path.match.uri_template.v3; import "udpa/annotations/status.proto"; import "validate/validate.proto"; -option java_package = "io.envoyproxy.envoy.extensions.path.match.pattern_template.v3"; -option java_outer_classname = "PatternTemplateMatchProto"; +option java_package = "io.envoyproxy.envoy.extensions.path.match.uri_template.v3"; +option java_outer_classname = "UriTemplateMatchProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/path/match/pattern_template/v3;pattern_templatev3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/path/match/uri_template/v3;uri_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; -// [#protodoc-title: Pattern Template Match Config] -// [#extension: envoy.path.match.pattern_template.pattern_template_matcher] +// [#protodoc-title: Uri Template Match Config] +// [#extension: envoy.path.match.pattern_template.uri_template_matcher] // If specified, the route is a template match rule meaning that the // ``:path`` header (without the query string) must match the given @@ -39,6 +39,6 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // * ``/videos/{file}`` would match ``/videos/1080p5000_00001.m4s`` // // * ``/**.mpd`` would match ``/content/123/india/dash/55/manifest.mpd`` -message PatternTemplateMatchConfig { +message UriTemplateMatchConfig { string path_template = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; } diff --git a/api/envoy/extensions/path/rewrite/pattern_template/v3/BUILD b/api/envoy/extensions/path/rewrite/uri_template/v3/BUILD similarity index 100% rename from api/envoy/extensions/path/rewrite/pattern_template/v3/BUILD rename to api/envoy/extensions/path/rewrite/uri_template/v3/BUILD diff --git a/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto b/api/envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.proto similarity index 86% rename from api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto rename to api/envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.proto index 3cdef3d438d4e..138135c341c04 100644 --- a/api/envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.proto +++ b/api/envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.proto @@ -1,18 +1,18 @@ syntax = "proto3"; -package envoy.extensions.path.rewrite.pattern_template.v3; +package envoy.extensions.path.rewrite.uri_template.v3; import "udpa/annotations/status.proto"; import "validate/validate.proto"; -option java_package = "io.envoyproxy.envoy.extensions.path.rewrite.pattern_template.v3"; -option java_outer_classname = "PatternTemplateRewriteProto"; +option java_package = "io.envoyproxy.envoy.extensions.path.rewrite.uri_template.v3"; +option java_outer_classname = "UriTemplateRewriteProto"; option java_multiple_files = true; -option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/path/rewrite/pattern_template/v3;pattern_templatev3"; +option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/path/rewrite/uri_template/v3;uri_templatev3"; option (udpa.annotations.file_status).package_version_status = ACTIVE; -// [#protodoc-title: Pattern Template Rewrite Config] -// [#extension: envoy.path.rewrite.pattern_template.pattern_template_rewriter] +// [#protodoc-title: Uri Template Rewrite Config] +// [#extension: envoy.path.rewrite.pattern_template.uri_template_rewriter] // Indicates that during forwarding, portions of the path that match the // pattern should be rewritten, even allowing the substitution of variables @@ -53,6 +53,6 @@ option (udpa.annotations.file_status).package_version_status = ACTIVE; // * The path pattern ``/content/{format}/{lang}/{id}/{file}.vtt`` paired with a substitution // string of ``/{lang}/{format}/{file}.vtt`` would transform ``/content/hls/en-us/12345/en_193913.vtt`` // into ``/en-us/hls/en_193913.vtt``. -message PatternTemplateRewriteConfig { +message UriTemplateRewriteConfig { string path_template_rewrite = 1 [(validate.rules).string = {min_len: 1 max_len: 256}]; } diff --git a/api/versioning/BUILD b/api/versioning/BUILD index ca5ad9b7e1c9f..1f0b53c0d71fd 100644 --- a/api/versioning/BUILD +++ b/api/versioning/BUILD @@ -184,8 +184,8 @@ proto_library( "//envoy/extensions/network/dns_resolver/cares/v3:pkg", "//envoy/extensions/network/dns_resolver/getaddrinfo/v3:pkg", "//envoy/extensions/network/socket_interface/v3:pkg", - "//envoy/extensions/path/match/pattern_template/v3:pkg", - "//envoy/extensions/path/rewrite/pattern_template/v3:pkg", + "//envoy/extensions/path/match/uri_template/v3:pkg", + "//envoy/extensions/path/rewrite/uri_template/v3:pkg", "//envoy/extensions/quic/crypto_stream/v3:pkg", "//envoy/extensions/quic/proof_source/v3:pkg", "//envoy/extensions/rate_limit_descriptors/expr/v3:pkg", diff --git a/envoy/router/path_matcher.h b/envoy/router/path_matcher.h index 7934ca1cfbc73..c8806c7149cd1 100644 --- a/envoy/router/path_matcher.h +++ b/envoy/router/path_matcher.h @@ -28,9 +28,9 @@ class PathMatcher : Logger::Loggable { virtual bool match(absl::string_view path) const PURE; /** - * @return the match pattern. + * @return the match uri_template. */ - virtual absl::string_view pattern() const PURE; + virtual absl::string_view uri_template() const PURE; /** * @return the name of the path matcher. diff --git a/envoy/router/path_rewriter.h b/envoy/router/path_rewriter.h index 1f1b0205c987c..8a6b23515e538 100644 --- a/envoy/router/path_rewriter.h +++ b/envoy/router/path_rewriter.h @@ -42,9 +42,9 @@ class PathRewriter : Logger::Loggable { absl::string_view rewrite_pattern) const PURE; /** - * @return the rewrite pattern. + * @return the rewrite uri_template. */ - virtual absl::string_view pattern() const PURE; + virtual absl::string_view uri_template() const PURE; /** * @return the name of the pattern rewriter. diff --git a/source/common/router/BUILD b/source/common/router/BUILD index 2b50b6fa2b20f..fc9f685e96723 100644 --- a/source/common/router/BUILD +++ b/source/common/router/BUILD @@ -70,8 +70,8 @@ envoy_cc_library( "//source/common/tracing:http_tracer_lib", "//source/common/upstream:retry_factory_lib", "//source/extensions/early_data:default_early_data_policy_lib", - "//source/extensions/path/match/pattern_template:config", - "//source/extensions/path/rewrite/pattern_template:config", + "//source/extensions/path/match/uri_template:config", + "//source/extensions/path/rewrite/uri_template:config", "@envoy_api//envoy/config/common/matcher/v3:pkg_cc_proto", "@envoy_api//envoy/config/core/v3:pkg_cc_proto", "@envoy_api//envoy/config/route/v3:pkg_cc_proto", diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 3c19d3b82fd5e..d34dddc25c24c 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -48,8 +48,8 @@ #include "source/common/tracing/http_tracer_impl.h" #include "source/common/upstream/retry_factory.h" #include "source/extensions/early_data/default_early_data_policy.h" -#include "source/extensions/path/match/pattern_template/pattern_template_match.h" -#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" +#include "source/extensions/path/match/uri_template/uri_template_match.h" +#include "source/extensions/path/rewrite/uri_template/uri_template_rewrite.h" #include "absl/strings/match.h" @@ -1496,18 +1496,18 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - match_pattern_(path_match_policy_.pathMatcher()->pattern()){}; + uri_template_(path_match_policy_.pathMatcher()->uri_template()){}; void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { - finalizePathHeader(headers, path_match_policy_.pathMatcher()->pattern(), + finalizePathHeader(headers, path_match_policy_.pathMatcher()->uri_template(), insert_envoy_original_path); } absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { return currentUrlPathAfterRewriteWithMatchedPath(headers, - path_match_policy_.pathMatcher()->pattern()); + path_match_policy_.pathMatcher()->uri_template()); } RouteConstSharedPtr diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index f188712720566..509727c98774b 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -1080,7 +1080,7 @@ class PathMatchPolicyRouteEntryImpl : public RouteEntryImplBase { ProtobufMessage::ValidationVisitor& validator); // Router::PathMatchCriterion - const std::string& matcher() const override { return match_pattern_; } + const std::string& matcher() const override { return uri_template_; } PathMatchType matchType() const override { return PathMatchType::Policy; } // Router::Matchable @@ -1097,7 +1097,7 @@ class PathMatchPolicyRouteEntryImpl : public RouteEntryImplBase { currentUrlPathAfterRewrite(const Http::RequestHeaderMap& headers) const override; private: - const std::string match_pattern_; + const std::string uri_template_; }; /** diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 47353b1744e16..3809ac1a4706c 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -3,369 +3,332 @@ EXTENSIONS = { # # Access loggers # - - "envoy.access_loggers.file": "//source/extensions/access_loggers/file:config", - "envoy.access_loggers.extension_filters.cel": "//source/extensions/access_loggers/filters/cel:config", - "envoy.access_loggers.http_grpc": "//source/extensions/access_loggers/grpc:http_config", - "envoy.access_loggers.tcp_grpc": "//source/extensions/access_loggers/grpc:tcp_config", - "envoy.access_loggers.open_telemetry": "//source/extensions/access_loggers/open_telemetry:config", - "envoy.access_loggers.stdout": "//source/extensions/access_loggers/stream:config", - "envoy.access_loggers.stderr": "//source/extensions/access_loggers/stream:config", - "envoy.access_loggers.wasm": "//source/extensions/access_loggers/wasm:config", + "envoy.access_loggers.file": "//source/extensions/access_loggers/file:config", + "envoy.access_loggers.extension_filters.cel": "//source/extensions/access_loggers/filters/cel:config", + "envoy.access_loggers.http_grpc": "//source/extensions/access_loggers/grpc:http_config", + "envoy.access_loggers.tcp_grpc": "//source/extensions/access_loggers/grpc:tcp_config", + "envoy.access_loggers.open_telemetry": "//source/extensions/access_loggers/open_telemetry:config", + "envoy.access_loggers.stdout": "//source/extensions/access_loggers/stream:config", + "envoy.access_loggers.stderr": "//source/extensions/access_loggers/stream:config", + "envoy.access_loggers.wasm": "//source/extensions/access_loggers/wasm:config", # # Clusters # - - "envoy.clusters.aggregate": "//source/extensions/clusters/aggregate:cluster", - "envoy.clusters.dynamic_forward_proxy": "//source/extensions/clusters/dynamic_forward_proxy:cluster", - "envoy.clusters.redis": "//source/extensions/clusters/redis:redis_cluster", + "envoy.clusters.aggregate": "//source/extensions/clusters/aggregate:cluster", + "envoy.clusters.dynamic_forward_proxy": "//source/extensions/clusters/dynamic_forward_proxy:cluster", + "envoy.clusters.redis": "//source/extensions/clusters/redis:redis_cluster", # # Compression # - - "envoy.compression.gzip.compressor": "//source/extensions/compression/gzip/compressor:config", - "envoy.compression.gzip.decompressor": "//source/extensions/compression/gzip/decompressor:config", - "envoy.compression.brotli.compressor": "//source/extensions/compression/brotli/compressor:config", - "envoy.compression.brotli.decompressor": "//source/extensions/compression/brotli/decompressor:config", - "envoy.compression.zstd.compressor": "//source/extensions/compression/zstd/compressor:config", - "envoy.compression.zstd.decompressor": "//source/extensions/compression/zstd/decompressor:config", + "envoy.compression.gzip.compressor": "//source/extensions/compression/gzip/compressor:config", + "envoy.compression.gzip.decompressor": "//source/extensions/compression/gzip/decompressor:config", + "envoy.compression.brotli.compressor": "//source/extensions/compression/brotli/compressor:config", + "envoy.compression.brotli.decompressor": "//source/extensions/compression/brotli/decompressor:config", + "envoy.compression.zstd.compressor": "//source/extensions/compression/zstd/compressor:config", + "envoy.compression.zstd.decompressor": "//source/extensions/compression/zstd/decompressor:config", # # Config validators # - - "envoy.config.validators.minimum_clusters_validator": "//source/extensions/config/validators/minimum_clusters:config", + "envoy.config.validators.minimum_clusters_validator": "//source/extensions/config/validators/minimum_clusters:config", # # gRPC Credentials Plugins # - - "envoy.grpc_credentials.file_based_metadata": "//source/extensions/grpc_credentials/file_based_metadata:config", - "envoy.grpc_credentials.aws_iam": "//source/extensions/grpc_credentials/aws_iam:config", + "envoy.grpc_credentials.file_based_metadata": "//source/extensions/grpc_credentials/file_based_metadata:config", + "envoy.grpc_credentials.aws_iam": "//source/extensions/grpc_credentials/aws_iam:config", # # WASM # - - "envoy.bootstrap.wasm": "//source/extensions/bootstrap/wasm:config", + "envoy.bootstrap.wasm": "//source/extensions/bootstrap/wasm:config", # # Health checkers # - - "envoy.health_checkers.redis": "//source/extensions/health_checkers/redis:config", + "envoy.health_checkers.redis": "//source/extensions/health_checkers/redis:config", # # Input Matchers # - - "envoy.matching.matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", - "envoy.matching.matchers.ip": "//source/extensions/matching/input_matchers/ip:config", + "envoy.matching.matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", + "envoy.matching.matchers.ip": "//source/extensions/matching/input_matchers/ip:config", # # Generic Inputs # - - "envoy.matching.common_inputs.environment_variable": "//source/extensions/matching/common_inputs/environment_variable:config", + "envoy.matching.common_inputs.environment_variable": "//source/extensions/matching/common_inputs/environment_variable:config", # # HTTP filters # - - "envoy.filters.http.adaptive_concurrency": "//source/extensions/filters/http/adaptive_concurrency:config", - "envoy.filters.http.admission_control": "//source/extensions/filters/http/admission_control:config", - "envoy.filters.http.alternate_protocols_cache": "//source/extensions/filters/http/alternate_protocols_cache:config", - "envoy.filters.http.aws_lambda": "//source/extensions/filters/http/aws_lambda:config", - "envoy.filters.http.aws_request_signing": "//source/extensions/filters/http/aws_request_signing:config", - "envoy.filters.http.bandwidth_limit": "//source/extensions/filters/http/bandwidth_limit:config", - "envoy.filters.http.buffer": "//source/extensions/filters/http/buffer:config", - "envoy.filters.http.cache": "//source/extensions/filters/http/cache:config", - "envoy.filters.http.cdn_loop": "//source/extensions/filters/http/cdn_loop:config", - "envoy.filters.http.compressor": "//source/extensions/filters/http/compressor:config", - "envoy.filters.http.cors": "//source/extensions/filters/http/cors:config", - "envoy.filters.http.composite": "//source/extensions/filters/http/composite:config", - "envoy.filters.http.csrf": "//source/extensions/filters/http/csrf:config", - "envoy.filters.http.decompressor": "//source/extensions/filters/http/decompressor:config", - "envoy.filters.http.dynamic_forward_proxy": "//source/extensions/filters/http/dynamic_forward_proxy:config", - "envoy.filters.http.ext_authz": "//source/extensions/filters/http/ext_authz:config", - "envoy.filters.http.ext_proc": "//source/extensions/filters/http/ext_proc:config", - "envoy.filters.http.fault": "//source/extensions/filters/http/fault:config", - "envoy.filters.http.file_system_buffer": "//source/extensions/filters/http/file_system_buffer:config", - "envoy.filters.http.gcp_authn": "//source/extensions/filters/http/gcp_authn:config", - "envoy.filters.http.grpc_http1_bridge": "//source/extensions/filters/http/grpc_http1_bridge:config", - "envoy.filters.http.grpc_http1_reverse_bridge": "//source/extensions/filters/http/grpc_http1_reverse_bridge:config", - "envoy.filters.http.grpc_json_transcoder": "//source/extensions/filters/http/grpc_json_transcoder:config", - "envoy.filters.http.grpc_stats": "//source/extensions/filters/http/grpc_stats:config", - "envoy.filters.http.grpc_web": "//source/extensions/filters/http/grpc_web:config", - "envoy.filters.http.header_to_metadata": "//source/extensions/filters/http/header_to_metadata:config", - "envoy.filters.http.health_check": "//source/extensions/filters/http/health_check:config", - "envoy.filters.http.ip_tagging": "//source/extensions/filters/http/ip_tagging:config", - "envoy.filters.http.jwt_authn": "//source/extensions/filters/http/jwt_authn:config", + "envoy.filters.http.adaptive_concurrency": "//source/extensions/filters/http/adaptive_concurrency:config", + "envoy.filters.http.admission_control": "//source/extensions/filters/http/admission_control:config", + "envoy.filters.http.alternate_protocols_cache": "//source/extensions/filters/http/alternate_protocols_cache:config", + "envoy.filters.http.aws_lambda": "//source/extensions/filters/http/aws_lambda:config", + "envoy.filters.http.aws_request_signing": "//source/extensions/filters/http/aws_request_signing:config", + "envoy.filters.http.bandwidth_limit": "//source/extensions/filters/http/bandwidth_limit:config", + "envoy.filters.http.buffer": "//source/extensions/filters/http/buffer:config", + "envoy.filters.http.cache": "//source/extensions/filters/http/cache:config", + "envoy.filters.http.cdn_loop": "//source/extensions/filters/http/cdn_loop:config", + "envoy.filters.http.compressor": "//source/extensions/filters/http/compressor:config", + "envoy.filters.http.cors": "//source/extensions/filters/http/cors:config", + "envoy.filters.http.composite": "//source/extensions/filters/http/composite:config", + "envoy.filters.http.csrf": "//source/extensions/filters/http/csrf:config", + "envoy.filters.http.decompressor": "//source/extensions/filters/http/decompressor:config", + "envoy.filters.http.dynamic_forward_proxy": "//source/extensions/filters/http/dynamic_forward_proxy:config", + "envoy.filters.http.ext_authz": "//source/extensions/filters/http/ext_authz:config", + "envoy.filters.http.ext_proc": "//source/extensions/filters/http/ext_proc:config", + "envoy.filters.http.fault": "//source/extensions/filters/http/fault:config", + "envoy.filters.http.file_system_buffer": "//source/extensions/filters/http/file_system_buffer:config", + "envoy.filters.http.gcp_authn": "//source/extensions/filters/http/gcp_authn:config", + "envoy.filters.http.grpc_http1_bridge": "//source/extensions/filters/http/grpc_http1_bridge:config", + "envoy.filters.http.grpc_http1_reverse_bridge": "//source/extensions/filters/http/grpc_http1_reverse_bridge:config", + "envoy.filters.http.grpc_json_transcoder": "//source/extensions/filters/http/grpc_json_transcoder:config", + "envoy.filters.http.grpc_stats": "//source/extensions/filters/http/grpc_stats:config", + "envoy.filters.http.grpc_web": "//source/extensions/filters/http/grpc_web:config", + "envoy.filters.http.header_to_metadata": "//source/extensions/filters/http/header_to_metadata:config", + "envoy.filters.http.health_check": "//source/extensions/filters/http/health_check:config", + "envoy.filters.http.ip_tagging": "//source/extensions/filters/http/ip_tagging:config", + "envoy.filters.http.jwt_authn": "//source/extensions/filters/http/jwt_authn:config", # Disabled by default - "envoy.filters.http.kill_request": "//source/extensions/filters/http/kill_request:kill_request_config", - "envoy.filters.http.local_ratelimit": "//source/extensions/filters/http/local_ratelimit:config", - "envoy.filters.http.lua": "//source/extensions/filters/http/lua:config", - "envoy.filters.http.oauth2": "//source/extensions/filters/http/oauth2:config", - "envoy.filters.http.on_demand": "//source/extensions/filters/http/on_demand:config", - "envoy.filters.http.original_src": "//source/extensions/filters/http/original_src:config", - "envoy.filters.http.ratelimit": "//source/extensions/filters/http/ratelimit:config", - "envoy.filters.http.rbac": "//source/extensions/filters/http/rbac:config", - "envoy.filters.http.router": "//source/extensions/filters/http/router:config", - "envoy.filters.http.set_metadata": "//source/extensions/filters/http/set_metadata:config", - "envoy.filters.http.tap": "//source/extensions/filters/http/tap:config", - "envoy.filters.http.wasm": "//source/extensions/filters/http/wasm:config", - "envoy.filters.http.stateful_session": "//source/extensions/filters/http/stateful_session:config", + "envoy.filters.http.kill_request": "//source/extensions/filters/http/kill_request:kill_request_config", + "envoy.filters.http.local_ratelimit": "//source/extensions/filters/http/local_ratelimit:config", + "envoy.filters.http.lua": "//source/extensions/filters/http/lua:config", + "envoy.filters.http.oauth2": "//source/extensions/filters/http/oauth2:config", + "envoy.filters.http.on_demand": "//source/extensions/filters/http/on_demand:config", + "envoy.filters.http.original_src": "//source/extensions/filters/http/original_src:config", + "envoy.filters.http.ratelimit": "//source/extensions/filters/http/ratelimit:config", + "envoy.filters.http.rbac": "//source/extensions/filters/http/rbac:config", + "envoy.filters.http.router": "//source/extensions/filters/http/router:config", + "envoy.filters.http.set_metadata": "//source/extensions/filters/http/set_metadata:config", + "envoy.filters.http.tap": "//source/extensions/filters/http/tap:config", + "envoy.filters.http.wasm": "//source/extensions/filters/http/wasm:config", + "envoy.filters.http.stateful_session": "//source/extensions/filters/http/stateful_session:config", # # Listener filters # - - "envoy.filters.listener.http_inspector": "//source/extensions/filters/listener/http_inspector:config", + "envoy.filters.listener.http_inspector": "//source/extensions/filters/listener/http_inspector:config", # NOTE: The original_dst filter is implicitly loaded if original_dst functionality is # configured on the listener. Do not remove it in that case or configs will fail to load. - "envoy.filters.listener.original_dst": "//source/extensions/filters/listener/original_dst:config", - "envoy.filters.listener.original_src": "//source/extensions/filters/listener/original_src:config", + "envoy.filters.listener.original_dst": "//source/extensions/filters/listener/original_dst:config", + "envoy.filters.listener.original_src": "//source/extensions/filters/listener/original_src:config", # NOTE: The proxy_protocol filter is implicitly loaded if proxy_protocol functionality is # configured on the listener. Do not remove it in that case or configs will fail to load. - "envoy.filters.listener.proxy_protocol": "//source/extensions/filters/listener/proxy_protocol:config", - "envoy.filters.listener.tls_inspector": "//source/extensions/filters/listener/tls_inspector:config", + "envoy.filters.listener.proxy_protocol": "//source/extensions/filters/listener/proxy_protocol:config", + "envoy.filters.listener.tls_inspector": "//source/extensions/filters/listener/tls_inspector:config", # # Network filters # - - "envoy.filters.network.connection_limit": "//source/extensions/filters/network/connection_limit:config", - "envoy.filters.network.direct_response": "//source/extensions/filters/network/direct_response:config", - "envoy.filters.network.dubbo_proxy": "//source/extensions/filters/network/dubbo_proxy:config", - "envoy.filters.network.echo": "//source/extensions/filters/network/echo:config", - "envoy.filters.network.ext_authz": "//source/extensions/filters/network/ext_authz:config", - "envoy.filters.network.http_connection_manager": "//source/extensions/filters/network/http_connection_manager:config", - "envoy.filters.network.local_ratelimit": "//source/extensions/filters/network/local_ratelimit:config", - "envoy.filters.network.mongo_proxy": "//source/extensions/filters/network/mongo_proxy:config", - "envoy.filters.network.ratelimit": "//source/extensions/filters/network/ratelimit:config", - "envoy.filters.network.rbac": "//source/extensions/filters/network/rbac:config", - "envoy.filters.network.redis_proxy": "//source/extensions/filters/network/redis_proxy:config", - "envoy.filters.network.tcp_proxy": "//source/extensions/filters/network/tcp_proxy:config", - "envoy.filters.network.thrift_proxy": "//source/extensions/filters/network/thrift_proxy:config", - "envoy.filters.network.sni_cluster": "//source/extensions/filters/network/sni_cluster:config", - "envoy.filters.network.sni_dynamic_forward_proxy": "//source/extensions/filters/network/sni_dynamic_forward_proxy:config", - "envoy.filters.network.wasm": "//source/extensions/filters/network/wasm:config", - "envoy.filters.network.zookeeper_proxy": "//source/extensions/filters/network/zookeeper_proxy:config", + "envoy.filters.network.connection_limit": "//source/extensions/filters/network/connection_limit:config", + "envoy.filters.network.direct_response": "//source/extensions/filters/network/direct_response:config", + "envoy.filters.network.dubbo_proxy": "//source/extensions/filters/network/dubbo_proxy:config", + "envoy.filters.network.echo": "//source/extensions/filters/network/echo:config", + "envoy.filters.network.ext_authz": "//source/extensions/filters/network/ext_authz:config", + "envoy.filters.network.http_connection_manager": "//source/extensions/filters/network/http_connection_manager:config", + "envoy.filters.network.local_ratelimit": "//source/extensions/filters/network/local_ratelimit:config", + "envoy.filters.network.mongo_proxy": "//source/extensions/filters/network/mongo_proxy:config", + "envoy.filters.network.ratelimit": "//source/extensions/filters/network/ratelimit:config", + "envoy.filters.network.rbac": "//source/extensions/filters/network/rbac:config", + "envoy.filters.network.redis_proxy": "//source/extensions/filters/network/redis_proxy:config", + "envoy.filters.network.tcp_proxy": "//source/extensions/filters/network/tcp_proxy:config", + "envoy.filters.network.thrift_proxy": "//source/extensions/filters/network/thrift_proxy:config", + "envoy.filters.network.sni_cluster": "//source/extensions/filters/network/sni_cluster:config", + "envoy.filters.network.sni_dynamic_forward_proxy": "//source/extensions/filters/network/sni_dynamic_forward_proxy:config", + "envoy.filters.network.wasm": "//source/extensions/filters/network/wasm:config", + "envoy.filters.network.zookeeper_proxy": "//source/extensions/filters/network/zookeeper_proxy:config", # # UDP filters # - - "envoy.filters.udp.dns_filter": "//source/extensions/filters/udp/dns_filter:config", - "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", + "envoy.filters.udp.dns_filter": "//source/extensions/filters/udp/dns_filter:config", + "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", # # Resource monitors # - - "envoy.resource_monitors.fixed_heap": "//source/extensions/resource_monitors/fixed_heap:config", - "envoy.resource_monitors.injected_resource": "//source/extensions/resource_monitors/injected_resource:config", + "envoy.resource_monitors.fixed_heap": "//source/extensions/resource_monitors/fixed_heap:config", + "envoy.resource_monitors.injected_resource": "//source/extensions/resource_monitors/injected_resource:config", # # Stat sinks # - - "envoy.stat_sinks.dog_statsd": "//source/extensions/stat_sinks/dog_statsd:config", - "envoy.stat_sinks.graphite_statsd": "//source/extensions/stat_sinks/graphite_statsd:config", - "envoy.stat_sinks.hystrix": "//source/extensions/stat_sinks/hystrix:config", - "envoy.stat_sinks.metrics_service": "//source/extensions/stat_sinks/metrics_service:config", - "envoy.stat_sinks.statsd": "//source/extensions/stat_sinks/statsd:config", - "envoy.stat_sinks.wasm": "//source/extensions/stat_sinks/wasm:config", + "envoy.stat_sinks.dog_statsd": "//source/extensions/stat_sinks/dog_statsd:config", + "envoy.stat_sinks.graphite_statsd": "//source/extensions/stat_sinks/graphite_statsd:config", + "envoy.stat_sinks.hystrix": "//source/extensions/stat_sinks/hystrix:config", + "envoy.stat_sinks.metrics_service": "//source/extensions/stat_sinks/metrics_service:config", + "envoy.stat_sinks.statsd": "//source/extensions/stat_sinks/statsd:config", + "envoy.stat_sinks.wasm": "//source/extensions/stat_sinks/wasm:config", # # Thrift filters # - - "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", - "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", - "envoy.filters.thrift.rate_limit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", + "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", + "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", + "envoy.filters.thrift.rate_limit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", # # Tracers # - - "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", - "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", - "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", - "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", - "envoy.tracers.xray": "//source/extensions/tracers/xray:config", - "envoy.tracers.skywalking": "//source/extensions/tracers/skywalking:config", - "envoy.tracers.opentelemetry": "//source/extensions/tracers/opentelemetry:config", + "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", + "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", + "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", + "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", + "envoy.tracers.xray": "//source/extensions/tracers/xray:config", + "envoy.tracers.skywalking": "//source/extensions/tracers/skywalking:config", + "envoy.tracers.opentelemetry": "//source/extensions/tracers/opentelemetry:config", # # Transport sockets # - - "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", - "envoy.transport_sockets.http_11_proxy": "//source/extensions/transport_sockets/http_11_proxy:upstream_config", - "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", - "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", - "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", - "envoy.transport_sockets.starttls": "//source/extensions/transport_sockets/starttls:config", - "envoy.transport_sockets.tcp_stats": "//source/extensions/transport_sockets/tcp_stats:config", - "envoy.transport_sockets.internal_upstream": "//source/extensions/transport_sockets/internal_upstream:config", + "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", + "envoy.transport_sockets.http_11_proxy": "//source/extensions/transport_sockets/http_11_proxy:upstream_config", + "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", + "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", + "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", + "envoy.transport_sockets.starttls": "//source/extensions/transport_sockets/starttls:config", + "envoy.transport_sockets.tcp_stats": "//source/extensions/transport_sockets/tcp_stats:config", + "envoy.transport_sockets.internal_upstream": "//source/extensions/transport_sockets/internal_upstream:config", # # Retry host predicates # - - "envoy.retry_host_predicates.previous_hosts": "//source/extensions/retry/host/previous_hosts:config", - "envoy.retry_host_predicates.omit_canary_hosts": "//source/extensions/retry/host/omit_canary_hosts:config", - "envoy.retry_host_predicates.omit_host_metadata": "//source/extensions/retry/host/omit_host_metadata:config", + "envoy.retry_host_predicates.previous_hosts": "//source/extensions/retry/host/previous_hosts:config", + "envoy.retry_host_predicates.omit_canary_hosts": "//source/extensions/retry/host/omit_canary_hosts:config", + "envoy.retry_host_predicates.omit_host_metadata": "//source/extensions/retry/host/omit_host_metadata:config", # # Retry priorities # - - "envoy.retry_priorities.previous_priorities": "//source/extensions/retry/priority/previous_priorities:config", + "envoy.retry_priorities.previous_priorities": "//source/extensions/retry/priority/previous_priorities:config", # # CacheFilter plugins # - "envoy.extensions.http.cache.simple": "//source/extensions/filters/http/cache/simple_http_cache:config", + "envoy.extensions.http.cache.simple": "//source/extensions/filters/http/cache/simple_http_cache:config", # # Internal redirect predicates # - "envoy.internal_redirect_predicates.allow_listed_routes": "//source/extensions/internal_redirect/allow_listed_routes:config", - "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", - "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", + "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", + "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", # # Path Pattern Match and Path Pattern Rewrite # - "envoy.path.match.pattern_template.pattern_template_matcher": "//source/extensions/path/match/pattern_template:config", - "envoy.path.rewrite.pattern_template.pattern_template_rewriter": "//source/extensions/path/rewrite/pattern_template:config", + "envoy.path.match.uri_template.uri_template_matcher": "//source/extensions/path/match/uri_template:config", + "envoy.path.rewrite.uri_template.uri_template_rewriter": "//source/extensions/path/rewrite/uri_template:config", # # Http Upstreams (excepting envoy.upstreams.http.generic which is hard-coded into the build so not registered here) # - - "envoy.upstreams.http.http": "//source/extensions/upstreams/http/http:config", - "envoy.upstreams.http.tcp": "//source/extensions/upstreams/http/tcp:config", + "envoy.upstreams.http.http": "//source/extensions/upstreams/http/http:config", + "envoy.upstreams.http.tcp": "//source/extensions/upstreams/http/tcp:config", # # Watchdog actions # - - "envoy.watchdog.profile_action": "//source/extensions/watchdog/profile_action:config", + "envoy.watchdog.profile_action": "//source/extensions/watchdog/profile_action:config", # # WebAssembly runtimes # - - "envoy.wasm.runtime.null": "//source/extensions/wasm_runtime/null:config", - "envoy.wasm.runtime.v8": "//source/extensions/wasm_runtime/v8:config", - "envoy.wasm.runtime.wamr": "//source/extensions/wasm_runtime/wamr:config", - "envoy.wasm.runtime.wavm": "//source/extensions/wasm_runtime/wavm:config", - "envoy.wasm.runtime.wasmtime": "//source/extensions/wasm_runtime/wasmtime:config", + "envoy.wasm.runtime.null": "//source/extensions/wasm_runtime/null:config", + "envoy.wasm.runtime.v8": "//source/extensions/wasm_runtime/v8:config", + "envoy.wasm.runtime.wamr": "//source/extensions/wasm_runtime/wamr:config", + "envoy.wasm.runtime.wavm": "//source/extensions/wasm_runtime/wavm:config", + "envoy.wasm.runtime.wasmtime": "//source/extensions/wasm_runtime/wasmtime:config", # # Rate limit descriptors # - - "envoy.rate_limit_descriptors.expr": "//source/extensions/rate_limit_descriptors/expr:config", + "envoy.rate_limit_descriptors.expr": "//source/extensions/rate_limit_descriptors/expr:config", # # IO socket # - - "envoy.io_socket.user_space": "//source/extensions/io_socket/user_space:config", - "envoy.bootstrap.internal_listener": "//source/extensions/bootstrap/internal_listener:config", + "envoy.io_socket.user_space": "//source/extensions/io_socket/user_space:config", + "envoy.bootstrap.internal_listener": "//source/extensions/bootstrap/internal_listener:config", # # TLS peer certification validators # - - "envoy.tls.cert_validator.spiffe": "//source/extensions/transport_sockets/tls/cert_validator/spiffe:config", + "envoy.tls.cert_validator.spiffe": "//source/extensions/transport_sockets/tls/cert_validator/spiffe:config", # # HTTP header formatters # - - "envoy.http.stateful_header_formatters.preserve_case": "//source/extensions/http/header_formatters/preserve_case:config", + "envoy.http.stateful_header_formatters.preserve_case": "//source/extensions/http/header_formatters/preserve_case:config", # # Original IP detection # - - "envoy.http.original_ip_detection.custom_header": "//source/extensions/http/original_ip_detection/custom_header:config", - "envoy.http.original_ip_detection.xff": "//source/extensions/http/original_ip_detection/xff:config", + "envoy.http.original_ip_detection.custom_header": "//source/extensions/http/original_ip_detection/custom_header:config", + "envoy.http.original_ip_detection.xff": "//source/extensions/http/original_ip_detection/xff:config", # # Stateful session # - - "envoy.http.stateful_session.cookie": "//source/extensions/http/stateful_session/cookie:config", + "envoy.http.stateful_session.cookie": "//source/extensions/http/stateful_session/cookie:config", # # QUIC extensions # - - "envoy.quic.crypto_stream.server.quiche": "//source/extensions/quic/crypto_stream:envoy_quic_default_crypto_server_stream", - "envoy.quic.proof_source.filter_chain": "//source/extensions/quic/proof_source:envoy_quic_default_proof_source", + "envoy.quic.crypto_stream.server.quiche": "//source/extensions/quic/crypto_stream:envoy_quic_default_crypto_server_stream", + "envoy.quic.proof_source.filter_chain": "//source/extensions/quic/proof_source:envoy_quic_default_proof_source", # # UDP packet writers # - "envoy.udp_packet_writer.default": "//source/extensions/udp_packet_writer/default:config", - "envoy.udp_packet_writer.gso": "//source/extensions/udp_packet_writer/gso:config", + "envoy.udp_packet_writer.default": "//source/extensions/udp_packet_writer/default:config", + "envoy.udp_packet_writer.gso": "//source/extensions/udp_packet_writer/gso:config", # # Formatter # - - "envoy.formatter.metadata": "//source/extensions/formatter/metadata:config", - "envoy.formatter.req_without_query": "//source/extensions/formatter/req_without_query:config", + "envoy.formatter.metadata": "//source/extensions/formatter/metadata:config", + "envoy.formatter.req_without_query": "//source/extensions/formatter/req_without_query:config", # # Key value store # - - "envoy.key_value.file_based": "//source/extensions/key_value/file_based:config_lib", + "envoy.key_value.file_based": "//source/extensions/key_value/file_based:config_lib", # # RBAC matchers # - - "envoy.rbac.matchers.upstream_ip_port": "//source/extensions/filters/common/rbac/matchers:upstream_ip_port_lib", + "envoy.rbac.matchers.upstream_ip_port": "//source/extensions/filters/common/rbac/matchers:upstream_ip_port_lib", # # DNS Resolver # # c-ares DNS resolver extension is recommended to be enabled to maintain the legacy DNS resolving behavior. - "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", + "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", # apple DNS resolver extension is only needed in MacOS build plus one want to use apple library for DNS resolving. - "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", + "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", # getaddrinfo DNS resolver extension can be used when the system resolver is desired (e.g., Android) - "envoy.network.dns_resolver.getaddrinfo": "//source/extensions/network/dns_resolver/getaddrinfo:config", + "envoy.network.dns_resolver.getaddrinfo": "//source/extensions/network/dns_resolver/getaddrinfo:config", # # Custom matchers # - - "envoy.matching.custom_matchers.trie_matcher": "//source/extensions/common/matcher:trie_matcher_lib", + "envoy.matching.custom_matchers.trie_matcher": "//source/extensions/common/matcher:trie_matcher_lib", # # Header Validators # - - "envoy.http.header_validators.envoy_default": "//source/extensions/http/header_validators/envoy_default:config", + "envoy.http.header_validators.envoy_default": "//source/extensions/http/header_validators/envoy_default:config", # # Early Data option # - - "envoy.route.early_data_policy.default": "//source/extensions/early_data:default_early_data_policy_lib", + "envoy.route.early_data_policy.default": "//source/extensions/early_data:default_early_data_policy_lib", } # These can be changed to ["//visibility:public"], for downstream builds which diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index 7b67f60f1ed78..e67ee05ca1607 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -746,22 +746,22 @@ envoy.matching.matchers.ip: status: stable type_urls: - envoy.extensions.matching.input_matchers.ip.v3.Ip -envoy.path.match.pattern_template.pattern_template_matcher: +envoy.path.match.pattern_template.uri_template_matcher: categories: - envoy.path.match security_posture: unknown status: stable undocumented: true type_urls: - - envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig -envoy.path.rewrite.pattern_template.pattern_template_rewriter: + - envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig +envoy.path.rewrite.pattern_template.uri_template_rewriter: categories: - envoy.path.rewrite security_posture: unknown status: stable undocumented: true type_urls: - - envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + - envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig envoy.quic.proof_source.filter_chain: categories: - envoy.quic.proof_source diff --git a/source/extensions/path/match/pattern_template/BUILD b/source/extensions/path/match/uri_template/BUILD similarity index 60% rename from source/extensions/path/match/pattern_template/BUILD rename to source/extensions/path/match/uri_template/BUILD index 2171f8b8ae2d6..a8a0b7c25255b 100644 --- a/source/extensions/path/match/pattern_template/BUILD +++ b/source/extensions/path/match/uri_template/BUILD @@ -12,21 +12,21 @@ licenses(["notice"]) # Apache 2 envoy_extension_package() envoy_cc_library( - name = "pattern_template_match_lib", - srcs = ["pattern_template_match.cc"], - hdrs = ["pattern_template_match.h"], + name = "uri_template_match_lib", + srcs = ["uri_template_match.cc"], + hdrs = ["uri_template_match.h"], visibility = [ "//source/common/router:__subpackages__", - "//source/extensions/path/rewrite/pattern_template:__subpackages__", - "//test/extensions/path/match/pattern_template:__subpackages__", - "//test/extensions/path/rewrite/pattern_template:__subpackages__", + "//source/extensions/path/rewrite/uri_template:__subpackages__", + "//test/extensions/path/match/uri_template:__subpackages__", + "//test/extensions/path/rewrite/uri_template:__subpackages__", ], deps = [ "//envoy/router:router_path_match_policy_interface", "//source/common/protobuf", "//source/common/protobuf:utility_lib", "//source/extensions/path/uri_template_lib", - "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/match/uri_template/v3:pkg_cc_proto", ], ) @@ -36,10 +36,10 @@ envoy_cc_extension( hdrs = ["config.h"], visibility = ["//visibility:public"], deps = [ - ":pattern_template_match_lib", + ":uri_template_match_lib", "//envoy/registry", "//envoy/router:router_path_match_policy_interface", "//source/extensions/path/uri_template_lib", - "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/match/uri_template/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/match/pattern_template/config.cc b/source/extensions/path/match/uri_template/config.cc similarity index 64% rename from source/extensions/path/match/pattern_template/config.cc rename to source/extensions/path/match/uri_template/config.cc index cc78366ced7c1..b5ba82e0b0798 100644 --- a/source/extensions/path/match/pattern_template/config.cc +++ b/source/extensions/path/match/uri_template/config.cc @@ -1,4 +1,4 @@ -#include "source/extensions/path/match/pattern_template/config.h" +#include "source/extensions/path/match/uri_template/config.h" #include "envoy/registry/registry.h" #include "envoy/router/path_matcher.h" @@ -8,7 +8,7 @@ namespace Extensions { namespace UriTemplate { namespace Match { -REGISTER_FACTORY(PatternTemplateMatcherFactory, Router::PathMatcherFactory); +REGISTER_FACTORY(UriTemplateMatcherFactory, Router::PathMatcherFactory); } // namespace Match } // namespace UriTemplate diff --git a/source/extensions/path/match/pattern_template/config.h b/source/extensions/path/match/uri_template/config.h similarity index 58% rename from source/extensions/path/match/pattern_template/config.h rename to source/extensions/path/match/uri_template/config.h index 6ee5c86a0a797..12d4a25cc764f 100644 --- a/source/extensions/path/match/pattern_template/config.h +++ b/source/extensions/path/match/uri_template/config.h @@ -1,42 +1,42 @@ #pragma once -#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" -#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" +#include "envoy/extensions/path/match/uri_template/v3/uri_template_match.pb.h" +#include "envoy/extensions/path/match/uri_template/v3/uri_template_match.pb.validate.h" #include "envoy/router/path_matcher.h" #include "source/common/protobuf/message_validator_impl.h" #include "source/common/protobuf/utility.h" -#include "source/extensions/path/match/pattern_template/pattern_template_match.h" +#include "source/extensions/path/match/uri_template/uri_template_match.h" namespace Envoy { namespace Extensions { namespace UriTemplate { namespace Match { -class PatternTemplateMatcherFactory : public Router::PathMatcherFactory { +class UriTemplateMatcherFactory : public Router::PathMatcherFactory { public: absl::StatusOr createPathMatcher(const Protobuf::Message& config) override { auto path_match_config = MessageUtil::downcastAndValidate< - const envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig&>( + const envoy::extensions::path::match::uri_template::v3::UriTemplateMatchConfig&>( config, ProtobufMessage::getStrictValidationVisitor()); if (!UriTemplate::isValidMatchPattern(path_match_config.path_template()).ok()) { - return absl::InvalidArgumentError(fmt::format("path_match_policy.path_template {} is invalid", + return absl::InvalidArgumentError(fmt::format("path_match_policy.uri_template {} is invalid", path_match_config.path_template())); } - return std::make_shared(path_match_config); + return std::make_shared(path_match_config); } // Router::PathMatcherFactory ProtobufTypes::MessagePtr createEmptyConfigProto() override { return std::make_unique< - envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig>(); + envoy::extensions::path::match::uri_template::v3::UriTemplateMatchConfig>(); } std::string name() const override { - return "envoy.path.match.pattern_template.pattern_template_matcher"; + return "envoy.path.match.uri_template.uri_template_matcher"; } }; diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.cc b/source/extensions/path/match/uri_template/uri_template_match.cc similarity index 74% rename from source/extensions/path/match/pattern_template/pattern_template_match.cc rename to source/extensions/path/match/uri_template/uri_template_match.cc index 76cf86697e3f5..e9df81aa1b713 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.cc +++ b/source/extensions/path/match/uri_template/uri_template_match.cc @@ -1,4 +1,4 @@ -#include "source/extensions/path/match/pattern_template/pattern_template_match.h" +#include "source/extensions/path/match/uri_template/uri_template_match.h" #include #include @@ -17,13 +17,13 @@ namespace Extensions { namespace UriTemplate { namespace Match { -bool PatternTemplateMatcher::match(absl::string_view path) const { +bool UriTemplateMatcher::match(absl::string_view path) const { RE2 matching_pattern_regex = RE2(convertPathPatternSyntaxToRegex(path_template_).value()); return RE2::FullMatch(Internal::toStringPiece(Http::PathUtil::removeQueryAndFragment(path)), matching_pattern_regex); } -absl::string_view PatternTemplateMatcher::pattern() const { return path_template_; } +absl::string_view UriTemplateMatcher::uri_template() const { return path_template_; } } // namespace Match } // namespace UriTemplate diff --git a/source/extensions/path/match/pattern_template/pattern_template_match.h b/source/extensions/path/match/uri_template/uri_template_match.h similarity index 60% rename from source/extensions/path/match/pattern_template/pattern_template_match.h rename to source/extensions/path/match/uri_template/uri_template_match.h index 8f201615dfc5c..be082e5e10985 100644 --- a/source/extensions/path/match/pattern_template/pattern_template_match.h +++ b/source/extensions/path/match/uri_template/uri_template_match.h @@ -2,7 +2,7 @@ #include -#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" +#include "envoy/extensions/path/match/uri_template/v3/uri_template_match.pb.h" #include "envoy/router/path_matcher.h" #include "source/extensions/path/uri_template_lib/uri_template.h" @@ -15,27 +15,27 @@ namespace Extensions { namespace UriTemplate { namespace Match { -const absl::string_view NAME = "envoy.path.match.pattern_template.pattern_template_matcher"; +const absl::string_view NAME = "envoy.path.match.uri_template.uri_template_matcher"; /** - * PatternTemplateMatcher allows matching based on pattern templates. - * Examples of several pattern templates types are below: + * UriTemplateMatcher allows matching based on uri templates. + * Examples of several uri templates types are below: * Variable: /foo/bar/{var} * Will match any path that starts with /foo/bar and has one additional segment * Literal: /foo/bar/goo * Will match only a path of /foo/bar/goo */ -class PatternTemplateMatcher : public Router::PathMatcher { +class UriTemplateMatcher : public Router::PathMatcher { public: - explicit PatternTemplateMatcher( - const envoy::extensions::path::match::pattern_template::v3::PatternTemplateMatchConfig& + explicit UriTemplateMatcher( + const envoy::extensions::path::match::uri_template::v3::UriTemplateMatchConfig& config) : path_template_(config.path_template()) {} // Router::PathMatcher bool match(absl::string_view path) const override; - absl::string_view pattern() const override; + absl::string_view uri_template() const override; absl::string_view name() const override { return NAME; } diff --git a/source/extensions/path/rewrite/pattern_template/BUILD b/source/extensions/path/rewrite/uri_template/BUILD similarity index 60% rename from source/extensions/path/rewrite/pattern_template/BUILD rename to source/extensions/path/rewrite/uri_template/BUILD index ede4bd47a243b..85835bf510e61 100644 --- a/source/extensions/path/rewrite/pattern_template/BUILD +++ b/source/extensions/path/rewrite/uri_template/BUILD @@ -12,22 +12,22 @@ licenses(["notice"]) # Apache 2 envoy_extension_package() envoy_cc_library( - name = "pattern_template_rewrite_lib", - srcs = ["pattern_template_rewrite.cc"], - hdrs = ["pattern_template_rewrite.h"], + name = "uri_template_rewrite_lib", + srcs = ["uri_template_rewrite.cc"], + hdrs = ["uri_template_rewrite.h"], visibility = [ "//source/common/router:__subpackages__", - "//test/extensions/path/rewrite/pattern_template:__subpackages__", + "//test/extensions/path/rewrite/uri_template:__subpackages__", ], deps = [ "//envoy/router:router_path_match_policy_interface", "//envoy/router:router_path_rewrite_policy_interface", "//source/common/protobuf", "//source/common/protobuf:utility_lib", - "//source/extensions/path/match/pattern_template:pattern_template_match_lib", + "//source/extensions/path/match/uri_template:uri_template_match_lib", "//source/extensions/path/uri_template_lib", - "@envoy_api//envoy/extensions/path/match/pattern_template/v3:pkg_cc_proto", - "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/match/uri_template/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/rewrite/uri_template/v3:pkg_cc_proto", ], ) @@ -37,10 +37,10 @@ envoy_cc_extension( hdrs = ["config.h"], visibility = ["//visibility:public"], deps = [ - ":pattern_template_rewrite_lib", + ":uri_template_rewrite_lib", "//envoy/registry", "//envoy/router:router_path_rewrite_policy_interface", "//source/extensions/path/uri_template_lib", - "@envoy_api//envoy/extensions/path/rewrite/pattern_template/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/path/rewrite/uri_template/v3:pkg_cc_proto", ], ) diff --git a/source/extensions/path/rewrite/pattern_template/config.cc b/source/extensions/path/rewrite/uri_template/config.cc similarity index 64% rename from source/extensions/path/rewrite/pattern_template/config.cc rename to source/extensions/path/rewrite/uri_template/config.cc index 10c36835cee2b..5a2bd14460767 100644 --- a/source/extensions/path/rewrite/pattern_template/config.cc +++ b/source/extensions/path/rewrite/uri_template/config.cc @@ -1,4 +1,4 @@ -#include "source/extensions/path/rewrite/pattern_template/config.h" +#include "source/extensions/path/rewrite/uri_template/config.h" #include "envoy/registry/registry.h" #include "envoy/router/path_rewriter.h" @@ -8,7 +8,7 @@ namespace Extensions { namespace UriTemplate { namespace Rewrite { -REGISTER_FACTORY(PatternTemplateRewriterFactory, Router::PathRewriterFactory); +REGISTER_FACTORY(UriTemplateRewriterFactory, Router::PathRewriterFactory); } // namespace Rewrite } // namespace UriTemplate diff --git a/source/extensions/path/rewrite/pattern_template/config.h b/source/extensions/path/rewrite/uri_template/config.h similarity index 58% rename from source/extensions/path/rewrite/pattern_template/config.h rename to source/extensions/path/rewrite/uri_template/config.h index 91f53b4ffb8bc..9df7c89b24773 100644 --- a/source/extensions/path/rewrite/pattern_template/config.h +++ b/source/extensions/path/rewrite/uri_template/config.h @@ -1,23 +1,23 @@ #pragma once -#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" -#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" +#include "envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.pb.h" +#include "envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.pb.validate.h" #include "envoy/router/path_rewriter.h" -#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" +#include "source/extensions/path/rewrite/uri_template/uri_template_rewrite.h" namespace Envoy { namespace Extensions { namespace UriTemplate { namespace Rewrite { -class PatternTemplateRewriterFactory : public Router::PathRewriterFactory { +class UriTemplateRewriterFactory : public Router::PathRewriterFactory { public: absl::StatusOr createPathRewriter(const Protobuf::Message& rewrite_config) override { auto path_rewrite_config = - MessageUtil::downcastAndValidate( + MessageUtil::downcastAndValidate( rewrite_config, ProtobufMessage::getStrictValidationVisitor()); if (!UriTemplate::isValidRewritePattern(path_rewrite_config.path_template_rewrite()).ok()) { @@ -25,17 +25,17 @@ class PatternTemplateRewriterFactory : public Router::PathRewriterFactory { fmt::format("path_rewrite_policy.path_template_rewrite {} is invalid", path_rewrite_config.path_template_rewrite())); } - return std::make_shared(path_rewrite_config); + return std::make_shared(path_rewrite_config); } // Router::PathRewriterFactory ProtobufTypes::MessagePtr createEmptyConfigProto() override { return std::make_unique< - envoy::extensions::path::rewrite::pattern_template::v3::PatternTemplateRewriteConfig>(); + envoy::extensions::path::rewrite::uri_template::v3::UriTemplateRewriteConfig>(); } std::string name() const override { - return "envoy.path.rewrite.pattern_template.pattern_template_rewriter"; + return "envoy.path.rewrite.uri_template.uri_template_rewriter"; } }; diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc similarity index 88% rename from source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc rename to source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc index c3d58604c50c1..93aff9732a866 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.cc +++ b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc @@ -1,4 +1,4 @@ -#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" +#include "source/extensions/path/rewrite/uri_template/uri_template_rewrite.h" #include #include @@ -6,7 +6,7 @@ #include #include "source/common/http/path_utility.h" -#include "source/extensions/path/match/pattern_template/pattern_template_match.h" +#include "source/extensions/path/match/uri_template/uri_template_match.h" #include "source/extensions/path/uri_template_lib/uri_template_internal.h" #include "absl/status/statusor.h" @@ -25,7 +25,7 @@ namespace Rewrite { #endif absl::Status -PatternTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher, +UriTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher, bool active_matcher) const { if (!active_matcher || path_matcher->name() != Extensions::UriTemplate::Match::NAME) { return absl::InvalidArgumentError(fmt::format("unable to use {} extension without {} extension", @@ -35,17 +35,17 @@ PatternTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSharedPtr pa // This is needed to match up variable values. // Validation between extensions as they share rewrite pattern variables. - if (!isValidSharedVariableSet(rewrite_pattern_, path_matcher->pattern()).ok()) { + if (!isValidSharedVariableSet(rewrite_pattern_, path_matcher->uri_template()).ok()) { return absl::InvalidArgumentError( fmt::format("mismatch between variables in path_match_policy {} and path_rewrite_policy {}", - path_matcher->pattern(), rewrite_pattern_)); + path_matcher->uri_template(), rewrite_pattern_)); } return absl::OkStatus(); } absl::StatusOr -PatternTemplateRewriter::rewritePath(absl::string_view pattern, +UriTemplateRewriter::rewritePath(absl::string_view pattern, absl::string_view matched_path) const { absl::StatusOr regex_pattern = convertPathPatternSyntaxToRegex(matched_path); if (!regex_pattern.ok()) { diff --git a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.h similarity index 61% rename from source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h rename to source/extensions/path/rewrite/uri_template/uri_template_rewrite.h index ffb5ef2107065..34e911d884491 100644 --- a/source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h +++ b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.h @@ -2,10 +2,10 @@ #include -#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.h" -#include "envoy/extensions/path/match/pattern_template/v3/pattern_template_match.pb.validate.h" -#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.h" -#include "envoy/extensions/path/rewrite/pattern_template/v3/pattern_template_rewrite.pb.validate.h" +#include "envoy/extensions/path/match/uri_template/v3/uri_template_match.pb.h" +#include "envoy/extensions/path/match/uri_template/v3/uri_template_match.pb.validate.h" +#include "envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.pb.h" +#include "envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.pb.validate.h" #include "envoy/router/path_matcher.h" #include "envoy/router/path_rewriter.h" @@ -21,27 +21,27 @@ namespace Extensions { namespace UriTemplate { namespace Rewrite { -const absl::string_view NAME = "envoy.path.rewrite.pattern_template.pattern_template_rewriter"; +const absl::string_view NAME = "envoy.path.rewrite.uri_template.pattern_template_rewriter"; /** - * PatternTemplateRewriter allows rewriting paths based on match pattern variables provided - * in PatternTemplateMatcher. + * UriTemplateRewriter allows rewriting paths based on match pattern variables provided + * in UriTemplateMatcher. * * Example: - * PatternTemplateMatcher = /foo/bar/{var} - * PatternTemplateRewriter = /foo/{var} + * UriTemplateMatcher = /foo/bar/{var} + * UriTemplateRewriter = /foo/{var} * Will replace segment of path with value of {var} * e.g. /foo/bar/cat -> /foo/cat */ -class PatternTemplateRewriter : public Router::PathRewriter { +class UriTemplateRewriter : public Router::PathRewriter { public: - explicit PatternTemplateRewriter( - const envoy::extensions::path::rewrite::pattern_template::v3::PatternTemplateRewriteConfig& + explicit UriTemplateRewriter( + const envoy::extensions::path::rewrite::uri_template::v3::UriTemplateRewriteConfig& rewrite_config) : rewrite_pattern_(rewrite_config.path_template_rewrite()) {} // Router::PathRewriter - absl::string_view pattern() const override { return rewrite_pattern_; } + absl::string_view uri_template() const override { return rewrite_pattern_; } /** * Concatenates literals and extracts variable values to form the final rewritten path. diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index c7d1525c6077a..f7cc35a9307d8 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -8848,16 +8848,16 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsFilledFromConfigInRouteAction) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/bar/{country}/{lang}" route: cluster: some-cluster path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; @@ -8884,9 +8884,9 @@ TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{lang}/{state}" case_sensitive: false route: { cluster: path-pattern-cluster} @@ -8916,25 +8916,25 @@ TEST_F(RouteMatcherTest, MixedPathPatternMatch) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{lang}/{state}" case_sensitive: false route: { cluster: path-pattern-cluster-one} - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/boo/{go}/{fly}/{bat}" case_sensitive: false route: { cluster: path-pattern-cluster-two} - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/foo/boo/{go}/{fly}/{bat}/{sno}" case_sensitive: false route: { cluster: path-pattern-cluster-three} @@ -8983,17 +8983,17 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimple) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/rest/{two}/{one}" )EOF"; NiceMock stream_info; @@ -9017,17 +9017,17 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimpleTwo) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one=*}/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{two}/{one}" )EOF"; NiceMock stream_info; @@ -9051,31 +9051,31 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteCaseSensitive) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{two}" case_sensitive: true route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{two}/{one}" - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/REST/{one}/{two}" case_sensitive: true route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/TEST/{one}" )EOF"; NiceMock stream_info; @@ -9106,17 +9106,17 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingBracket) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{two}/{one}" )EOF"; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); @@ -9134,17 +9134,17 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/rest/{one}/{two}/{missing}" )EOF"; NiceMock stream_info; @@ -9165,17 +9165,17 @@ TEST_F(RouteMatcherTest, PatternMatchInvalidVariableName) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{on==e}/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/rest/{one}/{two}/{missing}" )EOF"; NiceMock stream_info; @@ -9194,17 +9194,17 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardUnnamedVariable) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/*/{two}" case_sensitive: false route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{two}" )EOF"; NiceMock stream_info; @@ -9228,17 +9228,17 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariable) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/**" case_sensitive: false route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{one}" )EOF"; NiceMock stream_info; @@ -9263,17 +9263,17 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariableNamed) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one=*}/{last=**}" case_sensitive: false route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{last}" )EOF"; NiceMock stream_info; @@ -9298,17 +9298,17 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardMiddleVariableNamed) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{middle=videos/*}/end" case_sensitive: false route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{middle}" )EOF"; NiceMock stream_info; @@ -9333,17 +9333,17 @@ TEST_F(RouteMatcherTest, PatternMatchCaseSensitiveVariableNames) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{One}/end" case_sensitive: false route: cluster: "path-pattern-cluster-one" path_rewrite_policy: - name: envoy.path.rewrite.pattern_template.pattern_template_rewrite_predicate + name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/{One}/{one}" )EOF"; NiceMock stream_info; @@ -9368,9 +9368,9 @@ TEST_F(RouteMatcherTest, PatternMatchCaseTooManyVariableNames) { routes: - match: path_match_policy: - name: envoy.path.match.pattern_template.pattern_template_match_predicate + name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/rest/{one}/{two}/{three}/{four}/{five}/{six}" case_sensitive: false route: diff --git a/test/extensions/path/match/pattern_template/BUILD b/test/extensions/path/match/pattern_template/BUILD index e1b121448a626..9b84b9d0bac61 100644 --- a/test/extensions/path/match/pattern_template/BUILD +++ b/test/extensions/path/match/pattern_template/BUILD @@ -14,9 +14,9 @@ envoy_package() envoy_extension_cc_test( name = "config_test", srcs = ["config_test.cc"], - extension_names = ["envoy.path.match.pattern_template.pattern_template_matcher"], + extension_names = ["envoy.path.match.uri_template.pattern_template_matcher"], deps = [ - "//source/extensions/path/match/pattern_template:config", + "//source/extensions/path/match/uri_template:config", "//test/mocks/server:factory_context_mocks", ], ) @@ -24,10 +24,10 @@ envoy_extension_cc_test( envoy_extension_cc_test( name = "library_test", srcs = ["library_test.cc"], - extension_names = ["envoy.path.match.pattern_template.pattern_template_matcher"], + extension_names = ["envoy.path.match.uri_template.pattern_template_matcher"], deps = [ - "//source/extensions/path/match/pattern_template:config", - "//source/extensions/path/match/pattern_template:pattern_template_match_lib", + "//source/extensions/path/match/uri_template:config", + "//source/extensions/path/match/uri_template:pattern_template_match_lib", "//test/mocks/server:factory_context_mocks", ], ) diff --git a/test/extensions/path/match/pattern_template/config_test.cc b/test/extensions/path/match/pattern_template/config_test.cc index 7e058e10a3c0f..c7842a783152e 100644 --- a/test/extensions/path/match/pattern_template/config_test.cc +++ b/test/extensions/path/match/pattern_template/config_test.cc @@ -13,9 +13,9 @@ namespace Match { TEST(ConfigTest, TestEmptyConfig) { const std::string yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_matcher + name: envoy.path.match.uri_template.pattern_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; @@ -26,7 +26,7 @@ TEST(ConfigTest, TestEmptyConfig) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_matcher"); + EXPECT_EQ(factory->name(), "envoy.path.match.uri_template.pattern_template_matcher"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -40,9 +40,9 @@ TEST(ConfigTest, TestEmptyConfig) { TEST(ConfigTest, InvalidConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_matcher + name: envoy.path.match.uri_template.pattern_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country" )EOF"; @@ -53,7 +53,7 @@ TEST(ConfigTest, InvalidConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_matcher"); + EXPECT_EQ(factory->name(), "envoy.path.match.uri_template.pattern_template_matcher"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -70,9 +70,9 @@ TEST(ConfigTest, InvalidConfigSetup) { TEST(ConfigTest, TestConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_matcher + name: envoy.path.match.uri_template.pattern_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; @@ -83,7 +83,7 @@ TEST(ConfigTest, TestConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.match.pattern_template.pattern_template_matcher"); + EXPECT_EQ(factory->name(), "envoy.path.match.uri_template.pattern_template_matcher"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); diff --git a/test/extensions/path/match/pattern_template/library_test.cc b/test/extensions/path/match/pattern_template/library_test.cc index 78963d45cf4f7..4272d6e3b38ff 100644 --- a/test/extensions/path/match/pattern_template/library_test.cc +++ b/test/extensions/path/match/pattern_template/library_test.cc @@ -30,15 +30,15 @@ Router::PathMatcherSharedPtr createMatcherFromYaml(std::string yaml_string) { TEST(MatchTest, BasicUsage) { const std::string yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_matcher + name: envoy.path.match.uri_template.pattern_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; Router::PathMatcherSharedPtr predicate = createMatcherFromYaml(yaml_string); EXPECT_EQ(predicate->pattern(), "/bar/{lang}/{country}"); - EXPECT_EQ(predicate->name(), "envoy.path.match.pattern_template.pattern_template_matcher"); + EXPECT_EQ(predicate->name(), "envoy.path.match.uri_template.pattern_template_matcher"); EXPECT_TRUE(predicate->match("/bar/en/us")); } diff --git a/test/extensions/path/rewrite/pattern_template/BUILD b/test/extensions/path/rewrite/pattern_template/BUILD index c9af17dcc1fdd..e293edfd8e4fa 100644 --- a/test/extensions/path/rewrite/pattern_template/BUILD +++ b/test/extensions/path/rewrite/pattern_template/BUILD @@ -14,9 +14,9 @@ envoy_package() envoy_extension_cc_test( name = "config_test", srcs = ["config_test.cc"], - extension_names = ["envoy.path.rewrite.pattern_template.pattern_template_rewriter"], + extension_names = ["envoy.path.rewrite.uri_template.pattern_template_rewriter"], deps = [ - "//source/extensions/path/rewrite/pattern_template:config", + "//source/extensions/path/rewrite/uri_template:config", "//test/mocks/server:factory_context_mocks", ], ) @@ -24,12 +24,12 @@ envoy_extension_cc_test( envoy_extension_cc_test( name = "library_test", srcs = ["library_test.cc"], - extension_names = ["envoy.path.rewrite.pattern_template.pattern_template_rewriter"], + extension_names = ["envoy.path.rewrite.uri_template.pattern_template_rewriter"], deps = [ - "//source/extensions/path/match/pattern_template:config", - "//source/extensions/path/match/pattern_template:pattern_template_match_lib", - "//source/extensions/path/rewrite/pattern_template:config", - "//source/extensions/path/rewrite/pattern_template:pattern_template_rewrite_lib", + "//source/extensions/path/match/uri_template:config", + "//source/extensions/path/match/uri_template:pattern_template_match_lib", + "//source/extensions/path/rewrite/uri_template:config", + "//source/extensions/path/rewrite/uri_template:pattern_template_rewrite_lib", "//test/mocks/server:factory_context_mocks", ], ) diff --git a/test/extensions/path/rewrite/pattern_template/config_test.cc b/test/extensions/path/rewrite/pattern_template/config_test.cc index 85d2b30f7cca8..4823fe25cffea 100644 --- a/test/extensions/path/rewrite/pattern_template/config_test.cc +++ b/test/extensions/path/rewrite/pattern_template/config_test.cc @@ -13,9 +13,9 @@ namespace Rewrite { TEST(ConfigTest, TestEmptyConfig) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.pattern_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; @@ -26,7 +26,7 @@ TEST(ConfigTest, TestEmptyConfig) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); + EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -40,9 +40,9 @@ TEST(ConfigTest, TestEmptyConfig) { TEST(ConfigTest, InvalidConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewritere + name: envoy.path.rewrite.uri_template.pattern_template_rewritere typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country" )EOF"; @@ -53,7 +53,7 @@ TEST(ConfigTest, InvalidConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); + EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -70,9 +70,9 @@ TEST(ConfigTest, InvalidConfigSetup) { TEST(ConfigTest, TestConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.pattern_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; @@ -83,7 +83,7 @@ TEST(ConfigTest, TestConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); + EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -98,9 +98,9 @@ TEST(ConfigTest, TestConfigSetup) { TEST(ConfigTest, TestInvalidConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.pattern_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/country}" )EOF"; @@ -111,7 +111,7 @@ TEST(ConfigTest, TestInvalidConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); + EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc index 6674d9cddc6dc..d60a245f38cd8 100644 --- a/test/extensions/path/rewrite/pattern_template/library_test.cc +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -47,35 +47,35 @@ Router::PathRewriterSharedPtr createRewriterFromYaml(std::string yaml_string) { TEST(RewriteTest, BasicSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.pattern_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); EXPECT_EQ(predicate->pattern(), "/bar/{lang}/{country}"); - EXPECT_EQ(predicate->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); + EXPECT_EQ(predicate->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); } TEST(RewriteTest, BasicUsage) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.pattern_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); EXPECT_EQ(predicate->rewritePath("/bar/en/usa", "/bar/{country}/{lang}").value(), "/bar/usa/en"); - EXPECT_EQ(predicate->name(), "envoy.path.rewrite.pattern_template.pattern_template_rewriter"); + EXPECT_EQ(predicate->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); } TEST(RewriteTest, RewriteInvalidRegex) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.pattern_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; @@ -88,16 +88,16 @@ TEST(RewriteTest, RewriteInvalidRegex) { TEST(RewriteTest, MatchPatternValidation) { const std::string rewrite_yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.pattern_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/foo/{lang}/{country}" )EOF"; const std::string match_yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_matcher + name: envoy.path.match.uri_template.pattern_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; @@ -109,16 +109,16 @@ TEST(RewriteTest, MatchPatternValidation) { TEST(RewriteTest, MatchPatternInactive) { const std::string rewrite_yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.pattern_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/foo/{lang}/{country}" )EOF"; const std::string match_yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_matcher + name: envoy.path.match.uri_template.pattern_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; @@ -128,23 +128,23 @@ TEST(RewriteTest, MatchPatternInactive) { absl::Status error = rewrite_predicate->isCompatibleMatchPolicy(match_predicate, false); EXPECT_FALSE(error.ok()); EXPECT_EQ(error.message(), - "unable to use envoy.path.rewrite.pattern_template.pattern_template_rewriter " - "extension without envoy.path.match.pattern_template.pattern_template_matcher " + "unable to use envoy.path.rewrite.uri_template.pattern_template_rewriter " + "extension without envoy.path.match.uri_template.pattern_template_matcher " "extension"); } TEST(RewriteTest, MatchPatternMismatchedVars) { const std::string rewrite_yaml_string = R"EOF( - name: envoy.path.rewrite.pattern_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.pattern_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.pattern_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig path_template_rewrite: "/foo/{lang}/{missing}" )EOF"; const std::string match_yaml_string = R"EOF( - name: envoy.path.match.pattern_template.pattern_template_matcher + name: envoy.path.match.uri_template.pattern_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.pattern_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index 42232f744f1e8..cb9d3f0cd0a71 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -296,5 +296,5 @@ visibility_excludes: - source/extensions/udp_packet_writer/default/BUILD - source/extensions/udp_packet_writer/gso/BUILD - source/extensions/path/uri_template_lib/BUILD -- source/extensions/path/match/pattern_template/BUILD -- source/extensions/path/rewrite/pattern_template/BUILD +- source/extensions/path/match/uri_template/BUILD +- source/extensions/path/rewrite/uri_template/BUILD From d9e1293f6b295bab96c5575fcd2bf881f038e29e Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 2 Sep 2022 20:59:45 +0000 Subject: [PATCH 213/238] Progress towards renaming Signed-off-by: silverstar195 --- .../match/pattern_template/config_test.cc | 22 ++++---- .../match/pattern_template/library_test.cc | 16 +++--- .../rewrite/pattern_template/config_test.cc | 30 +++++----- .../rewrite/pattern_template/library_test.cc | 56 +++++++++---------- 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/test/extensions/path/match/pattern_template/config_test.cc b/test/extensions/path/match/pattern_template/config_test.cc index c7842a783152e..d8309428bef4e 100644 --- a/test/extensions/path/match/pattern_template/config_test.cc +++ b/test/extensions/path/match/pattern_template/config_test.cc @@ -8,14 +8,14 @@ namespace Envoy { namespace Extensions { -namespace PatternTemplate { +namespace UriTemplate { namespace Match { TEST(ConfigTest, TestEmptyConfig) { const std::string yaml_string = R"EOF( - name: envoy.path.match.uri_template.pattern_template_matcher + name: envoy.path.match.uri_template.uri_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; @@ -26,7 +26,7 @@ TEST(ConfigTest, TestEmptyConfig) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.match.uri_template.pattern_template_matcher"); + EXPECT_EQ(factory->name(), "envoy.path.match.uri_template.uri_template_matcher"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -40,9 +40,9 @@ TEST(ConfigTest, TestEmptyConfig) { TEST(ConfigTest, InvalidConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.match.uri_template.pattern_template_matcher + name: envoy.path.match.uri_template.uri_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/bar/{lang}/{country" )EOF"; @@ -53,7 +53,7 @@ TEST(ConfigTest, InvalidConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.match.uri_template.pattern_template_matcher"); + EXPECT_EQ(factory->name(), "envoy.path.match.uri_template.uri_template_matcher"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -70,9 +70,9 @@ TEST(ConfigTest, InvalidConfigSetup) { TEST(ConfigTest, TestConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.match.uri_template.pattern_template_matcher + name: envoy.path.match.uri_template.uri_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; @@ -83,7 +83,7 @@ TEST(ConfigTest, TestConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.match.uri_template.pattern_template_matcher"); + EXPECT_EQ(factory->name(), "envoy.path.match.uri_template.uri_template_matcher"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -97,6 +97,6 @@ TEST(ConfigTest, TestConfigSetup) { } } // namespace Match -} // namespace PatternTemplate +} // namespace UriTemplate } // namespace Extensions } // namespace Envoy diff --git a/test/extensions/path/match/pattern_template/library_test.cc b/test/extensions/path/match/pattern_template/library_test.cc index 4272d6e3b38ff..b48aa7f9d44d4 100644 --- a/test/extensions/path/match/pattern_template/library_test.cc +++ b/test/extensions/path/match/pattern_template/library_test.cc @@ -1,6 +1,6 @@ #include "source/common/config/utility.h" -#include "source/extensions/path/match/pattern_template/config.h" -#include "source/extensions/path/match/pattern_template/pattern_template_match.h" +#include "source/extensions/path/match/uri_template/config.h" +#include "source/extensions/path/match/uri_template/uri_template_match.h" #include "test/mocks/server/factory_context.h" #include "test/test_common/environment.h" @@ -9,7 +9,7 @@ namespace Envoy { namespace Extensions { -namespace PatternTemplate { +namespace UriTemplate { namespace Match { Router::PathMatcherSharedPtr createMatcherFromYaml(std::string yaml_string) { @@ -30,20 +30,20 @@ Router::PathMatcherSharedPtr createMatcherFromYaml(std::string yaml_string) { TEST(MatchTest, BasicUsage) { const std::string yaml_string = R"EOF( - name: envoy.path.match.uri_template.pattern_template_matcher + name: envoy.path.match.uri_template.uri_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; Router::PathMatcherSharedPtr predicate = createMatcherFromYaml(yaml_string); - EXPECT_EQ(predicate->pattern(), "/bar/{lang}/{country}"); - EXPECT_EQ(predicate->name(), "envoy.path.match.uri_template.pattern_template_matcher"); + EXPECT_EQ(predicate->uri_template(), "/bar/{lang}/{country}"); + EXPECT_EQ(predicate->name(), "envoy.path.match.uri_template.uri_template_matcher"); EXPECT_TRUE(predicate->match("/bar/en/us")); } } // namespace Match -} // namespace PatternTemplate +} // namespace UriTemplate } // namespace Extensions } // namespace Envoy diff --git a/test/extensions/path/rewrite/pattern_template/config_test.cc b/test/extensions/path/rewrite/pattern_template/config_test.cc index 4823fe25cffea..96e7d564e820d 100644 --- a/test/extensions/path/rewrite/pattern_template/config_test.cc +++ b/test/extensions/path/rewrite/pattern_template/config_test.cc @@ -1,5 +1,5 @@ #include "source/common/config/utility.h" -#include "source/extensions/path/rewrite/pattern_template/config.h" +#include "source/extensions/path/rewrite/uri_template/config.h" #include "test/mocks/server/factory_context.h" #include "test/test_common/environment.h" @@ -8,14 +8,14 @@ namespace Envoy { namespace Extensions { -namespace PatternTemplate { +namespace UriTemplate { namespace Rewrite { TEST(ConfigTest, TestEmptyConfig) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.uri_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.uri_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; @@ -26,7 +26,7 @@ TEST(ConfigTest, TestEmptyConfig) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); + EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.uri_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -40,9 +40,9 @@ TEST(ConfigTest, TestEmptyConfig) { TEST(ConfigTest, InvalidConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.uri_template.pattern_template_rewritere + name: envoy.path.rewrite.uri_template.uri_template_rewritere typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country" )EOF"; @@ -53,7 +53,7 @@ TEST(ConfigTest, InvalidConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); + EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.uri_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -70,9 +70,9 @@ TEST(ConfigTest, InvalidConfigSetup) { TEST(ConfigTest, TestConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.uri_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.uri_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; @@ -83,7 +83,7 @@ TEST(ConfigTest, TestConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); + EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.uri_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -98,9 +98,9 @@ TEST(ConfigTest, TestConfigSetup) { TEST(ConfigTest, TestInvalidConfigSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.uri_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.uri_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/country}" )EOF"; @@ -111,7 +111,7 @@ TEST(ConfigTest, TestInvalidConfigSetup) { &Envoy::Config::Utility::getAndCheckFactory(config); EXPECT_NE(nullptr, factory); - EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); + EXPECT_EQ(factory->name(), "envoy.path.rewrite.uri_template.uri_template_rewriter"); auto message = Envoy::Config::Utility::translateAnyToFactoryConfig( config.typed_config(), ProtobufMessage::getStrictValidationVisitor(), *factory); @@ -127,6 +127,6 @@ TEST(ConfigTest, TestInvalidConfigSetup) { } } // namespace Rewrite -} // namespace PatternTemplate +} // namespace UriTemplate } // namespace Extensions } // namespace Envoy diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc index d60a245f38cd8..61b417ba637f5 100644 --- a/test/extensions/path/rewrite/pattern_template/library_test.cc +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -1,7 +1,7 @@ #include "source/common/config/utility.h" -#include "source/extensions/path/match/pattern_template/pattern_template_match.h" -#include "source/extensions/path/rewrite/pattern_template/config.h" -#include "source/extensions/path/rewrite/pattern_template/pattern_template_rewrite.h" +#include "source/extensions/path/match/uri_template/uri_template_match.h" +#include "source/extensions/path/rewrite/uri_template/config.h" +#include "source/extensions/path/rewrite/uri_template/uri_template_rewrite.h" #include "test/mocks/server/factory_context.h" #include "test/test_common/environment.h" @@ -10,7 +10,7 @@ namespace Envoy { namespace Extensions { -namespace PatternTemplate { +namespace UriTemplate { namespace Rewrite { Router::PathMatcherSharedPtr createMatcherPredicateFromYaml(std::string yaml_string) { @@ -47,35 +47,35 @@ Router::PathRewriterSharedPtr createRewriterFromYaml(std::string yaml_string) { TEST(RewriteTest, BasicSetup) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.uri_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.uri_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); - EXPECT_EQ(predicate->pattern(), "/bar/{lang}/{country}"); - EXPECT_EQ(predicate->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); + EXPECT_EQ(predicate->uri_template(), "/bar/{lang}/{country}"); + EXPECT_EQ(predicate->name(), "envoy.path.rewrite.uri_template.uri_template_rewriter"); } TEST(RewriteTest, BasicUsage) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.uri_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.uri_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); EXPECT_EQ(predicate->rewritePath("/bar/en/usa", "/bar/{country}/{lang}").value(), "/bar/usa/en"); - EXPECT_EQ(predicate->name(), "envoy.path.rewrite.uri_template.pattern_template_rewriter"); + EXPECT_EQ(predicate->name(), "envoy.path.rewrite.uri_template.uri_template_rewriter"); } TEST(RewriteTest, RewriteInvalidRegex) { const std::string yaml_string = R"EOF( - name: envoy.path.rewrite.uri_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.uri_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; @@ -88,16 +88,16 @@ TEST(RewriteTest, RewriteInvalidRegex) { TEST(RewriteTest, MatchPatternValidation) { const std::string rewrite_yaml_string = R"EOF( - name: envoy.path.rewrite.uri_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.uri_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/foo/{lang}/{country}" )EOF"; const std::string match_yaml_string = R"EOF( - name: envoy.path.match.uri_template.pattern_template_matcher + name: envoy.path.match.uri_template.uri_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; @@ -109,16 +109,16 @@ TEST(RewriteTest, MatchPatternValidation) { TEST(RewriteTest, MatchPatternInactive) { const std::string rewrite_yaml_string = R"EOF( - name: envoy.path.rewrite.uri_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.uri_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/foo/{lang}/{country}" )EOF"; const std::string match_yaml_string = R"EOF( - name: envoy.path.match.uri_template.pattern_template_matcher + name: envoy.path.match.uri_template.uri_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; @@ -128,23 +128,23 @@ TEST(RewriteTest, MatchPatternInactive) { absl::Status error = rewrite_predicate->isCompatibleMatchPolicy(match_predicate, false); EXPECT_FALSE(error.ok()); EXPECT_EQ(error.message(), - "unable to use envoy.path.rewrite.uri_template.pattern_template_rewriter " - "extension without envoy.path.match.uri_template.pattern_template_matcher " + "unable to use envoy.path.rewrite.uri_template.uri_template_rewriter " + "extension without envoy.path.match.uri_template.uri_template_matcher " "extension"); } TEST(RewriteTest, MatchPatternMismatchedVars) { const std::string rewrite_yaml_string = R"EOF( - name: envoy.path.rewrite.uri_template.pattern_template_rewriter + name: envoy.path.rewrite.uri_template.uri_template_rewriter typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/foo/{lang}/{missing}" )EOF"; const std::string match_yaml_string = R"EOF( - name: envoy.path.match.uri_template.pattern_template_matcher + name: envoy.path.match.uri_template.uri_template_matcher typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/bar/{lang}/{country}" )EOF"; @@ -158,6 +158,6 @@ TEST(RewriteTest, MatchPatternMismatchedVars) { } } // namespace Rewrite -} // namespace PatternTemplate +} // namespace UriTemplate } // namespace Extensions } // namespace Envoy From a0ae40767dab760cf6a91ec3957ee98054876ae0 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 6 Sep 2022 13:56:08 +0000 Subject: [PATCH 214/238] Format Signed-off-by: silverstar195 --- CODEOWNERS | 4 ++-- .../path/match/uri_template/v3/uri_template_match.proto | 2 +- .../path/rewrite/uri_template/v3/uri_template_rewrite.proto | 2 +- source/extensions/extensions_metadata.yaml | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index 41131b8ea666c..5a04691b74982 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -266,9 +266,9 @@ extensions/filters/http/oauth2 @derekargueta @snowp # zookeeper /*/extensions/filters/network/zookeeper_proxy @snowp @JuniorHsu # path match by pattern -/*/extensions/path/match/pattern_template @alyssawilk @yanjunxiang-google +/*/extensions/path/match/uri_template @alyssawilk @yanjunxiang-google # path rewrite by pattern -/*/extensions/path/rewrite/pattern_template @alyssawilk @yanjunxiang-google +/*/extensions/path/rewrite/uri_template @alyssawilk @yanjunxiang-google # Intentionally exempt (treated as core code) /*/extensions/filters/common @UNOWNED @UNOWNED diff --git a/api/envoy/extensions/path/match/uri_template/v3/uri_template_match.proto b/api/envoy/extensions/path/match/uri_template/v3/uri_template_match.proto index 877954345b21e..27579f5b45b92 100644 --- a/api/envoy/extensions/path/match/uri_template/v3/uri_template_match.proto +++ b/api/envoy/extensions/path/match/uri_template/v3/uri_template_match.proto @@ -12,7 +12,7 @@ option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pat option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Uri Template Match Config] -// [#extension: envoy.path.match.pattern_template.uri_template_matcher] +// [#extension: envoy.path.match.uri_template.uri_template_matcher] // If specified, the route is a template match rule meaning that the // ``:path`` header (without the query string) must match the given diff --git a/api/envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.proto b/api/envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.proto index 138135c341c04..0d35ca5281396 100644 --- a/api/envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.proto +++ b/api/envoy/extensions/path/rewrite/uri_template/v3/uri_template_rewrite.proto @@ -12,7 +12,7 @@ option go_package = "github.com/envoyproxy/go-control-plane/envoy/extensions/pat option (udpa.annotations.file_status).package_version_status = ACTIVE; // [#protodoc-title: Uri Template Rewrite Config] -// [#extension: envoy.path.rewrite.pattern_template.uri_template_rewriter] +// [#extension: envoy.path.rewrite.uri_template.uri_template_rewriter] // Indicates that during forwarding, portions of the path that match the // pattern should be rewritten, even allowing the substitution of variables diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index e67ee05ca1607..216faeb4da89f 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -746,7 +746,7 @@ envoy.matching.matchers.ip: status: stable type_urls: - envoy.extensions.matching.input_matchers.ip.v3.Ip -envoy.path.match.pattern_template.uri_template_matcher: +envoy.path.match.uri_template.uri_template_matcher: categories: - envoy.path.match security_posture: unknown @@ -754,7 +754,7 @@ envoy.path.match.pattern_template.uri_template_matcher: undocumented: true type_urls: - envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig -envoy.path.rewrite.pattern_template.uri_template_rewriter: +envoy.path.rewrite.uri_template.uri_template_rewriter: categories: - envoy.path.rewrite security_posture: unknown From bad506f63ce2b04f852185f47652a307d2b15a46 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 6 Sep 2022 14:08:13 +0000 Subject: [PATCH 215/238] Format Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 4 ++-- source/extensions/path/match/uri_template/config.h | 4 +--- .../path/match/uri_template/uri_template_match.h | 3 +-- source/extensions/path/rewrite/uri_template/config.h | 7 +++---- .../path/rewrite/uri_template/uri_template_rewrite.cc | 10 ++++------ .../path/rewrite/pattern_template/library_test.cc | 7 +++---- 6 files changed, 14 insertions(+), 21 deletions(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index d34dddc25c24c..7b59dd960b56d 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1506,8 +1506,8 @@ void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& he absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { - return currentUrlPathAfterRewriteWithMatchedPath(headers, - path_match_policy_.pathMatcher()->uri_template()); + return currentUrlPathAfterRewriteWithMatchedPath( + headers, path_match_policy_.pathMatcher()->uri_template()); } RouteConstSharedPtr diff --git a/source/extensions/path/match/uri_template/config.h b/source/extensions/path/match/uri_template/config.h index 12d4a25cc764f..8f08b7ea3b18d 100644 --- a/source/extensions/path/match/uri_template/config.h +++ b/source/extensions/path/match/uri_template/config.h @@ -35,9 +35,7 @@ class UriTemplateMatcherFactory : public Router::PathMatcherFactory { envoy::extensions::path::match::uri_template::v3::UriTemplateMatchConfig>(); } - std::string name() const override { - return "envoy.path.match.uri_template.uri_template_matcher"; - } + std::string name() const override { return "envoy.path.match.uri_template.uri_template_matcher"; } }; } // namespace Match diff --git a/source/extensions/path/match/uri_template/uri_template_match.h b/source/extensions/path/match/uri_template/uri_template_match.h index be082e5e10985..06bfd9c2d4da5 100644 --- a/source/extensions/path/match/uri_template/uri_template_match.h +++ b/source/extensions/path/match/uri_template/uri_template_match.h @@ -28,8 +28,7 @@ const absl::string_view NAME = "envoy.path.match.uri_template.uri_template_match class UriTemplateMatcher : public Router::PathMatcher { public: explicit UriTemplateMatcher( - const envoy::extensions::path::match::uri_template::v3::UriTemplateMatchConfig& - config) + const envoy::extensions::path::match::uri_template::v3::UriTemplateMatchConfig& config) : path_template_(config.path_template()) {} // Router::PathMatcher diff --git a/source/extensions/path/rewrite/uri_template/config.h b/source/extensions/path/rewrite/uri_template/config.h index 9df7c89b24773..7ec1fb8392e1a 100644 --- a/source/extensions/path/rewrite/uri_template/config.h +++ b/source/extensions/path/rewrite/uri_template/config.h @@ -15,10 +15,9 @@ class UriTemplateRewriterFactory : public Router::PathRewriterFactory { public: absl::StatusOr createPathRewriter(const Protobuf::Message& rewrite_config) override { - auto path_rewrite_config = - MessageUtil::downcastAndValidate( - rewrite_config, ProtobufMessage::getStrictValidationVisitor()); + auto path_rewrite_config = MessageUtil::downcastAndValidate< + const envoy::extensions::path::rewrite::uri_template::v3::UriTemplateRewriteConfig&>( + rewrite_config, ProtobufMessage::getStrictValidationVisitor()); if (!UriTemplate::isValidRewritePattern(path_rewrite_config.path_template_rewrite()).ok()) { return absl::InvalidArgumentError( diff --git a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc index 93aff9732a866..83da173ca19b4 100644 --- a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc +++ b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc @@ -24,9 +24,8 @@ namespace Rewrite { #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif -absl::Status -UriTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher, - bool active_matcher) const { +absl::Status UriTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher, + bool active_matcher) const { if (!active_matcher || path_matcher->name() != Extensions::UriTemplate::Match::NAME) { return absl::InvalidArgumentError(fmt::format("unable to use {} extension without {} extension", Extensions::UriTemplate::Rewrite::NAME, @@ -44,9 +43,8 @@ UriTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_m return absl::OkStatus(); } -absl::StatusOr -UriTemplateRewriter::rewritePath(absl::string_view pattern, - absl::string_view matched_path) const { +absl::StatusOr UriTemplateRewriter::rewritePath(absl::string_view pattern, + absl::string_view matched_path) const { absl::StatusOr regex_pattern = convertPathPatternSyntaxToRegex(matched_path); if (!regex_pattern.ok()) { return absl::InvalidArgumentError("Unable to parse matched_path"); diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/pattern_template/library_test.cc index 61b417ba637f5..073bec0d3643e 100644 --- a/test/extensions/path/rewrite/pattern_template/library_test.cc +++ b/test/extensions/path/rewrite/pattern_template/library_test.cc @@ -127,10 +127,9 @@ TEST(RewriteTest, MatchPatternInactive) { absl::Status error = rewrite_predicate->isCompatibleMatchPolicy(match_predicate, false); EXPECT_FALSE(error.ok()); - EXPECT_EQ(error.message(), - "unable to use envoy.path.rewrite.uri_template.uri_template_rewriter " - "extension without envoy.path.match.uri_template.uri_template_matcher " - "extension"); + EXPECT_EQ(error.message(), "unable to use envoy.path.rewrite.uri_template.uri_template_rewriter " + "extension without envoy.path.match.uri_template.uri_template_matcher " + "extension"); } TEST(RewriteTest, MatchPatternMismatchedVars) { From 1ba7fe7e56ffd3a931899bdaf035ba7ed15de3a8 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 6 Sep 2022 14:10:01 +0000 Subject: [PATCH 216/238] Format Signed-off-by: silverstar195 --- source/extensions/extensions_metadata.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index 216faeb4da89f..d64fcae80c5ae 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -753,7 +753,7 @@ envoy.path.match.uri_template.uri_template_matcher: status: stable undocumented: true type_urls: - - envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + - envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig envoy.path.rewrite.uri_template.uri_template_rewriter: categories: - envoy.path.rewrite @@ -761,7 +761,7 @@ envoy.path.rewrite.uri_template.uri_template_rewriter: status: stable undocumented: true type_urls: - - envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + - envoy.extensions.path.rewrite.uri_template.v3.UriemplateRewriteConfig envoy.quic.proof_source.filter_chain: categories: - envoy.quic.proof_source From 5da0dbb5419abd8bb6419244ec6ecdaa484bbd71 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 6 Sep 2022 14:43:29 +0000 Subject: [PATCH 217/238] Spelling Signed-off-by: silverstar195 --- source/extensions/extensions_metadata.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index d64fcae80c5ae..23bafba48eeb3 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -761,7 +761,7 @@ envoy.path.rewrite.uri_template.uri_template_rewriter: status: stable undocumented: true type_urls: - - envoy.extensions.path.rewrite.uri_template.v3.UriemplateRewriteConfig + - envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig envoy.quic.proof_source.filter_chain: categories: - envoy.quic.proof_source From 016b0477fb0b0090094da8ffbf688e5e9d410ad8 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 6 Sep 2022 15:47:00 +0000 Subject: [PATCH 218/238] Fix tests Signed-off-by: silverstar195 --- .../path/match/uri_template/config.h | 2 +- test/common/router/config_impl_test.cc | 66 +++++++++---------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/source/extensions/path/match/uri_template/config.h b/source/extensions/path/match/uri_template/config.h index 8f08b7ea3b18d..8c0b4977aa0ea 100644 --- a/source/extensions/path/match/uri_template/config.h +++ b/source/extensions/path/match/uri_template/config.h @@ -22,7 +22,7 @@ class UriTemplateMatcherFactory : public Router::PathMatcherFactory { config, ProtobufMessage::getStrictValidationVisitor()); if (!UriTemplate::isValidMatchPattern(path_match_config.path_template()).ok()) { - return absl::InvalidArgumentError(fmt::format("path_match_policy.uri_template {} is invalid", + return absl::InvalidArgumentError(fmt::format("path_match_policy.path_template {} is invalid", path_match_config.path_template())); } diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index f7cc35a9307d8..5146a6b6001db 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -8850,14 +8850,14 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsFilledFromConfigInRouteAction) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/bar/{country}/{lang}" route: cluster: some-cluster path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/bar/{lang}/{country}" )EOF"; @@ -8868,11 +8868,11 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsFilledFromConfigInRouteAction) { const auto& pattern_match_policy = config.route(headers, 0)->routeEntry()->pathMatchPolicy(); EXPECT_TRUE(pattern_match_policy.enabled()); - EXPECT_EQ(pattern_match_policy.pathMatcher()->pattern(), "/bar/{country}/{lang}"); + EXPECT_EQ(pattern_match_policy.pathMatcher()->uri_template(), "/bar/{country}/{lang}"); const auto& pattern_rewrite_policy = config.route(headers, 0)->routeEntry()->pathRewritePolicy(); EXPECT_TRUE(pattern_rewrite_policy.enabled()); - EXPECT_EQ(pattern_rewrite_policy.pathRewriter()->pattern(), "/bar/{lang}/{country}"); + EXPECT_EQ(pattern_rewrite_policy.pathRewriter()->uri_template(), "/bar/{lang}/{country}"); } TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { @@ -8886,7 +8886,7 @@ TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{lang}/{state}" case_sensitive: false route: { cluster: path-pattern-cluster} @@ -8918,7 +8918,7 @@ TEST_F(RouteMatcherTest, MixedPathPatternMatch) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{lang}/{state}" case_sensitive: false route: { cluster: path-pattern-cluster-one} @@ -8926,7 +8926,7 @@ TEST_F(RouteMatcherTest, MixedPathPatternMatch) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/boo/{go}/{fly}/{bat}" case_sensitive: false route: { cluster: path-pattern-cluster-two} @@ -8934,7 +8934,7 @@ TEST_F(RouteMatcherTest, MixedPathPatternMatch) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/foo/boo/{go}/{fly}/{bat}/{sno}" case_sensitive: false route: { cluster: path-pattern-cluster-three} @@ -8985,7 +8985,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimple) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{one}/{two}" case_sensitive: false route: @@ -8993,7 +8993,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimple) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/rest/{two}/{one}" )EOF"; NiceMock stream_info; @@ -9019,7 +9019,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimpleTwo) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{one=*}/{two}" case_sensitive: false route: @@ -9027,7 +9027,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteSimpleTwo) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/{two}/{one}" )EOF"; NiceMock stream_info; @@ -9053,7 +9053,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteCaseSensitive) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{one}/{two}" case_sensitive: true route: @@ -9061,13 +9061,13 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteCaseSensitive) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/{two}/{one}" - match: path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/REST/{one}/{two}" case_sensitive: true route: @@ -9075,7 +9075,7 @@ TEST_F(RouteMatcherTest, PatternMatchRewriteCaseSensitive) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/TEST/{one}" )EOF"; NiceMock stream_info; @@ -9108,7 +9108,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingBracket) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{one/{two}" case_sensitive: false route: @@ -9116,7 +9116,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingBracket) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/{two}/{one}" )EOF"; factory_context_.cluster_manager_.initializeClusters({"path-pattern-cluster-one"}, {}); @@ -9136,7 +9136,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{one}/{two}" case_sensitive: false route: @@ -9144,7 +9144,7 @@ TEST_F(RouteMatcherTest, PatternMatchConfigMissingVariable) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/rest/{one}/{two}/{missing}" )EOF"; NiceMock stream_info; @@ -9167,7 +9167,7 @@ TEST_F(RouteMatcherTest, PatternMatchInvalidVariableName) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{on==e}/{two}" case_sensitive: false route: @@ -9175,7 +9175,7 @@ TEST_F(RouteMatcherTest, PatternMatchInvalidVariableName) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/rest/{one}/{two}/{missing}" )EOF"; NiceMock stream_info; @@ -9196,7 +9196,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardUnnamedVariable) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/*/{two}" case_sensitive: false route: @@ -9204,7 +9204,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardUnnamedVariable) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/{two}" )EOF"; NiceMock stream_info; @@ -9230,7 +9230,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariable) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{one}/**" case_sensitive: false route: @@ -9238,7 +9238,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariable) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/{one}" )EOF"; NiceMock stream_info; @@ -9265,7 +9265,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariableNamed) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{one=*}/{last=**}" case_sensitive: false route: @@ -9273,7 +9273,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardAtEndVariableNamed) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/{last}" )EOF"; NiceMock stream_info; @@ -9300,7 +9300,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardMiddleVariableNamed) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{one}/{middle=videos/*}/end" case_sensitive: false route: @@ -9308,7 +9308,7 @@ TEST_F(RouteMatcherTest, PatternMatchWildcardMiddleVariableNamed) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/{middle}" )EOF"; NiceMock stream_info; @@ -9335,7 +9335,7 @@ TEST_F(RouteMatcherTest, PatternMatchCaseSensitiveVariableNames) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{one}/{One}/end" case_sensitive: false route: @@ -9343,7 +9343,7 @@ TEST_F(RouteMatcherTest, PatternMatchCaseSensitiveVariableNames) { path_rewrite_policy: name: envoy.path.rewrite.uri_template.pattern_template_rewrite_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.PatternTemplateRewriteConfig + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig path_template_rewrite: "/{One}/{one}" )EOF"; NiceMock stream_info; @@ -9370,7 +9370,7 @@ TEST_F(RouteMatcherTest, PatternMatchCaseTooManyVariableNames) { path_match_policy: name: envoy.path.match.uri_template.pattern_template_match_predicate typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.PatternTemplateMatchConfig + "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig path_template: "/rest/{one}/{two}/{three}/{four}/{five}/{six}" case_sensitive: false route: From cad3d1c4c111fd49241b47962aa38e3ed7c0fc58 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 6 Sep 2022 19:07:19 +0000 Subject: [PATCH 219/238] Format and rename Signed-off-by: silverstar195 --- envoy/router/path_matcher.h | 2 +- envoy/router/path_rewriter.h | 2 +- source/common/router/config_impl.cc | 8 ++++---- .../path/match/uri_template/uri_template_match.cc | 2 +- .../path/match/uri_template/uri_template_match.h | 2 +- .../path/rewrite/uri_template/uri_template_rewrite.cc | 4 ++-- .../path/rewrite/uri_template/uri_template_rewrite.h | 2 +- test/common/router/config_impl_test.cc | 4 ++-- .../path/match/{pattern_template => uri_template}/BUILD | 6 +++--- .../{pattern_template => uri_template}/config_test.cc | 2 +- .../{pattern_template => uri_template}/library_test.cc | 0 .../path/rewrite/{pattern_template => uri_template}/BUILD | 8 ++++---- .../{pattern_template => uri_template}/config_test.cc | 0 .../{pattern_template => uri_template}/library_test.cc | 0 14 files changed, 21 insertions(+), 21 deletions(-) rename test/extensions/path/match/{pattern_template => uri_template}/BUILD (72%) rename test/extensions/path/match/{pattern_template => uri_template}/config_test.cc (98%) rename test/extensions/path/match/{pattern_template => uri_template}/library_test.cc (100%) rename test/extensions/path/rewrite/{pattern_template => uri_template}/BUILD (67%) rename test/extensions/path/rewrite/{pattern_template => uri_template}/config_test.cc (100%) rename test/extensions/path/rewrite/{pattern_template => uri_template}/library_test.cc (100%) diff --git a/envoy/router/path_matcher.h b/envoy/router/path_matcher.h index c8806c7149cd1..f4dfd9b5df57c 100644 --- a/envoy/router/path_matcher.h +++ b/envoy/router/path_matcher.h @@ -30,7 +30,7 @@ class PathMatcher : Logger::Loggable { /** * @return the match uri_template. */ - virtual absl::string_view uri_template() const PURE; + virtual absl::string_view uriTemplate() const PURE; /** * @return the name of the path matcher. diff --git a/envoy/router/path_rewriter.h b/envoy/router/path_rewriter.h index 8a6b23515e538..deaffe92dc995 100644 --- a/envoy/router/path_rewriter.h +++ b/envoy/router/path_rewriter.h @@ -44,7 +44,7 @@ class PathRewriter : Logger::Loggable { /** * @return the rewrite uri_template. */ - virtual absl::string_view uri_template() const PURE; + virtual absl::string_view uriTemplate() const PURE; /** * @return the name of the pattern rewriter. diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 7b59dd960b56d..544fab4c15f63 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -1496,18 +1496,18 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - uri_template_(path_match_policy_.pathMatcher()->uri_template()){}; + uri_template_(path_match_policy_.pathMatcher()->uriTemplate()){}; void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { - finalizePathHeader(headers, path_match_policy_.pathMatcher()->uri_template(), + finalizePathHeader(headers, path_match_policy_.pathMatcher()->uriTemplate(), insert_envoy_original_path); } absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { - return currentUrlPathAfterRewriteWithMatchedPath( - headers, path_match_policy_.pathMatcher()->uri_template()); + return currentUrlPathAfterRewriteWithMatchedPath(headers, + path_match_policy_.pathMatcher()->uriTemplate()); } RouteConstSharedPtr diff --git a/source/extensions/path/match/uri_template/uri_template_match.cc b/source/extensions/path/match/uri_template/uri_template_match.cc index e9df81aa1b713..a156508b9a884 100644 --- a/source/extensions/path/match/uri_template/uri_template_match.cc +++ b/source/extensions/path/match/uri_template/uri_template_match.cc @@ -23,7 +23,7 @@ bool UriTemplateMatcher::match(absl::string_view path) const { matching_pattern_regex); } -absl::string_view UriTemplateMatcher::uri_template() const { return path_template_; } +absl::string_view UriTemplateMatcher::uriTemplate() const { return path_template_; } } // namespace Match } // namespace UriTemplate diff --git a/source/extensions/path/match/uri_template/uri_template_match.h b/source/extensions/path/match/uri_template/uri_template_match.h index 06bfd9c2d4da5..39f652b06f057 100644 --- a/source/extensions/path/match/uri_template/uri_template_match.h +++ b/source/extensions/path/match/uri_template/uri_template_match.h @@ -34,7 +34,7 @@ class UriTemplateMatcher : public Router::PathMatcher { // Router::PathMatcher bool match(absl::string_view path) const override; - absl::string_view uri_template() const override; + absl::string_view uriTemplate() const override; absl::string_view name() const override { return NAME; } diff --git a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc index 83da173ca19b4..54e7bf68ef6c3 100644 --- a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc +++ b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc @@ -34,10 +34,10 @@ absl::Status UriTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSha // This is needed to match up variable values. // Validation between extensions as they share rewrite pattern variables. - if (!isValidSharedVariableSet(rewrite_pattern_, path_matcher->uri_template()).ok()) { + if (!isValidSharedVariableSet(rewrite_pattern_, path_matcher->uriTemplate()).ok()) { return absl::InvalidArgumentError( fmt::format("mismatch between variables in path_match_policy {} and path_rewrite_policy {}", - path_matcher->uri_template(), rewrite_pattern_)); + path_matcher->uriTemplate(), rewrite_pattern_)); } return absl::OkStatus(); diff --git a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.h b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.h index 34e911d884491..edc5130b3254e 100644 --- a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.h +++ b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.h @@ -41,7 +41,7 @@ class UriTemplateRewriter : public Router::PathRewriter { : rewrite_pattern_(rewrite_config.path_template_rewrite()) {} // Router::PathRewriter - absl::string_view uri_template() const override { return rewrite_pattern_; } + absl::string_view uriTemplate() const override { return rewrite_pattern_; } /** * Concatenates literals and extracts variable values to form the final rewritten path. diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index 5146a6b6001db..b771a41adbbe2 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -8868,11 +8868,11 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsFilledFromConfigInRouteAction) { const auto& pattern_match_policy = config.route(headers, 0)->routeEntry()->pathMatchPolicy(); EXPECT_TRUE(pattern_match_policy.enabled()); - EXPECT_EQ(pattern_match_policy.pathMatcher()->uri_template(), "/bar/{country}/{lang}"); + EXPECT_EQ(pattern_match_policy.pathMatcher()->uriTemplate(), "/bar/{country}/{lang}"); const auto& pattern_rewrite_policy = config.route(headers, 0)->routeEntry()->pathRewritePolicy(); EXPECT_TRUE(pattern_rewrite_policy.enabled()); - EXPECT_EQ(pattern_rewrite_policy.pathRewriter()->uri_template(), "/bar/{lang}/{country}"); + EXPECT_EQ(pattern_rewrite_policy.pathRewriter()->uriTemplate(), "/bar/{lang}/{country}"); } TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { diff --git a/test/extensions/path/match/pattern_template/BUILD b/test/extensions/path/match/uri_template/BUILD similarity index 72% rename from test/extensions/path/match/pattern_template/BUILD rename to test/extensions/path/match/uri_template/BUILD index 9b84b9d0bac61..fa5b36a775bc6 100644 --- a/test/extensions/path/match/pattern_template/BUILD +++ b/test/extensions/path/match/uri_template/BUILD @@ -14,7 +14,7 @@ envoy_package() envoy_extension_cc_test( name = "config_test", srcs = ["config_test.cc"], - extension_names = ["envoy.path.match.uri_template.pattern_template_matcher"], + extension_names = ["envoy.path.match.uri_template.uri_template_matcher"], deps = [ "//source/extensions/path/match/uri_template:config", "//test/mocks/server:factory_context_mocks", @@ -24,10 +24,10 @@ envoy_extension_cc_test( envoy_extension_cc_test( name = "library_test", srcs = ["library_test.cc"], - extension_names = ["envoy.path.match.uri_template.pattern_template_matcher"], + extension_names = ["envoy.path.match.uri_template.uri_template_matcher"], deps = [ "//source/extensions/path/match/uri_template:config", - "//source/extensions/path/match/uri_template:pattern_template_match_lib", + "//source/extensions/path/match/uri_template:uri_template_match_lib", "//test/mocks/server:factory_context_mocks", ], ) diff --git a/test/extensions/path/match/pattern_template/config_test.cc b/test/extensions/path/match/uri_template/config_test.cc similarity index 98% rename from test/extensions/path/match/pattern_template/config_test.cc rename to test/extensions/path/match/uri_template/config_test.cc index d8309428bef4e..67a78aa8fd4b2 100644 --- a/test/extensions/path/match/pattern_template/config_test.cc +++ b/test/extensions/path/match/uri_template/config_test.cc @@ -1,5 +1,5 @@ #include "source/common/config/utility.h" -#include "source/extensions/path/match/pattern_template/config.h" +#include "source/extensions/path/match/uri_template/config.h" #include "test/mocks/server/factory_context.h" #include "test/test_common/environment.h" diff --git a/test/extensions/path/match/pattern_template/library_test.cc b/test/extensions/path/match/uri_template/library_test.cc similarity index 100% rename from test/extensions/path/match/pattern_template/library_test.cc rename to test/extensions/path/match/uri_template/library_test.cc diff --git a/test/extensions/path/rewrite/pattern_template/BUILD b/test/extensions/path/rewrite/uri_template/BUILD similarity index 67% rename from test/extensions/path/rewrite/pattern_template/BUILD rename to test/extensions/path/rewrite/uri_template/BUILD index e293edfd8e4fa..cd9b2db0813a3 100644 --- a/test/extensions/path/rewrite/pattern_template/BUILD +++ b/test/extensions/path/rewrite/uri_template/BUILD @@ -14,7 +14,7 @@ envoy_package() envoy_extension_cc_test( name = "config_test", srcs = ["config_test.cc"], - extension_names = ["envoy.path.rewrite.uri_template.pattern_template_rewriter"], + extension_names = ["envoy.path.rewrite.uri_template.uri_template_rewriter"], deps = [ "//source/extensions/path/rewrite/uri_template:config", "//test/mocks/server:factory_context_mocks", @@ -24,12 +24,12 @@ envoy_extension_cc_test( envoy_extension_cc_test( name = "library_test", srcs = ["library_test.cc"], - extension_names = ["envoy.path.rewrite.uri_template.pattern_template_rewriter"], + extension_names = ["envoy.path.rewrite.uri_template.uri_template_rewriter"], deps = [ "//source/extensions/path/match/uri_template:config", - "//source/extensions/path/match/uri_template:pattern_template_match_lib", + "//source/extensions/path/match/uri_template:uri_template_match_lib", "//source/extensions/path/rewrite/uri_template:config", - "//source/extensions/path/rewrite/uri_template:pattern_template_rewrite_lib", + "//source/extensions/path/rewrite/uri_template:uri_template_rewrite_lib", "//test/mocks/server:factory_context_mocks", ], ) diff --git a/test/extensions/path/rewrite/pattern_template/config_test.cc b/test/extensions/path/rewrite/uri_template/config_test.cc similarity index 100% rename from test/extensions/path/rewrite/pattern_template/config_test.cc rename to test/extensions/path/rewrite/uri_template/config_test.cc diff --git a/test/extensions/path/rewrite/pattern_template/library_test.cc b/test/extensions/path/rewrite/uri_template/library_test.cc similarity index 100% rename from test/extensions/path/rewrite/pattern_template/library_test.cc rename to test/extensions/path/rewrite/uri_template/library_test.cc From 23d11a13de6da7a17f250fbfe9c0c163f6caf910 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 6 Sep 2022 19:16:50 +0000 Subject: [PATCH 220/238] Format and rename Signed-off-by: silverstar195 --- test/extensions/path/match/uri_template/library_test.cc | 2 +- test/extensions/path/rewrite/uri_template/library_test.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/extensions/path/match/uri_template/library_test.cc b/test/extensions/path/match/uri_template/library_test.cc index b48aa7f9d44d4..bf696640d37a8 100644 --- a/test/extensions/path/match/uri_template/library_test.cc +++ b/test/extensions/path/match/uri_template/library_test.cc @@ -37,7 +37,7 @@ TEST(MatchTest, BasicUsage) { )EOF"; Router::PathMatcherSharedPtr predicate = createMatcherFromYaml(yaml_string); - EXPECT_EQ(predicate->uri_template(), "/bar/{lang}/{country}"); + EXPECT_EQ(predicate->uriTemplate(), "/bar/{lang}/{country}"); EXPECT_EQ(predicate->name(), "envoy.path.match.uri_template.uri_template_matcher"); EXPECT_TRUE(predicate->match("/bar/en/us")); diff --git a/test/extensions/path/rewrite/uri_template/library_test.cc b/test/extensions/path/rewrite/uri_template/library_test.cc index 073bec0d3643e..d0ad33d5ed969 100644 --- a/test/extensions/path/rewrite/uri_template/library_test.cc +++ b/test/extensions/path/rewrite/uri_template/library_test.cc @@ -54,7 +54,7 @@ TEST(RewriteTest, BasicSetup) { )EOF"; Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); - EXPECT_EQ(predicate->uri_template(), "/bar/{lang}/{country}"); + EXPECT_EQ(predicate->uriTemplate(), "/bar/{lang}/{country}"); EXPECT_EQ(predicate->name(), "envoy.path.rewrite.uri_template.uri_template_rewriter"); } From 83d9498f7a89f1a6bb4d20e045f34d58896d36d5 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 6 Sep 2022 19:25:08 +0000 Subject: [PATCH 221/238] Format and rename Signed-off-by: silverstar195 --- test/extensions/path/rewrite/uri_template/library_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/path/rewrite/uri_template/library_test.cc b/test/extensions/path/rewrite/uri_template/library_test.cc index d0ad33d5ed969..8762592433b77 100644 --- a/test/extensions/path/rewrite/uri_template/library_test.cc +++ b/test/extensions/path/rewrite/uri_template/library_test.cc @@ -104,7 +104,7 @@ TEST(RewriteTest, MatchPatternValidation) { Router::PathRewriterSharedPtr rewrite_predicate = createRewriterFromYaml(rewrite_yaml_string); Router::PathMatcherSharedPtr match_predicate = createMatcherPredicateFromYaml(match_yaml_string); - EXPECT_TRUE(rewrite_predicate->isCompatibleMatchPolicy(match_predicate, true).ok()); + EXPECT_TRUE(rewrite_predicate->isCompatiblePathMatcher(match_predicate, true).ok()); } TEST(RewriteTest, MatchPatternInactive) { From 3166bd45d88ad405d32eec0f39b5799baf614f07 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Tue, 6 Sep 2022 19:32:43 +0000 Subject: [PATCH 222/238] Format and rename Signed-off-by: silverstar195 --- test/extensions/path/rewrite/uri_template/library_test.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/extensions/path/rewrite/uri_template/library_test.cc b/test/extensions/path/rewrite/uri_template/library_test.cc index 8762592433b77..47d74cab2090c 100644 --- a/test/extensions/path/rewrite/uri_template/library_test.cc +++ b/test/extensions/path/rewrite/uri_template/library_test.cc @@ -125,7 +125,7 @@ TEST(RewriteTest, MatchPatternInactive) { Router::PathRewriterSharedPtr rewrite_predicate = createRewriterFromYaml(rewrite_yaml_string); Router::PathMatcherSharedPtr match_predicate = createMatcherPredicateFromYaml(match_yaml_string); - absl::Status error = rewrite_predicate->isCompatibleMatchPolicy(match_predicate, false); + absl::Status error = rewrite_predicate->isCompatiblePathMatcher(match_predicate, false); EXPECT_FALSE(error.ok()); EXPECT_EQ(error.message(), "unable to use envoy.path.rewrite.uri_template.uri_template_rewriter " "extension without envoy.path.match.uri_template.uri_template_matcher " @@ -150,7 +150,7 @@ TEST(RewriteTest, MatchPatternMismatchedVars) { Router::PathRewriterSharedPtr rewrite_predicate = createRewriterFromYaml(rewrite_yaml_string); Router::PathMatcherSharedPtr match_predicate = createMatcherPredicateFromYaml(match_yaml_string); - absl::Status error = rewrite_predicate->isCompatibleMatchPolicy(match_predicate, true); + absl::Status error = rewrite_predicate->isCompatiblePathMatcher(match_predicate, true); EXPECT_FALSE(error.ok()); EXPECT_EQ(error.message(), "mismatch between variables in path_match_policy " "/bar/{lang}/{country} and path_rewrite_policy /foo/{lang}/{missing}"); From 06ff4ae511bc6417e9d0e835a669f8b970a78f5a Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 7 Sep 2022 20:05:36 +0000 Subject: [PATCH 223/238] Remove policy classes Signed-off-by: silverstar195 --- envoy/router/BUILD | 8 +- envoy/router/path_rewriter.h | 3 +- envoy/router/router.h | 6 +- source/common/http/async_client_impl.cc | 4 +- source/common/http/async_client_impl.h | 10 +- source/common/router/config_impl.cc | 108 +++++++----------- source/common/router/config_impl.h | 68 ++--------- source/common/router/delegating_route_impl.cc | 8 +- source/common/router/delegating_route_impl.h | 4 +- .../extensions/path/match/uri_template/BUILD | 4 +- .../match/uri_template/uri_template_match.h | 2 - .../path/rewrite/uri_template/BUILD | 6 +- .../uri_template/uri_template_rewrite.cc | 5 +- .../uri_template/uri_template_rewrite.h | 7 +- test/common/router/config_impl_test.cc | 16 +-- .../path/rewrite/uri_template/library_test.cc | 24 ++-- test/mocks/router/mocks.cc | 9 -- test/mocks/router/mocks.h | 25 ++-- 18 files changed, 110 insertions(+), 207 deletions(-) diff --git a/envoy/router/BUILD b/envoy/router/BUILD index 37ba1aea9e2c7..ba9790e4928d2 100644 --- a/envoy/router/BUILD +++ b/envoy/router/BUILD @@ -64,8 +64,8 @@ envoy_cc_library( external_deps = ["abseil_optional"], deps = [ ":internal_redirect_interface", - ":router_path_match_policy_interface", - ":router_path_rewrite_policy_interface", + ":path_matcher_interface", + ":path_rewriter_interface", "//envoy/access_log:access_log_interface", "//envoy/common:conn_pool_interface", "//envoy/common:matchers_interface", @@ -148,7 +148,7 @@ envoy_cc_library( ) envoy_cc_library( - name = "router_path_match_policy_interface", + name = "path_matcher_interface", hdrs = ["path_matcher.h"], deps = [ "//envoy/config:typed_config_interface", @@ -159,7 +159,7 @@ envoy_cc_library( ) envoy_cc_library( - name = "router_path_rewrite_policy_interface", + name = "path_rewriter_interface", hdrs = ["path_rewriter.h"], deps = [ "//envoy/config:typed_config_interface", diff --git a/envoy/router/path_rewriter.h b/envoy/router/path_rewriter.h index deaffe92dc995..ca31f3c30f79f 100644 --- a/envoy/router/path_rewriter.h +++ b/envoy/router/path_rewriter.h @@ -27,8 +27,7 @@ class PathRewriter : Logger::Loggable { * @param active_policy true if user provided policy * @return true if current path match policy is acceptable */ - virtual absl::Status isCompatiblePathMatcher(PathMatcherSharedPtr path_matcher, - bool active_matcher) const PURE; + virtual absl::Status isCompatiblePathMatcher(PathMatcherSharedPtr path_matcher) const PURE; /** * Rewrites the current path to the specified output. Return a failure in case rewrite diff --git a/envoy/router/router.h b/envoy/router/router.h index 97e00b0917a7f..7a47691b297e0 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -808,7 +808,7 @@ enum class PathMatchType { Exact, Regex, PathSeparatedPrefix, - Policy, + Template, }; /** @@ -963,12 +963,12 @@ class RouteEntry : public ResponseEntry { /** * @return const PathMatcherPolicy& the path match policy for the route. */ - virtual const PathMatchPolicy& pathMatchPolicy() const PURE; + virtual const PathMatcherSharedPtr& pathMatcher() const PURE; /** * @return const PathRewriterPolicy& the path match rewrite for the route. */ - virtual const PathRewritePolicy& pathRewritePolicy() const PURE; + virtual const PathRewriterSharedPtr& pathRewriter() const PURE; /** * @return uint32_t any route cap on bytes which should be buffered for shadowing or retries. diff --git a/source/common/http/async_client_impl.cc b/source/common/http/async_client_impl.cc index 68dd1497794ea..85fc95d8d7ae1 100644 --- a/source/common/http/async_client_impl.cc +++ b/source/common/http/async_client_impl.cc @@ -20,8 +20,8 @@ const std::vector> const AsyncStreamImpl::NullHedgePolicy AsyncStreamImpl::RouteEntryImpl::hedge_policy_; const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::RouteEntryImpl::rate_limit_policy_; const Router::InternalRedirectPolicyImpl AsyncStreamImpl::RouteEntryImpl::internal_redirect_policy_; -const Router::PathMatchPolicyImpl AsyncStreamImpl::RouteEntryImpl::path_match_policy_; -const Router::PathRewritePolicyImpl AsyncStreamImpl::RouteEntryImpl::path_rewrite_policy_; +const Router::PathMatcherSharedPtr AsyncStreamImpl::RouteEntryImpl::extension_path_matcher_; +const Router::PathRewriterSharedPtr AsyncStreamImpl::RouteEntryImpl::extension_path_rewriter_; const std::vector AsyncStreamImpl::RouteEntryImpl::shadow_policies_; const AsyncStreamImpl::NullVirtualHost AsyncStreamImpl::RouteEntryImpl::virtual_host_; const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::NullVirtualHost::rate_limit_policy_; diff --git a/source/common/http/async_client_impl.h b/source/common/http/async_client_impl.h index ec973f3082708..95a7868259f40 100644 --- a/source/common/http/async_client_impl.h +++ b/source/common/http/async_client_impl.h @@ -231,9 +231,9 @@ class AsyncStreamImpl : public AsyncClient::Stream, const Router::InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } - const Router::PathMatchPolicy& pathMatchPolicy() const override { return path_match_policy_; } - const Router::PathRewritePolicy& pathRewritePolicy() const override { - return path_rewrite_policy_; + const Router::PathMatcherSharedPtr& pathMatcher() const override { return extension_path_matcher_; } + const Router::PathRewriterSharedPtr& pathRewriter() const override { + return extension_path_rewriter_; } uint32_t retryShadowBufferLimit() const override { return std::numeric_limits::max(); @@ -298,8 +298,8 @@ class AsyncStreamImpl : public AsyncClient::Stream, static const NullHedgePolicy hedge_policy_; static const NullRateLimitPolicy rate_limit_policy_; static const Router::InternalRedirectPolicyImpl internal_redirect_policy_; - static const Router::PathMatchPolicyImpl path_match_policy_; - static const Router::PathRewritePolicyImpl path_rewrite_policy_; + static const Router::PathMatcherSharedPtr extension_path_matcher_; + static const Router::PathRewriterSharedPtr extension_path_rewriter_; static const std::vector shadow_policies_; static const NullVirtualHost virtual_host_; static const std::multimap opaque_config_; diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 544fab4c15f63..87a922842f3c3 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -360,52 +360,6 @@ std::vector InternalRedirectPolicyImpl::pred return predicates; } -PathRewritePolicyImpl::PathRewritePolicyImpl() : enabled_(false){}; - -PathRewritePolicyImpl::PathRewritePolicyImpl( - const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator) - : enabled_(true) { - - auto& factory = Envoy::Config::Utility::getAndCheckFactory(typed_config); - - config_ = Envoy::Config::Utility::translateAnyToFactoryConfig(typed_config.typed_config(), - validator, factory); - - absl::StatusOr rewriter = factory.createPathRewriter(*config_); - - if (!rewriter.ok()) { - throw EnvoyException(std::string(rewriter.status().message())); - } - - rewriter_ = rewriter.value(); -} - -PathRewriterSharedPtr PathRewritePolicyImpl::pathRewriter() const { return rewriter_; } - -PathMatchPolicyImpl::PathMatchPolicyImpl() : enabled_(false){}; - -PathMatchPolicyImpl::PathMatchPolicyImpl( - const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator) - : enabled_(true) { - - auto& factory = Envoy::Config::Utility::getAndCheckFactory(typed_config); - - config_ = Envoy::Config::Utility::translateAnyToFactoryConfig(typed_config.typed_config(), - validator, factory); - - absl::StatusOr matcher = factory.createPathMatcher(*config_); - - if (!matcher.ok()) { - throw EnvoyException(std::string(matcher.status().message())); - } - - path_matcher_ = matcher.value(); -} - -PathMatcherSharedPtr PathMatchPolicyImpl::pathMatcher() const { return path_matcher_; } - absl::flat_hash_set InternalRedirectPolicyImpl::buildRedirectResponseCodes( const envoy::config::route::v3::InternalRedirectPolicy& policy_config) const { if (policy_config.redirect_response_codes_size() == 0) { @@ -530,8 +484,8 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, ProtobufMessage::ValidationVisitor& validator) : case_sensitive_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.match(), case_sensitive, true)), prefix_rewrite_(route.route().prefix_rewrite()), - path_match_policy_(buildPathMatchPolicy(route, validator)), - path_rewrite_policy_(buildPathRewritePolicy(route, validator)), + extension_path_matcher_(buildPathMatchPolicy(route, validator)), + extension_path_rewriter_(buildPathRewritePolicy(route, validator)), host_rewrite_(route.route().host_rewrite_literal()), vhost_(vhost), auto_host_rewrite_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.route(), auto_host_rewrite, false)), auto_host_rewrite_header_(!route.route().host_rewrite_header().empty() @@ -698,7 +652,7 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, } int num_rewrite_polices = 0; - if (path_rewrite_policy_.enabled()) { + if (extension_path_rewriter_ != nullptr) { ++num_rewrite_polices; } @@ -721,9 +675,9 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, regex_rewrite_substitution_ = rewrite_spec.substitution(); } - if (path_rewrite_policy_.enabled()) { - absl::Status compatible_status = path_rewrite_policy_.pathRewriter()->isCompatiblePathMatcher( - path_match_policy_.pathMatcher(), path_match_policy_.enabled()); + if (extension_path_rewriter_ != nullptr) { + absl::Status compatible_status = extension_path_rewriter_->isCompatiblePathMatcher( + extension_path_matcher_); if (!compatible_status.ok()) { throw EnvoyException(std::string(compatible_status.message())); } @@ -894,7 +848,7 @@ void RouteEntryImplBase::finalizeRequestHeaders(Http::RequestHeaderMap& headers, // Handle path rewrite absl::optional container; if (!getPathRewrite(headers, container).empty() || regex_rewrite_ != nullptr || - path_rewrite_policy_.enabled()) { + extension_path_rewriter_ != nullptr) { rewritePathHeader(headers, insert_envoy_original_path); } } @@ -1030,11 +984,11 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa } } - if (path_rewrite_policy_.enabled()) { + if (extension_path_rewriter_ != nullptr) { absl::string_view just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); absl::StatusOr new_path = - path_rewrite_policy_.pathRewriter()->rewritePath(just_path, matched_path); + extension_path_rewriter_->rewritePath(just_path, matched_path); // if rewrite fails return old path. if (!new_path.ok()) { @@ -1234,23 +1188,47 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( } return InternalRedirectPolicyImpl{policy_config, validator, current_route_name}; } -PathRewritePolicyImpl +PathRewriterSharedPtr RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { if (route.route().has_path_rewrite_policy()) { - return PathRewritePolicyImpl{route.route().path_rewrite_policy(), validator}; + auto& factory = Envoy::Config::Utility::getAndCheckFactory( + route.route().path_rewrite_policy()); + + ProtobufTypes::MessagePtr config = Envoy::Config::Utility::translateAnyToFactoryConfig( + route.route().path_rewrite_policy().typed_config(), validator, factory); + + absl::StatusOr rewriter = factory.createPathRewriter(*config); + + if (!rewriter.ok()) { + throw EnvoyException(std::string(rewriter.status().message())); + } + + return rewriter.value(); } - return {}; + return nullptr; } -PathMatchPolicyImpl +PathMatcherSharedPtr RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { if (route.match().has_path_match_policy()) { - return PathMatchPolicyImpl{route.match().path_match_policy(), validator}; + auto& factory = Envoy::Config::Utility::getAndCheckFactory( + route.match().path_match_policy()); + + ProtobufTypes::MessagePtr config = Envoy::Config::Utility::translateAnyToFactoryConfig( + route.match().path_match_policy().typed_config(), validator, factory); + + absl::StatusOr matcher = factory.createPathMatcher(*config); + + if (!matcher.ok()) { + throw EnvoyException(std::string(matcher.status().message())); + } + + return matcher.value(); } - return {}; + return nullptr; } DecoratorConstPtr RouteEntryImplBase::parseDecorator(const envoy::config::route::v3::Route& route) { @@ -1496,18 +1474,18 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - uri_template_(path_match_policy_.pathMatcher()->uriTemplate()){}; + uri_template_(extension_path_matcher_->uriTemplate()){}; void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { - finalizePathHeader(headers, path_match_policy_.pathMatcher()->uriTemplate(), + finalizePathHeader(headers, extension_path_matcher_->uriTemplate(), insert_envoy_original_path); } absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { return currentUrlPathAfterRewriteWithMatchedPath(headers, - path_match_policy_.pathMatcher()->uriTemplate()); + extension_path_matcher_->uriTemplate()); } RouteConstSharedPtr @@ -1515,7 +1493,7 @@ PathMatchPolicyRouteEntryImpl::matches(const Http::RequestHeaderMap& headers, const StreamInfo::StreamInfo& stream_info, uint64_t random_value) const { if (RouteEntryImplBase::matchRoute(headers, stream_info, random_value) && - path_match_policy_.pathMatcher()->match(headers.getPathValue())) { + extension_path_matcher_->match(headers.getPathValue())) { return clusterEntry(headers, random_value); } return nullptr; diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 509727c98774b..72ad6712c1cab 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -457,52 +457,6 @@ class RouteTracingImpl : public RouteTracing { Tracing::CustomTagMap custom_tags_; }; -/** - * Implementation of PathMatchPolicy that reads from the proto - * PathMatchPolicy of the RouteAction. - */ -class PathMatchPolicyImpl : public PathMatchPolicy { -public: - PathMatchPolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator); - - // Constructs a disabled PathMatchPolicyImpl - PathMatchPolicyImpl(); - - // Router::PathMatchPolicy - bool enabled() const override { return enabled_; } - - PathMatcherSharedPtr pathMatcher() const override; - -private: - const bool enabled_; - ProtobufTypes::MessagePtr config_; - PathMatcherSharedPtr path_matcher_; -}; - -/** - * Implementation of PathRewritePolicyImpl that reads from the proto - * PathRewritePolicyImpl of the RouteAction. - */ -class PathRewritePolicyImpl : public PathRewritePolicy { -public: - PathRewritePolicyImpl(const envoy::config::core::v3::TypedExtensionConfig typed_config, - ProtobufMessage::ValidationVisitor& validator); - - // Constructs a disabled PathRewritePolicyImpl - PathRewritePolicyImpl(); - - // Router::PathRewritePolicy - bool enabled() const override { return enabled_; } - - PathRewriterSharedPtr pathRewriter() const override; - -private: - const bool enabled_; - ProtobufTypes::MessagePtr config_; - PathRewriterSharedPtr rewriter_; -}; - /** * Implementation of InternalRedirectPolicy that reads from the proto * InternalRedirectPolicy of the RouteAction. @@ -615,8 +569,8 @@ class RouteEntryImplBase : public RouteEntry, return internal_redirect_policy_; } - const PathMatchPolicyImpl& pathMatchPolicy() const override { return path_match_policy_; } - const PathRewritePolicyImpl& pathRewritePolicy() const override { return path_rewrite_policy_; } + const PathMatcherSharedPtr& pathMatcher() const override { return extension_path_matcher_; } + const PathRewriterSharedPtr& pathRewriter() const override { return extension_path_rewriter_; } uint32_t retryShadowBufferLimit() const override { return retry_shadow_buffer_limit_; } const std::vector& shadowPolicies() const override { return shadow_policies_; } @@ -731,11 +685,11 @@ class RouteEntryImplBase : public RouteEntry, const InternalRedirectPolicy& internalRedirectPolicy() const override { return parent_->internalRedirectPolicy(); } - const PathMatchPolicyImpl& pathMatchPolicy() const override { - return parent_->pathMatchPolicy(); + const PathMatcherSharedPtr& pathMatcher() const override { + return parent_->pathMatcher(); } - const PathRewritePolicyImpl& pathRewritePolicy() const override { - return parent_->pathRewritePolicy(); + const PathRewriterSharedPtr& pathRewriter() const override { + return parent_->pathRewriter(); } uint32_t retryShadowBufferLimit() const override { return parent_->retryShadowBufferLimit(); } const std::vector& shadowPolicies() const override { @@ -902,8 +856,8 @@ class RouteEntryImplBase : public RouteEntry, const std::string prefix_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_redirect_; - const PathMatchPolicyImpl path_match_policy_; - const PathRewritePolicyImpl path_rewrite_policy_; + const PathMatcherSharedPtr extension_path_matcher_; + const PathRewriterSharedPtr extension_path_rewriter_; std::string regex_rewrite_substitution_; std::string regex_rewrite_redirect_substitution_; const std::string host_rewrite_; @@ -985,10 +939,10 @@ class RouteEntryImplBase : public RouteEntry, ProtobufMessage::ValidationVisitor& validator, absl::string_view current_route_name) const; - PathMatchPolicyImpl buildPathMatchPolicy(envoy::config::route::v3::Route route, + PathMatcherSharedPtr buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const; - PathRewritePolicyImpl buildPathRewritePolicy(envoy::config::route::v3::Route route, + PathRewriterSharedPtr buildPathRewritePolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const; RouteConstSharedPtr pickClusterViaClusterHeader(const Http::LowerCaseString& cluster_header_name, @@ -1081,7 +1035,7 @@ class PathMatchPolicyRouteEntryImpl : public RouteEntryImplBase { // Router::PathMatchCriterion const std::string& matcher() const override { return uri_template_; } - PathMatchType matchType() const override { return PathMatchType::Policy; } + PathMatchType matchType() const override { return PathMatchType::Template; } // Router::Matchable RouteConstSharedPtr matches(const Http::RequestHeaderMap& headers, diff --git a/source/common/router/delegating_route_impl.cc b/source/common/router/delegating_route_impl.cc index 6864272261568..f60a255a10d18 100644 --- a/source/common/router/delegating_route_impl.cc +++ b/source/common/router/delegating_route_impl.cc @@ -76,12 +76,12 @@ const RetryPolicy& DelegatingRouteEntry::retryPolicy() const { return base_route_->routeEntry()->retryPolicy(); } -const PathMatchPolicy& DelegatingRouteEntry::pathMatchPolicy() const { - return base_route_->routeEntry()->pathMatchPolicy(); +const PathMatcherSharedPtr& DelegatingRouteEntry::pathMatcher() const { + return base_route_->routeEntry()->pathMatcher(); } -const PathRewritePolicy& DelegatingRouteEntry::pathRewritePolicy() const { - return base_route_->routeEntry()->pathRewritePolicy(); +const PathRewriterSharedPtr& DelegatingRouteEntry::pathRewriter() const { + return base_route_->routeEntry()->pathRewriter(); } const InternalRedirectPolicy& DelegatingRouteEntry::internalRedirectPolicy() const { diff --git a/source/common/router/delegating_route_impl.h b/source/common/router/delegating_route_impl.h index 2fb75d7d3ba8f..aa7b41d01f925 100644 --- a/source/common/router/delegating_route_impl.h +++ b/source/common/router/delegating_route_impl.h @@ -84,8 +84,8 @@ class DelegatingRouteEntry : public Router::RouteEntry { Upstream::ResourcePriority priority() const override; const RateLimitPolicy& rateLimitPolicy() const override; const RetryPolicy& retryPolicy() const override; - const Router::PathMatchPolicy& pathMatchPolicy() const override; - const Router::PathRewritePolicy& pathRewritePolicy() const override; + const Router::PathMatcherSharedPtr& pathMatcher() const override; + const Router::PathRewriterSharedPtr& pathRewriter() const override; const InternalRedirectPolicy& internalRedirectPolicy() const override; uint32_t retryShadowBufferLimit() const override; const std::vector& shadowPolicies() const override; diff --git a/source/extensions/path/match/uri_template/BUILD b/source/extensions/path/match/uri_template/BUILD index a8a0b7c25255b..4ba146a5b340e 100644 --- a/source/extensions/path/match/uri_template/BUILD +++ b/source/extensions/path/match/uri_template/BUILD @@ -22,7 +22,7 @@ envoy_cc_library( "//test/extensions/path/rewrite/uri_template:__subpackages__", ], deps = [ - "//envoy/router:router_path_match_policy_interface", + "//envoy/router:path_matcher_interface", "//source/common/protobuf", "//source/common/protobuf:utility_lib", "//source/extensions/path/uri_template_lib", @@ -38,7 +38,7 @@ envoy_cc_extension( deps = [ ":uri_template_match_lib", "//envoy/registry", - "//envoy/router:router_path_match_policy_interface", + "//envoy/router:path_matcher_interface", "//source/extensions/path/uri_template_lib", "@envoy_api//envoy/extensions/path/match/uri_template/v3:pkg_cc_proto", ], diff --git a/source/extensions/path/match/uri_template/uri_template_match.h b/source/extensions/path/match/uri_template/uri_template_match.h index 39f652b06f057..23ecd89a23605 100644 --- a/source/extensions/path/match/uri_template/uri_template_match.h +++ b/source/extensions/path/match/uri_template/uri_template_match.h @@ -33,9 +33,7 @@ class UriTemplateMatcher : public Router::PathMatcher { // Router::PathMatcher bool match(absl::string_view path) const override; - absl::string_view uriTemplate() const override; - absl::string_view name() const override { return NAME; } private: diff --git a/source/extensions/path/rewrite/uri_template/BUILD b/source/extensions/path/rewrite/uri_template/BUILD index 85835bf510e61..a4bb5d17c0578 100644 --- a/source/extensions/path/rewrite/uri_template/BUILD +++ b/source/extensions/path/rewrite/uri_template/BUILD @@ -20,8 +20,8 @@ envoy_cc_library( "//test/extensions/path/rewrite/uri_template:__subpackages__", ], deps = [ - "//envoy/router:router_path_match_policy_interface", - "//envoy/router:router_path_rewrite_policy_interface", + "//envoy/router:path_matcher_interface", + "//envoy/router:path_rewriter_interface", "//source/common/protobuf", "//source/common/protobuf:utility_lib", "//source/extensions/path/match/uri_template:uri_template_match_lib", @@ -39,7 +39,7 @@ envoy_cc_extension( deps = [ ":uri_template_rewrite_lib", "//envoy/registry", - "//envoy/router:router_path_rewrite_policy_interface", + "//envoy/router:path_rewriter_interface", "//source/extensions/path/uri_template_lib", "@envoy_api//envoy/extensions/path/rewrite/uri_template/v3:pkg_cc_proto", ], diff --git a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc index 54e7bf68ef6c3..fe74dda829f77 100644 --- a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc +++ b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc @@ -24,9 +24,8 @@ namespace Rewrite { #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif -absl::Status UriTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher, - bool active_matcher) const { - if (!active_matcher || path_matcher->name() != Extensions::UriTemplate::Match::NAME) { +absl::Status UriTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher) const { + if (path_matcher == nullptr || path_matcher->name() != Extensions::UriTemplate::Match::NAME) { return absl::InvalidArgumentError(fmt::format("unable to use {} extension without {} extension", Extensions::UriTemplate::Rewrite::NAME, Extensions::UriTemplate::Match::NAME)); diff --git a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.h b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.h index edc5130b3254e..8da0b30e4b842 100644 --- a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.h +++ b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.h @@ -21,7 +21,7 @@ namespace Extensions { namespace UriTemplate { namespace Rewrite { -const absl::string_view NAME = "envoy.path.rewrite.uri_template.pattern_template_rewriter"; +const absl::string_view NAME = "envoy.path.rewrite.uri_template.uri_template_rewriter"; /** * UriTemplateRewriter allows rewriting paths based on match pattern variables provided @@ -53,10 +53,7 @@ class UriTemplateRewriter : public Router::PathRewriter { */ absl::StatusOr rewritePath(absl::string_view pattern, absl::string_view matched_path) const override; - - absl::Status isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher, - bool active_matcher) const override; - + absl::Status isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher) const override; absl::string_view name() const override { return NAME; } private: diff --git a/test/common/router/config_impl_test.cc b/test/common/router/config_impl_test.cc index b771a41adbbe2..32b77cad6900b 100644 --- a/test/common/router/config_impl_test.cc +++ b/test/common/router/config_impl_test.cc @@ -8836,8 +8836,8 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsDisabledWhenNotSpecifiedInRouteAct TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); Http::TestRequestHeaderMapImpl headers = genRedirectHeaders("idle.lyft.com", "/regex", true, false); - const auto& pattern_template_policy = config.route(headers, 0)->routeEntry()->pathMatchPolicy(); - EXPECT_FALSE(pattern_template_policy.enabled()); + const auto& pattern_template_policy = config.route(headers, 0)->routeEntry()->pathMatcher(); + EXPECT_TRUE(pattern_template_policy == nullptr); } TEST_F(RouteConfigurationV2, TemplatePatternIsFilledFromConfigInRouteAction) { @@ -8866,13 +8866,13 @@ TEST_F(RouteConfigurationV2, TemplatePatternIsFilledFromConfigInRouteAction) { TestConfigImpl config(parseRouteConfigurationFromYaml(yaml), factory_context_, true); Http::TestRequestHeaderMapImpl headers = genHeaders("path.prefix.com", "/bar/one/two", "GET"); - const auto& pattern_match_policy = config.route(headers, 0)->routeEntry()->pathMatchPolicy(); - EXPECT_TRUE(pattern_match_policy.enabled()); - EXPECT_EQ(pattern_match_policy.pathMatcher()->uriTemplate(), "/bar/{country}/{lang}"); + const auto& pattern_match_policy = config.route(headers, 0)->routeEntry()->pathMatcher(); + EXPECT_TRUE(pattern_match_policy != nullptr); + EXPECT_EQ(pattern_match_policy->uriTemplate(), "/bar/{country}/{lang}"); - const auto& pattern_rewrite_policy = config.route(headers, 0)->routeEntry()->pathRewritePolicy(); - EXPECT_TRUE(pattern_rewrite_policy.enabled()); - EXPECT_EQ(pattern_rewrite_policy.pathRewriter()->uriTemplate(), "/bar/{lang}/{country}"); + const auto& pattern_rewrite_policy = config.route(headers, 0)->routeEntry()->pathRewriter(); + EXPECT_TRUE(pattern_rewrite_policy != nullptr); + EXPECT_EQ(pattern_rewrite_policy->uriTemplate(), "/bar/{lang}/{country}"); } TEST_F(RouteMatcherTest, SimplePathPatternMatchOnly) { diff --git a/test/extensions/path/rewrite/uri_template/library_test.cc b/test/extensions/path/rewrite/uri_template/library_test.cc index 47d74cab2090c..46535f9e817f1 100644 --- a/test/extensions/path/rewrite/uri_template/library_test.cc +++ b/test/extensions/path/rewrite/uri_template/library_test.cc @@ -101,10 +101,10 @@ TEST(RewriteTest, MatchPatternValidation) { path_template: "/bar/{lang}/{country}" )EOF"; - Router::PathRewriterSharedPtr rewrite_predicate = createRewriterFromYaml(rewrite_yaml_string); - Router::PathMatcherSharedPtr match_predicate = createMatcherPredicateFromYaml(match_yaml_string); + Router::PathRewriterSharedPtr rewriter = createRewriterFromYaml(rewrite_yaml_string); + Router::PathMatcherSharedPtr matcher = createMatcherPredicateFromYaml(match_yaml_string); - EXPECT_TRUE(rewrite_predicate->isCompatiblePathMatcher(match_predicate, true).ok()); + EXPECT_TRUE(rewriter->isCompatiblePathMatcher(matcher).ok()); } TEST(RewriteTest, MatchPatternInactive) { @@ -115,17 +115,9 @@ TEST(RewriteTest, MatchPatternInactive) { path_template_rewrite: "/foo/{lang}/{country}" )EOF"; - const std::string match_yaml_string = R"EOF( - name: envoy.path.match.uri_template.uri_template_matcher - typed_config: - "@type": type.googleapis.com/envoy.extensions.path.match.uri_template.v3.UriTemplateMatchConfig - path_template: "/bar/{lang}/{country}" -)EOF"; - - Router::PathRewriterSharedPtr rewrite_predicate = createRewriterFromYaml(rewrite_yaml_string); - Router::PathMatcherSharedPtr match_predicate = createMatcherPredicateFromYaml(match_yaml_string); + Router::PathRewriterSharedPtr rewriter = createRewriterFromYaml(rewrite_yaml_string); - absl::Status error = rewrite_predicate->isCompatiblePathMatcher(match_predicate, false); + absl::Status error = rewriter->isCompatiblePathMatcher(nullptr); EXPECT_FALSE(error.ok()); EXPECT_EQ(error.message(), "unable to use envoy.path.rewrite.uri_template.uri_template_rewriter " "extension without envoy.path.match.uri_template.uri_template_matcher " @@ -147,10 +139,10 @@ TEST(RewriteTest, MatchPatternMismatchedVars) { path_template: "/bar/{lang}/{country}" )EOF"; - Router::PathRewriterSharedPtr rewrite_predicate = createRewriterFromYaml(rewrite_yaml_string); - Router::PathMatcherSharedPtr match_predicate = createMatcherPredicateFromYaml(match_yaml_string); + Router::PathRewriterSharedPtr rewriter = createRewriterFromYaml(rewrite_yaml_string); + Router::PathMatcherSharedPtr matcher = createMatcherPredicateFromYaml(match_yaml_string); - absl::Status error = rewrite_predicate->isCompatiblePathMatcher(match_predicate, true); + absl::Status error = rewriter->isCompatiblePathMatcher(matcher); EXPECT_FALSE(error.ok()); EXPECT_EQ(error.message(), "mismatch between variables in path_match_policy " "/bar/{lang}/{country} and path_rewrite_policy /foo/{lang}/{missing}"); diff --git a/test/mocks/router/mocks.cc b/test/mocks/router/mocks.cc index 74b27d634ec30..72762b45ec577 100644 --- a/test/mocks/router/mocks.cc +++ b/test/mocks/router/mocks.cc @@ -28,14 +28,6 @@ MockInternalRedirectPolicy::MockInternalRedirectPolicy() { ON_CALL(*this, enabled()).WillByDefault(Return(false)); } -MockPathMatchPolicy::MockPathMatchPolicy() { - ON_CALL(*this, enabled()).WillByDefault(Return(false)); -} - -MockPathRewritePolicy::MockPathRewritePolicy() { - ON_CALL(*this, enabled()).WillByDefault(Return(false)); -} - MockRetryState::MockRetryState() = default; void MockRetryState::expectHeadersRetry() { @@ -108,7 +100,6 @@ MockRouteEntry::MockRouteEntry() { ON_CALL(*this, rateLimitPolicy()).WillByDefault(ReturnRef(rate_limit_policy_)); ON_CALL(*this, retryPolicy()).WillByDefault(ReturnRef(retry_policy_)); ON_CALL(*this, internalRedirectPolicy()).WillByDefault(ReturnRef(internal_redirect_policy_)); - ON_CALL(*this, pathMatchPolicy()).WillByDefault(ReturnRef(path_match_policy_)); ON_CALL(*this, retryShadowBufferLimit()) .WillByDefault(Return(std::numeric_limits::max())); ON_CALL(*this, shadowPolicies()).WillByDefault(ReturnRef(shadow_policies_)); diff --git a/test/mocks/router/mocks.h b/test/mocks/router/mocks.h index fed00408bcf8d..eaa26ab71561a 100644 --- a/test/mocks/router/mocks.h +++ b/test/mocks/router/mocks.h @@ -168,28 +168,21 @@ class MockInternalRedirectPredicate : public InternalRedirectPredicate { MOCK_METHOD(absl::string_view, name, (), (const)); }; -class MockPathRewritePolicy : public PathRewritePolicy { -public: - MockPathRewritePolicy(); - MOCK_METHOD(bool, enabled, (), (const)); - MOCK_METHOD(PathRewriterSharedPtr, pathRewriter, (), (const)); -}; - class MockPathRewriter : public PathRewriter { public: MOCK_METHOD(absl::string_view, name, (), (const)); -}; + MOCK_METHOD(absl::StatusOr, rewritePath, (absl::string_view path, absl::string_view rewrite_pattern), (const)); + MOCK_METHOD(absl::string_view, uriTemplate, (), (const)); + MOCK_METHOD(absl::Status, isCompatiblePathMatcher, (PathMatcherSharedPtr path_matcher), (const)); -class MockPathMatchPolicy : public PathMatchPolicy { -public: - MockPathMatchPolicy(); - MOCK_METHOD(bool, enabled, (), (const)); - MOCK_METHOD(PathMatcherSharedPtr, pathMatcher, (), (const)); }; class MockPathMatcher : public PathMatcher { public: MOCK_METHOD(absl::string_view, name, (), (const)); + MOCK_METHOD(bool, match, (absl::string_view path), (const)); + MOCK_METHOD(absl::string_view, uriTemplate, (), (const)); + }; class MockRetryState : public RetryState { @@ -432,6 +425,8 @@ class MockRouteEntry : public RouteEntry { MOCK_METHOD(const UpgradeMap&, upgradeMap, (), (const)); MOCK_METHOD(const std::string&, routeName, (), (const)); MOCK_METHOD(const EarlyDataPolicy&, earlyDataPolicy, (), (const)); + MOCK_METHOD(const PathMatcherSharedPtr&, pathMatcher, (), (const)); + MOCK_METHOD(const PathRewriterSharedPtr&, pathRewriter, (), (const)); const RouteStatsContextOptRef routeStatsContext() const override { return RouteStatsContextOptRef(); @@ -443,8 +438,8 @@ class MockRouteEntry : public RouteEntry { TestVirtualCluster virtual_cluster_; TestRetryPolicy retry_policy_; testing::NiceMock internal_redirect_policy_; - testing::NiceMock path_match_policy_; - testing::NiceMock path_rewrite_policy_; + testing::NiceMock extension_path_matcher_; + testing::NiceMock extension_path_rewriter_; TestHedgePolicy hedge_policy_; testing::NiceMock rate_limit_policy_; std::vector shadow_policies_; From 77216446e0d09751c1c2cedd5c031e132f132d01 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Wed, 7 Sep 2022 20:17:39 +0000 Subject: [PATCH 224/238] Remove policy classes Signed-off-by: silverstar195 --- source/common/http/async_client_impl.h | 4 +++- source/common/router/config_impl.cc | 10 ++++------ source/common/router/config_impl.h | 10 +++------- .../path/rewrite/uri_template/uri_template_rewrite.cc | 3 ++- test/mocks/router/mocks.h | 5 ++--- 5 files changed, 14 insertions(+), 18 deletions(-) diff --git a/source/common/http/async_client_impl.h b/source/common/http/async_client_impl.h index 95a7868259f40..41ad6a797823e 100644 --- a/source/common/http/async_client_impl.h +++ b/source/common/http/async_client_impl.h @@ -231,7 +231,9 @@ class AsyncStreamImpl : public AsyncClient::Stream, const Router::InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } - const Router::PathMatcherSharedPtr& pathMatcher() const override { return extension_path_matcher_; } + const Router::PathMatcherSharedPtr& pathMatcher() const override { + return extension_path_matcher_; + } const Router::PathRewriterSharedPtr& pathRewriter() const override { return extension_path_rewriter_; } diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 87a922842f3c3..b778ec917e572 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -676,8 +676,8 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, } if (extension_path_rewriter_ != nullptr) { - absl::Status compatible_status = extension_path_rewriter_->isCompatiblePathMatcher( - extension_path_matcher_); + absl::Status compatible_status = + extension_path_rewriter_->isCompatiblePathMatcher(extension_path_matcher_); if (!compatible_status.ok()) { throw EnvoyException(std::string(compatible_status.message())); } @@ -1478,14 +1478,12 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { - finalizePathHeader(headers, extension_path_matcher_->uriTemplate(), - insert_envoy_original_path); + finalizePathHeader(headers, extension_path_matcher_->uriTemplate(), insert_envoy_original_path); } absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { - return currentUrlPathAfterRewriteWithMatchedPath(headers, - extension_path_matcher_->uriTemplate()); + return currentUrlPathAfterRewriteWithMatchedPath(headers, extension_path_matcher_->uriTemplate()); } RouteConstSharedPtr diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 72ad6712c1cab..ce193da98be59 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -685,12 +685,8 @@ class RouteEntryImplBase : public RouteEntry, const InternalRedirectPolicy& internalRedirectPolicy() const override { return parent_->internalRedirectPolicy(); } - const PathMatcherSharedPtr& pathMatcher() const override { - return parent_->pathMatcher(); - } - const PathRewriterSharedPtr& pathRewriter() const override { - return parent_->pathRewriter(); - } + const PathMatcherSharedPtr& pathMatcher() const override { return parent_->pathMatcher(); } + const PathRewriterSharedPtr& pathRewriter() const override { return parent_->pathRewriter(); } uint32_t retryShadowBufferLimit() const override { return parent_->retryShadowBufferLimit(); } const std::vector& shadowPolicies() const override { return parent_->shadowPolicies(); @@ -940,7 +936,7 @@ class RouteEntryImplBase : public RouteEntry, absl::string_view current_route_name) const; PathMatcherSharedPtr buildPathMatchPolicy(envoy::config::route::v3::Route route, - ProtobufMessage::ValidationVisitor& validator) const; + ProtobufMessage::ValidationVisitor& validator) const; PathRewriterSharedPtr buildPathRewritePolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const; diff --git a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc index fe74dda829f77..8e0ab82a590d0 100644 --- a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc +++ b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc @@ -24,7 +24,8 @@ namespace Rewrite { #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #endif -absl::Status UriTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher) const { +absl::Status +UriTemplateRewriter::isCompatiblePathMatcher(Router::PathMatcherSharedPtr path_matcher) const { if (path_matcher == nullptr || path_matcher->name() != Extensions::UriTemplate::Match::NAME) { return absl::InvalidArgumentError(fmt::format("unable to use {} extension without {} extension", Extensions::UriTemplate::Rewrite::NAME, diff --git a/test/mocks/router/mocks.h b/test/mocks/router/mocks.h index eaa26ab71561a..2512c488163d0 100644 --- a/test/mocks/router/mocks.h +++ b/test/mocks/router/mocks.h @@ -171,10 +171,10 @@ class MockInternalRedirectPredicate : public InternalRedirectPredicate { class MockPathRewriter : public PathRewriter { public: MOCK_METHOD(absl::string_view, name, (), (const)); - MOCK_METHOD(absl::StatusOr, rewritePath, (absl::string_view path, absl::string_view rewrite_pattern), (const)); + MOCK_METHOD(absl::StatusOr, rewritePath, + (absl::string_view path, absl::string_view rewrite_pattern), (const)); MOCK_METHOD(absl::string_view, uriTemplate, (), (const)); MOCK_METHOD(absl::Status, isCompatiblePathMatcher, (PathMatcherSharedPtr path_matcher), (const)); - }; class MockPathMatcher : public PathMatcher { @@ -182,7 +182,6 @@ class MockPathMatcher : public PathMatcher { MOCK_METHOD(absl::string_view, name, (), (const)); MOCK_METHOD(bool, match, (absl::string_view path), (const)); MOCK_METHOD(absl::string_view, uriTemplate, (), (const)); - }; class MockRetryState : public RetryState { From 09b63b4922493cd24abc0d5d5482c3fd4576df92 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 8 Sep 2022 19:14:55 +0000 Subject: [PATCH 225/238] Remove old policy classes and address comments Signed-off-by: silverstar195 --- envoy/router/path_rewriter.h | 3 +- envoy/router/router.h | 42 +-------- source/common/http/async_client_impl.cc | 2 +- source/common/http/async_client_impl.h | 6 +- source/common/router/config_impl.cc | 109 ++++++++++++------------ source/common/router/config_impl.h | 18 ++-- test/mocks/router/mocks.h | 8 +- tools/extensions/extensions_schema.yaml | 3 + 8 files changed, 76 insertions(+), 115 deletions(-) diff --git a/envoy/router/path_rewriter.h b/envoy/router/path_rewriter.h index ca31f3c30f79f..af8642df52ea2 100644 --- a/envoy/router/path_rewriter.h +++ b/envoy/router/path_rewriter.h @@ -24,7 +24,6 @@ class PathRewriter : Logger::Loggable { * Determines if the matcher policy is compatible. * * @param path_match_policy current path match policy for route - * @param active_policy true if user provided policy * @return true if current path match policy is acceptable */ virtual absl::Status isCompatiblePathMatcher(PathMatcherSharedPtr path_matcher) const PURE; @@ -54,7 +53,7 @@ class PathRewriter : Logger::Loggable { using PathRewriterSharedPtr = std::shared_ptr; /** - * Factory for PathRewrite. + * Factory for PathRewriter. */ class PathRewriterFactory : public Envoy::Config::TypedFactory { public: diff --git a/envoy/router/router.h b/envoy/router/router.h index 7a47691b297e0..c1b79c36c69aa 100644 --- a/envoy/router/router.h +++ b/envoy/router/router.h @@ -304,44 +304,6 @@ class RetryPolicy { */ enum class RetryStatus { No, NoOverflow, NoRetryLimitExceeded, Yes }; -/** - * PathMatchPolicy from the route configuration. - */ -class PathMatchPolicy { -public: - virtual ~PathMatchPolicy() = default; - - /** - * @return whether path match policy is enabled on this route. - */ - virtual bool enabled() const PURE; - - /** - * Returns the stored target route PathMatcher. - * @return a PathMatcher instance. - */ - virtual PathMatcherSharedPtr pathMatcher() const PURE; -}; - -/** - * PathRewriterPolicy from the route configuration. - */ -class PathRewritePolicy { -public: - virtual ~PathRewritePolicy() = default; - - /** - * @return whether path rewrite policy is enabled on this route. - */ - virtual bool enabled() const PURE; - - /** - * Returns the stored target route PathRewriter. - * @return a PathRewriter instance. - */ - virtual PathRewriterSharedPtr pathRewriter() const PURE; -}; - /** * InternalRedirectPolicy from the route configuration. */ @@ -961,12 +923,12 @@ class RouteEntry : public ResponseEntry { virtual const InternalRedirectPolicy& internalRedirectPolicy() const PURE; /** - * @return const PathMatcherPolicy& the path match policy for the route. + * @return const PathMatcherSharedPtr& the path match policy for the route. */ virtual const PathMatcherSharedPtr& pathMatcher() const PURE; /** - * @return const PathRewriterPolicy& the path match rewrite for the route. + * @return const PathRewriterSharedPtr& the path match rewrite for the route. */ virtual const PathRewriterSharedPtr& pathRewriter() const PURE; diff --git a/source/common/http/async_client_impl.cc b/source/common/http/async_client_impl.cc index 731fbe35c22e5..bff28f14c6f16 100644 --- a/source/common/http/async_client_impl.cc +++ b/source/common/http/async_client_impl.cc @@ -21,7 +21,7 @@ const AsyncStreamImpl::NullHedgePolicy AsyncStreamImpl::RouteEntryImpl::hedge_po const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::RouteEntryImpl::rate_limit_policy_; const Router::InternalRedirectPolicyImpl AsyncStreamImpl::RouteEntryImpl::internal_redirect_policy_; const Router::PathMatcherSharedPtr AsyncStreamImpl::RouteEntryImpl::extension_path_matcher_; -const Router::PathRewriterSharedPtr AsyncStreamImpl::RouteEntryImpl::extension_path_rewriter_; +const Router::PathRewriterSharedPtr AsyncStreamImpl::RouteEntryImpl::path_rewriter_; const std::vector AsyncStreamImpl::RouteEntryImpl::shadow_policies_; const AsyncStreamImpl::NullVirtualHost AsyncStreamImpl::RouteEntryImpl::virtual_host_; const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::NullVirtualHost::rate_limit_policy_; diff --git a/source/common/http/async_client_impl.h b/source/common/http/async_client_impl.h index e690937387fa4..3f4b18c6c2099 100644 --- a/source/common/http/async_client_impl.h +++ b/source/common/http/async_client_impl.h @@ -234,9 +234,7 @@ class AsyncStreamImpl : public AsyncClient::Stream, const Router::PathMatcherSharedPtr& pathMatcher() const override { return extension_path_matcher_; } - const Router::PathRewriterSharedPtr& pathRewriter() const override { - return extension_path_rewriter_; - } + const Router::PathRewriterSharedPtr& pathRewriter() const override { return path_rewriter_; } uint32_t retryShadowBufferLimit() const override { return std::numeric_limits::max(); } @@ -301,7 +299,7 @@ class AsyncStreamImpl : public AsyncClient::Stream, static const NullRateLimitPolicy rate_limit_policy_; static const Router::InternalRedirectPolicyImpl internal_redirect_policy_; static const Router::PathMatcherSharedPtr extension_path_matcher_; - static const Router::PathRewriterSharedPtr extension_path_rewriter_; + static const Router::PathRewriterSharedPtr path_rewriter_; static const std::vector shadow_policies_; static const NullVirtualHost virtual_host_; static const std::multimap opaque_config_; diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index b778ec917e572..98e164efaff0d 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -107,7 +107,7 @@ RouteEntryImplBaseConstSharedPtr createAndValidateRoute( break; } case envoy::config::route::v3::RouteMatch::PathSpecifierCase::kPathMatchPolicy: { - route = std::make_shared( + route = std::make_shared( vhost, route_config, optional_http_filters, factory_context, validator); break; } @@ -485,7 +485,7 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, : case_sensitive_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.match(), case_sensitive, true)), prefix_rewrite_(route.route().prefix_rewrite()), extension_path_matcher_(buildPathMatchPolicy(route, validator)), - extension_path_rewriter_(buildPathRewritePolicy(route, validator)), + path_rewriter_(buildPathRewritePolicy(route, validator)), host_rewrite_(route.route().host_rewrite_literal()), vhost_(vhost), auto_host_rewrite_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.route(), auto_host_rewrite, false)), auto_host_rewrite_header_(!route.route().host_rewrite_header().empty() @@ -652,7 +652,7 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, } int num_rewrite_polices = 0; - if (extension_path_rewriter_ != nullptr) { + if (path_rewriter_ != nullptr) { ++num_rewrite_polices; } @@ -675,9 +675,9 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, regex_rewrite_substitution_ = rewrite_spec.substitution(); } - if (extension_path_rewriter_ != nullptr) { + if (path_rewriter_ != nullptr) { absl::Status compatible_status = - extension_path_rewriter_->isCompatiblePathMatcher(extension_path_matcher_); + path_rewriter_->isCompatiblePathMatcher(extension_path_matcher_); if (!compatible_status.ok()) { throw EnvoyException(std::string(compatible_status.message())); } @@ -848,7 +848,7 @@ void RouteEntryImplBase::finalizeRequestHeaders(Http::RequestHeaderMap& headers, // Handle path rewrite absl::optional container; if (!getPathRewrite(headers, container).empty() || regex_rewrite_ != nullptr || - extension_path_rewriter_ != nullptr) { + path_rewriter_ != nullptr) { rewritePathHeader(headers, insert_envoy_original_path); } } @@ -966,29 +966,30 @@ absl::optional RouteEntryImplBase::currentUrlPathAfterRewriteWithMa const Http::RequestHeaderMap& headers, absl::string_view matched_path) const { absl::optional container; const auto& rewrite = getPathRewrite(headers, container); - if (!rewrite.empty() || regex_rewrite_ != nullptr) { - // TODO(perf): can we avoid the string copy for the common case? - std::string path(headers.getPathValue()); - - if (!rewrite.empty()) { - ASSERT(case_sensitive_ ? absl::StartsWith(path, matched_path) - : absl::StartsWithIgnoreCase(path, matched_path)); - return path.replace(0, matched_path.size(), rewrite); - } + if (rewrite.empty() && regex_rewrite_ == nullptr && path_rewriter_ == nullptr) { + // There are no rewrites configured. + return {}; + } - if (regex_rewrite_ != nullptr) { - // Replace the entire path, but preserve the query parameters - auto just_path(Http::PathUtil::removeQueryAndFragment(path)); - return path.replace(0, just_path.size(), - regex_rewrite_->replaceAll(just_path, regex_rewrite_substitution_)); - } + // TODO(perf): can we avoid the string copy for the common case? + std::string path(headers.getPathValue()); + if (!rewrite.empty()) { + ASSERT(case_sensitive_ ? absl::StartsWith(path, matched_path) + : absl::StartsWithIgnoreCase(path, matched_path)); + return path.replace(0, matched_path.size(), rewrite); + } + + if (regex_rewrite_ != nullptr) { + // Replace the entire path, but preserve the query parameters + auto just_path(Http::PathUtil::removeQueryAndFragment(path)); + return path.replace(0, just_path.size(), + regex_rewrite_->replaceAll(just_path, regex_rewrite_substitution_)); } - if (extension_path_rewriter_ != nullptr) { + if (path_rewriter_ != nullptr) { absl::string_view just_path(Http::PathUtil::removeQueryAndFragment(headers.getPathValue())); - absl::StatusOr new_path = - extension_path_rewriter_->rewritePath(just_path, matched_path); + absl::StatusOr new_path = path_rewriter_->rewritePath(just_path, matched_path); // if rewrite fails return old path. if (!new_path.ok()) { @@ -1191,44 +1192,44 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( PathRewriterSharedPtr RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { - if (route.route().has_path_rewrite_policy()) { - auto& factory = Envoy::Config::Utility::getAndCheckFactory( - route.route().path_rewrite_policy()); + if (!route.route().has_path_rewrite_policy()) { + return nullptr; + } - ProtobufTypes::MessagePtr config = Envoy::Config::Utility::translateAnyToFactoryConfig( - route.route().path_rewrite_policy().typed_config(), validator, factory); + auto& factory = Envoy::Config::Utility::getAndCheckFactory( + route.route().path_rewrite_policy()); - absl::StatusOr rewriter = factory.createPathRewriter(*config); + ProtobufTypes::MessagePtr config = Envoy::Config::Utility::translateAnyToFactoryConfig( + route.route().path_rewrite_policy().typed_config(), validator, factory); - if (!rewriter.ok()) { - throw EnvoyException(std::string(rewriter.status().message())); - } + absl::StatusOr rewriter = factory.createPathRewriter(*config); - return rewriter.value(); + if (!rewriter.ok()) { + throw EnvoyException(std::string(rewriter.status().message())); } - return nullptr; + + return rewriter.value(); } PathMatcherSharedPtr RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { - if (route.match().has_path_match_policy()) { - auto& factory = Envoy::Config::Utility::getAndCheckFactory( - route.match().path_match_policy()); - - ProtobufTypes::MessagePtr config = Envoy::Config::Utility::translateAnyToFactoryConfig( - route.match().path_match_policy().typed_config(), validator, factory); + if (!route.match().has_path_match_policy()) { + return nullptr; + } + auto& factory = Envoy::Config::Utility::getAndCheckFactory( + route.match().path_match_policy()); - absl::StatusOr matcher = factory.createPathMatcher(*config); + ProtobufTypes::MessagePtr config = Envoy::Config::Utility::translateAnyToFactoryConfig( + route.match().path_match_policy().typed_config(), validator, factory); - if (!matcher.ok()) { - throw EnvoyException(std::string(matcher.status().message())); - } + absl::StatusOr matcher = factory.createPathMatcher(*config); - return matcher.value(); + if (!matcher.ok()) { + throw EnvoyException(std::string(matcher.status().message())); } - return nullptr; + return matcher.value(); } DecoratorConstPtr RouteEntryImplBase::parseDecorator(const envoy::config::route::v3::Route& route) { @@ -1468,7 +1469,7 @@ void RouteEntryImplBase::WeightedClusterEntry::traversePerFilterConfig( } } -PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( +UriTemplateMatcherRouteEntryImpl::UriTemplateMatcherRouteEntryImpl( const VirtualHostImpl& vhost, const envoy::config::route::v3::Route& route, const OptionalHttpFilters& optional_http_filters, Server::Configuration::ServerFactoryContext& factory_context, @@ -1476,20 +1477,20 @@ PathMatchPolicyRouteEntryImpl::PathMatchPolicyRouteEntryImpl( : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), uri_template_(extension_path_matcher_->uriTemplate()){}; -void PathMatchPolicyRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, - bool insert_envoy_original_path) const { +void UriTemplateMatcherRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, + bool insert_envoy_original_path) const { finalizePathHeader(headers, extension_path_matcher_->uriTemplate(), insert_envoy_original_path); } -absl::optional PathMatchPolicyRouteEntryImpl::currentUrlPathAfterRewrite( +absl::optional UriTemplateMatcherRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { return currentUrlPathAfterRewriteWithMatchedPath(headers, extension_path_matcher_->uriTemplate()); } RouteConstSharedPtr -PathMatchPolicyRouteEntryImpl::matches(const Http::RequestHeaderMap& headers, - const StreamInfo::StreamInfo& stream_info, - uint64_t random_value) const { +UriTemplateMatcherRouteEntryImpl::matches(const Http::RequestHeaderMap& headers, + const StreamInfo::StreamInfo& stream_info, + uint64_t random_value) const { if (RouteEntryImplBase::matchRoute(headers, stream_info, random_value) && extension_path_matcher_->match(headers.getPathValue())) { return clusterEntry(headers, random_value); diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index ce193da98be59..f975a8fd32fc8 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -570,7 +570,7 @@ class RouteEntryImplBase : public RouteEntry, } const PathMatcherSharedPtr& pathMatcher() const override { return extension_path_matcher_; } - const PathRewriterSharedPtr& pathRewriter() const override { return extension_path_rewriter_; } + const PathRewriterSharedPtr& pathRewriter() const override { return path_rewriter_; } uint32_t retryShadowBufferLimit() const override { return retry_shadow_buffer_limit_; } const std::vector& shadowPolicies() const override { return shadow_policies_; } @@ -853,7 +853,7 @@ class RouteEntryImplBase : public RouteEntry, Regex::CompiledMatcherPtr regex_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_redirect_; const PathMatcherSharedPtr extension_path_matcher_; - const PathRewriterSharedPtr extension_path_rewriter_; + const PathRewriterSharedPtr path_rewriter_; std::string regex_rewrite_substitution_; std::string regex_rewrite_redirect_substitution_; const std::string host_rewrite_; @@ -1019,15 +1019,15 @@ class RouteEntryImplBase : public RouteEntry, }; /** - * Route entry implementation for path match policy based routing. + * Route entry implementation for uri template match based routing. */ -class PathMatchPolicyRouteEntryImpl : public RouteEntryImplBase { +class UriTemplateMatcherRouteEntryImpl : public RouteEntryImplBase { public: - PathMatchPolicyRouteEntryImpl(const VirtualHostImpl& vhost, - const envoy::config::route::v3::Route& route, - const OptionalHttpFilters& optional_http_filters, - Server::Configuration::ServerFactoryContext& factory_context, - ProtobufMessage::ValidationVisitor& validator); + UriTemplateMatcherRouteEntryImpl(const VirtualHostImpl& vhost, + const envoy::config::route::v3::Route& route, + const OptionalHttpFilters& optional_http_filters, + Server::Configuration::ServerFactoryContext& factory_context, + ProtobufMessage::ValidationVisitor& validator); // Router::PathMatchCriterion const std::string& matcher() const override { return uri_template_; } diff --git a/test/mocks/router/mocks.h b/test/mocks/router/mocks.h index 2512c488163d0..334f2c809dc27 100644 --- a/test/mocks/router/mocks.h +++ b/test/mocks/router/mocks.h @@ -396,8 +396,8 @@ class MockRouteEntry : public RouteEntry { MOCK_METHOD(const RateLimitPolicy&, rateLimitPolicy, (), (const)); MOCK_METHOD(const RetryPolicy&, retryPolicy, (), (const)); MOCK_METHOD(const InternalRedirectPolicy&, internalRedirectPolicy, (), (const)); - MOCK_METHOD(const PathMatchPolicy&, pathMatchPolicy, (), (const)); - MOCK_METHOD(const PathRewritePolicy&, pathRewritePolicy, (), (const)); + MOCK_METHOD(const PathMatcherSharedPtr&, pathMatcher, (), (const)); + MOCK_METHOD(const PathRewriterSharedPtr&, pathRewriter, (), (const)); MOCK_METHOD(uint32_t, retryShadowBufferLimit, (), (const)); MOCK_METHOD(const std::vector&, shadowPolicies, (), (const)); MOCK_METHOD(std::chrono::milliseconds, timeout, (), (const)); @@ -424,8 +424,6 @@ class MockRouteEntry : public RouteEntry { MOCK_METHOD(const UpgradeMap&, upgradeMap, (), (const)); MOCK_METHOD(const std::string&, routeName, (), (const)); MOCK_METHOD(const EarlyDataPolicy&, earlyDataPolicy, (), (const)); - MOCK_METHOD(const PathMatcherSharedPtr&, pathMatcher, (), (const)); - MOCK_METHOD(const PathRewriterSharedPtr&, pathRewriter, (), (const)); const RouteStatsContextOptRef routeStatsContext() const override { return RouteStatsContextOptRef(); @@ -438,7 +436,7 @@ class MockRouteEntry : public RouteEntry { TestRetryPolicy retry_policy_; testing::NiceMock internal_redirect_policy_; testing::NiceMock extension_path_matcher_; - testing::NiceMock extension_path_rewriter_; + testing::NiceMock path_rewriter_; TestHedgePolicy hedge_policy_; testing::NiceMock rate_limit_policy_; std::vector shadow_policies_; diff --git a/tools/extensions/extensions_schema.yaml b/tools/extensions/extensions_schema.yaml index 8707f682b5dd9..34b41b583e569 100644 --- a/tools/extensions/extensions_schema.yaml +++ b/tools/extensions/extensions_schema.yaml @@ -109,6 +109,9 @@ categories: - envoy.matching.http.custom_matchers - envoy.matching.network.input - envoy.matching.network.custom_matchers +- envoy.filters.http.upstream +- envoy.path.match +- envoy.path.rewrite status_values: - name: stable From bbf017a4676c8f9bc81e0ff24e93a64c86a14907 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 8 Sep 2022 19:27:06 +0000 Subject: [PATCH 226/238] Revert extensions_build_config.bzl Signed-off-by: silverstar195 --- source/extensions/extensions_build_config.bzl | 356 ++++++++++-------- 1 file changed, 196 insertions(+), 160 deletions(-) diff --git a/source/extensions/extensions_build_config.bzl b/source/extensions/extensions_build_config.bzl index 3809ac1a4706c..5530795f7e662 100644 --- a/source/extensions/extensions_build_config.bzl +++ b/source/extensions/extensions_build_config.bzl @@ -3,332 +3,368 @@ EXTENSIONS = { # # Access loggers # - "envoy.access_loggers.file": "//source/extensions/access_loggers/file:config", - "envoy.access_loggers.extension_filters.cel": "//source/extensions/access_loggers/filters/cel:config", - "envoy.access_loggers.http_grpc": "//source/extensions/access_loggers/grpc:http_config", - "envoy.access_loggers.tcp_grpc": "//source/extensions/access_loggers/grpc:tcp_config", - "envoy.access_loggers.open_telemetry": "//source/extensions/access_loggers/open_telemetry:config", - "envoy.access_loggers.stdout": "//source/extensions/access_loggers/stream:config", - "envoy.access_loggers.stderr": "//source/extensions/access_loggers/stream:config", - "envoy.access_loggers.wasm": "//source/extensions/access_loggers/wasm:config", + + "envoy.access_loggers.file": "//source/extensions/access_loggers/file:config", + "envoy.access_loggers.extension_filters.cel": "//source/extensions/access_loggers/filters/cel:config", + "envoy.access_loggers.http_grpc": "//source/extensions/access_loggers/grpc:http_config", + "envoy.access_loggers.tcp_grpc": "//source/extensions/access_loggers/grpc:tcp_config", + "envoy.access_loggers.open_telemetry": "//source/extensions/access_loggers/open_telemetry:config", + "envoy.access_loggers.stdout": "//source/extensions/access_loggers/stream:config", + "envoy.access_loggers.stderr": "//source/extensions/access_loggers/stream:config", + "envoy.access_loggers.wasm": "//source/extensions/access_loggers/wasm:config", # # Clusters # - "envoy.clusters.aggregate": "//source/extensions/clusters/aggregate:cluster", - "envoy.clusters.dynamic_forward_proxy": "//source/extensions/clusters/dynamic_forward_proxy:cluster", - "envoy.clusters.redis": "//source/extensions/clusters/redis:redis_cluster", + + "envoy.clusters.aggregate": "//source/extensions/clusters/aggregate:cluster", + "envoy.clusters.dynamic_forward_proxy": "//source/extensions/clusters/dynamic_forward_proxy:cluster", + "envoy.clusters.redis": "//source/extensions/clusters/redis:redis_cluster", # # Compression # - "envoy.compression.gzip.compressor": "//source/extensions/compression/gzip/compressor:config", - "envoy.compression.gzip.decompressor": "//source/extensions/compression/gzip/decompressor:config", - "envoy.compression.brotli.compressor": "//source/extensions/compression/brotli/compressor:config", - "envoy.compression.brotli.decompressor": "//source/extensions/compression/brotli/decompressor:config", - "envoy.compression.zstd.compressor": "//source/extensions/compression/zstd/compressor:config", - "envoy.compression.zstd.decompressor": "//source/extensions/compression/zstd/decompressor:config", + + "envoy.compression.gzip.compressor": "//source/extensions/compression/gzip/compressor:config", + "envoy.compression.gzip.decompressor": "//source/extensions/compression/gzip/decompressor:config", + "envoy.compression.brotli.compressor": "//source/extensions/compression/brotli/compressor:config", + "envoy.compression.brotli.decompressor": "//source/extensions/compression/brotli/decompressor:config", + "envoy.compression.zstd.compressor": "//source/extensions/compression/zstd/compressor:config", + "envoy.compression.zstd.decompressor": "//source/extensions/compression/zstd/decompressor:config", # # Config validators # - "envoy.config.validators.minimum_clusters_validator": "//source/extensions/config/validators/minimum_clusters:config", + + "envoy.config.validators.minimum_clusters_validator": "//source/extensions/config/validators/minimum_clusters:config", # # gRPC Credentials Plugins # - "envoy.grpc_credentials.file_based_metadata": "//source/extensions/grpc_credentials/file_based_metadata:config", - "envoy.grpc_credentials.aws_iam": "//source/extensions/grpc_credentials/aws_iam:config", + + "envoy.grpc_credentials.file_based_metadata": "//source/extensions/grpc_credentials/file_based_metadata:config", + "envoy.grpc_credentials.aws_iam": "//source/extensions/grpc_credentials/aws_iam:config", # # WASM # - "envoy.bootstrap.wasm": "//source/extensions/bootstrap/wasm:config", + + "envoy.bootstrap.wasm": "//source/extensions/bootstrap/wasm:config", # # Health checkers # - "envoy.health_checkers.redis": "//source/extensions/health_checkers/redis:config", + + "envoy.health_checkers.redis": "//source/extensions/health_checkers/redis:config", # # Input Matchers # - "envoy.matching.matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", - "envoy.matching.matchers.ip": "//source/extensions/matching/input_matchers/ip:config", + + "envoy.matching.matchers.consistent_hashing": "//source/extensions/matching/input_matchers/consistent_hashing:config", + "envoy.matching.matchers.ip": "//source/extensions/matching/input_matchers/ip:config", # # Generic Inputs # - "envoy.matching.common_inputs.environment_variable": "//source/extensions/matching/common_inputs/environment_variable:config", + + "envoy.matching.common_inputs.environment_variable": "//source/extensions/matching/common_inputs/environment_variable:config", # # HTTP filters # - "envoy.filters.http.adaptive_concurrency": "//source/extensions/filters/http/adaptive_concurrency:config", - "envoy.filters.http.admission_control": "//source/extensions/filters/http/admission_control:config", - "envoy.filters.http.alternate_protocols_cache": "//source/extensions/filters/http/alternate_protocols_cache:config", - "envoy.filters.http.aws_lambda": "//source/extensions/filters/http/aws_lambda:config", - "envoy.filters.http.aws_request_signing": "//source/extensions/filters/http/aws_request_signing:config", - "envoy.filters.http.bandwidth_limit": "//source/extensions/filters/http/bandwidth_limit:config", - "envoy.filters.http.buffer": "//source/extensions/filters/http/buffer:config", - "envoy.filters.http.cache": "//source/extensions/filters/http/cache:config", - "envoy.filters.http.cdn_loop": "//source/extensions/filters/http/cdn_loop:config", - "envoy.filters.http.compressor": "//source/extensions/filters/http/compressor:config", - "envoy.filters.http.cors": "//source/extensions/filters/http/cors:config", - "envoy.filters.http.composite": "//source/extensions/filters/http/composite:config", - "envoy.filters.http.csrf": "//source/extensions/filters/http/csrf:config", - "envoy.filters.http.decompressor": "//source/extensions/filters/http/decompressor:config", - "envoy.filters.http.dynamic_forward_proxy": "//source/extensions/filters/http/dynamic_forward_proxy:config", - "envoy.filters.http.ext_authz": "//source/extensions/filters/http/ext_authz:config", - "envoy.filters.http.ext_proc": "//source/extensions/filters/http/ext_proc:config", - "envoy.filters.http.fault": "//source/extensions/filters/http/fault:config", - "envoy.filters.http.file_system_buffer": "//source/extensions/filters/http/file_system_buffer:config", - "envoy.filters.http.gcp_authn": "//source/extensions/filters/http/gcp_authn:config", - "envoy.filters.http.grpc_http1_bridge": "//source/extensions/filters/http/grpc_http1_bridge:config", - "envoy.filters.http.grpc_http1_reverse_bridge": "//source/extensions/filters/http/grpc_http1_reverse_bridge:config", - "envoy.filters.http.grpc_json_transcoder": "//source/extensions/filters/http/grpc_json_transcoder:config", - "envoy.filters.http.grpc_stats": "//source/extensions/filters/http/grpc_stats:config", - "envoy.filters.http.grpc_web": "//source/extensions/filters/http/grpc_web:config", - "envoy.filters.http.header_to_metadata": "//source/extensions/filters/http/header_to_metadata:config", - "envoy.filters.http.health_check": "//source/extensions/filters/http/health_check:config", - "envoy.filters.http.ip_tagging": "//source/extensions/filters/http/ip_tagging:config", - "envoy.filters.http.jwt_authn": "//source/extensions/filters/http/jwt_authn:config", + + "envoy.filters.http.adaptive_concurrency": "//source/extensions/filters/http/adaptive_concurrency:config", + "envoy.filters.http.admission_control": "//source/extensions/filters/http/admission_control:config", + "envoy.filters.http.alternate_protocols_cache": "//source/extensions/filters/http/alternate_protocols_cache:config", + "envoy.filters.http.aws_lambda": "//source/extensions/filters/http/aws_lambda:config", + "envoy.filters.http.aws_request_signing": "//source/extensions/filters/http/aws_request_signing:config", + "envoy.filters.http.bandwidth_limit": "//source/extensions/filters/http/bandwidth_limit:config", + "envoy.filters.http.buffer": "//source/extensions/filters/http/buffer:config", + "envoy.filters.http.cache": "//source/extensions/filters/http/cache:config", + "envoy.filters.http.cdn_loop": "//source/extensions/filters/http/cdn_loop:config", + "envoy.filters.http.compressor": "//source/extensions/filters/http/compressor:config", + "envoy.filters.http.cors": "//source/extensions/filters/http/cors:config", + "envoy.filters.http.composite": "//source/extensions/filters/http/composite:config", + "envoy.filters.http.csrf": "//source/extensions/filters/http/csrf:config", + "envoy.filters.http.decompressor": "//source/extensions/filters/http/decompressor:config", + "envoy.filters.http.dynamic_forward_proxy": "//source/extensions/filters/http/dynamic_forward_proxy:config", + "envoy.filters.http.ext_authz": "//source/extensions/filters/http/ext_authz:config", + "envoy.filters.http.ext_proc": "//source/extensions/filters/http/ext_proc:config", + "envoy.filters.http.fault": "//source/extensions/filters/http/fault:config", + "envoy.filters.http.file_system_buffer": "//source/extensions/filters/http/file_system_buffer:config", + "envoy.filters.http.gcp_authn": "//source/extensions/filters/http/gcp_authn:config", + "envoy.filters.http.grpc_http1_bridge": "//source/extensions/filters/http/grpc_http1_bridge:config", + "envoy.filters.http.grpc_http1_reverse_bridge": "//source/extensions/filters/http/grpc_http1_reverse_bridge:config", + "envoy.filters.http.grpc_json_transcoder": "//source/extensions/filters/http/grpc_json_transcoder:config", + "envoy.filters.http.grpc_stats": "//source/extensions/filters/http/grpc_stats:config", + "envoy.filters.http.grpc_web": "//source/extensions/filters/http/grpc_web:config", + "envoy.filters.http.header_to_metadata": "//source/extensions/filters/http/header_to_metadata:config", + "envoy.filters.http.health_check": "//source/extensions/filters/http/health_check:config", + "envoy.filters.http.ip_tagging": "//source/extensions/filters/http/ip_tagging:config", + "envoy.filters.http.jwt_authn": "//source/extensions/filters/http/jwt_authn:config", # Disabled by default - "envoy.filters.http.kill_request": "//source/extensions/filters/http/kill_request:kill_request_config", - "envoy.filters.http.local_ratelimit": "//source/extensions/filters/http/local_ratelimit:config", - "envoy.filters.http.lua": "//source/extensions/filters/http/lua:config", - "envoy.filters.http.oauth2": "//source/extensions/filters/http/oauth2:config", - "envoy.filters.http.on_demand": "//source/extensions/filters/http/on_demand:config", - "envoy.filters.http.original_src": "//source/extensions/filters/http/original_src:config", - "envoy.filters.http.ratelimit": "//source/extensions/filters/http/ratelimit:config", - "envoy.filters.http.rbac": "//source/extensions/filters/http/rbac:config", - "envoy.filters.http.router": "//source/extensions/filters/http/router:config", - "envoy.filters.http.set_metadata": "//source/extensions/filters/http/set_metadata:config", - "envoy.filters.http.tap": "//source/extensions/filters/http/tap:config", - "envoy.filters.http.wasm": "//source/extensions/filters/http/wasm:config", - "envoy.filters.http.stateful_session": "//source/extensions/filters/http/stateful_session:config", + "envoy.filters.http.kill_request": "//source/extensions/filters/http/kill_request:kill_request_config", + "envoy.filters.http.local_ratelimit": "//source/extensions/filters/http/local_ratelimit:config", + "envoy.filters.http.lua": "//source/extensions/filters/http/lua:config", + "envoy.filters.http.oauth2": "//source/extensions/filters/http/oauth2:config", + "envoy.filters.http.on_demand": "//source/extensions/filters/http/on_demand:config", + "envoy.filters.http.original_src": "//source/extensions/filters/http/original_src:config", + "envoy.filters.http.ratelimit": "//source/extensions/filters/http/ratelimit:config", + "envoy.filters.http.rbac": "//source/extensions/filters/http/rbac:config", + "envoy.filters.http.router": "//source/extensions/filters/http/router:config", + "envoy.filters.http.set_metadata": "//source/extensions/filters/http/set_metadata:config", + "envoy.filters.http.tap": "//source/extensions/filters/http/tap:config", + "envoy.filters.http.wasm": "//source/extensions/filters/http/wasm:config", + "envoy.filters.http.stateful_session": "//source/extensions/filters/http/stateful_session:config", # # Listener filters # - "envoy.filters.listener.http_inspector": "//source/extensions/filters/listener/http_inspector:config", + + "envoy.filters.listener.http_inspector": "//source/extensions/filters/listener/http_inspector:config", # NOTE: The original_dst filter is implicitly loaded if original_dst functionality is # configured on the listener. Do not remove it in that case or configs will fail to load. - "envoy.filters.listener.original_dst": "//source/extensions/filters/listener/original_dst:config", - "envoy.filters.listener.original_src": "//source/extensions/filters/listener/original_src:config", + "envoy.filters.listener.original_dst": "//source/extensions/filters/listener/original_dst:config", + "envoy.filters.listener.original_src": "//source/extensions/filters/listener/original_src:config", # NOTE: The proxy_protocol filter is implicitly loaded if proxy_protocol functionality is # configured on the listener. Do not remove it in that case or configs will fail to load. - "envoy.filters.listener.proxy_protocol": "//source/extensions/filters/listener/proxy_protocol:config", - "envoy.filters.listener.tls_inspector": "//source/extensions/filters/listener/tls_inspector:config", + "envoy.filters.listener.proxy_protocol": "//source/extensions/filters/listener/proxy_protocol:config", + "envoy.filters.listener.tls_inspector": "//source/extensions/filters/listener/tls_inspector:config", # # Network filters # - "envoy.filters.network.connection_limit": "//source/extensions/filters/network/connection_limit:config", - "envoy.filters.network.direct_response": "//source/extensions/filters/network/direct_response:config", - "envoy.filters.network.dubbo_proxy": "//source/extensions/filters/network/dubbo_proxy:config", - "envoy.filters.network.echo": "//source/extensions/filters/network/echo:config", - "envoy.filters.network.ext_authz": "//source/extensions/filters/network/ext_authz:config", - "envoy.filters.network.http_connection_manager": "//source/extensions/filters/network/http_connection_manager:config", - "envoy.filters.network.local_ratelimit": "//source/extensions/filters/network/local_ratelimit:config", - "envoy.filters.network.mongo_proxy": "//source/extensions/filters/network/mongo_proxy:config", - "envoy.filters.network.ratelimit": "//source/extensions/filters/network/ratelimit:config", - "envoy.filters.network.rbac": "//source/extensions/filters/network/rbac:config", - "envoy.filters.network.redis_proxy": "//source/extensions/filters/network/redis_proxy:config", - "envoy.filters.network.tcp_proxy": "//source/extensions/filters/network/tcp_proxy:config", - "envoy.filters.network.thrift_proxy": "//source/extensions/filters/network/thrift_proxy:config", - "envoy.filters.network.sni_cluster": "//source/extensions/filters/network/sni_cluster:config", - "envoy.filters.network.sni_dynamic_forward_proxy": "//source/extensions/filters/network/sni_dynamic_forward_proxy:config", - "envoy.filters.network.wasm": "//source/extensions/filters/network/wasm:config", - "envoy.filters.network.zookeeper_proxy": "//source/extensions/filters/network/zookeeper_proxy:config", + + "envoy.filters.network.connection_limit": "//source/extensions/filters/network/connection_limit:config", + "envoy.filters.network.direct_response": "//source/extensions/filters/network/direct_response:config", + "envoy.filters.network.dubbo_proxy": "//source/extensions/filters/network/dubbo_proxy:config", + "envoy.filters.network.echo": "//source/extensions/filters/network/echo:config", + "envoy.filters.network.ext_authz": "//source/extensions/filters/network/ext_authz:config", + "envoy.filters.network.http_connection_manager": "//source/extensions/filters/network/http_connection_manager:config", + "envoy.filters.network.local_ratelimit": "//source/extensions/filters/network/local_ratelimit:config", + "envoy.filters.network.mongo_proxy": "//source/extensions/filters/network/mongo_proxy:config", + "envoy.filters.network.ratelimit": "//source/extensions/filters/network/ratelimit:config", + "envoy.filters.network.rbac": "//source/extensions/filters/network/rbac:config", + "envoy.filters.network.redis_proxy": "//source/extensions/filters/network/redis_proxy:config", + "envoy.filters.network.tcp_proxy": "//source/extensions/filters/network/tcp_proxy:config", + "envoy.filters.network.thrift_proxy": "//source/extensions/filters/network/thrift_proxy:config", + "envoy.filters.network.sni_cluster": "//source/extensions/filters/network/sni_cluster:config", + "envoy.filters.network.sni_dynamic_forward_proxy": "//source/extensions/filters/network/sni_dynamic_forward_proxy:config", + "envoy.filters.network.wasm": "//source/extensions/filters/network/wasm:config", + "envoy.filters.network.zookeeper_proxy": "//source/extensions/filters/network/zookeeper_proxy:config", # # UDP filters # - "envoy.filters.udp.dns_filter": "//source/extensions/filters/udp/dns_filter:config", - "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", + + "envoy.filters.udp.dns_filter": "//source/extensions/filters/udp/dns_filter:config", + "envoy.filters.udp_listener.udp_proxy": "//source/extensions/filters/udp/udp_proxy:config", # # Resource monitors # - "envoy.resource_monitors.fixed_heap": "//source/extensions/resource_monitors/fixed_heap:config", - "envoy.resource_monitors.injected_resource": "//source/extensions/resource_monitors/injected_resource:config", + + "envoy.resource_monitors.fixed_heap": "//source/extensions/resource_monitors/fixed_heap:config", + "envoy.resource_monitors.injected_resource": "//source/extensions/resource_monitors/injected_resource:config", # # Stat sinks # - "envoy.stat_sinks.dog_statsd": "//source/extensions/stat_sinks/dog_statsd:config", - "envoy.stat_sinks.graphite_statsd": "//source/extensions/stat_sinks/graphite_statsd:config", - "envoy.stat_sinks.hystrix": "//source/extensions/stat_sinks/hystrix:config", - "envoy.stat_sinks.metrics_service": "//source/extensions/stat_sinks/metrics_service:config", - "envoy.stat_sinks.statsd": "//source/extensions/stat_sinks/statsd:config", - "envoy.stat_sinks.wasm": "//source/extensions/stat_sinks/wasm:config", + + "envoy.stat_sinks.dog_statsd": "//source/extensions/stat_sinks/dog_statsd:config", + "envoy.stat_sinks.graphite_statsd": "//source/extensions/stat_sinks/graphite_statsd:config", + "envoy.stat_sinks.hystrix": "//source/extensions/stat_sinks/hystrix:config", + "envoy.stat_sinks.metrics_service": "//source/extensions/stat_sinks/metrics_service:config", + "envoy.stat_sinks.statsd": "//source/extensions/stat_sinks/statsd:config", + "envoy.stat_sinks.wasm": "//source/extensions/stat_sinks/wasm:config", # # Thrift filters # - "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", - "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", - "envoy.filters.thrift.rate_limit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", + + "envoy.filters.thrift.router": "//source/extensions/filters/network/thrift_proxy/router:config", + "envoy.filters.thrift.header_to_metadata": "//source/extensions/filters/network/thrift_proxy/filters/header_to_metadata:config", + "envoy.filters.thrift.rate_limit": "//source/extensions/filters/network/thrift_proxy/filters/ratelimit:config", # # Tracers # - "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", - "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", - "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", - "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", - "envoy.tracers.xray": "//source/extensions/tracers/xray:config", - "envoy.tracers.skywalking": "//source/extensions/tracers/skywalking:config", - "envoy.tracers.opentelemetry": "//source/extensions/tracers/opentelemetry:config", + + "envoy.tracers.dynamic_ot": "//source/extensions/tracers/dynamic_ot:config", + "envoy.tracers.datadog": "//source/extensions/tracers/datadog:config", + "envoy.tracers.zipkin": "//source/extensions/tracers/zipkin:config", + "envoy.tracers.opencensus": "//source/extensions/tracers/opencensus:config", + "envoy.tracers.xray": "//source/extensions/tracers/xray:config", + "envoy.tracers.skywalking": "//source/extensions/tracers/skywalking:config", + "envoy.tracers.opentelemetry": "//source/extensions/tracers/opentelemetry:config", # # Transport sockets # - "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", - "envoy.transport_sockets.http_11_proxy": "//source/extensions/transport_sockets/http_11_proxy:upstream_config", - "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", - "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", - "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", - "envoy.transport_sockets.starttls": "//source/extensions/transport_sockets/starttls:config", - "envoy.transport_sockets.tcp_stats": "//source/extensions/transport_sockets/tcp_stats:config", - "envoy.transport_sockets.internal_upstream": "//source/extensions/transport_sockets/internal_upstream:config", + + "envoy.transport_sockets.alts": "//source/extensions/transport_sockets/alts:config", + "envoy.transport_sockets.http_11_proxy": "//source/extensions/transport_sockets/http_11_proxy:upstream_config", + "envoy.transport_sockets.upstream_proxy_protocol": "//source/extensions/transport_sockets/proxy_protocol:upstream_config", + "envoy.transport_sockets.raw_buffer": "//source/extensions/transport_sockets/raw_buffer:config", + "envoy.transport_sockets.tap": "//source/extensions/transport_sockets/tap:config", + "envoy.transport_sockets.starttls": "//source/extensions/transport_sockets/starttls:config", + "envoy.transport_sockets.tcp_stats": "//source/extensions/transport_sockets/tcp_stats:config", + "envoy.transport_sockets.internal_upstream": "//source/extensions/transport_sockets/internal_upstream:config", # # Retry host predicates # - "envoy.retry_host_predicates.previous_hosts": "//source/extensions/retry/host/previous_hosts:config", - "envoy.retry_host_predicates.omit_canary_hosts": "//source/extensions/retry/host/omit_canary_hosts:config", - "envoy.retry_host_predicates.omit_host_metadata": "//source/extensions/retry/host/omit_host_metadata:config", + + "envoy.retry_host_predicates.previous_hosts": "//source/extensions/retry/host/previous_hosts:config", + "envoy.retry_host_predicates.omit_canary_hosts": "//source/extensions/retry/host/omit_canary_hosts:config", + "envoy.retry_host_predicates.omit_host_metadata": "//source/extensions/retry/host/omit_host_metadata:config", # # Retry priorities # - "envoy.retry_priorities.previous_priorities": "//source/extensions/retry/priority/previous_priorities:config", + + "envoy.retry_priorities.previous_priorities": "//source/extensions/retry/priority/previous_priorities:config", # # CacheFilter plugins # - "envoy.extensions.http.cache.simple": "//source/extensions/filters/http/cache/simple_http_cache:config", + "envoy.extensions.http.cache.simple": "//source/extensions/filters/http/cache/simple_http_cache:config", # # Internal redirect predicates # - "envoy.internal_redirect_predicates.allow_listed_routes": "//source/extensions/internal_redirect/allow_listed_routes:config", - "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", - "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", - # - # Path Pattern Match and Path Pattern Rewrite - # - "envoy.path.match.uri_template.uri_template_matcher": "//source/extensions/path/match/uri_template:config", - "envoy.path.rewrite.uri_template.uri_template_rewriter": "//source/extensions/path/rewrite/uri_template:config", + "envoy.internal_redirect_predicates.allow_listed_routes": "//source/extensions/internal_redirect/allow_listed_routes:config", + "envoy.internal_redirect_predicates.previous_routes": "//source/extensions/internal_redirect/previous_routes:config", + "envoy.internal_redirect_predicates.safe_cross_scheme": "//source/extensions/internal_redirect/safe_cross_scheme:config", # # Http Upstreams (excepting envoy.upstreams.http.generic which is hard-coded into the build so not registered here) # - "envoy.upstreams.http.http": "//source/extensions/upstreams/http/http:config", - "envoy.upstreams.http.tcp": "//source/extensions/upstreams/http/tcp:config", + + "envoy.upstreams.http.http": "//source/extensions/upstreams/http/http:config", + "envoy.upstreams.http.tcp": "//source/extensions/upstreams/http/tcp:config", # # Watchdog actions # - "envoy.watchdog.profile_action": "//source/extensions/watchdog/profile_action:config", + + "envoy.watchdog.profile_action": "//source/extensions/watchdog/profile_action:config", # # WebAssembly runtimes # - "envoy.wasm.runtime.null": "//source/extensions/wasm_runtime/null:config", - "envoy.wasm.runtime.v8": "//source/extensions/wasm_runtime/v8:config", - "envoy.wasm.runtime.wamr": "//source/extensions/wasm_runtime/wamr:config", - "envoy.wasm.runtime.wavm": "//source/extensions/wasm_runtime/wavm:config", - "envoy.wasm.runtime.wasmtime": "//source/extensions/wasm_runtime/wasmtime:config", + + "envoy.wasm.runtime.null": "//source/extensions/wasm_runtime/null:config", + "envoy.wasm.runtime.v8": "//source/extensions/wasm_runtime/v8:config", + "envoy.wasm.runtime.wamr": "//source/extensions/wasm_runtime/wamr:config", + "envoy.wasm.runtime.wavm": "//source/extensions/wasm_runtime/wavm:config", + "envoy.wasm.runtime.wasmtime": "//source/extensions/wasm_runtime/wasmtime:config", # # Rate limit descriptors # - "envoy.rate_limit_descriptors.expr": "//source/extensions/rate_limit_descriptors/expr:config", + + "envoy.rate_limit_descriptors.expr": "//source/extensions/rate_limit_descriptors/expr:config", # # IO socket # - "envoy.io_socket.user_space": "//source/extensions/io_socket/user_space:config", - "envoy.bootstrap.internal_listener": "//source/extensions/bootstrap/internal_listener:config", + + "envoy.io_socket.user_space": "//source/extensions/io_socket/user_space:config", + "envoy.bootstrap.internal_listener": "//source/extensions/bootstrap/internal_listener:config", # # TLS peer certification validators # - "envoy.tls.cert_validator.spiffe": "//source/extensions/transport_sockets/tls/cert_validator/spiffe:config", + + "envoy.tls.cert_validator.spiffe": "//source/extensions/transport_sockets/tls/cert_validator/spiffe:config", # # HTTP header formatters # - "envoy.http.stateful_header_formatters.preserve_case": "//source/extensions/http/header_formatters/preserve_case:config", + + "envoy.http.stateful_header_formatters.preserve_case": "//source/extensions/http/header_formatters/preserve_case:config", # # Original IP detection # - "envoy.http.original_ip_detection.custom_header": "//source/extensions/http/original_ip_detection/custom_header:config", - "envoy.http.original_ip_detection.xff": "//source/extensions/http/original_ip_detection/xff:config", + + "envoy.http.original_ip_detection.custom_header": "//source/extensions/http/original_ip_detection/custom_header:config", + "envoy.http.original_ip_detection.xff": "//source/extensions/http/original_ip_detection/xff:config", # # Stateful session # - "envoy.http.stateful_session.cookie": "//source/extensions/http/stateful_session/cookie:config", + + "envoy.http.stateful_session.cookie": "//source/extensions/http/stateful_session/cookie:config", # # QUIC extensions # - "envoy.quic.crypto_stream.server.quiche": "//source/extensions/quic/crypto_stream:envoy_quic_default_crypto_server_stream", - "envoy.quic.proof_source.filter_chain": "//source/extensions/quic/proof_source:envoy_quic_default_proof_source", + + "envoy.quic.crypto_stream.server.quiche": "//source/extensions/quic/crypto_stream:envoy_quic_default_crypto_server_stream", + "envoy.quic.proof_source.filter_chain": "//source/extensions/quic/proof_source:envoy_quic_default_proof_source", # # UDP packet writers # - "envoy.udp_packet_writer.default": "//source/extensions/udp_packet_writer/default:config", - "envoy.udp_packet_writer.gso": "//source/extensions/udp_packet_writer/gso:config", + "envoy.udp_packet_writer.default": "//source/extensions/udp_packet_writer/default:config", + "envoy.udp_packet_writer.gso": "//source/extensions/udp_packet_writer/gso:config", # # Formatter # - "envoy.formatter.metadata": "//source/extensions/formatter/metadata:config", - "envoy.formatter.req_without_query": "//source/extensions/formatter/req_without_query:config", + + "envoy.formatter.metadata": "//source/extensions/formatter/metadata:config", + "envoy.formatter.req_without_query": "//source/extensions/formatter/req_without_query:config", # # Key value store # - "envoy.key_value.file_based": "//source/extensions/key_value/file_based:config_lib", + + "envoy.key_value.file_based": "//source/extensions/key_value/file_based:config_lib", # # RBAC matchers # - "envoy.rbac.matchers.upstream_ip_port": "//source/extensions/filters/common/rbac/matchers:upstream_ip_port_lib", + + "envoy.rbac.matchers.upstream_ip_port": "//source/extensions/filters/common/rbac/matchers:upstream_ip_port_lib", # # DNS Resolver # # c-ares DNS resolver extension is recommended to be enabled to maintain the legacy DNS resolving behavior. - "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", + "envoy.network.dns_resolver.cares": "//source/extensions/network/dns_resolver/cares:config", # apple DNS resolver extension is only needed in MacOS build plus one want to use apple library for DNS resolving. - "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", + "envoy.network.dns_resolver.apple": "//source/extensions/network/dns_resolver/apple:config", # getaddrinfo DNS resolver extension can be used when the system resolver is desired (e.g., Android) - "envoy.network.dns_resolver.getaddrinfo": "//source/extensions/network/dns_resolver/getaddrinfo:config", + "envoy.network.dns_resolver.getaddrinfo": "//source/extensions/network/dns_resolver/getaddrinfo:config", # # Custom matchers # - "envoy.matching.custom_matchers.trie_matcher": "//source/extensions/common/matcher:trie_matcher_lib", + + "envoy.matching.custom_matchers.trie_matcher": "//source/extensions/common/matcher:trie_matcher_lib", # # Header Validators # - "envoy.http.header_validators.envoy_default": "//source/extensions/http/header_validators/envoy_default:config", + + "envoy.http.header_validators.envoy_default": "//source/extensions/http/header_validators/envoy_default:config", + # + # Path Pattern Match and Path Pattern Rewrite + # + "envoy.path.match.uri_template.uri_template_matcher": "//source/extensions/path/match/uri_template:config", + "envoy.path.rewrite.uri_template.uri_template_rewriter": "//source/extensions/path/rewrite/uri_template:config", # # Early Data option # - "envoy.route.early_data_policy.default": "//source/extensions/early_data:default_early_data_policy_lib", + + "envoy.route.early_data_policy.default": "//source/extensions/early_data:default_early_data_policy_lib", } # These can be changed to ["//visibility:public"], for downstream builds which From a9c59e4cb99cc11071fb73e6778aff7b10d2c2f2 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 9 Sep 2022 13:40:28 +0000 Subject: [PATCH 227/238] Change names for small nit Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 18 +++++++++--------- source/common/router/config_impl.h | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 98e164efaff0d..53a5c890c90e8 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -484,8 +484,8 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, ProtobufMessage::ValidationVisitor& validator) : case_sensitive_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.match(), case_sensitive, true)), prefix_rewrite_(route.route().prefix_rewrite()), - extension_path_matcher_(buildPathMatchPolicy(route, validator)), - path_rewriter_(buildPathRewritePolicy(route, validator)), + path_matcher_(buildPathMatcher(route, validator)), + path_rewriter_(buildPathRewriter(route, validator)), host_rewrite_(route.route().host_rewrite_literal()), vhost_(vhost), auto_host_rewrite_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(route.route(), auto_host_rewrite, false)), auto_host_rewrite_header_(!route.route().host_rewrite_header().empty() @@ -677,7 +677,7 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, if (path_rewriter_ != nullptr) { absl::Status compatible_status = - path_rewriter_->isCompatiblePathMatcher(extension_path_matcher_); + path_rewriter_->isCompatiblePathMatcher(path_matcher_); if (!compatible_status.ok()) { throw EnvoyException(std::string(compatible_status.message())); } @@ -1190,7 +1190,7 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( return InternalRedirectPolicyImpl{policy_config, validator, current_route_name}; } PathRewriterSharedPtr -RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::Route route, +RouteEntryImplBase::buildPathRewriter(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { if (!route.route().has_path_rewrite_policy()) { return nullptr; @@ -1212,7 +1212,7 @@ RouteEntryImplBase::buildPathRewritePolicy(envoy::config::route::v3::Route route } PathMatcherSharedPtr -RouteEntryImplBase::buildPathMatchPolicy(envoy::config::route::v3::Route route, +RouteEntryImplBase::buildPathMatcher(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const { if (!route.match().has_path_match_policy()) { return nullptr; @@ -1475,16 +1475,16 @@ UriTemplateMatcherRouteEntryImpl::UriTemplateMatcherRouteEntryImpl( Server::Configuration::ServerFactoryContext& factory_context, ProtobufMessage::ValidationVisitor& validator) : RouteEntryImplBase(vhost, route, optional_http_filters, factory_context, validator), - uri_template_(extension_path_matcher_->uriTemplate()){}; + uri_template_(path_matcher_->uriTemplate()){}; void UriTemplateMatcherRouteEntryImpl::rewritePathHeader(Http::RequestHeaderMap& headers, bool insert_envoy_original_path) const { - finalizePathHeader(headers, extension_path_matcher_->uriTemplate(), insert_envoy_original_path); + finalizePathHeader(headers, path_matcher_->uriTemplate(), insert_envoy_original_path); } absl::optional UriTemplateMatcherRouteEntryImpl::currentUrlPathAfterRewrite( const Http::RequestHeaderMap& headers) const { - return currentUrlPathAfterRewriteWithMatchedPath(headers, extension_path_matcher_->uriTemplate()); + return currentUrlPathAfterRewriteWithMatchedPath(headers, path_matcher_->uriTemplate()); } RouteConstSharedPtr @@ -1492,7 +1492,7 @@ UriTemplateMatcherRouteEntryImpl::matches(const Http::RequestHeaderMap& headers, const StreamInfo::StreamInfo& stream_info, uint64_t random_value) const { if (RouteEntryImplBase::matchRoute(headers, stream_info, random_value) && - extension_path_matcher_->match(headers.getPathValue())) { + path_matcher_->match(headers.getPathValue())) { return clusterEntry(headers, random_value); } return nullptr; diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index f975a8fd32fc8..4b22007fcfa20 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -569,7 +569,7 @@ class RouteEntryImplBase : public RouteEntry, return internal_redirect_policy_; } - const PathMatcherSharedPtr& pathMatcher() const override { return extension_path_matcher_; } + const PathMatcherSharedPtr& pathMatcher() const override { return path_matcher_; } const PathRewriterSharedPtr& pathRewriter() const override { return path_rewriter_; } uint32_t retryShadowBufferLimit() const override { return retry_shadow_buffer_limit_; } @@ -852,7 +852,7 @@ class RouteEntryImplBase : public RouteEntry, const std::string prefix_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_; Regex::CompiledMatcherPtr regex_rewrite_redirect_; - const PathMatcherSharedPtr extension_path_matcher_; + const PathMatcherSharedPtr path_matcher_; const PathRewriterSharedPtr path_rewriter_; std::string regex_rewrite_substitution_; std::string regex_rewrite_redirect_substitution_; @@ -935,10 +935,10 @@ class RouteEntryImplBase : public RouteEntry, ProtobufMessage::ValidationVisitor& validator, absl::string_view current_route_name) const; - PathMatcherSharedPtr buildPathMatchPolicy(envoy::config::route::v3::Route route, + PathMatcherSharedPtr buildPathMatcher(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const; - PathRewriterSharedPtr buildPathRewritePolicy(envoy::config::route::v3::Route route, + PathRewriterSharedPtr buildPathRewriter(envoy::config::route::v3::Route route, ProtobufMessage::ValidationVisitor& validator) const; RouteConstSharedPtr pickClusterViaClusterHeader(const Http::LowerCaseString& cluster_header_name, From 7d1c8d8e0c1bb268881987b074d2fd55b520d48d Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 9 Sep 2022 13:46:18 +0000 Subject: [PATCH 228/238] Change name Signed-off-by: silverstar195 --- test/per_file_coverage.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/per_file_coverage.sh b/test/per_file_coverage.sh index da38225bcdc15..6db4adfd00d75 100755 --- a/test/per_file_coverage.sh +++ b/test/per_file_coverage.sh @@ -72,7 +72,7 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/network/dns_resolver/getaddrinfo:96.3" "source/extensions/path/rewrite:88.1" "source/extensions/path/:96.1" -"source/extensions/path/rewrite/pattern_template:88.1" +"source/extensions/path/rewrite/uri_template:88.1" "source/extensions/rate_limit_descriptors:95.5" "source/extensions/rate_limit_descriptors/expr:95.5" "source/extensions/stat_sinks/common:96.4" From a28f4dd148bebba53c1f4e7e9d5654e0f628b746 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 9 Sep 2022 14:23:50 +0000 Subject: [PATCH 229/238] Format Signed-off-by: silverstar195 --- source/common/router/config_impl.cc | 7 +++---- source/common/router/config_impl.h | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/source/common/router/config_impl.cc b/source/common/router/config_impl.cc index 53a5c890c90e8..b41cf84b1643a 100644 --- a/source/common/router/config_impl.cc +++ b/source/common/router/config_impl.cc @@ -676,8 +676,7 @@ RouteEntryImplBase::RouteEntryImplBase(const VirtualHostImpl& vhost, } if (path_rewriter_ != nullptr) { - absl::Status compatible_status = - path_rewriter_->isCompatiblePathMatcher(path_matcher_); + absl::Status compatible_status = path_rewriter_->isCompatiblePathMatcher(path_matcher_); if (!compatible_status.ok()) { throw EnvoyException(std::string(compatible_status.message())); } @@ -1191,7 +1190,7 @@ InternalRedirectPolicyImpl RouteEntryImplBase::buildInternalRedirectPolicy( } PathRewriterSharedPtr RouteEntryImplBase::buildPathRewriter(envoy::config::route::v3::Route route, - ProtobufMessage::ValidationVisitor& validator) const { + ProtobufMessage::ValidationVisitor& validator) const { if (!route.route().has_path_rewrite_policy()) { return nullptr; } @@ -1213,7 +1212,7 @@ RouteEntryImplBase::buildPathRewriter(envoy::config::route::v3::Route route, PathMatcherSharedPtr RouteEntryImplBase::buildPathMatcher(envoy::config::route::v3::Route route, - ProtobufMessage::ValidationVisitor& validator) const { + ProtobufMessage::ValidationVisitor& validator) const { if (!route.match().has_path_match_policy()) { return nullptr; } diff --git a/source/common/router/config_impl.h b/source/common/router/config_impl.h index 4b22007fcfa20..78d2eb7084155 100644 --- a/source/common/router/config_impl.h +++ b/source/common/router/config_impl.h @@ -936,10 +936,10 @@ class RouteEntryImplBase : public RouteEntry, absl::string_view current_route_name) const; PathMatcherSharedPtr buildPathMatcher(envoy::config::route::v3::Route route, - ProtobufMessage::ValidationVisitor& validator) const; + ProtobufMessage::ValidationVisitor& validator) const; PathRewriterSharedPtr buildPathRewriter(envoy::config::route::v3::Route route, - ProtobufMessage::ValidationVisitor& validator) const; + ProtobufMessage::ValidationVisitor& validator) const; RouteConstSharedPtr pickClusterViaClusterHeader(const Http::LowerCaseString& cluster_header_name, const Http::HeaderMap& headers) const; From e90c05f73150c44899696217811170d17828a39e Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 9 Sep 2022 14:43:57 +0000 Subject: [PATCH 230/238] Format Signed-off-by: silverstar195 --- source/common/http/async_client_impl.cc | 2 +- source/common/http/async_client_impl.h | 6 ++---- test/mocks/router/mocks.h | 2 +- tools/spelling/spelling_dictionary.txt | 3 --- 4 files changed, 4 insertions(+), 9 deletions(-) diff --git a/source/common/http/async_client_impl.cc b/source/common/http/async_client_impl.cc index bff28f14c6f16..f2b42d77aa440 100644 --- a/source/common/http/async_client_impl.cc +++ b/source/common/http/async_client_impl.cc @@ -20,7 +20,7 @@ const std::vector> const AsyncStreamImpl::NullHedgePolicy AsyncStreamImpl::RouteEntryImpl::hedge_policy_; const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::RouteEntryImpl::rate_limit_policy_; const Router::InternalRedirectPolicyImpl AsyncStreamImpl::RouteEntryImpl::internal_redirect_policy_; -const Router::PathMatcherSharedPtr AsyncStreamImpl::RouteEntryImpl::extension_path_matcher_; +const Router::PathMatcherSharedPtr AsyncStreamImpl::RouteEntryImpl::path_matcher_; const Router::PathRewriterSharedPtr AsyncStreamImpl::RouteEntryImpl::path_rewriter_; const std::vector AsyncStreamImpl::RouteEntryImpl::shadow_policies_; const AsyncStreamImpl::NullVirtualHost AsyncStreamImpl::RouteEntryImpl::virtual_host_; diff --git a/source/common/http/async_client_impl.h b/source/common/http/async_client_impl.h index 3f4b18c6c2099..aaaffa4781cd0 100644 --- a/source/common/http/async_client_impl.h +++ b/source/common/http/async_client_impl.h @@ -231,9 +231,7 @@ class AsyncStreamImpl : public AsyncClient::Stream, const Router::InternalRedirectPolicy& internalRedirectPolicy() const override { return internal_redirect_policy_; } - const Router::PathMatcherSharedPtr& pathMatcher() const override { - return extension_path_matcher_; - } + const Router::PathMatcherSharedPtr& pathMatcher() const override { return path_matcher_; } const Router::PathRewriterSharedPtr& pathRewriter() const override { return path_rewriter_; } uint32_t retryShadowBufferLimit() const override { return std::numeric_limits::max(); @@ -298,7 +296,7 @@ class AsyncStreamImpl : public AsyncClient::Stream, static const NullHedgePolicy hedge_policy_; static const NullRateLimitPolicy rate_limit_policy_; static const Router::InternalRedirectPolicyImpl internal_redirect_policy_; - static const Router::PathMatcherSharedPtr extension_path_matcher_; + static const Router::PathMatcherSharedPtr path_matcher_; static const Router::PathRewriterSharedPtr path_rewriter_; static const std::vector shadow_policies_; static const NullVirtualHost virtual_host_; diff --git a/test/mocks/router/mocks.h b/test/mocks/router/mocks.h index 334f2c809dc27..ba3b848d11efc 100644 --- a/test/mocks/router/mocks.h +++ b/test/mocks/router/mocks.h @@ -435,7 +435,7 @@ class MockRouteEntry : public RouteEntry { TestVirtualCluster virtual_cluster_; TestRetryPolicy retry_policy_; testing::NiceMock internal_redirect_policy_; - testing::NiceMock extension_path_matcher_; + testing::NiceMock path_matcher_; testing::NiceMock path_rewriter_; TestHedgePolicy hedge_policy_; testing::NiceMock rate_limit_policy_; diff --git a/tools/spelling/spelling_dictionary.txt b/tools/spelling/spelling_dictionary.txt index eb6239ca322fd..f73bdc94ef382 100644 --- a/tools/spelling/spelling_dictionary.txt +++ b/tools/spelling/spelling_dictionary.txt @@ -230,11 +230,8 @@ LEDS LEV LF LHS -hls integrations -jkl js -lang hls jkl lang From 2abde45bf13c3fe8191a6e6058b1807d571e4d4d Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 9 Sep 2022 20:04:32 +0000 Subject: [PATCH 231/238] Changed dictionary Signed-off-by: silverstar195 --- tools/spelling/spelling_dictionary.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/spelling/spelling_dictionary.txt b/tools/spelling/spelling_dictionary.txt index f73bdc94ef382..4e5d4da653a7b 100644 --- a/tools/spelling/spelling_dictionary.txt +++ b/tools/spelling/spelling_dictionary.txt @@ -230,9 +230,8 @@ LEDS LEV LF LHS -integrations -js hls +integrations jkl lang libsxg From c7c0cf337f4824de8933181437a539536d20ee17 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 9 Sep 2022 21:30:12 +0000 Subject: [PATCH 232/238] Added tests Signed-off-by: silverstar195 --- .../rewrite/uri_template/uri_template_rewrite.cc | 14 +++----------- .../path/rewrite/uri_template/library_test.cc | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc index 8e0ab82a590d0..2967f74b7cd07 100644 --- a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc +++ b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc @@ -51,17 +51,12 @@ absl::StatusOr UriTemplateRewriter::rewritePath(absl::string_view p } std::string regex_pattern_str = *std::move(regex_pattern); - absl::StatusOr rewrite_pattern = - parseRewritePattern(rewrite_pattern_, regex_pattern_str); + // validated on construction of UriTemplateRewriter + RewriteSegments rewrite_pattern_segments = parseRewritePattern(rewrite_pattern_, regex_pattern_str).value(); - if (!rewrite_pattern.ok()) { - return absl::InvalidArgumentError("Unable to parse path rewrite pattern"); - } - - const RewriteSegments& rewrite_pattern_segments = *std::move(rewrite_pattern); RE2 regex = RE2(Internal::toStringPiece(regex_pattern_str)); if (!regex.ok()) { - return absl::InternalError(regex.error()); + return absl::InternalError("Regex library failed"); } // First capture is the whole matched regex pattern. @@ -79,9 +74,6 @@ absl::StatusOr UriTemplateRewriter::rewritePath(absl::string_view p if (literal != nullptr) { absl::StrAppend(&new_path, *literal); } else if (capture_index != nullptr) { - if (*capture_index < 1 || *capture_index >= capture_num) { - return absl::InvalidArgumentError("Invalid variable index"); - } absl::StrAppend(&new_path, absl::string_view(captures[*capture_index].as_string())); } } diff --git a/test/extensions/path/rewrite/uri_template/library_test.cc b/test/extensions/path/rewrite/uri_template/library_test.cc index 46535f9e817f1..f2802ab776546 100644 --- a/test/extensions/path/rewrite/uri_template/library_test.cc +++ b/test/extensions/path/rewrite/uri_template/library_test.cc @@ -71,6 +71,20 @@ TEST(RewriteTest, BasicUsage) { EXPECT_EQ(predicate->name(), "envoy.path.rewrite.uri_template.uri_template_rewriter"); } +TEST(RewriteTest, PatternNotMatched) { + const std::string yaml_string = R"EOF( + name: envoy.path.rewrite.uri_template.uri_template_rewriter + typed_config: + "@type": type.googleapis.com/envoy.extensions.path.rewrite.uri_template.v3.UriTemplateRewriteConfig + path_template_rewrite: "/bar/{lang}/{country}/{test}" +)EOF"; + + Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); + absl::StatusOr rewrite_or_error = predicate->rewritePath("/bar/en/usa", "/bar/{country}/{lang}/{test}"); + EXPECT_FALSE(rewrite_or_error.ok()); + EXPECT_EQ(rewrite_or_error.status().message(), "Pattern not match"); +} + TEST(RewriteTest, RewriteInvalidRegex) { const std::string yaml_string = R"EOF( name: envoy.path.rewrite.uri_template.uri_template_rewriter From 3d148606bc03c15ad4e9c0691c63f78247379fa9 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Fri, 9 Sep 2022 21:39:22 +0000 Subject: [PATCH 233/238] Format Signed-off-by: silverstar195 --- .../path/rewrite/uri_template/uri_template_rewrite.cc | 3 ++- test/extensions/path/rewrite/uri_template/library_test.cc | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc index 2967f74b7cd07..2e69b72e7b0fb 100644 --- a/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc +++ b/source/extensions/path/rewrite/uri_template/uri_template_rewrite.cc @@ -52,7 +52,8 @@ absl::StatusOr UriTemplateRewriter::rewritePath(absl::string_view p std::string regex_pattern_str = *std::move(regex_pattern); // validated on construction of UriTemplateRewriter - RewriteSegments rewrite_pattern_segments = parseRewritePattern(rewrite_pattern_, regex_pattern_str).value(); + RewriteSegments rewrite_pattern_segments = + parseRewritePattern(rewrite_pattern_, regex_pattern_str).value(); RE2 regex = RE2(Internal::toStringPiece(regex_pattern_str)); if (!regex.ok()) { diff --git a/test/extensions/path/rewrite/uri_template/library_test.cc b/test/extensions/path/rewrite/uri_template/library_test.cc index f2802ab776546..641738cc3fe94 100644 --- a/test/extensions/path/rewrite/uri_template/library_test.cc +++ b/test/extensions/path/rewrite/uri_template/library_test.cc @@ -80,9 +80,10 @@ TEST(RewriteTest, PatternNotMatched) { )EOF"; Router::PathRewriterSharedPtr predicate = createRewriterFromYaml(yaml_string); - absl::StatusOr rewrite_or_error = predicate->rewritePath("/bar/en/usa", "/bar/{country}/{lang}/{test}"); + absl::StatusOr rewrite_or_error = + predicate->rewritePath("/bar/en/usa", "/bar/{country}/{lang}/{test}"); EXPECT_FALSE(rewrite_or_error.ok()); - EXPECT_EQ(rewrite_or_error.status().message(), "Pattern not match"); + EXPECT_EQ(rewrite_or_error.status().message(), "Pattern not match"); } TEST(RewriteTest, RewriteInvalidRegex) { From 72501086ce26d0f79d438222cbbf94de620e7f14 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Mon, 12 Sep 2022 15:50:07 +0000 Subject: [PATCH 234/238] Removed reduced coverage Signed-off-by: silverstar195 --- test/per_file_coverage.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/per_file_coverage.sh b/test/per_file_coverage.sh index 6db4adfd00d75..27c060a409771 100755 --- a/test/per_file_coverage.sh +++ b/test/per_file_coverage.sh @@ -70,9 +70,6 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/io_socket:96.2" "source/extensions/io_socket/user_space:96.2" "source/extensions/network/dns_resolver/getaddrinfo:96.3" -"source/extensions/path/rewrite:88.1" -"source/extensions/path/:96.1" -"source/extensions/path/rewrite/uri_template:88.1" "source/extensions/rate_limit_descriptors:95.5" "source/extensions/rate_limit_descriptors/expr:95.5" "source/extensions/stat_sinks/common:96.4" From df35c908d5f47dd6a11166c8a9cdb2237f51e2ee Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 15 Sep 2022 15:40:55 +0000 Subject: [PATCH 235/238] Add fuzzing Signed-off-by: silverstar195 --- test/extensions/path/uri_template_lib/BUILD | 12 ++++++++++ .../uri_template_corpus/UriTemplate_Mixed.txt | 1 + .../UriTemplate_MixedCases.txt | 1 + .../UriTemplate_Simple.txt | 1 + .../UriTemplate_SimpleVariable.txt | 1 + .../UriTemplate_SingleForwardSlash.txt | 1 + .../UriTemplate_TooManyVariables.txt | 1 + .../uri_template_fuzz_test.cc | 23 +++++++++++++++++++ 8 files changed, 41 insertions(+) create mode 100644 test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_Mixed.txt create mode 100644 test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_MixedCases.txt create mode 100644 test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_Simple.txt create mode 100644 test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_SimpleVariable.txt create mode 100644 test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_SingleForwardSlash.txt create mode 100644 test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_TooManyVariables.txt create mode 100644 test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc diff --git a/test/extensions/path/uri_template_lib/BUILD b/test/extensions/path/uri_template_lib/BUILD index 87eda04edce71..d2e591357114e 100644 --- a/test/extensions/path/uri_template_lib/BUILD +++ b/test/extensions/path/uri_template_lib/BUILD @@ -1,5 +1,6 @@ load( "//bazel:envoy_build_system.bzl", + "envoy_cc_fuzz_test", "envoy_cc_test", "envoy_package", ) @@ -34,3 +35,14 @@ envoy_cc_test( "@com_googlesource_code_re2//:re2", ], ) + +envoy_cc_fuzz_test( + name = "uri_template_fuzz_test", + srcs = ["uri_template_fuzz_test.cc"], + corpus = "uri_template_corpus", + deps = [ + "//source/common/common:statusor_lib", + "//source/extensions/path/uri_template_lib", + "//test/fuzz:utility_lib", + ], +) diff --git a/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_Mixed.txt b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_Mixed.txt new file mode 100644 index 0000000000000..148f82a1546cb --- /dev/null +++ b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_Mixed.txt @@ -0,0 +1 @@ +/{one}/{two}/three/{four}/five/{max}/max_two \ No newline at end of file diff --git a/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_MixedCases.txt b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_MixedCases.txt new file mode 100644 index 0000000000000..3e3733aa9043a --- /dev/null +++ b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_MixedCases.txt @@ -0,0 +1 @@ +/{One}/{Two}/{threE}/{fOUR}/{fiVe}/{max}/{max_tWo} \ No newline at end of file diff --git a/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_Simple.txt b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_Simple.txt new file mode 100644 index 0000000000000..5cd553993e48b --- /dev/null +++ b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_Simple.txt @@ -0,0 +1 @@ +/simple/test/pattern \ No newline at end of file diff --git a/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_SimpleVariable.txt b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_SimpleVariable.txt new file mode 100644 index 0000000000000..4fec33bacee68 --- /dev/null +++ b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_SimpleVariable.txt @@ -0,0 +1 @@ +/simple/test/pattern/{var} \ No newline at end of file diff --git a/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_SingleForwardSlash.txt b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_SingleForwardSlash.txt new file mode 100644 index 0000000000000..35ec3b9d7586b --- /dev/null +++ b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_SingleForwardSlash.txt @@ -0,0 +1 @@ +/ \ No newline at end of file diff --git a/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_TooManyVariables.txt b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_TooManyVariables.txt new file mode 100644 index 0000000000000..074b6ce2dcf9b --- /dev/null +++ b/test/extensions/path/uri_template_lib/uri_template_corpus/UriTemplate_TooManyVariables.txt @@ -0,0 +1 @@ +/{one}/{two}/{three}/{four}/{five}/{max}/{max_two} \ No newline at end of file diff --git a/test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc b/test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc new file mode 100644 index 0000000000000..7d4e536fa2e16 --- /dev/null +++ b/test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc @@ -0,0 +1,23 @@ +#include "source/common/common/statusor.h" +#include "source/extensions/path/uri_template_lib/uri_template.h" + +#include "test/fuzz/fuzz_runner.h" +#include "test/fuzz/utility.h" + +#include "absl/strings/string_view.h" + +namespace Envoy { +namespace Fuzz { + +DEFINE_FUZZER(const uint8_t* buf, size_t len) { + absl::string_view input(reinterpret_cast(buf), len); + + // test rewriter parser + absl::StatusOr> rewrite = Envoy::Extensions::UriTemplate::parseRewritePattern(input); + + // test matcher parser + absl::StatusOr match = Envoy::Extensions::UriTemplate::convertPathPatternSyntaxToRegex(input); +} + +} // namespace Fuzz +} // namespace Envoy From 97f366675106cdadc6fd1ea88f3d552567f49046 Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 15 Sep 2022 16:01:18 +0000 Subject: [PATCH 236/238] Tests Signed-off-by: silverstar195 --- .../path/uri_template_lib/uri_template_fuzz_test.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc b/test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc index 7d4e536fa2e16..af12a935f50a5 100644 --- a/test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc @@ -13,10 +13,12 @@ DEFINE_FUZZER(const uint8_t* buf, size_t len) { absl::string_view input(reinterpret_cast(buf), len); // test rewriter parser - absl::StatusOr> rewrite = Envoy::Extensions::UriTemplate::parseRewritePattern(input); + absl::StatusOr> rewrite = + Envoy::Extensions::UriTemplate::parseRewritePattern(input); // test matcher parser - absl::StatusOr match = Envoy::Extensions::UriTemplate::convertPathPatternSyntaxToRegex(input); + absl::StatusOr match = + Envoy::Extensions::UriTemplate::convertPathPatternSyntaxToRegex(input); } } // namespace Fuzz From 34504f28adf642cd445d5bd274e2a91460c0677b Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 15 Sep 2022 18:22:09 +0000 Subject: [PATCH 237/238] Tests Signed-off-by: silverstar195 --- test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc b/test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc index af12a935f50a5..3392248a48e24 100644 --- a/test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc +++ b/test/extensions/path/uri_template_lib/uri_template_fuzz_test.cc @@ -17,7 +17,7 @@ DEFINE_FUZZER(const uint8_t* buf, size_t len) { Envoy::Extensions::UriTemplate::parseRewritePattern(input); // test matcher parser - absl::StatusOr match = + absl::StatusOr match = Envoy::Extensions::UriTemplate::convertPathPatternSyntaxToRegex(input); } From 51873d8885d88499b05a5408db6746d463978ffd Mon Sep 17 00:00:00 2001 From: silverstar195 Date: Thu, 15 Sep 2022 18:43:13 +0000 Subject: [PATCH 238/238] Change security_posture Signed-off-by: silverstar195 --- source/extensions/extensions_metadata.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/extensions/extensions_metadata.yaml b/source/extensions/extensions_metadata.yaml index 7713ba4e3da2b..e066c6f717125 100644 --- a/source/extensions/extensions_metadata.yaml +++ b/source/extensions/extensions_metadata.yaml @@ -757,7 +757,7 @@ envoy.matching.matchers.ip: envoy.path.match.uri_template.uri_template_matcher: categories: - envoy.path.match - security_posture: unknown + security_posture: robust_to_untrusted_downstream_and_upstream status: stable undocumented: true type_urls: @@ -765,7 +765,7 @@ envoy.path.match.uri_template.uri_template_matcher: envoy.path.rewrite.uri_template.uri_template_rewriter: categories: - envoy.path.rewrite - security_posture: unknown + security_posture: robust_to_untrusted_downstream_and_upstream status: stable undocumented: true type_urls: