diff --git a/internal/machine/cluster.go b/internal/machine/cluster.go index 9dab4f5d..ea14bdfe 100644 --- a/internal/machine/cluster.go +++ b/internal/machine/cluster.go @@ -92,7 +92,7 @@ func newClusterController( func (cc *clusterController) Run(ctx context.Context) error { defer close(cc.stopped) - if err := firewall.ConfigureIptablesChains(); err != nil { + if err := firewall.ConfigureIptablesChains(network.MachineIP(cc.state.Network.Subnet)); err != nil { return fmt.Errorf("configure iptables chains: %w", err) } diff --git a/internal/machine/firewall/iptables_darwin.go b/internal/machine/firewall/iptables_darwin.go index 046ec6fc..e6de392e 100644 --- a/internal/machine/firewall/iptables_darwin.go +++ b/internal/machine/firewall/iptables_darwin.go @@ -1,9 +1,12 @@ package firewall -import "fmt" +import ( + "fmt" + "net/netip" +) // ConfigureIptablesChains is a stub for Darwin. -func ConfigureIptablesChains() error { +func ConfigureIptablesChains(machineIP netip.Addr) error { return fmt.Errorf("not supported on Darwin") } diff --git a/internal/machine/firewall/iptables_linux.go b/internal/machine/firewall/iptables_linux.go index 95555ca0..ee637786 100644 --- a/internal/machine/firewall/iptables_linux.go +++ b/internal/machine/firewall/iptables_linux.go @@ -3,6 +3,7 @@ package firewall import ( "fmt" "log/slog" + "net/netip" "strconv" "strings" @@ -18,7 +19,7 @@ const ( ) // ConfigureIptablesChains sets up custom iptables chains and initial firewall rules for Uncloud networking. -func ConfigureIptablesChains() error { +func ConfigureIptablesChains(machineIP netip.Addr) error { if err := createIptablesChains(); err != nil { return err } @@ -27,10 +28,24 @@ func ConfigureIptablesChains() error { ipt6 := iptables.GetIptable(iptables.IPv6) // Allow WireGuard traffic to the machine. - acceptWireGuardRule := []string{"-p", "udp", "--dport", strconv.Itoa(network.WireGuardPort), "-j", "ACCEPT"} - err := ipt4.ProgramRule(iptables.Filter, UncloudInputChain, iptables.Insert, acceptWireGuardRule) - if err != nil { - return fmt.Errorf("insert iptables rule '%s': %w", strings.Join(acceptWireGuardRule, " "), err) + acceptWireGuardRule := []string{ + "-p", "udp", + "--dport", strconv.Itoa(network.WireGuardPort), + "-j", "ACCEPT", + } + // Allow cluster machines to access the unregistry (embedded image registry) on the machine to push/pull images. + // TODO: allow access only from the machine IPs (10.210.N.1) but not the containers running on them. Use ipset? + acceptUnregistryRule := []string{ + "-i", network.WireGuardInterfaceName, + "-d", machineIP.String(), + "-p", "tcp", + "--dport", strconv.Itoa(constants.UnregistryPort), + "-j", "ACCEPT", + } + for _, rule := range [][]string{acceptUnregistryRule, acceptWireGuardRule} { + if err := ipt4.ProgramRule(iptables.Filter, UncloudInputChain, iptables.Insert, rule); err != nil { + return fmt.Errorf("insert iptables rule '%s': %w", strings.Join(rule, " "), err) + } } // Allow cluster machines to access Machine API via the management IPv6 WireGuard network. @@ -50,7 +65,7 @@ func ConfigureIptablesChains() error { "-j", "ACCEPT", } for _, rule := range [][]string{acceptMachineAPIRule, acceptCorrosionGossipRule} { - if err = ipt6.ProgramRule(iptables.Filter, UncloudInputChain, iptables.Insert, rule); err != nil { + if err := ipt6.ProgramRule(iptables.Filter, UncloudInputChain, iptables.Insert, rule); err != nil { return fmt.Errorf("insert ip6tables rule '%s': %w", strings.Join(rule, " "), err) } }