Repository navigation
How can I capture an authorization change as a portable event in Casbin? #1764
|
Hello Casbin community, I’m validating a vendor-neutral approach for representing an authorization change as: BEFORE → authorization state I’m looking for one small, controlled, non-production example using Casbin. For example, a simple policy change where we can observe the authorization state before and after the modification, trigger an authorization decision, and identify the corresponding policy state and evidence. No production access, credentials, or customer data are required. The goal is to validate whether this transition can be represented as a portable authorization-change event. What would be the recommended way to capture the BEFORE/CHANGE/AFTER state together with the corresponding authorization decision and policy evidence in Casbin? Thank you, |
Replies: 1 comment
|
Casbin has the pieces for this built in: read the policy before and after, apply the change through the management API, and use import copy, json
from datetime import datetime, timezone
import casbin
# model.conf: RBAC (r = sub, obj, act; g = _, _; m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act)
# policy.csv:
# p, editor, report, write
# g, alice, viewer
e = casbin.Enforcer("model.conf", "policy.csv")
request = ("alice", "report", "write")
def snapshot():
# get_policy()/get_grouping_policy() return Casbin's live lists, so copy them
return copy.deepcopy({"policies": e.get_policy(), "groupings": e.get_grouping_policy()})
def decide():
allowed, explain = e.enforce_ex(*request)
return {"request": list(request), "allowed": allowed, "matched_rule": explain}
before = {"state": snapshot(), "decision": decide()}
change = {"op": "add_grouping_policy", "rule": ["alice", "editor"]}
e.add_grouping_policy(*change["rule"])
after = {"state": snapshot(), "decision": decide()}
print(json.dumps({"at": datetime.now(timezone.utc).isoformat(),
"before": before, "change": change, "after": after}, indent=2))Running it gives:
One gotcha I hit while testing: without the The same calls exist in Go ( |
Casbin has the pieces for this built in: read the policy before and after, apply the change through the management API, and use
enforce_ex(EnforceExin Go) as the evidence, since it returns the decision and the policy rule that produced it. Here's a minimal, self-contained example with pycasbin (1.43.0):