From a27348567f6b9db947a5ca7185f912999123579c Mon Sep 17 00:00:00 2001 From: JoaoBastos023 Date: Sun, 17 Nov 2024 20:18:02 +0000 Subject: [PATCH] Updated client functs with state --- delivery1/client/bin/rep_activate_subject | 9 +++++- delivery1/client/bin/rep_add_doc | 13 +++++++-- delivery1/client/bin/rep_add_subject | 11 ++++++-- delivery1/client/bin/rep_create_org | 13 +++++++-- delivery1/client/bin/rep_create_session | 10 +++++-- delivery1/client/bin/rep_delete_doc | 9 +++++- delivery1/client/bin/rep_get_doc_file | 9 +++++- delivery1/client/bin/rep_get_doc_metadata | 12 +++++++- delivery1/client/bin/rep_get_file | 9 +++++- delivery1/client/bin/rep_list_docs | 9 +++++- delivery1/client/bin/rep_list_orgs | 6 +++- delivery1/client/bin/rep_list_subjects | 9 +++++- delivery1/client/bin/rep_subject_credentials | 21 ++++++-------- delivery1/client/bin/rep_suspend_subject | 11 ++++++-- delivery1/client/subject.py | 29 ++++++++++---------- 15 files changed, 133 insertions(+), 47 deletions(-) mode change 100644 => 100755 delivery1/client/bin/rep_activate_subject mode change 100644 => 100755 delivery1/client/bin/rep_add_doc mode change 100644 => 100755 delivery1/client/bin/rep_add_subject mode change 100644 => 100755 delivery1/client/bin/rep_delete_doc mode change 100644 => 100755 delivery1/client/bin/rep_get_doc_file mode change 100644 => 100755 delivery1/client/bin/rep_get_doc_metadata mode change 100644 => 100755 delivery1/client/bin/rep_list_subjects mode change 100644 => 100755 delivery1/client/bin/rep_suspend_subject diff --git a/delivery1/client/bin/rep_activate_subject b/delivery1/client/bin/rep_activate_subject old mode 100644 new mode 100755 index 4e31156..d114190 --- a/delivery1/client/bin/rep_activate_subject +++ b/delivery1/client/bin/rep_activate_subject @@ -6,16 +6,23 @@ import requests import json import argparse +sys.path.append(os.path.abspath("../")) +from subject import main + logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) #session file - username def activateSubject(args): parser = argparse.ArgumentParser() + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('session', nargs='?', default=None) parser.add_argument('username', nargs='?',default=None) diff --git a/delivery1/client/bin/rep_add_doc b/delivery1/client/bin/rep_add_doc old mode 100644 new mode 100755 index 4fb31a9..730fd95 --- a/delivery1/client/bin/rep_add_doc +++ b/delivery1/client/bin/rep_add_doc @@ -6,18 +6,25 @@ import requests import json import argparse -from cryptLib import encryptionFuncts +sys.path.append(os.path.abspath("../")) +from subject import main + +from lib import encryption_functs logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) # session file - document name - file def addDoc(args): parser = argparse.ArgumentParser() + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('session', nargs='?', default=None) parser.add_argument('name', nargs='?', default=None) parser.add_argument('file', nargs='?', default=None) @@ -41,7 +48,7 @@ def addDoc(args): with open(args.file, 'rb') as f: content = f.read() - content = encryptionFuncts.encryptFile(state['REP_PUB_KEY'], content) + content = encryption_functs.encrypt_file(state['REP_PUB_KEY'], content) doc = {'name' : args.name, 'content' : content} diff --git a/delivery1/client/bin/rep_add_subject b/delivery1/client/bin/rep_add_subject old mode 100644 new mode 100755 index 840bcdf..cb47a7c --- a/delivery1/client/bin/rep_add_subject +++ b/delivery1/client/bin/rep_add_subject @@ -6,18 +6,25 @@ import requests import json import argparse -from cryptLib import encryptionFuncts +sys.path.append(os.path.abspath("../")) +from subject import main + +from lib import encryption_functs logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) # session file - username - name - email - credentials file def addSubject(args): parser = argparse.ArgumentParser() + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('session', nargs='?', default=None) parser.add_argument('username', nargs='?', default=None) parser.add_argument('name', nargs='?', default=None) diff --git a/delivery1/client/bin/rep_create_org b/delivery1/client/bin/rep_create_org index 4ee1be0..e1d723c 100755 --- a/delivery1/client/bin/rep_create_org +++ b/delivery1/client/bin/rep_create_org @@ -7,7 +7,10 @@ import json import re import argparse -from cryptLib import encryptionFuncts +sys.path.append(os.path.abspath("../")) +from subject import main + +from lib import encryption_functs @@ -15,7 +18,7 @@ logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) # Create organization # organization - username - name - email - public key file @@ -23,6 +26,10 @@ def createOrganization(args): parser = argparse.ArgumentParser() + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('org', nargs='?', default=None) parser.add_argument('username', nargs='?', default=None) parser.add_argument('name', nargs='?', default=None) @@ -47,7 +54,7 @@ def createOrganization(args): sys.exit(-1) # load public key from file - pubKey = encryptionFuncts.load_public_key(args.key) + pubKey = encryption_functs.load_public_key(args.key) input = {'name' : args.org, 'username' : args.username, 'full_name' : args.name, 'email' : args.email, 'public_key' : pubKey} diff --git a/delivery1/client/bin/rep_create_session b/delivery1/client/bin/rep_create_session index 667985c..78e9a61 100755 --- a/delivery1/client/bin/rep_create_session +++ b/delivery1/client/bin/rep_create_session @@ -5,7 +5,9 @@ import argparse import logging import json import requests -import re + +sys.path.append(os.path.abspath("../")) +from subject import main # Identity attributes # {'username' : '', 'full_name' : '', 'email' : '', public_key : '' } @@ -14,13 +16,17 @@ logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) # org - username - password - credentials file - session file def createSession(args): parser = argparse.ArgumentParser() + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('org', nargs='?', default=None) parser.add_argument('username', nargs='?', default=None) parser.add_argument('password', nargs='?', default=None) diff --git a/delivery1/client/bin/rep_delete_doc b/delivery1/client/bin/rep_delete_doc old mode 100644 new mode 100755 index 34c1805..2a734df --- a/delivery1/client/bin/rep_delete_doc +++ b/delivery1/client/bin/rep_delete_doc @@ -6,16 +6,23 @@ import requests import json import argparse +sys.path.append(os.path.abspath("../")) +from subject import main + logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) # session file - document name def delDoc(args): parser = argparse.ArgumentParser() + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('session', nargs='?', default=None) parser.add_argument('name', nargs='?', default=None) diff --git a/delivery1/client/bin/rep_get_doc_file b/delivery1/client/bin/rep_get_doc_file old mode 100644 new mode 100755 index 5a9f25c..6e8f860 --- a/delivery1/client/bin/rep_get_doc_file +++ b/delivery1/client/bin/rep_get_doc_file @@ -6,16 +6,23 @@ import requests import json import argparse +sys.path.append(os.path.abspath("../")) +from subject import main + logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) # session file - document name - output file(optional) def getDoc(args): parser = argparse.ArgumentParser() + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('session', nargs='?', default=None) parser.add_argument('name', nargs='?', default=None) parser.add_argument('output', nargs='?', default=None) diff --git a/delivery1/client/bin/rep_get_doc_metadata b/delivery1/client/bin/rep_get_doc_metadata old mode 100644 new mode 100755 index b5edaf1..565df25 --- a/delivery1/client/bin/rep_get_doc_metadata +++ b/delivery1/client/bin/rep_get_doc_metadata @@ -6,16 +6,26 @@ import requests import json import argparse +sys.path.append(os.path.abspath("../")) +from subject import main + logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) + +print(state) # session file - document name def getDocMetadata(args): parser = argparse.ArgumentParser() + + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('session', nargs='?', default=None) parser.add_argument('name', nargs='?', default=None) diff --git a/delivery1/client/bin/rep_get_file b/delivery1/client/bin/rep_get_file index 74e5e15..40f0d1a 100755 --- a/delivery1/client/bin/rep_get_file +++ b/delivery1/client/bin/rep_get_file @@ -6,6 +6,9 @@ import json import requests import argparse +sys.path.append(os.path.abspath("../")) +from subject import main + # Identity attributes # {'username' : '', 'full_name' : '', 'email' : '', public_key : '' } @@ -13,13 +16,17 @@ logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) #get file #file handle - file(optonal) def getFile(args): parser = argparse.ArgumentParser() + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('filehandle', nargs='?', default=None) parser.add_argument('file', nargs='?', default=None) diff --git a/delivery1/client/bin/rep_list_docs b/delivery1/client/bin/rep_list_docs index 9a1a1d5..bee43e3 100755 --- a/delivery1/client/bin/rep_list_docs +++ b/delivery1/client/bin/rep_list_docs @@ -7,11 +7,14 @@ import json import argparse import datetime +sys.path.append(os.path.abspath("../")) +from subject import main + logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) def validDate(date): try: @@ -24,6 +27,10 @@ def validDate(date): def list_docs(args): parser = argparse.ArgumentParser() + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('session', nargs='?', default=None) parser.add_argument("-s", '--username', nargs=1, help="Username") parser.add_argument("-dnt", '--newerThan', help="Date new than") diff --git a/delivery1/client/bin/rep_list_orgs b/delivery1/client/bin/rep_list_orgs index ddcef62..135c742 100755 --- a/delivery1/client/bin/rep_list_orgs +++ b/delivery1/client/bin/rep_list_orgs @@ -1,9 +1,13 @@ #!/bin/python3 +import os import sys import logging import json import requests +sys.path.append(os.path.abspath("../")) +from subject import main + # Identity attributes # {'username' : '', 'full_name' : '', 'email' : '', public_key : '' } @@ -11,7 +15,7 @@ logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) def listOrganizations(): try: diff --git a/delivery1/client/bin/rep_list_subjects b/delivery1/client/bin/rep_list_subjects old mode 100644 new mode 100755 index eec0b7d..e3f4160 --- a/delivery1/client/bin/rep_list_subjects +++ b/delivery1/client/bin/rep_list_subjects @@ -6,17 +6,24 @@ import requests import json import argparse +sys.path.append(os.path.abspath("../")) +from subject import main + logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) # session file - username(optional) def list_subjects(args): parser = argparse.ArgumentParser() + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('session', nargs='?', default=None) parser.add_argument('username', nargs='?', default=None) diff --git a/delivery1/client/bin/rep_subject_credentials b/delivery1/client/bin/rep_subject_credentials index 17d9f78..731fae6 100755 --- a/delivery1/client/bin/rep_subject_credentials +++ b/delivery1/client/bin/rep_subject_credentials @@ -3,39 +3,34 @@ import sys import logging import argparse -from cryptLib import keyPair +from lib import key_pair logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) # Generate a key pair for a subject -# password - file for keys +# password - file for public key, file for private key def generateKeyPair(args): parser = argparse.ArgumentParser() parser.add_argument('password', nargs='?', default=None) - parser.add_argument('file', nargs='?', default=None) + parser.add_argument('pubfile', nargs='?', default=None) + parser.add_argument('privfile', nargs='?', default=None) args = parser.parse_args() - if len(args) != 2: + if len(args) != 3: logger.error("Need password and file to store keys") sys.exit(-1) #Generate the key pair - keys = keyPair.generate_key_pair(args.password) - - #Get the 2 different keys - pubKey = '' - privateKey = '' - - with open(args.file, "wb") as f: - f.write(pubKey.encode() + b"\n\n" + privateKey.encode()) + key_pair.generate_key_pair(args.pubfile, args.privfile, 2048, args.password) + return 0 if __name__ == '__main__': - generateKeyPair(sys.argv) + generateKeyPair(sys.argv[1:]) diff --git a/delivery1/client/bin/rep_suspend_subject b/delivery1/client/bin/rep_suspend_subject old mode 100644 new mode 100755 index 61c1b25..6708ce5 --- a/delivery1/client/bin/rep_suspend_subject +++ b/delivery1/client/bin/rep_suspend_subject @@ -6,16 +6,23 @@ import requests import json import argparse +sys.path.append(os.path.abspath("../")) +from subject import main + logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) -state = {} +state = main(sys.argv) # session file - username def suspendSubject(args): parser = argparse.ArgumentParser() + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + parser.add_argument('session', nargs='?', default=None) parser.add_argument('username', nargs='?',default=None) @@ -33,4 +40,4 @@ def suspendSubject(args): req = requests.post(f'http://{state['REP_ADDRESS']}/subject/', json=json.dumps(subject)) if __name__ == '__main__': - suspendSubject(sys.argv) \ No newline at end of file + suspendSubject(sys.argv[1:]) \ No newline at end of file diff --git a/delivery1/client/subject.py b/delivery1/client/subject.py index 31a7376..8d34ffc 100644 --- a/delivery1/client/subject.py +++ b/delivery1/client/subject.py @@ -51,13 +51,13 @@ def parse_args(state): parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") - # parser.add_argument("-c", "--command", help="Command to execute") - # parser.add_argument('arg0', nargs='?', default=None) - # parser.add_argument('arg1', nargs='?', default=None) - # parser.add_argument('arg2', nargs='?', default=None) - # parser.add_argument('arg3', nargs='?', default=None) - # parser.add_argument('arg4', nargs='?', default=None) - # parser.add_argument('arg5', nargs='?', default=None) + parser.add_argument("-c", "--command", help="Command to execute") + parser.add_argument('arg0', nargs='?', default=None) + parser.add_argument('arg1', nargs='?', default=None) + parser.add_argument('arg2', nargs='?', default=None) + parser.add_argument('arg3', nargs='?', default=None) + parser.add_argument('arg4', nargs='?', default=None) + parser.add_argument('arg5', nargs='?', default=None) args = parser.parse_args() if args.verbose: @@ -77,10 +77,10 @@ def parse_args(state): state['REP_ADDRESS'] = args.repo[0] logger.info('Overriding REP_ADDRESS from command line') - if args.command: - logger.info("Command: " + args.command) + #if args.command: + # logger.info("Command: " + args.command) - return state, {'command': args.command, 'arg0': args.arg0, 'arg1': args.arg1, 'arg2': args.arg2, 'arg3': args.arg3, 'arg4': args.arg4, 'arg5': args.arg5} + return state#, {'command': args.command, 'arg0': args.arg0, 'arg1': args.arg1, 'arg2': args.arg2, 'arg3': args.arg3, 'arg4': args.arg4, 'arg5': args.arg5} def save(state): state_dir = os.path.join(os.path.expanduser('~'), '.sio') @@ -98,11 +98,12 @@ def main(args): state = load_state() state = parse_env(state) - state, args = parse_args(state) + #state, args = parse_args(state) + state = parse_args(state) - # if 'REP_ADDRESS' not in state: - # logger.error("Must define Repository Address") - # sys.exit(-1) + if 'REP_ADDRESS' not in state: + logger.error("Must define Repository Address") + sys.exit(-1) # if 'REP_PUB_KEY' not in state: # logger.error("Must set the Repository Public Key")