Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit c334d83

Browse files
committed
Use iptables -I to make the rules dynamic
Signed-off-by: Keith Mattix II <[email protected]>
1 parent 2de59eb commit c334d83

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

pkg/injector/iptables.go

+26-32
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,31 @@ import (
1010
"github.com/openservicemesh/osm/pkg/constants"
1111
)
1212

13-
func genIPTablesOutboundStaticRules(proxyMode configv1alpha2.LocalProxyMode) []string {
14-
// iptablesOutboundStaticRules is the list of iptables rules related to outbound traffic interception and redirection
15-
iptablesOutboundStaticRules := []string{
16-
// Redirects outbound TCP traffic hitting OSM_PROXY_OUT_REDIRECT chain to Envoy's outbound listener port
17-
fmt.Sprintf("-A OSM_PROXY_OUT_REDIRECT -p tcp -j REDIRECT --to-port %d", constants.EnvoyOutboundListenerPort),
18-
19-
// Traffic to the Proxy Admin port flows to the Proxy -- not redirected
20-
fmt.Sprintf("-A OSM_PROXY_OUT_REDIRECT -p tcp --dport %d -j ACCEPT", constants.EnvoyAdminPort),
21-
}
22-
23-
if proxyMode == configv1alpha2.LocalProxyModePodIP {
24-
// For envoy -> local service container proxying, send traffic to pod IP instead of localhost
25-
iptablesOutboundStaticRules = append(iptablesOutboundStaticRules, fmt.Sprintf("-A OUTPUT -p tcp -o lo -d 127.0.0.1/32 -m owner --uid-owner %d -j DNAT --to-destination $POD_IP", constants.EnvoyUID))
26-
}
13+
// iptablesOutboundStaticRules is the list of iptables rules related to outbound traffic interception and redirection
14+
var iptablesOutboundStaticRules = []string{
15+
// Redirects outbound TCP traffic hitting OSM_PROXY_OUT_REDIRECT chain to Envoy's outbound listener port
16+
fmt.Sprintf("-A OSM_PROXY_OUT_REDIRECT -p tcp -j REDIRECT --to-port %d", constants.EnvoyOutboundListenerPort),
2717

28-
iptablesOutboundStaticRules = append(iptablesOutboundStaticRules, []string{
29-
// For all other outbound TCP traffic jump from OUTPUT chain to OSM_PROXY_OUTBOUND chain
30-
"-A OUTPUT -p tcp -j OSM_PROXY_OUTBOUND",
18+
// Traffic to the Proxy Admin port flows to the Proxy -- not redirected
19+
fmt.Sprintf("-A OSM_PROXY_OUT_REDIRECT -p tcp --dport %d -j ACCEPT", constants.EnvoyAdminPort),
3120

32-
// Outbound traffic from Envoy to the local app over the loopback interface should jump to the inbound proxy redirect chain.
33-
// So when an app directs traffic to itself via the k8s service, traffic flows as follows:
34-
// app -> local envoy's outbound listener -> iptables -> local envoy's inbound listener -> app
35-
fmt.Sprintf("-A OSM_PROXY_OUTBOUND -o lo ! -d 127.0.0.1/32 -m owner --uid-owner %d -j OSM_PROXY_IN_REDIRECT", constants.EnvoyUID),
21+
// For outbound TCP traffic jump from OUTPUT chain to OSM_PROXY_OUTBOUND chain
22+
"-A OUTPUT -p tcp -j OSM_PROXY_OUTBOUND",
3623

37-
// Outbound traffic from the app to itself over the loopback interface is not be redirected via the proxy.
38-
// E.g. when app sends traffic to itself via the pod IP.
39-
fmt.Sprintf("-A OSM_PROXY_OUTBOUND -o lo -m owner ! --uid-owner %d -j RETURN", constants.EnvoyUID),
24+
// Outbound traffic from Envoy to the local app over the loopback interface should jump to the inbound proxy redirect chain.
25+
// So when an app directs traffic to itself via the k8s service, traffic flows as follows:
26+
// app -> local envoy's outbound listener -> iptables -> local envoy's inbound listener -> app
27+
fmt.Sprintf("-A OSM_PROXY_OUTBOUND -o lo ! -d 127.0.0.1/32 -m owner --uid-owner %d -j OSM_PROXY_IN_REDIRECT", constants.EnvoyUID),
4028

41-
// Don't redirect Envoy traffic back to itself, return it to the next chain for processing
42-
fmt.Sprintf("-A OSM_PROXY_OUTBOUND -m owner --uid-owner %d -j RETURN", constants.EnvoyUID),
29+
// Outbound traffic from the app to itself over the loopback interface is not be redirected via the proxy.
30+
// E.g. when app sends traffic to itself via the pod IP.
31+
fmt.Sprintf("-A OSM_PROXY_OUTBOUND -o lo -m owner ! --uid-owner %d -j RETURN", constants.EnvoyUID),
4332

44-
// Skip localhost traffic, doesn't need to be routed via the proxy
45-
"-A OSM_PROXY_OUTBOUND -d 127.0.0.1/32 -j RETURN",
46-
}...)
33+
// Don't redirect Envoy traffic back to itself, return it to the next chain for processing
34+
fmt.Sprintf("-A OSM_PROXY_OUTBOUND -m owner --uid-owner %d -j RETURN", constants.EnvoyUID),
4735

48-
return iptablesOutboundStaticRules
36+
// Skip localhost traffic, doesn't need to be routed via the proxy
37+
"-A OSM_PROXY_OUTBOUND -d 127.0.0.1/32 -j RETURN",
4938
}
5039

5140
// iptablesInboundStaticRules is the list of iptables rules related to inbound traffic interception and redirection
@@ -105,11 +94,16 @@ func generateIptablesCommands(proxyMode configv1alpha2.LocalProxyMode, outboundI
10594
cmds = append(cmds, rule)
10695
}
10796

108-
iptablesOutboundStaticRules := genIPTablesOutboundStaticRules(proxyMode)
109-
11097
// 3. Create outbound rules
11198
cmds = append(cmds, iptablesOutboundStaticRules...)
11299

100+
if proxyMode == configv1alpha2.LocalProxyModePodIP {
101+
// For envoy -> local service container proxying, send traffic to pod IP instead of localhost
102+
// *Note: it is important to use the insert option '-I' instead of the append option '-A' to ensure the
103+
// DNAT to the pod ip for envoy -> localhost traffic happens before the rule that redirects traffic to the proxy
104+
iptablesOutboundStaticRules = append(cmds, fmt.Sprintf("-I OUTPUT -p tcp -o lo -d 127.0.0.1/32 -m owner --uid-owner %d -j DNAT --to-destination $POD_IP", constants.EnvoyUID))
105+
}
106+
113107
// Ignore outbound traffic in specified interfaces
114108
for _, iface := range networkInterfaceExclusionList {
115109
cmds = append(cmds, fmt.Sprintf("-A OSM_PROXY_OUTBOUND -o %s -j RETURN", iface))

0 commit comments

Comments
 (0)