casbin / casbin-rs

An authorization library that supports access control models like ACL, RBAC, ABAC in Rust.
https://casbin.org
Apache License 2.0
861 stars 74 forks source link

Question About ABAC Usage And Debug #324

Closed hxndg closed 10 months ago

hxndg commented 10 months ago

I write a simle code to test abac usage, but it keep telling me somethin of "failed to user enforcer enforce Casbin Evaluation Error: ErrorMismatchDataType("bool", "()", 1:1)" I add the enable_log to trace debug, but rust compiler tell me this function does not exist, so how should i debug it ?

let rt = runtime::Runtime::new().unwrap();
        let model_path = "reverse_abac_model.conf";
        let result = rt.block_on(async {
            Enforcer::new(model_path, ()).await
        });
        match result {
            Ok(real_enforcer) => {
                // real_enforcer.enable_log(true);
                match real_enforcer.enforce(("a","b","c")) {
                    Ok(unauthorized) => {
                        panic! ("u got be kidding")
                    },
                    Err(e) => panic!("failed to user enforcer enforce {e}"),
                }
                ...
            },
            Err(e) => panic!("failed to init default model, {e}"),
        }
hsluoyz commented 10 months ago

@hxndg try your model in editor first: https://editor.casbin.org/

hxndg commented 10 months ago

@hxndg try your model in editor first: https://editor.casbin.org/

model file is same with editor "abac with policy" default model, I just paste it from editor. Is there any way to show casbin-rs enforcer process ? enable_log function not find

[request_definition] r = sub, obj, act

[policy_definition] p = sub_rule, obj, act

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

[matchers] m = eval(p.sub_rule) && r.obj == p.obj && r.act == p.act

hsluoyz commented 10 months ago

@hxndg try to run this ABAC 1 test case, it runs well at my side:

image

https://github.com/casbin/casbin-rs/blob/7a6bedbe4dbf1b255ae96fb14d6753b476a2660b/src/enforcer.rs#L1441-L1497

hxndg commented 10 months ago

@hxndg try to run this ABAC 1 test case, it runs well at my side:

image

https://github.com/casbin/casbin-rs/blob/7a6bedbe4dbf1b255ae96fb14d6753b476a2660b/src/enforcer.rs#L1441-L1497

Thanks for your reply, I change the code to import a default policy file, all things seems alright then. I guess should not use below code to start a new enforcer without any policy/adapter added.

Enforcer::new(model_path, ()).await