Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate rules objects loaded from plugin rule modules #1542

Merged
merged 1 commit into from May 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/ansiblelint/rules/__init__.py
Expand Up @@ -176,10 +176,13 @@ def matchyaml(self, file: Lintable) -> List[MatchError]:
return matches


def load_plugins(directory: str) -> List[AnsibleLintRule]:
"""Return a list of rule classes."""
result = []
def is_valid_rule(rule: AnsibleLintRule) -> bool:
"""Check if given rule is valid or not."""
return isinstance(rule, AnsibleLintRule) and bool(rule.id) and bool(rule.shortdesc)


def load_plugins(directory: str) -> Iterator[AnsibleLintRule]:
"""Yield a rule class."""
for pluginfile in glob.glob(os.path.join(directory, '[A-Za-z]*.py')):

pluginname = os.path.basename(pluginfile.replace('.py', ''))
Expand All @@ -188,9 +191,13 @@ def load_plugins(directory: str) -> List[AnsibleLintRule]:
if spec and isinstance(spec.loader, Loader):
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
obj = getattr(module, pluginname)()
result.append(obj)
return result
try:
rule = getattr(module, pluginname)()
if is_valid_rule(rule):
yield rule

except (TypeError, ValueError, AttributeError):
_logger.warning("Skipped invalid rule from %s", pluginname)


class RulesCollection:
Expand Down