CLI
This commit is contained in:
parent
5dc6ca05ce
commit
f5e4a421bb
7 changed files with 166 additions and 47 deletions
73
src/config.rs
Normal file
73
src/config.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
use crate::policy::{Action, Policy};
|
||||
|
||||
use std::{io::Read, net::SocketAddr, str::FromStr};
|
||||
|
||||
pub struct Config {
|
||||
pub listen_addr: SocketAddr,
|
||||
pub pass_addr: SocketAddr,
|
||||
pub default_action: Action,
|
||||
pub challenge_timeout: u64,
|
||||
pub policy_groups: Vec<Vec<Policy>>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn from_file(path: &str) -> Self {
|
||||
let mut config_file = std::fs::File::open(path).expect("Cannot open config file");
|
||||
let mut config_str = String::new();
|
||||
config_file
|
||||
.read_to_string(&mut config_str)
|
||||
.expect("Cannot read config file");
|
||||
let config_yaml = saphyr::Yaml::load_from_str(&config_str).expect("Error parsing config");
|
||||
let config_doc = &config_yaml[0];
|
||||
|
||||
Config {
|
||||
listen_addr: config_doc["listen"]
|
||||
.as_str()
|
||||
.expect("Missing listen address in config")
|
||||
.parse()
|
||||
.expect("Invalid listen address"),
|
||||
pass_addr: config_doc["pass"]
|
||||
.as_str()
|
||||
.expect("Missing pass address in config")
|
||||
.parse()
|
||||
.expect("Invalid pass address"),
|
||||
challenge_timeout: config_doc["challenge-timeout"]
|
||||
.as_i64()
|
||||
.expect("Missing challenge timeout in config") as u64,
|
||||
default_action: Action::from_str(
|
||||
config_doc["default-action"]
|
||||
.as_str()
|
||||
.expect("Missing default action in config"),
|
||||
)
|
||||
.expect("Invalid default action"),
|
||||
policy_groups: config_doc["policy-groups"]
|
||||
.as_vec()
|
||||
.expect("Missing policies in config")
|
||||
.iter()
|
||||
.map(|policy_group| {
|
||||
policy_group
|
||||
.as_vec()
|
||||
.expect("Missing policies in config")
|
||||
.iter()
|
||||
.map(|policy| Policy {
|
||||
name: policy["name"]
|
||||
.as_str()
|
||||
.expect("Expected policy name string")
|
||||
.to_string(),
|
||||
first_line_regex: policy["first-line"]
|
||||
.as_str()
|
||||
.expect("Expected policy first line regex string")
|
||||
.to_string(),
|
||||
action: Action::from_str(
|
||||
policy["action"]
|
||||
.as_str()
|
||||
.expect("Expected policy action string"),
|
||||
)
|
||||
.expect("Invalid policy action"),
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue