X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=kernel_hardening_checker%2Fengine.py;h=56aa80f44e17589377390453789f5a8a86f500e5;hb=b87f0b675171581857d3cb2f01ce74f1dcf72248;hp=b78b1013a2e84b2a2de0b12196b9093cfdc290bc;hpb=d66b81885f5e8f69e5c5268e8a3fd22d07dbf03c;p=kconfig-hardened-check.git diff --git a/kernel_hardening_checker/engine.py b/kernel_hardening_checker/engine.py index b78b101..56aa80f 100644 --- a/kernel_hardening_checker/engine.py +++ b/kernel_hardening_checker/engine.py @@ -11,12 +11,14 @@ This module is the engine of checks. # pylint: disable=missing-class-docstring,missing-function-docstring # pylint: disable=line-too-long,invalid-name,too-many-branches +import sys + GREEN_COLOR = '\x1b[32m' RED_COLOR = '\x1b[31m' COLOR_END = '\x1b[0m' def colorize_result(input_text): - if input_text is None: + if input_text is None or not sys.stdout.isatty(): return input_text if input_text.startswith('OK'): color = GREEN_COLOR @@ -58,7 +60,7 @@ class OptCheck: self.result = None @property - def type(self): + def opt_type(self): return None def set_state(self, data): @@ -99,21 +101,21 @@ class OptCheck: self.result = f'FAIL: "{self.state}"' def table_print(self, _mode, with_results): - print(f'{self.name:<40}|{self.type:^7}|{self.expected:^12}|{self.decision:^10}|{self.reason:^18}', end='') + print(f'{self.name:<40}|{self.opt_type:^7}|{self.expected:^12}|{self.decision:^10}|{self.reason:^18}', end='') if with_results: print(f'| {colorize_result(self.result)}', end='') def json_dump(self, with_results): dump = { "option_name": self.name, - "type": self.type, + "type": self.opt_type, "desired_val": self.expected, "decision": self.decision, "reason": self.reason, } if with_results: - dump["check_result_text"] = self.result - dump["check_result"] = "FAIL" not in self.result + dump["check_result"] = self.result + dump["check_result_bool"] = self.result.startswith('OK') return dump @@ -123,19 +125,19 @@ class KconfigCheck(OptCheck): self.name = f'CONFIG_{self.name}' @property - def type(self): + def opt_type(self): return 'kconfig' class CmdlineCheck(OptCheck): @property - def type(self): + def opt_type(self): return 'cmdline' class SysctlCheck(OptCheck): @property - def type(self): + def opt_type(self): return 'sysctl' @@ -150,7 +152,7 @@ class VersionCheck: self.result = None @property - def type(self): + def opt_type(self): return 'version' def set_state(self, data): @@ -197,7 +199,7 @@ class ComplexOptCheck: self.result = None @property - def type(self): + def opt_type(self): return 'complex' @property @@ -226,9 +228,9 @@ class ComplexOptCheck: def json_dump(self, with_results): dump = self.opts[0].json_dump(False) if with_results: - # Add 'result_text' and 'result' keys to the dictionary - dump["check_result_text"] = self.result - dump["check_result"] = "FAIL" not in self.result + # Add the 'check_result' and 'check_result_bool' keys to the dictionary + dump["check_result"] = self.result + dump["check_result_bool"] = self.result.startswith('OK') return dump @@ -296,33 +298,33 @@ SIMPLE_OPTION_TYPES = ('kconfig', 'cmdline', 'sysctl', 'version') def populate_simple_opt_with_data(opt, data, data_type): - assert(opt.type != 'complex'), \ + assert(opt.opt_type != 'complex'), \ f'unexpected ComplexOptCheck "{opt.name}"' - assert(opt.type in SIMPLE_OPTION_TYPES), \ - f'invalid opt type "{opt.type}"' + assert(opt.opt_type in SIMPLE_OPTION_TYPES), \ + f'invalid opt_type "{opt.opt_type}"' assert(data_type in SIMPLE_OPTION_TYPES), \ - f'invalid data type "{data_type}"' + f'invalid data_type "{data_type}"' assert(data), \ 'empty data' - if data_type != opt.type: + if data_type != opt.opt_type: return if data_type in ('kconfig', 'cmdline', 'sysctl'): opt.set_state(data.get(opt.name, None)) else: assert(data_type == 'version'), \ - f'unexpected data type "{data_type}"' + f'unexpected data_type "{data_type}"' opt.set_state(data) def populate_opt_with_data(opt, data, data_type): - assert(opt.type != 'version'), 'a single VersionCheck is useless' - if opt.type != 'complex': + assert(opt.opt_type != 'version'), 'a single VersionCheck is useless' + if opt.opt_type != 'complex': populate_simple_opt_with_data(opt, data, data_type) else: for o in opt.opts: - if o.type != 'complex': + if o.opt_type != 'complex': populate_simple_opt_with_data(o, data, data_type) else: # Recursion for nested ComplexOptCheck objects @@ -337,8 +339,8 @@ def populate_with_data(checklist, data, data_type): def override_expected_value(checklist, name, new_val): for opt in checklist: if opt.name == name: - assert(opt.type in ('kconfig', 'cmdline', 'sysctl')), \ - f'overriding an expected value for "{opt.type}" checks is not supported yet' + assert(opt.opt_type in ('kconfig', 'cmdline', 'sysctl')), \ + f'overriding an expected value for "{opt.opt_type}" checks is not supported yet' opt.expected = new_val