From 932b42cb3f47ce55051cf1c52cd7fe0dcc7850a7 Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Tue, 6 Dec 2022 10:20:48 -0500 Subject: [PATCH 1/6] chore: new env var to specify whether to validate SSL --- bento_service_registry/app.py | 12 +++++++++--- bento_service_registry/routes.py | 13 +++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/bento_service_registry/app.py b/bento_service_registry/app.py index bfd2710..18396de 100644 --- a/bento_service_registry/app.py +++ b/bento_service_registry/app.py @@ -19,12 +19,18 @@ from .routes import service_registry +def get_bento_debug(): + # This is a function to allow monkey-patching the environment on app startup. + return os.environ.get( + "CHORD_DEBUG", os.environ.get("BENTO_DEBUG", os.environ.get("QUART_ENV", "production")) + ).strip().lower() in ("true", "1", "development") + + def create_app(): app = Quart(__name__) app.config.from_mapping( - BENTO_DEBUG=os.environ.get( - "CHORD_DEBUG", os.environ.get("BENTO_DEBUG", os.environ.get("QUART_ENV", "production")) - ).strip().lower() in ("true", "1", "development"), + BENTO_DEBUG=get_bento_debug(), + BENTO_VALIDATE_SSL=not get_bento_debug(), CHORD_SERVICES=os.environ.get("CHORD_SERVICES", os.environ.get("BENTO_SERVICES", "chord_services.json")), CHORD_URL=os.environ.get("CHORD_URL", os.environ.get("BENTO_URL", "http://0.0.0.0:5000/")), # Own node's URL CONTACT_TIMEOUT=int(os.environ.get("CONTACT_TIMEOUT", 5)), diff --git a/bento_service_registry/routes.py b/bento_service_registry/routes.py index c6b2fa1..0f725bd 100644 --- a/bento_service_registry/routes.py +++ b/bento_service_registry/routes.py @@ -56,12 +56,7 @@ async def get_service(session: aiohttp.ClientSession, service_artifact: str) -> service_resp: dict[str, dict] = {} try: - async with session.get( - service_info_url, - headers=headers, - ssl=not current_app.config["BENTO_DEBUG"], - timeout=timeout, - ) as r: + async with session.get(service_info_url, headers=headers, timeout=timeout) as r: if r.status != 200: r_text = await r.text() print(f"[{SERVICE_NAME}] Non-200 status code on {service_artifact}: {r.status}\n" @@ -99,7 +94,8 @@ async def chord_services(): async def get_services() -> list[dict]: - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession( + connector=aiohttp.TCPConnector(ssl=current_app.config["BENTO_VALIDATE_SSL"])) as session: # noinspection PyTypeChecker service_list: list[Optional[dict]] = await asyncio.gather(*[ get_service(session, s["type"]["artifact"]) @@ -119,7 +115,8 @@ async def service_by_id(service_id: str): if service_id not in services_by_id: return quart_not_found_error(f"Service with ID {service_id} was not found in registry") - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession( + connector=aiohttp.TCPConnector(ssl=current_app.config["BENTO_VALIDATE_SSL"])) as session: return await get_service(session, services_by_id[service_id]["type"]["artifact"]) From 13e3eaf0984cb15eba230facf349e9812770cdf5 Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Tue, 6 Dec 2022 10:20:58 -0500 Subject: [PATCH 2/6] docs: document new env var --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e05f62f..f47c2ea 100644 --- a/README.md +++ b/README.md @@ -51,12 +51,15 @@ The following environment variables are used to configure the `bento_service_registry` service: ```bash -# Debug mode - when this is off, requests made to other services in the -# registry will not validate SSL certificates. +# Debug mode: # Setting FLASK_ENV=development will set this to True as well as enabling Flask # debug mode. CHORD_DEBUG=False +# When this is off, requests made to other services in the +# registry will not validate SSL certificates. Defaults to (not CHORD_DEBUG) +BENTO_VALIDATE_SSL=True + # Following the chord_services.json schema # (https://github.com/c3g/chord_singularity/blob/master/chord_services.schema.json) # A list of services on a single domain which are registered in the service From f05bc2100c7ff5466c0cf95de24d2d4c6baa7a0f Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Tue, 6 Dec 2022 10:21:06 -0500 Subject: [PATCH 3/6] chore: bump version to 0.7.1 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index a624131..8517ae6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "bento_service_registry" -version = "0.7.0" +version = "0.7.1" description = "An implementation of GA4GH Service Registry API for the Bento platform." authors = ["David Lougheed "] readme = "README.md" From e42cb6fb1a39c965c8b3bd76009fce22992b7b5b Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Tue, 6 Dec 2022 12:57:57 -0500 Subject: [PATCH 4/6] chore: update c3g url --- bento_service_registry/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bento_service_registry/routes.py b/bento_service_registry/routes.py index 0f725bd..086669a 100644 --- a/bento_service_registry/routes.py +++ b/bento_service_registry/routes.py @@ -139,7 +139,7 @@ async def get_service_info() -> dict: "description": "Service registry for a Bento platform node.", "organization": { "name": "C3G", - "url": "http://www.computationalgenomics.ca" + "url": "https://www.computationalgenomics.ca" }, "contactUrl": "mailto:david.lougheed@mail.mcgill.ca", "version": __version__, From dd1b6df9bcfa536ab3352e123de56aa84e03ceab Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Tue, 6 Dec 2022 12:58:08 -0500 Subject: [PATCH 5/6] fix: service info tests using static app --- tests/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 65bd6bf..130125d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,9 +25,9 @@ def client_debug_mode(): async def _service_info_fixt(): - from bento_service_registry.app import application + from bento_service_registry.app import create_app from bento_service_registry.routes import get_service_info - async with application.app_context(): + async with create_app().app_context(): return await get_service_info() From 4a17993a451de291e73c07c6ebf7772dbd3d66ee Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Tue, 6 Dec 2022 12:58:21 -0500 Subject: [PATCH 6/6] test: add tests for get_bento_debug --- bento_service_registry/constants.py | 6 +++--- tests/test_api.py | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/bento_service_registry/constants.py b/bento_service_registry/constants.py index f3abe38..331f1c8 100644 --- a/bento_service_registry/constants.py +++ b/bento_service_registry/constants.py @@ -8,13 +8,13 @@ "SERVICE_NAME", ] -SERVICE_ARTIFACT = "service-registry" +SERVICE_ARTIFACT: str = "service-registry" # For exact implementations, this should be org.ga4gh/service-registry/1.0.0. # In our case, most of our services diverge or will at some point, so use ca.c3g.bento as the group. -SERVICE_TYPE = { +SERVICE_TYPE: dict[str, str] = { "group": "ca.c3g.bento", "artifact": SERVICE_ARTIFACT, "version": __version__, } -SERVICE_NAME = "Bento Service Registry" +SERVICE_NAME: str = "Bento Service Registry" diff --git a/tests/test_api.py b/tests/test_api.py index 0aaaddb..f5cbddc 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,10 +1,19 @@ import pytest +from bento_service_registry.app import get_bento_debug # Cannot import anything from bento_service_registry here; has to be within # individual tests. Otherwise, we cannot configure the environment variables # to our liking for each test. +def test_bento_debug_off(client): + assert not get_bento_debug() + + +def test_bento_debug_on(client_debug_mode): + assert get_bento_debug() + + @pytest.mark.asyncio async def test_service_info(client): r = await client.get("/service-info")