Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
feat(agent): cancel tasks when an agent leaves a fleet
Browse files Browse the repository at this point in the history
refactor post_updateItem by splitting its content and reordering its actions

Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry authored and DIOHz0r committed Mar 26, 2018
1 parent 6afc6bb commit 4b6f99a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 16 deletions.
66 changes: 52 additions & 14 deletions inc/agent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,25 +644,14 @@ public function pre_deleteItem() {
*/
public function post_updateItem($history = 1) {
if (in_array('plugin_flyvemdm_fleets_id', $this->updates)) {
// create tasks for the agent from already applied policies
$fleetId = $this->fields['plugin_flyvemdm_fleets_id'];
$this->updateSubscription();
$newFleet = new PluginFlyvemdmFleet();
if ($newFleet->getFromDB($fleetId)) {
if ($newFleet->getFromDB($this->fields['plugin_flyvemdm_fleets_id'])) {
// Create task status for the agent and the applied policies
$task = new PluginFlyvemdmTask();
$tasks = $task->find("`plugin_flyvemdm_fleets_id` = '$fleetId'");
foreach ($tasks as $row) {
$taskStatus = new PluginFlyvemdmTaskstatus();
$taskStatus->add([
'plugin_flyvemdm_agents_id' => $this->getID(),
'plugin_flyvemdm_tasks_id' => $row['id'],
'status' => 'pending',
]);
}
$this->createTaskStatuses($newFleet);
}

// update tasks for the agent from already applied policies in the old fleet
$this->updateSubscription();
if (isset($this->oldvalues['plugin_flyvemdm_fleets_id'])) {
$oldFleet = new PluginFlyvemdmFleet();
$oldFleet->getFromDB($this->oldvalues['plugin_flyvemdm_fleets_id']);
Expand All @@ -687,6 +676,55 @@ public function post_updateItem($history = 1) {
}
}

/**
* creates task statuses for the agent and the associated fleet
* @param PluginFlyvemdmFleet $fleet
*/
private function createTaskStatuses(PluginFlyvemdmFleet $fleet) {
$fleetId = $fleet->getID();
$task = new PluginFlyvemdmTask();
$rows = $task->find("`plugin_flyvemdm_fleets_id` = '$fleetId'");
foreach ($rows as $row) {
$taskStatus = new PluginFlyvemdmTaskstatus();
$taskStatus->add([
'plugin_flyvemdm_agents_id' => $this->getID(),
'plugin_flyvemdm_tasks_id' => $row['id'],
'status' => 'pending',
]);
}
}

/**
* cancels task statuses for the agent anf the given fleet
* @param PluginFlyvemdmFleet $fleet
*/
private function cancelTaskStatuses(PluginFlyvemdmFleet $fleet) {
global $DB;

$fleetId = $fleet->getID();
$request = [
'FROM' => PluginFlyvemdmTaskstatus::getTable(),
'INNER JOIN' => [
PluginFlyvemdmTask::getTable() => [
'FKEY' => [
PluginFlyvemdmTask::getTable() => 'id',
PluginFlyvemdmTaskstatus::getTable() => PluginFlyvemdmTask::getForeignKeyField()
]
]
],
'WHERE' => [
PluginFlyvemdmFleet::getForeignKeyField() => [$fleetId]
]
];
$status = new PluginFlyvemdmTaskstatus();
foreach ($DB->request($request) as $row) {
$status->update([
'id' => $row['id'],
'status' => 'canceled',
]);
}
}

/**
* Actions done after the restore of the item
* @return void
Expand Down
45 changes: 43 additions & 2 deletions tests/suite-integration/PluginFlyvemdmTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ public function testApplyPolicy() {
$policy = new \PluginFlyvemdmPolicy();
$policy->getFromDBByCrit(['symbol' => 'storageEncryption']);
$this->boolean($policy->isNewItem())->isFalse("Could not find the test policy");
$fleetId = $fleet->getID();

$fleetFk = \PluginFlyvemdmFleet::getForeignKeyField();
$policyFk = \PluginFlyvemdmPolicy::getForeignKeyField();
$task = $this->newTestedInstance();
$taskId = $task->add([
$fleetFk => $fleetId,
$fleetFk => $$fleet->getID(),
$policyFk => $policy->getID(),
'value' => '0',
]);
Expand Down Expand Up @@ -157,6 +156,48 @@ public function testApplyPolicy() {
// Check task statuses are deleted
$rows = $taskStatus->find("`$taskFk` = '$taskId'");
$this->integer(count($rows))->isEqualTo(0);

// Test tassk status is created when an agent joins a fleet having policies
// Create a 2nd fleet
$fleet2 = $this->createFleet();

// Apply a policy
$policy = new \PluginFlyvemdmPolicy();
$policy->getFromDBByCrit(['symbol' => 'disableWifi']);
$this->boolean($policy->isNewItem())->isFalse("Could not find the test policy");

$fleetFk = \PluginFlyvemdmFleet::getForeignKeyField();
$policyFk = \PluginFlyvemdmPolicy::getForeignKeyField();
$task2 = $this->newTestedInstance();
$taskId2 = $task->add([
$fleetFk => $fleet2->getID(),
$policyFk => $policy->getID(),
'value' => '0',
]);
$this->boolean($task->isNewItem())
->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));

// Join the 2nd fleet
$this->boolean($agent->update([
'id' => $agent->getID(),
'plugin_flyvemdm_fleets_id' => $fleet2->getID(),
]))->isTrue();

// Check a task status is created for the agent
$taskStatus2 = new \PluginFlyvemdmTaskstatus();
$taskFk = $task::getForeignKeyField();
$rows = $taskStatus2->find("`$taskFk` = '$taskId2'");
$this->integer(count($rows))->isEqualTo(1);
foreach ($rows as $row) {
$this->string($row['status'])->isEqualTo('pending');
}

// Check the old task status is canceled
$rows = $taskStatus->find("`$taskFk` = '$taskId'");
$this->integer(count($rows))->isEqualTo(1);
foreach ($rows as $row) {
$this->string($row['status'])->isEqualTo('canceled');
}
}

/**
Expand Down

0 comments on commit 4b6f99a

Please sign in to comment.