Beispiel #1
0
    def _handleUpdateProxyAccount(self, confInfo, args):
        settings = ("proxy_url", "proxy_username", "proxy_password",
                    "proxy_port", "proxy_enabled", "proxy_rdns", "proxy_type")

        if not utils.is_true(args["proxy_enabled"][0]):
            _LOGGER.info("Disabling proxy")
            for k in settings:
                if k not in ("proxy_type", "proxy_rdns"):
                    args[k] = [""]
            args["proxy_enabled"][0] = "0"

            confDict = self.readConf("service_now")
            if (confDict.get("snow_proxy")
                    and confDict["snow_proxy"].get("proxy_url")):
                proxy_url = confDict["snow_proxy"]["proxy_url"]
                _LOGGER.info("Remove credentials for proxy %s", proxy_url)
                mgr = cred.CredentialManager(self.getSessionKey(),
                                             scc.getMgmtUri())
                mgr.delete(proxy_url, self.dummy, self.appName)

        if (utils.is_true(args["proxy_enabled"][0])
                and args["proxy_username"][0]
                and not args["proxy_password"][0]):
            _LOGGER.error("Missing proxy password")
            raise Exception("Missing proxy password")

        self._handleUpdateAccount(confInfo, args, "snow_proxy", settings)
Beispiel #2
0
def build_http_connection(config, timeout=120):
    """
    @config: dict like, proxy and account information are in the following
             format {
                 "username": xx,
                 "password": yy,
                 "proxy_url": zz,
                 "proxy_port": aa,
                 "proxy_username": bb,
                 "proxy_password": cc,
                 "proxy_type": http,http_no_tunnel,sock4,sock5,
                 "proxy_rdns": 0 or 1,
             }
    @return: Http2.Http object
    """

    proxy_type_to_code = {
        "http": socks.PROXY_TYPE_HTTP,
        "http_no_tunnel": socks.PROXY_TYPE_HTTP_NO_TUNNEL,
        "socks4": socks.PROXY_TYPE_SOCKS4,
        "socks5": socks.PROXY_TYPE_SOCKS5,
    }
    if config.get("proxy_type") in proxy_type_to_code:
        proxy_type = proxy_type_to_code[config["proxy_type"]]
    else:
        proxy_type = socks.PROXY_TYPE_HTTP

    rdns = utils.is_true(config.get("proxy_rdns"))

    proxy_info = None

    disable_ssl_validation = utils.is_true(
        config.get("disable_ssl_certificate_validation"))

    if config.get("proxy_url") and config.get("proxy_port"):
        if config.get("proxy_username") and config.get("proxy_password"):
            proxy_info = ProxyInfo(proxy_type=proxy_type,
                                   proxy_host=config["proxy_url"],
                                   proxy_port=int(config["proxy_port"]),
                                   proxy_user=config["proxy_username"],
                                   proxy_pass=config["proxy_password"],
                                   proxy_rdns=rdns)
        else:
            proxy_info = ProxyInfo(proxy_type=proxy_type,
                                   proxy_host=config["proxy_url"],
                                   proxy_port=int(config["proxy_port"]),
                                   proxy_rdns=rdns)
    http = Http(proxy_info=proxy_info,
                timeout=timeout,
                disable_ssl_certificate_validation=disable_ssl_validation)
    if config.get("username") and config.get("password"):
        http.add_credentials(config["username"], config["password"])
    return http
Beispiel #3
0
    def _set_defaults(self, defaults):
        """
        @parser: default collection configs
        """

        default_when = self._get_datetime(defaults["since_when"])
        # collect category interval name, default interval, default priority
        for sc in self.stanza_configs:
            if not sc.get("duration", None):
                sc["duration"] = int(defaults["collection_interval"])
            else:
                sc["duration"] = int(sc["duration"])

            if not sc.get("priority", None):
                sc["priority"] = int(defaults["priority"])
            else:
                sc["priority"] = int(sc["priority"])

            if not sc.get("since_when", None):
                sc["since_when"] = default_when
            else:
                sc["since_when"] = self._get_datetime(sc["since_when"])

            sc["proxy_enabled"] = 0
            if defaults.get("proxy_enabled"):
                if utils.is_true(defaults["proxy_enabled"]):
                    sc["proxy_enabled"] = 1
            sc["record_count"] = int(defaults["record_count"])
            sc["display_value"] = defaults.get("display_value", "")

        self.meta_configs["loglevel"] = defaults["loglevel"]
Beispiel #4
0
 def _get_display_value(self, config):
     dv = config.get("display_value", "")
     if utils.is_false(dv):
         return "false"
     if utils.is_true(dv):
         return "true"
     if dv and dv in self._SUPPORTED_DISPLAY_VALUES:
         return dv
     return sc.DEFAULT_DISPLAY_VALUE
Beispiel #5
0
    def _get_default_configs(self):
        """
        Get default configuration of this TA
        If default/service_now.conf doesn't contain the
        default config assign the default configuration
        """

        defaults = {}
        self.conf_manager.reload_confs(("service_now", ))
        snow_conf = self.conf_manager.get_conf("nobody", self.appname,
                                               "service_now")
        if not snow_conf:
            _LOGGER.error("Failed to get service_now.conf")
            raise Exception("Failed to get service_now.conf")

        snow_conf = {stanza["stanza"]: stanza for stanza in snow_conf}
        for stanza in ("snow_account", "snow_default", "snow_proxy"):
            defaults.update(snow_conf[stanza])

        if defaults["proxy_port"]:
            defaults["proxy_port"] = int(defaults["proxy_port"])

        if defaults["proxy_rdns"]:
            defaults["proxy_rdns"] = utils.is_true(defaults["proxy_rdns"])

        if defaults.get("proxy_type"):
            valid_proxy_types = ("http", "http_no_tunnel", "socks4", "socks5")
            if defaults["proxy_type"] not in valid_proxy_types:
                msg = "Invalid proxy type={}, only {} are supported".format(
                    defaults["proxy_type"], valid_proxy_types)
                _LOGGER.error(msg)
                raise Exception(msg)
        else:
            defaults["proxy_type"] = "http"

        if utils.is_false(defaults["proxy_enabled"]):
            defaults["proxy_url"] = ""
            defaults["proxy_port"] = ""

        conf_copy = copy.deepcopy(defaults)
        self._decrypt_existing_credentials(conf_copy)

        keys = (("collection_interval", 60), ("priority", 10),
                ("loglevel", "INFO"), ("since_when", "now"),
                ("display_value", consts.DEFAULT_DISPLAY_VALUE))

        for k, v in keys:
            if k not in defaults:
                defaults[k] = v

        return defaults