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

Commit

Permalink
feat(enrollment): handle version of agent sent in enrollment process
Browse files Browse the repository at this point in the history
Agents send their version when enrolling
  • Loading branch information
btry committed Dec 16, 2016
1 parent 6d90572 commit f8be502
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 389 deletions.
34 changes: 25 additions & 9 deletions inc/agent.class.php
Expand Up @@ -82,7 +82,7 @@ public static function getTypeName($nb=0) {
*/
public function getRights($interface = 'central') {
$rights = parent::getRights();
/// For additional righrs if needed
/// For additional rights if needed
//$rights[self::RIGHTS] = self::getTypeName();

return $rights;
Expand Down Expand Up @@ -339,7 +339,7 @@ public function post_addItem() {
*/
public function post_getFromDB() {
// set Topic after getting an item
// Useful for post_dpurgeItem
// Useful for post_purgeItem
$this->getTopic();
$this->setupMqttAccess();
}
Expand Down Expand Up @@ -726,8 +726,8 @@ protected static function checkChallengeCombinations($authFactors) {
$method = self::ENROLL_AGENT_TOKEN;
}

// or require challenge on a entity token only
} else if (array_key_exists('entityToken', $authFactors)) {
// or require challenge on a entity token only
if (count($authFactors) == 1) {
$method = self::ENROLL_ENTITY_TOKEN;
}
Expand All @@ -741,12 +741,13 @@ protected static function checkChallengeCombinations($authFactors) {
* @param array $input Enrollment data
*/
protected function enrollByInvitationToken($input) {
$invitationToken = $input['_invitation_token'];
$email = $input['_email'];
$serial = $input['_serial'];
$csr = $input['csr'];
$firstname = $input['firstname'];
$lastname = $input['lastname'];
$invitationToken = isset($input['_invitation_token']) ? $input['_invitation_token'] : null;
$email = isset($input['_email']) ? $input['_email'] : null;
$serial = isset($input['_serial']) ? $input['_serial'] : null;
$csr = isset($input['csr']) ? $input['csr'] : null;
$firstname = isset($input['firstname']) ? $input['firstname'] : null;
$lastname = isset($input['lastname']) ? $input['lastname'] : null;
$version = isset($input['version']) ? $input['version'] : null;

$input = array();

Expand All @@ -766,6 +767,20 @@ protected function enrollByInvitationToken($input) {
return false;
}

if (empty($version)) {
$event = __('Agent version missing', 'storkmdm');
$this->filterMessages($event);
$this->logInvitationEvent($invitation, $event);
return false;
}

if (preg_match('#^[\d\.]+$#', $version) !== 1 ) {
$event = __('Bad agent version', 'storkmdm');
$this->filterMessages($event);
$this->logInvitationEvent($invitation, $event);
return false;
}

// Check the invitation is pending
if ($invitation->getField('status') != 'pending') {
$event = __('Invitation is not pending', 'storkmdm');
Expand Down Expand Up @@ -904,6 +919,7 @@ protected function enrollByInvitationToken($input) {
$input['plugin_storkmdm_fleets_id'] = $defaultFleet->getID();
$input['_invitations_id'] = $invitation->getID();
$input['enroll_status'] = 'enrolled';
$input['version'] = $version;
return $input;

}
Expand Down
78 changes: 39 additions & 39 deletions tests/0010_Integration/DeviceEnrollmentTest.php
Expand Up @@ -81,51 +81,51 @@ public function testEnrollAgentWithBadToken() {
public function testEnrollAgentWithoutVersion() {
// test disabled for now
$this->assertTrue(true);
// $invitationLog = new PluginStorkmdmInvitationlog();
// $rows = $invitationLog->find("1");
// $logCount = count($rows);

// $invitation = self::$fixture['invitation'];
// $agent = new PluginStorkmdmAgent();
// $agentId = $agent ->add([
// 'entities_id' => $_SESSION['glpiactive_entity'],
// '_email' => self::$fixture['guestEmail'],
// '_invitation_token' => $invitation->getField('invitation_token'),
// '_serial' => 'AZERTY',
// 'csr' => '',
// 'firstname' => 'John',
// 'lastname' => 'Doe',
// ]);
// $this->assertFalse($agentId);

// $rows = $invitationLog->find("1");
// $this->assertEquals($logCount + 1, count($rows));
$invitationLog = new PluginStorkmdmInvitationlog();
$rows = $invitationLog->find("1");
$logCount = count($rows);

$invitation = self::$fixture['invitation'];
$agent = new PluginStorkmdmAgent();
$agentId = $agent ->add([
'entities_id' => $_SESSION['glpiactive_entity'],
'_email' => self::$fixture['guestEmail'],
'_invitation_token' => $invitation->getField('invitation_token'),
'_serial' => 'AZERTY',
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
]);
$this->assertFalse($agentId);

$rows = $invitationLog->find("1");
$this->assertEquals($logCount + 1, count($rows));
}

public function testEnrollAgentWithBadVersion() {
// test disabled for now
$this->assertTrue(true);

// $invitationLog = new PluginStorkmdmInvitationlog();
// $rows = $invitationLog->find("1");
// $logCount = count($rows);

// $invitation = self::$fixture['invitation'];
// $agent = new PluginStorkmdmAgent();
// $agentId = $agent ->add([
// 'entities_id' => $_SESSION['glpiactive_entity'],
// '_email' => self::$fixture['guestEmail'],
// '_invitation_token' => $invitation->getField('invitation_token'),
// '_serial' => 'AZERTY',
// 'csr' => '',
// 'firstname' => 'John',
// 'lastname' => 'Doe',
// 'version' => 'bad version',
// ]);
// $this->assertFalse($agentId);

// $rows = $invitationLog->find("1");
// $this->assertEquals($logCount + 1, count($rows));
$invitationLog = new PluginStorkmdmInvitationlog();
$rows = $invitationLog->find("1");
$logCount = count($rows);

$invitation = self::$fixture['invitation'];
$agent = new PluginStorkmdmAgent();
$agentId = $agent ->add([
'entities_id' => $_SESSION['glpiactive_entity'],
'_email' => self::$fixture['guestEmail'],
'_invitation_token' => $invitation->getField('invitation_token'),
'_serial' => 'AZERTY',
'csr' => '',
'firstname' => 'John',
'lastname' => 'Doe',
'version' => 'bad version',
]);
$this->assertFalse($agentId);

$rows = $invitationLog->find("1");
$this->assertEquals($logCount + 1, count($rows));
}

public function testEnrollAgentWithEmptySerial() {
Expand Down

0 comments on commit f8be502

Please sign in to comment.