Ir al contenido principal

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 will use reflection to retrieve the obj member variable in that struct or class for you.

Here is a definition for the r.obj struct or 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")
nota

Enabling the function of accepting JSON parameters may result in a performance drop of 1.1 to 1.5 times.

How to use ABAC?

To use ABAC, you need to do two things:

  1. Specify the attributes in the model matcher.
  2. Pass in the struct or class instance for the element as an argument to Casbin's Enforce() function.
peligro

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.

consejo

You can use multiple ABAC attributes in a matcher. For example: m = r.sub.Domain == r.obj.Domain.

consejo

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. For example, "keyMatch("bob", r.sub.Role)" will not be split.

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.

This is the policy that is used against the model for 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