diff --git a/.gitignore b/.gitignore index dc92701..40590d2 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ dist/ sophos_firewall_audit/results_html* firewalls.yaml /rule_export* +error.log diff --git a/pyproject.toml b/pyproject.toml index dc8ccb5..9ef2efc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sophos-firewall-audit" -version = "1.0.8" +version = "1.0.9" description = "Sophos Firewall Audit" authors = ["Matt Mullen "] readme = "README.md" diff --git a/sophos_firewall_audit/postaudit_web.py b/sophos_firewall_audit/postaudit_web.py index 893887c..4e32710 100755 --- a/sophos_firewall_audit/postaudit_web.py +++ b/sophos_firewall_audit/postaudit_web.py @@ -105,11 +105,20 @@ def parse_results(results): with open(os.path.join(parent_directory, "results.json"), "r", encoding="utf-8") as fn: results = json.loads(fn.read()) + error_list = [] + try: + with open(os.path.join(parent_directory, "error.log"), "r", encoding="utf-8") as fn: + for line in fn.readlines: + error_list.append(line.strip()) + except FileNotFoundError: + logging.warning("File error.log not found. This is normal if there were no connectivity errors during the audit.") + html_table = parse_results(results) msg_subject = "Firewall Audit Report" template = env.get_template("email_body_web.j2") msg_body = template.render(html_table=html_table, + error_list=error_list, url=os.environ["URL"]) logging.info("Sending email...") send_email(msg_subject, msg_body, os.environ["SMTP_RECIPIENT"]) diff --git a/sophos_firewall_audit/sophosfirewallaudit.py b/sophos_firewall_audit/sophosfirewallaudit.py index 4e5bb0b..58f90cf 100755 --- a/sophos_firewall_audit/sophosfirewallaudit.py +++ b/sophos_firewall_audit/sophosfirewallaudit.py @@ -172,6 +172,8 @@ def main(): os.mkdir(local_dirname) os.mkdir(web_dirname) + error_list = [] + for firewall in firewalls: fw = SophosFirewall( username=os.environ['VAULT_SECRET_KEY'] if args.use_vault else fw_username, @@ -184,6 +186,7 @@ def main(): fw.login() except Exception as Error: logging.error(f"Error connecting to firewall {firewall['hostname']}: {Error}") + error_list.append(f"{firewall['hostname']: {Error}}") continue if not args.rule_export: @@ -196,7 +199,11 @@ def main(): generate_audit_output(status_dict, local_dirname, web_dirname) elif args.rule_export: generate_rule_output(firewalls, local_dirname, web_dirname) - + + if error_list: + with open("error.log", "w", encoding="utf-8") as f: + for line in error_list: + f.write(f"{line}\n") if __name__ == "__main__": main() \ No newline at end of file diff --git a/sophos_firewall_audit/templates/email_body_web.j2 b/sophos_firewall_audit/templates/email_body_web.j2 index 82dc5ca..70e1a8d 100644 --- a/sophos_firewall_audit/templates/email_body_web.j2 +++ b/sophos_firewall_audit/templates/email_body_web.j2 @@ -4,4 +4,12 @@
Please review the detailed results here.
-
\ No newline at end of file +
+{% if error_list %} +Errors: +
+{% for line in error_list %} +{{ line }} +
+{% endfor %} +{% endif %} \ No newline at end of file