From 373dd0f8c74bbc056feca67805297d198976ed84 Mon Sep 17 00:00:00 2001 From: itiB Date: Tue, 5 Apr 2022 01:52:44 +0900 Subject: [PATCH] Add: id, level validation --- src/filter.rs | 2 +- src/main.rs | 6 ++---- src/options/level_tuning.rs | 28 +++++++++++++++++++--------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/filter.rs b/src/filter.rs index 92293a62..0ce16e1a 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -10,7 +10,7 @@ use std::io::BufWriter; use std::io::{BufRead, BufReader}; lazy_static! { - static ref IDS_REGEX: Regex = + pub static ref IDS_REGEX: Regex = Regex::new(r"^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$").unwrap(); } diff --git a/src/main.rs b/src/main.rs index b7fda73d..cbbdf7a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,10 +120,8 @@ impl App { { if Path::new(level_tuning_config_path).exists() { if let Err(err) = LevelTuning::run(level_tuning_config_path) { - AlertMessage::alert( - &mut BufWriter::new(std::io::stderr().lock()), - &err, - ).ok(); + AlertMessage::alert(&mut BufWriter::new(std::io::stderr().lock()), &err) + .ok(); } } else { AlertMessage::alert( diff --git a/src/options/level_tuning.rs b/src/options/level_tuning.rs index d195ff42..88e14ab5 100644 --- a/src/options/level_tuning.rs +++ b/src/options/level_tuning.rs @@ -4,7 +4,6 @@ use crate::yaml::ParseYaml; use std::collections::HashMap; use std::fs::{self, File}; use std::io::Write; - pub struct LevelTuning {} impl LevelTuning { @@ -18,11 +17,26 @@ impl LevelTuning { let mut tuning_map: HashMap = HashMap::new(); read_result.unwrap().into_iter().try_for_each(|line| -> Result<(), String> { let id = match line.get(0) { - Some(_id) => _id, // TODO: Validate id + Some(_id) => { + if !filter::IDS_REGEX.is_match(_id) { + return Result::Err(format!("Failed to read level tuning file. {} is not correct id format, fix it.", _id)); + } + _id + } _ => return Result::Err("Failed to read id...".to_string()) }; let level = match line.get(1) { - Some(_level) => _level, // TODO: Validate level + Some(_level) => { + if _level.starts_with("informational") + || _level.starts_with("low") + || _level.starts_with("medium") + || _level.starts_with("high") + || _level.starts_with("critical") { + _level + } else { + return Result::Err("level tuning file's level must in informational, low, medium, high, critical".to_string()) + } + } _ => return Result::Err("Failed to read level...".to_string()) }; tuning_map.insert(id.to_string(), level.to_string()); @@ -71,16 +85,12 @@ impl LevelTuning { content = content.replace(&past_level, "level: critical"); } - let mut file = match File::options() - .write(true) - .truncate(true) - .open(&path) - { + let mut file = match File::options().write(true).truncate(true).open(&path) { Ok(file) => file, Err(e) => return Result::Err(e.to_string()), }; - file.write_all(content.as_bytes()).unwrap(); // TODO: use result + file.write_all(content.as_bytes()).unwrap(); println!( "level: {} -> {}", rule["level"].as_str().unwrap(),