Skip to content

Commit

Permalink
Add fromJson JNI method for Policy object
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Smirnov <[email protected]>
  • Loading branch information
n-smir committed Sep 30, 2024
1 parent 07a1b3e commit b765b21
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public String toJson() throws InternalException, NullPointerException {
return toJsonJni(policySrc);
}

public static Policy fromJson(String policyId, String policyJson) throws InternalException, NullPointerException {
var policyText = fromJsonJni(policyJson);
return new Policy(policyText, policyId);
}

public static Policy parseStaticPolicy(String policyStr) throws InternalException, NullPointerException {
var policyText = parsePolicyJni(policyStr);
return new Policy(policyText, null);
Expand All @@ -100,4 +105,5 @@ private static native String parsePolicyTemplateJni(String policyTemplateStr)
throws InternalException, NullPointerException;

private native String toJsonJni(String policyStr) throws InternalException, NullPointerException;
private static native String fromJsonJni(String policyJsonStr) throws InternalException, NullPointerException;
}
18 changes: 18 additions & 0 deletions CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,22 @@ public void policyTemplateToJsonFailureTests() throws InternalException {
assertTrue(e.getMessage().contains("expected a static policy, got a template containing the slot ?resource"));
}
}

@Test
public void policyFromJsonTest() throws InternalException {
assertThrows(NullPointerException.class, () -> {
String nullJson = null;
Policy.fromJson(null, nullJson);
});
assertThrows(InternalException.class, () -> {
String invalidJson = "effect\":\"permit\",\"principal\":{\"op\":\"All\"},\"action\":{\"op\":\"All\"}";
Policy.fromJson(null, invalidJson);
});

String validJson = "{\"effect\":\"permit\",\"principal\":{\"op\":\"All\"},\"action\":{\"op\":\"All\"},"
+ "\"resource\":{\"op\":\"All\"},\"conditions\":[]}";
Policy p = Policy.fromJson(null, validJson);
String actualJson = p.toJson();
assertEquals(validJson, actualJson);
}
}
28 changes: 28 additions & 0 deletions CedarJavaFFI/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,34 @@ fn to_json_internal<'a>(env: &mut JNIEnv<'a>, policy_jstr: JString<'a>) -> Resul
}
}

#[jni_fn("com.cedarpolicy.model.policy.Policy")]
pub fn fromJsonJni<'a>(mut env: JNIEnv<'a>, _: JClass, policy_json_jstr: JString<'a>) -> jvalue {
match from_json_internal(&mut env, policy_json_jstr) {
Err(e) => jni_failed(&mut env, e.as_ref()),
Ok(policy_text) => policy_text.as_jni(),
}
}

fn from_json_internal<'a>(
env: &mut JNIEnv<'a>,
policy_json_jstr: JString<'a>,
) -> Result<JValueOwned<'a>> {
if policy_json_jstr.is_null() {
raise_npe(env)
} else {
let policy_json_jstring = env.get_string(&policy_json_jstr)?;
let policy_json_string = String::from(policy_json_jstring);
let policy_json_value: Value = serde_json::from_str(&policy_json_string)?;
match Policy::from_json(None, policy_json_value) {
Err(e) => Err(Box::new(e)),
Ok(p) => {
let policy_text = format!("{}", p);
Ok(JValueGen::Object(env.new_string(&policy_text)?.into()))
}
}
}
}

#[jni_fn("com.cedarpolicy.value.EntityIdentifier")]
pub fn getEntityIdentifierRepr<'a>(mut env: JNIEnv<'a>, _: JClass, obj: JObject<'a>) -> jvalue {
match get_entity_identifier_repr_internal(&mut env, obj) {
Expand Down

0 comments on commit b765b21

Please sign in to comment.