2021-08-23 21:43:05 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-11-07 15:38:21 +01:00
|
|
|
nbdns "github.com/netbirdio/netbird/dns"
|
2022-11-11 20:36:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2021-08-23 21:43:05 +02:00
|
|
|
"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"
|
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)
|
2022-11-07 15:38:21 +01:00
|
|
|
Name string
|
|
|
|
// DNSLabel is the parsed peer name for domain resolution. It is used to form an FQDN by appending the account's
|
|
|
|
// domain to the peer label. e.g. peer-dns-label.netbird.cloud
|
|
|
|
DNSLabel string
|
|
|
|
// Status peer's management connection status
|
2021-08-24 11:50:19 +02:00
|
|
|
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,
|
2022-11-07 15:38:21 +01:00
|
|
|
DNSLabel: p.DNSLabel,
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 10:46:12 +01:00
|
|
|
// Copy PeerStatus
|
|
|
|
func (p *PeerStatus) Copy() *PeerStatus {
|
|
|
|
return &PeerStatus{
|
|
|
|
LastSeen: p.LastSeen,
|
|
|
|
Connected: p.Connected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
// GetPeer looks up peer by its public WireGuard key
|
|
|
|
func (am *DefaultAccountManager) GetPeer(peerPubKey string) (*Peer, error) {
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
account, err := am.Store.GetAccountByPeerPubKey(peerPubKey)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
return account.FindPeerByPubKey(peerPubKey)
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
// GetPeers returns a list of peers under the given account filtering out peers that do not belong to a user if
|
|
|
|
// the current user is not an admin.
|
|
|
|
func (am *DefaultAccountManager) GetPeers(accountID, userID string) ([]*Peer, error) {
|
2022-11-07 17:52:23 +01:00
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := account.FindUser(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-21 17:45:14 +01:00
|
|
|
peers := make([]*Peer, 0)
|
|
|
|
peersMap := make(map[string]*Peer)
|
2022-11-05 10:24:50 +01:00
|
|
|
for _, peer := range account.Peers {
|
|
|
|
if !user.IsAdmin() && user.Id != peer.UserID {
|
|
|
|
// only display peers that belong to the current user if the current user is not an admin
|
|
|
|
continue
|
|
|
|
}
|
2022-11-21 17:45:14 +01:00
|
|
|
p := peer.Copy()
|
|
|
|
peers = append(peers, p)
|
|
|
|
peersMap[peer.Key] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch all the peers that have access to the user's peers
|
|
|
|
for _, peer := range peers {
|
|
|
|
aclPeers := am.getPeersByACL(account, peer.Key)
|
|
|
|
for _, p := range aclPeers {
|
|
|
|
peersMap[p.Key] = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
peers = make([]*Peer, 0, len(peersMap))
|
|
|
|
for _, peer := range peersMap {
|
|
|
|
peers = append(peers, peer)
|
2022-11-05 10:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return peers, nil
|
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
// MarkPeerConnected marks peer as connected (true) or disconnected (false)
|
2022-11-07 12:10:56 +01:00
|
|
|
func (am *DefaultAccountManager) MarkPeerConnected(peerPubKey string, connected bool) error {
|
2021-08-24 11:50:19 +02:00
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
account, err := am.Store.GetAccountByPeerPubKey(peerPubKey)
|
2021-08-24 11:50:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-07 17:52:23 +01:00
|
|
|
unlock := am.Store.AcquireAccountLock(account.Id)
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
// ensure that we consider modification happened meanwhile (because we were outside the account lock when we fetched the account)
|
|
|
|
account, err = am.Store.GetAccount(account.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
peer, err := account.FindPeerByPubKey(peerPubKey)
|
2021-08-24 11:50:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-08 10:46:12 +01:00
|
|
|
newStatus := peer.Status.Copy()
|
|
|
|
newStatus.LastSeen = time.Now()
|
|
|
|
newStatus.Connected = connected
|
|
|
|
peer.Status = newStatus
|
2022-11-07 12:10:56 +01:00
|
|
|
account.UpdatePeer(peer)
|
|
|
|
|
2022-11-08 10:46:12 +01:00
|
|
|
err = am.Store.SavePeerStatus(account.Id, peerPubKey, *newStatus)
|
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) {
|
2022-11-07 17:52:23 +01:00
|
|
|
|
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
2022-06-23 17:04:53 +02:00
|
|
|
|
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-06-23 17:04:53 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
//TODO Peer.ID migration: we will need to replace search by ID here
|
|
|
|
peer, err := account.FindPeerByPubKey(update.Key)
|
2022-06-23 17:04:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if peer.Name != "" {
|
2022-11-07 12:10:56 +01:00
|
|
|
peer.Name = update.Name
|
2022-06-23 17:04:53 +02:00
|
|
|
}
|
2022-11-07 12:10:56 +01:00
|
|
|
peer.SSHEnabled = update.SSHEnabled
|
2022-06-23 17:04:53 +02:00
|
|
|
|
2022-11-07 15:38:21 +01:00
|
|
|
existingLabels := account.getPeerDNSLabels()
|
|
|
|
|
|
|
|
newLabel, err := getPeerHostLabel(peer.Name, existingLabels)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.DNSLabel = newLabel
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
account.UpdatePeer(peer)
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
err = am.Store.SaveAccount(account)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
err = am.updateAccountPeers(account)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
return peer, nil
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
// DeletePeer removes peer from the account by its IP
|
|
|
|
func (am *DefaultAccountManager) DeletePeer(accountID string, peerPubKey string) (*Peer, error) {
|
2022-11-07 17:52:23 +01:00
|
|
|
|
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
2021-09-07 18:36:46 +02:00
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
account, err := am.Store.GetAccount(accountID)
|
2022-01-14 14:34:27 +01:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-01-14 14:34:27 +01:00
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
peer, err := account.FindPeerByPubKey(peerPubKey)
|
2021-09-07 18:36:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
account.DeletePeer(peerPubKey)
|
|
|
|
|
2022-01-14 14:34:27 +01:00
|
|
|
err = am.Store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
err = am.peersUpdateManager.SendUpdate(peerPubKey,
|
2021-09-07 18:36:46 +02:00
|
|
|
&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-11-07 12:10:56 +01:00
|
|
|
// TODO Peer.ID migration: we will need to replace search by Peer.ID here
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
am.peersUpdateManager.CloseChannel(peerPubKey)
|
2021-09-07 18:36:46 +02:00
|
|
|
return peer, nil
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
// GetPeerByIP returns peer by its IP
|
|
|
|
func (am *DefaultAccountManager) GetPeerByIP(accountID string, peerIP string) (*Peer, error) {
|
2022-11-07 17:52:23 +01:00
|
|
|
|
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
account, err := am.Store.GetAccount(accountID)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, peer := range account.Peers {
|
|
|
|
if peerIP == peer.IP.String() {
|
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.NotFound, "peer with IP %s not found", peerIP)
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-01-16 17:10:36 +01:00
|
|
|
// GetNetworkMap returns Network map for a given peer (omits original peer from the Peers result)
|
2022-11-07 12:10:56 +01:00
|
|
|
func (am *DefaultAccountManager) GetNetworkMap(peerPubKey string) (*NetworkMap, error) {
|
2021-08-23 21:43:05 +02:00
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
account, err := am.Store.GetAccountByPeerPubKey(peerPubKey)
|
2021-08-23 21:43:05 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
aclPeers := am.getPeersByACL(account, peerPubKey)
|
|
|
|
routesUpdate := account.GetPeersRoutes(append(aclPeers, account.Peers[peerPubKey]))
|
2022-08-18 18:22:15 +02:00
|
|
|
|
2022-11-07 15:38:21 +01:00
|
|
|
var zones []nbdns.CustomZone
|
|
|
|
peersCustomZone := getPeersCustomZone(account, am.dnsDomain)
|
|
|
|
if peersCustomZone.Domain != "" {
|
|
|
|
zones = append(zones, peersCustomZone)
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsUpdate := nbdns.Config{
|
|
|
|
ServiceEnable: true,
|
|
|
|
CustomZones: zones,
|
|
|
|
NameServerGroups: getPeerNSGroups(account, peerPubKey),
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:10:36 +01:00
|
|
|
return &NetworkMap{
|
2022-11-07 15:38:21 +01:00
|
|
|
Peers: aclPeers,
|
|
|
|
Network: account.Network.Copy(),
|
|
|
|
Routes: routesUpdate,
|
|
|
|
DNSConfig: dnsUpdate,
|
2022-01-16 17:10:36 +01:00
|
|
|
}, 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
|
2022-11-07 12:10:56 +01:00
|
|
|
func (am *DefaultAccountManager) GetPeerNetwork(peerPubKey string) (*Network, error) {
|
2022-06-24 21:30:51 +02:00
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
account, err := am.Store.GetAccountByPeerPubKey(peerPubKey)
|
2022-06-24 21:30:51 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-06-24 21:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-08 16:14:36 +01:00
|
|
|
func (am *DefaultAccountManager) AddPeer(setupKey, userID string, peer *Peer) (*Peer, error) {
|
2021-08-23 21:43:05 +02:00
|
|
|
|
|
|
|
upperKey := strings.ToUpper(setupKey)
|
|
|
|
var account *Account
|
|
|
|
var err error
|
2022-11-08 16:14:36 +01:00
|
|
|
addedByUser := false
|
|
|
|
if len(userID) > 0 {
|
|
|
|
addedByUser = true
|
2022-11-07 12:10:56 +01:00
|
|
|
account, err = am.Store.GetAccountByUser(userID)
|
2022-05-05 20:02:15 +02:00
|
|
|
} else {
|
2022-11-08 16:14:36 +01:00
|
|
|
account, err = am.Store.GetAccountBySetupKey(setupKey)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.NotFound, "failed adding new peer: account not found")
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 17:52:23 +01:00
|
|
|
unlock := am.Store.AcquireAccountLock(account.Id)
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
// ensure that we consider modification happened meanwhile (because we were outside the account lock when we fetched the account)
|
|
|
|
account, err = am.Store.GetAccount(account.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-08 16:14:36 +01:00
|
|
|
if !addedByUser {
|
|
|
|
// validate the setup key if adding with a key
|
|
|
|
sk, err := account.FindSetupKey(upperKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sk.IsValid() {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.PreconditionFailed, "couldn't add peer: setup key is invalid")
|
2022-11-07 15:38:21 +01:00
|
|
|
}
|
2022-11-08 16:14:36 +01:00
|
|
|
|
|
|
|
account.SetupKeys[sk.Key] = sk.IncrementUsage()
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 16:14:36 +01:00
|
|
|
takenIps := account.getTakenIPs()
|
|
|
|
existingLabels := account.getPeerDNSLabels()
|
|
|
|
|
2022-11-07 15:38:21 +01:00
|
|
|
newLabel, err := getPeerHostLabel(peer.Name, existingLabels)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.DNSLabel = newLabel
|
2021-08-23 21:43:05 +02:00
|
|
|
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,
|
2022-11-07 15:38:21 +01:00
|
|
|
DNSLabel: newLabel,
|
2022-06-23 17:04:53 +02:00
|
|
|
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)
|
|
|
|
|
2022-11-08 16:14:36 +01:00
|
|
|
var groupsToAdd []string
|
|
|
|
if addedByUser {
|
|
|
|
groupsToAdd, err = account.getUserGroups(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
groupsToAdd, err = account.getSetupKeyGroups(upperKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:39:46 +02:00
|
|
|
if len(groupsToAdd) > 0 {
|
|
|
|
for _, s := range groupsToAdd {
|
|
|
|
if g, ok := account.Groups[s]; ok && g.Name != "All" {
|
|
|
|
g.Peers = append(g.Peers, newPeer.Key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
account.Peers[newPeer.Key] = newPeer
|
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 {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2021-08-23 21:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-11-07 12:10:56 +01:00
|
|
|
func (am *DefaultAccountManager) UpdatePeerSSHKey(peerPubKey string, sshKey string) error {
|
2022-06-23 17:04:53 +02:00
|
|
|
|
|
|
|
if sshKey == "" {
|
2022-11-07 12:10:56 +01:00
|
|
|
log.Debugf("empty SSH key provided for peer %s, skipping update", peerPubKey)
|
2022-06-23 17:04:53 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
account, err := am.Store.GetAccountByPeerPubKey(peerPubKey)
|
2022-06-23 17:04:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-07 17:52:23 +01:00
|
|
|
unlock := am.Store.AcquireAccountLock(account.Id)
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
// ensure that we consider modification happened meanwhile (because we were outside the account lock when we fetched the account)
|
|
|
|
account, err = am.Store.GetAccount(account.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
peer, err := account.FindPeerByPubKey(peerPubKey)
|
2022-06-23 17:04:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
if peer.SSHKey == sshKey {
|
|
|
|
log.Debugf("same SSH key provided for peer %s, skipping update", peerPubKey)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
peer.SSHKey = sshKey
|
|
|
|
account.UpdatePeer(peer)
|
2022-06-23 17:04:53 +02:00
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
err = am.Store.SaveAccount(account)
|
2022-06-23 17:04:53 +02:00
|
|
|
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
|
2022-11-07 12:10:56 +01:00
|
|
|
func (am *DefaultAccountManager) UpdatePeerMeta(peerPubKey string, meta PeerSystemMeta) error {
|
2022-05-23 13:03:57 +02:00
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
account, err := am.Store.GetAccountByPeerPubKey(peerPubKey)
|
2022-05-23 13:03:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-07 17:52:23 +01:00
|
|
|
unlock := am.Store.AcquireAccountLock(account.Id)
|
|
|
|
defer unlock()
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
peer, err := account.FindPeerByPubKey(peerPubKey)
|
2022-05-23 13:03:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
// Avoid overwriting UIVersion if the update was triggered sole by the CLI client
|
|
|
|
if meta.UIVersion == "" {
|
2022-11-07 12:10:56 +01:00
|
|
|
meta.UIVersion = peer.Meta.UIVersion
|
2022-05-25 23:25:02 +02:00
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
peer.Meta = meta
|
|
|
|
account.UpdatePeer(peer)
|
2022-05-25 23:25:02 +02:00
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
err = am.Store.SaveAccount(account)
|
2022-05-23 13:03:57 +02:00
|
|
|
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-11-07 12:10:56 +01:00
|
|
|
func (am *DefaultAccountManager) getPeersByACL(account *Account, peerPubKey string) []*Peer {
|
2022-06-04 22:02:22 +02:00
|
|
|
var peers []*Peer
|
2022-11-07 12:10:56 +01:00
|
|
|
srcRules, dstRules := account.GetPeerRules(peerPubKey)
|
2022-06-04 22:02:22 +02:00
|
|
|
|
|
|
|
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
|
2022-11-07 12:10:56 +01:00
|
|
|
if _, ok := peersSet[peer.Key]; peer.Key != peerPubKey && !ok {
|
2022-06-04 22:02:22 +02:00
|
|
|
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 {
|
2022-11-07 12:10:56 +01:00
|
|
|
peers := account.GetPeers()
|
2022-06-24 21:30:51 +02:00
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
for _, peer := range peers {
|
2022-11-08 11:38:40 +01:00
|
|
|
remotePeerNetworkMap, err := am.GetNetworkMap(peer.Key)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return err
|
2022-11-08 11:38:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
update := toSyncResponse(nil, peer, nil, remotePeerNetworkMap)
|
|
|
|
err = am.peersUpdateManager.SendUpdate(peer.Key, &UpdateMessage{Update: update})
|
2022-06-04 22:02:22 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return err
|
2022-06-04 22:02:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|