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

Commit

Permalink
Use admin_patterns for all admin APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Sep 16, 2020
1 parent 44dec6c commit 63f1d6f
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 26 deletions.
1 change: 1 addition & 0 deletions changelog.d/8331.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use the `admin_patterns` helper in additional locations.
4 changes: 2 additions & 2 deletions synapse/rest/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

import logging
import platform
import re

import synapse
from synapse.api.errors import Codes, NotFoundError, SynapseError
from synapse.http.server import JsonResource
from synapse.http.servlet import RestServlet, parse_json_object_from_request
from synapse.rest.admin._base import (
admin_patterns,
assert_requester_is_admin,
historical_admin_path_patterns,
)
Expand Down Expand Up @@ -61,7 +61,7 @@


class VersionServlet(RestServlet):
PATTERNS = (re.compile("^/_synapse/admin/v1/server_version$"),)
PATTERNS = admin_patterns("/server_version$")

def __init__(self, hs):
self.res = {
Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/admin/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def historical_admin_path_patterns(path_regex):
]


def admin_patterns(path_regex: str):
def admin_patterns(path_regex: str, version: str = "v1"):
"""Returns the list of patterns for an admin endpoint
Args:
Expand All @@ -54,7 +54,7 @@ def admin_patterns(path_regex: str):
Returns:
A list of regex patterns.
"""
admin_prefix = "^/_synapse/admin/v1"
admin_prefix = "^/_synapse/admin/" + version
patterns = [re.compile(admin_prefix + path_regex)]
return patterns

Expand Down
15 changes: 5 additions & 10 deletions synapse/rest/admin/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import re

from synapse.api.errors import NotFoundError, SynapseError
from synapse.http.servlet import (
RestServlet,
assert_params_in_dict,
parse_json_object_from_request,
)
from synapse.rest.admin._base import assert_requester_is_admin
from synapse.rest.admin._base import admin_patterns, assert_requester_is_admin
from synapse.types import UserID

logger = logging.getLogger(__name__)
Expand All @@ -32,10 +31,8 @@ class DeviceRestServlet(RestServlet):
Get, update or delete the given user's device
"""

PATTERNS = (
re.compile(
"^/_synapse/admin/v2/users/(?P<user_id>[^/]*)/devices/(?P<device_id>[^/]*)$"
),
PATTERNS = admin_patterns(
"/users/(?P<user_id>[^/]*)/devices/(?P<device_id>[^/]*)$", "v2"
)

def __init__(self, hs):
Expand Down Expand Up @@ -98,7 +95,7 @@ class DevicesRestServlet(RestServlet):
Retrieve the given user's devices
"""

PATTERNS = (re.compile("^/_synapse/admin/v2/users/(?P<user_id>[^/]*)/devices$"),)
PATTERNS = admin_patterns("/users/(?P<user_id>[^/]*)/devices$", "v2")

def __init__(self, hs):
"""
Expand Down Expand Up @@ -131,9 +128,7 @@ class DeleteDevicesRestServlet(RestServlet):
key which lists the device_ids to delete.
"""

PATTERNS = (
re.compile("^/_synapse/admin/v2/users/(?P<user_id>[^/]*)/delete_devices$"),
)
PATTERNS = admin_patterns("/users/(?P<user_id>[^/]*)/delete_devices$", "v2")

def __init__(self, hs):
self.hs = hs
Expand Down
5 changes: 2 additions & 3 deletions synapse/rest/admin/purge_room_servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
# 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.
import re

from synapse.http.servlet import (
RestServlet,
assert_params_in_dict,
parse_json_object_from_request,
)
from synapse.rest.admin import assert_requester_is_admin
from synapse.rest.admin._base import admin_patterns


class PurgeRoomServlet(RestServlet):
Expand All @@ -35,7 +34,7 @@ class PurgeRoomServlet(RestServlet):
{}
"""

PATTERNS = (re.compile("^/_synapse/admin/v1/purge_room$"),)
PATTERNS = admin_patterns("/purge_room$")

def __init__(self, hs):
"""
Expand Down
9 changes: 4 additions & 5 deletions synapse/rest/admin/server_notice_servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# 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.
import re

from synapse.api.constants import EventTypes
from synapse.api.errors import SynapseError
from synapse.http.servlet import (
Expand All @@ -22,6 +20,7 @@
parse_json_object_from_request,
)
from synapse.rest.admin import assert_requester_is_admin
from synapse.rest.admin._base import admin_patterns
from synapse.rest.client.transactions import HttpTransactionCache
from synapse.types import UserID

Expand Down Expand Up @@ -56,13 +55,13 @@ def __init__(self, hs):
self.snm = hs.get_server_notices_manager()

def register(self, json_resource):
PATTERN = "^/_synapse/admin/v1/send_server_notice"
PATTERN = "/send_server_notice"
json_resource.register_paths(
"POST", (re.compile(PATTERN + "$"),), self.on_POST, self.__class__.__name__
"POST", admin_patterns(PATTERN + "$"), self.on_POST, self.__class__.__name__
)
json_resource.register_paths(
"PUT",
(re.compile(PATTERN + "/(?P<txn_id>[^/]*)$"),),
admin_patterns(PATTERN + "/(?P<txn_id>[^/]*)$"),
self.on_PUT,
self.__class__.__name__,
)
Expand Down
8 changes: 4 additions & 4 deletions synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import hashlib
import hmac
import logging
import re
from http import HTTPStatus

from synapse.api.constants import UserTypes
Expand All @@ -29,6 +28,7 @@
parse_string,
)
from synapse.rest.admin._base import (
admin_patterns,
assert_requester_is_admin,
assert_user_is_admin,
historical_admin_path_patterns,
Expand Down Expand Up @@ -60,7 +60,7 @@ async def on_GET(self, request, user_id):


class UsersRestServletV2(RestServlet):
PATTERNS = (re.compile("^/_synapse/admin/v2/users$"),)
PATTERNS = admin_patterns("/users$", "v2")

"""Get request to list all local users.
This needs user to have administrator access in Synapse.
Expand Down Expand Up @@ -105,7 +105,7 @@ async def on_GET(self, request):


class UserRestServletV2(RestServlet):
PATTERNS = (re.compile("^/_synapse/admin/v2/users/(?P<user_id>[^/]+)$"),)
PATTERNS = admin_patterns("/users/(?P<user_id>[^/]+)$", "v2")

"""Get request to list user details.
This needs user to have administrator access in Synapse.
Expand Down Expand Up @@ -642,7 +642,7 @@ class UserAdminServlet(RestServlet):
{}
"""

PATTERNS = (re.compile("^/_synapse/admin/v1/users/(?P<user_id>[^/]*)/admin$"),)
PATTERNS = admin_patterns("/users/(?P<user_id>[^/]*)/admin$")

def __init__(self, hs):
self.hs = hs
Expand Down

0 comments on commit 63f1d6f

Please sign in to comment.