@@ -25,8 +25,16 @@ const (
2525 EnvoyConfigContentEnvVar = "ENVOY_CONFIG_CONTENT"
2626 EnvoyConfigInitContainerName = "inject-envoy-config"
2727 EnvoyPort = 10000
28+ EnvoyUID = 101
29+ DNSProxyPort = 15053
2830)
2931
32+ type NftablesParams struct {
33+ EnvoyUID int
34+ EnvoyPort int
35+ DNSProxyPort int
36+ }
37+
3038const nftablesSetupScript = `
3139if ! command -v nft &> /dev/null; then
3240 echo "nftables (nft) is not installed"
3644# These nftables rules intercept DNS requests (UDP+TCP)
3745# and redirect to a DNS proxy provided by Envoy
3846cat <<EOF > /tmp/dns_redirect.nft
39- table inet envoy_dns_interception {
40- chain redirect_dns_output {
47+ table inet envoy_proxy {
48+ chain envoy_output {
4149 type nat hook output priority dstnat; policy accept;
4250
43- # Rule to accept DNS from skuid 101 (Envoy) - UDP
44- meta skuid == 101 udp dport 53 counter accept comment "Accept Envoy UDP DNS"
51+ # Skip Envoy's own traffic
52+ meta skuid == {{.EnvoyUID}} return
53+
54+ # DNS redirection
55+ udp dport 53 counter redirect to :{{.DNSProxyPort}} comment "DNS UDP to Envoy"
56+ tcp dport 53 counter redirect to :{{.DNSProxyPort}} comment "DNS TCP to Envoy"
4557
46- # Rule to accept DNS from skuid 101 (Envoy) - TCP
47- meta skuid == 101 tcp dport 53 counter accept comment "Accept Envoy TCP DNS"
58+ # Skip traffic already going to Envoy port
59+ tcp dport {{.EnvoyPort}} return
60+ tcp dport 9901 return
4861
49- # Rules to redirect DNS
50- meta skuid != 101 udp dport 53 counter redirect to :15053 comment "Webhook: UDP DNS to Envoy"
51- meta skuid != 101 tcp dport 53 counter redirect to :15053 comment "Webhook: TCP DNS to Envoy"
62+ # Redirect loopback TCP traffic (using tcp dport range to match all TCP)
63+ ip daddr 127.0.0.1/8 tcp dport 1-65535 counter redirect to :{{.EnvoyPort}} comment "Loopback IPv4 to Envoy"
64+ ip6 daddr ::1/128 tcp dport 1-65535 counter redirect to :{{.EnvoyPort}} comment "Loopback IPv6 to Envoy"
5265 }
5366}
5467EOF
@@ -58,7 +71,7 @@ nft -f /tmp/dns_redirect.nft
5871echo "nftables DNS redirection rules applied."
5972
6073echo "Applied rules:"
61- nft list table inet envoy_dns_interception
74+ nft list table inet envoy_proxy
6275`
6376
6477type EnvoyConfigParams struct {
@@ -162,13 +175,19 @@ func NewEnvoy(params EnvoyConfigParams) (*Envoy, error) {
162175 },
163176 }
164177
178+ nftTablesParams := NftablesParams {
179+ EnvoyUID : EnvoyUID ,
180+ EnvoyPort : EnvoyPort ,
181+ DNSProxyPort : DNSProxyPort ,
182+ }
183+
165184 tmpl , err := template .New ("initScript" ).Parse (nftablesSetupScript )
166185 if err != nil {
167186 return nil , fmt .Errorf ("failed to parse nftables init script template: %w" , err )
168187 }
169188
170189 var renderedScript bytes.Buffer
171- if err := tmpl .Execute (& renderedScript , params ); err != nil {
190+ if err := tmpl .Execute (& renderedScript , nftTablesParams ); err != nil {
172191 return nil , fmt .Errorf ("failed to render nftables init script template with params: %w" , err )
173192 }
174193
0 commit comments