我需要在 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/