Beispiel #1
0
 def __init__(self):
     self.d = Dialog(dialog="dialog")
     self.logger = Logger()
     self.utils = Utils(self)
     self.elastic = Elastic(self)
     self.telegram = Telegram(self)
     self.configuration = Configuration(self)
     self.d.set_background_title("SNAP-TOOL")
Beispiel #2
0
class FormDialog:
    """
	Property that stores an object of type Dialog.
	"""
    d = None
    """
	Property that stores an object of type Utils.
	"""
    utils = None
    """
	Property that stores an object of type Elastic.
	"""
    elastic = None
    """
	Property that stores an object of type Logger.
	"""
    logger = None
    """
	Property that stores an object of type Telegram.
	"""
    telegram = None

    configuration = None
    """
	Constructor for the FormDialog class.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	"""
    def __init__(self):
        self.d = Dialog(dialog="dialog")
        self.logger = Logger()
        self.utils = Utils(self)
        self.elastic = Elastic(self)
        self.telegram = Telegram(self)
        self.configuration = Configuration(self)
        self.d.set_background_title("SNAP-TOOL")

    """
	Method that generates the menu interface.

	Parameters:
	self -- An instantiated object of the FormDialogs class.
	text -- Text displayed on the interface.
	options -- List of options that make up the menu.
	title -- Title displayed on the interface.

	Return:
	tag_menu -- Chosen option.
	"""

    def getMenu(self, text, options, title):
        code_menu, tag_menu = self.d.menu(text=text,
                                          choices=options,
                                          title=title)
        if code_menu == self.d.OK:
            return tag_menu
        if code_menu == self.d.CANCEL:
            exit(0)

    """
	Method that generates the interface for entering decimal or floating type data.

	Parameters:
	self -- An instantiated object of the FormDialogs class.
	text -- Text displayed on the interface.
	initial_value -- Default value shown on the interface.

	Return:
	tag_inputbox -- Decimal or float value entered.
	"""

    def getDataNumberDecimal(self, text, initial_value):
        decimal_reg_exp = re_compile(r'^[1-9](\.[0-9]+)?$')
        while True:
            code_inputbox, tag_inputbox = self.d.inputbox(text=text,
                                                          height=10,
                                                          width=50,
                                                          init=initial_value)
            if code_inputbox == self.d.OK:
                if (not self.utils.validateRegularExpression(
                        decimal_reg_exp, tag_inputbox)):
                    self.d.msgbox(
                        text=
                        "\nInvalid data entered. Required value (decimal or float).",
                        height=8,
                        width=50,
                        title="Error Message")
                else:
                    return tag_inputbox
            elif code_inputbox == self.d.CANCEL:
                self.mainMenu()

    """
	Method that generates an interface to enter an IP address.

	Parameters:
	self -- An instantiated object of the FormDialogs class.
	text -- Text displayed on the interface.
	initial_value -- Default value shown on the interface.

	Return:
	tag_inputbox -- IP address entered.
	"""

    def getDataIP(self, text, initial_value):
        ip_reg_exp = re_compile(
            r'^(?:(?:[1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^localhost$'
        )
        while True:
            code_inputbox, tag_inputbox = self.d.inputbox(text=text,
                                                          height=10,
                                                          width=50,
                                                          init=initial_value)
            if code_inputbox == self.d.OK:
                if (not self.utils.validateRegularExpression(
                        ip_reg_exp, tag_inputbox)):
                    self.d.msgbox(
                        text=
                        "\nInvalid data entered. Required value (IP address).",
                        height=8,
                        width=50,
                        title="Error Message")
                else:
                    return tag_inputbox
            elif code_inputbox == self.d.CANCEL:
                self.mainMenu()

    """
	Method that generates an interface to enter a port.

	Parameters:
	self -- An instantiated object of the FormDialogs class.
	text -- Text displayed on the interface.
	initial_value -- Default value shown on the interface.

	Return:
	tag_inputbox -- Port entered.
	"""

    def getDataPort(self, text, initial_value):
        port_reg_exp = re_compile(
            r'^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$'
        )
        while True:
            code_inputbox, tag_inputbox = self.d.inputbox(text=text,
                                                          height=10,
                                                          width=50,
                                                          init=initial_value)
            if code_inputbox == self.d.OK:
                if (not self.utils.validateRegularExpression(
                        port_reg_exp, tag_inputbox)):
                    self.d.msgbox(
                        text=
                        "\nInvalid data entered. Required value (0 - 65535).",
                        height=8,
                        width=50,
                        title="Error Message")
                else:
                    return tag_inputbox
            elif code_inputbox == self.d.CANCEL:
                self.mainMenu()

    """
	Method that generates an interface to enter text.

	Parameters:
	self -- An instantiated object of the FormDialogs class.
	text -- Text displayed on the interface.
	initial_value -- Default value shown on the interface.

	Return:
	tag_inputbox -- Text entered.
	"""

    def getDataInputText(self, text, initial_value):
        while True:
            code_inputbox, tag_inputbox = self.d.inputbox(text=text,
                                                          height=10,
                                                          width=50,
                                                          init=initial_value)
            if code_inputbox == self.d.OK:
                if tag_inputbox == "":
                    self.d.msgbox(
                        text=
                        "\nInvalid data entered. Required value (not empty).",
                        height=8,
                        width=50,
                        title="Error Message")
                else:
                    return tag_inputbox
            elif code_inputbox == self.d.CANCEL:
                self.mainMenu()

    """
	Method that generates an interface to enter a password.

	Parameters:
	self -- An instantiated object of the FormDialogs class.
	text -- Text displayed on the interface.
	initial_value -- Default value shown on the interface.

	Return:
	tag_passwordbox -- Password entered.
	"""

    def getDataPassword(self, text, initial_value):
        while True:
            code_passwordbox, tag_passwordbox = self.d.passwordbox(
                text=text,
                height=10,
                width=50,
                init=initial_value,
                insecure=True)
            if code_passwordbox == self.d.OK:
                if tag_passwordbox == "":
                    self.d.msgbox(
                        text=
                        "\nInvalid data entered. Required value (not empty).",
                        height=8,
                        width=50,
                        title="Error Message")
                else:
                    return tag_passwordbox
            elif code_passwordbox == self.d.CANCEL:
                self.mainMenu()

    """
	Method that generates a decision-making interface (yes / no).

	Parameters:
	self -- An instantiated object of the FormDialogs class.
	text -- Text displayed on the interface.
	title -- Title displayed on the interface.

	Return:
	tag_yesno -- Chosen option (yes or no).
	"""

    def getDataYesOrNo(self, text, title):
        tag_yesno = self.d.yesno(text=text, height=10, width=50, title=title)
        return tag_yesno

    """
	Method that generates an interface with several available options, and where only one of them can be chosen.

	Parameters:
	self -- An instantiated object of the FormDialogs class.
	text -- Text displayed on the interface.
	options -- List of options that make up the interface.
	title -- Title displayed on the interface.

	Return:
	tag_radiolist -- Chosen option.
	"""

    def getDataRadioList(self, text, options, title):
        while True:
            code_radiolist, tag_radiolist = self.d.radiolist(text=text,
                                                             width=65,
                                                             choices=options,
                                                             title=title)
            if code_radiolist == self.d.OK:
                if len(tag_radiolist) == 0:
                    self.d.msgbox(text="\nSelect at least one option.",
                                  height=7,
                                  width=50,
                                  title="Error Message")
                else:
                    return tag_radiolist
            elif code_radiolist == self.d.CANCEL:
                self.mainMenu()

    """
	Method that generates an interface with several available options, and where you can choose one or more of them.

	Parameters:
	self -- An instantiated object of the FormDialogs class.
	text -- Text displayed on the interface.
	options -- List of options that make up the interface.
	title -- Title displayed on the interface.

	Return:
	tag_checklist -- List with the chosen options.
	"""

    def getDataCheckList(self, text, options, title):
        while True:
            code_checklist, tag_checklist = self.d.checklist(text=text,
                                                             width=75,
                                                             choices=options,
                                                             title=title)
            if code_checklist == self.d.OK:
                if len(tag_checklist) == 0:
                    self.d.msgbox(text="\nSelect at least one option.",
                                  height=7,
                                  width=50,
                                  title="Error Message")
                else:
                    return tag_checklist
            elif code_checklist == self.d.CANCEL:
                self.mainMenu()

    """
	Method that generates an interface to select a file.

	Parameters:
	self -- An instantiated object of the FormDialogs class.
	initial_path -- Initial path in the interface.
	title -- Title displayed on the interface.
	extension_file -- Allowed file extension.

	Return:
	tag_fselect -- Path of the selected file.
	"""

    def getFile(self, initial_path, title, extension_file):
        while True:
            code_fselect, tag_fselect = self.d.fselect(filepath=initial_path,
                                                       height=8,
                                                       width=50,
                                                       title=title)
            if code_fselect == self.d.OK:
                if tag_fselect == "":
                    self.d.msgbox(text="\nSelect a file. Required value: " +
                                  extension_file + " file.",
                                  height=7,
                                  width=50,
                                  title="Error Message")
                else:
                    ext_file = Path(tag_fselect).suffix
                    if not ext_file == extension_file:
                        self.d.msgbox(
                            text="\nSelect a file. Required value: " +
                            extension_file + " file.",
                            height=7,
                            width=50,
                            title="Error Message")
                    else:
                        return tag_fselect
            elif code_fselect == self.d.CANCEL:
                self.mainMenu()

    """
	Method that generates an interface to select a directory.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	initial_path -- Initial path in the interface.
	title -- Title displayed on the interface.

	Return:
	tag_dselect -- Selected directory.
	"""

    def getDirectory(self, initial_path, title):
        while True:
            code_dselect, tag_dselect = self.d.dselect(filepath=initial_path,
                                                       height=8,
                                                       width=50,
                                                       title=title)
            if code_dselect == self.d.OK:
                if tag_dselect == "":
                    self.d.msgbox(
                        text=
                        "\nSelect a directory. Required value (not empty).",
                        height=7,
                        width=50,
                        title="Error Message")
                else:
                    return tag_dselect
            elif code_dselect == self.d.CANCEL:
                self.mainMenu()

    """
	Method that generates an interface with scroll box.

	Parameters:
	self -- An instantiated object of the FormDialogs class.
	text -- Text displayed on the interface.
	title -- Title displayed on the interface.
	"""

    def getScrollBox(self, text, title):
        code_scrollbox = self.d.scrollbox(text=text,
                                          height=15,
                                          width=70,
                                          title=title)

    """
	Method that defines the action to be performed on the Snap-Tool configuration file (creation or modification).

	Parameters:
	self -- An instantiated object of the FormDialog class.
	"""

    def defineConfiguration(self):
        options_conf_false = [("Create", "Create the configuration file", 0)]

        options_conf_true = [("Modify", "Modify the configuration file", 0)]

        if not path.exists(self.configuration.conf_file):
            opt_conf_false = self.getDataRadioList("Select a option:",
                                                   options_conf_false,
                                                   "Configuration Options")
            if opt_conf_false == "Create":
                self.configuration.createConfiguration()
        else:
            opt_conf_true = self.getDataRadioList("Select a option:",
                                                  options_conf_true,
                                                  "Configuration Options")
            if opt_conf_true == "Modify":
                self.configuration.updateConfiguration()

    """
	Method that creates a repository of type FS in ElasticSearch.

	Parameters:
	self -- An instantiated object of the FormDialog class.

	Exceptions:
	KeyError -- A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary (dict).
	"""

    def createRepository(self):
        try:
            if not path.exists(self.configuration.conf_file):
                self.d.msgbox(text="\nConfiguration file not found.",
                              height=7,
                              width=50,
                              title="Error Message")
            else:
                repository_name = self.getDataInputText(
                    "Enter the name to be assigned to the repository:",
                    "repository_name")
                path_repository = self.getDirectory("/etc/Snap-Tool",
                                                    "Repository path")
                compress_repository = self.getDataYesOrNo(
                    "\nDo you require metadata files to be stored compressed?",
                    "Repository compression")
                if compress_repository == "ok":
                    compress_repository = True
                else:
                    compress_repository = False
                snap_tool_conf = self.utils.readYamlFile(
                    self.configuration.conf_file, 'r')
                conn_es = self.elastic.getConnectionElastic()
                self.elastic.createRepositoryFS(conn_es, repository_name,
                                                path_repository,
                                                compress_repository)
                message_create_repository = self.telegram.getMessageCreateRepository(
                    repository_name, path_repository, compress_repository)
                self.telegram.sendTelegramAlert(
                    self.utils.decryptAES(
                        snap_tool_conf['telegram_chat_id']).decode('utf-8'),
                    self.utils.decryptAES(
                        snap_tool_conf['telegram_bot_token']).decode('utf-8'),
                    message_create_repository)
                self.logger.createSnapToolLog(
                    "Repository created: " + repository_name, 1)
                self.d.msgbox(text="\nRepository created: " + repository_name,
                              height=7,
                              width=50,
                              title="Notification Message")
                conn_es.transport.close()
            self.mainMenu()
        except KeyError as exception:
            self.logger.createSnapToolLog("Key Error: " + exception, 3)
            self.d.msgbox(
                text=
                "\nFailed to create snapshot. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.mainMenu()

    """
	Method that removes one or more FS type repositories in ElasticSearch.

	Parameters:
	self -- An instantiated object of the FormDialog class.

	Exceptions:
	KeyError -- A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary (dict).
	"""

    def deleteRepository(self):
        try:
            if not path.exists(self.configuration.conf_file):
                self.d.msgbox(text="\nConfiguration file not found.",
                              height=7,
                              width=50,
                              title="Error Message")
            else:
                conn_es = self.elastic.getConnectionElastic()
                list_aux_repositories = self.elastic.getAllRepositories(
                    conn_es)
                if len(list_aux_repositories) == 0:
                    self.d.msgbox(text="\nThere are no repositories created.",
                                  height=7,
                                  width=50,
                                  title="Notification Message")
                else:
                    snap_tool_conf = self.utils.readYamlFile(
                        self.configuration.conf_file, 'r')
                    list_all_repositories = self.utils.convertListToCheckOrRadioList(
                        list_aux_repositories, "Repository Name")
                    opt_repos = self.getDataCheckList(
                        "Select one or more options:", list_all_repositories,
                        "Repositories")
                    confirm_delete_repos = self.getDataYesOrNo(
                        "\nAre you sure to delete the following repository(s)?",
                        "Delete repositories")
                    if confirm_delete_repos == "ok":
                        message_to_display = "\nDeleted repositories:\n\n"
                        for repo_name in opt_repos:
                            self.elastic.deleteRepositoryFS(conn_es, repo_name)
                            message_to_display += "\n- " + repo_name
                            message_delete_repository = self.telegram.getMessageDeleteRepository(
                                repo_name)
                            self.telegram.sendTelegramAlert(
                                self.utils.decryptAES(
                                    snap_tool_conf['telegram_chat_id']).decode(
                                        'utf-8'),
                                self.utils.decryptAES(
                                    snap_tool_conf['telegram_bot_token']).
                                decode('utf-8'), message_delete_repository)
                            self.logger.createSnapToolLog(
                                "Deleted repository: " + repo_name, 2)
                        self.getScrollBox(message_to_display,
                                          "Deleted repositories")
                conn_es.transport.close()
            self.mainMenu()
        except KeyError as exception:
            self.logger.createSnapToolLog("Key Error: " + exception, 3)
            self.d.msgbox(
                text=
                "\nFailed to delete repository. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.mainMenu()

    """
	Method that creates a snapshot of a particular index and allows the index to be deleted or not.

	Parameters:
	self -- An instantiated object of the FormDialog class.

	Exceptions:
	KeyError -- A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary (dict). 
	"""

    def createSnapshot(self):
        try:
            if not path.exists(self.configuration.conf_file):
                self.d.msgbox(text="\nConfiguration file not found.",
                              height=7,
                              width=50,
                              title="Error Message")
            else:
                conn_es = self.elastic.getConnectionElastic()
                list_aux_indices = self.elastic.getIndices(conn_es)
                if len(list_aux_indices) == 0:
                    self.d.msgbox(text="\nThere are no indexes to back up.",
                                  height=7,
                                  width=50,
                                  title="Notification Message")
                else:
                    list_all_indices = self.utils.convertListToCheckOrRadioList(
                        list_aux_indices, "Index name")
                    opt_index = self.getDataRadioList("Select a option:",
                                                      list_all_indices,
                                                      "Indices")
                    list_aux_repositories = self.elastic.getAllRepositories(
                        conn_es)
                    if len(list_aux_repositories) == 0:
                        self.d.msgbox(text="\nThere are no repositories.",
                                      height=7,
                                      width=50,
                                      title="Notification Message")
                    else:
                        snap_tool_conf = self.utils.readYamlFile(
                            self.configuration.conf_file, 'r')
                        list_all_repositories = self.utils.convertListToCheckOrRadioList(
                            list_aux_repositories, "Repository name")
                        opt_repo = self.getDataRadioList(
                            "Select a option:", list_all_repositories,
                            "Repositories")
                        self.elastic.createSnapshot(conn_es, opt_repo,
                                                    opt_index)
                        message_creation_start = self.telegram.getMessageStartCreationSnapshot(
                            opt_index, opt_repo)
                        self.telegram.sendTelegramAlert(
                            self.utils.decryptAES(
                                snap_tool_conf['telegram_chat_id']).decode(
                                    'utf-8'),
                            self.utils.decryptAES(
                                snap_tool_conf['telegram_bot_token']).decode(
                                    'utf-8'), message_creation_start)
                        self.logger.createSnapToolLog(
                            "Snapshot creation has started: " + opt_index, 1)
                        while True:
                            status_snapshot = self.elastic.getStatusSnapshot(
                                conn_es, opt_repo, opt_index)
                            if status_snapshot == "SUCCESS":
                                break
                            sleep(60)
                        snapshot_info = self.elastic.getSnapshotInfo(
                            conn_es, opt_repo, opt_index)
                        self.logger.createSnapToolLog(
                            "Snapshot creation has finished: " + opt_index, 1)
                        message_creation_end = self.telegram.getMessageEndSnapshot(
                            opt_index, opt_repo,
                            snapshot_info['snapshots'][0]['start_time'],
                            snapshot_info['snapshots'][0]['end_time'])
                        self.telegram.sendTelegramAlert(
                            self.utils.decryptAES(
                                snap_tool_conf['telegram_chat_id']).decode(
                                    'utf-8'),
                            self.utils.decryptAES(
                                snap_tool_conf['telegram_bot_token']).decode(
                                    'utf-8'), message_creation_end)
                        self.d.msgbox(text="\nSnapshot created: " + opt_index,
                                      height=7,
                                      width=50,
                                      title="Notification Message")
                        if snap_tool_conf['is_delete_index'] == True:
                            self.elastic.deleteIndex(conn_es, opt_index)
                            if not conn_es.indices.exists(opt_index):
                                self.logger.createSnapToolLog(
                                    "Index removed: " + opt_index, 1)
                                message_delete_index = self.telegram.getMessageDeleteIndex(
                                    opt_index)
                                self.telegram.sendTelegramAlert(
                                    self.utils.decryptAES(
                                        snap_tool_conf['telegram_chat_id']).
                                    decode('utf-8'),
                                    self.utils.decryptAES(
                                        snap_tool_conf['telegram_bot_token']).
                                    decode('utf-8'), message_delete_index)
                                self.d.msgbox(text="\nIndex removed: " +
                                              opt_index,
                                              height=7,
                                              width=50,
                                              title="Notification Message")
                        else:
                            delete_index = self.getDataYesOrNo(
                                "\nDo you want to delete the index?\n\n- " +
                                opt_index, "Delete Index")
                            if delete_index == "ok":
                                self.elastic.deleteIndex(conn_es, opt_index)
                                if not conn_es.indices.exists(opt_index):
                                    self.logger.createSnapToolLog(
                                        "Index removed: " + opt_index, 1)
                                    message_delete_index = self.telegram.getMessageDeleteIndex(
                                        opt_index)
                                    self.telegram.sendTelegramAlert(
                                        self.utils.decryptAES(
                                            snap_tool_conf['telegram_chat_id']
                                        ).decode('utf-8'),
                                        self.utils.decryptAES(snap_tool_conf[
                                            'telegram_bot_token']).decode(
                                                'utf-8'), message_delete_index)
                                    self.d.msgbox(text="\nIndex removed: " +
                                                  opt_index,
                                                  height=7,
                                                  width=50,
                                                  title="Notification Message")
                conn_es.transport.close()
            self.mainMenu()
        except KeyError as exception:
            self.logger.createSnapToolLog("Key Error: " + exception, 3)
            self.d.msgbox(
                text=
                "\nFailed to create snapshot. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.mainMenu()

    """
	Method that removes one or more snapshots from ElasticSearch.

	Parameters:
	self -- An instantiated object of the FormDialog class.

	Exceptions:
	KeyError -- A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary (dict). 
	"""

    def deleteSnapshot(self):
        try:
            if not path.exists(self.configuration.conf_file):
                self.d.msgbox(text="\nConfiguration file not found.",
                              height=7,
                              width=50,
                              title="Error Message")
            else:
                conn_es = self.elastic.getConnectionElastic()
                list_aux_repositories = self.elastic.getAllRepositories(
                    conn_es)
                if len(list_aux_repositories) == 0:
                    self.d.msgbox(text="\nThere are no repositories created.",
                                  height=7,
                                  width=50,
                                  title="Notification Message")
                else:
                    list_all_repositories = self.utils.convertListToCheckOrRadioList(
                        list_aux_repositories, "Repository Name")
                    opt_repo = self.getDataRadioList("Select a option:",
                                                     list_all_repositories,
                                                     "Repositories")
                    list_aux_snapshots = self.elastic.getAllSnapshots(
                        conn_es, opt_repo)
                    if len(list_aux_snapshots) == 0:
                        self.d.msgbox(text="\nThere are no snapshots created.",
                                      height=7,
                                      width=50,
                                      title="Notification Message")
                    else:
                        snap_tool_conf = self.utils.readYamlFile(
                            self.configuration.conf_file, 'r')
                        list_all_snapshots = self.utils.convertListToCheckOrRadioList(
                            list_aux_snapshots, "Snapshot Name")
                        opt_snapshots = self.getDataCheckList(
                            "Select one or more options:", list_all_snapshots,
                            "Snapshots")
                        delete_snapshot = self.getDataYesOrNo(
                            "\nAre you sure to delete the selected snapshot(s)?",
                            "Delete Snapshot(s)")
                        if delete_snapshot == "ok":
                            for snapshot in opt_snapshots:
                                self.elastic.deleteSnapshotElastic(
                                    conn_es, opt_repo, snapshot)
                                message_delete_snapshot = self.telegram.getMessageDeleteSnapshot(
                                    snapshot, opt_repo)
                                self.telegram.sendTelegramAlert(
                                    self.utils.decryptAES(
                                        snap_tool_conf['telegram_chat_id']).
                                    decode('utf-8'),
                                    self.utils.decryptAES(
                                        snap_tool_conf['telegram_bot_token']).
                                    decode('utf-8'), message_delete_snapshot)
                            message = "\nThe following snapshots were removed:\n\n"
                            message += self.utils.convertListToString(
                                opt_snapshots)
                            self.getScrollBox(message, "Snapshot(s) deleted")
                conn_es.transport.close()
            self.mainMenu()
        except KeyError as exception:
            self.logger.createSnapToolLog("Key Error: " + exception, 3)
            self.d.msgbox(
                text=
                "\nFailed to delete snapshot(s). For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.mainMenu()

    """
	Method that restores a particular snapshot from ElasticSearch.

	Parameters:
	self -- An instantiated object of the FormDialog class.

	Exceptions:
	KeyError -- A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary (dict).
	"""

    def restoreSnapshot(self):
        try:
            if not path.exists(self.configuration.conf_file):
                self.d.msgbox(text="\nConfiguration file not found.",
                              height=7,
                              width=50,
                              title="Error Message")
            else:
                conn_es = self.elastic.getConnectionElastic()
                list_aux_repositories = self.elastic.getAllRepositories(
                    conn_es)
                if len(list_aux_repositories) == 0:
                    self.d.msgbox(text="\nThere are no repositories created.",
                                  height=7,
                                  width=50,
                                  title="Notification Message")
                else:
                    list_all_repositories = self.utils.convertListToCheckOrRadioList(
                        list_aux_repositories, "Repository Name")
                    opt_repo = self.getDataRadioList("Select a option:",
                                                     list_all_repositories,
                                                     "Repositories")
                    list_aux_snapshots = self.elastic.getAllSnapshots(
                        conn_es, opt_repo)
                    if len(list_aux_snapshots) == 0:
                        self.d.msgbox(text="\nThere are no snapshots created.",
                                      height=7,
                                      width=50,
                                      title="Notification Message")
                    else:
                        snap_tool_conf = self.utils.readYamlFile(
                            self.configuration.conf_file, 'r')
                        list_all_snapshots = self.utils.convertListToCheckOrRadioList(
                            list_aux_snapshots, "Snapshot Name")
                        opt_snapshot = self.getDataRadioList(
                            "Select a option:", list_all_snapshots,
                            "Snapshots")
                        self.elastic.restoreSnapshot(conn_es, opt_repo,
                                                     opt_snapshot)
                        message_restore_snapshot = self.telegram.getMessageRestoreSnapshot(
                            opt_repo, opt_snapshot)
                        self.telegram.sendTelegramAlert(
                            self.utils.decryptAES(
                                snap_tool_conf['telegram_chat_id']).decode(
                                    'utf-8'),
                            self.utils.decryptAES(
                                snap_tool_conf['telegram_bot_token']).decode(
                                    'utf-8'), message_restore_snapshot)
                        self.logger.createSnapToolLog(
                            "Snapshot restored: " + opt_snapshot, 1)
                        self.d.msgbox(text="\nSnapshot restored: " +
                                      opt_snapshot + '.',
                                      height=7,
                                      width=50,
                                      title="Notification Message")
                conn_es.transport.close()
            self.mainMenu()
        except KeyError as exception:
            self.logger.createSnapToolLog("Key Error: " + exception, 3)
            self.d.msgbox(
                text=
                "\nFailed to restore snapshot. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.mainMenu()

    """
	Method that mounts a snapshot as a searchable snapshot in ElasticSearch.

	Parameters:
	self -- An instantiated object of the FormDialog class.

	Exceptions:
	KeyError -- A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary (dict).
	"""

    def mountSearchableSnapshot(self):
        try:
            if not path.exists(self.configuration.conf_file):
                self.d.msgbox(text="\nConfiguration file not found.",
                              height=7,
                              width=50,
                              title="Error Message")
            else:
                conn_es = self.elastic.getConnectionElastic()
                list_aux_repositories = self.elastic.getAllRepositories(
                    conn_es)
                if len(list_aux_repositories) == 0:
                    self.d.msgbox(text="\nThere are no repositories created.",
                                  height=7,
                                  width=50,
                                  title="Notification Message")
                else:
                    list_all_repositories = self.utils.convertListToCheckOrRadioList(
                        list_aux_repositories, "Repository Name")
                    opt_repo = self.getDataRadioList("Select a option:",
                                                     list_all_repositories,
                                                     "Repositories")
                    list_aux_snapshots = self.elastic.getAllSnapshots(
                        conn_es, opt_repo)
                    if len(list_aux_snapshots) == 0:
                        self.d.msgbox(text="\nThere are no snapshots created.",
                                      height=7,
                                      width=50,
                                      title="Notification Message")
                    else:
                        snap_tool_conf = self.utils.readYamlFile(
                            self.configuration.conf_file, 'r')
                        list_all_snapshots = self.utils.convertListToCheckOrRadioList(
                            list_aux_snapshots, "Snapshot Name")
                        opt_snapshot = self.getDataRadioList(
                            "Select a option:", list_all_snapshots,
                            "Snapshots")
                        self.elastic.mountSearchableSnapshot(
                            conn_es, opt_repo, opt_snapshot)
                        message_searchable_snapshot = self.telegram.getMessageSearchableSnapshot(
                            opt_repo, opt_snapshot)
                        self.telegram.sendTelegramAlert(
                            self.utils.decryptAES(
                                snap_tool_conf['telegram_chat_id']).decode(
                                    'utf-8'),
                            self.utils.decryptAES(
                                snap_tool_conf['telegram_bot_token']).decode(
                                    'utf-8'), message_searchable_snapshot)
                        self.logger.createSnapToolLog(
                            "Snapshot mounted as searchable snapshot: " +
                            opt_snapshot, 1)
                        self.d.msgbox(
                            text="\nSnapshot mounted as searchable snapshot: "
                            + opt_snapshot + '.',
                            height=8,
                            width=50,
                            title="Notification Message")
                conn_es.transport.close()
            self.mainMenu()
        except KeyError as exception:
            self.logger.createSnapToolLog("Key Error: " + exception, 3)
            self.d.msgbox(
                text=
                "\nFailed to mount snapshot as a searchable snapshot. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.mainMenu()

    """
	Method that removes one or more indexes in ElasticSearch.

	Parameters:
	self -- An instantiated object of the FormDialog class.

	Exceptions:
	KeyError -- A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary (dict).
	"""

    def deleteIndices(self):
        try:
            if not path.exists(self.configuration.conf_file):
                self.d.msgbox(text="\nConfiguration file not found.",
                              height=7,
                              width=50,
                              title="Error Message")
            else:
                conn_es = self.elastic.getConnectionElastic()
                list_aux_indices = self.elastic.getIndices(conn_es)
                if len(list_aux_indices) == 0:
                    self.d.msgbox(text="\nThere are no indexes to remove.",
                                  height=7,
                                  width=50,
                                  title="Notification Message")
                else:
                    list_all_indices = self.utils.convertListToCheckOrRadioList(
                        list_aux_indices, "Index name")
                    opt_indices = self.getDataCheckList(
                        "Select a option:", list_all_indices, "Indices")
                    confirm_delete_indices = self.getDataYesOrNo(
                        "\nAre you sure to delete the selected indices?",
                        "Delete indices")
                    if confirm_delete_indices == "ok":
                        snap_tool_conf = self.utils.readYamlFile(
                            self.configuration.conf_file, 'r')
                        message_to_display = "\nIndices removed:\n"
                        for index_name in opt_indices:
                            self.elastic.deleteIndex(conn_es, index_name)
                            message_to_display += "\n- " + index_name
                            self.logger.createSnapToolLog(
                                "Index removed: " + index_name, 2)
                            message_delete_indices = self.telegram.getMessageDeleteIndex(
                                index_name)
                            self.telegram.sendTelegramAlert(
                                self.utils.decryptAES(
                                    snap_tool_conf['telegram_chat_id']).decode(
                                        'utf-8'),
                                self.utils.decryptAES(
                                    snap_tool_conf['telegram_bot_token']).
                                decode('utf-8'), message_delete_indices)
                        self.getScrollBox(message_to_display,
                                          "Indices Removed")
                conn_es.transport.close()
            self.mainMenu()
        except KeyError as exception:
            self.logger.createSnapToolLog("Key Error: " + exception, 3)
            self.d.msgbox(
                text=
                "\nFailed to delete indices. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.mainMenu()

    """
	Method that displays information related to the percentage of occupied disk space of the nodes of the elasticsearch cluster.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	"""

    def showNodesDiskSpace(self):
        message_to_display = "Occupied space in nodes:\n\n"
        conn_es = self.elastic.getConnectionElastic()
        nodes_info = self.elastic.getNodesInformation(conn_es)
        for node in nodes_info:
            message_to_display += "- " + nodes_info[node]['name'] + '\n'
            total_disk = nodes_info[node]['fs']['total']['total_in_bytes']
            available_disk = nodes_info[node]['fs']['total'][
                'available_in_bytes']
            percentage = 100 - (available_disk * 100 / total_disk)
            message_to_display += "Percent occupied on disk: " + str(
                round(percentage, 2)) + "%\n\n"
        conn_es.transport.close()
        self.getScrollBox(message_to_display, "Node Information")
        self.mainMenu()

    """
	Method that displays a message on the screen with information about the application.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	"""

    def getAbout(self):
        message = "\nCopyright@2021 Tekium. All rights reserved.\nSnap-Tool v3.1\nAuthor: Erick Rodriguez\nEmail: [email protected], [email protected]\n" + "License: GPLv3\n\nSnap-Tool is a tool that allows the management of snaphots in\nElasticSearch through a graphical interface."
        self.getScrollBox(message, "About")
        self.mainMenu()

    """
	Method that launches an action based on the option chosen in the main menu.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	option -- Chosen option.
	"""

    def switchMmenu(self, option):
        if option == 1:
            self.defineConfiguration()
        elif option == 2:
            self.repositoryMenu()
        elif option == 3:
            self.snapshotMenu()
        elif option == 4:
            self.indicesMenu()
        elif option == 5:
            self.showNodesDiskSpace()
        elif option == 6:
            self.getAbout()
        elif option == 7:
            exit(0)

    """
	Method that launches an action based on the option chosen in the Repositories menu.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	option -- Chosen option.
	"""

    def switchRmenu(self, option):
        if option == 1:
            self.createRepository()
        elif option == 2:
            self.deleteRepository()

    """
	Method that launches an action based on the option chosen in the Snapshots menu.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	option -- Chosen option.
	"""

    def switchSmenu(self, option):
        if option == 1:
            self.createSnapshot()
        elif option == 2:
            self.deleteSnapshot()
        elif option == 3:
            self.restoreSnapshot()
        elif option == 4:
            self.mountSearchableSnapshot()

    """
	Method that launches an action based on the option chosen in the Indices menu.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	option -- Chosen option.
	"""

    def switchImenu(self, option):
        if option == 1:
            self.deleteIndices()

    """
	Method that defines the menu on the actions to be carried out in the main menu.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	"""

    def mainMenu(self):
        options_mm = [("1", "Snap-Tool Configuration"), ("2", "Repositories"),
                      ("3", "Snapshots"), ("4", "Indices"),
                      ("5", "Nodes information"), ("6", "About"),
                      ("7", "Exit")]

        option_mm = self.getMenu("Select a option:", options_mm, "Main Menu")
        self.switchMmenu(int(option_mm))

    """
	Method that defines the menu of actions that can be performed in relation to repositories.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	"""

    def repositoryMenu(self):
        options_rm = [("1", "Create repository"), ("2", "Delete repositories")]

        option_rm = self.getMenu("Select a option:", options_rm,
                                 "Repositories menu")
        self.switchRmenu(int(option_rm))

    """
	Method that defines the menu of actions that can be performed in relation to snaphosts.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	"""

    def snapshotMenu(self):
        options_sm = [("1", "Create Snapshot"), ("2", "Delete Snapshot(s)"),
                      ("3", "Restore snapshot"),
                      ("4", "Mount searchable snapshot")]

        option_sm = self.getMenu("Select a option:", options_sm,
                                 "Snapshots Menu")
        self.switchSmenu(int(option_sm))

    """
	Method that defines the menu of actions that can be performed in relation to indices.

	Parameters:
	self -- An instantiated object of the FormDialog class.
	"""

    def indicesMenu(self):
        options_im = [("1", "Delete indices")]

        option_im = self.getMenu("Select a option:", options_im,
                                 "Indices Menu")
        self.switchImenu(int(option_im))
Beispiel #3
0
class Telegram:
    """
	Property that stores an object of type Utils.
	"""
    utils = None
    """
	Property that stores an object of type Logger.
	"""
    logger = None
    """
	Constructor for the Telegram class.

	Parameters:
	self -- An instantiated object of the Telegram class.
	form_dialog -- FormDialog class object.
	"""
    def __init__(self, form_dialog):
        self.logger = Logger()
        self.utils = Utils(form_dialog)

    """
	Method that sends the alert to the telegram channel.

	Parameters:
	self -- Instance object.
	telegram_chat_id -- Telegram channel identifier to which the letter will be sent.
	telegram_bot_token -- Token of the Telegram bot that is the administrator of the Telegram channel to which the alerts will be sent.
	message -- Message to be sent to the Telegram channel.
	"""

    def sendTelegramAlert(self, telegram_chat_id, telegram_bot_token, message):
        if len(message) > 4096:
            message = "The size of the message in Telegram (4096) has been exceeded. Overall size: " + str(
                len(message))
        c = Curl()
        url = 'https://api.telegram.org/bot' + str(
            telegram_bot_token) + '/sendMessage'
        c.setopt(c.URL, url)
        data = {'chat_id': telegram_chat_id, 'text': message}
        pf = urlencode(data)
        c.setopt(c.POSTFIELDS, pf)
        c.perform_rs()
        status_code = c.getinfo(HTTP_CODE)
        c.close()
        self.getStatusByTelegramCode(status_code)

    """
	Method that creates the header of the message that will be sent to Telegram.

	Parameters:
	self -- An instantiated object of the Telegram class.

	Return:
	header -- Header of the message.
	"""

    def getHeaderMessage(self):
        header = u'\u26A0\uFE0F' + " " + 'Snap-Tool' + " " + u'\u26A0\uFE0F' + "\n\n" + u'\u23F0' + " Alert sent: " + strftime(
            "%c") + "\n\n\n"
        return header

    """
	Method that generates the message in Telegram for when a repository is created.

	Parameters:
	self -- An instantiated object of the Telegram class.
	repository_name -- Name of the repository created.
	path_repository -- Path of the repository created.
	compress_repository -- Whether the repository will have compression or not.

	Return:
	message -- Message to send.
	"""

    def getMessageCreateRepository(self, repository_name, path_repository,
                                   compress_repository):
        message = self.getHeaderMessage()
        message += u'\u2611\uFE0F' + " Action: Repository created\n"
        message += u'\u2611\uFE0F' + " Repository name: " + repository_name + '\n'
        message += u'\u2611\uFE0F' + " Repository path: " + path_repository + '\n'
        message += u'\u2611\uFE0F' + " Repository compression: " + str(
            compress_repository)
        return message

    """
	Method that generates the message in Telegram for when a repository is deleted.

	Parameters:
	self -- An instantiated object of the Telegram class.
	repository_name -- Deleted repository name.

	Return:
	message -- Message to send.
	"""

    def getMessageDeleteRepository(self, repository_name):
        message = self.getHeaderMessage()
        message += u'\u2611\uFE0F' + " Action: Repository deleted\n"
        message += u'\u2611\uFE0F' + " Repository name: " + repository_name
        return message

    """
	Method that generates the message in Telegram for when a snapshot is deleted.

	Parameters:
	self -- An instantiated object of the Telegram class.
	snapshot_name -- Name of the deleted snapshot.
	repository_name -- Name of the repository where the snapshot was hosted.

	Return:
	message -- Message to send.
	"""

    def getMessageDeleteSnapshot(self, snapshot_name, repository_name):
        message = self.getHeaderMessage()
        message += u'\u2611\uFE0F' + " Action: Snaphot removed\n"
        message += u'\u2611\uFE0F' + " Snapshot name: " + snapshot_name + '\n'
        message += u'\u2611\uFE0F' + " Repository name: " + repository_name
        return message

    """
	Method that generates the message in Telegram for when a snapshot is restored.

	Parameters:
	self -- An instantiated object of the Telegram class.
	repository_name -- Name of the repository where the snapshot to be restored is hosted.
	snapshot_name -- Name of the restored snapshot.

	Return:
	message -- Message to send.
	"""

    def getMessageRestoreSnapshot(self, repository_name, snapshot_name):
        message = self.getHeaderMessage()
        message += u'\u2611\uFE0F' + " Action: Snapshot restore\n"
        message += u'\u2611\uFE0F' + " Snapshot name: " + snapshot_name + '\n'
        message += u'\u2611\uFE0F' + " Repository name: " + repository_name
        return message

    """
	Method that generates the message in Telegram for when a snapshot is mounted as a searchable snapshot.

	Parameters:
	self -- An instantiated object of the Telegram class.
	repository_name -- Name of the repository where the snapshot that will be mounted as a searchable snapshot is stored.
	snapshot_name -- Name of the snapshot to be mounted as a searchable snapshot.

	Return:
	message -- Message to send.
	"""

    def getMessageSearchableSnapshot(self, repository_name, snapshot_name):
        message = self.getHeaderMessage()
        message += u'\u2611\uFE0F' + " Action: Snapshot mounted as a searchable snapshot\n"
        message += u'\u2611\uFE0F' + " Snapshot name: " + snapshot_name + '\n'
        message += u'\u2611\uFE0F' + " Repository name: " + repository_name
        return message

    """
	Method that generates the message in Telegram for when the creation of a snapshot has begun.

	Parameters:
	self -- An instantiated object of the Telegram class.
	index_name -- Name of the index that will be saved in the snapshot.
	repository_name -- Name of the repository where the snapshot will be saved.

	Return:
	message -- Message to send.
	"""

    def getMessageStartCreationSnapshot(self, index_name, repository_name):
        message = self.getHeaderMessage()
        message += u'\u2611\uFE0F' + " Action: Snapshot creation has started\n"
        message += u'\u2611\uFE0F' + " Snapshot name: " + index_name + '\n'
        message += u'\u2611\uFE0F' + " Index name: " + index_name + '\n'
        message += u'\u2611\uFE0F' + " Repository name: " + repository_name
        return message

    """
	Method that generates the message in Telegram for when the creation of a snapshot has finished.

	Parameters:
	self -- An instantiated object of the Telegram class.
	snapshot_name -- Name of the snapshot created.
	repository_name -- Name of the repository where the snapshot was saved.
	start_time -- Time when snapshot creation started.
	end_time -- Time when snapshot creation finished.

	Return:
	message -- Message to send.
	"""

    def getMessageEndSnapshot(self, snapshot_name, repository_name, start_time,
                              end_time):
        message = self.getHeaderMessage()
        message += u'\u2611\uFE0F' + " Action: Snapshot creation completed\n"
        message += u'\u2611\uFE0F' + " Snapshot name: " + snapshot_name + '\n'
        message += u'\u2611\uFE0F' + " Repository name: " + repository_name + '\n'
        message += u'\u2611\uFE0F' + " Start time: " + str(start_time) + '\n'
        message += u'\u2611\uFE0F' + " End time: " + str(end_time)
        return message

    """
	Method that generates the message in Telegram for when an index is eliminated.

	Parameters:
	self -- An instantiated object of the Telegram class.
	index_name -- Index name removed.

	Return:
	message -- Message to send.
	"""

    def getMessageDeleteIndex(self, index_name):
        message = self.getHeaderMessage()
        message += u'\u2611\uFE0F' + " Action: Index removed\n"
        message += u'\u2611\uFE0F' + " Index name: " + index_name
        return message

    """
	Method that prints the status of the alert delivery based on the response HTTP code.

	Parameters:
	self -- An instantiated object of the Telegram class.
	telegram_code -- HTTP code in response to the request made to Telegram.
	"""

    def getStatusByTelegramCode(self, telegram_code):
        if telegram_code == 200:
            self.logger.createSnapToolLog("Telegram message sent.", 1)
        elif telegram_code == 400:
            self.logger.createSnapToolLog(
                "Telegram message not sent. Status: Bad request.", 3)
        elif telegram_code == 401:
            self.logger.createSnapToolLog(
                "Telegram message not sent. Status: Unauthorized.", 3)
        elif telegram_code == 404:
            self.logger.createSnapToolLog(
                "Telegram message not sent. Status: Not found.", 3)
Beispiel #4
0
 def __init__(self, form_dialog):
     self.logger = Logger()
     self.utils = Utils(form_dialog)
Beispiel #5
0
	def __init__(self, form_dialog):
		self.logger = Logger()
		self.form_dialog = form_dialog
		self.passphrase = self.getPassphrase()
Beispiel #6
0
class Utils:
	"""
	Property that saves the passphrase that will be used for the decryption process.
	"""
	passphrase = None

	"""
	Property that stores an object of type Logger.
	"""
	logger = None

	"""
	Property that stores an object of type FormDialog.
	"""
	form_dialog = None

	"""
	Constructor for the Utils class.

	Parameters:
	self -- An instantiated object of the Utils class.
	form_dialog -- FormDialog class object.
	"""
	def __init__(self, form_dialog):
		self.logger = Logger()
		self.form_dialog = form_dialog
		self.passphrase = self.getPassphrase()

	"""
	Method that creates a YAML file.

	Parameters:
	self -- An instantiated object of the Utils class.
	data -- Information that will be stored in the YAML file.
	path_file_yaml -- YAML file path.
	mode -- Mode in which the YAML file will be opened.

	Exceptions:
	IOError -- It is an error raised when an input/output operation fails.
	"""
	def createYamlFile(self, data, path_file_yaml, mode):
		try:
			with open(path_file_yaml, mode) as file_yaml:
				safe_dump(data, file_yaml, default_flow_style = False)
		except IOError as exception:
			self.logger.createSnapToolLog(exception, 3)
			self.form_dialog.d.msgbox(text = "\nError creating YAML file. For more information, see the logs.", height = 8, width = 50, title = "Error Message")
			self.form_dialog.mainMenu()

	"""
	Method that obtains and stores the content of a YAML file in a variable.

	Parameters:
	self -- An instantiated object of the Utils class.
	path_file_yaml -- YAML file path.
	mode -- Mode in which the YAML file will be opened.

	Return:
	data_file_yaml -- Variable that stores the content of the YAML file.

	Exceptions:
	IOError -- It is an error raised when an input/output operation fails.
	"""
	def readYamlFile(self, path_file_yaml, mode):
		try:
			with open(path_file_yaml, mode) as file_yaml:
				data_file_yaml = safe_load(file_yaml)
		except (IOError, FileNotFoundError) as exception:
			self.logger.createSnapToolLog(exception, 3)
			self.form_dialog.d.msgbox(text = "\nError opening or reading the YAML file. For more information, see the logs.", height = 8, width = 50, title = "Error Message")
			self.form_dialog.mainMenu()
		else:
			return data_file_yaml

	"""
	Method that defines a directory based on the main Snap-Tool directory.

	Parameters:
	self -- An instantiated object of the Utils class.
	path_dir -- Directory that is added to the main Snap-Tool directory.

	Return:
	path_final -- Defined final path.

	Exceptions:
	OSError -- This exception is raised when a system function returns a system-related error, including I/O failures such as “file not found” or “disk full” (not for illegal argument types or other incidental errors).
	TypeError -- Raised when an operation or function is applied to an object of inappropriate type. The associate value is a string giving details about the type mismatch.
	"""
	def getPathSnapTool(self, path_dir):
		path_main = "/etc/Snap-Tool"
		try:
			path_final = path.join(path_main, path_dir)
		except (OSError, TypeError) as exception:
			self.logger.createLogSnapToolLog(exception, 3)
			self.form_dialog.d.msgbox(text = "\nAn error has occurred. For more information, see the logs.", height = 8, width = 50, title = "Error Message")
			self.form_dialog.mainMenu()
		else:
			return path_final

	"""
	Method that obtains the passphrase used for the process of encrypting and decrypting a file.

	Parameters:
	self -- An instantiated object of the Utils class.

	Return:
	pass_key -- Passphrase in a character string.

	Exceptions:
	FileNotFoundError -- This is an exception in python and it comes when a file does not exist and we want to use it. 
	"""
	def getPassphrase(self):
		try:
			file_key = open(self.getPathSnapTool('conf') + '/key','r')
			pass_key = file_key.read()
			file_key.close()
		except FileNotFoundError as exception:
			self.logger.createSnapToolLog(exception, 3)
			self.form_dialog.d.msgbox(text = "\nError opening or reading the Key file. For more information, see the logs.", height = 8, width = 50, title = "Error Message")
			self.form_dialog.mainMenu()
		else:
			return pass_key

	"""
	Method that validates an entered value based on a defined regular expression.

	Parameters:
	self -- An instantiated object of the Utils class.
	regular_expression -- Regular expression with which the data will be validated.
	data_entered -- Data to be validated.

	Return:
	If the data entered is valid or not.
	"""
	def validateRegularExpression(self, regular_expression, data_entered):
		if(not regular_expression.match(data_entered)):
			return False
		return True

	"""
	Method that converts a list into a list that can be used for a checklist or radiolist in the application.

	Parameters:
	self -- An instantiated object of the Utils class.
	list_to_convert -- List to convert.
	text -- Text that will be displayed next to the item in the interface.

	Return:
	new_list_convert -- Converted list.
	"""
	def convertListToCheckOrRadioList(self, list_to_convert, text):
		new_list_convert = []
		for item in list_to_convert:
			new_list_convert.append((item, text, 0))
		return new_list_convert

	"""
	Method that converts a list of objects to text.
	
	Parameters:
	self -- An instantiated object of the Utils class.
	list_to_convert -- List to convert.

	Return:
	message -- Obtained text.
	"""
	def convertListToString(self, list_to_convert):
		message = ""
		for item in list_to_convert:
			message += "- " + item + '\n'
		return message

	"""
	Method that obtains the hash of a file.

	Parameters:
	self -- An instantiated object of the Utils class.
	file -- Path of the file from which the hash function will be obtained.

	Return:
	Hash of the file.

	Exceptions:
	IOError -- It is an error raised when an input/output operation fails.
	"""
	def getHashToFile(self, path_file):
		try:
			hash_sha = sha256()
			with open(path_file, 'rb') as file_to_hash:
				for block in iter(lambda: file_to_hash.read(4096), b""):
					hash_sha.update(block)
		except IOError as exception:
			self.logger.createSnapToolLog(exception, 3)
			self.form_dialog.d.msgbox(text = "\nError getting the file's hash function. For more information, see the logs.", height = 8, width = 50, title = "Error Message")
			self.form_dialog.mainMenu()
		else:
			return hash_sha.hexdigest()

	"""
	Method that encrypts a text string.

	Parameters:
	self -- An instantiated object of the Utils class.
	text -- Text to encrypt.

	Return:
	Encrypted text.

	Exceptions:
	Exception -- It is thrown when any exception is found.
	"""
	def encryptAES(self, text):
		try:
			text_bytes = bytes(text, 'utf-8')
			key = sha256(self.passphrase.encode()).digest()
			IV = Random.new().read(AES.block_size)
			aes = AES.new(key, AES.MODE_CBC, IV)
		except Exception as exception:
			self.logger.createSnapToolLog(exception, 3)
			self.form_dialog.d.msgbox(text = "\nFailed to encrypt the data. For more information, see the logs.", height = 8, width = 50, title = "Error Message")
			self.form_dialog.mainMenu()
		else:
			return b64encode(IV + aes.encrypt(pad(text_bytes, AES.block_size)))

	"""
	Method that decrypts a text string.

	Parameters:
	self -- An instantiated object of the Utils class.
	text_encrypt -- Text to decipher.

	Return:
	Character string with decrypted text.

	Exceptions:
	binascii.Error -- Is raised if were incorrectly padded or if there are non-alphabet characters present in the string. 
	"""
	def decryptAES(self, text_encrypt):
		try:
			key = sha256(self.passphrase.encode()).digest()
			text_encrypt = b64decode(text_encrypt)
			IV = text_encrypt[:AES.block_size]
			aes = AES.new(key, AES.MODE_CBC, IV)
		except binascii.Error as exception:
			self.logger.createSnapToolLog(exception, 3)
			self.form_dialog.d.msgbox(text = "\nFailed to decrypt the data. For more information, see the logs.", height = 8, width = 50, title = "Error Message")
			self.form_dialog.mainMenu()
		else:
			return unpad(aes.decrypt(text_encrypt[AES.block_size:]), AES.block_size)
class Configuration:
    """
	Property that stores an object of type Utils.
	"""
    utils = None
    """
	Property that stores an object of type Logger.
	"""
    logger = None
    """
	Property that stores an object of type FormDialog.
	"""
    form_dialog = None
    """
	Property that stores the path of the configuration file.
	"""
    conf_file = None
    """
	Constructor for the Configuration class.

	Parameters:
	self -- An instantiated object of the Configuration class.
	form_dialog -- FormDialog class object.
	"""
    def __init__(self, form_dialog):
        self.logger = Logger()
        self.form_dialog = form_dialog
        self.utils = Utils(form_dialog)
        self.conf_file = self.utils.getPathSnapTool(
            "conf") + "/snap_tool_conf.yaml"

    """
	Method that requests the data for the creation of the
	Snap-Tool configuration file.

	Parameters:
	self -- An instantiated object of the Configuration class.
	"""

    def createConfiguration(self):
        data_conf = []
        version_es = self.form_dialog.getDataNumberDecimal(
            "Enter the ElasticSearch version:", "7.15")
        data_conf.append(version_es)
        host_es = self.form_dialog.getDataIP(
            "Enter the ElasticSearch IP address:", "localhost")
        data_conf.append(host_es)
        port_es = self.form_dialog.getDataPort(
            "Enter the ElasticSearch listening port:", "9200")
        data_conf.append(port_es)
        use_ssl = self.form_dialog.getDataYesOrNo(
            "\nDo you want Snap-Tool to connect to ElasticSearch using the SSL/TLS protocol?",
            "Connection Via SSL/TLS")
        if use_ssl == "ok":
            data_conf.append(True)
            valid_certificate = self.form_dialog.getDataYesOrNo(
                "\nDo you want the certificate for SSL/TLS communication to be validated?",
                "Certificate Validation")
            if valid_certificate == "ok":
                data_conf.append(True)
                path_cert_file = self.form_dialog.getFile(
                    '/etc/Snap-Tool', "Select the CA certificate:", ".pem")
                data_conf.append(path_cert_file)
            else:
                data_conf.append(False)
        else:
            data_conf.append(False)
        http_auth = self.form_dialog.getDataYesOrNo(
            "\nIs the use of HTTP authentication required to connect to ElasticSearch?",
            "HTTP Authentication")
        if http_auth == "ok":
            data_conf.append(True)
            user_http_auth = self.utils.encryptAES(
                self.form_dialog.getDataInputText(
                    "Enter the username for HTTP authentication:",
                    "snap_tool_user"))
            data_conf.append(user_http_auth.decode('utf-8'))
            pass_http_auth = self.utils.encryptAES(
                self.form_dialog.getDataPassword(
                    "Enter the user's password for HTTP authentication:",
                    "password"))
            data_conf.append(pass_http_auth.decode('utf-8'))
        else:
            data_conf.append(False)
        telegram_bot_token = self.utils.encryptAES(
            self.form_dialog.getDataInputText(
                "Enter the Telegram bot token:",
                "751988420:AAHrzn7RXWxVQQNha0tQUzyouE5lUcPde1g"))
        data_conf.append(telegram_bot_token.decode('utf-8'))
        telegram_chat_id = self.utils.encryptAES(
            self.form_dialog.getDataInputText(
                "Enter the Telegram channel identifier:", "-1002365478941"))
        data_conf.append(telegram_chat_id.decode('utf-8'))
        is_delete_index = self.form_dialog.getDataYesOrNo(
            "\nDo you want to enable automatic index removal?",
            "Automatic index removal")
        if is_delete_index == "ok":
            data_conf.append(True)
        else:
            data_conf.append(False)
        self.createFileConfiguration(data_conf)
        if path.exists(self.conf_file):
            self.form_dialog.d.msgbox(text="\nConfiguration file created",
                                      height=7,
                                      width=50,
                                      title="Notification Message")
            self.logger.createSnapToolLog("Configuration file created", 2)
        else:
            self.form_dialog.d.msgbox(
                text=
                "\nError creating configuration file. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
        self.form_dialog.mainMenu()

    """
	Method that modifies one or more fields of the Snap-Tool
	configuration file.

	Parameters:
	self -- An instantiated object of the Configuration class.

	Exceptions:
	KeyError -- A Python KeyError exception is what is raised
				when you try to access a key that isn’t in a
				dictionary (dict). 
	"""

    def updateConfiguration(self):
        options_conf_fields = [
            ("Version", "ElasticSearch Version", 0),
            ("Host", "ElasticSearch Host", 0),
            ("Port", "ElasticSearch Port", 0),
            ("SSL/TLS", "Enable or disable SSL/TLS connection", 0),
            ("HTTP Authentication", "Enable or disable Http authentication",
             0), ("Bot Token", "Telegram bot token", 0),
            ("Chat ID", "Telegram channel identifier", 0),
            ("Delete Index", "Enable or disable automatic index removal", 0)
        ]

        options_ssl_true = [("Disable", "Disable SSL/TLS communication", 0),
                            ("Certificate Validation",
                             "Modify certificate validation", 0)]

        options_ssl_false = [("Enable", "Enable SSL/TLS communication", 0)]

        options_valid_cert_true = [
            ("Disable", "Disable certificate validation", 0),
            ("Certificate File", "Change certificate file", 0)
        ]

        options_valid_cert_false = [("Enable", "Enable certificate validation",
                                     0)]

        options_http_auth_true = [
            ("Disable", "Disable HTTP authentication", 0),
            ("Data", "Modify HTTP authentication data", 0)
        ]

        options_http_auth_false = [("Enable", "Enable HTTP authentication", 0)]

        options_http_auth_data = [("Username",
                                   "Username for HTTP authentication", 0),
                                  ("Password", "User password", 0)]

        options_delete_index_true = [("Disable",
                                      "Disable automatic index deletion", 0)]

        options_delete_index_false = [("Enable",
                                       "Enables automatic index deletion", 0)]

        flag_version = 0
        flag_host = 0
        flag_port = 0
        flag_use_ssl = 0
        flag_http_auth = 0
        flag_bot_token = 0
        flag_chat_id = 0
        flag_delete_index = 0
        opt_conf_fields = self.form_dialog.getDataCheckList(
            "Select one or more options:", options_conf_fields,
            "Configuration File Fields")
        for option in opt_conf_fields:
            if option == "Version":
                flag_version = 1
            elif option == "Host":
                flag_host = 1
            elif option == "Port":
                flag_port = 1
            elif option == "SSL/TLS":
                flag_use_ssl = 1
            elif option == "HTTP Authentication":
                flag_http_auth = 1
            elif option == "Bot Token":
                flag_bot_token = 1
            elif option == "Chat ID":
                flag_chat_id = 1
            elif option == "Delete Index":
                flag_delete_index = 1
        try:
            data_conf = self.utils.readYamlFile(self.conf_file, 'rU')
            hash_data_conf = self.utils.getHashToFile(self.conf_file)
            if flag_version == 1:
                version_es = self.form_dialog.getDataNumberDecimal(
                    "Enter the ElasticSearch version:",
                    data_conf['es_version'])
                data_conf['es_version'] = version_es
            if flag_host == 1:
                host_es = self.form_dialog.getDataIP(
                    "Enter the ElasticSearch IP address:",
                    data_conf['es_host'])
                data_conf['es_host'] = host_es
            if flag_port == 1:
                port_es = self.form_dialog.getDataPort(
                    "Enter the ElasticSearch listening port:",
                    str(data_conf['es_port']))
                data_conf['es_port'] = int(port_es)
            if flag_use_ssl == 1:
                if data_conf['use_ssl'] == True:
                    opt_ssl_true = self.form_dialog.getDataRadioList(
                        "Select a option:", options_ssl_true,
                        "Connection via SSL/TLS")
                    if opt_ssl_true == "Disable":
                        del data_conf['valid_certificate']
                        if 'path_certificate' in data_conf:
                            del data_conf['path_certificate']
                        data_conf['use_ssl'] = False
                    elif opt_ssl_true == "Certificate Validation":
                        if data_conf['valid_certificate'] == True:
                            opt_valid_cert_true = self.form_dialog.getDataRadioList(
                                "Select a option:", options_valid_cert_true,
                                "Certificate Validation")
                            if opt_valid_cert_true == "Disable":
                                if 'path_certificate' in data_conf:
                                    del data_conf['path_certificate']
                                data_conf['valid_certificate'] = False
                            elif opt_valid_cert_true == "Certificate File":
                                path_cert_file = self.form_dialog.getFile(
                                    data_conf['path_certificate'],
                                    "Select the CA certificate:", ".pem")
                                data_conf['path_certificate'] = path_cert_file
                        else:
                            opt_valid_cert_false = self.form_dialog.getDataRadioList(
                                "Select a option:", options_valid_cert_false,
                                "Certificate Validation")
                            if opt_valid_cert_false == "Enable":
                                data_conf['valid_certificate'] = True
                                path_cert_file = self.form_dialog.getFile(
                                    '/etc/Snap-Tool',
                                    "Select the CA certificate:", ".pem")
                                valid_cert_json = {
                                    'path_certificate': path_cert_file
                                }
                                data_conf.update(valid_cert_json)
                else:
                    opt_ssl_false = self.form_dialog.getDataRadioList(
                        "Select a option:", options_ssl_false,
                        "Connection via SSL/TLS")
                    if opt_ssl_false == "Enable":
                        data_conf['use_ssl'] = True
                        valid_certificate = self.form_dialog.getDataYesOrNo(
                            "\nDo you want the certificates for SSL/TLS communication to be validated?",
                            "Certificate Validation")
                        if valid_certificate == "ok":
                            path_cert_file = self.form_dialog.getFile(
                                '/etc/Snap-Tool', "Select the CA certificate:",
                                ".pem")
                            valid_cert_json = {
                                'valid_certificate': True,
                                'path_certificate': path_cert_file
                            }
                        else:
                            valid_cert_json = {'valid_certificate': False}
                        data_conf.update(valid_cert_json)
            if flag_http_auth == 1:
                if data_conf['use_http_auth'] == True:
                    opt_http_auth_true = self.form_dialog.getDataRadioList(
                        "Select a option:", options_http_auth_true,
                        "HTTP Authentication")
                    if opt_http_auth_true == "Disable":
                        del (data_conf['http_auth_user'])
                        del (data_conf['http_auth_pass'])
                        data_conf['use_http_auth'] = False
                    elif opt_http_auth_true == "Data":
                        flag_http_auth_user = 0
                        flag_http_auth_pass = 0
                        opt_http_auth_data = self.form_dialog.getDataCheckList(
                            "Select one or more options:",
                            options_http_auth_data, "HTTP Authentication")
                        for option in opt_http_auth_data:
                            if option == "Username":
                                flag_http_auth_user = 1
                            elif option == "Password":
                                flag_http_auth_pass = 1
                        if flag_http_auth_user == 1:
                            user_http_auth = self.utils.encryptAES(
                                self.form_dialog.getDataInputText(
                                    "Enter the username for HTTP authentication:",
                                    "snap_tool_user"))
                            data_conf[
                                'http_auth_user'] = user_http_auth.decode(
                                    'utf-8')
                        if flag_http_auth_pass == 1:
                            pass_http_auth = self.utils.encryptAES(
                                self.form_dialog.getDataPassword(
                                    "Enter the user's password for HTTP authentication:",
                                    "password"))
                            data_conf[
                                'http_auth_pass'] = pass_http_auth.decode(
                                    'utf-8')
                else:
                    opt_http_auth_false = self.form_dialog.getDataRadioList(
                        "Select a option:", options_http_auth_false,
                        "HTTP Authentication")
                    if opt_http_auth_false == "Enable":
                        user_http_auth = self.utils.encryptAES(
                            self.form_dialog.getDataInputText(
                                "Enter the username for HTTP authentication:",
                                "snap_tool_user"))
                        pass_http_auth = self.utils.encryptAES(
                            self.form_dialog.getDataPassword(
                                "Enter the user's password for HTTP authentication:",
                                "password"))
                        http_auth_data_json = {
                            'http_auth_user': user_http_auth.decode('utf-8'),
                            'http_auth_pass': pass_http_auth.decode('utf-8')
                        }
                        data_conf.update(http_auth_data_json)
                        data_conf['use_http_auth'] = True
            if flag_bot_token == 1:
                telegram_bot_token = self.utils.encryptAES(
                    self.form_dialog.getDataInputText(
                        "Enter the Telegram bot token:",
                        self.utils.decryptAES(
                            data_conf['telegram_bot_token']).decode('utf-8')))
                data_conf['telegram_bot_token'] = telegram_bot_token.decode(
                    'utf-8')
            if flag_chat_id == 1:
                telegram_chat_id = self.utils.encryptAES(
                    self.form_dialog.getDataInputText(
                        "Enter the Telegram channel identifier:",
                        self.utils.decryptAES(
                            data_conf['telegram_chat_id']).decode('utf-8')))
                data_conf['telegram_chat_id'] = telegram_chat_id.decode(
                    'utf-8')
            if flag_delete_index == 1:
                if data_conf['is_delete_index'] == True:
                    opt_delete_index_true = self.form_dialog.getDataRadioList(
                        "Select a option:", options_delete_index_true,
                        "Automatic index removal")
                    if opt_delete_index_true == "Disable":
                        data_conf['is_delete_index'] = False
                else:
                    opt_delete_index_false = self.form_dialog.getDataRadioList(
                        "Select a option:", options_delete_index_false,
                        "Automatic index removal")
                    if opt_delete_index_false == "Enable":
                        data_conf['is_delete_index'] = True
            self.utils.createYamlFile(data_conf, self.conf_file, 'w')
            hash_data_conf_upd = self.utils.getHashToFile(self.conf_file)
            if hash_data_conf == hash_data_conf_upd:
                self.form_dialog.d.msgbox(
                    text="\nThe configuration file was not modified.",
                    height=7,
                    width=50,
                    title="Notification Message")
            else:
                self.logger.createSnapToolLog(
                    "The configuration file was modified", 1)
                self.form_dialog.d.msgbox(
                    text="\nThe configuration file was modified.",
                    height=7,
                    width=50,
                    title="Notification Message")
            self.form_dialog.mainMenu()
        except (OSError, KeyError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nError modifying the configuration file. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()

    """
	Method that creates the YAML file with the data entered
	for the Snap-Tool configuration file.

	Parameters:
	self -- An instantiated object of the Configuration class.
	data_conf -- List containing all the data entered for
				 the configuration file.
	
	Exceptions:
	OSError -- This exception is raised when a system
			   function returns a system-related error,
			   including I/O failures such as “file not
			   found” or “disk full” (not for illegal
			   argument types or other incidental errors).
	"""

    def createFileConfiguration(self, data_conf):
        data_json = {
            'es_version': data_conf[0],
            'es_host': data_conf[1],
            'es_port': int(data_conf[2]),
            'use_ssl': data_conf[3]
        }

        if data_conf[3] == True:
            if data_conf[4] == True:
                valid_certificate_json = {
                    'valid_certificate': data_conf[4],
                    'path_certificate': data_conf[5]
                }
                last_index = 5
            else:
                valid_certificate_json = {'valid_certificate': data_conf[4]}
                last_index = 4
            data_json.update(valid_certificate_json)
        else:
            last_index = 3
        if data_conf[last_index + 1] == True:
            http_auth_json = {
                'use_http_auth': data_conf[last_index + 1],
                'http_auth_user': data_conf[last_index + 2],
                'http_auth_pass': data_conf[last_index + 3]
            }
            last_index += 3
        else:
            http_auth_json = {'use_http_auth': data_conf[last_index + 1]}
            last_index += 1
        data_json.update(http_auth_json)
        aux_json = {
            'telegram_bot_token': data_conf[last_index + 1],
            'telegram_chat_id': data_conf[last_index + 2],
            'is_delete_index': data_conf[last_index + 3]
        }
        data_json.update(aux_json)

        self.utils.createYamlFile(data_json, self.conf_file, 'w')
 def __init__(self, form_dialog):
     self.logger = Logger()
     self.form_dialog = form_dialog
     self.utils = Utils(form_dialog)
     self.conf_file = self.utils.getPathSnapTool(
         "conf") + "/snap_tool_conf.yaml"
Beispiel #9
0
class Elastic:
    """
	Disable warning message.
	"""
    if not warnoptions:
        from warnings import simplefilter
        simplefilter("ignore")
    """
	Property that stores an object of type Utils.
	"""
    utils = None
    """
	Property that stores an object of type Logger.
	"""
    logger = None
    """
	Property that stores an object of type FormDialog.
	"""
    form_dialog = None
    """
	Constructor for the Elastic class.

	Parameters:
	self -- An instantiated object of the Elastic class.
	form_dialog -- FormDialog class object.
	"""
    def __init__(self, form_dialog):
        self.logger = Logger()
        self.form_dialog = form_dialog
        self.utils = Utils(form_dialog)

    """
	Method that creates a connection object with ElasticSearch.

	Parameters:
	self -- An instantiated object of the Elastic class.

	Return:
	conn_es -- Object that contains the connection to ElasticSearch.

	Exceptions:
	KeyError -- A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary (dict). 
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES. 
	exceptions.AuthenticationException -- Exception representing a 401 status code.
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	requests.exceptions.InvalidURL -- The URL provided was somehow invalid.
	"""

    def getConnectionElastic(self):
        conn_es = None
        snap_tool_conf = self.utils.readYamlFile(
            self.utils.getPathSnapTool('conf') + '/snap_tool_conf.yaml', 'r')
        try:
            if (not snap_tool_conf['use_ssl']
                    == True) and (not snap_tool_conf['use_http_auth'] == True):
                conn_es = Elasticsearch(
                    snap_tool_conf['es_host'],
                    port=snap_tool_conf['es_port'],
                    connection_class=RequestsHttpConnection,
                    use_ssl=False)
            if (not snap_tool_conf['use_ssl']
                    == True) and snap_tool_conf['use_http_auth'] == True:
                conn_es = Elasticsearch(
                    snap_tool_conf['es_host'],
                    port=snap_tool_conf['es_port'],
                    connection_class=RequestsHttpConnection,
                    http_auth=(
                        self.utils.decryptAES(
                            snap_tool_conf['http_auth_user']).decode('utf-8'),
                        self.utils.decryptAES(
                            snap_tool_conf['http_auth_pass']).decode('utf-8')),
                    use_ssl=False)
            if snap_tool_conf['use_ssl'] == True and (
                    not snap_tool_conf['use_http_auth'] == True):
                if not snap_tool_conf['valid_certificate']:
                    conn_es = Elasticsearch(
                        snap_tool_conf['es_host'],
                        port=snap_tool_conf['es_port'],
                        connection_class=RequestsHttpConnection,
                        use_ssl=True,
                        verify_certs=False,
                        ssl_show_warn=False)
                else:
                    context = create_default_context(
                        cafile=snap_tool_conf['path_certificate'])
                    conn_es = Elasticsearch(
                        snap_tool_conf['es_host'],
                        port=snap_tool_conf['es_port'],
                        connection_class=RequestsHttpConnection,
                        use_ssl=True,
                        verify_certs=True,
                        ssl_context=context)
            if snap_tool_conf['use_ssl'] == True and snap_tool_conf[
                    'use_http_auth'] == True:
                if not snap_tool_conf['valid_certificate'] == True:
                    conn_es = Elasticsearch(
                        snap_tool_conf['es_host'],
                        port=snap_tool_conf['es_port'],
                        connection_class=RequestsHttpConnection,
                        http_auth=(self.utils.decryptAES(
                            snap_tool_conf['http_auth_user']).decode('utf-8'),
                                   self.utils.decryptAES(
                                       snap_tool_conf['http_auth_pass']).
                                   decode('utf-8')),
                        use_ssl=True,
                        verify_certs=False,
                        ssl_show_warn=False)
                else:
                    context = create_default_context(
                        cafile=snap_tool_conf['path_certificate'])
                    conn_es = Elasticsearch(
                        snap_tool_conf['es_host'],
                        port=snap_tool_conf['es_port'],
                        connection_class=RequestsHttpConnection,
                        http_auth=(self.utils.decryptAES(
                            snap_tool_conf['http_auth_user']).decode('utf-8'),
                                   self.utils.decryptAES(
                                       snap_tool_conf['http_auth_pass']).
                                   decode('utf-8')),
                        use_ssl=True,
                        verify_certs=True,
                        ssl_context=context)
            if not conn_es == None:
                self.logger.createSnapToolLog(
                    "Established connection with: " +
                    snap_tool_conf['es_host'] + ':' +
                    str(snap_tool_conf['es_port']), 1)
        except (KeyError, exceptions.ConnectionError,
                exceptions.AuthenticationException,
                exceptions.AuthorizationException, InvalidURL) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to connect to ElasticSearch. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            exit(1)
        else:
            return conn_es

    """
	Method that creates a repository type FS.

	Parameters:
	self -- An instantiated object of the Elastic class.
	conn_es -- Object that contains the connection to ElasticSearch.
	repository_name -- Name of the repository.
	path_repository -- Repository path.
	compress_repository -- Whether the metadata files are stored compressed.

	Exceptions:
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	exceptions.TransportError -- Exception raised when ES returns a non-OK (>=400) HTTP status code. Or when an actual connection error happens; in that case the status_code will be set to 'N/A'.
	"""

    def createRepositoryFS(self, conn_es, repository_name, path_repository,
                           compress_repository):
        try:
            conn_es.snapshot.create_repository(repository=repository_name,
                                               body={
                                                   "type": "fs",
                                                   "settings": {
                                                       "location":
                                                       path_repository,
                                                       "compress":
                                                       compress_repository
                                                   }
                                               })
        except (exceptions.AuthorizationException, exceptions.ConnectionError,
                exceptions.TransportError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nError creating repository. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()

    """
	Method that removes a repository type FS.

	Parameters:
	self -- An instantiated object of the Elastic class.
	conn_es -- Object that contains the connection to ElasticSearch.
	repository_name -- Name of the repository to delete.

	Exceptions:
	exceptions.NotFoundError -- Exception representing a 404 status code.
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def deleteRepositoryFS(self, conn_es, repository_name):
        try:
            conn_es.snapshot.delete_repository(repository=repository_name)
        except (exceptions.NotFoundError, exceptions.AuthorizationException,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to delete repository. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()

    """
	Method that creates a snapshot of an index.

	Parameters:
	self -- An instantiated object of the Elastic class.
	conn_es -- Object that contains the connection to ElasticSearch.
	repository_name -- Repository where the snapshot will be stored.
	index_name -- Name of the index to be backed up in the snapshot.

	Exceptions:
	exceptions.RequestError -- Exception representing a 400 status code. 
	exceptions.NotFoundError -- Exception representing a 404 status code.
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def createSnapshot(self, conn_es, repository_name, index_name):
        try:
            conn_es.snapshot.create(repository=repository_name,
                                    snapshot=index_name,
                                    body={
                                        'indices': index_name,
                                        "include_global_state": False
                                    },
                                    wait_for_completion=False)
        except (exceptions.AuthorizationException, exceptions.RequestError,
                exceptions.NotFoundError,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to create snapshot. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()

    """
	Method that removes a snapshot.

	Parameters:
	conn_es -- Object that contains the connection to ElasticSearch.
	repository_name -- Name of the repository where the snapshot to delete is stored.
	snapshot_name -- Name of the snapshot to delete.

	Exceptions:
	exceptions.NotFoundError -- Exception representing a 404 status code.
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def deleteSnapshotElastic(self, conn_es, repository_name, snapshot_name):
        try:
            conn_es.snapshot.delete(repository=repository_name,
                                    snapshot=snapshot_name,
                                    request_timeout=7200)
        except (exceptions.NotFoundError, exceptions.AuthorizationException,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to delete snapshot. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()

    """
	Method that restores a snapshot.

	Parameters:
	conn_es -- Object that contains the connection to ElasticSearch.
	repository_name -- Repository where the snapshot is stored.
	snapshot_name -- Name of the snapshot to restore.

	Exceptions:
	exceptions.NotFoundError -- Exception representing a 404 status code.
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def restoreSnapshot(self, conn_es, repository_name, snapshot_name):
        try:
            conn_es.snapshot.restore(repository=repository_name,
                                     snapshot=snapshot_name)
        except (exceptions.NotFoundError, exceptions.AuthorizationException,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to restore snapshot. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()

    """
	Method that mounts a snapshot as a searchable snapshot.

	Parameters:
	conn_es -- Object that contains the connection to ElasticSearch.
	repository_name -- Name of the repository where the snapshot that will be mounted as a searchable snapshot is stored.
	snapshot_name -- Name of the snapshot to be mounted as a searchable snapshot.

	Exceptions:
	exceptions.NotFoundError -- Exception representing a 404 status code.
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def mountSearchableSnapshot(self, conn_es, repository_name, snapshot_name):
        try:
            conn_es.searchable_snapshots.mount(repository=repository_name,
                                               snapshot=snapshot_name,
                                               body={"index": snapshot_name},
                                               wait_for_completion=False,
                                               request_timeout=7200)
        except (exceptions.NotFoundError, exceptions.AuthorizationException,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to mount snapshot as a searchable snapshot. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()

    """
	Method that removes an index from ElasticSearch.

	Parameters:
	self -- An instantiated object of the Elastic class.
	conn_es -- Object that contains the connection to ElasticSearch.
	index_name -- Name of the index to be removed.

	Exceptions:
	exceptions.NotFoundError -- Exception representing a 404 status code.
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def deleteIndex(self, conn_es, index_name):
        try:
            conn_es.indices.delete(index=index_name)
        except (exceptions.NotFoundError, exceptions.AuthorizationException,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to delete index. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()

    """
	Method that gets the status of a snapshot.

	Parameters:
	self -- An instantiated object of the Elastic class.
	conn_es -- Object that contains the connection to ElasticSearch.
	repository_name -- Repository where the snapshot is stored.
	snapshot_name -- Name of the snapshot from which the status will be obtained.

	Return:
	status_snapshot -- Status of the snapshot.

	Exceptions:
	exceptions.NotFoundError -- Exception representing a 404 status code.
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def getStatusSnapshot(self, conn_es, repository_name, snapshot_name):
        try:
            status_aux = conn_es.snapshot.status(repository=repository_name,
                                                 snapshot=snapshot_name)
            status_snapshot = status_aux['snapshots'][0]['state']
        except (exceptions.NotFoundError, exceptions.AuthorizationException,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to get snapshot status. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()
        else:
            return status_snapshot

    """
	Method that gets the status of a snapshot.

	Parameters:
	self -- An instantiated object of the Elastic class.
	conn_es -- Object that contains the connection to ElasticSearch.
	repository_name -- Repository where the snapshot is stored.
	snapshot_name -- Name of the snapshot from which the information will be obtained.

	Return:
	snapshot_info -- Information obtained from the snapshot.

	Exceptions:
	exceptions.NotFoundError -- Exception representing a 404 status code.
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def getSnapshotInfo(self, conn_es, repository_name, snapshot_name):
        try:
            snapshot_info = conn_es.snapshot.get(repository=repository_name,
                                                 snapshot=snapshot_name)
        except (exceptions.NotFoundError, exceptions.AuthorizationException,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to get snapshot status. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()
        else:
            return snapshot_info

    """
	Method that obtains a list with the names of the ElasticSearch indexes.

	Parameters:
	self -- An instantiated object of the Elastic class.
	conn_es -- Object that contains the connection to ElasticSearch.

	Return:
	list_all_indices -- List with the names of the indices found.

	Exceptions:
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def getIndices(self, conn_es):
        list_all_indices = []
        try:
            list_all_indices = conn_es.indices.get('*')
            list_all_indices = sorted([
                index for index in list_all_indices
                if not index.startswith(".")
            ])
        except (exceptions.AuthorizationException,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to get created repositories. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()
        else:
            return list_all_indices

    """
	Method that gets a list of all the snapshots created so far.

	Parameters:
	conn_es -- Object that contains the connection to ElasticSearch.
	repository_name -- Repository where the snapshots are stored.

	Return:
	list_all_snapshots -- List with the names of all snapshots found in the repository.

	Exceptions:
	exceptions.NotFoundError -- Exception representing a 404 status code.
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def getAllSnapshots(self, conn_es, repository_name):
        list_all_snapshots = []
        try:
            snapshots_info = conn_es.snapshot.get(repository=repository_name,
                                                  snapshot='_all')
            for snapshot in snapshots_info['snapshots']:
                list_all_snapshots.append(snapshot['snapshot'])
            list_all_snapshots = sorted(list_all_snapshots)
        except (exceptions.NotFoundError, exceptions.AuthorizationException,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to get snapshots. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()
        else:
            return list_all_snapshots

    """
	Method that gets the repositories created in ElasticSearch.

	Parameters:
	self -- An instantiated object of the Elastic class.
	conn_es -- Object that contains the connection to ElasticSearch.

	Return:
	list_all_repositories -- List with the names of the repositories found.

	Exceptions:
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def getAllRepositories(self, conn_es):
        list_all_repositories = []
        try:
            repositories_info = conn_es.cat.repositories(format="json")
            for repository in repositories_info:
                list_all_repositories.append(repository['id'])
        except (exceptions.AuthorizationException,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nFailed to get created repositories. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()
        else:
            return list_all_repositories

    """
	Method that obtains information related to the disk space corresponding to the nodes belonging to the elasticsearch cluster.

	Parameters:
	self -- An instantiated object of the Elastic class.
	conn_es -- Object that contains the connection to ElasticSearch.

	Exceptions:
	exceptions.AuthorizationException -- Exception representing a 403 status code.
	exceptions.ConnectionError --  Error raised when there was an exception while talking to ES.
	"""

    def getNodesInformation(self, conn_es):
        try:
            nodes_info = conn_es.nodes.stats(metric='fs')['nodes']
        except (exceptions.AuthorizationException,
                exceptions.ConnectionError) as exception:
            self.logger.createSnapToolLog(exception, 3)
            self.form_dialog.d.msgbox(
                text=
                "\nError when obtaining the information of the nodes. For more information, see the logs.",
                height=8,
                width=50,
                title="Error Message")
            self.form_dialog.mainMenu()
        else:
            return nodes_info