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

Commit

Permalink
feat(policy): add reboot command
Browse files Browse the repository at this point in the history
Signed-off-by: Domingo Oropeza <[email protected]>
  • Loading branch information
DIOHz0r committed Jan 3, 2019
1 parent 89cc4d4 commit 0c501f0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 15 deletions.
7 changes: 7 additions & 0 deletions front/agent.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
'_ping_request' => '',
]);
Html::back();
} else if (isset($_POST['reboot'])) {
$agent->check($_POST['id'], UPDATE);
$agent->update([
'id' => $_POST['id'],
'_reboot' => '',
]);
Html::back();
} else if (isset($_POST['geolocate'])) {
$agent->check($_POST['id'], UPDATE);
$agent->update([
Expand Down
51 changes: 50 additions & 1 deletion inc/agent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ public function showForm($ID, array $options = []) {
'canUpdate' => $canUpdate,
'agent' => $fields,
'pingButton' => Html::submit(_x('button', 'Ping'), ['name' => 'ping']),
'rebootButton' => Html::submit(_x('button', 'Reboot'), ['name' => 'reboot']),
'geolocateButton' => Html::submit(_x('button', 'Geolocate'), ['name' => 'geolocate']),
'inventoryButton' => Html::submit(_x('button', 'Inventory'), ['name' => 'inventory']),

Expand Down Expand Up @@ -592,7 +593,7 @@ public function prepareInputForUpdate($input) {
}

//Send a connection status request to the device
if (isset($input['_ping_request']) || isset($input['_geolocate_request']) || isset($input['_inventory_request'])) {
if (isset($input['_ping_request']) || isset($input['_reboot']) || isset($input['_geolocate_request']) || isset($input['_inventory_request'])) {
if ($this->getTopic() === null) {
Session::addMessageAfterRedirect(__("The device is not enrolled yet", 'flyvemdm'));
return false;
Expand All @@ -608,6 +609,15 @@ public function prepareInputForUpdate($input) {
}
}

if (isset($input['_reboot'])) {
try {
$this->sendRebootQuery();
} catch (AgentSendQueryException $exception) {
Session::addMessageAfterRedirect($exception->getMessage());
return false;
}
}

if (isset($input['_geolocate_request'])) {
try {
$this->sendGeolocationQuery();
Expand Down Expand Up @@ -1679,6 +1689,7 @@ public static function getTopicsToCleanup() {
return array_merge($topics, [
'Command/Subscribe',
'Command/Ping',
'Command/Reboot',
'Command/Geolocate',
'Command/Inventory',
'Command/Lock',
Expand Down Expand Up @@ -1781,6 +1792,44 @@ private function sendInventoryQuery() {
throw new AgentSendQueryException(__("Timeout querying the device inventory", 'flyvemdm'));
}

/**
* Sends a message on the subtopic dedicated to ping requests
*
* @return bool
* @throws AgentSendQueryException
*/
private function sendRebootQuery() {
$message = json_encode(['query' => 'Reboot'], JSON_UNESCAPED_SLASHES);
$brokerMessage = new BrokerMessage($message);
$envelopeConfig = [];
$topic = $this->getTopic();
if ($topic !== null) {
$finalTopic = $topic . "/Command/Reboot";
$envelopeConfig[] = new MqttEnvelope([
'topic' => $finalTopic,
]);
$envelopeConfig[] = new FcmEnvelope([
'topic' => $finalTopic,
'scope' => $this->getPushNotificationInfo(),
]);
}
$envelope = new BrokerEnvelope($brokerMessage, $envelopeConfig);
$this->notify($envelope);

$loopCount = 50;
$updatedAgent = new self();
while ($loopCount > 0) {
usleep(200000); // 200 milliseconds
$loopCount--;
$updatedAgent->getFromDB($this->getID());
if ($updatedAgent->getField('last_contact') != $this->fields['last_contact']) {
Session::addMessageAfterRedirect(__('The device restarted', 'flyvemdm'));
return true;
}
}
throw new AgentSendQueryException(__("Timeout querying the device", 'flyvemdm'));
}

/**
* Sends a message on the subtopic dedicated to ping requests
*
Expand Down
1 change: 1 addition & 0 deletions tests/src/Flyvemdm/Tests/CommonTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ public static function commandList() {
return [
'Command/Subscribe',
'Command/Ping',
'Command/Reboot',
'Command/Geolocate',
'Command/Inventory',
'Command/Lock',
Expand Down
42 changes: 42 additions & 0 deletions tests/suite-integration/PluginFlyvemdmAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,48 @@ public function testPingRequest() {
);
}

/**
* test ping message
* @tags testPingRequest
*/
public function testRebootRequest() {
list($user, $serial, $guestEmail, $invitation) = $this->createUserInvitation(\User::getForeignKeyField());
$agent = $this->agentFromInvitation($user, $guestEmail, $serial,
$invitation->getField('invitation_token'));

// Get enrolment data to enable the agent's MQTT account
$this->boolean($agent->getFromDB($agent->getID()))->isTrue();

// Find the last existing ID of logged MQTT messages
$log = new \PluginFlyvemdmMqttlog();
$lastLogId = \PluginFlyvemdmCommon::getMax($log, '', 'id');

$updateSuccess = $agent->update([
'id' => $agent->getID(),
'_reboot' => '',
]);

// Update shall fail because the ping answer will not occur
$this->boolean($updateSuccess)->isFalse();

// Get the latest MQTT message
sleep(2);

$logEntryFound = 0;
$rows = $log->find("`direction` = 'O' AND `id` > '$lastLogId'");
foreach ($rows as $row) {
if ($row['topic'] == $agent->getTopic() . '/Command/Reboot') {
$logEntryFound = $row['id'];
break;
}
}
$this->integer((int) $logEntryFound)->isGreaterThan(0);

// check the message
$mqttMessage = ['query' => 'Reboot'];
$this->string($row['message'])->isEqualTo(json_encode($mqttMessage, JSON_UNESCAPED_SLASHES));
}

/**
* test geolocate message
* @tags testGeolocateRequest
Expand Down
31 changes: 17 additions & 14 deletions tpl/agent.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@
<td>{{ agent.notification_type|upper }}</td>
</tr>
{% if canUpdate == true %}
<tr class='tab_bg_1'>
<td>
{{ __('Actions', 'flyvemdm') }}
</td>
<td>
{{ pingButton|raw }}
</td>
<td>
{{ geolocateButton|raw }}
</td>
<td>
{{ inventoryButton|raw }}
</td>
</tr>
<tr class="tab_bg_1">
<th colspan="4" class="subheader">{{ __('Actions', 'flyvemdm') }}</th>
</tr>
<tr class='tab_bg_2'>
<td align="center">
{{ pingButton|raw }}
</td>
<td align="center">
{{ rebootButton|raw }}
</td>
<td align="center">
{{ geolocateButton|raw }}
</td>
<td align="center">
{{ inventoryButton|raw }}
</td>
</tr>
{% endif %}

0 comments on commit 0c501f0

Please sign in to comment.