This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix setting a user's external_id via the admin API returns 500 and deletes users existing external mappings if that external ID is already mapped #11051
Merged
erikjohnston
merged 7 commits into
matrix-org:develop
from
dklimpel:external_ids_transaction
Oct 21, 2021
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
49cc814
Fix setting a user's external_id via the admin API returns
dklimpel 7fd4c49
newsfile
dklimpel caedb57
add own exception
dklimpel 949b1ae
rewrite db calls
dklimpel f6a2092
add exception handling to `record_user_external_id` and
dklimpel e40bcd4
move functions
dklimpel fa32735
mypy
dklimpel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix a bug where setting a user's external_id via the admin API returns 500 and deletes users existing external mappings if that external ID is already mapped. |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,14 +16,18 @@ | |
import logging | ||
import random | ||
import re | ||
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union | ||
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple, Union | ||
|
||
import attr | ||
|
||
from synapse.api.constants import UserTypes | ||
from synapse.api.errors import Codes, StoreError, SynapseError, ThreepidValidationError | ||
from synapse.metrics.background_process_metrics import wrap_as_background_process | ||
from synapse.storage.database import DatabasePool, LoggingDatabaseConnection | ||
from synapse.storage.database import ( | ||
DatabasePool, | ||
LoggingDatabaseConnection, | ||
LoggingTransaction, | ||
) | ||
from synapse.storage.databases.main.cache import CacheInvalidationWorkerStore | ||
from synapse.storage.databases.main.stats import StatsStore | ||
from synapse.storage.types import Connection, Cursor | ||
|
@@ -589,14 +593,30 @@ async def record_user_external_id( | |
external_id: id on that system | ||
user_id: complete mxid that it is mapped to | ||
""" | ||
await self.db_pool.simple_insert( | ||
await self.db_pool.runInteraction( | ||
"record_user_external_id", | ||
self._record_user_external_id_txn, | ||
auth_provider, | ||
external_id, | ||
user_id, | ||
) | ||
|
||
def _record_user_external_id_txn( | ||
self, | ||
txn: LoggingTransaction, | ||
auth_provider: str, | ||
external_id: str, | ||
user_id: str, | ||
) -> None: | ||
|
||
self.db_pool.simple_insert_txn( | ||
txn, | ||
table="user_external_ids", | ||
values={ | ||
"auth_provider": auth_provider, | ||
"external_id": external_id, | ||
"user_id": user_id, | ||
}, | ||
desc="record_user_external_id", | ||
) | ||
|
||
async def remove_user_external_id( | ||
|
@@ -611,16 +631,78 @@ async def remove_user_external_id( | |
external_id: id on that system | ||
user_id: complete mxid that it is mapped to | ||
""" | ||
await self.db_pool.simple_delete( | ||
await self.db_pool.runInteraction( | ||
"remove_user_external_id", | ||
self._remove_user_external_id_txn, | ||
auth_provider, | ||
external_id, | ||
user_id, | ||
) | ||
|
||
def _remove_user_external_id_txn( | ||
self, | ||
txn: LoggingTransaction, | ||
auth_provider: str, | ||
external_id: str, | ||
user_id: str, | ||
) -> None: | ||
|
||
self.db_pool.simple_delete_txn( | ||
txn, | ||
table="user_external_ids", | ||
keyvalues={ | ||
"auth_provider": auth_provider, | ||
"external_id": external_id, | ||
"user_id": user_id, | ||
}, | ||
desc="remove_user_external_id", | ||
) | ||
|
||
async def record_and_remove_user_external_id( | ||
self, | ||
record_external_ids: Set[Tuple[str, str]], | ||
remove_external_ids: Set[Tuple[str, str]], | ||
user_id: str, | ||
) -> None: | ||
"""Record and remove mappings from external user ids to a mxid | ||
in a single transaction. | ||
|
||
Args: | ||
record_external_ids: | ||
set with tuple of auth_provider and external_id to record | ||
remove_external_ids: | ||
set with tuple of auth_provider and external_id to remove | ||
user_id: complete mxid that it is mapped to | ||
Raises: | ||
StoreError if the new external_id could not be mapped. | ||
""" | ||
|
||
def _record_and_remove_user_external_id_txn( | ||
txn: LoggingTransaction, | ||
): | ||
for auth_provider, external_id in record_external_ids: | ||
self._record_user_external_id_txn( | ||
txn, | ||
auth_provider, | ||
external_id, | ||
user_id, | ||
) | ||
|
||
for auth_provider, external_id in remove_external_ids: | ||
self._remove_user_external_id_txn( | ||
txn, | ||
auth_provider, | ||
external_id, | ||
user_id, | ||
) | ||
|
||
try: | ||
await self.db_pool.runInteraction( | ||
"record_and_remove_user_external_id", | ||
_record_and_remove_user_external_id_txn, | ||
) | ||
except self.database_engine.module.IntegrityError: | ||
raise StoreError(409, "External id already in use.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're trying to avoid setting HTTP status codes in the guts of the storage layer, as it leads to weird bugs when code gets reused. Could you add a quick new exception type and catch that in the rest handler please? It just needs to be something like: class ExternalIDReuseException(Exception):
pass |
||
|
||
async def get_user_by_external_id( | ||
self, auth_provider: str, external_id: str | ||
) -> Optional[str]: | ||
|
Oops, something went wrong.
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.
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.
I think it might be easier to instead have a storage function that takes the
external_ids
and just delete all existing mappings for the user and insert allexternal_ids
. It's saves us from having to do the dance of pulling the existing IDs outThere 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.
I have
record_and_remove_user_external_id
toreplace_user_external_id
replace_user_external_id
to delete all mappings and add given mappingsremove_user_external_id
to orign/delop branch, no_txn
call is neededremove_user_external_ids
to delete all mappings with one sql query_remove_user_external_ids_txn