Skip to content

Instantly share code, notes, and snippets.

@calam1
Last active December 14, 2022 21:33
Show Gist options
  • Save calam1/725c02ced9e5dc0ae57b21ee9f13bfb8 to your computer and use it in GitHub Desktop.
Save calam1/725c02ced9e5dc0ae57b21ee9f13bfb8 to your computer and use it in GitHub Desktop.
envoyfilter writing authorization header in the response and setting cookie values
---
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: lua-filter
namespace: dev
spec:
workloadSelector:
labels:
app: bff
configPatches:
- applyTo: HTTP_FILTER
match:
context: SIDECAR_INBOUND
listener:
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 stringSplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
print (str)
table.insert(t, str)
end
return t
end
function envoy_on_request(request_handle)
headers = request_handle:headers()
for key, value in pairs(headers) do
request_handle:logWarn("key: " .. key .. " <--> value: " .. value)
end
request_handle:streamInfo():dynamicMetadata():set("request_headers", "request_authority", headers:get("x-custom-header-authz"))
request_handle:streamInfo():dynamicMetadata():set("envoy.filters.http.lua", "cookies", headers:get("cookie"))
end
function envoy_on_response(response_handle)
local meta = response_handle:streamInfo():dynamicMetadata():get("request_headers")
for key, value in pairs(meta) do
response_handle:headers():add("Authorization", "Bearer " .. value)
response_handle:headers():add("set-cookie", "x-jwt=" .. value)
end
local cookies = response_handle:streamInfo():dynamicMetadata():get("envoy.filters.http.lua")
local sep = ";"
for key, value in pairs(cookies) do
splits = stringSplit(value, sep)
for k, v in pairs(splits) do
response_handle:headers():add("set-cookie", v)
response_handle:logWarn("COOKIESkey: " .. k .. " <--> COOKIESvalue: " .. v)
end
end
end
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment