1
#include "source/extensions/matching/input_matchers/cel_matcher/matcher.h"
2

            
3
#include "envoy/matcher/matcher.h"
4

            
5
namespace Envoy {
6
namespace Extensions {
7
namespace Matching {
8
namespace InputMatchers {
9
namespace CelMatcher {
10

            
11
using ::Envoy::Extensions::Matching::Http::CelInput::CelMatchData;
12
using ::xds::type::v3::CelExpression;
13

            
14
CelInputMatcher::CelInputMatcher(CelMatcherSharedPtr cel_matcher,
15
                                 Filters::Common::Expr::BuilderInstanceSharedConstPtr builder)
16
115
    : compiled_expr_([&]() {
17
115
        auto compiled_expr =
18
115
            Filters::Common::Expr::CompiledExpression::Create(builder, cel_matcher->expr_match());
19
115
        if (!compiled_expr.ok()) {
20
1
          throw EnvoyException(
21
1
              absl::StrCat("failed to create an expression: ", compiled_expr.status().message()));
22
1
        }
23
114
        return std::move(compiled_expr.value());
24
115
      }()) {}
25

            
26
107
Matcher::MatchResult CelInputMatcher::match(const DataInputGetResult& input) {
27
107
  Protobuf::Arena arena;
28
107
  if (auto cel_data = input.customData<CelMatchData>(); cel_data) {
29
107
    auto eval_result = compiled_expr_.evaluate(*cel_data->activation_, &arena);
30
107
    if (eval_result.ok() && eval_result.value().IsBool()) {
31
101
      if (eval_result.value().BoolOrDie()) {
32
70
        return Matcher::MatchResult::Matched;
33
70
      }
34
101
    }
35
37
    if (Runtime::runtimeFeatureEnabled(
36
37
            "envoy.reloadable_features.enable_cel_response_path_matching") &&
37
37
        cel_data->needs_response() && !cel_data->has_response_data()) {
38
3
      return Matcher::MatchResult::InsufficientData;
39
3
    }
40
37
  }
41
34
  return Matcher::MatchResult::NoMatch;
42
107
}
43

            
44
} // namespace CelMatcher
45
} // namespace InputMatchers
46
} // namespace Matching
47
} // namespace Extensions
48
} // namespace Envoy