mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-19 17:31:39 +02:00
Avoid storing account if no peer meta or expiration change (#875)
* Avoid storing account if no peer meta or expiration change * remove extra log * Update management/server/peer.go Co-authored-by: Misha Bragin <bangvalo@gmail.com> * Clarify why we need to skip account update --------- Co-authored-by: Misha Bragin <bangvalo@gmail.com>
This commit is contained in:
parent
3876cb26f4
commit
48a8b52740
@ -28,6 +28,17 @@ type PeerSystemMeta struct {
|
|||||||
UIVersion string
|
UIVersion string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p PeerSystemMeta) isEqual(other PeerSystemMeta) bool {
|
||||||
|
return p.Hostname == other.Hostname &&
|
||||||
|
p.GoOS == other.GoOS &&
|
||||||
|
p.Kernel == other.Kernel &&
|
||||||
|
p.Core == other.Core &&
|
||||||
|
p.Platform == other.Platform &&
|
||||||
|
p.OS == other.OS &&
|
||||||
|
p.WtVersion == other.WtVersion &&
|
||||||
|
p.UIVersion == other.UIVersion
|
||||||
|
}
|
||||||
|
|
||||||
type PeerStatus struct {
|
type PeerStatus struct {
|
||||||
// LastSeen is the last time peer was connected to the management service
|
// LastSeen is the last time peer was connected to the management service
|
||||||
LastSeen time.Time
|
LastSeen time.Time
|
||||||
@ -114,13 +125,19 @@ func (p *Peer) Copy() *Peer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateMeta updates peer's system meta data
|
// UpdateMetaIfNew updates peer's system metadata if new information is provided
|
||||||
func (p *Peer) UpdateMeta(meta PeerSystemMeta) {
|
// returns true if meta was updated, false otherwise
|
||||||
|
func (p *Peer) UpdateMetaIfNew(meta PeerSystemMeta) bool {
|
||||||
// Avoid overwriting UIVersion if the update was triggered sole by the CLI client
|
// Avoid overwriting UIVersion if the update was triggered sole by the CLI client
|
||||||
if meta.UIVersion == "" {
|
if meta.UIVersion == "" {
|
||||||
meta.UIVersion = p.Meta.UIVersion
|
meta.UIVersion = p.Meta.UIVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if p.Meta.isEqual(meta) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
p.Meta = meta
|
p.Meta = meta
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarkLoginExpired marks peer's status expired or not
|
// MarkLoginExpired marks peer's status expired or not
|
||||||
@ -654,6 +671,8 @@ func (am *DefaultAccountManager) LoginPeer(login PeerLogin) (*Peer, *NetworkMap,
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this flag prevents unnecessary calls to the persistent store.
|
||||||
|
shouldStoreAccount := false
|
||||||
updateRemotePeers := false
|
updateRemotePeers := false
|
||||||
if peerLoginExpired(peer, account) {
|
if peerLoginExpired(peer, account) {
|
||||||
err = checkAuth(login.UserID, peer)
|
err = checkAuth(login.UserID, peer)
|
||||||
@ -664,19 +683,26 @@ func (am *DefaultAccountManager) LoginPeer(login PeerLogin) (*Peer, *NetworkMap,
|
|||||||
// UserID is present, meaning that JWT validation passed successfully in the API layer.
|
// UserID is present, meaning that JWT validation passed successfully in the API layer.
|
||||||
updatePeerLastLogin(peer, account)
|
updatePeerLastLogin(peer, account)
|
||||||
updateRemotePeers = true
|
updateRemotePeers = true
|
||||||
|
shouldStoreAccount = true
|
||||||
}
|
}
|
||||||
|
|
||||||
peer = updatePeerMeta(peer, login.Meta, account)
|
peer, updated := updatePeerMeta(peer, login.Meta, account)
|
||||||
|
if updated {
|
||||||
|
shouldStoreAccount = true
|
||||||
|
}
|
||||||
|
|
||||||
peer, err = am.checkAndUpdatePeerSSHKey(peer, account, login.SSHKey)
|
peer, err = am.checkAndUpdatePeerSSHKey(peer, account, login.SSHKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = am.Store.SaveAccount(account)
|
if shouldStoreAccount {
|
||||||
if err != nil {
|
err = am.Store.SaveAccount(account)
|
||||||
return nil, nil, err
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if updateRemotePeers {
|
if updateRemotePeers {
|
||||||
err = am.updateAccountPeers(account)
|
err = am.updateAccountPeers(account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -850,10 +876,12 @@ func (am *DefaultAccountManager) GetPeer(accountID, peerID, userID string) (*Pee
|
|||||||
return nil, status.Errorf(status.Internal, "user %s has no access to peer %s under account %s", userID, peerID, accountID)
|
return nil, status.Errorf(status.Internal, "user %s has no access to peer %s under account %s", userID, peerID, accountID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updatePeerMeta(peer *Peer, meta PeerSystemMeta, account *Account) *Peer {
|
func updatePeerMeta(peer *Peer, meta PeerSystemMeta, account *Account) (*Peer, bool) {
|
||||||
peer.UpdateMeta(meta)
|
if peer.UpdateMetaIfNew(meta) {
|
||||||
account.UpdatePeer(peer)
|
account.UpdatePeer(peer)
|
||||||
return peer
|
return peer, true
|
||||||
|
}
|
||||||
|
return peer, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPeerRules returns a list of source or destination rules of a given peer.
|
// GetPeerRules returns a list of source or destination rules of a given peer.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user