Skip to content

Commit

Permalink
fix: Prefix project_id to all admin emails (#885)
Browse files Browse the repository at this point in the history
This prefixes the project's ID to every mail sent via
`email.sendEMailToAdmins`, which helps to identify the project where the
error occurs, especially when the same sib-credentials are used by
multiple projects.
  • Loading branch information
phorward authored Oct 10, 2023
1 parent 0e7a54f commit b601875
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/viur/core/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ def sendEMailToAdmins(subject: str, body: str, *args, **kwargs) -> bool:
for user_skel in conf["viur.mainApp"].user.viewSkel().all().filter("access =", "root").fetch():
users.append(user_skel["name"])

# Prefix the instance's project_id to subject
subject = conf["viur.instance.project_id"] + ": " + subject

if users:
ret = sendEMail(dests=users, stringTemplate=os.linesep.join((subject, body)), *args, **kwargs)
success = True
Expand Down Expand Up @@ -432,9 +435,9 @@ def check_sib_quota() -> None:
if entity["latest_warning_for"] == limit:
logging.info(f"Already send an email for {limit = }.")
break

sendEMailToAdmins(
f"SendInBlue email budget for {conf['viur.instance.project_id']}: "
f"{credits} ({idx}. warning)",
f"SendInBlue email budget {credits} ({idx}. warning)",
f"The SendInBlue email budget reached {credits} credits "
f"for {data['email']}. Please increase soon.",
)
Expand Down
8 changes: 5 additions & 3 deletions src/viur/core/modules/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -1429,9 +1429,11 @@ def createNewUserIfNotExists():
logging.error("Something went wrong when trying to add admin user %s with Password %s", uname, pw)
logging.exception(e)
return
logging.warning("ViUR created a new admin-user for you! Username: %s, Password: %s", uname, pw)
email.sendEMailToAdmins("Your new ViUR password",
"ViUR created a new admin-user for you! Username: %s, Password: %s" % (uname, pw))

msg = f"ViUR created a new admin-user for you!\nUsername: {uname}\nPassword: {pw}"

logging.warning(msg)
email.sendEMailToAdmins("New ViUR password", msg)


# DEPRECATED ATTRIBUTES HANDLING
Expand Down
15 changes: 8 additions & 7 deletions src/viur/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,16 @@ def deferred(self, *args, **kwargs):
if not conf["viur.instance.is_dev_server"] or os.getenv("TASKS_EMULATOR") is None:
logging.critical('Detected an attempted XSRF attack. This request did not originate from Task Queue.')
raise errors.Forbidden()

# Check if the retry count exceeds our warning threshold
retryCount = req.headers.get("X-Appengine-Taskretrycount", None)
if retryCount:
if int(retryCount) == self.retryCountWarningThreshold:
from viur.core import email
email.sendEMailToAdmins("Deferred task retry count exceeded warning threshold",
"Task %s will now be retried for the %sth time." % (
req.headers.get("X-Appengine-Taskname", ""),
retryCount))
if retryCount and int(retryCount) == self.retryCountWarningThreshold:
from viur.core import email
email.sendEMailToAdmins(
"Deferred task retry counter exceeded warning threshold",
f"""Task {req.headers.get("X-Appengine-Taskname", "")} is retried for the {retryCount}th time."""
)

cmd, data = json.loads(req.body, object_hook=jsonDecodeObjectHook)
funcPath, args, kwargs, env = data
logging.debug(f"Call task {funcPath} with {cmd=} {args=} {kwargs=} {env=}")
Expand Down

0 comments on commit b601875

Please sign in to comment.