-
Notifications
You must be signed in to change notification settings - Fork 5
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
Err out better when failing to eval CEL #135
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ use crate::configuration::{DataItem, DataType, PatternExpression}; | |
use crate::data::Predicate; | ||
use crate::envoy::{RateLimitDescriptor, RateLimitDescriptor_Entry}; | ||
use cel_interpreter::Value; | ||
use log::debug; | ||
use log::{debug, error}; | ||
use protobuf::RepeatedField; | ||
use serde::Deserialize; | ||
use std::cell::OnceCell; | ||
|
@@ -31,7 +31,16 @@ impl Action { | |
if predicates.is_empty() { | ||
self.conditions.is_empty() || self.conditions.iter().all(PatternExpression::applies) | ||
} else { | ||
predicates.iter().all(Predicate::test) | ||
predicates | ||
.iter() | ||
.enumerate() | ||
.all(|(pos, predicate)| match predicate.test() { | ||
Ok(b) => b, | ||
Err(err) => { | ||
error!("Failed to evaluate {}: {}", self.predicates[pos], err); | ||
panic!("Err out of this!") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
@@ -60,14 +69,20 @@ impl Action { | |
.expect("Expression must be compiled by now") | ||
.eval() | ||
{ | ||
Value::Int(n) => format!("{n}"), | ||
Value::UInt(n) => format!("{n}"), | ||
Value::Float(n) => format!("{n}"), | ||
// todo this probably should be a proper string literal! | ||
Value::String(s) => (*s).clone(), | ||
Value::Bool(b) => format!("{b}"), | ||
Value::Null => "null".to_owned(), | ||
_ => panic!("Only scalar values can be sent as data"), | ||
Ok(value) => match value { | ||
Value::Int(n) => format!("{n}"), | ||
Value::UInt(n) => format!("{n}"), | ||
Value::Float(n) => format!("{n}"), | ||
// todo this probably should be a proper string literal! | ||
Value::String(s) => (*s).clone(), | ||
Value::Bool(b) => format!("{b}"), | ||
Value::Null => "null".to_owned(), | ||
_ => panic!("Only scalar values can be sent as data"), | ||
}, | ||
Err(err) => { | ||
error!("Failed to evaluate {}: {}", cel.value, err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... and this ... |
||
panic!("Err out of this!") | ||
} | ||
}, | ||
), | ||
DataType::Selector(selector_item) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use crate::configuration::action::Action; | ||
use crate::configuration::PatternExpression; | ||
use crate::data::Predicate; | ||
use log::error; | ||
use serde::Deserialize; | ||
use std::cell::OnceCell; | ||
|
||
|
@@ -51,7 +52,19 @@ impl ActionSet { | |
.iter() | ||
.all(|m| m.applies()) | ||
} else { | ||
predicates.iter().all(Predicate::test) | ||
predicates | ||
.iter() | ||
.enumerate() | ||
.all(|(pos, predicate)| match predicate.test() { | ||
Ok(b) => b, | ||
Err(err) => { | ||
error!( | ||
"Failed to evaluate {}: {}", | ||
self.route_rule_conditions.predicates[pos], err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... and this. |
||
); | ||
panic!("Err out of this!") | ||
} | ||
}) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,7 @@ impl Expression { | |
Self::new_expression(expression, true) | ||
} | ||
|
||
pub fn eval(&self) -> Value { | ||
pub fn eval(&self) -> Result<Value, String> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only real change really... |
||
let mut ctx = create_context(); | ||
if self.extended { | ||
Self::add_extended_capabilities(&mut ctx) | ||
|
@@ -70,7 +70,7 @@ impl Expression { | |
map.get(&binding.into()).cloned().unwrap_or(Value::Null), | ||
); | ||
} | ||
Value::resolve(&self.expression, &ctx).expect("Cel expression couldn't be evaluated") | ||
Value::resolve(&self.expression, &ctx).map_err(|err| format!("{err:?}")) | ||
} | ||
|
||
/// Add support for `queryMap`, see [`decode_query_string`] | ||
|
@@ -198,10 +198,13 @@ impl Predicate { | |
}) | ||
} | ||
|
||
pub fn test(&self) -> bool { | ||
pub fn test(&self) -> Result<bool, String> { | ||
match self.expression.eval() { | ||
Value::Bool(result) => result, | ||
_ => false, | ||
Ok(value) => match value { | ||
Value::Bool(result) => Ok(result), | ||
_ => Err(format!("Expected boolean value, got {value:?}")), | ||
}, | ||
Err(err) => Err(err), | ||
} | ||
} | ||
} | ||
|
@@ -582,7 +585,7 @@ mod tests { | |
let predicate = Predicate::new("source.port == 65432").expect("This is valid CEL!"); | ||
property::test::TEST_PROPERTY_VALUE | ||
.set(Some(("source.port".into(), 65432_i64.to_le_bytes().into()))); | ||
assert!(predicate.test()); | ||
assert!(predicate.test().expect("This must evaluate properly!")); | ||
} | ||
|
||
#[test] | ||
|
@@ -604,43 +607,61 @@ mod tests { | |
]), | ||
"true".bytes().collect(), | ||
))); | ||
let value = Expression::new("auth.identity.anonymous").unwrap().eval(); | ||
let value = Expression::new("auth.identity.anonymous") | ||
.unwrap() | ||
.eval() | ||
.expect("This must evaluate!"); | ||
assert_eq!(value, true.into()); | ||
|
||
property::test::TEST_PROPERTY_VALUE.set(Some(( | ||
property::Path::new(vec!["filter_state", "wasm.kuadrant.auth.identity.age"]), | ||
"42".bytes().collect(), | ||
))); | ||
let value = Expression::new("auth.identity.age").unwrap().eval(); | ||
let value = Expression::new("auth.identity.age") | ||
.unwrap() | ||
.eval() | ||
.expect("This must evaluate!"); | ||
assert_eq!(value, 42.into()); | ||
|
||
property::test::TEST_PROPERTY_VALUE.set(Some(( | ||
property::Path::new(vec!["filter_state", "wasm.kuadrant.auth.identity.age"]), | ||
"42.3".bytes().collect(), | ||
))); | ||
let value = Expression::new("auth.identity.age").unwrap().eval(); | ||
let value = Expression::new("auth.identity.age") | ||
.unwrap() | ||
.eval() | ||
.expect("This must evaluate!"); | ||
assert_eq!(value, 42.3.into()); | ||
|
||
property::test::TEST_PROPERTY_VALUE.set(Some(( | ||
property::Path::new(vec!["filter_state", "wasm.kuadrant.auth.identity.age"]), | ||
"\"John\"".bytes().collect(), | ||
))); | ||
let value = Expression::new("auth.identity.age").unwrap().eval(); | ||
let value = Expression::new("auth.identity.age") | ||
.unwrap() | ||
.eval() | ||
.expect("This must evaluate!"); | ||
assert_eq!(value, "John".into()); | ||
|
||
property::test::TEST_PROPERTY_VALUE.set(Some(( | ||
property::Path::new(vec!["filter_state", "wasm.kuadrant.auth.identity.name"]), | ||
"-42".bytes().collect(), | ||
))); | ||
let value = Expression::new("auth.identity.name").unwrap().eval(); | ||
let value = Expression::new("auth.identity.name") | ||
.unwrap() | ||
.eval() | ||
.expect("This must evaluate!"); | ||
assert_eq!(value, (-42).into()); | ||
|
||
// let's fall back to strings, as that's what we read and set in store_metadata | ||
property::test::TEST_PROPERTY_VALUE.set(Some(( | ||
property::Path::new(vec!["filter_state", "wasm.kuadrant.auth.identity.age"]), | ||
"some random crap".bytes().collect(), | ||
))); | ||
let value = Expression::new("auth.identity.age").unwrap().eval(); | ||
let value = Expression::new("auth.identity.age") | ||
.unwrap() | ||
.eval() | ||
.expect("This must evaluate!"); | ||
assert_eq!(value, "some random crap".into()); | ||
} | ||
|
||
|
@@ -661,7 +682,7 @@ mod tests { | |
", | ||
) | ||
.expect("This is valid!"); | ||
assert!(predicate.test()); | ||
assert!(predicate.test().expect("This must evaluate properly!")); | ||
|
||
property::test::TEST_PROPERTY_VALUE.set(Some(( | ||
"request.query".into(), | ||
|
@@ -675,15 +696,15 @@ mod tests { | |
", | ||
) | ||
.expect("This is valid!"); | ||
assert!(predicate.test()); | ||
assert!(predicate.test().expect("This must evaluate properly!")); | ||
|
||
property::test::TEST_PROPERTY_VALUE.set(Some(( | ||
"request.query".into(), | ||
"%F0%9F%91%BE".bytes().collect(), | ||
))); | ||
let predicate = | ||
Predicate::route_rule("queryMap(request.query) == {'👾': ''}").expect("This is valid!"); | ||
assert!(predicate.test()); | ||
assert!(predicate.test().expect("This must evaluate properly!")); | ||
} | ||
|
||
#[test] | ||
|
@@ -721,7 +742,8 @@ mod tests { | |
))); | ||
let value = Expression::new("getHostProperty(['foo', 'bar.baz'])") | ||
.unwrap() | ||
.eval(); | ||
.eval() | ||
.expect("This must evaluate!"); | ||
assert_eq!(value, Value::Bytes(Arc::new(b"\xCA\xFE".to_vec()))); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... and these are the real things that I wanted...