Skip to content

Commit

Permalink
Adding toJson JNI method for Policy object (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-creamer-amazon authored Aug 13, 2024
1 parent 100f225 commit d8ba863
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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 @@ -97,4 +101,6 @@ public static boolean validateTemplateLinkedPolicy(Policy p, EntityUID principal
private static native String parsePolicyJni(String policyStr) throws InternalException, NullPointerException;
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;
}
34 changes: 33 additions & 1 deletion CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class PolicyTests {
@Test
Expand Down Expand Up @@ -99,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 @@ -316,6 +316,26 @@ fn validate_template_linked_policy_internal<'a>(
}
}

#[jni_fn("com.cedarpolicy.model.slice.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

0 comments on commit d8ba863

Please sign in to comment.