Merge branch 'salvatoret/default-storage-folder' into 'main'

Move default storage config to `veilid-core`

See merge request veilid/veilid!245
This commit is contained in:
Christien Rioux 2024-01-19 18:52:42 +00:00
commit 1a4cab5007
2 changed files with 98 additions and 148 deletions

View File

@ -1,3 +1,5 @@
use std::path::PathBuf;
use directories::ProjectDirs;
use crate::*; use crate::*;
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
@ -253,13 +255,28 @@ pub struct VeilidConfigTLS {
impl Default for VeilidConfigTLS { impl Default for VeilidConfigTLS {
fn default() -> Self { fn default() -> Self {
Self { Self {
certificate_path: String::new(), certificate_path: get_default_ssl_directory("certs/server.crt"),
private_key_path: String::new(), private_key_path: get_default_ssl_directory("keys/server.key"),
connection_initial_timeout_ms: 2000, connection_initial_timeout_ms: 2000,
} }
} }
} }
pub fn get_default_ssl_directory(sub_path: &str) -> String {
let default_path = PathBuf::from("/etc/veilid-server/ssl").join(sub_path);
#[cfg(unix)]
if default_path.exists() {
return default_path.to_string_lossy().into();
}
ProjectDirs::from("org", "Veilid", "Veilid")
.map(|dirs| dirs.data_local_dir().join("ssl").join(sub_path))
.unwrap_or_else(|| PathBuf::from("./ssl").join(sub_path))
.to_string_lossy()
.into()
}
/// Configure the Distributed Hash Table (DHT) /// Configure the Distributed Hash Table (DHT)
/// ///
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
@ -428,21 +445,56 @@ impl Default for VeilidConfigNetwork {
} }
} }
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[cfg_attr(target_arch = "wasm32", derive(Tsify))] #[cfg_attr(target_arch = "wasm32", derive(Tsify))]
pub struct VeilidConfigTableStore { pub struct VeilidConfigTableStore {
pub directory: String, pub directory: String,
pub delete: bool, pub delete: bool,
} }
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] impl Default for VeilidConfigTableStore {
fn default() -> Self {
Self {
directory: get_default_store_path("table_store"),
delete: false,
}
}
}
fn get_default_store_path(store_type: &str) -> String {
#[cfg(unix)]
{
let globalpath = PathBuf::from(format!("/var/db/veilid-server/{}", store_type));
if globalpath.exists() {
return globalpath.to_string_lossy().into();
}
}
ProjectDirs::from("org", "Veilid", "Veilid")
.map(|dirs| dirs.data_local_dir().to_path_buf())
.unwrap_or_else(|| PathBuf::from("./"))
.join(store_type)
.to_string_lossy()
.into()
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[cfg_attr(target_arch = "wasm32", derive(Tsify))] #[cfg_attr(target_arch = "wasm32", derive(Tsify))]
pub struct VeilidConfigBlockStore { pub struct VeilidConfigBlockStore {
pub directory: String, pub directory: String,
pub delete: bool, pub delete: bool,
} }
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] impl Default for VeilidConfigBlockStore {
fn default() -> Self {
Self {
directory: get_default_store_path("block_store"),
delete: false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[cfg_attr(target_arch = "wasm32", derive(Tsify))] #[cfg_attr(target_arch = "wasm32", derive(Tsify))]
pub struct VeilidConfigProtectedStore { pub struct VeilidConfigProtectedStore {
pub allow_insecure_fallback: bool, pub allow_insecure_fallback: bool,
@ -454,6 +506,19 @@ pub struct VeilidConfigProtectedStore {
pub new_device_encryption_key_password: Option<String>, pub new_device_encryption_key_password: Option<String>,
} }
impl Default for VeilidConfigProtectedStore {
fn default() -> Self {
Self {
allow_insecure_fallback: false,
always_use_insecure_storage: false,
directory: get_default_store_path("protected_store"),
delete: false,
device_encryption_key_password: String::from(""),
new_device_encryption_key_password: None,
}
}
}
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[cfg_attr(target_arch = "wasm32", derive(Tsify))] #[cfg_attr(target_arch = "wasm32", derive(Tsify))]
pub struct VeilidConfigCapabilities { pub struct VeilidConfigCapabilities {

View File

@ -157,27 +157,23 @@ core:
) )
.replace( .replace(
"%TABLE_STORE_DIRECTORY%", "%TABLE_STORE_DIRECTORY%",
&Settings::get_default_table_store_path().to_string_lossy(), &VeilidConfigTableStore::default().directory,
) )
.replace( .replace(
"%BLOCK_STORE_DIRECTORY%", "%BLOCK_STORE_DIRECTORY%",
&Settings::get_default_block_store_path().to_string_lossy(), &VeilidConfigBlockStore::default().directory,
) )
.replace( .replace(
"%DIRECTORY%", "%DIRECTORY%",
&Settings::get_default_protected_store_directory().to_string_lossy(), &VeilidConfigProtectedStore::default().directory,
) )
.replace( .replace(
"%CERTIFICATE_PATH%", "%CERTIFICATE_PATH%",
&Settings::get_default_certificate_directory() &VeilidConfigTLS::default().certificate_path
.join("server.crt")
.to_string_lossy(),
) )
.replace( .replace(
"%PRIVATE_KEY_PATH%", "%PRIVATE_KEY_PATH%",
&Settings::get_default_private_key_directory() &VeilidConfigTLS::default().private_key_path
.join("server.key")
.to_string_lossy(),
) )
.replace( .replace(
"%REMOTE_MAX_SUBKEY_CACHE_MEMORY_MB%", "%REMOTE_MAX_SUBKEY_CACHE_MEMORY_MB%",
@ -529,8 +525,8 @@ pub struct Protocol {
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct Tls { pub struct Tls {
pub certificate_path: PathBuf, pub certificate_path: String,
pub private_key_path: PathBuf, pub private_key_path: String,
pub connection_initial_timeout_ms: u32, pub connection_initial_timeout_ms: u32,
} }
@ -610,13 +606,13 @@ pub struct Testing {
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct TableStore { pub struct TableStore {
pub directory: PathBuf, pub directory: String,
pub delete: bool, pub delete: bool,
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct BlockStore { pub struct BlockStore {
pub directory: PathBuf, pub directory: String,
pub delete: bool, pub delete: bool,
} }
@ -624,7 +620,7 @@ pub struct BlockStore {
pub struct ProtectedStore { pub struct ProtectedStore {
pub allow_insecure_fallback: bool, pub allow_insecure_fallback: bool,
pub always_use_insecure_storage: bool, pub always_use_insecure_storage: bool,
pub directory: PathBuf, pub directory: String,
pub delete: bool, pub delete: bool,
pub device_encryption_key_password: String, pub device_encryption_key_password: String,
pub new_device_encryption_key_password: Option<String>, pub new_device_encryption_key_password: Option<String>,
@ -784,122 +780,16 @@ impl Settings {
/// `/Users/<user>/Library/Application Support/org.Veilid.Veilid` /// `/Users/<user>/Library/Application Support/org.Veilid.Veilid`
/// ///
pub fn get_default_config_path() -> PathBuf { pub fn get_default_config_path() -> PathBuf {
let default_path = PathBuf::from("/etc/veilid-server/veilid-server.conf");
#[cfg(unix)] #[cfg(unix)]
{ if default_path.exists() {
let globalpath = PathBuf::from("/etc/veilid-server/veilid-server.conf"); return default_path;
if globalpath.exists() {
return globalpath;
}
} }
let mut cfg_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") ProjectDirs::from("org", "Veilid", "Veilid")
{ .map(|dirs| dirs.config_dir().join("veilid-server.conf"))
PathBuf::from(my_proj_dirs.config_dir()) .unwrap_or_else(|| PathBuf::from("./veilid-server.conf"))
} else {
PathBuf::from("./")
};
cfg_path.push("veilid-server.conf");
cfg_path
}
pub fn get_default_table_store_path() -> PathBuf {
#[cfg(unix)]
{
let globalpath = PathBuf::from("/var/db/veilid-server/table_store");
if globalpath.exists() {
return globalpath;
}
}
let mut ts_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
ts_path.push("table_store");
ts_path
}
pub fn get_default_block_store_path() -> PathBuf {
#[cfg(unix)]
{
let globalpath = PathBuf::from("/var/db/veilid-server/block_store");
if globalpath.exists() {
return globalpath;
}
}
let mut bs_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
bs_path.push("block_store");
bs_path
}
pub fn get_default_protected_store_directory() -> PathBuf {
#[cfg(unix)]
{
let globalpath = PathBuf::from("/var/db/veilid-server/protected_store");
if globalpath.exists() {
return globalpath;
}
}
let mut ps_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
ps_path.push("protected_store");
ps_path
}
pub fn get_default_certificate_directory() -> PathBuf {
#[cfg(unix)]
{
let mut globalpath = PathBuf::from("/etc/veilid-server");
if globalpath.exists() {
globalpath.push("ssl");
globalpath.push("certs");
return globalpath;
}
}
let mut c_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
c_path.push("ssl");
c_path.push("certs");
c_path
}
pub fn get_default_private_key_directory() -> PathBuf {
#[cfg(unix)]
{
let mut globalpath = PathBuf::from("/etc/veilid-server");
if globalpath.exists() {
globalpath.push("ssl");
globalpath.push("keys");
return globalpath;
}
}
let mut pk_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
pk_path.push("ssl");
pk_path.push("keys");
pk_path
} }
pub fn get_default_remote_max_subkey_cache_memory_mb() -> u32 { pub fn get_default_remote_max_subkey_cache_memory_mb() -> u32 {
@ -918,7 +808,7 @@ impl Settings {
.cmp(&a.mount_point().to_string_lossy().len()) .cmp(&a.mount_point().to_string_lossy().len())
}); });
for disk in sys.disks() { for disk in sys.disks() {
if dht_storage_path.starts_with(disk.mount_point()) { if dht_storage_path.starts_with(&*disk.mount_point().to_string_lossy()) {
let available_mb = disk.available_space() / 1_000_000u64; let available_mb = disk.available_space() / 1_000_000u64;
if available_mb > 40_000 { if available_mb > 40_000 {
// Default to 10GB if more than 40GB is available // Default to 10GB if more than 40GB is available
@ -1127,8 +1017,7 @@ impl Settings {
.core .core
.protected_store .protected_store
.directory .directory
.to_string_lossy() .clone(),
.to_string(),
)), )),
"protected_store.delete" => Ok(Box::new(inner.core.protected_store.delete)), "protected_store.delete" => Ok(Box::new(inner.core.protected_store.delete)),
"protected_store.device_encryption_key_password" => Ok(Box::new( "protected_store.device_encryption_key_password" => Ok(Box::new(
@ -1151,8 +1040,7 @@ impl Settings {
.core .core
.table_store .table_store
.directory .directory
.to_string_lossy() .clone(),
.to_string(),
)), )),
"table_store.delete" => Ok(Box::new(inner.core.table_store.delete)), "table_store.delete" => Ok(Box::new(inner.core.table_store.delete)),
@ -1161,8 +1049,7 @@ impl Settings {
.core .core
.block_store .block_store
.directory .directory
.to_string_lossy() .clone(),
.to_string(),
)), )),
"block_store.delete" => Ok(Box::new(inner.core.block_store.delete)), "block_store.delete" => Ok(Box::new(inner.core.block_store.delete)),
@ -1316,8 +1203,7 @@ impl Settings {
.network .network
.tls .tls
.certificate_path .certificate_path
.to_string_lossy() .clone(),
.to_string(),
)), )),
"network.tls.private_key_path" => Ok(Box::new( "network.tls.private_key_path" => Ok(Box::new(
inner inner
@ -1325,8 +1211,7 @@ impl Settings {
.network .network
.tls .tls
.private_key_path .private_key_path
.to_string_lossy() .clone(),
.to_string(),
)), )),
"network.tls.connection_initial_timeout_ms" => Ok(Box::new( "network.tls.connection_initial_timeout_ms" => Ok(Box::new(
inner.core.network.tls.connection_initial_timeout_ms, inner.core.network.tls.connection_initial_timeout_ms,
@ -1565,13 +1450,13 @@ mod tests {
assert_eq!( assert_eq!(
s.core.table_store.directory, s.core.table_store.directory,
Settings::get_default_table_store_path() VeilidConfigTableStore::default().directory,
); );
assert!(!s.core.table_store.delete); assert!(!s.core.table_store.delete);
assert_eq!( assert_eq!(
s.core.block_store.directory, s.core.block_store.directory,
Settings::get_default_block_store_path() VeilidConfigBlockStore::default().directory,
); );
assert!(!s.core.block_store.delete); assert!(!s.core.block_store.delete);
@ -1579,7 +1464,7 @@ mod tests {
assert!(s.core.protected_store.always_use_insecure_storage); assert!(s.core.protected_store.always_use_insecure_storage);
assert_eq!( assert_eq!(
s.core.protected_store.directory, s.core.protected_store.directory,
Settings::get_default_protected_store_directory() VeilidConfigProtectedStore::default().directory
); );
assert!(!s.core.protected_store.delete); assert!(!s.core.protected_store.delete);
assert_eq!(s.core.protected_store.device_encryption_key_password, ""); assert_eq!(s.core.protected_store.device_encryption_key_password, "");
@ -1637,11 +1522,11 @@ mod tests {
// //
assert_eq!( assert_eq!(
s.core.network.tls.certificate_path, s.core.network.tls.certificate_path,
Settings::get_default_certificate_directory().join("server.crt") VeilidConfigTLS::default().certificate_path
); );
assert_eq!( assert_eq!(
s.core.network.tls.private_key_path, s.core.network.tls.private_key_path,
Settings::get_default_private_key_directory().join("server.key") VeilidConfigTLS::default().private_key_path
); );
assert_eq!(s.core.network.tls.connection_initial_timeout_ms, 2_000u32); assert_eq!(s.core.network.tls.connection_initial_timeout_ms, 2_000u32);
// //