From 47af457162bf37b19976a9065aaf2d012fbe0a10 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 10 Dec 2022 13:22:09 +0100 Subject: [PATCH] Implemented destructor for main --- scripts/classes/AbstractSplittedSecret.py | 5 +++- scripts/classes/Cleanup.py | 15 ++++------ scripts/classes/Decryption.py | 8 +++--- scripts/classes/Encryption.py | 6 ++-- scripts/main.py | 35 ++++++++++++++--------- 5 files changed, 38 insertions(+), 31 deletions(-) diff --git a/scripts/classes/AbstractSplittedSecret.py b/scripts/classes/AbstractSplittedSecret.py index a7c26b1..e8820b1 100644 --- a/scripts/classes/AbstractSplittedSecret.py +++ b/scripts/classes/AbstractSplittedSecret.py @@ -8,6 +8,9 @@ class AbstractSplittedSecret(Cli): MAXIMUM_SECRET_HOLDERS = 9 MINIMUM_SECRET_HOLDERS = 2 + TYPE_ENCRYPTED="encrypted" + TYPE_DECRYPTED="decrypted" + def __init__(self): super(Cli, self).__init__() self.data_folder = "data/" @@ -28,7 +31,7 @@ class AbstractSplittedSecret(Cli): return self.getFolderPath(folder_type) + "user_files/" def getFileExtension(self,file_type): - if file_type == "encrypted": + if file_type == AbstractSplittedSecret.TYPE_ENCRYPTED: return '.gpg' return '' diff --git a/scripts/classes/Cleanup.py b/scripts/classes/Cleanup.py index 3de55cc..02c5911 100644 --- a/scripts/classes/Cleanup.py +++ b/scripts/classes/Cleanup.py @@ -1,6 +1,5 @@ from .AbstractSplittedSecret import AbstractSplittedSecret class Cleanup(AbstractSplittedSecret): - def __init__(self): super(Cleanup, self).__init__() @@ -17,21 +16,17 @@ class Cleanup(AbstractSplittedSecret): except: pass - def deleteAllFiles(self,file_type): + def cleanupFiles(self,file_type): for folder_path in self.getAllFilePaths(file_type): self.deleteAllFilesInFolder(folder_path) - - def deleteAllEncryptedFiles(self): - for folder_path in self.encrypted_files_folders: - self.deleteAllFilesInFolder(folder_path) def cleanupForUser(self,user): try: - self.executeCommand('find "' + self.getFolderPath("encrypted") + '" -not -name "*' + str(user) +'*" -type f -print | xargs rm -v') + self.executeCommand('find "' + self.getFolderPath(AbstractSplittedSecret.TYPE_ENCRYPTED) + '" -not -name "*' + str(user) +'*" -type f -print | xargs rm -v') except: pass - self.deleteAllFiles("decrypted") + self.cleanupFiles(AbstractSplittedSecret.TYPE_DECRYPTED) def deleteAll(self): - self.deleteAllFiles("encrypted") - self.deleteAllFiles("decrypted") \ No newline at end of file + self.cleanupFiles(AbstractSplittedSecret.TYPE_ENCRYPTED) + self.cleanupFiles(AbstractSplittedSecret.TYPE_DECRYPTED) \ No newline at end of file diff --git a/scripts/classes/Decryption.py b/scripts/classes/Decryption.py index 2ee6200..23bd76f 100644 --- a/scripts/classes/Decryption.py +++ b/scripts/classes/Decryption.py @@ -9,7 +9,7 @@ class Decryption(AbstractSplittedSecret): def initializeUser(self,user_id): self.user_id=str(user_id) - self.user_file_decrypted_path = self.getUserFilePath(self.user_id,"decrypted") + self.user_file_decrypted_path = self.getUserFilePath(self.user_id,AbstractSplittedSecret.TYPE_DECRYPTED) def initializeUserDataDecryption(self): self.decryptUserFile() @@ -59,10 +59,10 @@ class Decryption(AbstractSplittedSecret): self.executeCommand('gpg --batch --passphrase "'+ password + '" -o "' + output_file_path +'" "'+ input_file_path+'"') def decryptUserFile(self): - input_file_path = self.getUserFilePath(self.user_id,"encrypted") + input_file_path = self.getUserFilePath(self.user_id,AbstractSplittedSecret.TYPE_ENCRYPTED) self.decryptFile(self.user_password, input_file_path, self.user_file_decrypted_path) def decryptAccumulatedFile(self): - input_file_path = self.getAccumulatedFilePath("encrypted") - output_file_path = self.getAccumulatedFilePath("decrypted") + input_file_path = self.getAccumulatedFilePath(AbstractSplittedSecret.TYPE_ENCRYPTED) + output_file_path = self.getAccumulatedFilePath(AbstractSplittedSecret.TYPE_DECRYPTED) self.decryptFile(self.user_password, input_file_path, output_file_path) \ No newline at end of file diff --git a/scripts/classes/Encryption.py b/scripts/classes/Encryption.py index f8ba2df..1675cb8 100644 --- a/scripts/classes/Encryption.py +++ b/scripts/classes/Encryption.py @@ -94,7 +94,7 @@ class Encryption(AbstractSplittedSecret): def encryptGroupFiles(self): for password_group_index_int in self.group_mapped_data: - encrypted_group_password_file_path = self.getGroupFilePath(password_group_index_int,"encrypted") + encrypted_group_password_file_path = self.getGroupFilePath(password_group_index_int,AbstractSplittedSecret.TYPE_ENCRYPTED) self.encryptStringToFile(self.master_password,encrypted_group_password_file_path,self.group_mapped_data[password_group_index_int]['password']) def encryptToJsonFile(self,data,file_path,password): @@ -102,13 +102,13 @@ class Encryption(AbstractSplittedSecret): def encryptUserData(self): for user_id in self.user_mapped_data: - file_path=self.getUserFilePath(user_id,"encrypted") + file_path=self.getUserFilePath(user_id,AbstractSplittedSecret.TYPE_ENCRYPTED) data=self.user_mapped_data[user_id] password=self.user_mapped_data[user_id]['user_password'] self.encryptToJsonFile(data,file_path,password) def encryptAccumulatedData(self): - file_path=self.getAccumulatedFilePath("encrypted") + file_path=self.getAccumulatedFilePath(AbstractSplittedSecret.TYPE_ENCRYPTED) data={"user_mapped": self.user_mapped_data, "group_mapped": self.group_mapped_data} self.encryptToJsonFile(data,file_path,self.master_password) diff --git a/scripts/main.py b/scripts/main.py index 72d07c0..16cd4c9 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -5,6 +5,15 @@ from classes.Decryption import Decryption from getpass import getpass from classes.AbstractSplittedSecret import AbstractSplittedSecret +cleanup = Cleanup() + +def clean_exit(): + print("Cleaning up.") + cleanup.cleanupFiles(AbstractSplittedSecret.TYPE_DECRYPTED) + print("Leaving program. Goodby :)") + exit(); + pass + if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--mode',type=str, dest='mode',required=True,choices=['cleanup','encrypt','decrypt']) @@ -15,22 +24,20 @@ if __name__ == '__main__': parser.add_argument('--user',type=int, dest='user',choices=AbstractSplittedSecret.getSecretHoldersRange(),required=False) parser.add_argument('--add-user-information',type=bool, dest='add_user_information', default=False, required=False, action=argparse.BooleanOptionalAction) args = parser.parse_args() - mode = args.mode - + print("Splitted Secret Interface started.") - print("Selected Mode: " + mode) + print("Selected Mode: " + args.mode) - if mode == 'cleanup': - cleanup = Cleanup() + if args.mode == 'cleanup': if args.user is None: print("Delete all files.") cleanup.deleteAll() - exit() + clean_exit() print("Delete files for user <<" + str(args.user) + ">>"); cleanup.cleanupForUser(args.user) - exit() + clean_exit() - if mode == 'decrypt': + if args.mode == 'decrypt': decrypt = Decryption() if args.master_password is None: if args.user is None: @@ -55,7 +62,7 @@ if __name__ == '__main__': decrypt.initializeUserDataDecryption(); except Exception as error: print("An error occured. Propably you passed a wrong password :( The error is: " + str(error)) - exit() + clean_exit() print("File decrypted :) \n") print("Please contact the following persons and tell them that you need help to encrypt the data: \n") for contact_id in decrypt.user_data['contacts']: @@ -76,13 +83,14 @@ if __name__ == '__main__': break except Exception as error: print("The following error occured <<" + str(error) + ">> :( \n Please try again :)") - exit() + + clean_exit() print("Decrypting accumulated file...") decrypt.setUserPassword(args.master_password) decrypt.decryptAccumulatedFile() - exit() + clean_exit() - if mode == 'encrypt': + if args.mode == 'encrypt': if args.master_password is None: print("Please enter the master password:") master_password = getpass() @@ -96,4 +104,5 @@ if __name__ == '__main__': encrypt.addInformationToUser(user_id, label, str(input())) encrypt.compileData() encrypt.encrypt() - exit() \ No newline at end of file + clean_exit() +clean_exit() \ No newline at end of file