ABAC
什么是ABAC模式?
ABAC是 基于属性的访问控制
,可以使用主体、客体或动作的属性,而不是字符串本身来控制访问。 您之前可能就已经听过 XACML ,是一个复杂的 ABAC 访问控制语言。 与XACML相比,Casbin的ABAC非常简单: 在ABAC中,可以使用struct(或基于编程语言的类实例) 而不是字符串来表示模型元素。
例如,ABAC的官方实例如下:
[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
我们在 matcher
中使用 r.obj.Owner
代替 r.obj
。 在 Enforce()
函数中传递的 r.obj
函数是结构或类实例,而不是字符串。 Casbin将使用映像来检索 obj
结构或类中的成员变量。
这里是 r.obj
construction 或 class 的定义:
type testResource struct {
Name string
Owner string
}
如何使用ABAC?
简单地说,要使用ABAC,您需要做两件事:
- 在模型匹配器中指定属性。
- 将元素的结构或类实例作为Casbin的
Enforce()
的参数传入。
warning
目前,仅有形如r.sub
, r.obj
, r.act
等请求元素支持ABAC。 您不能在policy元素上使用它,比如p.sub
,因为在Casbin的policy中没有定义结构或者类。
您可以在匹配器中使用多个ABAC属性,例如:m = r.sub.Domain == r.obj.Domain
。
:::
适配复杂且大量的ABAC规则
上述ABAC实施实例的核心非常简单。 但授权系统通常需要非常复杂和大量的ABAC规则。 To fit this necessity the above implementation will increase the verbosity of the model to a large extent. So, it’s wise to add the rules in the policy instead of in the model. This is done by introducing a eval()
functional construct. Below is the example instance to manage such ABAC models.
This is the definition of the CONF
file used for defining 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
Here, p.sub_rule
is of type struct or class(user-defined type) which consists of necessary attributes to be used in the policy.
This is the policy that is used against the model for Enforcement
. Now, you can use the object instance which is 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