-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathSESRuleToSlack-Template.yaml
349 lines (304 loc) · 11.5 KB
/
SESRuleToSlack-Template.yaml
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# MIT License
# Copyright (c) 2022 Chris Farris (https://www.chrisfarris.com)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Note - You must have already setup a verified identity for the domain and configured the MX Records as described here:
# https://docs.aws.amazon.com/ses/latest/dg/receiving-email-setting-up.html
# You must also create the S3 Bucket specified by pDeliveryBucketName parameter
AWSTemplateFormatVersion: '2010-09-09'
Description: Create an inbound SES Rule to push all email from a specific domain to a slack channel
Parameters:
pSlackWebhookSecret:
Description: Name of the Secrets Manager secret where the WebHook is stored
Type: String
pIconEmoji:
Description: Slack Emoji to use
Type: String
Default: ':email:'
pRuleSetName:
Type: String
Description: Name of the RuleSet for this account
pCreateRuleSet:
Description: Create a RuleSet, or using an existing one. An account can only have one active RuleSet at a time
Type: String
AllowedValues:
- "yes"
- "no"
pCreateBucket:
Description: Create a new Bucket, or reuse and existing one
Type: String
AllowedValues:
- "yes"
- "no"
pDomain:
Type: String
Description: Domain Name that is SES should recieve email for.
pDeliveryBucketName:
Type: String
Description: Pre-existing S3 Bucket to Send the raw emails to.
Conditions:
cCreateRuleSet: !Equals [ !Ref pCreateRuleSet, "yes"]
cCreateBucket: !Equals [ !Ref pCreateBucket, "yes"]
Resources:
MessageS3Bucket:
DeletionPolicy: Retain
Condition: cCreateBucket
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref pDeliveryBucketName
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerEnforced
PublicAccessBlockConfiguration:
BlockPublicAcls: TRUE
BlockPublicPolicy: TRUE
IgnorePublicAcls: TRUE
RestrictPublicBuckets: TRUE
BillingBucketPolicy:
Type: AWS::S3::BucketPolicy
Condition: cCreateBucket
Properties:
Bucket: !Ref MessageS3Bucket
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: AllowSESPuts
Effect: Allow
Principal:
Service: ses.amazonaws.com
Action: s3:PutObject
Resource: !Sub "arn:aws:s3:::${pDeliveryBucketName}/*"
Condition:
StringEquals:
AWS:SourceAccount: !Ref AWS::AccountId
StringLike:
AWS:SourceArn: arn:aws:ses:*
SESReceiptRuleSet:
Type: AWS::SES::ReceiptRuleSet
Condition: cCreateRuleSet
Properties:
RuleSetName: !Ref pRuleSetName
SESReceiptRule:
Type: 'AWS::SES::ReceiptRule'
DependsOn:
- SlackNotificationLambdaPermission
Properties:
RuleSetName: !Ref pRuleSetName
Rule:
Name: !Ref pDomain
Enabled: true
ScanEnabled: true
Recipients:
- !Ref pDomain
- !Sub ".${pDomain}"
Actions:
- LambdaAction:
FunctionArn: !GetAtt EmailToSlackLambda.Arn
- S3Action:
BucketName: !Ref pDeliveryBucketName
ObjectKeyPrefix: !Sub "${pDomain}/"
EmailToSlackLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: [ lambda.amazonaws.com ]
Action: ['sts:AssumeRole']
Path: /
Policies:
- PolicyName: CloudWatch
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- 'cloudwatch:*'
Effect: Allow
Resource: '*'
- PolicyName: logs
PolicyDocument:
Version: '2012-10-17'
Statement:
- Resource: '*'
Action:
- 'logs:*'
Effect: Allow
- PolicyName: SecretAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: secretsmanager:GetSecretValue
Resource: !Sub "arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:${pSlackWebhookSecret}*"
- PolicyName: "FetchMessagesFromS3"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: s3:GetObject
Resource:
- !Sub "arn:aws:s3:::${pDeliveryBucketName}/${pDomain}/*"
EmailToSlackLambda:
Type: AWS::Lambda::Function
Properties:
Description: Send SNS AlertTopics to Slack Channel
Runtime: python3.7
Handler: index.lambda_handler
Timeout: '80'
FunctionName: !Sub "${AWS::StackName}"
Role: !GetAtt EmailToSlackLambdaRole.Arn
Environment:
Variables:
LOG_LEVEL: INFO
WEBHOOK: !Ref pSlackWebhookSecret
ICON_EMOJI: !Ref pIconEmoji
BUCKET: !Ref pDeliveryBucketName
DOMAIN: !Ref pDomain
Code:
ZipFile: |
# Lambda to send SES Emails Messages to Slack
from base64 import b64decode
from botocore.exceptions import ClientError
import boto3
import email
import json
import os
import urllib3
import logging
logger = logging.getLogger()
logger.setLevel(getattr(logging, os.getenv('LOG_LEVEL', default='INFO')))
logging.getLogger('botocore').setLevel(logging.WARNING)
logging.getLogger('boto3').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
def get_webhook(secret_name):
client = boto3.client('secretsmanager')
try:
get_secret_value_response = client.get_secret_value(SecretId=secret_name)
except ClientError as e:
logger.critical(f"Unable to get secret value for {secret_name}: {e}")
return(None)
else:
if 'SecretString' in get_secret_value_response:
secret_value = get_secret_value_response['SecretString']
else:
secret_value = get_secret_value_response['SecretBinary']
try:
secret_dict = json.loads(secret_value)
return(secret_dict['webhook_url'])
except Exception as e:
logger.critical(f"Error during Credential and Service extraction: {e}")
raise
WEBHOOK=get_webhook(os.environ['WEBHOOK'])
http = urllib3.PoolManager()
def lambda_handler(event, context):
logger.info("Received event: " + json.dumps(event, sort_keys=True))
for record in event['Records']:
process_message(record['ses'])
# end handler
def process_message(message):
logger.debug("processing message: " + json.dumps(message, sort_keys=True))
message_id = message['mail']['messageId']
raw_message = fetch_from_s3(message_id)
if raw_message is None:
message_body = "Unable to get Message"
else:
message_body = parse_email_file(raw_message).decode('utf-8')
logger.debug(f"Found Message body: {message_body}")
slack_message = {
'icon_emoji': os.environ['ICON_EMOJI'],
'attachments': [
{
'footer': message_id,
'pretext': f"New Email for {os.environ['DOMAIN']}",
'color': "red",
'title': f"Subject: {message['mail']['commonHeaders']['subject']}",
'fields': [
{"title": "From:","value": message['mail']['commonHeaders']['from'][0], "short": True},
{"title": "To:","value": str(message['mail']['commonHeaders']['to']), "short": True},
{"title": "Date:","value": message['mail']['commonHeaders']['date'], "short": True},
{"title": "Body:","value": message_body,"short": False},
],
# "text": "Message body goes here",
'mrkdwn_in': ["title"],
}
]
}
try:
r = http.request('POST', WEBHOOK, body=json.dumps(slack_message))
logger.info(f"Message posted")
except Exception as e:
logger.error(f"Request failed: {e}")
# End process_message()
def parse_email_file(raw_message):
b = email.message_from_string(raw_message)
body = ""
if b.is_multipart():
for part in b.walk():
ctype = part.get_content_type()
cdispo = str(part.get('Content-Disposition'))
# skip any text/plain (txt) attachments
if ctype == 'text/plain' and 'attachment' not in cdispo:
body = part.get_payload(decode=True) # decode
break
# not multipart - i.e. plain text, no attachments, keeping fingers crossed
else:
body = b.get_payload(decode=True)
return(body)
def fetch_from_s3(message_id):
s3_client = boto3.client('s3')
try:
object_key = f"{os.environ['DOMAIN']}/{message_id}"
response = s3_client.get_object(Bucket=os.environ['BUCKET'], Key=object_key)
return (response['Body'].read().decode('utf-8'))
except Exception as e:
logger.error(f"Unable to get Object s3://{os.environ['BUCKET']}/{object_key}: {e}")
return(None)
# End of Function
SlackNotificationLambdaInvocationRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- ses.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: ExecuteSlackLambda
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- lambda:InvokeFunction
Effect: Allow
Resource:
- !GetAtt EmailToSlackLambda.Arn
SlackNotificationLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt EmailToSlackLambda.Arn
Principal: ses.amazonaws.com
SourceArn: !Sub "arn:aws:ses:${AWS::Region}:${AWS::AccountId}:receipt-rule-set/${pRuleSetName}:receipt-rule/${pDomain}"
Action: lambda:invokeFunction
Outputs:
TemplateVersion:
Value: "1.0.0"