2019-04-05 20:05:06 +00:00
|
|
|
"""Review candidates."""
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
import codecs
|
2019-04-03 15:42:46 +00:00
|
|
|
import copy
|
2019-04-02 14:34:03 +00:00
|
|
|
import os
|
|
|
|
|
2021-01-30 16:54:38 +00:00
|
|
|
from ansible.plugins.loader import module_loader
|
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
from ansiblelater import LOG
|
2019-04-04 13:35:47 +00:00
|
|
|
from ansiblelater.logger import flag_extra
|
2024-01-25 20:40:15 +00:00
|
|
|
from ansiblelater.rule import RuleBase, SingleRules
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
class Candidate:
|
2019-04-02 14:34:03 +00:00
|
|
|
"""
|
|
|
|
Meta object for all files which later has to process.
|
|
|
|
|
|
|
|
Each file passed to later will be classified by type and
|
|
|
|
bundled with necessary meta informations for rule processing.
|
|
|
|
"""
|
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
def __init__(self, filename, settings={}, rules=[]): # noqa
|
2019-04-02 14:34:03 +00:00
|
|
|
self.path = filename
|
|
|
|
self.binary = False
|
|
|
|
self.vault = False
|
2024-01-30 21:35:45 +00:00
|
|
|
self.filemeta = type(self).__name__.lower()
|
|
|
|
self.kind = type(self).__name__.lower()
|
2021-01-09 12:16:08 +00:00
|
|
|
self.faulty = False
|
2021-01-30 15:52:48 +00:00
|
|
|
self.config = settings.config
|
|
|
|
self.settings = settings
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
with codecs.open(filename, mode="rb", encoding="utf-8") as f:
|
|
|
|
if f.readline().startswith("$ANSIBLE_VAULT"):
|
|
|
|
self.vault = True
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
self.binary = True
|
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
def _filter_rules(self):
|
|
|
|
target_rules = []
|
|
|
|
includes = self.config["rules"]["include_filter"]
|
2021-01-30 15:52:48 +00:00
|
|
|
excludes = self.config["rules"]["exclude_filter"]
|
2019-04-03 15:42:46 +00:00
|
|
|
|
2019-04-17 10:33:23 +00:00
|
|
|
if len(includes) == 0:
|
2024-01-27 18:56:35 +00:00
|
|
|
includes = [s.rid for s in self.rules]
|
2019-04-17 10:33:23 +00:00
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
for rule in self.rules:
|
2024-01-27 18:56:35 +00:00
|
|
|
if rule.rid in includes and rule.rid not in excludes:
|
2024-01-25 20:40:15 +00:00
|
|
|
target_rules.append(rule)
|
2019-04-03 15:42:46 +00:00
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
return target_rules
|
2019-04-03 15:42:46 +00:00
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
def review(self):
|
2019-04-02 14:34:03 +00:00
|
|
|
errors = 0
|
2024-01-25 20:40:15 +00:00
|
|
|
self.rules = SingleRules(self.config["rules"]["dir"]).rules
|
2019-04-02 14:34:03 +00:00
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
for rule in self._filter_rules():
|
2024-01-30 21:35:45 +00:00
|
|
|
if self.kind not in rule.types:
|
2019-04-02 14:34:03 +00:00
|
|
|
continue
|
2019-04-15 15:26:02 +00:00
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
result = rule.check(self, self.config)
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
if not result:
|
2024-01-27 18:56:35 +00:00
|
|
|
LOG.error(f"rule '{rule.rid}' returns an empty result object. Check failed!")
|
2021-01-30 16:50:40 +00:00
|
|
|
continue
|
2020-04-05 12:33:43 +00:00
|
|
|
|
|
|
|
labels = {
|
|
|
|
"tag": "review",
|
2024-01-25 20:40:15 +00:00
|
|
|
"rule": rule.description,
|
2020-04-05 12:33:43 +00:00
|
|
|
"file": self.path,
|
2023-11-10 13:50:48 +00:00
|
|
|
"passed": True,
|
2020-04-05 12:33:43 +00:00
|
|
|
}
|
2019-04-03 15:42:46 +00:00
|
|
|
|
2024-01-27 18:56:35 +00:00
|
|
|
if rule.rid and rule.rid.strip():
|
|
|
|
labels["rid"] = rule.rid
|
2019-04-05 10:23:18 +00:00
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
for err in result.errors:
|
2019-04-03 15:42:46 +00:00
|
|
|
err_labels = copy.copy(labels)
|
|
|
|
err_labels["passed"] = False
|
2023-01-09 10:59:25 +00:00
|
|
|
|
2024-01-27 18:56:35 +00:00
|
|
|
rid = self._format_id(rule.rid)
|
2023-01-09 10:59:25 +00:00
|
|
|
path = self.path
|
2024-01-25 20:40:15 +00:00
|
|
|
description = rule.description
|
2023-01-09 10:59:25 +00:00
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
if isinstance(err, RuleBase.Error):
|
2019-04-03 15:42:46 +00:00
|
|
|
err_labels.update(err.to_dict())
|
|
|
|
|
2024-01-27 18:56:35 +00:00
|
|
|
msg = f"{rid}rule '{description}' not met:\n{path}:{err}"
|
2021-10-10 15:00:44 +00:00
|
|
|
|
2024-01-27 18:56:35 +00:00
|
|
|
if rule.rid not in self.config["rules"]["warning_filter"]:
|
2024-01-25 20:40:15 +00:00
|
|
|
LOG.error(msg, extra=flag_extra(err_labels))
|
|
|
|
errors = errors + 1
|
|
|
|
else:
|
|
|
|
LOG.warning(msg, extra=flag_extra(err_labels))
|
2019-04-02 14:34:03 +00:00
|
|
|
|
2019-04-15 15:26:02 +00:00
|
|
|
return errors
|
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
@staticmethod
|
2024-01-25 20:40:15 +00:00
|
|
|
def classify(filename, settings={}, rules=[]): # noqa
|
2021-01-30 15:52:48 +00:00
|
|
|
parentdir = os.path.basename(os.path.dirname(filename))
|
|
|
|
basename = os.path.basename(filename)
|
2022-12-16 20:48:17 +00:00
|
|
|
ext = os.path.splitext(filename)[1][1:]
|
2021-01-30 15:52:48 +00:00
|
|
|
|
|
|
|
if parentdir in ["tasks"]:
|
2024-01-25 20:40:15 +00:00
|
|
|
return Task(filename, settings, rules)
|
2021-01-30 15:52:48 +00:00
|
|
|
if parentdir in ["handlers"]:
|
2024-01-25 20:40:15 +00:00
|
|
|
return Handler(filename, settings, rules)
|
2021-01-30 15:52:48 +00:00
|
|
|
if parentdir in ["vars", "defaults"]:
|
2024-01-25 20:40:15 +00:00
|
|
|
return RoleVars(filename, settings, rules)
|
2021-01-30 15:52:48 +00:00
|
|
|
if "group_vars" in filename.split(os.sep):
|
2024-01-25 20:40:15 +00:00
|
|
|
return GroupVars(filename, settings, rules)
|
2021-01-30 15:52:48 +00:00
|
|
|
if "host_vars" in filename.split(os.sep):
|
2024-01-25 20:40:15 +00:00
|
|
|
return HostVars(filename, settings, rules)
|
2021-10-10 20:14:01 +00:00
|
|
|
if parentdir in ["meta"] and "main" in basename:
|
2024-01-25 20:40:15 +00:00
|
|
|
return Meta(filename, settings, rules)
|
2021-10-10 20:14:01 +00:00
|
|
|
if parentdir in ["meta"] and "argument_specs" in basename:
|
2024-01-25 20:40:15 +00:00
|
|
|
return ArgumentSpecs(filename, settings, rules)
|
2023-11-10 13:50:48 +00:00
|
|
|
if parentdir in [
|
|
|
|
"library",
|
|
|
|
"lookup_plugins",
|
|
|
|
"callback_plugins",
|
|
|
|
"filter_plugins",
|
|
|
|
] or filename.endswith(".py"):
|
2024-01-25 20:40:15 +00:00
|
|
|
return Code(filename, settings, rules)
|
2021-02-12 15:48:45 +00:00
|
|
|
if basename == "inventory" or basename == "hosts" or parentdir in ["inventories"]:
|
2024-01-25 20:40:15 +00:00
|
|
|
return Inventory(filename, settings, rules)
|
2023-11-10 13:50:48 +00:00
|
|
|
if "rolesfile" in basename or ("requirements" in basename and ext in ["yaml", "yml"]):
|
2024-01-25 20:40:15 +00:00
|
|
|
return Rolesfile(filename, settings, rules)
|
2021-01-30 15:52:48 +00:00
|
|
|
if "Makefile" in basename:
|
2024-01-25 20:40:15 +00:00
|
|
|
return Makefile(filename, settings, rules)
|
2021-01-30 15:52:48 +00:00
|
|
|
if "templates" in filename.split(os.sep) or basename.endswith(".j2"):
|
2024-01-25 20:40:15 +00:00
|
|
|
return Template(filename, settings, rules)
|
2021-01-30 15:52:48 +00:00
|
|
|
if "files" in filename.split(os.sep):
|
2024-01-25 20:40:15 +00:00
|
|
|
return File(filename, settings, rules)
|
2021-01-30 15:52:48 +00:00
|
|
|
if basename.endswith(".yml") or basename.endswith(".yaml"):
|
2024-01-25 20:40:15 +00:00
|
|
|
return Playbook(filename, settings, rules)
|
2021-01-30 15:52:48 +00:00
|
|
|
if "README" in basename:
|
2024-01-25 20:40:15 +00:00
|
|
|
return Doc(filename, settings, rules)
|
2021-01-30 15:52:48 +00:00
|
|
|
return None
|
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
def _format_id(self, rule_id):
|
2024-01-27 18:56:35 +00:00
|
|
|
rid = rule_id.strip()
|
|
|
|
if rid:
|
|
|
|
rule_id = f"[{rid}] "
|
2019-04-05 10:23:18 +00:00
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
return rule_id
|
2019-04-05 10:23:18 +00:00
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
def __repr__(self):
|
2024-01-30 21:35:45 +00:00
|
|
|
return f"{self.kind} ({self.path})"
|
2019-04-02 14:34:03 +00:00
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
def __getitem__(self, item):
|
2019-04-02 14:34:03 +00:00
|
|
|
return self.__dict__.get(item)
|
|
|
|
|
|
|
|
|
|
|
|
class RoleFile(Candidate):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible role file."""
|
2020-04-05 12:33:43 +00:00
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
def __init__(self, filename, settings={}, rules=[]): # noqa
|
|
|
|
super().__init__(filename, settings, rules)
|
2019-04-02 14:34:03 +00:00
|
|
|
|
2019-04-03 15:42:46 +00:00
|
|
|
parentdir = os.path.dirname(os.path.abspath(filename))
|
|
|
|
while parentdir != os.path.dirname(parentdir):
|
|
|
|
role_modules = os.path.join(parentdir, "library")
|
|
|
|
if os.path.exists(role_modules):
|
|
|
|
module_loader.add_directory(role_modules)
|
|
|
|
break
|
|
|
|
parentdir = os.path.dirname(parentdir)
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Playbook(Candidate):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible playbook."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Task(RoleFile):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible task file."""
|
2020-04-05 12:33:43 +00:00
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
def __init__(self, filename, settings={}, rules=[]): # noqa
|
|
|
|
super().__init__(filename, settings, rules)
|
2024-01-30 21:35:45 +00:00
|
|
|
self.filemeta = "tasks"
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Handler(RoleFile):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible handler file."""
|
2020-04-05 12:33:43 +00:00
|
|
|
|
2024-01-25 20:40:15 +00:00
|
|
|
def __init__(self, filename, settings={}, rules=[]): # noqa
|
|
|
|
super().__init__(filename, settings, rules)
|
2024-01-30 21:35:45 +00:00
|
|
|
self.filemeta = "handlers"
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Vars(Candidate):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible vars file."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-01-09 19:41:46 +00:00
|
|
|
class InventoryVars(Candidate):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible inventory vars."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class HostVars(InventoryVars):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible host vars."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class GroupVars(InventoryVars):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible group vars."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class RoleVars(RoleFile):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible role vars."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Meta(RoleFile):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible meta file."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-10-10 20:14:01 +00:00
|
|
|
class ArgumentSpecs(RoleFile):
|
|
|
|
"""Object classified as Ansible roles argument specs file."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-01-09 19:41:46 +00:00
|
|
|
class Inventory(Candidate):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible inventory file."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-01-09 19:41:46 +00:00
|
|
|
class Code(Candidate):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as code file."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Template(RoleFile):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible template file."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-01-09 19:41:46 +00:00
|
|
|
class Doc(Candidate):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as documentation file."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-01-09 19:41:46 +00:00
|
|
|
class Makefile(Candidate):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as makefile."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class File(RoleFile):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as generic file."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-01-09 19:41:46 +00:00
|
|
|
class Rolesfile(Candidate):
|
2020-04-05 12:54:39 +00:00
|
|
|
"""Object classified as Ansible roles file."""
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
pass
|