-
Notifications
You must be signed in to change notification settings - Fork 0
/
googlepeople_connector.py
589 lines (444 loc) · 23.3 KB
/
googlepeople_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
# File: googlepeople_connector.py
#
# Copyright (c) 2021-2023 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 json
import os
from html import unescape
import phantom.app as phantom
import phantom.utils as ph_utils
import requests
from bs4 import UnicodeDammit
from google.oauth2 import service_account
from googleapiclient import discovery
from googleapiclient.errors import HttpError
from phantom.action_result import ActionResult
from phantom.base_connector import BaseConnector
from googlepeople_consts import *
init_path = '{}/dependencies/google/__init__.py'.format(
os.path.dirname(os.path.abspath(__file__))
)
try:
open(init_path, 'a+').close()
except:
pass
class RetVal(tuple):
def __new__(cls, val1, val2=None):
return tuple.__new__(RetVal, (val1, val2))
class GooglePeopleConnector(BaseConnector):
def __init__(self):
# Call the BaseConnectors init first
super(GooglePeopleConnector, self).__init__()
self._login_email = None
self._key_dict = None
self._state = None
def _get_error_message_from_exception(self, e):
""" This method is used to get appropriate error messages from the exception.
:param e: Exception object
:return: error message
"""
try:
if e.args:
if len(e.args) > 1:
error_code = e.args[0]
error_message = e.args[1]
elif len(e.args) == 1:
error_code = ERROR_CODE_MESSAGE
error_message = e.args[0]
else:
error_code = ERROR_CODE_MESSAGE
error_message = ERROR_MESSAGE_UNAVAILABLE
except:
error_code = ERROR_CODE_MESSAGE
error_message = ERROR_MESSAGE_UNAVAILABLE
try:
if error_code in ERROR_CODE_MESSAGE:
error_text = "Error Message: {0}".format(error_message)
else:
error_text = "Error Code: {0}. Error Message: {1}".format(error_code, error_message)
except:
self.debug_print(PARSE_ERROR_MESSAGE)
error_text = PARSE_ERROR_MESSAGE
return error_text
def _validate_integer(self, action_result, parameter, key):
if parameter is not None:
try:
if not float(parameter).is_integer():
return action_result.set_status(phantom.APP_ERROR, INVALID_INTEGER_ERROR_MESSAGE.format(key)), None
parameter = int(parameter)
except:
return action_result.set_status(phantom.APP_ERROR, INVALID_INTEGER_ERROR_MESSAGE.format(key)), None
if parameter <= 0:
return action_result.set_status(phantom.APP_ERROR, INVALID_NON_ZERO_NON_NEGATIVE_INTEGER_ERROR_MESSAGE.format(key)), None
return phantom.APP_SUCCESS, parameter
def _create_client(self, action_result, scopes):
credentials = None
try:
credentials = service_account.Credentials.from_service_account_info(self._key_dict, scopes=scopes)
except Exception as e:
err_message = self._get_error_message_from_exception(e)
return RetVal(action_result.set_status(
phantom.APP_ERROR, "Unable to get the credentials from the key json. {}".format(err_message)), None)
if self._login_email:
try:
credentials = credentials.with_subject(self._login_email)
except Exception as e:
err_message = self._get_error_message_from_exception(e)
return RetVal(
action_result.set_status(phantom.APP_ERROR, "Failed to create delegated credentials. {}".format(err_message)), None)
try:
client = discovery.build('people', 'v1', credentials=credentials)
except Exception as e:
err_message = self._get_error_message_from_exception(e)
return RetVal(action_result.set_status(phantom.APP_ERROR, "Unable to create client. {}".format(err_message)), None)
return RetVal(phantom.APP_SUCCESS, client)
def _paginator(self, client, fields, limit):
"""
This method repeatedly makes API calls until the requested number of records are fetched from the server.
:param client: The object of google client API
:param fields: The fields to be fetched
:param limit: The number of records to be fetched
"""
kwargs = dict()
list_items = list()
page_token = None
action_id = self.get_action_identifier()
kwargs.update({'pageSize': 1000})
while True:
if page_token:
kwargs.update({"pageToken": page_token})
if action_id == "list_other_contacts":
kwargs.update({'readMask': fields})
response = client.otherContacts().list(**kwargs).execute()
if response.get("otherContacts"):
list_items.extend(response.get("otherContacts"))
elif action_id == "list_directory":
kwargs.update({'sources': ['DIRECTORY_SOURCE_TYPE_DOMAIN_CONTACT', 'DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE']})
kwargs.update({'readMask': fields})
response = client.people().listDirectoryPeople(**kwargs).execute()
if response.get("people"):
list_items.extend(response.get("people"))
elif action_id == "list_people":
kwargs.update({'sources': ['READ_SOURCE_TYPE_CONTACT']})
kwargs.update({'personFields': fields})
response = client.people().connections().list(resourceName='people/me', **kwargs).execute()
if response.get("connections"):
list_items.extend(response.get("connections"))
if limit and len(list_items) >= limit:
return list_items[:limit]
page_token = response.get('nextPageToken')
if not page_token:
break
return list_items
def _handle_test_connectivity(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
scopes = [GOOGLE_CONTACTS_SCOPE]
self.save_progress("Creating Google People client...")
ret_val, client = self._create_client(action_result, scopes)
if phantom.is_fail(ret_val):
self.save_progress("Test Connectivity Failed")
return action_result.get_status()
self.save_progress("Getting list of connections for {}".format(self._login_email))
try:
client.people().connections().list(resourceName='people/me', personFields='names,emailAddresses').execute()
except Exception as e:
self.save_progress("Test Connectivity Failed")
err_message = unescape(
UnicodeDammit(self._get_error_message_from_exception(e)).unicode_markup.encode('utf-8').decode('unicode_escape'))
return action_result.set_status(phantom.APP_ERROR, "Error while listing connections. {}".format(err_message))
# Return success
self.save_progress("Test Connectivity Passed")
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_list_other_contacts(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
scopes = [GOOGLE_OTHER_CONTACTS_SCOPE_READ_ONLY]
ret_val, client = self._create_client(action_result, scopes)
if phantom.is_fail(ret_val):
self.debug_print(GOOGLE_CREATE_CLIENT_FAILED_MESSAGE)
return action_result.get_status()
read_mask = param.get('read_mask', 'names,emailAddresses')
# Validation for comma-separated value
masks = [x.strip() for x in read_mask.split(",")]
masks = list(filter(None, masks))
if not masks:
return action_result.set_status(phantom.APP_ERROR, INVALID_COMMA_SEPARATED_ERROR_MESSAGE.format('read mask'))
read_mask = ",".join(masks)
limit = param.get('limit')
# Validate 'limit' action parameter
ret_val, limit = self._validate_integer(action_result, limit, LIMIT_KEY)
if phantom.is_fail(ret_val):
return action_result.get_status()
try:
otherContacts = self._paginator(client, read_mask, limit)
except HttpError as e:
if "_get_reason" in dir(e):
return action_result.set_status(phantom.APP_ERROR, "{}. {}".format(GOOGLE_LIST_OTHER_CONTACTS_FAILED_MESSAGE, e._get_reason()))
err_message = self._get_error_message_from_exception(e)
self.debug_print("Exception message: {}".format(err_message))
return action_result.set_status(phantom.APP_ERROR, GOOGLE_LIST_OTHER_CONTACTS_FAILED_MESSAGE)
except Exception as e:
err_message = self._get_error_message_from_exception(e)
self.debug_print("Exception message: {}".format(err_message))
return action_result.set_status(phantom.APP_ERROR, GOOGLE_LIST_OTHER_CONTACTS_FAILED_MESSAGE)
num_otherContacts = len(otherContacts)
for contact in otherContacts:
action_result.add_data(contact)
action_result.update_summary({'total_otherContacts_returned': num_otherContacts})
return action_result.set_status(
phantom.APP_SUCCESS, 'Successfully retrieved {} otherContact{}'.format(
num_otherContacts, '' if num_otherContacts == 1 else 's'
)
)
def _handle_copy_contact(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 = [GOOGLE_CONTACTS_SCOPE, GOOGLE_OTHER_CONTACTS_SCOPE_READ_ONLY]
ret_val, client = self._create_client(action_result, scopes)
if phantom.is_fail(ret_val):
self.debug_print(GOOGLE_CREATE_CLIENT_FAILED_MESSAGE)
return action_result.get_status()
resource_name = param['resource_name']
if OTHER_CONTACTS_RESOURCE_NAME_PREFIX not in resource_name:
return action_result.set_status(phantom.APP_ERROR, "Resource name of contact to be copied must be 'otherContact'")
data = {}
copy_mask = param.get('copy_mask', 'names,emailAddresses,phoneNumbers')
# Validation for comma-separated value
masks = [x.strip() for x in copy_mask.split(",")]
masks = list(filter(None, masks))
if not masks:
return action_result.set_status(phantom.APP_ERROR, INVALID_COMMA_SEPARATED_ERROR_MESSAGE.format('copy mask'))
copy_mask = ",".join(masks)
data.update({'copyMask': copy_mask})
try:
response = client.otherContacts().copyOtherContactToMyContactsGroup(resourceName=resource_name, body=data).execute()
except HttpError as e:
if "_get_reason" in dir(e):
return action_result.set_status(phantom.APP_ERROR, "{}. {}".format(GOOGLE_COPY_CONTACT_FAILED_MESSAGE, e._get_reason()))
err_message = self._get_error_message_from_exception(e)
self.debug_print("Exception message: {}".format(err_message))
return action_result.set_status(phantom.APP_ERROR, GOOGLE_COPY_CONTACT_FAILED_MESSAGE)
except Exception as e:
err_message = self._get_error_message_from_exception(e)
self.debug_print("Exception message: {}".format(err_message))
return action_result.set_status(phantom.APP_ERROR, GOOGLE_COPY_CONTACT_FAILED_MESSAGE)
action_result.add_data(response)
action_result.update_summary({'total_contacts_copied': 1})
return action_result.set_status(phantom.APP_SUCCESS, 'Successfully copied 1 contact')
def _handle_list_directory(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 = [GOOGLE_DIRECTORY_SCOPE_READ_ONLY]
ret_val, client = self._create_client(action_result, scopes)
if phantom.is_fail(ret_val):
self.debug_print(GOOGLE_CREATE_CLIENT_FAILED_MESSAGE)
return action_result.get_status()
read_mask = param.get('read_mask', 'names,emailAddresses')
# Validation for comma-separated value
masks = [x.strip() for x in read_mask.split(",")]
masks = list(filter(None, masks))
if not masks:
return action_result.set_status(phantom.APP_ERROR, INVALID_COMMA_SEPARATED_ERROR_MESSAGE.format('read mask'))
read_mask = ",".join(masks)
limit = param.get('limit')
# Validate 'limit' action parameter
ret_val, limit = self._validate_integer(action_result, limit, LIMIT_KEY)
if phantom.is_fail(ret_val):
return action_result.get_status()
try:
directoryPeople = self._paginator(client, read_mask, limit)
except HttpError as e:
if "_get_reason" in dir(e):
return action_result.set_status(phantom.APP_ERROR, "{}. {}".format(GOOGLE_LIST_DIRECTORY_FAILED_MESSAGE, e._get_reason()))
err_message = self._get_error_message_from_exception(e)
self.debug_print("Exception message: {}".format(err_message))
return action_result.set_status(phantom.APP_ERROR, GOOGLE_LIST_DIRECTORY_FAILED_MESSAGE)
except Exception as e:
err_message = self._get_error_message_from_exception(e)
self.debug_print("Exception message: {}".format(err_message))
return action_result.set_status(phantom.APP_ERROR, GOOGLE_LIST_DIRECTORY_FAILED_MESSAGE)
num_directoryPeople = len(directoryPeople)
action_result.update_summary({'total_people_returned': num_directoryPeople})
for person in directoryPeople:
action_result.add_data(person)
return action_result.set_status(
phantom.APP_SUCCESS, 'Successfully retrieved {} {}'.format(
num_directoryPeople, 'person' if num_directoryPeople == 1 else 'people'
)
)
def _handle_get_user_profile(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 = [GOOGLE_PROFILE_SCOPE, GOOGLE_CONTACTS_SCOPE]
resource_name = param['resource_name']
if resource_name.startswith('otherContacts/'):
return action_result.set_status(phantom.APP_ERROR, "This action cannot be performed on otherContacts")
ret_val, client = self._create_client(action_result, scopes)
if phantom.is_fail(ret_val):
self.debug_print(GOOGLE_CREATE_CLIENT_FAILED_MESSAGE)
return action_result.get_status()
kwargs = {'sources': ['READ_SOURCE_TYPE_CONTACT']}
person_fields = param.get('person_fields', 'names,emailAddresses')
# Validation for comma-separated value
fields = [x.strip() for x in person_fields.split(",")]
fields = list(filter(None, fields))
if not fields:
return action_result.set_status(phantom.APP_ERROR, INVALID_COMMA_SEPARATED_ERROR_MESSAGE.format('person fields'))
person_fields = ",".join(fields)
kwargs.update({'personFields': person_fields})
try:
response = client.people().get(resourceName=resource_name, **kwargs).execute()
except HttpError as e:
if "_get_reason" in dir(e):
return action_result.set_status(phantom.APP_ERROR, "{}. {}".format(GOOGLE_GET_USER_PROFILE_FAILED_MESSAGE, e._get_reason()))
err_message = self._get_error_message_from_exception(e)
self.debug_print("Exception message: {}".format(err_message))
return action_result.set_status(phantom.APP_ERROR, GOOGLE_GET_USER_PROFILE_FAILED_MESSAGE)
except Exception as e:
err_message = self._get_error_message_from_exception(e)
self.debug_print("Exception message: {}".format(err_message))
return action_result.set_status(phantom.APP_ERROR, GOOGLE_GET_USER_PROFILE_FAILED_MESSAGE)
action_result.add_data(response)
action_result.update_summary({'resource_id_returned': response.get('resourceName')})
return action_result.set_status(phantom.APP_SUCCESS, "Successfully retrieved user profile")
def _handle_list_people(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 = [GOOGLE_CONTACTS_SCOPE]
ret_val, client = self._create_client(action_result, scopes)
if phantom.is_fail(ret_val):
self.debug_print(GOOGLE_CREATE_CLIENT_FAILED_MESSAGE)
return action_result.get_status()
person_fields = param.get('person_fields', 'names,emailAddresses')
# Validation for comma-separated value
fields = [x.strip() for x in person_fields.split(",")]
fields = list(filter(None, fields))
if not fields:
return action_result.set_status(phantom.APP_ERROR, INVALID_COMMA_SEPARATED_ERROR_MESSAGE.format('person fields'))
person_fields = ",".join(fields)
limit = param.get('limit')
# Validate 'limit' action parameter
ret_val, limit = self._validate_integer(action_result, limit, LIMIT_KEY)
if phantom.is_fail(ret_val):
return action_result.get_status()
try:
people = self._paginator(client, person_fields, limit)
except HttpError as e:
if "_get_reason" in dir(e):
return action_result.set_status(phantom.APP_ERROR, "{}. {}".format(GOOGLE_LIST_PEOPLE_FAILED_MESSAGE, e._get_reason()))
err_message = self._get_error_message_from_exception(e)
self.debug_print("Exception message: {}".format(err_message))
return action_result.set_status(phantom.APP_ERROR, GOOGLE_LIST_PEOPLE_FAILED_MESSAGE)
except Exception as e:
err_message = self._get_error_message_from_exception(e)
self.debug_print("Exception message: {}".format(err_message))
return action_result.set_status(phantom.APP_ERROR, GOOGLE_LIST_PEOPLE_FAILED_MESSAGE)
for person in people:
action_result.add_data(person)
num_people = len(people)
action_result.update_summary({'total_people_returned': num_people})
return action_result.set_status(
phantom.APP_SUCCESS, 'Successfully retrieved {} user{}'.format(num_people, '' if num_people == 1 else 's')
)
def handle_action(self, param):
ret_val = phantom.APP_SUCCESS
# Get the action that we are supposed to execute for this App Run
action_id = self.get_action_identifier()
self.debug_print("action_id: {}".format(self.get_action_identifier()))
if action_id == 'test_connectivity':
ret_val = self._handle_test_connectivity(param)
elif action_id == 'list_other_contacts':
ret_val = self._handle_list_other_contacts(param)
elif action_id == 'copy_contact':
ret_val = self._handle_copy_contact(param)
elif action_id == 'list_directory':
ret_val = self._handle_list_directory(param)
elif action_id == 'get_user_profile':
ret_val = self._handle_get_user_profile(param)
elif action_id == 'list_people':
ret_val = self._handle_list_people(param)
return ret_val
def initialize(self):
config = self.get_config()
self._state = self.load_state()
key_json = config['key_json']
try:
self._key_dict = json.loads(key_json)
except Exception as e:
err_message = self._get_error_message_from_exception(e)
self.debug_print("Exception message: {}".format(err_message))
return self.set_status(
phantom.APP_ERROR, "Please provide a valid value for the 'Contents of service account JSON file' asset configuration parameter")
self._login_email = config['login_email']
if not ph_utils.is_email(self._login_email):
return self.set_status(phantom.APP_ERROR, "Please provide a valid value for the 'Login email' asset configuration parameter")
return phantom.APP_SUCCESS
def finalize(self):
self.save_state(self._state)
return phantom.APP_SUCCESS
if __name__ == '__main__':
import argparse
import sys
import pudb
pudb.set_trace()
argparser = argparse.ArgumentParser()
argparser.add_argument('input_test_json', help='Input Test JSON file')
argparser.add_argument('-u', '--username', help='username', required=False)
argparser.add_argument('-p', '--password', help='password', required=False)
argparser.add_argument('-v', '--verify', action='store_true', help='verify', required=False, default=False)
args = argparser.parse_args()
session_id = None
username = args.username
password = args.password
verify = args.verify
if username is not None and password is None:
# User specified a username but not a password, so ask
import getpass
password = getpass.getpass("Password: ")
if username and password:
login_url = BaseConnector._get_phantom_base_url() + '/login'
try:
print("Accessing the Login page")
r = requests.get(login_url, verify=verify) # nosemgrep: python.requests.best-practice.use-timeout.use-timeout
csrftoken = r.cookies['csrftoken']
data = dict()
data['username'] = username
data['password'] = password
data['csrfmiddlewaretoken'] = csrftoken
headers = dict()
headers['Cookie'] = 'csrftoken=' + csrftoken
headers['Referer'] = login_url
print("Logging into Platform to get the session id")
r2 = requests.post(login_url, verify=verify, # nosemgrep: python.requests.best-practice.use-timeout.use-timeout
data=data, headers=headers)
session_id = r2.cookies['sessionid']
except Exception as e:
print("Unable to get session id from the platform. Error: " + str(e))
sys.exit(1)
if (len(sys.argv) < 2):
print("No test json specified as input")
sys.exit(0)
with open(args.input_test_json) as f:
in_json = f.read()
in_json = json.loads(in_json)
print(json.dumps(in_json, indent=4))
connector = GooglePeopleConnector()
connector.print_progress_message = True
if session_id is not None:
in_json['user_session_token'] = session_id
connector._set_csrf_info(csrftoken, headers['Referer'])
ret_val = connector._handle_action(json.dumps(in_json), None)
print(json.dumps(json.loads(ret_val), indent=4))
sys.exit(0)