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

Add support for policies printing in World #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -2,6 +2,7 @@

import com.clevercloud.biscuit.crypto.TokenSignature;
import com.clevercloud.biscuit.datalog.expressions.Expression;
import com.clevercloud.biscuit.token.Policy;
import com.clevercloud.biscuit.token.builder.Utils;
import io.vavr.control.Option;

Expand Down Expand Up @@ -186,9 +187,14 @@ public String print_check(final Check c) {
return res + String.join(" or ", queries);
}

public String print_policy(final Policy p) {
return String.join(" or ", p.print(this));
}

public String print_world(final World w) {
final List<String> facts = w.facts().stream().map((f) -> this.print_fact(f)).collect(Collectors.toList());
final List<String> rules = w.rules().stream().map((r) -> this.print_rule(r)).collect(Collectors.toList());
final List<String> policies = w.policies().stream().map((p) -> this.print_policy(p)).collect(Collectors.toList());

StringBuilder b = new StringBuilder();
b.append("World {\n\tfacts: [\n\t\t");
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/clevercloud/biscuit/datalog/World.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.clevercloud.biscuit.datalog;

import com.clevercloud.biscuit.error.Error;
import com.clevercloud.biscuit.token.Policy;

import java.io.Serializable;
import java.time.Instant;
Expand All @@ -13,6 +14,7 @@
public class World implements Serializable {
private final Set<Fact> facts;
private final List<Rule> rules;
private final List<Policy> policies;

public void add_fact(final Fact fact) {
this.facts.add(fact);
Expand All @@ -26,6 +28,10 @@ public void add_rule(final Rule rule) {
this.rules.add(rule);
}

public void add_policy(final Policy policy) {
this.policies.add(policy);
}

public void clearRules() {
this.rules.clear();
}
Expand Down Expand Up @@ -72,6 +78,8 @@ public final Set<Fact> facts() {

public List<Rule> rules() { return this.rules; }

public List<Policy> policies() { return this.policies; }

public final Set<Fact> query(final Predicate pred) {
return this.facts.stream().filter((f) -> {
if (f.predicate().name() != pred.name()) {
Expand Down Expand Up @@ -108,22 +116,32 @@ public final boolean query_match(final Rule rule, SymbolTable symbols) {
public World() {
this.facts = new HashSet<>();
this.rules = new ArrayList<>();
this.policies = new ArrayList<>();
}

public World(Set<Fact> facts) {
this.facts = new HashSet<>();
this.facts.addAll(facts);
this.rules = new ArrayList<>();
this.policies = new ArrayList<>();
}

public World(Set<Fact> facts, List<Rule> rules) {
this.facts = facts;
this.rules = rules;
this.policies = new ArrayList<>();
}

public World(Set<Fact> facts, List<Rule> rules, List<Check> checks) {
this.facts = facts;
this.rules = rules;
this.policies = new ArrayList<>();
}

public World(Set<Fact> facts, List<Rule> rules, List<Check> checks, List<Policy> policies) {
this.facts = facts;
this.rules = rules;
this.policies = policies;
}

public World(World w) {
Expand All @@ -137,6 +155,11 @@ public World(World w) {
this.rules.add(rule);
}

this.policies = new ArrayList<>();
for(Policy policy: w.policies) {
this.policies.add(policy);
}

}

public String print(SymbolTable symbol_table) {
Expand All @@ -154,6 +177,12 @@ public String print(SymbolTable symbol_table) {
s.append(symbol_table.print_rule(r));
}

s.append("\n\t\t]\n\t\tpolicies: [");
for(Policy policy: this.policies) {
s.append("\n\t\t\t");
s.append(symbol_table.print_policy(policy));
}

s.append("\n\t\t]\n\t}");

return s.toString();
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/clevercloud/biscuit/token/Authorizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ public Authorizer allow() {
Arrays.asList(new Expression.Value(new Term.Bool(true)))
));

this.policies.add(new Policy(q, Policy.Kind.Allow));
Policy policy = new Policy(q, Policy.Kind.Allow);
this.policies.add(policy);
world.add_policy(policy);
return this;
}

Expand All @@ -214,7 +216,9 @@ public Authorizer deny() {
Arrays.asList(new Expression.Value(new Term.Bool(true)))
));

this.policies.add(new Policy(q, Policy.Kind.Deny));
Policy policy = new Policy(q, Policy.Kind.Deny);
this.policies.add(policy);
world.add_policy(policy);
return this;
}

Expand All @@ -229,11 +233,13 @@ public Authorizer add_policy(String s) throws Error.Parser {
Tuple2<String, com.clevercloud.biscuit.token.Policy> t = res.get();

this.policies.add(t._2);
world.add_policy(t._2);
return this;
}

public Authorizer add_policy(Policy p) {
this.policies.add(p);
world.add_policy(p);
return this;
}

Expand Down Expand Up @@ -452,6 +458,7 @@ public Tuple2<Long, AuthorizedWorld> authorize(RunLimits limits) throws Error.Ti
public String print_world() {
final List<String> facts = this.world.facts().stream().map((f) -> this.symbols.print_fact(f)).collect(Collectors.toList());
final List<String> rules = this.world.rules().stream().map((r) -> this.symbols.print_rule(r)).collect(Collectors.toList());
final List<String> policies = this.world.policies().stream().map((p) -> this.symbols.print_policy(p)).collect(Collectors.toList());

List<String> checks = new ArrayList<>();

Expand Down Expand Up @@ -480,6 +487,8 @@ public String print_world() {
b.append(String.join(",\n\t\t", rules));
b.append("\n\t],\n\tchecks: [\n\t\t");
b.append(String.join(",\n\t\t", checks));
b.append("\n\t],\n\tpolicies: [\n\t\t");
b.append(String.join(",\n\t\t", policies));
b.append("\n\t]\n}");

return b.toString();
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/clevercloud/biscuit/token/Policy.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Policy {
public enum Kind {
Expand Down Expand Up @@ -41,9 +42,20 @@ public com.clevercloud.biscuit.datalog.Check convert(SymbolTable symbols) {
public String toString() {
switch(this.kind) {
case Allow:
return "allow if "+queries;
return "allow if "+ queries;
case Deny:
return "deny if "+queries;
return "deny if " + queries;
}
return null;
}

public String print(SymbolTable symbolTable) {
String formattedQueries = queries.stream().map((q) -> symbolTable.print_rule(q.convert(symbolTable))).collect(Collectors.joining());
switch (this.kind) {
case Allow:
return "allow if " + formattedQueries;
case Deny:
return "deny if " + formattedQueries;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
import static com.clevercloud.biscuit.token.builder.Utils.*;

public class AuthorizerTest {

@Test
public void testAuthorizerPolicy() throws Parser {
Authorizer authorizer = new Authorizer();
List<Policy> policies = authorizer.policies;
authorizer.deny();
assertEquals(1, policies.size());
assertEquals(policies.size(), authorizer.world.policies().size());

authorizer.add_policy(new Policy(
Arrays.asList(
Expand All @@ -32,8 +33,10 @@ public void testAuthorizerPolicy() throws Parser {
)
), Policy.Kind.Deny));
assertEquals(2, policies.size());
assertEquals(policies.size(), authorizer.world.policies().size());

authorizer.add_policy("deny if true");
authorizer.add_policy("allow if true");
assertEquals(3, policies.size());
assertEquals(policies.size(), authorizer.world.policies().size());
}
}