harn-vm 0.10.162

Async bytecode virtual machine for the Harn programming language
Documentation
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;

use async_trait::async_trait;
use keyring_core::{CredentialStore, Entry, Error as KeyringError};

use super::{
    emit_secret_access_event, ensure_scoped_secret_access_allowed, RotationHandle, SecretBytes,
    SecretDeleteRequest, SecretError, SecretId, SecretMeta, SecretProvider,
};

static PLATFORM_STORE: OnceLock<Arc<CredentialStore>> = OnceLock::new();

#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum NativeKeyringError {
    #[error(transparent)]
    Keyring(#[from] KeyringError),
    #[error("credential contains invalid UTF-8: {0}")]
    Utf8(#[from] std::string::FromUtf8Error),
    #[error("credential store verification failed: {0}")]
    Verification(&'static str),
    #[error(
        "credential store did not answer within {}ms; a locked collection waiting on an \
         interactive unlock never returns on a headless host",
        .timeout.as_millis()
    )]
    Unresponsive { timeout: Duration },
    #[error(
        "credential store is locked, and this process does not raise unlock prompts; unlock \
         it, or set {SECRET_INTERACTIVE_ENV}=1 when a person can answer one"
    )]
    Locked,
}

/// What a non-interactive probe learned about the credential store.
///
/// A store that cannot be reached at all is an error, classified by
/// [`NativeKeyringError::unavailable_reason`]. These are the two states of a
/// store that answered.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NativeKeyringAvailability {
    /// The store can serve a credential without raising a prompt.
    Available,
    /// The store is present, but serving a credential needs an unlock prompt.
    Locked,
}

/// A stable reason that the operating-system credential store cannot service
/// requests in the current process. Callers may use this to distinguish an
/// unavailable desktop session from an operational keyring failure.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NativeKeyringUnavailable {
    /// No platform adapter was linked into this product build.
    AdapterMissing,
    /// The platform store exists but cannot be accessed by this process.
    StorageInaccessible,
    /// The operation requires desktop interaction that this process cannot show.
    InteractionRequired,
}

impl NativeKeyringUnavailable {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::AdapterMissing => "adapter_missing",
            Self::StorageInaccessible => "storage_inaccessible",
            Self::InteractionRequired => "interaction_required",
        }
    }
}

impl fmt::Display for NativeKeyringUnavailable {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

impl NativeKeyringError {
    /// Classify errors that mean the native store is unavailable to this
    /// process. Other platform failures remain operational errors.
    pub fn unavailable_reason(&self) -> Option<NativeKeyringUnavailable> {
        match self {
            Self::Keyring(KeyringError::NoDefaultStore) => {
                Some(NativeKeyringUnavailable::AdapterMissing)
            }
            Self::Keyring(KeyringError::NoStorageAccess(_)) => {
                Some(NativeKeyringUnavailable::StorageInaccessible)
            }
            #[cfg(all(feature = "native-keyring", target_os = "macos"))]
            Self::Keyring(KeyringError::PlatformFailure(error))
                if error
                    .downcast_ref::<security_framework::base::Error>()
                    .is_some_and(|error| {
                        // errSecInteractionNotAllowed: common for SSH/headless
                        // agents whose login keychain cannot present an unlock
                        // prompt. errSecAuthFailed is what an item's access
                        // control answers once this process has turned
                        // dialogs off (see `keychain_interaction_allowed`):
                        // the approval dialog it would have shown, refused.
                        error.code() == -25308
                            || (error.code() == -25293 && !keychain_interaction_allowed())
                    }) =>
            {
                Some(NativeKeyringUnavailable::InteractionRequired)
            }
            // A store that never answers is the same fact the macOS arm
            // reports as an error code: the platform wants a human at a
            // prompt this process cannot present.
            Self::Unresponsive { .. } | Self::Locked => {
                Some(NativeKeyringUnavailable::InteractionRequired)
            }
            _ => None,
        }
    }
}

/// Cross-platform access to the operating system's native credential store.
///
/// The keyring ecosystem owns the platform mappings and secure-storage API
/// calls. Harn only supplies the stable `(service, user)` namespace used by its
/// runtime and host capability.
pub struct NativeKeyring {
    service: String,
    entries: Mutex<HashMap<String, Arc<Entry>>>,
    store: Option<Arc<CredentialStore>>,
}

impl fmt::Debug for NativeKeyring {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("NativeKeyring")
            .field("service", &self.service)
            .finish_non_exhaustive()
    }
}

impl NativeKeyring {
    pub fn new(service: impl Into<String>) -> Self {
        Self {
            service: service.into(),
            entries: Mutex::new(HashMap::new()),
            store: None,
        }
    }

    #[cfg(test)]
    fn with_store(service: impl Into<String>, store: Arc<CredentialStore>) -> Self {
        Self {
            service: service.into(),
            entries: Mutex::new(HashMap::new()),
            store: Some(store),
        }
    }

    pub fn service(&self) -> &str {
        &self.service
    }

    pub fn get(&self, user: &str) -> Result<Option<Vec<u8>>, NativeKeyringError> {
        match self.entry(user)?.get_secret() {
            Ok(secret) => Ok(Some(secret)),
            Err(KeyringError::NoEntry) => Ok(None),
            Err(error) => Err(error.into()),
        }
    }

    pub fn get_string(&self, user: &str) -> Result<Option<String>, NativeKeyringError> {
        self.get(user)?
            .map(String::from_utf8)
            .transpose()
            .map_err(Into::into)
    }

    pub fn set(&self, user: &str, secret: &[u8]) -> Result<(), NativeKeyringError> {
        self.entry(user)?.set_secret(secret).map_err(Into::into)
    }

    pub fn set_string(&self, user: &str, secret: &str) -> Result<(), NativeKeyringError> {
        self.set(user, secret.as_bytes())
    }

    pub fn delete(&self, user: &str) -> Result<bool, NativeKeyringError> {
        match self.entry(user)?.delete_credential() {
            Ok(()) => Ok(true),
            Err(KeyringError::NoEntry) => Ok(false),
            Err(error) => Err(error.into()),
        }
    }

    pub fn list(&self) -> Result<Vec<String>, NativeKeyringError> {
        self.refuse_prompt_when_unattended()?;
        let mut users = self
            .search_service()?
            .into_iter()
            .filter_map(|entry| entry.get_specifiers())
            .filter_map(|(service, user)| (service == self.service).then_some(user))
            .collect::<Vec<_>>();
        users.sort();
        users.dedup();
        Ok(users)
    }

    /// Whether an entry exists for `user`, without reading its secret.
    ///
    /// [`Self::list`] searches item *attributes*; it never asks the platform
    /// for item data. On macOS that is the whole difference: reading data
    /// raises a Keychain access dialog for any binary not on the item's ACL,
    /// while enumerating attributes raises none. A caller that only needs to
    /// know whether a credential exists pays nothing here.
    pub fn contains(&self, user: &str) -> Result<bool, NativeKeyringError> {
        Ok(self.list()?.iter().any(|entry| entry == user))
    }

    /// How long [`Self::availability`] and [`Self::verify_round_trip`] wait for
    /// the platform store.
    ///
    /// Matches the deadline the CLI's diagnostic subprocess probes use, so a
    /// caller that runs several checks sees one consistent worst case.
    pub const HEALTHCHECK_TIMEOUT: Duration = Duration::from_secs(5);

    /// Whether the store can serve a credential, asked without any chance of
    /// an interactive prompt and without writing anything.
    ///
    /// On Linux this searches every Secret Service collection for the
    /// service's items and reads the default collection's lock flag; a
    /// locked match in any collection, or a locked default collection,
    /// reports [`NativeKeyringAvailability::Locked`]. The keyring adapter
    /// cannot be used for it: every adapter call that
    /// meets a locked item asks the desktop to unlock it and waits for an
    /// answer, which on a headless host never comes. The read runs on the
    /// calling thread under [`Self::HEALTHCHECK_TIMEOUT`], and a timeout
    /// drops the connection, so no thread outlives the call.
    ///
    /// Elsewhere it is an attribute search, which reads no item data. macOS
    /// answers a locked keychain with an interaction error once dialogs are
    /// off for the process (see [`keychain_interaction_allowed`]), and that
    /// error is reported as [`NativeKeyringAvailability::Locked`]. Windows
    /// Credential Manager has no lock to report.
    ///
    /// This proves the store is reachable, not that it is writable;
    /// [`Self::verify_round_trip`] proves that.
    pub fn availability(&self) -> Result<NativeKeyringAvailability, NativeKeyringError> {
        #[cfg(all(
            feature = "native-keyring",
            unix,
            not(any(target_os = "macos", target_os = "ios", target_os = "android"))
        ))]
        if self.store.is_none() {
            return super::secret_service_lock::secret_service_availability(
                &self.service,
                Self::HEALTHCHECK_TIMEOUT,
            );
        }
        match self.search_service() {
            Ok(_) => Ok(NativeKeyringAvailability::Available),
            Err(error)
                if error.unavailable_reason()
                    == Some(NativeKeyringUnavailable::InteractionRequired) =>
            {
                Ok(NativeKeyringAvailability::Locked)
            }
            Err(error) => Err(error),
        }
    }

    /// Prove the store round-trips a credential: write a probe, read it back,
    /// and delete it, within [`Self::HEALTHCHECK_TIMEOUT`].
    ///
    /// This is the opt-in check. It runs [`Self::availability`] first and returns
    /// [`NativeKeyringError::Locked`] for a locked store without writing, so
    /// it does not raise the Secret Service unlock prompt either. The write
    /// still runs on its own thread under a deadline, because a store can
    /// lock between the two calls. A thread abandoned at the deadline stays
    /// parked inside the platform client for the life of the process: that
    /// call has no cancellation, and a parked thread costs a stack while a
    /// hung process costs the run.
    pub fn verify_round_trip(&self) -> Result<String, NativeKeyringError> {
        self.verify_round_trip_within(Self::HEALTHCHECK_TIMEOUT)
    }

    /// [`Self::verify_round_trip`] with an explicit deadline, for tests.
    pub fn verify_round_trip_within(
        &self,
        timeout: Duration,
    ) -> Result<String, NativeKeyringError> {
        use std::sync::mpsc::RecvTimeoutError;

        if self.availability()? == NativeKeyringAvailability::Locked {
            return Err(NativeKeyringError::Locked);
        }
        let probe = Self {
            service: self.service.clone(),
            entries: Mutex::new(HashMap::new()),
            store: self.store.clone(),
        };
        let (sender, receiver) = std::sync::mpsc::channel();
        crate::runtime_stack::builder()
            .name("harn-keyring-round-trip".to_string())
            .spawn(move || {
                let user = format!("__harn_probe__:{}", uuid::Uuid::now_v7().simple());
                let _ = sender.send(probe.round_trip_with_user(&user));
            })
            .map_err(|_| {
                NativeKeyringError::Verification("could not start the round-trip probe")
            })?;

        match receiver.recv_timeout(timeout) {
            Ok(result) => result,
            Err(RecvTimeoutError::Timeout) => Err(NativeKeyringError::Unresponsive { timeout }),
            Err(RecvTimeoutError::Disconnected) => Err(NativeKeyringError::Verification(
                "the round-trip probe ended without reporting a result",
            )),
        }
    }

    fn round_trip_with_user(&self, user: &str) -> Result<String, NativeKeyringError> {
        const PROBE_VALUE: &[u8] = b"harn-keyring-healthcheck";

        self.set(user, PROBE_VALUE)?;
        let read_result = self.get(user);
        let delete_result = self.delete(user);

        if !delete_result? {
            return Err(NativeKeyringError::Verification(
                "stored probe could not be deleted",
            ));
        }
        match read_result? {
            Some(value) if value == PROBE_VALUE => Ok(format!(
                "service '{}' passed write, read, and delete checks",
                self.service
            )),
            Some(_) => Err(NativeKeyringError::Verification(
                "read returned a different value",
            )),
            None => Err(NativeKeyringError::Verification(
                "stored probe could not be read",
            )),
        }
    }

    fn search_service(&self) -> Result<Vec<Entry>, NativeKeyringError> {
        let store = self.store()?;
        #[cfg(target_os = "windows")]
        let pattern = format!(r"\.{}$", regex::escape(&self.service));
        #[cfg(target_os = "windows")]
        let spec = HashMap::from([("pattern", pattern.as_str())]);
        #[cfg(not(target_os = "windows"))]
        let spec = HashMap::from([("service", self.service.as_str())]);
        Ok(store.search(&spec)?)
    }

    /// Refuse, rather than prompt, when this process may not raise a dialog
    /// and the Linux Secret Service is locked.
    ///
    /// macOS gets the same refusal from the platform once dialogs are off
    /// for the process. The Secret Service has no such switch: the adapter
    /// unlocks whatever locked item an operation meets, and the unlock is a
    /// dialog. So the lock is checked first, through [`Self::availability`], which
    /// never asks for one. Only a confirmed lock refuses; when the probe
    /// itself fails, the operation runs and reports its own error.
    fn refuse_prompt_when_unattended(&self) -> Result<(), NativeKeyringError> {
        #[cfg(all(
            feature = "native-keyring",
            unix,
            not(any(target_os = "macos", target_os = "ios", target_os = "android"))
        ))]
        if self.store.is_none()
            && !keychain_interaction_allowed()
            && matches!(self.availability(), Ok(NativeKeyringAvailability::Locked))
        {
            return Err(NativeKeyringError::Locked);
        }
        Ok(())
    }

    fn entry(&self, user: &str) -> Result<Arc<Entry>, NativeKeyringError> {
        self.refuse_prompt_when_unattended()?;
        let mut entries = self.entries.lock().expect("keyring cache poisoned");
        if let Some(entry) = entries.get(user) {
            return Ok(entry.clone());
        }
        let entry = Arc::new(self.store()?.build(self.service(), user, None)?);
        entries.insert(user.to_string(), entry.clone());
        Ok(entry)
    }

    fn store(&self) -> Result<Arc<CredentialStore>, NativeKeyringError> {
        if let Some(store) = &self.store {
            return Ok(store.clone());
        }
        if let Some(store) = PLATFORM_STORE.get() {
            return Ok(store.clone());
        }
        let store = platform_store()?;
        let _ = PLATFORM_STORE.set(store.clone());
        Ok(PLATFORM_STORE.get().cloned().unwrap_or(store))
    }
}

fn platform_store() -> Result<Arc<CredentialStore>, NativeKeyringError> {
    #[cfg(all(feature = "native-keyring", target_os = "macos"))]
    {
        if !keychain_interaction_allowed() {
            disable_keychain_interaction_for_process();
        }
        let store: Arc<CredentialStore> = apple_native_keyring_store::keychain::Store::new()?;
        return Ok(store);
    }
    #[cfg(all(feature = "native-keyring", target_os = "ios"))]
    {
        let store: Arc<CredentialStore> = apple_native_keyring_store::protected::Store::new()?;
        return Ok(store);
    }
    #[cfg(all(feature = "native-keyring", target_os = "windows"))]
    {
        let store: Arc<CredentialStore> = windows_native_keyring_store::Store::new()?;
        return Ok(store);
    }
    #[cfg(all(
        feature = "native-keyring",
        unix,
        not(any(target_os = "macos", target_os = "ios", target_os = "android"))
    ))]
    {
        let store: Arc<CredentialStore> = zbus_secret_service_keyring_store::Store::new()?;
        return Ok(store);
    }
    #[allow(unreachable_code)]
    Err(KeyringError::NoDefaultStore.into())
}

/// Opts a process with no terminal back into credential-store dialogs, for a
/// host that launches Harn without one while a person is at the machine.
pub const SECRET_INTERACTIVE_ENV: &str = "HARN_SECRET_INTERACTIVE";

/// Whether this process may raise a credential-store dialog: a macOS Keychain
/// access dialog, or a Linux Secret Service unlock prompt.
///
/// A dialog needs someone to answer it. A test run, an eval, an agent's
/// subprocess or a scheduled job has no terminal and nobody watching, so a
/// read there either blocks on a prompt forever or interrupts whoever is at
/// the machine. Worse, a Keychain ACL binds to the binary's code identity, and
/// every rebuild of an ad hoc signed `harn` is a new one: "Always Allow" never
/// sticks, so the prompt returns on every build.
///
/// So a process prompts only when it has a terminal on stdin and is not
/// running under CI, or when [`SECRET_INTERACTIVE_ENV`] says a person is
/// present. Everywhere else a read that would prompt fails as
/// [`SecretError::NeedsUserApproval`]. On Linux the refusal comes from
/// checking the collection's lock first; see
/// `NativeKeyring::refuse_prompt_when_unattended`.
pub fn keychain_interaction_allowed() -> bool {
    use std::io::IsTerminal;
    match std::env::var(SECRET_INTERACTIVE_ENV).ok().as_deref() {
        Some("1" | "true") => return true,
        Some("0" | "false") => return false,
        _ => {}
    }
    let under_ci =
        std::env::var_os("CI").is_some_and(|value| !value.is_empty() && value != "false");
    std::io::stdin().is_terminal() && !under_ci
}

/// Turn Keychain dialogs off for the rest of this process.
///
/// The setting is process-wide, which is why it is applied once, when the
/// platform store is first opened, and never scoped to one call: a scoped lock
/// re-enables dialogs on drop while another thread may be mid-read. Leaking the
/// guard is the point. A failure to apply it is not fatal; the read that
/// follows can still prompt, which is the behavior this replaces.
#[cfg(all(feature = "native-keyring", target_os = "macos"))]
fn disable_keychain_interaction_for_process() {
    if let Ok(lock) =
        security_framework::os::macos::keychain::SecKeychain::disable_user_interaction()
    {
        std::mem::forget(lock);
    }
}

#[derive(Debug)]
pub struct KeyringSecretProvider {
    keyring: NativeKeyring,
}

impl KeyringSecretProvider {
    pub fn new(namespace: impl Into<String>) -> Self {
        Self {
            keyring: NativeKeyring::new(namespace),
        }
    }

    #[cfg(test)]
    pub(super) fn with_store(namespace: impl Into<String>, store: Arc<CredentialStore>) -> Self {
        Self {
            keyring: NativeKeyring::with_store(namespace, store),
        }
    }

    pub fn service(&self) -> &str {
        self.keyring.service()
    }

    pub async fn delete(&self, id: &SecretId) -> Result<(), SecretError> {
        self.keyring
            .delete(&account_name(id))
            .map(|_| ())
            .map_err(|error| backend_error("delete", error))
    }

    /// See [`NativeKeyring::availability`]. The error keeps its
    /// [`NativeKeyringError::unavailable_reason`] for the caller to report.
    pub fn availability(&self) -> Result<NativeKeyringAvailability, NativeKeyringError> {
        self.keyring.availability()
    }

    /// See [`NativeKeyring::verify_round_trip`].
    pub fn verify_round_trip(&self) -> Result<String, NativeKeyringError> {
        self.keyring.verify_round_trip()
    }
}

#[async_trait]
impl SecretProvider for KeyringSecretProvider {
    async fn get(&self, id: &SecretId) -> Result<SecretBytes, SecretError> {
        match self
            .keyring
            .get(&account_name(id))
            .map_err(|error| read_error(id, error))?
        {
            Some(bytes) => {
                emit_secret_access_event("keyring", id);
                Ok(SecretBytes::from(bytes))
            }
            None => Err(SecretError::NotFound {
                provider: "keyring".to_string(),
                id: id.clone(),
            }),
        }
    }

    async fn put(&self, id: &SecretId, value: SecretBytes) -> Result<(), SecretError> {
        value.with_exposed(|bytes| {
            self.keyring
                .set(&account_name(id), bytes)
                .map_err(|error| backend_error("store", error))
        })
    }

    async fn rotate(&self, _id: &SecretId) -> Result<RotationHandle, SecretError> {
        Err(SecretError::Unsupported {
            provider: "keyring".to_string(),
            operation: "rotate",
        })
    }

    async fn delete_scoped(&self, request: SecretDeleteRequest) -> Result<(), SecretError> {
        ensure_scoped_secret_access_allowed("delete", &request.id)?;
        self.delete(&request.id).await
    }

    /// Presence from the attribute-only search rather than an item read, so
    /// asking "does this credential exist" costs no Keychain access dialog.
    ///
    /// No `audit.secret_access` event is emitted: nothing read a secret. The
    /// event records value access, and firing it here would report reads that
    /// did not happen.
    async fn contains(&self, id: &SecretId) -> Result<bool, SecretError> {
        self.keyring
            .contains(&account_name(id))
            .map_err(|error| read_error(id, error))
    }

    async fn list(&self, _prefix: &SecretId) -> Result<Vec<SecretMeta>, SecretError> {
        Err(SecretError::Unsupported {
            provider: "keyring".to_string(),
            operation: "list",
        })
    }

    fn namespace(&self) -> &str {
        self.service()
    }

    fn supports_versions(&self) -> bool {
        false
    }

    fn kind(&self) -> &'static str {
        "keyring"
    }

    fn locator(&self, id: &SecretId) -> Option<String> {
        Some(format!(
            "service {} account {}",
            self.service(),
            account_name(id)
        ))
    }
}

/// A read the platform refused because it wanted a person at a dialog is
/// typed, so callers can say "needs approval" instead of "missing".
fn read_error(id: &SecretId, error: NativeKeyringError) -> SecretError {
    if error.unavailable_reason() == Some(NativeKeyringUnavailable::InteractionRequired) {
        return SecretError::NeedsUserApproval {
            provider: "keyring".to_string(),
            id: id.clone(),
        };
    }
    backend_error("read", error)
}

fn backend_error(operation: &str, error: NativeKeyringError) -> SecretError {
    SecretError::Backend {
        provider: "keyring".to_string(),
        message: format!("failed to {operation} keyring credential: {error}"),
    }
}

fn account_name(id: &SecretId) -> String {
    let mut account = String::new();
    if !id.namespace.is_empty() {
        account.push_str(&sanitize_component(&id.namespace));
        account.push('/');
    }
    account.push_str(&sanitize_component(&id.name));
    match id.version {
        super::SecretVersion::Latest => {}
        super::SecretVersion::Exact(version) => {
            account.push('#');
            account.push('v');
            account.push_str(&version.to_string());
        }
    }
    account
}

fn sanitize_component(value: &str) -> String {
    let normalized = value
        .chars()
        .map(|ch| {
            if ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.' | ':' | '/') {
                ch
            } else {
                '_'
            }
        })
        .collect::<String>();
    if normalized.is_empty() {
        "_".to_string()
    } else {
        normalized
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn native_keyring_round_trips_and_lists_service_users() {
        let keyring = NativeKeyring::with_store(
            "harn.native-test",
            keyring_core::mock::Store::new().unwrap(),
        );
        keyring.set_string("alpha", "one").unwrap();
        keyring.set_string("beta", "two").unwrap();

        assert_eq!(keyring.get_string("alpha").unwrap().as_deref(), Some("one"));
        assert_eq!(keyring.list().unwrap(), vec!["alpha", "beta"]);
        assert!(keyring.delete("alpha").unwrap());
        assert!(!keyring.delete("alpha").unwrap());
    }

    #[test]
    fn round_trip_proves_write_read_delete_and_leaves_no_probe() {
        let keyring = NativeKeyring::with_store(
            "harn.healthcheck-test",
            keyring_core::mock::Store::new().unwrap(),
        );

        let detail = keyring
            .round_trip_with_user("__harn_probe__:test")
            .expect("writable mock keyring");

        assert!(detail.contains("passed write, read, and delete checks"));
        assert_eq!(keyring.get("__harn_probe__:test").unwrap(), None);
    }

    /// Presence must not read the secret.
    ///
    /// The falsifier is the point: the stored credential is rigged so that any
    /// *data* access fails. `contains` still answers, because it searches item
    /// attributes. Implement it as `get(...).is_ok()` and this test fails —
    /// which is exactly the regression that made one `connect status` raise a
    /// Keychain access dialog per stored secret (#7749).
    #[tokio::test]
    async fn presence_is_answered_without_reading_the_secret() {
        let store = keyring_core::mock::Store::new().unwrap();
        let credential_store: Arc<CredentialStore> = store;
        let keyring =
            NativeKeyring::with_store("harn.presence-test", Arc::clone(&credential_store));
        keyring.set_string("alpha/token", "super-secret").unwrap();

        // Rig the stored item so reading its data fails. On a real macOS
        // keychain this is the ACL dialog; here it is a hard error, which is
        // the observable stand-in.
        credential_store
            .build("harn.presence-test", "alpha/token", None)
            .unwrap()
            .as_any()
            .downcast_ref::<keyring_core::mock::Cred>()
            .unwrap()
            .set_error(KeyringError::Invalid(
                "reading this item's data is not allowed".to_string(),
                "value read attempted".to_string(),
            ));

        let provider = KeyringSecretProvider::with_store("harn.presence-test", credential_store);
        let present = SecretId::new("alpha", "token");

        assert!(
            provider.contains(&present).await.unwrap(),
            "presence must come from the attribute search, not a value read"
        );
        // Guard against passing for the wrong reason: the value read really is
        // broken, so a `get`-based implementation could not have returned true.
        assert!(provider.get(&present).await.is_err());
        assert!(!provider
            .contains(&SecretId::new("alpha", "absent"))
            .await
            .unwrap());
    }

    /// The control for the case above: the trait's default `contains` really
    /// is value-based.
    ///
    /// Together the two are the falsifier, without anyone having to edit the
    /// override and re-run. This one shows a provider that implements only
    /// `get` propagates a read failure out of `contains`; the keyring case
    /// shows the keyring provider answers `true` for an item whose `get`
    /// fails. No value-reading implementation can do both.
    #[tokio::test]
    async fn the_default_presence_implementation_reads_the_value() {
        struct UnreadableProvider {
            namespace: String,
        }

        #[async_trait]
        impl SecretProvider for UnreadableProvider {
            async fn get(&self, _id: &SecretId) -> Result<SecretBytes, SecretError> {
                Err(SecretError::Backend {
                    provider: self.namespace.clone(),
                    message: "value read attempted".to_string(),
                })
            }

            async fn put(&self, _id: &SecretId, _value: SecretBytes) -> Result<(), SecretError> {
                unimplemented!("presence control never writes")
            }

            async fn rotate(&self, _id: &SecretId) -> Result<RotationHandle, SecretError> {
                unimplemented!("presence control never rotates")
            }

            async fn list(&self, _prefix: &SecretId) -> Result<Vec<SecretMeta>, SecretError> {
                unimplemented!("presence control never lists")
            }

            fn namespace(&self) -> &str {
                &self.namespace
            }

            fn supports_versions(&self) -> bool {
                false
            }
        }

        let error = UnreadableProvider {
            namespace: "fixture".to_string(),
        }
        .contains(&SecretId::new("alpha", "token"))
        .await
        .expect_err("the default presence check reads the value, so a read failure surfaces");
        assert!(error.to_string().contains("value read attempted"));
    }

    #[test]
    fn round_trip_rejects_a_store_that_is_reachable_but_not_writable() {
        let store = keyring_core::mock::Store::new().unwrap();
        let credential_store: Arc<CredentialStore> = store;
        let entry = credential_store
            .build("harn.healthcheck-read-only", "__harn_probe__:test", None)
            .unwrap();
        entry
            .as_any()
            .downcast_ref::<keyring_core::mock::Cred>()
            .unwrap()
            .set_error(KeyringError::Invalid(
                "mock read-only store".to_string(),
                "write denied".to_string(),
            ));
        let keyring = NativeKeyring::with_store("harn.healthcheck-read-only", credential_store);

        let error = keyring
            .round_trip_with_user("__harn_probe__:test")
            .expect_err("read-only store must fail the round trip");

        assert!(error.to_string().contains("mock read-only store"));
    }

    /// A credential store that never answers, so the deadline is the only
    /// thing that can end a probe against it.
    ///
    /// The obvious way to test a deadline is to set it to zero, and that is
    /// what this test used to do. It does not test the deadline: the probe
    /// runs on its own thread, and `recv_timeout` hands back a result that is
    /// already in the channel no matter how short the deadline is, so a zero
    /// deadline only fails the probe when the caller loses a race against
    /// thread startup. On one host the probe was slower and the test passed;
    /// on a hosted runner the probe won and the same code reported a healthy
    /// store under an expired deadline.
    ///
    /// Blocking in `build` reproduces what the deadline exists for. A locked
    /// Secret Service collection stops answering there, inside the platform
    /// client, with no way to cancel it from the calling thread.
    #[derive(Debug)]
    struct NeverAnswersStore {
        released: Mutex<bool>,
        released_signal: std::sync::Condvar,
    }

    impl NeverAnswersStore {
        fn new() -> Arc<Self> {
            Arc::new(Self {
                released: Mutex::new(false),
                released_signal: std::sync::Condvar::new(),
            })
        }

        /// Let the abandoned probe thread finish.
        ///
        /// Production leaves it parked for the life of the process on purpose,
        /// which is fine for a command that is about to exit and rude in a
        /// test binary that keeps running.
        fn release(&self) {
            *self.released.lock().expect("release lock") = true;
            self.released_signal.notify_all();
        }
    }

    impl keyring_core::api::CredentialStoreApi for NeverAnswersStore {
        fn vendor(&self) -> String {
            "harn-test/never-answers".to_string()
        }

        fn id(&self) -> String {
            "never-answers".to_string()
        }

        fn build(
            &self,
            _service: &str,
            _user: &str,
            _modifiers: Option<&HashMap<&str, &str>>,
        ) -> keyring_core::Result<Entry> {
            let mut released = self.released.lock().expect("release lock");
            while !*released {
                released = self
                    .released_signal
                    .wait(released)
                    .expect("wait for release");
            }
            Err(KeyringError::NoStorageAccess(Box::new(
                std::io::Error::other("the test released a parked probe"),
            )))
        }

        /// Searching answers, so the availability probe passes and the write is
        /// what stops answering.
        fn search(&self, _spec: &HashMap<&str, &str>) -> keyring_core::Result<Vec<Entry>> {
            Ok(Vec::new())
        }

        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
    }

    #[test]
    fn round_trip_gives_up_on_a_store_that_does_not_answer() {
        // Positive control first: the same call against a store that does
        // answer passes, so the refusal below is the deadline firing and not
        // the probe failing for some other reason.
        NativeKeyring::with_store(
            "harn.healthcheck-deadline",
            keyring_core::mock::Store::new().unwrap(),
        )
        .verify_round_trip_within(Duration::from_secs(5))
        .expect("a responsive store passes within the deadline");

        let store = NeverAnswersStore::new();
        let blocking: Arc<CredentialStore> = store.clone();
        let keyring = NativeKeyring::with_store("harn.healthcheck-deadline", blocking);

        let error = keyring
            .verify_round_trip_within(Duration::from_millis(50))
            .expect_err("a store that never answers must not report a healthy store");

        assert!(
            matches!(error, NativeKeyringError::Unresponsive { .. }),
            "expected an unresponsive-store error, got {error}"
        );
        assert!(
            error.to_string().contains("interactive unlock"),
            "the error must name why a store stops answering: {error}"
        );
        assert_eq!(
            error.unavailable_reason(),
            Some(NativeKeyringUnavailable::InteractionRequired),
            "a store awaiting a prompt is unavailable, not an operational failure"
        );

        store.release();
    }

    /// A store whose search answers with a fixed result and whose every
    /// entry build fails, so any attempt to write is visible as an error.
    #[derive(Debug)]
    struct SearchOnlyStore {
        search: fn() -> keyring_core::Result<Vec<Entry>>,
    }

    impl SearchOnlyStore {
        fn credential_store(
            search: fn() -> keyring_core::Result<Vec<Entry>>,
        ) -> Arc<CredentialStore> {
            Arc::new(Self { search })
        }
    }

    impl keyring_core::api::CredentialStoreApi for SearchOnlyStore {
        fn vendor(&self) -> String {
            "harn-test/search-only".to_string()
        }

        fn id(&self) -> String {
            "search-only".to_string()
        }

        fn build(
            &self,
            _service: &str,
            _user: &str,
            _modifiers: Option<&HashMap<&str, &str>>,
        ) -> keyring_core::Result<Entry> {
            Err(KeyringError::Invalid(
                "entry".to_string(),
                "write attempted".to_string(),
            ))
        }

        fn search(&self, _spec: &HashMap<&str, &str>) -> keyring_core::Result<Vec<Entry>> {
            (self.search)()
        }

        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
    }

    /// The default diagnostic answers from a search and writes nothing.
    ///
    /// The store fails every write, so an availability probe that wrote would fail
    /// here. The round trip against the same store is the control: it does
    /// write, and does fail.
    #[test]
    fn availability_answers_without_writing() {
        let keyring = NativeKeyring::with_store(
            "harn.availability-test",
            SearchOnlyStore::credential_store(|| Ok(Vec::new())),
        );

        assert_eq!(
            keyring.availability().unwrap(),
            NativeKeyringAvailability::Available
        );
        let error = keyring
            .verify_round_trip()
            .expect_err("the opt-in round trip writes, and this store refuses writes");
        assert!(error.to_string().contains("write attempted"), "{error}");
    }

    /// The negative control: the probe can fail, and says why.
    #[test]
    fn availability_reports_an_unreachable_store_as_unavailable_with_its_reason() {
        let keyring = NativeKeyring::with_store(
            "harn.availability-unreachable",
            SearchOnlyStore::credential_store(|| {
                Err(KeyringError::NoStorageAccess(Box::new(
                    std::io::Error::other("no session bus"),
                )))
            }),
        );

        let error = keyring
            .availability()
            .expect_err("a store that cannot be reached is not available");
        assert_eq!(
            error.unavailable_reason(),
            Some(NativeKeyringUnavailable::StorageInaccessible)
        );
        assert!(error.to_string().contains("no session bus"), "{error}");
    }

    /// A locked keychain is reported as locked, distinct from unavailable,
    /// and the opt-in round trip refuses it without attempting a write.
    #[cfg(all(feature = "native-keyring", target_os = "macos"))]
    #[test]
    fn availability_reports_a_locked_keychain_as_locked() {
        let keyring = NativeKeyring::with_store(
            "harn.availability-locked",
            SearchOnlyStore::credential_store(|| {
                // errSecInteractionNotAllowed: the keychain needs unlocking
                // and dialogs are off for this process.
                Err(KeyringError::PlatformFailure(Box::new(
                    security_framework::base::Error::from_code(-25308),
                )))
            }),
        );

        assert_eq!(
            keyring.availability().unwrap(),
            NativeKeyringAvailability::Locked
        );
        assert!(matches!(
            keyring.verify_round_trip(),
            Err(NativeKeyringError::Locked)
        ));
    }
}