def setUp(self): self._db = Database(logger, True) new_api_key = binascii.hexlify(os.urandom(8)).decode('utf-8').upper() node_id = self._db.create_api_key(new_api_key) self.assertGreater(node_id, 0) self.api_key = new_api_key self.node_id = node_id
def post_event(api_key): with current_app.app_context(): environment = Environment(current_app) db = Database(logger=current_app.logger, env=environment) event_log = EventLog(db, logger=current_app.logger) try: node = event_log.get_api_key_object(api_key) event_log.update_quota(node) posted_data = request.get_json(force=True) if "event_type_id" in posted_data: event_type_id = int(posted_data["event_type_id"]) if event_type_id in event_log.event_type_ids_as_set: node_id = node.node_id user_id = None if "user_id" in posted_data and posted_data["user_id"] is not None: user_id = int(posted_data["user_id"]) del posted_data["user_id"] if user_id <= 0: raise ValueError("user_id must be positive or omitted") new_event = event_log.log_event(posted_data, event_type_id, user_id, node_id) return Response(json.dumps({"success": True, "event_id": new_event.event_id})) except APIKeyRateLimited: abort(420) except APIKeyInvalid: abort(404) except APIKeySuspended: abort(403)
def setUp(self) -> None: # my default development environment values dev = Environment("localhost", "localhost", "pico", "password", "picoevent", "picoevent_test_db", 1000, 3600, "localhost", "localhost") self._db = Database(env=dev) self._event_log = EventLog(self._db) self._event_type_ids = self._event_log.list_event_types()
def setUp(self): self._db = Database(logger, True) self._target_api_keys = random.randint(5, 100) self._api_keys = [] for x in range(0, self._target_api_keys): new_api_key = binascii.hexlify( os.urandom(8)).decode('utf-8').upper() result = self._db.create_api_key(new_api_key) self.assertGreater(result, 0) self._api_keys.append(new_api_key)
def setUp(self): self._db = Database(logger, True) self._event_log = EventLog(self._db, logger) self._added_events = [] x = 0 for each_event in DEFAULT_EVENTS: event_type_id = self._event_log.add_event_type(each_event) self.assertGreater(event_type_id, 0) self._added_events.append(each_event) if x == 4: break x += 1
def __init__(self, db=None, logger=None): self._cache = redis.Redis() if db: self._db = db else: self._db = Database(logger) self._logger = logger event_types = [] for each in self.list_event_types(): event_types.append(each[1]) missing_events = set(DEFAULT_EVENTS) - set(event_types) for new_event in missing_events: self._db.create_event_type(new_event)
def admin_create_api_key(): with current_app.app_context(): environment = Environment(current_app) session_token = request.form["token"] db = Database(logger=current_app.logger, env=environment) analyst_user = db.validate_session(session_token) if analyst_user and analyst_user.has_permission("create-api-key"): new_api_key = binascii.hexlify(os.urandom(8)).decode('utf-8').upper() result = db.create_api_key(new_api_key) if result == -1: flash("Could not create API key.", category="error") return redirect(url_for("admin.home", session_token=session_token)) else: abort(403)
def change_administrator_password(self): _db = Database(self._logger, False, False, self._env) admin_uid = _db.admin_user_id if admin_uid > 0: new_password = input("New administrator password: "******"admin", new_password) except DatabaseException: print( "Database exception: unable to reset administrators password." ) else: print( "Invalid administrator user ID, database could be corrupted.") print( "Consider creating a new empty database and starting over.\n")
def list_event_types(api_key): with current_app.app_context(): environment = Environment(current_app) db = Database(logger=current_app.logger, env=environment) event_log = EventLog(db, logger=current_app.logger) try: node = event_log.get_api_key_object(api_key) event_log.update_quota(node) output = json.dumps({"success": True, "event_types": event_log.list_event_types()}) return output except APIKeyRateLimited: abort(420) except APIKeyInvalid: abort(404) except APIKeySuspended: abort(403)
def latest_events(limit, session_token): with current_app.app_context(): environment = Environment(current_app) db = Database(logger=current_app.logger, env=environment, read_only=True) try: user = db.validate_session(session_token) # TODO: permissions event_log = EventLog(db, logger=current_app.logger) _latest_events = event_log.retrieve_events(limit=limit) json_array = [] for each_event in _latest_events: json_array.append(str(each_event)) return Response(json.dumps({"success": True, "count": len(json_array), "events": json_array})) except DatabaseException: abort(403)
def setUp(self) -> None: self._db = Database(logger, True) self._mock_data = dict() total_events = random.randint(0, self.MAX_EVENTS_LOGGED) for each in DEFAULT_EVENTS: event_type_id = self._db.create_event_type(each) self.assertGreater(event_type_id, 0, msg="Failed to create event type") mock_events = [] now = datetime.now() for x in range(0, total_events): mock_event_data = { "floatMock": random.random(), "datetimeMock": random_later_datetime().isoformat(), "integerMock": random.randint(0, 1500000), "stringMock": "a" * random.randint(0, 1024) } new_mock_event = Event(0, event_type_id, each, mock_event_data, 0, 1, now) mock_events.append(new_mock_event) self._mock_data[each] = mock_events event_type = random.choice(DEFAULT_EVENTS) mock_events = self._mock_data[event_type] event_type_id = None for each_mock in mock_events: if event_type_id is None: event_type_id = each_mock.event_type_id event_id = self._db.log_event(each_mock.event_data, each_mock.event_type_id, each_mock.node_id, each_mock.user_id) self.assertGreater(event_id, 0, msg="Failed to log event.") each_mock.event_id = event_id event_type_count = self._db.get_event_count( event_type_id=event_type_id) self.assertEqual(event_type_count, len(mock_events)) logger.info("Events logged: {0} ({1})".format(event_type_count, event_type))
def add_event_type(session_token): with current_app.app_context(): environment = Environment(current_app) db = Database(logger=current_app.logger, env=environment) user_object = db.validate_session(session_token) if user_object and user_object.has_permission("add-event-type"): new_event_type = request.form["new_event_type"] if EVENT_TYPE_REGEX.match(new_event_type): new_event_type_id = db.create_event_type(new_event_type) if new_event_type_id: return Response(json.dumps({"new_event_type_id": new_event_type_id, "success": True})) else: return Response(json.dumps({"success": False, "error_message": "Could not add to database.", "error_code": 1})) else: return Response(json.dumps({"success": False, "error_message": "Invalid event type name.", "error_code": 2})) return Response(status=403)
def setUp(self): self._db = Database(logger, True) self._mock_data = dict() now = datetime.now() for each in DEFAULT_EVENTS: event_type_id = self._db.create_event_type(each) self.assertGreater(event_type_id, 0, msg="Failed to create event type") mock_events = [] for x in range(0, random.randint(10, 50)): mock_event_data = { "floatMock": random.random(), "datetimeMock": random_later_datetime().isoformat(), "integerMock": random.randint(0, 1500000), "stringMock": "a" * random.randint(0, 1024) } new_mock_event = Event(0, event_type_id, each, mock_event_data, 0, 1, now) mock_events.append(new_mock_event) self._mock_data[each] = mock_events
def login(): with current_app.app_context(): environment = Environment(current_app) input_data = request.get_json(True) username = input_data["username"] password = input_data["password"] db = Database(logger=current_app.logger, env=environment) try: result = db.login(username, password) session_token = result[0] return json.dumps({ "success": True, "session_token": session_token.decode('utf-8') }) except DatabaseException: flash("Invalid e-mail/password combination.", category="error") return json.dumps({ "success": False, "error_msg": "Invalid e-mail/password combination." })
def home(session_token): with current_app.app_context(): environment = Environment(current_app) db = Database(logger=current_app.logger, env=environment) event_log = EventLog(db, current_app.logger) user = db.validate_session(session_token) if user: event_types = event_log.list_event_types() color_schema_css = "" color_schema = DefaultColorSchema() for x in range(0, len(event_types)): rgb = color_schema.rgb(x) css = "#event_row_id_{0} {{ background-color: rgb({1},{2},{3}); }}".format( x, rgb[0], rgb[1], rgb[2]) color_schema_css += css + "\n" api_keys = event_log.list_api_keys() return render_template("admin_control_panel.jinja2", session_token=session_token, event_types=event_types, api_keys=api_keys, color_schema_css=color_schema_css) return redirect(url_for("admin_no_session"))
def add_event_type(): with current_app.app_context(): environment = Environment(current_app) session_token = request.form['token'] new_event_name = request.form['new_event_name'] db = Database(logger=current_app.logger, env=environment) user = db.validate_session(session_token) if user and user.has_permission("add-event-type"): event_log = EventLog(db, current_app.logger) try: new_event_id = event_log.add_event_type(new_event_name) if new_event_id > 0: return redirect( url_for("admin.home", session_token=session_token)) except EventLogException: current_app.logger.error( "Event log exception on add_event_type: {0}".format( new_event_name)) flash("Event log/database exception on add_event_type function.", "error") return redirect(url_for("admin.home", session_token=session_token)) flash("Not authorized.", category="error")
def __init__(self, environment, user_id, session_token, use_logger=None): self._db = Database(env=environment, logger=use_logger) self._session_token = session_token self._user_id = user_id
def display_menu(self): if self._state == "USER_ADMIN": self._user_admin_mgr.main_menu() print("Console Administration Main Menu\n") try: test_db = Database(self._logger, True, False, self._env) del test_db except DatabaseException: print( "Warning: Test database does not exist or is not accessible with supplied credentials." ) print("\n") x = 1 for choice in ConsoleMainMenuManager.MAIN_MENU: print("{0}.) {1}".format(x, choice)) x += 1 choice = self.select_item(1, 3) if choice == 1: self.change_administrator_password() elif choice == 2: _db = Database(self._logger, False, False, self._env) # node_id, api_key, created, quota, next_reset, events_posted all_api_keys = _db.list_api_keys() del _db print( "Node ID\tAPI Key\tCreated\tQuota\tNext Reset\tEvents Posted") for each_key in all_api_keys: print("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}".format( each_key[0], each_key[1], each_key[2].isoformat(), each_key[3], each_key[4], each_key[5])) elif choice == 3: print( "Would you like to set a custom quota reset interval for the new API key?" ) custom_reset_seconds = None if self.confirm(): day_seconds = 3600 * 24 print( "Choose a quota reset interval in seconds between 1 and {0}" .format(day_seconds)) custom_reset_seconds = self.select_item(1, day_seconds) print("Would you like to set a custom quota for the new API key?") custom_quota = None if self.confirm(): print("Choose a custom quota between 100 and 50000") custom_quota = self.select_item(100, 50000) new_api_key = binascii.hexlify( os.urandom(8)).decode('utf-8').upper() _db = Database(self._logger, False, False, self._env) node_id = _db.create_api_key(new_api_key, custom_quota, custom_reset_seconds) if node_id > 0: api_key_info = _db.validate_api_key(new_api_key) print( "New API Key {0} generated with a quota of {1}, next reset {1}." .format(api_key_info[1], api_key_info[4], api_key_info[5])) else: print( "Could not create a new API key, check log file for more info." ) del _db elif choice == 4: print( "Reset quota (you will need the node id from the API key list)" ) _db = Database(self._logger, False, False, self._env) all_node_ids = set() for each_api_key in _db.list_api_keys: all_node_ids.add(each_api_key[0]) repeat = True node_id = None while repeat: user_input = int(input("Node ID: ")) if user_input in all_node_ids: node_id = user_input break print( "Node ID {0} not in the list of active API keys. Try again?" ) repeat = self.confirm() if node_id: next_reset = _db.reset_quota print( "Would you like to set a custom reset interval (default: 3600 seconds)" ) if self.confirm(): day_seconds = 3600 * 24 print( "Choose a quota reset interval in seconds between 1 and {0}" .format(day_seconds)) next_reset = datetime.now() + timedelta( seconds=self.select_item(1, day_seconds)) _db.reset_quota(node_id, next_reset) elif choice == 5: self.user_admin_mode(self._user_id, self._session_token) else: print("Bye") sys.exit(0) input("Press Enter to continue")
print( "Could not open the PicoEvent/config/config.json file for reading. Exiting." ) sys.exit() logger = None if "console_logging_enabled" in config and config[ "console_logging_enabled"]: if "console_log_file" in config: print("Logging to file: {0}".format(config["console_log_file"])) logger = setup_file_logging(config["console_log_file"]) env = ConsoleEnvironment() context_manager = ConsoleMainMenuManager(env, logger) db = Database(env=env, logger=logger) session_id = None admin_user_id = -1 def login_admin(): attempts = 0 while attempts < MAX_LOGIN_ATTEMPTS: password = input("admin password: "******"admin", password) return login_info[0] except DatabaseException: attempts += 1 print( "Login failed, try again. ({0} attempts remaining)".format( MAX_LOGIN_ATTEMPTS - attempts))
def setUp(self): self._db = Database(logger, True)
def setUp(self): self._db = Database(logger, True) for each in DEFAULT_EVENTS: self._db.create_event_type(each)