使用限制
MSE云原生网关版本为1.2.11及以上。
注意事项
出于安全考虑,MSE云原生网关默认禁用以下Lua库和函数。
debug.debug
debug.getfenv
debug.getregistry
dofile
io
loadfile
os.execute
os.getenv
os.remove
os.rename
os.tmpname
操作步骤
- 登录MSE管理控制台,并在顶部菜单栏选择地域。
- 在左侧导航栏,选择,单击目标网关名称。
- 在网关详情页面,在左侧导航栏,选择插件市场。
在全部插件页签,选择自定义页签,然后单击lua资源卡片。
在插件配置页签,配置如下参数。
在应用范围区域,选择Lua插件的应用范围。
在插件配置区域,单击编辑,然后在代码编辑框中填写Lua代码,最后单击保存。
在生效开关区域,打开生效开关,然后在开启对话框,单击确认。
API参考
关于网关提供的Lua API详细信息,请参见Lua。
如果您有使用JSON的诉求,可通过如下方式。
引入JSON包:local json = require "json"
。
序列化JSON:local json_str = json.encode(obj)
。
反序列化JSON:local obj = json.decode(json_str)
。
如果在序列化或反序列化时出现错误,Lua将调用error函数抛出错误,并终止当前处理。
常见用例
打印完整的请求应答信息
- 登录MSE网关管理控制台,并在顶部菜单栏选择地域。
- 在左侧导航栏,选择,单击目标网关名称。
在左侧导航栏,单击插件市场,然后单击目标插件。
单击插件配置,在插件配置中配置以下Lua代码。
说明 根据此代码配置,只会打印以下content-type
类型的请求Body和应答Body,且Body不能超过1024字节(1 KB)。
展开查看代码
local maxBodySize = 1024
function check_content_readable(type)
if type == nil then
return false
end
if string.find(type, "application/x-www-form-urlencoded") or string.find(type, "application/json") or string.find(type, "text/plain") then
return true
end
return false
end
function envoy_on_request(request_handle)
local headers = request_handle:headers()
local headersStr = ""
local contentType
for key, value in pairs(headers) do
if key == "content-type" then
contentType = value
end
headersStr = headersStr .. key .. "=" .. value .. ", "
end
request_handle:streamInfo():dynamicMetadata():set("envoy.lua","request_headers",headersStr)
local requestBody = ""
if check_content_readable(contentType) then
for chunk in request_handle:bodyChunks() do
if (chunk:length() > 0) then
requestBody = requestBody .. chunk:getBytes(0, chunk:length())
end
if (#requestBody > maxBodySize) then
requestBody = requestBody .. "<truncated>"
break
end
end
end
request_handle:streamInfo():dynamicMetadata():set("envoy.lua","request_body",requestBody)
end
function envoy_on_response(response_handle)
local headers = response_handle:headers()
local headersStr = ""
local contentType
for key, value in pairs(headers) do
if key == "content-type" then
contentType = value
end
headersStr = headersStr .. key .. "=" .. value .. ", "
end
local responseBody = ""
if check_content_readable(contentType) then
for chunk in response_handle:bodyChunks() do
if (chunk:length() > 0) then
responseBody = responseBody .. chunk:getBytes(0, chunk:length())
end
if (#responseBody > maxBodySize) then
responseBody = responseBody .. "<truncated>"
break
end
end
end
local reqHeaders = ""
local reqBody = ""
local metadata = response_handle:streamInfo():dynamicMetadata():get("envoy.lua")
if metadata ~= nil then
local headers = response_handle:streamInfo():dynamicMetadata():get("envoy.lua")["request_headers"]
if headers ~= nil then
reqHeaders = headers
end
local body = response_handle:streamInfo():dynamicMetadata():get("envoy.lua")["request_body"]
if body ~= nil then
reqBody = body
end
end
response_handle:logInfo("request Headers: [" .. reqHeaders .. "] request Body: [" .. string.gsub(reqBody,"\n","\\n") .. "] response Headers: [" .. headersStr .. "] response Body: [" .. string.gsub(responseBody,"\n","\\n") .. "]")
end
单击插件日志,开启日志投递配置。
开启日志投递配置后,插件日志将投递到SLS并可在页面中查看。
如下所示,可以使用访问日志中的request-id
,在Lua插件日志中找到完整的请求和响应信息。
