Skip to content

Commit 3f4f210

Browse files
Handle the conditions error at KuadrantFilter level
Signed-off-by: Adam Cattermole <[email protected]>
1 parent 81cdb73 commit 3f4f210

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed

src/data/cel.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ pub struct Predicate {
302302
expression: Expression,
303303
}
304304

305+
pub type PredicateResult = Result<bool, EvaluationError>;
306+
305307
impl Predicate {
306308
pub fn new(predicate: &str) -> Result<Self, ParseError> {
307309
Ok(Self {
@@ -318,7 +320,7 @@ impl Predicate {
318320
})
319321
}
320322

321-
pub fn test(&self) -> Result<bool, EvaluationError> {
323+
pub fn test(&self) -> PredicateResult {
322324
match self.expression.eval() {
323325
Ok(value) => match value {
324326
Value::Bool(result) => Ok(result),
@@ -336,11 +338,11 @@ impl Predicate {
336338
}
337339

338340
pub trait PredicateVec {
339-
fn apply(&self) -> Result<bool, EvaluationError>;
341+
fn apply(&self) -> PredicateResult;
340342
}
341343

342344
impl PredicateVec for Vec<Predicate> {
343-
fn apply(&self) -> Result<bool, EvaluationError> {
345+
fn apply(&self) -> PredicateResult {
344346
if self.is_empty() {
345347
return Ok(true);
346348
}

src/data/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub use cel::debug_all_well_known_attributes;
1010

1111
pub use cel::Expression;
1212
pub use cel::Predicate;
13+
pub use cel::PredicateResult;
1314
pub use cel::PredicateVec;
1415

1516
pub use attribute::errors::PropertyError;

src/filter/kuadrant_filter.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::filter::operations::{
44
};
55
use crate::runtime_action_set::RuntimeActionSet;
66
use crate::service::{GrpcErrResponse, GrpcRequest, HeaderResolver, Headers};
7-
use log::{debug, warn};
7+
use log::{debug, error, warn};
88
use proxy_wasm::traits::{Context, HttpContext};
99
use proxy_wasm::types::{Action, Status};
1010
use std::mem;
@@ -59,15 +59,26 @@ impl HttpContext for KuadrantFilter {
5959
.index
6060
.get_longest_match_action_sets(self.request_authority().as_ref())
6161
{
62-
if let Some(action_set) = action_sets
63-
.iter()
64-
.find(|action_set| action_set.conditions_apply(/* self */))
65-
{
66-
debug!(
67-
"#{} action_set selected {}",
68-
self.context_id, action_set.name
69-
);
70-
action = self.start_flow(Rc::clone(action_set))
62+
for action_set in action_sets {
63+
match action_set.conditions_apply() {
64+
Ok(true) => {
65+
debug!(
66+
"#{} action_set selected {}",
67+
self.context_id, action_set.name
68+
);
69+
action = self.start_flow(Rc::clone(action_set));
70+
break;
71+
}
72+
Ok(false) => continue,
73+
Err(e) => {
74+
error!(
75+
"#{} on_http_request_headers: failed to apply conditions: {:?}",
76+
self.context_id, e
77+
);
78+
self.die(GrpcErrResponse::new_internal_server_error());
79+
break;
80+
}
81+
}
7182
}
7283
}
7384

src/runtime_action_set.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::configuration::{ActionSet, Service};
2-
use crate::data::{Predicate, PredicateVec};
2+
use crate::data::{Predicate, PredicateResult, PredicateVec};
33
use crate::runtime_action::RuntimeAction;
44
use crate::service::{GrpcErrResponse, HeaderKind, IndexedGrpcRequest};
55
use std::collections::HashMap;
@@ -56,9 +56,8 @@ impl RuntimeActionSet {
5656
folded_actions
5757
}
5858

59-
pub fn conditions_apply(&self) -> bool {
60-
//todo(adam-cattermole): do not expect
61-
self.route_rule_predicates.apply().expect("REMOVE")
59+
pub fn conditions_apply(&self) -> PredicateResult {
60+
self.route_rule_predicates.apply()
6261
}
6362

6463
pub fn find_first_grpc_request(&self) -> Option<IndexedGrpcRequest> {
@@ -105,7 +104,8 @@ mod test {
105104
let runtime_action_set = RuntimeActionSet::new(&action_set, &HashMap::default())
106105
.expect("should not happen from an empty set of actions");
107106

108-
assert!(runtime_action_set.conditions_apply())
107+
assert!(runtime_action_set.conditions_apply().is_ok());
108+
assert!(runtime_action_set.conditions_apply().unwrap());
109109
}
110110

111111
#[test]
@@ -122,7 +122,8 @@ mod test {
122122
let runtime_action_set = RuntimeActionSet::new(&action_set, &HashMap::default())
123123
.expect("should not happen from an empty set of actions");
124124

125-
assert!(runtime_action_set.conditions_apply())
125+
assert!(runtime_action_set.conditions_apply().is_ok());
126+
assert!(runtime_action_set.conditions_apply().unwrap());
126127
}
127128

128129
#[test]
@@ -139,12 +140,12 @@ mod test {
139140
let runtime_action_set = RuntimeActionSet::new(&action_set, &HashMap::default())
140141
.expect("should not happen from an empty set of actions");
141142

142-
assert!(!runtime_action_set.conditions_apply())
143+
assert!(runtime_action_set.conditions_apply().is_ok());
144+
assert!(!runtime_action_set.conditions_apply().unwrap());
143145
}
144146

145147
#[test]
146-
#[should_panic]
147-
fn when_a_cel_expression_does_not_evaluate_to_bool_panics() {
148+
fn when_a_cel_expression_does_not_evaluate_to_bool_returns_error() {
148149
let action_set = ActionSet::new(
149150
"some_name".to_owned(),
150151
RouteRuleConditions {
@@ -156,7 +157,7 @@ mod test {
156157

157158
let runtime_action_set = RuntimeActionSet::new(&action_set, &HashMap::default())
158159
.expect("should not happen from an empty set of actions");
159-
runtime_action_set.conditions_apply();
160+
assert!(runtime_action_set.conditions_apply().is_err());
160161
}
161162

162163
fn build_rl_service() -> Service {

0 commit comments

Comments
 (0)