Skip to content

Commit

Permalink
Fix #119
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Nov 2, 2024
1 parent eab58b9 commit 453dbcc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
56 changes: 55 additions & 1 deletion src/data/cel.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::data::get_attribute;
use crate::data::property::{host_get_map, Path};
use cel_interpreter::extractors::This;
use cel_interpreter::objects::{Map, ValueType};
use cel_interpreter::{Context, Value};
use cel_interpreter::{Context, ExecutionError, ResolveResult, Value};
use cel_parser::{parse, Expression as CelExpression, Member, ParseError};
use chrono::{DateTime, FixedOffset};
use proxy_wasm::types::{Bytes, Status};
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
Expand Down Expand Up @@ -45,6 +47,8 @@ impl Expression {
let mut ctx = Context::default();
let Map { map } = self.build_data_map();

ctx.add_function("getHostProperty", get_host_property);

// if expression was "auth.identity.anonymous",
// {
// "auth": { "identity": { "anonymous": true } }
Expand All @@ -63,6 +67,42 @@ impl Expression {
}
}

#[cfg(test)]
pub fn inner_host_get_property(path: Vec<&str>) -> Result<Option<Bytes>, Status> {
super::property::host_get_property(&Path::new(path.into()))

Check failure on line 72 in src/data/cel.rs

View workflow job for this annotation

GitHub Actions / Clippy

useless conversion to the same type: `std::vec::Vec<&str>`
}

#[cfg(not(test))]
pub fn inner_host_get_property(path: Vec<&str>) -> Result<Option<Bytes>, Status> {
proxy_wasm::hostcalls::get_property(path)
}

fn get_host_property(This(this): This<Value>) -> ResolveResult {
match this {
Value::List(ref items) => {
let mut tokens = Vec::with_capacity(items.len());
for item in items.iter() {
match item {
Value::String(token) => tokens.push(token.as_str()),
_ => return Err(this.error_expected_type(ValueType::String)),
}
}

match inner_host_get_property(tokens) {
Ok(data) => match data {
None => Ok(Value::Null),
Some(bytes) => Ok(Value::Bytes(bytes.into())),
},
Err(err) => Err(ExecutionError::FunctionError {
function: "hostcalls::get_property".to_string(),
message: format!("Status: {:?}", err),
}),
}
}
_ => Err(this.error_expected_type(ValueType::List)),
}
}

#[derive(Clone, Debug)]
pub struct Predicate {
expression: Expression,
Expand Down Expand Up @@ -451,6 +491,8 @@ mod tests {
use crate::data::cel::{known_attribute_for, Expression, Predicate};
use crate::data::property;
use cel_interpreter::objects::ValueType;
use cel_interpreter::Value;
use std::sync::Arc;

#[test]
fn predicates() {
Expand Down Expand Up @@ -545,4 +587,16 @@ mod tests {
_ => panic!("Not supposed to get here!"),
}
}

#[test]
fn expression_access_host() {
property::test::TEST_PROPERTY_VALUE.set(Some((
property::Path::new(vec!["foo", "bar.baz"]),
b"\xCA\xFE".to_vec(),
)));
let value = Expression::new("getHostProperty(['foo', 'bar.baz'])")
.unwrap()
.eval();
assert_eq!(value, Value::Bytes(Arc::new(b"\xCA\xFE".to_vec())));
}
}
4 changes: 2 additions & 2 deletions src/data/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn wasm_prop(tokens: &[&str]) -> Path {
}

#[cfg(test)]
fn host_get_property(path: &Path) -> Result<Option<Vec<u8>>, Status> {
pub(super) fn host_get_property(path: &Path) -> Result<Option<Vec<u8>>, Status> {
debug!("get_property: {:?}", path);
match test::TEST_PROPERTY_VALUE.take() {
None => Err(Status::NotFound),
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn host_get_map(path: &Path) -> Result<HashMap<String, String>, String> {
}

#[cfg(not(test))]
fn host_get_property(path: &Path) -> Result<Option<Vec<u8>>, Status> {
pub(super) fn host_get_property(path: &Path) -> Result<Option<Vec<u8>>, Status> {
debug!("get_property: {:?}", path);
proxy_wasm::hostcalls::get_property(path.tokens())
}
Expand Down

0 comments on commit 453dbcc

Please sign in to comment.