From 859a86bf39df6eafa6988215cb84556fc9623ea0 Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Sun, 4 Oct 2026 11:37:55 +0000 Subject: Gate GET /datasets/{dataset}/probesets/{probeset} on gn-auth Require a valid gn-auth bearer token on this endpoint unconditionally. For a dataset that isn't publicly readable, check the group:resource:view-resource privilege via a new auth::authorise_trait, the "Dataset::Trait"-keyed equivalent of authorise (POST /auth/data/authorisation instead of /auth/resource/authorisation) -- gn-auth resolves the resource itself from the dataset name, so no separate resource-id lookup is needed here. A trait never linked to a gn-auth resource (gn-auth answers 404) is treated as "no privileges", not a request failure. Denied requests get a 403; a failure to reach/parse gn-auth's answer gets a 502, distinct from both that and this service's own 500s. is_publicly_readable now also takes the dataset's CreateTime and treats anything older than 5 years as public regardless of its public/confidentiality flags -- a confidentiality embargo isn't meant to last forever. No admin/sysadmin bypass is implemented. Register a "bearer_token" OpenAPI security scheme (src/lib.rs) so the new security(...) annotation on this endpoint's #[utoipa::path] has something to point at. tests/dataset_probeset_info.rs and its Python mirror gain cases for the missing-token (401) and denied-but-existing (403) outcomes, run against a real gn-auth instance set up as described in docs/gn-auth.md; the 403 case uses UTHSC_BXD_Cochlea_0225, a real non-public, non-embargoed dataset never linked into gn-auth. tests/common gains get_with_token and auth_server_available/auth_token helpers for this. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d --- test/python/test_dataset_probeset_info.py | 88 +++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 21 deletions(-) (limited to 'test/python') diff --git a/test/python/test_dataset_probeset_info.py b/test/python/test_dataset_probeset_info.py index cfb219c..569e9f0 100644 --- a/test/python/test_dataset_probeset_info.py +++ b/test/python/test_dataset_probeset_info.py @@ -1,15 +1,22 @@ """Mirrors tests/dataset_probeset_info.rs. Needs a live MariaDB (SQL_URI); skips with a message rather than failing when none is -reachable, same as test_group_by_name.py. +reachable, same as test_group_by_name.py. Also needs a live gn-auth +(AUTH_SERVER_URL) and GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md), since +this endpoint now requires a bearer token; skips when either is +missing. """ import http.client import json +import os import socket import unittest +from urllib.parse import urlparse from server_test_case import HOST, PORT, ServerTestCase +AUTH_SERVER_URL = os.environ.get("AUTH_SERVER_URL", "http://127.0.0.1:8081/") + def database_reachable(): try: @@ -19,18 +26,45 @@ def database_reachable(): return False +def auth_server_reachable(): + url = urlparse(AUTH_SERVER_URL) + conn = http.client.HTTPConnection(url.hostname, url.port or 80, timeout=2) + try: + conn.request("GET", url.path.rstrip("/") + "/auth/public-jwks") + return conn.getresponse().status == 200 + except OSError: + return False + finally: + conn.close() + + +def auth_token(): + return os.environ.get("GN4_TEST_AUTH_TOKEN") + + class DatasetProbesetInfoTest(ServerTestCase): - def test_dataset_probeset_info_returns_gene_annotation(self): + def require_fixtures(self): if not database_reachable(): self.skipTest("no database on 127.0.0.1:3306") + if not auth_token(): + self.skipTest("set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)") + if not auth_server_reachable(): + self.skipTest(f"no gn-auth reachable at {AUTH_SERVER_URL}") + def get(self, path, with_token=True): conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) try: - conn.request("GET", "/datasets/Br_U_0803_M/probesets/100001_at") + headers = {"Authorization": f"Bearer {auth_token()}"} if with_token else {} + conn.request("GET", path, headers=headers) response = conn.getresponse() - body = response.read() + return response, response.read() finally: conn.close() + + def test_dataset_probeset_info_returns_gene_annotation(self): + self.require_fixtures() + + response, body = self.get("/datasets/Br_U_0803_M/probesets/100001_at") self.assertEqual(response.status, 200) info = json.loads(body) @@ -38,17 +72,19 @@ class DatasetProbesetInfoTest(ServerTestCase): self.assertEqual(info["symbol"], "Cd3g") self.assertIsInstance(info["mean"], float) - def test_dataset_probeset_info_unknown_dataset_returns_404(self): + def test_dataset_probeset_info_without_a_token_returns_401(self): if not database_reachable(): self.skipTest("no database on 127.0.0.1:3306") - conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) - try: - conn.request("GET", "/datasets/not-a-real-dataset/probesets/100001_at") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, _body = self.get( + "/datasets/Br_U_0803_M/probesets/100001_at", with_token=False + ) + self.assertEqual(response.status, 401) + + def test_dataset_probeset_info_unknown_dataset_returns_404(self): + self.require_fixtures() + + response, body = self.get("/datasets/not-a-real-dataset/probesets/100001_at") self.assertEqual(response.status, 404) error = json.loads(body) @@ -57,16 +93,9 @@ class DatasetProbesetInfoTest(ServerTestCase): self.assertEqual(error["operation_id"], "dataset_probeset_info") def test_dataset_probeset_info_unknown_probeset_in_dataset_returns_404(self): - if not database_reachable(): - self.skipTest("no database on 127.0.0.1:3306") + self.require_fixtures() - conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) - try: - conn.request("GET", "/datasets/Br_U_0803_M/probesets/not-a-real-probeset") - response = conn.getresponse() - body = response.read() - finally: - conn.close() + response, body = self.get("/datasets/Br_U_0803_M/probesets/not-a-real-probeset") self.assertEqual(response.status, 404) error = json.loads(body) @@ -74,6 +103,23 @@ class DatasetProbesetInfoTest(ServerTestCase): self.assertEqual(error["code"], "probeset_not_found") self.assertEqual(error["operation_id"], "dataset_probeset_info") + def test_dataset_probeset_info_non_public_unlinked_dataset_returns_403(self): + """UTHSC_BXD_Cochlea_0225 (public=1, confidentiality=1, created + 2025-02-13) is neither publicly readable by the flags nor old + enough for the embargo-age rule, and has never been linked to a + gn-auth resource -- so even the token's owner has no privilege + on it. + """ + self.require_fixtures() + + response, body = self.get( + "/datasets/UTHSC_BXD_Cochlea_0225/probesets/ENSMUSG00000028180" + ) + self.assertEqual(response.status, 403) + + error = json.loads(body) + self.assertEqual(error["code"], "forbidden") + if __name__ == "__main__": unittest.main() -- cgit 1.4.1