From b4fa815ba9087117a956811c5033c1095d1cf20e Mon Sep 17 00:00:00 2001 From: JoaoBastos023 Date: Tue, 12 Nov 2024 10:26:56 +0000 Subject: [PATCH] Initial Subject Structure --- delivery1/client/bin/rep_create_org | 43 ++++++++++++++++++++ delivery1/client/bin/rep_create_session | 24 +++++++++++ delivery1/client/bin/rep_decrypt_file | 37 +++++++++++++++++ delivery1/client/bin/rep_get_file | 42 +++++++++++++++++++ delivery1/client/bin/rep_list_orgs | 29 +++++++++++++ delivery1/client/bin/rep_subject_credentials | 31 ++++++++++++++ 6 files changed, 206 insertions(+) create mode 100755 delivery1/client/bin/rep_create_org create mode 100755 delivery1/client/bin/rep_create_session create mode 100755 delivery1/client/bin/rep_decrypt_file create mode 100755 delivery1/client/bin/rep_get_file create mode 100755 delivery1/client/bin/rep_list_orgs create mode 100755 delivery1/client/bin/rep_subject_credentials diff --git a/delivery1/client/bin/rep_create_org b/delivery1/client/bin/rep_create_org new file mode 100755 index 0000000..1ebf7c8 --- /dev/null +++ b/delivery1/client/bin/rep_create_org @@ -0,0 +1,43 @@ +#!/bin/python3 +import os +import sys +import logging +import requests +import json +import re + +logging.basicConfig(format='%(levelname)s\t- %(message)s') +logger = logging.getLogger() +logger.setLevel(logging.INFO) + +state = {} + +# Create organization +def createOrganization(args): + + # {'name', 'username' , 'full_name', 'email' , 'public_key' } + + if len(args) != 5: + logger.error("Missing parameters. Expected: 'name' 'username' 'full name' 'email' 'public key' ") + sys.exit(-1) + + for item in args: + if item == '': + logger.error("Need a valid " + item) + sys.exit(-1) + + input = {'name' : args[0], 'username' : args[1], 'full_name' : args[2], 'email' : args[3], 'public_key' : args[4]} + + if not re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', input['email']): + logger.error("Need a valid email.") + sys.exit(-1) + + if (not os.path.isfile(input['public_key'])): + logger.error("File '" + input['public_key'] + "' not found.") + sys.exit(-1) + + req = requests.post(f'http://{state['REP_ADDRESS']}/organization/create', json=json.dumps(input)) + + +if __name__ == '__main__': + createOrganization(sys.argv[1:]) diff --git a/delivery1/client/bin/rep_create_session b/delivery1/client/bin/rep_create_session new file mode 100755 index 0000000..7600017 --- /dev/null +++ b/delivery1/client/bin/rep_create_session @@ -0,0 +1,24 @@ +#!/bin/python3 +import os +import sys +import argparse +import logging +import json +import requests +import re +from getpass import getpass + +# Identity attributes +# {'username' : '', 'full_name' : '', 'email' : '', public_key : '' } + +logging.basicConfig(format='%(levelname)s\t- %(message)s') +logger = logging.getLogger() +logger.setLevel(logging.INFO) + +state = {} + +def createSession(args): + pass + +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 new file mode 100755 index 0000000..db42d3d --- /dev/null +++ b/delivery1/client/bin/rep_decrypt_file @@ -0,0 +1,37 @@ +#!/bin/python3 +import os +import sys +import logging + +logging.basicConfig(format='%(levelname)s\t- %(message)s') +logger = logging.getLogger() +logger.setLevel(logging.INFO) + +#send to stdout contents of decrypted file +# encrypted file - encryption metadata +def decryptFile(args): + if len(args) != 3: + logger.error("Need encrypted file and it's metadata.") + sys.exit(-1) + + # If first argument is not a file or not found + if (not os.path.isfile(args[1])): + logger.error("File '" + args[1] + "' not found.") + sys.exit(-1) + + if (not os.path.isfile(args[2])): + logger.error("File '" + args[2] + "' not found.") + sys.exit(-1) + + #Get private key to decrypt + privateKey = '' + + #Decrypt file + content = 'decrypt(privateKey, args[1])' + + # Send decrypted content to stdout + sys.stdout.write(content) + + +if __name__ == '__main__': + decryptFile(sys.argv) diff --git a/delivery1/client/bin/rep_get_file b/delivery1/client/bin/rep_get_file new file mode 100755 index 0000000..c314236 --- /dev/null +++ b/delivery1/client/bin/rep_get_file @@ -0,0 +1,42 @@ +#!/bin/python3 +import os +import sys +import logging +import json +import requests + +# Identity attributes +# {'username' : '', 'full_name' : '', 'email' : '', public_key : '' } + +logging.basicConfig(format='%(levelname)s\t- %(message)s') +logger = logging.getLogger() +logger.setLevel(logging.INFO) + +state = {} + +#get file +#file handle - file +def getFile(args): + + if len(args) < 1: + logger.error("Need a file handle.") + sys.exit(-1) + + if len(args) == 2: + if (not os.path.isfile(args[1])): + logger.error("File '" + args[1] + "' not found.") + sys.exit(-1) + + file = json.loads(requests.get(f'http://{state['REP_ADDRESS']}/file'), params = {"file_handle" : args[0]}) + + # decrypt file + content = '' #decrypt(file.encode('utf-8')) + + if len(args) == 1: + sys.stdout.write(content) + else: + with open(args[1], "wb") as f: + f.write(content) + +if __name__ == '__main__': + getFile(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 new file mode 100755 index 0000000..ddcef62 --- /dev/null +++ b/delivery1/client/bin/rep_list_orgs @@ -0,0 +1,29 @@ +#!/bin/python3 +import sys +import logging +import json +import requests + +# Identity attributes +# {'username' : '', 'full_name' : '', 'email' : '', public_key : '' } + +logging.basicConfig(format='%(levelname)s\t- %(message)s') +logger = logging.getLogger() +logger.setLevel(logging.INFO) + +state = {} + +def listOrganizations(): + try: + orgs = json.loads(requests.get(f'http://{state['REP_ADDRESS']}/organization/list')) + orgs.raise_for_status() + + except requests.exceptions.RequestException as errex: + logger.error("Failed to obtain response from server.") + sys.exit(1) + + for org in orgs: + sys.stdout.write(org['id'] + " - " + org['name']) + +if __name__ == '__main__': + listOrganizations(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 new file mode 100755 index 0000000..d9f40de --- /dev/null +++ b/delivery1/client/bin/rep_subject_credentials @@ -0,0 +1,31 @@ +#!/bin/python3 +import sys +import logging + +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 +def generateKeyPair(args): + + if len(args) != 3: + logger.error("Need password and file to store keys") + sys.exit(-1) + + + #Generate the key pair + keyPair = '' + + #Get the 2 different keys + pubKey = '' + privateKey = '' + + with open(args[2], "wb") as f: + f.write(pubKey.encode() + b"\n\n" + privateKey.encode()) + + +if __name__ == '__main__': + generateKeyPair(sys.argv) +