def router(path="/"):
    updated_register = healthcheck(register)
    host_header = request.headers["Host"]
    for entry in config["hosts"]:
        if host_header == entry["host"]:
            healthy_server = get_healthy_server(entry["host"],
                                                updated_register)
            if not healthy_server:
                return "No Backends servers available", 503
            headers = process_rules(config, host_header,
                                    {k: v
                                     for k, v in request.headers.items()},
                                    "header")
            params = process_rules(config, host_header,
                                   {k: v
                                    for k, v in request.args.items()}, "param")
            rewrite_path = ""
            if path == "v1":
                rewrite_path = process_rewrite_rules(config, host_header, path)
            response = requests.get("http://{}{}".format(
                healthy_server.endpoint, "/" + rewrite_path),
                                    headers=headers,
                                    params=params)
            return response.content, response.status_code

    for entry in config["paths"]:
        if ("/" + path) == entry["path"]:
            healthy_server = get_healthy_server(entry["path"], register)
            if not healthy_server:
                return "No Backends servers available", 503
            response = requests.get("http://{}".format(
                healthy_server.endpoint))
            return response.content, response.status_code

    return "Not Found", 404
Exemple #2
0
def test_process_rules():
    input = yaml.safe_load('''
        hosts:
          - host: www.mango.com
            header_rules:
              add:
                MyCustomHeader: Test
              remove:
                Host: www.mango.com
            servers:
              - localhost:8081
              - localhost:8082
          - host: www.apple.com
            servers:
              - localhost:9081
              - localhost:9082
        paths:
          - path: /mango
            servers:
              - localhost:8081
              - localhost:8082
          - path: /apple
            servers:
              - localhost:9081
              - localhost:9082
    ''')
    headers = {'Host': 'www.mango.com'}
    results = process_rules(input, 'www.mango.com', headers, 'header')
    assert results == {'MyCustomHeader': 'Test'}
Exemple #3
0
def test_process_param_rules():
    input = yaml.safe_load('''
        hosts:
          - host: www.mango.com
            param_rules:
              add:
                MyCustomParam: Test
              remove:
                RemoveMe: Remove
            servers:
              - localhost:8081
              - localhost:8082
          - host: www.apple.com
            servers:
              - localhost:9081
              - localhost:9082
        paths:
          - path: /mango
            servers:
              - localhost:8081
              - localhost:8082
          - path: /apple
            servers:
              - localhost:9081
              - localhost:9082
    ''')
    params = {'RemoveMe': 'Remove'}
    results = process_rules(input, 'www.mango.com', params, 'param')
    assert results == {'MyCustomParam': 'Test'}
Exemple #4
0
def test_process_param_rules():
    input = yaml.safe_load('''
        hosts:
          - host: www.mango.com
            param_rules:
              add:
                MyCustomParam: Test
              remove:
                RemoveMe: Remove
            servers:
              - localhost:8081
              - localhost:8082
          - host: www.apple.com
            servers:
              - localhost:9081
              - localhost:9082
        paths:
          - path: /mango
            servers:
              - localhost:8081
              - localhost:8082
          - path: /apple
            servers:
              - localhost:9081
              - localhost:9082
    ''')
    params = {"RemoveMe": "Remove"}
    results = process_rules(input, "www.mango.com", params, "param")
    assert results == {"MyCustomParam": "Test"}
Exemple #5
0
def test_process_header_rules():
    input = yaml.safe_load('''
        hosts:
          - host: www.mango.com
            header_rules:
              add: 
                MyCustomHeader: Test
              remove: 
                Host: www.mango.com
            servers:
              - localhost:8081
              - localhost:8082
          - host: www.apple.com
            servers:
              - localhost:9081
              - localhost:9082
        paths:
          - path: /mango
            servers:
              - localhost:8081
              - localhost:8082
          - path: /apple
            servers:
              - localhost:9081
              - localhost:9082
    ''')
    headers = {"Host": "www.mango.com"}
    results = process_rules(input, "www.mango.com", headers, "header")
    assert results == {"MyCustomHeader": "Test"}
def test_process_header_rules():
    input = yaml.safe_load(
        """
            hosts:
              - host: www.anthrax.com
                header_rules:
                  add:
                    MyCustomHeader: Test
                  remove:
                    Host: www.anthrax.com
                servers:
                  - localhost:8081
                  - localhost:8082
              - host: www.metallica.com
                servers:
                  - localhost:9081
                  - localhost:9082
            paths:
              - path: /anthrax
                servers:
                  - localhost:8081
                  - localhost:8082
              - path: /metallica
                servers:
                  - localhost:9081
                  - localhost:9082
        """
    )
    headers = {"Host": "www.anthrax.com"}
    results = process_rules(input, "www.anthrax.com", headers, "header")
    assert results == {"MyCustomHeader": "Test"}
def test_process_cookie_rules():
    input = yaml.safe_load(
        """
            hosts:
              - host: www.anthrax.com
                cookie_rules:
                  add:
                    ThrashCookie: Rock on!
                  remove:
                    RemoveCookie: Remove
                servers:
                  - localhost:8081
                  - localhost:8082
              - host: www.metallica.com
                servers:
                  - localhost:9081
                  - localhost:9082
            paths:
              - path: /anthrax
                servers:
                  - localhost:8081
                  - localhost:8082
              - path: /metallica
                servers:
                  - localhost:9081
                  - localhost:9082
        """
    )
    cookie = {"RemoveCookie": "Remove"}
    results = process_rules(input, "www.anthrax.com", cookie, "cookie")
    assert results == {"ThrashCookie": "Rock on!"}
def test_process_post_data_rules():
    input = yaml.safe_load(
        """
            hosts:
              - host: www.anthrax.com
                post_data_rules:
                  add:
                    Token: Test
                  remove:
                    File Placeholder: Test
                servers:
                  - localhost:8081
                  - localhost:8082
              - host: www.metallica.com
                servers:
                  - localhost:9081
                  - localhost:9082
            paths:
              - path: /anthrax
                servers:
                  - localhost:8081
                  - localhost:8082
              - path: /metallica
                servers:
                  - localhost:9081
                  - localhost:9082
        """
    )
    post_data = {"File Placeholder": "File"}
    results = process_rules(input, "www.anthrax.com", post_data, "post_data")
    assert results == {"Token": "Test"}
def process_param_rules():
    input = yaml.safe_load(
        """
            hosts:
              - host: www.anthrax.com
                param_rules:
                  add:
                    MyCustomParam: Test
                  remove:
                    RemoveMe: Remove
                servers:
                  - localhost:8081
                  - localhost:8082
              - host: www.metallica.com
                servers:
                  - localhost:9081
                  - localhost:9082
            paths:
              - path: /anthrax
                servers:
                  - localhost:8081
                  - localhost:8082
              - path: /metallica
                servers:
                  - localhost:9081
                  - localhost:9082
        """
    )
    params = {"RemoveMe": "Remove"}
    results = process_rules(input, "www.anthrax.com", params, "param")
    assert results == {"MyCustomParam": "Test"}
def router(path='/'):
    updated_register = healthcheck(register)
    host_header = request.headers['Host']

    if not process_firewall_rules_flag(
            config, host_header, request.environ['REMOTE_ADDR'], f'/{path}'):
        return 'Forbidden', 403

    for entry in config['hosts']:
        if host_header == entry['host']:
            healthy_server = get_healthy_server(entry['host'],
                                                updated_register)
            if not healthy_server:
                return 'No backend servers available.', 503
            headers = process_rules(config, host_header,
                                    {k: v
                                     for k, v in request.headers.items()},
                                    'header')
            params = process_rules(config, host_header,
                                   {k: v
                                    for k, v in request.args.items()}, 'param')
            rewrite_path = ''
            if path == 'v1':
                rewrite_path = process_rewrite_rules(config, host_header, path)
            response = requests.get(
                f'http://{healthy_server.endpoint}/{rewrite_path}',
                headers=headers,
                params=params)
            return response.content, response.status_code

    for entry in config['paths']:
        if ('/' + path) == entry['path']:
            healthy_server = get_healthy_server(entry['path'], register)
            if not healthy_server:
                return 'No backend servers available.', 503
            healthy_server.open_connections += 1
            response = requests.get(f'http://{healthy_server.endpoint}')
            healthy_server.open_connections -= 1
            return response.content, response.status_code

    return 'Not Found', 404
def router(path="/"):
    updated_register = healthcheck(register)
    host_header = request.headers["Host"]
    header_dictionary = {k: v for k, v in request.headers.items()}

    if not process_firewall_rules_flag(
            config,
            host_header,
            request.environ["REMOTE_ADDR"],
            f"/{path}",
            header_dictionary,
    ):
        return "Forbidden", 403

    for entry in config["hosts"]:
        try:
            algo = entry["algo"]
        except KeyError:
            pass
        if algo == "weight":
            try:
                weights = [weights for weights in entry["weights"]]
            except KeyError:
                weights = None
        else:
            weights = None

        if host_header == entry["host"]:
            healthy_server = get_healthy_server(entry["host"],
                                                updated_register, algo,
                                                weights)
            if not healthy_server:
                return "No backend servers available.", 503
            headers = process_rules(
                config,
                host_header,
                {k: v
                 for k, v in request.headers.items()},
                "header",
            )
            params = process_rules(config, host_header,
                                   {k: v
                                    for k, v in request.args.items()}, "param")
            post_data = process_rules(config, host_header,
                                      {k: v
                                       for k, v in request.data}, "post_data")
            cookies = process_rules(config, host_header,
                                    {k: v
                                     for k, v in request.cookies}, "cookie")
            rewrite_path = ""
            if path == "v1":
                rewrite_path = process_rewrite_rules(config, host_header, path)
            response = requests.get(
                f"http://{healthy_server.endpoint}/{rewrite_path}",
                headers=headers,
                params=params,
                data=post_data,
                cookies=cookies,
            )
            return response.content, response.status_code

    for entry in config["paths"]:
        if ("/" + path) == entry["path"]:
            healthy_server = get_healthy_server(entry["path"], register)
            if not healthy_server:
                return "No backend servers available", 503
            healthy_server.open_connections += 1
            response = requests.get(f"http://{healthy_server.endpoint}")
            healthy_server.open_connections -= 1
            return response.content, response.status_code

    return "Not Found", 404