Skip to content

Instantly share code, notes, and snippets.

@Axik
Created April 7, 2023 15:52
Show Gist options
  • Save Axik/65dd6d72c4db53d0a31649a4aa1eb805 to your computer and use it in GitHub Desktop.
Save Axik/65dd6d72c4db53d0a31649a4aa1eb805 to your computer and use it in GitHub Desktop.
inject a tenant ID into MongoDB
static_resources:
listeners:
- name: listener_0
address:
socket_address:
address: 0.0.0.0
port_value: 27017
filter_chains:
- filters:
- name: envoy.filters.network.mongo_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.mongo_proxy.v3.MongoProxy
stat_prefix: mongo
access_log:
- name: envoy.access_loggers.file
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
path: /dev/stdout
generate_id: true
emit_dynamic_metadata: true
decorate_all_responses: true
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)
local headers = request_handle:headers()
local tenant_id = headers:get("x-tenant-id")
if tenant_id ~= nil then
local query = request_handle:body():asBytes()
local new_query = string.gsub(query, '{"%$query":', '{"%$query": {"tenant_id": "' .. tenant_id .. '"},')
request_handle:logInfo("Rewrote query to: " .. new_query)
request_handle:body():setBytes(new_query)
end
end
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: tcp
cluster: mongo
access_log:
- name: envoy.access_loggers.file
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
path: /dev/stdout
clusters:
- name: mongo
connect_timeout: 0.25s
type: logical_dns
dns_lookup_family: V4_ONLY
lb_policy: round_robin
load_assignment:
cluster_name: mongo
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: mongo-service
port_value: 27017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment