From 97c1236f44db613618d58f1b3cc1af54a9b001c7 Mon Sep 17 00:00:00 2001 From: Tiago Garcia Date: Wed, 18 Dec 2024 14:41:51 +0000 Subject: [PATCH] Fix file retrieve Signed-off-by: Tiago Garcia --- delivery2/client/bin/rep_decrypt_file | 8 -------- delivery2/client/bin/rep_get_doc_file | 8 ++++++-- delivery2/client/bin/rep_get_file | 7 ------- delivery2/client/tests/test_client.py | 10 +++++++--- delivery2/lib/__init__.py | 4 ++++ delivery2/lib/symmetric_encryption.py | 5 ++++- 6 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 delivery2/lib/__init__.py diff --git a/delivery2/client/bin/rep_decrypt_file b/delivery2/client/bin/rep_decrypt_file index 7516f60..90bb5f9 100755 --- a/delivery2/client/bin/rep_decrypt_file +++ b/delivery2/client/bin/rep_decrypt_file @@ -34,16 +34,8 @@ def decryptFile(args): logger.error("File '" + args.encrypted + "' not found.") sys.exit(1) - # if (not os.path.isfile(BASE_DIR + args.metadata)): - # logger.error("File '" + args.metadata + "' not found.") - # sys.exit(1) - - #Decrypt file - # print(args.metadata) metadata = json.loads(args.metadata) - print(BASE_DIR + args.encrypted) - content = symmetric_encryption.decrypt_file(bytes.fromhex(metadata['key']), BASE_DIR + args.encrypted) with open(BASE_DIR + 'decryped_file.txt', 'w') as f: diff --git a/delivery2/client/bin/rep_get_doc_file b/delivery2/client/bin/rep_get_doc_file index 04eb973..1ba99bc 100755 --- a/delivery2/client/bin/rep_get_doc_file +++ b/delivery2/client/bin/rep_get_doc_file @@ -69,13 +69,17 @@ def getDoc(args): logger.error("Failed to obtain response from server.") sys.exit(-1) - file = file.json() + file = file.content if not digest.get_hash(file) == metadata['file_handle']: logger.error("Files integrity is lost.") sys.exit(-1) - content = symmetric_encryption.decrypt_file(file) + with open(BASE_DIR + 'encrypted_file', 'wb') as f: + f.write(file) + + content = symmetric_encryption.decrypt_file(bytes.fromhex(metadata['key']), BASE_DIR + 'encrypted_file') + os.remove(BASE_DIR + 'encrypted_file') if args.output: with open(BASE_DIR + args.output, 'w') as f: diff --git a/delivery2/client/bin/rep_get_file b/delivery2/client/bin/rep_get_file index fe8a95d..a45f382 100755 --- a/delivery2/client/bin/rep_get_file +++ b/delivery2/client/bin/rep_get_file @@ -37,12 +37,7 @@ def getFile(args): if not args.filehandle: logger.error("Need a file handle.") sys.exit(1) - #else: - # if not os.path.isfile(BASE_DIR + args.filehandle): - # logger.error("File '" + args.filehandle + "' not found" ) - # sys.exit(1) - #Get file try: file = requests.get(f'http://{state['REP_ADDRESS']}/file/get/' + args.filehandle + '/content') file.raise_for_status() @@ -50,8 +45,6 @@ def getFile(args): logger.error("Failed to obtain response from server.") sys.exit(-1) - print(file) - if not args.file: sys.stdout.write(file.text) else: diff --git a/delivery2/client/tests/test_client.py b/delivery2/client/tests/test_client.py index 8cc5b42..839aa42 100644 --- a/delivery2/client/tests/test_client.py +++ b/delivery2/client/tests/test_client.py @@ -112,19 +112,23 @@ def test_rep_get_file(): def test_decrypt_file(): # Test the rep_decrypt_file command - process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_decrypt_file file.txt '{json.dumps(metadata)}'", shell=True) + encryption_metadata = { + 'alg': metadata['alg'], + 'key': metadata['key'] + } + process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_decrypt_file file.txt '{json.dumps(encryption_metadata)}'", shell=True) process.wait() assert process.returncode == 0 def test_rep_get_doc_file(): # Test the rep_get_doc_file - process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_get_doc_file session.json doc ", shell=True) + process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_get_doc_file session.json doc file.txt", shell=True) process.wait() assert process.returncode == 0 def test_rep_delete_doc(): # Test the rep_get_doc_file - process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_delete_doc session.json doc ", shell=True) + process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_delete_doc session.json doc", shell=True) process.wait() assert process.returncode == 0 diff --git a/delivery2/lib/__init__.py b/delivery2/lib/__init__.py new file mode 100644 index 0000000..1b2fe5b --- /dev/null +++ b/delivery2/lib/__init__.py @@ -0,0 +1,4 @@ +from .asymmetric_functs import * +from .symmetric_encryption import * +from .digest import * +from .key_pair import * \ No newline at end of file diff --git a/delivery2/lib/symmetric_encryption.py b/delivery2/lib/symmetric_encryption.py index ece52ff..811e8b5 100644 --- a/delivery2/lib/symmetric_encryption.py +++ b/delivery2/lib/symmetric_encryption.py @@ -62,5 +62,8 @@ def decrypt_file(key, input_file, output_file=None) -> str: # Finalize decryption plaintext_content += decryptor.finalize() - return plaintext_content.decode('utf-8', errors='ignore') + try: + return plaintext_content.decode() + except UnicodeDecodeError: + return plaintext_content