Envoy Filter with a Lua script to fetch data from other API

Viewed 2481

I've got a Envoy Filter in which I add a header to every HTTP request. The header's value comes from API.

Let's assume two configurations of the filter. In the configuration below I added a hardcoded version of my header. It was checked in the logs of my target application and it works.

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

This time I want to have the header's value coming from my API. Unfortunately, this setup doesn't work and I have no idea why. I have checked the Lua script on my local machine, and the script itself works but as soon as I provide the script to the filter, no header is added.

 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
0 Answers
Related