2020-12-12 13:58:04 +01:00
|
|
|
use std::default::Default;
|
2021-09-19 19:47:29 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-09-15 14:31:10 +02:00
|
|
|
use std::collections::HashMap;
|
2020-02-18 16:55:19 +01:00
|
|
|
|
2021-10-20 12:58:05 +02:00
|
|
|
use serde::{Serialize, Deserialize, Deserializer, Serializer};
|
2020-09-15 14:31:10 +02:00
|
|
|
|
2021-10-20 12:58:05 +02:00
|
|
|
use std::fmt::Formatter;
|
|
|
|
use std::net::{SocketAddr, IpAddr, ToSocketAddrs};
|
|
|
|
use std::str::FromStr;
|
|
|
|
use serde::de::Error;
|
2022-03-13 20:14:50 +01:00
|
|
|
use crate::authorization::permissions::{PermRule, PrivilegesBuf};
|
2022-03-15 19:14:04 +01:00
|
|
|
use crate::authorization::roles::Role;
|
2020-09-15 14:31:10 +02:00
|
|
|
|
2021-10-27 14:49:45 +02:00
|
|
|
type Result<T> = std::result::Result<T, serde_dhall::Error>;
|
|
|
|
|
2020-12-12 13:58:04 +01:00
|
|
|
pub fn read(path: &Path) -> Result<Config> {
|
2020-12-14 11:02:46 +01:00
|
|
|
serde_dhall::from_file(path)
|
|
|
|
.parse()
|
|
|
|
.map_err(Into::into)
|
2020-02-14 12:20:17 +01:00
|
|
|
}
|
|
|
|
|
2022-03-13 20:14:50 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
/// A description of a machine
|
|
|
|
///
|
|
|
|
/// This is the struct that a machine is serialized to/from.
|
|
|
|
/// Combining this with the actual state of the system will return a machine
|
|
|
|
pub struct MachineDescription {
|
|
|
|
/// The name of the machine. Doesn't need to be unique but is what humans will be presented.
|
|
|
|
pub name: String,
|
|
|
|
|
|
|
|
/// An optional description of the Machine.
|
|
|
|
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "deser_option")]
|
|
|
|
pub description: Option<String>,
|
|
|
|
|
|
|
|
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "deser_option")]
|
|
|
|
pub wiki: Option<String>,
|
|
|
|
|
|
|
|
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "deser_option")]
|
|
|
|
pub category: Option<String>,
|
|
|
|
|
|
|
|
/// The permission required
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub privs: PrivilegesBuf,
|
|
|
|
}
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2020-12-12 13:58:04 +01:00
|
|
|
pub struct Config {
|
|
|
|
/// A list of address/port pairs to listen on.
|
2021-10-20 12:58:05 +02:00
|
|
|
pub listens: Vec<Listen>,
|
2020-02-14 12:20:17 +01:00
|
|
|
|
2020-12-12 13:58:04 +01:00
|
|
|
/// Machine descriptions to load
|
2022-03-13 20:14:50 +01:00
|
|
|
pub machines: HashMap<String, MachineDescription>,
|
2020-12-12 13:58:04 +01:00
|
|
|
|
2020-12-14 11:02:46 +01:00
|
|
|
/// Actors to load and their configuration options
|
|
|
|
pub actors: HashMap<String, ModuleConfig>,
|
|
|
|
|
|
|
|
/// Initiators to load and their configuration options
|
|
|
|
pub initiators: HashMap<String, ModuleConfig>,
|
|
|
|
|
|
|
|
pub mqtt_url: String,
|
2020-12-14 12:39:01 +01:00
|
|
|
|
2021-10-27 21:32:50 +02:00
|
|
|
pub actor_connections: Vec<(String, String)>,
|
|
|
|
pub init_connections: Vec<(String, String)>,
|
2021-09-19 19:47:29 +02:00
|
|
|
|
|
|
|
pub db_path: PathBuf,
|
2022-03-12 01:27:41 +01:00
|
|
|
pub auditlog_path: PathBuf,
|
2021-09-19 22:53:43 +02:00
|
|
|
|
2022-03-15 19:14:04 +01:00
|
|
|
pub roles: HashMap<String, Role>,
|
2022-03-11 22:13:54 +01:00
|
|
|
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub tlsconfig: TlsListen,
|
|
|
|
|
|
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
|
|
pub tlskeylog: Option<PathBuf>,
|
|
|
|
|
|
|
|
#[serde(default, skip)]
|
|
|
|
pub verbosity: isize,
|
|
|
|
|
|
|
|
#[serde(default, skip)]
|
|
|
|
pub log_format: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
pub fn is_quiet(&self) -> bool {
|
|
|
|
self.verbosity < 0
|
|
|
|
}
|
2021-09-19 22:53:43 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 11:02:46 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct ModuleConfig {
|
2020-12-14 12:39:01 +01:00
|
|
|
pub module: String,
|
2020-12-14 11:02:46 +01:00
|
|
|
pub params: HashMap<String, String>
|
|
|
|
}
|
|
|
|
|
2022-03-11 22:13:54 +01:00
|
|
|
pub struct ListenSock {
|
|
|
|
listen: Listen,
|
|
|
|
tls_config: TlsListen,
|
|
|
|
}
|
|
|
|
|
2022-03-11 22:17:51 +01:00
|
|
|
|
|
|
|
pub(crate) fn deser_option<'de, D, T>(d: D) -> std::result::Result<Option<T>, D::Error>
|
|
|
|
where D: serde::Deserializer<'de>, T: serde::Deserialize<'de>,
|
|
|
|
{
|
|
|
|
Ok(T::deserialize(d).ok())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-10-20 12:58:05 +02:00
|
|
|
pub struct Listen {
|
|
|
|
address: String,
|
2022-03-11 22:17:51 +01:00
|
|
|
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "deser_option")]
|
2021-10-20 12:58:05 +02:00
|
|
|
port: Option<u16>,
|
|
|
|
}
|
|
|
|
|
2022-03-11 22:13:54 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
|
|
|
pub struct TlsListen {
|
|
|
|
pub certfile: PathBuf,
|
|
|
|
pub keyfile: PathBuf,
|
|
|
|
|
|
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
|
|
pub ciphers: Option<String>,
|
|
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
|
|
pub tls_min_version: Option<String>,
|
|
|
|
#[serde(default = "Vec::new", skip_serializing_if = "Vec::is_empty")]
|
|
|
|
pub protocols: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Listen {
|
|
|
|
pub fn to_tuple(&self) -> (&str, u16) {
|
|
|
|
(self.address.as_str(), self.port.unwrap_or(DEFAULT_PORT))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:58:05 +02:00
|
|
|
impl std::fmt::Display for Listen {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}:{}", &self.address, self.port.unwrap_or(DEFAULT_PORT))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToSocketAddrs for Listen {
|
|
|
|
type Iter = <(String, u16) as ToSocketAddrs>::Iter;
|
|
|
|
|
|
|
|
fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
|
|
|
|
if let Some(port) = self.port {
|
|
|
|
(self.address.as_str(), port).to_socket_addrs()
|
|
|
|
} else {
|
|
|
|
(self.address.as_str(), DEFAULT_PORT).to_socket_addrs()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 11:02:46 +01:00
|
|
|
impl Default for Config {
|
2020-02-18 16:55:19 +01:00
|
|
|
fn default() -> Self {
|
2020-12-14 11:02:46 +01:00
|
|
|
let mut actors: HashMap::<String, ModuleConfig> = HashMap::new();
|
|
|
|
let mut initiators: HashMap::<String, ModuleConfig> = HashMap::new();
|
2022-03-13 20:14:50 +01:00
|
|
|
let mut machines = HashMap::new();
|
2020-12-14 11:02:46 +01:00
|
|
|
|
|
|
|
actors.insert("Actor".to_string(), ModuleConfig {
|
2020-12-14 12:39:01 +01:00
|
|
|
module: "Shelly".to_string(),
|
2020-12-14 11:02:46 +01:00
|
|
|
params: HashMap::new(),
|
|
|
|
});
|
|
|
|
initiators.insert("Initiator".to_string(), ModuleConfig {
|
2020-12-14 12:39:01 +01:00
|
|
|
module: "TCP-Listen".to_string(),
|
2020-12-14 11:02:46 +01:00
|
|
|
params: HashMap::new(),
|
|
|
|
});
|
|
|
|
|
2020-12-12 13:58:04 +01:00
|
|
|
Config {
|
2021-10-20 12:58:05 +02:00
|
|
|
listens: vec![
|
2020-12-14 11:02:46 +01:00
|
|
|
Listen {
|
2021-10-20 12:58:05 +02:00
|
|
|
address: "127.0.0.1".to_string(),
|
|
|
|
port: None,
|
2020-12-14 11:02:46 +01:00
|
|
|
}
|
2021-10-20 12:58:05 +02:00
|
|
|
],
|
2021-09-19 19:47:29 +02:00
|
|
|
actors,
|
|
|
|
initiators,
|
2022-03-13 20:14:50 +01:00
|
|
|
machines,
|
2020-12-14 11:02:46 +01:00
|
|
|
mqtt_url: "tcp://localhost:1883".to_string(),
|
2021-10-27 21:32:50 +02:00
|
|
|
actor_connections: vec![
|
2020-12-14 12:39:01 +01:00
|
|
|
("Testmachine".to_string(), "Actor".to_string()),
|
2021-10-27 21:32:50 +02:00
|
|
|
],
|
|
|
|
init_connections: vec![
|
2020-12-14 12:39:01 +01:00
|
|
|
("Initiator".to_string(), "Testmachine".to_string()),
|
2021-10-27 21:32:50 +02:00
|
|
|
],
|
2021-09-19 19:47:29 +02:00
|
|
|
|
|
|
|
db_path: PathBuf::from("/run/bffh/database"),
|
2022-03-12 01:27:41 +01:00
|
|
|
auditlog_path: PathBuf::from("/var/log/bffh/audit.log"),
|
2021-09-19 22:53:43 +02:00
|
|
|
roles: HashMap::new(),
|
2022-03-11 22:13:54 +01:00
|
|
|
|
|
|
|
tlsconfig: TlsListen {
|
|
|
|
certfile: PathBuf::from("./bffh.crt"),
|
|
|
|
keyfile: PathBuf::from("./bffh.key"),
|
|
|
|
.. Default::default()
|
|
|
|
},
|
|
|
|
|
|
|
|
tlskeylog: None,
|
|
|
|
verbosity: 0,
|
|
|
|
log_format: "Full".to_string(),
|
2020-02-18 16:55:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The default port in the non-assignable i.e. free-use area
|
|
|
|
pub const DEFAULT_PORT: u16 = 59661;
|