我有一个Envoy过滤器,其中向每个HTTP请求添加了一个标头。标头的值来自API。
我们假设过滤器有两种配置。在下面的配置中,我添加了标题的硬编码版本。已在我的目标应用程序的日志中对其进行了检查,并且可以正常工作。
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: lua-filter
spec:
configPatches:
- applyTo: HTTP_FILTER
match:
context: ANY
listener:
portNumber: 7123
filterChain:
filter:
name: "envoy.http_connection_manager"
subFilter:
name: "envoy.router"
patch:
operation: INSERT_BEFORE
value:
name: envoy.lua
typed_config:
"@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
inlineCode: |
function envoy_on_request(request_handle)
request_handle:headers():add("authorization", "it works!")
end
这次,我想让标头的值来自我的API。不幸的是,此设置不起作用,我也不知道为什么。我已经在本地计算机上检查了Lua脚本,该脚本本身可以工作,但是只要将脚本提供给过滤器,就不会添加任何头文件。
typed_config:
"@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
inlineCode: |
function envoy_on_request(request_handle)
local http = require('socket.http')
local json = require('json')
local ltn12 = require "ltn12"
local reqbody="my request body"
local respbody = {}
local body, code, headers, status = http.request {
method = "POST",
url = "http://my-address",
source = ltn12.source.string(reqbody),
headers =
{
["Accept"] = "*/*",
["Accept-Encoding"] = "gzip, deflate",
["Accept-Language"] = "en-us",
["Content-Type"] = "application/x-www-form-urlencoded",
["content-length"] = string.len(reqbody)
},
sink = ltn12.sink.table(respbody)
}
respbody = table.concat(respbody)
parsed = json.decode(respbody)
token = parsed["token-value"]
request_handle:headers():add("authorization",token)
end