Skip to content

Commit

Permalink
cel: support extension regex functions (#38401)
Browse files Browse the repository at this point in the history
Signed-off-by: zirain <[email protected]>
  • Loading branch information
zirain authored Feb 15, 2025
1 parent eb185a1 commit 7cffa39
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelogs/current.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ minor_behavior_changes:
change: |
Ignore request_header_mode field of :ref:`mode_override <envoy_v3_api_field_service.ext_proc.v3.ProcessingResponse.mode_override>`
when comparing the mode_override against allowed_override_modes as request_header mode override is not applicable.
- area: cel
change: |
Support extension regex fuctions(e.g. ``re.extract``, ``re.capture`, ``re.captureN``) in CEL.
bug_fixes:
# *Changes expected to improve the state of the world and are unlikely to have negative effects*
Expand Down
1 change: 1 addition & 0 deletions source/extensions/filters/common/expr/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ envoy_cc_library(
"@com_google_cel_cpp//eval/public:cel_expr_builder_factory",
"@com_google_cel_cpp//eval/public:cel_expression",
"@com_google_cel_cpp//eval/public:cel_value",
"@com_google_cel_cpp//extensions:regex_functions",
],
)

Expand Down
9 changes: 9 additions & 0 deletions source/extensions/filters/common/expr/evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "envoy/common/exception.h"
#include "envoy/singleton/manager.h"

#include "extensions/regex_functions.h"

#include "eval/public/builtin_func_registrar.h"
#include "eval/public/cel_expr_builder_factory.h"

Expand Down Expand Up @@ -104,6 +106,7 @@ BuilderPtr createBuilder(Protobuf::Arena* arena) {
options.enable_comprehension = false;
options.enable_regex = true;
options.regex_max_program_size = 100;
options.enable_qualified_identifier_rewrites = true;
options.enable_string_conversion = false;
options.enable_string_concat = false;
options.enable_list_concat = false;
Expand All @@ -121,6 +124,12 @@ BuilderPtr createBuilder(Protobuf::Arena* arena) {
throw CelException(
absl::StrCat("failed to register built-in functions: ", register_status.message()));
}
auto ext_register_status =
cel::extensions::RegisterRegexFunctions(builder->GetRegistry(), options);
if (!ext_register_status.ok()) {
throw CelException(absl::StrCat("failed to register extension regex functions: ",
ext_register_status.message()));
}
return builder;
}

Expand Down
16 changes: 16 additions & 0 deletions test/extensions/formatter/cel/cel_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ TEST_F(CELFormatterTest, TestInvalidExpression) {
*Envoy::Formatter::SubstitutionFormatStringUtils::fromProtoConfig(config_, context_),
EnvoyException, "Not able to parse filter expression: .*");
}

TEST_F(CELFormatterTest, TestRegexExtFunctions) {
const std::string yaml = R"EOF(
text_format_source:
inline_string: "%CEL(request.url_path.contains('request'))% %CEL(re.extract('', '', ''))%"
formatters:
- name: envoy.formatter.cel
typed_config:
"@type": type.googleapis.com/envoy.extensions.formatter.cel.v3.Cel
)EOF";
TestUtility::loadFromYaml(yaml, config_);

auto formatter =
*Envoy::Formatter::SubstitutionFormatStringUtils::fromProtoConfig(config_, context_);
EXPECT_EQ("true ", formatter->formatWithContext(formatter_context_, stream_info_));
}
#endif

} // namespace Formatter
Expand Down

0 comments on commit 7cffa39

Please sign in to comment.