Плагин Kong не запускает блокировку доступа - PullRequest
0 голосов
/ 22 мая 2018

Я разрабатываю плагин для Kong API Gateway.Я создал службу, указывающую на другую службу в локальной сети, и в основном все запросы к моей службе перенаправляются на другую, пока что все хорошо.

Что нужно сделать плагину - это захватить поле Authorization Bearerв заголовке и передайте в восходящий сервис как часть URI.Например,

Запрос получен по адресу: localhost / service

. В своем заголовке имеется носитель авторизации, содержащий JWT

. Плагин должен его получить, возьмите JWT.и проанализировать его в URI для вышестоящей службы: productionServer / service / 9a8udoadzlkndid813gru1gr <-JWT взял из заголовка </p>

Моя попытка до сих пор:

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

Идея состоит в том, что после каждого запроса,блок доступа в конце будет выполнен, затем он перенаправит на мой файл доступа

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, после установки плагина выше, я получаю:

--- JUST EXTENDED THE BASE PLUGIN ------ INSTACIATED ITSELF ---

Какой элемент управления вставлен в код для его отслеживания.

Есть идеи?

Ответы [ 2 ]

0 голосов
/ 30 мая 2018

Существует плагин Kong, который может выполнять проверку токена OAuth 2.0, см. kong-oidc .Вы можете развернуть это.

0 голосов
/ 23 мая 2018

На самом деле использование io.write не рекомендуется, поэтому мне пришлось изменить его на:

ngx.log (ngx.WARN, "SOME MESSAGE")

Последоступ к блочному коду прошел нормально.

...