envoy - using lua with http requests
Here we will use an envoy configuration that allow us to run lua scripts to intercept http request. This static configuration
static_resources:
listeners:
- name: listener_0
address:
socket_address:
address: 0.0.0.0
port_value: 8080
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
stat_prefix: ingress_http
codec_type: AUTO
route_config:
name: local_route
virtual_hosts:
- name: backend
domains: ["*"]
routes:
- match: { prefix: "/" }
route:
cluster: httpbin
http_filters:
- name: envoy.filters.http.lua
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua
inline_code: |
function envoy_on_request(request_handle)
request_handle:logInfo("Lua filter: Request received")
request_handle:headers():add("x-added-by", "lua-filter")
end
function envoy_on_response(response_handle)
response_handle:logInfo("Lua filter: Response processing")
response_handle:headers():add("x-response-added-by", "lua-filter")
end
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
clusters:
- name: httpbin
connect_timeout: 5s
type: LOGICAL_DNS
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: httpbin
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: httpbin.org
port_value: 80
dns_lookup_family: V4_ONLY
To run this via docker we simply do the following:-
docker run --rm -it -v ".\envoy-lua.yaml:/etc/envoy/envoy.yaml"
-p 8080:8080 envoyproxy/envoy:v1.31-latest
As you can see from the static configuration, we are using port 8080.
To test this out, run
curl -v http://localhost:8080/get
And you should be able to see the following responses from envoy.
Comments