From 0730d711b541b284bad6fa5a97a319279943d595 Mon Sep 17 00:00:00 2001 From: someone Date: Sun, 28 Apr 2024 15:42:57 +0200 Subject: [PATCH] moved test request to engines.rs + extracted mountpath from request --- go_client/tests/secret_test.go | 2 +- src/engines.rs | 27 +++++++++++++++++++++++++-- src/main.rs | 29 ++++++----------------------- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/go_client/tests/secret_test.go b/go_client/tests/secret_test.go index ac3bff7..f5ab9c5 100644 --- a/go_client/tests/secret_test.go +++ b/go_client/tests/secret_test.go @@ -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), ) diff --git a/src/engines.rs b/src/engines.rs index 0066d21..f9efdaa 100644 --- a/src/engines.rs +++ b/src/engines.rs @@ -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, 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) -> &'static str { // TODO: Find a solution for this mess // async fn handler(request: Request) -> Result, StatusCode> { diff --git a/src/main.rs b/src/main.rs index a9266d8..7f42184 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 {:?}",