From f0694a6bba93325c0d7f5f37df1fc55687f75b9b Mon Sep 17 00:00:00 2001 From: Mark Creamer Date: Tue, 2 Jul 2024 16:20:23 -0400 Subject: [PATCH 1/4] Adding toJson JNI method for Policy object Signed-off-by: Mark Creamer --- .../com/cedarpolicy/model/policy/Policy.java | 6 ++++++ CedarJavaFFI/src/interface.rs | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/CedarJava/src/main/java/com/cedarpolicy/model/policy/Policy.java b/CedarJava/src/main/java/com/cedarpolicy/model/policy/Policy.java index edfaea59..dd343fbc 100644 --- a/CedarJava/src/main/java/com/cedarpolicy/model/policy/Policy.java +++ b/CedarJava/src/main/java/com/cedarpolicy/model/policy/Policy.java @@ -65,6 +65,10 @@ public String toString() { return "// Policy ID: " + policyID + "\n" + policySrc; } + public String toJson() { + return toJsonJni(policySrc); + } + public static Policy parseStaticPolicy(String policyStr) throws InternalException, NullPointerException { var policyText = parsePolicyJni(policyStr); return new Policy(policyText, null); @@ -96,4 +100,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 NullPointerException; } diff --git a/CedarJavaFFI/src/interface.rs b/CedarJavaFFI/src/interface.rs index 3b6f331c..e7821952 100644 --- a/CedarJavaFFI/src/interface.rs +++ b/CedarJavaFFI/src/interface.rs @@ -380,6 +380,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> { + 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) { From 432af6b098e446af3e520bd44f70c0ae7e158830 Mon Sep 17 00:00:00 2001 From: Mark Creamer Date: Wed, 3 Jul 2024 10:36:51 -0400 Subject: [PATCH 2/4] Adding exception declarations to toJson Signed-off-by: Mark Creamer --- .../src/main/java/com/cedarpolicy/model/policy/Policy.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CedarJava/src/main/java/com/cedarpolicy/model/policy/Policy.java b/CedarJava/src/main/java/com/cedarpolicy/model/policy/Policy.java index 180dd1a9..00e529ea 100644 --- a/CedarJava/src/main/java/com/cedarpolicy/model/policy/Policy.java +++ b/CedarJava/src/main/java/com/cedarpolicy/model/policy/Policy.java @@ -79,7 +79,7 @@ public String toString() { return "// Policy ID: " + policyID + "\n" + policySrc; } - public String toJson() { + public String toJson() throws InternalException, NullPointerException { return toJsonJni(policySrc); } @@ -115,5 +115,5 @@ private static native String parsePolicyTemplateJni(String policyTemplateStr) private static native boolean validateTemplateLinkedPolicyJni(String templateText, EntityUID principal, EntityUID resource) throws InternalException, NullPointerException; - private native String toJsonJni(String policyStr) throws NullPointerException; + private native String toJsonJni(String policyStr) throws InternalException, NullPointerException; } From 770a678e8c9adb2a87dc378337b7d8cbf78662c3 Mon Sep 17 00:00:00 2001 From: Mark Creamer Date: Wed, 3 Jul 2024 11:49:47 -0400 Subject: [PATCH 3/4] Adding tests for toJson Signed-off-by: Mark Creamer --- .../java/com/cedarpolicy/PolicyTests.java | 31 +++++++++++++++++++ CedarJavaFFI/src/interface.rs | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java b/CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java index 1e9fb17e..22b16021 100644 --- a/CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java +++ b/CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java @@ -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 @@ -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); + String actualJson = template.toJson(); + fail("Expected InternalException"); + } catch (InternalException e) { + assertTrue(e.getMessage().contains("expected a static policy, got a template containing the slot ?resource")); + } + } } diff --git a/CedarJavaFFI/src/interface.rs b/CedarJavaFFI/src/interface.rs index e7821952..7587604f 100644 --- a/CedarJavaFFI/src/interface.rs +++ b/CedarJavaFFI/src/interface.rs @@ -380,7 +380,7 @@ fn validate_template_linked_policy_internal<'a>( } } -#[jni_fn("com.cedarpolicy.model.slice.Policy")] +#[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()), From e09b0e4684ddad5acbd8dcfd3203194576781197 Mon Sep 17 00:00:00 2001 From: Mark Creamer Date: Wed, 3 Jul 2024 12:01:56 -0400 Subject: [PATCH 4/4] Fixing dead store checkstyle Signed-off-by: Mark Creamer --- CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java b/CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java index 22b16021..e1fc27f5 100644 --- a/CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java +++ b/CedarJava/src/test/java/com/cedarpolicy/PolicyTests.java @@ -125,7 +125,7 @@ public void policyTemplateToJsonFailureTests() throws InternalException { try { String tbody = "permit(principal == ?principal, action, resource in ?resource);"; Policy template = Policy.parsePolicyTemplate(tbody); - String actualJson = template.toJson(); + template.toJson(); fail("Expected InternalException"); } catch (InternalException e) { assertTrue(e.getMessage().contains("expected a static policy, got a template containing the slot ?resource"));