From de51b58a291acfccba9bb8bea6924ddfc707e21b Mon Sep 17 00:00:00 2001 From: raimond visser Date: Sun, 14 Jan 2024 12:35:08 +0100 Subject: [PATCH] adding new files --- .cargo/config.toml | 0 src/server_config.rs | 122 +++++++++++++++++++++++++++++++++++++++++++ src/test_server.rs | 0 tests/test_server.rs | 0 4 files changed, 122 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 src/server_config.rs create mode 100644 src/test_server.rs create mode 100644 tests/test_server.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..e69de29 diff --git a/src/server_config.rs b/src/server_config.rs new file mode 100644 index 0000000..7a1e05f --- /dev/null +++ b/src/server_config.rs @@ -0,0 +1,122 @@ +use anyhow::{Context, Result}; +use serde_derive::{Deserialize, Serialize}; +use std::fs; +use std::path::PathBuf; + +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct ServerConfig { + pub server: Server, + pub repos: Repos, + pub tls: Option, + pub authorization: Authorization, + pub accesscontrol: AccessControl, +} + +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct Repos { + pub storage_path: String, +} + +// This assumes that it makes no sense to have one but not the other +// So we if acl_path is given, we require the auth_path too. +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct AccessControl { + pub acl_path: Option, + //if not private all repo are accessible for any user + pub private_repo: bool, + //force access to append only for all + pub append_only: bool, +} + +// This assumes that it makes no sense to have one but not the other +// So we if acl_path is given, we require the auth_path too. +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct Authorization { + pub auth_path: Option, + //use authorization file + pub use_auth: bool, +} + +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct Server { + pub host_dns_name: String, + pub port: usize, + //HTTP, or HTTPS + pub protocol: String, +} + +#[derive(Clone, Serialize, Deserialize, Debug)] +pub struct TLS { + pub key_path: String, + pub cert_path: String, +} + +impl ServerConfig { + pub fn from_file(pth: &PathBuf) -> Result { + let s = fs::read_to_string(&pth).context("Can not read server configuration file")?; + let config: ServerConfig = + toml::from_str(&s).context("Can not convert file to server configuration")?; + Ok(config) + } + + pub fn to_file(&self, pth: &PathBuf) -> Result<()> { + let toml_string = + toml::to_string(&self).context("Could not serialize SeverConfig to TOML value")?; + fs::write(&pth, toml_string).context("Could not write ServerConfig to file!")?; + Ok(()) + } +} + +#[cfg(test)] +mod test { + use super::Server; + use crate::config::server_config::{AccessControl, Authorization, Repos, ServerConfig, TLS}; + use std::fs; + use std::path::Path; + + #[test] + fn test_server_config() { + let server_path = Path::new("tmp_test_data").join("rustic"); + fs::create_dir_all(&server_path).unwrap(); + + let server = Server { + host_dns_name: "127.0.0.1".to_string(), + port: 2222, + protocol: "HTTP".to_string(), + }; + + let tls: Option = Some(TLS { + key_path: "somewhere".to_string(), + cert_path: "somewhere/else".to_string(), + }); + + let repos: Repos = Repos { + storage_path: server_path.join("repos").to_string_lossy().into(), + }; + + let auth = Authorization { + auth_path: Some("auth_path".to_string()), + use_auth: true, + }; + + let access = AccessControl { + acl_path: Some("acl_path".to_string()), + private_repo: true, + append_only: true, + }; + + // Try to write + let config = ServerConfig { + server, + repos, + tls, + authorization: auth, + accesscontrol: access, + }; + let config_file = server_path.join("rustic_server.test.toml"); + config.to_file(&config_file).unwrap(); + + // Try to read + let _tmp_config = ServerConfig::from_file(&config_file).unwrap(); + } +} diff --git a/src/test_server.rs b/src/test_server.rs new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_server.rs b/tests/test_server.rs new file mode 100644 index 0000000..e69de29