about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-06 17:10:04 +0000
committerPjotr <wrk@bosa.lan>2026-11-06 17:10:04 +0000
commitfc59b08510022ed6eeb07a39481ae9918a62561f (patch)
tree7ad6d03d62054a49341a3d39c698afc918d9a54a /scripts
parent485531c9cf2185e81ba27509dec8ac96467feb21 (diff)
downloadgenenetwork4-fc59b08510022ed6eeb07a39481ae9918a62561f.tar.gz
tests: run against a real database and index, with a gn-auth stub
The suites skip rather than fail whatever needs a database, an index or
a token they cannot get, which is right in the build sandbox but leaves
most endpoints unexercised. With a live MariaDB, a full Xapian index
(GN4_FULL_INDEX) and a token, the same commands run the whole suite:
225 Rust tests pass with 0 failures, and the Python suite runs all 94
with no skips -- as against 77 skipped without them.

The token was the only piece with no local stand-in, so add
scripts/gn-auth-stub.py: it serves the JWKS matching the committed test
key, answers /auth/token (so POST /get-test-token works) and answers
both authorisation endpoints with "no privileges", the stranger case
the non-public 403 tests assert. --grant-resource-id grants one
resource id to the fixture token for the owner-side tests.

* scripts/gn-auth-stub.py: new file.
* README.md (Tests): "Against a real database and index", what the
three inputs are, the full run, and what still skips and why.
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/gn-auth-stub.py212
1 files changed, 212 insertions, 0 deletions
diff --git a/scripts/gn-auth-stub.py b/scripts/gn-auth-stub.py
new file mode 100755
index 0000000..a9f6abd
--- /dev/null
+++ b/scripts/gn-auth-stub.py
@@ -0,0 +1,212 @@
+#!/usr/bin/env python3
+"""A local stand-in for gn-auth, so the test suites can be run fully --
+against a real database and a real index -- without a gn-auth instance.
+
+The suites skip (rather than fail) anything that needs a token they
+cannot get, which is the right behaviour in the Guix build sandbox but
+leaves most endpoints untested there. Set `GN4_TEST_AUTH_TOKEN` and this
+stub instead, and they run:
+
+    scripts/gn-auth-stub.py &                  # prints the token
+    export GN4_TEST_AUTH_TOKEN=$(curl -s -X POST \\
+        http://127.0.0.1:8081/auth/token | python3 -c \\
+        'import json,sys; print(json.load(sys.stdin)["access_token"])')
+
+What it answers, and why each is needed:
+
+- `GET /auth/public-jwks` -- the key set the token is verified against.
+  The token is signed with `test/data/test_rsa_private.pem`, whose
+  public half is `test/data/test_jwks.json`: the same fixture pair
+  `tests/search_native_db.rs` spins up in-process.
+- `POST /auth/token` -- gn-auth's password grant, so `POST
+  /get-test-token` (src/routes/dev.rs) can serve a token, which is how
+  `tests/*.rs` and `test/python/test_*.py` obtain one themselves.
+- `POST /auth/resource/authorisation` and `POST /auth/data/authorisation`
+  -- resource- and trait-level privilege lookups (src/auth.rs). They
+  answer "no privileges" by default, i.e. the stranger/anonymous case,
+  which is what the non-public endpoints' 403 tests assert. Pass
+  `--grant-resource-id` to grant `group:resource:view-resource` on one
+  resource id to the fixture token, for the owner-side tests
+  (`GN4_TEST_RESOURCE_ID` must name the same id).
+
+`iss` has to match genenetwork4's `AUTH_SERVER_URL` exactly (it is
+passed to `Validation::set_issuer` verbatim), including the trailing
+slash the default `http://127.0.0.1:8081/` carries -- that is what
+`--port`/`--issuer` are for.
+"""
+
+import argparse
+import base64
+import json
+import os
+import subprocess
+import time
+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+
+REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+JWKS_FILE = os.path.join(REPO, "test", "data", "test_jwks.json")
+KEY_FILE = os.path.join(REPO, "test", "data", "test_rsa_private.pem")
+
+VIEW_RESOURCE_PRIVILEGE = "group:resource:view-resource"
+
+
+def b64u(raw):
+    return base64.urlsafe_b64encode(raw).rstrip(b"=").decode()
+
+
+def sign(issuer, kid, subject, scope):
+    """An RS256 token matching test_jwks.json, in gn-auth's claim shape."""
+    now = int(time.time())
+    header = {"alg": "RS256", "typ": "JWT", "kid": kid}
+    claims = {
+        "sub": subject,
+        "scope": scope,
+        "iss": issuer,
+        "iat": now,
+        "exp": now + 86400,
+    }
+    signing_input = ".".join(
+        b64u(json.dumps(part, separators=(",", ":")).encode())
+        for part in (header, claims)
+    )
+    signature = subprocess.run(
+        ["openssl", "dgst", "-sha256", "-sign", KEY_FILE],
+        input=signing_input.encode(),
+        capture_output=True,
+        check=True,
+    ).stdout
+    return signing_input + "." + b64u(signature)
+
+
+def handler_for(token, grant_ids, jwks_document):
+    class Handler(BaseHTTPRequestHandler):
+        def _json(self, payload, status=200):
+            body = json.dumps(payload).encode()
+            self.send_response(status)
+            self.send_header("Content-Type", "application/json")
+            self.send_header("Content-Length", str(len(body)))
+            self.end_headers()
+            self.wfile.write(body)
+
+        def _body(self):
+            length = int(self.headers.get("Content-Length") or 0)
+            return self.rfile.read(length) if length else b""
+
+        def do_GET(self):
+            if self.path.rstrip("/") == "/auth/public-jwks":
+                body = jwks_document.encode()
+                self.send_response(200)
+                self.send_header("Content-Type", "application/json")
+                self.send_header("Content-Length", str(len(body)))
+                self.end_headers()
+                self.wfile.write(body)
+            else:
+                self._json({"error": "not found"}, 404)
+
+        def do_POST(self):
+            body = self._body()
+            path = self.path.rstrip("/")
+            if path == "/auth/token":
+                self._json(
+                    {
+                        "access_token": token,
+                        "token_type": "Bearer",
+                        "scope": "profile group resource",
+                    }
+                )
+            elif path == "/auth/resource/authorisation":
+                # The fixture token holds the privilege on the granted
+                # resource ids and nothing anywhere else; any other
+                # bearer token (e.g. a syntactically valid stranger) gets
+                # no roles, exactly as gn-auth treats an anonymous call.
+                requested = json.loads(body or b"{}").get("resource-ids") or []
+                own_token = self.headers.get("Authorization") == "Bearer " + token
+                granted = [i for i in requested if i in grant_ids] if own_token else []
+                self._json(
+                    {
+                        resource_id: {
+                            "roles": [
+                                {"privileges": [{"privilege_id": VIEW_RESOURCE_PRIVILEGE}]}
+                            ]
+                        }
+                        for resource_id in granted
+                    }
+                )
+            elif path == "/auth/data/authorisation":
+                self._json({"authorisation": []})
+            else:
+                self._json({"error": "not found"}, 404)
+
+        def log_message(self, fmt, *args):
+            pass
+
+    return Handler
+
+
+def main():
+    parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
+    parser.add_argument(
+        "--port",
+        type=int,
+        default=8081,
+        help="port to listen on (default 8081, genenetwork4's default AUTH_SERVER_URL)",
+    )
+    parser.add_argument(
+        "--issuer",
+        default=None,
+        help="iss claim to mint (default http://127.0.0.1:<port>/, matching "
+        "genenetwork4's default AUTH_SERVER_URL -- it must match that string "
+        "exactly, trailing slash included)",
+    )
+    parser.add_argument(
+        "--subject",
+        default="gn4-test-stub",
+        help="sub claim to mint (default gn4-test-stub)",
+    )
+    parser.add_argument(
+        "--scope",
+        default="profile group resource",
+        help="scope claim to mint",
+    )
+    parser.add_argument(
+        "--grant-resource-id",
+        action="append",
+        default=[],
+        metavar="ID",
+        help="resource id the fixture token holds view-resource on (repeatable); "
+        "set GN4_TEST_RESOURCE_ID to the same value. Without it every "
+        "authorisation answer is 'no privileges'.",
+    )
+    parser.add_argument(
+        "--token-file",
+        default=None,
+        help="write the minted token here as well as printing it",
+    )
+    args = parser.parse_args()
+
+    issuer = args.issuer or f"http://127.0.0.1:{args.port}/"
+    jwks_document = open(JWKS_FILE).read()
+    kid = json.loads(jwks_document)["jwks"][0]["kid"]
+    token = sign(issuer, kid, args.subject, args.scope)
+
+    if args.token_file:
+        with open(args.token_file, "w") as handle:
+            handle.write(token + "\n")
+    print(token, flush=True)
+
+    server = ThreadingHTTPServer(
+        ("127.0.0.1", args.port),
+        handler_for(token, set(args.grant_resource_id), jwks_document),
+    )
+    print(
+        f"gn-auth stub on http://127.0.0.1:{args.port}/ (iss {issuer}"
+        + (f", granting {', '.join(args.grant_resource_id)}" if args.grant_resource_id else "")
+        + ")",
+        file=__import__("sys").stderr,
+        flush=True,
+    )
+    server.serve_forever()
+
+
+if __name__ == "__main__":
+    main()