def _update_site_specific_global_settings(self): site_mgmt = SiteManagementFactory().factory() configured_sites = site_mgmt.load_sites() for site_id, site_spec in configured_sites.items(): if site_globals_editable(site_id, site_spec): self._update_global_config(site_spec.setdefault("globals", {})) site_mgmt.save_sites(configured_sites, activate=False)
def test_update_basic_site_config(): _write_site_config({ "heute": { "alias": "Die central Site", "disable_wato": True, "disabled": False, "insecure": False, "multisiteurl": "", "persist": False, "replicate_ec": False, "replication": None, "timeout": 10, "user_login": True, "url_prefix": "/heute/", "proxy": None, "socket": ("local", None), }, }) update_site_config(SiteId("heute"), SiteId("haha")) site_mgmt = SiteManagementFactory().factory() all_sites = site_mgmt.load_sites() # Site entry has been renamed assert "heute" not in all_sites assert "haha" in all_sites # url_prefix is updated assert all_sites["haha"]["url_prefix"] == "/haha/"
def do_site_login(site_id, name, password): sites = SiteManagementFactory().factory().load_sites() site = sites[site_id] if not name: raise MKUserError("_name", _("Please specify your administrator login on the remote site.")) if not password: raise MKUserError("_passwd", _("Please specify your password.")) # Trying basic auth AND form based auth to ensure the site login works. # Adding _ajaxid makes the web service fail silently with an HTTP code and # not output HTML code for an error screen. url = site["multisiteurl"] + 'login.py' post_data = { '_login': '******', '_username': name, '_password': password, '_origtarget': 'automation_login.py?_version=%s&_edition_short=%s' % (cmk.__version__, cmk.edition_short()), '_plain_error': '1', } response = get_url( url, site.get('insecure', False), auth=(name, password), data=post_data).strip() if '<html>' in response.lower(): message = _("Authentication to web service failed.<br>Message:<br>%s") % \ html.strip_tags(html.strip_scripts(response)) if config.debug: message += "<br>" + _("Automation URL:") + " <tt>%s</tt><br>" % url raise MKAutomationException(message) elif not response: raise MKAutomationException(_("Empty response from web service")) else: try: return ast.literal_eval(response) except SyntaxError: raise MKAutomationException(response)
def do_site_login(site_id: SiteId, name: UserId, password: str) -> str: sites = SiteManagementFactory().factory().load_sites() site = sites[site_id] if not name: raise MKUserError( "_name", _("Please specify your administrator login on the remote site.")) if not password: raise MKUserError("_passwd", _("Please specify your password.")) # Trying basic auth AND form based auth to ensure the site login works. # Adding _ajaxid makes the web service fail silently with an HTTP code and # not output HTML code for an error screen. url = site["multisiteurl"] + "login.py" post_data = { "_login": "******", "_username": name, "_password": password, "_origtarget": "automation_login.py?_version=%s&_edition_short=%s" % (cmk_version.__version__, cmk_version.edition_short()), "_plain_error": "1", } response = get_url(url, site.get("insecure", False), auth=(name, password), data=post_data).strip() if "<html>" in response.lower(): message = _("Authentication to web service failed.<br>Message:<br>%s" ) % escaping.strip_tags(escaping.strip_scripts(response)) if config.debug: message += "<br>" + _("Automation URL:") + " <tt>%s</tt><br>" % url raise MKAutomationException(message) if not response: raise MKAutomationException(_("Empty response from web service")) try: eval_response = ast.literal_eval(response) except SyntaxError: raise MKAutomationException(response) if isinstance(eval_response, dict): if cmk_version.is_managed_edition( ) and eval_response["edition_short"] != "cme": raise MKUserError( None, _("The Check_MK Managed Services Edition can only " "be connected with other sites using the CME."), ) return eval_response["login_secret"] return eval_response
def update_site_config(old_site_id: SiteId, new_site_id: SiteId) -> None: """Update the Checkmk GUI site configuration This mainly updates the sites.mk, but also triggers changes on the following files when calling save_sites(). - etc/check_mk/liveproxyd.mk (CEE/CME only) - etc/check_mk/conf.d/distributed_wato.mk - etc/check_mk/dcd.d/wato/distributed.mk - etc/nagvis/conf.d/cmk_backends.ini.php """ changed = False site_mgmt = SiteManagementFactory().factory() all_sites = site_mgmt.load_sites() if old_site_id in all_sites: changed = True # 1. Transform entry in all sites logger.debug("Rename site configuration") site_spec = all_sites[new_site_id] = all_sites.pop(old_site_id) # 2. Update the sites URL prefix site_spec["url_prefix"] = site_spec["url_prefix"].replace(f"/{old_site_id}/", f"/{new_site_id}/") # 3. Update the configuration connection site_spec["multisiteurl"] = site_spec["multisiteurl"].replace(f"/{old_site_id}/", f"/{new_site_id}/") # Iterate all sites and check for status host entries refering to the renamed site for this_site_id, site_cfg in all_sites.items(): status_host = site_cfg.get("status_host") if status_host and status_host[0] == old_site_id: logger.debug("Update status host of site %s", this_site_id) changed = True site_cfg["status_host"] = (new_site_id, site_cfg["status_host"][1]) if changed: site_mgmt.save_sites(all_sites, activate=True)
def _get_effective_global_setting(self, varname: str) -> Any: global_settings = load_configuration_settings() default_values = ABCConfigDomain.get_all_default_globals() if cmk.gui.config.is_wato_slave_site(): current_settings = load_configuration_settings(site_specific=True) else: sites = SiteManagementFactory.factory().load_sites() current_settings = sites[config.omd_site()].get("globals", {}) if varname in current_settings: return current_settings[varname] if varname in global_settings: return global_settings[varname] return default_values[varname]
def _site_is_using_livestatus_proxy(site_id): site_configs = SiteManagementFactory().factory().load_sites() return site_configs[site_id].get("proxy") is not None
def test_update_remote_site_status_host_config(): _write_site_config({ "stable": { "alias": "Die central Site", "socket": ("local", None), "proxy": None, "timeout": 10, "persist": False, "url_prefix": "/stable/", "status_host": None, "disabled": False, "replication": None, "multisiteurl": "", "disable_wato": True, "insecure": False, "user_login": True, "user_sync": "all", "replicate_ec": False, "replicate_mkps": False, }, "remote": { "alias": "Die remote Site 1", "socket": ( "tcp", { "address": ("127.0.0.1", 6810), "tls": ("encrypted", { "verify": True }) }, ), "proxy": { "params": None }, "timeout": 2, "persist": False, "url_prefix": "/remote/", "status_host": ("stable", "af"), "disabled": False, "replication": "slave", "multisiteurl": "http://localhost/remote/check_mk/", "disable_wato": True, "insecure": False, "user_login": True, "user_sync": None, "replicate_ec": True, "replicate_mkps": False, "secret": "watosecret", }, }) update_site_config(SiteId("stable"), SiteId("dingdong")) site_mgmt = SiteManagementFactory().factory() all_sites = site_mgmt.load_sites() # Site entry has been renamed assert "stable" not in all_sites assert "dingdong" in all_sites # URLs have been updated assert all_sites["dingdong"]["url_prefix"] == "/dingdong/" # Remote site URLs have not been modified # Remote site status host was updated assert all_sites["remote"]["url_prefix"] == "/remote/" assert all_sites["remote"][ "multisiteurl"] == "http://localhost/remote/check_mk/" assert all_sites["remote"]["status_host"] == ("dingdong", "af")