ShaoZeMing commented on a change in pull request #3554: URL: https://github.com/apache/apisix/pull/3554#discussion_r572605834
########## File path: apisix/plugins/auth-hook.lua ########## @@ -0,0 +1,371 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +local core = require("apisix.core") +local json = require("apisix.core.json") +local http = require("resty.http") +local ck = require("resty.cookie") +local ngx = ngx +local ipairs = ipairs +local type = type +local rawget = rawget +local rawset = rawset +local setmetatable = setmetatable +local string = string + +local plugin_name = "auth-hook" +local hook_lrucache = core.lrucache.new({ + ttl = 60, count = 1024 +}) + +local schema = { + type = "object", + properties = { + auth_hook_id = { + type = "string", + minLength = 1, + maxLength = 100, + default = "unset" + }, + auth_hook_uri = { + type = "string", + minLength = 1, + maxLength = 4096 + }, + auth_hook_method = { + type = "string", + default = "GET", + enum = { "GET", "POST" }, + }, + hook_headers = { + type = "array", + items = { + type = "string", + minLength = 1, + maxLength = 100 + }, + uniqueItems = true + }, + hook_args = { + type = "array", + items = { + type = "string", + minLength = 1, + maxLength = 100 + }, + uniqueItems = true + }, + hook_res_to_headers = { + type = "array", + items = { + type = "string", + minLength = 1, + maxLength = 100 + }, + uniqueItems = true + }, + hook_keepalive = { + type = "boolean", + default = true + }, + hook_keepalive_timeout = { + type = "integer", + minimum = 1000, + default = 60000 + }, + hook_keepalive_pool = { + type = "integer", + minimum = 1, + default = 5 + }, + hook_res_to_header_prefix = { + type = "string", + default = "X-", + minLength = 1, + maxLength = 100 + }, + hook_cache = { + type = "boolean", + default = false + }, + check_termination = { + type = "boolean", + default = true + }, + }, + required = { "auth_hook_uri" }, +} + +local _M = { + version = 0.1, + priority = 1000, + name = plugin_name, + schema = schema, +} + +function _M.check_schema(conf) + + return core.schema.check(schema, conf) + +end + +local function get_auth_token(ctx) + local token = ctx.var.http_x_auth_token + if token then + return token + end + + token = ctx.var.http_authorization + if token then + return token + end + + token = ctx.var.arg_auth_token + if token then + return token + end + + local cookie, err = ck:new() + if not cookie or err then + return nil + end + + local val, error = cookie:get("auth-token") + if error then + return nil + end + return val +end + +local function fail_response(message, init_values) + local response = init_values or {} + response.message = message + return response +end + +--初始化headers +local function new_table() + 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 + + +--proxy headers +local function request_headers(config, ctx) + local req_headers = new_table() + local headers = core.request.headers(ctx) + local hook_headers = config.hook_headers + if not hook_headers then + return req_headers + end + for i, field in ipairs(hook_headers) do + local v = headers[field] + if v then + req_headers[field] = v + end + end + return req_headers +end + + +--init headers +local function res_init_headers(config) + + local prefix = config.hook_res_to_header_prefix or '' + local hook_res_to_headers = config.hook_res_to_headers + + if type(hook_res_to_headers) ~= "table" then + return + end + core.request.set_header(prefix .. "auth-data", nil) + for i, val in ipairs(hook_res_to_headers) do + local f = string.gsub(val, '_', '-') + core.request.set_header(prefix .. f, nil) + core.response.set_header(prefix .. f, nil) + + end + return +end + +--res headers +local function res_to_headers(config, data) + + local prefix = config.hook_res_to_header_prefix or '' + local hook_res_to_headers = config.hook_res_to_headers + if type(hook_res_to_headers) ~= "table" or type(data) ~= "table" then + return + end + core.request.set_header(prefix .. "auth-data", core.json.encode(data)) + for i, val in ipairs(hook_res_to_headers) do + local v = data[val] + if v then + if type(v) == "table" then + v = core.json.encode(v) + end + local f = string.gsub(val, '_', '-') + core.request.set_header(prefix .. f, v) + core.response.set_header(prefix .. f, v) + + end + end + return +end + + +--proxy args +local function get_hook_args(hook_args) + + local req_args = new_table() + if not hook_args then + return req_args + end + local args = ngx.req.get_uri_args() + for i, field in ipairs(hook_args) do + local v = args[field] + if v then + req_args[field] = v + end + end + return req_args +end + +-- Configure request parameters. +local function hook_configure_params(args, config, myheaders) + -- TLS verification. + myheaders["Content-Type"] = "application/json; charset=utf-8" + local auth_hook_params = { + ssl_verify = false, + method = config.auth_hook_method, + headers = myheaders, + } + if config.hook_keepalive then + auth_hook_params.keepalive_timeout = config.hook_keepalive_timeout + auth_hook_params.keepalive_pool = config.hook_keepalive_pool + else + auth_hook_params.keepalive = config.hook_keepalive + end + local url = config.auth_hook_uri .. "?" .. ngx.encode_args(args) + return auth_hook_params, url +end + +-- timeout in ms +local function http_req(url, auth_hook_params) + + local httpc = http.new() + httpc:set_timeout(1000 * 10) + local res, err = httpc:request_uri(url, auth_hook_params) + if err then + core.log.error("FAIL REQUEST [ ", core.json.encode(auth_hook_params), + " ] failed! res is nil, err:", err) + return nil, err + end + + return res +end + +local function get_auth_info(config, ctx, action, path, client_ip, auth_token) + local errmsg + local myheaders = request_headers(config, ctx) + myheaders["X-Client-Ip"] = client_ip + myheaders["Authorization"] = auth_token + myheaders["Auth-Hook-Id"] = config.auth_hook_id + local args = get_hook_args(config.hook_args) + args['hook_path'] = path + args['hook_action'] = action + args['hook_client_ip'] = client_ip + core.response.set_header("hook-cache", 'no-cache') + local auth_hook_params, url = hook_configure_params(args, config, myheaders) + local res, err = http_req(url, auth_hook_params) + if err then + core.log.error("fail request: ", url, ", err:", err) + return { + status = 500, + err = "request to hook-server failed, err:" .. err + } Review comment: OK. I optimized ---------------------------------------------------------------- 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: [email protected]
