forked from z1pti3/jimi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjimi_web.py
More file actions
352 lines (328 loc) · 13.6 KB
/
jimi_web.py
File metadata and controls
352 lines (328 loc) · 13.6 KB
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
from flask import Flask, request, render_template, make_response, redirect, send_file, flash
from werkzeug.utils import secure_filename
import _thread
import time
import json
import requests
import uuid
from pathlib import Path
import re
import subprocess
import os
from operator import itemgetter
# Setup and define API ( required before other modules )
from core import api, settings
apiSettings = settings.config["api"]["web"]
api.createServer("jimi_web",template_folder=str(Path("react-web","build")),static_folder=str(Path("react-web","build","static")))
import jimi
# Other pages
from web import modelEditor, conductEditor, codify
# Add plugin blueprints
pluginPages = []
plugins = os.listdir("plugins")
for plugin in plugins:
if os.path.isfile(Path("plugins/{0}/web/{0}.py".format(plugin))):
mod = __import__("plugins.{0}.web.{0}".format(plugin), fromlist=["pluginPages"])
jimi.api.webServer.register_blueprint(mod.pluginPages,url_prefix='/plugin')
hidden = False
try:
hidden = mod.pluginPagesHidden
except:
pass
if not hidden:
pluginPages.append(plugin)
# Installing
if "webui" not in jimi.db.list_collection_names():
if jimi.logging.debugEnabled:
jimi.logging.debug("DB Collection webui Not Found : Creating...")
jimi.model.registerModel("flowData","_flowData","_document","core.models.webui")
@jimi.api.webServer.errorhandler(404)
def notFound(e):
return render_template("index.html")
@jimi.api.webServer.route("/")
def indexPage():
return render_template("index.html")
@jimi.api.webServer.route("/debugFlow/")
def debugFlowPage():
return render_template("debugFlowEditor.html",CSRF=jimi.api.g.sessionData["CSRF"])
# Should be migrated into plugins.py
@jimi.api.webServer.route(jimi.api.base+"plugins/")
def loadPluginPages():
userPlugins = []
userModels = jimi.plugin._plugin().getAsClass(sessionData=jimi.api.g.sessionData,query={ "name" : { "$in" : pluginPages } },sort=[("name", 1)])
for userModel in userModels:
if userModel.name in pluginPages:
userPlugins.append({ "id" : userModel._id, "name" : userModel.name})
return { "results" : userPlugins }, 200
# Should be migrated into models/conduct.py
@jimi.api.webServer.route(jimi.api.base+"conducts/")
def loadConducts():
return { "results" : jimi.conduct._conduct().query(sessionData=jimi.api.g.sessionData,query={ "name" : { "$exists" : True } },sort=[( "name", 1 )])["results"] }, 200
# Should be migrated into workers.py
@jimi.api.webServer.route(jimi.api.base+"/workers/", methods=["GET"])
@jimi.auth.adminEndpoint
def workerPage():
apiEndpoint = "workers/"
servers = jimi.cluster.getAll()
results = []
for url in servers:
response = jimi.helpers.apiCall("GET",apiEndpoint,token=jimi.api.g.sessionToken,overrideURL=url)
responseJson = json.loads(response.text)
if responseJson:
for job in responseJson["results"]:
job["server"] = url
results.append(job)
return { "results" : results }, 200
# Should be migrated into admin.py or cache.py
@jimi.api.webServer.route(jimi.api.base+"/clearCache/", methods=["GET"])
@jimi.auth.adminEndpoint
def clearCachePage():
results = []
apiEndpoint = "admin/clearCache/"
servers = jimi.cluster.getAll()
for url in servers:
response = jimi.helpers.apiCall("GET",apiEndpoint,token=jimi.api.g.sessionToken,overrideURL=url)
if response.status_code != 200:
results.append({ "server" : url, "result" : False })
else:
results.append({ "server" : url, "result" : True })
return { "results" : results }, 200
# Should be migrated into admin.py
@jimi.api.webServer.route(jimi.api.base+"/clearStartChecks/", methods=["GET"])
@jimi.auth.adminEndpoint
def clearStartChecksPage():
apiEndpoint = "admin/clearStartChecks/"
url = jimi.cluster.getMaster()
response = jimi.helpers.apiCall("GET",apiEndpoint,token=jimi.api.g.sessionToken,overrideURL=url)
if response.status_code != 200:
return { "server" : url, "result" : False }, 403
else:
return { "server" : url, "result" : True }, 200
@jimi.api.webServer.route("/login")
def loginPage():
return render_template("index.html")
@jimi.api.webServer.route("/conduct/PropertyTypes/", methods=["GET"])
def getConductPropertyTypes():
result = []
models = jimi.model._model().query(jimi.api.g.sessionData,query={
"$and" : [
{
"$or" : [
{ "name" : "action" },
{ "classType" : "_action" },
{ "classType" : "_trigger" },
{ "name" : "trigger" }
]
},
{
"$or" : [
{ "hidden" : False },
{ "hidden" : { "$exists" : False } }
]
}
]
},sort=[( "name", 1 )])["results"]
for modelItem in models:
result.append({ "_id" : modelItem["_id"], "name" : modelItem["name"] })
return { "results" : result }, 200
@jimi.api.webServer.route("/conduct/<conductID>/flowProperties/<flowID>/", methods=["GET"])
def getConductFlowProperties(conductID,flowID):
conductObj = jimi.conduct._conduct().query(jimi.api.g.sessionData,id=conductID)["results"]
if len(conductObj) == 1:
conductObj = conductObj[0]
else:
return {}, 404
flow = [ x for x in conductObj["flow"] if x["flowID"] == flowID]
if len(flow) == 1:
flow = flow[0]
formData = None
manifest = None
if "type" in flow:
if flow["type"] == "trigger":
triggerObj = jimi.trigger._trigger().query(jimi.api.g.sessionData,id=flow["triggerID"])["results"]
if len(triggerObj) == 1:
triggerObj = triggerObj[0]
else:
return {}, 404
_class = jimi.model._model().getAsClass(jimi.api.g.sessionData,id=triggerObj["classID"])
if len(_class) == 1:
_class = _class[0].classObject()
else:
return {}, 404
triggerObj = _class().getAsClass(jimi.api.g.sessionData,id=triggerObj["_id"])
if len(triggerObj) == 1:
triggerObj = triggerObj[0]
else:
return {},404
if "_properties" in dir(triggerObj):
formData = triggerObj._properties().generate(triggerObj)
else:
formData = jimi.webui._properties().generate(triggerObj)
manifest = triggerObj.manifest__
elif flow["type"] == "action":
actionObj = jimi.action._action().query(jimi.api.g.sessionData,id=flow["actionID"])["results"]
if len(actionObj) == 1:
actionObj = actionObj[0]
else:
return {},404
_class = jimi.model._model().getAsClass(jimi.api.g.sessionData,id=actionObj["classID"])
if len(_class) == 1:
_class = _class[0].classObject()
actionObj = _class().getAsClass(jimi.api.g.sessionData,id=actionObj["_id"])
if len(actionObj) == 1:
actionObj = actionObj[0]
else:
return {}, 404
if "_properties" in dir(actionObj):
formData = actionObj._properties().generate(actionObj)
else:
formData = jimi.webui._properties().generate(actionObj)
manifest = actionObj.manifest__
return { "formData" : formData, "manifest" : manifest }, 200
@jimi.api.webServer.route("/conduct/<conductID>/flow/<flowID>/", methods=["GET"])
def getConductFlow(conductID,flowID):
conductObj = jimi.conduct._conduct().query(jimi.api.g.sessionData,id=conductID)["results"]
if len(conductObj) == 1:
conductObj = conductObj[0]
else:
return {},404
flow = [ x for x in conductObj["flow"] if x["flowID"] == flowID]
if len(flow) == 1:
flow = flow[0]
if "type" in flow:
flowType = flow["type"]
if "{0}{1}".format(flowType,"ID") in flow:
result = { "type" : flowType, "{0}{1}".format(flowType,"ID") : flow["{0}{1}".format(flowType,"ID")]}
return { "result" : result }, 200
return { }, 404
@jimi.api.webServer.route("/conduct/<conductID>/forceTrigger/<flowID>/", methods=["POST"])
def forceTrigger(conductID,flowID):
conductObj = jimi.conduct._conduct().query(jimi.api.g.sessionData,id=conductID)["results"]
if len(conductObj) == 1:
conductObj = conductObj[0]
else:
return {}, 404
flow = [ x for x in conductObj["flow"] if x["flowID"] == flowID]
flow = flow[0]
data = json.loads(api.request.data)
apiEndpoint = "scheduler/{0}/".format(flow["triggerID"])
if "_id" in jimi.api.g.sessionData:
jimi.audit._audit().add("trigger","force",{ "_id" : jimi.api.g.sessionData["_id"], "user" : jimi.api.g.sessionData["user"], "conductID" : conductID, "flowID" : flowID })
else:
jimi.audit._audit().add("trigger","force",{ "user" : "system", "conductID" : conductID, "flowID" : flowID })
host,port = jimi.cluster.getMaster()
url="http://{0}:{1}".format(host,port)
jimi.helpers.apiCall("POST",apiEndpoint,{ "action" : "trigger", "events" : data["events"] },token=jimi.api.g.sessionToken,overrideURL=url)
return { }, 200
@jimi.api.webServer.route("/conduct/<conductID>/flowlogic/<flowID>/<nextflowID>/", methods=["GET"])
def getConductFlowLogic(conductID,flowID,nextflowID):
conductObj = jimi.conduct._conduct().query(jimi.api.g.sessionData,id=conductID)["results"]
if len(conductObj) == 1:
conductObj = conductObj[0]
else:
return {}, 404
flow = [ x for x in conductObj["flow"] if x["flowID"] == flowID]
if len(flow) == 1:
flow = flow[0]
if "next" in flow:
for nextFlow in flow["next"]:
if type(nextFlow) is str:
if nextflowID == nextFlow:
return {"result" : {"logic" : True}}, 200
elif type(nextFlow) is dict:
if "order" not in nextFlow:
nextFlow["order"] = 0
if nextflowID == nextFlow["flowID"]:
return {"result" : {"logic" : nextFlow["logic"], "order" : nextFlow["order"]}}, 200
return { }, 404
@jimi.api.webServer.route("/conduct/<conductID>/flowlogic/<flowID>/<nextflowID>/", methods=["POST"])
def setConductFlowLogic(conductID,flowID,nextflowID):
conductObj = jimi.conduct._conduct().getAsClass(jimi.api.g.sessionData,id=conductID)
if len(conductObj) == 1:
conductObj = conductObj[0]
else:
return {},404
access, accessIDs, adminBypass = jimi.db.ACLAccess(jimi.api.g.sessionData,conductObj.acl,"write")
if access:
flow = [ x for x in conductObj.flow if x["flowID"] == flowID]
data = json.loads(jimi.api.request.data)
if len(flow) == 1:
flow = flow[0]
if "next" in flow:
found = False
for key, nextFlow in enumerate(flow["next"]):
if type(nextFlow) is str:
if nextFlow == nextflowID:
nextFlow = {"flowID" : nextflowID}
found = True
elif type(nextFlow) is dict:
if nextFlow["flowID"] == nextflowID:
found = True
if found:
if "true" == data["logic"].lower():
flow["next"][key] = {"flowID" : nextFlow["flowID"], "logic" : True, "order" : 0}
elif "false" == data["logic"].lower():
flow["next"][key] = {"flowID" : nextFlow["flowID"], "logic" : False, "order" : 0}
elif data["logic"].startswith("if"):
flow["next"][key] = {"flowID" : nextFlow["flowID"], "logic" : data["logic"], "order" : 0}
else:
try:
flow["next"][key] = {"flowID" : nextFlow["flowID"], "logic" : int(data["logic"]), "order" : 0}
except ValueError:
return { }, 403
flow["next"][key]["order"] = int(data["order"])
# Sorting the list so we dont need to do this at flow runtime
try:
flow["next"] = sorted(flow["next"], key=itemgetter("order"), reverse=False)
except KeyError:
for value in flow["next"]:
if "order" not in value:
value["order"] = 0
flow["next"] = sorted(flow["next"], key=itemgetter("order"), reverse=False)
if "_id" in api.g.sessionData:
jimi.audit._audit().add("flowLogic","update",{ "_id" : jimi.api.g.sessionData["_id"], "user" : jimi.api.g.sessionData["user"], "conductID" : conductID, "flowID" : flowID, "nextflowID" : nextflowID, "logic" : data["logic"] })
else:
jimi.audit._audit().add("flowLogic","update",{ "user" : "system", "conductID" : conductID, "flowID" : flowID , "nextflowID" : nextflowID, "logic" : data["logic"] })
conductObj.update(["flow"],sessionData=jimi.api.g.sessionData)
return { }, 200
else:
return {},403
return { }, 404
@jimi.api.webServer.route("/cleanup/", methods=["GET","DELETE"])
@jimi.auth.adminEndpoint
def cleanupPage():
actions = jimi.action._action().query(jimi.api.g.sessionData,query={ "name" : { "$nin" : ["resetTrigger","failedTriggers"] } },fields=["_id","name","lastUpdateTime"])["results"]
triggers = jimi.trigger._trigger().query(jimi.api.g.sessionData,query={ "name" : { "$nin" : ["resetTrigger","failedTriggers"] } },fields=["_id","name","lastUpdateTime"])["results"]
actionids = [ x["_id"] for x in actions ]
triggerids = [ x["_id"] for x in triggers ]
conducts = jimi.conduct._conduct().query(jimi.api.g.sessionData,query={ "$or" : [ { "flow.triggerID" : { "$in" : triggerids } }, { "flow.actionID" : { "$in" : actionids } } ] },fields=["_id","name","flow"])["results"]
for c in conducts:
for flow in c["flow"]:
if "actionID" in flow:
if flow["actionID"] in actionids:
actionids.remove(flow["actionID"])
if "triggerID" in flow:
if flow["triggerID"] in triggerids:
triggerids.remove(flow["triggerID"])
unusedActionObjects = []
unusedActionObjectsIds = []
for actionid in actionids:
a = [ x for x in actions if x["_id"] == actionid ]
if a:
unusedActionObjects.append({ "name" : a[0]["name"], "_id" : a[0]["_id"] })
unusedActionObjectsIds.append(jimi.db.ObjectId(a[0]["_id"]))
unusedTriggerObjects = []
unusedTriggerObjectsIds = []
for triggerid in triggerids:
t= [ x for x in triggers if x["_id"] == triggerid ]
if t:
unusedTriggerObjects.append({ "name" : t[0]["name"], "_id" : t[0]["_id"] })
unusedTriggerObjectsIds.append(jimi.db.ObjectId(t[0]["_id"]))
if request.method == "DELETE":
jimi.action._action().api_delete(query={ "_id" : { "$in" : unusedActionObjectsIds } })
jimi.trigger._trigger().api_delete(query={ "_id" : { "$in" : unusedTriggerObjectsIds } })
return { },200
return render_template("cleanupObjects.html", unusedActionObjects=unusedActionObjects, unusedTriggerObjects=unusedTriggerObjects, CSRF=api.g.sessionData["CSRF"])
api.startServer(debug=True, use_reloader=False, host=apiSettings["bind"], port=apiSettings["port"], threaded=True)
while True:
time.sleep(1)