forked from siderolabs/talos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement ingress firewall rules
Fixes siderolabs#4421 Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
- Loading branch information
Showing
34 changed files
with
1,368 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
internal/app/machined/pkg/controllers/network/nftables_chain_config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package network | ||
|
||
import ( | ||
"cmp" | ||
"context" | ||
"fmt" | ||
"slices" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/safe" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"github.com/siderolabs/gen/optional" | ||
"github.com/siderolabs/gen/xslices" | ||
"github.com/siderolabs/go-pointer" | ||
"go.uber.org/zap" | ||
|
||
v1alpha1runtime "github.com/siderolabs/talos/internal/app/machined/pkg/runtime" | ||
"github.com/siderolabs/talos/pkg/machinery/constants" | ||
"github.com/siderolabs/talos/pkg/machinery/nethelpers" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/config" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/network" | ||
) | ||
|
||
// NfTablesChainConfigController generates configuration for kmsg log delivery. | ||
type NfTablesChainConfigController struct { | ||
V1Alpha1Mode v1alpha1runtime.Mode | ||
} | ||
|
||
// Name implements controller.Controller interface. | ||
func (ctrl *NfTablesChainConfigController) Name() string { | ||
return "network.NfTablesChainConfigController" | ||
} | ||
|
||
// Inputs implements controller.Controller interface. | ||
func (ctrl *NfTablesChainConfigController) Inputs() []controller.Input { | ||
return []controller.Input{ | ||
{ | ||
Namespace: config.NamespaceName, | ||
Type: config.MachineConfigType, | ||
ID: optional.Some(config.V1Alpha1ID), | ||
Kind: controller.InputWeak, | ||
}, | ||
} | ||
} | ||
|
||
// Outputs implements controller.Controller interface. | ||
func (ctrl *NfTablesChainConfigController) Outputs() []controller.Output { | ||
return []controller.Output{ | ||
{ | ||
Type: network.NfTablesChainType, | ||
Kind: controller.OutputShared, | ||
}, | ||
} | ||
} | ||
|
||
// Run implements controller.Controller interface. | ||
// | ||
//nolint:gocyclo | ||
func (ctrl *NfTablesChainConfigController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) (err error) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-r.EventCh(): | ||
} | ||
|
||
cfg, err := safe.ReaderGetByID[*config.MachineConfig](ctx, r, config.V1Alpha1ID) | ||
if err != nil && !state.IsNotFoundError(err) { | ||
return fmt.Errorf("error getting machine config: %w", err) | ||
} | ||
|
||
r.StartTrackingOutputs() | ||
|
||
if cfg != nil { | ||
if err = safe.WriterModify(ctx, r, network.NewNfTablesChain(network.NamespaceName, "ingress"), | ||
func(chain *network.NfTablesChain) error { | ||
spec := chain.TypedSpec() | ||
|
||
spec.Type = nethelpers.ChainTypeFilter | ||
spec.Hook = nethelpers.ChainHookInput | ||
spec.Priority = nethelpers.ChainPriorityFilter | ||
|
||
// preamble | ||
spec.Rules = []network.NfTablesRule{ | ||
// trusted interfaces: loopback, siderolink and kubespan | ||
{ | ||
MatchIIfName: &network.NfTablesIfNameMatch{ | ||
InterfaceName: "lo", | ||
Operator: nethelpers.OperatorEqual, | ||
}, | ||
Verdict: pointer.To(nethelpers.VerdictAccept), | ||
}, | ||
{ | ||
MatchIIfName: &network.NfTablesIfNameMatch{ | ||
InterfaceName: constants.SideroLinkName, | ||
Operator: nethelpers.OperatorEqual, | ||
}, | ||
Verdict: pointer.To(nethelpers.VerdictAccept), | ||
}, | ||
{ | ||
MatchIIfName: &network.NfTablesIfNameMatch{ | ||
InterfaceName: constants.KubeSpanLinkName, | ||
Operator: nethelpers.OperatorEqual, | ||
}, | ||
Verdict: pointer.To(nethelpers.VerdictAccept), | ||
}, | ||
} | ||
|
||
defaultAction := cfg.Config().NetworkRules().DefaultAction() | ||
|
||
if defaultAction == nethelpers.DefaultActionBlock { | ||
// allow ICMP and ICMPv6 explicitly | ||
spec.Rules = append(spec.Rules, | ||
network.NfTablesRule{ | ||
MatchLayer4: &network.NfTablesLayer4Match{ | ||
Protocol: nethelpers.ProtocolICMP, | ||
}, | ||
Verdict: pointer.To(nethelpers.VerdictAccept), | ||
}, | ||
network.NfTablesRule{ | ||
MatchLayer4: &network.NfTablesLayer4Match{ | ||
Protocol: nethelpers.ProtocolICMPv6, | ||
}, | ||
Verdict: pointer.To(nethelpers.VerdictAccept), | ||
}, | ||
) | ||
} | ||
|
||
for _, rule := range cfg.Config().NetworkRules().Rules() { | ||
portRanges := rule.PortRanges() | ||
|
||
// sort port ranges, machine config validation ensures that there are no overlaps | ||
slices.SortFunc(portRanges, func(a, b [2]uint16) int { | ||
return cmp.Compare(a[0], b[0]) | ||
}) | ||
|
||
// if default accept, drop anything that doesn't match the rule | ||
verdict := nethelpers.VerdictDrop | ||
|
||
if defaultAction == nethelpers.DefaultActionBlock { | ||
verdict = nethelpers.VerdictAccept | ||
} | ||
|
||
spec.Rules = append(spec.Rules, | ||
network.NfTablesRule{ | ||
MatchSourceAddress: &network.NfTablesAddressMatch{ | ||
IncludeSubnets: rule.Subnets(), | ||
ExcludeSubnets: rule.ExceptSubnets(), | ||
Invert: defaultAction == nethelpers.DefaultActionAccept, | ||
}, | ||
MatchLayer4: &network.NfTablesLayer4Match{ | ||
Protocol: rule.Protocol(), | ||
MatchDestinationPort: &network.NfTablesPortMatch{ | ||
Ranges: xslices.Map(portRanges, func(pr [2]uint16) network.PortRange { | ||
return network.PortRange{Lo: pr[0], Hi: pr[1]} | ||
}), | ||
}, | ||
}, | ||
Verdict: pointer.To(verdict), | ||
}, | ||
) | ||
} | ||
|
||
if defaultAction == nethelpers.DefaultActionBlock { | ||
// drop everything else | ||
spec.Rules = append(spec.Rules, | ||
network.NfTablesRule{ | ||
Verdict: pointer.To(nethelpers.VerdictDrop), | ||
}, | ||
) | ||
} | ||
|
||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err = safe.CleanupOutputs[*network.NfTablesChain](ctx, r); err != nil { | ||
return err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.