Skip to content

Commit

Permalink
Merge pull request #7 from bento-platform/toggle-ssl
Browse files Browse the repository at this point in the history
Add environment variable to toggle ssl
  • Loading branch information
davidlougheed authored Jan 6, 2023
2 parents bd28950 + 4a17993 commit 0e25caf
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 20 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions bento_service_registry/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
6 changes: 3 additions & 3 deletions bento_service_registry/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 6 additions & 9 deletions bento_service_registry/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"])
Expand All @@ -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"])


Expand All @@ -142,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:[email protected]",
"version": __version__,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
9 changes: 9 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down

0 comments on commit 0e25caf

Please sign in to comment.