-
-
Notifications
You must be signed in to change notification settings - Fork 536
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
[management] Refactor User JWT group sync #2690
Conversation
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID) | ||
defer unlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly I think we can not yet get rid of the account lock as long as we still have saveAccount operations somewhere
management/server/account.go
Outdated
account, err := am.requestBuffer.GetAccountWithBackpressure(ctx, accountID) | ||
if err != nil { | ||
return fmt.Errorf("error getting account: %w", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having this part within the transaction might be unfortunate. Can we add a flag that after a successful transaction, we do the account peers update?
management/server/account.go
Outdated
group, err := transaction.GetGroupByID(ctx, LockingStrengthShare, g, accountID) | ||
if err != nil { | ||
log.WithContext(ctx).Debugf("group %s not found while saving user activity event of account %s", g, accountID) | ||
} else { | ||
meta := map[string]any{ | ||
"group": group.Name, "group_id": group.ID, | ||
"is_service_user": user.IsServiceUser, "user_name": user.ServiceUserName, | ||
} | ||
am.StoreEvent(ctx, user.Id, user.Id, accountID, activity.GroupAddedToUser, meta) | ||
} | ||
} | ||
|
||
for _, g := range removeOldGroups { | ||
if group := account.GetGroup(g); group != nil { | ||
am.StoreEvent(ctx, user.Id, user.Id, account.Id, activity.GroupRemovedFromUser, | ||
map[string]any{ | ||
"group": group.Name, | ||
"group_id": group.ID, | ||
"is_service_user": user.IsServiceUser, | ||
"user_name": user.ServiceUserName}) | ||
group, err := transaction.GetGroupByID(ctx, LockingStrengthShare, g, accountID) | ||
if err != nil { | ||
log.WithContext(ctx).Debugf("group %s not found while saving user activity event of account %s", g, accountID) | ||
} else { | ||
meta := map[string]any{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having the event writing as part of the transaction should be good from a security perspective. But rolling back in case of failing event storing will change how our system works. I think this is a good approach but we should discuss this so everyone is on the same page.
func (s *SqlStore) AddUserPeersToGroups(ctx context.Context, accountID string, userID string, groupIDs []string) error { | ||
if len(groupIDs) == 0 { | ||
return nil | ||
} | ||
|
||
var userPeerIDs []string | ||
result := s.db.WithContext(ctx).Clauses(clause.Locking{Strength: string(LockingStrengthShare)}).Select("id"). | ||
Where("account_id = ? AND user_id = ?", accountID, userID).Model(&nbpeer.Peer{}).Find(&userPeerIDs) | ||
if result.Error != nil { | ||
return status.Errorf(status.Internal, "issue finding user peers") | ||
} | ||
|
||
groupsToUpdate := make([]*nbgroup.Group, 0, len(groupIDs)) | ||
for _, gid := range groupIDs { | ||
group, err := s.GetGroupByID(ctx, LockingStrengthShare, gid, accountID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
groupPeers := make(map[string]struct{}) | ||
for _, pid := range group.Peers { | ||
groupPeers[pid] = struct{}{} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is too much business logic in the store. This needs to be moved into the account manager
func (s *SqlStore) RemoveUserPeersFromGroups(ctx context.Context, accountID string, userID string, groupIDs []string) error { | ||
if len(groupIDs) == 0 { | ||
return nil | ||
} | ||
|
||
var userPeerIDs []string | ||
result := s.db.WithContext(ctx).Clauses(clause.Locking{Strength: string(LockingStrengthShare)}).Select("id"). | ||
Where("account_id = ? AND user_id = ?", accountID, userID).Model(&nbpeer.Peer{}).Find(&userPeerIDs) | ||
if result.Error != nil { | ||
return status.Errorf(status.Internal, "issue finding user peers") | ||
} | ||
|
||
groupsToUpdate := make([]*nbgroup.Group, 0, len(groupIDs)) | ||
for _, gid := range groupIDs { | ||
group, err := s.GetGroupByID(ctx, LockingStrengthShare, gid, accountID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if group.Name == "All" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also to much business logic in store
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
# Conflicts: # management/server/file_store.go
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
0c25837
to
adf8d48
Compare
Quality Gate passedIssues Measures |
Describe your changes
Issue ticket number and link
Checklist