about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-10-04 11:37:55 +0000
committerPjotr <wrk@bosa.lan>2026-10-04 11:37:55 +0000
commit859a86bf39df6eafa6988215cb84556fc9623ea0 (patch)
treee31e62d0d891dce85be8a09d5094d9d8f06cf7e0 /src
parent0a58bfdbec94d3c249a905226bd33480198f2e95 (diff)
downloadgenenetwork4-859a86bf39df6eafa6988215cb84556fc9623ea0.tar.gz
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d
Diffstat (limited to 'src')
-rw-r--r--src/auth.rs182
-rw-r--r--src/lib.rs24
-rw-r--r--src/routes/datasets.rs106
3 files changed, 288 insertions, 24 deletions
diff --git a/src/auth.rs b/src/auth.rs
index ccbefe4..d9cc8c4 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -166,6 +166,10 @@ fn fetch_jwks(auth_server_url: &str) -> Result<Vec<DecodingKey>, String> {
         .collect()
 }
 
+/// A dataset older than this is treated as publicly readable
+/// regardless of its `public`/`confidentiality` flags.
+const PUBLIC_AFTER_YEARS: i64 = 5;
+
 /// Matches GeneNetwork2's own threshold for whether a dataset is
 /// readable without authorization (e.g. `ProbeSetFreeze.public > 0 AND
 /// ProbeSetFreeze.confidentiality < 1`, used throughout
@@ -173,8 +177,51 @@ fn fetch_jwks(auth_server_url: &str) -> Result<Vec<DecodingKey>, String> {
 /// column too, but GeneNetwork2 never actually filters on it -- it's
 /// informational only there -- so this only applies to dataset-level
 /// (`*Freeze`) rows, which carry both columns.
-pub fn is_publicly_readable(public: u8, confidentiality: u8) -> bool {
-    public > 0 && confidentiality < 1
+///
+/// Additionally, a dataset older than `PUBLIC_AFTER_YEARS` is treated
+/// as public regardless of those flags -- confidentiality embargoes
+/// are not meant to last forever. `create_time` is the dataset's
+/// `CreateTime` column, as `YYYY-MM-DD ...`; if it can't be parsed,
+/// this falls back to the flags alone.
+pub fn is_publicly_readable(public: u8, confidentiality: u8, create_time: &str) -> bool {
+    (public > 0 && confidentiality < 1) || is_older_than_public_after_years(create_time)
+}
+
+fn is_older_than_public_after_years(create_time: &str) -> bool {
+    let Some((year, month, day)) = parse_date_prefix(create_time) else {
+        return false;
+    };
+    let created_days = days_from_civil(year, month, day);
+    let now_days = match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
+        Ok(since_epoch) => (since_epoch.as_secs() / 86400) as i64,
+        Err(_) => return false,
+    };
+    now_days - created_days >= PUBLIC_AFTER_YEARS * 365
+}
+
+/// Parse the `YYYY-MM-DD` prefix of a `CreateTime` value such as
+/// `"2015-03-04 00:00:00"`.
+fn parse_date_prefix(s: &str) -> Option<(i64, i64, i64)> {
+    let mut parts = s.get(0..10)?.split('-');
+    let year: i64 = parts.next()?.parse().ok()?;
+    let month: i64 = parts.next()?.parse().ok()?;
+    let day: i64 = parts.next()?.parse().ok()?;
+    Some((year, month, day))
+}
+
+/// Days since the Unix epoch (1970-01-01) for a date in the proleptic
+/// Gregorian calendar. Howard Hinnant's `days_from_civil`
+/// (https://howardhinnant.github.io/date_algorithms.html) -- used here
+/// instead of a date/time crate since this is the only place a date
+/// needs parsing.
+fn days_from_civil(y: i64, m: i64, d: i64) -> i64 {
+    let y = if m <= 2 { y - 1 } else { y };
+    let era = if y >= 0 { y } else { y - 399 } / 400;
+    let yoe = y - era * 400;
+    let mp = (m + 9) % 12;
+    let doy = (153 * mp + 2) / 5 + d - 1;
+    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
+    era * 146097 + doe - 719468
 }
 
 #[derive(Serialize)]
@@ -284,15 +331,138 @@ fn fetch_resource_privileges(
         .unwrap_or_default())
 }
 
+#[derive(Serialize)]
+struct DataAuthorisationRequest<'a> {
+    traits: [&'a str; 1],
+}
+
+#[derive(Deserialize)]
+struct DataAuthorisationResponse {
+    #[serde(default)]
+    authorisation: Vec<TraitAuthorisation>,
+}
+
+#[derive(Deserialize)]
+struct TraitAuthorisation {
+    #[serde(default)]
+    privileges: Vec<String>,
+}
+
+/// Ask gn-auth whether `token`'s holder has `privilege` on the trait
+/// `dataset_name::trait_name` (`POST /auth/data/authorisation`; see
+/// docs/gn-auth.md). This is the dataset/trait-keyed equivalent of
+/// [`authorise`], for data addressed by a `(dataset, probeset)` pair
+/// rather than a gn-auth resource id directly -- gn-auth resolves the
+/// resource itself from the trait's dataset name. Cached per (sub,
+/// dataset::trait) for `AUTHORISATION_TTL`, same as [`authorise`].
+///
+/// Only call this for data that is *not* [`is_publicly_readable`];
+/// public data needs a valid token (use [`AuthToken`] alone) but no
+/// per-resource check.
+pub fn authorise_trait(
+    auth_server_url: &str,
+    token: &AuthToken,
+    dataset_name: &str,
+    trait_name: &str,
+    privilege: &str,
+) -> Result<bool, String> {
+    let privileges = trait_privileges(auth_server_url, token, dataset_name, trait_name)?;
+    Ok(privileges.iter().any(|p| p == privilege))
+}
+
+fn trait_privileges(
+    auth_server_url: &str,
+    token: &AuthToken,
+    dataset_name: &str,
+    trait_name: &str,
+) -> Result<Vec<String>, String> {
+    static CACHE: OnceLock<Mutex<HashMap<(String, String), (Instant, Vec<String>)>>> =
+        OnceLock::new();
+    let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
+    let key = (token.sub.clone(), format!("{dataset_name}::{trait_name}"));
+
+    {
+        let guard = cache.lock().unwrap();
+        if let Some((fetched_at, privileges)) = guard.get(&key) {
+            if fetched_at.elapsed() < AUTHORISATION_TTL {
+                return Ok(privileges.clone());
+            }
+        }
+    }
+
+    let privileges = fetch_trait_privileges(auth_server_url, token, dataset_name, trait_name)?;
+    cache
+        .lock()
+        .unwrap()
+        .insert(key, (Instant::now(), privileges.clone()));
+    Ok(privileges)
+}
+
+fn fetch_trait_privileges(
+    auth_server_url: &str,
+    token: &AuthToken,
+    dataset_name: &str,
+    trait_name: &str,
+) -> Result<Vec<String>, String> {
+    let url = format!(
+        "{}/auth/data/authorisation",
+        auth_server_url.trim_end_matches('/')
+    );
+    let trait_fullname = format!("{dataset_name}::{trait_name}");
+    let body = DataAuthorisationRequest {
+        traits: [&trait_fullname],
+    };
+    // See fetch_resource_privileges for why this isn't .send_json().
+    let json = serde_json::to_vec(&body).map_err(|e| format!("encoding request body: {e}"))?;
+    let sent = ureq::post(&url)
+        .header("Authorization", &format!("Bearer {}", token.raw))
+        .content_type("application/json")
+        .send(json.as_slice());
+    let mut response = match sent {
+        Ok(response) => response,
+        // gn-auth answers a trait with no linked resource (i.e. one
+        // nobody has ever restricted) with a 404, not an empty
+        // privilege list -- treat it the same as "no privileges" here
+        // rather than as a request failure.
+        Err(ureq::Error::StatusCode(404)) => return Ok(Vec::new()),
+        Err(e) => return Err(format!("calling {url}: {e}")),
+    };
+    let doc: DataAuthorisationResponse = response
+        .body_mut()
+        .read_json()
+        .map_err(|e| format!("parsing authorisation response from {url}: {e}"))?;
+    Ok(doc
+        .authorisation
+        .into_iter()
+        .flat_map(|t| t.privileges)
+        .collect())
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
 
     #[test]
     fn is_publicly_readable_matches_genenetwork2_threshold() {
-        assert!(is_publicly_readable(1, 0));
-        assert!(is_publicly_readable(2, 0));
-        assert!(!is_publicly_readable(0, 0));
-        assert!(!is_publicly_readable(1, 1));
+        let recent = "2024-01-01 00:00:00";
+        assert!(is_publicly_readable(1, 0, recent));
+        assert!(is_publicly_readable(2, 0, recent));
+        assert!(!is_publicly_readable(0, 0, recent));
+        assert!(!is_publicly_readable(1, 1, recent));
+    }
+
+    #[test]
+    fn is_publicly_readable_also_true_past_the_embargo_age() {
+        assert!(is_publicly_readable(0, 1, "2000-01-01 00:00:00"));
+        assert!(!is_publicly_readable(0, 1, "2024-01-01 00:00:00"));
+        // Unparseable create_time: fall back to the flags alone.
+        assert!(!is_publicly_readable(0, 1, "not-a-date"));
+    }
+
+    #[test]
+    fn days_from_civil_matches_known_dates() {
+        assert_eq!(days_from_civil(1970, 1, 1), 0);
+        assert_eq!(days_from_civil(2000, 3, 1), 11017);
+        assert_eq!(days_from_civil(1969, 12, 31), -1);
     }
 }
diff --git a/src/lib.rs b/src/lib.rs
index 8377643..4013797 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -21,9 +21,31 @@ use error::ApiError;
     title = "GeneNetwork4 API",
     description = "GeneNetwork4 REST API",
     license(name = "AGPL-3.0-or-later")
-))]
+), modifiers(&SecurityAddon))]
 struct ApiDoc;
 
+/// Registers the "bearer_token" scheme used by endpoints that require a
+/// gn-auth token (see `security(...)` on their `#[utoipa::path]`, e.g.
+/// `routes::datasets::dataset_probeset_info`).
+struct SecurityAddon;
+
+impl utoipa::Modify for SecurityAddon {
+    fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
+        use utoipa::openapi::security::{HttpAuthScheme, HttpBuilder, SecurityScheme};
+        if let Some(components) = openapi.components.as_mut() {
+            components.add_security_scheme(
+                "bearer_token",
+                SecurityScheme::Http(
+                    HttpBuilder::new()
+                        .scheme(HttpAuthScheme::Bearer)
+                        .bearer_format("JWT")
+                        .build(),
+                ),
+            );
+        }
+    }
+}
+
 /// Build the application router. Public so integration tests (tests/) and
 /// the binary (src/main.rs) can both use it without binding a real socket.
 pub fn app() -> Router {
diff --git a/src/routes/datasets.rs b/src/routes/datasets.rs
index f1b5671..dd6adc5 100644
--- a/src/routes/datasets.rs
+++ b/src/routes/datasets.rs
@@ -6,10 +6,15 @@ use axum::extract::{Extension, Path};
 use axum::http::StatusCode;
 use tower_http::request_id::RequestId;
 
+use crate::auth::{self, AuthToken};
+use crate::config::Config;
 use crate::db;
 use crate::error::ApiError;
 use crate::request_id_string;
 
+/// The gn-auth privilege required to view a non-public probeset.
+const VIEW_RESOURCE_PRIVILEGE: &str = "group:resource:view-resource";
+
 #[utoipa::path(get, path = "/datasets/{dataset}", summary = "Get one expression dataset's full record by name or numeric id", params(
     ("dataset" = String, Path, description = "Dataset Name, Name2, FullName, ShortName or numeric Id")
 ), responses(
@@ -66,17 +71,59 @@ enum DatasetProbesetLookup<T> {
     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),
+}
+
+/// Either a database error, or a failure to reach/parse gn-auth's
+/// answer while checking authorization for a non-public dataset.
+enum DatasetAuthError {
+    Db(mysql::Error),
+    Auth(String),
+}
+
+impl From<mysql::Error> for DatasetAuthError {
+    fn from(e: mysql::Error) -> Self {
+        DatasetAuthError::Db(e)
+    }
+}
+
 fn get_dataset_probeset_info_result(
     dataset: &str,
     probeset: &str,
-) -> Result<DatasetProbesetLookup<db::ProbeSetInfo>, mysql::Error> {
-    match db::get_dataset_by_name(dataset)? {
-        None => Ok(DatasetProbesetLookup::DatasetNotFound),
-        Some(dataset) => match db::get_dataset_probeset_info(dataset.id, probeset)? {
-            None => Ok(DatasetProbesetLookup::ProbesetNotFound),
-            Some(info) => Ok(DatasetProbesetLookup::Found(info)),
-        },
+    auth_server_url: &str,
+    token: &AuthToken,
+) -> Result<DatasetProbesetInfoLookup, DatasetAuthError> {
+    let dataset = match db::get_dataset_by_name(dataset)? {
+        None => return Ok(DatasetProbesetInfoLookup::DatasetNotFound),
+        Some(dataset) => dataset,
+    };
+    let info = match db::get_dataset_probeset_info(dataset.id, probeset)? {
+        None => return Ok(DatasetProbesetInfoLookup::ProbesetNotFound),
+        Some(info) => info,
+    };
+    if !auth::is_publicly_readable(dataset.public, dataset.confidentiality, &dataset.create_time) {
+        let authorised = auth::authorise_trait(
+            auth_server_url,
+            token,
+            &dataset.name,
+            &info.name,
+            VIEW_RESOURCE_PRIVILEGE,
+        )
+        .map_err(DatasetAuthError::Auth)?;
+        if !authorised {
+            return Ok(DatasetProbesetInfoLookup::Forbidden);
+        }
     }
+    Ok(DatasetProbesetInfoLookup::Found(info))
 }
 
 #[utoipa::path(get, path = "/datasets/{dataset}/probesets/{probeset}", summary = "Get one expression trait's ProbeSetXRef/ProbeSet metadata", params(
@@ -84,18 +131,24 @@ fn get_dataset_probeset_info_result(
     ("probeset" = String, Path, description = "ProbeSet.Name within the dataset")
 ), responses(
     (status = 200, description = "The probeset's ProbeSetXRef/ProbeSet metadata", body = 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, 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_info(
     Extension(request_id): Extension<RequestId>,
     Path((dataset, probeset)): Path<(String, String)>,
+    token: AuthToken,
 ) -> Result<Json<db::ProbeSetInfo>, 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_info_result(&dataset, &probeset))
-        .await
+    let auth_server_url = Config::load().auth_server_url.clone();
+    tokio::task::spawn_blocking(move || {
+        get_dataset_probeset_info_result(&dataset, &probeset, &auth_server_url, &token)
+    })
+    .await
         .map_err(|e| {
             ApiError::new(
                 StatusCode::INTERNAL_SERVER_ERROR,
@@ -106,19 +159,38 @@ pub async fn dataset_probeset_info(
             )
             .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_info",
             )
-            .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_info",
+            )
+            .with_request_id(rid.clone()),
         })
         .and_then(|lookup| match lookup {
-            DatasetProbesetLookup::Found(info) => Ok(Json(info)),
-            DatasetProbesetLookup::DatasetNotFound => Err(ApiError::new(
+            DatasetProbesetInfoLookup::Found(info) => Ok(Json(info)),
+            DatasetProbesetInfoLookup::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_info",
+            )
+            .with_request_id(rid.clone())),
+            DatasetProbesetInfoLookup::DatasetNotFound => Err(ApiError::new(
                 StatusCode::NOT_FOUND,
                 "dataset_not_found",
                 "Dataset not found",
@@ -126,7 +198,7 @@ pub async fn dataset_probeset_info(
                 "dataset_probeset_info",
             )
             .with_request_id(rid.clone())),
-            DatasetProbesetLookup::ProbesetNotFound => Err(ApiError::new(
+            DatasetProbesetInfoLookup::ProbesetNotFound => Err(ApiError::new(
                 StatusCode::NOT_FOUND,
                 "probeset_not_found",
                 "Probeset not found",