Implemented destructor for main

This commit is contained in:
Kevin Veen-Birkenbach 2022-12-10 13:22:09 +01:00
parent f182e3226a
commit 47af457162
5 changed files with 38 additions and 31 deletions

View File

@ -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 ''

View File

@ -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")
self.cleanupFiles(AbstractSplittedSecret.TYPE_ENCRYPTED)
self.cleanupFiles(AbstractSplittedSecret.TYPE_DECRYPTED)

View File

@ -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)

View File

@ -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)

View File

@ -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()
clean_exit()
clean_exit()