devboy-storage 0.30.0

Secure credential storage for devboy-tools — OS keychain (macOS/Windows/Linux) with redacted SecretString plumbing.
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
//! Source-credential recursion check per [ADR-021] §4.
//!
//! A source `A` that declares
//! [`SecretSource::requires_credential`](crate::source::SecretSource::requires_credential)
//! `= Some(CredentialRef::Path(...))` must have its credential
//! resolved through a source `B` whose `requires_credential()` is
//! `None`. The router enforces this at *configuration load*, before
//! any `get()` is dispatched, so the user gets a structured error
//! rather than a runtime stack-overflow on the first secret read.
//!
//! ## Why one hop?
//!
//! The reasoning from ADR-021 §4: a Vault token cannot itself be
//! stored in Vault, because reading it would require Vault to
//! already be unlockable. The keychain (and the local-vault from
//! ADR-023, once unlocked) are the only sources that may hold
//! source-credentials, because they have no `requires_credential()`
//! of their own.
//!
//! Anything deeper than one hop is either a misconfiguration the
//! user did not realise, or a literal cycle. Both fail the load
//! with a typed error from this module.
//!
//! ## What the validator checks
//!
//! For every source `A` in [`RouterConfig::sources`] whose
//! `requires_credential()` is `Some(CredentialRef::Path(p))`:
//!
//! 1. `p` must live under the reserved `__sources/` namespace.
//!    Anything else is rejected with [`CredentialGraphError::BadCredentialPath`]
//!    so users can't accidentally route source-credentials through
//!    their normal manifest.
//! 2. `p` must resolve through the configured router rules to some
//!    source `B`.
//! 3. Walk the chain `A → B → C → …`. The first node whose
//!    `requires_credential()` is `None` (or
//!    `Some(CredentialRef::Sentinel)` — a sentinel means the
//!    source handles its own auth and is treated as terminal)
//!    closes the chain.
//! 4. If we revisit a node, the chain is a cycle —
//!    [`CredentialGraphError::Cycle`].
//! 5. Otherwise, if the chain is longer than one hop —
//!    [`CredentialGraphError::Deep`].
//!
//! `Sentinel`-typed credentials are *not* graph edges; the source
//! plugin interprets them natively (`biometric`,
//! `default-profile`). They terminate the walk with no traversal.
//!
//! [ADR-021]: https://github.com/meteora-pro/devboy-tools/blob/main/docs/architecture/adr/ADR-021-external-secret-sources.md
//! [`RouterConfig::sources`]: crate::router_config::RouterConfig::sources

use thiserror::Error;

use crate::router_config::RouterConfig;
use crate::router_resolve::{PathResolver, ResolveError};
use crate::secret_path::SecretPath;
use crate::source::CredentialRef;

/// Reserved prefix for source-authentication credential paths.
/// Anything outside this namespace is rejected as a credential
/// path; per ADR-021 §5 only `__sources/<source>/<profile>` paths
/// may carry source credentials.
pub const SOURCE_CREDENTIALS_PREFIX: &str = "__sources/";

// =============================================================================
// Errors
// =============================================================================

/// Failure modes for [`validate_source_credentials`].
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum CredentialGraphError {
    /// A source declared a credential at `path`, but `path` is not
    /// under the reserved [`SOURCE_CREDENTIALS_PREFIX`]. Per
    /// ADR-021 §5: source credentials must live under `__sources/`.
    //
    // `source_name` is intentionally not named `source` — thiserror
    // would interpret a field named `source` as the `#[source]`
    // chain root (cf. `UnroutableCredential::source_error`).
    #[error(
        "source '{source_name}' declares its credential at '{path}', but credential paths must live under `{SOURCE_CREDENTIALS_PREFIX}`"
    )]
    BadCredentialPath {
        /// The source whose `requires_credential()` returned this
        /// path.
        source_name: String,
        /// The offending path.
        path: String,
    },

    /// The credential path failed to resolve through the router.
    /// Usually means the user did not configure routing for
    /// `__sources/<source>/...` and there is no `[default]`.
    #[error("source '{source_name}' credential at '{path}' is unroutable: {source_error}")]
    UnroutableCredential {
        /// The source whose credential is unroutable.
        source_name: String,
        /// The credential path.
        path: String,
        /// Underlying resolver error.
        #[source]
        source_error: ResolveError,
    },

    /// `E_SOURCE_CREDENTIAL_CYCLE` — the credential graph contains
    /// a cycle. Reading any secret would loop forever.
    #[error(
        "source credential cycle detected: {}",
        chain.join(" -> ")
    )]
    Cycle {
        /// Source names visited, in order, ending with the source
        /// that closed the cycle.
        chain: Vec<String>,
    },

    /// `E_SOURCE_CREDENTIAL_DEEP` — the credential chain is longer
    /// than one hop. Per ADR-021 §4, only one level of indirection
    /// is allowed (Vault tokens live in keychain, not in another
    /// Vault).
    #[error(
        "source credential chain too deep (>1 hop): {}",
        chain.join(" -> ")
    )]
    Deep {
        /// Source names visited, in order. The first is the source
        /// whose credential is mis-routed; the rest are downstream
        /// dependencies.
        chain: Vec<String>,
    },
}

// =============================================================================
// Validator
// =============================================================================

/// Validate the source-credential graph defined by `config` and the
/// `requires_credential` lookup for each defined source.
///
/// `requires_credential` maps a source name to its
/// [`SecretSource::requires_credential`](crate::source::SecretSource::requires_credential)
/// return value. The router constructs sources first, then passes
/// `|name| sources[name].requires_credential()` here. Tests stub
/// the function with a static map.
///
/// Returns `Ok(())` when every source either has no credential, is
/// satisfied by a sentinel, or routes through exactly one
/// credential-free source. Otherwise returns the appropriate
/// [`CredentialGraphError`].
pub fn validate_source_credentials<F>(
    config: &RouterConfig,
    mut requires_credential: F,
) -> Result<(), CredentialGraphError>
where
    F: FnMut(&str) -> Option<CredentialRef>,
{
    let resolver = PathResolver::new(config);

    for src in &config.sources {
        let name = &src.name;
        let cred = requires_credential(name);
        match cred {
            None => continue,
            Some(CredentialRef::Sentinel(_)) => continue,
            Some(CredentialRef::Path(p)) => {
                ensure_credential_path_namespace(name, &p)?;
                walk_chain(name, &p, &resolver, &mut requires_credential)?;
            }
        }
    }

    Ok(())
}

fn ensure_credential_path_namespace(
    source_name: &str,
    path: &SecretPath,
) -> Result<(), CredentialGraphError> {
    if !path.as_str().starts_with(SOURCE_CREDENTIALS_PREFIX) {
        return Err(CredentialGraphError::BadCredentialPath {
            source_name: source_name.to_owned(),
            path: path.to_string(),
        });
    }
    Ok(())
}

/// Walk the chain starting from `start` whose credential lives at
/// `cred_path`. The chain length is counted in **edges** — a valid
/// one-hop chain has length 1.
fn walk_chain<F>(
    start: &str,
    cred_path: &SecretPath,
    resolver: &PathResolver<'_>,
    requires_credential: &mut F,
) -> Result<(), CredentialGraphError>
where
    F: FnMut(&str) -> Option<CredentialRef>,
{
    // `chain` records every source name we've visited so we can
    // (a) detect cycles and (b) include a useful trace in error
    // messages.
    let mut chain: Vec<String> = vec![start.to_owned()];
    let mut current_path = cred_path.clone();
    let mut hop = 0usize;

    loop {
        let decision = resolver.resolve(&current_path).map_err(|e| {
            CredentialGraphError::UnroutableCredential {
                source_name: start.to_owned(),
                path: current_path.to_string(),
                source_error: e,
            }
        })?;
        let next = decision.source().to_owned();
        hop += 1;

        // Cycle: revisiting a source we already saw — including
        // `start` itself if hop == 1 closes back to it.
        if chain.iter().any(|s| s == &next) {
            chain.push(next);
            return Err(CredentialGraphError::Cycle { chain });
        }
        chain.push(next.clone());

        let next_cred = requires_credential(&next);
        match next_cred {
            None | Some(CredentialRef::Sentinel(_)) => {
                // Terminal. Valid only if hop == 1; otherwise the
                // chain went too deep before closing.
                if hop > 1 {
                    return Err(CredentialGraphError::Deep { chain });
                }
                return Ok(());
            }
            Some(CredentialRef::Path(p)) => {
                if hop >= 1 {
                    // We already consumed the one allowed hop and
                    // the next node still needs a credential —
                    // either we eventually cycle back (caught on
                    // the next iteration) or we walk forever
                    // through credential-bearing sources, which is
                    // DEEP. Continue the walk so the loop can tell
                    // them apart.
                    ensure_credential_path_namespace(&next, &p)?;
                    current_path = p;
                    continue;
                }
            }
        }
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::router_config::RouterConfig;
    use std::collections::HashMap;

    fn cfg(toml: &str) -> RouterConfig {
        RouterConfig::parse(toml).expect("fixture config must parse")
    }

    fn p_internal(s: &str) -> SecretPath {
        SecretPath::parse_internal(s).expect("internal path must parse")
    }

    /// Convenience — build a `requires_credential` function over a
    /// static map. `None` entries mean credential-free.
    fn req_map(
        m: HashMap<String, Option<CredentialRef>>,
    ) -> impl FnMut(&str) -> Option<CredentialRef> {
        move |name| m.get(name).cloned().unwrap_or(None)
    }

    // -- 1) Valid one-hop chain -----------------------------------

    #[test]
    fn one_hop_chain_through_keychain_is_valid() {
        // vault-team needs a credential; it routes to keychain
        // (credential-free) via a `[[route]]` for the `__sources/`
        // namespace.
        let c = cfg(r#"
            [[source]]
            name = "vault-team"
            type = "vault"

            [[source]]
            name = "keychain"
            type = "keychain"

            [[route]]
            prefix = "__sources/"
            source = "keychain"
            "#);
        let req = req_map(
            [
                (
                    "vault-team".to_owned(),
                    Some(CredentialRef::Path(p_internal(
                        "__sources/vault-team/deploy",
                    ))),
                ),
                ("keychain".to_owned(), None),
            ]
            .into_iter()
            .collect(),
        );
        validate_source_credentials(&c, req).expect("one-hop chain should be valid");
    }

    // -- 2) Self-cycle -------------------------------------------

    #[test]
    fn self_loop_is_a_cycle() {
        // vault-team requires a credential whose path resolves
        // back to vault-team itself (via [secret] override).
        let c = cfg(r#"
            [[source]]
            name = "vault-team"
            type = "vault"

            [secret."__sources/vault-team/deploy"]
            source = "vault-team"
            reference = "secret/data/__sources/vault-team/deploy"
            "#);
        let req = req_map(
            [(
                "vault-team".to_owned(),
                Some(CredentialRef::Path(p_internal(
                    "__sources/vault-team/deploy",
                ))),
            )]
            .into_iter()
            .collect(),
        );
        let err = validate_source_credentials(&c, req).unwrap_err();
        match err {
            CredentialGraphError::Cycle { chain } => {
                assert_eq!(
                    chain,
                    vec!["vault-team".to_owned(), "vault-team".to_owned()]
                );
            }
            other => panic!("expected Cycle, got {other:?}"),
        }
    }

    // -- 3) Two-node cycle ---------------------------------------

    #[test]
    fn two_node_cycle_detected() {
        // A → B (via [secret] override of __sources/A/...)
        // B → A (via [secret] override of __sources/B/...)
        let c = cfg(r#"
            [[source]]
            name = "vault-a"
            type = "vault"

            [[source]]
            name = "vault-b"
            type = "vault"

            [secret."__sources/vault-a/x"]
            source = "vault-b"
            reference = "secret/a/x"

            [secret."__sources/vault-b/x"]
            source = "vault-a"
            reference = "secret/b/x"
            "#);
        let req = req_map(
            [
                (
                    "vault-a".to_owned(),
                    Some(CredentialRef::Path(p_internal("__sources/vault-a/x"))),
                ),
                (
                    "vault-b".to_owned(),
                    Some(CredentialRef::Path(p_internal("__sources/vault-b/x"))),
                ),
            ]
            .into_iter()
            .collect(),
        );
        let err = validate_source_credentials(&c, req).unwrap_err();
        match err {
            CredentialGraphError::Cycle { chain } => {
                // Either A->B->A or B->A->B depending on iteration
                // order over config.sources (Vec, declaration
                // order). With the config above we hit vault-a
                // first.
                assert_eq!(chain.first().unwrap(), "vault-a");
                assert_eq!(chain.last().unwrap(), "vault-a");
                assert!(chain.iter().any(|n| n == "vault-b"));
            }
            other => panic!("expected Cycle, got {other:?}"),
        }
    }

    // -- 4) Deep chain (no cycle) --------------------------------

    #[test]
    fn three_hop_chain_without_cycle_is_deep() {
        // A requires __sources/A/x → routes to B (B requires
        // __sources/B/y → routes to C (C credential-free)).
        let c = cfg(r#"
            [[source]]
            name = "vault-a"
            type = "vault"

            [[source]]
            name = "vault-b"
            type = "vault"

            [[source]]
            name = "keychain"
            type = "keychain"

            [secret."__sources/vault-a/x"]
            source = "vault-b"
            reference = "x"

            [secret."__sources/vault-b/y"]
            source = "keychain"
            reference = "y"
            "#);
        let req = req_map(
            [
                (
                    "vault-a".to_owned(),
                    Some(CredentialRef::Path(p_internal("__sources/vault-a/x"))),
                ),
                (
                    "vault-b".to_owned(),
                    Some(CredentialRef::Path(p_internal("__sources/vault-b/y"))),
                ),
                ("keychain".to_owned(), None),
            ]
            .into_iter()
            .collect(),
        );
        let err = validate_source_credentials(&c, req).unwrap_err();
        match err {
            CredentialGraphError::Deep { chain } => {
                assert_eq!(
                    chain,
                    vec![
                        "vault-a".to_owned(),
                        "vault-b".to_owned(),
                        "keychain".to_owned()
                    ]
                );
            }
            other => panic!("expected Deep, got {other:?}"),
        }
    }

    // -- 5) Nothing to check --------------------------------------

    #[test]
    fn no_source_with_credential_is_ok() {
        let c = cfg(r#"
            [[source]]
            name = "keychain"
            type = "keychain"

            [[source]]
            name = "env-store"
            type = "env-store"
            "#);
        let req = req_map(HashMap::new());
        validate_source_credentials(&c, req).unwrap();
    }

    // -- 6) Sentinel terminates without traversal -----------------

    #[test]
    fn sentinel_credential_is_terminal() {
        // 1Password's biometric session is a Sentinel — the source
        // handles its own auth, so the validator should not try to
        // route the credential.
        let c = cfg(r#"
            [[source]]
            name = "1p-personal"
            type = "1password"
            "#);
        let req = req_map(
            [(
                "1p-personal".to_owned(),
                Some(CredentialRef::Sentinel("biometric".to_owned())),
            )]
            .into_iter()
            .collect(),
        );
        validate_source_credentials(&c, req).unwrap();
    }

    // -- 7) Path outside __sources/ -------------------------------

    #[test]
    fn credential_path_outside_internal_namespace_rejected() {
        // ADR-020 path validation forbids `__*` outside
        // `parse_internal`, so we have to use a regular path here
        // (which is exactly the case we want to reject).
        let c = cfg(r#"
            [[source]]
            name = "vault-team"
            type = "vault"
            [[source]]
            name = "keychain"
            type = "keychain"

            [default]
            source = "keychain"
            "#);
        let req = req_map(
            [(
                "vault-team".to_owned(),
                Some(CredentialRef::Path(
                    SecretPath::parse("team/secret/token").unwrap(),
                )),
            )]
            .into_iter()
            .collect(),
        );
        let err = validate_source_credentials(&c, req).unwrap_err();
        match err {
            CredentialGraphError::BadCredentialPath { source_name, path } => {
                assert_eq!(source_name, "vault-team");
                assert_eq!(path, "team/secret/token");
            }
            other => panic!("expected BadCredentialPath, got {other:?}"),
        }
    }

    // -- 8) Unroutable credential ---------------------------------

    #[test]
    fn unroutable_credential_path_surfaces_resolve_error() {
        // No [default], no [[route]] for `__sources/`, no
        // [secret]. The path is unroutable.
        let c = cfg(r#"
            [[source]]
            name = "vault-team"
            type = "vault"
            [[source]]
            name = "keychain"
            type = "keychain"
            "#);
        let req = req_map(
            [(
                "vault-team".to_owned(),
                Some(CredentialRef::Path(p_internal(
                    "__sources/vault-team/deploy",
                ))),
            )]
            .into_iter()
            .collect(),
        );
        let err = validate_source_credentials(&c, req).unwrap_err();
        match err {
            CredentialGraphError::UnroutableCredential {
                source_name,
                path,
                source_error,
            } => {
                assert_eq!(source_name, "vault-team");
                assert_eq!(path, "__sources/vault-team/deploy");
                assert!(matches!(source_error, ResolveError::NoRoute { .. }));
            }
            other => panic!("expected UnroutableCredential, got {other:?}"),
        }
    }

    // -- 9) Default-routed __sources/ ------------------------------

    #[test]
    fn one_hop_chain_via_default_route_is_valid() {
        // No explicit __sources/ route; the default catches it.
        let c = cfg(r#"
            [[source]]
            name = "vault-team"
            type = "vault"

            [[source]]
            name = "keychain"
            type = "keychain"

            [default]
            source = "keychain"
            "#);
        let req = req_map(
            [
                (
                    "vault-team".to_owned(),
                    Some(CredentialRef::Path(p_internal(
                        "__sources/vault-team/deploy",
                    ))),
                ),
                ("keychain".to_owned(), None),
            ]
            .into_iter()
            .collect(),
        );
        validate_source_credentials(&c, req).unwrap();
    }
}