Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restructure slice & change instantiation to link #169

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -113,7 +113,7 @@ private static class AuthorizationRequest extends com.cedarpolicy.model.Authoriz
request.context,
request.schema,
request.enableRequestValidation);
this.slice = new BasicSlice(policySet.policies, entities, policySet.templates, policySet.templateInstantiations);
this.slice = new BasicSlice(policySet.policies, entities, policySet.templates, policySet.templateLinks);
}
}

Expand All @@ -124,7 +124,7 @@ private static final class PartialAuthorizationRequest {

PartialAuthorizationRequest(com.cedarpolicy.model.PartialAuthorizationRequest request, PolicySet policySet, Set<Entity> entities) {
this.request = request;
this.slice = new BasicSlice(policySet.policies, entities, policySet.templates, policySet.templateInstantiations);
this.slice = new BasicSlice(policySet.policies, entities, policySet.templates, policySet.templateLinks);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

/** Instantiation for policy template. */
public class Instantiation {
/** Link for policy template. */
public class LinkValue {
private final String slot;
private final EntityTypeAndId value;

/**
* Instantiation for policy template.
* Link for policy template.
*
* @param slot the slot in the template.
* @param value the value to put in the slot
*/
@JsonCreator
public Instantiation(
public LinkValue(
@JsonProperty("slot") String slot, @JsonProperty("value") EntityTypeAndId value) {
this.slot = slot;
this.value = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public static Policy parsePolicyTemplate(String templateStr) throws InternalExc
}

/**
* This method takes in a Policy and a list of Instantiations and calls Cedar JNI to ensure those slots
* can be used to instantiate the template. If the Template is validated ahead of time by using Policy.parsePolicyTemplate
* and the Instantiations are also ensured to be valid (for example, by validating their parts using EntityTypeName.parse
* and EntityIdentifier.parse), then this should only fail because the slots in the template don't match the instantiations
* This method takes in a template and a list of link values and calls Cedar JNI to ensure those slots
* can be used to link the template. If the template is validated ahead of time by using Policy.parsePolicyTemplate
* and the link values are also ensured to be valid (for example, by validating their parts using EntityTypeName.parse
* and EntityIdentifier.parse), then this should only fail because the slots in the template don't match the link values
* (barring JNI failures).
* @param p Policy object constructed from a valid template. Best if built from Policy.parsePolicyTemplate
* @param principal EntityUid to put into the principal slot. Leave null if there's no principal slot
Expand Down
20 changes: 10 additions & 10 deletions CedarJava/src/main/java/com/cedarpolicy/model/policy/PolicySet.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,43 @@
import java.nio.file.Files;
import java.nio.file.Path;

/** Policy Set containing policies in the Cedar language. */
/** Policy set containing policies in the Cedar language. */
public class PolicySet {
static {
LibraryLoader.loadLibrary();
}

/** Policy set. */
/** Static policies */
public Set<Policy> policies;

/** Template Instantiations. */
public List<TemplateInstantiation> templateInstantiations;
/** Template-linked policies */
public List<TemplateLink> templateLinks;

/** Templates. */
/** Policy templates */
public Set<Policy> templates;

public PolicySet() {
this.policies = Collections.emptySet();
this.templates = Collections.emptySet();
this.templateInstantiations = Collections.emptyList();
this.templateLinks = Collections.emptyList();
}

public PolicySet(Set<Policy> policies) {
this.policies = policies;
this.templates = Collections.emptySet();
this.templateInstantiations = Collections.emptyList();
this.templateLinks = Collections.emptyList();
}

public PolicySet(Set<Policy> policies, Set<Policy> templates) {
this.policies = policies;
this.templates = templates;
this.templateInstantiations = Collections.emptyList();
this.templateLinks = Collections.emptyList();
}

public PolicySet(Set<Policy> policies, Set<Policy> templates, List<TemplateInstantiation> templateInstantiations) {
public PolicySet(Set<Policy> policies, Set<Policy> templates, List<TemplateLink> templateLinks) {
this.policies = policies;
this.templates = templates;
this.templateInstantiations = templateInstantiations;
this.templateLinks = templateLinks;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,33 @@
import java.util.List;
import com.google.common.collect.ImmutableList;

/** Template instantiation. */
public class TemplateInstantiation {
/** Template-linked policy. */
public class TemplateLink {

@JsonProperty("templateId")
private final String templateId;

@JsonProperty("resultPolicyId")
private final String resultPolicyId;

private final List<Instantiation> instantiations;
@JsonProperty("instantiations")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing the cedar-policy ffi interface relies on this Json property name and will be renamed in a later change?

Copy link
Contributor Author

@khieta khieta Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, coming soon 😄

private final List<LinkValue> linkValues;

/**
* Template Instantiation.
* Template-linked policy.
*
* @param templateId the template ID.
* @param resultPolicyId the id of the resulting policy.
* @param instantiations the instantiations.
* @param linkValues the link values.
*/
@JsonCreator
public TemplateInstantiation(
public TemplateLink(
@JsonProperty("templateId") String templateId,
@JsonProperty("resultPolicyId") String resultPolicyId,
@JsonProperty("instantiations") List<Instantiation> instantiations) {
@JsonProperty("instantiations") List<LinkValue> linkValues) {
this.templateId = templateId;
this.resultPolicyId = resultPolicyId;
this.instantiations = ImmutableList.copyOf(instantiations);
this.linkValues = ImmutableList.copyOf(linkValues);
}

/** Get the template ID. */
Expand All @@ -59,8 +60,8 @@ public String getResultPolicyId() {
return resultPolicyId;
}

/** Get the instantiations to fill the slots. */
public List<Instantiation> getInstantiations() {
return instantiations;
/** Get the link values, which map slots to EUIDs. */
public List<LinkValue> getLinkValues() {
return linkValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.cedarpolicy.model.entity.Entity;
import com.cedarpolicy.model.policy.Policy;
import com.cedarpolicy.model.policy.TemplateInstantiation;
import com.cedarpolicy.model.policy.TemplateLink;
import com.cedarpolicy.value.Value;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand All @@ -41,22 +41,22 @@ public class BasicSlice implements Slice {
private final Map<String, String> templatePolicies;

@JsonProperty("templateInstantiations")
private final List<TemplateInstantiation> templateInstantiations;
private final List<TemplateLink> templateLinks;

/**
* Construct a Slice from Entity and Policy objects.
*
* @param policies Set of policies.
* @param entities Set of entities.
* @param templates Set of policy templates.
* @param templateInstantiations List of TemplateInstantiations.
* @param templateLinks List of templateLinks.
*/
@SuppressFBWarnings
public BasicSlice(
Set<Policy> policies,
Set<Entity> entities,
Set<Policy> templates,
List<TemplateInstantiation> templateInstantiations) {
List<TemplateLink> templateLinks) {
// Copy of previous constructor. We can't call the previous constructor because fields are
// final
this.policies = new HashMap<>();
Expand All @@ -79,7 +79,7 @@ public BasicSlice(

this.templatePolicies =
templates.stream().collect(Collectors.toMap(p -> p.policyID, p -> p.policySrc));
this.templateInstantiations = new ArrayList<TemplateInstantiation>(templateInstantiations);
this.templateLinks = new ArrayList<TemplateLink>(templateLinks);
}


Expand Down Expand Up @@ -128,8 +128,8 @@ public Map<String, String> getTemplates() {

@Override
@SuppressFBWarnings
public List<TemplateInstantiation> getTemplateInstantiations() {
return templateInstantiations;
public List<TemplateLink> getTemplateLinks() {
return templateLinks;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.cedarpolicy.model.slice;

import com.cedarpolicy.model.entity.Entity;
import com.cedarpolicy.model.policy.TemplateInstantiation;
import com.cedarpolicy.model.policy.TemplateLink;
import com.cedarpolicy.value.Value;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -68,9 +68,9 @@ public interface Slice {
Map<String, String> getTemplates();

/**
* Get the template instantiations.
* Get the template links.
*
* @return List of template instatiations
* @return List of template links
*/
List<TemplateInstantiation> getTemplateInstantiations();
List<TemplateLink> getTemplateLinks();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void serialize(
"entities", convertEntitiesToJsonEntities(slice.getEntities()));
jsonGenerator.writeObjectField("templates", slice.getTemplates());
jsonGenerator.writeObjectField(
"templateInstantiations", slice.getTemplateInstantiations());
"templateInstantiations", slice.getTemplateLinks());
jsonGenerator.writeEndObject();
}

Expand Down
23 changes: 11 additions & 12 deletions CedarJava/src/test/java/com/cedarpolicy/pbt/IntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import com.cedarpolicy.model.AuthorizationResponse;
import com.cedarpolicy.model.entity.Entity;
import com.cedarpolicy.model.policy.EntityTypeAndId;
import com.cedarpolicy.model.policy.Instantiation;
import com.cedarpolicy.model.policy.LinkValue;
import com.cedarpolicy.model.policy.Policy;
import com.cedarpolicy.model.policy.PolicySet;
import com.cedarpolicy.model.policy.TemplateInstantiation;
import com.cedarpolicy.model.policy.TemplateLink;
import com.cedarpolicy.value.Decimal;
import com.cedarpolicy.value.EntityUID;
import com.cedarpolicy.value.EntityTypeName;
Expand Down Expand Up @@ -554,20 +554,19 @@ public void testTemplateResourceAttribute() {
Set<Policy> templates = new HashSet<>();
templates.add(policy);

Instantiation instantiation =
new Instantiation(principalSlot, new EntityTypeAndId("User", "alice"));
LinkValue linkValue = new LinkValue(principalSlot, new EntityTypeAndId("User", "alice"));

final String instantiatedPolicyId = "ID0_alice";
TemplateInstantiation templateInstantiation =
new TemplateInstantiation(
final String linkId = "ID0_alice";
TemplateLink templateLink =
new TemplateLink(
policyId,
instantiatedPolicyId,
new ArrayList<Instantiation>(Arrays.asList(instantiation)));
linkId,
new ArrayList<LinkValue>(Arrays.asList(linkValue)));

ArrayList<TemplateInstantiation> templateInstantiations =
new ArrayList<TemplateInstantiation>(Arrays.asList(templateInstantiation));
ArrayList<TemplateLink> templateLinks =
new ArrayList<TemplateLink>(Arrays.asList(templateLink));

PolicySet policySet = new PolicySet(policies, templates, templateInstantiations);
PolicySet policySet = new PolicySet(policies, templates, templateLinks);
Map<String, Value> currentContext = new HashMap<>();
AuthorizationRequest request =
new AuthorizationRequest(
Expand Down
4 changes: 2 additions & 2 deletions CedarJavaFFI/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,11 @@ fn validate_template_linked_policy_internal<'a>(
}

let template_id = template.id().clone();
let instantiated_id = PolicyId::from_str("x")?;
let link_id = PolicyId::from_str("x")?;
let mut policy_set = PolicySet::new();
policy_set.add_template(template)?;

policy_set.link(template_id, instantiated_id, slots_map)?;
policy_set.link(template_id, link_id, slots_map)?;
Ok(JValueGen::Bool(1))
}
}
Expand Down