moved test request to engines.rs + extracted mountpath from request

This commit is contained in:
someone 2024-04-28 15:42:57 +02:00
parent aae1225d2d
commit 0730d711b5
3 changed files with 32 additions and 26 deletions

View file

@ -44,7 +44,7 @@ func TestWriteSecret(t *testing.T) {
_, err := client.Secrets.KvV2Write(ctx, "foo", schema.KvV2WriteRequest{
Data: map[string]any{
"password1": "abc123",
"password2": "correct horse battery staple",
"version": 1,
}},
vault.WithMountPath(mountpath),
)

View file

@ -1,7 +1,11 @@
pub mod kv;
use std::string;
use crate::engines::kv::logic::body_to_json;
use crate::engines::kv::structs::KvSecret;
use axum::{
extract::Request,
extract::{Path, Request},
http::StatusCode,
middleware::{self, Next},
response::Response,
@ -13,10 +17,29 @@ use log::*;
pub fn secrets_router() -> Router {
// Router::new().layer(map_request(handler))
Router::new()
Router::new().route("/:mount_path/data/foo", post(baz))
// .nest("/:path", kv2::asdasdsadsd())
}
/// Routing handler for path "/v1/kv-v2/data/foo"
/// expects payload as JSON, prints payload into struct
async fn baz(Path(mount_path): Path<String>, body: String) -> String {
let mut body_json = body_to_json(body);
let secret: KvSecret = KvSecret {
content: body_json["data"]["password1"].take().to_string(),
version: body_json["data"]["version"].take().as_i64().unwrap(),
};
log::debug!(
"Content: {}, Version: {}, path: {}",
secret.content,
secret.version,
mount_path
);
String::from("RoutingTest baz successful")
}
// async fn handler(Host(hostname): Host, request: Request<Body>) -> &'static str {
// TODO: Find a solution for this mess
// async fn handler(request: Request<Body>) -> Result<Request<Body>, StatusCode> {

View file

@ -1,11 +1,13 @@
use axum::{extract::Request, http::StatusCode, routing::get, Router};
use axum::{
extract::Request,
http::StatusCode,
routing::get,
Router,
};
use log::*;
use serde::Deserialize;
use std::{env, net::SocketAddr, str::FromStr};
use tokio::net::TcpListener;
use crate::engines::kv::logic::body_to_json;
mod auth;
mod common;
mod engines;
@ -30,7 +32,6 @@ async fn main() {
.nest("/v1/identity", identity::identity_router())
.nest("/v1/sys", sys::sys_router())
.nest("/v1", engines::secrets_router()) // mountable secret backends
// .route("/v1/kv-v2/data/foo", post(baz))
.fallback(fallback_route_unknown);
warn!("Listening on {}", listen_addr.to_string());
@ -39,24 +40,6 @@ async fn main() {
axum::serve(listener, app).await.unwrap();
}
#[derive(Deserialize, Debug)]
struct CreateSecret {
password1: String,
password2: String,
}
/// Routing handler for path "/v1/kw_mount_path/data/foo"
/// expects payload as JSON, prints payload into struct
async fn baz(body: String) -> String {
let mut body_json = body_to_json(body);
let secret: CreateSecret = CreateSecret {
password1: body_json["data"]["password1"].take().to_string(),
password2: body_json["data"]["password2"].take().to_string(),
};
log::debug!("Pass1: {}, Pass2: {}", secret.password1, secret.password2);
String::from("RoutingTest baz successful")
}
async fn fallback_route_unknown(req: Request) -> (StatusCode, &'static str) {
log::error!(
"Route not found: {} {}, payload {:?}",