From df6c9a528a492cd1807eacac679df67b1e5f3b6b Mon Sep 17 00:00:00 2001 From: bcmmbaga Date: Wed, 6 Nov 2024 16:01:07 +0300 Subject: [PATCH] Refactor UpdatePeer method to defer event logging and scheduling until after peer save Signed-off-by: bcmmbaga --- management/server/peer.go | 63 ++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/management/server/peer.go b/management/server/peer.go index 9f3fc8b0c..bef5c730b 100644 --- a/management/server/peer.go +++ b/management/server/peer.go @@ -234,6 +234,12 @@ func (am *DefaultAccountManager) UpdatePeer(ctx context.Context, accountID, user 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 { peer.SSHEnabled = update.SSHEnabled 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())) } - peerLabelUpdated := peer.Name != update.Name - - if peerLabelUpdated { + if peer.Name != update.Name { peer.Name = update.Name + peerLabelChanged = true existingLabels, err := am.getPeerDNSLabels(ctx, accountID) if err != nil { @@ -259,20 +264,44 @@ func (am *DefaultAccountManager) UpdatePeer(ctx context.Context, accountID, user } peer.DNSLabel = newLabel - - am.StoreEvent(ctx, userID, peer.ID, accountID, activity.PeerRenamed, peer.EventMeta(am.GetDNSDomain())) } if peer.LoginExpirationEnabled != update.LoginExpirationEnabled { - 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.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 - if !update.LoginExpirationEnabled { + if !peer.LoginExpirationEnabled { event = activity.PeerLoginExpirationDisabled } 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 !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 - + if inactivityExpirationChanged { event := activity.PeerInactivityExpirationEnabled - if !update.InactivityExpirationEnabled { + if !peer.InactivityExpirationEnabled { event = activity.PeerInactivityExpirationDisabled } 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 }