ABAC
What is the ABAC model?
ABAC stands for Attribute-Based Access Control. It allows you to control access by using the attributes (properties) of the subject, object, or action instead of using the string values themselves. You may have heard of a complicated ABAC access control language called XACML. Casbin's ABAC, on the other hand, is much simpler. In Casbin's ABAC, you can use structs or class instances instead of strings for model elements.
Let's take a look at the official ABAC example:
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == r.obj.Owner
In the matcher, we use r.obj.Owner
instead of r.obj
. The r.obj
passed in the Enforce()
function will be a struct or class instance rather than a string. Casbin将使用映像来检索 obj
结构或类中的成员变量。
这里是 r.obj
construction 或 class 的定义:
type testResource struct {
Name string
Owner string
}
If you want to pass parameters to the enforcer through JSON, you need to enable the function with e.EnableAcceptJsonRequest(true)
.
For example:
e, _ := NewEnforcer("examples/abac_model.conf")
e.EnableAcceptJsonRequest(true)
data1Json := `{ "Name": "data1", "Owner": "bob"}`
ok, _ := e.Enforce("alice", data1Json, "read")
Enabling the function of accepting JSON parameters may result in a performance drop of 1.1 to 1.5 times.
如何使用ABAC?
To use ABAC, you need to do two things:
- 在模型匹配器中指定属性。
- Pass in the struct or class instance for the element as an argument to Casbin's
Enforce()
function.
Currently, only request elements like r.sub
, r.obj
, r.act
, and so on support ABAC. You cannot use it on policy elements like p.sub
because there is no way to define a struct or class in Casbin's policy.
You can use multiple ABAC attributes in a matcher. For example: m = r.sub.Domain == r.obj.Domain
.
If you need to use a comma in a policy that conflicts with CSV's separator, you can escape it by surrounding the statement with quotation marks. 例如, "keyMatch("bob", r.sub.Role)"
将不会被视为csv文件分割。
Scaling the model for complex and large numbers of ABAC rules
The above implementation of the ABAC model is simple at its core. However, in many cases, the authorization system requires a complex and large number of ABAC rules. To accommodate this requirement, it is recommended to add the rules in the policy instead of the model. This can be done by introducing an eval()
functional construct. Here is an example:
This is the definition of the CONF
file used to define the ABAC model.
[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
In this example, p.sub_rule
is a struct or class (user-defined type) that contains the necessary attributes to be used in the policy.
这是针对Enforcement
模型使用的策略 Now, you can use the object instance passed to eval()
as a parameter to define certain ABAC constraints.
p, r.sub.Age > 18, /data1, read
p, r.sub.Age < 60, /data2, write