Hello:
When using Lua to extend evoyfilter for development, we need to use the third-party library cjson. I need trouble when executing Lua script. Please help me. Thank you.
My code is as follows:
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: auth-forward-filter
namespace: istio-system
spec:
workloadSelector:
# select by label in the same namespace
labels:
istio: ingressgateway
configPatches:
# The Envoy config you want to modify
- applyTo: HTTP_FILTER
match:
context: GATEWAY
listener:
filterChain:
filter:
name: "envoy.http_connection_manager"
subFilter:
name: "envoy.router"
patch:
operation: INSERT_BEFORE
value: # lua filter specification
name: envoy.lua
typed_config:
"@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
inlineCode: |-
function envoy_on_request(request_handle)
local cjson = require "cjson"
-- TODO 过滤白名单
local metadata = request_handle:metadata()
request_handle:logInfo("metatata: ")
request_handle:logInfo(metadata)
-- 获取token
local token = request_handle:headers():get("Authorization")
local path = request_handle:headers():get(":path")
local req_body = string.format('{"token": "%s", "path": "%s"}', token, path)
-- Make an HTTP call.
local headers, body = request_handle:httpCall(
"auth_cluster",
{
[":method"] = "POST",
[":path"] = "/api/v1/auth",
[":authority"] = "auth_cluster",
["set-cookie"] = { "lang=lua; Path=/", "type=binding; Path=/" },
["Content-Type"] = "application/json",
},
req_body,
5000)
request_handle:logInfo("auth response headers: ")
request_handle:logInfo(headers)
request_handle:logInfo("auth response body: ")
request_handle:logInfo(body)
local resp_status = headers["status"]
if resp_status == 200 then
local data = cjson.decode(body)
if data["code"] == 10000 then
request_handle:headers():add("organiz-id", 12)
request_handle:headers():add("user-id", 1)
request_handle:headers():remove("Cookie")
else
request_handle:respond({headers, body})
end
else
request_handle:respond({headers, body})
end
end
However, the following error occurred:
2021-03-04T06:00:44.382809Z error envoy lua script log: [string "function envoy_on_request(request_handle)..."]:2: module 'cjson' not found:
no field package.preload['cjson']
no file './cjson.lua'
no file '/tmp/tmp.OEDvoBtJhK/luajit/share/luajit-2.1.0-beta3/cjson.lua'
no file '/usr/local/share/lua/5.1/cjson.lua'
no file '/usr/local/share/lua/5.1/cjson/init.lua'
no file '/tmp/tmp.OEDvoBtJhK/luajit/share/lua/5.1/cjson.lua'
no file '/tmp/tmp.OEDvoBtJhK/luajit/share/lua/5.1/cjson/init.lua'
no file './cjson.so'
no file '/usr/local/lib/lua/5.1/cjson.so'
no file '/tmp/tmp.OEDvoBtJhK/luajit/lib/lua/5.1/cjson.so'
no file '/usr/local/lib/lua/5.1/loadall.so'