2021-08-23 21:43:05 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-05-21 15:21:39 +02:00
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
"github.com/netbirdio/netbird/management/proto"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2021-08-23 21:43:05 +02:00
|
|
|
)
|
|
|
|
|
2021-08-24 11:50:19 +02:00
|
|
|
// PeerSystemMeta is a metadata of a Peer machine system
|
|
|
|
type PeerSystemMeta struct {
|
|
|
|
Hostname string
|
|
|
|
GoOS string
|
|
|
|
Kernel string
|
|
|
|
Core string
|
|
|
|
Platform string
|
|
|
|
OS string
|
|
|
|
WtVersion string
|
2022-05-25 23:25:02 +02:00
|
|
|
UIVersion string
|
2021-08-24 11:50:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type PeerStatus struct {
|
2022-05-21 15:21:39 +02:00
|
|
|
// LastSeen is the last time peer was connected to the management service
|
2021-08-24 11:50:19 +02:00
|
|
|
LastSeen time.Time
|
2022-05-21 15:21:39 +02:00
|
|
|
// Connected indicates whether peer is connected to the management service or not
|
2021-08-24 11:50:19 +02:00
|
|
|
Connected bool
|
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
// Peer represents a machine connected to the network.
|
|
|
|
// The Peer is a Wireguard peer identified by a public key
|
2021-08-23 21:43:05 +02:00
|
|
|
type Peer struct {
|
2022-05-21 15:21:39 +02:00
|
|
|
// Wireguard public key
|
2021-08-23 21:43:05 +02:00
|
|
|
Key string
|
2022-05-21 15:21:39 +02:00
|
|
|
// A setup key this peer was registered with
|
2021-08-23 21:43:05 +02:00
|
|
|
SetupKey string
|
2022-05-21 15:21:39 +02:00
|
|
|
// IP address of the Peer
|
2021-08-23 21:43:05 +02:00
|
|
|
IP net.IP
|
2022-05-21 15:21:39 +02:00
|
|
|
// Meta is a Peer system meta data
|
2021-08-24 11:50:19 +02:00
|
|
|
Meta PeerSystemMeta
|
2022-05-21 15:21:39 +02:00
|
|
|
// Name is peer's name (machine name)
|
2021-08-24 11:50:19 +02:00
|
|
|
Name string
|
|
|
|
Status *PeerStatus
|
2022-05-21 15:21:39 +02:00
|
|
|
// The user ID that registered the peer
|
2022-05-05 20:02:15 +02:00
|
|
|
UserID string
|
2022-06-23 17:04:53 +02:00
|
|
|
// SSHKey is a public SSH key of the peer
|
|
|
|
SSHKey string
|
|
|
|
// SSHEnabled indicated whether SSH server is enabled on the peer
|
|
|
|
SSHEnabled bool
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
// Copy copies Peer object
|
2021-08-23 21:43:05 +02:00
|
|
|
func (p *Peer) Copy() *Peer {
|
|
|
|
return &Peer{
|
2022-06-23 17:04:53 +02:00
|
|
|
Key: p.Key,
|
|
|
|
SetupKey: p.SetupKey,
|
|
|
|
IP: p.IP,
|
|
|
|
Meta: p.Meta,
|
|
|
|
Name: p.Name,
|
|
|
|
Status: p.Status,
|
|
|
|
UserID: p.UserID,
|
|
|
|
SSHKey: p.SSHKey,
|
|
|
|
SSHEnabled: p.SSHEnabled,
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
// GetPeer returns a peer from a Store
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) GetPeer(peerKey string) (*Peer, error) {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
peer, err := am.Store.GetPeer(peerKey)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
// MarkPeerConnected marks peer as connected (true) or disconnected (false)
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) MarkPeerConnected(peerKey string, connected bool) error {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-24 11:50:19 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
peer, err := am.Store.GetPeer(peerKey)
|
2021-08-24 11:50:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetPeerAccount(peerKey)
|
2021-08-24 11:50:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerCopy := peer.Copy()
|
|
|
|
peerCopy.Status.LastSeen = time.Now()
|
|
|
|
peerCopy.Status.Connected = connected
|
2021-09-07 18:36:46 +02:00
|
|
|
err = am.Store.SavePeer(account.Id, peerCopy)
|
2021-08-24 11:50:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-23 17:04:53 +02:00
|
|
|
// UpdatePeer updates peer. Only Peer.Name and Peer.SSHEnabled can be updated.
|
|
|
|
func (am *DefaultAccountManager) UpdatePeer(accountID string, update *Peer) (*Peer, error) {
|
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
|
|
|
|
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
peer, err := am.Store.GetPeer(update.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerCopy := peer.Copy()
|
|
|
|
if peer.Name != "" {
|
|
|
|
peerCopy.Name = update.Name
|
|
|
|
}
|
|
|
|
peerCopy.SSHEnabled = update.SSHEnabled
|
|
|
|
|
|
|
|
err = am.Store.SavePeer(accountID, peerCopy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.updateAccountPeers(account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return peerCopy, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
// RenamePeer changes peer's name
|
|
|
|
func (am *DefaultAccountManager) RenamePeer(
|
|
|
|
accountId string,
|
|
|
|
peerKey string,
|
|
|
|
newName string,
|
|
|
|
) (*Peer, error) {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
peer, err := am.Store.GetPeer(peerKey)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerCopy := peer.Copy()
|
|
|
|
peerCopy.Name = newName
|
2021-09-07 18:36:46 +02:00
|
|
|
err = am.Store.SavePeer(accountId, peerCopy)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return peerCopy, nil
|
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
// DeletePeer removes peer from the account by it's IP
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) DeletePeer(accountId string, peerKey string) (*Peer, error) {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
|
|
|
|
2022-01-14 14:34:27 +01:00
|
|
|
account, err := am.Store.GetAccount(accountId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
|
|
|
}
|
|
|
|
|
2022-06-04 22:02:22 +02:00
|
|
|
// delete peer from groups
|
|
|
|
for _, g := range account.Groups {
|
|
|
|
for i, pk := range g.Peers {
|
|
|
|
if pk == peerKey {
|
|
|
|
g.Peers = append(g.Peers[:i], g.Peers[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
peer, err := am.Store.DeletePeer(accountId, peerKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-14 14:34:27 +01:00
|
|
|
account.Network.IncSerial()
|
|
|
|
err = am.Store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
err = am.peersUpdateManager.SendUpdate(peerKey,
|
|
|
|
&UpdateMessage{
|
|
|
|
Update: &proto.SyncResponse{
|
2022-01-16 17:10:36 +01:00
|
|
|
// fill those field for backward compatibility
|
2021-09-07 18:36:46 +02:00
|
|
|
RemotePeers: []*proto.RemotePeerConfig{},
|
|
|
|
RemotePeersIsEmpty: true,
|
2022-01-16 17:10:36 +01:00
|
|
|
// new field
|
|
|
|
NetworkMap: &proto.NetworkMap{
|
2022-03-10 18:18:38 +01:00
|
|
|
Serial: account.Network.CurrentSerial(),
|
2022-01-16 17:10:36 +01:00
|
|
|
RemotePeers: []*proto.RemotePeerConfig{},
|
|
|
|
RemotePeersIsEmpty: true,
|
|
|
|
},
|
2022-05-21 15:21:39 +02:00
|
|
|
},
|
|
|
|
})
|
2021-09-07 18:36:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-04 22:02:22 +02:00
|
|
|
if err := am.updateAccountPeers(account); err != nil {
|
2021-09-07 18:36:46 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
am.peersUpdateManager.CloseChannel(peerKey)
|
|
|
|
return peer, nil
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
// GetPeerByIP returns peer by it's IP
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) GetPeerByIP(accountId string, peerIP string) (*Peer, error) {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetAccount(accountId)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, peer := range account.Peers {
|
|
|
|
if peerIP == peer.IP.String() {
|
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, status.Errorf(codes.NotFound, "peer with IP %s not found", peerIP)
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:10:36 +01:00
|
|
|
// GetNetworkMap returns Network map for a given peer (omits original peer from the Peers result)
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) GetNetworkMap(peerKey string) (*NetworkMap, error) {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetPeerAccount(peerKey)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Invalid peer key %s", peerKey)
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:10:36 +01:00
|
|
|
return &NetworkMap{
|
2022-06-04 22:02:22 +02:00
|
|
|
Peers: am.getPeersByACL(account, peerKey),
|
2022-01-16 17:10:36 +01:00
|
|
|
Network: account.Network.Copy(),
|
|
|
|
}, err
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-06-24 21:30:51 +02:00
|
|
|
// GetPeerNetwork returns the Network for a given peer
|
|
|
|
func (am *DefaultAccountManager) GetPeerNetwork(peerKey string) (*Network, error) {
|
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
|
|
|
|
|
|
|
account, err := am.Store.GetPeerAccount(peerKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Invalid peer key %s", peerKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
return account.Network.Copy(), err
|
|
|
|
}
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
// AddPeer adds a new peer to the Store.
|
|
|
|
// Each Account has a list of pre-authorised SetupKey and if no Account has a given key err wit ha code codes.Unauthenticated
|
|
|
|
// will be returned, meaning the key is invalid
|
2022-05-05 20:02:15 +02:00
|
|
|
// If a User ID is provided, it means that we passed the authentication using JWT, then we look for account by User ID and register the peer
|
|
|
|
// to it. We also add the User ID to the peer metadata to identify registrant.
|
2021-08-23 21:43:05 +02:00
|
|
|
// Each new Peer will be assigned a new next net.IP from the Account.Network and Account.Network.LastIP will be updated (IP's are not reused).
|
2021-08-24 11:50:19 +02:00
|
|
|
// The peer property is just a placeholder for the Peer properties to pass further
|
2022-05-21 15:21:39 +02:00
|
|
|
func (am *DefaultAccountManager) AddPeer(
|
|
|
|
setupKey string,
|
|
|
|
userID string,
|
|
|
|
peer *Peer,
|
|
|
|
) (*Peer, error) {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-23 21:43:05 +02:00
|
|
|
|
|
|
|
upperKey := strings.ToUpper(setupKey)
|
|
|
|
|
|
|
|
var account *Account
|
|
|
|
var err error
|
|
|
|
var sk *SetupKey
|
2022-05-05 20:02:15 +02:00
|
|
|
if len(upperKey) != 0 {
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err = am.Store.GetAccountBySetupKey(upperKey)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
2022-05-21 15:21:39 +02:00
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.NotFound,
|
|
|
|
"unable to register peer, unable to find account with setupKey %s",
|
|
|
|
upperKey,
|
|
|
|
)
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sk = getAccountSetupKeyByKey(account, upperKey)
|
|
|
|
if sk == nil {
|
|
|
|
// shouldn't happen actually
|
2022-05-21 15:21:39 +02:00
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.NotFound,
|
|
|
|
"unable to register peer, unknown setupKey %s",
|
|
|
|
upperKey,
|
|
|
|
)
|
2022-05-05 20:02:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !sk.IsValid() {
|
2022-05-21 15:21:39 +02:00
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.FailedPrecondition,
|
|
|
|
"unable to register peer, its setup key is invalid (expired, overused or revoked)",
|
|
|
|
)
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 20:02:15 +02:00
|
|
|
} else if len(userID) != 0 {
|
|
|
|
account, err = am.Store.GetUserAccount(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "unable to register peer, unknown user with ID: %s", userID)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Empty setup key and jwt fail
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "no setup key or user id provided")
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var takenIps []net.IP
|
|
|
|
for _, peer := range account.Peers {
|
|
|
|
takenIps = append(takenIps, peer.IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
network := account.Network
|
2022-06-02 12:56:02 +02:00
|
|
|
nextIp, err := AllocatePeerIP(network.Net, takenIps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-23 21:43:05 +02:00
|
|
|
|
|
|
|
newPeer := &Peer{
|
2022-06-23 17:04:53 +02:00
|
|
|
Key: peer.Key,
|
|
|
|
SetupKey: upperKey,
|
|
|
|
IP: nextIp,
|
|
|
|
Meta: peer.Meta,
|
|
|
|
Name: peer.Name,
|
|
|
|
UserID: userID,
|
|
|
|
Status: &PeerStatus{Connected: false, LastSeen: time.Now()},
|
|
|
|
SSHEnabled: false,
|
|
|
|
SSHKey: peer.SSHKey,
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
// add peer to 'All' group
|
|
|
|
group, err := account.GetGroupAll()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
group.Peers = append(group.Peers, newPeer.Key)
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
account.Peers[newPeer.Key] = newPeer
|
2022-05-05 20:02:15 +02:00
|
|
|
if len(upperKey) != 0 {
|
|
|
|
account.SetupKeys[sk.Key] = sk.IncrementUsage()
|
|
|
|
}
|
2022-01-14 14:34:27 +01:00
|
|
|
account.Network.IncSerial()
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
err = am.Store.SaveAccount(account)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed adding peer")
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPeer, nil
|
|
|
|
}
|
2022-05-23 13:03:57 +02:00
|
|
|
|
2022-06-23 17:04:53 +02:00
|
|
|
// UpdatePeerSSHKey updates peer's public SSH key
|
|
|
|
func (am *DefaultAccountManager) UpdatePeerSSHKey(peerKey string, sshKey string) error {
|
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
|
|
|
|
|
|
|
if sshKey == "" {
|
|
|
|
log.Debugf("empty SSH key provided for peer %s, skipping update", peerKey)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
peer, err := am.Store.GetPeer(peerKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := am.Store.GetPeerAccount(peerKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerCopy := peer.Copy()
|
|
|
|
peerCopy.SSHKey = sshKey
|
|
|
|
|
|
|
|
err = am.Store.SavePeer(account.Id, peerCopy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger network map update
|
|
|
|
return am.updateAccountPeers(account)
|
|
|
|
}
|
|
|
|
|
2022-05-23 13:03:57 +02:00
|
|
|
// UpdatePeerMeta updates peer's system metadata
|
|
|
|
func (am *DefaultAccountManager) UpdatePeerMeta(peerKey string, meta PeerSystemMeta) error {
|
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
|
|
|
|
|
|
|
peer, err := am.Store.GetPeer(peerKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := am.Store.GetPeerAccount(peerKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerCopy := peer.Copy()
|
2022-05-25 23:25:02 +02:00
|
|
|
// Avoid overwriting UIVersion if the update was triggered sole by the CLI client
|
|
|
|
if meta.UIVersion == "" {
|
|
|
|
meta.UIVersion = peerCopy.Meta.UIVersion
|
|
|
|
}
|
|
|
|
|
2022-05-23 13:03:57 +02:00
|
|
|
peerCopy.Meta = meta
|
2022-05-25 23:25:02 +02:00
|
|
|
|
2022-05-23 13:03:57 +02:00
|
|
|
err = am.Store.SavePeer(account.Id, peerCopy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-04 22:02:22 +02:00
|
|
|
|
2022-06-23 17:04:53 +02:00
|
|
|
// getPeersByACL returns all peers that given peer has access to.
|
2022-06-04 22:02:22 +02:00
|
|
|
func (am *DefaultAccountManager) getPeersByACL(account *Account, peerKey string) []*Peer {
|
|
|
|
var peers []*Peer
|
|
|
|
srcRules, err := am.Store.GetPeerSrcRules(account.Id, peerKey)
|
|
|
|
if err != nil {
|
|
|
|
srcRules = []*Rule{}
|
|
|
|
}
|
|
|
|
|
|
|
|
dstRules, err := am.Store.GetPeerDstRules(account.Id, peerKey)
|
|
|
|
if err != nil {
|
|
|
|
dstRules = []*Rule{}
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := map[string]*Group{}
|
|
|
|
for _, r := range srcRules {
|
2022-06-14 10:32:54 +02:00
|
|
|
if r.Disabled {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-04 22:02:22 +02:00
|
|
|
if r.Flow == TrafficFlowBidirect {
|
|
|
|
for _, gid := range r.Destination {
|
|
|
|
if group, ok := account.Groups[gid]; ok {
|
|
|
|
groups[gid] = group
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range dstRules {
|
2022-06-14 10:32:54 +02:00
|
|
|
if r.Disabled {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-04 22:02:22 +02:00
|
|
|
if r.Flow == TrafficFlowBidirect {
|
|
|
|
for _, gid := range r.Source {
|
|
|
|
if group, ok := account.Groups[gid]; ok {
|
|
|
|
groups[gid] = group
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
peersSet := make(map[string]struct{})
|
|
|
|
for _, g := range groups {
|
|
|
|
for _, pid := range g.Peers {
|
|
|
|
peer, ok := account.Peers[pid]
|
|
|
|
if !ok {
|
|
|
|
log.Warnf(
|
|
|
|
"peer %s found in group %s but doesn't belong to account %s",
|
|
|
|
pid,
|
|
|
|
g.ID,
|
|
|
|
account.Id,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// exclude original peer
|
|
|
|
if _, ok := peersSet[peer.Key]; peer.Key != peerKey && !ok {
|
|
|
|
peersSet[peer.Key] = struct{}{}
|
|
|
|
peers = append(peers, peer.Copy())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return peers
|
|
|
|
}
|
|
|
|
|
2022-06-23 17:04:53 +02:00
|
|
|
// updateAccountPeers updates all peers that belong to an account.
|
|
|
|
// Should be called when changes have to be synced to peers.
|
2022-06-04 22:02:22 +02:00
|
|
|
func (am *DefaultAccountManager) updateAccountPeers(account *Account) error {
|
|
|
|
// notify other peers of the change
|
|
|
|
peers, err := am.Store.GetAccountPeers(account.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-24 21:30:51 +02:00
|
|
|
network := account.Network.Copy()
|
|
|
|
|
2022-06-04 22:02:22 +02:00
|
|
|
for _, p := range peers {
|
|
|
|
update := toRemotePeerConfig(am.getPeersByACL(account, p.Key))
|
|
|
|
err = am.peersUpdateManager.SendUpdate(p.Key,
|
|
|
|
&UpdateMessage{
|
|
|
|
Update: &proto.SyncResponse{
|
2022-06-23 17:04:53 +02:00
|
|
|
// fill deprecated fields for backward compatibility
|
2022-06-04 22:02:22 +02:00
|
|
|
RemotePeers: update,
|
|
|
|
RemotePeersIsEmpty: len(update) == 0,
|
|
|
|
// new field
|
|
|
|
NetworkMap: &proto.NetworkMap{
|
|
|
|
Serial: account.Network.CurrentSerial(),
|
|
|
|
RemotePeers: update,
|
|
|
|
RemotePeersIsEmpty: len(update) == 0,
|
2022-06-24 21:30:51 +02:00
|
|
|
PeerConfig: toPeerConfig(p, network),
|
2022-06-04 22:02:22 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|