From 768b00482dd755171d056f9c4c345479e128dfc7 Mon Sep 17 00:00:00 2001 From: Romke van Dijk Date: Mon, 23 Oct 2023 17:04:02 +0200 Subject: [PATCH 1/2] Updating maxAgent for Tasks now also updates TaskWrapper Fixes #1013 --- ci/tests/integration/MaxAgentsTest.class.php | 57 ++++++++++++++++++++ doc/changelog.md | 5 ++ src/inc/utils/TaskUtils.class.php | 8 ++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/ci/tests/integration/MaxAgentsTest.class.php b/ci/tests/integration/MaxAgentsTest.class.php index 8432d2ede..ef1db3253 100644 --- a/ci/tests/integration/MaxAgentsTest.class.php +++ b/ci/tests/integration/MaxAgentsTest.class.php @@ -44,6 +44,7 @@ public function run() { $hashlistId = $response["hashlistId"]; $this->testTaskMaxAgents($hashlistId); + $this->testTaskMaxAgents_bug_1013($hashlistId); $this->testSuperTaskMaxAgents($hashlistId); } finally { @@ -200,6 +201,62 @@ private function testTaskMaxAgents($hashlistId) { $this->testSuccess("MaxAgentsTest:testTaskMaxAgents()"); } + private function testTaskMaxAgents_bug_1013($hashlistId) { + $agent1 = $this->createAgent("agent-1013-1"); + $agent2 = $this->createAgent("agent-1013-2"); + + // disable existing tasks + $response = HashtopolisTestFramework::doRequest([ + "section" => "task", + "request" => "listTasks", + "accessKey" => "mykey" + ], HashtopolisTestFramework::REQUEST_UAPI); + foreach ($response["tasks"] as $task) { + $this->setTaskPriority($task["taskId"], 0); + } + + // Register a single task, max agents set to 1 + // Agent 1 should get the task, calculate keyspace and benchmark + // Set max agents to 2, agent 2 should get the task + + $response = $this->createTask([ + "name" => "task-1", + "hashlistId" => $hashlistId, + "attackCmd" => "#HL# -a 0 -r best64.rule example.dict", + "priority" => 100, + "color" => "FFFFFF", + "crackerVersionId" => 1, + "files" => [], + "maxAgents" => 1]); + + $task1Id = $response["taskId"]; + + // verify agent 1 is assigned to task 1 + $response = HashtopolisTestFramework::doRequest(["action" => "getTask", "token" => $agent1["token"]]); + if ($response["taskId"] != $task1Id) { + $this->testFailed("MaxAgentsTest:testTaskMaxAgents_bug_1013()", sprintf("Expected task with id '%d' for agent 1, instead got: %s", $task1Id, implode(", ", $response))); + return; + } + + // now set the task to only allow 2 agent to work on it + $response = HashtopolisTestFramework::doRequest([ + "section" => "task", + "request" => "setTaskMaxAgents", + "accessKey" => "mykey", + "taskId" => $task1Id, + "maxAgents" => 2 + ], HashtopolisTestFramework::REQUEST_UAPI); + + // verify agent 2 is NOT assigned to task 1 + $response = HashtopolisTestFramework::doRequest(["action" => "getTask", "token" => $agent2["token"]]); + if ($response["taskId"] != $task1Id) { + $this->testFailed("MaxAgentsTest:testTaskMaxAgents_bug_1013()", sprintf("Expected task with id '%d' for agent 2", implode(", ", $response))); + return; + } + + $this->testSuccess("MaxAgentsTest:testTaskMaxAgents_bug_1013()"); + } + private function testSuperTaskMaxAgents($hashlistId) { // disable existing tasks $response = HashtopolisTestFramework::doRequest([ diff --git a/doc/changelog.md b/doc/changelog.md index 4226c53c3..a913b999b 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -1,3 +1,8 @@ +# v0.14.1 -> x.x.x + +## Bugfixes +- Setting maxAgent after creating doesn't update the maxAgents of the taskwrapper. This only causes issues when the maxAgents was set at creation time. #1013 + # v0.14.0 -> 0.14.1 ## Tech Preview New API diff --git a/src/inc/utils/TaskUtils.class.php b/src/inc/utils/TaskUtils.class.php index 8d25a95d7..c06cacc50 100644 --- a/src/inc/utils/TaskUtils.class.php +++ b/src/inc/utils/TaskUtils.class.php @@ -591,8 +591,12 @@ public static function setSmallTask($taskId, $isSmall, $user) { */ public static function setTaskMaxAgents($taskId, $maxAgents, $user) { $task = TaskUtils::getTask($taskId, $user); + $taskWrapper = TaskUtils::getTaskWrapper($task->getTaskWrapperId(), $user); $maxAgents = intval($maxAgents); Factory::getTaskFactory()->set($task, Task::MAX_AGENTS, $maxAgents); + if ($taskWrapper->getTaskType() != DTaskTypes::SUPERTASK) { + Factory::getTaskWrapperFactory()->set($taskWrapper, TaskWrapper::MAX_AGENTS, $maxAgents); + } } /** @@ -702,7 +706,9 @@ public static function updateMaxAgents($taskId, $maxAgents, $user) { if ($maxAgents < 0) { throw new HTException("Invalid number of agents!"); } - + if ($taskWrapper->getTaskType() != DTaskTypes::SUPERTASK) { + Factory::getTaskWrapperFactory()->set($taskWrapper, TaskWrapper::MAX_AGENTS, $maxAgents); + } Factory::getTaskFactory()->set($task, Task::MAX_AGENTS, $maxAgents); } From b49d9d124b061a2b914e73dacf5255e4e4853514 Mon Sep 17 00:00:00 2001 From: Romke van Dijk Date: Mon, 23 Oct 2023 17:12:21 +0200 Subject: [PATCH 2/2] MaxAgents test now also deletes created agents --- ci/tests/integration/MaxAgentsTest.class.php | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/ci/tests/integration/MaxAgentsTest.class.php b/ci/tests/integration/MaxAgentsTest.class.php index ef1db3253..322d5e5f0 100644 --- a/ci/tests/integration/MaxAgentsTest.class.php +++ b/ci/tests/integration/MaxAgentsTest.class.php @@ -31,6 +31,15 @@ private function cleanup() { $status &= $this->deleteFileIfExists("example.dict"); $status &= $this->deleteFileIfExists("best64.rule"); + // delete the added agents + $status &= $this->deleteAgent("agent-1"); + $status &= $this->deleteAgent("agent-2"); + $status &= $this->deleteAgent("agent-1013-1"); + $status &= $this->deleteAgent("agent-1013-2"); + $status &= $this->deleteAgent("agent-pt-1"); + $status &= $this->deleteAgent("agent-pt-2"); + $status &= $this->deleteAgent("agent-pt-3"); + if (!$status) { HashtopolisTestFramework::log(HashtopolisTestFramework::LOG_ERROR, "Some cleanup failed, deleting task or deleting files not succesful!"); } @@ -504,6 +513,33 @@ private function createAgent($name) { return array("agentId" => $agent["agentId"], "token" => $token); } + private function deleteAgent($name) { + $response = HashtopolisTestFramework::doRequest([ + "section" => "agent", + "request" => "listAgents", + "accessKey" => "mykey" + ], HashtopolisTestFramework::REQUEST_UAPI); + $agent = current(array_filter($response["agents"], function($a) use ($name) { + return $a["name"] == $name; + })); + // if agent doesn't exists return true + if ($agent == null) { + return true; + } + $response = HashtopolisTestFramework::doRequest([ + "section" => "agent", + "request" => "deleteAgent", + "accessKey" => "mykey", + "agentId" => $agent["agentId"] + ], HashtopolisTestFramework::REQUEST_UAPI); + // if response is success return true + if ($response["response"] == "OK") { + return true; + } else { + return false; + } + } + private function createTask($values = []) { $query = [ "section" => "task",