使用Lua脚本的Envoy过滤器以从其他API提取数据

问题描述

我有一个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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)