2024-11-12 10:26:56 +00:00
|
|
|
#!/bin/python3
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
import re
|
2024-11-17 19:37:41 +00:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
from cryptLib import encryptionFuncts
|
|
|
|
|
|
|
|
|
2024-11-12 10:26:56 +00:00
|
|
|
|
|
|
|
logging.basicConfig(format='%(levelname)s\t- %(message)s')
|
|
|
|
logger = logging.getLogger()
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
state = {}
|
|
|
|
|
|
|
|
# Create organization
|
2024-11-17 19:37:41 +00:00
|
|
|
# organization - username - name - email - public key file
|
2024-11-12 10:26:56 +00:00
|
|
|
def createOrganization(args):
|
|
|
|
|
2024-11-17 19:37:41 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
2024-11-12 10:26:56 +00:00
|
|
|
|
2024-11-17 19:37:41 +00:00
|
|
|
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)
|
2024-11-12 10:26:56 +00:00
|
|
|
|
2024-11-17 19:37:41 +00:00
|
|
|
args = parser.parse_args()
|
2024-11-12 10:26:56 +00:00
|
|
|
|
2024-11-17 19:37:41 +00:00
|
|
|
#Validate num of arguments
|
|
|
|
if len(args) != 5:
|
|
|
|
logger.error("Need organization, username, name, email and key file.")
|
|
|
|
sys.exit(-1)
|
2024-11-12 10:26:56 +00:00
|
|
|
|
2024-11-17 19:37:41 +00:00
|
|
|
# Validate email
|
|
|
|
if not re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', args.email):
|
2024-11-12 10:26:56 +00:00
|
|
|
logger.error("Need a valid email.")
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2024-11-17 19:37:41 +00:00
|
|
|
# Validate key file
|
|
|
|
if (not os.path.isfile(args.key)):
|
|
|
|
logger.error("File '" + args.key + "' not found.")
|
2024-11-12 10:26:56 +00:00
|
|
|
sys.exit(-1)
|
|
|
|
|
2024-11-17 19:37:41 +00:00
|
|
|
# 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)
|
2024-11-12 10:26:56 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
createOrganization(sys.argv[1:])
|