diff --git a/modules/S28_python_run.sh b/modules/S28_python_run.sh index 23640b6b3..2bb233867 100755 --- a/modules/S28_python_run.sh +++ b/modules/S28_python_run.sh @@ -14,7 +14,6 @@ # Description: This is an experimental EMBA module. It is designed to run user-defined python # scripts during the analysis. # -# shellcheck disable=SC2004 S28_python_run() { module_log_init "${FUNCNAME[0]}" @@ -23,8 +22,8 @@ S28_python_run() { local lSCRIPT_DIR="${MOD_DIR}/${FUNCNAME[0]}" local lPYTHON_SCRIPT_COUNT=${#PYTHON_SCRIPTS[@]} - local lRESULTS=() - local lCOUNT_FINDINGS=0 + local lCOUNT_SUBMODULE_FINDINGS=0 + local lCOUNT_TOTAL_FINDINGS=0 local lSCRIPT="" if [[ ${lPYTHON_SCRIPT_COUNT} -gt 0 ]]; then @@ -33,14 +32,12 @@ S28_python_run() { for lSCRIPT in "${PYTHON_SCRIPTS[@]}"; do sub_module_title "Execution of Python runner for ${ORANGE}${lSCRIPT}${NC}" print_output "[*] Executing: ${ORANGE}${lSCRIPT_DIR}/${lSCRIPT}.py${NC}" - mapfile -t lRESULTS < <(python3 "${lSCRIPT_DIR}/${lSCRIPT}.py") - lCOUNT_FINDINGS=$(("${lCOUNT_FINDINGS}" + "${#lRESULTS[@]}")) - print_output "[*] Python module ${ORANGE}${lSCRIPT}${NC} reported a total of ${ORANGE}${#lRESULTS[@]}${NC} findings." - for lRESULT in "${lRESULTS[@]}"; do - print_output "\t- ${lRESULT}" - done + lCOUNT_SUBMODULE_FINDINGS=$(python3 "${lSCRIPT_DIR}/${lSCRIPT}.py" | grep "FINDINGS" | sed "s/FINDINGS://") + lCOUNT_TOTAL_FINDINGS=$((lCOUNT_TOTAL_FINDINGS + lCOUNT_SUBMODULE_FINDINGS)) + cat "${LOG_PATH_MODULE}/${lSCRIPT}.txt" >> "${LOG_FILE}" + print_output "[*] Python module ${ORANGE}${lSCRIPT}${NC} reported a total of ${ORANGE}${lCOUNT_SUBMODULE_FINDINGS}${NC} findings." done else @@ -48,6 +45,6 @@ S28_python_run() { fi sub_module_title "Final results for ${FUNCNAME[0]}" - print_output "Total results count: ${lCOUNT_FINDINGS}" - module_end_log "${FUNCNAME[0]}" "${lCOUNT_FINDINGS}" + print_output "Total results count: ${lCOUNT_TOTAL_FINDINGS}" + module_end_log "${FUNCNAME[0]}" "${lCOUNT_TOTAL_FINDINGS}" } diff --git a/modules/S28_python_run/embamodule.py b/modules/S28_python_run/embamodule.py index 9a049745d..72b582f78 100755 --- a/modules/S28_python_run/embamodule.py +++ b/modules/S28_python_run/embamodule.py @@ -29,11 +29,17 @@ class EmbaModule(): __del__: Close module files and destroy the class instance. + __write_formatted_log: + Base method for logging. Should not be called by Python modules directly. + log: Log a new message into the module log files. add_finding: Add a new finding to the module. This will later be used during report generation. + + panic: + Ensures propper logging when throwing exceptions. """ def __init__(self, argv: list, env: _Environ): @@ -43,22 +49,36 @@ def __init__(self, argv: list, env: _Environ): try: self.logfile_dir = env.get('LOG_PATH_MODULE') except: - raise Exception(f"Unable to determine log path for python module '{self.filename}'.") + self.panic(f"Unable to determine log path for python module '{self.filename}'.", Exception) try: self.logfile = open(f"{self.logfile_dir}/{self.filename}.txt", "w") except: - raise Exception("Unable to open log files for '{self.filename}'.") + self.panic("Unable to open log files for '{self.filename}'.", Exception) def __del__(self): - self.logfile.close() + self.logfile.close() + + + def __write_formatted_log(self, operator: str, text: str): + lines = text.split('\n') + for line in lines: + self.logfile.write(f"[{operator}] {line}\n") + def log(self, text: str): - self.logfile.write(f"{text}\n"); + self.__write_formatted_log("*", text) + def add_finding(self, description: str): self.findings.append(description) + self.__write_formatted_log(f"F{len(self.findings)}", description) + + + def panic(self, description: str, except_type: type[Exception]): + self.__write_formatted_log("!", description) + raise except_type(description) def setup_module(argv: list, env: _Environ): @@ -78,6 +98,7 @@ def setup_module(argv: list, env: _Environ): def shutdown_module(module: EmbaModule): """ Shut down an emba python module. + This will also print the amount of findings as an interger so EMBA can parse the number. Parameters: module (EmbaModule): A class instance of EmbaModule. @@ -85,7 +106,5 @@ def shutdown_module(module: EmbaModule): Returns: none """ - for finding in module.findings: - print(finding) - + print(f"FINDINGS:{len(module.findings)}", end="") del module diff --git a/modules/S28_python_run/example_script.py b/modules/S28_python_run/example_script.py index 2fe75f541..907f9241a 100755 --- a/modules/S28_python_run/example_script.py +++ b/modules/S28_python_run/example_script.py @@ -27,10 +27,9 @@ def main(): # This is just some example code. # The module logic would go here. - module.log("Received arguments:") - for key, value in enumerate(argv): - module.add_finding(f"Found argument: {key}:{value}") - module.log(f"\t- {key} :: {value}") + module.log("Received arguments a total of {len(environ)} environment variables.") + for key in environ.keys(): + module.add_finding(f"Found envvar: {key}={environ[key]}") # Shutdown module and report results. # This line is required