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

Commit

Permalink
fix(task): non unique tasks overwrite each other
Browse files Browse the repository at this point in the history
fix #302

mqtt topic is changed for all tasks, to ensure consistent MQTT tree definition
some refactor by the way

Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry authored and DIOHz0r committed Feb 8, 2018
1 parent 64d30ea commit 5a989aa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 63 deletions.
78 changes: 16 additions & 62 deletions inc/task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ private function deleteTaskStatuses() {
}

/**
* MQTT publish all policies applying to the fleet
* MQTT publish a policy applying to the fleet
*
* @param PluginFlyvemdmNotifiable $item
*/
Expand All @@ -370,38 +370,42 @@ public function publishPolicy(PluginFlyvemdmNotifiable $item) {
$fleetId = $fleet->getID();
$topic = $item->getTopic();

// Initialize a task status for each agent in the fleet
$agent = new PluginFlyvemdmAgent();
$rows = $agent->find("`plugin_flyvemdm_fleets_id` = '$fleetId'");
$fleetFk = $fleet::getForeignKeyField();
$rows = $agent->find("`$fleetFk` = '$fleetId'");
foreach ($rows as $row) {
$agent = new PluginFlyvemdmAgent();
if ($agent->getFromDB($row['id'])) {
$taskStatus = new PluginFlyvemdmTaskstatus();
$taskStatus->add([
'plugin_flyvemdm_agents_id' => $row['id'],
'plugin_flyvemdm_tasks_id' => $this->getID(),
'status' => 'pending',
$agent::getForeignKeyField() => $row['id'],
$this::getForeignKeyField() => $this->getID(),
'status' => 'pending',
]);
}
}

$policy = new PluginFlyvemdmPolicy();
$policyFk = $policy::getForeignKeyField();
$policyFactory = new PluginFlyvemdmPolicyFactory();
$appliedPolicy = $policyFactory->createFromDBByID($this->fields['plugin_flyvemdm_policies_id']);
$appliedPolicy = $policyFactory->createFromDBByID($this->fields[$policyFk]);
if ($appliedPolicy === null) {
Toolbox::logInFile('php-errors', "Plugin Flyvemdm : Policy ID " . $this->fields['plugin_flyvemdm_policies_id'] . "not found while generating MQTT message\n" );
Toolbox::logInFile('php-errors', "Plugin Flyvemdm : Policy ID " . $this->fields[$policyFk] . " not found while generating MQTT message\n" );
return;
}

$policy = new PluginFlyvemdmPolicy();
$policy->getFromDB($this->fields['plugin_flyvemdm_policies_id']);
$policy->getFromDB($this->fields[$policyFk]);
$policyName = $policy->getField('symbol');
$taskId = $this->getID();
$policyMessage = $appliedPolicy->getMqttMessage(
$this->fields['value'],
$this->fields['itemtype'],
$this->fields['items_id']
);
$policyMessage['taskId'] = $this->getID();
$encodedMessage = json_encode($policyMessage, JSON_UNESCAPED_SLASHES);
$fleet->notify("$topic/Policy/$policyName", $encodedMessage, 0, 1);
$fleet->notify("$topic/Policy/$policyName/Task/$taskId", $encodedMessage, 0, 1);
}
}

Expand All @@ -420,61 +424,11 @@ public function unpublishPolicy(PluginFlyvemdmNotifiable $item) {
$topic = $item->getTopic();

$policy = new PluginFlyvemdmPolicy();
$taskId = $this->getID();
$policy->getFromDB($this->fields['plugin_flyvemdm_policies_id']);
$policyName = $policy->getField('symbol');
$fleet->notify("$topic/Policy/$policyName", null, 0, 1);

}
}

/**
* MQTT publish all policies applying to the fleet
*
* @param PluginFlyvemdmNotifiable $item
* @param array $groups the notifiable is updated only for the following policies groups
*/
public function publishPolicies(PluginFlyvemdmNotifiable $item, $groups = []) {
if ($this->silent) {
return;
}

$fleet = $item->getFleet();

if ($fleet !== null && $fleet->getField('is_default') == '0') {
$fleetId = $fleet->getID();

if (count($groups) == 0) {
$groups = $this->getGroupsOfAppliedPolicies($fleet);
}

$topic = $item->getTopic();
foreach ($groups as $groupName) {
// get policies with data, including not applied policies having a default value
$policiesToApply = $this->getGroupOfPolicies($groupName, $fleet);
$fleet->notify("$topic/Policy/$policyName/Task/$taskId", null, 0, 1);

// create tasks for the agents of the fleet
$agent = new PluginFlyvemdmAgent();
$rows = $agent->find("`plugin_flyvemdm_fleets_id` = '$fleetId'");
foreach ($rows as $row) {
$agent = new PluginFlyvemdmAgent();
if ($agent->getFromDB($row['id'])) {
$this->createTaskStatus($agent, $policiesToApply);
}
}

// Build MQTT message for the group
try {
$groupToEncode = $this->buildMqttMessage($policiesToApply);
} catch (PolicyApplicationException $exception) {
Toolbox::logInFile('plugin_flyvemdm_communication',
'Fleet (' . $fleet->getField('name') . '): ' . $exception->getMessage());
continue;
}

// convert message into JSON and send it
$encodedGroup = json_encode([$groupName => $groupToEncode], JSON_UNESCAPED_SLASHES);
$fleet->notify("$topic/$groupName", $encodedGroup, 0, 1);
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion tests/suite-integration/PluginFlyvemdmTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ public function testApplyPolicy() {
$log = new \PluginFlyvemdmMqttlog();
$rows = $log->find('', '`date` DESC', '1');
$row = array_pop($rows);
$mqttLogId = $row['id'];

$policyName = $policy->getField('symbol');

// check the topic of the message
$this->string($row['topic'])->isEqualTo($fleet->getTopic() . "/Policy/$policyName");
$this->string($row['topic'])->isEqualTo($fleet->getTopic() . "/Policy/$policyName/Task/$taskId");

// check the message
$receivedMqttMessage = json_decode($row['message'], JSON_OBJECT_AS_ARRAY);
Expand All @@ -147,6 +148,15 @@ public function testApplyPolicy() {
'id' => $taskId,
], 1);

// CHeck a mqtt message is sent to remove the applied policy from MQTT
$rows = $log->find("`id` > '$mqttLogId' AND `direction`='O'", '`date` DESC', '1');
$this->array($rows)->size->isEqualTo(1);
$row = array_pop($rows);
// check the topic of the message
$this->string($row['topic'])->isEqualTo($fleet->getTopic() . "/Policy/$policyName/Task/$taskId");
// check the message
$this->string($row['message'])->isEqualTo('');

// Check task statuses are deleted
$rows = $taskStatus->find("`$taskFk` = '$taskId'");
$this->integer(count($rows))->isEqualTo(0);
Expand Down

0 comments on commit 5a989aa

Please sign in to comment.