2022-03-22 11:03:15 -04:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/peer"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/storewrapper"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetPeers returns the stored peers if the requested version differs from the stored version.
|
2022-04-13 06:39:55 -04:00
|
|
|
// peers include all vpn devices namely Coordinators, Nodes and Admins.
|
2022-03-22 11:03:15 -04:00
|
|
|
func (c *Core) GetPeers(resourceVersion int) (int, []peer.Peer, error) {
|
|
|
|
// Most often there's nothing to do, so first check without an expensive transaction.
|
|
|
|
curVer, err := c.data().GetPeersResourceVersion()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
if curVer == resourceVersion {
|
|
|
|
return curVer, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := c.store.BeginTransaction()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
txdata := storewrapper.StoreWrapper{Store: tx}
|
|
|
|
|
|
|
|
txVer, err := txdata.GetPeersResourceVersion()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
peers, err := txdata.GetPeers()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
return txVer, peers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPeer adds a peer to the store and the VPN.
|
|
|
|
func (c *Core) AddPeer(peer peer.Peer) error {
|
2022-03-29 10:49:11 -04:00
|
|
|
if err := c.AddPeerToVPN(peer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.AddPeerToStore(peer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPeerToVPN adds a peer to the the VPN.
|
|
|
|
func (c *Core) AddPeerToVPN(peer peer.Peer) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
// don't add myself to vpn
|
|
|
|
myIP, err := c.vpn.GetInterfaceIP()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if myIP != peer.VPNIP {
|
2022-04-13 06:39:55 -04:00
|
|
|
if err := c.vpn.AddPeer(peer.VPNPubKey, peer.PublicIP, peer.VPNIP); err != nil {
|
|
|
|
c.zaplogger.Error("failed to add peer to VPN", zap.Error(err), zap.String("peer public_ip", peer.PublicIP), zap.String("peer vpn_ip", peer.VPNIP))
|
2022-03-22 11:03:15 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-04-13 06:39:55 -04:00
|
|
|
c.zaplogger.Info("added peer to VPN", zap.String("role", peer.Role.String()), zap.String("coordinator public_ip", peer.PublicIP), zap.String("coordinator vpn_ip", peer.VPNIP))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-03-29 10:49:11 -04:00
|
|
|
return nil
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-03-29 10:49:11 -04:00
|
|
|
// AddPeerToStore adds a peer to the store.
|
|
|
|
func (c *Core) AddPeerToStore(peer peer.Peer) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
tx, err := c.store.BeginTransaction()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
txdata := storewrapper.StoreWrapper{Store: tx}
|
|
|
|
|
|
|
|
if err := txdata.IncrementPeersResourceVersion(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := txdata.PutPeer(peer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-29 10:49:11 -04:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-13 06:39:55 -04:00
|
|
|
c.zaplogger.Info("added peer to store", zap.String("peer public_ip", peer.PublicIP), zap.String("peer vpn_ip", peer.VPNIP))
|
2022-03-29 10:49:11 -04:00
|
|
|
return nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePeers synchronizes the peers known to the store and the vpn with the passed peers.
|
|
|
|
func (c *Core) UpdatePeers(peers []peer.Peer) error {
|
2022-03-25 11:05:17 -04:00
|
|
|
return c.vpn.UpdatePeers(peers)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|