diff options
Diffstat (limited to 'moonv4/tests/send_authz.py')
-rw-r--r-- | moonv4/tests/send_authz.py | 148 |
1 files changed, 42 insertions, 106 deletions
diff --git a/moonv4/tests/send_authz.py b/moonv4/tests/send_authz.py index a307a5c3..5766a0ec 100644 --- a/moonv4/tests/send_authz.py +++ b/moonv4/tests/send_authz.py @@ -1,7 +1,6 @@ import sys -import argparse -import logging import copy +import logging import threading from importlib.machinery import SourceFileLoader import requests @@ -10,107 +9,20 @@ import json import random from uuid import uuid4 from utils.pdp import check_pdp +from utils.parse import parse +import utils.config logger = None -HOST = None -PORT = None +HOST_MANAGER = None +PORT_MANAGER = None HOST_AUTHZ = None PORT_AUTHZ = None HOST_KEYSTONE = None PORT_KEYSTONE = None lock = threading.Lock() - - -def init(): - global logger, HOST, PORT, HOST_AUTHZ, PORT_AUTHZ, HOST_KEYSTONE, PORT_KEYSTONE - parser = argparse.ArgumentParser() - parser.add_argument('filename', help='scenario filename', nargs=1) - parser.add_argument("--verbose", "-v", action='store_true', - help="verbose mode") - parser.add_argument("--debug", "-d", action='store_true', - help="debug mode") - parser.add_argument("--dry-run", "-n", action='store_true', - help="Dry run", dest="dry_run") - parser.add_argument("--destination", - help="Set the type of output needed " - "(default: wrapper, other possible type: " - "interface).", - default="wrapper") - parser.add_argument("--host", - help="Set the name of the host to test " - "(default: 172.18.0.11).", - default="127.0.0.1") - parser.add_argument("--port", "-p", - help="Set the port of the host to test " - "(default: 8082).", - default="8082") - parser.add_argument("--host-authz", - help="Set the name of the host to test authorization " - "(default: 172.18.0.11).", - default="127.0.0.1", - dest="host_authz") - parser.add_argument("--port-authz", - help="Set the port of the host to test authorization " - "(default: 8081).", - default="8081", - dest="port_authz") - parser.add_argument("--host-keystone", - help="Set the name of the Keystone host " - "(default: 172.18.0.11).", - default="127.0.0.1") - parser.add_argument("--port-keystone", - help="Set the port of the Keystone host " - "(default: 5000).", - default="5000") - parser.add_argument("--stress-test", "-s", action='store_true', - dest='stress_test', - help="Execute stressing tests (warning delta measures " - "will be false, implies -t)") - parser.add_argument("--write", "-w", help="Write test data to a JSON file", - default="/tmp/data.json") - parser.add_argument("--pdp", help="Test on pdp PDP") - parser.add_argument("--request-per-second", - help="Number of requests per seconds", - type=int, dest="request_second", default=-1) - parser.add_argument("--limit", help="Limit request to LIMIT", type=int, - default=500) - args = parser.parse_args() - - FORMAT = '%(asctime)-15s %(levelname)s %(message)s' - if args.debug: - logging.basicConfig( - format=FORMAT, - level=logging.DEBUG) - elif args.verbose: - logging.basicConfig( - format=FORMAT, - level=logging.INFO) - else: - logging.basicConfig( - format=FORMAT, - level=logging.WARNING) - - logger = logging.getLogger(__name__) - - requests_log = logging.getLogger("requests.packages.urllib3") - requests_log.setLevel(logging.WARNING) - requests_log.propagate = True - - if args.stress_test: - args.testonly = True - - if args.filename: - logger.info("Loading: {}".format(args.filename[0])) - - HOST = args.host - PORT = args.port - HOST_AUTHZ = args.host_authz - PORT_AUTHZ = args.port_authz - HOST_KEYSTONE = args.host_keystone - PORT_KEYSTONE = args.port_keystone - return args +logger = logging.getLogger(__name__) def get_scenario(args): @@ -119,8 +31,9 @@ def get_scenario(args): def get_keystone_id(pdp_name): + global HOST_MANAGER, PORT_MANAGER keystone_project_id = None - for pdp_key, pdp_value in check_pdp(moon_url="http://{}:{}".format(HOST, PORT))["pdps"].items(): + for pdp_key, pdp_value in check_pdp(moon_url="http://{}:{}".format(HOST_MANAGER, PORT_MANAGER))["pdps"].items(): if pdp_name: if pdp_name != pdp_value["name"]: continue @@ -181,17 +94,23 @@ def _send(url, data=None, stress_test=False): logger.warning("Unable to connect to server") return {} if not stress_test: - if res.status_code == 200: - logger.warning("\033[1m{}\033[m \033[32mGrant\033[m".format(url)) - elif res.status_code == 401: - logger.warning("\033[1m{}\033[m \033[31mDeny\033[m".format(url)) - else: - logger.error("\033[1m{}\033[m {} {}".format(url, res.status_code, res.text)) try: j = res.json() + if res.status_code == 200: + logger.warning("\033[1m{}\033[m \033[32mGrant\033[m".format(url)) + elif res.status_code == 401: + logger.warning("\033[1m{}\033[m \033[31mDeny\033[m".format(url)) + else: + logger.error("\033[1m{}\033[m {} {}".format(url, res.status_code, res.text)) except Exception as e: - logger.exception(e) - logger.error(res.text) + if res.text == "True": + logger.warning("\033[1m{}\033[m \033[32mGrant\033[m".format(url)) + elif res.text == "False": + logger.warning("\033[1m{}\033[m \033[31mDeny\033[m".format(url)) + else: + logger.error("\033[1m{}\033[m {} {}".format(url, res.status_code, res.text)) + logger.exception(e) + logger.error(res.text) else: if j.get("result"): # logger.warning("{} \033[32m{}\033[m".format(url, j.get("result"))) @@ -224,6 +143,7 @@ class AsyncGet(threading.Thread): def send_requests(scenario, keystone_project_id, request_second=1, limit=500, dry_run=None, stress_test=False, destination="wrapper"): + global HOST_AUTHZ, PORT_AUTHZ backgrounds = [] time_data = list() start_timing = time.time() @@ -234,11 +154,14 @@ def send_requests(scenario, keystone_project_id, request_second=1, limit=500, while request_cpt < limit: rule = (random.choice(SUBJECTS), random.choice(OBJECTS), random.choice(ACTIONS)) if destination.lower() == "wrapper": - url = "http://{}:{}/authz".format(HOST_AUTHZ, PORT_AUTHZ, keystone_project_id, "/".join(rule)) + url = "http://{}:{}/authz".format(HOST_AUTHZ, PORT_AUTHZ) data = { 'target': { "user_id": random.choice(SUBJECTS), - "target": {"name": random.choice(OBJECTS)} + "target": { + "name": random.choice(OBJECTS) + }, + "project_id": keystone_project_id }, 'credentials': None, 'rule': random.choice(ACTIONS) @@ -287,7 +210,20 @@ def get_delta(time_data): def main(): - args = init() + global HOST_MANAGER, PORT_MANAGER, HOST_AUTHZ, PORT_AUTHZ + + args = parse() + consul_host = args.consul_host + consul_port = args.consul_port + conf_data = utils.config.get_config_data(consul_host, consul_port) + + HOST_MANAGER = conf_data['manager_host'] + PORT_MANAGER = conf_data['manager_port'] + HOST_AUTHZ = args.authz_host + PORT_AUTHZ = args.authz_port + # HOST_KEYSTONE = conf_data['keystone_host'] + # PORT_KEYSTONE = conf_data['manager_host'] + scenario = get_scenario(args) keystone_project_id = get_keystone_id(args.pdp) time_data = send_requests( |