Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proactively expire peers' login per account #698

Merged
merged 25 commits into from
Feb 27, 2023

Conversation

braginini
Copy link
Contributor

@braginini braginini commented Feb 20, 2023

Describe your changes

Goals:

  • Enable peer login expiration when adding new peer
  • Expire peer's login when the time comes

The account manager triggers peer expiration routine in future if the
following conditions are true:

  • peer expiration is enabled for the account
  • there is at least one peer that has expiration enabled and is connected

The time of the next expiration check is based on the nearest peer expiration.
Account manager finds a peer with the oldest last login (auth) timestamp and
calculates the time when it has to run the routine as a sum of the configured
peer login expiration duration and the peer's last login time.

When triggered, the expiration routine checks whether there are expired peers.
The management server closes the update channel of these peers and updates
network map of other peers to exclude expired peers so that the expired peers
are not able to connect anywhere.

The account manager can reschedule or cancel peer expiration in the following cases:

  • when admin changes account setting (peer expiration enable/disable)
  • when admin updates the expiration duration of the account
  • when admin updates peer expiration (enable/disable)
  • when peer connects (Sync)

P.S. The network map calculation was updated to exclude peers that have login expired.

Issue ticket number and link

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • Extended the README / documentation, if necessary

@braginini braginini changed the title Enable peer login expiration by default when adding a peer Proactively expire peers' login per account Feb 22, 2023
@braginini braginini marked this pull request as ready for review February 22, 2023 16:35
@braginini braginini marked this pull request as draft February 22, 2023 16:36
@braginini braginini requested review from mlsmaycon, gigovich, pappz and pascal-fischer and removed request for mlsmaycon February 22, 2023 16:48
@@ -307,6 +308,53 @@ func (a *Account) GetGroup(groupID string) *Group {
return a.Groups[groupID]
}

// GetExpiredPeers returns peers tha have been expired
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a 't' in that

}

// GetNextPeerExpiration returns the minimum duration in which the next peer of the account will expire and true if it was found.
// If there is no peer that expires this function returns false and a zero time.Duration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we return max value not 0 if no peer is found

If there is no peer that is not yet expired, this function returns false and the max time.Duration value as default

Comment on lines 330 to 344
if len(peersWithExpiry) == 0 {
return nextExpiry, false
}
for _, peer := range peersWithExpiry {
// consider only connected peers because others will require login on connecting to the management server
if peer.Status.LoginExpired || !peer.Status.Connected {
continue
}
_, duration := peer.LoginExpired(a.Settings.PeerLoginExpiration)
if duration < nextExpiry {
nextExpiry = duration
}
}

return nextExpiry, nextExpiry != time.Duration(1<<63-1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe go with nil instead of max value for nextExpiry if no more expiring peers are available as it is more clean from a coding point of view to not return a value if there is no value available.

Comment on lines 727 to 737
for _, peer := range account.GetExpiredPeers() {
peerIDs = append(peerIDs, peer.ID)
peer.MarkLoginExpired(true)
account.UpdatePeer(peer)
err = am.Store.SavePeerStatus(account.Id, peer.ID, *peer.Status)
if err != nil {
log.Errorf("failed saving peer status while expiring peer %s", peer.ID)
// todo return retry?
return false, 0
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure on this one but I think we do way to much computing overhead here.

account.GetExpiredPeers() will return the complete list of expired peers. Even peers that have been expired for months. So in case we we have lots of zombies in the account that are not used anymore but are doing database overrides and peer updates for all of them on any run with the same (unchanged) data.

I would add additional check for status changes at the top of the loop:

if peer.Status.LoginExpired {
     continue
}

}
}

log.Debugf("discovered %d peers to expire for account %s", len(peerIDs), account.Id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log message should also show wrong number of peers. Not newly expired but total expired in account

@braginini braginini marked this pull request as ready for review February 27, 2023 10:06
@braginini braginini merged commit f984b8a into main Feb 27, 2023
@braginini braginini deleted the feature/force_peer_expiration branch February 27, 2023 15:44
pulsastrix pushed a commit to pulsastrix/netbird that referenced this pull request Dec 24, 2023
Goals:

Enable peer login expiration when adding new peer
Expire peer's login when the time comes
The account manager triggers peer expiration routine in future if the
following conditions are true:

peer expiration is enabled for the account
there is at least one peer that has expiration enabled and is connected
The time of the next expiration check is based on the nearest peer expiration.
Account manager finds a peer with the oldest last login (auth) timestamp and
calculates the time when it has to run the routine as a sum of the configured
peer login expiration duration and the peer's last login time.

When triggered, the expiration routine checks whether there are expired peers.
The management server closes the update channel of these peers and updates
network map of other peers to exclude expired peers so that the expired peers
are not able to connect anywhere.

The account manager can reschedule or cancel peer expiration in the following cases:

when admin changes account setting (peer expiration enable/disable)
when admin updates the expiration duration of the account
when admin updates peer expiration (enable/disable)
when peer connects (Sync)
P.S. The network map calculation was updated to exclude peers that have login expired.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants