-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat(routing): implemented back iptables filtering #360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,7 +35,11 @@ const ( | |||||||||
| RoutingModeDefault = RoutingModeBPF | ||||||||||
| RoutingModeBPF = "eBPF" | ||||||||||
| RoutingModeLoopback = "Loopback" | ||||||||||
| RoutingModeIPTables = "IPTables" | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should do it in lowercase? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think uppercase like this is fine 👍 |
||||||||||
|
|
||||||||||
| GKEAnnotationServiceAccount = GroupGKE + "/gcp-service-account" | ||||||||||
| GKELabelNodeEnabled = GroupGKE + "/gke-metadata-server-enabled" | ||||||||||
|
|
||||||||||
| GKEMetadataServerAddressDefault = "169.254.169.254" | ||||||||||
| GKEMetadataServerPortDefault = 80 | ||||||||||
|
Comment on lines
+43
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice one, please check if we have these values anywhere else in the code and use these constants 🙏 |
||||||||||
| ) | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,13 +14,14 @@ require ( | |
| github.com/sirupsen/logrus v1.9.3 | ||
| github.com/spf13/pflag v1.0.6 | ||
| github.com/stretchr/testify v1.10.0 | ||
| github.com/vishvananda/netlink v1.3.0 | ||
| github.com/vishvananda/netlink v1.3.1-0.20250206174618-62fb240731fa | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this is needed but I think i ran There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But the CI failed it ran There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was also a diff on the file |
||
| golang.org/x/oauth2 v0.28.0 | ||
| google.golang.org/api v0.226.0 | ||
| k8s.io/api v0.32.3 | ||
| k8s.io/apimachinery v0.32.3 | ||
| k8s.io/client-go v0.32.3 | ||
| k8s.io/klog/v2 v2.130.1 | ||
| k8s.io/kubernetes v1.32.3 | ||
| sigs.k8s.io/controller-runtime v0.20.3 | ||
| ) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||||
| package iptables | ||||||||
|
|
||||||||
| import ( | ||||||||
| "errors" | ||||||||
| "fmt" | ||||||||
| "net/netip" | ||||||||
| "strconv" | ||||||||
|
|
||||||||
| "github.com/matheuscscp/gke-metadata-server/api" | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We put internal imports in a separate import block between the standard libraries and third party imports
Suggested change
|
||||||||
| "k8s.io/kubernetes/pkg/util/iptables" | ||||||||
| "k8s.io/utils/exec" | ||||||||
| ) | ||||||||
|
|
||||||||
| func LoadAndAttach(emulatorAddr netip.Addr, emulatorPort int) func() (func() error, error) { | ||||||||
| return func() (func() error, error) { | ||||||||
| // Create the following iptables rules: | ||||||||
| // iptables -t nat -A OUTPUT -d 169.254.169.254 -p tcp --dport 80 -j DNAT --to-destination <emulatorAddr> | ||||||||
| // iptables -A FORWARD -d <emulatorIP> -p tcp --dport <emulatorPort> -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT | ||||||||
|
|
||||||||
| // This rule essentially rewrites the destination of packets targeting the | ||||||||
| // GKE metadata server with the ip:port address of the emulator, i.e. it | ||||||||
| // effectively modifies the destination fields of matching packets. | ||||||||
| ipTables := iptables.New(exec.New(), iptables.ProtocolIPv4) | ||||||||
|
|
||||||||
| // match conditions | ||||||||
| natTableArgs := [...]string{ | ||||||||
lukas8219 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| "-d", api.GKEMetadataServerAddressDefault, | ||||||||
| "-p", "tcp", | ||||||||
| "--dport", strconv.Itoa(api.GKEMetadataServerPortDefault), | ||||||||
|
|
||||||||
| // action taken | ||||||||
| "-j", "DNAT", | ||||||||
| "--to-destination", emulatorAddr.String(), | ||||||||
| } | ||||||||
|
|
||||||||
| filterTableArgs := [...]string{ | ||||||||
| "-d", emulatorAddr.String(), | ||||||||
| "-p", "tcp", | ||||||||
| "--dport", strconv.Itoa(emulatorPort), | ||||||||
| "-m", "state", "--state", "NEW,ESTABLISHED,RELATED", // new or established connections | ||||||||
| // action taken | ||||||||
| "-j", "ACCEPT", | ||||||||
| } | ||||||||
|
|
||||||||
| close := func() error { | ||||||||
| err1 := ipTables.DeleteRule( | ||||||||
| iptables.Table(iptables.TableNAT), | ||||||||
| iptables.ChainOutput, | ||||||||
| natTableArgs[:]..., | ||||||||
lukas8219 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| ) | ||||||||
| err2 := ipTables.DeleteRule( | ||||||||
| iptables.Table(iptables.TableNAT), | ||||||||
| iptables.ChainForward, | ||||||||
| filterTableArgs[:]..., | ||||||||
| ) | ||||||||
| return errors.Join(err1, err2) | ||||||||
lukas8219 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| } | ||||||||
|
|
||||||||
| _, err := ipTables.EnsureRule( | ||||||||
| iptables.Append, | ||||||||
| iptables.TableNAT, // NAT rules are applied before routing | ||||||||
| iptables.ChainOutput, // output chain is for locally generated traffic | ||||||||
| natTableArgs[:]..., | ||||||||
| ) | ||||||||
| if err != nil { | ||||||||
| return nil, fmt.Errorf("error adding DNAT rule: %w", err) | ||||||||
| } | ||||||||
|
|
||||||||
| // This rule ensures that packets destined to the emulator IP and port | ||||||||
| // are accepted to be forwarded by the host, i.e. prevents the host from | ||||||||
| // dropping matching packets. | ||||||||
| _, err = ipTables.EnsureRule( | ||||||||
matheuscscp marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| iptables.Append, | ||||||||
| iptables.TableFilter, // filter table is for access control (should packets be forwarded or dropped?) | ||||||||
| iptables.ChainForward, // forward chain is for packets that are being routed, i.e. not destined to the local host | ||||||||
| filterTableArgs[:]..., | ||||||||
| ) | ||||||||
| return close, err | ||||||||
| } | ||||||||
| } | ||||||||
Uh oh!
There was an error while loading. Please reload this page.