Line data Source code
1 : #include "source/extensions/matching/input_matchers/cel_matcher/matcher.h" 2 : 3 : namespace Envoy { 4 : namespace Extensions { 5 : namespace Matching { 6 : namespace InputMatchers { 7 : namespace CelMatcher { 8 : 9 : using ::Envoy::Extensions::Matching::Http::CelInput::CelMatchData; 10 : using ::xds::type::v3::CelExpression; 11 : 12 : CelInputMatcher::CelInputMatcher(CelMatcherSharedPtr cel_matcher, 13 : Filters::Common::Expr::BuilderInstanceSharedPtr builder) 14 0 : : builder_(builder), cel_matcher_(std::move(cel_matcher)) { 15 0 : const CelExpression& input_expr = cel_matcher_->expr_match(); 16 0 : switch (input_expr.expr_specifier_case()) { 17 0 : case CelExpression::ExprSpecifierCase::kParsedExpr: 18 0 : compiled_expr_ = Filters::Common::Expr::createExpression(builder_->builder(), 19 0 : input_expr.parsed_expr().expr()); 20 0 : return; 21 0 : case CelExpression::ExprSpecifierCase::kCheckedExpr: 22 0 : compiled_expr_ = Filters::Common::Expr::createExpression(builder_->builder(), 23 0 : input_expr.checked_expr().expr()); 24 0 : return; 25 0 : case CelExpression::ExprSpecifierCase::EXPR_SPECIFIER_NOT_SET: 26 0 : PANIC_DUE_TO_PROTO_UNSET; 27 0 : } 28 0 : PANIC_DUE_TO_CORRUPT_ENUM; 29 0 : } 30 : 31 0 : bool CelInputMatcher::match(const MatchingDataType& input) { 32 0 : Protobuf::Arena arena; 33 0 : if (auto* ptr = absl::get_if<std::shared_ptr<::Envoy::Matcher::CustomMatchData>>(&input); 34 0 : ptr != nullptr) { 35 0 : CelMatchData* cel_data = dynamic_cast<CelMatchData*>((*ptr).get()); 36 : // Compiled expression should not be nullptr at this point because the program should have 37 : // encountered a panic in the constructor earlier if any such error cases occurred. CEL matching 38 : // data should also not be nullptr since any errors should have been thrown by the CEL library 39 : // already. 40 0 : ASSERT(compiled_expr_ != nullptr && cel_data != nullptr); 41 : 42 0 : auto eval_result = compiled_expr_->Evaluate(*cel_data->activation_, &arena); 43 0 : if (eval_result.ok() && eval_result.value().IsBool()) { 44 0 : return eval_result.value().BoolOrDie(); 45 0 : } 46 0 : } 47 : 48 0 : return false; 49 0 : } 50 : 51 : } // namespace CelMatcher 52 : } // namespace InputMatchers 53 : } // namespace Matching 54 : } // namespace Extensions 55 : } // namespace Envoy