From 53b01c94a12b1132453792a27fa7a8ed73b7750d Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Tue, 10 Apr 2018 14:12:02 +0200 Subject: [PATCH 1/5] feat(M2M): save mqtt prefix in configuration Signed-off-by: Thierry Bugier --- inc/agent.class.php | 2 + inc/common.class.php | 23 +++++++++++ inc/config.class.php | 7 ++++ inc/mqtthandler.class.php | 3 ++ install/installer.class.php | 1 + install/upgrade/update_to_dev.php | 1 + tests/suite-unit/PluginFlyvemdmCommon.php | 49 +++++++++++++++++++++++ tpl/config-messagequeue.html.twig | 5 +++ 8 files changed, 91 insertions(+) diff --git a/inc/agent.class.php b/inc/agent.class.php index fe6e0060..89f23304 100644 --- a/inc/agent.class.php +++ b/inc/agent.class.php @@ -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; diff --git a/inc/common.class.php b/inc/common.class.php index 5122daee..458523d6 100644 --- a/inc/common.class.php +++ b/inc/common.class.php @@ -207,4 +207,27 @@ public static function getGlpiVersion() { ? GLPI_PREVER : GLPI_VERSION; } + + /** + * Removes the mqtt prefix from + * + * @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; + } } diff --git a/inc/config.class.php b/inc/config.class.php index a981a59b..be609bd0 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -398,6 +398,13 @@ 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'] .= '/'; + } + } + if (isset($_SESSION['plugin_flyvemdm_wizard_step'])) { $input = static::processStep($input); if (count($input) > 0 && $input !== false) { diff --git a/inc/mqtthandler.class.php b/inc/mqtthandler.class.php index 1e8d0316..e9ea4f08 100644 --- a/inc/mqtthandler.class.php +++ b/inc/mqtthandler.class.php @@ -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); @@ -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); } } diff --git a/install/installer.class.php b/install/installer.class.php index 5544b88d..82d8309c 100644 --- a/install/installer.class.php +++ b/install/installer.class.php @@ -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' => '', diff --git a/install/upgrade/update_to_dev.php b/install/upgrade/update_to_dev.php index ad05392d..cc703bc9 100644 --- a/install/upgrade/update_to_dev.php +++ b/install/upgrade/update_to_dev.php @@ -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']); diff --git a/tests/suite-unit/PluginFlyvemdmCommon.php b/tests/suite-unit/PluginFlyvemdmCommon.php index 923b4db8..4e67339c 100644 --- a/tests/suite-unit/PluginFlyvemdmCommon.php +++ b/tests/suite-unit/PluginFlyvemdmCommon.php @@ -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); + } + } } diff --git a/tpl/config-messagequeue.html.twig b/tpl/config-messagequeue.html.twig index 7c710834..cd3b17f5 100644 --- a/tpl/config-messagequeue.html.twig +++ b/tpl/config-messagequeue.html.twig @@ -20,6 +20,11 @@ min="1" max="65535"> {{ __('A port number between 1025 and 65535, standard port is 1883', 'flyvemdm') }} + + {{ __('MQTT prefix', 'flyvemdm') }} + + {{ __('Prepend all topics with a prefix (useful if a MQTT broker is mutualized). The mqtt daemon must be restarted.', 'flyvemdm') }} + {{ __('MQTT broker port for TLS', 'flyvemdm') }} Date: Mon, 30 Apr 2018 18:12:06 +0200 Subject: [PATCH 2/5] style(install): fix coding style Signed-off-by: Thierry Bugier --- tools/cli_install.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/cli_install.php b/tools/cli_install.php index 2783efa4..656a6eb2 100644 --- a/tools/cli_install.php +++ b/tools/cli_install.php @@ -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', From 36c1e27dd5301254bdfc1f70cca13829023266fd Mon Sep 17 00:00:00 2001 From: Domingo Oropeza Date: Tue, 15 May 2018 16:13:41 -0400 Subject: [PATCH 3/5] feat(config): lock mqtt prefix if new data is created Signed-off-by: Domingo Oropeza --- inc/agent.class.php | 4 ++++ inc/config.class.php | 7 +++++++ inc/fleet.class.php | 4 ++++ inc/invitation.class.php | 4 ++++ install/installer.class.php | 1 + tpl/config-messagequeue.html.twig | 8 ++++++-- 6 files changed, 26 insertions(+), 2 deletions(-) diff --git a/inc/agent.class.php b/inc/agent.class.php index 89f23304..453f63ee 100644 --- a/inc/agent.class.php +++ b/inc/agent.class.php @@ -545,6 +545,10 @@ public function prepareInputForUpdate($input) { public function post_addItem() { // Notify the agent about its fleets $this->updateSubscription(); + + if (PluginFlyvemdmConfig::canUpdate()) { + // Here we must update mqtt_prefix_locked + } } public function post_getFromDB() { diff --git a/inc/config.class.php b/inc/config.class.php index be609bd0..e82c72cc 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -530,4 +530,11 @@ public function isGlpiConfigured() { return true; } + + public function prepareInputForUpdate($input) { + if ($input['mqtt_prefix'] != $this->fields['mqtt_prefix'] && $this->fields['mqtt_prefix_locked'] == 0) { + $input['mqtt_prefix_locked'] = 1; + } + return $input; + } } diff --git a/inc/fleet.class.php b/inc/fleet.class.php index 463c4d22..716e7d97 100644 --- a/inc/fleet.class.php +++ b/inc/fleet.class.php @@ -302,6 +302,10 @@ public function post_addItem() { $topic = $this->getTopic(); $this->notify("$topic/Policy/$policyName", null, 0, 1); } + + if (PluginFlyvemdmConfig::canUpdate()) { + // Here we must update mqtt_prefix_locked + } } /** diff --git a/inc/invitation.class.php b/inc/invitation.class.php index 1c0524e3..398e4b26 100644 --- a/inc/invitation.class.php +++ b/inc/invitation.class.php @@ -270,6 +270,10 @@ public function post_addItem() { // Sent invitation email $this->sendInvitation(); + + if (PluginFlyvemdmConfig::canUpdate()) { + // Here we must update mqtt_prefix_locked + } } public function post_updateItem($history = 1) { diff --git a/install/installer.class.php b/install/installer.class.php index 82d8309c..4534bd26 100644 --- a/install/installer.class.php +++ b/install/installer.class.php @@ -608,6 +608,7 @@ protected function createInitialConfig() { 'mqtt_user' => self::BACKEND_MQTT_USER, 'mqtt_passwd' => $MdmMqttPassword, 'mqtt_prefix' => '', + 'mqtt_prefix_locked' => '0', 'instance_id' => $instanceId, 'registered_profiles_id' => '', 'guest_profiles_id' => '', diff --git a/tpl/config-messagequeue.html.twig b/tpl/config-messagequeue.html.twig index cd3b17f5..0c58243f 100644 --- a/tpl/config-messagequeue.html.twig +++ b/tpl/config-messagequeue.html.twig @@ -22,8 +22,12 @@ {{ __('MQTT prefix', 'flyvemdm') }} - - {{ __('Prepend all topics with a prefix (useful if a MQTT broker is mutualized). The mqtt daemon must be restarted.', 'flyvemdm') }} + + {{ __('Prepend all topics with a prefix (useful if a MQTT broker is mutualized). The mqtt daemon must be restarted after the change.', 'flyvemdm') }} +
+ {{ __('Note: This value can be changed only once and if no new invitation, agent or fleet has been created.', 'flyvemdm') }} + {{ __('MQTT broker port for TLS', 'flyvemdm') }} From f52f0f472817536e8b636cc69a948510a7265b2e Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Wed, 16 May 2018 11:32:11 +0200 Subject: [PATCH 4/5] feat(config): lock mqtt prefix setting Signed-off-by: Thierry Bugier --- inc/agent.class.php | 4 ---- inc/common.class.php | 2 +- inc/config.class.php | 17 ++++++++++------- inc/fleet.class.php | 4 ---- inc/invitation.class.php | 4 ---- install/installer.class.php | 1 - 6 files changed, 11 insertions(+), 21 deletions(-) diff --git a/inc/agent.class.php b/inc/agent.class.php index 453f63ee..89f23304 100644 --- a/inc/agent.class.php +++ b/inc/agent.class.php @@ -545,10 +545,6 @@ public function prepareInputForUpdate($input) { public function post_addItem() { // Notify the agent about its fleets $this->updateSubscription(); - - if (PluginFlyvemdmConfig::canUpdate()) { - // Here we must update mqtt_prefix_locked - } } public function post_getFromDB() { diff --git a/inc/common.class.php b/inc/common.class.php index 458523d6..dd3896d5 100644 --- a/inc/common.class.php +++ b/inc/common.class.php @@ -209,7 +209,7 @@ public static function getGlpiVersion() { } /** - * Removes the mqtt prefix from + * 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 diff --git a/inc/config.class.php b/inc/config.class.php index e82c72cc..1be03577 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -405,6 +405,16 @@ public static function configUpdate($input) { } } + //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) { @@ -530,11 +540,4 @@ public function isGlpiConfigured() { return true; } - - public function prepareInputForUpdate($input) { - if ($input['mqtt_prefix'] != $this->fields['mqtt_prefix'] && $this->fields['mqtt_prefix_locked'] == 0) { - $input['mqtt_prefix_locked'] = 1; - } - return $input; - } } diff --git a/inc/fleet.class.php b/inc/fleet.class.php index 716e7d97..463c4d22 100644 --- a/inc/fleet.class.php +++ b/inc/fleet.class.php @@ -302,10 +302,6 @@ public function post_addItem() { $topic = $this->getTopic(); $this->notify("$topic/Policy/$policyName", null, 0, 1); } - - if (PluginFlyvemdmConfig::canUpdate()) { - // Here we must update mqtt_prefix_locked - } } /** diff --git a/inc/invitation.class.php b/inc/invitation.class.php index 398e4b26..1c0524e3 100644 --- a/inc/invitation.class.php +++ b/inc/invitation.class.php @@ -270,10 +270,6 @@ public function post_addItem() { // Sent invitation email $this->sendInvitation(); - - if (PluginFlyvemdmConfig::canUpdate()) { - // Here we must update mqtt_prefix_locked - } } public function post_updateItem($history = 1) { diff --git a/install/installer.class.php b/install/installer.class.php index 4534bd26..82d8309c 100644 --- a/install/installer.class.php +++ b/install/installer.class.php @@ -608,7 +608,6 @@ protected function createInitialConfig() { 'mqtt_user' => self::BACKEND_MQTT_USER, 'mqtt_passwd' => $MdmMqttPassword, 'mqtt_prefix' => '', - 'mqtt_prefix_locked' => '0', 'instance_id' => $instanceId, 'registered_profiles_id' => '', 'guest_profiles_id' => '', From 0bfa768b0482b213eb6d6953ac7a57ad9a202646 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Wed, 16 May 2018 15:02:10 +0200 Subject: [PATCH 5/5] ci(travis): fix formatting Signed-off-by: Thierry Bugier --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index f000bddc..8e63fe80 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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