post_secret
Changes to be committed: modified: go_client/tests/secret_test.go modified: migrations/20240506135416_testdata.sql modified: src/engines/kv.rs
This commit is contained in:
parent
a6de2133fd
commit
eab776a8d1
3 changed files with 39 additions and 13 deletions
|
|
@ -66,7 +66,7 @@ func TestWriteSecret(t *testing.T) {
|
|||
if err != nil {
|
||||
log.Fatal("kv2: Failed to write secret:\n\t", err)
|
||||
}
|
||||
log.Println("kv2: Tried to write Secret at foo at mountpath: ", mountpath)
|
||||
log.Println("kv2: Tried to write Secret at bar at mountpath: ", mountpath)
|
||||
}
|
||||
|
||||
func TestWriteSecret2(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -4,3 +4,5 @@ INSERT INTO metadata VALUES ("bar", false, DateTime('now'), "30d", 4, DateTime('
|
|||
|
||||
INSERT INTO secret_versions VALUES ("secret_data", DateTime('now'), DateTime('now'), 1, "bar");
|
||||
INSERT INTO secret_versions VALUES ("more_secret_data", DateTime('now'), datetime('now', '+30 day'), 2, "bar");
|
||||
|
||||
INSERT INTO metadata VALUES ("foo", false, DateTime('now'), "30d", 4, DateTime('now'), '{"foo": "customData"}');
|
||||
|
|
@ -24,7 +24,7 @@ use chrono::{DateTime, Utc};
|
|||
use db_structs::*;
|
||||
use log::{error, info};
|
||||
use serde_json;
|
||||
use sqlx::{error, Row};
|
||||
use sqlx::Row;
|
||||
use std::{collections::HashMap, convert::Infallible};
|
||||
|
||||
pub fn kv_router(pool: DatabaseDriver) -> Router {
|
||||
|
|
@ -108,7 +108,7 @@ async fn post_data(
|
|||
State(pool): State<DatabaseDriver>,
|
||||
Path(path): Path<String>,
|
||||
extract::Json(payload): extract::Json<KvSecretReq>,
|
||||
) -> &'static str {
|
||||
) -> Result<impl IntoResponse, Infallible> {
|
||||
// Insert Metadata first -> Else: Error because of foreign key constraint
|
||||
|
||||
log::debug!(
|
||||
|
|
@ -118,30 +118,54 @@ async fn post_data(
|
|||
payload.options,
|
||||
path
|
||||
);
|
||||
|
||||
let existing_secrets = sqlx::query("SELECT * FROM secret_versions WHERE secret_path = $1")
|
||||
.bind(&path)
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut version: i64 = existing_secrets.len().try_into().unwrap();
|
||||
version += 1;
|
||||
let data = serde_json::to_string(&payload.data).unwrap();
|
||||
log::debug!("Received data: {:?}", data);
|
||||
let created_time = Utc::now().to_string();
|
||||
let created_time = Utc::now();
|
||||
let created_time_string = created_time.to_string();
|
||||
let deletion_time = "12-12-2024 12:00:00"; // TODO:
|
||||
let version = "1";
|
||||
match sqlx::query("INSERT INTO secret_versions VALUES ($1, $2, $3, $4, $5)")
|
||||
.bind(data)
|
||||
.bind(created_time)
|
||||
.bind(&data)
|
||||
.bind(created_time_string)
|
||||
.bind(deletion_time)
|
||||
.bind(version)
|
||||
.bind(path)
|
||||
.bind(&version)
|
||||
.bind(&path)
|
||||
.execute(&pool)
|
||||
.await
|
||||
{
|
||||
Ok(v) => {
|
||||
info!("{:?}", v);
|
||||
|
||||
// match sqlx::query("SELECT custom_data FROM metadata where path = $1").bind(&path).fetch_one(&pool).await{
|
||||
// Ok(v)=> {
|
||||
// // let meta = v.get("custom_data"); // TODO: get metadata from query
|
||||
|
||||
// }
|
||||
// Err(e) => {
|
||||
// panic!("Couldn't find metadata");
|
||||
// }
|
||||
// }
|
||||
let return_struct = KvSecretResData {
|
||||
created_time : created_time,
|
||||
custom_metadata : None,
|
||||
deletion_time : None,
|
||||
destroyed : false,
|
||||
version : version
|
||||
};
|
||||
return Ok((StatusCode::OK, Json(return_struct)).into_response());
|
||||
|
||||
}
|
||||
Err(e) => {
|
||||
error!("{:?}", e);
|
||||
return Ok((StatusCode::INTERNAL_SERVER_ERROR, Json(e.to_string())).into_response());
|
||||
}
|
||||
}
|
||||
|
||||
todo!("not implemented")
|
||||
}
|
||||
|
||||
/* mock for return
|
||||
|
|
|
|||
Loading…
Reference in a new issue