From 7b523d9450a3e7fef252458d4fbe0fe9e0f4928c Mon Sep 17 00:00:00 2001 From: Czar Echavez Date: Tue, 30 Jul 2024 11:33:52 +0100 Subject: [PATCH] #2769: added changes which should align with 2735 once merged --- src/primaite/game/agent/actions.py | 12 +++++----- .../simulator/network/hardware/base.py | 12 ---------- .../test_remote_user_account_actions.py | 23 ++++++++++++++----- .../test_user_account_change_password.py | 14 +++++++++-- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/primaite/game/agent/actions.py b/src/primaite/game/agent/actions.py index 266c667b..19442818 100644 --- a/src/primaite/game/agent/actions.py +++ b/src/primaite/game/agent/actions.py @@ -1077,9 +1077,9 @@ class NodeAccountsChangePasswordAction(AbstractAction): def __init__(self, manager: "ActionManager", **kwargs) -> None: super().__init__(manager=manager) - def form_request(self, node_id: str) -> RequestFormat: + def form_request(self, node_id: str, username: str, current_password: str, new_password: str) -> RequestFormat: """Return the action formatted as a request which can be ingested by the PrimAITE simulation.""" - return ["network", "node", node_id, "change_password"] + return ["network", "node", node_id, "accounts", "change_password", username, current_password, new_password] class NodeSessionsRemoteLoginAction(AbstractAction): @@ -1088,9 +1088,9 @@ class NodeSessionsRemoteLoginAction(AbstractAction): def __init__(self, manager: "ActionManager", **kwargs) -> None: super().__init__(manager=manager) - def form_request(self, node_id: str) -> RequestFormat: + def form_request(self, node_id: str, username: str, password: str) -> RequestFormat: """Return the action formatted as a request which can be ingested by the PrimAITE simulation.""" - return ["network", "node", node_id, "remote_logon"] + return ["network", "node", node_id, "sessions", "remote_login", username, password] class NodeSessionsRemoteLogoutAction(AbstractAction): @@ -1099,9 +1099,9 @@ class NodeSessionsRemoteLogoutAction(AbstractAction): def __init__(self, manager: "ActionManager", **kwargs) -> None: super().__init__(manager=manager) - def form_request(self, node_id: str) -> RequestFormat: + def form_request(self, node_id: str, remote_session_id: str) -> RequestFormat: """Return the action formatted as a request which can be ingested by the PrimAITE simulation.""" - return ["network", "node", node_id, "remote_logoff"] + return ["network", "node", node_id, "sessions", "remote_logout", remote_session_id] class ActionManager: diff --git a/src/primaite/simulator/network/hardware/base.py b/src/primaite/simulator/network/hardware/base.py index 3ef33ac3..15c44821 100644 --- a/src/primaite/simulator/network/hardware/base.py +++ b/src/primaite/simulator/network/hardware/base.py @@ -1071,18 +1071,6 @@ class Node(SimComponent): rm.add_request( "logoff", RequestType(func=lambda request, context: RequestResponse.from_bool(False), validator=_node_is_on) ) # TODO implement logoff request - rm.add_request( - "change_password", - RequestType(func=lambda request, context: RequestResponse.from_bool(False), validator=_node_is_on), - ) # TODO implement change_password request - rm.add_request( - "remote_logon", - RequestType(func=lambda request, context: RequestResponse.from_bool(False), validator=_node_is_on), - ) # TODO implement remote_logon request - rm.add_request( - "remote_logoff", - RequestType(func=lambda request, context: RequestResponse.from_bool(False), validator=_node_is_on), - ) # TODO implement remote_logoff request self._os_request_manager = RequestManager() self._os_request_manager.add_request( diff --git a/tests/integration_tests/game_layer/actions/user_account_actions/test_remote_user_account_actions.py b/tests/integration_tests/game_layer/actions/user_account_actions/test_remote_user_account_actions.py index 807715bb..2e282d77 100644 --- a/tests/integration_tests/game_layer/actions/user_account_actions/test_remote_user_account_actions.py +++ b/tests/integration_tests/game_layer/actions/user_account_actions/test_remote_user_account_actions.py @@ -1,38 +1,49 @@ # © Crown-owned copyright 2024, Defence Science and Technology Laboratory UK +from primaite.simulator.network.hardware.nodes.host.computer import Computer def test_remote_logon(game_and_agent): """Test that the remote session login action works.""" game, agent = game_and_agent + client_1: Computer = game.simulation.network.get_node_by_hostname("client_1") + + client_1.user_manager.add_user(username="test_user", password="password", bypass_can_perform_action=True) + action = ( "NODE_SESSIONS_REMOTE_LOGIN", - {"node_id": 0}, + {"node_id": 0, "username": "test_user", "password": "password"}, ) agent.store_action(action) game.step() - # TODO Assert that there is a logged in user + assert len(client_1.user_session_manager.remote_sessions) == 1 def test_remote_logoff(game_and_agent): """Test that the remote session logout action works.""" game, agent = game_and_agent + client_1: Computer = game.simulation.network.get_node_by_hostname("client_1") + + client_1.user_manager.add_user(username="test_user", password="password", bypass_can_perform_action=True) + action = ( "NODE_SESSIONS_REMOTE_LOGIN", - {"node_id": 0}, + {"node_id": 0, "username": "test_user", "password": "password"}, ) agent.store_action(action) game.step() - # TODO Assert that there is a logged in user + assert len(client_1.user_session_manager.remote_sessions) == 1 + + remote_session_id = client_1.user_session_manager.remote_sessions[0].uuid action = ( "NODE_SESSIONS_REMOTE_LOGOUT", - {"node_id": 0}, + {"node_id": 0, "remote_session_id": remote_session_id}, ) agent.store_action(action) game.step() - # TODO Assert the user has logged out + assert len(client_1.user_session_manager.remote_sessions) == 0 diff --git a/tests/integration_tests/game_layer/actions/user_account_actions/test_user_account_change_password.py b/tests/integration_tests/game_layer/actions/user_account_actions/test_user_account_change_password.py index 27328100..3e6f55f6 100644 --- a/tests/integration_tests/game_layer/actions/user_account_actions/test_user_account_change_password.py +++ b/tests/integration_tests/game_layer/actions/user_account_actions/test_user_account_change_password.py @@ -1,13 +1,23 @@ # © Crown-owned copyright 2024, Defence Science and Technology Laboratory UK +from primaite.simulator.network.hardware.nodes.host.computer import Computer + + def test_remote_logon(game_and_agent): """Test that the remote session login action works.""" game, agent = game_and_agent + client_1: Computer = game.simulation.network.get_node_by_hostname("client_1") + + client_1.user_manager.add_user(username="test_user", password="password", bypass_can_perform_action=True) + user = next((user for user in client_1.user_manager.users.values() if user.username == "test_user"), None) + + assert user.password == "password" + action = ( "NODE_ACCOUNTS_CHANGEPASSWORD", - {"node_id": 0}, + {"node_id": 0, "username": user.username, "current_password": user.password, "new_password": "test_pass"}, ) agent.store_action(action) game.step() - # TODO Assert that the user account password is changed + assert user.password == "test_pass"