+ fix EVERYTHING

+ add versioning (only as counter)
+ get_secret from sled
= refactor serde conversion to JSON
This commit is contained in:
sam 2024-04-19 18:59:04 +02:00
parent 2c430758f1
commit a94a496b62
2 changed files with 64 additions and 21 deletions

View file

@ -16,14 +16,14 @@ mod tests {
let temp_secret = TempSecret { content: String::from("Hallo"), version: 12 };
let serialized = serialize_secret_json(&temp_secret);
println!("string serialized: {:?}", serialized);
let deserialized = deserialize_secret_struct(&serialized);
println!("Struct field from deserialized: {}", deserialized.content)
let deserialized = deserialize_secret_struct(&serialized.unwrap());
println!("Struct field from deserialized: {}", deserialized.unwrap().content)
}
}
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
pub struct TempSecret {
pub content: String,
pub version: i64
@ -35,11 +35,11 @@ pub struct TempSecret {
// }
/// serialize secret to JSON String
pub fn serialize_secret_json(secret: &TempSecret) -> String {
serde_json::to_string(&secret).unwrap()
pub fn serialize_secret_json(secret: &TempSecret) -> Result<String, serde_json::Error> {
serde_json::to_string(&secret)
}
/// deserialize JSON String to secret
pub fn deserialize_secret_struct(raw: &String) -> TempSecret{
serde_json::from_str(&raw).unwrap()
pub fn deserialize_secret_struct(raw: &String) -> Result<TempSecret, serde_json::Error>{
serde_json::from_str(&raw)
}

View file

@ -12,27 +12,45 @@ mod tests {
assert_eq!(result, 4);
}
#[test]
fn test_sled() {
fn test_update_secret() {
let db: sled::Db = sled::open("sled_db").unwrap();
update_secret(db, "foo", TempSecret{version: 1, content: "foo".to_string()});
update_secret(&db, "foo", TempSecret{version: -99, content: "cool".to_string()});
}
#[test]
fn test_get_secret() {
let db: sled::Db = sled::open("sled_db").unwrap();
get_secret(&db, "foo");
}
}
use sled::Db;
use base::{serialize_secret_json, TempSecret};
use base::{deserialize_secret_struct, serialize_secret_json, TempSecret};
/// [TODO] Currently no proper versioning
/// inserts a secret. If there was already a secret in the given path, the version is incremented
fn update_secret(db: Db, path: &str, mut secret: TempSecret) {
if let Ok(Some(_)) = db.get(path) { // Idiomatic way. Ok(Some(_)) is true when something was found
secret.version += 1;
print!("something was found \n")
fn update_secret(db: &Db, path: &str, mut secret: TempSecret) {
match get_secret(db, path) {
Some(old_secret) => {
// case secret found. TODO save it somewhere for versioning
secret.version = old_secret.version + 1;
#[cfg(test)]
print!("something was found. new version {} \n", secret.version)
}
None => {
// case new secret
secret.version = 1;
}
}
// if let secret_json = serialize_secret_json(&secret) {
// let _res = db.insert(path, secret_json); // maybe this can be handled cleaner
match serialize_secret_json(&secret) {
Ok(secret_json) => {
match db.insert(path, secret_json) {
#[cfg(test)]
println!("String: {:?}", secret_json.clone());
let as_ivec = sled::IVec::from(secret_json.into_bytes()); // maybe outsource this in a fn later
#[cfg(test)]
println!("ivec: {:?}", as_ivec);
match db.insert(path, as_ivec) {
Ok(_) => println!("Secret inserted"),
Err(e) => eprintln!("Failed to insert secret: {}", e),
}
@ -41,9 +59,34 @@ fn update_secret(db: Db, path: &str, mut secret: TempSecret) {
}
}
// /// read and return a secret from the DB
// /// if there is no secret, return None
// fn get_secret(db: Db, path: &str) -> Option<TempSecret>{
// let raw_secret = db.get(path);
// return None
// }
// read and return a secret from the DB
//if there is no secret, return None
fn get_secret(db: &Db, path: &str) -> Option<TempSecret>{
let raw_secret;
match db.get(path) {
Ok(Some(ivec)) => {
raw_secret = ivec;
}
Err(e) => {
eprintln!("Error on retrieving secret: {}", e);
return None;
}
Ok(None) => {
return None;
}
}
// outsource this in a fn later. TODO maybe deal with unwrap
let as_str = String::from_utf8(raw_secret.to_vec()).unwrap();
match deserialize_secret_struct(&as_str) {
Ok(secret) => {
#[cfg(test)]
println!("got some secret: {:?}", secret);
return Some(secret);
}
Err(e) => {
eprintln!("error on secret deserialization: {}", e);
return None;
}
}
}