Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
os.environ = {"GREYNOISE_TIMEOUT": str(expected["timeout"])}
os.path.isfile.return_value = True
file_content = textwrap.dedent(
"""\
[greynoise]
api_key = {}
api_server = {}
timeout = unexpected
""".format(
expected["api_key"], expected["api_server"],
)
)
open().__enter__.return_value = StringIO(file_content)
config = load_config()
assert config == expected
open().__enter__.assert_called()
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": "",
"api_server": "https://enterprise.api.greynoise.io",
"timeout": 60,
}
os.environ = {}
os.path.isfile.return_value = True
file_content = textwrap.dedent(
"""\
[greynoise]
api_key = {}
api_server = {}
timeout = {}
""".format(
expected["api_key"], expected["api_server"], expected["timeout"],
)
)
open().__enter__.return_value = StringIO(file_content)
config = load_config()
assert config == expected
open().__enter__.assert_called()
os.environ = {"GREYNOISE_API_KEY": expected["api_key"]}
os.path.isfile.return_value = True
file_content = textwrap.dedent(
"""\
[greynoise]
api_key = unexpected
api_server = {}
timeout = {}
""".format(
expected["api_server"], expected["timeout"],
)
)
open().__enter__.return_value = StringIO(file_content)
config = load_config()
assert config == expected
open().__enter__.assert_called()
os.environ = {"GREYNOISE_TIMEOUT": "invalid"}
os.path.isfile.return_value = True
file_content = textwrap.dedent(
"""\
[greynoise]
api_key = {}
api_server = {}
timeout = {}
""".format(
expected["api_key"], expected["api_server"], expected["timeout"],
)
)
open().__enter__.return_value = StringIO(file_content)
config = load_config()
assert config == expected
open().__enter__.assert_called()
def wrapper(*args, **kwargs):
context = click.get_current_context()
api_key = context.params.get("api_key")
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"]
quick_check_parser.set_defaults(func=quick_check)
multi_quick_check_parser = subparsers.add_parser(
"multi_quick_check", help=multi_quick_check.__doc__.rstrip(".")
)
multi_quick_check_parser.add_argument(
"ip_address", type=ip_address_parameter, nargs="+", help="IP address"
)
multi_quick_check_parser.set_defaults(func=multi_quick_check)
actors_parser = subparsers.add_parser("actors", help=actors.__doc__.rstrip("."))
actors_parser.set_defaults(func=actors)
args = parser.parse_args(argv)
if not args.api_key:
config = load_config()
if not config["api_key"]:
prog = os.path.basename(sys.argv[0])
print(
"Error: 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)
)
)
sys.exit(-1)
args.api_key = config["api_key"]
args.api_client = GreyNoise(args.api_key)
def __init__(self, api_key=None, api_server=None, timeout=None, use_cache=True):
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.session = requests.Session()
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()