From 7889ca31535d596980d6fe5f46476439e57caec5 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 29 Jan 2022 01:36:37 -0500 Subject: [PATCH 1/5] Delete audit logs when they exceed 100 entries --- app/classes/models/management.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/classes/models/management.py b/app/classes/models/management.py index 7a8fa64f..36057015 100644 --- a/app/classes/models/management.py +++ b/app/classes/models/management.py @@ -191,6 +191,14 @@ class helpers_management: Audit_Log.log_msg: audit_msg, Audit_Log.source_ip: source_ip }).execute() + #todo make this user configurable + #deletes records when they're more than 100 + ordered = Audit_Log.select().order_by(+Audit_Log.created) + for item in ordered: + if Audit_Log.select().count() > 100: + Audit_Log.delete().where(Audit_Log.audit_id == item.audit_id).execute() + else: + return @staticmethod def add_to_audit_log_raw(user_name, user_id, server_id, log_msg, source_ip): @@ -201,7 +209,13 @@ class helpers_management: Audit_Log.log_msg: log_msg, Audit_Log.source_ip: source_ip }).execute() - + #deletes records when they're more than 100 + ordered = Audit_Log.select().order_by(+Audit_Log.created) + for item in ordered: + if Audit_Log.select().count() > 100: + Audit_Log.delete().where(Audit_Log.audit_id == item.audit_id).execute() + else: + return #************************************************************************************************ # Schedules Methods #************************************************************************************************ From 184a042e2aa36adb6f5466df27eeabbb68e87ef7 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 29 Jan 2022 01:37:28 -0500 Subject: [PATCH 2/5] Delete audit logs when they exceed 300 entries --- app/classes/models/management.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/classes/models/management.py b/app/classes/models/management.py index 36057015..56dedaa2 100644 --- a/app/classes/models/management.py +++ b/app/classes/models/management.py @@ -195,7 +195,7 @@ class helpers_management: #deletes records when they're more than 100 ordered = Audit_Log.select().order_by(+Audit_Log.created) for item in ordered: - if Audit_Log.select().count() > 100: + if Audit_Log.select().count() > 300: Audit_Log.delete().where(Audit_Log.audit_id == item.audit_id).execute() else: return @@ -212,7 +212,7 @@ class helpers_management: #deletes records when they're more than 100 ordered = Audit_Log.select().order_by(+Audit_Log.created) for item in ordered: - if Audit_Log.select().count() > 100: + if Audit_Log.select().count() > 300: Audit_Log.delete().where(Audit_Log.audit_id == item.audit_id).execute() else: return From 37ccc447c44d77c3cbcf1c92178157b2092361a1 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 29 Jan 2022 16:37:00 -0500 Subject: [PATCH 3/5] Fix backup config bug --- app/classes/models/management.py | 5 +++-- app/classes/shared/tasks.py | 3 +++ app/classes/web/panel_handler.py | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/classes/models/management.py b/app/classes/models/management.py index 7a8fa64f..1dded136 100644 --- a/app/classes/models/management.py +++ b/app/classes/models/management.py @@ -1,3 +1,4 @@ +from http import server import sys import logging import datetime @@ -291,10 +292,10 @@ class helpers_management: @staticmethod def set_backup_config(server_id: int, backup_path: str = None, max_backups: int = None): logger.debug(f"Updating server {server_id} backup config with {locals()}") - try: + if Backups.select().where(Backups.server_id == server_id).count() != 0: new_row = False conf = {} - except IndexError: + else: conf = { "directories": None, "max_backups": 0, diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index 8a2c6855..df680b31 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -17,6 +17,7 @@ from app.classes.models.management import management_helper from app.classes.controllers.users_controller import Users_Controller from app.classes.controllers.servers_controller import Servers_Controller from app.classes.models.servers import servers_helper +from app.classes.models.users import users_helper logger = logging.getLogger('apscheduler') @@ -373,6 +374,8 @@ class TasksManager: if not event.exception: if str(event.job_id).isnumeric(): task = management_helper.get_scheduled_task_model(int(event.job_id)) + management_helper.add_to_audit_log_raw('system', users_helper.get_user_id_by_name('system'), task.server_id, + f"Task with id {task.schedule_id} completed successfully", '127.0.0.1') if task.one_time: self.remove_job(task.schedule_id) logger.info("one time task detected. Deleting...") diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 92ea613c..1c37fe6c 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -452,6 +452,10 @@ class PanelHandler(BaseHandler): return server = self.controller.get_server_obj(server_id) + management_helper.add_to_audit_log_raw( + self.controller.users.get_user_by_id(exec_user['user_id'])['username'], exec_user['user_id'], server_id, + f"Backup now executed for server {server_id} ", + source_ip=self.get_remote_ip()) server.backup_server() self.redirect(f"/panel/server_detail?id={server_id}&subpage=backup") From e69a3be381688e8eaaf63c3a0eb22b45ffa38f08 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 30 Jan 2022 15:28:48 -0500 Subject: [PATCH 4/5] Fix bug where stopping a server without crash detection would crash crafty...ironic, I know. --- app/classes/shared/server.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index 005b48be..2f27ffdb 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -322,9 +322,10 @@ class Server: def stop_server(self): if self.settings['stop_command']: self.send_command(self.settings['stop_command']) - #remove crash detection watcher - logger.info(f"Removing crash watcher for server {self.name}") - self.server_scheduler.remove_job('c_' + str(self.server_id)) + if self.settings['crash_detection']: + #remove crash detection watcher + logger.info(f"Removing crash watcher for server {self.name}") + self.server_scheduler.remove_job('c_' + str(self.server_id)) else: #windows will need to be handled separately for Ctrl+C self.process.terminate() @@ -408,7 +409,7 @@ class Server: print("crash detected") # clear the old scheduled watcher task - self.server_scheduler.remove_job("c_"+str(self.server_id)) + self.server_scheduler.remove_job(f"c_{self.server_id}") # the server crashed, or isn't found - so let's reset things. logger.warning(f"The server {name} seems to have vanished unexpectedly, did it crash?") From ac19c86089f9e591f7d03bf8ccef2b6d492999ee Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 30 Jan 2022 15:34:09 -0500 Subject: [PATCH 5/5] Appease the linter --- app/classes/models/management.py | 1 - app/classes/web/panel_handler.py | 1 - 2 files changed, 2 deletions(-) diff --git a/app/classes/models/management.py b/app/classes/models/management.py index 91fb98db..ea2d8487 100644 --- a/app/classes/models/management.py +++ b/app/classes/models/management.py @@ -1,4 +1,3 @@ -from http import server import sys import logging import datetime diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 1c37fe6c..24caa18f 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -17,7 +17,6 @@ from tornado.ioloop import IOLoop #TZLocal is set as a hidden import on win pipeline from tzlocal import get_localzone from cron_validator import CronValidator -from app.classes.controllers.servers_controller import Servers_Controller from app.classes.models.server_permissions import Enum_Permissions_Server from app.classes.models.crafty_permissions import Enum_Permissions_Crafty