refactoring and implementation

This commit is contained in:
Kevin Veen-Birkenbach 2022-12-09 20:10:12 +01:00
parent 4860282de1
commit f2b2fd1b07
5 changed files with 84 additions and 20 deletions

View File

@ -1,11 +1,30 @@
from .Cli import Cli from .Cli import Cli
class AbstractSplittedSecret(Cli): class AbstractSplittedSecret(Cli):
def __init__(self): def __init__(self):
super(Cli, self).__init__() super(Cli, self).__init__()
self.encrypted_folder="data/encrypted/" self.data_folder = "data/"
self.decrypted_folder="data/encrypted/"
self.encrypted_group_files_folder = self.encrypted_folder + "group_files/" def getFolderPath(self,folder_type):
self.decrypted_group_files_folder = self.decrypted_folder + "group_files/" return self.data_folder + folder_type + "/"
self.encrypted_user_files_folder = self.encrypted_folder + "user_files/"
self.decrypted_user_files_folder = self.encrypted_folder + "user_files/" def getGroupFilesFolderPath(self,folder_type):
return self.getFolderPath(folder_type) + "group_files/"
def getUserFilesFolderPath(self,folder_type):
return self.getFolderPath(folder_type) + "user_files/"
def getFileExtension(self,file_type):
if file_type == "encrypted":
return ''
return '.gpg'
def getUserFilePath(self,user_id,file_type):
return self.getUserFilesFolderPath(file_type)+user_id+'.json' + self.getFileExtension(file_type);
def getGroupFilePath(self,group_id,file_type):
return self.getGroupFilesFolderPath(file_type) + str(group_id) + '.txt' + self.getFileExtension(file_type);
def getAccumulatedFilePath(self,file_type):
return self.getFolderPath(file_type) + 'accumulated.json' + self.getFileExtension(file_type);

View File

@ -1,9 +1,15 @@
from .AbstractSplittedSecret import AbstractSplittedSecret from .AbstractSplittedSecret import AbstractSplittedSecret
class Cleanup(AbstractSplittedSecret): class Cleanup(AbstractSplittedSecret):
def __init__(self): def __init__(self):
super(Cleanup, self).__init__() super(Cleanup, self).__init__()
self.encrypted_files_folders = [self.decrypted_group_files_folder,self.decrypted_user_files_folder]
self.decrypted_files_folders = [self.encrypted_group_files_folder,self.encrypted_user_files_folder] def getAllFilePaths(self,file_type):
return [
self.getGroupFilesFolderPath(file_type),
self.getUserFilesFolderPath(file_type),
self.getAccumulatedFilePath(file_type)
]
def deleteAllFilesInFolder(self,folder_path): def deleteAllFilesInFolder(self,folder_path):
try: try:
@ -13,8 +19,8 @@ class Cleanup(AbstractSplittedSecret):
except: except:
pass pass
def deleteAllDecryptedFiles(self): def deleteAllFiles(self,file_type):
for folder_path in self.decrypted_files_folders: for folder_path in self.getAllFilePaths(file_type):
self.deleteAllFilesInFolder(folder_path) self.deleteAllFilesInFolder(folder_path)
def deleteAllEncryptedFiles(self): def deleteAllEncryptedFiles(self):
@ -22,11 +28,11 @@ class Cleanup(AbstractSplittedSecret):
self.deleteAllFilesInFolder(folder_path) self.deleteAllFilesInFolder(folder_path)
def cleanupForUser(self,user): def cleanupForUser(self,user):
self.executeCommand('find "' + self.encrypted_folder + '" -not -name "*' + str(user) +'*" -type f -print | xargs rm -v') self.executeCommand('find "' + self.getFolderPath("encrypted") + '" -not -name "*' + str(user) +'*" -type f -print | xargs rm -v')
print(self.getCommandString()) print(self.getCommandString())
print(self.getOutputString()) print(self.getOutputString())
def deleteAll(self): def deleteAll(self):
self.deleteAllEncryptedFiles() self.deleteAllFiles("encrypted")
self.deleteAllDecryptedFiles() self.deleteAllFiles("decrypted")

View File

@ -0,0 +1,23 @@
from .AbstractSplittedSecret import AbstractSplittedSecret
class Decrypt(AbstractSplittedSecret):
def __init__(self):
self.user_id='0';
self.user_password=''
pass
def setUserId(self,user_id):
self.user_id=str(user_id)
def setUserPassword(self,user_password):
self.user_password = str(user_password)
def decryptFile(self,password,input_file_path):
self.executeCommand('gpg --batch --passphrase "'+ password + '" '+ file_path)
print(self.getCommandString())
print(self.getOutputString())
def decryptUserFile(self):
input_file_path = self.getUserFilePath(self.user_id)
self.decryptFile(self.user_password, file_path)

View File

@ -84,24 +84,26 @@ class Generate(AbstractSplittedSecret):
index += 1 index += 1
def encryptStringToFile(self,text,output_file,password): def encryptStringToFile(self,text,output_file,password):
self.executeCommand('echo \'' + text + '\' | gpg --symmetric --armor --batch --passphrase "' + password + '" -o "' + output_file + '.gpg"') self.executeCommand('echo \'' + text + '\' | gpg --symmetric --armor --batch --passphrase "' + password + '" -o "' + output_file + '"')
print(self.getCommandString()) print(self.getCommandString())
def generateEncryptedGroupFiles(self): def generateEncryptedGroupFiles(self):
for password_group_index_int in self.group_mapped_data: for password_group_index_int in self.group_mapped_data:
encrypted_splitted_password_file = AbstractSplittedSecret().encrypted_group_files_folder + str(password_group_index_int) + ".txt" encrypted_group_password_file_path = self.getGroupFilePath(password_group_index_int,"encrypted")
self.encryptStringToFile(self.master_password,encrypted_splitted_password_file,self.group_mapped_data[password_group_index_int]['password']) 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): def encryptToJsonFile(self,data,file_path,password):
self.encryptStringToFile(json.dumps(data,ensure_ascii=False), file_path, password) self.encryptStringToFile(json.dumps(data,ensure_ascii=False), file_path, password)
def encryptUserMappedData(self): def encryptUserMappedData(self):
for user_id in self.user_mapped_data: for user_id in self.user_mapped_data:
file_path=self.encrypted_user_files_folder+user_id+'.json' file_path=self.getUserFilePath(user_id,"encrypted")
self.encryptToJsonFile(self.user_mapped_data[user_id]['groups'],file_path,self.user_mapped_data[user_id]['user_password']) data=self.user_mapped_data[user_id]['groups']
password=self.user_mapped_data[user_id]['user_password']
self.encryptToJsonFile(data,file_path,password)
def encryptAccumulatedMappedData(self): def encryptAccumulatedMappedData(self):
file_path=self.encrypted_folder+'accumulated.json' file_path=self.getAccumulatedFilePath("encrypted")
data={"user_mapped": self.user_mapped_data, "group_mapped": self.group_mapped_data} data={"user_mapped": self.user_mapped_data, "group_mapped": self.group_mapped_data}
self.encryptToJsonFile(data,file_path,self.master_password) self.encryptToJsonFile(data,file_path,self.master_password)

View File

@ -1,11 +1,12 @@
import argparse import argparse
from classes.Generate import Generate from classes.Generate import Generate
from classes.Cleanup import Cleanup from classes.Cleanup import Cleanup
from classes.Decrypt import Decrypt
from getpass import getpass from getpass import getpass
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--mode',type=str, dest='mode',required=True,choices=['cleanup','generate']) parser.add_argument('--mode',type=str, dest='mode',required=True,choices=['cleanup','generate','decrypt'])
parser.add_argument('--amount',type=int, dest='amount_of_secret_holders',required=False,choices=range(1,9)) parser.add_argument('--amount',type=int, dest='amount_of_secret_holders',required=False,choices=range(1,9))
parser.add_argument('--quota', type=int, dest='decryption_quota', choices=range(1,101),required=False) parser.add_argument('--quota', type=int, dest='decryption_quota', choices=range(1,101),required=False)
parser.add_argument('--master-password',type=str, dest='master_password',required=False) parser.add_argument('--master-password',type=str, dest='master_password',required=False)
@ -18,6 +19,19 @@ if __name__ == '__main__':
cleanup.deleteAll() cleanup.deleteAll()
exit() exit()
cleanup.cleanupForUser(args.user) cleanup.cleanupForUser(args.user)
exit()
if args.mode == 'decrypt':
decrypt = Decrypt()
if args.user is None:
print("Please type in the user number:")
decrypt.setUser(int(input()))
else:
decrypt.setUser(args.user)
print("Please enter the master password:")
user_password = getpass()
exit()
if args.mode == 'generate': if args.mode == 'generate':
if args.master_password is None: if args.master_password is None: