Example #1
0
def get_site_config(site_id: SiteId) -> SiteConfiguration:
    s: SiteConfiguration = SiteConfiguration(dict(active_config.sites.get(site_id, {})))
    # Now make sure that all important keys are available.
    # Add missing entries by supplying default values.
    s.setdefault("alias", site_id)
    s.setdefault("socket", ("local", None))
    s.setdefault("url_prefix", "../")  # relative URL from /check_mk/
    # Astroid 2.x bug prevents us from using NewType https://github.com/PyCQA/pylint/issues/2296
    s["id"] = site_id  # pylint: disable=unsupported-assignment-operation
    return SiteConfiguration(s)
Example #2
0
def site(site_id):
    # type: (SiteId) -> SiteConfiguration
    s = SiteConfiguration(dict(sites.get(site_id, {})))
    # Now make sure that all important keys are available.
    # Add missing entries by supplying default values.
    s.setdefault("alias", site_id)
    s.setdefault("socket", ("local", None))
    s.setdefault("url_prefix", "../")  # relative URL from /check_mk/
    s["id"] = site_id
    return s
Example #3
0
def _site_config_for_livestatus(site_id, site):
    # type: (SiteId, SiteConfiguration) -> SiteConfiguration
    """Prepares a site config specification for the livestatus module

    In case the GUI connects to the local livestatus proxy there are several
    special things to do:
    a) Tell livestatus not to strip away the cache header
    b) Connect in plain text to the sites local proxy unix socket
    """
    site = SiteConfiguration(site.copy())

    if site["proxy"] is not None:
        site["cache"] = site["proxy"].get("cache", True)

    else:
        if site["socket"][0] in ["tcp", "tcp6"]:
            site["tls"] = site["socket"][1]["tls"]

    site["socket"] = encode_socket_for_livestatus(site_id, site)

    return site
Example #4
0
def _site_config_for_livestatus(
        site_id: SiteId, site_spec: SiteConfiguration) -> SiteConfiguration:
    """Prepares a site config specification for the livestatus module

    In case the GUI connects to the local livestatus proxy there are several
    special things to do:
    a) Tell livestatus not to strip away the cache header
    b) Connect in plain text to the sites local proxy unix socket
    """
    copied_site: SiteConfiguration = SiteConfiguration(site_spec.copy())

    # Astroid 2.x bug prevents us from using NewType https://github.com/PyCQA/pylint/issues/2296
    # pylint: disable=unsupported-assignment-operation
    if copied_site["proxy"] is not None:
        copied_site["cache"] = site_spec["proxy"].get("cache", True)
    else:
        if copied_site["socket"][0] in ["tcp", "tcp6"]:
            copied_site["tls"] = site_spec["socket"][1]["tls"]
    copied_site["socket"] = encode_socket_for_livestatus(site_id, site_spec)

    return copied_site
Example #5
0
def do_site_login(site: SiteConfiguration, name: UserId, password: str) -> str:
    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 active_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
Example #6
0
def get_effective_global_setting(site_id: SiteId, is_remote_site: bool, varname: str) -> Any:
    global_settings = load_configuration_settings()
    default_values = ABCConfigDomain.get_all_default_globals()

    if is_remote_site:
        current_settings = load_configuration_settings(site_specific=True)
    else:
        sites = SiteManagementFactory.factory().load_sites()
        current_settings = sites.get(site_id, SiteConfiguration({})).get("globals", {})

    if varname in current_settings:
        return current_settings[varname]

    if varname in global_settings:
        return global_settings[varname]

    return default_values[varname]
Example #7
0
def default_single_site_configuration():
    # type: () -> SiteConfigurations
    return SiteConfigurations({
        omd_site(): SiteConfiguration({
            'alias': _("Local site %s") % omd_site(),
            'socket': ("local", None),
            'disable_wato': True,
            'disabled': False,
            'insecure': False,
            'url_prefix': "/%s/" % omd_site(),
            'multisiteurl': '',
            'persist': False,
            'replicate_ec': False,
            'replication': None,
            'timeout': 5,
            'user_login': True,
            'proxy': None,
        })
    })
Example #8
0
def default_single_site_configuration() -> SiteConfigurations:
    return SiteConfigurations(
        {
            omd_site(): SiteConfiguration(
                {
                    "alias": _("Local site %s") % omd_site(),
                    "socket": ("local", None),
                    "disable_wato": True,
                    "disabled": False,
                    "insecure": False,
                    "url_prefix": url_prefix(),
                    "multisiteurl": "",
                    "persist": False,
                    "replicate_ec": False,
                    "replication": None,
                    "timeout": 5,
                    "user_login": True,
                    "proxy": None,
                }
            )
        }
    )
Example #9
0
def test_get_replication_components(edition, monkeypatch, replicate_ec,
                                    replicate_mkps, is_pre_17_remote_site):
    partial_site_config = SiteConfiguration({})
    # Astroid 2.x bug prevents us from using NewType https://github.com/PyCQA/pylint/issues/2296
    # pylint: disable=unsupported-assignment-operation
    if replicate_ec is not None:
        partial_site_config["replicate_ec"] = replicate_ec
    if replicate_mkps is not None:
        partial_site_config["replicate_mkps"] = replicate_mkps

    expected = _expected_replication_paths()

    if not replicate_ec:
        expected = [
            e for e in expected if e.ident not in ["mkeventd", "mkeventd_mkp"]
        ]

    if not replicate_mkps:
        expected = [e for e in expected if e.ident not in ["local", "mkps"]]

    if is_pre_17_remote_site:
        for repl_path in expected:
            if repl_path.ident in {
                    "check_mk",
                    "multisite",
                    "liveproxyd",
                    "mkeventd",
                    "dcd",
                    "mknotify",
            }:
                if "sitespecific.mk" not in repl_path.excludes:
                    repl_path.excludes.append("sitespecific.mk")

            if repl_path.ident == "dcd" and "distributed.mk" not in repl_path.excludes:
                repl_path.excludes.append("distributed.mk")

        expected += [
            ReplicationPath(
                ty="file",
                ident="sitespecific",
                site_path="site_globals/sitespecific.mk",
                excludes=[],
            ),
        ]

    if not is_pre_17_remote_site:
        expected += [
            ReplicationPath(
                ty="file",
                ident="distributed_wato",
                site_path="etc/check_mk/conf.d/distributed_wato.mk",
                excludes=[".*new*"],
            ),
            ReplicationPath(
                ty="dir",
                ident="omd",
                site_path="etc/omd",
                excludes=["allocated_ports", "site.conf", ".*new*"],
            ),
        ]

    assert sorted(
        activate_changes._get_replication_components(
            partial_site_config, is_pre_17_remote_site)) == sorted(expected)