コード例 #1
0
 def __init__(self, api_key=None, timeout=7, use_cache=True):
     if api_key is None:
         api_key = load_config()["api_key"]
     self.api_key = api_key
     self.timeout = timeout
     self.use_cache = use_cache
     self.session = requests.Session()
コード例 #2
0
ファイル: test_util.py プロジェクト: manojnclude/pygreynoise
    def test_defaults(self, os):
        """Default values returned if configuration file is not found."""
        os.environ = {}
        os.path.isfile.return_value = False

        config = load_config()
        assert config == {"api_key": "", "timeout": 60}
コード例 #3
0
ファイル: decorator.py プロジェクト: mtonsmann/SA-GreyNoise
    def wrapper(*args, **kwargs):
        context = click.get_current_context()
        api_key = context.params.get("api_key")
        offering = context.params.get("offering")
        config = load_config()

        if api_key is None:
            if not config["api_key"]:
                prog_name = context.parent.info_name
                click.echo(
                    "\nError: API key not found.\n\n"
                    "To fix this problem, please use any of the following methods "
                    "(in order of precedence):\n"
                    "- Pass it using the -k/--api-key option.\n"
                    "- Set it in the GREYNOISE_API_KEY environment variable.\n"
                    "- Run {!r} to save it to the configuration file.\n".
                    format("{} setup".format(prog_name)))
                context.exit(-1)
            api_key = config["api_key"]

        if offering is None:
            if not config["offering"]:
                offering = "enterprise"
            else:
                offering = config["offering"]

        api_client = GreyNoise(
            api_key=api_key,
            offering=offering,
            timeout=config["timeout"],
            integration_name="cli",
        )
        return function(api_client, *args, **kwargs)
コード例 #4
0
    def test_default_api_key(self, os):
        """API key set to empty string by default"""
        os.environ = {}
        os.path.isfile.return_value = False

        config = load_config()
        assert config["api_key"] == ""
コード例 #5
0
ファイル: api.py プロジェクト: manojnclude/pygreynoise
 def __init__(self, api_key=None, timeout=None, use_cache=True):
     if api_key is None or timeout is None:
         config = load_config()
         if api_key is None:
             api_key = config["api_key"]
         if timeout is None:
             timeout = config["timeout"]
     self.api_key = api_key
     self.timeout = timeout
     self.use_cache = use_cache
     self.session = requests.Session()
コード例 #6
0
    def test_api_key_from_environment_variable(self, os, open):
        """API key value retrieved from environment variable."""
        expected = "<api_key>"

        os.environ = {"GREYNOISE_API_KEY": expected}
        os.path.isfile.return_value = True
        file_content = textwrap.dedent("""\
            [greynoise]
            api_key = unexpected
            """)
        open().__enter__.return_value = StringIO(file_content)

        config = load_config()
        assert config["api_key"] == expected
        open().__enter__.assert_called()
コード例 #7
0
    def test_api_key_from_configuration_file(self, os, open):
        """API key value retrieved from configuration file."""
        expected = "<api_key>"

        os.environ = {}
        os.path.isfile.return_value = True
        file_content = textwrap.dedent("""\
            [greynoise]
            api_key = {}
            """.format(expected))
        open().__enter__.return_value = StringIO(file_content)

        config = load_config()
        assert config["api_key"] == expected
        open().__enter__.assert_called()
コード例 #8
0
ファイル: test_util.py プロジェクト: manojnclude/pygreynoise
    def test_timeout_from_environment_variable(self, os, open):
        """Timeout value retrieved from environment variable."""
        expected = {"api_key": "<api_key>", "timeout": 123456}

        os.environ = {"GREYNOISE_TIMEOUT": str(expected["timeout"])}
        os.path.isfile.return_value = True
        file_content = textwrap.dedent("""\
            [greynoise]
            api_key = {}
            timeout = unexpected
            """.format(expected["api_key"]))
        open().__enter__.return_value = StringIO(file_content)

        config = load_config()
        assert config == expected
        open().__enter__.assert_called()
コード例 #9
0
ファイル: test_util.py プロジェクト: manojnclude/pygreynoise
    def test_values_from_configuration_file(self, os, open):
        """Values retrieved from configuration file."""
        expected = {"api_key": "<api_key>", "timeout": 123456}

        os.environ = {}
        os.path.isfile.return_value = True
        file_content = textwrap.dedent("""\
            [greynoise]
            api_key = {}
            timeout = {}
            """.format(expected["api_key"], expected["timeout"]))
        open().__enter__.return_value = StringIO(file_content)

        config = load_config()
        assert config == expected
        open().__enter__.assert_called()
コード例 #10
0
ファイル: __init__.py プロジェクト: mtonsmann/SA-GreyNoise
    def __init__(
        self,
        api_key=None,
        api_server=None,
        timeout=None,
        proxy=None,
        use_cache=True,
        integration_name=None,
        cache_max_size=None,
        cache_ttl=None,
        offering=None,
    ):
        if any(
            configuration_value is None
            for configuration_value in (api_key, timeout, api_server, proxy, offering)
        ):
            config = load_config()
            if api_key is None:
                api_key = config["api_key"]
            if api_server is None:
                api_server = config["api_server"]
            if timeout is None:
                timeout = config["timeout"]
            if proxy is None:
                proxy = config["proxy"]
            if offering is None:
                offering = config["offering"]
        self.api_key = api_key
        self.api_server = api_server
        self.timeout = timeout
        self.proxy = proxy
        self.use_cache = use_cache
        self.integration_name = integration_name
        self.session = requests.Session()
        self.offering = offering

        if cache_ttl is None or not isinstance(cache_ttl, int):
            cache_ttl = 3600
        self.cache_ttl = cache_ttl

        if cache_max_size is None or not isinstance(cache_max_size, int):
            cache_max_size = 1000
        self.cache_max_size = cache_max_size

        if use_cache:
            self.ip_quick_check_cache = initialize_cache(cache_max_size, cache_ttl)
            self.ip_context_cache = initialize_cache(cache_max_size, cache_ttl)
コード例 #11
0
ファイル: test_util.py プロジェクト: manojnclude/pygreynoise
    def test_timeout_from_environment_variable_with_invalid_value(
            self, os, open):
        """Invalid timeout value in environment variable is ignored."""
        expected = {"api_key": "<api_key>", "timeout": 123456}

        os.environ = {"GREYNOISE_TIMEOUT": "invalid"}
        os.path.isfile.return_value = True
        file_content = textwrap.dedent("""\
            [greynoise]
            api_key = {}
            timeout = {}
            """.format(expected["api_key"], expected["timeout"]))
        open().__enter__.return_value = StringIO(file_content)

        config = load_config()
        assert config == expected
        open().__enter__.assert_called()
コード例 #12
0
 def __init__(
     self,
     api_key=None,
     api_server=None,
     timeout=None,
     use_cache=True,
     integration_name=None,
 ):
     if any(configuration_value is None
            for configuration_value in (api_key, timeout, api_server)):
         config = load_config()
         if api_key is None:
             api_key = config["api_key"]
         if api_server is None:
             api_server = config["api_server"]
         if timeout is None:
             timeout = config["timeout"]
     self.api_key = api_key
     self.api_server = api_server
     self.timeout = timeout
     self.use_cache = use_cache
     self.integration_name = integration_name
     self.session = requests.Session()