mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-21 23:53:14 +01:00
[management] Add activity events to group propagation flow (#2916)
This commit is contained in:
parent
f66bbcc54c
commit
aa575d6f44
@ -965,7 +965,9 @@ func (am *DefaultAccountManager) getJWTGroupsChanges(user *User, groups []*nbgro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UserGroupsAddToPeers adds groups to all peers of user
|
// UserGroupsAddToPeers adds groups to all peers of user
|
||||||
func (a *Account) UserGroupsAddToPeers(userID string, groups ...string) {
|
func (a *Account) UserGroupsAddToPeers(userID string, groups ...string) map[string][]string {
|
||||||
|
groupUpdates := make(map[string][]string)
|
||||||
|
|
||||||
userPeers := make(map[string]struct{})
|
userPeers := make(map[string]struct{})
|
||||||
for pid, peer := range a.Peers {
|
for pid, peer := range a.Peers {
|
||||||
if peer.UserID == userID {
|
if peer.UserID == userID {
|
||||||
@ -979,6 +981,8 @@ func (a *Account) UserGroupsAddToPeers(userID string, groups ...string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oldPeers := group.Peers
|
||||||
|
|
||||||
groupPeers := make(map[string]struct{})
|
groupPeers := make(map[string]struct{})
|
||||||
for _, pid := range group.Peers {
|
for _, pid := range group.Peers {
|
||||||
groupPeers[pid] = struct{}{}
|
groupPeers[pid] = struct{}{}
|
||||||
@ -992,16 +996,25 @@ func (a *Account) UserGroupsAddToPeers(userID string, groups ...string) {
|
|||||||
for pid := range groupPeers {
|
for pid := range groupPeers {
|
||||||
group.Peers = append(group.Peers, pid)
|
group.Peers = append(group.Peers, pid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
groupUpdates[gid] = difference(group.Peers, oldPeers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return groupUpdates
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserGroupsRemoveFromPeers removes groups from all peers of user
|
// UserGroupsRemoveFromPeers removes groups from all peers of user
|
||||||
func (a *Account) UserGroupsRemoveFromPeers(userID string, groups ...string) {
|
func (a *Account) UserGroupsRemoveFromPeers(userID string, groups ...string) map[string][]string {
|
||||||
|
groupUpdates := make(map[string][]string)
|
||||||
|
|
||||||
for _, gid := range groups {
|
for _, gid := range groups {
|
||||||
group, ok := a.Groups[gid]
|
group, ok := a.Groups[gid]
|
||||||
if !ok || group.Name == "All" {
|
if !ok || group.Name == "All" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oldPeers := group.Peers
|
||||||
|
|
||||||
update := make([]string, 0, len(group.Peers))
|
update := make([]string, 0, len(group.Peers))
|
||||||
for _, pid := range group.Peers {
|
for _, pid := range group.Peers {
|
||||||
peer, ok := a.Peers[pid]
|
peer, ok := a.Peers[pid]
|
||||||
@ -1013,7 +1026,10 @@ func (a *Account) UserGroupsRemoveFromPeers(userID string, groups ...string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
group.Peers = update
|
group.Peers = update
|
||||||
|
groupUpdates[gid] = difference(oldPeers, group.Peers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return groupUpdates
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildManager creates a new DefaultAccountManager with a provided Store
|
// BuildManager creates a new DefaultAccountManager with a provided Store
|
||||||
@ -1175,6 +1191,11 @@ func (am *DefaultAccountManager) UpdateAccountSettings(ctx context.Context, acco
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = am.handleGroupsPropagationSettings(ctx, oldSettings, newSettings, userID, accountID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("groups propagation failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
updatedAccount := account.UpdateSettings(newSettings)
|
updatedAccount := account.UpdateSettings(newSettings)
|
||||||
|
|
||||||
err = am.Store.SaveAccount(ctx, account)
|
err = am.Store.SaveAccount(ctx, account)
|
||||||
@ -1185,6 +1206,19 @@ func (am *DefaultAccountManager) UpdateAccountSettings(ctx context.Context, acco
|
|||||||
return updatedAccount, nil
|
return updatedAccount, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (am *DefaultAccountManager) handleGroupsPropagationSettings(ctx context.Context, oldSettings, newSettings *Settings, userID, accountID string) error {
|
||||||
|
if oldSettings.GroupsPropagationEnabled != newSettings.GroupsPropagationEnabled {
|
||||||
|
if newSettings.GroupsPropagationEnabled {
|
||||||
|
am.StoreEvent(ctx, userID, accountID, accountID, activity.UserGroupPropagationEnabled, nil)
|
||||||
|
// Todo: retroactively add user groups to all peers
|
||||||
|
} else {
|
||||||
|
am.StoreEvent(ctx, userID, accountID, accountID, activity.UserGroupPropagationDisabled, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (am *DefaultAccountManager) handleInactivityExpirationSettings(ctx context.Context, account *Account, oldSettings, newSettings *Settings, userID, accountID string) error {
|
func (am *DefaultAccountManager) handleInactivityExpirationSettings(ctx context.Context, account *Account, oldSettings, newSettings *Settings, userID, accountID string) error {
|
||||||
|
|
||||||
if newSettings.PeerInactivityExpirationEnabled {
|
if newSettings.PeerInactivityExpirationEnabled {
|
||||||
|
@ -148,6 +148,9 @@ const (
|
|||||||
AccountPeerInactivityExpirationDurationUpdated Activity = 67
|
AccountPeerInactivityExpirationDurationUpdated Activity = 67
|
||||||
|
|
||||||
SetupKeyDeleted Activity = 68
|
SetupKeyDeleted Activity = 68
|
||||||
|
|
||||||
|
UserGroupPropagationEnabled Activity = 69
|
||||||
|
UserGroupPropagationDisabled Activity = 70
|
||||||
)
|
)
|
||||||
|
|
||||||
var activityMap = map[Activity]Code{
|
var activityMap = map[Activity]Code{
|
||||||
@ -222,6 +225,9 @@ var activityMap = map[Activity]Code{
|
|||||||
AccountPeerInactivityExpirationDisabled: {"Account peer inactivity expiration disabled", "account.peer.inactivity.expiration.disable"},
|
AccountPeerInactivityExpirationDisabled: {"Account peer inactivity expiration disabled", "account.peer.inactivity.expiration.disable"},
|
||||||
AccountPeerInactivityExpirationDurationUpdated: {"Account peer inactivity expiration duration updated", "account.peer.inactivity.expiration.update"},
|
AccountPeerInactivityExpirationDurationUpdated: {"Account peer inactivity expiration duration updated", "account.peer.inactivity.expiration.update"},
|
||||||
SetupKeyDeleted: {"Setup key deleted", "setupkey.delete"},
|
SetupKeyDeleted: {"Setup key deleted", "setupkey.delete"},
|
||||||
|
|
||||||
|
UserGroupPropagationEnabled: {"User group propagation enabled", "account.setting.group.propagation.enable"},
|
||||||
|
UserGroupPropagationDisabled: {"User group propagation disabled", "account.setting.group.propagation.disable"},
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringCode returns a string code of the activity
|
// StringCode returns a string code of the activity
|
||||||
|
@ -805,15 +805,20 @@ func (am *DefaultAccountManager) SaveOrAddUsers(ctx context.Context, accountID,
|
|||||||
expiredPeers = append(expiredPeers, blockedPeers...)
|
expiredPeers = append(expiredPeers, blockedPeers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
peerGroupsAdded := make(map[string][]string)
|
||||||
|
peerGroupsRemoved := make(map[string][]string)
|
||||||
if update.AutoGroups != nil && account.Settings.GroupsPropagationEnabled {
|
if update.AutoGroups != nil && account.Settings.GroupsPropagationEnabled {
|
||||||
removedGroups := difference(oldUser.AutoGroups, update.AutoGroups)
|
removedGroups := difference(oldUser.AutoGroups, update.AutoGroups)
|
||||||
// need force update all auto groups in any case they will not be duplicated
|
// need force update all auto groups in any case they will not be duplicated
|
||||||
account.UserGroupsAddToPeers(oldUser.Id, update.AutoGroups...)
|
peerGroupsAdded = account.UserGroupsAddToPeers(oldUser.Id, update.AutoGroups...)
|
||||||
account.UserGroupsRemoveFromPeers(oldUser.Id, removedGroups...)
|
peerGroupsRemoved = account.UserGroupsRemoveFromPeers(oldUser.Id, removedGroups...)
|
||||||
}
|
}
|
||||||
|
|
||||||
events := am.prepareUserUpdateEvents(ctx, initiatorUser.Id, oldUser, newUser, account, transferredOwnerRole)
|
userUpdateEvents := am.prepareUserUpdateEvents(ctx, initiatorUser.Id, oldUser, newUser, account, transferredOwnerRole)
|
||||||
eventsToStore = append(eventsToStore, events...)
|
eventsToStore = append(eventsToStore, userUpdateEvents...)
|
||||||
|
|
||||||
|
userGroupsEvents := am.prepareUserGroupsEvents(ctx, initiatorUser.Id, oldUser, newUser, account, peerGroupsAdded, peerGroupsRemoved)
|
||||||
|
eventsToStore = append(eventsToStore, userGroupsEvents...)
|
||||||
|
|
||||||
updatedUserInfo, err := getUserInfo(ctx, am, newUser, account)
|
updatedUserInfo, err := getUserInfo(ctx, am, newUser, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -872,32 +877,78 @@ func (am *DefaultAccountManager) prepareUserUpdateEvents(ctx context.Context, in
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return eventsToStore
|
||||||
|
}
|
||||||
|
|
||||||
|
func (am *DefaultAccountManager) prepareUserGroupsEvents(ctx context.Context, initiatorUserID string, oldUser, newUser *User, account *Account, peerGroupsAdded, peerGroupsRemoved map[string][]string) []func() {
|
||||||
|
var eventsToStore []func()
|
||||||
if newUser.AutoGroups != nil {
|
if newUser.AutoGroups != nil {
|
||||||
removedGroups := difference(oldUser.AutoGroups, newUser.AutoGroups)
|
removedGroups := difference(oldUser.AutoGroups, newUser.AutoGroups)
|
||||||
addedGroups := difference(newUser.AutoGroups, oldUser.AutoGroups)
|
addedGroups := difference(newUser.AutoGroups, oldUser.AutoGroups)
|
||||||
for _, g := range removedGroups {
|
|
||||||
group := account.GetGroup(g)
|
|
||||||
if group != nil {
|
|
||||||
eventsToStore = append(eventsToStore, func() {
|
|
||||||
am.StoreEvent(ctx, initiatorUserID, oldUser.Id, account.Id, activity.GroupRemovedFromUser,
|
|
||||||
map[string]any{"group": group.Name, "group_id": group.ID, "is_service_user": newUser.IsServiceUser, "user_name": newUser.ServiceUserName})
|
|
||||||
})
|
|
||||||
|
|
||||||
} else {
|
removedEvents := am.handleGroupRemovedFromUser(ctx, initiatorUserID, oldUser, newUser, account, removedGroups, peerGroupsRemoved)
|
||||||
log.WithContext(ctx).Errorf("group %s not found while saving user activity event of account %s", g, account.Id)
|
eventsToStore = append(eventsToStore, removedEvents...)
|
||||||
}
|
|
||||||
}
|
addedEvents := am.handleGroupAddedToUser(ctx, initiatorUserID, oldUser, newUser, account, addedGroups, peerGroupsAdded)
|
||||||
for _, g := range addedGroups {
|
eventsToStore = append(eventsToStore, addedEvents...)
|
||||||
group := account.GetGroup(g)
|
}
|
||||||
if group != nil {
|
return eventsToStore
|
||||||
eventsToStore = append(eventsToStore, func() {
|
}
|
||||||
am.StoreEvent(ctx, initiatorUserID, oldUser.Id, account.Id, activity.GroupAddedToUser,
|
|
||||||
map[string]any{"group": group.Name, "group_id": group.ID, "is_service_user": newUser.IsServiceUser, "user_name": newUser.ServiceUserName})
|
func (am *DefaultAccountManager) handleGroupAddedToUser(ctx context.Context, initiatorUserID string, oldUser, newUser *User, account *Account, addedGroups []string, peerGroupsAdded map[string][]string) []func() {
|
||||||
})
|
var eventsToStore []func()
|
||||||
}
|
for _, g := range addedGroups {
|
||||||
|
group := account.GetGroup(g)
|
||||||
|
if group != nil {
|
||||||
|
eventsToStore = append(eventsToStore, func() {
|
||||||
|
am.StoreEvent(ctx, initiatorUserID, oldUser.Id, account.Id, activity.GroupAddedToUser,
|
||||||
|
map[string]any{"group": group.Name, "group_id": group.ID, "is_service_user": newUser.IsServiceUser, "user_name": newUser.ServiceUserName})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for groupID, peerIDs := range peerGroupsAdded {
|
||||||
|
group := account.GetGroup(groupID)
|
||||||
|
for _, peerID := range peerIDs {
|
||||||
|
peer := account.GetPeer(peerID)
|
||||||
|
eventsToStore = append(eventsToStore, func() {
|
||||||
|
meta := map[string]any{
|
||||||
|
"group": group.Name, "group_id": group.ID,
|
||||||
|
"peer_ip": peer.IP.String(), "peer_fqdn": peer.FQDN(am.GetDNSDomain()),
|
||||||
|
}
|
||||||
|
am.StoreEvent(ctx, activity.SystemInitiator, peer.ID, account.Id, activity.GroupAddedToPeer, meta)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return eventsToStore
|
||||||
|
}
|
||||||
|
|
||||||
|
func (am *DefaultAccountManager) handleGroupRemovedFromUser(ctx context.Context, initiatorUserID string, oldUser, newUser *User, account *Account, removedGroups []string, peerGroupsRemoved map[string][]string) []func() {
|
||||||
|
var eventsToStore []func()
|
||||||
|
for _, g := range removedGroups {
|
||||||
|
group := account.GetGroup(g)
|
||||||
|
if group != nil {
|
||||||
|
eventsToStore = append(eventsToStore, func() {
|
||||||
|
am.StoreEvent(ctx, initiatorUserID, oldUser.Id, account.Id, activity.GroupRemovedFromUser,
|
||||||
|
map[string]any{"group": group.Name, "group_id": group.ID, "is_service_user": newUser.IsServiceUser, "user_name": newUser.ServiceUserName})
|
||||||
|
})
|
||||||
|
|
||||||
|
} else {
|
||||||
|
log.WithContext(ctx).Errorf("group %s not found while saving user activity event of account %s", g, account.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for groupID, peerIDs := range peerGroupsRemoved {
|
||||||
|
group := account.GetGroup(groupID)
|
||||||
|
for _, peerID := range peerIDs {
|
||||||
|
peer := account.GetPeer(peerID)
|
||||||
|
eventsToStore = append(eventsToStore, func() {
|
||||||
|
meta := map[string]any{
|
||||||
|
"group": group.Name, "group_id": group.ID,
|
||||||
|
"peer_ip": peer.IP.String(), "peer_fqdn": peer.FQDN(am.GetDNSDomain()),
|
||||||
|
}
|
||||||
|
am.StoreEvent(ctx, activity.SystemInitiator, peer.ID, account.Id, activity.GroupRemovedFromPeer, meta)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
return eventsToStore
|
return eventsToStore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user