diff options
| author | Pjotr Prins <pjotr.public01@thebird.nl> | 2026-10-04 13:32:08 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-10-04 13:32:08 +0000 |
| commit | 21c69c1051715e6964fe9d4c8e06a326344fe6eb (patch) | |
| tree | 5e8fbf9892de09c9d095ae32c39de2e149195292 /src | |
| parent | dfbf105652140d52e50a96853f0a7ab91fec0776 (diff) | |
| download | genenetwork4-21c69c1051715e6964fe9d4c8e06a326344fe6eb.tar.gz | |
Gate GET /datasets/{dataset}/probesets/{probeset}/values on gn-auth
Same treatment as dataset_probeset_info: require a valid AuthToken unconditionally, and for a dataset that isn't publicly readable, check group:resource:view-resource via authorise_trait -- denied is a 403, gn-auth unreachable is a 502. Unify DatasetProbesetLookup/DatasetProbesetInfoLookup back into one generic enum (with the new Forbidden variant) now that both dataset_probeset_info and dataset_probeset_values need it, rather than keeping two near-identical enums. tests/dataset_probeset_values.rs and its Python mirror gain the same 401/403 cases as dataset_probeset_info's, run against a real gn-auth instance; the 403 case reuses UTHSC_BXD_Cochlea_0225, confirmed to have both ProbeSet metadata and ProbeSetData values rows so the test exercises the authorization check rather than ProbesetNotFound. 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/routes/datasets.rs | 110 |
1 files changed, 70 insertions, 40 deletions
diff --git a/src/routes/datasets.rs b/src/routes/datasets.rs index dd6adc5..52038b7 100644 --- a/src/routes/datasets.rs +++ b/src/routes/datasets.rs @@ -63,24 +63,16 @@ pub async fn dataset_by_name( }) } -/// What looking up a probeset trait within a dataset found, the -/// expression-dataset equivalent of GroupTraitLookup above. +/// 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 +/// authorized for it" -- both `dataset_probeset_info` and +/// `dataset_probeset_values` gate on gn-auth. enum DatasetProbesetLookup<T> { DatasetNotFound, ProbesetNotFound, - Found(T), -} - -/// What looking up and authorizing a probeset trait found. Unlike -/// [`DatasetProbesetLookup`], this also covers the "dataset isn't -/// public and the caller isn't authorized for it" outcome, since -/// `dataset_probeset_info` (unlike `dataset_probeset_values`) gates on -/// gn-auth. -enum DatasetProbesetInfoLookup { - DatasetNotFound, - ProbesetNotFound, Forbidden, - Found(db::ProbeSetInfo), + Found(T), } /// Either a database error, or a failure to reach/parse gn-auth's @@ -101,13 +93,13 @@ fn get_dataset_probeset_info_result( probeset: &str, auth_server_url: &str, token: &AuthToken, -) -> Result<DatasetProbesetInfoLookup, DatasetAuthError> { +) -> Result<DatasetProbesetLookup<db::ProbeSetInfo>, DatasetAuthError> { let dataset = match db::get_dataset_by_name(dataset)? { - None => return Ok(DatasetProbesetInfoLookup::DatasetNotFound), + None => return Ok(DatasetProbesetLookup::DatasetNotFound), Some(dataset) => dataset, }; let info = match db::get_dataset_probeset_info(dataset.id, probeset)? { - None => return Ok(DatasetProbesetInfoLookup::ProbesetNotFound), + None => return Ok(DatasetProbesetLookup::ProbesetNotFound), Some(info) => info, }; if !auth::is_publicly_readable(dataset.public, dataset.confidentiality, &dataset.create_time) { @@ -120,10 +112,10 @@ fn get_dataset_probeset_info_result( ) .map_err(DatasetAuthError::Auth)?; if !authorised { - return Ok(DatasetProbesetInfoLookup::Forbidden); + return Ok(DatasetProbesetLookup::Forbidden); } } - Ok(DatasetProbesetInfoLookup::Found(info)) + Ok(DatasetProbesetLookup::Found(info)) } #[utoipa::path(get, path = "/datasets/{dataset}/probesets/{probeset}", summary = "Get one expression trait's ProbeSetXRef/ProbeSet metadata", params( @@ -178,8 +170,8 @@ pub async fn dataset_probeset_info( .with_request_id(rid.clone()), }) .and_then(|lookup| match lookup { - DatasetProbesetInfoLookup::Found(info) => Ok(Json(info)), - DatasetProbesetInfoLookup::Forbidden => Err(ApiError::new( + DatasetProbesetLookup::Found(info) => Ok(Json(info)), + DatasetProbesetLookup::Forbidden => Err(ApiError::new( StatusCode::FORBIDDEN, "forbidden", "Not authorized", @@ -190,7 +182,7 @@ pub async fn dataset_probeset_info( "dataset_probeset_info", ) .with_request_id(rid.clone())), - DatasetProbesetInfoLookup::DatasetNotFound => Err(ApiError::new( + DatasetProbesetLookup::DatasetNotFound => Err(ApiError::new( StatusCode::NOT_FOUND, "dataset_not_found", "Dataset not found", @@ -198,7 +190,7 @@ pub async fn dataset_probeset_info( "dataset_probeset_info", ) .with_request_id(rid.clone())), - DatasetProbesetInfoLookup::ProbesetNotFound => Err(ApiError::new( + DatasetProbesetLookup::ProbesetNotFound => Err(ApiError::new( StatusCode::NOT_FOUND, "probeset_not_found", "Probeset not found", @@ -212,18 +204,31 @@ pub async fn dataset_probeset_info( fn get_dataset_probeset_values_result( dataset: &str, probeset: &str, -) -> Result<DatasetProbesetLookup<Vec<db::ProbeSetValue>>, mysql::Error> { - match db::get_dataset_by_name(dataset)? { - None => Ok(DatasetProbesetLookup::DatasetNotFound), - Some(dataset) => { - let values = db::get_dataset_probeset_values(dataset.id, probeset)?; - if values.is_empty() { - Ok(DatasetProbesetLookup::ProbesetNotFound) - } else { - Ok(DatasetProbesetLookup::Found(values)) - } + auth_server_url: &str, + token: &AuthToken, +) -> Result<DatasetProbesetLookup<Vec<db::ProbeSetValue>>, DatasetAuthError> { + let dataset = match db::get_dataset_by_name(dataset)? { + None => return Ok(DatasetProbesetLookup::DatasetNotFound), + Some(dataset) => dataset, + }; + let values = db::get_dataset_probeset_values(dataset.id, probeset)?; + if values.is_empty() { + return Ok(DatasetProbesetLookup::ProbesetNotFound); + } + if !auth::is_publicly_readable(dataset.public, dataset.confidentiality, &dataset.create_time) { + let authorised = auth::authorise_trait( + auth_server_url, + token, + &dataset.name, + probeset, + VIEW_RESOURCE_PRIVILEGE, + ) + .map_err(DatasetAuthError::Auth)?; + if !authorised { + return Ok(DatasetProbesetLookup::Forbidden); } } + Ok(DatasetProbesetLookup::Found(values)) } #[utoipa::path(get, path = "/datasets/{dataset}/probesets/{probeset}/values", summary = "Get one expression trait's value in each strain in the dataset", params( @@ -231,18 +236,24 @@ fn get_dataset_probeset_values_result( ("probeset" = String, Path, description = "ProbeSet.Name within the dataset") ), responses( (status = 200, description = "The dataset's per-strain values for the probeset", body = Vec<db::ProbeSetValue>), + (status = 403, description = "The dataset isn't public, and the caller isn't authorized for it", body = ApiError), (status = 404, description = "No matching dataset, or no matching probeset in that dataset", body = ApiError), - (status = 500, description = "Database error", 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_probeset_values( Extension(request_id): Extension<RequestId>, Path((dataset, probeset)): Path<(String, String)>, + token: AuthToken, ) -> Result<Json<Vec<db::ProbeSetValue>>, ApiError> { let rid = request_id_string(&request_id); let not_found_dataset = dataset.clone(); let not_found_probeset = probeset.clone(); - tokio::task::spawn_blocking(move || get_dataset_probeset_values_result(&dataset, &probeset)) - .await + let auth_server_url = Config::load().auth_server_url.clone(); + tokio::task::spawn_blocking(move || { + get_dataset_probeset_values_result(&dataset, &probeset, &auth_server_url, &token) + }) + .await .map_err(|e| { ApiError::new( StatusCode::INTERNAL_SERVER_ERROR, @@ -253,18 +264,37 @@ pub async fn dataset_probeset_values( ) .with_request_id(rid.clone()) })? - .map_err(|e| { - ApiError::new( + .map_err(|e| match e { + DatasetAuthError::Db(e) => ApiError::new( StatusCode::INTERNAL_SERVER_ERROR, "database_error", "Database error", e.to_string(), "dataset_probeset_values", ) - .with_request_id(rid.clone()) + .with_request_id(rid.clone()), + DatasetAuthError::Auth(e) => ApiError::new( + StatusCode::BAD_GATEWAY, + "auth_server_error", + "Could not check authorization", + e, + "dataset_probeset_values", + ) + .with_request_id(rid.clone()), }) .and_then(|lookup| match lookup { DatasetProbesetLookup::Found(values) => Ok(Json(values)), + DatasetProbesetLookup::Forbidden => Err(ApiError::new( + StatusCode::FORBIDDEN, + "forbidden", + "Not authorized", + format!( + "'{not_found_probeset}' in dataset '{not_found_dataset}' is not public, \ + and you are not authorized to view it." + ), + "dataset_probeset_values", + ) + .with_request_id(rid.clone())), DatasetProbesetLookup::DatasetNotFound => Err(ApiError::new( StatusCode::NOT_FOUND, "dataset_not_found", |
