#!/bin/python3 import os import sys import logging import requests import json import re import argparse from cryptLib import encryptionFuncts logging.basicConfig(format='%(levelname)s\t- %(message)s') logger = logging.getLogger() logger.setLevel(logging.INFO) state = {} # Create organization # organization - username - name - email - public key file def createOrganization(args): parser = argparse.ArgumentParser() parser.add_argument('org', nargs='?', default=None) parser.add_argument('username', nargs='?', default=None) parser.add_argument('name', nargs='?', default=None) parser.add_argument('email', nargs='?', default=None) parser.add_argument('key', nargs='?', default=None) args = parser.parse_args() #Validate num of arguments if len(args) != 5: logger.error("Need organization, username, name, email and key file.") 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) # Validate key file if (not os.path.isfile(args.key)): logger.error("File '" + args.key + "' not found.") sys.exit(-1) # load public key from file pubKey = encryptionFuncts.load_public_key(args.key) input = {'name' : args.org, 'username' : args.username, 'full_name' : args.name, 'email' : args.email, 'public_key' : pubKey} try: req = requests.post(f'http://{state['REP_ADDRESS']}/organization/create', json=json.dumps(input)) req.raise_for_status() except requests.exceptions.RequestException as errex: logger.error("Failed to obtain response from server.") sys.exit(1) if __name__ == '__main__': createOrganization(sys.argv[1:])