-
Notifications
You must be signed in to change notification settings - Fork 5.3k
accesslogs: add CEL-based extension filter #18363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
dc9165e
accesslogs: add CEL-based extension filter
douglas-reid c765a0e
add newlines at ends of file
douglas-reid bb77029
add extension
douglas-reid 97e0024
fix presubmits
douglas-reid 69b1cf4
fixup docs and deps
douglas-reid 7ef6a74
last dep issue
douglas-reid df35c8d
newlines for all
douglas-reid 96d010c
address windows build and test coverage
douglas-reid d31d546
add forgotten ifdef
douglas-reid dae58df
fixup
douglas-reid 5dd4d10
fix bad merge
douglas-reid 4dc7ccf
fix windows
douglas-reid c4a773a
fix version history bad merge
douglas-reid 9de322a
add more complex API example
douglas-reid 727a6fc
address review comments
douglas-reid f256832
fix ordering of features in current.rst
douglas-reid db71f55
Add todo for validation visitor
douglas-reid 47a04c1
Use proper extension category
douglas-reid 4f09ade
better todo
douglas-reid a7d113f
Merge branch 'main' of github.com:envoyproxy/envoy into cel-log-filter
douglas-reid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"], | ||
| ) |
26 changes: 26 additions & 0 deletions
26
api/envoy/extensions/access_loggers/filters/cel/v3/cel.proto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package envoy.extensions.access_loggers.filters.cel.v3; | ||
|
|
||
| import "udpa/annotations/status.proto"; | ||
|
|
||
| option java_package = "io.envoyproxy.envoy.extensions.access_loggers.filters.cel.v3"; | ||
| option java_outer_classname = "CelProto"; | ||
| option java_multiple_files = true; | ||
| option (udpa.annotations.file_status).package_version_status = ACTIVE; | ||
|
|
||
| // [#protodoc-title: ExpressionFilter] | ||
| // [#extension: envoy.access_loggers.extension_filters.cel] | ||
|
|
||
| // ExpressionFilter is an access logging filter that evaluates configured | ||
| // symbolic Common Expression Language expressions to inform the decision | ||
| // to generate an access log. | ||
| message ExpressionFilter { | ||
| // Expression that, when evaluated, will be used to filter access logs. | ||
| // Expressions are based on the set of Envoy :ref:`attributes <arch_overview_attributes>`. | ||
| // The provided expression must evaluate to true for logging (expression errors are considered false). | ||
| // Examples: | ||
| // - `response.code >= 400` | ||
| // - `(connection.mtls && request.headers['x-log-mtls'] == 'true') || request.url_path.contains('v1beta3')` | ||
| string expression = 1; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,4 @@ Access loggers | |
|
|
||
| v3/* | ||
| ../../extensions/access_loggers/*/v3/* | ||
| ../../extensions/access_loggers/filters/*/v3/* | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| Extension Filters | ||
| ================= | ||
|
|
||
| .. toctree:: | ||
| :glob: | ||
| :maxdepth: 2 | ||
|
|
||
| filters/filters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| Extension Filters | ||
| ================= | ||
|
|
||
| .. toctree:: | ||
| :glob: | ||
| :maxdepth: 2 | ||
|
|
||
| ../../../extensions/access_loggers/filters/*/v3/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| load( | ||
| "//bazel:envoy_build_system.bzl", | ||
| "envoy_cc_extension", | ||
| "envoy_extension_package", | ||
| ) | ||
|
|
||
| licenses(["notice"]) # Apache 2 | ||
|
|
||
| envoy_extension_package() | ||
|
|
||
| envoy_cc_extension( | ||
| name = "cel_lib", | ||
| srcs = ["cel.cc"], | ||
| hdrs = ["cel.h"], | ||
| extra_visibility = [ | ||
| "//test:__subpackages__", | ||
| ], | ||
| deps = [ | ||
| "//envoy/access_log:access_log_interface", | ||
| "//envoy/http:header_map_interface", | ||
| "//envoy/stream_info:stream_info_interface", | ||
| "//source/common/access_log:access_log_lib", | ||
| "//source/common/config:utility_lib", | ||
| "//source/common/protobuf", | ||
| "//source/common/protobuf:utility_lib", | ||
| "//source/extensions/filters/common/expr:evaluator_lib", | ||
| ], | ||
| ) | ||
|
|
||
| envoy_cc_extension( | ||
| name = "config", | ||
| srcs = ["config.cc"], | ||
| hdrs = ["config.h"], | ||
| copts = select({ | ||
| "//bazel:windows_x86_64": [], # TODO: fix the windows ANTLR build | ||
| "//conditions:default": [ | ||
| "-DUSE_CEL_PARSER", | ||
| ], | ||
| }), | ||
| extra_visibility = [ | ||
| "//test:__subpackages__", | ||
| ], | ||
| deps = [ | ||
| ":cel_lib", | ||
| "//envoy/access_log:access_log_interface", | ||
| "//envoy/http:header_map_interface", | ||
| "//envoy/registry", | ||
| "//envoy/stream_info:stream_info_interface", | ||
| "//source/common/access_log:access_log_lib", | ||
| "//source/common/config:utility_lib", | ||
| "//source/common/protobuf", | ||
| "//source/common/protobuf:utility_lib", | ||
| "//source/extensions/filters/common/expr:evaluator_lib", | ||
| "@envoy_api//envoy/extensions/access_loggers/filters/cel/v3:pkg_cc_proto", | ||
| ] + select( | ||
| { | ||
| "//bazel:windows_x86_64": [], | ||
| "//conditions:default": [ | ||
| "@com_google_cel_cpp//parser", | ||
| ], | ||
| }, | ||
| ), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #include "source/extensions/access_loggers/filters/cel/cel.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace AccessLoggers { | ||
| namespace Filters { | ||
| namespace CEL { | ||
|
|
||
| namespace Expr = Envoy::Extensions::Filters::Common::Expr; | ||
|
|
||
| CELAccessLogExtensionFilter::CELAccessLogExtensionFilter( | ||
| Expr::Builder& builder, const google::api::expr::v1alpha1::Expr& input_expr) | ||
| : parsed_expr_(input_expr) { | ||
| compiled_expr_ = Expr::createExpression(builder, parsed_expr_); | ||
| } | ||
|
|
||
| bool CELAccessLogExtensionFilter::evaluate( | ||
| const StreamInfo::StreamInfo& stream_info, const Http::RequestHeaderMap& request_headers, | ||
| const Http::ResponseHeaderMap& response_headers, | ||
| const Http::ResponseTrailerMap& response_trailers) const { | ||
| Protobuf::Arena arena; | ||
dio marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| auto eval_status = Expr::evaluate(*compiled_expr_, arena, stream_info, &request_headers, | ||
| &response_headers, &response_trailers); | ||
| if (!eval_status.has_value() || eval_status.value().IsError()) { | ||
| return false; | ||
| } | ||
| auto result = eval_status.value(); | ||
| return result.IsBool() ? result.BoolOrDie() : false; | ||
| } | ||
|
|
||
| } // namespace CEL | ||
| } // namespace Filters | ||
| } // namespace AccessLoggers | ||
| } // namespace Extensions | ||
| } // namespace Envoy | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #include "envoy/access_log/access_log.h" | ||
| #include "envoy/http/header_map.h" | ||
| #include "envoy/stream_info/stream_info.h" | ||
|
|
||
| #include "source/common/access_log/access_log_impl.h" | ||
| #include "source/common/config/utility.h" | ||
| #include "source/common/protobuf/message_validator_impl.h" | ||
| #include "source/common/protobuf/protobuf.h" | ||
| #include "source/common/protobuf/utility.h" | ||
| #include "source/extensions/filters/common/expr/evaluator.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace AccessLoggers { | ||
| namespace Filters { | ||
| namespace CEL { | ||
|
|
||
| class CELAccessLogExtensionFilter : public AccessLog::Filter { | ||
| public: | ||
| CELAccessLogExtensionFilter(Extensions::Filters::Common::Expr::Builder&, | ||
| const google::api::expr::v1alpha1::Expr&); | ||
|
|
||
| bool evaluate(const StreamInfo::StreamInfo& info, const Http::RequestHeaderMap& request_headers, | ||
| const Http::ResponseHeaderMap& response_headers, | ||
| const Http::ResponseTrailerMap& response_trailers) const override; | ||
|
|
||
| private: | ||
| const google::api::expr::v1alpha1::Expr parsed_expr_; | ||
| Extensions::Filters::Common::Expr::ExpressionPtr compiled_expr_; | ||
| }; | ||
|
|
||
| } // namespace CEL | ||
| } // namespace Filters | ||
| } // namespace AccessLoggers | ||
| } // namespace Extensions | ||
| } // namespace Envoy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #include "source/extensions/access_loggers/filters/cel/config.h" | ||
|
|
||
| #include "envoy/extensions/access_loggers/filters/cel/v3/cel.pb.h" | ||
|
|
||
| #include "source/extensions/access_loggers/filters/cel/cel.h" | ||
|
|
||
| #if defined(USE_CEL_PARSER) | ||
| #include "parser/parser.h" | ||
| #endif | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace AccessLoggers { | ||
| namespace Filters { | ||
| namespace CEL { | ||
|
|
||
| Envoy::AccessLog::FilterPtr CELAccessLogExtensionFilterFactory::createFilter( | ||
| const envoy::config::accesslog::v3::ExtensionFilter& config, Runtime::Loader&, | ||
| Random::RandomGenerator&) { | ||
|
|
||
| // TODO(douglas-reid): use factory_context validation. likely needs update to | ||
| // createFilter signature to pass in validation visitor. | ||
| auto factory_config = Config::Utility::translateToFactoryConfig( | ||
| config, Envoy::ProtobufMessage::getNullValidationVisitor(), *this); | ||
douglas-reid marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #if defined(USE_CEL_PARSER) | ||
| envoy::extensions::access_loggers::filters::cel::v3::ExpressionFilter cel_config = | ||
| *dynamic_cast<const envoy::extensions::access_loggers::filters::cel::v3::ExpressionFilter*>( | ||
| factory_config.get()); | ||
|
|
||
| auto parse_status = google::api::expr::parser::Parse(cel_config.expression()); | ||
| if (!parse_status.ok()) { | ||
| throw EnvoyException("Not able to parse filter expression: " + | ||
| parse_status.status().ToString()); | ||
| } | ||
|
|
||
| return std::make_unique<CELAccessLogExtensionFilter>(getOrCreateBuilder(), | ||
| parse_status.value().expr()); | ||
| #else | ||
| throw EnvoyException("CEL is not available for use in this environment."); | ||
| #endif | ||
| } | ||
|
|
||
| ProtobufTypes::MessagePtr CELAccessLogExtensionFilterFactory::createEmptyConfigProto() { | ||
| return std::make_unique<envoy::extensions::access_loggers::filters::cel::v3::ExpressionFilter>(); | ||
| } | ||
|
|
||
| Extensions::Filters::Common::Expr::Builder& | ||
| CELAccessLogExtensionFilterFactory::getOrCreateBuilder() { | ||
| if (expr_builder_ == nullptr) { | ||
| expr_builder_ = Extensions::Filters::Common::Expr::createBuilder(nullptr); | ||
| } | ||
| return *expr_builder_; | ||
| } | ||
|
|
||
| /** | ||
| * Static registration for the CELAccessLogExtensionFilter. @see RegisterFactory. | ||
| */ | ||
| REGISTER_FACTORY(CELAccessLogExtensionFilterFactory, Envoy::AccessLog::ExtensionFilterFactory); | ||
|
|
||
| } // namespace CEL | ||
| } // namespace Filters | ||
| } // namespace AccessLoggers | ||
| } // namespace Extensions | ||
| } // namespace Envoy | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #include "envoy/access_log/access_log.h" | ||
| #include "envoy/http/header_map.h" | ||
| #include "envoy/registry/registry.h" | ||
| #include "envoy/stream_info/stream_info.h" | ||
|
|
||
| #include "source/common/access_log/access_log_impl.h" | ||
| #include "source/common/config/utility.h" | ||
| #include "source/common/protobuf/message_validator_impl.h" | ||
| #include "source/common/protobuf/protobuf.h" | ||
| #include "source/common/protobuf/utility.h" | ||
| #include "source/extensions/filters/common/expr/evaluator.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace AccessLoggers { | ||
| namespace Filters { | ||
| namespace CEL { | ||
|
|
||
| class CELAccessLogExtensionFilterFactory : public Envoy::AccessLog::ExtensionFilterFactory { | ||
| public: | ||
| Envoy::AccessLog::FilterPtr | ||
| createFilter(const envoy::config::accesslog::v3::ExtensionFilter& config, Runtime::Loader&, | ||
| Random::RandomGenerator&) override; | ||
| ProtobufTypes::MessagePtr createEmptyConfigProto() override; | ||
| std::string name() const override { return "envoy.access_loggers.extension_filters.cel"; } | ||
|
|
||
| private: | ||
| Extensions::Filters::Common::Expr::Builder& getOrCreateBuilder(); | ||
| Extensions::Filters::Common::Expr::BuilderPtr expr_builder_; | ||
| }; | ||
|
|
||
| } // namespace CEL | ||
| } // namespace Filters | ||
| } // namespace AccessLoggers | ||
| } // namespace Extensions | ||
| } // namespace Envoy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.