jcasbin / jdbc-adapter

JDBC adapter for Casbin
https://github.com/casbin/jcasbin
Apache License 2.0
34 stars 38 forks source link

Question/Bug: savePolicy fails with NPE for ACL model #64

Closed ewesten closed 1 year ago

ewesten commented 1 year ago

Hello,

I am new to JCasbin, so apolologies if this is a rookie mistake.

Executing this code

String s = """
                # Request definition
                [request_definition]
                r = sub, obj, act

                # Policy definition
                [policy_definition]
                p = sub, obj, act

                # Policy effect
                [policy_effect]
                e = some(where (p.eft == allow))

                # Matchers
                [matchers]
                m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
                """;

Model model = newModel(s);
InitialContext context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup(DB_ALIAS);
Adapter adapter = new JDBCAdapter(dataSource);
Enforcer e = new Enforcer(model, adapter);
e.addPolicy("foo", "bar", "read");
e.savePolicy();

fails with a NPE

 java.lang.NullPointerException: Cannot invoke "java.util.Map.entrySet()" because the return value of "java.util.Map.get(Object)" is null
 at org.casbin.adapter.JDBCBaseAdapter.lambda$savePolicy$1(JDBCBaseAdapter.java:299)
 at dev.failsafe.Functions.lambda$toCtxSupplier$10(Functions.java:236)
 at dev.failsafe.Functions.lambda$get$0(Functions.java:46)
 at dev.failsafe.internal.RetryPolicyExecutor.lambda$apply$0(RetryPolicyExecutor.java:74)
 at dev.failsafe.SyncExecutionImpl.executeSync(SyncExecutionImpl.java:193)
 at dev.failsafe.FailsafeExecutor.call(FailsafeExecutor.java:376)
 at dev.failsafe.FailsafeExecutor.run(FailsafeExecutor.java:231)
at org.casbin.adapter.JDBCBaseAdapter.savePolicy(JDBCBaseAdapter.java:265)

The NPE occurs at https://github.com/jcasbin/jdbc-adapter/blob/master/src/main/java/org/casbin/adapter/JDBCBaseAdapter.java#L299 because model.model.get("g") is null.

Shouldn't the code handle the different authorization model types? (the model above is the default ACL model). Or am I missing something?

Regards, Eric

casbin-bot commented 1 year ago

@tangyang9464 @imp2002