From 6c85d17b8b0cf608307aee6952c06bd15c9690da Mon Sep 17 00:00:00 2001 From: Jin Jiu Date: Wed, 8 Jan 2025 17:20:57 +0800 Subject: [PATCH] Fixed an issue where post_config could not modify the core data structure (#99) * Add the validation of the storage keyword in the configuration file * Fixed an issue where post_config could not modify the core data structure --- src/cli/config.rs | 12 +++++++----- src/core.rs | 6 ++++-- src/handler.rs | 4 +--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/cli/config.rs b/src/cli/config.rs index 09a9efe7..7461b812 100644 --- a/src/cli/config.rs +++ b/src/cli/config.rs @@ -101,6 +101,8 @@ pub struct Storage { pub config: HashMap, } +static STORAGE_TYPE_KEYWORDS: &[&str] = &["file", "mysql"]; + fn default_bool_true() -> bool { true } @@ -182,11 +184,11 @@ where { let storage: HashMap = Deserialize::deserialize(deserializer)?; - // for key in storage.keys() { - // if key != "file" { - // return Err(serde::de::Error::custom("Invalid storage key")); - // } - // } + for key in storage.keys() { + if !STORAGE_TYPE_KEYWORDS.contains(&key.as_str()) { + return Err(serde::de::Error::custom("Invalid storage key")); + } + } Ok(storage) } diff --git a/src/core.rs b/src/core.rs index f43b284c..810e9ba3 100644 --- a/src/core.rs +++ b/src/core.rs @@ -135,9 +135,11 @@ impl Core { let cert_module = CertModule::new(self); self.module_manager.add_module(Arc::new(RwLock::new(Box::new(cert_module))))?; - let handlers = self.handlers.read()?; + let handlers = { + self.handlers.read()?.clone() + }; for handler in handlers.iter() { - match handler.post_config(Arc::clone(&core), config) { + match handler.post_config(self, config) { Ok(_) => { continue; } diff --git a/src/handler.rs b/src/handler.rs index d0acb712..618bac15 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -6,8 +6,6 @@ //! The `Handler` trait should be implemented in other module, such as the `rusty_vault::router` //! for instance. -use std::sync::{Arc, RwLock}; - use derive_more::Display; use async_trait::async_trait; @@ -22,7 +20,7 @@ use crate::{ pub trait Handler: Send + Sync { fn name(&self) -> String; - fn post_config(&self, _core: Arc>, _config: Option<&Config>) -> Result<(), RvError> { + fn post_config(&self, _core: &mut Core, _config: Option<&Config>) -> Result<(), RvError> { Err(RvError::ErrHandlerDefault) }