about summary refs log tree commit diff
path: root/src/auth.rs
AgeCommit message (Collapse)Author
4 dayslog: name the person and the credential, and resolve handles from the registryPjotr Prins
The access log said who (`user`) but not with what: with short tokens a request can be made by a shared credential, so "which person" and "which credential" are now separate fields -- `scheme` ("jwt" | "opaque") and, for an opaque token, `token` (its registry handle, e.g. `agent-ravanan-3`). Never the token or its hash, and the path still carries no query string. The line also gains `ts` (RFC 3339 UTC), so a redirected stderr file is self-describing rather than relying on journald's prefix. Hand-rolled civil-from-days rather than a new crate: one format, and no Cargo.lock drift. scripts/gn4-log.py reads such a log and shows it as a table with the handle joined against tokens.sqlite: the subject as the registry has it now (flagging a handle whose subject has been repointed), and the token's status today -- [revoked], [expired], [unknown] -- which is how "an agent was working at 14:02" is told apart from "an agent was working at 14:02 with a credential revoked since". It reads the registry read-only, passes non-JSON lines through in table mode (the process log interleaves backtraces), and offers --json and --summary for jq and for counting. CallerSlot now carries the whole Caller (scheme, id, subject) rather than just the subject string.
4 daystokens: short opaque API tokens in a single tokens.sqlitePjotr Prins
A second credential format beside gn-auth's JWT, for agents and scripts: `gn4ai_` + 56 body characters + 2 checksum characters = 64, from an alphabet with no look-alikes, so a typo is refused before the registry is consulted. Opaque by design -- nothing is stored in the token; every fact lives in one SQLite database (`tokens.sqlite`, GN4_API_TOKENS_DB, mode 0600, gitignored) keyed by the SHA-256 of the secret. Revoking is a row, takes effect on the next request, and a token in a log line reveals nothing; lose the registry and every short token stops working. Administration is local-only: GET/POST /tokens, DELETE /tokens/{id} and GET /tokens/who are behind a router layer that refuses any caller whose TCP peer is not 127.x.x.x/::1, and any request claiming to come through a proxy. A layer rather than a per-handler check, so nothing of a remote request is parsed before it is refused; there is no configuration switch, because minting credentials must never be reachable from the network. A short token reads public data only: every token-only endpoint, and `#[gnauth(secured)]` endpoints for public rows; a confidential dataset is refused with a 403 that names the JWT route, since gn-auth cannot validate an opaque token (the on-the-fly-bridge and registry-grant alternatives were both rejected for v1). scripts/gn4-token.py create|list|revoke|who drives the routes, so there is one implementation of the format and the revocation rules; the token is printed exactly once. Tests: the localhost guard over all four routes (remote peer, no peer, proxied header), and mint -> authenticate -> who -> revoke -> 401.
9 daysapi: name the caller on the response, so the log can say whoPjotr
Answering "is robw using this successfully, and the others?" from the access logs took an hour of inference -- user agents, search terms, which traits were mapped -- because nginx records the address and the subject is only in the token. Two callers can share an address (an agent and a person behind one NAT), one caller can move between addresses (an LLM's egress is a /24), and the answer for a given person was "probably 80.115.1.132". The API now sets `x-gn4-user: <token sub>` on every **authenticated** response, and the `gn4` log format records it as `user=`. The subject is only known inside the handler (the `AuthToken` extractor verifies the token there), so a middleware opens a slot in the request extensions, the extractor fills it, and the header is set on the way out; a request with no valid token gets no header, which is the honest answer and reads as `user=-`. The caller only ever sees an identity it already presented. * src/auth.rs: `USER_HEADER`, `CallerSlot`, and the extractor recording its subject. * src/lib.rs: the `tag_caller` middleware, outermost so the slot exists before any extractor runs. * tests/auth.rs: `an_authenticated_response_names_its_caller` -- the header equals the token's `sub`, and `/version` (no identity) carries none. * etc/nginx-vesuvio.pu.ac.ke.conf (and the deployed copy of it): `user=` in the log format. * README.md, docs/nginx.md (the format, an `awk` one-liner for per-user counts, and the caveat that old and new lines differ in field count), docs/www/operator-checklist.md (`user=-` means no token, not a token that failed late). 281 tests pass; the same four as before do not.
2026-10-14rustfmt: apply rustfmt across the treeAlexander_Kabui
Pure formatting sweep (import ordering, line wrapping); the tree had accumulated rustfmt drift. Separated from any semantic change so it stays reviewable and revertable on its own.
2026-10-13auth: add gnauth compile-time tags (secured/token_only/disabled)Pjotr
Adds a gnauth-macros proc-macro crate and a private-field Authorized capability type in src/auth.rs, so #[gnauth(secured)] is a real, compile-time-checked guarantee that a handler performs the gn-auth resource-authorisation round trip before sharing trait/probeset values, rather than just a comment. #[gnauth(token_only)] marks handlers that require a token but skip the round trip, and #[gnauth(disabled)] marks the two handlers that need no token at all. Also fixes groups.rs: group_trait_values, group_trait_info and group_traits previously performed no gn-auth round trip at all for published-trait data; they now resolve the group's PublishFreeze row and call the same auth::check used elsewhere, denying by default when no such row exists. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-08* src/response.rs: wrap every success response in one envelope.Pjotr
Breaking: every success body is now {"data", "meta": {"state", "took_ms", "request_id"}, "notes", "next"}; clients read `data`. Only `/` (plain text) and `/openapi.json` are unwrapped. * src/response.rs (Envelope, Meta, State, Ctx, RequestInfo): handlers take Ctx first and build the response with ctx.envelope(data), so timing and the request id are never filled in by hand. `state` is `complete` or `partial`; notes are server-authored only; `next` are concrete relative hrefs (path_segment percent-encodes names). * src/routes/*.rs, src/lib.rs (version): every handler returns Envelope<..> with next links; /search is `partial` when the locus lookup fails or a sorted 1000-hit window may be truncated, and /publications/{pubmed_id}/traits when datasets were left out for authorization. * src/error.rs (ApiError): add `took_ms`, an RFC 7807 extension member. * src/auth.rs (AuthToken): the 401 now carries request_id and took_ms (it never had a request_id); Ctx stores RequestInfo in the request extensions for it. * tests: common::data / envelope_data parse every success body through the envelope; tests/envelope.rs and test/python/test_envelope.py check the shape, errors, and that every `next` link resolves; tests/envelope_partial.rs checks the `partial` search without a database; tests/openapi.rs fails if a JSON success response is not an Envelope_* schema. * README.md, docs/SKILL.md, docs/gn-auth.md, RELEASE_NOTES.md: document the envelope and took_ms on errors. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-06* src/auth.rs (jwks, caches): factor complex types into aliases.Alexander_Kabui
Clippy type_complexity fired three times on the OnceLock<Mutex< HashMap<...>> statics. One shared alias per cache shape keeps the signatures readable without changing any behaviour.
2026-10-04Add GET /traits/{dataset}/... spanning ProbeSet and Publish dataPjotr Prins
A new, additional endpoint family that addresses both trait categories the same way -- (dataset, trait) -- matching GeneNetwork2's own generic wqflask.api.router.trait_sample_data/get_trait_info, rather than the two category-specific shapes genenetwork4 already has (/datasets/{dataset}/probesets... and /groups/{group}/traits...), which are unchanged and keep working exactly as before. - db::resolve_trait_dataset(name) resolves a dataset name against ProbeSetFreeze first, then the new db::PublishDataset (a PublishFreeze row, via the new get_publish_freeze_by_name) -- whichever matches decides the category. This means only dataset names are ever "guessed" across categories, not trait names/ids, which is the much smaller and safer surface: GN2's own equivalent tries ProbeSet.Name and PublishXRef.Id against the same bare trait string and silently falls back, which can return the wrong trait on a same-named collision. get_publish_freeze_by_name accepts a PublishFreeze's own Name/ShortName/Id or its owning group's Name/InbredSetName/FullName/ Id, mirroring GN2's own flexible matching (e.g. "BXDPublish" or "BXD"). - GET /traits/{dataset}: list every trait in a dataset, tagged by category. - GET /traits/{dataset}/{trait}: one trait's metadata. A non-numeric trait against a Publish dataset is a 404 (can't match a PublishXRef.Id), not a 500. - GET /traits/{dataset}/{trait}/values: that trait's per-strain values. TraitValue and ProbeSetValue are already identical in shape, so this unifies with no information loss, unlike the metadata endpoint above. Metadata responses are a tagged union ({"category": "probeset"| "publish", ...}) wrapping ProbeSetInfo/TraitInfo unchanged, rather than a lossy common-fields schema -- the two share little beyond mean/locus/lrs/additive. utoipa generates this as a oneOf with a category discriminator, verified against /openapi.json. Gated the same way as /datasets/{dataset}/probesets..., extended to Publish data for the first time: /groups/{group}/traits itself is still not gated, but reaching the same data through /traits/{group} now requires a token and checks PublishFreeze.public/confidentiality. No gn-auth-side change was needed -- POST /auth/data/authorisation already dispatches *Publish-suffixed dataset names to its phenotype resource resolver (gn_auth/auth/authorisation/data/phenotypes.py), the exact mechanism this integration already relies on for mRNA data. auth::VIEW_RESOURCE_PRIVILEGE moves from routes::datasets (private) to routes::auth (pub) so routes::traits can reuse it instead of duplicating the string. tests/traits.rs and its Python mirror cover both categories (including both ways of addressing a Publish dataset), the 401/403/404 cases, and the non-numeric-trait-against-Publish-dataset edge case, run against a real gn-auth instance. The Publish branch's deny path itself is only exercised via gn-auth's "unlinked dataset" fallback (already proven for ProbeSet) -- no non-public PublishFreeze exists in the database this was tested against. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d
2026-10-04Add GET /groups/{group}/traits and GET /datasets/{dataset}/probesetsPjotr Prins
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
2026-10-04Gate GET /datasets/{dataset}/probesets/{probeset} on gn-authPjotr Prins
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
2026-10-04Add token verification and gn-auth resource authorization.Pjotr Prins
* Cargo.toml (jsonwebtoken, ureq): New dependencies, RS256 JWT verification and gn-auth's HTTP API respectively. ureq pulls in ring for RSA verification, needing a C compiler at build time -- already provided by cargo-build-system's standard inputs, nothing added to guix.scm for it. Promote serde_json from dev-dependencies to dependencies; src/auth.rs needs it outside tests now. (mysql-common-derive, simple_asn1, time): Pin to the newest versions still compatible with the Guix-packaged rustc 1.85.1 (same trap as before: ureq's dependency chain pulls darling 0.23/time 0.3.46+, both needing rustc 1.88). * rust-package.scm: Regenerate with `guix import crate -f Cargo.lock genenetwork4`. * guix.scm (genenetwork4-git)[native-inputs]: Update crate list to match. * src/auth.rs (AuthToken): New extractor. Verifies a bearer token's RS256 signature and standard time claims (exp/nbf, checked only when present -- gn-auth's own validator treats them as optional too) against gn-auth's JWKS, cached and refreshed periodically (OnceLock<Mutex<HashMap<...>>>, same shape as db::pool()) rather than fetched per request. (is_publicly_readable): New function. Matches GeneNetwork2's own threshold (public > 0 AND confidentiality < 1, used throughout wqflask/api/router.py) for whether a dataset needs an authorization check at all; InbredSet/group rows carry a public column too, but GN2 never filters on it, so groups/species endpoints need no gating. (authorise): New function. Wraps gn-auth's POST /auth/resource/authorisation, caching the per-(user, resource) answer briefly (AUTHORISATION_TTL) so repeated requests for the same resource by the same user don't each cost a round trip. Not yet called from any route -- see RELEASE_NOTES.md. * src/config.rs (Config)[auth_server_url]: New field, AUTH_SERVER_URL config key, matching GeneNetwork3's own key of the same name. * src/error.rs (ApiError): Derive Debug; needed by tests/auth.rs's .unwrap() on a Result<_, ApiError>, and every error type should implement it regardless. * src/lib.rs: Register the new auth module. * tests/auth.rs: New file. Runs AuthToken verification and authorise() (including its cache) against a real gn-auth instance; set GN4_TEST_AUTH_TOKEN/GN4_TEST_RESOURCE_ID (see docs/gn-auth.md), skips otherwise. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d