diff --git a/.secrets.baseline b/.secrets.baseline index 68004bce..d1f8f1fa 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -230,7 +230,7 @@ "filename": "tests/default_test_settings.py", "hashed_secret": "afc848c316af1a89d49826c5ae9d00ed769415f3", "is_verified": false, - "line_number": 26 + "line_number": 41 } ], "tests/postgres/migrations/test_15f2e9345ade_create_tables.py": [ @@ -391,9 +391,9 @@ "filename": "tests/test_drs.py", "hashed_secret": "5666c088b494f26cd8f63ace013992f5fc391ce0", "is_verified": false, - "line_number": 31 + "line_number": 32 } ] }, - "generated_at": "2023-04-10T19:43:22Z" + "generated_at": "2023-04-12T20:10:41Z" } diff --git a/bin/indexd_settings.py b/bin/indexd_settings.py index 364aac2b..bcc67985 100644 --- a/bin/indexd_settings.py +++ b/bin/indexd_settings.py @@ -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( diff --git a/deployment/Secrets/indexd_settings.py b/deployment/Secrets/indexd_settings.py index 4b35c567..e7572d4f 100644 --- a/deployment/Secrets/indexd_settings.py +++ b/deployment/Secrets/indexd_settings.py @@ -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( diff --git a/indexd/default_settings.py b/indexd/default_settings.py index 18520fae..7b999bb7 100644 --- a/indexd/default_settings.py +++ b/indexd/default_settings.py @@ -45,24 +45,25 @@ "hints": [], "type": "dos", }, - { - "name": "DRS System", - "host": "https://example.com/api/ga4gh/drs/v1/", - "hints": [], - "type": { - "group": "org.ga4gh", - "artifact": "drs", - "version": "1.0.0", - }, - "version": "1.0.0", - "id": "com.example", - "organization": { - "name": "Gen3", - "url": "http://example.com/", - }, - }, ] +CONFIG["DRS_SERVICE_INFO"] = { + "name": "DRS System", + "host": "https://example.com/api/ga4gh/drs/v1/", + "hints": [], + "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} diff --git a/indexd/drs/blueprint.py b/indexd/drs/blueprint.py index 6f42a2a5..2c9a8a46 100644 --- a/indexd/drs/blueprint.py +++ b/indexd/drs/blueprint.py @@ -5,13 +5,13 @@ from indexd.errors import UserError from indexd.index.errors import NoRecordFound as IndexNoRecordFound from indexd.errors import IndexdUnexpectedError -from indexd.utils import drs_service_info_id_url_reversal +from indexd.utils import reverse_url blueprint = flask.Blueprint("drs", __name__) blueprint.config = dict() blueprint.index_driver = None -blueprint.dist = [] +blueprint.service_info = {} @blueprint.route("/ga4gh/drs/v1/service-info", methods=["GET"]) @@ -19,46 +19,46 @@ def get_drs_service_info(): """ Returns DRS compliant service information """ - drs_dist = {} - - reverse_domain_name = drs_service_info_id_url_reversal(url=os.environ["HOSTNAME"]) - - if len(blueprint.dist) > 0: - # Check to see if the information is of type drs. If not, use the available information to return DRS compliant service information - for dist in blueprint.dist: - if ( - "type" in dist - and isinstance(dist["type"], dict) - and "artifact" in dist["type"] - and dist["type"]["artifact"] == "drs" - ): - drs_dist = dist - if drs_dist == {}: - drs_dist = blueprint.dist[0] + + reverse_domain_name = reverse_url(url=os.environ["HOSTNAME"]) ret = { - "id": drs_dist.get("id", reverse_domain_name), - "name": drs_dist.get("name", "DRS System"), - "version": drs_dist.get("version", "1.0.0"), + "id": blueprint.service_info.get("id", reverse_domain_name), + "name": blueprint.service_info.get("name", "DRS System"), + "version": blueprint.service_info.get("version", "1.0.3"), "type": { - "group": drs_dist.get("group", "org.ga4gh"), - "artifact": drs_dist.get("artifact", "drs"), - }, - "organization": { - "name": "Gen3", + "group": blueprint.service_info.get("group", "org.ga4gh"), + "artifact": blueprint.service_info.get("artifact", "drs"), }, } - if "type" in drs_dist and isinstance(drs_dist["type"], dict): - ret["type"]["version"] = drs_dist.get("type").get("version", "1.0.0") + ret["organization"] = {} + + if "type" in blueprint.service_info and isinstance( + blueprint.service_info["type"], dict + ): + ret["type"]["version"] = blueprint.service_info.get("type").get( + "version", "1.0.3" + ) else: - ret["type"]["version"] = "1.0.0" + ret["type"]["version"] = "1.0.3" - if "organization" in drs_dist and "url" in drs_dist["organization"]: - ret["organization"]["url"] = drs_dist["organization"]["url"] + if ( + "organization" in blueprint.service_info + and "url" in blueprint.service_info["organization"] + ): + ret["organization"]["url"] = blueprint.service_info["organization"]["url"] else: ret["organization"]["url"] = "https://" + os.environ["HOSTNAME"] + if ( + "organization" in blueprint.service_info + and "name" in blueprint.service_info["organization"] + ): + ret["organization"]["name"] = blueprint.service_info["organization"]["name"] + else: + ret["organization"]["name"] = "CTDS" + return flask.jsonify(ret), 200 @@ -384,5 +384,5 @@ def get_config(setup_state): @blueprint.record def get_config(setup_state): - if "DIST" in setup_state.app.config: - blueprint.dist = setup_state.app.config["DIST"] + if "DRS_SERVICE_INFO" in setup_state.app.config: + blueprint.service_info = setup_state.app.config["DRS_SERVICE_INFO"] diff --git a/indexd/utils.py b/indexd/utils.py index 0dae276b..d17bfc0d 100644 --- a/indexd/utils.py +++ b/indexd/utils.py @@ -191,7 +191,7 @@ def migrate_database(driver, migrate_functions, current_schema_version, model): s.add(schema_version) -def drs_service_info_id_url_reversal(url): +def reverse_url(url): """ Reverse the domain name for drs service-info IDs Args: diff --git a/tests/default_test_settings.py b/tests/default_test_settings.py index ddc60962..b3ec4496 100644 --- a/tests/default_test_settings.py +++ b/tests/default_test_settings.py @@ -12,6 +12,21 @@ }, ] +CONFIG["DRS_SERVICE_INFO"] = { + "name": "DRS System", + "type": { + "group": "org.ga4gh", + "artifact": "drs", + "version": "1.0.3", + }, + "host": "https://fictitious-commons.io/", + "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} diff --git a/tests/test_drs.py b/tests/test_drs.py index 3cd5b7fc..4dc6d47f 100644 --- a/tests/test_drs.py +++ b/tests/test_drs.py @@ -1,3 +1,4 @@ +import flask import json import tests.conftest import requests @@ -157,74 +158,34 @@ def test_get_drs_with_encoded_slash(client, user): assert rec_2["self_uri"] == "drs://testprefix:" + rec_1["did"].split(":")[1] -def test_drs_service_info_without_drs_block_in_config(client): - """ - Test that the non-drs related, defualt distributions convert to drs service info friendly format - """ - expected_info = { - "id": "io.fictitious-commons", - "name": "testStage", - "type": { - "group": "org.ga4gh", - "artifact": "drs", - "version": "1.0.0", - }, - "organization": { - "name": "Gen3", - "url": "https://fictitious-commons.io", - }, - "version": "1.0.0", - } - - res = client.get("/ga4gh/drs/v1/service-info") - - assert res.status_code == 200 - assert res.json == expected_info - - 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.0", + "version": "1.0.3", }, - "version": "1.3.0", + "version": "1.0.3", "organization": { - "name": "Gen3", + "name": "CTDS", "url": "https://fictitious-commons.io", }, } - settings["config"]["DIST"].append( - { - "name": "DRS System", - "type": { - "group": "org.ga4gh", - "artifact": "drs", - "version": "1.0.0", - }, - "host": "https://fictitious-commons.io/", - "version": "1.3.0", - "organization": { - "name": "Gen3", - "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_dist_in_config(client): +def test_drs_service_info_no_information_configured(client): """ Test drs service info endpoint when dist is not configured in the indexd config file """ @@ -234,16 +195,16 @@ def test_drs_service_info_no_dist_in_config(client): "type": { "group": "org.ga4gh", "artifact": "drs", - "version": "1.0.0", + "version": "1.0.3", }, - "version": "1.0.0", + "version": "1.0.3", "organization": { - "name": "Gen3", + "name": "CTDS", "url": "https://fictitious-commons.io", }, } - settings["config"]["DIST"].clear() + settings["config"]["DRS_SERVICE_INFO"].clear() res = client.get("/ga4gh/drs/v1/service-info")