首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kong插件不运行访问阻止

Kong插件不运行访问阻止
EN

Stack Overflow用户
提问于 2018-05-22 23:21:34
回答 2查看 364关注 0票数 0

我正在开发一个Kong API Gateway的插件。我创建了一个服务,将它指向本地网络中的另一个服务,基本上,对我的服务的每个请求都被重定向到另一个服务,到目前为止一切正常。

插件所要做的就是获取头部中的Authorization header字段,并将其作为URI的一部分传递给上游服务。例如。

请求在以下位置收到: localhost/service

在其标头中,它有一个包含JWT授权载体

插件必须接收它,获取JWT并将其解析为上游服务的URI : productionServer/service/9a8udoadzlkndid813gru1gr <-JWT从标头获取

我到现在为止的尝试是:

代码语言:javascript
复制
local singletons = require "kong.singletons"
local BasePlugin = require "kong.plugins.base_plugin"
local responses = require "kong.tools.responses"
local constants = require "kong.constants"
local multipart = require "multipart"
local cjson = require "cjson"
local url = require "socket.url"
local access = require "kong.plugins.ctk.access"

local CtkHandler = BasePlugin:extend()

CtkHandler.PRIORITY = 3505
CtkHandler.VERSION = "0.1.0"

file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
io.input(file)
file:write("--- JUST EXTENDED THE BASE PLUGIN ---")


function CtkHandler:new()
  CtkHandler.super.new(self, "ctk")
  file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
  io.input(file)
  file:write("--- INSTACIATED ITSELF ---")  
end

function CtkHandler:access(conf)
  CtkHandler.super.access(self)
  file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
  io.input(file)
  file:write("--- STARTED THE ACCESS PART ---")
  do_authentication()
  access.execute(conf)
end

file:close()

return CtkHandler

其想法是,在每次请求之后,执行末尾的访问块,然后,他将重定向到我的访问文件

代码语言:javascript
复制
local singletons = require "kong.singletons"
local BasePlugin = require "kong.plugins.base_plugin"
local responses = require "kong.tools.responses"
local constants = require "kong.constants"
local multipart = require "multipart"
local cjson = require "cjson"
local url = require "socket.url"
local basic_serializer = require "kong.plugins.log-serializers.basic"
local string_format  = string.format
local ngx_set_header = ngx.req.set_header
local get_method     = ngx.req.get_method
local req_set_uri_args = ngx.req.set_uri_args
local req_get_uri_args = ngx.req.get_uri_args
local req_set_header = ngx.req.set_header
local req_get_headers = ngx.req.get_headers
local req_clear_header = ngx.req.clear_header
local req_set_method = ngx.req.set_method
local ngx_decode_args = ngx.decode_args
local ngx_re_gmatch  = ngx.re.gmatch
local string_format = string.format
local cjson_encode = cjson.encode
local ipairs = ipairs
local request = ngx.request

local function retrieve_token(request, conf)
    file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
    io.input(file)
    file:write("--- RUNNING RETRIEVE TOKEN ---")  
    local uri_parameters = request.get_uri_args()

    for _, v in ipairs(conf.uri_param_names) do
      if uri_parameters[v] then
        return uri_parameters[v]
      end
    end

    local ngx_var = ngx.var
    for _, v in ipairs(conf.cookie_names) do
      local jwt_cookie = ngx_var["cookie_" .. v]
      if jwt_cookie and jwt_cookie ~= "" then
        return jwt_cookie
      end
    end

    local authorization_header = request.get_headers()["authorization"]
    if authorization_header then
      local iterator, iter_err = ngx_re_gmatch(authorization_header, "\\s*[Bb]earer\\s+(.+)")
      if not iterator then
        return nil, iter_err
      end

      local m, err = iterator()
      if err then
        return nil, err
      end

      if m and #m > 0 then
        return m[1]
      end
    end
  end

  local function do_authentication(conf)
    file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
    io.input(file)
    file:write("--- RUNNING DO_AUTHENTICATION ---")  
    local token, err = retrieve_token(ngx.req, conf)
    if err then
      return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
    end

    local ttype = type(token)
    if ttype ~= "string" then
      if ttype == "nil" then
        return false, {status = 401}
      elseif ttype == "table" then
        return false, {status = 401, message = "Multiple tokens provided"}
      else
        return false, {status = 401, message = "Unrecognizable token"}
      end
      append_uri(token)
      return true
    end
  end

  local function append_uri(token)
    file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
    io.input(file)
    file:write("--- FUNCTION APPEND_URL ---")
    local uri = ngx.get_uri_args
    ngx.req.set_uri(ngx.unescape_uri("/" .. token))
  end

在Kong服务器中,安装了上面的插件后,我收到:

代码语言:javascript
复制
--- JUST EXTENDED THE BASE PLUGIN ------ INSTACIATED ITSELF ---

这是插入到代码中以跟踪它的控件。

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2018-05-24 00:17:24

实际上不推荐使用io.write,所以我要做的是将其更改为:

ngx.log(ngx.WARN,“一些消息”)

在那之后,块代码访问运行得很好。

票数 0
EN

Stack Overflow用户

发布于 2018-05-30 18:13:00

有一个Kong插件可以执行OAuth 2.0令牌验证,请参阅:kong-oidc。你可能想要部署它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50471278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档