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

WIP Save MQTT prefix in configuration #419

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ before_install:

before_script:
- "./tests/before_script.sh"

script:
- if [ "${TRAVIS_PHP_VERSION:0:3}" = "$CS" ] && [ "$GLPI_BRANCH" = "$AFTER_SUCCESS_BRANCH" ]; then COVERAGE="--nccfc CommonTreeDropdown CommonDropdown CommonDBTM CommonGLPI"; else COVERAGE="-ncc"; fi
- if [ -e ../../scripts/cliinstall.php ] ; then php ../../scripts/cliinstall.php --db=$OLDDBNAME --user=root --tests ; fi
Expand Down
2 changes: 2 additions & 0 deletions inc/agent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,8 @@ public function getTopic() {
* @return bool
*/
public function getByTopic($topic) {
$topic = PluginFlyvemdmCommon::removeMqttPrefix($topic);

$mqttPath = explode('/', $topic);
if (!isset($mqttPath[2])) {
return false;
Expand Down
23 changes: 23 additions & 0 deletions inc/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,27 @@ public static function getGlpiVersion() {
? GLPI_PREVER
: GLPI_VERSION;
}

/**
* Removes the mqtt prefix from a MQTT topic
*
* @param string $topic Topic to modify
* @param boolean $readConfig set to True to force reading the configuration
*
* @return string|false the topic without its prefix, or false if an error occured
*/
public static function removeMqttPrefix($topic, $readConfig = false) {
static $prefix = null;
if ($prefix === null || $readConfig) {
$config = Config::getConfigurationValues('flyvemdm', ['mqtt_prefix']);
$prefix = $config['mqtt_prefix'];
}

if (!PluginFlyvemdmCommon::startsWith($topic, $prefix)) {
return false;
}
$topic = substr($topic, strlen($prefix));

return $topic;
}
}
17 changes: 17 additions & 0 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,23 @@ public static function configUpdate($input) {
}
}

if (isset($input['mqtt_prefix']) && strlen($input['mqtt_prefix']) > 0) {
// Ensure there is a trailing slash
if (strrpos($input['mqtt_prefix'], '/') != strlen($input['mqtt_prefix']) - 1) {
$input['mqtt_prefix'] .= '/';
}
}

//lock mqtt prefix if needed
$DbUtil = new DbUtils();
$agentsCount = $DbUtil->countElementsInTable('glpi_plugin_flyvemdm_agents');
$fleetsCount = $DbUtil->countElementsInTable('glpi_plugin_flyvemdm_fleets');
$invitationCount = $DbUtil->countElementsInTable('glpi_plugin_flyvemdm_invitations');
if ($agentsCount + $fleetsCount + $invitationCount > 0) {
unset($input['mqtt_prefix']);
Session::addMessageAfterRedirect(__('Update of MQTT prefix ignored (invitations, agents or fleets created)', 'flyvemdm'), false, WARNING);
}

if (isset($_SESSION['plugin_flyvemdm_wizard_step'])) {
$input = static::processStep($input);
if (count($input) > 0 && $input !== false) {
Expand Down
3 changes: 3 additions & 0 deletions inc/mqtthandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public function pingresp(\sskaje\mqtt\MQTT $mqtt, \sskaje\mqtt\Message\PINGRESP
*/
public function publish(\sskaje\mqtt\MQTT $mqtt, \sskaje\mqtt\Message\PUBLISH $publish_object) {
$topic = $publish_object->getTopic();
$topic = PluginFlyvemdmCommon::removeMqttPrefix($topic);
$message = $publish_object->getMessage();
$this->log->saveIngoingMqttMessage($topic, $message);

Expand All @@ -143,6 +144,8 @@ public function publish(\sskaje\mqtt\MQTT $mqtt, \sskaje\mqtt\Message\PUBLISH $p
}
} else if ($topic === 'FlyvemdmManifest/Status/Version') {
$this->publishFlyveManifest($mqtt, $message);
// Force update of mqtt prefix, in case of configuration change
PluginFlyvemdmCommon::removeMqttPrefix('', true);
}
}

Expand Down
1 change: 1 addition & 0 deletions install/installer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ protected function createInitialConfig() {
'mqtt_broker_tls_ciphers' => self::DEFAULT_CIPHERS_LIST,
'mqtt_user' => self::BACKEND_MQTT_USER,
'mqtt_passwd' => $MdmMqttPassword,
'mqtt_prefix' => '',
'instance_id' => $instanceId,
'registered_profiles_id' => '',
'guest_profiles_id' => '',
Expand Down
1 change: 1 addition & 0 deletions install/upgrade/update_to_dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function plugin_flyvemdm_update_to_dev(Migration $migration) {
'android_bugcollector_login' => '',
'android_bugcollector_passwd' => '',
'invitation_deeplink' => PLUGIN_FLYVEMDM_DEEPLINK,
'mqtt_prefix' => '',
]);

$config = Config::getConfigurationValues('flyvemdm', ['mqtt_broker_tls']);
Expand Down
49 changes: 49 additions & 0 deletions tests/suite-unit/PluginFlyvemdmCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,53 @@ public function testSaveInventoryFile() {
$class::recursiveRmdir(FLYVEMDM_INVENTORY_PATH);
$this->string($inventoryExists)->isEqualTo($fileContent);
}
public function providerRemoveMqttPrefix() {
return [
[
'prefix' => '/',
'topic' => '/1/agent/42/some/sub/topic',
'expected' => '1/agent/42/some/sub/topic',
],
[
'prefix' => '/',
'topic' => '1/agent/42/some/sub/topic',
'expected' => false,
],
[
'prefix' => 'some/prefix/',
'topic' => 'some/prefix/1/agent/42/some/sub/topic',
'expected' => '1/agent/42/some/sub/topic',
],
[
'prefix' => 'some/prefix/',
'topic' => 'some/invalid/prefix/1/agent/42/some/sub/topic',
'expected' => false,
],

// Tests with empty prefix msut be at the end to not impact next tests
[
'prefix' => '',
'topic' => '1/agent/42/some/sub/topic',
'expected' => '1/agent/42/some/sub/topic',
],
[
'prefix' => '',
'topic' => '/1/agent/42/some/sub/topic',
'expected' => '/1/agent/42/some/sub/topic',
],
];
}

/**
* @dataProvider providerRemoveMqttPrefix
*/
public function testRemoveMqttPrefix($prefix, $topic, $expected) {
\Config::setConfigurationValues('flyvemdm', ['mqtt_prefix' => $prefix]);
$output = \PluginFlyvemdmCommon::removeMqttPrefix($topic, true);
if ($expected === false) {
$this->boolean($output)->isFalse();
} else {
$this->string($output)->isEqualTo($expected);
}
}
}
2 changes: 1 addition & 1 deletion tools/cli_install.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
WHERE `name` like 'full access from localhost'";
$DB->query($apiClientQuery);

if($dev) {
if ($dev) {
$entityConfig = new PluginFlyvemdmEntityConfig();
$entityConfig->getFromDBByCrit([
'entities_id' => '0',
Expand Down
9 changes: 9 additions & 0 deletions tpl/config-messagequeue.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
min="1" max="65535"></td>
<td>{{ __('A port number between 1025 and 65535, standard port is 1883', 'flyvemdm') }}</td>
</tr>
<tr class="tab_bg_1">
<td>{{ __('MQTT prefix', 'flyvemdm') }}</td>
<td><input type="text" name="mqtt_prefix" value="{{ config.mqtt_prefix }}"
{% if (config.mqtt_prefix_locked) == 1 %}disabled{% endif %}></td>
<td>{{ __('Prepend all topics with a prefix (useful if a MQTT broker is mutualized). The mqtt daemon must be restarted after the change.', 'flyvemdm') }}
<br>
<span class="red">{{ __('Note: This value can be changed only once and if no new invitation, agent or fleet has been created.', 'flyvemdm') }}</span>
</td>
</tr>
<tr class="tab_bg_1">
<td>{{ __('MQTT broker port for TLS', 'flyvemdm') }}</td>
<td><input type="number" name="mqtt_broker_tls_port" value="{{ config.mqtt_broker_tls_port }}"
Expand Down