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

Added a logic to disconnect users from ServiceNow on change of the encryption secret. #171

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

Nityanand13
Copy link
Contributor

@Nityanand13 Nityanand13 commented Feb 17, 2023

Summary

When an admin changes the encryption secret then all the users will be disconnected from ServiceNow and they have to reconnect their accounts to ServiceNow.

…e of the encryption secret. (#42)

* [MI-2032]: Modified the error when a user updates his encryption secret

* [MI-2032]: Review fixes done
1. Fixed linting errors

* [MI-2032]: Review fixes done
1.Improved code quality

* [MI-2032]: Review fixes done
1. Improved code quality

* [MI-2032]: Added a logic that if we change the encryption secret then at that time only all the users will be disconnected from service now.

* [MI-2032]: Modified code quality

* [MI-2032]: Review fixes done
1. Improved code quality

* [MI-2032]: Review fixes done
1. Implemented separate goroutine to delete users

* [MI-2032]: Review fixes done
1. Used goroutines inside a loop
2. Improved code readability
@hanzei hanzei added the Work In Progress Not yet ready for review label Mar 28, 2023
@ayusht2810
Copy link
Contributor

@hanzei the below PR is ready for review. Can please assign the designated reviewer to it.

@hanzei hanzei requested a review from mickmister August 8, 2023 10:38
@hanzei hanzei added 2: Dev Review Requires review by a core committer 3: QA Review Requires review by a QA tester and removed Work In Progress Not yet ready for review labels Aug 8, 2023
Comment on lines +146 to +148
if oldEncryptionSecret != "" && oldEncryptionSecret != p.getConfiguration().EncryptionSecret {
go p.store.DeleteUserTokenOnEncryptionSecretChange()
}
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we delete the users even if the oldEncryptionSecret is blank? As long as the secret changed

@mickmister mickmister requested a review from hanzei August 21, 2023 14:38
Copy link
Contributor

@hanzei hanzei left a comment

Choose a reason for hiding this comment

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

Thanks for the PR 👍

I'm wondering how the code behaves in a HA environment. Are there issues that can arise when multiple servers race to delete the user list?

@codecov-commenter
Copy link

codecov-commenter commented Sep 14, 2023

Codecov Report

Attention: Patch coverage is 0% with 66 lines in your changes are missing coverage. Please review.

Project coverage is 63.85%. Comparing base (a5ca244) to head (30cad00).

Files Patch % Lines
server/plugin/kv.go 0.00% 62 Missing ⚠️
server/plugin/configuration.go 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #171      +/-   ##
==========================================
- Coverage   65.40%   63.85%   -1.55%     
==========================================
  Files          17       17              
  Lines        2064     2114      +50     
==========================================
  Hits         1350     1350              
- Misses        674      724      +50     
  Partials       40       40              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hanzei hanzei added the Awaiting Submitter Action Blocked on the author label Oct 11, 2023
Copy link

@AayushChaudhary0001 AayushChaudhary0001 left a comment

Choose a reason for hiding this comment

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

The PR above has been tested for the following scenario:-

  • On changing the encryption secret in MM, the connected users are disconnected from Servicenow.

The PR was working fine for the above condition, LGTM. Approved.

@Kshitij-Katiyar
Copy link
Contributor

Thanks for the PR 👍

I'm wondering how the code behaves in a HA environment. Are there issues that can arise when multiple servers race to delete the user list?

@hanzei #171 (review) QA tested this PR for the HA environment and its working perfectly fine.

@hanzei
Copy link
Contributor

hanzei commented Mar 22, 2024

I'm leaving it to @mickmister to suggest next steps on this PR

Comment on lines 85 to 86
wg := new(sync.WaitGroup)
mu := new(sync.Mutex)
Copy link
Member

Choose a reason for hiding this comment

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

Right now we add to this wait group once per user. That seems a bit excessive. What if there are a very large amount of users? We should probably fan this out with at most 5-10 goroutines at a time. @hanzei Thoughts on this? Also, with the current strategy we would be slamming the database all at once, and on multiple nodes in an HA environment as @hanzei pointed out.

We definitely want to coordinate this so it only runs on one node. We can use cluster.Mutex to coordinate this. We did something similar (a "migration" of sorts) using this here mattermost/mattermost-plugin-gitlab#361.

This PR is a bit different though, since this will be run "on demand" when the secret changes. Here's how I see this working:

  • We check if the secret changed as usual. If not changed, do normal stuff without any of the below steps
  • We fetch a mutex for delete-all-users
  • With the mutex, we check the kv store key delete-all-users-running (@hanzei wondering about race conditions here, where the database may not be "ready" to return the freshly set key on another node's database connection. We've run into these timing things before where the kvstore had not reflected the most recently updated state in real-time)
    • If the key does exist
      • Immediately release the mutex
      • Do not call DeleteAllUsers
    • If the key does not exist
      • Set the delete-all-users-running value to true
      • In a defer block right below that, remove the delete-all-users-running key
      • Immediately release the mutex
      • Call DeleteAllUsers

This can all be done in a goroutine in OnConfigurationChange so we don't block that function from returning

Copy link
Contributor

Choose a reason for hiding this comment

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

We definitely want to coordinate this so it only runs on one node. We can use cluster.Mutex to coordinate this. We did something similar (a "migration" of sorts) using this here mattermost/mattermost-plugin-gitlab#361.

Strong 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why we are fanning out the requests at all.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I'm thinking the same now. I think we should do this synchronously here with no wait group or goroutines. @Kshitij-Katiyar What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, I think we should do this synchronously as this is a very minor edge case as well. @mickmister

hanzei
hanzei previously requested changes Mar 25, 2024
server/plugin/kv.go Outdated Show resolved Hide resolved
server/plugin/kv.go Outdated Show resolved Hide resolved
@ayusht2810
Copy link
Contributor

@hanzei @mickmister fixed the review comments. Please re-review.

@hanzei hanzei requested a review from mickmister March 31, 2024 08:48
@hanzei hanzei removed the Awaiting Submitter Action Blocked on the author label Mar 31, 2024
@ayusht2810
Copy link
Contributor

@mickmister @hanzei Gentle reminder to re-review the PR

@mickmister
Copy link
Member

There's no customer reported ticket associated with this, so I'm not sure if this is a priority that should require this complexity

@Kshitij-Katiyar
Copy link
Contributor

There's no customer reported ticket associated with this, so I'm not sure if this is a priority that should require this complexity

This was a problem we faced while developing, that's why we have added this feature as an enhancement.
@mickmister

Copy link
Member

@mickmister mickmister left a comment

Choose a reason for hiding this comment

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

The PR mostly LGTM. I think the goroutines used to fetch all users may be unnecessary. Otherwise looks good 👍

server/plugin/kv.go Outdated Show resolved Hide resolved
server/plugin/kv.go Outdated Show resolved Hide resolved
Comment on lines 85 to 86
wg := new(sync.WaitGroup)
mu := new(sync.Mutex)
Copy link
Member

Choose a reason for hiding this comment

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

Yeah I'm thinking the same now. I think we should do this synchronously here with no wait group or goroutines. @Kshitij-Katiyar What do you think?

server/plugin/kv.go Outdated Show resolved Hide resolved
@ayusht2810
Copy link
Contributor

@mickmister fixed the review comments. Please re-review

Copy link
Member

@mickmister mickmister left a comment

Choose a reason for hiding this comment

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

LGTM, just one suggestion to remove the lock since we're doing this synchronously now

server/plugin/kv.go Outdated Show resolved Hide resolved
server/plugin/kv.go Outdated Show resolved Hide resolved
@ayusht2810
Copy link
Contributor

@mickmister fixed the review comments. Please re-review

Copy link

@fmartingr fmartingr left a comment

Choose a reason for hiding this comment

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

Good work. I needed a few reads to understood what was going on, left my thought in comments but not blocking.

This would be fairly useful for other plugins as well @mickmister (mattermost/mattermost-plugin-google-calendar#61)

server/plugin/kv.go Outdated Show resolved Hide resolved
server/plugin/kv.go Show resolved Hide resolved
@ayusht2810
Copy link
Contributor

@fmartingr Fixed the review fixes. Please re-review

@fmartingr fmartingr self-requested a review June 20, 2024 13:22
Copy link

@fmartingr fmartingr left a comment

Choose a reason for hiding this comment

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

Re-approved. Thanks for addressing my concerns!

@mickmister mickmister removed the 2: Dev Review Requires review by a core committer label Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3: QA Review Requires review by a QA tester
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants