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 fromJson JNI method for Policy object #223

Merged
merged 1 commit into from
Sep 30, 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
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