diff options
| author | Pjotr Prins <pjotr.public01@thebird.nl> | 2026-10-04 14:38:12 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-10-04 14:38:12 +0000 |
| commit | 5e7870834fc29e707bc7332d83e4c68f05b9b8f7 (patch) | |
| tree | 1fb4e1724a0daa968dafb6d301276c73ad8d3c5d /src | |
| parent | 21c69c1051715e6964fe9d4c8e06a326344fe6eb (diff) | |
| download | genenetwork4-5e7870834fc29e707bc7332d83e4c68f05b9b8f7.tar.gz | |
Add GET /groups/{group}/traits and GET /datasets/{dataset}/probesets
List endpoints for the two trait domains, each the listing equivalent
of an existing singular endpoint:
- GET /groups/{group}/traits: every published (phenotype) trait in a
group (db::get_group_traits), same fields/join as
/groups/{group}/traits/{trait_id}, no token required -- matching its
singular sibling, which isn't gated either. A large group (BXD has
15000+ phenotype traits) can take several seconds and tens of
megabytes to return in full; no pagination yet, matching
GeneNetwork2's own wqflask.api.router.fetch_traits, which has no
default limit either.
- GET /datasets/{dataset}/probesets: every expression (ProbeSet) trait
in a dataset (db::get_dataset_probesets), same fields/join as
/datasets/{dataset}/probesets/{probeset}. Gated the same way as that
singular endpoint -- a valid token is required unconditionally, and
a non-public dataset is checked against gn-auth once for the whole
dataset via a new auth::authorise_dataset, rather than once per
probeset in the list. Leaving this listing ungated would have let a
caller read out a whole non-public dataset's trait data in one
request, bypassing the gate just added to the singular endpoint.
auth::authorise_dataset wraps authorise_trait with an empty trait
name: gn-auth resolves a trait's resource purely from its dataset
name, so the trait half of a "Dataset::Trait" string never affects
which privileges come back. Verified directly against the live
instance both for an unlinked non-public dataset (403, deny) and
folded into dataset_probesets's own test for the public/embargo-aged
bypass path.
tests/group_traits.rs, tests/dataset_probesets.rs and their Python
mirrors cover both, run against a real gn-auth instance for the gated
one.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d
Diffstat (limited to 'src')
| -rw-r--r-- | src/auth.rs | 15 | ||||
| -rw-r--r-- | src/db.rs | 60 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/routes/datasets.rs | 104 | ||||
| -rw-r--r-- | src/routes/groups.rs | 54 |
5 files changed, 235 insertions, 0 deletions
diff --git a/src/auth.rs b/src/auth.rs index d9cc8c4..d4cb7d8 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -370,6 +370,21 @@ pub fn authorise_trait( Ok(privileges.iter().any(|p| p == privilege)) } +/// Ask gn-auth whether `token`'s holder has `privilege` on +/// `dataset_name` as a whole -- for listing every trait in a dataset, +/// where there's no single trait to name. gn-auth resolves a trait's +/// resource purely from its dataset name (see docs/gn-auth.md); the +/// trait half of a `"Dataset::Trait"` string never affects which +/// resource, and so which privileges, come back -- this just omits it. +pub fn authorise_dataset( + auth_server_url: &str, + token: &AuthToken, + dataset_name: &str, + privilege: &str, +) -> Result<bool, String> { + authorise_trait(auth_server_url, token, dataset_name, "", privilege) +} + fn trait_privileges( auth_server_url: &str, token: &AuthToken, diff --git a/src/db.rs b/src/db.rs index 393a861..903f2d6 100644 --- a/src/db.rs +++ b/src/db.rs @@ -372,6 +372,33 @@ pub fn get_group_trait_info( ) } +/// Fetch every published trait's metadata (see TraitInfo) within a +/// group, the listing equivalent of get_group_trait_info. +pub fn get_group_traits(group_id: u16) -> Result<Vec<TraitInfo>, mysql::Error> { + let pool = pool()?; + let mut conn = pool.get_conn()?; + conn.exec( + "SELECT PublishXRef.Id AS Id, PublishXRef.PhenotypeId AS PhenotypeId, \ + PublishXRef.PublicationId AS PublicationId, PublishXRef.mean AS Mean, \ + PublishXRef.Locus AS Locus, PublishXRef.LRS AS Lrs, \ + PublishXRef.additive AS Additive, PublishXRef.NSamples AS Nsamples, \ + Phenotype.Pre_publication_description AS PrePublicationDescription, \ + Phenotype.Post_publication_description AS PostPublicationDescription, \ + Phenotype.Original_description AS OriginalDescription, \ + Phenotype.Units AS Units, \ + Publication.PubMed_ID AS PubmedId, Publication.Title AS Title, \ + Publication.Authors AS Authors, Publication.Journal AS Journal, \ + Publication.Volume AS Volume, Publication.Pages AS Pages, \ + Publication.Month AS Month, Publication.Year AS Year \ + FROM PublishXRef \ + LEFT JOIN Phenotype ON Phenotype.Id = PublishXRef.PhenotypeId \ + LEFT JOIN Publication ON Publication.Id = PublishXRef.PublicationId \ + WHERE PublishXRef.InbredSetId = :group_id \ + ORDER BY PublishXRef.Id", + mysql::params! { "group_id" => group_id }, + ) +} + /// One expression (ProbeSet) dataset: a ProbeSetFreeze row, matching /// the shape of GeneNetwork2's GET /api/v_pre1/datasets/<group_name> /// response (field names there: Id, ProbeFreezeId, AvgID, @@ -585,6 +612,39 @@ pub fn get_dataset_probeset_info( ) } +/// Fetch every expression (ProbeSet) trait's metadata (see +/// ProbeSetInfo) within a dataset, the listing equivalent of +/// get_dataset_probeset_info. +pub fn get_dataset_probesets(dataset_id: u16) -> Result<Vec<ProbeSetInfo>, mysql::Error> { + let pool = pool()?; + let mut conn = pool.get_conn()?; + conn.exec_map( + "SELECT ProbeSet.Id, ProbeSet.Name, ProbeSet.Symbol, ProbeSet.description, \ + ProbeSet.Chr, ProbeSet.Mb, ProbeSetXRef.mean, ProbeSetXRef.Locus, \ + ProbeSetXRef.LRS, ProbeSetXRef.pValue, ProbeSetXRef.additive \ + FROM ProbeSet \ + JOIN ProbeSetXRef ON ProbeSetXRef.ProbeSetId = ProbeSet.Id \ + WHERE ProbeSetXRef.ProbeSetFreezeId = :dataset_id \ + ORDER BY ProbeSet.Id", + mysql::params! { "dataset_id" => dataset_id }, + |(probeset_id, name, symbol, description, chr, mb, mean, locus, lrs, p_value, additive)| { + ProbeSetInfo { + probeset_id, + name, + symbol, + description, + chr, + mb, + mean, + locus, + lrs, + p_value, + additive, + } + }, + ) +} + /// One strain's value for an expression (ProbeSet) trait, matching /// GeneNetwork2's wqflask.api.router.trait_sample_data (the ProbeSet /// branch, trimmed to just name and value); the expression equivalent diff --git a/src/lib.rs b/src/lib.rs index 4013797..4454e3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,8 +59,10 @@ pub fn app() -> Router { .routes(routes!(routes::groups::group_strains)) .routes(routes!(routes::groups::group_trait_values)) .routes(routes!(routes::groups::group_trait_info)) + .routes(routes!(routes::groups::group_traits)) .routes(routes!(routes::groups::group_datasets)) .routes(routes!(routes::datasets::dataset_by_name)) + .routes(routes!(routes::datasets::dataset_probesets)) .routes(routes!(routes::datasets::dataset_probeset_info)) .routes(routes!(routes::datasets::dataset_probeset_values)) .routes(routes!(routes::correlation::correlation)) diff --git a/src/routes/datasets.rs b/src/routes/datasets.rs index 52038b7..3e872f6 100644 --- a/src/routes/datasets.rs +++ b/src/routes/datasets.rs @@ -63,6 +63,110 @@ pub async fn dataset_by_name( }) } +/// What looking up and authorizing a dataset's probeset listing found. +/// Unlike [`DatasetProbesetLookup`], there's no single probeset to miss +/// -- a dataset with zero probesets is just an empty `Found(vec![])` -- +/// but there is still a dataset-level `Forbidden` outcome. +enum DatasetProbesetsLookup { + DatasetNotFound, + Forbidden, + Found(Vec<db::ProbeSetInfo>), +} + +fn get_dataset_probesets_result( + dataset: &str, + auth_server_url: &str, + token: &AuthToken, +) -> Result<DatasetProbesetsLookup, DatasetAuthError> { + let dataset = match db::get_dataset_by_name(dataset)? { + None => return Ok(DatasetProbesetsLookup::DatasetNotFound), + Some(dataset) => dataset, + }; + if !auth::is_publicly_readable(dataset.public, dataset.confidentiality, &dataset.create_time) { + let authorised = + auth::authorise_dataset(auth_server_url, token, &dataset.name, VIEW_RESOURCE_PRIVILEGE) + .map_err(DatasetAuthError::Auth)?; + if !authorised { + return Ok(DatasetProbesetsLookup::Forbidden); + } + } + Ok(DatasetProbesetsLookup::Found(db::get_dataset_probesets( + dataset.id, + )?)) +} + +#[utoipa::path(get, path = "/datasets/{dataset}/probesets", summary = "List the expression (ProbeSet) traits belonging to a dataset", params( + ("dataset" = String, Path, description = "Dataset Name, Name2, FullName, ShortName or numeric Id") +), responses( + (status = 200, description = "The probesets' ProbeSetXRef/ProbeSet metadata", body = Vec<db::ProbeSetInfo>), + (status = 403, description = "The dataset isn't public, and the caller isn't authorized for it", body = ApiError), + (status = 404, description = "No matching dataset", body = ApiError), + (status = 500, description = "Database error", body = ApiError), + (status = 502, description = "Could not reach gn-auth to check authorization", body = ApiError) +), security(("bearer_token" = [])))] +pub async fn dataset_probesets( + Extension(request_id): Extension<RequestId>, + Path(dataset): Path<String>, + token: AuthToken, +) -> Result<Json<Vec<db::ProbeSetInfo>>, ApiError> { + let rid = request_id_string(&request_id); + let not_found_name = dataset.clone(); + let auth_server_url = Config::load().auth_server_url.clone(); + tokio::task::spawn_blocking(move || { + get_dataset_probesets_result(&dataset, &auth_server_url, &token) + }) + .await + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "internal_error", + "Internal error", + e.to_string(), + "dataset_probesets", + ) + .with_request_id(rid.clone()) + })? + .map_err(|e| match e { + DatasetAuthError::Db(e) => ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "database_error", + "Database error", + e.to_string(), + "dataset_probesets", + ) + .with_request_id(rid.clone()), + DatasetAuthError::Auth(e) => ApiError::new( + StatusCode::BAD_GATEWAY, + "auth_server_error", + "Could not check authorization", + e, + "dataset_probesets", + ) + .with_request_id(rid.clone()), + }) + .and_then(|lookup| match lookup { + DatasetProbesetsLookup::Found(probesets) => Ok(Json(probesets)), + DatasetProbesetsLookup::Forbidden => Err(ApiError::new( + StatusCode::FORBIDDEN, + "forbidden", + "Not authorized", + format!( + "Dataset '{not_found_name}' is not public, and you are not authorized to view it." + ), + "dataset_probesets", + ) + .with_request_id(rid.clone())), + DatasetProbesetsLookup::DatasetNotFound => Err(ApiError::new( + StatusCode::NOT_FOUND, + "dataset_not_found", + "Dataset not found", + format!("No dataset matching '{not_found_name}'."), + "dataset_probesets", + ) + .with_request_id(rid)), + }) +} + /// What looking up and authorizing a probeset trait within a dataset /// found, the expression-dataset equivalent of GroupTraitLookup above. /// `Forbidden` covers "the dataset isn't public and the caller isn't diff --git a/src/routes/groups.rs b/src/routes/groups.rs index e4d512e..a27f8b2 100644 --- a/src/routes/groups.rs +++ b/src/routes/groups.rs @@ -309,6 +309,60 @@ pub async fn group_trait_info( }) } +#[utoipa::path(get, path = "/groups/{group}/traits", summary = "List the published (phenotype) traits belonging to a group", params( + ("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id") +), responses( + (status = 200, description = "The published traits belonging to the group", body = Vec<db::TraitInfo>), + (status = 404, description = "No matching group", body = ApiError), + (status = 500, description = "Database error", body = ApiError) +))] +pub async fn group_traits( + Extension(request_id): Extension<RequestId>, + Path(group): Path<String>, +) -> Result<Json<Vec<db::TraitInfo>>, ApiError> { + let rid = request_id_string(&request_id); + let not_found_name = group.clone(); + tokio::task::spawn_blocking(move || { + db::get_group_by_name(&group).and_then(|group| match group { + Some(group) => db::get_group_traits(group.id).map(Some), + None => Ok(None), + }) + }) + .await + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "internal_error", + "Internal error", + e.to_string(), + "group_traits", + ) + .with_request_id(rid.clone()) + })? + .map_err(|e| { + ApiError::new( + StatusCode::INTERNAL_SERVER_ERROR, + "database_error", + "Database error", + e.to_string(), + "group_traits", + ) + .with_request_id(rid.clone()) + })? + .map(Json) + .ok_or_else(|| { + ApiError::new( + StatusCode::NOT_FOUND, + "group_not_found", + "Group not found", + format!("No group matching '{not_found_name}'."), + "group_traits", + ) + .with_request_id(rid) + .with_next("/groups") + }) +} + #[utoipa::path(get, path = "/groups/{group}/datasets", summary = "List the expression (ProbeSet) datasets belonging to a group", params( ("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id") ), responses( |
