名字

wolf-rbac 是一个认证及授权(rbac)插件,它需要与 consumer 一起配合才能工作。同时需要添加 wolf-rbac 到一个 serviceroute 中。 rbac功能由wolf提供, 有关 wolf 的更多信息, 请参考wolf文档

属性

  • server: 设置 wolf-server 的访问地址, 如果未设置, 默认为: http://127.0.0.1:10080.
  • appid: 设置应用id, 该应用id, 需要是在 wolf-console 中已经添加的应用id.
  • header_prefix: 自定义http头的前缀, 默认为: X-. wolf-rbac在鉴权成功后, 会在请求头(用于传给后端)及响应头(用于传给前端)中添加3个头: X-UserId, X-Username, X-Nickname.

依赖项

安装 wolf, 并启动服务

Wolf快速起步

添加应用, 管理员, 普通用户, 权限, 资源 及给用户授权.

Wolf管理使用

如何启用

  1. 创建一个 consumer 对象,并设置插件 wolf-rbac 的值。
  1. curl http://127.0.0.1:9080/apisix/admin/consumers -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "username":"wolf_rbac",
  4. "plugins":{
  5. "wolf-rbac":{
  6. "server":"http://127.0.0.1:10080",
  7. "appid":"restful"
  8. }
  9. },
  10. "desc":"wolf-rbac"
  11. }'

你可以使用浏览器打开 dashboard:http://127.0.0.1:9080/apisix/dashboard/,通过 web 界面来完成上面的操作,先增加一个 consumer: wolf-rbac - 图1

然后在 consumer 页面中添加 wolf-rbac 插件: wolf-rbac - 图2

注意: 上面填写的 appid 需要在wolf控制台中已经存在的.

  1. 创建 Route 或 Service 对象,并开启 wolf-rbac 插件。
  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. "wolf-rbac": {}
  7. },
  8. "upstream": {
  9. "type": "roundrobin",
  10. "nodes": {
  11. "www.baidu.com:80": 1
  12. }
  13. }
  14. }'

测试插件

首先进行登录获取 wolf-rbac token:

下面的 appid, username, password 必须为wolf系统中真实存在的.

  • 以POST application/json方式登陆.
  1. curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/login -i \
  2. -H "Content-Type: application/json" \
  3. -d '{"appid": "restful", "username":"test", "password":"user-password"}'
  4. HTTP/1.1 200 OK
  5. Date: Wed, 24 Jul 2019 10:33:31 GMT
  6. Content-Type: text/plain
  7. Transfer-Encoding: chunked
  8. Connection: keep-alive
  9. Server: APISIX web server
  10. {"rbac_token":"V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts","user_info":{"nickname":"test","username":"test","id":"749"}}
  • 以POST x-www-form-urlencoded方式登陆
  1. curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/login -i \
  2. -H "Content-Type: application/x-www-form-urlencoded" \
  3. -d 'appid=restful&username=test&password=user-password'

使用获取到的 token 进行请求尝试

  • 缺少 token
  1. curl http://127.0.0.1:9080/ -H"Host: www.baidu.com" -i
  2. HTTP/1.1 401 Unauthorized
  3. ...
  4. {"message":"Missing rbac token in request"}
  • token 放到请求头(Authorization)中:
  1. curl http://127.0.0.1:9080/ -H"Host: www.baidu.com" \
  2. -H 'Authorization: V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts' -i
  3. HTTP/1.1 200 OK
  4. <!DOCTYPE html>
  • token 放到请求头(x-rbac-token)中:
  1. curl http://127.0.0.1:9080/ -H"Host: www.baidu.com" \
  2. -H 'x-rbac-token: V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts' -i
  3. HTTP/1.1 200 OK
  4. <!DOCTYPE html>
  • token 放到请求参数中:
  1. curl 'http://127.0.0.1:9080?rbac_token=V1%23restful%23eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts' -H"Host: www.baidu.com" -i
  2. HTTP/1.1 200 OK
  3. <!DOCTYPE html>
  • token 放到 cookie 中:
  1. curl http://127.0.0.1:9080 -H"Host: www.baidu.com" \
  2. --cookie x-rbac-token=V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts -i
  3. HTTP/1.1 200 OK
  4. <!DOCTYPE html>

获取 RBAC 用户信息

  1. curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/user_info \
  2. --cookie x-rbac-token=V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts -i
  3. HTTP/1.1 200 OK
  4. {
  5. "user_info":{
  6. "nickname":"test",
  7. "lastLogin":1582816780,
  8. "id":749,
  9. "username":"test",
  10. "appIDs":["restful"],
  11. "manager":"none",
  12. "permissions":{"USER_LIST":true},
  13. "profile":null,
  14. "roles":{},
  15. "createTime":1578820506,
  16. "email":""
  17. }
  18. }

修改 RBAC 用户密码

  1. curl http://127.0.0.1:9080/apisix/plugin/wolf-rbac/change_pwd \
  2. -H "Content-Type: application/json" \
  3. --cookie x-rbac-token=V1#restful#eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzQ5LCJ1c2VybmFtZSI6InRlc3QiLCJtYW5hZ2VyIjoiIiwiYXBwaWQiOiJyZXN0ZnVsIiwiaWF0IjoxNTc5NDQ5ODQxLCJleHAiOjE1ODAwNTQ2NDF9.n2-830zbhrEh6OAxn4K_yYtg5pqfmjpZAjoQXgtcuts -i \
  4. -X PUT -d '{"oldPassword": "old password", "newPassword": "new password"}'
  5. HTTP/1.1 200 OK
  6. {"message":"success to change password"}

禁用插件

当你想去掉 rbac-wolf 插件的时候,很简单,在routes中的插件配置中把对应的 插件 配置删除即可,无须重启服务,即刻生效:

  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. },
  7. "upstream": {
  8. "type": "roundrobin",
  9. "nodes": {
  10. "www.baidu.com:80": 1
  11. }
  12. }
  13. }'