silicon-iam-cli 1.2.0

Command-line client for Silicon IAM, built on the silicon-iam-client crate.
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! What the CLI remembers between invocations.
//!
//! The client crate is stateless by design, so everything durable lives here:
//! which service to talk to, which profile is current, and the tokens for
//! each. All of it sits under `~/.silicon-iam/`, and the file holding tokens is
//! created `0600` and re-checked on every write, because a credential readable
//! by other users on the machine is not a credential.

use std::{
    collections::BTreeMap,
    fs::{self, File},
    io::{Read as _, Write as _},
    path::{Path, PathBuf},
};

use serde::{Deserialize, Serialize};
use sha2::{Digest as _, Sha256};
use time::OffsetDateTime;
use uuid::Uuid;

use crate::error::{CliError, Result};

#[cfg(not(unix))]
use std::fs::OpenOptions;

const APP_DIRECTORY: &str = ".silicon-iam";
const CONFIG_FILE: &str = "config.json";
const CREDENTIALS_FILE: &str = "credentials.json";
const UPDATE_FILE: &str = "update.json";
const STORE_LOCK: &str = "credentials.lock";

/// Settings that are not secret.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
    /// Whether the installed CLI maintains itself from crates.io.
    #[serde(default = "enabled")]
    pub auto_update: bool,
    /// Profile used when `--profile` is not given.
    #[serde(default)]
    pub current_profile: Option<String>,
    /// Per-profile settings.
    #[serde(default)]
    pub profiles: BTreeMap<String, Profile>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            auto_update: true,
            current_profile: None,
            profiles: BTreeMap::new(),
        }
    }
}

/// Non-secret throttle state for the crates.io updater.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct UpdateState {
    /// Compiled version for which the last check was attempted.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub checked_version: Option<String>,
    /// Time of the last check attempt, including a registry/install failure.
    #[serde(default, with = "time::serde::rfc3339::option")]
    pub checked_at: Option<OffsetDateTime>,
}

/// One named service and the defaults that go with it.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Profile {
    /// Base URL of the service.
    pub url: String,
    /// Organization assumed when a command does not name one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub org: Option<String>,
    /// Organization defaults inside isolated testing environments.
    ///
    /// Production and every testing plane have independent data, so carrying
    /// a production handle into a test plane only manufactures misleading
    /// not-found responses.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub test_orgs: BTreeMap<Uuid, String>,
}

/// Tokens, kept apart from the settings so the secret file can be locked down
/// on its own and pointed at in a bug report without it.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Credentials {
    /// Production sessions, keyed by profile.
    #[serde(default)]
    pub sessions: BTreeMap<String, Session>,
    /// Testing-environment sessions, partitioned by profile and environment.
    #[serde(default)]
    pub test_sessions: BTreeMap<String, BTreeMap<Uuid, Session>>,
    /// Testing root keys, partitioned by profile and addressed by public UUID.
    #[serde(default)]
    pub testing_environment_keys: BTreeMap<String, BTreeMap<Uuid, String>>,
}

impl Credentials {
    /// Finds the session for exactly one production or testing scope.
    pub fn session(&self, profile: &str, environment_id: Option<Uuid>) -> Option<&Session> {
        match environment_id {
            Some(id) => self.test_sessions.get(profile)?.get(&id),
            None => self.sessions.get(profile),
        }
    }

    /// Replaces the session for exactly one production or testing scope.
    pub fn set_session(&mut self, profile: &str, environment_id: Option<Uuid>, session: Session) {
        match environment_id {
            Some(id) => {
                self.test_sessions
                    .entry(profile.to_owned())
                    .or_default()
                    .insert(id, session);
            }
            None => {
                self.sessions.insert(profile.to_owned(), session);
            }
        }
    }

    /// Removes the session for exactly one production or testing scope.
    pub fn remove_session(&mut self, profile: &str, environment_id: Option<Uuid>) -> bool {
        match environment_id {
            Some(id) => self
                .test_sessions
                .get_mut(profile)
                .and_then(|sessions| sessions.remove(&id))
                .is_some(),
            None => self.sessions.remove(profile).is_some(),
        }
    }

    /// Finds the secret root key behind a public environment id.
    pub fn testing_environment_key(&self, profile: &str, environment_id: Uuid) -> Option<&str> {
        self.testing_environment_keys
            .get(profile)?
            .get(&environment_id)
            .map(String::as_str)
    }

    /// Stores or replaces the root key behind a public environment id.
    pub fn set_testing_environment_key(
        &mut self,
        profile: &str,
        environment_id: Uuid,
        key: String,
    ) {
        self.testing_environment_keys
            .entry(profile.to_owned())
            .or_default()
            .insert(environment_id, key);
    }
}

/// The actor represented by a stored session.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SessionActor {
    /// A person signing in through a verified contact challenge.
    #[default]
    Carbon,
    /// A service identity signing in with its Silicon credential.
    Silicon,
}

/// A remote logout that may have committed even if its response was lost.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PendingLogoutMode {
    /// Revoke only the session represented by the stored credential.
    CurrentSession,
    /// Revoke every session owned by the Carbon.
    AllSessions,
}

/// Durable retry identity for a remote logout.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PendingLogout {
    /// Scope bound into the original logout request.
    pub mode: PendingLogoutMode,
    /// Idempotency key reserved before the request was sent.
    pub idempotency_key: String,
}

/// One signed-in session.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Session {
    /// Short-lived credential presented on each request.
    pub access_token: String,
    /// Rotating credential used to obtain the next access token.
    pub refresh_token: String,
    /// When the access token stops being accepted.
    #[serde(with = "time::serde::rfc3339")]
    pub expires_at: OffsetDateTime,
    /// Which kind of principal owns the tokens.
    #[serde(default)]
    pub actor_type: SessionActor,
    /// Public Carbon or Silicon identifier for display and refresh continuity.
    #[serde(alias = "carbon_id")]
    pub actor_id: String,
    /// Key reserved before a rotating refresh request is sent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pending_refresh_key: Option<String>,
    /// Request identity retained until remote logout is confirmed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pending_logout: Option<PendingLogout>,
}

impl Session {
    /// Whether the access token is close enough to expiry to renew now.
    ///
    /// Renews a minute early: a token that expires while a request is in
    /// flight fails the request, and the margin costs nothing.
    #[must_use]
    pub fn needs_refresh(&self) -> bool {
        self.expires_at <= OffsetDateTime::now_utc() + time::Duration::minutes(1)
    }
}

/// The directory everything is stored under.
///
/// # Errors
///
/// Returns an error when the home directory cannot be determined.
pub fn home() -> Result<PathBuf> {
    let home = std::env::var_os("SILICON_IAM_HOME")
        .map(PathBuf::from)
        .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(APP_DIRECTORY)))
        .ok_or_else(|| {
            CliError::Config(
                "cannot locate a home directory; set SILICON_IAM_HOME to choose one".to_owned(),
            )
        })?;
    Ok(home)
}

/// Reads the settings, or an empty set when none have been written yet.
///
/// # Errors
///
/// Returns an error when the file exists but cannot be read or parsed.
pub fn load_config() -> Result<Config> {
    StoreDirectory::open()?.read_json(CONFIG_FILE)
}

/// Holds the shared read/modify/write lock for local state.
///
/// The lock file is permanent: unlinking it would let concurrent processes
/// acquire locks on different inodes. Dropping this value releases the lock.
pub struct LockedStore {
    directory: StoreDirectory,
    _lock: File,
}

/// Locks local state before reading a snapshot that will be modified.
///
/// # Errors
///
/// Returns an error for unsafe paths or unreadable/unlockable local state.
pub fn lock() -> Result<LockedStore> {
    let directory = StoreDirectory::open()?;
    let lock = directory.lock(STORE_LOCK)?;
    Ok(LockedStore {
        directory,
        _lock: lock,
    })
}

impl LockedStore {
    /// Reads settings while excluding other state writers.
    ///
    /// # Errors
    ///
    /// Returns an error when the settings are unsafe, unreadable or invalid.
    pub fn load_config(&self) -> Result<Config> {
        self.directory.read_json(CONFIG_FILE)
    }

    /// Atomically replaces settings while retaining the state lock.
    ///
    /// # Errors
    ///
    /// Returns an error when the settings cannot be safely persisted.
    pub fn save_config(&self, config: &Config) -> Result<()> {
        self.directory.write_json(CONFIG_FILE, config)
    }

    fn load_credentials(&self) -> Result<Credentials> {
        self.directory.read_json(CREDENTIALS_FILE)
    }

    fn save_credentials(&self, credentials: &Credentials) -> Result<()> {
        self.directory.write_json(CREDENTIALS_FILE, credentials)
    }
}

/// Reads automatic-update throttle state, or an empty value.
///
/// # Errors
///
/// Returns an error when the state exists but cannot be read or parsed.
pub fn load_update_state() -> Result<UpdateState> {
    StoreDirectory::open()?.read_json(UPDATE_FILE)
}

/// Stores automatic-update throttle state without any credentials.
///
/// # Errors
///
/// Returns an error when the state cannot be written.
pub fn save_update_state(state: &UpdateState) -> Result<()> {
    let store = lock()?;
    let previous: UpdateState = store.directory.read_json(UPDATE_FILE)?;
    // A slower concurrent check must not move the last-attempt clock
    // backwards. A future value, however, must be repairable after clock skew.
    if previous.checked_at > state.checked_at
        && previous.checked_at <= Some(OffsetDateTime::now_utc())
    {
        return Ok(());
    }
    store.directory.write_json(UPDATE_FILE, state)
}

/// Attempts to serialize an updater check and installation for this home.
///
/// # Errors
///
/// Returns an error for an unsafe home/lock path or an operating-system failure.
pub fn try_lock_updater_check() -> Result<Option<File>> {
    StoreDirectory::open()?.try_lock("updater-check.lock")
}

/// Reads stored sessions, or an empty set.
///
/// # Errors
///
/// Returns an error when the file exists but cannot be read or parsed.
pub fn load_credentials() -> Result<Credentials> {
    StoreDirectory::open()?.read_json(CREDENTIALS_FILE)
}

/// Serializes one profile/environment's complete session transitions.
///
/// Unlike the short state-file lock, this lock can span a network refresh or
/// logout. Independent sessions continue working, and login/logout cannot race
/// a refresh commit for the same session.
pub struct LockedSession {
    directory: StoreDirectory,
    _lock: File,
    profile: String,
    environment_id: Option<Uuid>,
}

/// Acquires the session lock before re-reading its current credentials.
///
/// # Errors
///
/// Returns an error for an unsafe home, or an unsafe/unlockable lock file.
pub fn lock_session(profile: &str, environment_id: Option<Uuid>) -> Result<LockedSession> {
    let directory = StoreDirectory::open()?;
    let mut digest = Sha256::new();
    digest.update(profile.as_bytes());
    digest.update([0]);
    if let Some(id) = environment_id {
        digest.update(id.as_bytes());
    }
    let name = format!("session-{:x}.lock", digest.finalize());
    let lock = directory.lock(&name)?;
    Ok(LockedSession {
        directory,
        _lock: lock,
        profile: profile.to_owned(),
        environment_id,
    })
}

impl LockedSession {
    /// Reads the latest session after its transition lock has been acquired.
    ///
    /// # Errors
    ///
    /// Returns an error for unsafe or unreadable state, or when signed out.
    pub fn session(&self) -> Result<Session> {
        let credentials: Credentials = self.directory.read_json(CREDENTIALS_FILE)?;
        credentials
            .session(&self.profile, self.environment_id)
            .cloned()
            .ok_or(CliError::NotSignedIn)
    }

    /// Merges this session into the latest document, preserving other sessions.
    ///
    /// # Errors
    ///
    /// Returns an error when state cannot be safely locked, read or persisted.
    pub fn remember(&self, session: Session) -> Result<()> {
        let _lock = self.directory.lock(STORE_LOCK)?;
        let mut credentials: Credentials = self.directory.read_json(CREDENTIALS_FILE)?;
        credentials.set_session(&self.profile, self.environment_id, session);
        self.directory.write_json(CREDENTIALS_FILE, &credentials)
    }

    /// Removes only this session from the latest document.
    ///
    /// # Errors
    ///
    /// Returns an error when state cannot be safely locked, read or persisted.
    pub fn forget(&self) -> Result<bool> {
        let _lock = self.directory.lock(STORE_LOCK)?;
        let mut credentials: Credentials = self.directory.read_json(CREDENTIALS_FILE)?;
        let existed = credentials.remove_session(&self.profile, self.environment_id);
        self.directory.write_json(CREDENTIALS_FILE, &credentials)?;
        Ok(existed)
    }
}

/// Merges an environment key into the latest credential document.
///
/// # Errors
///
/// Returns an error when state cannot be safely locked, read or persisted.
pub fn remember_testing_environment(
    profile: &str,
    environment_id: Uuid,
    key: String,
) -> Result<()> {
    let store = lock()?;
    let mut credentials = store.load_credentials()?;
    credentials.set_testing_environment_key(profile, environment_id, key);
    store.save_credentials(&credentials)
}

struct StoreDirectory {
    path: PathBuf,
    // Pin the verified directory so renaming its path cannot redirect writes.
    #[cfg(unix)]
    file: File,
}

impl StoreDirectory {
    fn open() -> Result<Self> {
        let path = home()?;
        let mut builder = fs::DirBuilder::new();
        builder.recursive(true);
        #[cfg(unix)]
        {
            use std::os::unix::fs::DirBuilderExt as _;
            builder.mode(0o700);
        }
        builder
            .create(&path)
            .map_err(|error| state_error(&path, error))?;
        #[cfg(unix)]
        {
            use rustix::fs::{Mode, OFlags, open};
            use std::os::unix::fs::MetadataExt as _;

            let file = File::from(
                open(
                    &path,
                    OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
                    Mode::empty(),
                )
                .map_err(|error| state_error(&path, error))?,
            );
            let metadata = file.metadata().map_err(|error| state_error(&path, error))?;
            if metadata.uid() != rustix::process::geteuid().as_raw() || metadata.mode() & 0o077 != 0
            {
                return Err(CliError::Config(format!(
                    "{} must be owned by the current user and private (0700); use chmod 700 on your IAM home",
                    path.display()
                )));
            }
            Ok(Self { path, file })
        }
        #[cfg(not(unix))]
        {
            let metadata =
                fs::symlink_metadata(&path).map_err(|error| state_error(&path, error))?;
            if metadata.file_type().is_symlink() || !metadata.is_dir() {
                return Err(state_error(
                    &path,
                    "the IAM home must be a real directory, not a link",
                ));
            }
            Ok(Self { path })
        }
    }

    fn open_file(&self, name: &str, create: bool, exclusive: bool) -> std::io::Result<File> {
        #[cfg(unix)]
        {
            use rustix::fs::{Mode, OFlags, openat};

            let mut flags = OFlags::NOFOLLOW | OFlags::CLOEXEC | OFlags::NONBLOCK;
            flags |= if create {
                OFlags::RDWR | OFlags::CREATE
            } else {
                OFlags::RDONLY
            };
            if exclusive {
                flags |= OFlags::EXCL;
            }
            Ok(File::from(openat(
                &self.file,
                name,
                flags,
                Mode::from_raw_mode(0o600),
            )?))
        }
        #[cfg(not(unix))]
        {
            let path = self.path.join(name);
            match fs::symlink_metadata(&path) {
                Ok(metadata) if metadata.file_type().is_symlink() => {
                    return Err(std::io::Error::other(
                        "symbolic links are not allowed in IAM state",
                    ));
                }
                Err(error) if error.kind() != std::io::ErrorKind::NotFound => return Err(error),
                _ => {}
            }
            let mut options = OpenOptions::new();
            options
                .read(true)
                .write(create)
                .create(create)
                .create_new(exclusive);
            #[cfg(windows)]
            {
                use std::os::windows::fs::OpenOptionsExt as _;
                // Open the reparse point itself; never follow a swapped link.
                options.custom_flags(0x0020_0000);
            }
            options.open(path)
        }
    }

    fn validate_file(&self, name: &str, file: &File) -> Result<()> {
        let path = self.path.join(name);
        let metadata = file.metadata().map_err(|error| state_error(&path, error))?;
        if !metadata.is_file() {
            return Err(state_error(
                &path,
                "IAM state must be a regular file, not a link or device",
            ));
        }
        #[cfg(unix)]
        {
            use std::os::unix::fs::MetadataExt as _;
            if metadata.uid() != rustix::process::geteuid().as_raw()
                || metadata.mode() & 0o022 != 0
                || metadata.nlink() > 1
            {
                return Err(state_error(
                    &path,
                    "IAM state must be owned by the current user, not writable by others, and not hard-linked",
                ));
            }
        }
        Ok(())
    }

    fn lock(&self, name: &str) -> Result<File> {
        let file = self.open_file(name, true, false).map_err(|error| {
            state_error(&self.path.join(name), format!("cannot open lock: {error}"))
        })?;
        self.validate_file(name, &file)?;
        file.lock().map_err(|error| {
            state_error(
                &self.path.join(name),
                format!("cannot acquire lock: {error}"),
            )
        })?;
        Ok(file)
    }

    fn try_lock(&self, name: &str) -> Result<Option<File>> {
        let file = self
            .open_file(name, true, false)
            .map_err(|error| state_error(&self.path.join(name), error))?;
        self.validate_file(name, &file)?;
        match file.try_lock() {
            Ok(()) => Ok(Some(file)),
            Err(std::fs::TryLockError::WouldBlock) => Ok(None),
            Err(error) => Err(state_error(&self.path.join(name), error)),
        }
    }

    fn read_json<T: Default + serde::de::DeserializeOwned>(&self, name: &str) -> Result<T> {
        let mut file = match self.open_file(name, false, false) {
            Ok(file) => file,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(T::default()),
            Err(error) => return Err(state_error(&self.path.join(name), error)),
        };
        self.validate_file(name, &file)?;
        let mut bytes = Vec::new();
        file.read_to_end(&mut bytes)
            .map_err(|error| state_error(&self.path.join(name), error))?;
        serde_json::from_slice(&bytes).map_err(|error| state_error(&self.path.join(name), error))
    }

    fn write_json<T: Serialize>(&self, name: &str, value: &T) -> Result<()> {
        // Explicitly reject an existing symlink instead of replacing it. All
        // operations below are relative to the same private, pinned directory.
        match self.open_file(name, false, false) {
            Ok(file) => self.validate_file(name, &file)?,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
            Err(error) => return Err(state_error(&self.path.join(name), error)),
        }
        let temporary = format!(".{name}.{}.tmp", Uuid::now_v7());
        self.write_temporary(name, &temporary, value)
    }

    fn write_temporary<T: Serialize>(&self, name: &str, temporary: &str, value: &T) -> Result<()> {
        let path = self.path.join(name);
        let mut encoded =
            serde_json::to_vec_pretty(value).map_err(|error| state_error(&path, error))?;
        encoded.push(b'\n');
        let mut file = self
            .open_file(temporary, true, true)
            .map_err(|error| state_error(&path, error))?;
        // Only clean up after exclusive creation succeeded: a name collision
        // must never remove a file that this operation did not create.
        let result = (|| {
            file.write_all(&encoded)
                .map_err(|error| state_error(&path, error))?;
            file.sync_all().map_err(|error| state_error(&path, error))?;
            drop(file);
            #[cfg(unix)]
            {
                rustix::fs::renameat(&self.file, temporary, &self.file, name)
                    .map_err(|error| state_error(&path, error))?;
                self.file
                    .sync_all()
                    .map_err(|error| state_error(&path, error))?;
            }
            #[cfg(not(unix))]
            fs::rename(self.path.join(temporary), &path)
                .map_err(|error| state_error(&path, error))?;
            Ok(())
        })();
        if result.is_err() {
            #[cfg(unix)]
            let _ = rustix::fs::unlinkat(&self.file, temporary, rustix::fs::AtFlags::empty());
            #[cfg(not(unix))]
            let _ = fs::remove_file(self.path.join(temporary));
        }
        result
    }
}

fn state_error(path: &Path, error: impl std::fmt::Display) -> CliError {
    CliError::Config(format!("cannot access {}: {error}", path.display()))
}

const fn enabled() -> bool {
    true
}

#[cfg(test)]
mod tests {
    use time::{Duration, OffsetDateTime};
    use uuid::Uuid;

    use super::{Config, Credentials, Profile, Session, SessionActor};

    #[test]
    fn automatic_updates_default_on_for_new_and_old_configs() {
        assert!(Config::default().auto_update);
        let decoded = serde_json::from_str::<Config>("{}");
        assert!(decoded.is_ok_and(|config| config.auto_update));
    }

    #[test]
    fn old_profiles_load_without_test_environment_defaults() {
        let decoded =
            serde_json::from_str::<Profile>(r#"{"url":"https://example.test","org":"production"}"#);
        assert!(decoded.is_ok_and(|profile| profile.test_orgs.is_empty()));
    }

    fn session(expires_in: Duration) -> Session {
        Session {
            access_token: "cat_x".to_owned(),
            refresh_token: "rft_x".to_owned(),
            expires_at: OffsetDateTime::now_utc() + expires_in,
            actor_type: SessionActor::Carbon,
            actor_id: "founder".to_owned(),
            pending_refresh_key: None,
            pending_logout: None,
        }
    }

    #[test]
    fn old_carbon_sessions_keep_their_identity() {
        let decoded = serde_json::from_str::<Credentials>(
            r#"{
                "sessions": {
                    "default": {
                        "access_token":"cat_x",
                        "refresh_token":"rft_x",
                        "expires_at":"2026-09-04T00:00:00Z",
                        "carbon_id":"founder"
                    }
                }
            }"#,
        );
        let Ok(credentials) = decoded else {
            panic!("the old credentials-file shape must remain readable");
        };
        let Some(session) = credentials.session("default", None) else {
            panic!("the old Carbon session must remain present");
        };
        assert_eq!(session.actor_type, SessionActor::Carbon);
        assert_eq!(session.actor_id, "founder");
    }

    #[test]
    fn silicon_sessions_round_trip_with_their_actor_kind() {
        let mut credentials = Credentials::default();
        let mut silicon = session(Duration::minutes(30));
        silicon.access_token = "sat_x".to_owned();
        silicon.actor_type = SessionActor::Silicon;
        silicon.actor_id = "builder:tos".to_owned();
        credentials.set_session("default", None, silicon);

        let encoded = serde_json::to_vec(&credentials);
        let Ok(encoded) = encoded else {
            panic!("credentials must serialize");
        };
        let decoded = serde_json::from_slice::<Credentials>(&encoded);
        let Ok(decoded) = decoded else {
            panic!("credentials must deserialize");
        };
        let Some(session) = decoded.session("default", None) else {
            panic!("the Silicon session must remain present");
        };
        assert_eq!(session.actor_type, SessionActor::Silicon);
        assert_eq!(session.actor_id, "builder:tos");
        assert_eq!(session.access_token, "sat_x");
    }

    #[test]
    fn a_session_is_renewed_before_it_actually_expires() {
        assert!(!session(Duration::minutes(30)).needs_refresh());
        // Inside the margin: renewing now avoids failing a request that is
        // about to be sent.
        assert!(session(Duration::seconds(30)).needs_refresh());
        assert!(session(Duration::seconds(-1)).needs_refresh());
    }

    #[test]
    fn production_and_each_test_environment_have_independent_sessions() {
        let first_id = Uuid::from_u128(1);
        let second_id = Uuid::from_u128(2);
        let mut credentials = Credentials::default();
        credentials.set_session("default", None, session(Duration::minutes(5)));
        let mut first = session(Duration::minutes(6));
        first.actor_id = "first-test".to_owned();
        credentials.set_session("default", Some(first_id), first);
        let mut second = session(Duration::minutes(7));
        second.actor_id = "second-test".to_owned();
        credentials.set_session("default", Some(second_id), second);

        assert_eq!(
            credentials
                .session("default", None)
                .map(|value| value.actor_id.as_str()),
            Some("founder")
        );
        assert_eq!(
            credentials
                .session("default", Some(first_id))
                .map(|value| value.actor_id.as_str()),
            Some("first-test")
        );
        assert_eq!(
            credentials
                .session("default", Some(second_id))
                .map(|value| value.actor_id.as_str()),
            Some("second-test")
        );
    }

    #[test]
    fn environment_keys_are_looked_up_by_public_id_and_profile() {
        let id = Uuid::from_u128(7);
        let mut credentials = Credentials::default();
        credentials.set_testing_environment_key("work", id, "a".repeat(32));

        assert_eq!(
            credentials.testing_environment_key("work", id),
            Some("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
        );
        assert_eq!(credentials.testing_environment_key("other", id), None);
    }
}