diff --git a/microservices/gatewayApi/tests/conftest.py b/microservices/gatewayApi/tests/conftest.py index 55fe469..bdf6a35 100644 --- a/microservices/gatewayApi/tests/conftest.py +++ b/microservices/gatewayApi/tests/conftest.py @@ -1,10 +1,29 @@ import pytest import os import sys +import json from functools import wraps +import logging +log = logging.getLogger(__name__) +from logging.config import dictConfig # sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +dictConfig({ + 'version': 1, + 'formatters': {'default': { + 'format': '%(asctime)s [%(process)3d] %(levelname)5s %(module)-15s: %(message)s', + }}, + 'handlers': {'app': { + 'class': 'logging.StreamHandler', + 'stream': 'ext://flask.logging.wsgi_errors_stream', + 'formatter': 'default' + }}, + 'root': { + 'level': 'DEBUG', + 'handlers': ['app'] + } +}) @pytest.fixture @@ -71,7 +90,8 @@ def json(): "next": None } return Response - elif (path == 'http://kong/certificates?tags=gwa.ns.mytest'): + elif (path == 'http://kong/certificates?tags=gwa.ns.mytest' or + path == 'http://kong/certificates?tags=gwa.ns.sescookie'): class Response: def json(): return { @@ -123,6 +143,20 @@ class Response: # def json(): # return {} return Response + elif (url == 'http://kube-api/namespaces/sescookie/routes'): + class Response: + status_code = 201 + matched = { + 'hosts': ['myapi.api.gov.bc.ca'], + 'ns_attributes': {'perm-domains': ['.api.gov.bc.ca', '.cluster.local']}, + 'overrides': { + 'aps.route.session.cookie.enabled': ['myapi.api.gov.bc.ca'] + }, + 'select_tag': 'ns.sescookie.dev' + } + + assert json.dumps(kwargs['json'], sort_keys=True) == json.dumps(matched, sort_keys=True) + return Response elif (url == 'http://kube-api/namespaces/ns1/routes'): class Response: status_code = 201 @@ -139,6 +173,12 @@ class Response: def json(): return {} return Response + elif (url == 'http://kube-api/namespaces/sescookie/local_tls'): + class Response: + status_code = 200 + def json(): + return {} + return Response else: raise Exception(url) diff --git a/microservices/gatewayApi/tests/routes/v2/test_gateway.py b/microservices/gatewayApi/tests/routes/v2/test_gateway.py index abc6f5a..dd8f9ec 100644 --- a/microservices/gatewayApi/tests/routes/v2/test_gateway.py +++ b/microservices/gatewayApi/tests/routes/v2/test_gateway.py @@ -74,3 +74,26 @@ def test_happy_sync_gateway_call(client): response = client.put('/v2/namespaces/mytest/gateway', json=data) assert response.status_code == 200 assert json.dumps(response.json) == '{"message": "Sync successful.", "results": "Deck reported no changes"}' + +def test_happy_with_session_cookie_gateway_call(client): + configFile = ''' + services: + - name: my-service + host: myupstream.local + tags: ["ns.sescookie.dev", "another"] + routes: + - name: route-1 + hosts: [ myapi.api.gov.bc.ca ] + tags: ["ns.sescookie.dev", "aps.route.session.cookie.enabled"] + plugins: + - name: acl-auth + tags: ["ns.sescookie.dev"] + ''' + + data={ + "configFile": configFile, + "dryRun": False + } + response = client.put('/v2/namespaces/sescookie/gateway', json=data) + assert response.status_code == 200 + assert json.dumps(response.json) == '{"message": "Sync successful.", "results": "Deck reported no changes"}' diff --git a/microservices/gatewayApi/v1/routes/gateway.py b/microservices/gatewayApi/v1/routes/gateway.py index cabaa90..0751e9a 100644 --- a/microservices/gatewayApi/v1/routes/gateway.py +++ b/microservices/gatewayApi/v1/routes/gateway.py @@ -21,7 +21,7 @@ from clients.portal import record_gateway_event from clients.kong import get_routes from clients.ocp_networksecuritypolicy import get_ocp_service_namespaces, check_nsp, apply_nsp, delete_nsp -from clients.ocp_routes import get_host_list, prepare_apply_routes, prepare_delete_routes, apply_routes, delete_routes +from clients.ocp_routes import get_host_list, get_route_overrides from clients.ocp_gateway_secret import prep_submitted_config, prep_and_apply_secret, write_submitted_config from utils.validators import host_valid @@ -330,11 +330,14 @@ def write_config(namespace: str) -> object: route_payload = { "hosts": get_host_list(tempFolder), "select_tag": selectTag, - "ns_attributes": ns_attributes.getAttrs() + "ns_attributes": ns_attributes.getAttrs(), + "overrides": { + "aps.route.session.cookie.enabled": get_route_overrides(tempFolder, "aps.route.session.cookie.enabled") + } } dp = get_data_plane(ns_attributes) + log.debug("[%s] - Initiating request to kube API %s" % (dp, route_payload)) rqst_url = app.config['data_planes'][dp]["kube-api"] - log.debug("[%s] - Initiating request to kube API" % (dp)) res = session.put(rqst_url + "/namespaces/%s/routes" % namespace, json=route_payload, auth=( app.config['kubeApiCreds']['kubeApiUser'], app.config['kubeApiCreds']['kubeApiPass'])) log.debug("[%s] - The kube API responded with %s" % (dp, res.status_code)) diff --git a/microservices/gatewayApi/v2/routes/gateway.py b/microservices/gatewayApi/v2/routes/gateway.py index 0bea9ac..7401867 100644 --- a/microservices/gatewayApi/v2/routes/gateway.py +++ b/microservices/gatewayApi/v2/routes/gateway.py @@ -342,8 +342,8 @@ def write_config(namespace: str) -> object: "aps.route.session.cookie.enabled": get_route_overrides(tempFolder, "aps.route.session.cookie.enabled") } } + log.debug("[%s] - Initiating request to kube API %s" % (dp, route_payload)) rqst_url = app.config['data_planes'][dp]["kube-api"] - log.debug("[%s] - Initiating request to kube API" % (dp)) res = session.put(rqst_url + "/namespaces/%s/routes" % namespace, json=route_payload, auth=( app.config['kubeApiCreds']['kubeApiUser'], app.config['kubeApiCreds']['kubeApiPass'])) log.debug("[%s] - The kube API responded with %s" % (dp, res.status_code)) diff --git a/microservices/kubeApi/clients/ocp_routes.py b/microservices/kubeApi/clients/ocp_routes.py index 4d9167a..cbcf8fe 100644 --- a/microservices/kubeApi/clients/ocp_routes.py +++ b/microservices/kubeApi/clients/ocp_routes.py @@ -153,6 +153,8 @@ def prepare_apply_routes(ns, select_tag, hosts, root_path, data_plane, ns_templa templ_version = ns_template_version if overrides and 'aps.route.session.cookie.enabled' in overrides and host in overrides['aps.route.session.cookie.enabled']: templ_version = 'v1' + else: + logger.debug("[%s] %s No override applied %s", select_tag, hosts, str(overrides)) route_template = ROUTES[templ_version]["ROUTE"]