mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 08:16:39 +00:00
Refactor UpdatePeer method to defer event logging and scheduling until after peer save
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
@@ -234,6 +234,12 @@ func (am *DefaultAccountManager) UpdatePeer(ctx context.Context, accountID, user
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sshChanged, peerLabelChanged, loginExpirationChanged, inactivityExpirationChanged bool
|
||||||
|
|
||||||
|
if peer.SSHEnabled != update.SSHEnabled {
|
||||||
|
peer.SSHEnabled = update.SSHEnabled
|
||||||
|
sshChanged = true
|
||||||
|
}
|
||||||
if peer.SSHEnabled != update.SSHEnabled {
|
if peer.SSHEnabled != update.SSHEnabled {
|
||||||
peer.SSHEnabled = update.SSHEnabled
|
peer.SSHEnabled = update.SSHEnabled
|
||||||
event := activity.PeerSSHEnabled
|
event := activity.PeerSSHEnabled
|
||||||
@@ -243,10 +249,9 @@ func (am *DefaultAccountManager) UpdatePeer(ctx context.Context, accountID, user
|
|||||||
am.StoreEvent(ctx, userID, peer.IP.String(), accountID, event, peer.EventMeta(am.GetDNSDomain()))
|
am.StoreEvent(ctx, userID, peer.IP.String(), accountID, event, peer.EventMeta(am.GetDNSDomain()))
|
||||||
}
|
}
|
||||||
|
|
||||||
peerLabelUpdated := peer.Name != update.Name
|
if peer.Name != update.Name {
|
||||||
|
|
||||||
if peerLabelUpdated {
|
|
||||||
peer.Name = update.Name
|
peer.Name = update.Name
|
||||||
|
peerLabelChanged = true
|
||||||
|
|
||||||
existingLabels, err := am.getPeerDNSLabels(ctx, accountID)
|
existingLabels, err := am.getPeerDNSLabels(ctx, accountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -259,20 +264,44 @@ func (am *DefaultAccountManager) UpdatePeer(ctx context.Context, accountID, user
|
|||||||
}
|
}
|
||||||
|
|
||||||
peer.DNSLabel = newLabel
|
peer.DNSLabel = newLabel
|
||||||
|
|
||||||
am.StoreEvent(ctx, userID, peer.ID, accountID, activity.PeerRenamed, peer.EventMeta(am.GetDNSDomain()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if peer.LoginExpirationEnabled != update.LoginExpirationEnabled {
|
if peer.LoginExpirationEnabled != update.LoginExpirationEnabled {
|
||||||
|
|
||||||
if !peer.AddedWithSSOLogin() {
|
if !peer.AddedWithSSOLogin() {
|
||||||
return nil, status.Errorf(status.PreconditionFailed, "this peer hasn't been added with the SSO login, therefore the login expiration can't be updated")
|
return nil, status.Errorf(status.PreconditionFailed, "this peer hasn't been added with the SSO login, therefore the login expiration can't be updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
peer.LoginExpirationEnabled = update.LoginExpirationEnabled
|
peer.LoginExpirationEnabled = update.LoginExpirationEnabled
|
||||||
|
loginExpirationChanged = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if peer.InactivityExpirationEnabled != update.InactivityExpirationEnabled {
|
||||||
|
if !peer.AddedWithSSOLogin() {
|
||||||
|
return nil, status.Errorf(status.PreconditionFailed, "this peer hasn't been added with the SSO login, therefore the inactivity expiration can't be updated")
|
||||||
|
}
|
||||||
|
peer.InactivityExpirationEnabled = update.InactivityExpirationEnabled
|
||||||
|
inactivityExpirationChanged = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = am.Store.SavePeer(ctx, LockingStrengthUpdate, accountID, peer); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if sshChanged {
|
||||||
|
event := activity.PeerSSHEnabled
|
||||||
|
if !peer.SSHEnabled {
|
||||||
|
event = activity.PeerSSHDisabled
|
||||||
|
}
|
||||||
|
am.StoreEvent(ctx, userID, peer.IP.String(), accountID, event, peer.EventMeta(am.GetDNSDomain()))
|
||||||
|
}
|
||||||
|
|
||||||
|
if peerLabelChanged {
|
||||||
|
am.StoreEvent(ctx, userID, peer.ID, accountID, activity.PeerRenamed, peer.EventMeta(am.GetDNSDomain()))
|
||||||
|
am.updateAccountPeers(ctx, accountID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if loginExpirationChanged {
|
||||||
event := activity.PeerLoginExpirationEnabled
|
event := activity.PeerLoginExpirationEnabled
|
||||||
if !update.LoginExpirationEnabled {
|
if !peer.LoginExpirationEnabled {
|
||||||
event = activity.PeerLoginExpirationDisabled
|
event = activity.PeerLoginExpirationDisabled
|
||||||
}
|
}
|
||||||
am.StoreEvent(ctx, userID, peer.IP.String(), accountID, event, peer.EventMeta(am.GetDNSDomain()))
|
am.StoreEvent(ctx, userID, peer.IP.String(), accountID, event, peer.EventMeta(am.GetDNSDomain()))
|
||||||
@@ -282,15 +311,9 @@ func (am *DefaultAccountManager) UpdatePeer(ctx context.Context, accountID, user
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if peer.InactivityExpirationEnabled != update.InactivityExpirationEnabled {
|
if inactivityExpirationChanged {
|
||||||
if !peer.AddedWithSSOLogin() {
|
|
||||||
return nil, status.Errorf(status.PreconditionFailed, "this peer hasn't been added with the SSO login, therefore the login expiration can't be updated")
|
|
||||||
}
|
|
||||||
|
|
||||||
peer.InactivityExpirationEnabled = update.InactivityExpirationEnabled
|
|
||||||
|
|
||||||
event := activity.PeerInactivityExpirationEnabled
|
event := activity.PeerInactivityExpirationEnabled
|
||||||
if !update.InactivityExpirationEnabled {
|
if !peer.InactivityExpirationEnabled {
|
||||||
event = activity.PeerInactivityExpirationDisabled
|
event = activity.PeerInactivityExpirationDisabled
|
||||||
}
|
}
|
||||||
am.StoreEvent(ctx, userID, peer.IP.String(), accountID, event, peer.EventMeta(am.GetDNSDomain()))
|
am.StoreEvent(ctx, userID, peer.IP.String(), accountID, event, peer.EventMeta(am.GetDNSDomain()))
|
||||||
@@ -300,14 +323,6 @@ func (am *DefaultAccountManager) UpdatePeer(ctx context.Context, accountID, user
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = am.Store.SavePeer(ctx, LockingStrengthUpdate, accountID, peer); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if peerLabelUpdated {
|
|
||||||
am.updateAccountPeers(ctx, accountID)
|
|
||||||
}
|
|
||||||
|
|
||||||
return peer, nil
|
return peer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user