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

[MI-3629] Added cluster mutexes and solved the problem of race conditions for whitelist #351

Merged
merged 27 commits into from
Oct 25, 2023

Conversation

manojmalik20
Copy link
Collaborator

Summary

  • Created store functions for locking and unlocking the whitelist
  • Modified the store fuctions to use transactions for accessing the whitelist
  • Created new functions for processing and validating the configuration and used them everywhere
  • Fixed an existing bug in the needsConnect API
  • Added the logic of locking and accessing the whitelist in the complete OAuth API
  • Added the logic of reverting the OAuth if there's an error in accessing the whitelist

Related PRs

#342

…lowed through a whitelist

Created a new config setting called connectedUsersAllowed
Added validation on this config setting that its value should be greater than or equal to the no. of connected users already present in the DB
Added the logic to validate this in the OnActivate function
Created several store functions for different usecases
Modified the connect command handler to check for whitelist
Added the logic to store the user in whitelist in the oAuth redirect API handler
Fixed a bug in the OAuth flow regarding storing the user in whitelist
Added the whitelist logic in the connect bot command
Added unit tests for connect and connect bot slash command
Added unit tests for the store functions
…itelist size when updating the config value

Changed the help text and name of the config variable
Removed the function of getting count of connected users from the store
…itions for whitelist

Created store functions for locking and unlocking the whitelist
Modified the store fuctions to use transactions for accessing the whitelist
Created new functions for processing and validating the configuration and used them everywhere
Fixed an existing bug in the needsConnect API
Added the logic of locking and accessing the whitelist in the complete OAuth API
Added the logic of reverting the OAuth if there's an error in accessing the whitelist
@manojmalik20 manojmalik20 added Difficulty/3:Hard Hard ticket Test Cases Includes unit test cases changes labels Oct 18, 2023
Base automatically changed from MI-3606 to main October 18, 2023 13:52
Copy link
Contributor

@ayusht2810 ayusht2810 left a comment

Choose a reason for hiding this comment

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

Fix ci

server/api.go Outdated Show resolved Hide resolved
server/configuration.go Outdated Show resolved Hide resolved
server/configuration.go Outdated Show resolved Hide resolved
server/store/store.go Outdated Show resolved Hide resolved
server/api.go Outdated Show resolved Hide resolved
server/plugin.go Show resolved Hide resolved
Changed definition of a function to take the transaction as the first parameter
Modified the validation of the configuration variables
Fixed the data race in the store test functions
server/store/store_test.go Outdated Show resolved Hide resolved
Copy link
Member

@jespino jespino left a comment

Choose a reason for hiding this comment

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

It looks good to me.

server/api.go Outdated Show resolved Hide resolved
server/api.go Outdated Show resolved Hide resolved
…ed the commit transaction and unlock whitelist flow in OAuth redirect handler
Copy link
Member

@lieut-data lieut-data left a comment

Choose a reason for hiding this comment

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

I'm hesitant to see something like LOCK TABLE used outside of exceptional scenarios. Is there a problem description of what race conditions we're trying to solve?

@manojmalik20
Copy link
Collaborator Author

Is there a problem description of what race conditions we're trying to solve?

@lieut-data Consider this scenario: There is only one slot left in the whitelist. Two users run the /msteams connect command simultaneously. The code checks the size of the whitelist and sees that one slot is empty, so it responds with the link to connect their accounts. Now, both users click on the link to connect their accounts and are going through the OAuth process, the user who completes the OAuth first should be connected and acquire the spot on the whitelist. The other should see the respective error message that the max limit of connected users has been reached and he can't connect even though his OAuth was successful. But before this PR, both users were able to connect and the whitelist size was increasing more than what is allowed.

@lieut-data
Copy link
Member

Thanks for clarifying @manojmalik20! Might we consider using our cluster.Mutex for this scenario instead?

See https://github.com/mattermost/mattermost/blob/4a0cf786bece1209a918ef5c19c99b69349e88e2/server/public/pluginapi/cluster/mutex.go#L32-L41

…mutex

Removed the store function Lock and Unlock Whitelist along with the unit tests
Added a new mutex in the plugin struct for whitelist
Added the logic to lock and unlock mutex in connect slash command handlers
Added the logic to lock and unlock mutex in OAuth redirect handler
Fixed the failing unit tests
@manojmalik20 manojmalik20 changed the title [MI-3629] Added table level locks and solved the problem of race conditions for whitelist [MI-3629] Added cluster mutexes and solved the problem of race conditions for whitelist Oct 25, 2023
server/plugin_test.go Outdated Show resolved Hide resolved
server/store/sqlstore/store.go Outdated Show resolved Hide resolved
server/store/sqlstore/store.go Outdated Show resolved Hide resolved
server/store/sqlstore/store.go Outdated Show resolved Hide resolved
server/api.go Outdated Show resolved Hide resolved
Copy link
Member

@lieut-data lieut-data left a comment

Choose a reason for hiding this comment

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

Thanks, @manojmalik20! Awesome to see this simplify things. A few follow up comments.

server/command.go Outdated Show resolved Hide resolved
server/command.go Outdated Show resolved Hide resolved
server/store/sqlstore/store.go Outdated Show resolved Hide resolved
server/store/sqlstore/store.go Outdated Show resolved Hide resolved
server/store/sqlstore/store.go Outdated Show resolved Hide resolved
Removed the use of transaction from the parameters of whitelist functions
Fixed the failing unit tests
Removed the logic of locking/unlocking whitelist mutex when running connect slash command
Added better error handling for duplicate entry errors of Postgres and MySQL
Copy link
Member

@lieut-data lieut-data left a comment

Choose a reason for hiding this comment

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

Thanks, @manojmalik20!

@manojmalik20 manojmalik20 merged commit c2cf8e8 into main Oct 25, 2023
1 check passed
@manojmalik20 manojmalik20 deleted the MI-3629 branch October 25, 2023 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty/3:Hard Hard ticket Test Cases Includes unit test cases changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants