membphis commented on a change in pull request #1095: feature: Add wolf rbac plugin URL: https://github.com/apache/incubator-apisix/pull/1095#discussion_r373776873
########## File path: lua/apisix/plugins/wolf-rbac.lua ########## @@ -0,0 +1,376 @@ + +local core = require("apisix.core") +local ck = require("resty.cookie") +local consumer = require("apisix.consumer") +local json = require("apisix.core.json") +local ngx_re = require("ngx.re") +local http = require("resty.http") +local ipairs = ipairs +local ngx = ngx +local plugin_name = "wolf-rbac" + + +local schema = { + type = "object", + properties = { + appid = { + type = "string", + default = "unset" + }, + server = { + type = "string", + default = "http://127.0.0.1:10080" + }, + } +} + +local _M = { + version = 0.1, + priority = 2555, + type = 'auth', + name = plugin_name, + schema = schema, +} + + +local create_consume_cache +do + local consumer_ids = {} + + function create_consume_cache(consumers) + core.table.clear(consumer_ids) + + for _, consumer in ipairs(consumers.nodes) do + core.log.info("consumer node: ", core.json.delay_encode(consumer)) + consumer_ids[consumer.auth_conf.appid] = consumer + end + + return consumer_ids + end + +end -- do + +local token_version = 'V1' +local function create_rbac_token(appid, wolf_token) + return token_version .. "#" .. appid .. "#" .. wolf_token +end + +local function parse_rbac_token(rbac_token) + local res, err = ngx_re.split(rbac_token, "#", nil, nil, 3) + if not res then + return { err = err} + end + + if #res ~= 3 or res[1] ~= token_version then + return { err = 'invalid rbac token: version'} + end + local appid = res[2] + local wolf_token = res[3] + + return {appid = appid, wolf_token = wolf_token} +end + +local function new_headers() + local t = {} + local lt = {} + local _mt = { + __index = function(t, k) + return rawget(lt, string.lower(k)) + end, + __newindex = function(t, k, v) + rawset(t, k, v) + rawset(lt, string.lower(k), v) + end, + } + return setmetatable(t, _mt) +end + +-- timeout in ms +local function http_req(method, uri, body, myheaders, timeout) + if myheaders == nil then myheaders = new_headers() end + + local httpc = http.new() + if timeout then + httpc:set_timeout(timeout) + end + + local params = {method = method, headers = myheaders, body = body, ssl_verify = false} + local res, err = httpc:request_uri(uri, params) + if err then + core.log.error("FAIL REQUEST [ ",core.json.delay_encode( + {method = method, uri = uri, body = body, headers = myheaders}), + " ] failed! res is nil, err:", err) + return nil, err + end + + return res +end + +local function http_get(uri, myheaders, timeout) + return http_req("GET", uri, nil, myheaders, timeout) +end + +local function http_post(uri, body, myheaders, timeout) + return http_req("POST", uri, body, myheaders, timeout) +end + +local function http_put(uri, body, myheaders, timeout) + return http_req("PUT", uri, body, myheaders, timeout) +end + +function _M.check_schema(conf) + core.log.info("input conf: ", core.json.delay_encode(conf)) + + local ok, err = core.schema.check(schema, conf) + if not ok then + return false, err + end + + return true +end + + +local function fetch_rbac_token(ctx) + if ctx.var.arg_rbac_token then + return ngx.unescape_uri(ctx.var.arg_rbac_token) + end + + if ctx.var.http_authorization then + return ctx.var.http_authorization + end + + if ctx.var.http_x_rbac_token then + return ctx.var.http_x_rbac_token + end + + return ctx.var['cookie_x-rbac-token'] +end + +local function loadjson(str) + local ok, jso = pcall(function() return json.decode(str) end) + if ok then + return jso + else + return nil, jso + end +end + +local function check_url_permission(server, appid, action, resName, clientIP, wolf_token) + local retry_max = 3 + local errmsg = nil + local userInfo = nil + local res = nil + local err = nil + local access_check_url = server .. "/wolf/rbac/access_check" + local headers = new_headers() + headers["x-rbac-token"] = wolf_token + headers["Content-Type"] = "application/json; charset=utf-8" + local args = { appID = appid, resName = resName, action = action, clientIP = clientIP} + local url = access_check_url .. "?" .. ngx.encode_args(args) + local timeout = 1000 * 10 + + for i = 1, retry_max do + -- TODO: read apisix info. + res, err = http_get(url, headers, timeout) + if err then + errmsg = 'check permission failed!' .. tostring(err) + break + else + core.log.info("check permission request:", url, ", status:", res.status, ",body:", core.json.delay_encode(res.body)) + if res.status < 500 then + break + else + core.log.info("request [curl -v ", url, "] failed! status:", res.status) + if i < retry_max then + ngx.sleep(0.1) + end + end + end + end + + if err then + core.log.error("fail request: ", url, ", err:", err) + return {status = 500, err = "request to wolf-server failed, err:" .. tostring(err)} + end + + if res.status ~= 200 and res.status ~= 401 then + return {status = 500, err = 'request to wolf-server failed, status:' .. tostring(res.status)} + end + + local body, err = loadjson(res.body) + if err then + errmsg = 'check permission failed! parse response json failed!' + core.log.error( "loadjson(", res.body, ") failed! err:", err) + return {status = res.status, err = errmsg} + else + if body.data then + userInfo = body.data.userInfo + end + errmsg = body.reason + return {status = res.status, err = errmsg, userInfo = userInfo} + end +end + + +function _M.rewrite(conf, ctx) + local url = ctx.var.uri + local action = ctx.var.request_method + local clientIP = core.request.get_ip(ctx) + local permItem = {action = action, url = url, clientIP = clientIP} + + local rbac_token = fetch_rbac_token(ctx) + if rbac_token == nil then + core.log.info("no permission to access ", core.json.delay_encode(permItem), ", need login!") + return 401, {message = "Missing rbac token in request"} + end + + local tokenInfo = parse_rbac_token(rbac_token) + core.log.info("token info: ", core.json.delay_encode(tokenInfo)) + if tokenInfo.err then + return 401, {message = 'invalid rbac token: parse failed'} + end + + + local appid = tokenInfo.appid + local wolf_token = tokenInfo.wolf_token + permItem.appid = appid + permItem.wolf_token = wolf_token + + local consumer_conf = consumer.plugin(plugin_name) + if not consumer_conf then + return 401, {message = "Missing related consumer"} + end + + local consumers = core.lrucache.plugin(plugin_name, "consumers_key", + consumer_conf.conf_version, + create_consume_cache, consumer_conf) + + core.log.info("------ consumers: ", core.json.delay_encode(consumers)) + local consumer = consumers[appid] + if not consumer then + core.log.error("consumer [", appid, "] not found") + return 401, {message = "Invalid appid in rbac token"} + end + core.log.info("consumer: ", core.json.delay_encode(consumer)) + local server = consumer.auth_conf.server + + local url = ctx.var.uri + local action = ctx.var.request_method + local clientIP = core.request.get_ip(ctx) + local permItem = {appid = appid, action = action, url = url, clientIP = clientIP, wolf_token = wolf_token} + + local res = check_url_permission(server, appid, action, url, clientIP, wolf_token) + core.log.info(" check_url_permission(", core.json.delay_encode(permItem), ") res: ",core.json.delay_encode(res)) + + local username = nil + local nickname = nil + if type(res.userInfo) == 'table' then + local userInfo = res.userInfo + core.response.set_header("X-UserId", userInfo.id) + core.response.set_header("X-Username", userInfo.username) + core.response.set_header("X-Nickname", ngx.escape_uri(userInfo.nickname) or userInfo.username) + ctx.userInfo = userInfo + username = userInfo.username + nickname = userInfo.nickname + end + + if res.status == 200 then + --- + else + -- no permission. + core.log.error(" check_url_permission(", core.json.delay_encode(permItem), ") failed, res: ",core.json.delay_encode(res)) + return 401, {message = res.err, username = username, nickname = nickname} + end + core.log.info("hit wolf-rbac rewrite") +end + +local function get_args() + local args, err + ngx.req.read_body() + if string.find(ngx.req.get_headers()["Content-Type"] or "","application/json", 0) then Review comment: We can turn off the pattern matching feature by using the optional fourth argument plain. `> = string.find("Hello Lua user", "%su", 1, true) -- turn on plain searches, now not found` http://lua-users.org/wiki/StringLibraryTutorial ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services