2019-07-27 08:54:31 +02:00
|
|
|
use rand::prelude::*;
|
2019-09-14 10:21:41 +02:00
|
|
|
use std::fmt;
|
2019-07-15 01:15:25 +02:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2019-09-04 03:06:37 +02:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct IntegrationTestConfig {
|
|
|
|
pub admin_password: String,
|
|
|
|
}
|
|
|
|
|
2019-09-14 10:21:41 +02:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct TlsConfiguration {
|
|
|
|
pub ca: String,
|
|
|
|
pub cert: String,
|
|
|
|
pub key: String,
|
|
|
|
}
|
|
|
|
|
2020-01-09 11:07:14 +01:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Default)]
|
2018-11-26 07:13:22 +01:00
|
|
|
pub struct Configuration {
|
|
|
|
pub address: String,
|
|
|
|
pub threads: usize,
|
2019-07-27 08:54:31 +02:00
|
|
|
// db type later
|
2018-11-26 07:13:22 +01:00
|
|
|
pub db_path: String,
|
|
|
|
pub maximum_request: usize,
|
2019-04-18 03:28:33 +02:00
|
|
|
pub secure_cookies: bool,
|
2019-09-14 10:21:41 +02:00
|
|
|
pub tls_config: Option<TlsConfiguration>,
|
2019-07-27 08:54:31 +02:00
|
|
|
pub cookie_key: [u8; 32],
|
2019-09-04 03:06:37 +02:00
|
|
|
pub integration_test_config: Option<Box<IntegrationTestConfig>>,
|
2018-11-26 07:13:22 +01:00
|
|
|
}
|
|
|
|
|
2019-09-14 10:21:41 +02:00
|
|
|
impl fmt::Display for Configuration {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "address: {}, ", self.address)
|
|
|
|
.and_then(|_| write!(f, "thread count: {}, ", self.threads))
|
|
|
|
.and_then(|_| write!(f, "dbpath: {}, ", self.db_path))
|
|
|
|
.and_then(|_| write!(f, "max request size: {}b, ", self.maximum_request))
|
|
|
|
.and_then(|_| write!(f, "secure cookies: {}, ", self.secure_cookies))
|
|
|
|
.and_then(|_| write!(f, "with TLS: {}, ", self.tls_config.is_some()))
|
|
|
|
.and_then(|_| {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"integration mode: {}",
|
|
|
|
self.integration_test_config.is_some()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 07:13:22 +01:00
|
|
|
impl Configuration {
|
|
|
|
pub fn new() -> Self {
|
2019-07-27 08:54:31 +02:00
|
|
|
let mut c = Configuration {
|
2018-11-26 07:13:22 +01:00
|
|
|
address: String::from("127.0.0.1:8080"),
|
2019-09-14 10:21:41 +02:00
|
|
|
threads: num_cpus::get(),
|
2018-11-26 07:13:22 +01:00
|
|
|
db_path: String::from(""),
|
2020-01-09 11:07:14 +01:00
|
|
|
maximum_request: 262_144, // 256k
|
2019-04-18 03:28:33 +02:00
|
|
|
// log type
|
|
|
|
// log path
|
2019-07-27 08:54:31 +02:00
|
|
|
// TODO #63: default true in prd
|
2020-01-09 11:07:14 +01:00
|
|
|
secure_cookies: !cfg!(test),
|
2019-09-14 10:21:41 +02:00
|
|
|
tls_config: None,
|
2019-07-27 08:54:31 +02:00
|
|
|
cookie_key: [0; 32],
|
2019-09-04 03:06:37 +02:00
|
|
|
integration_test_config: None,
|
2019-07-27 08:54:31 +02:00
|
|
|
};
|
|
|
|
let mut rng = StdRng::from_entropy();
|
|
|
|
rng.fill(&mut c.cookie_key);
|
|
|
|
c
|
2018-11-26 07:13:22 +01:00
|
|
|
}
|
2019-07-15 01:15:25 +02:00
|
|
|
|
|
|
|
pub fn update_db_path(&mut self, p: &PathBuf) {
|
|
|
|
match p.to_str() {
|
|
|
|
Some(p) => self.db_path = p.to_string(),
|
|
|
|
None => {
|
|
|
|
error!("Invalid DB path supplied");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-14 10:21:41 +02:00
|
|
|
|
|
|
|
pub fn update_bind(&mut self, b: &Option<String>) {
|
|
|
|
self.address = b
|
|
|
|
.as_ref()
|
2020-01-09 11:07:14 +01:00
|
|
|
.cloned()
|
2019-09-14 10:21:41 +02:00
|
|
|
.unwrap_or_else(|| String::from("127.0.0.1:8080"));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_tls(
|
|
|
|
&mut self,
|
|
|
|
ca: &Option<PathBuf>,
|
|
|
|
cert: &Option<PathBuf>,
|
|
|
|
key: &Option<PathBuf>,
|
|
|
|
) {
|
|
|
|
match (ca, cert, key) {
|
|
|
|
(None, None, None) => {}
|
|
|
|
(Some(cap), Some(certp), Some(keyp)) => {
|
|
|
|
let cas = match cap.to_str() {
|
|
|
|
Some(cav) => cav.to_string(),
|
|
|
|
None => {
|
|
|
|
error!("Invalid CA path");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let certs = match certp.to_str() {
|
|
|
|
Some(certv) => certv.to_string(),
|
|
|
|
None => {
|
|
|
|
error!("Invalid Cert path");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let keys = match keyp.to_str() {
|
|
|
|
Some(keyv) => keyv.to_string(),
|
|
|
|
None => {
|
|
|
|
error!("Invalid Key path");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.tls_config = Some(TlsConfiguration {
|
|
|
|
ca: cas,
|
|
|
|
cert: certs,
|
|
|
|
key: keys,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
error!("Invalid TLS configuration - must provide ca, cert and key!");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-26 07:13:22 +01:00
|
|
|
}
|