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
|
|
|
|
import re
|
|
|
|
|
2021-01-30 16:54:38 +00:00
|
|
|
from ansible.plugins.loader import module_loader
|
2023-10-16 10:16:44 +00:00
|
|
|
from packaging.version import Version
|
2021-01-30 16:54:38 +00:00
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
from ansiblelater import LOG, utils
|
2019-04-04 13:35:47 +00:00
|
|
|
from ansiblelater.logger import flag_extra
|
2023-02-10 07:51:17 +00:00
|
|
|
from ansiblelater.standard import SingleStandards, StandardBase
|
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.
|
|
|
|
"""
|
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
def __init__(self, filename, settings={}, standards=[]): # noqa
|
2019-04-02 14:34:03 +00:00
|
|
|
self.path = filename
|
|
|
|
self.binary = False
|
|
|
|
self.vault = False
|
|
|
|
self.filetype = 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
|
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
def _get_version(self):
|
2023-01-09 10:59:25 +00:00
|
|
|
name = type(self).__name__
|
2019-04-11 11:00:36 +00:00
|
|
|
path = self.path
|
2019-04-02 14:34:03 +00:00
|
|
|
version = None
|
2021-02-01 19:27:00 +00:00
|
|
|
config_version = self.config["rules"]["version"].strip()
|
|
|
|
|
|
|
|
if config_version:
|
|
|
|
version_config_re = re.compile(r"([\d.]+)")
|
|
|
|
match = version_config_re.match(config_version)
|
|
|
|
if match:
|
|
|
|
version = match.group(1)
|
2019-04-02 14:34:03 +00:00
|
|
|
|
2019-04-11 11:00:36 +00:00
|
|
|
if not self.binary:
|
|
|
|
if isinstance(self, RoleFile):
|
|
|
|
parentdir = os.path.dirname(os.path.abspath(self.path))
|
|
|
|
while parentdir != os.path.dirname(parentdir):
|
|
|
|
meta_file = os.path.join(parentdir, "meta", "main.yml")
|
|
|
|
if os.path.exists(meta_file):
|
|
|
|
path = meta_file
|
|
|
|
break
|
|
|
|
parentdir = os.path.dirname(parentdir)
|
|
|
|
|
2021-02-01 19:27:00 +00:00
|
|
|
version_file_re = re.compile(r"^# Standards:\s*([\d.]+)")
|
2019-04-11 11:00:36 +00:00
|
|
|
with codecs.open(path, mode="rb", encoding="utf-8") as f:
|
|
|
|
for line in f:
|
2021-02-01 19:27:00 +00:00
|
|
|
match = version_file_re.match(line)
|
2019-04-11 11:00:36 +00:00
|
|
|
if match:
|
|
|
|
version = match.group(1)
|
2019-04-02 14:34:03 +00:00
|
|
|
|
2023-01-09 15:25:16 +00:00
|
|
|
if version:
|
2023-01-09 10:59:25 +00:00
|
|
|
LOG.info(f"{name} {path} declares standards version {version}")
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
return version
|
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
def _filter_standards(self):
|
2019-04-03 15:42:46 +00:00
|
|
|
target_standards = []
|
2021-01-30 15:52:48 +00:00
|
|
|
includes = self.config["rules"]["filter"]
|
|
|
|
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:
|
2021-01-30 15:52:48 +00:00
|
|
|
includes = [s.sid for s in self.standards]
|
2019-04-17 10:33:23 +00:00
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
for standard in self.standards:
|
|
|
|
if standard.sid in includes and standard.sid not in excludes:
|
2019-04-17 10:33:23 +00:00
|
|
|
target_standards.append(standard)
|
2019-04-03 15:42:46 +00:00
|
|
|
|
|
|
|
return target_standards
|
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
def review(self):
|
2019-04-02 14:34:03 +00:00
|
|
|
errors = 0
|
2021-01-30 15:52:48 +00:00
|
|
|
self.standards = SingleStandards(self.config["rules"]["standards"]).rules
|
2023-01-09 15:25:16 +00:00
|
|
|
self.version_config = self._get_version()
|
|
|
|
self.version = self.version_config or utils.standards_latest(self.standards)
|
2019-04-02 14:34:03 +00:00
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
for standard in self._filter_standards():
|
2019-04-03 15:42:46 +00:00
|
|
|
if type(self).__name__.lower() not in standard.types:
|
2019-04-02 14:34:03 +00:00
|
|
|
continue
|
2019-04-15 15:26:02 +00:00
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
result = standard.check(self, self.config)
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
if not result:
|
2021-01-30 16:50:40 +00:00
|
|
|
LOG.error(
|
2023-01-09 10:59:25 +00:00
|
|
|
f"Standard '{standard.sid}' returns an empty result object. Check failed!"
|
2020-04-05 12:33:43 +00:00
|
|
|
)
|
2021-01-30 16:50:40 +00:00
|
|
|
continue
|
2020-04-05 12:33:43 +00:00
|
|
|
|
|
|
|
labels = {
|
|
|
|
"tag": "review",
|
2021-01-30 15:52:48 +00:00
|
|
|
"standard": standard.description,
|
2020-04-05 12:33:43 +00:00
|
|
|
"file": self.path,
|
|
|
|
"passed": True
|
|
|
|
}
|
2019-04-03 15:42:46 +00:00
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
if standard.sid and standard.sid.strip():
|
|
|
|
labels["sid"] = standard.sid
|
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
|
|
|
|
|
|
|
sid = self._format_id(standard.sid)
|
|
|
|
path = self.path
|
|
|
|
description = standard.description
|
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
if isinstance(err, StandardBase.Error):
|
2019-04-03 15:42:46 +00:00
|
|
|
err_labels.update(err.to_dict())
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
if not standard.version:
|
2020-04-05 12:33:43 +00:00
|
|
|
LOG.warning(
|
2023-01-09 10:59:25 +00:00
|
|
|
f"{sid}Best practice '{description}' not met:\n{path}:{err}",
|
2020-04-05 12:33:43 +00:00
|
|
|
extra=flag_extra(err_labels)
|
|
|
|
)
|
2023-10-16 10:16:44 +00:00
|
|
|
elif Version(standard.version) > Version(self.version):
|
2020-04-05 12:33:43 +00:00
|
|
|
LOG.warning(
|
2023-01-09 10:59:25 +00:00
|
|
|
f"{sid}Future standard '{description}' not met:\n{path}:{err}",
|
2020-04-05 12:33:43 +00:00
|
|
|
extra=flag_extra(err_labels)
|
|
|
|
)
|
2019-04-02 14:34:03 +00:00
|
|
|
else:
|
2023-01-09 10:59:25 +00:00
|
|
|
msg = f"{sid}Standard '{description}' not met:\n{path}:{err}"
|
2021-10-10 15:00:44 +00:00
|
|
|
|
|
|
|
if standard.sid not in self.config["rules"]["warning_filter"]:
|
|
|
|
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
|
2023-02-10 07:51:17 +00:00
|
|
|
def classify(filename, settings={}, standards=[]): # 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"]:
|
|
|
|
return Task(filename, settings, standards)
|
|
|
|
if parentdir in ["handlers"]:
|
|
|
|
return Handler(filename, settings, standards)
|
|
|
|
if parentdir in ["vars", "defaults"]:
|
|
|
|
return RoleVars(filename, settings, standards)
|
|
|
|
if "group_vars" in filename.split(os.sep):
|
|
|
|
return GroupVars(filename, settings, standards)
|
|
|
|
if "host_vars" in filename.split(os.sep):
|
|
|
|
return HostVars(filename, settings, standards)
|
2021-10-10 20:14:01 +00:00
|
|
|
if parentdir in ["meta"] and "main" in basename:
|
2021-01-30 15:52:48 +00:00
|
|
|
return Meta(filename, settings, standards)
|
2021-10-10 20:14:01 +00:00
|
|
|
if parentdir in ["meta"] and "argument_specs" in basename:
|
|
|
|
return ArgumentSpecs(filename, settings, standards)
|
2021-01-30 15:52:48 +00:00
|
|
|
if (
|
|
|
|
parentdir in ["library", "lookup_plugins", "callback_plugins", "filter_plugins"]
|
|
|
|
or filename.endswith(".py")
|
|
|
|
):
|
|
|
|
return Code(filename, settings, standards)
|
2021-02-12 15:48:45 +00:00
|
|
|
if basename == "inventory" or basename == "hosts" or parentdir in ["inventories"]:
|
2021-01-30 15:52:48 +00:00
|
|
|
return Inventory(filename, settings, standards)
|
2022-12-16 20:48:17 +00:00
|
|
|
if ("rolesfile" in basename or ("requirements" in basename and ext in ["yaml", "yml"])):
|
2021-01-30 15:52:48 +00:00
|
|
|
return Rolesfile(filename, settings, standards)
|
|
|
|
if "Makefile" in basename:
|
|
|
|
return Makefile(filename, settings, standards)
|
|
|
|
if "templates" in filename.split(os.sep) or basename.endswith(".j2"):
|
|
|
|
return Template(filename, settings, standards)
|
|
|
|
if "files" in filename.split(os.sep):
|
|
|
|
return File(filename, settings, standards)
|
|
|
|
if basename.endswith(".yml") or basename.endswith(".yaml"):
|
|
|
|
return Playbook(filename, settings, standards)
|
|
|
|
if "README" in basename:
|
|
|
|
return Doc(filename, settings, standards)
|
|
|
|
return None
|
|
|
|
|
2019-04-05 10:23:18 +00:00
|
|
|
def _format_id(self, standard_id):
|
2023-01-09 10:59:25 +00:00
|
|
|
sid = standard_id.strip()
|
|
|
|
if sid:
|
|
|
|
standard_id = f"[{sid}] "
|
2019-04-05 10:23:18 +00:00
|
|
|
|
2023-06-28 07:55:18 +00:00
|
|
|
return standard_id
|
2019-04-05 10:23:18 +00:00
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
def __repr__(self):
|
2023-01-09 10:59:25 +00:00
|
|
|
return f"{type(self).__name__} ({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
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
def __init__(self, filename, settings={}, standards=[]): # noqa
|
|
|
|
super().__init__(filename, settings, standards)
|
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
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
def __init__(self, filename, settings={}, standards=[]): # noqa
|
|
|
|
super().__init__(filename, settings, standards)
|
2019-04-02 14:34:03 +00:00
|
|
|
self.filetype = "tasks"
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
def __init__(self, filename, settings={}, standards=[]): # noqa
|
|
|
|
super().__init__(filename, settings, standards)
|
2019-04-02 14:34:03 +00:00
|
|
|
self.filetype = "handlers"
|
|
|
|
|
|
|
|
|
|
|
|
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
|