2021-06-02 01:42:40 +02:00
|
|
|
//! The server configuration as processed from the startup wrapper. This controls a number of
|
|
|
|
//! variables that determine how our backends, query server, and frontends are configured.
|
|
|
|
//!
|
|
|
|
//! These components should be "per server". Any "per domain" config should be in the system
|
|
|
|
//! or domain entries that are able to be replicated.
|
|
|
|
|
2019-07-27 08:54:31 +02:00
|
|
|
use rand::prelude::*;
|
2019-09-14 10:21:41 +02:00
|
|
|
use std::fmt;
|
2021-05-06 12:58:22 +02:00
|
|
|
use std::str::FromStr;
|
2019-07-15 01:15:25 +02:00
|
|
|
|
2019-09-04 03:06:37 +02:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct IntegrationTestConfig {
|
2021-04-25 03:35:56 +02:00
|
|
|
pub admin_user: String,
|
2019-09-04 03:06:37 +02:00
|
|
|
pub admin_password: String,
|
|
|
|
}
|
|
|
|
|
2019-09-14 10:21:41 +02:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct TlsConfiguration {
|
2021-02-16 02:40:25 +01:00
|
|
|
pub chain: String,
|
2019-09-14 10:21:41 +02:00
|
|
|
pub key: String,
|
|
|
|
}
|
|
|
|
|
2021-05-06 12:58:22 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
|
|
|
|
pub enum ServerRole {
|
|
|
|
WriteReplica,
|
|
|
|
WriteReplicaNoUI,
|
|
|
|
ReadOnlyReplica,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ServerRole {
|
|
|
|
fn default() -> Self {
|
|
|
|
ServerRole::WriteReplica
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 07:00:08 +02:00
|
|
|
impl ToString for ServerRole {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match self {
|
|
|
|
ServerRole::WriteReplica => "write replica".to_string(),
|
|
|
|
ServerRole::WriteReplicaNoUI => "write replica (no ui)".to_string(),
|
|
|
|
ServerRole::ReadOnlyReplica => "read only replica".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 12:58:22 +02:00
|
|
|
impl FromStr for ServerRole {
|
|
|
|
type Err = &'static str;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
"write_replica" => Ok(ServerRole::WriteReplica),
|
|
|
|
"write_replica_no_ui" => Ok(ServerRole::WriteReplicaNoUI),
|
|
|
|
"read_only_replica" => Ok(ServerRole::ReadOnlyReplica),
|
|
|
|
_ => Err("Must be one of write_replica, write_replica_no_ui, read_only_replica"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2020-06-10 04:07:43 +02:00
|
|
|
pub ldapaddress: Option<String>,
|
2018-11-26 07:13:22 +01:00
|
|
|
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,
|
2020-08-04 08:52:57 +02:00
|
|
|
pub db_fs_type: Option<String>,
|
2021-04-14 01:56:40 +02:00
|
|
|
pub db_arc_size: Option<usize>,
|
2018-11-26 07:13:22 +01:00
|
|
|
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>>,
|
2020-06-18 02:30:42 +02:00
|
|
|
pub log_level: Option<u32>,
|
2020-12-02 02:12:07 +01:00
|
|
|
pub origin: String,
|
2021-05-06 12:58:22 +02:00
|
|
|
pub role: ServerRole,
|
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)
|
2020-06-10 04:07:43 +02:00
|
|
|
.and_then(|_| match &self.ldapaddress {
|
|
|
|
Some(la) => write!(f, "ldap address: {}, ", la),
|
|
|
|
None => write!(f, "ldap address: disabled, "),
|
|
|
|
})
|
2019-09-14 10:21:41 +02:00
|
|
|
.and_then(|_| write!(f, "thread count: {}, ", self.threads))
|
|
|
|
.and_then(|_| write!(f, "dbpath: {}, ", self.db_path))
|
2021-04-14 01:56:40 +02:00
|
|
|
.and_then(|_| match self.db_arc_size {
|
|
|
|
Some(v) => write!(f, "arcsize: {}, ", v),
|
|
|
|
None => write!(f, "arcsize: AUTO, "),
|
|
|
|
})
|
2019-09-14 10:21:41 +02:00
|
|
|
.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()))
|
2020-06-21 13:57:48 +02:00
|
|
|
.and_then(|_| match self.log_level {
|
|
|
|
Some(u) => write!(f, "with log_level: {:x}, ", u),
|
|
|
|
None => write!(f, "with log_level: default, "),
|
|
|
|
})
|
2021-07-24 07:00:08 +02:00
|
|
|
.and_then(|_| write!(f, "role: {}, ", self.role.to_string()))
|
2019-09-14 10:21:41 +02:00
|
|
|
.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"),
|
2020-06-10 04:07:43 +02:00
|
|
|
ldapaddress: None,
|
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-08-04 08:52:57 +02:00
|
|
|
db_fs_type: None,
|
2021-04-14 01:56:40 +02:00
|
|
|
db_arc_size: None,
|
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,
|
2020-06-18 02:30:42 +02:00
|
|
|
log_level: None,
|
2020-12-02 02:12:07 +01:00
|
|
|
origin: "https://idm.example.com".to_string(),
|
2021-05-06 12:58:22 +02:00
|
|
|
role: ServerRole::WriteReplica,
|
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
|
|
|
|
2020-06-18 02:30:42 +02:00
|
|
|
pub fn update_log_level(&mut self, log_level: Option<u32>) {
|
|
|
|
self.log_level = log_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_db_path(&mut self, p: &str) {
|
|
|
|
self.db_path = p.to_string();
|
2019-07-15 01:15:25 +02:00
|
|
|
}
|
2020-08-04 08:52:57 +02:00
|
|
|
|
2021-04-14 01:56:40 +02:00
|
|
|
pub fn update_db_arc_size(&mut self, v: Option<usize>) {
|
|
|
|
self.db_arc_size = v
|
|
|
|
}
|
|
|
|
|
2020-08-04 08:52:57 +02:00
|
|
|
pub fn update_db_fs_type(&mut self, p: &Option<String>) {
|
|
|
|
self.db_fs_type = p.as_ref().map(|v| v.to_lowercase());
|
|
|
|
}
|
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"));
|
|
|
|
}
|
|
|
|
|
2020-06-10 04:07:43 +02:00
|
|
|
pub fn update_ldapbind(&mut self, l: &Option<String>) {
|
|
|
|
self.ldapaddress = l.clone();
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:12:07 +01:00
|
|
|
pub fn update_origin(&mut self, o: &str) {
|
|
|
|
self.origin = o.to_string();
|
|
|
|
}
|
|
|
|
|
2021-05-06 12:58:22 +02:00
|
|
|
pub fn update_role(&mut self, r: ServerRole) {
|
|
|
|
self.role = r;
|
|
|
|
}
|
|
|
|
|
2021-02-16 02:40:25 +01:00
|
|
|
pub fn update_tls(&mut self, chain: &Option<String>, key: &Option<String>) {
|
|
|
|
match (chain, key) {
|
|
|
|
(None, None) => {}
|
|
|
|
(Some(chainp), Some(keyp)) => {
|
|
|
|
let chain = chainp.to_string();
|
|
|
|
let key = keyp.to_string();
|
|
|
|
self.tls_config = Some(TlsConfiguration { chain, key })
|
2019-09-14 10:21:41 +02:00
|
|
|
}
|
|
|
|
_ => {
|
2021-02-16 02:40:25 +01:00
|
|
|
eprintln!("ERROR: Invalid TLS configuration - must provide chain and key!");
|
2019-09-14 10:21:41 +02:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-26 07:13:22 +01:00
|
|
|
}
|