lua - 我无法在 Kong 预功能插件上使用 lua Resty.http

标签 lua gateway kong kong-plugin

我需要在 Kong 网关上发出任何请求之前发出外部安全请求。

为此,我使用了插件 Kong Functions(Pre-Plugins),它允许我在任何请求之前执行 lua 脚本。

脚本:

local http = require "resty.http"

-- Pega o valor do header 'token' da requisição
local token = kong.request.get_header("token")

-- Verifica se o header 'token' foi encontrado
if not token then
    ngx.log(ngx.ERR, "token not found in request headers")
    ngx.exit(ngx.HTTP_BAD_REQUEST)
end

-- Configuração da requisição HTTP
local httpc = http.new()
local res, err = httpc:request_uri("my_request_url", {
    method = "GET",
    headers = {
        ["token"] = token
    },
    ssl_verify = false
})

-- Verifica se houve algum erro na requisição
if not res then
    ngx.log(ngx.ERR, "failed to request token validation: ", err)
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end

-- Verifica se a validação do token falhou
if res.status ~= 200 then
    ngx.log(ngx.ERR, "token validation failed with status code: ", res.status)
    ngx.exit(ngx.HTTP_UNAUTHORIZED)
end

但我收到此错误:

init.lua:386 [pre-function] /usr/local/share/lua/5.1/kong/tools/kong-lua-sandbox.lua:171: /usr/local/share/lua/5.1/kong/tools/sandbox.lua:88: require 'resty.http' not allowed within sandbox,

我尝试在我的容器上添加一个环境:

  - name: KONG_LUA_SANDBOX_GLOBALS
    value: "package.loaded['resty.http'] = require 'resty.http'"

但是我不断收到相同的错误,即使重新启动容器后,我真的不知道下一步该怎么做。

最佳答案

我对这个配置很感兴趣:

I tryed to add an env on my container:

  • name: KONG_LUA_SANDBOX_GLOBALS
    value: "package.loaded['resty.http'] = require 'resty.http'"

我不知道任何名为 lua_sandbox_globals 的配置。你的意思是untrusted_lua_sandbox_requires

默认情况下 Kong 的无服务器插件 disallow任何要运行的 require 语句,您都可以使用 untrusted_lua_sandbox_requires 来请求模块,而无需让 Kong 退出沙盒模式。

如果您想将resty.http添加到允许加载的包列表中。将模块名称设置为环境变量的值就足够了:

name: KONG_UNTRUSTED_LUA_SANDBOX_REQUIRES
value: resty.http

关于lua - 我无法在 Kong 预功能插件上使用 lua Resty.http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75825260/

相关文章:

file-io - 尝试让 Lua 中的程序从文件中的各个行读取信息

error-handling - Lua中的错误处理

api - 如何实现基本的 API 网关

linux - Linux 中两种不同的网关以及被动网关的 ping

孔 {"message":"no Route matched with those values"}

socket.io - 通过 Kong 代理的套接字 Io - 客户端连接保持关闭

winapi - Lua Alien - 使用 WinAPI 将消息发送到 Notepad.exe

lua - 图像显示在 Corona Simulator 的左上角

linux - 如何路由数据包以使用 localhost 作为网关?

java - Kong API 在配置服务后返回 404