adjusted client changes

This commit is contained in:
JoaoBastos023 2024-11-20 20:49:40 +00:00
parent 583e804342
commit ae9ae8b510
16 changed files with 86 additions and 69 deletions

View File

@ -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:])

View File

@ -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:

View File

@ -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)

View File

@ -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:])

View File

@ -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:])

View 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:])

View File

@ -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)

View 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)

View File

@ -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:])

View 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:])

View File

@ -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:])

View File

@ -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()

View File

@ -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:])

View File

@ -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:])

View File

@ -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:])

View File

@ -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)