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

add parse euid api #52

Merged
merged 3 commits into from
Oct 6, 2023
Merged
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
43 changes: 42 additions & 1 deletion CedarJavaFFI/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ use jni::{
};
use jni_fn::jni_fn;
use serde::{Deserialize, Serialize};
use std::thread;
use std::{str::FromStr, thread};

const V0_AUTH_OP: &str = "AuthorizationOperation";
const V0_VALIDATE_OP: &str = "ValidateOperation";
const V0_PARSE_EUID_OP: &str = "ParseEntityUidOperation";

fn build_err_obj(env: JNIEnv<'_>, err: &str) -> jstring {
env.new_string(
Expand Down Expand Up @@ -105,6 +106,7 @@ fn call_cedar(call: &str, input: &str) -> String {
let result = match call.as_str() {
V0_AUTH_OP => json_is_authorized(&input),
V0_VALIDATE_OP => json_validate(&input),
V0_PARSE_EUID_OP => json_parse_entity_uid(&input),
_ => InterfaceResult::fail_internally(format!("unsupported operation: {}", call)),
};
serde_json::to_string(&result).expect("could not serialise response")
Expand All @@ -116,10 +118,49 @@ struct JavaInterfaceCall {
arguments: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct ParseEUIDCall {
euid: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct ParseEUIDOutput {
ty: String,
id: String,
}

/// public string-based JSON interface to be invoked by FFIs. Takes in a `ParseEUIDCall`, parses it and (if successful)
/// returns a serialized `ParseEUIDOutput`
pub fn json_parse_entity_uid(input: &str) -> InterfaceResult {
match serde_json::from_str::<ParseEUIDCall>(input) {
Err(e) => {
InterfaceResult::fail_internally(format!("error parsing call to parse EntityUID: {e:}"))
}
Ok(euid_call) => match cedar_policy::EntityUid::from_str(euid_call.euid.as_str()) {
Ok(euid) => match serde_json::to_string(&ParseEUIDOutput {
ty: euid.type_name().to_string(),
id: euid.id().to_string(),
}) {
Ok(s) => InterfaceResult::succeed(s),
Err(e) => {
InterfaceResult::fail_internally(format!("error serializing EntityUID: {e:}"))
}
},
Err(e) => InterfaceResult::fail_internally(format!("error parsing EntityUID: {e:}")),
},
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn parse_entityuid() {
let result = call_cedar("ParseEntityUidOperation", r#"{"euid": "User::\"Alice\""} "#);
assert_success(result);
}

#[test]
fn empty_authorization_call_succeeds() {
let result = call_cedar(
Expand Down