Skip to content

Commit

Permalink
[management] exclude self from network map if self is routing peer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-fischer authored Jan 2, 2025
1 parent 782e3f8 commit bc013e4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ func Test_SetupKeys_GetAll(t *testing.T) {
return tc.expectedResponse[i].UsageLimit < tc.expectedResponse[j].UsageLimit
})

for i, _ := range tc.expectedResponse {
for i := range tc.expectedResponse {
validateCreatedKey(t, tc.expectedResponse[i], &got[i])

key, err := am.GetSetupKey(context.Background(), testing_tools.TestAccountId, testing_tools.TestUserId, got[i].Id)
Expand Down
1 change: 1 addition & 0 deletions management/server/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func (a *Account) addNetworksRoutingPeers(
}

delete(sourcePeers, peer.ID)
delete(networkRoutesPeers, peer.ID)

for _, existingPeer := range peersToConnect {
delete(sourcePeers, existingPeer.ID)
Expand Down
49 changes: 41 additions & 8 deletions management/server/types/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,12 @@ func Test_AddNetworksRoutingPeersAddsMissingPeers(t *testing.T) {

func Test_AddNetworksRoutingPeersIgnoresExistingPeers(t *testing.T) {
account := setupTestAccount()
peer := &nbpeer.Peer{Key: "peer1"}
peer := &nbpeer.Peer{Key: "peer1Key", ID: "peer1"}
networkResourcesRoutes := []*route.Route{
{Peer: "peer2Key"},
}
peersToConnect := []*nbpeer.Peer{
{Key: "peer2Key"},
{Key: "peer2Key", ID: "peer2"},
}
expiredPeers := []*nbpeer.Peer{}

Expand All @@ -352,26 +352,41 @@ func Test_AddNetworksRoutingPeersIgnoresExistingPeers(t *testing.T) {

func Test_AddNetworksRoutingPeersAddsExpiredPeers(t *testing.T) {
account := setupTestAccount()
peer := &nbpeer.Peer{Key: "peer1Key"}
peer := &nbpeer.Peer{Key: "peer1Key", ID: "peer1"}
networkResourcesRoutes := []*route.Route{
{Peer: "peer2Key"},
{Peer: "peer3Key"},
{Peer: "peer2Key", PeerID: "peer2"},
{Peer: "peer3Key", PeerID: "peer3"},
}
peersToConnect := []*nbpeer.Peer{
{Key: "peer2Key"},
{Key: "peer2Key", ID: "peer2"},
}
expiredPeers := []*nbpeer.Peer{
{Key: "peer3Key"},
{Key: "peer3Key", ID: "peer3"},
}

result := account.addNetworksRoutingPeers(networkResourcesRoutes, peer, peersToConnect, expiredPeers, false, map[string]struct{}{})
require.Len(t, result, 1)
require.Equal(t, "peer2Key", result[0].Key)
}

func Test_AddNetworksRoutingPeersExcludesSelf(t *testing.T) {
account := setupTestAccount()
peer := &nbpeer.Peer{Key: "peer1Key", ID: "peer1"}
networkResourcesRoutes := []*route.Route{
{Peer: "peer1Key", PeerID: "peer1"},
{Peer: "peer2Key", PeerID: "peer2"},
}
peersToConnect := []*nbpeer.Peer{}
expiredPeers := []*nbpeer.Peer{}

result := account.addNetworksRoutingPeers(networkResourcesRoutes, peer, peersToConnect, expiredPeers, true, map[string]struct{}{})
require.Len(t, result, 1)
require.Equal(t, "peer2Key", result[0].Key)
}

func Test_AddNetworksRoutingPeersHandlesNoMissingPeers(t *testing.T) {
account := setupTestAccount()
peer := &nbpeer.Peer{Key: "peer1"}
peer := &nbpeer.Peer{Key: "peer1key", ID: "peer1"}
networkResourcesRoutes := []*route.Route{}
peersToConnect := []*nbpeer.Peer{}
expiredPeers := []*nbpeer.Peer{}
Expand Down Expand Up @@ -755,3 +770,21 @@ func Test_NetworksNetMapGenWithTwoPostureChecks(t *testing.T) {
t.Errorf("%s should not have source range of peer2 %s", rules[0].SourceRanges, accNetResourcePeer2IP.String())
}
}

func Test_NetworksNetMapGenShouldExcludeOtherRouters(t *testing.T) {
account := getBasicAccountsWithResource()

account.Peers["router2Id"] = &nbpeer.Peer{Key: "router2Key", ID: "router2Id", AccountID: accID, IP: net.IP{192, 168, 1, 4}}
account.NetworkRouters = append(account.NetworkRouters, &routerTypes.NetworkRouter{
ID: "router2Id",
NetworkID: network1ID,
AccountID: accID,
Peer: "router2Id",
})

// validate routes for router1
isRouter, networkResourcesRoutes, sourcePeers := account.GetNetworkResourcesRoutesToSync(context.Background(), accNetResourceRouter1ID, account.GetResourcePoliciesMap(), account.GetResourceRoutersMap())
assert.True(t, isRouter, "should be router")
assert.Len(t, networkResourcesRoutes, 1, "expected network resource route don't match")
assert.Len(t, sourcePeers, 2, "expected source peers don't match")
}

0 comments on commit bc013e4

Please sign in to comment.