Repository: matrix-org/synapse

Issue:
`/admin/whois` broken in v1.44.0rc1
### Description

`/_matrix/client/r0/admin/whois/{user_id}` is broken and returns `{"errcode":"M_UNKNOWN","error":"Internal server error"}`

https://sentry.matrix.org/sentry/synapse-matrixorg/issues/231740/

```
Traceback (most recent call last):
  File "/home/squah/repos/synapse/synapse/http/server.py", line 258, in _async_render_wrapper
    callback_return = await self._async_render(request)
  File "/home/squah/repos/synapse/synapse/http/server.py", line 446, in _async_render
    callback_return = await raw_callback_return
  File "/home/squah/repos/synapse/synapse/rest/admin/users.py", line 565, in on_GET
    ret = await self.admin_handler.get_whois(target_user)
  File "/home/squah/repos/synapse/synapse/handlers/admin.py", line 42, in get_whois
    sessions = await self.store.get_user_ip_and_agents(user)
  File "/home/squah/repos/synapse/synapse/storage/databases/main/client_ips.py", line 593, in get_user_ip_and_agents
    )
  File "/home/squah/repos/synapse/synapse/storage/databases/main/client_ips.py", line 594, in <genexpr>
TypeError: tuple indices must be integers or slices, not str
```

Note that the sytest test for this endpoint passes because during the test, `get_user_ip_and_agents` obtains the user data from `_batch_row_update` in-memory and no user data has been persisted to the database yet.

### Steps to reproduce

```sh
curl 'https://matrix.org/_matrix/client/r0/admin/whois/@squah:matrix.org' -H 'accept: application/json' -H 'authorization: Bearer syt_<... borrow this from Element Web>'
```
replacing matrix.org with your own homeserver, the user id with your own, and the token with your own.

### Version information

- **Version**: v1.44.0rc1



Discussion:

Resolving patch:
diff --git a/synapse/storage/databases/main/client_ips.py b/synapse/storage/databases/main/client_ips.py
--- a/synapse/storage/databases/main/client_ips.py
+++ b/synapse/storage/databases/main/client_ips.py
@@ -591,8 +591,8 @@ def get_recent(txn):
         )
 
         results.update(
-            ((row["access_token"], row["ip"]), (row["user_agent"], row["last_seen"]))
-            for row in rows
+            ((access_token, ip), (user_agent, last_seen))
+            for access_token, ip, user_agent, last_seen in rows
         )
         return [
             {


Test patch:
diff --git a/tests/storage/test_client_ips.py b/tests/storage/test_client_ips.py
--- a/tests/storage/test_client_ips.py
+++ b/tests/storage/test_client_ips.py
@@ -15,9 +15,12 @@
 
 from unittest.mock import Mock
 
+from parameterized import parameterized
+
 import synapse.rest.admin
 from synapse.http.site import XForwardedForRequest
 from synapse.rest.client import login
+from synapse.types import UserID
 
 from tests import unittest
 from tests.server import make_request
@@ -143,6 +146,37 @@ def test_insert_new_client_ip_none_device_id(self):
             ],
         )
 
+    @parameterized.expand([(False,), (True,)])
+    def test_get_user_ip_and_agents(self, after_persisting: bool):
+        """Test `get_user_ip_and_agents` for persisted and unpersisted data"""
+        self.reactor.advance(12345678)
+
+        user_id = "@user:id"
+        user = UserID.from_string(user_id)
+
+        # Insert a user IP
+        self.get_success(
+            self.store.insert_client_ip(
+                user_id, "access_token", "ip", "user_agent", "MY_DEVICE"
+            )
+        )
+
+        if after_persisting:
+            # Trigger the storage loop
+            self.reactor.advance(10)
+
+        self.assertEqual(
+            self.get_success(self.store.get_user_ip_and_agents(user)),
+            [
+                {
+                    "access_token": "access_token",
+                    "ip": "ip",
+                    "user_agent": "user_agent",
+                    "last_seen": 12345678000,
+                },
+            ],
+        )
+
     @override_config({"limit_usage_by_mau": False, "max_mau_value": 50})
     def test_disabled_monthly_active_user(self):
         user_id = "@user:server"