Skip to content
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 @@ -79,6 +79,10 @@ public String toString() {
return "// Policy ID: " + policyID + "\n" + policySrc;
}

public String toJson() throws InternalException, NullPointerException {
return toJsonJni(policySrc);
}

public static Policy parseStaticPolicy(String policyStr) throws InternalException, NullPointerException {
var policyText = parsePolicyJni(policyStr);
return new Policy(policyText, null);
Expand Down Expand Up @@ -110,4 +114,6 @@ private static native String parsePolicyTemplateJni(String policyTemplateStr)
throws InternalException, NullPointerException;
private static native boolean validateTemplateLinkedPolicyJni(String templateText, EntityUID principal, EntityUID resource)
throws InternalException, NullPointerException;

private native String toJsonJni(String policyStr) throws InternalException, NullPointerException;
}
31 changes: 31 additions & 0 deletions CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class PolicyTests {
@Test
Expand Down Expand Up @@ -100,4 +101,34 @@ public void validateTemplateLinkedPolicyFailsWhenExpected() {
Policy.validateTemplateLinkedPolicy(p3, principal, resource);
});
}

@Test
public void staticPolicyToJsonTests() throws InternalException {
assertThrows(NullPointerException.class, () -> {
Policy p = new Policy(null, null);
p.toJson();
});
assertThrows(InternalException.class, () -> {
Policy p = new Policy("permit();", null);
p.toJson();
});

Policy p = Policy.parseStaticPolicy("permit(principal, action, resource);");
String actualJson = p.toJson();
String expectedJson = "{\"effect\":\"permit\",\"principal\":{\"op\":\"All\"},\"action\":{\"op\":\"All\"},"
+ "\"resource\":{\"op\":\"All\"},\"conditions\":[]}";
assertEquals(expectedJson, actualJson);
}

@Test
public void policyTemplateToJsonFailureTests() throws InternalException {
try {
String tbody = "permit(principal == ?principal, action, resource in ?resource);";
Policy template = Policy.parsePolicyTemplate(tbody);
template.toJson();
fail("Expected InternalException");
} catch (InternalException e) {
assertTrue(e.getMessage().contains("expected a static policy, got a template containing the slot ?resource"));
}
}
}
20 changes: 20 additions & 0 deletions CedarJavaFFI/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,26 @@ fn validate_template_linked_policy_internal<'a>(
}
}

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

fn to_json_internal<'a>(env: &mut JNIEnv<'a>, policy_jstr: JString<'a>) -> Result<JValueOwned<'a>> {
if policy_jstr.is_null() {
raise_npe(env)
} else {
let policy_jstring = env.get_string(&policy_jstr)?;
let policy_string = String::from(policy_jstring);
let policy = Policy::from_str(&policy_string)?;
let policy_json = serde_json::to_string(&policy.to_json().unwrap())?;
Ok(JValueGen::Object(env.new_string(&policy_json)?.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