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

Commit

Permalink
feat(enrollment): check the version of the agent at enrollment time
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Sep 6, 2017
1 parent aee9e9a commit 8bceef3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 22 deletions.
31 changes: 31 additions & 0 deletions inc/agent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ class PluginFlyvemdmAgent extends CommonDBTM implements PluginFlyvemdmNotifiable

protected $topic = null;

/**
*
* Returns the minimum version of the agent accepted by the backend
*
* @param string $mdmType the type of the agent.
*
* @return string the minimum version of the agent depending on its type
*/
private function getMinVersioForType($mdmType) {
switch ($mdmType) {
case 'android':
return '2.0';
break;

case 'apple':
return '1.0';
break;
}

return '';
}

/**
* get mdm types availables
*/
Expand Down Expand Up @@ -986,6 +1008,15 @@ protected function enrollByInvitationToken($input) {
return false;
}

// Check the agent matches the minimum version requirement of the backend
$minVersion = $this->getMinVersioForType($mdmType);
if (version_compare($minVersion, $version) > 0) {
$event = __('The agent version is too low', 'flyvemdm');
$this->filterMessages($event);
$this->logInvitationEvent($invitation, $event);
return false;
}

// Check the invitation is pending
if ($invitation->getField('status') != 'pending') {
$event = __('Invitation is not pending', 'flyvemdm');
Expand Down
65 changes: 45 additions & 20 deletions tests/suite-integration/PluginFlyvemdmAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

class PluginFlyvemdmAgent extends CommonTestCase {

private $minAndroidVersion = '2.0.0';

public function beforeTestMethod($method) {
$this->resetState();
parent::beforeTestMethod($method);
Expand Down Expand Up @@ -84,7 +86,7 @@ public function testDeviceCountLimit() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]);
// Agent creation should succeed
Expand All @@ -110,7 +112,7 @@ public function testDeviceCountLimit() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]);
// Device limit reached : agent creation should fail
Expand Down Expand Up @@ -140,7 +142,7 @@ public function testEnrollAgent() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand All @@ -163,7 +165,7 @@ public function testEnrollAgent() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
]
);
$this->boolean($agent->isNewItem(), json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT))->isTrue();
Expand All @@ -185,7 +187,7 @@ public function testEnrollAgent() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'alien MDM',
]
);
Expand Down Expand Up @@ -241,6 +243,29 @@ public function testEnrollAgent() {
$rows = $invitationLog->find("`plugin_flyvemdm_invitations_id` = '$inviationId'");
$this->integer(count($rows))->isEqualTo($expectedLogCount);

// Test enrollment with a too low version
$rows = $invitationLog->find("1");

$agent = $this->enrollFromInvitation(
$user, [
'entities_id' => $_SESSION['glpiactive_entity'],
'_email' => $guestEmail,
'_invitation_token' => $invitation->getField('invitation_token'),
'_serial' => $serial,
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.9',
'type' => 'android',
]
);
$this->boolean($agent->isNewItem())->isTrue();

$invitationLog = new \PluginFlyvemdmInvitationlog();
$expectedLogCount++;
$rows = $invitationLog->find("`plugin_flyvemdm_invitations_id` = '$inviationId'");
$this->integer(count($rows))->isEqualTo($expectedLogCount);

// test enrollment without serial or uuid
$agent = $this->enrollFromInvitation(
$user, [
Expand All @@ -250,7 +275,7 @@ public function testEnrollAgent() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand All @@ -271,7 +296,7 @@ public function testEnrollAgent() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'apple',
]
);
Expand Down Expand Up @@ -381,7 +406,7 @@ public function testEnrollAgent() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'apple',
]
);
Expand Down Expand Up @@ -411,7 +436,7 @@ public function testEnrollWithUuid() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down Expand Up @@ -442,7 +467,7 @@ public function testUnenrollAgent() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down Expand Up @@ -493,7 +518,7 @@ public function testDelete() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down Expand Up @@ -537,7 +562,7 @@ public function testDeviceOnlineChange() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down Expand Up @@ -570,7 +595,7 @@ public function testChangeFleet() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down Expand Up @@ -627,7 +652,7 @@ public function testPurgeEnroledAgent() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down Expand Up @@ -678,7 +703,7 @@ public function testPurgeAgent() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down Expand Up @@ -733,7 +758,7 @@ public function testPingRequest() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down Expand Up @@ -785,7 +810,7 @@ public function testGeolocateRequest() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down Expand Up @@ -836,7 +861,7 @@ public function testInventoryRequest() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down Expand Up @@ -891,7 +916,7 @@ public function testLockAndWipe() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down Expand Up @@ -941,7 +966,7 @@ public function testMoveBetweenFleets() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down
4 changes: 3 additions & 1 deletion tests/suite-integration/PluginFlyvemdmFleet.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

class PluginFlyvemdmFleet extends CommonTestCase {

private $minAndroidVersion = '2.0.0';

public function beforeTestMethod($method) {
$this->resetState();
parent::beforeTestMethod($method);
Expand Down Expand Up @@ -75,7 +77,7 @@ public function testAddAgentToFleet() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down
4 changes: 3 additions & 1 deletion tests/suite-integration/PluginFlyvemdmTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

class PluginFlyvemdmTask extends CommonTestCase {

private $minAndroidVersion = '2.0.0';

public function beforeTestMethod($method) {
$this->resetState();
parent::beforeTestMethod($method);
Expand Down Expand Up @@ -64,7 +66,7 @@ public function testApplyPolicy() {
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => '1.0.0',
'version' => $this->minAndroidVersion,
'type' => 'android',
]
);
Expand Down

0 comments on commit 8bceef3

Please sign in to comment.