email: Only send the first x bytes of detail in email

This commit is contained in:
deajan 2025-07-03 13:44:55 +02:00
parent bdf2e56cce
commit fe15bcf0c5
2 changed files with 15 additions and 1 deletions

View file

@ -51,3 +51,6 @@ ALLOWED_UPGRADE_SERVER_IDS = ("npbackup.upgrader", "npbackup.deployment_server")
# Replacement string for sensitive data
HIDDEN_BY_NPBACKUP = "_[o_O]_hidden_by_npbackup"
# Maximum number of characters for details content in emails
MAX_EMAIL_DETAIL_LENGTH = 1000

View file

@ -24,6 +24,7 @@ from npbackup.restic_metrics import (
from npbackup.__version__ import __intname__ as NAME, version_dict
from npbackup.__debug__ import fmt_json
from resources.customization import OEM_STRING
from npbackup.__env__ import MAX_EMAIL_DETAIL_LENGTH
logger = getLogger()
@ -325,7 +326,17 @@ def send_metrics_mail(
body += f"\nDate: {date}"
if isinstance(restic_result, dict):
body += f"\n\nDetail: {fmt_json(restic_result)}"
try:
restic_result = fmt_json(restic_result)
except TypeError:
# TypeError may happen on ls command which contains a json of LSNodes when HAVE_MSGSPEC is True
pass
# Convert the dict to a string so we can analyze its length
restic_result = str(restic_result)
if len(restic_result) > MAX_EMAIL_DETAIL_LENGTH:
body += f"\n\nDetail:\n{restic_result[0:MAX_EMAIL_DETAIL_LENGTH]} [... truncated]"
else:
body += f"\n\nDetail:\n{restic_result}"
body += f"\n\nGenerated by {OEM_STRING} {version_dict['version']}\n"