| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | | use crate::authn::{Authenticator, Caller, Refusal}; |
| 7 | | use crate::event::{Auth, Event, Method, NewEvent}; |
| 8 | | use crate::keys::KeyPair; |
| 9 | | use crate::ledger::{KeyResolver, Ledger}; |
| 10 | | use crate::presence::Presence; |
| 11 | | use crate::principals::{Principal, Principals}; |
| 12 | | use crate::proof::Proof; |
| 13 | | use anyhow::{bail, Context, Result}; |
| 14 | | use serde::{Deserialize, Serialize}; |
| 15 | | use std::path::{Path, PathBuf}; |
| 16 | | use std::sync::Arc; |
| 17 | | use tokio::sync::{Mutex, RwLock}; |
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | | pub const OWNER_KINDS: &[&str] = &["identity.rotated", "agent.added", "agent.disabled", "project.added"]; |
| 22 | |
|
| 23 | | pub struct Daemon { |
| 24 | | pub data: PathBuf, |
| 25 | | pub ledger: Ledger, |
| 26 | | pub principals: RwLock<Principals>, |
| 27 | | pub authn: Authenticator, |
| 28 | | pub presence: Mutex<Presence>, |
| 29 | | |
| 30 | | pub idle_waits: Mutex<std::collections::HashMap<String, (u32, u64)>>, |
| 31 | | } |
| 32 | |
|
| 33 | | impl Daemon { |
| 34 | | |
| 35 | 10× | pub async fn start(data: &Path) -> Result<Arc<Daemon>> { |
| 36 | 10× | let db = data.join("spectra.db"); |
| 37 | 10× | if !db.exists() { |
| 38 | 0× | bail!("no Spectra in {} (run `spectra init`)", data.display()); |
| 39 | 10× | } |
| 40 | 10× | let instance = KeyPair::instance(data, "spectra")?; |
| 41 | 10× | let ledger = Ledger::open(&db, Some(instance)).await?; |
| 42 | 10× | let principals = Principals::load(&ledger).await?; |
| 43 | 10× | let d = Arc::new(Daemon { |
| 44 | 10× | data: data.to_path_buf(), |
| 45 | 10× | ledger, |
| 46 | 10× | principals: RwLock::new(principals), |
| 47 | 10× | authn: Authenticator::default(), |
| 48 | 10× | presence: Mutex::new(Presence::default()), |
| 49 | 10× | idle_waits: Mutex::new(Default::default()), |
| 50 | 10× | }); |
| 51 | 10× | d.clone().watch_principals(); |
| 52 | 10× | d.clone().tick_presence(); |
| 53 | 10× | Ok(d) |
| 54 | 10× | } |
| 55 | |
|
| 56 | | |
| 57 | 10× | fn watch_principals(self: Arc<Self>) { |
| 58 | 10× | let mut rx = self.ledger.subscribe(); |
| 59 | 10× | tokio::spawn(async move { |
| 60 | | loop { |
| 61 | 81× | match rx.recv().await { |
| 62 | 14× | Ok(e) |
| 63 | 73× | if e.kind.starts_with("identity.") |
| 64 | 72× | || e.kind.starts_with("agent.") && e.kind != "agent.presence" && e.kind != "agent.status" => |
| 65 | | { |
| 66 | 14× | if let Ok(p) = Principals::load(&self.ledger).await { |
| 67 | 12× | *self.principals.write().await = p; |
| 68 | 0× | } |
| 69 | | } |
| 70 | 59× | Ok(_) => {} |
| 71 | | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => { |
| 72 | 0× | if let Ok(p) = Principals::load(&self.ledger).await { |
| 73 | 0× | *self.principals.write().await = p; |
| 74 | 0× | } |
| 75 | | } |
| 76 | 0× | Err(_) => return, |
| 77 | | } |
| 78 | | } |
| 79 | 0× | }); |
| 80 | 10× | } |
| 81 | |
|
| 82 | | |
| 83 | | |
| 84 | 10× | fn tick_presence(self: Arc<Self>) { |
| 85 | 10× | tokio::spawn(async move { |
| 86 | 10× | let mut every = tokio::time::interval(std::time::Duration::from_secs(15)); |
| 87 | | loop { |
| 88 | 20× | every.tick().await; |
| 89 | 10× | let events = self.presence.lock().await.tick(crate::ids::now_ms()); |
| 90 | 10× | if !events.is_empty() { |
| 91 | 0× | let _ = self.ledger.append(events).await; |
| 92 | 10× | } |
| 93 | 10× | let _ = self.ledger.append(vec![]).await; |
| 94 | | } |
| 95 | | }); |
| 96 | 10× | } |
| 97 | |
|
| 98 | | |
| 99 | | |
| 100 | | |
| 101 | 74× | pub async fn authenticate(&self, proof: Option<&Proof>, method: &str, body: &[u8], peer: Option<Peer>) -> Result<Caller, Refusal> { |
| 102 | 74× | let now = crate::ids::now_ms(); |
| 103 | 74× | let r = { |
| 104 | 74× | let p = self.principals.read().await; |
| 105 | 74× | self.authn.authenticate(&p, proof, method, body, now, peer.map(|x| x.uid)) |
| 106 | | }; |
| 107 | 58× | match &r { |
| 108 | 1× | Err(Refusal::WrongUid { agent, bound, from }) => { |
| 109 | 1× | let e = NewEvent::system( |
| 110 | 1× | "guard.impersonation", |
| 111 | 1× | serde_json::json!({ "agent": agent, "bound_uid": bound, "from_uid": from, "from_pid": peer.map(|x| x.pid), "method": method }), |
| 112 | | ) |
| 113 | 1× | .subject(agent.clone()); |
| 114 | 1× | let _ = self.ledger.append_one(e).await; |
| 115 | 1× | tracing::warn!(agent, bound, from, "a key used from the wrong Unix user"); |
| 116 | | } |
| 117 | 1× | Ok(Caller { principal: Principal::Agent { name, .. }, learn_uid: Some(uid), .. }) => { |
| 118 | 1× | let e = NewEvent::system("agent.uid_bound", serde_json::json!({ "name": name, "uid": uid })).subject(name.clone()); |
| 119 | 1× | let _ = self.ledger.append_one(e).await; |
| 120 | | } |
| 121 | 72× | _ => {} |
| 122 | | } |
| 123 | 74× | r |
| 124 | 74× | } |
| 125 | |
|
| 126 | | |
| 127 | | |
| 128 | 40× | pub async fn touch(&self, who: &Principal, new_session: bool) -> Option<String> { |
| 129 | 40× | let Principal::Agent { name, .. } = who else { return None }; |
| 130 | 40× | let (id, started) = self.presence.lock().await.request(name, crate::ids::now_ms(), new_session); |
| 131 | 40× | if let Some(e) = started { |
| 132 | 11× | let _ = self.ledger.append_one(e).await; |
| 133 | 29× | } |
| 134 | 40× | Some(id) |
| 135 | 40× | } |
| 136 | |
|
| 137 | | |
| 138 | | |
| 139 | 2× | pub async fn accept_owner_events(&self, events: Vec<WireEvent>) -> Result<Vec<u64>> { |
| 140 | 2× | let mut principals = self.principals.read().await.clone(); |
| 141 | 2× | let projects: Vec<String> = events |
| 142 | 2× | .iter() |
| 143 | 4× | .filter(|e| e.kind == "project.added") |
| 144 | 2× | .filter_map(|e| e.payload.get("name").and_then(|n| n.as_str()).map(String::from)) |
| 145 | 2× | .collect(); |
| 146 | 2× | let mut out = Vec::new(); |
| 147 | 4× | for w in events { |
| 148 | 4× | let n = w.into_new_event()?; |
| 149 | 4× | if n.kind == "channel.created" { |
| 150 | | |
| 151 | 1× | let p = &n.payload; |
| 152 | 1× | let ok = p.get("kind").and_then(|k| k.as_str()) == Some("project") |
| 153 | 1× | && p.get("name").and_then(|x| x.as_str()).is_some_and(|x| projects.iter().any(|q| q == x)); |
| 154 | 1× | if !ok || n.auth.method != Method::System { |
| 155 | 0× | bail!("a channel is only made with its project"); |
| 156 | 1× | } |
| 157 | 1× | out.push(n); |
| 158 | 1× | continue; |
| 159 | 3× | } |
| 160 | 3× | if !OWNER_KINDS.contains(&n.kind.as_str()) { |
| 161 | 0× | bail!("{} cannot be handed to the daemon", n.kind); |
| 162 | 3× | } |
| 163 | 3× | if n.actor != "human" { |
| 164 | 0× | bail!("only the owner hands events to the daemon"); |
| 165 | 3× | } |
| 166 | | |
| 167 | | |
| 168 | 3× | let probe = probe_event(&n); |
| 169 | 3× | principals.check(&probe).map_err(|why| anyhow::anyhow!("refused {}: {why}", n.kind))?; |
| 170 | 3× | if n.kind != "identity.rotated" { |
| 171 | 2× | let key = oxy_core::hash::ObjectRef(n.auth.key.context("unsigned")?); |
| 172 | 2× | if !matches!(principals.principal(&key, crate::ids::now_ms()), Ok(Principal::Owner { .. })) { |
| 173 | 0× | bail!("refused {}: not signed by the owner", n.kind); |
| 174 | 2× | } |
| 175 | 1× | } |
| 176 | 3× | out.push(n); |
| 177 | | } |
| 178 | 2× | let appended = self.ledger.append(out).await?; |
| 179 | 2× | let _ = self.ledger.checkpoint().await; |
| 180 | | |
| 181 | 2× | *self.principals.write().await = Principals::load(&self.ledger).await?; |
| 182 | 4× | Ok(appended.iter().map(|e| e.seq).collect()) |
| 183 | 2× | } |
| 184 | |
|
| 185 | | |
| 186 | 10× | pub async fn shutdown(&self) { |
| 187 | 10× | let _ = self.ledger.checkpoint().await; |
| 188 | 10× | } |
| 189 | | } |
| 190 | |
|
| 191 | |
|
| 192 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 193 | | pub struct Peer { |
| 194 | | pub uid: u32, |
| 195 | | pub pid: Option<i32>, |
| 196 | | } |
| 197 | |
|
| 198 | |
|
| 199 | | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 200 | | pub struct WireEvent { |
| 201 | | pub kind: String, |
| 202 | | pub actor: String, |
| 203 | | pub subject: Option<String>, |
| 204 | | pub project: Option<String>, |
| 205 | | pub payload: serde_json::Value, |
| 206 | | |
| 207 | | pub key: Option<String>, |
| 208 | | pub proof: Option<String>, |
| 209 | | } |
| 210 | |
|
| 211 | | impl WireEvent { |
| 212 | 4× | pub fn from_new(n: &NewEvent) -> WireEvent { |
| 213 | 4× | WireEvent { |
| 214 | 4× | kind: n.kind.clone(), |
| 215 | 4× | actor: n.actor.clone(), |
| 216 | 4× | subject: n.subject.clone(), |
| 217 | 4× | project: n.project.clone(), |
| 218 | 4× | payload: n.payload.clone(), |
| 219 | 4× | key: n.auth.key.map(hex::encode), |
| 220 | 4× | proof: n.auth.proof.as_ref().map(hex::encode), |
| 221 | 4× | } |
| 222 | 4× | } |
| 223 | |
|
| 224 | 4× | fn into_new_event(self) -> Result<NewEvent> { |
| 225 | 4× | let auth = match (&self.key, &self.proof) { |
| 226 | 3× | (Some(k), Some(p)) => Auth { |
| 227 | 3× | key: Some(hex::decode(k).ok().and_then(|b| b.try_into().ok()).context("a key is 32 bytes of hex")?), |
| 228 | 3× | method: Method::Signature, |
| 229 | | assurance: 2, |
| 230 | 3× | proof: Some(hex::decode(p).context("a proof is hex")?), |
| 231 | | }, |
| 232 | 1× | _ => Auth::system(), |
| 233 | | }; |
| 234 | 4× | let mut n = NewEvent::system(&self.kind, self.payload).by(self.actor, auth); |
| 235 | 4× | n.subject = self.subject; |
| 236 | 4× | n.project = self.project; |
| 237 | 4× | Ok(n) |
| 238 | 4× | } |
| 239 | | } |
| 240 | |
|
| 241 | |
|
| 242 | 3× | fn probe_event(n: &NewEvent) -> Event { |
| 243 | 3× | let now = crate::ids::now_ms(); |
| 244 | 3× | Event { |
| 245 | 3× | seq: 0, |
| 246 | 3× | id: String::new(), |
| 247 | 3× | at_ms: now, |
| 248 | 3× | hlc: crate::hlc::Hlc { wall_ms: now, logical: 0 }, |
| 249 | 3× | prev: [0; 32], |
| 250 | 3× | hash: [0; 32], |
| 251 | 3× | auth: n.auth.clone(), |
| 252 | 3× | kind: n.kind.clone(), |
| 253 | 3× | actor: n.actor.clone(), |
| 254 | 3× | session: n.session.clone(), |
| 255 | 3× | project: n.project.clone(), |
| 256 | 3× | subject: n.subject.clone(), |
| 257 | 3× | payload: Some(crate::event::canonical_json(&n.payload)), |
| 258 | 3× | trace: n.trace.clone(), |
| 259 | 3× | } |
| 260 | 3× | } |