-
Notifications
You must be signed in to change notification settings - Fork 0
/
outlook_git.py
407 lines (324 loc) · 11.7 KB
/
outlook_git.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
import os
import requests
import json
import socket
import itertools
from datetime import datetime
from datetime import timedelta
# It will be worth checking out this link on Getting Started with MS Graph O-Auth flow
# https://docs.microsoft.com/en-us/graph/auth-v2-user
######### DEFINE ENV ('prod', 'qa', 'dev') ###############
env = 'dev'
# Get Client - Id/Secret (of MS App) registered
cred_conf = {}
try:
cred_confFile = open("cred_conf.json", 'r')
cred_conf = json.loads(cred_confFile)
except:
print("Graph Crentials file missing")
exit(-1)
if( len(cred_conf['client_id']) or len(cred_conf['client_secret']) or len(cred_conf['redirect_uri']) or len(cred_conf['scope'])):
print("One or more credentials are empty, please correct!")
exit(-1)
client_id = cred_conf['client_id']
client_secret = cred_conf['client_secret']
redirect_uri = cred_conf['redirect_uri']
# scope = 'user.read+Mail.ReadWrite+Mail.Send'
scope = cred_conf['scope']
######### FOLDER ID MAP UPDATE ###########
folderIdMap = {}
try:
host_foldermap = open("folderIdMap.json", 'r')
folderIdMap = json.loads(host_foldermap.read())
except:
print("Folder ID map not found")
##### InfluxDB COnfig. ########
influxConfig = {}
try:
inf_conf = open("influxDb_config.json", 'r')
influxConfig_all = json.loads(inf_conf.read())
influxConfig = influxConfig_all[env]
except:
pass
######################
# eventMap = {
# 'error': {
# 'threshold': 3,
# 'timePeriod': 3
# }
# }
eventMap = {}
try:
config_file = open("alert_config.json", 'r')
eventMap = json.loads(config_file.read())
except:
print("No Events config file found")
exit()
################
eventTimeStamps = {}
eventHosts = {}
# Get the below URL
# auth_url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id="+ client_id +"&redirect_uri=" + redirect_uri + "&response_type=code&scope=offline_access+" + scope
# https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=c5ce7a56-2a52-45ff-a530-b7183c04e7bb&redirect_uri=http://localhost/&response_type=code&scope=offline_access%20user.read%20mail.readwrite%20mail.send
#enter the Auth code to genereate the Refresh token
# end-point to use
#
def writeNowTime():
global token_timestamp
f = open('/tmp/.time_stamp', 'w')
f.write(str(token_timestamp))
f.close()
# get refesh token
def get_set_refreshToken():
global auth_code
global refresh_token
token_endpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
p_load = "tenant=common&grant_type=authorization_code&code="+ auth_code + "&redirect_uri=" + redirect_uri + "&client_id=" + client_id + "&client_secret=" + client_secret
response = requests.post(token_endpoint, data=p_load, headers=headers)
if response.status_code == 200 and response.text != None:
parsed_response = json.loads(response.text)
refresh_token = str(parsed_response['refresh_token'])
return refresh_token
else:
print("Generate Refresh_Access_token - Failed - Exiting")
exit(-1)
# get access token
def get_accessToken():
global refresh_token
global client_id
global client_secret
global redirect_uri
token_endpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
p_load = "tenant=common&grant_type=refresh_token&refresh_token="+ refresh_token + "&redirect_uri=" + redirect_uri + "&client_id=" + client_id + "&client_secret=" + client_secret
response = requests.post(token_endpoint, data=p_load, headers=headers)
if response.status_code == 200 and response.text != None:
parsed_response = json.loads(response.text)
access_token = str(parsed_response['access_token'])
return access_token
else:
print("Generate Access_token - Failed - Exiting")
exit(-1)
def access_token():
global refresh_token
global token
global token_timestamp
if refresh_token == '':
print("No Refresh Token - Exiting")
exit(-1)
#get_set_refreshToken()
if token == '':
token = get_accessToken()
token_timestamp = datetime.now()
writeNowTime()
else:
try:
f = open('/tmp/.time_stamp', 'r')
token_timestamp = datetime.strptime(f.read(), '%Y-%m-%d %H:%M:%S.%f')
f.close()
nowTime = datetime.now()
deltaTime = (nowTime - token_timestamp).seconds
if deltaTime.seconds > 2700:
token = get_accessToken()
token_timestamp = datetime.now()
writeNowTime()
except:
token = get_accessToken()
token_timestamp = datetime.now()
writeNowTime()
return token
def folderId():
global folderIdMap
hostname = socket.gethostname()
if folderIdMap.get(hostname) != None:
return folderIdMap[hostname]
else:
url = 'https://graph.microsoft.com/v1.0/me/mailFolders'
headers = {'Authorization': 'Bearer '+access_token() , 'Content-Type': 'application/json'}
#list all and see if exists:
response = requests.get(url, headers=headers)
if response.status_code == 200:
parsed_response = json.loads(response.text)
folders = parsed_response['value']
for item in folders:
if str(item['displayName']) == hostname:
folderIdMap[hostname] = item['id']
f = open('folderIdMap.json', 'w')
f.write(json.dumps(folderIdMap))
f.close()
return item['id']
createURL = 'https://graph.microsoft.com/v1.0/me/mailFolders'
p_load = {'displayName': hostname}
head = {'Authorization': 'Bearer '+access_token() , 'Content-Type': 'application/json'}
response2 = requests.post(createURL, json=p_load, headers=head)
if response2.status_code == 201:
parsed_response2 = json.loads(response2.text)
if str(parsed_response2['displayName']) == hostname:
folderIdMap[hostname] = parsed_response2['id']
f = open('folderIdMap.json', 'w')
f.write(json.dumps(folderIdMap))
f.close()
return parsed_response2['id']
else:
exit(-1)
else:
exit(-1)
else:
exit(-1)
def moveEmail(msgId):
hostname = socket.gethostname()
url = 'https://graph.microsoft.com/v1.0/me/messages/' + msgId + '/move'
headers = {'Authorization': 'Bearer '+access_token() , 'Content-Type': 'application/json'}
p_load = {'destinationId': folderId()}
response = requests.post(url, json=p_load, headers=headers)
if response.status_code == 201:
parsed_response = json.loads(response.text)
return parsed_response['id']
return ''
def makeBatch(endpoint, method, ids, payload=[], operation=""):
batch = {}
batch['requests'] = []
count= 1
for i in ids:
blck = {'id': count, 'method': method, 'url': endpoint + i + operation}
batch['requests'].append(blck)
def listUnread(folderId='inbox'):
msgIds = []
#url = 'https://graph.microsoft.com/v1.0/me/mailFolders/'+ folderId + '/messages'
url = 'https://graph.microsoft.com/v1.0/me/mailFolders/'+ folderId + '/messages?$filter=isRead eq false&$select=id&$orderby=createdDateTime'
headers = {'Authorization': 'Bearer '+access_token() , 'Content-Type': 'application/json', 'outlook.body-content-type': 'text'}
#p_load = {'destinationId': folderId}
response = requests.get(url, headers=headers)
if response.status_code == 200:
parsed_response = json.loads(response.text)
messages = parsed_response['value']
msgIds = []
for msg in messages:
msgIds.append(msg['id'])
else:
print('Inbox unreachable - Listing failed')
exit(-1)
return msgIds
def processEmails(listIds):
global influxFlag
global eventMap
global eventTimeStamps
headers = {'Authorization': 'Bearer '+access_token() , 'Content-Type': 'application/json', 'outlook.body-content-type': 'text'}
for msId in listIds:
url = 'https://graph.microsoft.com/v1.0/me/messages/' + msId
response = requests.get(url, headers=headers)
if response.status_code == 200:
message = json.loads(response.text)
if influxFlag == 1:
insertInflux(message)
alrtMsg = message['subject']
if alrtMsg in eventMap.keys():
dtTime = datetime.strptime(message['createdDateTime'], '%Y-%m-%dT%H:%M:%SZ')
host = message['sender']['emailAddress']['address']
eventTimeStamps[alrtMsg][host]['Ids'].append(msId)
eventTimeStamps[alrtMsg][host]['timeStamp'].append(dtTime)
if influxFlag == 0:
processAlerts()
def processAlerts():
global eventMap
global eventTimeStamps
for k in eventTimeStamps.keys():
timePeriod = eventMap[k]['timePeriod']
timeDiff = timedelta(minutes=timePeriod[0], seconds=timePeriod[1])
threshold = eventMap[k]['threshold']
for host in eventTimeStamps[k].keys():
errorArray = eventTimeStamps[k][host]['timeStamp']
if len(errorArray) >= threshold:
while(len(eventTimeStamps[k][host]['Ids']) >= threshold):
errorArray = eventTimeStamps[k][host]['timeStamp']
idsArray = eventTimeStamps[k][host]['Ids']
deltaArr = [y-x for x, y in itertools.izip(errorArray, errorArray[1:])]
sumtime = timedelta(minutes=0, seconds=0)
for i in deltaArr[0:threshold-1]:
sumtime += i
if sumtime <= timeDiff:
genAlert(k, [str(dt) for dt in errorArray[0:threshold]])
for popi in range(0, threshold):
markAsRead(eventTimeStamps[k][host]['Ids'][0])
eventTimeStamps[k][host]['Ids'].pop(0)
eventTimeStamps[k][host]['timeStamp'].pop(0)
else:
markAsRead(eventTimeStamps[k][host]['Ids'][0])
eventTimeStamps[k][host]['Ids'].pop(0)
eventTimeStamps[k][host]['timeStamp'].pop(0)
def markAsRead(msgId):
headers = {'Authorization': 'Bearer '+access_token() , 'Content-Type': 'application/json', 'outlook.body-content-type': 'text'}
url = 'https://graph.microsoft.com/v1.0/me/messages/' + msgId
body = {'isRead': True}
response = requests.patch(url, json=body, headers=headers)
if response.status_code == 200:
return True
def insertInflux(host, error, timeStamp):
global influxConfig
timeStamp = int((timeStamp-datetime(1970, 1, 1)).total_seconds())
p_load = "pam_email,host=" + host + ",error=" + error + " count=1 " + str(timeStamp)
url = influxConfig['url'] + "db=" + influxConfig['db'] + "&precision=s"
if influxConfig['user'] != '':
url += '&u=' + influxConfig['user'] + "&p=" + influxConfig['pass']
resp = requests.post(url, p_load)
def genAlert(errorSubject, messageBody):
# if we want to send an alert to Bosun/Netcool via APIs
# for now we send email to Netcool/ops center for eg:
sendEmail(errorSubject, '[email protected]', messageBody)
def sendEmail(subject, to_address, message):
print("sendEmail")
headers = {'Authorization': 'Bearer '+access_token() , 'Content-Type': 'application/json', 'outlook.body-content-type': 'text'}
url = 'https://graph.microsoft.com/v1.0/me/sendMail/'
body = {
'message': {
"subject": subject,
"importance":"High",
"body":{
"contentType":"Text",
"content": str(message)
},
"toRecipients":[
{
"emailAddress":{
"address": to_address
}
}
]
}
}
print(body)
response = requests.post(url, json=body, headers=headers)
print(response)
if response.status_code == 202:
return True
def createeventTimeStamps():
hosts = ['linux23usa']
for k in eventMap.keys():
eventTimeStamps[k] = {}
for host in hosts:
eventTimeStamps[k][host] = {}
eventTimeStamps[k][host]['Ids'] = []
eventTimeStamps[k][host]['timeStamp'] = []
return
### Intiliazating variables and methods
createeventTimeStamps()
influxFlag = 0
########################
# folderId = 'AQMkA......CAQwAAAA='
token = ''
refresh_token = 'MCRpsnCfEOgb......*pvc4oRejbzXjIwsocd*o$'
if (len(refresh_token) == 0):
# auth_code = input()
auth_code = 'Mf6............cc4'
print "Using the auth_code to get the refresh token: " + str(auth_code)
get_set_refreshToken()
unread_msgs = []
moved_ids = []
unread_msgs = listUnread('inbox')
if len(unread_msgs) == 0:
unread_msgs = listUnread('inbox')
for msg in unread_msgs:
moveEmail(msg)
moved_ids = listUnread(folderId())
processEmails(moved_ids)