Skip to content

Commit

Permalink
Merge pull request #1014 from hashtopolis/bug/1013
Browse files Browse the repository at this point in the history
Updating maxAgent for Tasks now also updates TaskWrapper
  • Loading branch information
s3inlc authored Oct 23, 2023
2 parents 76ccc3c + b49d9d1 commit 623136c
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
93 changes: 93 additions & 0 deletions ci/tests/integration/MaxAgentsTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
Expand All @@ -44,6 +53,7 @@ public function run() {
$hashlistId = $response["hashlistId"];

$this->testTaskMaxAgents($hashlistId);
$this->testTaskMaxAgents_bug_1013($hashlistId);
$this->testSuperTaskMaxAgents($hashlistId);
}
finally {
Expand Down Expand Up @@ -200,6 +210,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([
Expand Down Expand Up @@ -447,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",
Expand Down
5 changes: 5 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/inc/utils/TaskUtils.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 623136c

Please sign in to comment.