From ae9ae8b5105acfd59eea837d63c1820f95274855 Mon Sep 17 00:00:00 2001 From: JoaoBastos023 Date: Wed, 20 Nov 2024 20:49:40 +0000 Subject: [PATCH] adjusted client changes --- delivery1/client/bin/rep_activate_subject | 8 +++--- delivery1/client/bin/rep_add_doc | 26 +++++++++----------- delivery1/client/bin/rep_add_subject | 8 +++--- delivery1/client/bin/rep_create_org | 10 +++++--- delivery1/client/bin/rep_create_session | 8 +++--- delivery1/client/bin/rep_decrypt_file | 12 ++++----- delivery1/client/bin/rep_delete_doc | 6 ++--- delivery1/client/bin/rep_get_doc_file | 12 ++++----- delivery1/client/bin/rep_get_doc_metadata | 8 +++--- delivery1/client/bin/rep_get_file | 7 ++++-- delivery1/client/bin/rep_list_docs | 15 +++++------ delivery1/client/bin/rep_list_orgs | 4 ++- delivery1/client/bin/rep_list_subjects | 11 ++++++--- delivery1/client/bin/rep_subject_credentials | 4 +-- delivery1/client/bin/rep_suspend_subject | 10 +++++--- delivery1/client/bin/subject.py | 6 ++--- 16 files changed, 86 insertions(+), 69 deletions(-) diff --git a/delivery1/client/bin/rep_activate_subject b/delivery1/client/bin/rep_activate_subject index 388a764..cbc7854 100755 --- a/delivery1/client/bin/rep_activate_subject +++ b/delivery1/client/bin/rep_activate_subject @@ -31,11 +31,11 @@ def activateSubject(args): if not args.session or not args.username: logger.error("Need session file and username.") - sys.exit(-1) + sys.exit(1) if (not os.path.isfile(BASE_DIR + args.session)): logger.error("File '" + args.session + "' not found.") - sys.exit(-1) + sys.exit(1) # Get session file content with open(BASE_DIR + args.session, 'r') as f: @@ -46,7 +46,9 @@ def activateSubject(args): req.raise_for_status() except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) + + sys.exit(0) if __name__ == '__main__': activateSubject(sys.argv[1:]) \ No newline at end of file diff --git a/delivery1/client/bin/rep_add_doc b/delivery1/client/bin/rep_add_doc index 1fe73b6..a170ce3 100755 --- a/delivery1/client/bin/rep_add_doc +++ b/delivery1/client/bin/rep_add_doc @@ -36,26 +36,25 @@ def addDoc(args): if not args.session or not args.name or not args.file: logger.error("Need session file, document's name and file to upload.") - sys.exit(-1) + sys.exit(1) if (not os.path.isfile(os.path.join(BASE_DIR, args.session))): logger.error("File '" + args.session + "' not found.") - sys.exit(-1) + sys.exit(1) if (not os.path.isfile(os.path.join(BASE_DIR, args.file))): logger.error("File '" + args.file + "' not found") - sys.exit(-1) + sys.exit(1) #Get session file content with open(os.path.join(BASE_DIR, args.session), 'r') as f: args.session = json.load(f) - # Get hash of document's name for doc handle - doc_name = digest.get_hash(bytes(args.name, encoding='utf-8')) - + #Encrypt content + key, nonce = encrypt_file(BASE_DIR + args.file, BASE_DIR + 'encryptedText') #Upload document metadata - doc = {'document_name' : doc_name} + doc = {'document_name' : args.name, 'key' : key, 'alg' : 'AES-CFB', 'nonce' : nonce } try: req = requests.post(f'http://{state['REP_ADDRESS']}/file/upload', json=json.dumps(doc), headers={'Authorization': args.session['token']}) @@ -63,20 +62,19 @@ def addDoc(args): except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) #Upload Document content - #Create salt - salt = os.urandom(16) - - #Encrypt content - encrypt_file(salt, BASE_DIR + args.file, BASE_DIR + 'encryptedText') with open(BASE_DIR + 'encryptedText', 'rb') as f: content = f.read() + file = {'file' : open(BASE_DIR + args.file, 'rb')} + try: - req = requests.post(f'http://{state['REP_ADDRESS']}/file/upload', json=json.dumps({'dw' : content.hex()}), headers={'Authorization': args.session['token']}) + req = requests.post(f'http://{state['REP_ADDRESS']}/file/upload', + files=file, + headers={'Authorization': args.session['token'], 'File-Checksum' : content.hex()}) req.raise_for_status() except requests.exceptions.RequestException as errex: diff --git a/delivery1/client/bin/rep_add_subject b/delivery1/client/bin/rep_add_subject index 412c945..854b3de 100755 --- a/delivery1/client/bin/rep_add_subject +++ b/delivery1/client/bin/rep_add_subject @@ -36,11 +36,11 @@ def addSubject(args): if not args.session or not args.username or not args.name or not args.email or not args.credentials: logger.error("Need session file, username, name, email and credentials file.") - sys.exit(-1) + sys.exit(1) if (not os.path.isfile(BASE_DIR + args.session)): logger.error("File '" + args.session + "' not found.") - sys.exit(-1) + sys.exit(1) # Get session file content with open(BASE_DIR + args.session, 'r') as f: @@ -48,7 +48,7 @@ def addSubject(args): if (not os.path.isfile(BASE_DIR + args.credentials)): logger.error("File '" + args.file + "' not found") - sys.exit(-1) + sys.exit(1) subject = {'username' : args.username, 'name' : args.name, 'email' : args.email, 'credentials_file' : args.credentials} @@ -58,7 +58,7 @@ def addSubject(args): except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) sys.exit(0) diff --git a/delivery1/client/bin/rep_create_org b/delivery1/client/bin/rep_create_org index 9fd4831..ace0959 100755 --- a/delivery1/client/bin/rep_create_org +++ b/delivery1/client/bin/rep_create_org @@ -41,17 +41,17 @@ def createOrganization(args): if not args.org or not args.username or not args.name or not args.email or not args.pubkey: logger.error("Need organization, username, name, email and key file.") - sys.exit(-1) + sys.exit(1) # Validate email if not re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', args.email): logger.error("Need a valid email.") - sys.exit(-1) + sys.exit(1) # Validate key file if (not os.path.isfile(BASE_DIR + args.pubkey)): logger.error("File '" + args.pubkey + "' not found.") - sys.exit(-1) + sys.exit(1) # load public key from file pubKey = asymmetric_functs.load_public_key(BASE_DIR + args.pubkey) @@ -63,7 +63,9 @@ def createOrganization(args): req.raise_for_status() except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) + + sys.exit(0) if __name__ == '__main__': createOrganization(sys.argv[1:]) diff --git a/delivery1/client/bin/rep_create_session b/delivery1/client/bin/rep_create_session index 9ad9f24..eb12fc8 100755 --- a/delivery1/client/bin/rep_create_session +++ b/delivery1/client/bin/rep_create_session @@ -38,11 +38,11 @@ def createSession(args): if not args.org or not args.username or not args.password or not args.credentials or not args.session: logger.error("Need organization, username, password, credentials and session file") - sys.exit(-1) + sys.exit(1) if (not os.path.isfile(BASE_DIR + args.credentials)): logger.error("File '" + args.credentials + "' not found.") - sys.exit(-1) + sys.exit(1) session = {'org' : args.org, 'username' : args.username, 'password' : args.password, 'credentials_file' : args.credentials} @@ -53,10 +53,12 @@ def createSession(args): req.raise_for_status() except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server") - sys.exit(1) + sys.exit(-1) with open(BASE_DIR + args.session, 'w') as f: f.write(req.json()) + sys.exit(0) + if __name__ == '__main__': createSession(sys.argv[1:]) \ No newline at end of file diff --git a/delivery1/client/bin/rep_decrypt_file b/delivery1/client/bin/rep_decrypt_file index 8ebf6d2..7e90d6c 100755 --- a/delivery1/client/bin/rep_decrypt_file +++ b/delivery1/client/bin/rep_decrypt_file @@ -24,28 +24,28 @@ def decryptFile(args): args = parser.parse_args() - if len(args) != 2: + if not args.encrypted or not args.metadata: logger.error("Need encrypted file and it's metadata.") - sys.exit(-1) + sys.exit(1) # If first argument is not a file or not found if (not os.path.isfile(BASE_DIR + args.encrypted)): logger.error("File '" + args.encrypted + "' not found.") - sys.exit(-1) + sys.exit(1) if (not os.path.isfile(BASE_DIR + args.metadata)): logger.error("File '" + args.metadata + "' not found.") - sys.exit(-1) + sys.exit(1) #Decrypt file metadata = json.loads(BASE_DIR + args.metadata) - salt = metadata['salt'] - content = symmetric_encryption.decrypt_file(metadata['password'], args.encrypted) + content = symmetric_encryption.decrypt_file(args.encrypted) # Send decrypted content to stdout sys.stdout.write(content) + sys.exit(0) if __name__ == '__main__': decryptFile(sys.argv[1:]) diff --git a/delivery1/client/bin/rep_delete_doc b/delivery1/client/bin/rep_delete_doc index 4926e93..7f72d7b 100755 --- a/delivery1/client/bin/rep_delete_doc +++ b/delivery1/client/bin/rep_delete_doc @@ -32,11 +32,11 @@ def delDoc(args): if not args.session or not args.name: logger.error("Need session file and document's name.") - sys.exit(-1) + sys.exit(1) if (not os.path.isfile(BASE_DIR + args.session)): logger.error("File '" + args.session + "' not found.") - sys.exit(-1) + sys.exit(1) # Get session file content with open(BASE_DIR + args.session, 'r') as f: @@ -51,7 +51,7 @@ def delDoc(args): except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) sys.exit(0) diff --git a/delivery1/client/bin/rep_get_doc_file b/delivery1/client/bin/rep_get_doc_file index 67db4a8..1a0af30 100755 --- a/delivery1/client/bin/rep_get_doc_file +++ b/delivery1/client/bin/rep_get_doc_file @@ -9,6 +9,7 @@ import argparse from subject import main from lib import digest +from lib import symmetric_encryption logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() @@ -65,24 +66,23 @@ def getDoc(args): file.raise_for_status() except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) file = file.json() if not digest.get_hash(file) == metadata['file_handle']: logger.error("Files integrity is lost.") - sys.exit(1) + sys.exit(-1) - key, alg = metadata['key'], metadata['alg'] - salt, passwd = alg['salt'], alg['password'] + content = symmetric_encryption.decrypt_file(file) if args.output: with open(BASE_DIR + args.output, 'w') as f: - f.write(file) + f.write(content) else: - sys.stdout.write(file) + sys.stdout.write(content) diff --git a/delivery1/client/bin/rep_get_doc_metadata b/delivery1/client/bin/rep_get_doc_metadata index d84b10e..bf5c6a8 100755 --- a/delivery1/client/bin/rep_get_doc_metadata +++ b/delivery1/client/bin/rep_get_doc_metadata @@ -33,11 +33,11 @@ def getDocMetadata(args): if not args.session or not args.name: logger.error("Need session file and document's name.") - sys.exit(-1) + sys.exit(1) if (not os.path.isfile(BASE_DIR + args.session)): logger.error("File '" + args.session + "' not found.") - sys.exit(-1) + sys.exit(1) # Get session file content with open(BASE_DIR + args.session, 'r') as f: @@ -51,11 +51,13 @@ def getDocMetadata(args): except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) metadata = metadata.json() sys.stdout.write(metadata) + sys.exit(0) + if __name__ == '__main__': getDocMetadata(sys.argv[1:]) \ No newline at end of file diff --git a/delivery1/client/bin/rep_get_file b/delivery1/client/bin/rep_get_file index 1b0e573..f81cac8 100755 --- a/delivery1/client/bin/rep_get_file +++ b/delivery1/client/bin/rep_get_file @@ -36,10 +36,11 @@ def getFile(args): if not args.filehandle: logger.error("Need a file handle.") - sys.exit(-1) + 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: @@ -47,7 +48,7 @@ def getFile(args): file.raise_for_status() except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) file = file.json() @@ -57,5 +58,7 @@ def getFile(args): with open(BASE_DIR + args.file, "wb") as f: f.write(file) + sys.exit(0) + if __name__ == '__main__': getFile(sys.argv[1:]) \ No newline at end of file diff --git a/delivery1/client/bin/rep_list_docs b/delivery1/client/bin/rep_list_docs index a675d00..a3be21d 100755 --- a/delivery1/client/bin/rep_list_docs +++ b/delivery1/client/bin/rep_list_docs @@ -46,10 +46,10 @@ def list_docs(args): if args.session: if (not os.path.isfile(BASE_DIR + args.session)): logger.error("File '" + args.session + "' not found.") - sys.exit(-1) + sys.exit(1) else: logger.error("Need session file.") - sys.exit(-1) + sys.exit(1) # Get session file content with open(BASE_DIR + args.session, 'r') as f: @@ -68,7 +68,7 @@ def list_docs(args): except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) subjects = subjects.json() @@ -86,7 +86,7 @@ def list_docs(args): except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) subjects = subjects.json() @@ -104,24 +104,25 @@ def list_docs(args): except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) subjects = subjects.json() else: try: - subjects = requests.get(f'http://{state['REP_ADDRESS']}/file/list', headers={'Authorization': args.session['token']}) + subjects = requests.get(f'http://{state['REP_ADDRESS']}/file/list', json=json.dumps({}), headers={'Authorization': args.session['token']}) subjects.raise_for_status() except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) subjects = subjects.json() for s in subjects: sys.stdout.write(s['id'] + " - " + s['username']) + sys.exit(0) if __name__ == '__main__': list_docs(sys.argv[1:]) \ No newline at end of file diff --git a/delivery1/client/bin/rep_list_orgs b/delivery1/client/bin/rep_list_orgs index 13f268d..22ab79b 100755 --- a/delivery1/client/bin/rep_list_orgs +++ b/delivery1/client/bin/rep_list_orgs @@ -23,10 +23,12 @@ def listOrganizations(): except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) for org in orgs.json(): sys.stdout.write(str(org['id']) + " - " + org['name']) + sys.exit(0) + if __name__ == '__main__': listOrganizations() \ No newline at end of file diff --git a/delivery1/client/bin/rep_list_subjects b/delivery1/client/bin/rep_list_subjects index 1d2734d..6733da8 100755 --- a/delivery1/client/bin/rep_list_subjects +++ b/delivery1/client/bin/rep_list_subjects @@ -34,10 +34,10 @@ def list_subjects(args): if args.session: if (not os.path.isfile(BASE_DIR + args.session)): logger.error("File '" + args.session + "' not found.") - sys.exit(-1) + sys.exit(1) else: logger.error("Need session file.") - sys.exit(-1) + sys.exit(1) # Get session file content with open(BASE_DIR + args.session, 'r') as f: @@ -52,21 +52,24 @@ def list_subjects(args): except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) else: try: subjects = requests.get(f'https://{state['REP_ADDRESS']}/user/list', + json=json.dumps({}), headers={'Authorization': args.session['token']}) subjects.raise_for_status() except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) for s in subjects.json(): sys.stdout.write(s['id'] + " - " + s['username']) + sys.exit(0) + if __name__ == '__main__': list_subjects(sys.argv[1:]) \ No newline at end of file diff --git a/delivery1/client/bin/rep_subject_credentials b/delivery1/client/bin/rep_subject_credentials index a446031..b020445 100755 --- a/delivery1/client/bin/rep_subject_credentials +++ b/delivery1/client/bin/rep_subject_credentials @@ -27,13 +27,13 @@ def generateKeyPair(args): if not args.password or not args.pubfile or not args.privfile: logger.error("Need password and file to store keys") - sys.exit(-1) + sys.exit(1) #Generate the key pair key_pair.generate_key_pair(BASE_DIR + args.pubfile, BASE_DIR + args.privfile, 2048, args.password) - return 0 + sys.exit(0) if __name__ == '__main__': generateKeyPair(sys.argv[1:]) diff --git a/delivery1/client/bin/rep_suspend_subject b/delivery1/client/bin/rep_suspend_subject index 003e4ce..daef1cb 100755 --- a/delivery1/client/bin/rep_suspend_subject +++ b/delivery1/client/bin/rep_suspend_subject @@ -30,13 +30,13 @@ def suspendSubject(args): args = parser.parse_args() - if len(args) != 2: + if not args.session or not args.username: logger.error("Need session file and username.") - sys.exit(-1) + sys.exit(1) if (not os.path.isfile(BASE_DIR + args.session)): logger.error("File '" + args.session + "' not found.") - sys.exit(-1) + sys.exit(1) # Get session file content with open(BASE_DIR + args.session, 'r') as f: @@ -47,7 +47,9 @@ def suspendSubject(args): req.raise_for_status() except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") - sys.exit(1) + sys.exit(-1) + + sys.exit(0) if __name__ == '__main__': suspendSubject(sys.argv[1:]) \ No newline at end of file diff --git a/delivery1/client/bin/subject.py b/delivery1/client/bin/subject.py index fc4e915..94cf766 100644 --- a/delivery1/client/bin/subject.py +++ b/delivery1/client/bin/subject.py @@ -66,7 +66,7 @@ def parse_args(state): if args.key: if not os.path.exists(args.key[0]) or not os.path.isfile(args.key[0]): logger.error(f'Key file not found or invalid: {args.key[0]}') - sys.exit(-1) + sys.exit(1) with open(args.key[0], 'r') as f: state['REP_PUB_KEY'] = f.read() @@ -102,11 +102,11 @@ def main(args): if 'REP_ADDRESS' not in state: logger.error("Must define Repository Address") - sys.exit(-1) + sys.exit(1) # if 'REP_PUB_KEY' not in state: # logger.error("Must set the Repository Public Key") - # sys.exit(-1) + # sys.exit(1) save(state)