Skip to content

Commit 348d981

Browse files
[management] expire invalid peer (#4275)
1 parent d1e0b7f commit 348d981

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ require (
6363
github.com/miekg/dns v1.1.59
6464
github.com/mitchellh/hashstructure/v2 v2.0.2
6565
github.com/nadoo/ipset v0.5.0
66-
github.com/netbirdio/management-integrations/integrations v0.0.0-20250724151510-c007bc6b392c
66+
github.com/netbirdio/management-integrations/integrations v0.0.0-20250805075620-df61803a4461
6767
github.com/netbirdio/signal-dispatcher/dispatcher v0.0.0-20250514131221-a464fd5f30cb
6868
github.com/okta/okta-sdk-golang/v2 v2.18.0
6969
github.com/oschwald/maxminddb-golang v1.12.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,8 @@ github.com/netbirdio/go-netroute v0.0.0-20240611143515-f59b0e1d3944 h1:TDtJKmM6S
503503
github.com/netbirdio/go-netroute v0.0.0-20240611143515-f59b0e1d3944/go.mod h1:sHA6TRxjQ6RLbnI+3R4DZo2Eseg/iKiPRfNmcuNySVQ=
504504
github.com/netbirdio/ice/v3 v3.0.0-20240315174635-e72a50fcb64e h1:PURA50S8u4mF6RrkYYCAvvPCixhqqEiEy3Ej6avh04c=
505505
github.com/netbirdio/ice/v3 v3.0.0-20240315174635-e72a50fcb64e/go.mod h1:YMLU7qbKfVjmEv7EoZPIVEI+kNYxWCdPK3VS0BU+U4Q=
506-
github.com/netbirdio/management-integrations/integrations v0.0.0-20250724151510-c007bc6b392c h1:OtX903X0FKEE+fcsp/P2701md7X/xbi/W/ojWIJNKSk=
507-
github.com/netbirdio/management-integrations/integrations v0.0.0-20250724151510-c007bc6b392c/go.mod h1:Gi9raplYzCCyh07Olw/DVfCJTFgpr1WCXJ/Q+8TSA9Q=
506+
github.com/netbirdio/management-integrations/integrations v0.0.0-20250805075620-df61803a4461 h1:86MeBP7UxexphbOWpO5dxf5SLKlpGPYTDGR4m5WJ/Ew=
507+
github.com/netbirdio/management-integrations/integrations v0.0.0-20250805075620-df61803a4461/go.mod h1:Gi9raplYzCCyh07Olw/DVfCJTFgpr1WCXJ/Q+8TSA9Q=
508508
github.com/netbirdio/service v0.0.0-20240911161631-f62744f42502 h1:3tHlFmhTdX9axERMVN63dqyFqnvuD+EMJHzM7mNGON8=
509509
github.com/netbirdio/service v0.0.0-20240911161631-f62744f42502/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
510510
github.com/netbirdio/signal-dispatcher/dispatcher v0.0.0-20250514131221-a464fd5f30cb h1:Cr6age+ePALqlSvtp7wc6lYY97XN7rkD1K4XEDmY+TU=

management/server/account.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ func BuildManager(
251251
}()
252252
}
253253

254-
am.integratedPeerValidator.SetPeerInvalidationListener(func(accountID string) {
255-
am.onPeersInvalidated(ctx, accountID)
254+
am.integratedPeerValidator.SetPeerInvalidationListener(func(accountID string, peerIDs []string) {
255+
am.onPeersInvalidated(ctx, accountID, peerIDs)
256256
})
257257

258258
return am, nil
@@ -1719,9 +1719,27 @@ func (am *DefaultAccountManager) GetDNSDomain(settings *types.Settings) string {
17191719
return settings.DNSDomain
17201720
}
17211721

1722-
func (am *DefaultAccountManager) onPeersInvalidated(ctx context.Context, accountID string) {
1723-
log.WithContext(ctx).Debugf("validated peers has been invalidated for account %s", accountID)
1724-
am.BufferUpdateAccountPeers(ctx, accountID)
1722+
func (am *DefaultAccountManager) onPeersInvalidated(ctx context.Context, accountID string, peerIDs []string) {
1723+
peers := []*nbpeer.Peer{}
1724+
log.WithContext(ctx).Debugf("invalidating peers %v for account %s", peerIDs, accountID)
1725+
for _, peerID := range peerIDs {
1726+
peer, err := am.GetPeer(ctx, accountID, peerID, activity.SystemInitiator)
1727+
if err != nil {
1728+
log.WithContext(ctx).Errorf("failed to get invalidated peer %s for account %s: %v", peerID, accountID, err)
1729+
continue
1730+
}
1731+
peers = append(peers, peer)
1732+
}
1733+
if len(peers) > 0 {
1734+
err := am.expireAndUpdatePeers(ctx, accountID, peers)
1735+
if err != nil {
1736+
log.WithContext(ctx).Errorf("failed to expire and update invalidated peers for account %s: %v", accountID, err)
1737+
return
1738+
}
1739+
} else {
1740+
log.WithContext(ctx).Debugf("running invalidation with no invalid peers")
1741+
}
1742+
log.WithContext(ctx).Debugf("invalidated peers have been expired for account %s", accountID)
17251743
}
17261744

17271745
func (am *DefaultAccountManager) FindExistingPostureCheck(accountID string, checks *posture.ChecksDefinition) (*posture.Checks, error) {

management/server/integrated_validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (MockIntegratedValidator) PeerDeleted(_ context.Context, _, _ string, extra
151151
return nil
152152
}
153153

154-
func (MockIntegratedValidator) SetPeerInvalidationListener(func(accountID string)) {
154+
func (MockIntegratedValidator) SetPeerInvalidationListener(func(accountID string, peerIDs []string)) {
155155
// just a dummy
156156
}
157157

management/server/integrations/integrated_validator/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type IntegratedValidator interface {
1616
IsNotValidPeer(ctx context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *types.ExtraSettings) (bool, bool, error)
1717
GetValidatedPeers(ctx context.Context, accountID string, groups []*types.Group, peers []*nbpeer.Peer, extraSettings *types.ExtraSettings) (map[string]struct{}, error)
1818
PeerDeleted(ctx context.Context, accountID, peerID string, extraSettings *types.ExtraSettings) error
19-
SetPeerInvalidationListener(fn func(accountID string))
19+
SetPeerInvalidationListener(fn func(accountID string, peerIDs []string))
2020
Stop(ctx context.Context)
2121
ValidateFlowResponse(ctx context.Context, peerKey string, flowResponse *proto.PKCEAuthorizationFlow) *proto.PKCEAuthorizationFlow
2222
}

management/server/user.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ func (am *DefaultAccountManager) BuildUserInfosForAccount(ctx context.Context, a
938938

939939
// expireAndUpdatePeers expires all peers of the given user and updates them in the account
940940
func (am *DefaultAccountManager) expireAndUpdatePeers(ctx context.Context, accountID string, peers []*nbpeer.Peer) error {
941+
log.WithContext(ctx).Debugf("Expiring %d peers for account %s", len(peers), accountID)
941942
settings, err := am.Store.GetAccountSettings(ctx, store.LockingStrengthShare, accountID)
942943
if err != nil {
943944
return err
@@ -968,7 +969,7 @@ func (am *DefaultAccountManager) expireAndUpdatePeers(ctx context.Context, accou
968969
if len(peerIDs) != 0 {
969970
// this will trigger peer disconnect from the management service
970971
am.peersUpdateManager.CloseChannels(ctx, peerIDs)
971-
am.UpdateAccountPeers(ctx, accountID)
972+
am.BufferUpdateAccountPeers(ctx, accountID)
972973
}
973974
return nil
974975
}

0 commit comments

Comments
 (0)