Skip to content
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

Fix #119 #130

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}

#[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
Loading