143 lines
3.7 KiB
Rust
143 lines
3.7 KiB
Rust
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<String, String>;
|
|
|
|
// 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<HashMap<String, String>>,
|
|
// Version does not exist for create/update operations
|
|
// pub version: Option<i64>,
|
|
}
|
|
|
|
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<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 }
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct ErrorStruct {
|
|
pub err: String,
|
|
}
|
|
impl ErrorStruct {
|
|
pub fn into_response(self) -> Response<Body> {
|
|
let body = self.err;
|
|
(StatusCode::NOT_FOUND, body).into_response()
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
/// HTTP Request to destroy secret versions
|
|
pub struct KvSecretDestrReq {
|
|
pub versions: Vec<i64>,
|
|
}
|
|
|
|
#[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<Utc>,
|
|
pub deletion_time: DateTime<Utc>,
|
|
pub destroyed: bool,
|
|
}
|
|
|
|
#[derive(Serialize, Debug)]
|
|
/// contained by [KvMetaRes]
|
|
pub struct KvMetaResData {
|
|
pub cas_required: bool,
|
|
pub created_time: DateTime<Utc>,
|
|
pub current_version: i64,
|
|
pub delete_version_after: Option<String>,
|
|
pub max_versions: i64,
|
|
pub oldest_version: i64,
|
|
pub updated_time: DateTime<Utc>,
|
|
pub custom_metadata: Option<HashMap<String, String>>,
|
|
pub versions: HashMap<i64, KvMetaResVersionData>,
|
|
// 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<bool>,
|
|
// pub cas_required: bool,
|
|
pub delete_version_after: Option<String>,
|
|
pub max_versions: i64,
|
|
// pub updated_time: Option<DateTime<Utc>>,
|
|
pub custom_metadata: Option<HashMap<String, String>>,
|
|
}
|