Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PPS-261): (feat) Add service-info endpoint to DRS #356

Merged
merged 12 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
"filename": "tests/default_test_settings.py",
"hashed_secret": "afc848c316af1a89d49826c5ae9d00ed769415f3",
"is_verified": false,
"line_number": 26
"line_number": 40
}
],
"tests/postgres/migrations/test_15f2e9345ade_create_tables.py": [
Expand Down Expand Up @@ -391,9 +391,9 @@
"filename": "tests/test_drs.py",
"hashed_secret": "5666c088b494f26cd8f63ace013992f5fc391ce0",
"is_verified": false,
"line_number": 31
"line_number": 32
}
]
},
"generated_at": "2023-03-23T20:46:39Z"
"generated_at": "2023-04-13T17:00:13Z"
}
4 changes: 4 additions & 0 deletions bin/indexd_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def load_json(file_name):
if dist:
CONFIG["DIST"] = json.loads(dist)

drs_service_info = environ.get("DRS_SERVICE_INFO", None)
if drs_service_info:
CONFIG["DRS_SERVICE_INFO"] = json.loads(drs_service_info)

CONFIG["INDEX"] = {
"driver": SQLAlchemyIndexDriver(
"postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}".format(
Expand Down
4 changes: 4 additions & 0 deletions deployment/Secrets/indexd_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def load_json(file_name):
if dist:
CONFIG["DIST"] = json.loads(dist)

drs_service_info = environ.get("DRS_SERVICE_INFO", None)
if drs_service_info:
CONFIG["DRS_SERVICE_INFO"] = json.loads(drs_service_info)

CONFIG["INDEX"] = {
"driver": SQLAlchemyIndexDriver(
"postgresql+psycopg2://{usr}:{psw}@{pghost}:{pgport}/{db}".format(
Expand Down
21 changes: 15 additions & 6 deletions indexd/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,23 @@
"hints": [],
"type": "dos",
},
{
"name": "DRS System",
"host": "https://example.com/api/ga4gh/drs/v1/",
"hints": [],
"type": "drs",
},
]

CONFIG["DRS_SERVICE_INFO"] = {
paulineribeyre marked this conversation as resolved.
Show resolved Hide resolved
"name": "DRS System",
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.3",
},
"version": "1.0.3",
"id": "com.example",
"organization": {
"name": "CTDS",
"url": "http://example.com/",
},
}

AUTH = SQLAlchemyAuthDriver("sqlite:///auth.sq3")

settings = {"config": CONFIG, "auth": AUTH}
45 changes: 45 additions & 0 deletions indexd/drs/blueprint.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
import os
import re
import flask
import json
from indexd.errors import AuthError, AuthzError
from indexd.errors import UserError
from indexd.index.errors import NoRecordFound as IndexNoRecordFound
from indexd.errors import IndexdUnexpectedError
from indexd.utils import reverse_url

blueprint = flask.Blueprint("drs", __name__)

blueprint.config = dict()
blueprint.index_driver = None
blueprint.service_info = {}


@blueprint.route("/ga4gh/drs/v1/service-info", methods=["GET"])
def get_drs_service_info():
"""
Returns DRS compliant service information
"""

reverse_domain_name = reverse_url(url=os.environ["HOSTNAME"])

ret = {
"id": reverse_domain_name,
"name": "DRS System",
"version": "1.0.3",
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.3",
},
"organization": {
"name": "CTDS",
"url": "https://" + os.environ["HOSTNAME"],
},
}

if blueprint.service_info:
for key, value in blueprint.service_info.items():
if key in ret:
if isinstance(value, dict):
for inner_key, inner_value in value.items():
ret[key][inner_key] = inner_value
else:
ret[key] = value

return flask.jsonify(ret), 200


@blueprint.route("/ga4gh/drs/v1/objects/<path:object_id>", methods=["GET"])
Expand Down Expand Up @@ -329,3 +368,9 @@ def handle_unexpected_error(err):
def get_config(setup_state):
index_config = setup_state.app.config["INDEX"]
blueprint.index_driver = index_config["driver"]


@blueprint.record
def get_config(setup_state):
if "DRS_SERVICE_INFO" in setup_state.app.config:
blueprint.service_info = setup_state.app.config["DRS_SERVICE_INFO"]
21 changes: 21 additions & 0 deletions indexd/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import re
from urllib.parse import urlparse


def hint_match(record, hints):
Expand Down Expand Up @@ -189,3 +190,23 @@ def migrate_database(driver, migrate_functions, current_schema_version, model):
f(engine=driver.engine, session=s)
schema_version.version += 1
s.add(schema_version)


def reverse_url(url):
paulineribeyre marked this conversation as resolved.
Show resolved Hide resolved
"""
Reverse the domain name for drs service-info IDs
Args:
url (str): url of the domain
example: drs.example.org

returns:
id (str): DRS service-info ID
example: org.example.drs
"""
parsed_url = urlparse(url)
if parsed_url.scheme in ["http", "https"]:
url = parsed_url.hostname
segments = url.split(".")
reversed_segments = reversed(segments)
res = ".".join(reversed_segments)
return res
16 changes: 15 additions & 1 deletion tests/default_test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,23 @@
"host": "https://fictitious-commons.io/index/",
"hints": [".*dg\\.4503.*"],
"type": "indexd",
}
},
]

CONFIG["DRS_SERVICE_INFO"] = {
"name": "DRS System",
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.3",
},
"version": "1.0.3",
"organization": {
"name": "CTDS",
"url": "https://fictitious-commons.io",
},
}

os.environ["PRESIGNED_FENCE_URL"] = "https://fictitious-commons.io/"
os.environ["HOSTNAME"] = "fictitious-commons.io"
settings = {"config": CONFIG, "auth": AUTH}
Expand Down
59 changes: 59 additions & 0 deletions tests/test_drs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import flask
import json
import tests.conftest
import requests
Expand Down Expand Up @@ -155,3 +156,61 @@ def test_get_drs_with_encoded_slash(client, user):
assert rec_2["checksums"][0]["type"] == k
assert rec_2["version"]
assert rec_2["self_uri"] == "drs://testprefix:" + rec_1["did"].split(":")[1]


def test_drs_service_info_endpoint(client):
"""
Test drs service endpoint with drs service info friendly distribution information
"""
app = flask.Flask(__name__)

expected_info = {
"id": "io.fictitious-commons",
"name": "DRS System",
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.3",
},
"version": "1.0.3",
"organization": {
"name": "CTDS",
"url": "https://fictitious-commons.io",
},
}

res = client.get("/ga4gh/drs/v1/service-info")

assert res.status_code == 200
assert res.json == expected_info


def test_drs_service_info_no_information_configured(client):
"""
Test drs service info endpoint when dist is not configured in the indexd config file
"""
expected_info = {
"id": "io.fictitious-commons",
"name": "DRS System",
"type": {
"group": "org.ga4gh",
"artifact": "drs",
"version": "1.0.3",
},
"version": "1.0.3",
"organization": {
"name": "CTDS",
"url": "https://fictitious-commons.io",
},
}
backup = settings["config"]["DRS_SERVICE_INFO"].copy()

try:
settings["config"]["DRS_SERVICE_INFO"].clear()

res = client.get("/ga4gh/drs/v1/service-info")

assert res.status_code == 200
assert res.json == expected_info
finally:
settings["config"]["DRS_SERVICE_INFO"] = backup