Skip to content

Commit f3e657a

Browse files
committed
Remove implicit inbound ssh firewall rules and change default port
1 parent 0e5dc9d commit f3e657a

File tree

3 files changed

+8
-100
lines changed

3 files changed

+8
-100
lines changed

client/internal/acl/manager.go

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
nberrors "github.com/netbirdio/netbird/client/errors"
1818
firewall "github.com/netbirdio/netbird/client/firewall/manager"
1919
"github.com/netbirdio/netbird/client/internal/acl/id"
20-
"github.com/netbirdio/netbird/client/ssh"
2120
"github.com/netbirdio/netbird/management/domain"
2221
mgmProto "github.com/netbirdio/netbird/management/proto"
2322
)
@@ -86,30 +85,8 @@ func (d *DefaultManager) ApplyFiltering(networkMap *mgmProto.NetworkMap, dnsRout
8685
}
8786

8887
func (d *DefaultManager) applyPeerACLs(networkMap *mgmProto.NetworkMap) {
89-
rules, squashedProtocols := d.squashAcceptRules(networkMap)
88+
rules := d.squashAcceptRules(networkMap)
9089

91-
enableSSH := networkMap.PeerConfig != nil &&
92-
networkMap.PeerConfig.SshConfig != nil &&
93-
networkMap.PeerConfig.SshConfig.SshEnabled
94-
if _, ok := squashedProtocols[mgmProto.RuleProtocol_ALL]; ok {
95-
enableSSH = enableSSH && !ok
96-
}
97-
if _, ok := squashedProtocols[mgmProto.RuleProtocol_TCP]; ok {
98-
enableSSH = enableSSH && !ok
99-
}
100-
101-
// if TCP protocol rules not squashed and SSH enabled
102-
// we add default firewall rule which accepts connection to any peer
103-
// in the network by SSH (TCP 22 port).
104-
if enableSSH {
105-
rules = append(rules, &mgmProto.FirewallRule{
106-
PeerIP: "0.0.0.0",
107-
Direction: mgmProto.RuleDirection_IN,
108-
Action: mgmProto.RuleAction_ACCEPT,
109-
Protocol: mgmProto.RuleProtocol_TCP,
110-
Port: strconv.Itoa(ssh.DefaultSSHPort),
111-
})
112-
}
11390

11491
// if we got empty rules list but management not set networkMap.FirewallRulesIsEmpty flag
11592
// we have old version of management without rules handling, we should allow all traffic
@@ -373,9 +350,7 @@ func (d *DefaultManager) getPeerRuleID(
373350
//
374351
// NOTE: It will not squash two rules for same protocol if one covers all peers in the network,
375352
// but other has port definitions or has drop policy.
376-
func (d *DefaultManager) squashAcceptRules(
377-
networkMap *mgmProto.NetworkMap,
378-
) ([]*mgmProto.FirewallRule, map[mgmProto.RuleProtocol]struct{}) {
353+
func (d *DefaultManager) squashAcceptRules(networkMap *mgmProto.NetworkMap, ) []*mgmProto.FirewallRule {
379354
totalIPs := 0
380355
for _, p := range append(networkMap.RemotePeers, networkMap.OfflinePeers...) {
381356
for range p.AllowedIps {
@@ -479,11 +454,11 @@ func (d *DefaultManager) squashAcceptRules(
479454

480455
// if all protocol was squashed everything is allow and we can ignore all other rules
481456
if _, ok := squashedProtocols[mgmProto.RuleProtocol_ALL]; ok {
482-
return squashedRules, squashedProtocols
457+
return squashedRules
483458
}
484459

485460
if len(squashedRules) == 0 {
486-
return networkMap.FirewallRules, squashedProtocols
461+
return networkMap.FirewallRules
487462
}
488463

489464
var rules []*mgmProto.FirewallRule
@@ -500,7 +475,7 @@ func (d *DefaultManager) squashAcceptRules(
500475
rules = append(rules, r)
501476
}
502477

503-
return append(rules, squashedRules...), squashedProtocols
478+
return append(rules, squashedRules...)
504479
}
505480

506481
// getRuleGroupingSelector takes all rule properties except IP address to build selector

client/internal/acl/manager_test.go

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func TestDefaultManagerSquashRules(t *testing.T) {
249249
}
250250

251251
manager := &DefaultManager{}
252-
rules, _ := manager.squashAcceptRules(networkMap)
252+
rules := manager.squashAcceptRules(networkMap)
253253
assert.Equal(t, 2, len(rules))
254254

255255
r := rules[0]
@@ -326,73 +326,6 @@ func TestDefaultManagerSquashRulesNoAffect(t *testing.T) {
326326
}
327327

328328
manager := &DefaultManager{}
329-
rules, _ := manager.squashAcceptRules(networkMap)
329+
rules := manager.squashAcceptRules(networkMap)
330330
assert.Equal(t, len(networkMap.FirewallRules), len(rules))
331331
}
332-
333-
func TestDefaultManagerEnableSSHRules(t *testing.T) {
334-
networkMap := &mgmProto.NetworkMap{
335-
PeerConfig: &mgmProto.PeerConfig{
336-
SshConfig: &mgmProto.SSHConfig{
337-
SshEnabled: true,
338-
},
339-
},
340-
RemotePeers: []*mgmProto.RemotePeerConfig{
341-
{AllowedIps: []string{"10.93.0.1"}},
342-
{AllowedIps: []string{"10.93.0.2"}},
343-
{AllowedIps: []string{"10.93.0.3"}},
344-
},
345-
FirewallRules: []*mgmProto.FirewallRule{
346-
{
347-
PeerIP: "10.93.0.1",
348-
Direction: mgmProto.RuleDirection_IN,
349-
Action: mgmProto.RuleAction_ACCEPT,
350-
Protocol: mgmProto.RuleProtocol_TCP,
351-
},
352-
{
353-
PeerIP: "10.93.0.2",
354-
Direction: mgmProto.RuleDirection_IN,
355-
Action: mgmProto.RuleAction_ACCEPT,
356-
Protocol: mgmProto.RuleProtocol_TCP,
357-
},
358-
{
359-
PeerIP: "10.93.0.3",
360-
Direction: mgmProto.RuleDirection_OUT,
361-
Action: mgmProto.RuleAction_ACCEPT,
362-
Protocol: mgmProto.RuleProtocol_UDP,
363-
},
364-
},
365-
}
366-
367-
ctrl := gomock.NewController(t)
368-
defer ctrl.Finish()
369-
370-
ifaceMock := mocks.NewMockIFaceMapper(ctrl)
371-
ifaceMock.EXPECT().IsUserspaceBind().Return(true).AnyTimes()
372-
ifaceMock.EXPECT().SetFilter(gomock.Any())
373-
network := netip.MustParsePrefix("172.0.0.1/32")
374-
375-
ifaceMock.EXPECT().Name().Return("lo").AnyTimes()
376-
ifaceMock.EXPECT().Address().Return(wgaddr.Address{
377-
IP: network.Addr(),
378-
Network: network,
379-
}).AnyTimes()
380-
ifaceMock.EXPECT().GetWGDevice().Return(nil).AnyTimes()
381-
382-
fw, err := firewall.NewFirewall(ifaceMock, nil, flowLogger, false)
383-
require.NoError(t, err)
384-
defer func() {
385-
err = fw.Close(nil)
386-
require.NoError(t, err)
387-
}()
388-
389-
acl := NewDefaultManager(fw)
390-
391-
acl.ApplyFiltering(networkMap, false)
392-
393-
expectedRules := 3
394-
if fw.IsStateful() {
395-
expectedRules = 3 // 2 inbound rules + SSH rule
396-
}
397-
assert.Equal(t, expectedRules, len(acl.peerRulesPairs))
398-
}

client/ssh/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
// DefaultSSHPort is the default SSH port of the NetBird's embedded SSH server
21-
const DefaultSSHPort = 44338
21+
const DefaultSSHPort = 22022
2222

2323
// TerminalTimeout is the timeout for terminal session to be ready
2424
const TerminalTimeout = 10 * time.Second

0 commit comments

Comments
 (0)