Skip to content

Commit 8b7a511

Browse files
authored
Use localhost TCP redirection to the proxy (#34)
* add rules to redirect localhost TCP traffic to the proxy * add params for nftables init script * bump to latest release * simplify rules and enforce precedence * alternative approach * update table name in the list and also avoid redirection for Envoy admin port * bump version in preparation for future release
1 parent 2dff947 commit 8b7a511

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

internal/const/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ const (
3838
const (
3939
DebugUIContainerName = "spiffe-enable-ui"
4040
DebugUIPort = 8000
41-
DefaultDebugUIImage = "ghcr.io/cofide/spiffe-enable-ui:v0.2.3"
41+
DefaultDebugUIImage = "ghcr.io/cofide/spiffe-enable-ui:v0.3.0"
4242
EnvVarUIImage = "SPIFFE_ENABLE_UI_IMAGE"
4343
)

internal/helper/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// Images
1717
var (
1818
SPIFFEHelperImage = "ghcr.io/spiffe/spiffe-helper:0.10.1"
19-
InitHelperImage = "ghcr.io/cofide/spiffe-enable-init:v0.2.3"
19+
InitHelperImage = "ghcr.io/cofide/spiffe-enable-init:v0.3.0"
2020
)
2121

2222
// Constants

internal/proxy/config.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3038
const nftablesSetupScript = `
3139
if ! command -v nft &> /dev/null; then
3240
echo "nftables (nft) is not installed"
@@ -36,19 +44,24 @@ fi
3644
# These nftables rules intercept DNS requests (UDP+TCP)
3745
# and redirect to a DNS proxy provided by Envoy
3846
cat <<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
}
5467
EOF
@@ -58,7 +71,7 @@ nft -f /tmp/dns_redirect.nft
5871
echo "nftables DNS redirection rules applied."
5972
6073
echo "Applied rules:"
61-
nft list table inet envoy_dns_interception
74+
nft list table inet envoy_proxy
6275
`
6376

6477
type 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

Comments
 (0)