def __init__(self):
        super(KeyringUtils, self).__init__()

        config_file = resilient.get_config_file()
        print(u"Configuration file: {}".format(config_file))

        # Read configuration options.
        if config_file:
            config_path = resilient.ensure_unicode(config_file)
            config_path = os.path.expanduser(config_path)
            if os.path.exists(config_path):
                try:
                    self.config = configparser.ConfigParser(interpolation=None)
                    with open(config_path, 'r', encoding='utf-8') as f:
                        first_byte = f.read(1)
                        if first_byte != u'\ufeff':
                            # Not a BOM, no need to skip first byte
                            f.seek(0)
                        self.config.read_file(f)
                except Exception as exc:
                    logger.warn(u"Couldn't read config file '%s': %s", config_path, exc)
                    self.config = None
            else:
                logger.warn(u"Couldn't read config file '%s'", config_file)
        else:
            logger.warn(u"Couldn't read config file")
 def get_action_field_label(self, fieldname, value_id):
     """Get the label for an action-field value id"""
     field = self._action_fields[fieldname]
     for value in field["values"]:
         if value["enabled"]:
             if value["value"] == value_id:
                 return value["label"]
     return ensure_unicode(value_id)  # fallback
 def get_function_field_label(self, fieldname, value_id):
     """Get the label for a function input-field value id"""
     if self._function_fields is None:
         return None
     field = self._function_fields[fieldname]
     for value in field["values"]:
         if value["enabled"]:
             if value["value"] == value_id:
                 return value["label"]
     return ensure_unicode(value_id)  # fallback
Esempio n. 4
0
 def normalize_name(self, domain):
     """Produce a normalized version for comparison"""
     # Strip leading and trailing spaces
     domain = domain.strip()
     # First, decode IDNA to unicode
     try:
         domain = domain.encode("utf-8").decode("idna")
     except (UnicodeError, UnicodeDecodeError):
         domain = ensure_unicode(domain)
     # Normalize unicode strings
     # domain = unicodedata.normalize("NFKD", domain)
     domain = self.confusable.skeleton(domain)
     return domain
    def normalize_name(self, domain):
        """Produce a normalized version for comparison"""
        # Strip leading and trailing spaces
        domain = domain.strip()
        # First, decode IDNA to unicode
        try:
            domain = domain.encode("utf-8").decode("idna")
        except (UnicodeError, UnicodeDecodeError):
            domain = ensure_unicode(domain)
        # Normalize unicode strings
        # domain = unicodedata.normalize("NFKD", domain)
        domain = self.confusable.skeleton(domain)

        # strip off any aspect of a URL name
        parsed = urlsplit(domain)
        if parsed.scheme:
            return parsed.hostname
        else:
            return parsed.path
def _post_process_args(args):
    # Post-process any options that reference keyring or environment variables
    opts = parse_parameters(vars(args))
    args = ConfigDict(opts)

    # Post-processing for other special options
    password = args.password
    while (not password) and (not args.get("no_prompt_password")):
        password = getpass.getpass()
    args["password"] = ensure_unicode(password)

    if args.get("cafile"):
        args["cafile"] = os.path.expanduser(args.cafile)

    if args.get("stomp_cafile"):
        args["stomp_cafile"] = os.path.expanduser(args.stomp_cafile)

    if args.get("proxy_host"):
        args["proxy"] = get_proxy_dict(args)

    return args
Esempio n. 7
0
    def __init__(self, config_file=None):
        super(ArgumentParser, self).__init__()

        # Read configuration options.
        if config_file:
            config_path = ensure_unicode(config_file)
            config_path = os.path.expanduser(config_path)
            if os.path.exists(config_path):
                try:
                    self.config = configparser.ConfigParser(interpolation=None)
                    with open(config_path, 'r', encoding='utf-8') as f:
                        first_byte = f.read(1)
                        if first_byte != u'\ufeff':
                            # Not a BOM, no need to skip first byte
                            f.seek(0)
                        self.config.read_file(f)
                except Exception as exc:
                    logger.warn(u"Couldn't read config file '%s': %s",
                                config_path, exc)
                    self.config = None
            else:
                logger.warn(u"Couldn't read config file '%s'", config_file)

        default_email = self.getopt("resilient", "email")
        default_password = self.getopt("resilient", "password")
        default_key_id = self.getopt("resilient", "api_key_id")
        default_key_secret = self.getopt("resilient", "api_key_secret")
        default_host = self.getopt("resilient", "host")
        default_port = self.getopt("resilient", "port") or self.DEFAULT_PORT
        default_proxy = self.getopts("resilient", "proxy")
        default_org = self.getopt("resilient", "org")
        default_cafile = self.getopt("resilient", "cafile")
        default_cache_ttl = int(self.getopt("resilient", "cache_ttl") or 0)
        default_proxy_host = self.getopt("resilient", "proxy_host")
        default_proxy_port = self.getopt("resilient", "proxy_port") or 0
        default_proxy_user = self.getopt("resilient", "proxy_user")
        default_proxy_password = self.getopt("resilient", "proxy_password")
        default_stomp_prefetch_limit = int(
            self.getopt("resilient", "stomp_prefetch_limit") or 20)
        default_resilient_mock = self.getopt("resilient", "resilient_mock")

        self.add_argument(
            "--email",
            default=default_email,
            help=
            "The email address to use to authenticate to the Resilient server."
        )

        self.add_argument(
            "--password",
            default=default_password,
            help="WARNING:  This is an insecure option since the password "
            "will be visible to other processes and stored in the "
            "command history.  The password to use to authenticate "
            "to the Resilient server.  If omitted, the you will be prompted.")

        self.add_argument("--api_key_id",
                          default=default_key_id,
                          help="The api key id for API key.")

        self.add_argument(
            "--api_key_secret",
            default=default_key_secret,
            help="WARNING:  This is an insecure option since the key secret "
            "will be visible to other processes and stored in the "
            "command history.  The password to use to authenticate "
            "to the Resilient server.  If omitted, the you will be prompted.")

        self.add_argument("--host",
                          default=default_host,
                          required=default_host is None,
                          help="Resilient server host name.")

        self.add_argument("--port",
                          type=int,
                          default=default_port,
                          help="Resilient server REST API port number.")

        self.add_argument(
            "--proxy",
            default=default_proxy,
            nargs="*",
            help="An optional HTTP proxy to use when connecting.")

        self.add_argument(
            "--org",
            default=default_org,
            help="The name of the organization to use.  If you are a member "
            "of just one organization, then you can omit this argument.")

        self.add_argument(
            "--cafile",
            default=default_cafile,
            help="The name of a file that contains trusted certificates.")

        self.add_argument("--cache-ttl",
                          default=default_cache_ttl or 240,
                          type=int,
                          help="TTL for API responses when using cached_get")

        self.add_argument("--proxy_host",
                          default=default_proxy_host,
                          help="HTTP Proxy host for Resilient Connection.")

        self.add_argument("--proxy_port",
                          type=int,
                          default=default_proxy_port,
                          help="HTTP Proxy port for Resilient Connection.")

        self.add_argument(
            "--proxy_user",
            default=default_proxy_user,
            help="HTTP Proxy username for Resilient connection authentication."
        )

        self.add_argument(
            "--proxy_password",
            default=default_proxy_password,
            help="HTTP Proxy password for Resilient connection authentication."
        )

        self.add_argument(
            "--stomp-prefetch-limit",
            default=default_stomp_prefetch_limit,
            type=int,
            help=
            "MAX number of Action Module messages to send before ACK is required"
        )

        v_resc = get_resilient_circuits_version()

        # Having --resilient-mock here allows us to run unit tests for resilient-circuits and resilient-sdk
        # This argument will be added if:
        # - resilient-circuits is not installed
        # - resilient-circuits is installed and is > 34
        if not v_resc or (v_resc and v_resc.get("major") > 34):
            self.add_argument("--resilient-mock",
                              default=default_resilient_mock,
                              help="<path_to_mock_module>.NameOfMockClass")