From 91e62c647ea0790545eac2cc8d526289cadc856e Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Mon, 13 Nov 2023 14:14:13 -0500 Subject: [PATCH] Create text file in config folder with init creds --- app/classes/shared/helpers.py | 119 ++++++++++++++++++++++++++++++ app/classes/shared/main_models.py | 4 +- main.py | 41 ++++++---- 3 files changed, 147 insertions(+), 17 deletions(-) diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index 62ce8819..a769eb25 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -6,6 +6,8 @@ import json import tempfile import time import uuid +import random +import array import string import base64 import socket @@ -361,6 +363,123 @@ class Helpers: return result_of_check == 0 + def create_pass(self): + # maximum length of password needed + # this can be changed to suit your password length + max_len = 12 + + # declare arrays of the character that we need in out password + # Represented as chars to enable easy string concatenation + digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] + locase = [ + "a", + "b", + "c", + "d", + "e", + "f", + "g", + "h", + "i", + "j", + "k", + "m", + "n", + "o", + "p", + "q", + "r", + "s", + "t", + "u", + "v", + "w", + "x", + "y", + "z", + ] + + upcase = [ + "A", + "B", + "C", + "D", + "E", + "F", + "G", + "H", + "I", + "J", + "K", + "M", + "N", + "O", + "P", + "Q", + "R", + "S", + "T", + "U", + "V", + "W", + "X", + "Y", + "Z", + ] + + symbols = [ + "@", + "#", + "$", + "%", + "=", + ":", + "?", + ".", + "/", + "|", + "~", + "*", + "(", + ")", + ] + + # combines all the character arrays above to form one array + combo = digits + upcase + locase + symbols + + # randomly select at least one character from each character set above + rand_digit = random.choice(digits) + rand_upper = random.choice(upcase) + rand_lower = random.choice(locase) + rand_symbol = random.choice(symbols) + + # combine the character randomly selected above + # at this stage, the password contains only 4 characters but + # we want a 12-character password + temp_pass = rand_digit + rand_upper + rand_lower + rand_symbol + + # now that we are sure we have at least one character from each + # set of characters, we fill the rest of + # the password length by selecting randomly from the combined + # list of character above. + for x in range(max_len - 4): + temp_pass = temp_pass + random.choice(combo) + + # convert temporary password into array and shuffle to + # prevent it from having a consistent pattern + # where the beginning of the password is predictable + temp_pass_list = array.array("u", temp_pass) + random.shuffle(temp_pass_list) + + # traverse the temporary password array and append the chars + # to form the password + password = "" + for x in temp_pass_list: + password = password + x + + # print out password + return password + @staticmethod def cmdparse(cmd_in): # Parse a string into arguments diff --git a/app/classes/shared/main_models.py b/app/classes/shared/main_models.py index 4bfca52c..2bb3e3fa 100644 --- a/app/classes/shared/main_models.py +++ b/app/classes/shared/main_models.py @@ -14,13 +14,13 @@ class DatabaseBuilder: self.management_helper = management_helper self.users_helper = users_helper - def default_settings(self): + def default_settings(self, password="crafty"): logger.info("Fresh Install Detected - Creating Default Settings") Console.info("Fresh Install Detected - Creating Default Settings") default_data = self.helper.find_default_password() username = default_data.get("username", "admin") - password = default_data.get("password", "crafty") + password = default_data.get("password", password) self.users_helper.add_user( username=username, diff --git a/main.py b/main.py index 143dfb4f..32606cce 100644 --- a/main.py +++ b/main.py @@ -144,6 +144,18 @@ if __name__ == "__main__": installer = DatabaseBuilder(database, helper, user_helper, management_helper) FRESH_INSTALL = installer.is_fresh_install() + if getattr(sys, "frozen", False): + app_path = os.path.dirname(sys.executable) + RUN_MODE = "Frozen/executable" + else: + try: + app_full_path = os.path.realpath(__file__) + app_path = os.path.dirname(app_full_path) + RUN_MODE = "Non-interactive (e.g. 'python main.py')" + except NameError: + app_path = os.getcwd() + RUN_MODE = "Interactive" + if FRESH_INSTALL: Console.debug("Fresh install detected") Console.warning( @@ -152,7 +164,14 @@ if __name__ == "__main__": f"through your router/firewall if you would like to be able " f"to access Crafty remotely." ) - installer.default_settings() + password = helper.create_pass() + installer.default_settings(password) + with open( + os.path.join(app_path, "app", "config", "default-creds.txt"), + "w", + encoding="utf-8", + ) as f: + f.write(json.dumps({"username": "admin", "password": password}, indent=4)) else: Console.debug("Existing install detected") Console.info("Checking for reset secret flag") @@ -227,22 +246,10 @@ if __name__ == "__main__": internet_check_thread = Thread(target=internet_check, name="internet_check") - def controller_setup(): + def controller_setup(application_path, running_mode): if not controller.check_system_user(): controller.add_system_user() - if getattr(sys, "frozen", False): - application_path = os.path.dirname(sys.executable) - running_mode = "Frozen/executable" - else: - try: - app_full_path = os.path.realpath(__file__) - application_path = os.path.dirname(app_full_path) - running_mode = "Non-interactive (e.g. 'python main.py')" - except NameError: - application_path = os.getcwd() - running_mode = "Interactive" - controller.set_project_root(application_path) master_server_dir = controller.management.get_master_server_dir() if master_server_dir == "": @@ -262,7 +269,11 @@ if __name__ == "__main__": helper, tasks_manager, migration_manager, controller, import3 ) - controller_setup_thread = Thread(target=controller_setup, name="controller_setup") + controller_setup_thread = Thread( + target=controller_setup, + name="controller_setup", + args=[RUN_MODE, app_path], + ) def setup_starter(): if not args.daemon: