Skip to main content
Version: Next

wolf-rbac

描述#

wolf-rbac 插件为 role-based access control 系统提供了添加 wolf 到 Route 或 Service 的功能。此插件需要与 Consumer 一起使用。

属性#

名称类型必选项默认值描述
serverstring"http://127.0.0.1:12180"wolf-server 的服务地址。
appidstring"unset"wolf-console 中已经添加的应用 id。
header_prefixstring"X-"自定义 HTTP 头的前缀。wolf-rbac 在鉴权成功后,会在请求头 (用于传给后端) 及响应头 (用于传给前端) 中添加 3 个 header:X-UserId, X-Username, X-Nickname

接口#

该插件在启用时将会增加以下接口:

  • /apisix/plugin/wolf-rbac/login
  • /apisix/plugin/wolf-rbac/change_pwd
  • /apisix/plugin/wolf-rbac/user_info
note

以上接口需要通过 public-api 插件暴露。

前提条件#

如果要使用这个插件,你必须要安装 wolf 并启动它。

完成后,你需要添加applicationadminregular userpermissionresource 等字段,并将用户授权到 wolf-console

启用插件#

首先需要创建一个 Consumer 并配置该插件,如下所示:

curl http://127.0.0.1:9080/apisix/admin/consumers  \-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{  "username":"wolf_rbac",  "plugins":{    "wolf-rbac":{      "server":"http://127.0.0.1:12180",      "appid":"restful"    }  },  "desc":"wolf-rbac"}'
note

示例中填写的 appid,必须是已经在 wolf 控制台中存在的。

然后你需要添加 wolf-rbac 插件到 Route 或 Service 中。

curl http://127.0.0.1:9080/apisix/admin/routes/1  \-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "methods": ["GET"],    "uri": "/*",    "plugins": {        "wolf-rbac": {}    },    "upstream": {        "type": "roundrobin",        "nodes": {            "www.baidu.com:80": 1        }    }}'

你还可以通过 APISIX Dashboard 的 Web 界面完成上述操作。

测试插件#

你可以使用 public-api 插件来暴露 API.

curl http://127.0.0.1:9080/apisix/admin/routes/wal \-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "uri": "/apisix/plugin/wolf-rbac/login",    "plugins": {        "public-api": {}    }}'

同样,你需要参考上述命令为 change_pwduser_info 两个 API 配置路由。

现在你可以登录并获取 wolf rbac_token

curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/login -i \-H "Content-Type: application/json" \-d '{"appid": "restful", "username":"test", "password":"user-password", "authType":1}'
HTTP/1.1 200 OKDate: Wed, 24 Jul 2019 10:33:31 GMTContent-Type: text/plainTransfer-Encoding: chunkedConnection: keep-aliveServer: APISIX web server{"rbac_token":"V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts","user_info":{"nickname":"test","username":"test","id":"749"}}
note

上述示例中,appidusernamepassword 必须为 wolf 系统中真实存在的。

authType 为认证类型,1 为密码认证(默认),2 为 LDAP 认证。wolf 从 0.5.0 版本开始支持了 LDAP 认证。

也可以使用 x-www-form-urlencoded 方式登陆:

curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/login -i \-H "Content-Type: application/x-www-form-urlencoded" \-d 'appid=restful&username=test&password=user-password'

现在开始测试 Route:

  • 缺少 token
curl http://127.0.0.1:9080/ -H"Host: www.baidu.com" -i
HTTP/1.1 401 Unauthorized...{"message":"Missing rbac token in request"}
  • token 放到请求头 (Authorization) 中:
curl http://127.0.0.1:9080/ -H"Host: www.baidu.com" \-H 'Authorization: V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts' -i
HTTP/1.1 200 OK
<!DOCTYPE html>
  • token 放到请求头 (x-rbac-token) 中:
curl http://127.0.0.1:9080/ -H"Host: www.baidu.com" \-H 'x-rbac-token: V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts' -i
HTTP/1.1 200 OK
<!DOCTYPE html>
  • token 放到请求参数中:
curl 'http://127.0.0.1:9080?rbac_token=V1%23restful%23eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts' -H"Host: www.baidu.com" -i
HTTP/1.1 200 OK
<!DOCTYPE html>
  • token 放到 cookie 中:
curl http://127.0.0.1:9080 -H"Host: www.baidu.com" \--cookie x-rbac-token=V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts -i
HTTP/1.1 200 OK
<!DOCTYPE html>
  • 获取用户信息:
curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/user_info \--cookie x-rbac-token=V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts -i
HTTP/1.1 200 OK{    "user_info":{        "nickname":"test",        "lastLogin":1582816780,        "id":749,        "username":"test",        "appIDs":["restful"],        "manager":"none",        "permissions":{"USER_LIST":true},        "profile":null,        "roles":{},        "createTime":1578820506,        "email":""    }}
  • 更改用户的密码:
curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/change_pwd \-H "Content-Type: application/json" \--cookie x-rbac-token=V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts -i \-X PUT -d '{"oldPassword": "old password", "newPassword": "new password"}'
HTTP/1.1 200 OK{"message":"success to change password"}

禁用插件#

当你需要禁用 wolf-rbac 插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

curl http://127.0.0.1:9080/apisix/admin/routes/1  \-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "methods": ["GET"],    "uri": "/*",    "plugins": {    },    "upstream": {        "type": "roundrobin",        "nodes": {            "www.baidu.com:80": 1        }    }}'