Convert expected format for AS regex to include exclusivity.
Previously you just specified the regex as a string, now it expects a JSON object with a 'regex' key and an 'exclusive' boolean, as per spec.
This commit is contained in:
parent
a025055643
commit
16b90764ad
|
@ -46,19 +46,31 @@ class ApplicationService(object):
|
||||||
def _check_namespaces(self, namespaces):
|
def _check_namespaces(self, namespaces):
|
||||||
# Sanity check that it is of the form:
|
# Sanity check that it is of the form:
|
||||||
# {
|
# {
|
||||||
# users: ["regex",...],
|
# users: [ {regex: "[A-z]+.*", exclusive: true}, ...],
|
||||||
# aliases: ["regex",...],
|
# aliases: [ {regex: "[A-z]+.*", exclusive: true}, ...],
|
||||||
# rooms: ["regex",...],
|
# rooms: [ {regex: "[A-z]+.*", exclusive: true}, ...],
|
||||||
# }
|
# }
|
||||||
if not namespaces:
|
if not namespaces:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
for ns in ApplicationService.NS_LIST:
|
for ns in ApplicationService.NS_LIST:
|
||||||
|
if ns not in namespaces:
|
||||||
|
namespaces[ns] = []
|
||||||
|
continue
|
||||||
|
|
||||||
if type(namespaces[ns]) != list:
|
if type(namespaces[ns]) != list:
|
||||||
raise ValueError("Bad namespace value for '%s'", ns)
|
raise ValueError("Bad namespace value for '%s'" % ns)
|
||||||
for regex in namespaces[ns]:
|
for regex_obj in namespaces[ns]:
|
||||||
if not isinstance(regex, basestring):
|
if not isinstance(regex_obj, dict):
|
||||||
raise ValueError("Expected string regex for ns '%s'", ns)
|
raise ValueError("Expected dict regex for ns '%s'" % ns)
|
||||||
|
if not isinstance(regex_obj.get("exclusive"), bool):
|
||||||
|
raise ValueError(
|
||||||
|
"Expected bool for 'exclusive' in ns '%s'" % ns
|
||||||
|
)
|
||||||
|
if not isinstance(regex_obj.get("regex"), basestring):
|
||||||
|
raise ValueError(
|
||||||
|
"Expected string for 'regex' in ns '%s'" % ns
|
||||||
|
)
|
||||||
return namespaces
|
return namespaces
|
||||||
|
|
||||||
def _matches_regex(self, test_string, namespace_key):
|
def _matches_regex(self, test_string, namespace_key):
|
||||||
|
|
|
@ -48,18 +48,12 @@ class RegisterRestServlet(AppServiceRestServlet):
|
||||||
400, "Missed required keys: as_token(str) / url(str)."
|
400, "Missed required keys: as_token(str) / url(str)."
|
||||||
)
|
)
|
||||||
|
|
||||||
namespaces = {
|
try:
|
||||||
"users": [],
|
app_service = ApplicationService(
|
||||||
"rooms": [],
|
as_token, as_url, params["namespaces"]
|
||||||
"aliases": []
|
)
|
||||||
}
|
except ValueError as e:
|
||||||
|
raise SynapseError(400, e.message)
|
||||||
if "namespaces" in params:
|
|
||||||
self._parse_namespace(namespaces, params["namespaces"], "users")
|
|
||||||
self._parse_namespace(namespaces, params["namespaces"], "rooms")
|
|
||||||
self._parse_namespace(namespaces, params["namespaces"], "aliases")
|
|
||||||
|
|
||||||
app_service = ApplicationService(as_token, as_url, namespaces)
|
|
||||||
|
|
||||||
app_service = yield self.handler.register(app_service)
|
app_service = yield self.handler.register(app_service)
|
||||||
hs_token = app_service.hs_token
|
hs_token = app_service.hs_token
|
||||||
|
@ -68,23 +62,6 @@ class RegisterRestServlet(AppServiceRestServlet):
|
||||||
"hs_token": hs_token
|
"hs_token": hs_token
|
||||||
}))
|
}))
|
||||||
|
|
||||||
def _parse_namespace(self, target_ns, origin_ns, ns):
|
|
||||||
if ns not in target_ns or ns not in origin_ns:
|
|
||||||
return # nothing to parse / map through to.
|
|
||||||
|
|
||||||
possible_regex_list = origin_ns[ns]
|
|
||||||
if not type(possible_regex_list) == list:
|
|
||||||
raise SynapseError(400, "Namespace %s isn't an array." % ns)
|
|
||||||
|
|
||||||
for regex in possible_regex_list:
|
|
||||||
if not isinstance(regex, basestring):
|
|
||||||
raise SynapseError(
|
|
||||||
400, "Regex '%s' isn't a string in namespace %s" %
|
|
||||||
(regex, ns)
|
|
||||||
)
|
|
||||||
|
|
||||||
target_ns[ns] = origin_ns[ns]
|
|
||||||
|
|
||||||
|
|
||||||
class UnregisterRestServlet(AppServiceRestServlet):
|
class UnregisterRestServlet(AppServiceRestServlet):
|
||||||
"""Handles AS registration with the home server.
|
"""Handles AS registration with the home server.
|
||||||
|
|
|
@ -13,6 +13,8 @@
|
||||||
# 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 simplejson
|
||||||
|
from simplejson import JSONDecodeError
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
|
||||||
from synapse.api.errors import StoreError
|
from synapse.api.errors import StoreError
|
||||||
|
@ -23,12 +25,18 @@ from ._base import SQLBaseStore
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def log_failure(failure):
|
||||||
|
logger.error("Failed to detect application services: %s", failure.value)
|
||||||
|
logger.error(failure.getTraceback())
|
||||||
|
|
||||||
|
|
||||||
class ApplicationServiceStore(SQLBaseStore):
|
class ApplicationServiceStore(SQLBaseStore):
|
||||||
|
|
||||||
def __init__(self, hs):
|
def __init__(self, hs):
|
||||||
super(ApplicationServiceStore, self).__init__(hs)
|
super(ApplicationServiceStore, self).__init__(hs)
|
||||||
self.services_cache = []
|
self.services_cache = []
|
||||||
self.cache_defer = self._populate_cache()
|
self.cache_defer = self._populate_cache()
|
||||||
|
self.cache_defer.addErrback(log_failure)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def unregister_app_service(self, token):
|
def unregister_app_service(self, token):
|
||||||
|
@ -128,11 +136,11 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||||
)
|
)
|
||||||
for (ns_int, ns_str) in enumerate(ApplicationService.NS_LIST):
|
for (ns_int, ns_str) in enumerate(ApplicationService.NS_LIST):
|
||||||
if ns_str in service.namespaces:
|
if ns_str in service.namespaces:
|
||||||
for regex in service.namespaces[ns_str]:
|
for regex_obj in service.namespaces[ns_str]:
|
||||||
txn.execute(
|
txn.execute(
|
||||||
"INSERT INTO application_services_regex("
|
"INSERT INTO application_services_regex("
|
||||||
"as_id, namespace, regex) values(?,?,?)",
|
"as_id, namespace, regex) values(?,?,?)",
|
||||||
(as_id, ns_int, regex)
|
(as_id, ns_int, simplejson.dumps(regex_obj))
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -215,10 +223,12 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||||
try:
|
try:
|
||||||
services[as_token]["namespaces"][
|
services[as_token]["namespaces"][
|
||||||
ApplicationService.NS_LIST[ns_int]].append(
|
ApplicationService.NS_LIST[ns_int]].append(
|
||||||
res["regex"]
|
simplejson.loads(res["regex"])
|
||||||
)
|
)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
logger.error("Bad namespace enum '%s'. %s", ns_int, res)
|
logger.error("Bad namespace enum '%s'. %s", ns_int, res)
|
||||||
|
except JSONDecodeError:
|
||||||
|
logger.error("Bad regex object '%s'", res["regex"])
|
||||||
|
|
||||||
# TODO get last successful txn id f.e. service
|
# TODO get last successful txn id f.e. service
|
||||||
for service in services.values():
|
for service in services.values():
|
||||||
|
|
Loading…
Reference in New Issue