Skip to content

Commit

Permalink
add parse euid api (#52)
Browse files Browse the repository at this point in the history
Co-authored-by: Kesha Hietala <[email protected]>
  • Loading branch information
andrewmwells-amazon and khieta authored Oct 6, 2023
1 parent b40c4ae commit 828529b
Showing 1 changed file with 42 additions and 1 deletion.
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

0 comments on commit 828529b

Please sign in to comment.