def api_add_agent(): if not utils.verify_digest(flask.request.data, flask.request.headers["Authorization"]): app.logger.warning("Failed HMAC Auth") flask.abort(401) payload = flask.request.get_json() if utils.is_dict_empty(["id", "name", "location", "secret"], payload): app.logger.warning("Mandatory fields empty") flask.abort(404) if len(payload["id"]) > 50: app.logger.warning("ID too long (> 50)") flask.abort(404) if len(payload["secret"]) > 256: app.logger.warning("Secret too long (> 256)") flask.abort(404) if db.get_agent(payload["id"].strip()) is not None: #TODO: replace with API error instead app.logger.warning("Attempted to add agent with existing id") flask.abort(404) db.add_agent(payload["id"].strip(), payload["name"], payload["location"], payload["secret"], int(time.time()), payload.get("description")) return "", 200
def agent(agent_id): db_agent = db.get_agent(agent_id) db_agent_reports = db.get_reports_by_agent(agent_id) if db_agent is None: flask.abort(404) return flask.render_template("agent.html", agent=db_agent, agent_reports=db_agent_reports)
def get_agent_image(agent_id: str): db_agent = db.get_agent(agent_id.strip()) if db_agent is None: flask.abort(404) file_name = werkzeug.utils.secure_filename( db_agent["id"]) + "." + app.config["IMG_TYPE"] if os.path.isfile(os.path.join(app.config["IMG_AGENT"], file_name)): return flask.send_from_directory(app.config["IMG_AGENT"], file_name) else: return flask.send_from_directory(app.static_folder, "images/agent-img-404.png")
def api_add_report(): if not utils.verify_digest(flask.request.data, flask.request.headers["Authorization"]): app.logger.warning("Failed HMAC Auth") flask.abort(401) payload = flask.request.get_json() agent_id = payload["agent"].strip() if utils.is_dict_empty(["agent"], payload): app.logger.warning("Mandatory fields empty") flask.abort(404) db_agent = db.get_agent(agent_id) if agent is None: #TODO: replace with API error instead app.logger.warning("Attempted to upload report for nonexistent agent") flask.abort(404) #TODO: replace with proper short unique id generation report_id = utils.generate_short_uuid( str(random.random() * 1000) + str(payload["time"]) + agent_id, 8) db.add_report(report_id, payload["time"], db_agent["location"], agent_id) for report_image in payload["images"]: image_id = report_id + "_" + report_image["location"] try: images.save_image(image_id, report_image["image"], images.ImageType.REPORT) db.add_report_image(image_id, report_image["location"], 0, report_id) #thumbnail image for smaller version for dashboard images.save_image(image_id + ".thumb", report_image["image"], images.ImageType.REPORT_THUMB) except (KeyError, IOError): flask.abort(500) return "", 200
def api_add_agent_image(): if not utils.verify_digest(flask.request.data, flask.request.headers["Authorization"]): app.logger.warning("Failed HMAC Auth") flask.abort(401) payload = flask.request.get_json() agent_id = payload["id"].strip() if utils.is_dict_empty(["id", "image"], payload): app.logger.warning("Mandatory fields empty") flask.abort(404) if db.get_agent(agent_id) is None: #TODO: replace with API error instead app.logger.warning("Attempted to upload picture for nonexistent agent") flask.abort(404) try: images.save_image(agent_id, payload["image"], images.ImageType.AGENT) except (KeyError, IOError): flask.abort(500) return "", 200