| 1 | |
|
| 2 | |
|
| 3 | | use super::format::{short_id, signature_line, when}; |
| 4 | | use super::json; |
| 5 | | use super::open; |
| 6 | | use super::ui; |
| 7 | | use anyhow::Result; |
| 8 | | use oxy_core::hash::ChangeHash; |
| 9 | | use oxy_core::hash::KeyId; |
| 10 | | use oxy_core::identity::{Delegate, Identity, Scope}; |
| 11 | | use oxy_core::objects::Hlc; |
| 12 | | use oxy_core::protocol::SignedPolicy; |
| 13 | | use oxy_core::repo::{self, IdentityScope, PolicyUpdate, Repo, Rotation, SignatureStatus}; |
| 14 | | use std::path::{Path, PathBuf}; |
| 15 | |
|
| 16 | | const DAY_MS: u64 = 24 * 60 * 60 * 1000; |
| 17 | |
|
| 18 | 0× | fn expiry_from_days(days: u64) -> Option<u64> { |
| 19 | 0× | (days > 0).then(|| Hlc::now().wall_ms + days * DAY_MS) |
| 20 | 0× | } |
| 21 | |
|
| 22 | |
|
| 23 | 0× | fn expiry_from_duration(s: &str) -> Result<u64> { |
| 24 | 0× | let s = s.trim(); |
| 25 | 0× | let (n, unit) = match s.char_indices().find(|(_, c)| !c.is_ascii_digit()) { |
| 26 | 0× | Some((i, _)) => (&s[..i], &s[i..]), |
| 27 | 0× | None => (s, "d"), |
| 28 | | }; |
| 29 | 0× | let n: u64 = n.parse().map_err(|_| { |
| 30 | 0× | anyhow::anyhow!("--expires takes 30m, 8h, 7d or a number of days, not {s:?}") |
| 31 | 0× | })?; |
| 32 | 0× | let ms = match unit { |
| 33 | 0× | "m" => 60_000, |
| 34 | 0× | "h" => 3_600_000, |
| 35 | 0× | "d" => DAY_MS, |
| 36 | 0× | _ => anyhow::bail!("--expires takes 30m, 8h, 7d or a number of days, not {s:?}"), |
| 37 | | }; |
| 38 | 0× | if n == 0 { |
| 39 | 0× | anyhow::bail!("--expires needs a length of time; leave it out for no expiry"); |
| 40 | 0× | } |
| 41 | 0× | Ok(Hlc::now().wall_ms + n * ms) |
| 42 | 0× | } |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 1× | fn print_delegates(repo: &Repo, delegates: &[Delegate]) { |
| 47 | 1× | if delegates.is_empty() { |
| 48 | 1× | return; |
| 49 | 0× | } |
| 50 | 0× | println!("{}", ui::kv("delegates", delegates.len().to_string())); |
| 51 | 0× | let now = Hlc::now().wall_ms; |
| 52 | 0× | for d in delegates { |
| 53 | 0× | let who = repo |
| 54 | 0× | .identity(&d.key) |
| 55 | 0× | .map(|i| format!("{} <{}>", i.name, i.email)) |
| 56 | 0× | .unwrap_or_else(|_| "(unknown identity)".into()); |
| 57 | 0× | let until = match d.expires { |
| 58 | 0× | Some(e) if now > e => ui::err(format!("expired {}", ui::ago(e))), |
| 59 | 0× | Some(e) => ui::dim(format!("until {}", when(e))), |
| 60 | 0× | None => String::new(), |
| 61 | | }; |
| 62 | 0× | println!( |
| 63 | | " {} {} {} {}", |
| 64 | 0× | ui::author(who), |
| 65 | 0× | ui::hash(d.key.short(12)), |
| 66 | 0× | ui::dim(d.scope.to_string()), |
| 67 | | until |
| 68 | | ); |
| 69 | | } |
| 70 | 1× | } |
| 71 | |
|
| 72 | |
|
| 73 | 0× | fn resolve_key(repo: &Repo, prefix: &str) -> Result<KeyId> { |
| 74 | 0× | if let Some(k) = KeyId::from_hex(prefix) { |
| 75 | 0× | return Ok(k); |
| 76 | 0× | } |
| 77 | 0× | let mut candidates: Vec<KeyId> = Vec::new(); |
| 78 | 0× | if let Some((_, sp)) = repo.current_policy()? { |
| 79 | 0× | candidates.extend(sp.policy.identities.iter().map(|i| i.hash())); |
| 80 | 0× | candidates.extend(sp.policy.revoked.iter().copied()); |
| 81 | 0× | } |
| 82 | 0× | if let Some(own) = repo.config.author_key { |
| 83 | 0× | candidates.push(own); |
| 84 | 0× | if let Ok(id) = repo.identity(&own) { |
| 85 | 0× | candidates.extend(id.delegates.iter().map(|d| d.key)); |
| 86 | 0× | } |
| 87 | 0× | } |
| 88 | 0× | candidates.sort(); |
| 89 | 0× | candidates.dedup(); |
| 90 | 0× | let matches: Vec<_> = candidates |
| 91 | 0× | .into_iter() |
| 92 | 0× | .filter(|k| k.starts_with_hex(prefix)) |
| 93 | 0× | .collect(); |
| 94 | 0× | match matches.len() { |
| 95 | 0× | 1 => Ok(matches[0]), |
| 96 | 0× | 0 => anyhow::bail!("no known key starts with {prefix:?}"), |
| 97 | 0× | _ => anyhow::bail!("{prefix:?} is ambiguous"), |
| 98 | | } |
| 99 | 0× | } |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | 2× | fn report_update(u: &PolicyUpdate, out: Option<PathBuf>) -> Result<()> { |
| 104 | 2× | if u.applied { |
| 105 | 2× | println!( |
| 106 | | "{} policy serial {} {}", |
| 107 | 2× | ui::done("Applied"), |
| 108 | | u.policy.serial, |
| 109 | 2× | ui::dim("(push to publish it)") |
| 110 | | ); |
| 111 | 2× | return Ok(()); |
| 112 | 0× | } |
| 113 | 0× | let path = out.unwrap_or_else(|| PathBuf::from(format!("policy-{}.proposal", u.policy.serial))); |
| 114 | 0× | std::fs::write(&path, u.signed().encode())?; |
| 115 | 0× | println!( |
| 116 | | "{} policy serial {} needs {} of {} root signatures; has {}. Wrote {} for another root to run {}", |
| 117 | 0× | ui::caution(), |
| 118 | | u.policy.serial, |
| 119 | | u.policy.root_threshold, |
| 120 | 0× | u.policy.roots.len(), |
| 121 | 0× | u.sigs.len(), |
| 122 | 0× | ui::path(path.display()), |
| 123 | 0× | ui::dim(format!("oxy policy sign {}", path.display())) |
| 124 | | ); |
| 125 | 0× | Ok(()) |
| 126 | 2× | } |
| 127 | |
|
| 128 | 0× | fn report_rotation(repo: &Repo, r: &Rotation, verb: &str) -> Result<()> { |
| 129 | 0× | println!( |
| 130 | | "{} identity {} <{}>", |
| 131 | 0× | ui::done(verb), |
| 132 | 0× | ui::author(&r.identity.name), |
| 133 | | r.identity.email |
| 134 | | ); |
| 135 | 0× | println!("{}", ui::kv("old key", ui::dim(r.old.to_hex()))); |
| 136 | 0× | println!("{}", ui::kv("new key", r.new.to_hex())); |
| 137 | 0× | print_delegates(repo, &r.identity.delegates); |
| 138 | 0× | if let Some(e) = r.identity.expires { |
| 139 | 0× | println!("{}", ui::kv("expires", when(e))); |
| 140 | 0× | } |
| 141 | 0× | match &r.policy { |
| 142 | 0× | Some(u) => report_update(u, None)?, |
| 143 | 0× | None => println!( |
| 144 | | " {} {}", |
| 145 | 0× | ui::note(), |
| 146 | 0× | ui::dim("no policy names the old key; export the new identity for policy holders") |
| 147 | | ), |
| 148 | | } |
| 149 | 0× | Ok(()) |
| 150 | 0× | } |
| 151 | |
|
| 152 | 6× | pub fn create( |
| 153 | 6× | name: Option<String>, |
| 154 | 6× | email: Option<String>, |
| 155 | 6× | local: bool, |
| 156 | 6× | expires: Option<u64>, |
| 157 | 6× | ) -> Result<i32> { |
| 158 | | |
| 159 | | |
| 160 | 6× | let mut repo = match open() { |
| 161 | 4× | Ok(r) => r, |
| 162 | 1× | Err(e) if local => return Err(e), |
| 163 | | Err(_) => { |
| 164 | 1× | let (key, id) = repo::create_user_identity( |
| 165 | 1× | name.as_deref(), |
| 166 | 1× | email.as_deref(), |
| 167 | 1× | expires.and_then(expiry_from_days), |
| 168 | 0× | )?; |
| 169 | 1× | println!( |
| 170 | | "{} identity {} <{}>", |
| 171 | 1× | ui::done("Created"), |
| 172 | 1× | ui::author(&id.name), |
| 173 | | id.email |
| 174 | | ); |
| 175 | 1× | println!("{}", ui::kv("key", key)); |
| 176 | 1× | println!( |
| 177 | | "{}", |
| 178 | 1× | ui::kv("secret", ui::path(repo::default_keys_dir().display())) |
| 179 | | ); |
| 180 | 1× | println!( |
| 181 | | "{}", |
| 182 | 1× | ui::kv( |
| 183 | 1× | "scope", |
| 184 | 1× | format!( |
| 185 | | "default author in {}", |
| 186 | 1× | ui::path(repo::default_user_config().display()) |
| 187 | | ) |
| 188 | | ) |
| 189 | | ); |
| 190 | 1× | println!(" {} {}", ui::note(), ui::dim("changes in every repository carry this author; run oxy config --user core.sign change to sign each commit")); |
| 191 | 1× | return Ok(0); |
| 192 | | } |
| 193 | | }; |
| 194 | 4× | let name = name.unwrap_or_else(|| repo.config.author_name.clone()); |
| 195 | 4× | let email = email.unwrap_or_else(|| repo.config.author_email.clone()); |
| 196 | 4× | let scope = if local { |
| 197 | 0× | IdentityScope::Repo |
| 198 | | } else { |
| 199 | 4× | IdentityScope::User |
| 200 | | }; |
| 201 | 4× | let (key, _) = |
| 202 | 4× | repo.create_identity_with(&name, &email, scope, expires.and_then(expiry_from_days))?; |
| 203 | 4× | println!( |
| 204 | | "{} identity {} <{email}>", |
| 205 | 4× | ui::done("Created"), |
| 206 | 4× | ui::author(&name) |
| 207 | | ); |
| 208 | 4× | println!("{}", ui::kv("key", key)); |
| 209 | 4× | println!( |
| 210 | | "{}", |
| 211 | 4× | ui::kv("secret", ui::path(repo::default_keys_dir().display())) |
| 212 | | ); |
| 213 | 4× | match scope { |
| 214 | 4× | IdentityScope::User => println!( |
| 215 | | "{}", |
| 216 | 4× | ui::kv( |
| 217 | 4× | "scope", |
| 218 | 4× | format!( |
| 219 | | "default author in {}", |
| 220 | 4× | ui::path(repo.user_config_path().display()) |
| 221 | | ) |
| 222 | | ) |
| 223 | | ), |
| 224 | | IdentityScope::Repo => { |
| 225 | 0× | println!("{}", ui::kv("scope", "this repository's .oxy/config only")) |
| 226 | | } |
| 227 | | } |
| 228 | 4× | if !repo.config.sign_changes { |
| 229 | 4× | println!(" {} {}", ui::note(), ui::dim("changes will carry this author; run oxy config --user core.sign change to sign each commit")); |
| 230 | 4× | } |
| 231 | 4× | Ok(0) |
| 232 | 6× | } |
| 233 | |
|
| 234 | 2× | pub fn show() -> Result<i32> { |
| 235 | 2× | let repo = open()?; |
| 236 | 2× | let Some(key) = repo.config.author_key else { |
| 237 | 1× | println!("No identity configured; run `oxy identity create`."); |
| 238 | 1× | return Ok(0); |
| 239 | | }; |
| 240 | 1× | let id = repo.identity(&key)?; |
| 241 | 1× | println!( |
| 242 | | "{} {} <{}>", |
| 243 | 1× | ui::section("identity"), |
| 244 | 1× | ui::author(&id.name), |
| 245 | | id.email |
| 246 | | ); |
| 247 | 1× | println!("{}", ui::kv("key", key)); |
| 248 | 1× | println!( |
| 249 | | "{}", |
| 250 | 1× | ui::kv( |
| 251 | 1× | "created", |
| 252 | 1× | format!( |
| 253 | | "{} {}", |
| 254 | 1× | when(id.created), |
| 255 | 1× | ui::dim(format!("({})", ui::ago(id.created))) |
| 256 | | ) |
| 257 | | ) |
| 258 | | ); |
| 259 | 1× | if let Some(e) = id.expires { |
| 260 | 0× | let expired = id.is_expired_at(Hlc::now().wall_ms); |
| 261 | 0× | println!( |
| 262 | | "{}", |
| 263 | 0× | ui::kv( |
| 264 | 0× | "expires", |
| 265 | 0× | if expired { |
| 266 | 0× | ui::err(format!("{} EXPIRED", when(e))) |
| 267 | | } else { |
| 268 | 0× | when(e) |
| 269 | | } |
| 270 | | ) |
| 271 | | ); |
| 272 | 1× | } |
| 273 | 1× | if let Some(p) = id.previous { |
| 274 | 0× | println!("{}", ui::kv("previous", ui::dim(p.short(12)))); |
| 275 | 1× | } |
| 276 | | |
| 277 | | |
| 278 | 1× | if let Some(pinned) = repo |
| 279 | 1× | .config |
| 280 | 1× | .repo_layer() |
| 281 | 1× | .get("author.key") |
| 282 | 1× | .and_then(|k| oxy_core::hash::KeyId::from_hex(k)) |
| 283 | | { |
| 284 | 0× | let user_key = repo::read_config_file(&repo::default_user_config()) |
| 285 | 0× | .ok() |
| 286 | 0× | .and_then(|m| { |
| 287 | 0× | m.get("author.key") |
| 288 | 0× | .and_then(|k| oxy_core::hash::KeyId::from_hex(k)) |
| 289 | 0× | }); |
| 290 | 0× | if let Some(u) = user_key.filter(|u| *u != pinned) { |
| 291 | 0× | let related = |from: oxy_core::hash::KeyId, to: oxy_core::hash::KeyId| { |
| 292 | 0× | let mut cur = Some(from); |
| 293 | 0× | for _ in 0..64 { |
| 294 | 0× | match cur { |
| 295 | 0× | Some(k) if k == to => return true, |
| 296 | 0× | Some(k) => cur = repo.identity(&k).ok().and_then(|i| i.previous), |
| 297 | 0× | None => break, |
| 298 | | } |
| 299 | | } |
| 300 | 0× | false |
| 301 | 0× | }; |
| 302 | 0× | let how = if related(pinned, u) { |
| 303 | 0× | "an older key of this identity" |
| 304 | 0× | } else if related(u, pinned) { |
| 305 | 0× | "a newer key of this identity" |
| 306 | | } else { |
| 307 | 0× | "another key" |
| 308 | | }; |
| 309 | 0× | println!( |
| 310 | | " {} {}", |
| 311 | 0× | ui::caution(), |
| 312 | 0× | ui::dim(format!( |
| 313 | | "this repository pins author.key {}; your user config names {how}, {}. Other checkouts follow the user config: `oxy config --unset author.key` here, or `oxy config --user author.key {}`", |
| 314 | 0× | pinned.short(12), |
| 315 | 0× | u.short(12), |
| 316 | 0× | pinned.to_hex() |
| 317 | | )) |
| 318 | | ); |
| 319 | 0× | } |
| 320 | 1× | } |
| 321 | 1× | print_delegates(&repo, &id.delegates); |
| 322 | 1× | if repo.is_revoked(&key)? { |
| 323 | 0× | println!( |
| 324 | 0× | "{}", |
| 325 | 0× | ui::kv( |
| 326 | 0× | "status", |
| 327 | 0× | ui::err(format!("{} REVOKED", ui::glyph("✖", "x"))) |
| 328 | 0× | ) |
| 329 | 0× | ); |
| 330 | 1× | } |
| 331 | 1× | println!( |
| 332 | | "{}", |
| 333 | 1× | ui::kv( |
| 334 | 1× | "secret", |
| 335 | 1× | if repo.secret_key()?.is_some() { |
| 336 | 1× | ui::ok(format!("{} available", ui::glyph("✔", "*"))) |
| 337 | | } else { |
| 338 | 0× | ui::err(format!( |
| 339 | | "{} not found in the key directory", |
| 340 | 0× | ui::glyph("✖", "x") |
| 341 | | )) |
| 342 | | } |
| 343 | | ) |
| 344 | | ); |
| 345 | 1× | println!( |
| 346 | | "{}", |
| 347 | 1× | ui::kv( |
| 348 | 1× | "signing", |
| 349 | 1× | if repo.config.sign_changes { |
| 350 | 1× | "every change (core.sign = change)" |
| 351 | | } else { |
| 352 | 0× | "at push via seals (core.sign = seal)" |
| 353 | | } |
| 354 | | ) |
| 355 | | ); |
| 356 | 1× | Ok(0) |
| 357 | 2× | } |
| 358 | |
|
| 359 | 1× | pub fn verify(change: Option<&str>, revset: Option<&str>, as_json: bool) -> Result<i32> { |
| 360 | 1× | let repo = open()?; |
| 361 | 1× | let hashes: Vec<ChangeHash> = match (change, revset) { |
| 362 | 0× | (_, Some(r)) => { |
| 363 | 0× | let wc = repo.working_copy().ok().map(|(h, _)| h); |
| 364 | 0× | repo.revset(r)? |
| 365 | | .hashes |
| 366 | 0× | .into_iter() |
| 367 | 0× | .filter(|h| Some(*h) != wc) |
| 368 | 0× | .collect() |
| 369 | | } |
| 370 | 0× | (Some(c), None) => vec![repo.resolve_change(c)?], |
| 371 | 1× | (None, None) => repo |
| 372 | 1× | .log()? |
| 373 | 1× | .into_iter() |
| 374 | 3× | .filter(|e| !e.is_working_copy) |
| 375 | 1× | .map(|e| e.hash) |
| 376 | 1× | .collect(), |
| 377 | | }; |
| 378 | 1× | let mut bad = 0; |
| 379 | 1× | if as_json { |
| 380 | 1× | let mut items = Vec::new(); |
| 381 | 2× | for h in &hashes { |
| 382 | 2× | let Ok(c) = repo.store().get_change(h) else { |
| 383 | 0× | continue; |
| 384 | | }; |
| 385 | 2× | let (status, key, name, revoked, reason) = match repo.authenticate_change(h)? { |
| 386 | 0× | SignatureStatus::Unsigned => ("unsigned", None, None, false, None), |
| 387 | | SignatureStatus::Valid { |
| 388 | 2× | key, |
| 389 | 2× | name, |
| 390 | 2× | sealed, |
| 391 | 2× | revoked, |
| 392 | | .. |
| 393 | | } => ( |
| 394 | 2× | if sealed { "sealed" } else { "signed" }, |
| 395 | 2× | Some(key.to_hex()), |
| 396 | 2× | Some(name), |
| 397 | 2× | revoked, |
| 398 | 2× | None, |
| 399 | | ), |
| 400 | 0× | SignatureStatus::Invalid(why) => { |
| 401 | 0× | bad += 1; |
| 402 | 0× | ("invalid", None, None, false, Some(why)) |
| 403 | | } |
| 404 | | }; |
| 405 | 2× | items.push(json::obj([ |
| 406 | 2× | ("hash", json::s(&h.to_hex())), |
| 407 | 2× | ("change_id", json::s(&c.change_id.to_hex())), |
| 408 | 2× | ("status", json::s(status)), |
| 409 | 2× | ("key", json::opt(key.as_deref(), json::s)), |
| 410 | 2× | ("name", json::opt(name.as_deref(), json::s)), |
| 411 | 2× | ("revoked", json::b(revoked)), |
| 412 | 2× | ("reason", json::opt(reason.as_deref(), json::s)), |
| 413 | 2× | ])); |
| 414 | | } |
| 415 | 1× | println!("{}", json::arr(items)); |
| 416 | 1× | return Ok(if bad > 0 { 1 } else { 0 }); |
| 417 | 0× | } |
| 418 | 0× | for h in hashes { |
| 419 | 0× | let Ok(c) = repo.store().get_change(&h) else { |
| 420 | 0× | println!( |
| 421 | | "{} {}", |
| 422 | 0× | ui::hash(format!("#{}", h.short(12))), |
| 423 | 0× | ui::dim("not present (shallow)") |
| 424 | | ); |
| 425 | 0× | continue; |
| 426 | | }; |
| 427 | 0× | let line = signature_line(&repo, &h)?; |
| 428 | 0× | let styled = if line.starts_with("INVALID") { |
| 429 | 0× | bad += 1; |
| 430 | 0× | ui::err(format!("{} {line}", ui::glyph("✖", "x"))) |
| 431 | 0× | } else if line == "unsigned" { |
| 432 | 0× | ui::dim(format!("{} {line}", ui::glyph("·", "-"))) |
| 433 | | } else { |
| 434 | 0× | ui::ok(format!("{} {line}", ui::glyph("✔", "v"))) |
| 435 | | }; |
| 436 | 0× | println!( |
| 437 | | "{} {} {styled}", |
| 438 | 0× | ui::id(short_id(&c)), |
| 439 | 0× | ui::hash(format!("#{}", h.short(12))) |
| 440 | | ); |
| 441 | | } |
| 442 | 0× | Ok(if bad > 0 { 1 } else { 0 }) |
| 443 | 1× | } |
| 444 | |
|
| 445 | |
|
| 446 | 6× | fn secret_key(key: &str) -> bool { |
| 447 | 6× | let k = key.to_ascii_lowercase(); |
| 448 | 6× | k.ends_with(".token") || k.contains("secret") || k.contains("password") |
| 449 | 6× | } |
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | 2× | fn mask(value: &str) -> String { |
| 454 | 2× | let shown: String = value.chars().take(10).collect(); |
| 455 | 2× | let rest = value.chars().count().saturating_sub(10); |
| 456 | 2× | if rest == 0 { |
| 457 | 1× | "…".to_string() |
| 458 | | } else { |
| 459 | 1× | format!("{shown}…({rest} more)") |
| 460 | | } |
| 461 | 2× | } |
| 462 | |
|
| 463 | 3× | pub fn config(key: &str, value: &str, user: bool, unset: bool) -> Result<i32> { |
| 464 | | |
| 465 | | |
| 466 | | |
| 467 | | let read; |
| 468 | 3× | let value = if unset { |
| 469 | 0× | "" |
| 470 | 3× | } else if value == "-" { |
| 471 | 0× | let mut line = String::new(); |
| 472 | 0× | std::io::stdin().read_line(&mut line)?; |
| 473 | 0× | read = line.trim().to_string(); |
| 474 | 0× | read.as_str() |
| 475 | | } else { |
| 476 | 3× | value |
| 477 | | }; |
| 478 | 3× | let said = if unset { |
| 479 | 0× | format!("{} {key}", ui::done("Unset")) |
| 480 | 3× | } else if secret_key(key) { |
| 481 | 0× | format!("{} {key} = {}", ui::done("Set"), mask(value)) |
| 482 | | } else { |
| 483 | 3× | format!("{} {key} = {value}", ui::done("Set")) |
| 484 | | }; |
| 485 | 3× | if user { |
| 486 | 2× | let path = repo::default_user_config(); |
| 487 | 2× | repo::Config::set_user(&path, key, value)?; |
| 488 | 2× | println!("{said} {}", ui::dim(format!("(in {})", path.display()))); |
| 489 | | } else { |
| 490 | 1× | let mut repo = open()?; |
| 491 | 1× | repo.config.set(key, value)?; |
| 492 | 1× | repo.save_config()?; |
| 493 | 1× | println!("{said} {}", ui::dim("(in this repository)")); |
| 494 | | } |
| 495 | 3× | Ok(0) |
| 496 | 3× | } |
| 497 | |
|
| 498 | 1× | pub fn export(path: Option<PathBuf>, pem: bool) -> Result<i32> { |
| 499 | 1× | let repo = open()?; |
| 500 | | |
| 501 | | |
| 502 | 1× | let key = if pem { |
| 503 | 1× | repo.signing_key() |
| 504 | | } else { |
| 505 | 0× | repo.config.author_key |
| 506 | | }; |
| 507 | 1× | let Some(key) = key else { |
| 508 | 0× | anyhow::bail!("no identity configured; run `oxy identity create`"); |
| 509 | | }; |
| 510 | 1× | let id = repo.identity(&key)?; |
| 511 | 1× | let ext = if pem { "pem" } else { "identity" }; |
| 512 | 1× | let path = path.unwrap_or_else(|| PathBuf::from(format!("{}.{ext}", key.short(16)))); |
| 513 | 1× | if pem { |
| 514 | 1× | std::fs::write(&path, oxy_core::dsse::public_pem(&id))?; |
| 515 | | } else { |
| 516 | 0× | std::fs::write(&path, id.encode())?; |
| 517 | | } |
| 518 | 1× | println!( |
| 519 | | "{} {} {} <{}> ({}) to {}", |
| 520 | 1× | ui::done("Wrote"), |
| 521 | 1× | if pem { "the public key of" } else { "identity" }, |
| 522 | 1× | ui::author(&id.name), |
| 523 | | id.email, |
| 524 | 1× | key.short(12), |
| 525 | 1× | ui::path(path.display()) |
| 526 | | ); |
| 527 | 1× | Ok(0) |
| 528 | 1× | } |
| 529 | |
|
| 530 | 0× | pub fn rotate() -> Result<i32> { |
| 531 | 0× | let mut repo = open()?; |
| 532 | 0× | let r = repo.rotate_identity()?; |
| 533 | 0× | report_rotation(&repo, &r, "Rotated")?; |
| 534 | 0× | Ok(0) |
| 535 | 0× | } |
| 536 | |
|
| 537 | 0× | pub fn renew(expires: u64) -> Result<i32> { |
| 538 | 0× | let mut repo = open()?; |
| 539 | 0× | let r = repo.renew_identity(expiry_from_days(expires))?; |
| 540 | 0× | report_rotation(&repo, &r, "Renewed")?; |
| 541 | 0× | Ok(0) |
| 542 | 0× | } |
| 543 | |
|
| 544 | 0× | pub fn revoke(key: Option<&str>, reason: &str) -> Result<i32> { |
| 545 | 0× | let repo = open()?; |
| 546 | 0× | let key = match key { |
| 547 | 0× | Some(k) => resolve_key(&repo, k)?, |
| 548 | 0× | None => repo |
| 549 | 0× | .config |
| 550 | 0× | .author_key |
| 551 | 0× | .ok_or_else(|| anyhow::anyhow!("no identity configured"))?, |
| 552 | | }; |
| 553 | 0× | let update = repo.revoke_identity(&key, reason)?; |
| 554 | 0× | println!( |
| 555 | | "{} key {}{}", |
| 556 | 0× | ui::done("Revoked"), |
| 557 | 0× | ui::hash(key.short(12)), |
| 558 | 0× | if reason.is_empty() { |
| 559 | 0× | String::new() |
| 560 | | } else { |
| 561 | 0× | format!(": {reason}") |
| 562 | | } |
| 563 | | ); |
| 564 | 0× | match update { |
| 565 | 0× | Some(u) => report_update(&u, None)?, |
| 566 | 0× | None => println!( |
| 567 | | " {} {}", |
| 568 | 0× | ui::note(), |
| 569 | 0× | ui::dim("recorded locally; no policy named the key") |
| 570 | | ), |
| 571 | | } |
| 572 | | |
| 573 | | |
| 574 | 0× | let own = repo.config.author_key; |
| 575 | 0× | let delegated = own.is_some_and(|o| { |
| 576 | 0× | repo.identity(&o) |
| 577 | 0× | .is_ok_and(|id| id.delegate(&key).is_some()) |
| 578 | 0× | }); |
| 579 | 0× | if delegated { |
| 580 | 0× | let mut repo = repo; |
| 581 | 0× | let r = repo.delegate_remove(&key)?; |
| 582 | 0× | report_rotation(&repo, &r, "Undelegated")?; |
| 583 | 0× | return Ok(0); |
| 584 | 0× | } |
| 585 | 0× | if repo.config.author_key == Some(key) { |
| 586 | 0× | println!( |
| 587 | 0× | " {} {}", |
| 588 | 0× | ui::caution(), |
| 589 | 0× | ui::warn("this was your configured key; run oxy identity create or rotate") |
| 590 | 0× | ); |
| 591 | 0× | } |
| 592 | 0× | Ok(0) |
| 593 | 0× | } |
| 594 | |
|
| 595 | 0× | pub fn delegate_add(identity_files: &[PathBuf], scope: &str, expires: Option<&str>) -> Result<i32> { |
| 596 | 0× | let mut repo = open()?; |
| 597 | 0× | let ids = identity_files |
| 598 | 0× | .iter() |
| 599 | 0× | .map(|f| { |
| 600 | 0× | Identity::decode( |
| 601 | 0× | &std::fs::read(f).map_err(|e| anyhow::anyhow!("{}: {e}", f.display()))?, |
| 602 | | ) |
| 603 | 0× | .map_err(Into::into) |
| 604 | 0× | }) |
| 605 | 0× | .collect::<Result<Vec<_>>>()?; |
| 606 | 0× | let scope = Scope::parse(scope).map_err(|e| anyhow::anyhow!("{e}"))?; |
| 607 | 0× | let expires = expires.map(expiry_from_duration).transpose()?; |
| 608 | 0× | let r = repo.delegate_add_many(ids, scope, expires)?; |
| 609 | 0× | report_rotation(&repo, &r, "Delegated")?; |
| 610 | 0× | println!( |
| 611 | | " {} {}", |
| 612 | 0× | ui::note(), |
| 613 | 0× | ui::dim("an agent configured with your author.key needs the new key: oxy config --user author.key NEW in its home") |
| 614 | | ); |
| 615 | 0× | if !scope.has(Scope::REFS) { |
| 616 | 0× | println!( |
| 617 | 0× | " {} {}", |
| 618 | 0× | ui::note(), |
| 619 | 0× | ui::dim("it may not move references: a push or tag it attests is refused") |
| 620 | 0× | ); |
| 621 | 0× | } |
| 622 | 0× | Ok(0) |
| 623 | 0× | } |
| 624 | |
|
| 625 | 0× | pub fn delegate_remove(key: &str) -> Result<i32> { |
| 626 | 0× | let mut repo = open()?; |
| 627 | 0× | let k = resolve_key(&repo, key)?; |
| 628 | 0× | let r = repo.delegate_remove(&k)?; |
| 629 | 0× | report_rotation(&repo, &r, "Undelegated")?; |
| 630 | 0× | Ok(0) |
| 631 | 0× | } |
| 632 | |
|
| 633 | 0× | pub fn delegate_list() -> Result<i32> { |
| 634 | 0× | let repo = open()?; |
| 635 | 0× | let Some(key) = repo.config.author_key else { |
| 636 | 0× | println!("No identity configured; run `oxy identity create`."); |
| 637 | 0× | return Ok(0); |
| 638 | | }; |
| 639 | 0× | let id = repo.identity(&key)?; |
| 640 | 0× | if id.delegates.is_empty() { |
| 641 | 0× | println!("{}", ui::dim("No delegates.")); |
| 642 | 0× | } |
| 643 | 0× | let now = Hlc::now().wall_ms; |
| 644 | 0× | for d in &id.delegates { |
| 645 | 0× | let who = repo |
| 646 | 0× | .identity(&d.key) |
| 647 | 0× | .map(|i| format!("{} <{}>", i.name, i.email)) |
| 648 | 0× | .unwrap_or_else(|_| "(unknown identity)".into()); |
| 649 | 0× | let until = match d.expires { |
| 650 | 0× | Some(e) if now > e => ui::err(format!("expired {}", ui::ago(e))), |
| 651 | 0× | Some(e) => ui::dim(format!("until {}", when(e))), |
| 652 | 0× | None => String::new(), |
| 653 | | }; |
| 654 | 0× | println!( |
| 655 | | " {} {} {} {} {}", |
| 656 | 0× | ui::ok(ui::glyph("🔑", "*")), |
| 657 | 0× | ui::hash(d.key.short(12)), |
| 658 | 0× | ui::author(who), |
| 659 | 0× | ui::dim(d.scope.to_string()), |
| 660 | | until |
| 661 | | ); |
| 662 | | } |
| 663 | 0× | Ok(0) |
| 664 | 0× | } |
| 665 | |
|
| 666 | 0× | pub fn policy_root_add(identity_file: &Path, out: Option<PathBuf>) -> Result<i32> { |
| 667 | 0× | let repo = open()?; |
| 668 | 0× | let id = Identity::decode(&std::fs::read(identity_file)?)?; |
| 669 | 0× | let key = id.hash(); |
| 670 | 0× | let u = repo.policy_root_add(id.clone())?; |
| 671 | 0× | println!( |
| 672 | | "{} root {} <{}> ({})", |
| 673 | 0× | ui::done("Added"), |
| 674 | 0× | ui::author(&id.name), |
| 675 | | id.email, |
| 676 | 0× | key.short(12) |
| 677 | | ); |
| 678 | 0× | report_update(&u, out)?; |
| 679 | 0× | Ok(0) |
| 680 | 0× | } |
| 681 | |
|
| 682 | 0× | pub fn policy_root_remove(key: &str, out: Option<PathBuf>) -> Result<i32> { |
| 683 | 0× | let repo = open()?; |
| 684 | 0× | let k = resolve_key(&repo, key)?; |
| 685 | 0× | let u = repo.policy_root_remove(&k)?; |
| 686 | 0× | println!("{} root {}", ui::done("Removed"), ui::hash(k.short(12))); |
| 687 | 0× | report_update(&u, out)?; |
| 688 | 0× | Ok(0) |
| 689 | 0× | } |
| 690 | |
|
| 691 | 0× | pub fn policy_threshold(n: u32, out: Option<PathBuf>) -> Result<i32> { |
| 692 | 0× | let repo = open()?; |
| 693 | 0× | let u = repo.policy_set_threshold(n)?; |
| 694 | 0× | println!("{} root threshold to {n}", ui::done("Set")); |
| 695 | 0× | report_update(&u, out)?; |
| 696 | 0× | Ok(0) |
| 697 | 0× | } |
| 698 | |
|
| 699 | 0× | pub fn policy_sign(proposal: &Path, out: Option<PathBuf>) -> Result<i32> { |
| 700 | 0× | let repo = open()?; |
| 701 | 0× | let sp = SignedPolicy::decode(&std::fs::read(proposal)?)?; |
| 702 | 0× | let u = repo.policy_sign(sp)?; |
| 703 | 0× | println!("{} policy serial {}", ui::done("Signed"), u.policy.serial); |
| 704 | 0× | report_update(&u, Some(out.unwrap_or_else(|| proposal.to_path_buf())))?; |
| 705 | 0× | Ok(0) |
| 706 | 0× | } |
| 707 | |
|
| 708 | 1× | pub fn policy_init() -> Result<i32> { |
| 709 | 1× | let repo = open()?; |
| 710 | 1× | let sp = repo.policy_init()?; |
| 711 | 1× | println!( |
| 712 | | "{} policy with root key {}", |
| 713 | 1× | ui::done("Created"), |
| 714 | 1× | sp.policy.roots[0].short(12) |
| 715 | | ); |
| 716 | 1× | report_update(&sp, None)?; |
| 717 | 1× | Ok(0) |
| 718 | 1× | } |
| 719 | |
|
| 720 | 3× | pub fn policy_show(as_json: bool) -> Result<i32> { |
| 721 | 3× | let repo = open()?; |
| 722 | 3× | let Some((r, sp)) = repo.current_policy()? else { |
| 723 | 1× | if as_json { |
| 724 | 1× | println!("null"); |
| 725 | 1× | } else { |
| 726 | 0× | println!( |
| 727 | 0× | "No policy: this repository is unverified. Run `oxy policy init` to create one." |
| 728 | 0× | ); |
| 729 | 0× | } |
| 730 | 1× | return Ok(0); |
| 731 | | }; |
| 732 | 2× | let p = &sp.policy; |
| 733 | 2× | if as_json { |
| 734 | 5× | let ident = |k: &oxy_core::hash::KeyId| { |
| 735 | 5× | let (name, email) = p |
| 736 | 5× | .identity(k) |
| 737 | 5× | .map(|i| (i.name.clone(), i.email.clone())) |
| 738 | 5× | .unwrap_or_default(); |
| 739 | 5× | json::obj([ |
| 740 | 5× | ("key", json::s(&k.to_hex())), |
| 741 | 5× | ("name", json::s(&name)), |
| 742 | 5× | ("email", json::s(&email)), |
| 743 | 5× | ]) |
| 744 | 5× | }; |
| 745 | 2× | println!( |
| 746 | | "{}", |
| 747 | 2× | json::obj([ |
| 748 | 2× | ("hash", json::s(&r.to_hex())), |
| 749 | 2× | ("serial", json::n(p.serial)), |
| 750 | 2× | ("expires_ms", json::n(p.expires)), |
| 751 | 2× | ( |
| 752 | 2× | "expired", |
| 753 | 2× | json::b(p.is_expired(oxy_core::objects::Hlc::now().wall_ms)) |
| 754 | 2× | ), |
| 755 | 2× | ("root_threshold", json::n(p.root_threshold)), |
| 756 | 2× | ("roots", json::arr(p.roots.iter().map(ident))), |
| 757 | | ( |
| 758 | 2× | "rules", |
| 759 | 2× | json::arr(p.rules.iter().map(|rule| { |
| 760 | 2× | json::obj([ |
| 761 | 2× | ("pattern", json::s(&rule.pattern)), |
| 762 | 2× | ("keys", json::arr(rule.keys.iter().map(ident))), |
| 763 | 2× | ("threshold", json::n(rule.threshold)), |
| 764 | 2× | ("require", json::s(rule.require.name())), |
| 765 | 2× | ("allow_conflicts", json::b(rule.allow_conflicts)), |
| 766 | | ( |
| 767 | 2× | "subjects", |
| 768 | 2× | json::arr(rule.subjects.iter().map(|g| { |
| 769 | 1× | json::obj([ |
| 770 | 1× | ("subject", json::s(&g.pattern)), |
| 771 | 1× | ("issuer", ident(&g.issuer)), |
| 772 | 1× | ("scope", json::s(&g.scope.to_string())), |
| 773 | 1× | ]) |
| 774 | 1× | })), |
| 775 | | ), |
| 776 | | ]) |
| 777 | 2× | })), |
| 778 | | ), |
| 779 | 2× | ("revoked", json::arr(p.revoked.iter().map(ident))), |
| 780 | | ]) |
| 781 | | ); |
| 782 | 2× | return Ok(0); |
| 783 | 0× | } |
| 784 | 0× | let name = |k: &oxy_core::hash::KeyId| { |
| 785 | 0× | p.identity(k) |
| 786 | 0× | .map(|i| format!("{} <{}>", i.name, i.email)) |
| 787 | 0× | .unwrap_or_else(|| "(unknown identity)".into()) |
| 788 | 0× | }; |
| 789 | 0× | let expired = p.is_expired(oxy_core::objects::Hlc::now().wall_ms); |
| 790 | 0× | println!( |
| 791 | | "{} {}{}serial {}{}expires {}{}", |
| 792 | 0× | ui::section("policy"), |
| 793 | 0× | ui::hash(format!("#{}", r.short(12))), |
| 794 | 0× | ui::sep(), |
| 795 | | p.serial, |
| 796 | 0× | ui::sep(), |
| 797 | 0× | when(p.expires), |
| 798 | 0× | if expired { |
| 799 | 0× | ui::err(format!(" {} EXPIRED", ui::glyph("✖", "x"))) |
| 800 | | } else { |
| 801 | 0× | String::new() |
| 802 | | } |
| 803 | | ); |
| 804 | 0× | println!( |
| 805 | | "{}", |
| 806 | 0× | ui::section(format!( |
| 807 | | "roots {}", |
| 808 | 0× | ui::dim(format!("(threshold {})", p.root_threshold)) |
| 809 | | )) |
| 810 | | ); |
| 811 | 0× | for k in &p.roots { |
| 812 | 0× | println!( |
| 813 | 0× | " {} {} {}", |
| 814 | 0× | ui::ok(ui::glyph("🔑", "*")), |
| 815 | 0× | ui::hash(k.short(12)), |
| 816 | 0× | ui::author(name(k)) |
| 817 | 0× | ); |
| 818 | 0× | } |
| 819 | 0× | println!("{}", ui::section("rules")); |
| 820 | 0× | for rule in &p.rules { |
| 821 | 0× | println!( |
| 822 | | " {} {:12} {}", |
| 823 | 0× | ui::ok(ui::glyph("▸", ">")), |
| 824 | 0× | ui::bookmark(&rule.pattern), |
| 825 | 0× | ui::dim(format!( |
| 826 | | "threshold {}, require {}, conflicts {}", |
| 827 | | rule.threshold, |
| 828 | 0× | rule.require.name(), |
| 829 | 0× | if rule.allow_conflicts { |
| 830 | 0× | "allowed" |
| 831 | | } else { |
| 832 | 0× | "refused" |
| 833 | | } |
| 834 | | )) |
| 835 | | ); |
| 836 | 0× | for k in &rule.keys { |
| 837 | 0× | println!(" {} {}", ui::hash(k.short(12)), ui::author(name(k))); |
| 838 | 0× | } |
| 839 | 0× | for g in &rule.subjects { |
| 840 | 0× | println!( |
| 841 | 0× | " {} {} {}", |
| 842 | 0× | ui::author(&g.pattern), |
| 843 | 0× | ui::dim(format!( |
| 844 | 0× | "certified by {} ({}),", |
| 845 | 0× | name(&g.issuer), |
| 846 | 0× | g.issuer.short(12) |
| 847 | 0× | )), |
| 848 | 0× | ui::dim(format!("scope {}", g.scope)) |
| 849 | 0× | ); |
| 850 | 0× | } |
| 851 | | } |
| 852 | 0× | if !p.revoked.is_empty() { |
| 853 | 0× | println!("{}", ui::section("revoked")); |
| 854 | 0× | for k in &p.revoked { |
| 855 | 0× | println!( |
| 856 | 0× | " {} {} {}", |
| 857 | 0× | ui::err(ui::glyph("✖", "x")), |
| 858 | 0× | ui::hash(k.short(12)), |
| 859 | 0× | ui::author(name(k)) |
| 860 | 0× | ); |
| 861 | 0× | } |
| 862 | 0× | } |
| 863 | 0× | Ok(0) |
| 864 | 3× | } |
| 865 | |
|
| 866 | 0× | pub fn policy_grant( |
| 867 | 0× | identity_file: &Path, |
| 868 | 0× | pattern: Option<&str>, |
| 869 | 0× | out: Option<PathBuf>, |
| 870 | 0× | ) -> Result<i32> { |
| 871 | 0× | let repo = open()?; |
| 872 | 0× | let id = Identity::decode(&std::fs::read(identity_file)?)?; |
| 873 | 0× | let key = id.hash(); |
| 874 | 0× | let u = repo.policy_grant(id.clone(), pattern)?; |
| 875 | 0× | println!( |
| 876 | | "{} {} <{}> ({}) on {}", |
| 877 | 0× | ui::done("Granted"), |
| 878 | 0× | ui::author(&id.name), |
| 879 | | id.email, |
| 880 | 0× | key.short(12), |
| 881 | 0× | ui::bookmark(pattern.unwrap_or("*")) |
| 882 | | ); |
| 883 | 0× | report_update(&u, out)?; |
| 884 | 0× | Ok(0) |
| 885 | 0× | } |
| 886 | |
|
| 887 | 1× | pub fn policy_grant_subject( |
| 888 | 1× | subject: &str, |
| 889 | 1× | issuer_file: &Path, |
| 890 | 1× | scope: &str, |
| 891 | 1× | pattern: Option<&str>, |
| 892 | 1× | out: Option<PathBuf>, |
| 893 | 1× | ) -> Result<i32> { |
| 894 | 1× | let repo = open()?; |
| 895 | 1× | let issuer = Identity::decode(&std::fs::read(issuer_file)?) |
| 896 | 1× | .map_err(|e| anyhow::anyhow!("{}: not an identity file: {e}", issuer_file.display()))?; |
| 897 | 1× | let scope = oxy_core::identity::Scope::parse(scope).map_err(|e| anyhow::anyhow!(e))?; |
| 898 | 1× | let key = issuer.hash(); |
| 899 | 1× | let u = repo.policy_grant_subject(subject, issuer.clone(), scope, pattern)?; |
| 900 | 1× | println!( |
| 901 | | "{} {} certified by {} ({}) on {}, scope {}", |
| 902 | 1× | ui::done("Granted"), |
| 903 | 1× | ui::author(subject), |
| 904 | | issuer.name, |
| 905 | 1× | key.short(12), |
| 906 | 1× | ui::bookmark(pattern.unwrap_or("*")), |
| 907 | | scope |
| 908 | | ); |
| 909 | 1× | report_update(&u, out)?; |
| 910 | 1× | Ok(0) |
| 911 | 1× | } |
| 912 | |
|
| 913 | 0× | pub fn policy_revoke(key: &str, out: Option<PathBuf>) -> Result<i32> { |
| 914 | 0× | let repo = open()?; |
| 915 | 0× | if repo.current_policy()?.is_none() { |
| 916 | 0× | anyhow::bail!("this repository has no policy"); |
| 917 | 0× | } |
| 918 | 0× | let full = resolve_key(&repo, key)?; |
| 919 | 0× | let u = repo.policy_revoke(&full)?; |
| 920 | 0× | println!("{} {}", ui::done("Revoked"), ui::hash(full.short(12))); |
| 921 | 0× | report_update(&u, out)?; |
| 922 | 0× | Ok(0) |
| 923 | 0× | } |
| 924 | |
|
| 925 | |
|
| 926 | 0× | pub fn seal(specs: &[String], as_json: bool) -> Result<i32> { |
| 927 | 0× | let repo = Repo::open(&std::env::current_dir()?)?; |
| 928 | 0× | let hashes: Vec<_> = if specs.is_empty() { |
| 929 | 0× | repo.sealable()? |
| 930 | | } else { |
| 931 | 0× | specs |
| 932 | 0× | .iter() |
| 933 | 0× | .map(|s| repo.resolve_change(s)) |
| 934 | 0× | .collect::<std::result::Result<_, _>>()? |
| 935 | | }; |
| 936 | 0× | if hashes.is_empty() { |
| 937 | 0× | if as_json { |
| 938 | 0× | println!("{{\"sealed\":[]}}"); |
| 939 | 0× | } else { |
| 940 | 0× | println!( |
| 941 | 0× | "{} nothing to seal: every change you can authenticate already is", |
| 942 | 0× | ui::done("Sealed") |
| 943 | 0× | ); |
| 944 | 0× | } |
| 945 | 0× | return Ok(0); |
| 946 | 0× | } |
| 947 | 0× | let mut refs = Vec::new(); |
| 948 | 0× | for chunk in hashes.chunks(oxy_core::attest::MAX_SEAL_HASHES) { |
| 949 | 0× | refs.push(repo.seal(chunk)?); |
| 950 | | } |
| 951 | 0× | if as_json { |
| 952 | 0× | let list: Vec<String> = hashes |
| 953 | 0× | .iter() |
| 954 | 0× | .map(|h| format!("\"{}\"", h.to_hex())) |
| 955 | 0× | .collect(); |
| 956 | 0× | let seals: Vec<String> = refs.iter().map(|r| format!("\"{}\"", r.to_hex())).collect(); |
| 957 | 0× | println!( |
| 958 | | "{{\"sealed\":[{}],\"seals\":[{}]}}", |
| 959 | 0× | list.join(","), |
| 960 | 0× | seals.join(",") |
| 961 | | ); |
| 962 | | } else { |
| 963 | 0× | println!( |
| 964 | | "{} {} change{} in {} seal{}; they travel with your next push", |
| 965 | 0× | ui::done("Sealed"), |
| 966 | 0× | hashes.len(), |
| 967 | 0× | if hashes.len() == 1 { "" } else { "s" }, |
| 968 | 0× | refs.len(), |
| 969 | 0× | if refs.len() == 1 { "" } else { "s" } |
| 970 | | ); |
| 971 | 0× | for h in &hashes { |
| 972 | 0× | let title = repo |
| 973 | 0× | .store() |
| 974 | 0× | .get_change(h) |
| 975 | 0× | .map(|c| c.message.lines().next().unwrap_or("").to_string()) |
| 976 | 0× | .unwrap_or_default(); |
| 977 | 0× | println!(" {} {}", ui::hash(h.short(12)), title); |
| 978 | | } |
| 979 | | } |
| 980 | 0× | Ok(0) |
| 981 | 0× | } |
| 982 | |
|
| 983 | |
|
| 984 | |
|
| 985 | 0× | pub fn sign_challenge(challenge_hex: &str, as_json: bool) -> Result<i32> { |
| 986 | 0× | let repo = open()?; |
| 987 | 0× | let Some((key, sk)) = repo.secret_key()? else { |
| 988 | 0× | anyhow::bail!("no secret key: run `oxy identity create` first"); |
| 989 | | }; |
| 990 | 0× | let challenge = hex_bytes(challenge_hex.trim()) |
| 991 | 0× | .ok_or_else(|| anyhow::anyhow!("the challenge must be hex"))?; |
| 992 | 0× | let sig = sk |
| 993 | 0× | .sign_challenge(&key, &challenge) |
| 994 | 0× | .map_err(|e| anyhow::anyhow!("{e}"))?; |
| 995 | 0× | let encoded = hex_string(&oxy_core::identity::Signature::encode_list(&[sig])); |
| 996 | 0× | if as_json { |
| 997 | 0× | println!( |
| 998 | 0× | "{{\"key\":\"{}\",\"signature\":\"{}\"}}", |
| 999 | 0× | key.to_hex(), |
| 1000 | 0× | encoded |
| 1001 | 0× | ); |
| 1002 | 0× | } else { |
| 1003 | 0× | println!("{encoded}"); |
| 1004 | 0× | } |
| 1005 | 0× | Ok(0) |
| 1006 | 0× | } |
| 1007 | |
|
| 1008 | 0× | fn hex_bytes(s: &str) -> Option<Vec<u8>> { |
| 1009 | 0× | if !s.len().is_multiple_of(2) { |
| 1010 | 0× | return None; |
| 1011 | 0× | } |
| 1012 | 0× | (0..s.len()) |
| 1013 | 0× | .step_by(2) |
| 1014 | 0× | .map(|i| u8::from_str_radix(&s[i..i + 2], 16).ok()) |
| 1015 | 0× | .collect() |
| 1016 | 0× | } |
| 1017 | |
|
| 1018 | 0× | fn hex_string(b: &[u8]) -> String { |
| 1019 | 0× | b.iter().map(|x| format!("{x:02x}")).collect() |
| 1020 | 0× | } |
| 1021 | |
|
| 1022 | | #[cfg(test)] |
| 1023 | | mod tests { |
| 1024 | | #[test] |
| 1025 | 1× | fn secrets_are_never_printed_whole() { |
| 1026 | 1× | assert!( |
| 1027 | 1× | super::secret_key("remote.earth.token") |
| 1028 | 1× | && super::secret_key("auth.secret") |
| 1029 | 1× | && !super::secret_key("remote.earth.url") |
| 1030 | | ); |
| 1031 | 1× | assert_eq!( |
| 1032 | 1× | super::mask("oxyhub_6b7dce13409b634d"), |
| 1033 | | "oxyhub_6b7…(13 more)" |
| 1034 | | ); |
| 1035 | 1× | assert_eq!(super::mask("short"), "…"); |
| 1036 | 1× | } |
| 1037 | | } |