From f71613976fc406250c38cfe5153e7335f50383d9 Mon Sep 17 00:00:00 2001 From: Giulia Naponiello Date: Jun 18 2018 11:45:12 +0000 Subject: [PATCH 1/2] Tolerate invalid gating.yaml Let's tollerate invalid gating.yaml files when they have: * malformed yaml syntax * RemoteRule inside the gating.yaml ...just skip the check and log a warning. --- diff --git a/greenwave/policies.py b/greenwave/policies.py index 014dfed..3f900fc 100644 --- a/greenwave/policies.py +++ b/greenwave/policies.py @@ -2,8 +2,11 @@ from fnmatch import fnmatch import yaml +import logging import greenwave.resources +log = logging.getLogger(__name__) + def validate_policies(policies, disallowed_rules=None): disallowed_rules = disallowed_rules or [] @@ -21,8 +24,10 @@ def validate_policies(policies, disallowed_rules=None): 'is not an instance of Rule' % rule) for disallowed_rule in disallowed_rules: if isinstance(rule, disallowed_rule): - raise RuntimeError('Policies are not configured properly as rule %s ' - 'is an instance of %s' % (rule, disallowed_rule)) + log.warning('Policies are not configured properly as rule %s ' + 'is an instance of %s' % (rule, disallowed_rule)) + return RuntimeError('Policies are not configured properly as rule %s ' + 'is an instance of %s' % (rule, disallowed_rule)) def subject_type_identifier_to_item(subject_type, subject_identifier): @@ -277,13 +282,24 @@ class RemoteRule(Rule): # greenwave extension file not found return [] - policies = yaml.safe_load_all(response) - # policies is a generator, so listifying it - policies = list(policies) + try: + policies = yaml.safe_load_all(response) + # policies is a generator, so listifying it + policies = list(policies) + except yaml.parser.ParserError: + # if the yaml file is malformed we skip these policies + log.warning("Error parsing the founded gating.yaml file.") + return [] # policies in dist-git are always about a package for policy in policies: policy.subject_type = 'koji_build' - validate_policies(policies, [RemoteRule]) + r = validate_policies(policies, [RemoteRule]) + if (r and type(r) == RuntimeError and + r.__str__().startswith("Policies are not configured properly as rule") and + "is an instance of Date: Jun 18 2018 15:50:03 +0000 Subject: [PATCH 2/2] Add a fake result to inform the user of an error Adding a fake result with outcome == NEEDS_INSPECTION when there is a misconfigured gating.yaml file. The user will see this result in the output of the decision API and will notice there is something wrong with his/her configuration. --- diff --git a/greenwave/policies.py b/greenwave/policies.py index 3f900fc..a021792 100644 --- a/greenwave/policies.py +++ b/greenwave/policies.py @@ -45,6 +45,17 @@ def subject_type_identifier_to_item(subject_type, subject_identifier): raise RuntimeError('Unrecognised subject type: %s' % subject_type) +def handle_remote_rule_misconfigured_yaml(results, subject_identifier, subject_type, note): + results.append( + {'data': { + 'item': [subject_identifier], + 'type': [subject_type], + 'id': -1, + 'outcome': 'NEEDS_INSPECTION', + 'note': note}}) + return results + + class Answer(object): """ Represents the result of evaluating a policy rule against a particular @@ -288,7 +299,10 @@ class RemoteRule(Rule): policies = list(policies) except yaml.parser.ParserError: # if the yaml file is malformed we skip these policies - log.warning("Error parsing the founded gating.yaml file.") + msg = "Error parsing the founded gating.yaml file." + log.warning(msg) + results = handle_remote_rule_misconfigured_yaml(results, subject_identifier, + subject_type, msg) return [] # policies in dist-git are always about a package for policy in policies: @@ -297,9 +311,11 @@ class RemoteRule(Rule): if (r and type(r) == RuntimeError and r.__str__().startswith("Policies are not configured properly as rule") and "is an instance of "))) assert len(decision) == 0