Skip to content

Commit

Permalink
widened interface for PolicyEnforcers to accept Iterable of PolicyEntry
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed May 10, 2022
1 parent d1fcbd6 commit 469c48b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
5 changes: 5 additions & 0 deletions policies/model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
<!-- Don't add excludes here before checking with the whole Ditto team -->
<!-- <exclude></exclude> -->
<exclude>org.eclipse.ditto.policies.model.signals.commands.PolicyCommandSizeValidator#DITTO_LIMITS_POLICIES_MAX_SIZE_BYTES</exclude>
<exclude>org.eclipse.ditto.policies.model.enforcers.PolicyEnforcers#defaultEvaluator(org.eclipse.ditto.policies.model.Policy)</exclude>
<exclude>org.eclipse.ditto.policies.model.enforcers.PolicyEnforcers#memoryOptimizedEvaluator(org.eclipse.ditto.policies.model.Policy)</exclude>
<exclude>org.eclipse.ditto.policies.model.enforcers.PolicyEnforcers#throughputOptimizedEvaluator(org.eclipse.ditto.policies.model.Policy)</exclude>
<exclude>org.eclipse.ditto.policies.model.enforcers.tree.TreeBasedPolicyEnforcer#createInstance(org.eclipse.ditto.policies.model.Policy)</exclude>
<exclude>org.eclipse.ditto.policies.model.enforcers.trie.TrieBasedPolicyEnforcer#newInstance(org.eclipse.ditto.policies.model.Policy)</exclude>
</excludes>
</parameter>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.policies.model.PolicyEntry;
import org.eclipse.ditto.policies.model.enforcers.tree.TreeBasedPolicyEnforcer;
import org.eclipse.ditto.policies.model.enforcers.trie.TrieBasedPolicyEnforcer;
import org.eclipse.ditto.policies.model.Policy;

/**
* Contains multiple implementations of {@link Enforcer}s suited for different use cases.
Expand All @@ -32,38 +32,38 @@ private PolicyEnforcers() {
* Returns a general purpose Enforcer which requires some memory and delivers very high throughput for
* most of the Policies.
*
* @param policy the Policy to initialize the evaluator with.
* @param policyEntries the Policy entries to initialize the evaluator with.
* @return the initialized general purpose Enforcer.
* @throws NullPointerException if {@code policy} is {@code null}.
* @throws NullPointerException if {@code policyEntries} is {@code null}.
*/
public static Enforcer defaultEvaluator(final Policy policy) {
return throughputOptimizedEvaluator(policy);
public static Enforcer defaultEvaluator(final Iterable<PolicyEntry> policyEntries) {
return throughputOptimizedEvaluator(policyEntries);
}

/**
* Returns a Enforcer which requires more memory (factor 2-4 more than {@link
* #memoryOptimizedEvaluator(org.eclipse.ditto.policies.model.Policy)}) but delivers very high throughput for most of the Policies, especially good
* Returns an Enforcer which requires more memory (factor 2-4 more than {@link
* #memoryOptimizedEvaluator(Iterable)}) but delivers very high throughput for most of the Policies, especially good
* for complex Policies with multiple subjects.
* <p>
* Building JsonViews has also a higher throughput with this algorithm.
*
* @param policy the Policy to initialize the evaluator with.
* @param policyEntries the Policy entries to initialize the evaluator with.
* @return the initialized throughput optimized Enforcer.
* @throws NullPointerException if {@code policy} is {@code null}.
* @throws NullPointerException if {@code policyEntries} is {@code null}.
*/
public static Enforcer throughputOptimizedEvaluator(final Policy policy) {
return TrieBasedPolicyEnforcer.newInstance(policy);
public static Enforcer throughputOptimizedEvaluator(final Iterable<PolicyEntry> policyEntries) {
return TrieBasedPolicyEnforcer.newInstance(policyEntries);
}

/**
* Returns a Enforcer which requires little memory and delivers good performance for most of the Policies.
* Returns an Enforcer which requires little memory and delivers good performance for most of the Policies.
*
* @param policy the Policy to initialize the evaluator with.
* @param policyEntries the Policy entries to initialize the evaluator with.
* @return the initialized memory optimized Enforcer.
* @throws NullPointerException if {@code policy} is {@code null}.
* @throws NullPointerException if {@code policyEntries} is {@code null}.
*/
public static Enforcer memoryOptimizedEvaluator(final Policy policy) {
return TreeBasedPolicyEnforcer.createInstance(policy);
public static Enforcer memoryOptimizedEvaluator(final Iterable<PolicyEntry> policyEntries) {
return TreeBasedPolicyEnforcer.createInstance(policyEntries);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.policies.model.EffectedPermissions;
import org.eclipse.ditto.policies.model.Permissions;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyEntry;
import org.eclipse.ditto.policies.model.Resource;
import org.eclipse.ditto.policies.model.ResourceKey;
Expand Down Expand Up @@ -71,15 +70,14 @@ private TreeBasedPolicyEnforcer(final Map<String, PolicyTreeNode> tree) {
/**
* Creates a new policy tree for execution of policy checks.
*
* @param policy the policy to create a tree for
* @param policyEntries the policy entries to create a tree for
* @return the generated {@code TreeBasedPolicyEnforcer}
* @throws NullPointerException if {@code policy} is {@code null}.
* @throws NullPointerException if {@code policyEntries} is {@code null}.
*/
public static TreeBasedPolicyEnforcer createInstance(final Policy policy) {
checkNotNull(policy, "policy");
public static TreeBasedPolicyEnforcer createInstance(final Iterable<PolicyEntry> policyEntries) {
checkNotNull(policyEntries, "policyEntries");
final Map<String, PolicyTreeNode> tree = new HashMap<>();

final Set<PolicyEntry> policyEntries = policy.getEntriesSet();
policyEntries.forEach(policyEntry -> {

final Subjects subjects = policyEntry.getSubjects();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@

import java.util.Set;

import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.policies.model.enforcers.EffectedSubjects;
import org.eclipse.ditto.policies.model.enforcers.Enforcer;
import org.eclipse.ditto.policies.model.Permissions;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyEntry;
import org.eclipse.ditto.policies.model.ResourceKey;
import org.eclipse.ditto.policies.model.enforcers.EffectedSubjects;
import org.eclipse.ditto.policies.model.enforcers.Enforcer;

/**
* Holds Algorithms to build trie-based indices for a policy and to perform policy checks based on those indices.
Expand Down Expand Up @@ -99,12 +98,12 @@ private TrieBasedPolicyEnforcer(final Iterable<PolicyEntry> policy) {
/**
* Constructs a trie-based policy enforcer from a policy.
*
* @param policy The policy to interpret.
* @param policyEntries The policy entries to interpret.
* @return The policy enforcer.
* @throws NullPointerException if {@code policy} is {@code null}.
* @throws NullPointerException if {@code policyEntries} is {@code null}.
*/
public static TrieBasedPolicyEnforcer newInstance(final Policy policy) {
return new TrieBasedPolicyEnforcer(checkNotNull(policy, "policy to interpret"));
public static TrieBasedPolicyEnforcer newInstance(final Iterable<PolicyEntry> policyEntries) {
return new TrieBasedPolicyEnforcer(checkNotNull(policyEntries, "policy entries to interpret"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@

import java.util.Set;

import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.policies.model.Permissions;
import org.eclipse.ditto.policies.model.PoliciesResourceType;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.ResourceKey;
import org.eclipse.ditto.policies.model.SubjectType;
import org.eclipse.ditto.policies.model.enforcers.EffectedSubjects;
import org.junit.Test;

/**
Expand All @@ -42,7 +41,7 @@ public final class TreeBasedPolicyEnforcerTest {
public void tryToCreateInstanceWithNullPolicy() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> TreeBasedPolicyEnforcer.createInstance(null))
.withMessage("The %s must not be null!", "policy")
.withMessage("The %s must not be null!", "policyEntries")
.withNoCause();
}

Expand Down

0 comments on commit 469c48b

Please sign in to comment.