-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgsgmail_connector.py
1277 lines (997 loc) · 51.4 KB
/
gsgmail_connector.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File: gsgmail_connector.py
#
# Copyright (c) 2017-2024 Splunk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific language governing permissions
# and limitations under the License.
#
#
# Phantom App imports
import base64
import email
import json
# Fix to add __init__.py in dependencies folder
import os
import sys
from copy import deepcopy
from email import encoders
from email.mime import application, audio, base, image, multipart, text
from email.mime.text import MIMEText
from io import BytesIO
import phantom.app as phantom
import phantom.utils as ph_utils
import phantom.vault as phantom_vault
import requests
from google.oauth2 import service_account
from googleapiclient import errors
from googleapiclient.http import MediaIoBaseUpload
from phantom.action_result import ActionResult
from phantom.base_connector import BaseConnector
from phantom.vault import Vault
from requests.structures import CaseInsensitiveDict
from gsgmail_consts import *
from gsgmail_process_email import ProcessMail
init_path = "{}/dependencies/google/__init__.py".format(os.path.dirname(os.path.abspath(__file__))) # noqa # noqa # noqa
# and _also_ debug the connector as a script via pudb
try:
open(init_path, "a+").close() # noqa
argv_temp = list(sys.argv)
except Exception:
pass
sys.argv = [""]
import apiclient # noqa
class RetVal2(tuple):
def __new__(cls, val1, val2=None):
return tuple.__new__(RetVal2, (val1, val2))
# Define the App Class
class GSuiteConnector(BaseConnector):
def __init__(self):
self._key_dict = None
self._domain = None
self._state = {}
self._login_email = None
self._dup_emails = 0
self._previous_emails = 0
self._last_email_epoch = None
# Call the BaseConnectors init first
super(GSuiteConnector, self).__init__()
def _create_service(self, action_result, scopes, api_name, api_version, delegated_user=None):
self.debug_print("key json:")
self.debug_print(self._key_dict)
# first the credentials
try:
credentials = service_account.Credentials.from_service_account_info(self._key_dict, scopes=scopes)
except Exception as e:
return RetVal2(
action_result.set_status(phantom.APP_ERROR, GSGMAIL_SERVICE_KEY_FAILED, self._get_error_message_from_exception(e)), None
)
if delegated_user:
try:
credentials = credentials.with_subject(delegated_user)
except Exception as e:
return RetVal2(
action_result.set_status(phantom.APP_ERROR, GSGMAIL_CREDENTIALS_FAILED, self._get_error_message_from_exception(e)), None
)
try:
service = apiclient.discovery.build(api_name, api_version, credentials=credentials)
except Exception as e:
return RetVal2(
action_result.set_status(
phantom.APP_ERROR,
FAILED_CREATE_SERVICE.format(
api_name, api_version, self._get_error_message_from_exception(e), GSMAIL_USER_VALID_MESSAGE.format(delegated_user)
),
None,
)
)
return RetVal2(phantom.APP_SUCCESS, service)
def initialize(self):
self._state = self.load_state()
if self._state:
self._last_email_epoch = self._state.get("last_email_epoch")
config = self.get_config()
key_json = config["key_json"]
try:
self._key_dict = json.loads(key_json)
except Exception as e:
return self.set_status(phantom.APP_ERROR, "Unable to load the key json", self._get_error_message_from_exception(e))
self._login_email = config["login_email"]
if not ph_utils.is_email(self._login_email):
return self.set_status(phantom.APP_ERROR, "Asset config 'login_email' failed validation")
try:
_, _, self._domain = self._login_email.partition("@")
except Exception:
return self.set_status(phantom.APP_ERROR, "Unable to extract domain from login_email")
return phantom.APP_SUCCESS
def _validate_integer(self, action_result, parameter, key, allow_zero=False):
"""
Validate an integer.
:param action_result: Action result or BaseConnector object
:param parameter: input parameter
:param key: input parameter message key
:allow_zero: whether zero should be considered as valid value or not
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS, integer value of the parameter or None in case of failure
"""
if parameter is not None:
try:
if not float(parameter).is_integer():
return action_result.set_status(phantom.APP_ERROR, GSGMAIL_INVALID_INTEGER_ERROR_MESSAGE.format(msg="", param=key)), None
parameter = int(parameter)
except Exception:
return action_result.set_status(phantom.APP_ERROR, GSGMAIL_INVALID_INTEGER_ERROR_MESSAGE.format(msg="", param=key)), None
if parameter < 0:
message = GSGMAIL_INVALID_INTEGER_ERROR_MESSAGE.format(msg="non-negative", param=key)
return action_result.set_status(phantom.APP_ERROR, message), None
if not allow_zero and parameter == 0:
msg = "non-zero positive"
return action_result.set_status(phantom.APP_ERROR, GSGMAIL_INVALID_INTEGER_ERROR_MESSAGE.format(msg=msg, param=key)), None
return phantom.APP_SUCCESS, parameter
def _get_error_message_from_exception(self, e):
"""
Get appropriate error message from the exception.
:param e: Exception object
:return: error message
"""
error_code = None
error_message = GSGMAIL_ERROR_MESSAGE_UNAVAILABLE
self.error_print("Error occurred.", e)
try:
if hasattr(e, "args"):
if len(e.args) > 1:
error_code = e.args[0]
error_message = e.args[1]
elif len(e.args) == 1:
error_message = e.args[0]
except Exception as e:
self.error_print("Error occurred while fetching exception information. Details: {}".format(str(e)))
if not error_code:
error_text = "Error Message: {}".format(error_message)
else:
error_text = "Error Code: {}. Error Message: {}".format(error_code, error_message)
return error_text
def _get_email_details(self, action_result, email_addr, email_id, service, results_format="metadata"):
kwargs = {"userId": email_addr, "id": email_id, "format": results_format}
try:
email_details = service.users().messages().get(**kwargs).execute()
except Exception as e:
return RetVal2(action_result.set_status(phantom.APP_ERROR, GSGMAIL_EMAIL_FETCH_FAILED, self._get_error_message_from_exception(e)))
return RetVal2(phantom.APP_SUCCESS, email_details)
def _map_email_details(self, input_email):
# The dictionary of header values
header_dict = dict()
# list of values that are to be extracted
headers_to_parse = ["subject", "delivered-to", "from", "to", "message-id"]
# get the payload
email_headers = input_email.pop("payload", {}).get("headers")
if not email_headers:
return input_email
for x in email_headers:
if not headers_to_parse:
break
header_name = x.get("name")
header_value = x.get("value", "")
if not header_name:
continue
if header_name.lower() not in headers_to_parse:
continue
key_name = header_name.lower().replace("-", "_")
header_dict[key_name] = header_value
headers_to_parse.remove(header_name.lower())
input_email.update(header_dict)
return phantom.APP_SUCCESS, input_email
def _get_email_headers_from_part(self, part):
email_headers = list(part.items())
if not email_headers:
return {}
# Convert the header tuple into a dictionary
headers = CaseInsensitiveDict()
# assume duplicate header names with unique values. ex: received
for x in email_headers:
try:
headers.setdefault(x[0].lower().replace("-", "_").replace(" ", "_"), []).append(x[1])
except Exception as e:
error_message = self._get_error_message_from_exception(e)
err = "Error occurred while converting the header tuple into a dictionary"
self.error_print("{}. {}".format(err, error_message))
headers = {k.lower(): "\n".join(v) for k, v in headers.items()}
return dict(headers)
def _handle_run_query(self, param):
# Implement the handler here, some basic code is already in
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
# Add an action result object to self (BaseConnector) to represent the action for this param
action_result = self.add_action_result(ActionResult(dict(param)))
# Create the credentials with the required scope
scopes = [GSGMAIL_AUTH_GMAIL_READ]
# Create a service here
self.save_progress("Creating GMail service object")
user_email = param["email"]
ret_val, service = self._create_service(action_result, scopes, "gmail", "v1", user_email)
if phantom.is_fail(ret_val):
return action_result.get_status()
# create the query string
query_string = ""
query_dict = {
"label": param.get("label"),
"subject": param.get("subject"),
"from": param.get("sender"),
"rfc822msgid": param.get("internet_message_id"),
}
query_string = " ".join("{}:{}".format(key, value) for key, value in query_dict.items() if value is not None)
if "body" in param:
query_string += " {0}".format(param.get("body"))
# if query is present, then override everything
if "query" in param:
query_string = param.get("query")
"""
# Check if there is something present in the query string
if (not query_string):
return action_result.set_status(phantom.APP_ERROR, "Please specify at-least one search criteria")
"""
ret_val, max_results = self._validate_integer(action_result, param.get("max_results", 100), "max_results")
if phantom.is_fail(ret_val):
return action_result.get_status()
kwargs = {"maxResults": max_results, "userId": user_email, "q": query_string}
page_token = param.get("page_token")
if page_token:
kwargs.update({"pageToken": page_token})
try:
messages_resp = service.users().messages().list(**kwargs).execute()
except Exception as e:
return action_result.set_status(phantom.APP_ERROR, "Failed to get messages", self._get_error_message_from_exception(e))
messages = messages_resp.get("messages", [])
next_page = messages_resp.get("nextPageToken")
summary = action_result.update_summary({"total_messages_returned": len(messages)})
for curr_message in messages:
curr_email_ar = ActionResult()
ret_val, email_details_resp = self._get_email_details(curr_email_ar, user_email, curr_message["id"], service)
if phantom.is_fail(ret_val):
continue
ret_val, email_details_resp = self._map_email_details(email_details_resp)
if phantom.is_fail(ret_val):
continue
action_result.add_data(email_details_resp)
if next_page:
summary["next_page_token"] = next_page
return action_result.set_status(phantom.APP_SUCCESS)
def _body_from_part(self, part):
charset = part.get_content_charset() or "utf-8"
# decode the base64 unicode bytestring into plain text
return part.get_payload(decode=True).decode(encoding=charset, errors="ignore")
def _create_artifact(self, file_name, attach_resp):
return {
"name": "Email Attachment Artifact",
"container_id": self.get_container_id(),
"cef": {
"vaultId": attach_resp[phantom.APP_JSON_HASH],
"fileHash": attach_resp[phantom.APP_JSON_HASH],
"file_hash": attach_resp[phantom.APP_JSON_HASH],
"fileName": file_name,
},
"run_automation": False,
"source_data_identifier": None,
}
def _parse_email_details(self, part, email_details):
headers = self._get_email_headers_from_part(part)
# split out important headers (for output table rendering)
if headers.get("to"):
email_details["to"] = headers["to"]
if headers.get("from"):
email_details["from"] = headers["from"]
if headers.get("subject"):
email_details["subject"] = headers["subject"]
part_type = part.get_content_type()
if part_type == "text/plain":
email_details["plain_bodies"].append(self._body_from_part(part))
elif part_type == "text/html":
email_details["html_bodies"].append(self._body_from_part(part))
email_details["email_headers"].append(headers)
def _get_payload_content(self, part):
if part.get_content_type().startswith("message/"):
return part.get_payload(0).as_string()
return part.get_payload(decode=True)
def _extract_attachment(self, part, action_result):
attach_resp = None
file_name = part.get_filename()
try:
# Create vault item with attachment payload
attach_resp = Vault.create_attachment(
self._get_payload_content(part),
container_id=self.get_container_id(),
file_name=file_name,
)
except Exception as e:
return action_result.set_status(
phantom.APP_ERROR,
f"Unable to add attachment: {file_name} Error: {self._get_error_message_from_exception(e)}",
)
if attach_resp.get("succeeded"):
# Create vault artifact
ret_val, msg, _ = self.save_artifact(self._create_artifact(file_name, attach_resp))
if phantom.is_fail(ret_val):
return action_result.set_status(
phantom.APP_ERROR,
f"Could not save artifact to container: {msg}",
)
return phantom.APP_SUCCESS
@staticmethod
def _is_attachment(part):
return "attachment" in str(part.get("Content-Disposition"))
def _init_detail_fields(self, email_details):
email_details["plain_bodies"] = []
email_details["html_bodies"] = []
email_details["email_headers"] = []
def _join_email_bodies(self, email_details):
email_details["parsed_plain_body"] = "\n\n".join(email_details.pop("plain_bodies"))
email_details["parsed_html_body"] = "\n\n".join(email_details.pop("html_bodies"))
def __recursive_part_traverse(
self,
part,
email_details,
action_result,
extract_attachments=False,
extract_nested=False,
in_attachment=False,
):
is_attachment = self._is_attachment(part)
# We are only gathering email data from top email, any attachment email should be omitted
if not is_attachment and not in_attachment:
self._parse_email_details(part, email_details)
ret_val = phantom.APP_SUCCESS
if is_attachment and extract_attachments:
ret_val = self._extract_attachment(part, action_result)
if phantom.is_fail(ret_val):
return ret_val
if not extract_nested and is_attachment:
return ret_val
if part.is_multipart():
for subpart in part.get_payload():
# We assume that everything that is under attachment is also an attachment
ret_val = ret_val and self.__recursive_part_traverse(
subpart,
email_details,
action_result,
extract_attachments,
extract_nested,
is_attachment or in_attachment,
)
return ret_val
def _parse_multipart_message(
self,
action_result,
msg,
email_details,
extract_attachments=False,
extract_nested=False,
):
self._init_detail_fields(email_details)
ret_val = self.__recursive_part_traverse(msg, email_details, action_result, extract_attachments, extract_nested)
self._join_email_bodies(email_details)
return ret_val
def _handle_get_email(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(ActionResult(dict(param)))
scopes = [GSGMAIL_AUTH_GMAIL_READ]
user_email = param["email"]
self.save_progress("Creating GMail service object")
ret_val, service = self._create_service(action_result, scopes, "gmail", "v1", user_email)
if phantom.is_fail(ret_val):
return action_result.get_status()
query_string = ""
if "internet_message_id" in param:
query_string += " rfc822msgid:{0}".format(param["internet_message_id"])
kwargs = {"q": query_string, "userId": user_email}
config = self.get_config()
format = param.get("format", None)
if not format:
format = config["default_format"]
try:
messages_resp = service.users().messages().list(**kwargs).execute()
except Exception as e:
return action_result.set_status(phantom.APP_ERROR, "Failed to get messages", self._get_error_message_from_exception(e))
messages = messages_resp.get("messages", [])
action_result.update_summary({"total_messages_returned": len(messages)})
for curr_message in messages:
curr_email_ar = ActionResult()
ret_val, email_details_resp = self._get_email_details(curr_email_ar, user_email, curr_message["id"], service, format)
if phantom.is_fail(ret_val):
continue
if format == "raw":
raw_encoded = base64.urlsafe_b64decode(email_details_resp.pop("raw").encode("UTF8"))
msg = email.message_from_bytes(raw_encoded)
if msg.is_multipart():
ret_val = self._parse_multipart_message(
action_result,
msg,
email_details_resp,
param.get("extract_attachments", False),
param.get("extract_nested", False),
)
if phantom.is_fail(ret_val):
return action_result.get_status()
else:
# not multipart
email_details_resp["email_headers"] = []
charset = msg.get_content_charset()
headers = self._get_email_headers_from_part(msg)
email_details_resp["email_headers"].append(headers)
try:
email_details_resp["parsed_plain_body"] = msg.get_payload(decode=True).decode(encoding=charset, errors="ignore")
except Exception as e:
message = self._get_error_message_from_exception(e)
self.error_print(f"Unable to add email body: {message}")
elif format == "metadata":
email_details_resp["email_headers"] = []
payload = email_details_resp.pop("payload")
email_details_resp["email_headers"].append(self._parse_msg_payload_headers(payload))
action_result.add_data(email_details_resp)
return action_result.set_status(phantom.APP_SUCCESS)
def _parse_msg_payload_headers(self, msg_payload):
email_headers = msg_payload.get("headers", [])
headers = CaseInsensitiveDict()
for x in email_headers:
try:
key = x["name"]
value = x["value"]
headers.setdefault(key.lower().replace("-", "_").replace(" ", "_"), []).append(value)
except Exception as e:
error_message = self._get_error_message_from_exception(e)
err = "Error occurred while converting the header tuple into a dictionary"
self.error_print("{}. {}".format(err, error_message))
headers = {k.lower(): "\n".join(v) for k, v in headers.items()}
return dict(headers)
def _handle_delete_email(self, param):
# Implement the handler here, some basic code is already in
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
# Add an action result object to self (BaseConnector) to represent the action for this param
action_result = self.add_action_result(ActionResult(dict(param)))
# Create the credentials with the required scope
scopes = [GSGMAIL_DELETE_EMAIL]
# Create a service here
self.save_progress("Creating GMail service object")
user_email = param["email"]
ret_val, service = self._create_service(action_result, scopes, "gmail", "v1", user_email)
if phantom.is_fail(ret_val):
return action_result.get_status()
email_ids = [x.strip() for x in param["id"].split(",")]
email_ids = list(filter(None, email_ids))
if not email_ids:
return action_result.set_status(phantom.APP_ERROR, "Please provide valid value for 'id' action parameter")
good_ids = set()
bad_ids = set()
for email_id in email_ids:
kwargs = {"id": email_id, "userId": user_email}
try:
get_msg_resp = service.users().messages().get(**kwargs).execute() # noqa
except apiclient.errors.HttpError:
self.error_print("Caught HttpError")
bad_ids.add(email_id)
continue
except Exception as e:
self.error_print("Exception name: {}".format(e.__class__.__name__))
error_message = self._get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, "Error checking email. ID: {} Reason: {}.".format(email_id, error_message))
good_ids.add(email_id)
if not good_ids:
summary = action_result.update_summary({})
summary["deleted_emails"] = list(good_ids)
summary["ignored_ids"] = list(bad_ids)
return action_result.set_status(
phantom.APP_SUCCESS, "All the provided emails were already deleted, Ignored Ids : {}".format(summary["ignored_ids"])
)
kwargs = {"body": {"ids": email_ids}, "userId": user_email}
try:
service.users().messages().batchDelete(**kwargs).execute()
except Exception as e:
return action_result.set_status(phantom.APP_ERROR, "Failed to delete messages", self._get_error_message_from_exception(e))
summary = action_result.update_summary({})
summary["deleted_emails"] = list(good_ids)
summary["ignored_ids"] = list(bad_ids)
return action_result.set_status(phantom.APP_SUCCESS, "Messages deleted, Ignored Ids : {}".format(summary["ignored_ids"]))
def _handle_get_users(self, param):
# Implement the handler here, some basic code is already in
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
# Add an action result object to self (BaseConnector) to represent the action for this param
action_result = self.add_action_result(ActionResult(dict(param)))
# Create the credentials with the required scope
scopes = [GSGMAIL_AUTH_GMAIL_ADMIN_DIR]
# Create a service here
self.save_progress("Creating AdminSDK service object")
ret_val, service = self._create_service(action_result, scopes, "admin", "directory_v1", self._login_email)
if phantom.is_fail(ret_val):
return action_result.get_status()
self.save_progress("Getting list of users for domain: {0}".format(self._domain))
ret_val, max_users = self._validate_integer(action_result, param.get("max_items", 500), "max_items")
if phantom.is_fail(ret_val):
return action_result.get_status()
kwargs = {"domain": self._domain, "maxResults": max_users, "orderBy": "email", "sortOrder": "ASCENDING"}
page_token = param.get("page_token")
if page_token:
kwargs.update({"pageToken": page_token})
try:
users_resp = service.users().list(**kwargs).execute()
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, GSGMAIL_USERS_FETCH_FAILED, error_message)
users = users_resp.get("users", [])
num_users = len(users)
next_page = users_resp.get("nextPageToken")
summary = action_result.update_summary({"total_users_returned": num_users})
for curr_user in users:
action_result.add_data(curr_user)
if next_page:
summary["next_page_token"] = next_page
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_get_user(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
# Add an action result object to self (BaseConnector) to represent the action for this param
action_result = self.add_action_result(ActionResult(dict(param)))
# Create the credentials with the required scope
scopes = [GSGMAIL_AUTH_GMAIL_READ]
self.save_progress("Creating AdminSDK service object")
ret_val, service = self._create_service(action_result, scopes, "gmail", "v1", param["email"])
if phantom.is_fail(ret_val):
return action_result.get_status()
try:
user_info = service.users().getProfile(userId="me").execute()
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, GSGMAIL_USER_FATCH_FAILED, error_message)
action_result.add_data(user_info)
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_test_connectivity(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
# Add an action result object to self (BaseConnector) to represent the action for this param
action_result = self.add_action_result(ActionResult(dict(param)))
# Create the credentials, with minimal scope info for test connectivity
scopes = [GSGMAIL_AUTH_GMAIL_ADMIN_DIR]
# Test connectivity does not return any data, it's the status that is more important
# and the progress messages
# Create a service here
self.save_progress("Creating AdminSDK service object")
ret_val, service = self._create_service(action_result, scopes, "admin", "directory_v1", self._login_email)
if phantom.is_fail(ret_val):
self.save_progress("Test Connectivity Failed")
return action_result.get_status()
self.save_progress("Getting list of users for domain: {0}".format(self._domain))
try:
service.users().list(domain=self._domain, maxResults=1, orderBy="email", sortOrder="ASCENDING").execute()
except Exception as e:
self.save_progress("Test Connectivity Failed")
return action_result.set_status(phantom.APP_ERROR, "Failed to get users", self._get_error_message_from_exception(e))
# Return success
self.save_progress("Test Connectivity Passed")
return action_result.set_status(phantom.APP_SUCCESS)
def _get_email_ids_to_process(
self,
service,
action_result,
max_results,
ingest_manner,
user_id="me",
labels=[],
include_spam_trash=False,
q=None,
use_ingest_limit=False,
):
kwargs = {"userId": user_id, "includeSpamTrash": include_spam_trash, "maxResults": GSMAIL_MAX_RESULT}
label_ids = []
if labels:
if "labels" not in self._state:
self._state["labels"] = {}
for label in labels:
if label.lower() not in self._state["labels"]:
try:
response = service.users().labels().list(userId=user_id).execute() # pylint: disable=E1101
gmail_labels = response["labels"]
for gmail_label in gmail_labels:
if gmail_label["name"].lower() == label.lower():
self._state["labels"][label.lower()] = gmail_label["id"]
except errors.HttpError as error:
return action_result.set_status(phantom.APP_ERROR, error), None
if label.lower() not in self._state["labels"]:
return action_result.set_status(phantom.APP_ERROR, 'Unable to find label "{}"'.format(label)), None
label_ids.append(self._state["labels"][label.lower()])
if label_ids:
kwargs["labelIds"] = label_ids
query = []
if q:
query.append(q)
using_oldest = ingest_manner == GSMAIL_OLDEST_INGEST_MANNER
using_latest = ingest_manner == GSMAIL_LATEST_INGEST_MANNER
if use_ingest_limit and not self.is_poll_now():
if self._last_email_epoch:
query.append("after:{}".format(self._last_email_epoch))
kwargs["q"] = " ".join(query)
try:
response = service.users().messages().list(**kwargs).execute() # pylint: disable=E1101
messages = []
if "messages" in response:
messages.extend(response["messages"])
while "nextPageToken" in response:
if max_results and using_latest and len(messages) > max_results:
break
kwargs["pageToken"] = response["nextPageToken"]
response = service.users().messages().list(**kwargs).execute() # pylint: disable=E1101
messages.extend(response["messages"])
message_ids = [x["id"] for x in messages]
if max_results and len(message_ids) > max_results:
if using_oldest:
message_ids = message_ids[-max_results:]
else:
message_ids = message_ids[:max_results]
if self._previous_emails:
message_ids = message_ids[: -self._previous_emails]
return action_result.set_status(phantom.APP_SUCCESS), message_ids
except errors.HttpError as error:
return action_result.set_status(phantom.APP_ERROR, error), None
def _handle_on_poll(self, param):
# Implement the handler here, some basic code is already in
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
# Add an action result object to self (BaseConnector) to represent the action for this param
action_result = self.add_action_result(ActionResult(dict(param)))
# Create the credentials with the required scope
scopes = [GSGMAIL_AUTH_GMAIL_READ]
# Create a service here
self.save_progress("Creating GMail service object")
# if user_email param is not present use login_email
config = self.get_config()
login_email = config["login_email"]
self.save_progress("login_email is {0}".format(login_email))
ret_val, service = self._create_service(action_result, scopes, "gmail", "v1", login_email)
if phantom.is_fail(ret_val):
return action_result.get_status()
if self.is_poll_now():
ret_val, max_emails = self._validate_integer(
action_result, param.get(phantom.APP_JSON_CONTAINER_COUNT), "container count", allow_zero=False
)
if phantom.is_fail(ret_val):
return action_result.get_status()
self.save_progress(GSMAIL_POLL_NOW_PROGRESS)
else:
ret_val1, first_run_max_emails = self._validate_integer(
action_result, config.get("first_run_max_emails", GSMAIL_DEFAULT_FIRST_RUN_MAX_EMAIL), "first_max_emails", allow_zero=False
)
ret_val2, max_containers = self._validate_integer(
action_result, config.get("max_containers", GSMAIL_DEFAULT_MAX_CONTAINER), "max_containers", allow_zero=False
)
if phantom.is_fail(ret_val1) or phantom.is_fail(ret_val2):
return action_result.get_status()
if self._state.get("first_run", True):
self._state["first_run"] = False
max_emails = first_run_max_emails
self.save_progress(GSMAIL_FIRST_INGES_DELETED)
else:
max_emails = max_containers
ingestion_data_type = config.get("data_type", "utf-8")
run_limit = deepcopy(max_emails)
action_result = self.add_action_result(ActionResult(dict(param)))
email_id = param.get(phantom.APP_JSON_CONTAINER_ID, False)
email_ids = [email_id]
total_ingested = 0
ingest_manner = config.get("ingest_manner", GSMAIL_OLDEST_INGEST_MANNER)
while True:
self._dup_emails = 0
if not email_id:
ret_val, email_ids = self._get_email_ids(action_result, config, service, max_emails, ingest_manner)
if phantom.is_fail(ret_val):
return action_result.get_status()
if not email_ids:
return action_result.set_status(phantom.APP_SUCCESS)
if not ingestion_data_type:
ingestion_data_type = "utf-8"
self._process_email_ids(action_result, config, service, email_ids, ingestion_data_type)
total_ingested += max_emails - self._dup_emails - self._previous_emails
if ingest_manner == GSMAIL_LATEST_INGEST_MANNER or total_ingested >= run_limit or self.is_poll_now():
break
self._previous_emails = max_emails
max_emails = max_emails + min(self._dup_emails, run_limit)
return phantom.APP_SUCCESS
def _create_message(self, sender, to, cc, bcc, subject, message_text, reply_to=None, additional_headers={}, vault_ids=[]):
message = multipart.MIMEMultipart("alternative")
message["to"] = to
message["from"] = sender
message["subject"] = subject
if cc:
message["cc"] = cc
if bcc:
message["bcc"] = bcc
if reply_to:
message["Reply-To"] = reply_to
for key, value in additional_headers.items():
message[key] = value
part1 = MIMEText(message_text, "plain")
message.attach(part1)
# Attach HTML part with plain text content
part2 = MIMEText(message_text, "html")
message.attach(part2)
current_size = 0
mime_consumer = {"text": text.MIMEText, "image": image.MIMEImage, "audio": audio.MIMEAudio}
for vault_id in vault_ids:
vault_info = self._get_vault_info(vault_id)
if not vault_info:
self.debug_print("Failed to find vault entry {}".format(vault_id))
continue
current_size += vault_info["size"]
if current_size > GSGMAIL_ATTACHMENTS_CUTOFF_SIZE:
self.debug_print("Total attachment size reached max capacity. No longer adding attachments after vault id {0}".format(vault_id))
break
content_type = vault_info["mime_type"]
main_type, sub_type = content_type.split("/", 1) if "/" in content_type else ("application", "octet-stream")
consumer = None
if main_type in mime_consumer:
consumer = mime_consumer[main_type]
elif main_type == "application" and sub_type == "pdf":
consumer = application.MIMEApplication
self.debug_print("Content type is {0}".format(content_type))
attachment_part = None
mode = "r" if main_type == "text" else "rb"
if not consumer:
attachment_part = base.MIMEBase(main_type, sub_type)
with open(vault_info["path"], mode=mode) as file:
file_content = file.read()
attachment_part.set_payload(file_content)
encoders.encode_base64(attachment_part)
else:
with open(vault_info["path"], mode=mode) as file:
attachment_part = consumer(file.read(), _subtype=sub_type)
attachment_part.add_header("Content-Disposition", "attachment; filename={0}".format(vault_info["name"]))
attachment_part.add_header("Content-Length", str(vault_info["size"])) # File size in bytes
attachment_part.add_header("Content-ID", vault_info["vault_id"])
message.attach(attachment_part)
return message
def _send_email(self, service, user_id, media, action_result):
try:
sent_message = service.users().messages().send(userId=user_id, body={}, media_body=media).execute()
return phantom.APP_SUCCESS, sent_message
except Exception as error:
self.debug_print("Error occured when sending draft: {0}".format(error))
return action_result.set_status(phantom.APP_ERROR, "Message not sent because of {0}".format(error)), None
def _get_vault_info(self, vault_id):
_, _, vault_infos = phantom_vault.vault_info(container_id=self.get_container_id(), vault_id=vault_id)
if not vault_infos:
_, _, vault_infos = phantom_vault.vault_info(vault_id=vault_id)
return vault_infos[0] if vault_infos else None
def _create_send_as_alias(self, service, user_id, alias_email, action_result, display_name=None):
send_as = {"sendAsEmail": alias_email, "replyAsEmail": alias_email, "treatAsAlias": True, "isPrimary": False}
if display_name:
send_as["displayName"] = display_name
try:
result = service.users().settings().sendAs().create(userId=user_id, body=send_as).execute()
return phantom.APP_SUCCESS, result
except errors.HttpError as error:
return action_result.set_status(phantom.APP_ERROR, error), None
def _create_alias(self, service, user_id, alias_email):
alias_body = {"alias": alias_email}
try:
result = service.users().aliases().insert(userKey=user_id, body=alias_body).execute()
return phantom.APP_SUCCESS, result
except errors.HttpError as error: