Refactored class cli

This commit is contained in:
Kevin Veen-Birkenbach 2022-12-10 22:03:29 +01:00
parent c06d1d34d2
commit bfcda49660
6 changed files with 25 additions and 20 deletions

View File

@ -3,8 +3,8 @@ The purpose of this software is to splitt a secret over multiple people. Just if
# testing # testing
```bash ```bash
python splitted-secret/scripts/main.py --mode cleanup && python scripts/main.py --mode cleanup &&
python splitted-secret/scripts/main.py --amount 3 --quota 50 --mode encrypt --add-user-information --master-password "ewrwerwerew" << END_OF_INPUTS python scripts/main.py --amount 3 --quota 50 --mode encrypt --add-user-information --master-password "ewrwerwerew" << END_OF_INPUTS
alpha bravo alpha bravo
123123812908 123123812908
asfdasd@asdskjd.de asfdasd@asdskjd.de
@ -46,7 +46,6 @@ END_OF_INPUTS
## todo ## todo
- implement tails setup script - implement tails setup script
- implement tmp mount for decrypted files
- add data-input attribut - add data-input attribut
- add data-output attribut - add data-output attribut

View File

@ -1,9 +1,6 @@
from .Cli import Cli
import os import os
class AbstractSplittedSecret(Cli): class AbstractSplittedSecret():
USER_PASSWORD_LENGTHS = 64
OVERALL_PASSWORD_LENGTHS = 128
# 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
@ -15,7 +12,6 @@ class AbstractSplittedSecret(Cli):
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):
super(Cli, self).__init__()
self.data_folder = os.path.join(self.ROOT_PATH,"data") + '/' self.data_folder = os.path.join(self.ROOT_PATH,"data") + '/'

View File

@ -1,6 +1,8 @@
from .AbstractSplittedSecret import AbstractSplittedSecret from .AbstractSplittedSecret import AbstractSplittedSecret
class Cleanup(AbstractSplittedSecret): class Cleanup(AbstractSplittedSecret):
def __init__(self): def __init__(self,cli):
self.cli = cli
super(Cleanup, self).__init__() super(Cleanup, self).__init__()
def getAllFilePaths(self,file_type): def getAllFilePaths(self,file_type):
@ -15,7 +17,7 @@ class Cleanup(AbstractSplittedSecret):
def deleteAllFilesInFolder(self,folder_path): def deleteAllFilesInFolder(self,folder_path):
try: try:
self.executeCommand('rm -r ' + folder_path + '*') self.cli.executeCommand('rm -r ' + folder_path + '*')
except Exception as error: except Exception as error:
print(error) print(error)
@ -25,7 +27,7 @@ class Cleanup(AbstractSplittedSecret):
def cleanupForUser(self,user): def cleanupForUser(self,user):
try: try:
self.executeCommand('find "' + self.getDataFolderPath(AbstractSplittedSecret.TYPE_ENCRYPTED) + '" -not -name "*' + str(user) +'*" -type f -print | xargs rm -v') self.cli.executeCommand('find "' + self.getDataFolderPath(AbstractSplittedSecret.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(AbstractSplittedSecret.TYPE_DECRYPTED)

View File

@ -1,11 +1,13 @@
from .AbstractSplittedSecret import AbstractSplittedSecret from .AbstractSplittedSecret import AbstractSplittedSecret
import json import json
from pathlib import Path from pathlib import Path
class Decryption(AbstractSplittedSecret): class Decryption(AbstractSplittedSecret):
def __init__(self): def __init__(self,cli):
self.user_id='0'; self.user_id='0';
self.user_password='' self.user_password=''
self.cli = cli
super(Decryption, self).__init__() super(Decryption, self).__init__()
def initializeUser(self,user_id): def initializeUser(self,user_id):
@ -97,7 +99,7 @@ class Decryption(AbstractSplittedSecret):
return data return data
def decryptFile(self,password,input_file_path,output_file_path): def decryptFile(self,password,input_file_path,output_file_path):
self.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.getUserFilePath(self.user_id,AbstractSplittedSecret.TYPE_ENCRYPTED)
@ -113,4 +115,4 @@ class Decryption(AbstractSplittedSecret):
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.executeCommand('gpg --batch --passphrase "' + self.getMasterPassword() + '" -d "' + self.getEncryptedMainDataFile() + '" | tar -xvzf - "' + self.getDecryptedMainDataStandartFolder() + '"') self.cli.executeCommand('gpg --batch --passphrase "' + self.getMasterPassword() + '" -d "' + self.getEncryptedMainDataFile() + '" | tar -xvzf - "' + self.getDecryptedMainDataStandartFolder() + '"')

View File

@ -8,7 +8,10 @@ from .AbstractSplittedSecret import AbstractSplittedSecret
class Encryption(AbstractSplittedSecret): class Encryption(AbstractSplittedSecret):
def __init__(self, amount_of_secret_holders, decryption_quota,master_password): USER_PASSWORD_LENGTHS = 64
OVERALL_PASSWORD_LENGTHS = 128
def __init__(self, cli, amount_of_secret_holders, decryption_quota,master_password):
super(Encryption, self).__init__() 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
@ -17,6 +20,7 @@ class Encryption(AbstractSplittedSecret):
self.group_members_amount=math.ceil(self.amount_of_secret_holders * self.quota_factor) self.group_members_amount=math.ceil(self.amount_of_secret_holders * self.quota_factor)
self.initializeUserData() self.initializeUserData()
self.initializeGroupData() self.initializeGroupData()
self.cli = cli
def initializeUserData(self): def initializeUserData(self):
self.user_mapped_data = {} self.user_mapped_data = {}
@ -90,7 +94,7 @@ class Encryption(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 + '"') self.cli.executeCommand('echo \'' + text + '\' | gpg --symmetric --armor --batch --passphrase "' + password + '" -o "' + output_file + '"')
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:
@ -113,7 +117,7 @@ class Encryption(AbstractSplittedSecret):
self.encryptToJsonFile(data,file_path,self.master_password) self.encryptToJsonFile(data,file_path,self.master_password)
def encryptMainData(self): def encryptMainData(self):
self.executeCommand('tar -cvzf - "' + self.getDecryptedMainDataStandartFolder() + '" | gpg -c --batch --passphrase "' + self.master_password +'" > "' + self.getEncryptedMainDataFile() + '"'); self.cli.executeCommand('tar -cvzf - "' + self.getDecryptedMainDataStandartFolder() + '" | gpg -c --batch --passphrase "' + self.master_password +'" > "' + self.getEncryptedMainDataFile() + '"');
def encryptAll(self): def encryptAll(self):
self.encryptUserFile() self.encryptUserFile()

View File

@ -5,8 +5,10 @@ from classes.Decryption import Decryption
from getpass import getpass from getpass import getpass
from classes.AbstractSplittedSecret import AbstractSplittedSecret from classes.AbstractSplittedSecret import AbstractSplittedSecret
import traceback import traceback
from classes.Cli import Cli
cleanup = Cleanup() cli = Cli()
cleanup = Cleanup(cli)
def clean_exit(): def clean_exit():
print("Cleaning up.") print("Cleaning up.")
@ -56,7 +58,7 @@ try:
standard_exit() standard_exit()
if args.mode == 'decrypt': if args.mode == 'decrypt':
decrypt = Decryption() decrypt = Decryption(cli)
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:")
@ -137,7 +139,7 @@ try:
master_password = getpass() master_password = getpass()
else: else:
master_password = args.master_password master_password = args.master_password
encrypt = Encryption(args.amount_of_secret_holders, args.decryption_quota, master_password) encrypt = Encryption(cli,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']: