| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | | use super::{identity, ui, Ctx}; |
| 7 | | use anyhow::{bail, Context, Result}; |
| 8 | | use serde_json::Value as Json; |
| 9 | | use spectra_core::daemon::WireEvent; |
| 10 | | use spectra_core::identity::Identity; |
| 11 | | use spectra_core::keys::KeyPair; |
| 12 | | use spectra_core::principals::{identity_payload, signed_event}; |
| 13 | | use spectra_mcp::client::{host_port, Client, Endpoint}; |
| 14 | | use spectra_mcp::tls::Pinned; |
| 15 | | use std::path::{Path, PathBuf}; |
| 16 | |
|
| 17 | |
|
| 18 | | #[derive(Clone, Debug)] |
| 19 | | pub struct ClientCfg { |
| 20 | | pub server: String, |
| 21 | | pub instance: String, |
| 22 | | pub tls_spki: [u8; 32], |
| 23 | | } |
| 24 | |
|
| 25 | | impl ClientCfg { |
| 26 | 20× | pub fn endpoint(&self) -> Result<Endpoint> { |
| 27 | 20× | Endpoint::tls(&self.server, self.tls_spki) |
| 28 | 20× | } |
| 29 | | } |
| 30 | |
|
| 31 | |
|
| 32 | 197× | pub fn path() -> PathBuf { |
| 33 | 197× | if let Some(p) = std::env::var_os("SPECTRA_SERVER").filter(|p| !p.is_empty()) { |
| 34 | 0× | return PathBuf::from(p); |
| 35 | 197× | } |
| 36 | 197× | config_dir().join("client.toml") |
| 37 | 197× | } |
| 38 | |
|
| 39 | |
|
| 40 | 197× | fn config_dir() -> PathBuf { |
| 41 | 197× | spectra_core::keys::default_home() |
| 42 | 197× | } |
| 43 | |
|
| 44 | |
|
| 45 | 191× | pub fn load() -> Result<Option<ClientCfg>> { |
| 46 | 191× | let p = path(); |
| 47 | 191× | let text = match std::fs::read_to_string(&p) { |
| 48 | 19× | Ok(t) => t, |
| 49 | 172× | Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), |
| 50 | 0× | Err(e) => return Err(e).with_context(|| format!("reading {}", p.display())), |
| 51 | | }; |
| 52 | 19× | let t: toml::Table = text.parse().with_context(|| format!("reading {}", p.display()))?; |
| 53 | 57× | let get = |k: &str| t.get(k).and_then(|v| v.as_str()).map(String::from).with_context(|| format!("{} has no {k}", p.display())); |
| 54 | 19× | let spki = hex::decode(get("tls_spki")?).ok().and_then(|b| b.try_into().ok()).with_context(|| format!("{}: tls_spki", p.display()))?; |
| 55 | 19× | Ok(Some(ClientCfg { server: get("server")?, instance: get("instance")?, tls_spki: spki })) |
| 56 | 191× | } |
| 57 | |
|
| 58 | 5× | fn save(cfg: &ClientCfg) -> Result<PathBuf> { |
| 59 | 5× | let p = path(); |
| 60 | 5× | if let Some(dir) = p.parent() { |
| 61 | 5× | std::fs::create_dir_all(dir)?; |
| 62 | 0× | } |
| 63 | 5× | std::fs::write( |
| 64 | 5× | &p, |
| 65 | 5× | format!( |
| 66 | | "# Spectra on another machine (spec §8.6), written by `spectra connect`.\n\nserver = \"{}\"\ninstance = \"{}\" # the instance key, delegated by the owner\ntls_spki = \"{}\" # the certificate the instance key vouches for\n", |
| 67 | | cfg.server, |
| 68 | | cfg.instance, |
| 69 | 5× | hex::encode(cfg.tls_spki) |
| 70 | | ), |
| 71 | 0× | )?; |
| 72 | 5× | Ok(p) |
| 73 | 5× | } |
| 74 | |
|
| 75 | |
|
| 76 | 9× | fn own(home: &Path, key: &str) -> Option<KeyPair> { |
| 77 | 9× | let keys = home.join("keys"); |
| 78 | 9× | KeyPair::load(&keys.join(format!("{key}.secret")), &keys.join(format!("{key}.identity"))).ok() |
| 79 | 9× | } |
| 80 | |
|
| 81 | 8× | pub async fn connect(_ctx: &Ctx, url: &str, owner_home: Option<PathBuf>, expect: Option<String>, yes: bool) -> Result<i32> { |
| 82 | 8× | let server = format!("https://{}", host_port(url)?); |
| 83 | 8× | let home = owner_home.unwrap_or_else(spectra_core::keys::default_home); |
| 84 | 8× | let mut owner = KeyPair::load_home(&home) |
| 85 | 8× | .with_context(|| format!("the owner connects with their own identity: none usable in {} (or --owner-home)", home.display()))?; |
| 86 | |
|
| 87 | | |
| 88 | 8× | let probe = Pinned::new(None); |
| 89 | 8× | let anon = Client::at(Endpoint::Tls { addr: host_port(url)?, config: probe.client_config()? }, None); |
| 90 | 8× | let r = anon.send("GET", "/api/v1/instance", &[], vec![]).await.with_context(|| format!("reaching {server}"))?; |
| 91 | 8× | if r.status != 200 { |
| 92 | 0× | bail!("{server} answered {} to /api/v1/instance: is it a Spectra?", r.status); |
| 93 | 8× | } |
| 94 | 8× | let answer = r.json()?; |
| 95 | 8× | let (_, seen) = probe.seen().context("the server showed no certificate")?; |
| 96 | 8× | let checked = spectra_mcp::instance::check(&answer, &seen)?; |
| 97 | 8× | let instance_key = checked.instance.hash(); |
| 98 | |
|
| 99 | | |
| 100 | 8× | let recorded = checked.owner.clone(); |
| 101 | 8× | let named = answer.get("named_owner").and_then(Json::as_str).map(String::from); |
| 102 | 8× | match (&recorded, &named) { |
| 103 | 2× | (Some(k), _) if *k != owner.key.to_hex() && own(&home, k).is_none() => { |
| 104 | 0× | bail!("{server} belongs to someone else (owner {})", &k[..12.min(k.len())]); |
| 105 | | } |
| 106 | 6× | (None, Some(k)) if own(&home, k).is_none() => { |
| 107 | 1× | bail!( |
| 108 | | "{server} was set up for {}, which is not an identity in {} (spectra init --owner takes `spectra identity show`'s key)", |
| 109 | 1× | &k[..12.min(k.len())], |
| 110 | 1× | home.display() |
| 111 | | ); |
| 112 | | } |
| 113 | 0× | (None, None) => bail!("{server} has no owner and names none"), |
| 114 | 7× | _ => {} |
| 115 | | } |
| 116 | |
|
| 117 | | |
| 118 | 7× | if !owner.identity.delegates_to(&instance_key, spectra_core::ids::now_ms()) { |
| 119 | 5× | println!("{}", ui::kv("server", ui::path(&server))); |
| 120 | 5× | println!("{}", ui::kv("instance", format!("{}{}{}", checked.name, ui::sep(), ui::hash(instance_key.to_hex())))); |
| 121 | 5× | println!(" {} {}", ui::note(), ui::dim("`spectra init` printed this instance's key on the server: check it is the same")); |
| 122 | 2× | match &expect { |
| 123 | 2× | Some(prefix) if instance_key.to_hex().starts_with(&prefix.trim().to_ascii_lowercase()) => {} |
| 124 | 1× | Some(prefix) => bail!("the server's instance key does not start with {prefix}: not trusted"), |
| 125 | 3× | None => super::agent::confirm(&format!("Trust {} as your Spectra", checked.name), yes)?, |
| 126 | | } |
| 127 | 3× | owner = spectra_core::keys::delegate(&home, instance_key, None)?; |
| 128 | 2× | } |
| 129 | |
|
| 130 | 5× | let cfg = ClientCfg { server: server.clone(), instance: instance_key.to_hex(), tls_spki: seen }; |
| 131 | 5× | let saved = save(&cfg)?; |
| 132 | 5× | let pinned = cfg.endpoint()?; |
| 133 | |
|
| 134 | | |
| 135 | 5× | let mut claimed = false; |
| 136 | 5× | match (&recorded, &named) { |
| 137 | 3× | (None, Some(k)) => { |
| 138 | 3× | let named_pair = own(&home, k).expect("checked above"); |
| 139 | 3× | let mut events = |
| 140 | 3× | vec![WireEvent::from_new(&signed_event(&named_pair, "human", "identity.owner", identity_payload(&named_pair.identity)))]; |
| 141 | 3× | if let Some(e) = identity::rotation(&named_pair.identity, &home)? { |
| 142 | 3× | events.push(WireEvent::from_new(&e)); |
| 143 | 3× | } |
| 144 | 3× | let r = Client::at(pinned.clone(), None).send("POST", "/api/v1/claim", &[], serde_json::to_vec(&events)?).await?; |
| 145 | 3× | if r.status != 200 { |
| 146 | 0× | bail!("{server} refused the claim: {}", problem(&r.body)); |
| 147 | 3× | } |
| 148 | 3× | claimed = true; |
| 149 | | } |
| 150 | 2× | (Some(k), _) if *k != owner.key.to_hex() => { |
| 151 | 0× | let rec_pair = own(&home, k).expect("checked above"); |
| 152 | 0× | let rec_id: Identity = rec_pair.identity.clone(); |
| 153 | 0× | if let Some(e) = identity::rotation(&rec_id, &home)? { |
| 154 | 0× | let body = serde_json::to_vec(&vec![WireEvent::from_new(&e)])?; |
| 155 | 0× | let r = Client::at(pinned.clone(), Some(rec_pair)).send("POST", "/api/v1/ledger/events", &[], body).await?; |
| 156 | 0× | if r.status != 200 { |
| 157 | 0× | bail!("{server} refused the owner's current identity: {}", problem(&r.body)); |
| 158 | 0× | } |
| 159 | 0× | } |
| 160 | | } |
| 161 | 2× | _ => {} |
| 162 | | } |
| 163 | |
|
| 164 | 5× | println!("{} {} {}", ui::done(if claimed { "Claimed" } else { "Connected" }), ui::subject(&checked.name), ui::path(&server)); |
| 165 | 5× | println!("{}", ui::kv("owner", format!("{}{}{}", ui::author(&owner.identity.name), ui::sep(), ui::hash(owner.key.short(12))))); |
| 166 | 5× | println!("{}", ui::kv("instance", format!("{}{}", ui::hash(instance_key.short(12)), ui::dim(" (delegated by you)")))); |
| 167 | 5× | println!( |
| 168 | | "{}", |
| 169 | 5× | ui::kv("certificate", format!("{}{}", ui::hash(hex::encode(&seen[..8])), ui::dim(" (pinned; vouched for by the instance key)"))) |
| 170 | | ); |
| 171 | 5× | println!("{}", ui::kv("config", ui::path(saved.display()))); |
| 172 | 5× | Ok(0) |
| 173 | 8× | } |
| 174 | |
|
| 175 | 0× | fn problem(body: &[u8]) -> String { |
| 176 | 0× | serde_json::from_slice::<Json>(body) |
| 177 | 0× | .ok() |
| 178 | 0× | .and_then(|v| v.get("detail").or_else(|| v.get("title")).and_then(Json::as_str).map(String::from)) |
| 179 | 0× | .unwrap_or_else(|| String::from_utf8_lossy(body).into_owned()) |
| 180 | 0× | } |