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 @@ -16,8 +16,10 @@

package com.cedarpolicy.model.policy;

import static com.cedarpolicy.CedarJson.objectWriter;
import com.cedarpolicy.loader.LibraryLoader;
import com.cedarpolicy.model.exception.InternalException;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -87,22 +89,33 @@ public Map<String, String> getTemplates() {

/**
* Gets number of static policies in the Policy Set.
*
*
* @return number of static policies, returns 0 if policies set is null
*/
public int getNumPolicies() {
return policies != null ? policies.size() : 0;
}
}

/**
* Gets number of templates in the Policy Set.
*
*
* @return number of templates, returns 0 if templates set is null
*/
public int getNumTemplates() {
return templates != null ? templates.size() : 0;
}

/**
* Converts the PolicySet object to a Cedar JSON string representation.
*
* @return Cedar JSON string representation of the PolicySet
* @throws InternalException if there is an error during JSON conversion in the Rust native code
* @throws JsonProcessingException if there is an error serializing the object to JSON
*/
public String toJson() throws InternalException, JsonProcessingException {
return policySetToJson(objectWriter().writeValueAsString(this));
}

/**
* Parse multiple policies and templates from a file into a PolicySet.
* @param filePath the path to the file containing the policies
Expand Down Expand Up @@ -130,4 +143,5 @@ public static PolicySet parsePolicies(String policiesString) throws InternalExce
}

private static native PolicySet parsePoliciesJni(String policiesStr) throws InternalException, NullPointerException;
private static native String policySetToJson(String policySetStr) throws InternalException, NullPointerException;
}
49 changes: 40 additions & 9 deletions CedarJava/src/test/java/com/cedarpolicy/PolicySetTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@

import java.io.IOException;
import java.nio.file.Path;
import com.fasterxml.jackson.core.JsonProcessingException;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static com.cedarpolicy.TestUtil.buildValidPolicySet;
import static com.cedarpolicy.TestUtil.buildInvalidPolicySet;

public class PolicySetTests {
private static final String TEST_RESOURCES_DIR = "src/test/resources/";

@Test
public void parsePoliciesTests() throws InternalException, IOException {
PolicySet policySet = PolicySet.parsePolicies(Path.of(TEST_RESOURCES_DIR + "policies.cedar"));
for (Policy p: policySet.policies) {
PolicySet policySet =
PolicySet.parsePolicies(Path.of(TEST_RESOURCES_DIR + "policies.cedar"));
for (Policy p : policySet.policies) {
assertNotNull(p.policySrc);
}
// Make sure the policy IDs are unique as Policies are made
Expand All @@ -46,13 +50,14 @@ public void parsePoliciesTests() throws InternalException, IOException {
@Test
public void parsePoliciesStringTests() throws InternalException {
PolicySet policySet = PolicySet.parsePolicies("permit(principal, action, resource);");
PolicySet policySet2 = PolicySet.parsePolicies("permit(principal, action, resource) when { principal has x && principal.x == 5};");
for (Policy p: policySet.policies) {
PolicySet policySet2 = PolicySet.parsePolicies(
"permit(principal, action, resource) when { principal has x && principal.x == 5};");
for (Policy p : policySet.policies) {
assertNotNull(p.policySrc);
}
assertEquals(1, policySet.policies.size());
assertEquals(0, policySet.templates.size());
for (Policy p: policySet2.policies) {
for (Policy p : policySet2.policies) {
assertNotNull(p.policySrc);
}
assertEquals(1, policySet2.policies.size());
Expand All @@ -61,13 +66,14 @@ public void parsePoliciesStringTests() throws InternalException {

@Test
public void parseTemplatesTests() throws InternalException, IOException {
PolicySet policySet = PolicySet.parsePolicies(Path.of(TEST_RESOURCES_DIR + "template.cedar"));
for (Policy p: policySet.policies) {
PolicySet policySet =
PolicySet.parsePolicies(Path.of(TEST_RESOURCES_DIR + "template.cedar"));
for (Policy p : policySet.policies) {
assertNotNull(p.policySrc);
}
assertEquals(2, policySet.policies.size());

for (Policy p: policySet.templates) {
for (Policy p : policySet.templates) {
assertNotNull(p.policySrc);
}
assertEquals(1, policySet.templates.size());
Expand Down Expand Up @@ -96,8 +102,33 @@ public void getNumTests() throws InternalException, IOException {
assertEquals(0, emptyPolicySet.getNumTemplates());

// Non-empty policy set
PolicySet policySet = PolicySet.parsePolicies(Path.of(TEST_RESOURCES_DIR + "template.cedar"));
PolicySet policySet =
PolicySet.parsePolicies(Path.of(TEST_RESOURCES_DIR + "template.cedar"));
assertEquals(2, policySet.getNumPolicies());
assertEquals(1, policySet.getNumTemplates());
}

@Test
public void policySetToJsonTests()
throws JsonProcessingException, IOException, InternalException {
// Tests valid PolicySet
PolicySet validPolicySet = buildValidPolicySet();
String validJson =
"{\"templates\":{\"t0\":{\"effect\":\"permit\",\"principal\":{\"op\":\"==\",\"slot\":\"?principal\"},"
+ "\"action\":{\"op\":\"==\",\"entity\":{\"type\":\"Action\",\"id\":\"View_Photo\"}},"
+ "\"resource\":{\"op\":\"in\",\"entity\":{\"type\":\"Album\",\"id\":\"Vacation\"}},\"conditions\":[]}},"
+ "\"staticPolicies\":{\"p1\":{\"effect\":\"permit\",\"principal\":{\"op\":\"==\","
+ "\"entity\":{\"type\":\"User\",\"id\":\"Bob\"}},"
+ "\"action\":{\"op\":\"==\",\"entity\":{\"type\":\"Action\",\"id\":\"View_Photo\"}},"
+ "\"resource\":{\"op\":\"in\",\"entity\":{\"type\":\"Album\",\"id\":\"Vacation\"}},\"conditions\":[]}},"
+ "\"templateLinks\":[{\"templateId\":\"t0\",\"newId\":\"tl0\",\"values\":{\"?principal\":"
+ "{\"__entity\":{\"type\":\"User\",\"id\":\"Alice\"}}}}]}";
assertEquals(validJson, validPolicySet.toJson());

// Tests invalid PolicySet
PolicySet invalidPolicySet = buildInvalidPolicySet();
assertThrows(InternalException.class, () -> {
invalidPolicySet.toJson();
});
}
}
63 changes: 63 additions & 0 deletions CedarJava/src/test/java/com/cedarpolicy/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@

import com.cedarpolicy.model.schema.Schema;
import com.cedarpolicy.model.schema.Schema.JsonOrCedar;
import com.cedarpolicy.model.policy.TemplateLink;
import com.cedarpolicy.model.policy.PolicySet;
import com.cedarpolicy.model.policy.LinkValue;
import com.cedarpolicy.model.policy.Policy;
import com.cedarpolicy.model.entity.Entity;
import com.cedarpolicy.value.EntityTypeName;

import java.util.HashSet;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/** Utils to help with tests. */
public final class TestUtil {
Expand All @@ -45,4 +55,57 @@ public static Schema loadSchemaResource(String schemaFile) {
throw new RuntimeException("Failed to load test schema file " + schemaFile, e);
}
}

public static PolicySet buildValidPolicySet() {
EntityTypeName principalType = EntityTypeName.parse("User").get();
Set<Policy> policies = new HashSet<>();
Set<Policy> templates = new HashSet<>();
ArrayList<TemplateLink> templateLinks = new ArrayList<TemplateLink>();
ArrayList<LinkValue> linkValueList = new ArrayList<>();

String fullPolicy =
"permit(principal == User::\"Bob\", action == Action::\"View_Photo\", resource in Album::\"Vacation\");";
Policy newPolicy = new Policy(fullPolicy, "p1");
policies.add(newPolicy);

String template = "permit(principal == ?principal, action == Action::\"View_Photo\", resource in Album::\"Vacation\");";
Policy policyTemplate = new Policy(template, "t0");
templates.add(policyTemplate);

Entity principal = new Entity(principalType.of("Alice"), new HashMap<>(), new HashSet<>());
LinkValue principalLinkValue = new LinkValue("?principal", principal.getEUID());
linkValueList.add(principalLinkValue);

TemplateLink templateLink = new TemplateLink("t0", "tl0", linkValueList);
templateLinks.add(templateLink);

return new PolicySet(policies, templates, templateLinks);
}

public static PolicySet buildInvalidPolicySet() {
EntityTypeName principalType = EntityTypeName.parse("User").get();
Set<Policy> policies = new HashSet<>();
Set<Policy> templates = new HashSet<>();
ArrayList<TemplateLink> templateLinks = new ArrayList<TemplateLink>();
ArrayList<LinkValue> linkValueList = new ArrayList<>();

String fullPolicy =
"permit(prinipal == User::\"Bob\", action == Action::\"View_Photo\", resource in Album::\"Vacation\");";
Policy newPolicy = new Policy(fullPolicy, "p1");
policies.add(newPolicy);

String template = "permit(principal, action == Action::\"View_Photo\", resource in Album::\"Vacation\");";
Policy policyTemplate = new Policy(template, "t0");
templates.add(policyTemplate);

Entity principal = new Entity(principalType.of("Alice"), new HashMap<>(), new HashSet<>());
LinkValue principalLinkValue = new LinkValue("?principal", principal.getEUID());
linkValueList.add(principalLinkValue);

TemplateLink templateLink = new TemplateLink("t0", "tl0", linkValueList);
templateLinks.add(templateLink);

return new PolicySet(policies, templates, templateLinks);
}

}
Loading