def addESAgentWorker(sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) requestStr = request.data agentConfig = json.loads(requestStr) agentName = agentConfig["agentName"] bad_chars = invalid_chars.intersection(agentName) if bad_chars: msg = "Invalid character in agentName: {}" util.debugPrint(msg.format(bad_chars)) abort(400) key = agentConfig["key"] bad_chars = invalid_chars.intersection(key) if bad_chars: msg = "Invalid character in key: {}" util.debugPrint(msg.format(bad_chars)) abort(400) Config.addESAgent(agentName, key) agents = Config.getESAgents() retval = {"esAgents": agents} retval[STATUS] = 'OK' return jsonify(retval) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def restartService(service, sessionId): """ Restart specified service URL Path: sessionId the session Id of the login in session. URL Args: None Request Body: A String of the name of the service """ try: util.debugPrint("restartService " + str(service)) if not authentication.checkSessionId(sessionId, ADMIN): abort(403) util.debugPrint("passed authentication") if restartThisService(service): return jsonify({"status": "OK"}) else: return jsonify({ "status": "NOK", "ErrorMessage": "Unknown service" }) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def setSystemConfigWorker(sessionId): try: util.debugPrint("setSystemConfig: " + sessionId) if not authentication.checkSessionId(sessionId, ADMIN): abort(403) util.debugPrint("passed authentication") requestStr = request.data systemConfig = json.loads(requestStr) (statusCode, message) = Config.verifySystemConfig(systemConfig) if not statusCode: util.debugPrint("did not verify sys config") return jsonify({"status": "NOK", "ErrorMessage": message}) util.debugPrint("setSystemConfig " + json.dumps( systemConfig, indent=4, )) if Config.setSystemConfig(systemConfig): return jsonify({"status": "OK"}) else: return jsonify({"status": "NOK", "ErrorMessage": "Unknown"}) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def clearlogs(sessionId): if not authentication.checkSessionId(sessionId, ADMIN): abort(403) try: for f in [ "/var/log/monitoring.log", "/var/log/federation.log", "/var/log/streaming.log", "/var/log/occupancy.log", "/var/log/flask/federation.log", "/var/log/flask/spectrumbrowser.log", "/var/log/flask/spectrumdb.log" ]: if os.path.exists(f): os.remove(f) serviceNames = [ "spectrumbrowser", "streaming", "occupancy", "monitoring", "federation", "spectrumdb" ] for service in serviceNames: restartThisService(service) return jsonify({"status": "OK"}) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getCaptureEvents(sensorId, startDate, dayCount, sessionId): """ get the capture events for a given sensor within the specified date range. """ try: if not authentication.checkSessionId(sessionId, USER): util.debugPrint("getCaptureEvents: failed authentication") abort(403) try: sdate = int(startDate) dcount = int(dayCount) except ValueError: abort(400) if sdate < 0 or dcount < 0: abort(400) elif dcount == 0: abort(400) return jsonify(CaptureDb.getEvents(sensorId, sdate, dcount)) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def deleteCaptureEvents(sensorId, startDate, sessionId): """ Delete the events from the capture db. Send a message to the sensor to do the same. """ try: if not authentication.checkSessionId(sessionId, ADMIN): util.debugPrint("deleteCaptureEvents: failed authentication") abort(403) sdate = int(startDate) if sdate < 0: util.debugPrint("deleteCaptureEvents: illegal param") abort(400) else: CaptureDb.deleteCaptureDb(sensorId, sdate) global memCache if memCache is None: memCache = MemCache() command = json.dumps({ "sensorId": sensorId, "timestamp": sdate, "command": "garbage_collect" }) sendCommandToSensor(sensorId, command) return jsonify({STATUS: "OK"}) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def verifySessionToken(sessionId): """ Check the session token. Return TRUE if session Token is good and false otherwise. URL Path: - sessionId: the session ID to check. HTTP Return code: - 200 OK returns a document {status:OK} or {status: NOK} depending upon whether the session token verified or not. """ try: if authentication.checkSessionId(sessionId, ADMIN): return jsonify({"status": "OK"}) else: return jsonify({"status": "NOK"}) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def runForensics(sensorId, algorithm, timestamp, sessionId): """ Run forensics at the sensor. This just relays the command to the sensor. The sensor will post back after the processing is done. timestamp -- the timestamp of the capture. algorithm -- the algortithm to appy (from the toolbox that lives on the sensor) sessionId -- the login session id. """ try: if not authentication.checkSessionId(sessionId, USER): util.debugPrint("runForensics - request body not found") abort(403) command = { "sensorId": sensorId, "timestamp": int(timestamp), "algorithm": algorithm, "command": "analyze" } sendCommandToSensor(sensorId, json.dumps(command)) return jsonify({STATUS: OK}) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def testArmSensor(sensorId, sessionId): """ URL Path: sessionId -- the session ID of the login session. sensorId -- the sensorId URL Args: None Request Body: - agentName: Name of the agent to arm/disarm sensor. - key : Key (password) of the agent to arm/disarm the sensor. HTTP Return Codes: - 200 OK: invocation was successful. - 403 Forbidden: authentication failure - 400 Bad request: Sensor is not a streaming sensor. Example Invocation: :: params = {} params["agentName"] = "NIST_ESC" params["key"] = "ESC_PASS" url = "https://{host}:8443/admin/armSensor/{self.sensorId}" r = requests.post(url, data=json.dumps(params), verify=False) """ try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) sensorConfig = SensorDb.getSensorObj(sensorId) if sensorConfig is None: abort(404) if not sensorConfig.isStreamingEnabled(): abort(400) persistent = request.args.get("persistent") if persistent is None: persistent = "false" DataStreamSharedState.sendCommandToSensor( sensorId, json.dumps({ "sensorId": sensorId, "command": "arm", "persistent": persistent })) return jsonify({STATUS: OK}) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def recomputeOccupanciesWorker(sensorId, sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): return make_response("Session not found", 403) return jsonify(RecomputeOccupancies.recomputeOccupancies(sensorId)) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def garbageCollectWorker(sensorId, sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): return make_response("Session not found", 403) return jsonify(GarbageCollect.runGarbageCollector(sensorId)) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def unfreezeRequestWorker(sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): return make_response("Session not found", 403) return jsonify(SessionLock.freezeRelease(sessionId)) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getSessionsWorker(sessionId): try: if not authentication.checkSessionId( sessionId, ADMIN, updateSessionTimer=False): return make_response("Session not found", 403) return jsonify(SessionLock.getSessions()) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def unlockAccountWorker(emailAddress, sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) util.debugPrint("unlockAccount") return jsonify(AccountsManagement.unlockAccount(emailAddress)) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getResourceData(ws): """ Handle resource data streaming requests from the web browser. Token is of the form <sessionID> => len(parts)==1 """ try: util.debugPrint("ResourceDataStreaming:getResourceData") token = ws.receive() if token is None: #or len(parts) < 2: ws.close() return sessionId = token if not authentication.checkSessionId(sessionId, "admin"): ws.close() util.debugPrint( "ResourceDataStreamng:failed to authenticate: user != " + sessionId) return memCache = memcache.Client(['127.0.0.1:11211'], debug=0) keys = MemCacheKeys.RESOURCEKEYS resourceData = {} secondsPerFrame = 1 while True: for resource in keys: key = str(resource).encode("UTF-8") value = memCache.get(key) if value is not None: resourceData[str(key)] = float(value) else: util.errorPrint("Unrecognized resource key " + key) client = MongoClient(getDbHost(), 27017) collection = client.systemResources.dbResources dbResources = collection.find_one({}) if dbResources is not None and dbResources["Disk"] is not None: resourceData["Disk"] = float(dbResources["Disk"]) util.debugPrint("resource Data = " + str(json.dumps(resourceData, indent=4))) ws.send(json.dumps(resourceData)) sleepTime = secondsPerFrame gevent.sleep(sleepTime) except: traceback.print_exc() util.debugPrint("Error writing to resource websocket") util.logStackTrace(traceback) ws.close()
def removePeerWorker(host, port, sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) Config.removePeer(host, int(port)) peers = Config.getPeers() retval = {"peers": peers} return jsonify(retval) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getESAgentsWorker(sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) agents = Config.getESAgents() retval = {"esAgents": agents} retval[STATUS] = 'OK' return jsonify(retval) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def createAccountWorker(sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) util.debugPrint("createAccount") requestStr = request.data accountData = json.loads(requestStr) return jsonify(AccountsManagement.createAccount(accountData)) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getInboundPeersWorker(sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) peerKeys = Config.getInboundPeers() retval = {"inboundPeers": peerKeys} retval[STATUS] = OK return jsonify(retval) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getFreqBandsWorker(sessionId): try: if not Config.isConfigured(): util.debugPrint("Please configure system") return make_response("Please configure system", 500) if not authentication.checkSessionId(sessionId, ADMIN): return make_response("Session not found.", 403) return jsonify(SensorDb.getFreqBands()) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getSystemConfigWorker(sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) systemConfig = Config.getSystemConfig() if systemConfig is None: config = Config.getDefaultConfig() return jsonify(config) else: return jsonify(systemConfig) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def addPeerWorker(host, port, protocol, sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) # TODO -- parameter checking. Config.addPeer(protocol, host, int(port)) peers = Config.getPeers() retval = {"peers": peers} retval[STATUS] = OK return jsonify(retval) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def setScreenConfigWorker(sessionId): try: util.debugPrint("setScreenConfig: " + sessionId) if not authentication.checkSessionId(sessionId, ADMIN): abort(403) requestStr = request.data screenConfig = json.loads(requestStr) if Config.setScreenConfig(screenConfig): return jsonify({"status": "OK"}) else: return jsonify({"status": "NOK", "ErrorMessage": "Unknown"}) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def updateSensorWorker(sessionId): try: util.debugPrint("updateSensor") if not Config.isConfigured(): util.debugPrint("Please configure system") return make_response("Please configure system", 500) if not authentication.checkSessionId(sessionId, ADMIN): return make_response("Session not found.", 403) requestStr = request.data sensorConfig = json.loads(requestStr) return jsonify(SensorDb.updateSensor(sensorConfig)) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getSensorInfoWorker(sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): return make_response("Session not found", 403) lastMessageFlagStr = request.args.get("getFirstLastMessages") if lastMessageFlagStr is not None and lastMessageFlagStr == "true": lastMessageFlag = True else: lastMessageFlag = False response = SensorDb.getSensors(getMessageDates=lastMessageFlag) return jsonify(response) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getSystemMessagesWorker(sensorId, sessionId): try: if not Config.isConfigured(): util.debugPrint("Please configure system") return make_response("Please configure system", 500) if not authentication.checkSessionId(sessionId, ADMIN): return make_response("Session not found.", 403) genzip = GenerateZipFileForDownload zipfile = genzip.generateSysMessagesZipFileForDownload( sensorId, sessionId) return jsonify(zipfile) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def deleteAllCaptureEvents(sensorId, sessionId): """ Delete all the the capture events from the capture db. Send a message to the sensor to do the same. """ try: if not authentication.checkSessionId(sessionId, ADMIN): util.debugPrint("deleteCaptureEvents: failed authentication") abort(403) else: CaptureDb.deleteCaptureDb(sensorId) return jsonify({STATUS: "OK"}) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getUserAccountsWorker(sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) util.debugPrint("getUserAccounts") userAccounts = AccountsManagement.getUserAccounts() retval = { "userAccounts": userAccounts, STATUS: "OK", "statusMessage": "" } return jsonify(retval) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def addInboundPeerWorker(sessionId): try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) requestStr = request.data peerConfig = json.loads(requestStr) util.debugPrint("peerConfig " + json.dumps(peerConfig, indent=4)) Config.addInboundPeer(peerConfig) peers = Config.getInboundPeers() retval = {"inboundPeers": peers} retval[STATUS] = "OK" return jsonify(retval) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise
def getDebugFlags(sessionId): """ Get the debug flags for this server instance. Note - debug flag settings are not persistent. """ try: if not authentication.checkSessionId(sessionId, ADMIN): abort(403) debugFlags = DebugFlags.getDebugFlags() return jsonify({"status": "OK", "debugFlags": debugFlags}) except: print "Unexpected error:", sys.exc_info()[0] print sys.exc_info() traceback.print_exc() util.logStackTrace(sys.exc_info()) raise