Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit f25af1f

Browse files
authored
Add cross-signing sigs to the keys object (#8234)
All the callers want this info in the same place, so let's reduce the duplication by doing it here.
1 parent 041ee97 commit f25af1f

File tree

3 files changed

+18
-34
lines changed

3 files changed

+18
-34
lines changed

changelog.d/8234.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor queries for device keys and cross-signatures.

synapse/storage/databases/main/devices.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,9 @@ async def _get_device_update_edus_by_remote(
291291
prev_id = stream_id
292292

293293
if device is not None:
294-
key_json = device.key_json
295-
if key_json:
296-
result["keys"] = db_to_json(key_json)
297-
298-
if device.signatures:
299-
for sig_user_id, sigs in device.signatures.items():
300-
result["keys"].setdefault("signatures", {}).setdefault(
301-
sig_user_id, {}
302-
).update(sigs)
294+
keys = device.keys
295+
if keys:
296+
result["keys"] = keys
303297

304298
device_display_name = device.display_name
305299
if device_display_name:

synapse/storage/databases/main/end_to_end_keys.py

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,8 @@ class DeviceKeyLookupResult:
4343

4444
# the key data from e2e_device_keys_json. Typically includes fields like
4545
# "algorithm", "keys" (including the curve25519 identity key and the ed25519 signing
46-
# key) and "signatures" (a signature of the structure by the ed25519 key)
47-
key_json = attr.ib(type=Optional[str])
48-
49-
# cross-signing sigs on this device.
50-
# dict from (signing user_id)->(signing device_id)->sig
51-
signatures = attr.ib(type=Optional[Dict[str, Dict[str, str]]], factory=dict)
46+
# key) and "signatures" (a map from (user id) to (key id/device_id) to signature.)
47+
keys = attr.ib(type=Optional[JsonDict])
5248

5349

5450
class EndToEndKeyWorkerStore(SQLBaseStore):
@@ -70,15 +66,9 @@ async def get_e2e_device_keys_for_federation_query(
7066
for device_id, device in user_devices.items():
7167
result = {"device_id": device_id}
7268

73-
key_json = device.key_json
74-
if key_json:
75-
result["keys"] = db_to_json(key_json)
76-
77-
if device.signatures:
78-
for sig_user_id, sigs in device.signatures.items():
79-
result["keys"].setdefault("signatures", {}).setdefault(
80-
sig_user_id, {}
81-
).update(sigs)
69+
keys = device.keys
70+
if keys:
71+
result["keys"] = keys
8272

8373
device_display_name = device.display_name
8474
if device_display_name:
@@ -114,16 +104,11 @@ async def get_e2e_device_keys_for_cs_api(
114104
for user_id, device_keys in results.items():
115105
rv[user_id] = {}
116106
for device_id, device_info in device_keys.items():
117-
r = db_to_json(device_info.key_json)
107+
r = device_info.keys
118108
r["unsigned"] = {}
119109
display_name = device_info.display_name
120110
if display_name is not None:
121111
r["unsigned"]["device_display_name"] = display_name
122-
if device_info.signatures:
123-
for sig_user_id, sigs in device_info.signatures.items():
124-
r.setdefault("signatures", {}).setdefault(
125-
sig_user_id, {}
126-
).update(sigs)
127112
rv[user_id][device_id] = r
128113

129114
return rv
@@ -140,6 +125,9 @@ async def get_e2e_device_keys_and_signatures(
140125
Any cross-signatures made on the keys by the owner of the device are also
141126
included.
142127
128+
The cross-signatures are added to the `signatures` field within the `keys`
129+
object in the response.
130+
143131
Args:
144132
query_list: List of pairs of user_ids and device_ids. Device id can be None
145133
to indicate "all devices for this user"
@@ -170,7 +158,7 @@ async def get_e2e_device_keys_and_signatures(
170158
(user_id, device_id)
171159
for user_id, dev in result.items()
172160
for device_id, d in dev.items()
173-
if d is not None
161+
if d is not None and d.keys is not None
174162
)
175163

176164
for batch in batch_iter(signature_query, 50):
@@ -183,8 +171,9 @@ async def get_e2e_device_keys_and_signatures(
183171
# add each cross-signing signature to the correct device in the result dict.
184172
for (user_id, key_id, device_id, signature) in cross_sigs_result:
185173
target_device_result = result[user_id][device_id]
186-
target_device_signatures = target_device_result.signatures
187-
174+
target_device_signatures = target_device_result.keys.setdefault(
175+
"signatures", {}
176+
)
188177
signing_user_signatures = target_device_signatures.setdefault(
189178
user_id, {}
190179
)
@@ -240,7 +229,7 @@ def _get_e2e_device_keys_txn(
240229
if include_deleted_devices:
241230
deleted_devices.remove((user_id, device_id))
242231
result.setdefault(user_id, {})[device_id] = DeviceKeyLookupResult(
243-
display_name, key_json
232+
display_name, db_to_json(key_json) if key_json else None
244233
)
245234

246235
if include_deleted_devices:

0 commit comments

Comments
 (0)