authz-casbin

Description

The authz-casbin Plugin is an authorization Plugin based on Lua Casbin. This Plugin supports powerful authorization scenarios based on various access control models.

Attributes

NameTypeRequiredDescription
model_pathstringTruePath of the Casbin model configuration file.
policy_pathstringTruePath of the Casbin policy file.
modelstringTrueCasbin model configuration in text format.
policystringTrueCasbin policy in text format.
usernamestringTrueHeader in the request that will be used in the request to pass the username (subject).
authz-casbin - 图1note

You must either specify the model_path, policy_path, and the username attributes or specify the model, policy and the username attributes in the Plugin configuration for it to be valid.

If you wish to use a global Casbin configuration, you can first specify model and policy attributes in the Plugin metadata and only the username attribute in the Plugin configuration. All Routes will use the Plugin configuration this way.

Metadata

NameTypeRequiredDescription
modelstringTrueCasbin model configuration in text format.
policystringTrueCasbin policy in text format.

Enabling the Plugin

You can enable the Plugin on a Route by either using the model/policy file paths or using the model/policy text in Plugin configuration/metadata.

By using model/policy file paths

The example below shows setting up Casbin authentication from your model/policy configuration file:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "authz-casbin": {
  5. "model_path": "/path/to/model.conf",
  6. "policy_path": "/path/to/policy.csv",
  7. "username": "user"
  8. }
  9. },
  10. "upstream": {
  11. "nodes": {
  12. "127.0.0.1:1980": 1
  13. },
  14. "type": "roundrobin"
  15. },
  16. "uri": "/*"
  17. }'

By using model/policy text in Plugin configuration

The example below shows setting up Casbin authentication from your model/policy text in your Plugin configuration:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "authz-casbin": {
  5. "model": "[request_definition]
  6. r = sub, obj, act
  7. [policy_definition]
  8. p = sub, obj, act
  9. [role_definition]
  10. g = _, _
  11. [policy_effect]
  12. e = some(where (p.eft == allow))
  13. [matchers]
  14. m = (g(r.sub, p.sub) || keyMatch(r.sub, p.sub)) && keyMatch(r.obj, p.obj) && keyMatch(r.act, p.act)",
  15. "policy": "p, *, /, GET
  16. p, admin, *, *
  17. g, alice, admin",
  18. "username": "user"
  19. }
  20. },
  21. "upstream": {
  22. "nodes": {
  23. "127.0.0.1:1980": 1
  24. },
  25. "type": "roundrobin"
  26. },
  27. "uri": "/*"
  28. }'

By using model/policy text in Plugin metadata

First, you need to send a PUT request to the Admin API to add the model and policy text to the Plugin metadata.

All Routes configured this way will use a single Casbin enforcer with the configured Plugin metadata. You can also update the model/policy in this way and the Plugin will automatically update to the new configuration.

  1. curl http://127.0.0.1:9080/apisix/admin/plugin_metadata/authz-casbin -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -i -X PUT -d '
  2. {
  3. "model": "[request_definition]
  4. r = sub, obj, act
  5. [policy_definition]
  6. p = sub, obj, act
  7. [role_definition]
  8. g = _, _
  9. [policy_effect]
  10. e = some(where (p.eft == allow))
  11. [matchers]
  12. m = (g(r.sub, p.sub) || keyMatch(r.sub, p.sub)) && keyMatch(r.obj, p.obj) && keyMatch(r.act, p.act)",
  13. "policy": "p, *, /, GET
  14. p, admin, *, *
  15. g, alice, admin"
  16. }'

Once you have updated the Plugin metadata, you can add the Plugin to a specific Route:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "authz-casbin": {
  5. "username": "user"
  6. }
  7. },
  8. "upstream": {
  9. "nodes": {
  10. "127.0.0.1:1980": 1
  11. },
  12. "type": "roundrobin"
  13. },
  14. "uri": "/*"
  15. }'
authz-casbin - 图2note

The Plugin Route configuration has a higher precedence than the Plugin metadata configuration. If the model/policy configuration is present in the Plugin Route configuration, it is used instead of the metadata configuration.

Example usage

We define the example model as:

  1. [request_definition]
  2. r = sub, obj, act
  3. [policy_definition]
  4. p = sub, obj, act
  5. [role_definition]
  6. g = _, _
  7. [policy_effect]
  8. e = some(where (p.eft == allow))
  9. [matchers]
  10. m = (g(r.sub, p.sub) || keyMatch(r.sub, p.sub)) && keyMatch(r.obj, p.obj) && keyMatch(r.act, p.act)

And the example policy as:

  1. p, *, /, GET
  2. p, admin, *, *
  3. g, alice, admin

See examples for more policy and model configurations.

The above configuration will let anyone access the homepage (/) using a GET request while only users with admin permissions can access other pages and use other request methods.

So if we make a get request to the homepage:

  1. curl -i http://127.0.0.1:9080/ -X GET

But if an unauthorized user tries to access any other page, they will get a 403 error:

  1. curl -i http://127.0.0.1:9080/res -H 'user: bob' -X GET
  2. HTTP/1.1 403 Forbidden

And only users with admin privileges can access the endpoints:

  1. curl -i http://127.0.0.1:9080/res -H 'user: alice' -X GET

Disable Plugin

To disable the authz-casbin Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/*",
  5. "plugins": {},
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:1980": 1
  10. }
  11. }
  12. }'