KV engine changes:

- Split structs into HTTP Requests/Responses and database objects
- Deprectate direct json interactions
This commit is contained in:
Laurenz 2024-05-08 17:37:10 +02:00
parent 18b2521a93
commit c97c09cde5
4 changed files with 76 additions and 47 deletions

View file

@ -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<String, String> = 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")
}

View file

@ -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<String, String>;
#[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<HashMap<String, String>>,
// Version does not exist for create/update operations
// pub version: Option<i64>,
// TODO add all fields
}
#[derive(Serialize, Debug)]
pub struct KvSecretResData {
pub created_time: DateTime<Utc>,
pub custom_metadata: Option<HashMap<String, String>>,
pub deletion_time: Option<DateTime<Utc>>,
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<Utc>,
pub deletion_time: Option<DateTime<Utc>>, // 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<Utc>,
@ -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,

View file

@ -0,0 +1,44 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub type KvSecretData = HashMap<String, String>;
// 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<HashMap<String, String>>,
// Version does not exist for create/update operations
// pub version: Option<i64>,
// 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<Utc>,
pub custom_metadata: Option<HashMap<String, String>>,
pub deletion_time: Option<DateTime<Utc>>,
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 }
}
}

View file

@ -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<String, serde_json::Error> {
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")]