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    /// Returns the matching mount and the path remainder relative to it.
24    pub fn resolve<'a>(&self, path: &'a str) -> Option<(&EngineMount, &'a str)> {
25        self.mounts
26            .iter()
27            .filter(|m| path.starts_with(&m.prefix))
28            .max_by_key(|m| m.prefix.len())
29            .map(move |m| (m, path.strip_prefix(&m.prefix).unwrap_or("")))
30    }
31}