Changed paths to dependency injection

This commit is contained in:
Kevin Veen-Birkenbach 2022-12-10 22:19:57 +01:00
parent bfcda49660
commit baefc461fa
5 changed files with 52 additions and 52 deletions

View File

@ -1,18 +1,18 @@
from .AbstractSplittedSecret import AbstractSplittedSecret from .Paths import Paths
class Cleanup(AbstractSplittedSecret): class Cleanup():
def __init__(self,cli): def __init__(self,cli,paths):
self.cli = cli self.cli = cli
super(Cleanup, self).__init__() self.paths = paths
def getAllFilePaths(self,file_type): def getAllFilePaths(self,file_type):
all_file_paths = [ all_file_paths = [
self.getGroupFilesFolderPath(file_type), self.paths.getGroupFilesFolderPath(file_type),
self.getUserFilesFolderPath(file_type), self.paths.getUserFilesFolderPath(file_type),
self.getAccumulatedFilePath(file_type) self.paths.getAccumulatedFilePath(file_type)
] ]
if file_type == AbstractSplittedSecret.TYPE_DECRYPTED: if file_type == Paths.TYPE_DECRYPTED:
all_file_paths.append(self.getDecryptedMainDataStandartFolder()) all_file_paths.append(self.paths.getDecryptedMainDataStandartFolder())
return all_file_paths return all_file_paths
def deleteAllFilesInFolder(self,folder_path): def deleteAllFilesInFolder(self,folder_path):
@ -27,11 +27,11 @@ class Cleanup(AbstractSplittedSecret):
def cleanupForUser(self,user): def cleanupForUser(self,user):
try: try:
self.cli.executeCommand('find "' + self.getDataFolderPath(AbstractSplittedSecret.TYPE_ENCRYPTED) + '" -not -name "*' + str(user) +'*" -type f -print | xargs rm -v') self.cli.executeCommand('find "' + self.paths.getDataFolderPath(Paths.TYPE_ENCRYPTED) + '" -not -name "*' + str(user) +'*" -type f -print | xargs rm -v')
except Exception as error: except Exception as error:
print(error) print(error)
self.cleanupFiles(AbstractSplittedSecret.TYPE_DECRYPTED) self.cleanupFiles(Paths.TYPE_DECRYPTED)
def deleteAll(self): def deleteAll(self):
self.cleanupFiles(AbstractSplittedSecret.TYPE_ENCRYPTED) self.cleanupFiles(Paths.TYPE_ENCRYPTED)
self.cleanupFiles(AbstractSplittedSecret.TYPE_DECRYPTED) self.cleanupFiles(Paths.TYPE_DECRYPTED)

View File

@ -1,18 +1,17 @@
from .AbstractSplittedSecret import AbstractSplittedSecret
import json import json
from pathlib import Path from pathlib import Path
class Decryption(AbstractSplittedSecret): class Decryption():
def __init__(self,cli): def __init__(self,cli,paths):
self.user_id='0'; self.user_id='0';
self.user_password='' self.user_password=''
self.cli = cli self.cli = cli
super(Decryption, self).__init__() self.paths = paths
def initializeUser(self,user_id): def initializeUser(self,user_id):
self.user_id=str(user_id) self.user_id=str(user_id)
self.user_file_decrypted_path = self.getUserFilePath(self.user_id,AbstractSplittedSecret.TYPE_DECRYPTED) self.user_file_decrypted_path = self.paths.getUserFilePath(self.user_id,self.paths.TYPE_DECRYPTED)
def initializeUserDataDecryption(self): def initializeUserDataDecryption(self):
self.decryptUserFile() self.decryptUserFile()
@ -22,7 +21,7 @@ class Decryption(AbstractSplittedSecret):
def initializeGroupDataEncryption(self): def initializeGroupDataEncryption(self):
self.group_name = self.getDecryptersGroupName() self.group_name = self.getDecryptersGroupName()
self.encrypted_group_file_path = self.getGroupFilePath(self.group_name, AbstractSplittedSecret.TYPE_DECRYPTED) self.encrypted_group_file_path = self.paths.getGroupFilePath(self.group_name, self.paths.TYPE_DECRYPTED)
self.decryptGroupFile() self.decryptGroupFile()
self.master_password = self.loadTxtFile(self.encrypted_group_file_path).strip() self.master_password = self.loadTxtFile(self.encrypted_group_file_path).strip()
@ -102,17 +101,17 @@ class Decryption(AbstractSplittedSecret):
self.cli.executeCommand('gpg --batch --passphrase "'+ password + '" -o "' + output_file_path +'" "'+ input_file_path+'"') self.cli.executeCommand('gpg --batch --passphrase "'+ password + '" -o "' + output_file_path +'" "'+ input_file_path+'"')
def decryptUserFile(self): def decryptUserFile(self):
input_file_path = self.getUserFilePath(self.user_id,AbstractSplittedSecret.TYPE_ENCRYPTED) input_file_path = self.paths.getUserFilePath(self.user_id,self.paths.TYPE_ENCRYPTED)
self.decryptFile(self.user_password, input_file_path, self.user_file_decrypted_path) self.decryptFile(self.user_password, input_file_path, self.user_file_decrypted_path)
def decryptGroupFile(self): def decryptGroupFile(self):
input_file_path = self.getGroupFilePath(self.group_name, AbstractSplittedSecret.TYPE_ENCRYPTED) input_file_path = self.paths.getGroupFilePath(self.group_name, self.paths.TYPE_ENCRYPTED)
self.decryptFile(self.getGroupPassword(), input_file_path, self.encrypted_group_file_path) self.decryptFile(self.getGroupPassword(), input_file_path, self.encrypted_group_file_path)
def decryptAccumulatedFile(self): def decryptAccumulatedFile(self):
input_file_path = self.getAccumulatedFilePath(AbstractSplittedSecret.TYPE_ENCRYPTED) input_file_path = self.paths.getAccumulatedFilePath(self.paths.TYPE_ENCRYPTED)
output_file_path = self.getAccumulatedFilePath(AbstractSplittedSecret.TYPE_DECRYPTED) output_file_path = self.paths.getAccumulatedFilePath(self.paths.TYPE_DECRYPTED)
self.decryptFile(self.user_password, input_file_path, output_file_path) self.decryptFile(self.user_password, input_file_path, output_file_path)
def decryptMainData(self): def decryptMainData(self):
self.cli.executeCommand('gpg --batch --passphrase "' + self.getMasterPassword() + '" -d "' + self.getEncryptedMainDataFile() + '" | tar -xvzf - "' + self.getDecryptedMainDataStandartFolder() + '"') self.cli.executeCommand('gpg --batch --passphrase "' + self.getMasterPassword() + '" -d "' + self.paths.getEncryptedMainDataFile() + '" | tar -xvzf - "' + self.paths.getDecryptedMainDataStandartFolder() + '"')

View File

@ -4,15 +4,14 @@ import math
import numpy import numpy
import re import re
import json import json
from .AbstractSplittedSecret import AbstractSplittedSecret from .Paths import Paths
class Encryption(AbstractSplittedSecret): class Encryption():
USER_PASSWORD_LENGTHS = 64 USER_PASSWORD_LENGTHS = 64
OVERALL_PASSWORD_LENGTHS = 128 OVERALL_PASSWORD_LENGTHS = 128
def __init__(self, cli, amount_of_secret_holders, decryption_quota,master_password): def __init__(self, cli, paths, amount_of_secret_holders, decryption_quota,master_password):
super(Encryption, self).__init__()
self.amount_of_secret_holders = amount_of_secret_holders self.amount_of_secret_holders = amount_of_secret_holders
self.decryption_quota = decryption_quota self.decryption_quota = decryption_quota
self.master_password = master_password self.master_password = master_password
@ -21,6 +20,7 @@ class Encryption(AbstractSplittedSecret):
self.initializeUserData() self.initializeUserData()
self.initializeGroupData() self.initializeGroupData()
self.cli = cli self.cli = cli
self.paths = paths
def initializeUserData(self): def initializeUserData(self):
self.user_mapped_data = {} self.user_mapped_data = {}
@ -98,7 +98,7 @@ class Encryption(AbstractSplittedSecret):
def encryptGroupFiles(self): def encryptGroupFiles(self):
for password_group_index_int in self.group_mapped_data: for password_group_index_int in self.group_mapped_data:
encrypted_group_password_file_path = self.getGroupFilePath(password_group_index_int,AbstractSplittedSecret.TYPE_ENCRYPTED) encrypted_group_password_file_path = self.paths.getGroupFilePath(password_group_index_int,Paths.TYPE_ENCRYPTED)
self.encryptStringToFile(self.master_password,encrypted_group_password_file_path,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):
@ -106,18 +106,18 @@ class Encryption(AbstractSplittedSecret):
def encryptUserFile(self): def encryptUserFile(self):
for user_id in self.user_mapped_data: for user_id in self.user_mapped_data:
file_path=self.getUserFilePath(user_id,AbstractSplittedSecret.TYPE_ENCRYPTED) file_path=self.paths.getUserFilePath(user_id,Paths.TYPE_ENCRYPTED)
data=self.user_mapped_data[user_id] data=self.user_mapped_data[user_id]
password=self.user_mapped_data[user_id]['user_password'] password=self.user_mapped_data[user_id]['user_password']
self.encryptToJsonFile(data,file_path,password) self.encryptToJsonFile(data,file_path,password)
def encryptAccumulatedFile(self): def encryptAccumulatedFile(self):
file_path=self.getAccumulatedFilePath(AbstractSplittedSecret.TYPE_ENCRYPTED) file_path=self.paths.getAccumulatedFilePath(Paths.TYPE_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)
def encryptMainData(self): def encryptMainData(self):
self.cli.executeCommand('tar -cvzf - "' + self.getDecryptedMainDataStandartFolder() + '" | gpg -c --batch --passphrase "' + self.master_password +'" > "' + self.getEncryptedMainDataFile() + '"'); self.cli.executeCommand('tar -cvzf - "' + self.paths.getDecryptedMainDataStandartFolder() + '" | gpg -c --batch --passphrase "' + self.master_password +'" > "' + self.paths.getEncryptedMainDataFile() + '"');
def encryptAll(self): def encryptAll(self):
self.encryptUserFile() self.encryptUserFile()

View File

@ -1,26 +1,25 @@
import os import os
class AbstractSplittedSecret(): class Paths():
# At the moment the programm can only deal with one digit numbers. # At the moment the programm can only deal with one digit numbers.
MAXIMUM_SECRET_HOLDERS = 9 MAXIMUM_SECRET_HOLDERS = 9
MINIMUM_SECRET_HOLDERS = 2 MINIMUM_SECRET_HOLDERS = 2
def getCoSecretHoldersRange():
return range(Paths.MINIMUM_SECRET_HOLDERS,Paths.MAXIMUM_SECRET_HOLDERS)
def getSecretHoldersRange():
return range(1,Paths.MAXIMUM_SECRET_HOLDERS)
TYPE_ENCRYPTED="encrypted" TYPE_ENCRYPTED="encrypted"
TYPE_DECRYPTED="decrypted" TYPE_DECRYPTED="decrypted"
ROOT_PATH= os.path.join(os.path.dirname(os.path.abspath(__file__)),"../","../") ROOT_PATH= os.path.join(os.path.dirname(os.path.abspath(__file__)),"../","../")
def __init__(self): def __init__(self):
self.data_folder = os.path.join(self.ROOT_PATH,"data") + '/' self.data_folder = os.path.join(self.ROOT_PATH,"data") + '/'
def getCoSecretHoldersRange():
return range(AbstractSplittedSecret.MINIMUM_SECRET_HOLDERS,AbstractSplittedSecret.MAXIMUM_SECRET_HOLDERS)
def getSecretHoldersRange():
return range(1,AbstractSplittedSecret.MAXIMUM_SECRET_HOLDERS)
def getDataFolderPath(self,folder_type): def getDataFolderPath(self,folder_type):
return self.data_folder + folder_type + "/" return self.data_folder + folder_type + "/"
@ -31,13 +30,13 @@ class AbstractSplittedSecret():
return self.getDataFolderPath(folder_type) + "user_files/" return self.getDataFolderPath(folder_type) + "user_files/"
def getEncryptedMainDataFile(self): def getEncryptedMainDataFile(self):
return self.getDataFolderPath(AbstractSplittedSecret.TYPE_ENCRYPTED) + "main_data.tar.gz.gpg" return self.getDataFolderPath(Paths.TYPE_ENCRYPTED) + "main_data.tar.gz.gpg"
def getDecryptedMainDataStandartFolder(self): def getDecryptedMainDataStandartFolder(self):
return self.getDataFolderPath(AbstractSplittedSecret.TYPE_DECRYPTED) + "main_data/" return self.getDataFolderPath(Paths.TYPE_DECRYPTED) + "main_data/"
def getFileExtension(self,file_type): def getFileExtension(self,file_type):
if file_type == AbstractSplittedSecret.TYPE_ENCRYPTED: if file_type == Paths.TYPE_ENCRYPTED:
return '.gpg' return '.gpg'
return '' return ''

View File

@ -3,24 +3,26 @@ from classes.Encryption import Encryption
from classes.Cleanup import Cleanup from classes.Cleanup import Cleanup
from classes.Decryption import Decryption from classes.Decryption import Decryption
from getpass import getpass from getpass import getpass
from classes.AbstractSplittedSecret import AbstractSplittedSecret from classes.Paths import Paths
import traceback import traceback
from classes.Cli import Cli from classes.Cli import Cli
from classes.Paths import Paths
cli = Cli() cli = Cli()
cleanup = Cleanup(cli) paths = Paths()
cleanup = Cleanup(cli,paths)
def clean_exit(): def clean_exit():
print("Cleaning up.") print("Cleaning up.")
try: try:
cleanup.cleanupFiles(AbstractSplittedSecret.TYPE_DECRYPTED) cleanup.cleanupFiles(Paths.TYPE_DECRYPTED)
except: except:
pass pass
standard_exit() standard_exit()
def dirty_exit(): def dirty_exit():
print("ATTENTION: SECURITY RISK !!!\nPROGRAM DIDN'T CLEAN UP DECRYPTED DATA. \nDECRYPTED DATA EXISTS AND CAN BE READ BY EVERYBODY!") print("ATTENTION: SECURITY RISK !!!\nPROGRAM DIDN'T CLEAN UP DECRYPTED DATA. \nDECRYPTED DATA EXISTS AND CAN BE READ BY EVERYBODY!")
print("TO REMOVE DECRYPTED DATA EXECUTE:\nmain.py --mode cleanup --file-types " + AbstractSplittedSecret.TYPE_DECRYPTED) print("TO REMOVE DECRYPTED DATA EXECUTE:\nmain.py --mode cleanup --file-types " + Paths.TYPE_DECRYPTED)
standard_exit() standard_exit()
def standard_exit(): def standard_exit():
@ -31,12 +33,12 @@ try:
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--mode',type=str, dest='mode',required=True,choices=['cleanup','encrypt','decrypt']) parser.add_argument('--mode',type=str, dest='mode',required=True,choices=['cleanup','encrypt','decrypt'])
parser.add_argument('--file-types',type=str, dest='file_types',required=False,choices=[AbstractSplittedSecret.TYPE_DECRYPTED, AbstractSplittedSecret.TYPE_ENCRYPTED]) parser.add_argument('--file-types',type=str, dest='file_types',required=False,choices=[Paths.TYPE_DECRYPTED, Paths.TYPE_ENCRYPTED])
parser.add_argument('--amount',type=int, dest='amount_of_secret_holders',required=False,choices=AbstractSplittedSecret.getCoSecretHoldersRange()) parser.add_argument('--amount',type=int, dest='amount_of_secret_holders',required=False,choices=Paths.getCoSecretHoldersRange())
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)
parser.add_argument('--user-password',type=str, dest='user_password',required=False) parser.add_argument('--user-password',type=str, dest='user_password',required=False)
parser.add_argument('--user',type=int, dest='user',choices=AbstractSplittedSecret.getSecretHoldersRange(),required=False) parser.add_argument('--user',type=int, dest='user',choices=Paths.getSecretHoldersRange(),required=False)
parser.add_argument('--add-user-information',type=bool, dest='add_user_information', default=False, required=False, action=argparse.BooleanOptionalAction) parser.add_argument('--add-user-information',type=bool, dest='add_user_information', default=False, required=False, action=argparse.BooleanOptionalAction)
args = parser.parse_args() args = parser.parse_args()
@ -58,7 +60,7 @@ try:
standard_exit() standard_exit()
if args.mode == 'decrypt': if args.mode == 'decrypt':
decrypt = Decryption(cli) decrypt = Decryption(cli,paths)
if args.master_password is None: if args.master_password is None:
if args.user is None: if args.user is None:
print("Type in the user id:") print("Type in the user id:")
@ -139,7 +141,7 @@ try:
master_password = getpass() master_password = getpass()
else: else:
master_password = args.master_password master_password = args.master_password
encrypt = Encryption(cli,args.amount_of_secret_holders, args.decryption_quota, master_password) encrypt = Encryption(cli,paths,args.amount_of_secret_holders, args.decryption_quota, master_password)
if args.add_user_information is not None: if args.add_user_information is not None:
for user_id in encrypt.user_mapped_data: for user_id in encrypt.user_mapped_data:
for label in ['name','phone','email','address']: for label in ['name','phone','email','address']: