Repository navigation
Conversation
Refreshing a token locks the user row and then the family row. Deleting a user let the ORM delete the family rows first and the user row after, so the two could deadlock, and PostgreSQL then aborted the deletion. Take the lock on the user row at the start of delete_user, without autoflush, so that both paths lock in the same order, also when the session holds pending changes to the families. Signed-off-by: Serhii <jshmitz@me.com>
The model's index=True yields refresh_token_tracker_user_id_idx under the project's naming convention, while the migration created ix_refresh_token_tracker_user_id, so flexmeasures db check reported the index as removed and added. Signed-off-by: Serhii <jshmitz@me.com>
…ad requests A request that fails validation, such as a missing, null, empty or non-string refresh-token, returns 422, as validation errors do elsewhere in the API. Only JSON that cannot be parsed returns 400. Signed-off-by: Serhii <jshmitz@me.com>
…ded safeguards Removing the inactive-user check from the token exchange, or the commit after an explicit revocation, left all tests green. The first is now covered by deactivating a user with a bulk update, which skips the listener that revokes the families. The second by rolling back the shared session after revoking, so that a later request cannot commit the pending revocation. Signed-off-by: Serhii <jshmitz@me.com>
Documentation build overview
14 files changed ·
|
5 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes for points 1, 4, 5 and 6 of my review of #2849, as four commits on top of c212713. Points 2 (families piling up) and 3 (
expires-infor a zero lifetime) are left out, because each needs a choice that is yours.delete_user, so that deleting a user and refreshing that user's token lock in the same order (point 1)refresh_token_tracker_user_id_idx, as the naming convention does (point 5)documentation/changelog.rst: not needed, the entry of Sec/refresh tokens #2849 covers thisA correction to my review. There I suggested
passive_deletes=Trueon the backref for point 1. That is not enough: it only helps while the user's families are not loaded in the session. Withuser.refresh_trackersloaded, the ORM still deletes the families before the user, and the deadlock is back. I reproduced that on PostgreSQL, so this PR takes the lock indelete_userinstead and leaves the relationship as it was. The lock is taken without autoflush, because a flush at that point would write pending changes to the families before the user is locked.Look & Feel
flexmeasures db check, after upgrading with this branch, no longer mentionsrefresh_token_tracker.Status codes of
POST /api/refreshAuthTokenandPOST /api/revokeRefreshToken, unchanged by this PR and now documented as they are:{},{"refresh-token": null},{"refresh-token": 12},{"refresh-token": ""}, an unknown field{sent asapplication/jsonHow to test
pytest flexmeasures/api/tests/test_refresh_tokens_fresh_db.pyruns 42 tests: four new test functions, andtest_malformed_refresh_requestsnow expects exactly 422. Each test for a safeguard fails when the code it covers is taken out:delete_usertest_deleting_a_user_locks_the_user_before_the_families(all three cases)modifiednot user.activeinexchange_refresh_tokentest_user_disabled_outside_the_orm_cannot_refreshdb.session.commit()after an explicit revocationtest_revocation_is_committed_by_the_revoking_requestThe deadlock itself, on PostgreSQL 18, with a refresh paused between its two locks and
delete_userstarted in that window:deadlock detected, deletion aborted (3 of 3)deadlock detected, deletion abortedThe migration upgrades from
bf22b21d0990, downgrades and upgrades again.The full suite gives 3504 passed and 7 failed on my machine. The same seven fail on c212713 here:
test_get_users_inactiveandtest_get_assets(two cases each, sorting by name, my database uses theC.UTF-8collation),test_numerical_errors[cbc]andtest_closest_sensor(two cases, cause not looked into).Further Improvements
Points 2 and 3 of the review.
Related Items
Targets
sec/refresh-tokens(#2849).Sign-off