57 lines
1.6 KiB
Rust
57 lines
1.6 KiB
Rust
pub mod kv;
|
|
|
|
use axum::{
|
|
body::Body,
|
|
extract::{Request, State},
|
|
http::Uri,
|
|
response::Response,
|
|
Router,
|
|
};
|
|
use log::*;
|
|
|
|
use crate::storage::DatabaseDriver;
|
|
|
|
pub fn secrets_router(pool: DatabaseDriver) -> Router<DatabaseDriver> {
|
|
Router::new().fallback(engine_handler).with_state(pool)
|
|
}
|
|
|
|
async fn engine_handler(State(pool): State<DatabaseDriver>, req: Request) -> Response<Body> {
|
|
match map_mount_points(req.uri(), &pool).await {
|
|
Some((mount_path, engine_type)) => {
|
|
info!("Found mount point {} of type {}", mount_path, engine_type);
|
|
todo!()
|
|
}
|
|
None => todo!(),
|
|
}
|
|
|
|
// Response::new(Body::from("Mount path:"))
|
|
}
|
|
|
|
async fn map_mount_points(req: &Uri, pool: &DatabaseDriver) -> Option<(String, String)> {
|
|
let mut mount_path_fragments: Vec<&str> = req.path().split('/').collect();
|
|
|
|
// Find longest matching existing mount path for the request
|
|
for _ in 1..mount_path_fragments.len() {
|
|
let path_str = mount_path_fragments.join("/");
|
|
let record = sqlx::query!(
|
|
"SELECT engine_type FROM secret_engines WHERE mount_point = $1",
|
|
path_str
|
|
)
|
|
.fetch_optional(pool)
|
|
.await;
|
|
|
|
if let Ok(Some(row)) = record {
|
|
trace!(
|
|
"Mount path {} found with {:?} engine for route request: {}",
|
|
mount_path_fragments.join("/"),
|
|
row.engine_type,
|
|
req.path()
|
|
);
|
|
|
|
return Some((mount_path_fragments.join("/"), row.engine_type));
|
|
} else {
|
|
mount_path_fragments.pop();
|
|
}
|
|
}
|
|
None
|
|
}
|