casbin / jcasbin

An authorization library that supports access control models like ACL, RBAC, ABAC in Java
https://casbin.org
Apache License 2.0
2.4k stars 464 forks source link

NullPointerException at the Funtion "public void savePolicy(Model model) {}" in FileAdapter.java #115

Closed xiaodong2077 closed 3 years ago

xiaodong2077 commented 3 years ago

Original Code of the function

@Override
    public void savePolicy(Model model) {
        if (filePath.equals("")) {
            throw new Error("invalid file path, file path cannot be empty");
        }

        StringBuilder tmp = new StringBuilder();

        for (Map.Entry<String, Assertion> entry : model.model.get("p").entrySet()) {
            String ptype = entry.getKey();
            Assertion ast = entry.getValue();

            for (List<String> rule : ast.policy) {
                tmp.append(ptype + ", ");
                tmp.append(Util.arrayToString(rule));
                tmp.append("\n");
            }
        }

        for (Map.Entry<String, Assertion> entry : model.model.get("g").entrySet()) {
            String ptype = entry.getKey();
            Assertion ast = entry.getValue();

            for (List<String> rule : ast.policy) {
                tmp.append(ptype + ", ");
                tmp.append(Util.arrayToString(rule));
                tmp.append("\n");
            }
        }

        savePolicyFile(tmp.toString().trim());
    }

Why I meet the NPE

I choose a keymatch_model.conf and a keymatch_policy.csv. Then I try to save a policy in my project. Here is my code:

  public static boolean addPolicy(Policy policy) {
        boolean addPolicy = enforcer.addPolicy(policy.getSub(), policy.getObj(), policy.getAct());
        enforcer.savePolicy();
        return addPolicy;
    }

Policy class is a packaged class of sub、obj、act for me to use casbin more conveniently.

Everytime I call this funtion, I will meet the NPE.

Where I meet the NPE

I use breakpoint to trace where it happened. And I found it at the function I gave out at the first paragraph.

The key line is:

...
  for (Map.Entry<String, Assertion> entry : model.model.get("g").entrySet()) 
...

The model file I download, in fact, doesn't have a g part.

[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

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

[matchers]
m = r.sub == p.sub && keyMatch(r.obj, p.obj) && regexMatch(r.act, p.act)

And the usage model.model.get("g").entrySet() is not suitable for this situation since my model file lost the g part.

I don't think the g part should be forced to use because I don't use the RBAC partten and I don't need a role.

hsluoyz commented 3 years ago

@Yienlilu the latest code has fixed this issue:

https://github.com/casbin/jcasbin/blob/5ec9e3fe08f3f80e8e99e1db3953e5cb68c016f9/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java#L334-L363

You can see: if (model.model.containsKey("g")) {

Please use latest release.

xiaodong2077 commented 3 years ago

@Yienlilu the latest code has fixed this issue:

https://github.com/casbin/jcasbin/blob/5ec9e3fe08f3f80e8e99e1db3953e5cb68c016f9/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java#L334-L363

You can see: if (model.model.containsKey("g")) {

Please use latest release.

Fine. I just see the newest version is 1.5.

I am using 1.2version beacuse I see it at the website: casbin.org

https://casbin.org/docs/zh-CN/get-started

I think this need to be updated.

hsluoyz commented 3 years ago

@Yienlilu actually latest version is 1.6.0: https://github.com/casbin/jcasbin/releases

Website has been updated: https://github.com/casbin/casbin-website/commit/015df152ec1aab51d592050f79e4052fc514e823

xiaodong2077 commented 3 years ago

@Yienlilu actually latest version is 1.6.0: https://github.com/casbin/jcasbin/releases

Website has been updated: casbin/casbin-website@015df15

Thank you!