Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Timo Ley
synapse
Commits
3a125625
Unverified
Commit
3a125625
authored
2 years ago
by
Andrew Morgan
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add some clarifying comments and refactor a portion of the `Keyring` class for readability (#14804)
parent
772e8c23
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
changelog.d/14804.misc
+1
-0
1 addition, 0 deletions
changelog.d/14804.misc
synapse/crypto/keyring.py
+43
-18
43 additions, 18 deletions
synapse/crypto/keyring.py
with
44 additions
and
18 deletions
changelog.d/14804.misc
0 → 100644
+
1
−
0
View file @
3a125625
Add some clarifying comments and refactor a portion of the `Keyring` class for readability.
\ No newline at end of file
This diff is collapsed.
Click to expand it.
synapse/crypto/keyring.py
+
43
−
18
View file @
3a125625
...
...
@@ -154,17 +154,21 @@ class Keyring:
if
key_fetchers
is
None
:
key_fetchers
=
(
# Fetch keys from the database.
StoreKeyFetcher
(
hs
),
# Fetch keys from a configured Perspectives server.
PerspectivesKeyFetcher
(
hs
),
# Fetch keys from the origin server directly.
ServerKeyFetcher
(
hs
),
)
self
.
_key_fetchers
=
key_fetchers
self
.
_
server
_queue
:
BatchingQueue
[
self
.
_
fetch_keys
_queue
:
BatchingQueue
[
_FetchKeyRequest
,
Dict
[
str
,
Dict
[
str
,
FetchKeyResult
]]
]
=
BatchingQueue
(
"
keyring_server
"
,
clock
=
hs
.
get_clock
(),
# The method called to fetch each key
process_batch_callback
=
self
.
_inner_fetch_key_requests
,
)
...
...
@@ -287,7 +291,7 @@ class Keyring:
minimum_valid_until_ts
=
verify_request
.
minimum_valid_until_ts
,
key_ids
=
list
(
key_ids_to_find
),
)
found_keys_by_server
=
await
self
.
_
server
_queue
.
add_to_queue
(
found_keys_by_server
=
await
self
.
_
fetch_keys
_queue
.
add_to_queue
(
key_request
,
key
=
verify_request
.
server_name
)
...
...
@@ -352,7 +356,17 @@ class Keyring:
async
def
_inner_fetch_key_requests
(
self
,
requests
:
List
[
_FetchKeyRequest
]
)
->
Dict
[
str
,
Dict
[
str
,
FetchKeyResult
]]:
"""
Processing function for the queue of `_FetchKeyRequest`.
"""
"""
Processing function for the queue of `_FetchKeyRequest`.
Takes a list of key fetch requests, de-duplicates them and then carries out
each request by invoking self._inner_fetch_key_request.
Args:
requests: A list of requests for homeserver verify keys.
Returns:
{server name: {key id: fetch key result}}
"""
logger
.
debug
(
"
Starting fetch for %s
"
,
requests
)
...
...
@@ -397,8 +411,23 @@ class Keyring:
async
def
_inner_fetch_key_request
(
self
,
verify_request
:
_FetchKeyRequest
)
->
Dict
[
str
,
FetchKeyResult
]:
"""
Attempt to fetch the given key by calling each key fetcher one by
one.
"""
Attempt to fetch the given key by calling each key fetcher one by one.
If a key is found, check whether its `valid_until_ts` attribute satisfies the
`minimum_valid_until_ts` attribute of the `verify_request`. If it does, we
refrain from asking subsequent fetchers for that key.
Even if the above check fails, we still return the found key - the caller may
still find the invalid key result useful. In this case, we continue to ask
subsequent fetchers for the invalid key, in case they return a valid result
for it. This can happen when fetching a stale key result from the database,
before querying the origin server for an up-to-date result.
Args:
verify_request: The request for a verify key. Can include multiple key IDs.
Returns:
A map of {key_id: the key fetch result}.
"""
logger
.
debug
(
"
Starting fetch for %s
"
,
verify_request
)
...
...
@@ -420,26 +449,22 @@ class Keyring:
if
not
key
:
continue
# If we already have a result for the given key ID we keep the
# If we already have a result for the given key ID
,
we keep the
# one with the highest `valid_until_ts`.
existing_key
=
found_keys
.
get
(
key_id
)
if
existing_key
:
if
key
.
valid_until_ts
<=
existing_key
.
valid_until_ts
:
continue
if
existing_key
and
existing_key
.
valid_until_ts
>
key
.
valid_until_ts
:
continue
# Check if this key's expiry timestamp is valid for the verify request.
if
key
.
valid_until_ts
>=
verify_request
.
minimum_valid_until_ts
:
# Stop looking for this key from subsequent fetchers.
missing_key_ids
.
discard
(
key_id
)
# We always store the returned key even if it doesn't the
# We always store the returned key even if it doesn't
meet
the
# `minimum_valid_until_ts` requirement, as some verification
# requests may still be able to be satisfied by it.
#
# We still keep looking for the key from other fetchers in that
# case though.
found_keys
[
key_id
]
=
key
if
key
.
valid_until_ts
<
verify_request
.
minimum_valid_until_ts
:
continue
missing_key_ids
.
discard
(
key_id
)
return
found_keys
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment