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
@@ -0,0 +1,17 @@
package com.cedarpolicy.formatter;

import com.cedarpolicy.loader.LibraryLoader;
import com.cedarpolicy.model.exception.InternalException;

public final class PolicyFormatter {

static {
LibraryLoader.loadLibrary();
}

private PolicyFormatter() {
}

public static native String policiesStrToPretty(String policies)
throws InternalException, NullPointerException;
}
40 changes: 40 additions & 0 deletions CedarJava/src/test/java/com/cedarpolicy/PolicyFormatterTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.cedarpolicy;

import com.cedarpolicy.formatter.PolicyFormatter;
import com.cedarpolicy.model.exception.InternalException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class PolicyFormatterTests {

private static final String TEST_RESOURCES_DIR = "src/test/resources/";

@Test
public void testPoliciesStrToPretty() throws Exception {
String unformattedCedarPolicy = Files.readString(
Path.of(TEST_RESOURCES_DIR + "unformatted_policy.cedar"));

String formattedCedarPolicy = Files.readString(
Path.of(TEST_RESOURCES_DIR + "formatted_policy.cedar"));

assertEquals(formattedCedarPolicy, PolicyFormatter.policiesStrToPretty(unformattedCedarPolicy));
}

@Test
public void testPoliciesStrToPrettyMalformedCedarPolicy() throws Exception {
String malformedCedarPolicy = Files.readString(
Path.of(TEST_RESOURCES_DIR + "malformed_policy_set.cedar"));

assertThrows(InternalException.class,
() -> PolicyFormatter.policiesStrToPretty(malformedCedarPolicy));
}

@Test
public void testPoliciesStrToPrettyNullSafety() {
assertThrows(NullPointerException.class, () -> PolicyFormatter.policiesStrToPretty(null));
}
}
6 changes: 6 additions & 0 deletions CedarJava/src/test/resources/formatted_policy.cedar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
permit (
principal,
action == Action::"update",
resource
)
when { resource.owner == principal };
6 changes: 6 additions & 0 deletions CedarJava/src/test/resources/unformatted_policy.cedar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
permit(
principal,
action
== Action::"update",
resource
) when {resource.owner == principal};
5 changes: 5 additions & 0 deletions CedarJavaFFI/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ crate_type = ["cdylib"]
version = "4.0.0"
git = "https://github.com/cedar-policy/cedar"
branch = "main"

[dependencies.cedar-policy-formatter]
version = "4.0.0"
git = "https://github.com/cedar-policy/cedar"
branch = "main"
29 changes: 29 additions & 0 deletions CedarJavaFFI/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use cedar_policy::{
ffi::{is_authorized_json_str, validate_json_str},
EntityUid, Policy, PolicySet, Schema, Template,
};
use cedar_policy_formatter::{policies_str_to_pretty, Config};
use jni::{
objects::{JClass, JObject, JString, JValueGen, JValueOwned},
sys::{jstring, jvalue},
Expand Down Expand Up @@ -460,3 +461,31 @@ fn get_euid_repr_internal<'a>(
Ok(jstring.into())
}
}

#[jni_fn("com.cedarpolicy.formatter.PolicyFormatter")]
pub fn policiesStrToPretty<'a>(
mut env: JNIEnv<'a>,
_: JClass,
policies_jstr: JString<'a>,
) -> jvalue {
match policies_str_to_pretty_internal(&mut env, policies_jstr) {
Ok(v) => v.as_jni(),
Err(e) => jni_failed(&mut env, e.as_ref()),
}
}

fn policies_str_to_pretty_internal<'a>(
env: &mut JNIEnv<'a>,
policies_jstr: JString<'a>,
) -> Result<JValueOwned<'a>> {
if policies_jstr.is_null() {
raise_npe(env)
} else {
let config = Config::default();
let policies_str = String::from(env.get_string(&policies_jstr)?);
match policies_str_to_pretty(&policies_str, &config) {
Ok(formatted_policies) => Ok(env.new_string(formatted_policies)?.into()),
Err(e) => Err(e.into()),
}
}
}
8 changes: 7 additions & 1 deletion configure_ci_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
cargo_file=CedarJavaFFI/Cargo.toml


sed -i -e '/\[dependencies.cedar-policy/,+4d' $cargo_file
sed -i -e '/\[dependencies.cedar-policy\]/,+4d' $cargo_file

echo "" >> $cargo_file
echo "[dependencies.cedar-policy]" >> $cargo_file
echo "path = \"../cedar/cedar-policy\"" >> $cargo_file

sed -i -e '/\[dependencies.cedar-policy-formatter\]/,+4d' $cargo_file

echo "" >> $cargo_file
echo "[dependencies.cedar-policy-formatter]" >> $cargo_file
echo "path = \"../cedar/cedar-policy-formatter\"" >> $cargo_file