| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use super::{ui, Ctx}; |
| 6 | | use anyhow::{bail, Context, Result}; |
| 7 | | use oxy_core::identity::Identity; |
| 8 | | use spectra_core::keys::KeyPair; |
| 9 | | use spectra_core::ledger::Ledger; |
| 10 | | use spectra_core::principals::{identity_payload, signed_event, Principals}; |
| 11 | | use std::path::{Path, PathBuf}; |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | 15× | pub fn owner_home(ctx: &Ctx, flag: Option<PathBuf>) -> PathBuf { |
| 16 | 15× | flag.or_else(|| ctx.config_owner_home()).unwrap_or_else(oxy_core::repo::default_home) |
| 17 | 15× | } |
| 18 | |
|
| 19 | 7× | pub async fn init(ctx: &Ctx, owner_home_flag: Option<PathBuf>, name: Option<String>) -> Result<i32> { |
| 20 | 7× | let data = &ctx.data; |
| 21 | 7× | let db = data.join("spectra.db"); |
| 22 | 7× | if db.exists() { |
| 23 | 1× | bail!("{} already holds a Spectra (spectra identity show)", data.display()); |
| 24 | 6× | } |
| 25 | 6× | let home = owner_home(ctx, owner_home_flag); |
| 26 | 6× | let owner = KeyPair::load_home(&home).with_context(|| { |
| 27 | 1× | format!("the owner signs with their own identity: none usable in {} (oxy identity create, or --owner-home)", home.display()) |
| 28 | 1× | })?; |
| 29 | 5× | std::fs::create_dir_all(data)?; |
| 30 | | #[cfg(unix)] |
| 31 | | { |
| 32 | | use std::os::unix::fs::PermissionsExt; |
| 33 | 5× | std::fs::set_permissions(data, std::fs::Permissions::from_mode(0o700))?; |
| 34 | | } |
| 35 | 5× | let name = name.unwrap_or_else(hostname); |
| 36 | 5× | let instance = KeyPair::instance(data, &name)?; |
| 37 | 5× | let ledger = Ledger::open(&db, Some(instance.clone())).await?; |
| 38 | 5× | ledger |
| 39 | 5× | .append(vec![ |
| 40 | 5× | signed_event(&owner, "human", "identity.owner", identity_payload(&owner.identity)), |
| 41 | 5× | signed_event(&instance, "system", "identity.instance", identity_payload(&instance.identity)), |
| 42 | 5× | ]) |
| 43 | 5× | .await?; |
| 44 | 5× | ledger.checkpoint().await?; |
| 45 | 5× | let home_s = home.display().to_string(); |
| 46 | 5× | std::fs::write( |
| 47 | 5× | data.join("spectra.toml"), |
| 48 | 5× | format!("# Spectra's configuration (spec §22.1).\n\n[owner]\nkey = \"{}\"\nhome = {}\n", owner.key.to_hex(), toml_str(&home_s)), |
| 49 | 0× | )?; |
| 50 | 5× | println!("{} {} {}", ui::done("Initialised"), ui::subject("Spectra"), ui::path(data.display())); |
| 51 | 5× | println!("{}", ui::kv("owner", format!("{}{}{}", ui::author(&owner.identity.name), ui::sep(), ui::hash(owner.key.short(12))))); |
| 52 | 5× | println!("{}", ui::kv("instance", format!("{}{}{}", name, ui::sep(), ui::hash(instance.key.short(12))))); |
| 53 | 5× | println!("{}", ui::kv("ledger", format!("{}{}", ui::path(db.display()), ui::dim(" (verified with spectra admin verify)")))); |
| 54 | 5× | println!(); |
| 55 | 5× | println!(" {} {}", ui::note(), ui::dim("so that checkpoints are verifiably from your machine, delegate to the instance key:")); |
| 56 | 5× | println!( |
| 57 | | " {}", |
| 58 | 5× | ui::id(format!("oxy identity delegate add {} --scope changes", data.join("keys").join("instance.identity").display())) |
| 59 | | ); |
| 60 | 5× | println!( |
| 61 | | " {} {} {}", |
| 62 | 5× | ui::note(), |
| 63 | 5× | ui::dim("then add agents with"), |
| 64 | 5× | ui::id("spectra agent add NAME --kind KIND --project PROJECT --home DIR") |
| 65 | | ); |
| 66 | 5× | Ok(0) |
| 67 | 7× | } |
| 68 | |
|
| 69 | 0× | pub async fn show(ctx: &Ctx, json: bool) -> Result<i32> { |
| 70 | 0× | let ledger = Ledger::open_read_only(&ctx.db()?).await?; |
| 71 | 0× | let p = Principals::load(&ledger).await?; |
| 72 | 0× | let owner = p.owner().context("no owner is recorded")?; |
| 73 | 0× | let instance = p.instance(); |
| 74 | 0× | let delegated = |
| 75 | 0× | instance.is_some_and(|i| owner.delegates_to(&i.hash(), spectra_core::principals::DELEGATION_SCOPE, spectra_core::ids::now_ms())); |
| 76 | 0× | if json { |
| 77 | 0× | println!( |
| 78 | | "{}", |
| 79 | 0× | serde_json::json!({ |
| 80 | 0× | "owner": { "name": owner.name, "key": owner.hash().to_hex() }, |
| 81 | 0× | "instance": instance.map(|i| serde_json::json!({ "name": i.name, "key": i.hash().to_hex(), "delegated": delegated })), |
| 82 | | }) |
| 83 | | ); |
| 84 | 0× | return Ok(0); |
| 85 | 0× | } |
| 86 | 0× | println!("{}", ui::section("Owner")); |
| 87 | 0× | println!("{}", ui::kv("name", ui::author(format!("{} <{}>", owner.name, owner.email)))); |
| 88 | 0× | println!("{}", ui::kv("key", ui::hash(owner.hash().to_hex()))); |
| 89 | 0× | println!("{}", ui::kv("delegates", owner.delegates.len())); |
| 90 | 0× | if let Some(i) = instance { |
| 91 | 0× | println!("{}", ui::section("Instance")); |
| 92 | 0× | println!("{}", ui::kv("name", &i.name)); |
| 93 | 0× | println!("{}", ui::kv("key", ui::hash(i.hash().to_hex()))); |
| 94 | 0× | if delegated { |
| 95 | 0× | println!("{}", ui::kv("status", ui::ok("delegated by the owner: checkpoints are verifiably yours"))); |
| 96 | 0× | } else { |
| 97 | 0× | println!("{}", ui::kv("status", ui::warn("not delegated by the owner yet"))); |
| 98 | 0× | println!( |
| 99 | 0× | " {} {}", |
| 100 | 0× | ui::note(), |
| 101 | 0× | ui::id(format!("oxy identity delegate add {} --scope changes", ctx.data.join("keys").join("instance.identity").display())) |
| 102 | 0× | ); |
| 103 | 0× | } |
| 104 | 0× | } |
| 105 | 0× | Ok(0) |
| 106 | 0× | } |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | 0× | pub async fn refresh(ctx: &Ctx, owner_home_flag: Option<PathBuf>) -> Result<i32> { |
| 111 | 0× | let ledger = ctx.writer().await?; |
| 112 | 0× | let outcome = refresh_owner(&ledger, &owner_home(ctx, owner_home_flag)).await?; |
| 113 | 0× | ledger.checkpoint().await?; |
| 114 | 0× | match outcome { |
| 115 | 0× | Some(id) => println!( |
| 116 | | "{} {}{}{}", |
| 117 | 0× | ui::done("Recorded"), |
| 118 | 0× | ui::subject("the owner's current identity"), |
| 119 | 0× | ui::sep(), |
| 120 | 0× | ui::hash(id.hash().short(12)) |
| 121 | | ), |
| 122 | 0× | None => println!("{} {}", ui::done("Current"), ui::subject("the owner's identity has not moved")), |
| 123 | | } |
| 124 | 0× | Ok(0) |
| 125 | 0× | } |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | 8× | pub async fn refresh_owner(ledger: &Ledger, home: &Path) -> Result<Option<Identity>> { |
| 130 | 8× | let p = Principals::load(ledger).await?; |
| 131 | 8× | let recorded = p.owner().context("no owner is recorded (spectra init)")?.clone(); |
| 132 | 8× | let pair = KeyPair::load_home(home)?; |
| 133 | 8× | if pair.key == recorded.hash() { |
| 134 | 7× | return Ok(None); |
| 135 | 1× | } |
| 136 | 1× | let mut chain = Vec::new(); |
| 137 | 1× | let mut at = pair.identity.clone(); |
| 138 | 1× | while let Some(prev) = at.previous { |
| 139 | 1× | if prev == recorded.hash() { |
| 140 | 1× | break; |
| 141 | 0× | } |
| 142 | 0× | let file = home.join("keys").join(format!("{}.identity", prev.to_hex())); |
| 143 | 0× | at = Identity::decode( |
| 144 | 0× | &std::fs::read(&file).with_context(|| format!("{} is not a successor of the recorded owner", pair.identity.name))?, |
| 145 | | ) |
| 146 | 0× | .map_err(|e| anyhow::anyhow!("{}: {e}", file.display()))?; |
| 147 | 0× | chain.push(hex::encode(at.encode())); |
| 148 | 0× | if chain.len() > 1000 { |
| 149 | 0× | bail!("the owner's identity chain is too long to follow"); |
| 150 | 0× | } |
| 151 | | } |
| 152 | 1× | if at.previous != Some(recorded.hash()) && pair.identity.previous != Some(recorded.hash()) { |
| 153 | 0× | bail!("{} in {} is not a successor of the recorded owner {}", pair.identity.name, home.display(), recorded.hash().short(12)); |
| 154 | 1× | } |
| 155 | 1× | let mut payload = identity_payload(&pair.identity); |
| 156 | 1× | payload["chain"] = chain.into(); |
| 157 | 1× | ledger.append_one(signed_event(&pair, "human", "identity.rotated", payload)).await?; |
| 158 | 1× | Ok(Some(pair.identity)) |
| 159 | 8× | } |
| 160 | |
|
| 161 | 4× | fn hostname() -> String { |
| 162 | 4× | std::fs::read_to_string("/etc/hostname") |
| 163 | 4× | .ok() |
| 164 | 4× | .map(|h| h.trim().to_string()) |
| 165 | 4× | .filter(|h| !h.is_empty()) |
| 166 | 4× | .unwrap_or_else(|| "spectra".into()) |
| 167 | 4× | } |
| 168 | |
|
| 169 | 5× | fn toml_str(s: &str) -> String { |
| 170 | 5× | serde_json::to_string(s).unwrap_or_default() |
| 171 | 5× | } |