浏览代码

remove password from log output + fix crash when sudo password is empty

nico 8 年之前
父节点
当前提交
71da0931b7

+ 42 - 0
Tests/test_neuron_model.py

@@ -0,0 +1,42 @@
+import os
+import unittest
+
+from kalliope.core.Models import Neuron
+
+
+class TestNeuronModule(unittest.TestCase):
+
+    def test_password_parameter(self):
+        neuron_name = "test"
+        neuron_parameters = {
+            "password": "my secret",
+            "parameter": "test"
+        }
+
+        neuron = Neuron()
+        neuron.name = neuron_name
+        neuron.parameters = neuron_parameters
+
+        print neuron.__str__()
+        expected_result = "Neuron: name: test, parameters: {'password': '*****', 'parameter': 'test'}"
+
+        self.assertEqual(neuron.__str__(), expected_result)
+
+    def test_password_in_parameter(self):
+        neuron_name = "test"
+        neuron_parameters = {
+            "password_parameter": "my secret",
+            "parameter": "test"
+        }
+
+        neuron = Neuron()
+        neuron.name = neuron_name
+        neuron.parameters = neuron_parameters
+
+        print neuron.__str__()
+        expected_result = "Neuron: name: test, parameters: {'parameter': 'test', 'password_parameter': '*****'}"
+
+        self.assertEqual(neuron.__str__(), expected_result)
+
+if __name__ == '__main__':
+    unittest.main()

+ 15 - 1
kalliope/core/Models/Neuron.py

@@ -22,7 +22,21 @@ class Neuron(object):
         }
 
     def __str__(self):
-        return "Neuron: name: %s, parameters: %s" % (self.name, self.parameters)
+        """
+        Return a string that describe the neuron. If a parameter contains the word "password",
+        the output of this parameter will be masked in order to not appears in clean in the console
+        :return: string description of the neuron
+        """
+        returned_string = str()
+        returned_string += "Neuron: name: %s" % self.name
+        cleaned_parameters = dict()
+        for key, value in self.parameters.iteritems():
+            if "password" in key:
+                cleaned_parameters[key] = "*****"
+            else:
+                cleaned_parameters[key] = value
+        returned_string += ", parameters: %s" % cleaned_parameters
+        return returned_string
 
     def __eq__(self, other):
         """

+ 1 - 1
kalliope/core/NeuronLauncher.py

@@ -20,7 +20,7 @@ class NeuronLauncher:
         :type neuron: Neuron
         :return:
         """
-        logger.debug("Run plugin \"%s\" with parameters %s" % (neuron.name, neuron.parameters))
+        logger.debug("Run neuron: \"%s\"" % (neuron.__str__()))
         sl = SettingLoader()
         settings = sl.settings
         neuron_folder = None

+ 0 - 1
kalliope/core/NeuronModule.py

@@ -64,7 +64,6 @@ class NeuronModule(object):
         # get the child who called the class
         child_name = self.__class__.__name__
         self.neuron_name = child_name
-        logger.debug("NeuronModule called from class %s with parameters: %s" % (child_name, str(kwargs)))
 
         sl = SettingLoader()
         self.settings = sl.settings

+ 18 - 11
kalliope/core/ResourcesManager.py

@@ -95,8 +95,10 @@ class ResourcesManager(object):
                             # if the target_path exists, then run the install file within the new repository
                             if target_path is not None:
                                 self.install_file_path = target_path + os.sep + INSTALL_FILE_NAME
-                                self.run_ansible_playbook_module(install_file_path=self.install_file_path)
-                                Utils.print_success("Module: %s installed" % module_name)
+                                if self.run_ansible_playbook_module(install_file_path=self.install_file_path):
+                                    Utils.print_success("Module: %s installed" % module_name)
+                                else:
+                                    Utils.print_danger("Module: %s not installed" % module_name)
                 else:
                     logger.debug("[ResourcesManager] installation cancelled, deleting temp repo %s"
                                  % str(self.tmp_path))
@@ -218,7 +220,7 @@ class ResourcesManager(object):
         try:
             shutil.move(tmp_path, new_absolute_neuron_path)
             return new_absolute_neuron_path
-        except OSError:
+        except shutil.Error:
             # the folder already exist
             Utils.print_warning("The module %s already exist in the path %s" % (name, target_folder))
             # remove the cloned repo
@@ -237,14 +239,19 @@ class ResourcesManager(object):
         Utils.print_info("Starting neuron installation")
         # ask the sudo password
         pswd = getpass.getpass('Sudo password:')
-        ansible_neuron_parameters = {
-            "task_file": install_file_path,
-            "sudo": True,
-            "sudo_user": "root",
-            "sudo_password": pswd
-        }
-        neuron = Neuron(name="ansible_playbook", parameters=ansible_neuron_parameters)
-        NeuronLauncher.start_neuron(neuron)
+        if not pswd or pswd == "":
+            Utils.print_warning("You must enter a sudo password")
+            return False
+        else:
+            ansible_neuron_parameters = {
+                "task_file": install_file_path,
+                "sudo": True,
+                "sudo_user": "root",
+                "sudo_password": pswd
+            }
+            neuron = Neuron(name="ansible_playbook", parameters=ansible_neuron_parameters)
+            NeuronLauncher.start_neuron(neuron)
+            return True
 
     @staticmethod
     def _check_supported_version(current_version, supported_versions):

+ 0 - 1
kalliope/core/Utils/Utils.py

@@ -101,7 +101,6 @@ class Utils(object):
         :param resources_dir: the resource directory to check for external resources
         :return:
         """
-        logger.debug("Run plugin %s with parameter %s" % (module_name, parameters))
         package_path = "kalliope." + package_name + "." + module_name.lower() + "." + module_name.lower()
         if resources_dir is not None:
             neuron_resource_path = resources_dir + os.sep + module_name.lower() \