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) # 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
def edit_dictionaries(dictionaries: 'Sequence[Tuple[str, Union[Transform, Dictionary]]]', value: Dict[str, Any], focus: Optional[str] = None, hover_help: bool = True, validate: Optional[Callable[[Any], None]] = None, title: Optional[str] = None, method: str = "GET", preview: bool = False, varprefix: str = "", formname: str = "form", consume_transid: bool = True): if request.get_ascii_input("filled_in") == formname and transactions.transaction_valid(): if not preview and consume_transid: transactions.check_transaction() messages: List[str] = [] new_value: Dict[str, Dict[str, Any]] = {} for keyname, vs_dict in dictionaries: dict_varprefix = varprefix + keyname new_value[keyname] = {} try: edited_value = vs_dict.from_html_vars(dict_varprefix) vs_dict.validate_value(edited_value, dict_varprefix) new_value[keyname].update(edited_value) except MKUserError as e: messages.append("%s: %s" % (vs_dict.title() or _("Properties"), e)) user_errors.add(e) except Exception as e: messages.append("%s: %s" % (vs_dict.title() or _("Properties"), e)) user_errors.add(MKUserError(None, str(e))) if validate and not user_errors: try: validate(new_value[keyname]) except MKUserError as e: messages.append(str(e)) user_errors.add(e) if messages: messages_joined = "".join(["%s<br>\n" % m for m in messages]) if not preview: html.show_error(messages_joined) else: raise MKUserError(None, messages_joined) else: return new_value html.begin_form(formname, method=method) for keyname, vs_dict in dictionaries: dict_varprefix = varprefix + keyname subvalue = value.get(keyname, {}) vs_dict.render_input_as_form(dict_varprefix, subvalue) end() # Should be ignored be hidden_fields, but I do not dare to change it there request.del_var("filled_in") html.hidden_fields() html.end_form()
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) return redirect(mode_url(self.back_mode)) return None
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()
def _evaluate_user_opts(self) -> Tuple[TableRows, bool, Optional[str]]: assert self.id is not None table_id = ensure_str(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_unicode_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
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) # 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
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()
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()
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) return redirect(mode_url(self.back_mode)) return None
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()