minos_codex/config/
mod.rs1use crate::secret_type::SecretType;
2use crate::MinosCodexError;
3use rust_embed::RustEmbed;
4use serde::Deserialize;
5use std::collections::HashMap;
6use std::str::Utf8Error;
7
8impl From<Utf8Error> for MinosCodexError {
9 fn from(err: Utf8Error) -> Self {
10 MinosCodexError::ConfigLoadError(std::io::Error::new(std::io::ErrorKind::InvalidData, err))
11 }
12}
13
14#[derive(Deserialize)]
15struct SecretTypeWrapper {
16 secret_type: SecretType,
17}
18
19#[derive(RustEmbed)]
20#[folder = "detections/"]
21struct ConfigAssets;
22
23pub struct Config {
24 secret_types: HashMap<String, SecretType>,
25}
26
27impl Config {
28 pub fn new() -> Self {
29 Config {
30 secret_types: HashMap::new(),
31 }
32 }
33
34 pub fn load_from_embedded() -> Result<Self, MinosCodexError> {
35 let mut config = Config::new();
36
37 for file in ConfigAssets::iter() {
38 if file.ends_with(".toml") {
39 let file_data = ConfigAssets::get(&file).ok_or_else(|| {
40 MinosCodexError::ConfigLoadError(std::io::Error::new(
41 std::io::ErrorKind::NotFound,
42 "File not found",
43 ))
44 })?;
45 let contents = std::str::from_utf8(file_data.data.as_ref())?;
46 let secret_type = Config::load_secret_type_from_data(contents)?;
47 config
48 .secret_types
49 .insert(secret_type.name.clone(), secret_type);
50 }
51 }
52
53 Ok(config)
54 }
55
56 fn load_secret_type_from_data(data: &str) -> Result<SecretType, MinosCodexError> {
57 let wrapper: SecretTypeWrapper = toml::from_str(data)?;
58 wrapper
59 .secret_type
60 .validate()
61 .map_err(MinosCodexError::ValidationError)?;
62 Ok(wrapper.secret_type)
63 }
64
65 pub fn get_secret_type(&self, name: &str) -> Option<&SecretType> {
66 self.secret_types.get(name)
67 }
68
69 pub fn secret_types(&self) -> impl Iterator<Item = &SecretType> {
70 self.secret_types.values()
71 }
72}