devsandbox 0.11.1

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! `credentials.toml`, next to `state.toml`: the home's credentials
//! (credential-broker, *`credentials.toml`*). One table per credential,
//! named as `devsandbox login <credential>` named it (`sentry-work`), holding
//! its kind, where its secret is (`source`), its grant, and the claim
//! versions approved for it and pending approval.
//!
//! An approved version keeps the canonical pinned claim
//! ([`Claim::canonical`]) under its hash ([`Claim::version`]): the broker
//! uses this copy, never the instance's, and `access approve` diffs a
//! pending one against it. Each is re-hashed at load, so a hand edit to a
//! claim without its version fails the file instead of approving content
//! nobody saw. `approved` is in approval order: the last is the latest.
//!
//! Written by `login` and `access approve` (and `run` recording a pending
//! version) under the sibling lock, temp file and rename
//! (`serve::locked_file`). Kept out of every container by `key_guard`.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use super::claim::Claim;
use super::review::View;

/// Every credential by name.
pub type Home = BTreeMap<String, Entry>;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Entry {
    /// The claim kind it serves (`sentry`): what a config's `access.<kind>`
    /// and a feature's claim name.
    pub kind: String,
    #[serde(default)]
    pub source: Source,
    /// The profiles whose instances may use it; unset: every profile, as a
    /// sandbox's unset `profiles` allows every declared one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub profiles: Option<Vec<String>>,
    /// The scopes it grants at most; unset: every scope of the claim.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max: Option<Vec<String>>,
    // Read from credential-broker step 4 (`approvers`), 5 (`members`) and 7
    // (`delegate`).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub members: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub approvers: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub delegate: Vec<String>,
    /// The token kinds it offers (`credentials::tokens::pick`); unset:
    /// `["user"]`.
    #[serde(default, rename = "as", skip_serializing_if = "Vec::is_empty")]
    pub as_: Vec<String>,
    /// Its secret may be one of the claim's `tokens.broad`, reaching every
    /// repo the account can (`login --broad`). Set when `login` creates
    /// it; changed only by editing the file, as the rest of the grant.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub broad: bool,
    /// The orgs a claim with an `orgs` field may reach, by slug: the
    /// account may be in more than a sandbox should touch. Set by
    /// `login --org` or by hand, as the rest of the grant.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub orgs: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub approved: Vec<Version>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub pending: Vec<Version>,
    /// Set by the broker ([`mark_broken`]), cleared by a login or a
    /// successful refresh. Refuses no call: it's for `access ls` (and, at
    /// credential-broker step 4, the approvers' notice).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub broken: Option<Broken>,
    /// RFC 3339, UTC: a pasted secret's expiry as the upstream reported it
    /// to the broker (the claim's `tokens.expiry` header); cleared by a
    /// login.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<String>,
    /// Asked scopes that go through without asking: *always* answers,
    /// and hand-written ones.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<Rule>,
}

/// An asked scope allowed for every call it matches: its `scope`, and each
/// field set equal to the call's.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Rule {
    pub scope: String,
    /// `owner/name`, any case.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repo: Option<String>,
    /// The instance's sandbox name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sandbox: Option<String>,
    /// The instance's profile.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub profile: Option<String>,
    /// The request whose *always* answer wrote it, for `access revoke`;
    /// hand-written rules have none.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
}

impl Rule {
    /// `repo`: the lowercase one a fenced claim's call names.
    pub fn matches(&self, scope: &str, repo: Option<&str>, sandbox: &str, profile: Option<&str>) -> bool {
        self.scope == scope
            && self.repo.as_deref().is_none_or(|r| repo.is_some_and(|c| r.eq_ignore_ascii_case(c)))
            && self.sandbox.as_deref().is_none_or(|s| s == sandbox)
            && self.profile.as_deref().is_none_or(|p| profile == Some(p))
    }
}

/// Why the broker stopped trusting the secret: a refused refresh, an
/// unreadable store, or a 401 on the claim's `check` route.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Broken {
    /// RFC 3339, UTC: when it was first marked.
    pub at: String,
    /// One line.
    pub reason: String,
}

impl Entry {
    pub fn new(kind: &str, profiles: Option<Vec<String>>) -> Entry {
        Entry {
            kind: kind.to_string(),
            source: Source::File,
            profiles,
            max: None,
            members: Vec::new(),
            approvers: Vec::new(),
            delegate: Vec::new(),
            as_: Vec::new(),
            broad: false,
            orgs: Vec::new(),
            approved: Vec::new(),
            pending: Vec::new(),
            broken: None,
            expires_at: None,
            rules: Vec::new(),
        }
    }

    /// The rule allowing `scope` for this call, if any.
    pub fn rule_for(&self, scope: &str, repo: Option<&str>, sandbox: &str, profile: Option<&str>) -> Option<&Rule> {
        self.rules.iter().find(|r| r.matches(scope, repo, sandbox, profile))
    }

    /// [`Entry::expires_at`] in unix seconds; checked at load.
    pub fn expires(&self) -> Option<u64> {
        self.expires_at.as_deref().and_then(crate::broker::lfs::parse_rfc3339)
    }

    /// The approved claim of `version`, canonical: what the broker enforces
    /// for an instance that ran with it.
    pub fn approved_claim(&self, version: &str) -> Option<&str> {
        self.approved.iter().find(|v| v.version == version).map(|v| v.claim.as_str())
    }

    pub fn latest_approved(&self) -> Option<&Version> {
        self.approved.last()
    }

    /// Drops it from `pending` too. Whether it was new.
    pub fn approve(&mut self, version: Version) -> bool {
        self.pending.retain(|p| p.version != version.version);
        if self.approved_claim(&version.version).is_some() {
            return false;
        }
        self.approved.push(version);
        true
    }

    fn check(&self, name: &str) -> Result<()> {
        super::check_name(name)?;
        super::claim::check_kind(&self.kind).context("`kind`")?;
        for p in self.profiles.iter().flatten() {
            crate::config::check_profile_name(p).context("`profiles`")?;
        }
        super::tokens::offered(&self.as_).context("`as`")?;
        if let Some(at) = &self.expires_at
            && self.expires().is_none()
        {
            bail!("`expires_at`: `{at}` isn't an RFC 3339 date-time");
        }
        for r in &self.rules {
            if let Some(id) = &r.id
                && !crate::broker::asks::is_request_id(id)
            {
                bail!("`rules`: `{id}` isn't a request id");
            }
            if let Some(p) = &r.profile {
                crate::config::check_profile_name(p).context("`rules`")?;
            }
        }
        for (field, versions) in [("approved", &self.approved), ("pending", &self.pending)] {
            for v in versions {
                v.check(&self.kind).with_context(|| format!("`{field}` version {}", v.short()))?;
            }
        }
        Ok(())
    }
}

/// Where the secret is. Only the file store so far.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub enum Source {
    /// `credentials::store`.
    #[default]
    File,
}

impl TryFrom<String> for Source {
    type Error = String;

    fn try_from(s: String) -> Result<Source, String> {
        match s.as_str() {
            "file" => Ok(Source::File),
            "keyring" => Err(format!("`source = \"keyring\"`: {} (only \"file\" for now)", crate::NOT_SUPPORTED_YET)),
            s if s.contains("://") => {
                Err(format!("`source = \"{s}\"`: external stores: {} (only \"file\" for now)", crate::NOT_SUPPORTED_YET))
            }
            s => Err(format!("unknown `source` `{s}` (only \"file\")")),
        }
    }
}

impl From<Source> for String {
    fn from(s: Source) -> String {
        match s {
            Source::File => "file".into(),
        }
    }
}

/// A claim version: its hash and the canonical pinned claim it hashes.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Version {
    pub version: String,
    pub claim: String,
}

impl Version {
    pub fn of(claim: &Claim) -> Version {
        Version { version: claim.version(), claim: claim.canonical() }
    }

    /// From canonical JSON a host sent, re-hashed here.
    pub fn from_canonical(claim: &str) -> Version {
        Version { version: hash(claim), claim: claim.to_string() }
    }

    /// Enough of the hash to tell versions apart on screen.
    pub fn short(&self) -> &str {
        &self.version[..self.version.len().min(12)]
    }

    pub fn view(&self) -> Result<View> {
        View::parse(&self.claim)
    }

    fn check(&self, kind: &str) -> Result<()> {
        if hash(&self.claim) != self.version {
            bail!("its claim doesn't hash to its version");
        }
        let view = self.view()?;
        if view.kind != kind {
            bail!("a `{}` claim, not `{kind}`", view.kind);
        }
        Ok(())
    }
}

/// [`Claim::version`]'s hash.
fn hash(canonical: &str) -> String {
    hex::encode(&Sha256::digest(canonical.as_bytes())[..])
}

/// Records `claim` (canonical) as pending for the credential `name`, unless
/// already approved or pending. Whether it was new.
pub fn record_pending(home: &mut Home, name: &str, claim: &str) -> Result<bool> {
    let entry = home.get_mut(name).with_context(|| format!("no credential `{name}`"))?;
    let version = Version::from_canonical(claim);
    version.check(&entry.kind)?;
    if entry.approved_claim(&version.version).is_some() || entry.pending.iter().any(|p| p.version == version.version) {
        return Ok(false);
    }
    entry.pending.push(version);
    Ok(true)
}

/// The credential an instance in `profile` takes for claim kind `kind`: the
/// one its config's `access.<kind>.credential` names (`named`), else the one
/// of that kind granted to `profile` (an entry without `profiles` is granted
/// to every profile, and to an instance without one). `Ok(None)`: none here.
/// Resolved per use, never kept: profiles and configs change.
pub fn resolve<'a>(
    home: &'a Home,
    kind: &str,
    named: Option<&str>,
    profile: Option<&str>,
) -> Result<Option<(&'a str, &'a Entry)>> {
    if let Some(name) = named {
        return match home.get_key_value(name) {
            None => Ok(None),
            Some((_, e)) if e.kind != kind => {
                bail!("`access.{kind}.credential` names `{name}`, a `{}` credential", e.kind)
            }
            Some((n, e)) => Ok(Some((n.as_str(), e))),
        };
    }
    let granted = |e: &Entry| match &e.profiles {
        None => true,
        Some(list) => profile.is_some_and(|p| list.iter().any(|l| l == p)),
    };
    let found: Vec<(&String, &Entry)> = home.iter().filter(|(_, e)| e.kind == kind && granted(e)).collect();
    match found[..] {
        [] => Ok(None),
        [(n, e)] => Ok(Some((n.as_str(), e))),
        _ => {
            let names: Vec<String> = found.iter().map(|(n, _)| format!("`{n}`")).collect();
            bail!(
                "credentials {} are all `{kind}` credentials granted here: name one in `access.{kind}.credential`",
                names.join(", ")
            )
        }
    }
}

/// `<data>/credentials.toml`.
pub fn path(data: &Path) -> PathBuf {
    data.join(crate::daemon_config::CREDENTIALS)
}

pub fn parse(text: &str) -> Result<Home> {
    let home: Home = toml::from_str(text)?;
    for (name, entry) in &home {
        entry.check(name).with_context(|| format!("credential `{name}`"))?;
    }
    Ok(home)
}

pub fn serialize(home: &Home) -> Result<String> {
    toml::to_string_pretty(home).context("cannot serialize credentials")
}

/// A missing file is no credentials.
pub fn load(path: &Path) -> Result<Home> {
    match std::fs::read_to_string(path) {
        Ok(text) => parse(&text).with_context(|| format!("invalid {}", path.display())),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Home::new()),
        Err(e) => Err(e).with_context(|| format!("cannot read {}", path.display())),
    }
}

/// Read-modify-write under the lock, saved only when `f` changed it; `f`'s
/// error leaves the file alone.
pub fn update<T>(path: &Path, f: impl FnOnce(&mut Home) -> Result<T>) -> Result<T> {
    crate::serve::locked_file::update(path, load, serialize, |home| {
        let before = home.clone();
        let out = f(home);
        if out.is_err() {
            *home = before;
        }
        out
    })?
}

/// Marks `name` broken unless it already is (the first reason stays until a
/// login). No such credential: nothing.
pub fn mark_broken(path: &Path, name: &str, broken: Broken) -> Result<()> {
    update(path, |h| {
        if let Some(e) = h.get_mut(name)
            && e.broken.is_none()
        {
            e.broken = Some(broken);
        }
        Ok(())
    })
}

/// Records `name`'s expiry (unix seconds). No such credential: nothing.
pub fn set_expires_at(path: &Path, name: &str, at: u64) -> Result<()> {
    update(path, |h| {
        if let Some(e) = h.get_mut(name) {
            e.expires_at = Some(crate::inbox::rfc3339(at));
        }
        Ok(())
    })
}

pub fn clear_broken(path: &Path, name: &str) -> Result<()> {
    update(path, |h| {
        if let Some(e) = h.get_mut(name) {
            e.broken = None;
        }
        Ok(())
    })
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate::credentials::claim;

    /// The intercept claim of `tests/fixtures/features/sentry`.
    pub(crate) fn sentry() -> Claim {
        fixture("sentry", "sentry")
    }

    /// The base-url claim of `tests/fixtures/features/sentry-cli`: listeners
    /// and rewrites, which no shipped claim uses.
    pub(crate) fn sentry_cli() -> Claim {
        fixture("sentry-cli", "sentry")
    }

    /// The repo-fenced intercept claim of `tests/fixtures/features/github`.
    pub(crate) fn github() -> Claim {
        fixture("github", "github")
    }

    fn fixture(dir: &str, kind: &str) -> Claim {
        let path = format!("{}/tests/fixtures/features/{dir}/devcontainer-feature.json", env!("CARGO_MANIFEST_DIR"));
        let v: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
        claim::parse(kind, &v["customizations"]["devsandbox"]["credentials"][kind]).unwrap()
    }

    #[test]
    fn round_trips_and_rehashes() {
        let mut entry = Entry::new("sentry", Some(vec!["work".into()]));
        entry.max = Some(vec!["releases.write".into()]);
        entry.approvers = vec!["laptop".into()];
        entry.as_ = vec!["user".into()];
        entry.broad = true;
        entry.orgs = vec!["acme".into()];
        assert!(entry.approve(Version::of(&sentry())));
        assert!(!entry.approve(Version::of(&sentry())), "approved once");
        let home = Home::from([("sentry-work".to_string(), entry)]);
        let text = serialize(&home).unwrap();
        assert!(text.starts_with("[sentry-work]\nkind = \"sentry\"\nsource = \"file\"\nprofiles = [\"work\"]\n"), "{text}");
        assert!(text.contains("as = [\"user\"]\nbroad = true\norgs = [\"acme\"]\n"), "{text}");
        let narrow = Home::from([("s".to_string(), Entry::new("sentry", None))]);
        let narrow_text = serialize(&narrow).unwrap();
        assert!(!narrow_text.contains("broad") && !narrow_text.contains("orgs"), "unset is left out");
        assert!(!text.contains("pending") && !text.contains("\nmembers"), "{text}");
        assert_eq!(parse(&text).unwrap(), home);
        let v = &home["sentry-work"].approved[0];
        assert_eq!(v.version, sentry().version());
        assert_eq!(home["sentry-work"].approved_claim(&v.version), Some(sentry().canonical().as_str()));

        let tampered = text.replace("de.sentry.io", "evil.example");
        let err = format!("{:#}", parse(&tampered).unwrap_err());
        assert!(err.contains("doesn't hash to its version"), "{err}");
        let other_kind = text.replace("kind = \"sentry\"", "kind = \"github\"");
        let err = format!("{:#}", parse(&other_kind).unwrap_err());
        assert!(err.contains("a `sentry` claim, not `github`"), "{err}");
    }

    #[test]
    fn fields_and_sources_are_checked() {
        assert_eq!(parse("").unwrap(), Home::new());
        let min = parse("[sentry-work]\nkind = \"sentry\"\n").unwrap();
        assert_eq!(min["sentry-work"], Entry::new("sentry", None));
        for (bad, wants) in [
            ("[x]\nkind = \"sentry\"\nsource = \"keyring\"\n", "keyring\"`: not supported yet!"),
            ("[x]\nkind = \"sentry\"\nsource = \"op://Private/x\"\n", "external stores: not supported yet!"),
            ("[x]\nkind = \"sentry\"\nsource = \"vault\"\n", "unknown `source`"),
            ("[x]\nkind = \"sentry\"\nbogus = 1\n", "bogus"),
            ("[x]\nsource = \"file\"\n", "kind"),
            ("[x]\nkind = \"Sentry\"\n", "the kind must be"),
            ("[X]\nkind = \"sentry\"\n", "credential name"),
            ("[x]\nkind = \"sentry\"\nprofiles = [\"Work\"]\n", "profile name"),
            ("[x]\nkind = \"github\"\nas = [\"bot\"]\n", "`as`"),
            ("[x]\nkind = \"github\"\nexpires_at = \"soon\"\n", "`expires_at`"),
        ] {
            let err = format!("{:#}", parse(bad).unwrap_err());
            assert!(err.contains(wants), "{bad}: {err}");
        }
    }

    #[test]
    fn rules_round_trip_and_match_each_field_they_set() {
        let text = "[gh]\nkind = \"github\"\nsource = \"file\"\n\n[[gh.rules]]\nscope = \"pr.write\"\nrepo = \"O/R\"\nsandbox = \"web\"\nid = \"R-0123456789AB\"\n\n[[gh.rules]]\nscope = \"issues.read\"\nprofile = \"work\"\n";
        let home = parse(text).unwrap();
        assert_eq!(serialize(&home).unwrap(), text);
        let e = &home["gh"];
        assert_eq!(e.rule_for("pr.write", Some("o/r"), "web", None).and_then(|r| r.id.as_deref()), Some("R-0123456789AB"));
        assert!(e.rule_for("pr.write", None, "web", None).is_none(), "a rule naming a repo needs the call's");
        assert!(e.rule_for("pr.write", Some("o/s"), "web", None).is_none());
        assert!(e.rule_for("pr.write", Some("o/r"), "api", None).is_none());
        assert!(e.rule_for("pr.read", Some("o/r"), "web", None).is_none());
        assert!(e.rule_for("issues.read", Some("any/repo"), "any", Some("work")).is_some());
        assert!(e.rule_for("issues.read", None, "any", Some("home")).is_none());
        assert!(e.rule_for("issues.read", None, "any", None).is_none());
        for (bad, wants) in [
            ("[gh]\nkind = \"github\"\n[[gh.rules]]\nscope = \"x\"\nid = \"gh\"\n", "isn't a request id"),
            ("[gh]\nkind = \"github\"\n[[gh.rules]]\nscope = \"x\"\nprofile = \"Work\"\n", "profile name"),
            ("[gh]\nkind = \"github\"\n[[gh.rules]]\nscope = \"x\"\nmember = \"m\"\n", "member"),
        ] {
            let err = format!("{:#}", parse(bad).unwrap_err());
            assert!(err.contains(wants), "{bad}: {err}");
        }
    }

    #[test]
    fn pending_versions_are_recorded_once_and_move_on_approval() {
        let mut home = Home::from([("s".to_string(), Entry::new("sentry", None))]);
        let canonical = sentry().canonical();
        assert!(record_pending(&mut home, "nope", &canonical).is_err());
        assert!(record_pending(&mut home, "s", &canonical).unwrap());
        assert!(!record_pending(&mut home, "s", &canonical).unwrap());
        assert!(record_pending(&mut home, "s", "{\"kind\":\"github\",\"claim\":{}}").is_err());
        let v = home["s"].pending[0].clone();
        assert_eq!(v.version, sentry().version());
        let entry = home.get_mut("s").unwrap();
        assert!(entry.approve(v));
        assert!(entry.pending.is_empty());
        assert!(!record_pending(&mut home, "s", &canonical).unwrap(), "already approved");
    }

    #[test]
    fn resolve_takes_the_named_entry_else_the_one_granted() {
        let mut work = Entry::new("sentry", Some(vec!["work".into()]));
        work.approve(Version::of(&sentry()));
        let home = Home::from([
            ("sentry-work".to_string(), work),
            ("sentry-oss".to_string(), Entry::new("sentry", Some(vec!["oss".into()]))),
            ("gh".to_string(), Entry::new("github", None)),
        ]);
        let name = |r: Result<Option<(&str, &Entry)>>| r.unwrap().map(|(n, _)| n.to_string());
        assert_eq!(name(resolve(&home, "sentry", None, Some("work"))), Some("sentry-work".into()));
        assert_eq!(name(resolve(&home, "sentry", None, Some("home"))), None);
        assert_eq!(name(resolve(&home, "sentry", None, None)), None);
        assert_eq!(name(resolve(&home, "github", None, None)), Some("gh".into()));
        assert_eq!(name(resolve(&home, "sentry", Some("sentry-oss"), Some("work"))), Some("sentry-oss".into()));
        assert_eq!(name(resolve(&home, "sentry", Some("nope"), None)), None);
        let err = resolve(&home, "sentry", Some("gh"), None).unwrap_err().to_string();
        assert!(err.contains("a `github` credential"), "{err}");

        let mut both = home.clone();
        both.get_mut("sentry-oss").unwrap().profiles = None;
        let err = resolve(&both, "sentry", None, Some("work")).unwrap_err().to_string();
        assert!(err.contains("`sentry-oss`, `sentry-work`") && err.contains("access.sentry.credential"), "{err}");
    }

    #[test]
    fn broken_is_marked_once_and_cleared() {
        let dir = std::env::temp_dir().join(format!("dsv-cred-home-broken-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        let p = path(&dir);
        update(&p, |h| Ok(h.insert("s".into(), Entry::new("sentry", None)))).unwrap();
        let broken = |reason: &str| Broken { at: "2026-11-21T10:00:00Z".into(), reason: reason.into() };
        mark_broken(&p, "s", broken("refused")).unwrap();
        mark_broken(&p, "s", broken("later")).unwrap();
        mark_broken(&p, "nope", broken("x")).unwrap();
        let text = std::fs::read_to_string(&p).unwrap();
        assert!(text.contains("[s.broken]\nat = \"2026-11-21T10:00:00Z\"\nreason = \"refused\"\n"), "{text}");
        assert_eq!(load(&p).unwrap()["s"].broken, Some(broken("refused")));
        clear_broken(&p, "s").unwrap();
        assert_eq!(load(&p).unwrap()["s"].broken, None);
        set_expires_at(&p, "s", 1_800_000_000).unwrap();
        let text = std::fs::read_to_string(&p).unwrap();
        assert!(text.contains("expires_at = \"2027-01-15T08:00:00Z\""), "{text}");
        assert_eq!(load(&p).unwrap()["s"].expires(), Some(1_800_000_000));
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn update_is_locked_and_atomic() {
        let dir = std::env::temp_dir().join(format!("dsv-cred-home-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        let p = path(&dir);
        update(&p, |h| Ok(h.insert("s".into(), Entry::new("sentry", None)))).unwrap();
        assert!(update(&p, |h| -> Result<()> {
            h.clear();
            bail!("no")
        })
        .is_err());
        assert_eq!(load(&p).unwrap().len(), 1, "a failed update leaves the file");
        assert!(dir.join("credentials.lock").exists());
        let _ = std::fs::remove_dir_all(&dir);
    }
}