diff --git a/src/engines/kv.rs b/src/engines/kv.rs index 19afc4d..42717d4 100644 --- a/src/engines/kv.rs +++ b/src/engines/kv.rs @@ -1,8 +1,9 @@ // TODO: Remove #![allow(dead_code)] -pub mod logic; -pub mod structs; +// pub mod logic; // TODO: Remove or correct errors +pub mod http_structs; +pub mod db_structs; // #[cfg(test)] // mod tests; @@ -10,7 +11,7 @@ pub mod structs; use std::{collections::HashMap, convert::Infallible}; use crate::{ - engines::kv::{logic::body_to_json, structs::*}, + engines::kv::http_structs::*, storage::DatabaseDriver, }; use axum::{ @@ -19,6 +20,7 @@ use axum::{ routing::*, Json, Router, }; +use chrono::DateTime; use log::{info, error}; use sqlx::Row; @@ -68,10 +70,13 @@ async fn get_data( ("version_number".to_string(), version.to_string()), ("secret_path".to_string(), v.get("secret_path")), ]); - let return_secret = KvSecretReq { - data: secret_content, - options: None, - }; + let return_secret = KvSecretRes::new(KvSecretResData { + created_time: DateTime::parse_from_rfc3339(secret_content.get("created_time").unwrap_or(&"".to_string())).unwrap_or_default().to_utc(), // TODO + custom_metadata: None, + deletion_time: None, // TODO + destroyed: false, + version: version, + }); info!("{:?}", return_secret); Ok(Json(return_secret)) @@ -80,8 +85,9 @@ async fn get_data( sqlx::Error::RowNotFound => { error!("{:?}", e); let error_data: HashMap = HashMap::from([("error".to_string(), "Secret not found".to_string())]); - let error_secret = KvSecretReq{data: error_data, options: None}; - Ok(Json(error_secret)) + // let error_secret = KvSecretRes{data: error_data, options: None}; // TODO: Create Error struct instead, consider to create global error struct + // Ok(Json()) + todo!() }, _ => panic!("{:?}", e), }, @@ -187,8 +193,8 @@ async fn post_meta( Path((mount_path, kv_path)): Path<(String, String)>, body: String, ) -> &'static str { - let mut body_json = body_to_json(body); - let meta_data: SecretMeta = Default::default(); + // let mut body_json = body_to_json(body); + // let meta_data: SecretMeta = Default::default(); todo!("not implemented") } diff --git a/src/engines/kv/structs.rs b/src/engines/kv/db_structs.rs similarity index 59% rename from src/engines/kv/structs.rs rename to src/engines/kv/db_structs.rs index 90b847b..491654d 100644 --- a/src/engines/kv/structs.rs +++ b/src/engines/kv/db_structs.rs @@ -1,44 +1,18 @@ +use std::collections::HashMap; + use chrono::{DateTime, Utc}; -use serde::{Deserialize, Serialize}; -use zeroize::ZeroizeOnDrop; -use std::{collections::HashMap, vec}; +use log::*; -pub type KvSecretData = HashMap; - -#[derive(Serialize, Deserialize, Debug)] -pub struct KvSecretReq { - /// Map (required) - pub data: KvSecretData, - /// Map (optional), may contain `cas` integer - // #[serde_as(as = "serde_with::EnumMap")] - pub options: Option>, - // Version does not exist for create/update operations - // pub version: Option, - // TODO add all fields -} - -#[derive(Serialize, Debug)] -pub struct KvSecretResData { - pub created_time: DateTime, - pub custom_metadata: Option>, - pub deletion_time: Option>, - pub destroyed: bool, - pub version: i64, -} - -#[derive(Serialize, Debug)] -pub struct KvSecretRes { - pub data: KvSecretResData, -} - -#[derive(Serialize, Deserialize, Debug)] +#[derive(Debug)] +#[deprecated(note = "Add Req or Res respecively if AND move to http file if intended; remove deprecation once used")] pub struct VersionMeta { pub created_time: DateTime, pub deletion_time: Option>, // optional deletion time pub destroyed: bool, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Debug)] +#[deprecated(note = "Add Req or Res respecively if AND move to http file if intended; remove deprecation once used")] pub struct SecretMeta { pub cas_required: bool, pub created_time: DateTime, @@ -58,7 +32,11 @@ pub struct SecretMeta { } impl Default for SecretMeta { + /// Use [Serde field defaults](https://serde.rs/attr-default.html) instead + /// TODO: remove this function fn default() -> Self { + warn!("DEPRECATED: Default of SecretMeta was used, to be removed"); + let current = Utc::now(); SecretMeta { cas_required: false, @@ -72,4 +50,4 @@ impl Default for SecretMeta { versions: vec![], } } -} +} \ No newline at end of file diff --git a/src/engines/kv/http_structs.rs b/src/engines/kv/http_structs.rs new file mode 100644 index 0000000..026a89f --- /dev/null +++ b/src/engines/kv/http_structs.rs @@ -0,0 +1,44 @@ +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +pub type KvSecretData = HashMap; + +// This file contains structures for serializing HTTP Responses (Res) and deserializing Requests (Req) for the KV engine + +#[derive(Deserialize, Debug)] +/// HTTP Request to create or update a secret +pub struct KvSecretReq { + /// Map (required) + pub data: KvSecretData, + /// Map (optional), may contain `cas` integer + // #[serde_as(as = "serde_with::EnumMap")] + pub options: Option>, + // Version does not exist for create/update operations + // pub version: Option, + // TODO add all fields +} + +#[derive(Serialize, Debug)] +/// HTTP Response to creating or updating a secret +/// Contained by [`KvSecretRes`] +pub struct KvSecretResData { + pub created_time: DateTime, + pub custom_metadata: Option>, + pub deletion_time: Option>, + pub destroyed: bool, + pub version: i64, +} + +#[derive(Serialize, Debug)] +/// HTTP Response to creating or updating a secret +/// Container of [`KvSecretResData`] +pub struct KvSecretRes { + pub data: KvSecretResData, +} + +impl KvSecretRes { + pub fn new(data: KvSecretResData) -> Self { + KvSecretRes { data } + } +} diff --git a/src/engines/kv/logic.rs b/src/engines/kv/logic.rs index b9cfb96..6683a07 100644 --- a/src/engines/kv/logic.rs +++ b/src/engines/kv/logic.rs @@ -1,13 +1,14 @@ use serde_json::Value; -use super::structs::*; +use super::{db_structs::SecretMeta, http_structs::*}; // TODO create default function #[deprecated(note = "Use Axum functionality with structs instead, also, this should be inlined if it is actually needed")] /// serialize secret to JSON String pub fn serialize_secret_json(secret: &KvSecretReq) -> Result { - serde_json::to_string(&secret) + todo!() + // serde_json::to_string(&secret) } #[deprecated(note = "Use Axum functionality with structs instead, also, this should be inlined if it is actually needed")]