Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_unknown_check(ruleset_unknown_check):
with pytest.raises(ColinRulesetException) as ex:
colin.get_checks(ruleset=ruleset_unknown_check)
assert str(ex.value) == "Check i_forgot_the_name can't be loaded, we couldn't find it."
def test_ruleset_version(version, should_raise):
if version == "":
r = {"banana": 123}
else:
r = {"version": version}
if should_raise:
with pytest.raises(ColinRulesetException):
Ruleset(ruleset=r)
else:
assert Ruleset(ruleset=r)
def get_ruleset_struct_from_fileobj(fileobj):
try:
logger.debug("Loading ruleset from file '{}'.".format(fileobj.name))
return RulesetStruct(yaml.safe_load(fileobj))
except Exception as ex:
msg = "Ruleset file '{}' cannot be loaded: {}".format(fileobj.name, ex)
logger.error(msg)
raise ColinRulesetException(msg)
"default"
:param ruleset_file: fileobj instance holding ruleset configuration
:param ruleset: dict, content of a ruleset file
:param checks_paths: list of str, directories where the checks are present
"""
self.check_loader = CheckLoader(get_checks_paths(checks_paths))
if ruleset:
self.ruleset_struct = RulesetStruct(ruleset)
elif ruleset_file:
self.ruleset_struct = get_ruleset_struct_from_fileobj(ruleset_file)
else:
logger.debug("Loading ruleset with the name '{}'.".format(ruleset_name))
ruleset_path = get_ruleset_file(ruleset=ruleset_name)
self.ruleset_struct = get_ruleset_struct_from_file(ruleset_path)
if self.ruleset_struct.version not in ["1", 1]:
raise ColinRulesetException("colin accepts only ruleset version '1'. You provided %r"
% self.ruleset_struct.version)
local_share = os.path.join(os.path.expanduser("~"),
".local",
RULESET_DIRECTORY)
if os.path.isdir(local_share):
logger.debug("Local ruleset directory found ('{}').".format(local_share))
ruleset_dirs.append(local_share)
usr_local_share = os.path.join("/usr/local", RULESET_DIRECTORY)
if os.path.isdir(usr_local_share):
logger.debug("Global ruleset directory found ('{}').".format(usr_local_share))
ruleset_dirs.append(usr_local_share)
if not ruleset_dirs:
msg = "Ruleset directory cannot be found."
logger.warning(msg)
raise ColinRulesetException(msg)
return ruleset_dirs
def get_ruleset_struct_from_file(file_path):
try:
with open(file_path, "r") as fd:
return get_ruleset_struct_from_fileobj(fd)
except ColinRulesetException as ex:
raise ex
except Exception as ex:
msg = "Ruleset '{}' cannot be loaded: {}".format(file_path, ex)
logger.error(msg)
raise ColinRulesetException(msg)
:param di: dict
:param required: bool, raises an exc if value is not found, otherwise returns None
:param path: list of str to navigate in the dict
:return: your value
"""
r = di
for p in path:
try:
r = r[p]
except KeyError:
if required:
logger.error("can't locate %s in ruleset dict, keys present: %s",
p, list(r.keys()))
logger.debug("full dict = %s", r)
raise ColinRulesetException("Validation error: can't locate %s in ruleset." % p)
return
return r
def get_ruleset_struct_from_file(file_path):
try:
with open(file_path, "r") as fd:
return get_ruleset_struct_from_fileobj(fd)
except ColinRulesetException as ex:
raise ex
except Exception as ex:
msg = "Ruleset '{}' cannot be loaded: {}".format(file_path, ex)
logger.error(msg)
raise ColinRulesetException(msg)