Skip to content

Commit e63d78f

Browse files
committed
Island: Move register_event_handlers to its own module
1 parent 9111df9 commit e63d78f

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from common import DIContainer
2+
from common.agent_events import AbstractAgentEvent, AgentShutdownEvent
3+
from common.event_queue import IAgentEventQueue
4+
from common.types import AgentID
5+
from monkey_island.cc.event_queue import IIslandEventQueue, IslandEventTopic
6+
7+
from .authentication_facade import AuthenticationFacade
8+
9+
10+
def register_event_handlers(container: DIContainer, authentication_facade: AuthenticationFacade):
11+
agent_event_queue = container.resolve(IAgentEventQueue)
12+
island_event_queue = container.resolve(IIslandEventQueue)
13+
14+
agent_event_queue.subscribe_type(
15+
AgentShutdownEvent, unregister_agent_on_shutdown(authentication_facade)
16+
)
17+
island_event_queue.subscribe(
18+
IslandEventTopic.AGENT_TIMED_OUT, unregister_agent_on_timeout(authentication_facade)
19+
)
20+
21+
22+
class unregister_agent_on_shutdown:
23+
def __init__(self, authentication_facade: AuthenticationFacade):
24+
self._authentication_facade = authentication_facade
25+
26+
def __call__(self, event: AbstractAgentEvent):
27+
agent_id = event.source
28+
self._authentication_facade.remove_user(str(agent_id))
29+
30+
31+
class unregister_agent_on_timeout:
32+
def __init__(self, authentication_facade: AuthenticationFacade):
33+
self._authentication_facade = authentication_facade
34+
35+
def __call__(self, agent_id: AgentID):
36+
self._authentication_facade.remove_user(str(agent_id))

monkey/monkey_island/cc/services/authentication_service/setup.py

+3-34
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
from flask_security import Security
66

77
from common import DIContainer
8-
from common.agent_events import AbstractAgentEvent, AgentShutdownEvent
9-
from common.event_queue import IAgentEventQueue
10-
from common.types import AgentID
11-
from monkey_island.cc.event_queue import IIslandEventQueue, IslandEventTopic
8+
from monkey_island.cc.event_queue import IIslandEventQueue
129
from monkey_island.cc.server_utils.encryption import ILockableEncryptor
1310

1411
from . import IOTPGenerator
@@ -17,6 +14,7 @@
1714
from .configure_flask_security import configure_flask_security
1815
from .flask_resources import register_resources
1916
from .mongo_otp_repository import MongoOTPRepository
17+
from .register_event_handlers import register_event_handlers
2018
from .token_generator import TokenGenerator
2119
from .token_parser import TokenParser
2220

@@ -28,7 +26,7 @@ def setup_authentication(api, app: Flask, container: DIContainer, data_dir: Path
2826
otp_generator = AuthenticationServiceOTPGenerator(authentication_facade)
2927
container.register_instance(IOTPGenerator, otp_generator)
3028

31-
_register_event_handlers(container, authentication_facade)
29+
register_event_handlers(container, authentication_facade)
3230
register_resources(api, authentication_facade, otp_generator, limiter)
3331

3432
# revoke all old tokens so that the user has to log in again on startup
@@ -54,32 +52,3 @@ def _build_authentication_facade(container: DIContainer, security: Security):
5452
token_parser,
5553
container.resolve(MongoOTPRepository),
5654
)
57-
58-
59-
def _register_event_handlers(container: DIContainer, authentication_facade: AuthenticationFacade):
60-
agent_event_queue = container.resolve(IAgentEventQueue)
61-
island_event_queue = container.resolve(IIslandEventQueue)
62-
63-
agent_event_queue.subscribe_type(
64-
AgentShutdownEvent, unregister_agent_on_shutdown(authentication_facade)
65-
)
66-
island_event_queue.subscribe(
67-
IslandEventTopic.AGENT_TIMED_OUT, unregister_agent_on_timeout(authentication_facade)
68-
)
69-
70-
71-
class unregister_agent_on_shutdown:
72-
def __init__(self, authentication_facade: AuthenticationFacade):
73-
self._authentication_facade = authentication_facade
74-
75-
def __call__(self, event: AbstractAgentEvent):
76-
agent_id = event.source
77-
self._authentication_facade.remove_user(str(agent_id))
78-
79-
80-
class unregister_agent_on_timeout:
81-
def __init__(self, authentication_facade: AuthenticationFacade):
82-
self._authentication_facade = authentication_facade
83-
84-
def __call__(self, agent_id: AgentID):
85-
self._authentication_facade.remove_user(str(agent_id))

0 commit comments

Comments
 (0)