-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[request_definition] | ||
r = sub, domain, obj, act | ||
|
||
[policy_definition] | ||
p = sub_rule, sub, dom, obj, act | ||
|
||
[role_definition] | ||
g = _, _, _ | ||
|
||
[policy_effect] | ||
e = some(where (p.eft == allow)) | ||
|
||
[matchers] | ||
m = eval(p.sub_rule) && g(r.sub, p.sub, r.domain) && r.obj == p.obj && r.act == p.act |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
p, r.domain == 'domain1', admin, domain1, data1, read | ||
p, r.domain == 'domain1', admin, domain1, data1, write | ||
p, r.domain == 'domain2', admin, domain2, data2, read | ||
p, r.domain == 'domain2', admin, domain2, data2, write | ||
g, alice, admin, domain1 | ||
g, bob, admin, domain2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[request_definition] | ||
r = sub, obj, act | ||
|
||
[policy_definition] | ||
p = sub, obj, act | ||
|
||
[role_definition] | ||
g = _, _ | ||
|
||
[policy_effect] | ||
e = some(where (p.eft == allow)) | ||
|
||
[matchers] | ||
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
p, alice, data1, read | ||
p, bob, data2, write | ||
p, data2_admin, data2, read | ||
p, data2_admin, data2, write | ||
g, alice, data2_admin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.casbin</groupId> | ||
<artifactId>casbin-java-cli</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<slf4j.version>1.7.36</slf4j.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.casbin</groupId> | ||
<artifactId>jcasbin</artifactId> | ||
<version>1.55.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>commons-cli</groupId> | ||
<artifactId>commons-cli</artifactId> | ||
<version>1.4</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>${slf4j.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<version>${slf4j.version}</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.casbin; | ||
|
||
import org.apache.commons.cli.*; | ||
import org.casbin.jcasbin.main.Enforcer; | ||
|
||
public class Client { | ||
public static void main(String[] args) { | ||
try { | ||
Object res = clientEnforce(args); | ||
System.out.println(res); | ||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static Object clientEnforce(String[] args) throws ParseException { | ||
Options options = new Options(); | ||
Option model = new Option("m", "model", true, "the path of the model file"); | ||
options.addOption(model); | ||
Option config = new Option("p", "policy", true, "the path of the policy file"); | ||
options.addOption(config); | ||
Option enforceCMD = new Option("e", "enforce", true, "enforce"); | ||
options.addOption(enforceCMD); | ||
Option enforceExCMD = new Option("ex", "enforceEx", true, "enforceEx"); | ||
options.addOption(enforceExCMD); | ||
CommandLineParser parser = new DefaultParser(); | ||
CommandLine cmd = parser.parse(options, args); | ||
String modelPath = cmd.getOptionValue("model"); | ||
String policyFile = cmd.getOptionValue("policy"); | ||
Enforcer e = new Enforcer(modelPath, policyFile); | ||
if (cmd.hasOption("enforce")) { | ||
String enforceArgs = cmd.getOptionValue("enforce"); | ||
return e.enforce(enforceArgs.split(",")); | ||
} else if (cmd.hasOption("enforceEx")) { | ||
String enforceExArgs = cmd.getOptionValue("enforceEx"); | ||
return e.enforceEx(enforceExArgs.split(",")); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.casbin; | ||
|
||
import org.apache.commons.cli.ParseException; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ClientTest { | ||
|
||
@Test | ||
public void testRBAC() throws ParseException { | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data1,read"}), true); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data1,write"}), false); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data2,read"}), true); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","alice,data2,write"}), true); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data1,read"}), false); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data1,write"}), false); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data2,read"}), false); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/rbac_model.conf","-p","examples/rbac_policy.csv","-e","bob,data2,write"}), true); | ||
} | ||
|
||
@Test | ||
public void testABAC() throws ParseException { | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","alice,domain1,data1,read"}), true); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","alice,domain1,data1,write"}), true); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","alice,domain2,data1,read"}), false); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","alice,domain2,data1,write"}), false); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","bob,domain1,data2,read"}), false); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","bob,domain1,data2,write"}), false); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","bob,domain2,data2,read"}), true); | ||
assertEquals(Client.clientEnforce(new String[]{"-m","examples/abac_rule_with_domains_model.conf","-p","examples/abac_rule_with_domains_policy.csv","-e","bob,domain2,data2,read"}), true); | ||
} | ||
|
||
} |