2021-08-23 21:43:05 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-05-21 17:27:04 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-08-23 21:43:05 +02:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
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{
|
2021-08-24 11:50:19 +02:00
|
|
|
Key: p.Key,
|
|
|
|
SetupKey: p.SetupKey,
|
|
|
|
IP: p.IP,
|
|
|
|
Meta: p.Meta,
|
|
|
|
Name: p.Name,
|
|
|
|
Status: p.Status,
|
2022-05-05 20:02:15 +02:00
|
|
|
UserID: p.UserID,
|
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-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")
|
|
|
|
}
|
|
|
|
|
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-05-21 15:21:39 +02:00
|
|
|
// notify other peers of the change
|
2021-09-07 18:36:46 +02:00
|
|
|
peers, err := am.Store.GetAccountPeers(accountId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range peers {
|
|
|
|
peersToSend := []*Peer{}
|
|
|
|
for _, remote := range peers {
|
|
|
|
if p.Key != remote.Key {
|
|
|
|
peersToSend = append(peersToSend, remote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update := toRemotePeerConfig(peersToSend)
|
|
|
|
err = am.peersUpdateManager.SendUpdate(p.Key,
|
|
|
|
&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: update,
|
|
|
|
RemotePeersIsEmpty: len(update) == 0,
|
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: update,
|
|
|
|
RemotePeersIsEmpty: len(update) == 0,
|
|
|
|
},
|
2022-05-21 15:21:39 +02:00
|
|
|
},
|
|
|
|
})
|
2021-09-07 18:36:46 +02:00
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
var res []*Peer
|
2022-05-21 15:21:39 +02:00
|
|
|
srcRules, err := am.Store.GetPeerSrcRules(account.Id, peerKey)
|
|
|
|
if err != nil {
|
|
|
|
return &NetworkMap{
|
|
|
|
Peers: res,
|
|
|
|
Network: account.Network.Copy(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dstRules, err := am.Store.GetPeerDstRules(account.Id, peerKey)
|
|
|
|
if err != nil {
|
|
|
|
return &NetworkMap{
|
|
|
|
Peers: res,
|
|
|
|
Network: account.Network.Copy(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := map[string]*Group{}
|
|
|
|
for _, r := range srcRules {
|
|
|
|
if r.Flow == TrafficFlowBidirect {
|
|
|
|
for _, gid := range r.Destination {
|
|
|
|
groups[gid] = account.Groups[gid]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range dstRules {
|
|
|
|
if r.Flow == TrafficFlowBidirect {
|
|
|
|
for _, gid := range r.Source {
|
|
|
|
groups[gid] = account.Groups[gid]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range groups {
|
|
|
|
for _, pid := range g.Peers {
|
2022-05-21 17:27:04 +02:00
|
|
|
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
|
|
|
|
}
|
2022-05-21 15:21:39 +02:00
|
|
|
// exclude original peer
|
|
|
|
if peer.Key != peerKey {
|
|
|
|
res = append(res, peer.Copy())
|
|
|
|
}
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:10:36 +01:00
|
|
|
return &NetworkMap{
|
|
|
|
Peers: res,
|
|
|
|
Network: 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
|
|
|
|
nextIp, _ := AllocatePeerIP(network.Net, takenIps)
|
|
|
|
|
|
|
|
newPeer := &Peer{
|
2021-08-24 11:50:19 +02:00
|
|
|
Key: peer.Key,
|
2022-05-05 20:02:15 +02:00
|
|
|
SetupKey: upperKey,
|
2021-08-24 11:50:19 +02:00
|
|
|
IP: nextIp,
|
|
|
|
Meta: peer.Meta,
|
|
|
|
Name: peer.Name,
|
2022-05-05 20:02:15 +02:00
|
|
|
UserID: userID,
|
2021-08-24 11:50:19 +02:00
|
|
|
Status: &PeerStatus{Connected: false, LastSeen: time.Now()},
|
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
|
|
|
|
}
|