-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exception handlers for failed encryption only for the encrypting …
…mail backend
- Loading branch information
Showing
3 changed files
with
139 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import inspect | ||
|
||
from django.conf import settings | ||
from django.core.mail import mail_admins | ||
|
||
from .models import Address | ||
from .settings import FAILURE_HANDLERS | ||
|
||
|
||
ADMIN_ADDRESSES = [admin[1] for admin in settings.ADMINS] | ||
|
||
|
||
def get_variable_from_exception(exception, variable_name): | ||
""" | ||
Grab the variable from closest frame in the stack | ||
""" | ||
for frame in reversed(inspect.trace()): | ||
try: | ||
# From http://stackoverflow.com/a/9059407/6461688 | ||
frame_variable = frame[0].f_locals[variable_name] | ||
except KeyError: | ||
pass | ||
else: | ||
return frame_variable | ||
else: | ||
raise KeyError("Variable '%s' not in any stack frames", variable_name) | ||
|
||
|
||
def default_handle_failed_encryption(exception): | ||
""" | ||
Handle failures when trying to encrypt alternative content for messages | ||
""" | ||
raise exception | ||
|
||
|
||
def default_handle_failed_alternative_encryption(exception): | ||
""" | ||
Handle failures when trying to encrypt alternative content for messages | ||
""" | ||
raise exception | ||
|
||
|
||
def default_handle_failed_attachment_encryption(exception): | ||
""" | ||
Handle failures when trying to encrypt alternative content for messages | ||
""" | ||
raise exception | ||
|
||
|
||
def force_mail_admins(unencrypted_message, address): | ||
""" | ||
Mail admins when encryption fails, and send the message unencrypted if | ||
the recipient is an admin | ||
""" | ||
|
||
if address in ADMIN_ADDRESSES: | ||
# We assume that it is more important to mail the admin *without* | ||
# encrypting the message | ||
force_send_message(unencrypted_message) | ||
else: | ||
mail_admins( | ||
"Failed encryption attempt", | ||
""" | ||
There was a problem encrypting an email message. | ||
Subject: "{subject}" | ||
Address: "{address}" | ||
""") | ||
|
||
|
||
def force_delete_key(address): | ||
""" | ||
Delete the key from the keyring and the Key and Address objects from the | ||
database | ||
""" | ||
address_object = Address.objects.get(address=address) | ||
address_object.key.delete() | ||
address_object.delete() | ||
|
||
|
||
def force_send_message(unencrypted_message): | ||
""" | ||
Send the message unencrypted | ||
""" | ||
unencrypted_message.do_not_encrypt_this_message = True | ||
unencrypted_message.send() | ||
|
||
|
||
def import_function(key): | ||
mod, _, function = FAILURE_HANDLERS[key].rpartition('.') | ||
try: | ||
# Python 3.4+ | ||
from importlib import import_module | ||
except ImportError: | ||
# Python < 3.4 | ||
# From http://stackoverflow.com/a/8255024/6461688 | ||
mod = __import__(mod, globals(), locals(), [function]) | ||
else: | ||
mod = import_module(mod) | ||
return getattr(mod, function) | ||
|
||
exception_handlers = { | ||
'message': 'handle_failed_message_encryption', | ||
'alternative': 'handle_failed_alternative_encryption', | ||
'attachment': 'handle_failed_attachment_encryption', | ||
} | ||
|
||
for key, value in exception_handlers.items(): | ||
locals()[value] = import_function(key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters