1

I have a envoy lua filter to intercept upstream response and call a external api from the lua filter's "envoy_on_response" method. I need request_handle's "authorization" value inside envoy_on_response()" coroutine and pass it to external api call. I'm doing currently as below:

 - name: envoy.filters.http.lua
      typed_config:
        "@type": type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua
        inline_code: |
          local authToken = ""
          -- Called on the request path.
          function envoy_on_request(request_handle)
            authToken = request_handle:headers():get("authorization")
          end
          -- Called on the response path.
          function envoy_on_response(response_handle)
            -- Make an HTTP call to an upstream host with the following headers, body, and timeout.
              local headers, body = response_handle:httpCall(
              "ext_authz-http-service",
              {
                [":method"] = "GET",
                [":path"] = "/v1/response-filter",
                [":authority"] = "my-api-service",
                ["authorization"] = authToken
              },
              "test body data",
              5000)
          end
          

Some where in the envoy doc, it was mentioned both "envoy_on_request()" & "envoy_on_response()" are called as co-routines and nothing should be shared between them. So, my question is, what is the right way to get and pass "authorization" header inside "envoy_on_response()" ?.

Thanks.

sriba
  • 745
  • 1
  • 6
  • 13

1 Answers1

0

You could use the Dynamic metadata object API for that. Storing information such as the header in the metadata works as follows:

function envoy_on_request(request_handle)
request_handle:streamInfo():dynamicMetadata():set("envoy.filters.http.lua.myfilter", "authorization", request_handle:headers():get("authorization"))
end

function envoy_on_response(response_handle)
  local authorization = response_handle:streamInfo():dynamicMetadata():get("envoy.filters.http.lua.myfilter")["authorization"]
end
dastrobu
  • 1,600
  • 1
  • 18
  • 32