From 0c4e03230e0f9bcf4f66f60bc5c8f88853475bd7 Mon Sep 17 00:00:00 2001 From: JackYifan <1779626055@qq.com> Date: Mon, 17 Jun 2024 20:25:52 +0800 Subject: [PATCH 1/2] feat: support CLI usage for jCasbin --- pom.xml | 28 ++++++++++++++- .../java/org/casbin/jcasbin/cli/Client.java | 36 +++++++++++++++++++ .../org/casbin/jcasbin/main/ClientTest.java | 35 ++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/casbin/jcasbin/cli/Client.java create mode 100644 src/test/java/org/casbin/jcasbin/main/ClientTest.java diff --git a/pom.xml b/pom.xml index 82eb2120..7c030fbd 100644 --- a/pom.xml +++ b/pom.xml @@ -164,6 +164,27 @@ + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + org.casbin.jcasbin.cli.Client + + + + + + @@ -177,7 +198,7 @@ org.slf4j slf4j-simple ${slf4j.version} - test + compile junit @@ -234,6 +255,11 @@ jackson-databind 2.16.1 + + commons-cli + commons-cli + 1.4 + diff --git a/src/main/java/org/casbin/jcasbin/cli/Client.java b/src/main/java/org/casbin/jcasbin/cli/Client.java new file mode 100644 index 00000000..70f80509 --- /dev/null +++ b/src/main/java/org/casbin/jcasbin/cli/Client.java @@ -0,0 +1,36 @@ +package org.casbin.jcasbin.cli; + +import org.apache.commons.cli.*; +import org.casbin.jcasbin.main.Enforcer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Client { + private static final Logger log = LoggerFactory.getLogger(Client.class); + + public static void main(String[] args) { + try { + boolean res = clientEnforce(args); + log.info("Result: {}", res); + } catch (ParseException e) { + log.error(e.getMessage()); + } + } + + public static boolean 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); + 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); + String enforce = cmd.getOptionValue("enforce"); + return e.enforce(enforce.split(",")); + } +} diff --git a/src/test/java/org/casbin/jcasbin/main/ClientTest.java b/src/test/java/org/casbin/jcasbin/main/ClientTest.java new file mode 100644 index 00000000..97328de6 --- /dev/null +++ b/src/test/java/org/casbin/jcasbin/main/ClientTest.java @@ -0,0 +1,35 @@ +package org.casbin.jcasbin.main; + +import org.apache.commons.cli.ParseException; +import org.casbin.jcasbin.cli.Client; +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); + } + +} From 19420e248207c06bba7571d4c0c0ce158ce640db Mon Sep 17 00:00:00 2001 From: JackYifan <1779626055@qq.com> Date: Sat, 29 Jun 2024 21:43:56 +0800 Subject: [PATCH 2/2] feat: output the result on the console --- src/main/java/org/casbin/jcasbin/cli/Client.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/casbin/jcasbin/cli/Client.java b/src/main/java/org/casbin/jcasbin/cli/Client.java index 70f80509..40cffc99 100644 --- a/src/main/java/org/casbin/jcasbin/cli/Client.java +++ b/src/main/java/org/casbin/jcasbin/cli/Client.java @@ -2,18 +2,15 @@ import org.apache.commons.cli.*; import org.casbin.jcasbin.main.Enforcer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class Client { - private static final Logger log = LoggerFactory.getLogger(Client.class); public static void main(String[] args) { try { boolean res = clientEnforce(args); - log.info("Result: {}", res); + System.out.println(res); } catch (ParseException e) { - log.error(e.getMessage()); + e.printStackTrace(); } }