Skip to main content

secrets_core/
router.rs

1use std::sync::Arc;
2
3use crate::engine::SecretsEngine;
4
5pub struct EngineMount {
6    pub prefix: String,
7    pub engine: Arc<dyn SecretsEngine>,
8}
9
10/// Maps request paths onto mounted secrets engines by longest-prefix match —
11/// the same dispatch model `Policy::is_allowed` uses for ACLs. Registering a
12/// new engine means adding one `EngineMount` here, nowhere else.
13#[derive(Default)]
14pub struct Router {
15    mounts: Vec<EngineMount>,
16}
17
18impl Router {
19    pub fn new(mounts: Vec<EngineMount>) -> Self {
20        Self { mounts }
21    }
22
23    /// Every registered mount, in registration order. Used to build the
24    /// server's `sys/help` index.
25    pub fn mounts(&self) -> &[EngineMount] {
26        &self.mounts
27    }
28
29    /// Returns the matching mount and the path remainder relative to it.
30    pub fn resolve<'a>(&self, path: &'a str) -> Option<(&EngineMount, &'a str)> {
31        self.mounts
32            .iter()
33            .filter(|m| path.starts_with(&m.prefix))
34            .max_by_key(|m| m.prefix.len())
35            .map(move |m| (m, path.strip_prefix(&m.prefix).unwrap_or("")))
36    }
37}