| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use super::*; |
| 6 | |
|
| 7 | | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 8 | | pub enum Health { |
| 9 | | Ok, |
| 10 | | Warn, |
| 11 | | Fail, |
| 12 | | } |
| 13 | |
|
| 14 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 15 | | pub struct Check { |
| 16 | | |
| 17 | | pub area: String, |
| 18 | | pub health: Health, |
| 19 | | pub message: String, |
| 20 | | |
| 21 | | pub hint: Option<String>, |
| 22 | | } |
| 23 | |
|
| 24 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 25 | | pub struct DoctorReport { |
| 26 | | pub checks: Vec<Check>, |
| 27 | | } |
| 28 | |
|
| 29 | | impl DoctorReport { |
| 30 | 1× | pub fn worst(&self) -> Health { |
| 31 | 1× | self.checks |
| 32 | 1× | .iter() |
| 33 | 1× | .map(|c| c.health) |
| 34 | 1× | .max() |
| 35 | 1× | .unwrap_or(Health::Ok) |
| 36 | 1× | } |
| 37 | |
|
| 38 | 42× | fn push( |
| 39 | 42× | &mut self, |
| 40 | 42× | area: impl Into<String>, |
| 41 | 42× | health: Health, |
| 42 | 42× | message: impl Into<String>, |
| 43 | 42× | hint: Option<String>, |
| 44 | 42× | ) { |
| 45 | 42× | self.checks.push(Check { |
| 46 | 42× | area: area.into(), |
| 47 | 42× | health, |
| 48 | 42× | message: message.into(), |
| 49 | 42× | hint, |
| 50 | 42× | }); |
| 51 | 42× | } |
| 52 | | } |
| 53 | |
|
| 54 | |
|
| 55 | | const STALE_LOCK_SECS: u64 = 60; |
| 56 | |
|
| 57 | | impl Repo { |
| 58 | | |
| 59 | 6× | pub fn doctor(&self, online: bool) -> DoctorReport { |
| 60 | 6× | let mut rep = DoctorReport::default(); |
| 61 | 6× | self.doctor_repository(&mut rep); |
| 62 | 6× | self.doctor_identity(&mut rep); |
| 63 | 6× | self.doctor_author(&mut rep); |
| 64 | 6× | self.doctor_portability(&mut rep); |
| 65 | 6× | self.doctor_trust(&mut rep); |
| 66 | 6× | for name in self.config.remotes.keys() { |
| 67 | 6× | self.doctor_remote(&mut rep, name, online); |
| 68 | 6× | } |
| 69 | 6× | if self.config.remotes.is_empty() && !self.bare { |
| 70 | 4× | rep.push("remotes", Health::Ok, "none configured", None); |
| 71 | 4× | } |
| 72 | 6× | rep |
| 73 | 6× | } |
| 74 | |
|
| 75 | 6× | fn doctor_repository(&self, rep: &mut DoctorReport) { |
| 76 | 6× | match self.head_op() { |
| 77 | 6× | Ok(_) => rep.push( |
| 78 | | "repository", |
| 79 | 6× | Health::Ok, |
| 80 | 6× | format!( |
| 81 | | "{} at {}", |
| 82 | 6× | if self.bare { |
| 83 | 0× | "bare repository" |
| 84 | | } else { |
| 85 | 6× | "repository" |
| 86 | | }, |
| 87 | 6× | self.root.display() |
| 88 | | ), |
| 89 | 6× | None, |
| 90 | | ), |
| 91 | 0× | Err(e) => rep.push( |
| 92 | | "repository", |
| 93 | 0× | Health::Fail, |
| 94 | 0× | format!("the operation log does not open: {e}"), |
| 95 | 0× | Some("run `oxy fsck` for details".into()), |
| 96 | | ), |
| 97 | | } |
| 98 | 6× | let mut locks = vec![self.oxy.join("ops").join("lock")]; |
| 99 | 6× | if let Ok(rd) = fs::read_dir(self.oxy.join("workspaces")) { |
| 100 | 6× | locks.extend(rd.flatten().map(|e| e.path().join("lock"))); |
| 101 | 0× | } |
| 102 | 12× | for lock in locks { |
| 103 | 12× | let age = fs::metadata(&lock) |
| 104 | 12× | .and_then(|m| m.modified()) |
| 105 | 12× | .ok() |
| 106 | 12× | .and_then(|t| t.elapsed().ok()); |
| 107 | 12× | if let Some(age) = age.filter(|a| a.as_secs() > STALE_LOCK_SECS) { |
| 108 | 0× | rep.push( |
| 109 | 0× | "repository", |
| 110 | 0× | Health::Warn, |
| 111 | 0× | format!( |
| 112 | 0× | "a lock left by a process that stopped {} s ago: {}", |
| 113 | 0× | age.as_secs(), |
| 114 | 0× | lock.display() |
| 115 | 0× | ), |
| 116 | 0× | Some( |
| 117 | 0× | "it is taken over automatically; delete it if no oxy command is running" |
| 118 | 0× | .into(), |
| 119 | 0× | ), |
| 120 | 0× | ); |
| 121 | 12× | } |
| 122 | | } |
| 123 | 6× | } |
| 124 | |
|
| 125 | 6× | fn doctor_identity(&self, rep: &mut DoctorReport) { |
| 126 | 6× | let Some(key) = self.config.author_key else { |
| 127 | 2× | rep.push( |
| 128 | | "identity", |
| 129 | 2× | Health::Warn, |
| 130 | | "no signing identity: pushes to a repository with a trust policy will be refused", |
| 131 | 2× | Some("create one with `oxy identity create`".into()), |
| 132 | | ); |
| 133 | 2× | return; |
| 134 | | }; |
| 135 | 4× | let id = match self.identity(&key) { |
| 136 | 3× | Ok(id) => id, |
| 137 | | |
| 138 | | |
| 139 | | Err(_) |
| 140 | 1× | if self |
| 141 | 1× | .keys_dir |
| 142 | 1× | .join(format!("{}.secret", key.to_hex())) |
| 143 | 1× | .is_file() => |
| 144 | | { |
| 145 | 1× | rep.push( |
| 146 | | "identity", |
| 147 | 1× | Health::Ok, |
| 148 | 1× | format!( |
| 149 | | "identity {} has its secret key; it is stored in the repository with your first signed change", |
| 150 | 1× | key.short(12) |
| 151 | | ), |
| 152 | 1× | None, |
| 153 | | ); |
| 154 | 1× | return; |
| 155 | | } |
| 156 | 0× | Err(e) => { |
| 157 | 0× | rep.push( |
| 158 | | "identity", |
| 159 | 0× | Health::Fail, |
| 160 | 0× | format!( |
| 161 | | "identity {} is configured but cannot be read: {e}", |
| 162 | 0× | key.short(12) |
| 163 | | ), |
| 164 | 0× | Some("restore it, or create a new one with `oxy identity create`".into()), |
| 165 | | ); |
| 166 | 0× | return; |
| 167 | | } |
| 168 | | }; |
| 169 | 3× | let secret = self.keys_dir.join(format!("{}.secret", key.to_hex())); |
| 170 | 3× | if !secret.is_file() { |
| 171 | 0× | rep.push( |
| 172 | | "identity", |
| 173 | 0× | Health::Fail, |
| 174 | 0× | format!("the secret key for {} is missing ({})", key.short(12), secret.display()), |
| 175 | 0× | Some("copy the key file back from a backup; without it nothing can be signed with this identity".into()), |
| 176 | | ); |
| 177 | 0× | return; |
| 178 | 3× | } |
| 179 | 3× | let now = Hlc::now().wall_ms; |
| 180 | 0× | match id.expires { |
| 181 | 0× | Some(t) if t <= now => rep.push( |
| 182 | | "identity", |
| 183 | 0× | Health::Fail, |
| 184 | 0× | format!("{} <{}> expired", id.name, id.email), |
| 185 | 0× | Some("renew it with `oxy identity renew`".into()), |
| 186 | | ), |
| 187 | 0× | Some(t) if t - now < 30 * 86_400_000 => rep.push( |
| 188 | | "identity", |
| 189 | 0× | Health::Warn, |
| 190 | 0× | format!( |
| 191 | | "{} <{}> expires in {} days", |
| 192 | | id.name, |
| 193 | | id.email, |
| 194 | 0× | (t - now) / 86_400_000 |
| 195 | | ), |
| 196 | 0× | Some("renew it with `oxy identity renew`".into()), |
| 197 | | ), |
| 198 | 3× | _ => rep.push( |
| 199 | | "identity", |
| 200 | 3× | Health::Ok, |
| 201 | 3× | format!("{} <{}> ({})", id.name, id.email, key.short(12)), |
| 202 | 3× | None, |
| 203 | | ), |
| 204 | | } |
| 205 | 6× | } |
| 206 | |
|
| 207 | | |
| 208 | | |
| 209 | 6× | fn doctor_portability(&self, rep: &mut DoctorReport) { |
| 210 | 6× | let Ok(heads) = self.bookmark_heads() else { |
| 211 | 0× | return; |
| 212 | | }; |
| 213 | 6× | let Ok(mut state) = self.load_state_history(&heads) else { |
| 214 | 0× | return; |
| 215 | | }; |
| 216 | 6× | let Ok(view) = self.tree_view(&mut state) else { |
| 217 | 0× | return; |
| 218 | | }; |
| 219 | 6× | let paths: Vec<&str> = view.entries.keys().map(String::as_str).collect(); |
| 220 | 6× | let mut problems: Vec<String> = |
| 221 | 6× | crate::portable::Platform::windows().problems(paths.iter().copied()); |
| 222 | 6× | problems.sort(); |
| 223 | 6× | problems.dedup(); |
| 224 | 6× | if problems.is_empty() { |
| 225 | 5× | rep.push( |
| 226 | | "portability", |
| 227 | 5× | Health::Ok, |
| 228 | 5× | format!( |
| 229 | | "every path of {} checks out on Linux, macOS and Windows", |
| 230 | | self.config.bookmark |
| 231 | | ), |
| 232 | 5× | None, |
| 233 | | ); |
| 234 | 5× | return; |
| 235 | 1× | } |
| 236 | 1× | let shown: Vec<&String> = problems.iter().take(5).collect(); |
| 237 | 1× | rep.push( |
| 238 | | "portability", |
| 239 | 1× | Health::Warn, |
| 240 | 1× | format!( |
| 241 | | "{} path problem{} on case-insensitive or Windows file systems: {}{}", |
| 242 | 1× | problems.len(), |
| 243 | 1× | if problems.len() == 1 { "" } else { "s" }, |
| 244 | 2× | shown.iter().map(|s| s.as_str()).collect::<Vec<_>>().join("; "), |
| 245 | 1× | if problems.len() > 5 { "; …" } else { "" } |
| 246 | | ), |
| 247 | 1× | Some("rename the paths so they differ by more than case and avoid Windows' reserved names and characters".into()), |
| 248 | | ); |
| 249 | 6× | } |
| 250 | |
|
| 251 | 6× | fn doctor_author(&self, rep: &mut DoctorReport) { |
| 252 | 6× | let defaulted = |
| 253 | 6× | self.config.author_email.ends_with("@localhost") || self.config.author_name.is_empty(); |
| 254 | 6× | if defaulted { |
| 255 | 2× | rep.push( |
| 256 | 2× | "author", |
| 257 | 2× | Health::Warn, |
| 258 | 2× | format!("changes are authored as {} <{}>, a default", self.config.author_name, self.config.author_email), |
| 259 | 2× | Some("set yours with `oxy config --user author.name NAME` and `oxy config --user author.email EMAIL`".into()), |
| 260 | 2× | ); |
| 261 | 4× | } else { |
| 262 | 4× | rep.push( |
| 263 | 4× | "author", |
| 264 | 4× | Health::Ok, |
| 265 | 4× | format!("{} <{}>", self.config.author_name, self.config.author_email), |
| 266 | 4× | None, |
| 267 | 4× | ); |
| 268 | 4× | } |
| 269 | 6× | } |
| 270 | |
|
| 271 | | |
| 272 | | |
| 273 | | |
| 274 | 6× | fn doctor_trust(&self, rep: &mut DoctorReport) { |
| 275 | 6× | let policy = match self.current_policy() { |
| 276 | 6× | Ok(p) => p, |
| 277 | 0× | Err(e) => { |
| 278 | 0× | rep.push( |
| 279 | | "trust", |
| 280 | 0× | Health::Fail, |
| 281 | 0× | format!("the trust policy does not load: {e}"), |
| 282 | 0× | Some("run `oxy fsck` for details".into()), |
| 283 | | ); |
| 284 | 0× | return; |
| 285 | | } |
| 286 | | }; |
| 287 | 6× | let Some((r, sp)) = policy else { |
| 288 | 3× | rep.push( |
| 289 | | "trust", |
| 290 | 3× | Health::Warn, |
| 291 | | "no trust policy: changes and bookmark moves fetched here are not verified", |
| 292 | 3× | Some( |
| 293 | 3× | "the repository's owner sets one: `oxy policy init`, then `oxy policy grant FILE` for each \ |
| 294 | 3× | contributor and `oxy policy grant-subject --issuer HUB_KEY agent:NAME@ACCOUNT` for keyless \ |
| 295 | 3× | agents, and `oxy push`" |
| 296 | 3× | .into(), |
| 297 | 3× | ), |
| 298 | | ); |
| 299 | 3× | return; |
| 300 | | }; |
| 301 | 3× | let p = &sp.policy; |
| 302 | 3× | let now = Hlc::now().wall_ms; |
| 303 | 3× | let what = format!( |
| 304 | | "policy #{} serial {}: {} root{}, {} rule{}", |
| 305 | 3× | &r.to_hex()[..12], |
| 306 | | p.serial, |
| 307 | 3× | p.roots.len(), |
| 308 | 3× | if p.roots.len() == 1 { "" } else { "s" }, |
| 309 | 3× | p.rules.len(), |
| 310 | 3× | if p.rules.len() == 1 { "" } else { "s" }, |
| 311 | | ); |
| 312 | 3× | let renew = Some( |
| 313 | 3× | "a root of the policy publishes a new serial (any `oxy policy` change does)".into(), |
| 314 | 3× | ); |
| 315 | 3× | if p.is_expired(now) { |
| 316 | 0× | rep.push("trust", Health::Fail, format!("{what}, expired"), renew); |
| 317 | 3× | } else if p.expires - now < 30 * 86_400_000 { |
| 318 | 0× | rep.push( |
| 319 | 0× | "trust", |
| 320 | 0× | Health::Warn, |
| 321 | 0× | format!("{what}, expires in {} days", (p.expires - now) / 86_400_000), |
| 322 | 0× | renew, |
| 323 | 0× | ); |
| 324 | 3× | } else { |
| 325 | 3× | rep.push( |
| 326 | 3× | "trust", |
| 327 | 3× | Health::Ok, |
| 328 | 3× | format!("{what}, expires in {} days", (p.expires - now) / 86_400_000), |
| 329 | 3× | None, |
| 330 | 3× | ); |
| 331 | 3× | } |
| 332 | 6× | } |
| 333 | |
|
| 334 | 6× | fn doctor_remote(&self, rep: &mut DoctorReport, name: &str, online: bool) { |
| 335 | 6× | let area = format!("remote {name}"); |
| 336 | 6× | let url = &self.config.remotes[name]; |
| 337 | 6× | let opts = self.remote_options(name); |
| 338 | 6× | let http = url.starts_with("http://") || url.starts_with("https://"); |
| 339 | 6× | let hint = |h: &str| Some(h.replace("<remote>", name)); |
| 340 | 6× | if url.starts_with("http://") && !is_loopback(url) { |
| 341 | 0× | rep.push( |
| 342 | 0× | &area, |
| 343 | 0× | Health::Warn, |
| 344 | 0× | format!("{url} is plain HTTP: the token travels unencrypted"), |
| 345 | 0× | hint("use the hub's https:// address"), |
| 346 | 0× | ); |
| 347 | 6× | } |
| 348 | 6× | if http && opts.token.is_none() { |
| 349 | 2× | rep.push( |
| 350 | 2× | &area, |
| 351 | 2× | Health::Warn, |
| 352 | 2× | "no token: fetching public repositories works, pushing will not", |
| 353 | 2× | hint("set one with `oxy config remote.<remote>.token TOKEN`"), |
| 354 | 2× | ); |
| 355 | 4× | } |
| 356 | 6× | if let Some(ca) = &opts.ca { |
| 357 | 2× | if !ca.is_file() { |
| 358 | 2× | rep.push(&area, Health::Fail, format!("the CA file {} does not exist", ca.display()), hint("download the hub's certificate from /.well-known/oxyhub-ca.pem and run `oxy config remote.<remote>.ca FILE`")); |
| 359 | 2× | return; |
| 360 | 0× | } |
| 361 | 4× | } |
| 362 | 4× | if !online { |
| 363 | 2× | rep.push(&area, Health::Ok, format!("{url} (not contacted)"), None); |
| 364 | 2× | return; |
| 365 | 2× | } |
| 366 | 2× | let reached = crate::protocol::open_remote(url, &opts).and_then(|r| r.repo_id()); |
| 367 | 0× | match reached { |
| 368 | 1× | Ok(theirs) if theirs == crate::protocol::UNBOUND_ID => rep.push( |
| 369 | 1× | &area, |
| 370 | 1× | Health::Ok, |
| 371 | 1× | format!("{url} is reachable and empty; the first push binds it"), |
| 372 | 1× | None, |
| 373 | | ), |
| 374 | 0× | Ok(theirs) if self.id().is_ok_and(|mine| mine == theirs) => rep.push( |
| 375 | 0× | &area, |
| 376 | 0× | Health::Ok, |
| 377 | 0× | format!("{url} is reachable and is this repository"), |
| 378 | 0× | None, |
| 379 | | ), |
| 380 | 0× | Ok(_) => rep.push( |
| 381 | 0× | &area, |
| 382 | 0× | Health::Fail, |
| 383 | 0× | format!("{url} is a different repository"), |
| 384 | 0× | hint("check the URL; each repository has its own id"), |
| 385 | | ), |
| 386 | 1× | Err(e) => { |
| 387 | 1× | let msg = e.to_string(); |
| 388 | 1× | let first = msg.lines().next().unwrap_or(&msg).to_string(); |
| 389 | 1× | rep.push( |
| 390 | 1× | &area, |
| 391 | 1× | Health::Fail, |
| 392 | 1× | first, |
| 393 | 1× | crate::protocol::hint_for(&msg).map(|h| h.replace("<remote>", name)), |
| 394 | | ); |
| 395 | | } |
| 396 | | } |
| 397 | 6× | } |
| 398 | | } |
| 399 | |
|
| 400 | 2× | fn is_loopback(url: &str) -> bool { |
| 401 | 2× | let host = url |
| 402 | 2× | .split("://") |
| 403 | 2× | .nth(1) |
| 404 | 2× | .unwrap_or("") |
| 405 | 2× | .split(['/', ':']) |
| 406 | 2× | .next() |
| 407 | 2× | .unwrap_or(""); |
| 408 | 2× | host == "localhost" || host == "127.0.0.1" || host == "[::1]" |
| 409 | 2× | } |