-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
403 lines (338 loc) · 12.3 KB
/
handler.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
""" Lambda function for the File Upload to S3 and Data Normalization and Encoding. """
import base64
import cgi
import datetime
import hashlib
import io
import logging
import os
import random
import uuid
import boto3
import jinja2
import phonenumbers
from botocore.config import Config
# -*- coding: utf-8 -*-
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def upload_file_to_s3(event: dict, _context: dict) -> dict:
"""
Uploads a file to S3 and returns an HTML form for file upload.
Args:
event (dict): The event data.
_context (dict): The context data.
Returns:
dict: The response containing the HTML form or an error message.
"""
logger.info("File upload request.")
# Initialize S3 and Jinja2 environment.
s3_client = boto3.client('s3',
config=Config(
region_name=os.environ["AWS_REGION"],
signature_version="s3v4"))
environment = jinja2.Environment(loader=jinja2.FileSystemLoader(
searchpath="./templates"))
# Get region code parameter.
region_code = os.environ["region_code"]
try:
region_code = event.get("queryStringParameters",
{}).get("region_code", region_code)
# pylint: disable=W0703
except Exception:
pass
if event["httpMethod"] == "GET":
try:
# Send the upload form.
template = environment.get_template(f"upload-{region_code}.tpl")
return {
"statusCode":
200,
"headers": {
"Content-Type": "text/html"
},
"body":
template.render({
"expires_in": os.environ["expires_in"],
"region_code": region_code,
"region": os.environ["AWS_REGION"],
"domain": event["requestContext"]["domainName"],
"stage": event["requestContext"]["stage"],
"path": event["requestContext"]["resourcePath"]
})
}
# pylint: disable=W0718
except Exception as error:
return {
"statusCode":
500,
"body":
f"An error occurred while rendering the template: {str(error)}"
}
if event["httpMethod"] == "POST":
# Generate unique keys using uuid.
key = str(uuid.uuid4()) + ".csv"
data_type = "email"
try:
# Parse multi-part data.
_, c_data = cgi.parse_header(event["headers"]["content-type"])
c_data["boundary"] = bytes(c_data["boundary"], "utf8")
form_data = cgi.parse_multipart(
io.BytesIO(bytes(event["body"], "utf8")), c_data)
# Get data_type parameter.
data_type = form_data.get("data_type", [data_type])[0]
# pylint: disable=W0718
except Exception as error:
return {
"statusCode":
400,
"body":
f"An error occurred while parsing the multi-part data: {str(error)}"
}
try:
# Store in S3.
header = data_type.encode() + b"\n"
data = header + form_data["file"][0]
s3_client.put_object(Bucket=os.environ["source_bucket"],
Key=key,
Body=data)
# Generate a pre-signed URL.
location = {}
for http_method in ["GET", "HEAD"]:
location[http_method] = s3_client.generate_presigned_url(
ClientMethod="get_object",
Params={
"Bucket": os.environ["destination_bucket"],
"Key": key
},
ExpiresIn=int(os.environ["expires_in"]) * 60,
HttpMethod=http_method)
# Returns a link to download a file.
template = environment.get_template(f"download-{region_code}.tpl")
return {
"statusCode":
200,
"headers": {
"Content-Type": "text/html"
},
"body":
template.render({
"key": key,
"location": location,
"expires_in": os.environ["expires_in"]
})
}
# pylint: disable=W0718
except Exception as error:
return {
"statusCode":
500,
"body":
f"An error occurred while storing the file in S3: {str(error)}"
}
return {"statusCode": 400, "body": "Invalid request."}
def clean_up_buckets(_event: dict, _context: dict) -> dict:
"""
Cleans up the source and destination buckets by deleting expired objects.
Args:
_event (dict): The event data.
_context (dict): The context data.
Returns:
dict: The response indicating the success or failure of the cleanup operation.
"""
logger.info("Clean up source and destination bucket.")
# Initialize S3.
s3_client = boto3.client('s3',
config=Config(
region_name=os.environ["AWS_REGION"],
signature_version="s3v4"))
# Expires_in minutes before current time.
date = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(
minutes=int(os.environ["expires_in"]))
# Scan buckets and delete old objects.
buckets = [os.environ["source_bucket"], os.environ["destination_bucket"]]
for bucket in buckets:
response = s3_client.list_objects_v2(Bucket=bucket)
if "Contents" in response:
for obj in response["Contents"]:
last_modified = obj["LastModified"]
key = obj["Key"]
if last_modified < date:
try:
# Delete expired object.
s3_client.delete_object(Bucket=bucket, Key=key)
logger.info(
"Deleted Key %s in Bucket %s (last_modified: %s).",
key, bucket, last_modified)
# pylint: disable=W0718
except Exception as error:
logger.error(
f"An error occurred while deleting Key %s in Bucket %s: {str(error)}",
key, bucket)
return {"statusCode": 200}
def normalization_and_encoding(event: dict, _context: dict) -> dict:
"""
Normalizes and encodes email addresses in S3 bucket files.
Args:
event (dict): The event data.
_context (dict): The context data.
Returns:
dict: The response indicating the success or failure of the normalization and encoding.
"""
logger.info("New files uploaded to the source bucket.")
# Initialize S3.
s3_client = boto3.client('s3',
config=Config(
region_name=os.environ["AWS_REGION"],
signature_version="s3v4"))
try:
source_bucket = event["Records"][0]["s3"]["bucket"]["name"]
key = event["Records"][0]["s3"]["object"]["key"]
logger.info("source: %s, key: %s", source_bucket, key)
# Process the file.
source = s3_client.get_object(Bucket=source_bucket, Key=key)
region_code = os.environ["region_code"].upper()
data_type = "email"
encoded_list = []
for line in source["Body"].iter_lines():
data_str = line.decode("utf-8").rstrip()
# Skip empty lines.
if len(data_str) == 0:
continue
# check if the first line is a header.
if data_str.lower() == "email" or data_str.lower() == "phone":
data_type = data_str.lower()
continue
# Process email addresses.
if data_type == "email" and is_email(data_str):
encoded_list.append(
base64_encode(hash_sha256(
normalize_email_string(data_str))))
# Process phone numbers.
elif data_type == "phone" and is_phone_number(
data_str, region_code):
encoded_list.append(
base64_encode(
hash_sha256(
normalize_phone_number(data_str, region_code))))
# pylint: disable=W0718
except Exception as error:
return {
"statusCode": 500,
"body":
f"An error occurred while processing the file: {str(error)}"
}
try:
destination_bucket = os.environ["destination_bucket"]
logger.info("destination: %s, key: %s", destination_bucket, key)
# Write to destination bucket.
buffer = io.TextIOWrapper(io.BytesIO(), encoding="utf-8")
for encoded in random_sort(encoded_list):
buffer.write(f"{encoded}\n")
buffer.seek(0)
s3_client.put_object(Bucket=destination_bucket,
Key=key,
Body=buffer.read())
# pylint: disable=W0718
except Exception as error:
return {
"statusCode":
500,
"body":
f"An error occurred while storing the file in the destination bucket: {str(error)}"
}
return {"statusCode": 200}
def is_email(data: str) -> bool:
"""
Determines if the string is an email address.
Args:
data (str): The string to check.
Returns:
bool: True if the string is an email address, False otherwise.
"""
if data.count("@") != 1:
return False
_, domain = data.split("@")
if domain.count(".") != 1:
return False
return True
def normalize_email_string(email: str) -> str:
"""
Normalizes an email string.
Args:
email (str): The email string to normalize.
Returns:
str: The normalized email string.
"""
# Remove leading and trailing spaces.
email = email.strip()
# Convert to lowercase.
email = email.lower()
# In gmail.com addresses only.
if email.endswith("@gmail.com"):
local, domain = email.split("@")
# Remove all dots.
local = local.replace(".", "")
# Remove everything after the first plus sign.
local = local.split("+")[0]
email = local + "@" + domain
return email
def is_phone_number(phone: str, region_code: str) -> bool:
"""
Determines if the string is a phone number.
Args:
phone (str): The string to check.
region_code (str): The region code of the phone number.
Returns:
bool: True if the string is a phone number, False otherwise.
"""
try:
phonenumbers.parse(phone, region_code)
return True
except phonenumbers.phonenumberutil.NumberParseException:
return False
def normalize_phone_number(phone: str, region_code: str) -> str:
"""
Normalizes a phone number string.
Args:
phone (str): The phone number string to normalize.
region_code (str): The region code of the phone number.
Returns:
str: The normalized phone number string.
"""
try:
phone = phonenumbers.format_number(
phonenumbers.parse(phone, region_code),
phonenumbers.PhoneNumberFormat.E164)
except phonenumbers.phonenumberutil.NumberParseException:
phone = ""
return phone
def base64_encode(data: bytes) -> str:
"""
Encodes bytes using Base64.
Args:
data (bytes): The bytes to encode.
Returns:
str: The Base64 encoded string.
"""
return base64.b64encode(data).decode()
def hash_sha256(data: str) -> bytes:
"""
Hashes a string using SHA256.
Args:
data (str): The string to hash.
Returns:
bytes: The hashed bytes.
"""
return hashlib.sha256(data.encode()).digest()
def random_sort(lst: list) -> list:
"""
Randomly sorts a list.
Args:
lst (list): The list to sort.
Returns:
list: The randomly sorted list.
"""
for i, _ in enumerate(lst):
j = random.randint(0, i)
lst[i], lst[j] = lst[j], lst[i]
return lst