-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleForwarder.py
318 lines (274 loc) · 11.4 KB
/
SimpleForwarder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
"""
To use, modify the DEFAULT_CONFIG. At a minimum, you'll need to
provide an emailBucket and change the forwardMapping to the addresses
you want to handle.
"""
from __future__ import print_function
import logging
import re
import boto3
import botocore
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)
DEFAULT_CONFIG = {
'fromEmail': '',
'subjectPrefix': {
'Default': '',
'admin@example.com': '[Admin] ',
'info@example.com': '[Info] ',
'members@example.com': '[Members] '
},
'emailBucket': 's3-bucket-for-email',
'emailKeyPrefix': '',
'forwardMapping': {
'admin@example.com': [
'user1@example.com',
'user2@example.com'
],
'info@example': [
'user1@example.com',
'user2@example.com',
'user3@example.com'
],
'members@svpsouthruislip.org.uk': [
'user3@example.com',
'user4@example.com',
'user5@example.com'
],
'example.com': [
# messages to users that don't match the above
]
}
}
def lambda_handler(event,
context,
ses_client=boto3.client('ses'),
s3_client=boto3.resource('s3'),
config=None):
"""
Handler function to be invoked with an inbound SES email as the
event.
"""
if not config:
config = DEFAULT_CONFIG
LOGGER.info("Got event: %s", event)
LOGGER.debug("Context: %s", context)
event = SESEmailEvent(event)
if event.is_spam() or event.is_virus():
LOGGER.error("Skipping email because it failed virus/spam check")
return
new_recipients = get_new_recipients(event.get_recipients(), config)
if len(new_recipients) > 0:
LOGGER.info("Rewriting original recipients %s to %s",
event.get_recipients(), new_recipients)
sender = SESSender(ses_client, LOGGER)
full_email = S3Object(s3_client,
config['emailBucket'],
event.get_email()['messageId'])
email = SESEmail(full_email.get('Body'),
event, sender, config, LOGGER)
email.send()
else:
LOGGER.info("Finishing event, no matching recipients")
def get_new_recipients(original_recipients, config):
""" Find the new recipients. """
original = [x.lower() for x in original_recipients]
new_recipients = []
forward_mapping = config['forwardMapping']
for recipient in original:
if recipient in forward_mapping:
new_recipients.extend(forward_mapping[recipient])
else:
domain = recipient.split('@')[1]
if domain in forward_mapping:
new_recipients.extend(forward_mapping[domain])
new_recipients = list(set(new_recipients))
return new_recipients
class SESForwarderError(Exception):
""" Exception wrapper. """
def __init__(self, value):
super(SESForwarderError, self).__init__(value)
self.value = value
def __str__(self):
return repr(self.value)
class SESEmailEvent(object):
""" Wraps an event with some simple getters."""
def __init__(self, event):
self._event = event
if not self._validate_event():
raise SESForwarderError("Invalid event: {}".format(event))
def get_email(self):
""" return just the email part of the event"""
return self._event['Records'][0]['ses']['mail']
def get_recipients(self):
""" Return just the recipients part of the event."""
return self._event['Records'][0]['ses']['receipt']['recipients']
def is_spam(self):
""" Return whether this may be spam."""
evnt = self._event
stat = evnt['Records'][0]['ses']['receipt']['spamVerdict']['status']
return stat != "PASS"
def is_virus(self):
""" Return whether this may contain a virus."""
evnt = self._event
stat = evnt['Records'][0]['ses']['receipt']['virusVerdict']['status']
return stat != "PASS"
def _validate_event(self):
""" Ensure that we have the bits we need. """
if 'Records' in self._event:
if len(self._event['Records']) > 0:
record = self._event['Records'][0]
if 'eventSource' in record and 'eventVersion' in record:
return record['eventSource'] == 'aws:ses'
class SESSender(object):
"""Wraps sending functionality for dependency injection during
testing."""
def __init__(self, awsClient, logger):
self._client = awsClient
self._logger = logger
self._email = None
def send(self, recipients, original_recipient):
""" Send the email to the specified recipients. """
if not self._email:
raise SESForwarderError("No email set before sending.")
try:
self._logger.debug("Sending email from: %s", original_recipient)
self._logger.debug("Sending destination: %s", recipients)
self._logger.debug("Sending email: %s", self._email)
return self._client.send_raw_email(
Destinations=recipients,
Source=original_recipient,
RawMessage={
'Data': self._email
})
except botocore.exceptions.ClientError as err:
self._logger.error("Failed to send message. Error: %s", err)
raise
def set_email(self, email):
""" Set the email to be sent."""
self._email = bytearray(email, 'utf-8')
def get_email(self):
""" Get the currently set email."""
return self._email
class S3Object(object):
"""Wraps getting an object from an S3 bucket for dependency injection
during testing."""
def __init__(self, awsStorageClient, bucket, objectId):
self._client = awsStorageClient
self._bucket = bucket
self._id = objectId
def get(self, section):
""" Return the specified section of the object as a blob"""
try:
return self._client.Object(
self._bucket, self._id).get()[section].read()
except botocore.exceptions.ClientError:
raise SESForwarderError(
"Failed to get object from s3 bucket: {0}, key: {1}".format(
self._bucket, self._id))
def get_bucket(self):
""" Currently active bucket."""
return self._bucket
def get_id(self):
""" Currently active id. """
return self._id
class SESEmail(object):
""" Wraps the email and methods to transform it. """
def __init__(self, emailS3Blob, event,
sender, config, logger):
whole = emailS3Blob.split('\r\n')
start_body = whole.index('')
self._header = whole[0:start_body]
self._body = whole[start_body:]
self._event = event
self._recipients = get_new_recipients(event.get_recipients(), config)
self._sender = sender
self._config = config
self._logger = logger
self._rewrite_header()
def email(self):
""" Return the processed email. """
result = '\r\n'.join(self._header) + '\r\n' + '\r\n'.join(self._body)
# Remove all DKIM-Signature headers, since we've modified the
# message and they'll be invalid anyway. We have to do this with
# regular expressions because they span lines.
pattern = re.compile(r"DKIM-Signature: .+\r\n(\s+.+\r\n)+",
re.MULTILINE)
return pattern.sub("", result)
def send(self):
""" Send the email. """
self._sender.set_email(self.email())
self._logger.info("Sending email. New: %s, Original: %s",
self._recipients,
self._event.get_recipients())
if len(self._recipients) and len(self._event.get_recipients()):
return self._sender.send(self._recipients,
self._event.get_recipients()[0])
else:
raise SESForwarderError(
"Attempt to send with no recipients or original_recipients.")
def _rewrite_header(self):
self._add_reply_to()
self._replace_from()
self._add_subject_prefix()
self._remove_return_path()
self._remove_sender_header()
def _add_reply_to(self):
prev_reply_to = self._get_header_section('Reply-To: ')
if len(prev_reply_to) > 0:
# remove any existing reply-to header
self._logger.info("removing exist reply-to header: %s",
prev_reply_to)
self._header = self._remove_header_section('Reply-To: ')
from_line = self._get_header_section('From: ')
if len(from_line) > 0:
reply_to = 'Reply-To: ' + from_line[0].replace('From: ', '')
self._logger.info("Adding Reply-To to the header: %s",
reply_to)
self._header.append(reply_to)
else:
self._logger.error("Unable to extract From address.")
def _replace_from(self):
# Replace the message's From: and Return-Path: headers with
# either the hard-coded from address from the configuration or
# the first, original recipient. Either is a verified
# domain. SES won't let us send from an unverified "From"
# address, so error out if this fails.
from_line = self._get_header_section('From: ')
if not len(self._event.get_recipients()) or len(from_line) == 0:
raise SESForwarderError(
"Failed to rewrite 'from' address. Header: {}".format(
self._header))
self._logger.info("Original from address: %s", from_line[0])
new_from = from_line[0]
original = self._config['fromEmail'] or self._event.get_recipients()[0]
self._logger.info("Replacing from address with: %s", original)
new_from = new_from.strip(' \r\n\t')
new_from = new_from.replace('<', 'at ')
new_from = new_from.replace('>', '')
new_from = new_from + ' <' + original + '>'
self._header[self._header.index(from_line[0])] = new_from
# do similar to the Return-Path line, if it exists
return_path = self._get_header_section('Return-Path: ')
if len(return_path) > 0:
index = self._header.index(return_path[0])
self._header[index] = "Return-Path: <" + original + ">"
def _add_subject_prefix(self):
prefix = self._config['subjectPrefix']['Default']
first_recipient = self._event.get_recipients()[0]
if first_recipient in self._config['subjectPrefix']:
prefix = self._config['subjectPrefix'][first_recipient]
subject = self._get_header_section('Subject: ')[0]
if subject:
indx = self._header.index(subject)
self._header[indx] = 'Subject: ' + \
prefix + \
subject.replace('Subject: ', '')
def _remove_return_path(self):
self._header = self._remove_header_section('Return-Path: ')
def _remove_sender_header(self):
self._header = self._remove_header_section('Sender: ')
def _get_header_section(self, start):
return [elem for elem in self._header if elem.startswith(start)]
def _remove_header_section(self, start):
return [elem for elem in self._header if not elem.startswith(start)]