Skip to main content

hyprstream_rpc/auth/
key_subject_resolver.rs

1//! Global signer-key → authorization-subject resolver (#446).
2//!
3//! # Why this exists
4//!
5//! The shared dispatch core ([`crate::service::dispatch::process_request`])
6//! verifies every socket-served request — including same-uid local IPC over
7//! Unix domain sockets — under [`crate::envelope::EnvelopeVerification::AnySigner`]
8//! (`iroh_rpc.rs::run_bridge_dispatch_loop` hardcodes `AnySigner`, used by both
9//! the iroh network plane and `uds_server`). `AnySigner` cryptographically
10//! verifies the COSE composite against the envelope's `cnf` (the signer's
11//! Ed25519 pubkey) but produces `key_derived_subject = anonymous` — so a
12//! signed IPC request from one service to another loses its identity and is
13//! denied any policy-gated write (e.g. `discovery:Announce`).
14//!
15//! The authoritative key→subject binding lives in the trust store
16//! (`hyprstream-service::TrustStore`, populated fail-closed under #441), but
17//! that crate sits *above* `hyprstream-rpc` in the dependency graph and cannot
18//! be referenced from the dispatch core. This module is the inversion-of-control
19//! seam: `hyprstream-service` installs a [`KeySubjectResolver`] at startup, and
20//! the default [`crate::service::RequestService::resolve_key_subject`] consults
21//! it. That makes *every* service (DiscoveryService included — it lives in
22//! `hyprstream-discovery`, which also cannot see the trust store) resolve a
23//! verified signer key to its authoritative `service:<name>` subject without
24//! each service crate having to depend on the trust store.
25//!
26//! # Fail-closed invariant
27//!
28//! Resolution NEVER fabricates an identity: an unregistered signer key resolves
29//! to `None` → `anonymous`. A genuinely anonymous caller (no registered key)
30//! therefore stays denied for policy-gated writes. This only honors the
31//! cryptographically-verified, *registered* identity of a legitimate signer —
32//! it never loosens a grant to `anonymous`.
33
34use std::sync::Arc;
35
36use parking_lot::RwLock;
37
38use crate::envelope::Subject;
39
40/// Resolves a verified Ed25519 signer pubkey to its authorization subject.
41///
42/// Implemented by the trust store layer (`hyprstream-service`) and installed
43/// via [`set_global`]. The input is the envelope `cnf` — the signer's Ed25519
44/// public key, already cryptographically verified by the COSE signature check
45/// before resolution is attempted.
46pub trait KeySubjectResolver: Send + Sync {
47    /// Resolve a verified signer pubkey to its authoritative subject.
48    ///
49    /// Returns `Some(subject)` only for a *registered* key (e.g.
50    /// `service:discovery`, or a user's bare `sub`). Returns `None` for an
51    /// unregistered/unknown key — the caller then stays `anonymous`
52    /// (fail-closed). Implementations MUST NOT derive a fallback identity.
53    fn resolve_subject(&self, signer_pubkey: &[u8; 32]) -> Option<Subject>;
54}
55
56static GLOBAL_KEY_SUBJECT_RESOLVER: RwLock<Option<Arc<dyn KeySubjectResolver>>> = RwLock::new(None);
57
58/// Install the global key→subject resolver.
59///
60/// Called once during startup by the trust-store layer. Idempotent —
61/// re-installing replaces the previous resolver.
62pub fn set_global(resolver: Arc<dyn KeySubjectResolver>) {
63    *GLOBAL_KEY_SUBJECT_RESOLVER.write() = Some(resolver);
64}
65
66/// Resolve a signer key to a subject via the installed global resolver.
67///
68/// Returns `None` if no resolver is installed (e.g. WASM client, tests that
69/// don't bootstrap the trust store) or the key is unregistered — both yield
70/// `anonymous`, preserving the fail-closed default.
71pub fn resolve_subject(signer_pubkey: &[u8; 32]) -> Option<Subject> {
72    GLOBAL_KEY_SUBJECT_RESOLVER
73        .read()
74        .as_ref()
75        .and_then(|r| r.resolve_subject(signer_pubkey))
76}