Skip to content

Commit

Permalink
Feat: Include Error log (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mamullen13316 authored Nov 25, 2024
1 parent 45e436c commit e706997
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ dist/
sophos_firewall_audit/results_html*
firewalls.yaml
/rule_export*
error.log
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <matt.mullen@sophos.com>"]
readme = "README.md"
Expand Down
9 changes: 9 additions & 0 deletions sophos_firewall_audit/postaudit_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
9 changes: 8 additions & 1 deletion sophos_firewall_audit/sophosfirewallaudit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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()
10 changes: 9 additions & 1 deletion sophos_firewall_audit/templates/email_body_web.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@
<br/>
Please review the detailed results <a href="{{ url }}">here</a>.
<br/>
<br/>
<br/>
{% if error_list %}
Errors:
<br/>
{% for line in error_list %}
{{ line }}
<br/>
{% endfor %}
{% endif %}

0 comments on commit e706997

Please sign in to comment.