Exemplo n.º 1
0
    def action(self) -> ActionResult:
        if transactions.check_transaction():
            value = self._vs_key().from_html_vars("key")
            request.del_var("key_p_passphrase")
            self._vs_key().validate_value(value, "key")

            key_file = self._get_uploaded(value["key_file"])
            if not key_file:
                raise MKUserError(None, _("You need to provide a key file."))

            if (
                not key_file.startswith("-----BEGIN ENCRYPTED PRIVATE KEY-----\n")
                or "-----END ENCRYPTED PRIVATE KEY-----\n" not in key_file
                or "-----BEGIN CERTIFICATE-----\n" not in key_file
                or not key_file.endswith("-----END CERTIFICATE-----\n")
            ):
                raise MKUserError(None, _("The file does not look like a valid key file."))

            self._upload_key(key_file, value["alias"], value["passphrase"])
            # FIXME: This leads to a circular import otherwise. This module (cmk.gui.key_mgmt) is
            #  clearly outside of either cmk.gui.plugins.wato and cmk.gui.cee.plugins.wato so this
            #  is obviously a very simple module-layer violation. This whole module should either
            #    * be moved into cmk.gui.cee.plugins.wato
            #    * or cmk.gui.cee.plugins.wato.module_registry should be moved up
            #  Either way, this is outside my scope right now and shall be fixed.
            from cmk.gui.plugins.wato.utils.base_modes import mode_url

            return HTTPRedirect(mode_url(self.back_mode), code=302)
        return None
Exemplo n.º 2
0
 def breadcrumb(self) -> Breadcrumb:
     # The ModeEditRuleset.breadcrumb_item does not know anything about the fact that this mode
     # is a child of the logwatch_rules ruleset. It can not construct the correct link to the
     # logwatch_rules ruleset in the breadcrumb. We hand over the ruleset variable name that we
     # are interested in to the mode. It's a bit hacky to do it this way, but it's currently the
     # only way to get these information to the modes breadcrumb method.
     with request.stashed_vars():
         request.set_var("varname", "logwatch_rules")
         request.del_var("host")
         request.del_var("service")
         return super().breadcrumb()
Exemplo n.º 3
0
    def _evaluate_user_opts(self) -> Tuple[TableRows, bool, Optional[str]]:
        assert self.id is not None
        table_id = self.id
        rows = self.rows

        search_term = None
        actions_enabled = self.options["searchable"] or self.options["sortable"]

        if not actions_enabled:
            return rows, False, None

        table_opts = user.tableoptions.setdefault(table_id, {})

        # Handle the initial visibility of the actions
        actions_visible = table_opts.get("actions_visible", False)
        if request.get_ascii_input("_%s_actions" % table_id):
            actions_visible = request.get_ascii_input("_%s_actions" %
                                                      table_id) == "1"
            table_opts["actions_visible"] = actions_visible

        if self.options["searchable"]:
            search_term = request.get_str_input_mandatory("search", "")
            # Search is always lower case -> case insensitive
            search_term = search_term.lower()
            if search_term:
                request.set_var("search", search_term)
                rows = _filter_rows(rows, search_term)

        if request.get_ascii_input("_%s_reset_sorting" % table_id):
            request.del_var("_%s_sort" % table_id)
            if "sort" in table_opts:
                del table_opts["sort"]  # persist

        if self.options["sortable"]:
            # Now apply eventual sorting settings
            sort = self._get_sort_column(table_opts)
            if sort is not None:
                request.set_var("_%s_sort" % table_id, sort)
                table_opts["sort"] = sort  # persist
                sort_col, sort_reverse = map(int, sort.split(",", 1))
                rows = _sort_rows(rows, sort_col, sort_reverse)

        if actions_enabled:
            user.save_tableoptions()

        return rows, actions_visible, search_term
Exemplo n.º 4
0
    def action(self) -> ActionResult:
        if transactions.check_transaction():
            value = self._vs_key().from_html_vars("key")
            # Remove the secret key from known URL vars. Otherwise later constructed URLs
            # which use the current page context will contain the passphrase which could
            # leak the secret information
            request.del_var("key_p_passphrase")
            self._vs_key().validate_value(value, "key")
            self._create_key(value["alias"], value["passphrase"])
            # FIXME: This leads to a circular import otherwise. This module (cmk.gui.key_mgmt) is
            #  clearly outside of either cmk.gui.plugins.wato and cmk.gui.cee.plugins.wato so this
            #  is obviously a very simple module-layer violation. This whole module should either
            #    * be moved into cmk.gui.cee.plugins.wato
            #    * or cmk.gui.cee.plugins.wato.module_registry should be moved up
            #  Either way, this is outside my scope right now and shall be fixed.
            from cmk.gui.plugins.wato.utils.base_modes import mode_url

            return HTTPRedirect(mode_url(self.back_mode))
        return None
Exemplo n.º 5
0
 def _show_try_form(self):
     html.begin_form("try")
     forms.header(_("Try Pattern Match"))
     forms.section(_("Hostname"))
     self._vs_host().render_input("host", self._hostname)
     forms.section(_("Logfile"))
     html.help(_("Here you need to insert the original file or pathname"))
     html.text_input("file", size=80)
     forms.section(_("Text to match"))
     html.help(
         _("You can insert some text (e.g. a line of the logfile) to test the patterns defined "
           "for this logfile. All patterns for this logfile are listed below. Matching patterns "
           'will be highlighted after clicking the "Try out" button.'))
     html.text_input("match", cssclass="match", size=100)
     forms.end()
     html.button("_try", _("Try out"))
     request.del_var("folder")  # Never hand over the folder here
     html.hidden_fields()
     html.end_form()
Exemplo n.º 6
0
    def _preview(self) -> None:
        html.begin_form("preview", method="POST")
        self._preview_form()

        attributes = self._attribute_choices()

        # first line could be missing in situation of import error
        csv_reader = self._open_csv_file()
        if not csv_reader:
            return  # don't try to show preview when CSV could not be read

        html.h2(_("Preview"))
        attribute_list = "<ul>%s</ul>" % "".join(
            ["<li>%s (%s)</li>" % a for a in attributes if a[0] is not None]
        )
        html.help(
            _(
                "This list shows you the first 10 rows from your CSV file in the way the import is "
                "currently parsing it. If the lines are not splitted correctly or the title line is "
                "not shown as title of the table, you may change the import settings above and try "
                "again."
            )
            + "<br><br>"
            + _(
                "The first row below the titles contains fields to specify which column of the "
                "CSV file should be imported to which attribute of the created hosts. The import "
                "progress is trying to match the columns to attributes automatically by using the "
                "titles found in the title row (if you have some). "
                "If you use the correct titles, the attributes can be mapped automatically. The "
                "currently available attributes are:"
            )
            + attribute_list
            + _(
                "You can change these assignments according to your needs and then start the "
                "import by clicking on the <i>Import</i> button above."
            )
        )

        # Wenn bei einem Host ein Fehler passiert, dann wird die Fehlermeldung zu dem Host angezeigt, so dass man sehen kann, was man anpassen muss.
        # Die problematischen Zeilen sollen angezeigt werden, so dass man diese als Block in ein neues CSV-File eintragen kann und dann diese Datei
        # erneut importieren kann.
        if self._has_title_line:
            try:
                headers = list(next(csv_reader))
            except StopIteration:
                headers = []  # nope, there is no header
        else:
            headers = []

        rows = list(csv_reader)

        # Determine how many columns should be rendered by using the longest column
        num_columns = max([len(r) for r in [headers] + rows])

        with table_element(
            sortable=False, searchable=False, omit_headers=not self._has_title_line
        ) as table:

            # Render attribute selection fields
            table.row()
            for col_num in range(num_columns):
                header = headers[col_num] if len(headers) > col_num else None
                table.cell(escape_to_html_permissive(header))
                attribute_varname = "attribute_%d" % col_num
                if request.var(attribute_varname):
                    attribute_method = request.get_ascii_input_mandatory(attribute_varname)
                else:
                    attribute_method = self._try_detect_default_attribute(attributes, header)
                    request.del_var(attribute_varname)

                html.dropdown(
                    "attribute_%d" % col_num, attributes, deflt=attribute_method, autocomplete="off"
                )

            # Render sample rows
            for row in rows:
                table.row()
                for cell in row:
                    table.cell(None, cell)

        html.end_form()