Skip to content

Commit ee588e1

Browse files
Revert "[management] allow local routing peer resource (#5814)" (#5847)
1 parent 2a8aacc commit ee588e1

7 files changed

Lines changed: 50 additions & 134 deletions

File tree

management/server/networks/resources/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (m *managerImpl) CreateResource(ctx context.Context, userID string, resourc
108108
return nil, status.NewPermissionDeniedError()
109109
}
110110

111-
resource, err = types.NewNetworkResource(resource.AccountID, resource.NetworkID, resource.Name, resource.Description, resource.Address, resource.GroupIDs, resource.OnRoutingPeer, resource.Enabled)
111+
resource, err = types.NewNetworkResource(resource.AccountID, resource.NetworkID, resource.Name, resource.Description, resource.Address, resource.GroupIDs, resource.Enabled)
112112
if err != nil {
113113
return nil, fmt.Errorf("failed to create new network resource: %w", err)
114114
}

management/server/networks/resources/types/resource.go

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,37 @@ func (p NetworkResourceType) String() string {
2929
}
3030

3131
type NetworkResource struct {
32-
ID string `gorm:"primaryKey"`
33-
NetworkID string `gorm:"index"`
34-
AccountID string `gorm:"index"`
35-
Name string
36-
Description string
37-
Type NetworkResourceType
38-
Address string `gorm:"-"`
39-
GroupIDs []string `gorm:"-"`
40-
Domain string
41-
Prefix netip.Prefix `gorm:"serializer:json"`
42-
Enabled bool
43-
OnRoutingPeer bool
32+
ID string `gorm:"primaryKey"`
33+
NetworkID string `gorm:"index"`
34+
AccountID string `gorm:"index"`
35+
Name string
36+
Description string
37+
Type NetworkResourceType
38+
Address string `gorm:"-"`
39+
GroupIDs []string `gorm:"-"`
40+
Domain string
41+
Prefix netip.Prefix `gorm:"serializer:json"`
42+
Enabled bool
4443
}
4544

46-
func NewNetworkResource(accountID, networkID, name, description, address string, groupIDs []string, onRoutingPeer, enabled bool) (*NetworkResource, error) {
45+
func NewNetworkResource(accountID, networkID, name, description, address string, groupIDs []string, enabled bool) (*NetworkResource, error) {
4746
resourceType, domain, prefix, err := GetResourceType(address)
4847
if err != nil {
4948
return nil, fmt.Errorf("invalid address: %w", err)
5049
}
5150

5251
return &NetworkResource{
53-
ID: xid.New().String(),
54-
AccountID: accountID,
55-
NetworkID: networkID,
56-
Name: name,
57-
Description: description,
58-
Type: resourceType,
59-
Address: address,
60-
Domain: domain,
61-
Prefix: prefix,
62-
GroupIDs: groupIDs,
63-
Enabled: enabled,
64-
OnRoutingPeer: onRoutingPeer,
52+
ID: xid.New().String(),
53+
AccountID: accountID,
54+
NetworkID: networkID,
55+
Name: name,
56+
Description: description,
57+
Type: resourceType,
58+
Address: address,
59+
Domain: domain,
60+
Prefix: prefix,
61+
GroupIDs: groupIDs,
62+
Enabled: enabled,
6563
}, nil
6664
}
6765

@@ -72,14 +70,13 @@ func (n *NetworkResource) ToAPIResponse(groups []api.GroupMinimum) *api.NetworkR
7270
}
7371

7472
return &api.NetworkResource{
75-
Id: n.ID,
76-
Name: n.Name,
77-
Description: &n.Description,
78-
Type: api.NetworkResourceType(n.Type.String()),
79-
Address: addr,
80-
Groups: groups,
81-
Enabled: n.Enabled,
82-
OnRoutingPeer: &n.OnRoutingPeer,
73+
Id: n.ID,
74+
Name: n.Name,
75+
Description: &n.Description,
76+
Type: api.NetworkResourceType(n.Type.String()),
77+
Address: addr,
78+
Groups: groups,
79+
Enabled: n.Enabled,
8380
}
8481
}
8582

@@ -89,28 +86,24 @@ func (n *NetworkResource) FromAPIRequest(req *api.NetworkResourceRequest) {
8986
if req.Description != nil {
9087
n.Description = *req.Description
9188
}
92-
if req.OnRoutingPeer != nil {
93-
n.OnRoutingPeer = *req.OnRoutingPeer
94-
}
9589
n.Address = req.Address
9690
n.GroupIDs = req.Groups
9791
n.Enabled = req.Enabled
9892
}
9993

10094
func (n *NetworkResource) Copy() *NetworkResource {
10195
return &NetworkResource{
102-
ID: n.ID,
103-
AccountID: n.AccountID,
104-
NetworkID: n.NetworkID,
105-
Name: n.Name,
106-
Description: n.Description,
107-
Type: n.Type,
108-
Address: n.Address,
109-
Domain: n.Domain,
110-
Prefix: n.Prefix,
111-
GroupIDs: n.GroupIDs,
112-
Enabled: n.Enabled,
113-
OnRoutingPeer: n.OnRoutingPeer,
96+
ID: n.ID,
97+
AccountID: n.AccountID,
98+
NetworkID: n.NetworkID,
99+
Name: n.Name,
100+
Description: n.Description,
101+
Type: n.Type,
102+
Address: n.Address,
103+
Domain: n.Domain,
104+
Prefix: n.Prefix,
105+
GroupIDs: n.GroupIDs,
106+
Enabled: n.Enabled,
114107
}
115108
}
116109

management/server/store/sql_store.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,7 @@ func (s *SqlStore) getNetworkRouters(ctx context.Context, accountID string) ([]*
22912291
}
22922292

22932293
func (s *SqlStore) getNetworkResources(ctx context.Context, accountID string) ([]*resourceTypes.NetworkResource, error) {
2294-
const query = `SELECT id, network_id, account_id, name, description, type, domain, prefix, enabled, on_routing_peer FROM network_resources WHERE account_id = $1`
2294+
const query = `SELECT id, network_id, account_id, name, description, type, domain, prefix, enabled FROM network_resources WHERE account_id = $1`
22952295
rows, err := s.pool.Query(ctx, query, accountID)
22962296
if err != nil {
22972297
return nil, err
@@ -2300,15 +2300,11 @@ func (s *SqlStore) getNetworkResources(ctx context.Context, accountID string) ([
23002300
var r resourceTypes.NetworkResource
23012301
var prefix []byte
23022302
var enabled sql.NullBool
2303-
var onRoutingPeer sql.NullBool
2304-
err := row.Scan(&r.ID, &r.NetworkID, &r.AccountID, &r.Name, &r.Description, &r.Type, &r.Domain, &prefix, &enabled, &onRoutingPeer)
2303+
err := row.Scan(&r.ID, &r.NetworkID, &r.AccountID, &r.Name, &r.Description, &r.Type, &r.Domain, &prefix, &enabled)
23052304
if err == nil {
23062305
if enabled.Valid {
23072306
r.Enabled = enabled.Bool
23082307
}
2309-
if onRoutingPeer.Valid {
2310-
r.OnRoutingPeer = onRoutingPeer.Bool
2311-
}
23122308
if prefix != nil {
23132309
_ = json.Unmarshal(prefix, &r.Prefix)
23142310
}

management/server/store/sql_store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2508,7 +2508,7 @@ func TestSqlStore_SaveNetworkResource(t *testing.T) {
25082508
accountID := "bf1c8084-ba50-4ce7-9439-34653001fc3b"
25092509
networkID := "ct286bi7qv930dsrrug0"
25102510

2511-
netResource, err := resourceTypes.NewNetworkResource(accountID, networkID, "resource-name", "", "example.com", []string{}, false, true)
2511+
netResource, err := resourceTypes.NewNetworkResource(accountID, networkID, "resource-name", "", "example.com", []string{}, true)
25122512
require.NoError(t, err)
25132513

25142514
err = store.SaveNetworkResource(context.Background(), netResource)

management/server/types/networkmap_components.go

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package types
22

33
import (
44
"context"
5+
"maps"
56
"net"
67
"net/netip"
78
"slices"
@@ -16,7 +17,6 @@ import (
1617
nbpeer "github.com/netbirdio/netbird/management/server/peer"
1718
"github.com/netbirdio/netbird/route"
1819
"github.com/netbirdio/netbird/shared/management/domain"
19-
"golang.org/x/exp/maps"
2020
)
2121

2222
const EnvNewNetworkMapCompacted = "NB_NETWORK_MAP_COMPACTED"
@@ -119,10 +119,9 @@ func (c *NetworkMapComponents) Calculate(ctx context.Context) *NetworkMap {
119119
routesUpdate := c.getRoutesToSync(targetPeerID, peersToConnect, peerGroups)
120120
routesFirewallRules := c.getPeerRoutesFirewallRules(ctx, targetPeerID)
121121

122-
isRouter, networkResourcesRoutes, sourcePeers, peerFirewallRules := c.getNetworkResourcesRoutesToSync(targetPeerID)
122+
isRouter, networkResourcesRoutes, sourcePeers := c.getNetworkResourcesRoutesToSync(targetPeerID)
123123
var networkResourcesFirewallRules []*RouteFirewallRule
124124
if isRouter {
125-
firewallRules = append(firewallRules, peerFirewallRules...)
126125
networkResourcesFirewallRules = c.getPeerNetworkResourceFirewallRules(ctx, targetPeerID, networkResourcesRoutes)
127126
}
128127

@@ -527,6 +526,7 @@ func (c *NetworkMapComponents) getRoutingPeerRoutes(peerID string) (enabledRoute
527526
return enabledRoutes, disabledRoutes
528527
}
529528

529+
530530
func (c *NetworkMapComponents) filterRoutesByGroups(routes []*route.Route, groupListMap LookupMap) []*route.Route {
531531
var filteredRoutes []*route.Route
532532
for _, r := range routes {
@@ -692,11 +692,10 @@ func (c *NetworkMapComponents) getRulePeers(rule *PolicyRule, postureChecks []st
692692
return distributionGroupPeers
693693
}
694694

695-
func (c *NetworkMapComponents) getNetworkResourcesRoutesToSync(peerID string) (bool, []*route.Route, map[string]struct{}, []*FirewallRule) {
695+
func (c *NetworkMapComponents) getNetworkResourcesRoutesToSync(peerID string) (bool, []*route.Route, map[string]struct{}) {
696696
var isRoutingPeer bool
697697
var routes []*route.Route
698698
allSourcePeers := make(map[string]struct{})
699-
localResourceFwRule := make([]*FirewallRule, 0)
700699

701700
for _, resource := range c.NetworkResources {
702701
if !resource.Enabled {
@@ -715,9 +714,6 @@ func (c *NetworkMapComponents) getNetworkResourcesRoutesToSync(peerID string) (b
715714

716715
addedResourceRoute := false
717716
for _, policy := range c.ResourcePoliciesMap[resource.ID] {
718-
if isRoutingPeer && resource.OnRoutingPeer {
719-
localResourceFwRule = append(localResourceFwRule, c.getLocalResourceFirewallRules(policy)...)
720-
}
721717
var peers []string
722718
if policy.Rules[0].SourceResource.Type == ResourceTypePeer && policy.Rules[0].SourceResource.ID != "" {
723719
peers = []string{policy.Rules[0].SourceResource.ID}
@@ -740,63 +736,7 @@ func (c *NetworkMapComponents) getNetworkResourcesRoutesToSync(peerID string) (b
740736
}
741737
}
742738

743-
return isRoutingPeer, routes, allSourcePeers, localResourceFwRule
744-
}
745-
746-
func (c *NetworkMapComponents) getLocalResourceFirewallRules(policy *Policy) []*FirewallRule {
747-
sourcePeerIDs := c.getPoliciesSourcePeers([]*Policy{policy})
748-
postureValidatedPeerIDs := c.getPostureValidPeers(maps.Keys(sourcePeerIDs), policy.SourcePostureChecks)
749-
750-
rules := make([]*FirewallRule, 0)
751-
for _, rule := range policy.Rules {
752-
if !rule.Enabled {
753-
continue
754-
}
755-
756-
protocol := rule.Protocol
757-
if protocol == PolicyRuleProtocolNetbirdSSH {
758-
continue
759-
}
760-
761-
for _, peerID := range postureValidatedPeerIDs {
762-
peer := c.GetPeerInfo(peerID)
763-
if peer == nil {
764-
continue
765-
}
766-
peerIP := peer.IP.String()
767-
768-
fr := FirewallRule{
769-
PolicyID: rule.ID,
770-
PeerIP: peerIP,
771-
Direction: FirewallRuleDirectionIN,
772-
Action: string(rule.Action),
773-
Protocol: string(protocol),
774-
}
775-
776-
if len(rule.Ports) == 0 && len(rule.PortRanges) == 0 {
777-
rules = append(rules, &fr)
778-
continue
779-
}
780-
781-
for _, port := range rule.Ports {
782-
portRule := fr
783-
portRule.Port = port
784-
rules = append(rules, &portRule)
785-
}
786-
787-
for _, portRange := range rule.PortRanges {
788-
if len(rule.Ports) > 0 {
789-
break
790-
}
791-
rangeRule := fr
792-
rangeRule.PortRange = portRange
793-
rules = append(rules, &rangeRule)
794-
}
795-
796-
}
797-
}
798-
799-
return rules
739+
return isRoutingPeer, routes, allSourcePeers
800740
}
801741

802742
func (c *NetworkMapComponents) getNetworkResourcesRoutes(resource *resourceTypes.NetworkResource, peerID string, router *routerTypes.NetworkRouter) []*route.Route {

shared/management/http/api/openapi.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,10 +1980,6 @@ components:
19801980
description: Network resource status
19811981
type: boolean
19821982
example: true
1983-
on_routing_peer:
1984-
description: Indicate if the resource is on a routing peer or not. It is needed if the resource is targeting the IP of the routing peer itself
1985-
type: boolean
1986-
example: true
19871983
required:
19881984
- name
19891985
- address

shared/management/http/api/types.gen.go

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)