Skip to content

Commit

Permalink
Ignore non Linux peers in the network map update
Browse files Browse the repository at this point in the history
  • Loading branch information
surik committed Sep 18, 2023
1 parent 8aaa951 commit 0a41619
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 35 deletions.
73 changes: 41 additions & 32 deletions management/server/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@ func (a *Account) filterRoutesByGroups(routes []*route.Route, groupListMap looku
func (a *Account) getEnabledAndDisabledRoutesByPeer(peerID string) ([]*route.Route, []*route.Route) {
var enabledRoutes []*route.Route
var disabledRoutes []*route.Route

takeRoute := func(r *route.Route, id string) {
peer := a.GetPeer(peerID)
if peer == nil {
log.Errorf("route %s has peer %s that doesn't exist under account %s", r.ID, peerID, a.Id)
return
}

if r.Enabled {
enabledRoutes = append(enabledRoutes, r)
return
}
disabledRoutes = append(disabledRoutes, r)
}

for _, r := range a.Routes {
if r.PeersGroup != "" {
group := a.GetGroup(r.PeersGroup)
Expand All @@ -261,36 +276,14 @@ func (a *Account) getEnabledAndDisabledRoutesByPeer(peerID string) ([]*route.Rou
continue
}
for _, id := range group.Peers {
peer := a.GetPeer(id)
if peer == nil {
log.Errorf("route %s has peers group %s which has %s that doesn't exist under account %s", r.ID, r.PeersGroup, id, a.Id)
continue
if id == peerID {
takeRoute(r, id)
break
}
rCopy := r.Copy()
rCopy.Peer = peer.Key
if r.Enabled {
enabledRoutes = append(enabledRoutes, rCopy)
continue
}
disabledRoutes = append(disabledRoutes, rCopy)
continue
}
}
if r.Peer == peerID {
// We need to set Peer.Key instead of Peer.ID because this object will be sent to agents as part of a network map.
// Ideally we should have a separate field for that, but fine for now.
peer := a.GetPeer(peerID)
if peer == nil {
log.Errorf("route %s has peer %s that doesn't exist under account %s", r.ID, peerID, a.Id)
continue
}
raut := r.Copy()
raut.Peer = peer.Key
if r.Enabled {
enabledRoutes = append(enabledRoutes, raut)
continue
}
disabledRoutes = append(disabledRoutes, raut)
takeRoute(r, peerID)
}
}
return enabledRoutes, disabledRoutes
Expand Down Expand Up @@ -341,20 +334,36 @@ func (a *Account) GetPeerNetworkMap(peerID, dnsDomain string) *NetworkMap {
// Please mind, that the returned route.Route objects will contain Peer.Key instead of Peer.ID.
routes := a.getRoutesToSync(peerID, peersToConnect)

// TODO(yury): each route can contain peers group. We should unfold them to peers
takePeer := func(id string) (*Peer, bool) {
peer := a.GetPeer(id)
if peer == nil || peer.Meta.GoOS != "linux" {
return nil, false
}
return peer, true
}

// We need to set Peer.Key instead of Peer.ID because this object will be sent to agents as part of a network map.
// Ideally we should have a separate field for that, but fine for now.
var routesUpdate []*route.Route
seenPeer := make(map[string]bool)
for _, r := range routes {
if r.PeersGroup == "" {
routesUpdate = append(routesUpdate, r)
if r.Peer != "" {
peer, valid := takePeer(r.Peer)
if !valid {
continue
}
rCopy := r.Copy()
rCopy.Peer = peer.Key
routesUpdate = append(routesUpdate, rCopy)
continue
}
seenPeer := make(map[string]bool)
if group := a.GetGroup(r.PeersGroup); group != nil {
for _, peerId := range group.Peers {
peer := a.GetPeer(peerId) // BROKEN!!!
if peer == nil {
peer, valid := takePeer(peerId)
if !valid {
continue
}

if _, ok := seenPeer[peer.Key]; !ok {
rCopy := r.Copy()
rCopy.Peer = peer.Key
Expand Down
2 changes: 0 additions & 2 deletions management/server/http/routes_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ func (h *RoutesHandler) CreateRoute(w http.ResponseWriter, r *http.Request) {
return
}

// TODO(yury): check that peers are Linux

newRoute, err := h.accountManager.CreateRoute(
account.Id, newPrefix.String(), peerId, peersGroupId,
req.Description, req.NetworkId, req.Masquerade, req.Metric, req.Groups, req.Enabled, user.Id,
Expand Down
33 changes: 32 additions & 1 deletion management/server/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (
const (
peer1Key = "BhRPtynAAYRDy08+q4HTMsos8fs4plTP4NOSh7C1ry8="
peer2Key = "/yF0+vCfv+mRR5k0dca0TrGdO/oiNeAI58gToZm5NyI="
peer3Key = "ayF0+vCfv+mRR5k0dca0TrGdO/oiNeAI58gToZm5NaF="
peer1ID = "peer-1-id"
peer2ID = "peer-2-id"
peer3ID = "peer-3-id"
routeGroup1 = "routeGroup1"
routeGroup2 = "routeGroup2"
routeGroupHA = "routeGroupHA"
Expand Down Expand Up @@ -1188,6 +1190,31 @@ func initTestRouteAccount(t *testing.T, am *DefaultAccountManager) (*Account, er
}
account.Peers[peer2.ID] = peer2

ips = account.getTakenIPs()
peer3IP, err := AllocatePeerIP(account.Network.Net, ips)
if err != nil {
return nil, err
}

peer3 := &Peer{
IP: peer3IP,
ID: peer3ID,
Key: peer3Key,
Name: "[email protected]",
UserID: userID,
Meta: PeerSystemMeta{
Hostname: "[email protected]",
GoOS: "darwin",
Kernel: "Darwin",
Core: "13.4.1",
Platform: "arm64",
OS: "darwin",
WtVersion: "development",
UIVersion: "development",
},
}
account.Peers[peer3.ID] = peer3

err = am.Store.SaveAccount(account)
if err != nil {
return nil, err
Expand All @@ -1204,6 +1231,10 @@ func initTestRouteAccount(t *testing.T, am *DefaultAccountManager) (*Account, er
if err != nil {
return nil, err
}
err = am.GroupAddPeer(accountID, groupAll.ID, peer3ID)
if err != nil {
return nil, err
}

newGroup := []*Group{
{
Expand All @@ -1219,7 +1250,7 @@ func initTestRouteAccount(t *testing.T, am *DefaultAccountManager) (*Account, er
{
ID: routeGroupHA,
Name: routeGroupHA,
Peers: []string{peer1.ID, peer2.ID},
Peers: []string{peer1.ID, peer2.ID, peer3.ID}, // we have one non Linux peer, see peer3
},
}

Expand Down

0 comments on commit 0a41619

Please sign in to comment.