-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathget_app_assignment_events.py
152 lines (79 loc) · 3.47 KB
/
get_app_assignment_events.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
import requests
import json
import re
import sys
import csv
orgName = ""
apiKey = ""
api_token = "SSWS "+ apiKey
headers = {'Accept':'application/json','Content-Type':'application/json','Authorization':api_token}
def GetPaginatedResponse(url):
response = requests.request("GET", url, headers=headers)
returnResponseList = []
responseJSON = json.dumps(response.json())
responseList = json.loads(responseJSON)
returnResponseList = returnResponseList + responseList
if "errorCode" in responseJSON:
print "\nYou encountered following Error: \n"
print responseJSON
print "\n"
return "Error"
else:
headerLink= response.headers["Link"]
count = 1
while str(headerLink).find("rel=\"next\"") > -1:
linkItems = str(headerLink).split(",")
nextCursorLink = ""
for link in linkItems:
if str(link).find("rel=\"next\"") > -1:
nextCursorLink = str(link)
nextLink = str(nextCursorLink.split(";")[0]).strip()
nextLink = nextLink[1:]
nextLink = nextLink[:-1]
url = nextLink
print "\nCalling Paginated Url " + str(url) + " " + str(count) + " \n"
response = requests.request("GET", url, headers=headers)
responseJSON = json.dumps(response.json())
responseList = json.loads(responseJSON)
returnResponseList = returnResponseList + responseList
headerLink= response.headers["Link"]
count += 1
returnJSON = json.dumps(returnResponseList)
return returnResponseList
def GetEvents():
eventsUrl = "https://"+orgName+".com/api/v1/events?filter=published gt \"2017-01-27T00:00:00.000Z\""
responseJSON = GetPaginatedResponse(eventsUrl)
userFile = open("App-Access-Events.csv", "wb")
print "\nAll Events Retrieved. Generating CSV...\n"
writer = csv.writer(userFile)
writer.writerow(["Category", "Message", "RequestUri", "Admin", "AppUser", "App","Timestamp" "EVENT_JSON"])
if responseJSON != "Error":
count = 0
for event in responseJSON:
categories = []
categories = event[u"action"][u"categories"]
if "Application Assignment" in categories:
admin = ""
appUser = ""
app = ""
category = categories[categories.index("Application Assignment")]
message = event[u"action"][u"message"]
requestUri = event[u"action"][u"requestUri"]
actors = event[u"actors"]
targets = event[u"targets"]
timestamp = event[u"published"]
for actor in actors:
if actor[u"objectType"] == "User":
if u"login" in actor:
admin = actor[u"login"]
for target in targets:
if target[u"objectType"] == "User":
if u"login" in target:
appUser = target[u"login"]
if target[u"objectType"] == "AppInstance":
if u"displayName" in target:
app = target[u"displayName"]
if admin != "":
writer.writerow([category, message, requestUri, admin, appUser, app, timestamp, event])
if __name__ == "__main__":
GetEvents()