Use admin_patterns for all admin APIs. (#8331)

This reduces duplication of the admin prefix in regular expressions.
This commit is contained in:
Patrick Cloke 2020-09-17 07:04:15 -04:00 committed by GitHub
parent 53284c425e
commit c3c9732c53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 26 deletions

1
changelog.d/8331.misc Normal file
View File

@ -0,0 +1 @@
Use the `admin_patterns` helper in additional locations.

View File

@ -16,13 +16,13 @@
import logging import logging
import platform import platform
import re
import synapse import synapse
from synapse.api.errors import Codes, NotFoundError, SynapseError from synapse.api.errors import Codes, NotFoundError, SynapseError
from synapse.http.server import JsonResource from synapse.http.server import JsonResource
from synapse.http.servlet import RestServlet, parse_json_object_from_request from synapse.http.servlet import RestServlet, parse_json_object_from_request
from synapse.rest.admin._base import ( from synapse.rest.admin._base import (
admin_patterns,
assert_requester_is_admin, assert_requester_is_admin,
historical_admin_path_patterns, historical_admin_path_patterns,
) )
@ -61,7 +61,7 @@ logger = logging.getLogger(__name__)
class VersionServlet(RestServlet): class VersionServlet(RestServlet):
PATTERNS = (re.compile("^/_synapse/admin/v1/server_version$"),) PATTERNS = admin_patterns("/server_version$")
def __init__(self, hs): def __init__(self, hs):
self.res = { self.res = {

View File

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

View File

@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging import logging
import re
from synapse.api.errors import NotFoundError, SynapseError from synapse.api.errors import NotFoundError, SynapseError
from synapse.http.servlet import ( from synapse.http.servlet import (
@ -21,7 +20,7 @@ from synapse.http.servlet import (
assert_params_in_dict, assert_params_in_dict,
parse_json_object_from_request, 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 from synapse.types import UserID
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -32,10 +31,8 @@ class DeviceRestServlet(RestServlet):
Get, update or delete the given user's device Get, update or delete the given user's device
""" """
PATTERNS = ( PATTERNS = admin_patterns(
re.compile( "/users/(?P<user_id>[^/]*)/devices/(?P<device_id>[^/]*)$", "v2"
"^/_synapse/admin/v2/users/(?P<user_id>[^/]*)/devices/(?P<device_id>[^/]*)$"
),
) )
def __init__(self, hs): def __init__(self, hs):
@ -98,7 +95,7 @@ class DevicesRestServlet(RestServlet):
Retrieve the given user's devices 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): def __init__(self, hs):
""" """
@ -131,9 +128,7 @@ class DeleteDevicesRestServlet(RestServlet):
key which lists the device_ids to delete. key which lists the device_ids to delete.
""" """
PATTERNS = ( PATTERNS = admin_patterns("/users/(?P<user_id>[^/]*)/delete_devices$", "v2")
re.compile("^/_synapse/admin/v2/users/(?P<user_id>[^/]*)/delete_devices$"),
)
def __init__(self, hs): def __init__(self, hs):
self.hs = hs self.hs = hs

View File

@ -12,14 +12,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import re
from synapse.http.servlet import ( from synapse.http.servlet import (
RestServlet, RestServlet,
assert_params_in_dict, assert_params_in_dict,
parse_json_object_from_request, parse_json_object_from_request,
) )
from synapse.rest.admin import assert_requester_is_admin from synapse.rest.admin import assert_requester_is_admin
from synapse.rest.admin._base import admin_patterns
class PurgeRoomServlet(RestServlet): class PurgeRoomServlet(RestServlet):
@ -35,7 +34,7 @@ class PurgeRoomServlet(RestServlet):
{} {}
""" """
PATTERNS = (re.compile("^/_synapse/admin/v1/purge_room$"),) PATTERNS = admin_patterns("/purge_room$")
def __init__(self, hs): def __init__(self, hs):
""" """

View File

@ -12,8 +12,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import re
from synapse.api.constants import EventTypes from synapse.api.constants import EventTypes
from synapse.api.errors import SynapseError from synapse.api.errors import SynapseError
from synapse.http.servlet import ( from synapse.http.servlet import (
@ -22,6 +20,7 @@ from synapse.http.servlet import (
parse_json_object_from_request, parse_json_object_from_request,
) )
from synapse.rest.admin import assert_requester_is_admin 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.rest.client.transactions import HttpTransactionCache
from synapse.types import UserID from synapse.types import UserID
@ -56,13 +55,13 @@ class SendServerNoticeServlet(RestServlet):
self.snm = hs.get_server_notices_manager() self.snm = hs.get_server_notices_manager()
def register(self, json_resource): def register(self, json_resource):
PATTERN = "^/_synapse/admin/v1/send_server_notice" PATTERN = "/send_server_notice"
json_resource.register_paths( 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( json_resource.register_paths(
"PUT", "PUT",
(re.compile(PATTERN + "/(?P<txn_id>[^/]*)$"),), admin_patterns(PATTERN + "/(?P<txn_id>[^/]*)$"),
self.on_PUT, self.on_PUT,
self.__class__.__name__, self.__class__.__name__,
) )

View File

@ -15,7 +15,6 @@
import hashlib import hashlib
import hmac import hmac
import logging import logging
import re
from http import HTTPStatus from http import HTTPStatus
from synapse.api.constants import UserTypes from synapse.api.constants import UserTypes
@ -29,6 +28,7 @@ from synapse.http.servlet import (
parse_string, parse_string,
) )
from synapse.rest.admin._base import ( from synapse.rest.admin._base import (
admin_patterns,
assert_requester_is_admin, assert_requester_is_admin,
assert_user_is_admin, assert_user_is_admin,
historical_admin_path_patterns, historical_admin_path_patterns,
@ -60,7 +60,7 @@ class UsersRestServlet(RestServlet):
class UsersRestServletV2(RestServlet): class UsersRestServletV2(RestServlet):
PATTERNS = (re.compile("^/_synapse/admin/v2/users$"),) PATTERNS = admin_patterns("/users$", "v2")
"""Get request to list all local users. """Get request to list all local users.
This needs user to have administrator access in Synapse. This needs user to have administrator access in Synapse.
@ -105,7 +105,7 @@ class UsersRestServletV2(RestServlet):
class UserRestServletV2(RestServlet): 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. """Get request to list user details.
This needs user to have administrator access in Synapse. This needs user to have administrator access in Synapse.
@ -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): def __init__(self, hs):
self.hs = hs self.hs = hs