Skip to content

Commit

Permalink
refactor(evaluator): run black
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomáš Sasák committed Jun 26, 2023
1 parent c300303 commit d6feeb8
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 176 deletions.
2 changes: 1 addition & 1 deletion evaluator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ After given evaluation, the new changes are sent to the Notificator service, `vu
},
"timestamp": <timestamp when message was recieved>
}
```
```
80 changes: 53 additions & 27 deletions evaluator/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
from common.config import Config
from common.utils import get_available_remediation_type


CFG = Config()
PROMETHEUS_PORT = CFG.prometheus_port or str(CFG.evaluator_prometheus_port)

# Prometheus timings
VMAAS_EVAL_TIME = Histogram("ve_evaluator_vmaas_evaluation_seconds", "Time spent checking a system for vmaas hits",
buckets=[0.1, 0.25, 0.5, 0.75, 1.0, 1.5, 2, 5])
EVAL_TIME = Histogram("ve_evaluator_evaluation_seconds", "Time spent fully evaluating a system",
buckets=[0.1, 0.25, 0.5, 0.75, 1.0, 1.5, 2, 5])
RULES_EVAL_TIME = Histogram("ve_evaluator_rules_evaluation_seconds", "Time spent checking a system for rule hits",
buckets=[0.1, 0.25, 0.5, 0.75, 1.0, 1.5, 2, 5])
VMAAS_EVAL_TIME = Histogram(
"ve_evaluator_vmaas_evaluation_seconds", "Time spent checking a system for vmaas hits", buckets=[0.1, 0.25, 0.5, 0.75, 1.0, 1.5, 2, 5]
)
EVAL_TIME = Histogram(
"ve_evaluator_evaluation_seconds", "Time spent fully evaluating a system", buckets=[0.1, 0.25, 0.5, 0.75, 1.0, 1.5, 2, 5]
)
RULES_EVAL_TIME = Histogram(
"ve_evaluator_rules_evaluation_seconds", "Time spent checking a system for rule hits", buckets=[0.1, 0.25, 0.5, 0.75, 1.0, 1.5, 2, 5]
)

# Prometheus counts
EVAL_COUNT = Counter("ve_evaluator_evaluations", "Number of evaluations attempted")
Expand Down Expand Up @@ -50,8 +54,20 @@
# system_platform row taken from DB
SystemPlatform = namedtuple("SystemPlatform", ["id", "inventory_id", "rh_account_id", "vmaas_json", "rule_results"])
# single vulnerability stored inside db
VulnerabilityDB = namedtuple("VulnerabilityDB", ["state", "sv_id", "rule_id", "when_mitigated", "mitigation_reason",
"advisories", "advisory_available", "rule_hit_details", "remediation_type_id"])
VulnerabilityDB = namedtuple(
"VulnerabilityDB",
[
"state",
"sv_id",
"rule_id",
"when_mitigated",
"mitigation_reason",
"advisories",
"advisory_available",
"rule_hit_details",
"remediation_type_id",
],
)


class VmaasErrorException(Exception):
Expand All @@ -64,13 +80,15 @@ class EvaluatorException(Exception):

class EvaluatorMessageType(Enum):
"""Message types which can arrive at kafka"""

EVALUATE_SYSTEM = "upload_new_file"
RE_EVALUATE_SYSTEM = "re-evaluate_system"


@dataclass
class SystemVulnerabilitiesRow:
"""Class represents single vulnerability for system"""

# pylint: disable=too-many-instance-attributes
state: VulnerabilityState

Expand All @@ -91,7 +109,9 @@ class SystemVulnerabilitiesRow:

def _populate_remediation_type(self):
"""Populate the remediation type id field"""
self.remediation_type_id = get_available_remediation_type(self.advisory_available, self.when_mitigated, self.mitigation_reason, self.playbook_count)
self.remediation_type_id = get_available_remediation_type(
self.advisory_available, self.when_mitigated, self.mitigation_reason, self.playbook_count
)

def __post_init__(self):
"""Called after constructor"""
Expand All @@ -108,21 +128,23 @@ def add_rule_info(self, rule_id: int, rule_hit_details: str, playbook_count: int

def format_db_dict(self) -> Dict:
"""Returns structure in dict, for DB operations"""
return {"state": self.state,
"cve_id": self.cve_id,
"advisories": self.advisories,
"advisory_available": self.advisory_available,
"when_mitigated": self.when_mitigated,
"rule_id": self.rule_id,
"rule_hit_details": self.rule_hit_details,
"mitigation_reason": self.mitigation_reason,
"remediation_type_id": self.remediation_type_id,
"system_id": self.system_id,
"rh_account_id": self.rh_account_id}
return {
"state": self.state,
"cve_id": self.cve_id,
"advisories": self.advisories,
"advisory_available": self.advisory_available,
"when_mitigated": self.when_mitigated,
"rule_id": self.rule_id,
"rule_hit_details": self.rule_hit_details,
"mitigation_reason": self.mitigation_reason,
"remediation_type_id": self.remediation_type_id,
"system_id": self.system_id,
"rh_account_id": self.rh_account_id,
}

def should_update_db(self, vulnerability_db: VulnerabilityDB) -> bool:
"""Resolves if system_vulnerabilities row should be updated,
compared to what is already inside of the DB"""
compared to what is already inside of the DB"""
if vulnerability_db.state is not self.state:
return True

Expand All @@ -135,17 +157,21 @@ def should_update_db(self, vulnerability_db: VulnerabilityDB) -> bool:
if vulnerability_db.remediation_type_id != self.remediation_type_id:
should_update = True

if (self.state is VulnerabilityState.VULNERABLE_BY_PACKAGE
or self.state is VulnerabilityState.VULNERABLE_BY_RULE_AND_PACKAGE
or self.state is VulnerabilityState.VULNERABLE_BY_PACKAGE_NOT_RULE):
if (
self.state is VulnerabilityState.VULNERABLE_BY_PACKAGE
or self.state is VulnerabilityState.VULNERABLE_BY_RULE_AND_PACKAGE
or self.state is VulnerabilityState.VULNERABLE_BY_PACKAGE_NOT_RULE
):
if vulnerability_db.advisories != self.advisories:
should_update = True
if vulnerability_db.advisory_available != self.advisory_available:
should_update = True

if (self.state is VulnerabilityState.VULNERABLE_BY_RULE
or self.state is VulnerabilityState.VULNERABLE_BY_RULE_AND_PACKAGE
or self.state is VulnerabilityState.VULNERABLE_BY_PACKAGE_NOT_RULE):
if (
self.state is VulnerabilityState.VULNERABLE_BY_RULE
or self.state is VulnerabilityState.VULNERABLE_BY_RULE_AND_PACKAGE
or self.state is VulnerabilityState.VULNERABLE_BY_PACKAGE_NOT_RULE
):
if vulnerability_db.rule_id != self.rule_id:
should_update = True
if vulnerability_db.rule_hit_details != self.rule_hit_details:
Expand Down
8 changes: 3 additions & 5 deletions evaluator/evaluator2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@

from common.logging import get_logger, init_logging
from common.status_app import create_status_app, create_status_runner
from common.utils import (a_ensure_minimal_schema_version, create_task_and_log)
from common.utils import a_ensure_minimal_schema_version, create_task_and_log
from common.database_handler import setup_async_db_pool
from common.mqueue import MQWriter, MQReader
from .processor import EvaluatorProcessor
from .common import (EvaluatorMessageType, PROMETHEUS_PORT,
CFG, MESSAGE_PARSE_ERROR)
from .common import EvaluatorMessageType, PROMETHEUS_PORT, CFG, MESSAGE_PARSE_ERROR

LOGGER = get_logger(__name__)

Expand Down Expand Up @@ -121,8 +120,7 @@ def main():

signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)
for sig in signals:
loop.add_signal_handler(
sig, lambda sig=sig: loop.create_task(evaluator.stop()))
loop.add_signal_handler(sig, lambda sig=sig: loop.create_task(evaluator.stop()))

create_task_and_log(evaluator.run(), LOGGER, loop)
loop.run_forever()
Expand Down
Loading

0 comments on commit d6feeb8

Please sign in to comment.