mgmt: factor out filtering by mgmt-permission:... events

Split actual filtering done by mgmt-permission: events into calling an
event and applying returned filters. This way filtering done in
mgmt.Events handler could reuse the same function.
This commit is contained in:
Marek Marczykowski-Górecki 2017-05-10 14:29:19 +02:00
parent 6926725e02
commit cddc507788
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -81,6 +81,14 @@ def api(name, *, no_payload=False):
return decorator return decorator
def apply_filters(iterable, filters):
'''Apply filters returned by mgmt-permission:... event'''
for selector in filters:
iterable = filter(selector, iterable)
return iterable
class AbstractQubesMgmt(object): class AbstractQubesMgmt(object):
'''Common code for Qubes Management Protocol handling '''Common code for Qubes Management Protocol handling
@ -167,9 +175,8 @@ class AbstractQubesMgmt(object):
def fire_event_for_filter(self, iterable, **kwargs): def fire_event_for_filter(self, iterable, **kwargs):
'''Fire an event on the source qube to filter for permission''' '''Fire an event on the source qube to filter for permission'''
for selector in self.fire_event_for_permission(**kwargs): return apply_filters(iterable,
iterable = filter(selector, iterable) self.fire_event_for_permission(**kwargs))
return iterable
class QubesMgmt(AbstractQubesMgmt): class QubesMgmt(AbstractQubesMgmt):
@ -588,8 +595,8 @@ class QubesMgmt(AbstractQubesMgmt):
return return
if event.startswith('mgmt-permission:'): if event.startswith('mgmt-permission:'):
return return
for selector in event_filters: if not list(apply_filters([(subject, event, kwargs)],
if not selector((subject, event, kwargs)): event_filters)):
return return
self.send_event(subject, event, **kwargs) self.send_event(subject, event, **kwargs)