use axum::{ body::Body, http::{Response, StatusCode}, response::IntoResponse, }; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use zeroize::Zeroize; pub type KvSecretData = HashMap; // This file contains structures for serializing HTTP Responses (Res) and deserializing Requests (Req) for the KV engine #[derive(Deserialize, Debug)] // #[zeroize(drop)] /// 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, } impl Zeroize for KvSecretReq { fn zeroize(&mut self) { // Zero out each field individually self.data = HashMap::new(); self.options = None; } } impl Drop for KvSecretReq { fn drop(&mut self) { self.zeroize(); } } #[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 } } } #[derive(Serialize)] pub struct ErrorStruct { pub err: String, } impl ErrorStruct { pub fn into_response(self) -> Response { let body = self.err; (StatusCode::NOT_FOUND, body).into_response() } } #[derive(Deserialize)] /// HTTP Request to destroy secret versions pub struct KvSecretDestrReq { pub versions: Vec, } #[derive(Serialize, Debug)] /// HTTP Response to Reading a Secret metadata /// Container of [`KvMetaResData`] pub struct KvMetaRes { pub data: KvMetaResData, } impl Default for KvMetaRes { fn default() -> Self { let now = Utc::now(); Self { data: KvMetaResData { cas_required: false, created_time: now, delete_version_after: Some("".to_string()), max_versions: 0, updated_time: now, custom_metadata: Some(HashMap::new()), current_version: 0, oldest_version: 0, versions: HashMap::new(), }, } } } #[derive(Serialize, Debug)] /// Metadata concerning a specific secret version /// contained by [KvMetaRes] pub struct KvMetaResVersionData { pub created_time: DateTime, pub deletion_time: DateTime, pub destroyed: bool, } #[derive(Serialize, Debug)] /// contained by [KvMetaRes] pub struct KvMetaResData { pub cas_required: bool, pub created_time: DateTime, pub current_version: i64, pub delete_version_after: Option, pub max_versions: i64, pub oldest_version: i64, pub updated_time: DateTime, pub custom_metadata: Option>, pub versions: HashMap, // here, the key to a version is the version number } #[derive(Serialize, Debug, Deserialize)] /// HTTP Request to post metadatas pub struct KvMetaReq { pub cas_required: Option, // pub cas_required: bool, pub delete_version_after: Option, pub max_versions: i64, // pub updated_time: Option>, pub custom_metadata: Option>, }