casbin / lua-casbin

An authorization library that supports access control models like ACL, RBAC, ABAC in Lua (Kong, APISIX, OpenResty)
Apache License 2.0
55 stars 18 forks source link

[Question] Is it possible to disable logs before creating the Enforcer? #155

Open mikyll opened 4 months ago

mikyll commented 4 months ago

I'm using casbin in a Custom Kong plugin, and I'm getting these log entries when loading the model and policy:

2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFOModel:
, context: ngx.timer
2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFO[r.r]:, context: ngx.timer
2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFO{RM = {}, key = "r", policy = {}, policyMap = {}, priorityIndex = -1, tokens = {"r_sub", "r_obj", "r_act"}, value = "sub, obj, act"}, context: ngx.timer
2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFO[p.p]:, context: ngx.timer
2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFO{RM = {}, key = "p", policy = {}, policyMap = {}, priorityIndex = -1, tokens = {"p_sub", "p_obj", "p_act"}, value = "sub, obj, act"}, context: ngx.timer
2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFO[e.e]:, context: ngx.timer
2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFO{RM = {}, key = "e", policy = {}, policyMap = {}, priorityIndex = -1, tokens = {}, value = "some(where (p_eft == allow))"}, context: ngx.timer
2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFO[m.m]:, context: ngx.timer
2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFO{RM = {}, key = "m", policy = {}, policyMap = {}, priorityIndex = -1, tokens = {}, value = "r_sub == p_sub && regexMatch(r_obj, p_obj) && regexMatch(r_act, p_act)"}, context: ngx.timer
2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFOPolicy:
, context: ngx.timer
2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFOp:   sub, obj, act:, context: ngx.timer
2024/05/09 14:13:02 [notice] 2441#0: *4 [lua] Log.lua:29: info(): INFO{my-list-of-policies}, context: ngx.timer

Goal: I'd like to enable/disable these log messages, based on a plugin configuration parameter (conf.debug).

I see that there is a enableLog() method, but is there a way to disable logs before creating the enforcer itself?

My code is basically this:

local CustomHandler = {
  NAME = "my-plugin",
  PRIORITY = 1000,
  VERSION = "0.1",
}

local casbin = require("casbin")

function CustomHandler:access(conf)
  local enforcer = casbin:new(conf.model_path, conf.policy_path)

  enforcer:enableLogs(conf.debug)

  -- [...]
end

return CustomHandler
casbin-bot commented 4 months ago

@Edmond-J-A @rushitote @techoner

hsluoyz commented 4 months ago

@mikyll why do you want to do this? Can't turn off after creation?

mikyll commented 4 months ago

@hsluoyz Since we have hundreds if not thousands of policy entries, I would like to make that logging entires configurable, in order to make it more readable. Moreover, in our plugin, the enforcer reloads the policies each time the plugin configuration changes, and that would add a lot of logging entries, making it harder to debug Kong-related issues

In short, it's just a matter of preference, so I'd just like to know if it's possible, and in case how 🙂

hsluoyz commented 4 months ago

@mikyll see how Go Casbin does this: https://github.com/casbin/casbin