-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessrequests.py
337 lines (286 loc) · 13.9 KB
/
accessrequests.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
import os
import sys
import time
import datetime
import subprocess
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import tkinter as tk
import requests
import config
import json
class AccessRequests:
######################################
def __init__(self, parent):
mainframe = ttk.Frame(parent, width=1000, padding="12 12 12 12")
mainframe.grid(column=0, row=1, sticky=(N, W, E, S), columnspan=3)
accreqLabel = ttk.Label(parent,text='Access Requests',font=('Helvetica bold',32),anchor='center')
accreqLabel.grid(column=0, row=1, sticky=(N, W, E, S), columnspan=3)
# cols must correspond to fields returned from calls to AppGovDB.get()
self.cols=('Project','AppId','Safe','Environment','DateTimeSubmitted','RequestId')
self.unapprTree = None
self.unprovTree = None
self.provTree = None
#################################
# Unapproved access requests
#################################
self.unapprFrame = ttk.LabelFrame(parent, text='Unapproved', padding="3 3 12 12")
self.unapprFrame.grid(column=0, row=2, sticky=(N, W, E, S), columnspan=3)
self.unapprFrame.columnconfigure(0, weight=1)
self.unapprFrame.rowconfigure(0, weight=1)
self.buildUnapprovedTree(self.unapprFrame)
#################################
# Approved/Not Provisioned access requests
#################################
self.unprovFrame = ttk.LabelFrame(parent, text='Approved/Not Provisioned', padding="3 3 12 12")
self.unprovFrame.grid(column=0, row=3, sticky=(N, W, E, S), columnspan=3)
self.unprovFrame.columnconfigure(0, weight=1)
self.unprovFrame.rowconfigure(0, weight=1)
self.buildUnprovisionedTree(self.unprovFrame)
#################################
# Provisioned access requests
#################################
self.provFrame = ttk.LabelFrame(parent, text='Provisioned', padding="3 3 12 12")
self.provFrame.grid(column=0, row=4, sticky=(N, W, E, S), columnspan=3)
self.provFrame.columnconfigure(0, weight=1)
self.provFrame.rowconfigure(0, weight=1)
self.buildProvisionedTree(self.provFrame)
# Refresh button at bottom right of main window
ttk.Button(parent, text="Refresh", command=self.refresh, style='Cybr.TButton').grid(column=3, row=1, sticky=E)
# Exit button at bottom right of main window
ttk.Button(parent, text="Exit", command=self.exit, style='Cybr.TButton').grid(column=3, row=5, sticky=E)
parent.bind("<Return>", self.exit)
for child in parent.winfo_children():
child.grid_configure(padx=10, pady=10)
######################################
# Build Unapproved access request treeview
def buildUnapprovedTree(self,parent):
# get unapproved access request data
apiEndpoint = config.cybr["apiEndpoint"]
accReqJson = json.loads(requests.get(apiEndpoint + '/appgovdb?filter=unapproved').content)
unapprList = accReqJson["unapproved"]
unapprValues = []
if unapprList:
reqKeyList = tuple(unapprList[0]) # get list of keys in json record
for req in unapprList: # for each key in each unapproved access request
reqValues = []
for key in reqKeyList:
reqValues.append(req[key]) # append just values
unapprValues.append(reqValues)
# get max column widths for each column
maxwidths = [20,20,20,20,20,20]
if self.unapprTree is not None:
self.unapprTree.destroy()
self.unapprTree = ttk.Treeview(parent, height=5, columns=self.cols)
# self.unapprScrollbar = Scrollbar(self.unapprTree, orient=VERTICAL, command=self.unapprTree.yview)
# self.unapprTree.configure(yscrollcommand=self.unapprScrollbar.set)
# self.unapprScrollbar.grid(row=1, column=7, sticky='ns')
self.unapprTree['show'] = 'headings'
for c in range(len(self.cols)):
self.unapprTree.column(self.cols[c], width=maxwidths[c], anchor='center')
self.unapprTree.heading(self.cols[c],text=self.cols[c])
for req in unapprValues:
self.unapprTree.insert('','end',value=req)
#++++++++++++++++++++++++++++++++++++++
# setup popup menu for approvals
self.unapprTree.popup_menu = tk.Menu(self.unapprTree, tearoff=0)
self.unapprTree.popup_menu.add_command(label="Approve", command=self.approve)
self.unapprTree.popup_menu.add_separator()
self.unapprTree.popup_menu.add_command(label="Reject", command=self.rejectSubmitted)
def do_popup(event):
# display the popup menu
try:
self.unapprTree.popup_menu.selection = self.unapprTree.set(self.unapprTree.identify_row(event.y))
self.unapprTree.popup_menu.post(event.x_root, event.y_root)
finally:
# make sure to release the grab (Tk 8.0a1 only)
self.unapprTree.popup_menu.grab_release()
self.unapprTree.bind("<Button-2>", do_popup)
self.unapprTree.grid(column=0, row=0, sticky=(N, W, E, S),columnspan=3)
######################################
def approve(self):
try:
selectedReqId = self.unapprTree.popup_menu.selection['RequestId']
selectedProject = self.unapprTree.popup_menu.selection['Project']
selectedAppId = self.unapprTree.popup_menu.selection['AppId']
selectedSafe= self.unapprTree.popup_menu.selection['Safe']
except LookupError:
print("Lookup error in approve.")
return
# update approval status of selected access request
apiEndpoint = config.cybr["apiEndpoint"] + '/appgovdb?accReqId=' + selectedReqId + '&status=approved'
accStatusChangeResponse = requests.put(apiEndpoint).content
self.buildUnapprovedTree(self.unapprFrame) # rebuild unapproved tree w/o approved access request
self.buildUnprovisionedTree(self.unprovFrame) # rebuild unprovisioned tree w/ approved access request
messagebox.showinfo("Access Approved",
"Access request APPROVED\n\nIdentity: "
+ selectedProject + "/" + selectedAppId
+ "\n\nSafe: " + selectedSafe);
######################################
def rejectSubmitted(self):
try:
selectedReqId = self.unapprTree.popup_menu.selection['RequestId']
selectedProject = self.unapprTree.popup_menu.selection['Project']
selectedAppId = self.unapprTree.popup_menu.selection['AppId']
selectedSafe= self.unapprTree.popup_menu.selection['Safe']
except LookupError:
print("Lookup error in rejectSubmitted.")
return
# update rejected status of selected access request
apiEndpoint = config.cybr["apiEndpoint"] + '/appgovdb?accReqId=' + selectedReqId + '&status=rejected'
accStatusChangeResponse = requests.put(apiEndpoint).content
self.buildUnapprovedTree(self.unapprFrame) # rebuild unapproved tree w/o rejected access request
messagebox.showinfo("Access Rejected",
"Access request REJECTED\n\nIdentity: "
+ selectedProject + "/" + selectedAppId
+ "\n\nSafe: " + selectedSafe);
######################################
def popup(self, event):
try:
self.popup_menu.tk_popup(event.x_root, event.y_root, 0)
finally:
self.popup_menu.grab_release()
######################################
def buildUnprovisionedTree(self,parent):
# get access request data from db
apiEndpoint = config.cybr["apiEndpoint"]
accReqJson = json.loads(requests.get(apiEndpoint + '/appgovdb?filter=unprovisioned').content)
unprovList = accReqJson["unprovisioned"]
unprovValues = []
if unprovList:
reqKeyList = tuple(unprovList[0]) # get list of keys in json record
for req in unprovList: # for each key in each unprovisioned access request
reqValues = []
for key in reqKeyList:
reqValues.append(req[key]) # append just values
unprovValues.append(reqValues)
# get max column widths for each column
maxwidths = [20,20,20,20,20,20]
if self.unprovTree is not None:
self.unprovTree.destroy()
self.unprovTree = ttk.Treeview(parent, height=5, columns=self.cols)
self.unprovTree['show'] = 'headings'
self.unprovTree.column('#0',minwidth=0,width=0) # minimize width of 'ghost' column
for c in range(len(self.cols)):
self.unprovTree.column(self.cols[c], width=maxwidths[c], anchor='center')
self.unprovTree.heading(self.cols[c],text=self.cols[c])
for req in unprovValues:
self.unprovTree.insert('','end',value=req)
#++++++++++++++++++++++++++++++++++++++
# setup popup menu for provisioning
self.unprovTree.popup_menu = tk.Menu(self.unprovTree, tearoff=0)
self.unprovTree.popup_menu.add_command(label="Provision", command=self.provision)
self.unprovTree.popup_menu.add_separator()
self.unprovTree.popup_menu.add_command(label="Reject", command=self.rejectApproved)
def do_popup(event):
# display the popup menu
try:
self.unprovTree.popup_menu.selection = self.unprovTree.set(self.unprovTree.identify_row(event.y))
self.unprovTree.popup_menu.post(event.x_root, event.y_root)
finally:
# make sure to release the grab (Tk 8.0a1 only)
self.unprovTree.popup_menu.grab_release()
self.unprovTree.bind("<Button-2>", do_popup)
self.unprovTree.grid(column=0, row=1, sticky=(N, W, E, S),columnspan=3)
######################################
def provision(self):
try:
selectedReqId = self.unprovTree.popup_menu.selection['RequestId']
selectedProject = self.unprovTree.popup_menu.selection['Project']
selectedAppId = self.unprovTree.popup_menu.selection['AppId']
selectedSafe= self.unprovTree.popup_menu.selection['Safe']
except LookupError:
print("Lookup error in provision.")
return
# make REST call to provision access request from DB
r = requests.post(url = config.cybr["apiEndpoint"] + "/provision?accReqId=" + selectedReqId, data = "")
self.buildUnprovisionedTree(self.unprovFrame) # rebuild unprovisioned tree w/o approved access request
self.buildProvisionedTree(self.provFrame) # rebuild provisioned tree w/ provisioned access request
messagebox.showinfo("Access Provisioned",
"Access request GRANTED\n\nIdentity: "
+ selectedProject + "/" + selectedAppId
+ "\n\nSafe: " + selectedSafe);
######################################
def rejectApproved(self):
try:
selectedReqId = self.unprovTree.popup_menu.selection['RequestId']
selectedProject = self.unprovTree.popup_menu.selection['Project']
selectedAppId = self.unprovTree.popup_menu.selection['AppId']
selectedSafe= self.unprovTree.popup_menu.selection['Safe']
except LookupError:
print("Lookup error in rejectApproved.")
return
# update rejected status of selected access request
apiEndpoint = config.cybr["apiEndpoint"] + '/appgovdb?accReqId=' + selectedReqId + '&status=rejected'
accStatusChangeResponse = requests.put(apiEndpoint).content
self.buildUnprovisionedTree(self.unprovFrame) # rebuild unprovisioned tree w/o rejected access request
messagebox.showinfo("Access Rejected",
"Access request REJECTED\n\nIdentity: "
+ selectedProject + "/" + selectedAppId
+ "\n\nSafe: " + selectedSafe);
######################################
def buildProvisionedTree(self,parent):
# get access request data from db
apiEndpoint = config.cybr["apiEndpoint"]
accReqJson = json.loads(requests.get(apiEndpoint + '/appgovdb?filter=provisioned').content)
provList = accReqJson["provisioned"]
provValues = []
if provList:
reqKeyList = tuple(provList[0]) # get list of keys in json record
for req in provList: # for each key in each provisioned access request
reqValues = []
for key in reqKeyList:
reqValues.append(req[key]) # append just values
provValues.append(reqValues)
# get max column widths for each column
maxwidths = [20,20,20,20,20,20]
if self.provTree is not None:
self.provTree.destroy()
self.provTree = ttk.Treeview(parent, height=5, columns=self.cols)
self.provTree['show'] = 'headings'
self.provTree.column('#0',minwidth=0,width=0) # minimize width of 'ghost' column
for c in range(len(self.cols)):
self.provTree.column(self.cols[c], width=maxwidths[c], anchor='center')
self.provTree.heading(self.cols[c],text=self.cols[c])
for req in provValues:
self.provTree.insert('','end',value=req)
#++++++++++++++++++++++++++++++++++++++
# setup popup menu for revoking access
self.provTree.popup_menu = tk.Menu(self.provTree, tearoff=0)
self.provTree.popup_menu.add_command(label="Revoke Access", command=self.revokeAccess)
def do_popup(event):
# display the popup menu
try:
self.provTree.popup_menu.selection = self.provTree.set(self.provTree.identify_row(event.y))
self.provTree.popup_menu.post(event.x_root, event.y_root)
finally:
# make sure to release the grab (Tk 8.0a1 only)
self.provTree.popup_menu.grab_release()
self.provTree.bind("<Button-2>", do_popup)
self.provTree.grid(column=0, row=1, sticky=(N, W, E, S),columnspan=3)
######################################
def revokeAccess(self):
try:
selectedReqId= self.provTree.popup_menu.selection['RequestId']
selectedProject = self.provTree.popup_menu.selection['Project']
selectedAppId = self.provTree.popup_menu.selection['AppId']
selectedSafe= self.provTree.popup_menu.selection['Safe']
except LookupError:
print("Lookup error in revokeAccess.")
return
# revoke access request
r = requests.delete(url = config.cybr["apiEndpoint"] + "/provision?accReqId=" + selectedReqId, data = "")
self.buildProvisionedTree(self.provFrame) # rebuild provisioned tree w/o revoked access request
messagebox.showinfo("Access Revoked",
"Access request REVOKED\n\nIdentity: "
+ selectedProject + "/" + selectedAppId
+ "\n\nSafe: " + selectedSafe);
######################################
def refresh(self, *args):
self.buildUnapprovedTree(self.unapprFrame)
self.buildUnprovisionedTree(self.unprovFrame)
self.buildProvisionedTree(self.provFrame)
######################################
def exit(self, *args):
sys.exit(0)