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

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into rav/server_keys/05…
Browse files Browse the repository at this point in the history
…-rewrite-gsvk-again
  • Loading branch information
richvdh committed May 31, 2019
2 parents a82c96b + fe79b5e commit c605da9
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 129 deletions.
1 change: 1 addition & 0 deletions changelog.d/5293.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where it is not possible to get events in the federation format with the request `GET /_matrix/client/r0/rooms/{roomId}/messages`.
1 change: 1 addition & 0 deletions changelog.d/5294.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix performance problems with the rooms stats background update.
1 change: 1 addition & 0 deletions changelog.d/5296.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor keyring.VerifyKeyRequest to use attr.s.
1 change: 1 addition & 0 deletions changelog.d/5300.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix noisy 'no key for server' logs.
1 change: 1 addition & 0 deletions changelog.d/5303.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clarify that the admin change password API logs the user out.
2 changes: 1 addition & 1 deletion docs/admin_api/user_admin_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ An empty body may be passed for backwards compatibility.
Reset password
==============

Changes the password of another user.
Changes the password of another user. This will automatically log the user out of all their devices.

The api is::

Expand Down
69 changes: 28 additions & 41 deletions synapse/crypto/keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
# limitations under the License.

import logging
from collections import namedtuple

import six
from six import raise_from
from six.moves import urllib

import attr
from signedjson.key import (
decode_verify_key_bytes,
encode_verify_key_base64,
Expand Down Expand Up @@ -57,22 +57,32 @@
logger = logging.getLogger(__name__)


VerifyKeyRequest = namedtuple(
"VerifyRequest", ("server_name", "key_ids", "json_object", "deferred")
)
"""
A request for a verify key to verify a JSON object.
@attr.s(slots=True, cmp=False)
class VerifyKeyRequest(object):
"""
A request for a verify key to verify a JSON object.
Attributes:
server_name(str): The name of the server to verify against.
Attributes:
server_name(str): The name of the server to verify against.
key_ids(set(str)): The set of key_ids to that could be used to verify the
JSON object
json_object(dict): The JSON object to verify.
deferred(Deferred[str, str, nacl.signing.VerifyKey]):
A deferred (server_name, key_id, verify_key) tuple that resolves when
a verify key has been fetched. The deferreds' callbacks are run with no
logcontext.
"""
key_ids(set[str]): The set of key_ids to that could be used to verify the
JSON object
json_object(dict): The JSON object to verify.
deferred(Deferred[str, str, nacl.signing.VerifyKey]):
A deferred (server_name, key_id, verify_key) tuple that resolves when
a verify key has been fetched. The deferreds' callbacks are run with no
logcontext.
If we are unable to find a key which satisfies the request, the deferred
errbacks with an M_UNAUTHORIZED SynapseError.
"""

server_name = attr.ib()
key_ids = attr.ib()
json_object = attr.ib()
deferred = attr.ib()


class KeyLookupError(ValueError):
Expand Down Expand Up @@ -772,31 +782,8 @@ def _handle_key_deferred(verify_request):
SynapseError if there was a problem performing the verification
"""
server_name = verify_request.server_name
try:
with PreserveLoggingContext():
_, key_id, verify_key = yield verify_request.deferred
except KeyLookupError as e:
logger.warn(
"Failed to download keys for %s: %s %s",
server_name,
type(e).__name__,
str(e),
)
raise SynapseError(
502, "Error downloading keys for %s" % (server_name,), Codes.UNAUTHORIZED
)
except Exception as e:
logger.exception(
"Got Exception when downloading keys for %s: %s %s",
server_name,
type(e).__name__,
str(e),
)
raise SynapseError(
401,
"No key for %s with id %s" % (server_name, verify_request.key_ids),
Codes.UNAUTHORIZED,
)
with PreserveLoggingContext():
_, key_id, verify_key = yield verify_request.deferred

json_object = verify_request.json_object

Expand Down
2 changes: 2 additions & 0 deletions synapse/rest/client/v1/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ def on_GET(self, request, room_id):
if filter_bytes:
filter_json = urlparse.unquote(filter_bytes.decode("UTF-8"))
event_filter = Filter(json.loads(filter_json))
if event_filter.filter_json.get("event_format", "client") == "federation":
as_client_event = False
else:
event_filter = None
msgs = yield self.pagination_handler.get_messages(
Expand Down
7 changes: 6 additions & 1 deletion synapse/storage/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,12 @@ def _get_total_state_event_counts_txn(self, txn, room_id):
"""
See get_total_state_event_counts.
"""
sql = "SELECT COUNT(*) FROM state_events WHERE room_id=?"
# We join against the events table as that has an index on room_id
sql = """
SELECT COUNT(*) FROM state_events
INNER JOIN events USING (room_id, event_id)
WHERE room_id=?
"""
txn.execute(sql, (room_id,))
row = txn.fetchone()
return row[0] if row else 0
Expand Down
33 changes: 11 additions & 22 deletions synapse/storage/roommember.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,26 +142,9 @@ def _get_room_summary_txn(txn):

return self.runInteraction("get_room_summary", _get_room_summary_txn)

def _get_user_count_in_room_txn(self, txn, room_id, membership):
def _get_user_counts_in_room_txn(self, txn, room_id):
"""
See get_user_count_in_room.
"""
sql = (
"SELECT count(*) FROM room_memberships as m"
" INNER JOIN current_state_events as c"
" ON m.event_id = c.event_id "
" AND m.room_id = c.room_id "
" AND m.user_id = c.state_key"
" WHERE c.type = 'm.room.member' AND c.room_id = ? AND m.membership = ?"
)

txn.execute(sql, (room_id, membership))
row = txn.fetchone()
return row[0]

def get_user_count_in_room(self, room_id, membership):
"""
Get the user count in a room with a particular membership.
Get the user count in a room by membership.
Args:
room_id (str)
Expand All @@ -170,9 +153,15 @@ def get_user_count_in_room(self, room_id, membership):
Returns:
Deferred[int]
"""
return self.runInteraction(
"get_users_in_room", self._get_user_count_in_room_txn, room_id, membership
)
sql = """
SELECT m.membership, count(*) FROM room_memberships as m
INNER JOIN current_state_events as c USING(event_id)
WHERE c.type = 'm.room.member' AND c.room_id = ?
GROUP BY m.membership
"""

txn.execute(sql, (room_id,))
return {row[0]: row[1] for row in txn}

@cached()
def get_invited_rooms_for_user(self, user_id):
Expand Down
28 changes: 28 additions & 0 deletions synapse/storage/schema/delta/54/stats2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright 2019 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

-- This delta file gets run after `54/stats.sql` delta.

-- We want to add some indices to the temporary stats table, so we re-insert
-- 'populate_stats_createtables' if we are still processing the rooms update.
INSERT INTO background_updates (update_name, progress_json)
SELECT 'populate_stats_createtables', '{}'
WHERE
'populate_stats_process_rooms' IN (
SELECT update_name FROM background_updates
)
AND 'populate_stats_createtables' NOT IN ( -- don't insert if already exists
SELECT update_name FROM background_updates
);
Loading

0 comments on commit c605da9

Please sign in to comment.