Extract reconcile (#85)
This commit is contained in:
parent
75b020146a
commit
bb0e44f06f
141 changed files with 294 additions and 36720 deletions
48
sync-server/src/config/database_config.rs
Normal file
48
sync-server/src/config/database_config.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
use std::{path::PathBuf, time::Duration};
|
||||
|
||||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_with::serde_as;
|
||||
|
||||
use crate::consts::{
|
||||
DEFAULT_CURSOR_TIMEOUT, DEFAULT_DATABASES_DIRECTORY_PATH, DEFAULT_MAX_CONNECTIONS_PER_VAULT,
|
||||
};
|
||||
|
||||
#[serde_with::serde_as]
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct DatabaseConfig {
|
||||
#[serde(default = "default_databases_directory_path")]
|
||||
pub databases_directory_path: PathBuf,
|
||||
|
||||
#[serde(default = "default_max_connections_per_vault")]
|
||||
pub max_connections_per_vault: u32,
|
||||
|
||||
#[serde(default = "default_cursor_timeout", rename = "cursor_timeout_seconds")]
|
||||
#[serde_as(as = "serde_with::DurationSeconds<u64>")]
|
||||
pub cursor_timeout: Duration,
|
||||
}
|
||||
|
||||
fn default_databases_directory_path() -> PathBuf {
|
||||
debug!("Using default databases directory path: {DEFAULT_DATABASES_DIRECTORY_PATH:?}");
|
||||
PathBuf::from(DEFAULT_DATABASES_DIRECTORY_PATH)
|
||||
}
|
||||
|
||||
fn default_max_connections_per_vault() -> u32 {
|
||||
debug!("Using default max connections: {DEFAULT_MAX_CONNECTIONS_PER_VAULT}");
|
||||
DEFAULT_MAX_CONNECTIONS_PER_VAULT
|
||||
}
|
||||
|
||||
fn default_cursor_timeout() -> Duration {
|
||||
debug!("Using default cursor timeout: {DEFAULT_CURSOR_TIMEOUT:?}");
|
||||
DEFAULT_CURSOR_TIMEOUT
|
||||
}
|
||||
|
||||
impl Default for DatabaseConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
databases_directory_path: default_databases_directory_path(),
|
||||
max_connections_per_vault: default_max_connections_per_vault(),
|
||||
cursor_timeout: default_cursor_timeout(),
|
||||
}
|
||||
}
|
||||
}
|
||||
50
sync-server/src/config/server_config.rs
Normal file
50
sync-server/src/config/server_config.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::consts::{
|
||||
DEFAULT_HOST, DEFAULT_MAX_BODY_SIZE_MB, DEFAULT_MAX_CLIENTS_PER_VAULT, DEFAULT_PORT,
|
||||
DEFAULT_RESPONSE_TIMEOUT_SECONDS,
|
||||
};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
|
||||
pub struct ServerConfig {
|
||||
#[serde(default = "default_host")]
|
||||
pub host: String,
|
||||
|
||||
#[serde(default = "default_port")]
|
||||
pub port: u16,
|
||||
|
||||
#[serde(default = "default_max_body_size_mb")]
|
||||
pub max_body_size_mb: usize,
|
||||
|
||||
#[serde(default = "default_max_clients_per_vault")]
|
||||
pub max_clients_per_vault: usize,
|
||||
|
||||
#[serde(default = "default_response_timeout_seconds")]
|
||||
pub response_timeout_seconds: u64,
|
||||
}
|
||||
|
||||
fn default_host() -> String {
|
||||
debug!("Using default server host: {DEFAULT_HOST}");
|
||||
DEFAULT_HOST.to_owned()
|
||||
}
|
||||
|
||||
fn default_port() -> u16 {
|
||||
debug!("Using default server port: {DEFAULT_PORT}");
|
||||
DEFAULT_PORT
|
||||
}
|
||||
|
||||
fn default_max_body_size_mb() -> usize {
|
||||
debug!("Using default max body size (MB): {DEFAULT_MAX_BODY_SIZE_MB}");
|
||||
DEFAULT_MAX_BODY_SIZE_MB
|
||||
}
|
||||
|
||||
fn default_max_clients_per_vault() -> usize {
|
||||
debug!("Using default max clients per vault: {DEFAULT_MAX_CLIENTS_PER_VAULT}");
|
||||
DEFAULT_MAX_CLIENTS_PER_VAULT
|
||||
}
|
||||
|
||||
fn default_response_timeout_seconds() -> u64 {
|
||||
debug!("Using default response timeout (seconds): {DEFAULT_RESPONSE_TIMEOUT_SECONDS}");
|
||||
DEFAULT_RESPONSE_TIMEOUT_SECONDS
|
||||
}
|
||||
164
sync-server/src/config/user_config.rs
Normal file
164
sync-server/src/config/user_config.rs
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
use bimap::BiHashMap;
|
||||
use rand::{Rng, distr::Alphanumeric, rng};
|
||||
use serde::{Deserialize, Deserializer, Serialize, de::Error};
|
||||
|
||||
use crate::app_state::database::models::VaultId;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct UserConfig {
|
||||
#[serde(default = "default_users", deserialize_with = "validate_users")]
|
||||
pub user_configs: Vec<User>,
|
||||
}
|
||||
|
||||
fn validate_users<'de, D>(deserializer: D) -> Result<Vec<User>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let users = Vec::<User>::deserialize(deserializer)?;
|
||||
|
||||
let mut user_token_map = BiHashMap::new();
|
||||
for user in &users {
|
||||
if let Some(existing_name) = user_token_map.get_by_right(&user.token) {
|
||||
return Err(D::Error::custom(format!(
|
||||
"Duplicate user token found: '{}' for users '{}' and '{}'. User tokens must be \
|
||||
unique.",
|
||||
user.token, existing_name, user.name
|
||||
)));
|
||||
}
|
||||
|
||||
if user_token_map.contains_left(&user.name) {
|
||||
return Err(D::Error::custom(format!(
|
||||
"Duplicate user name found: '{}'. User names must be unique.",
|
||||
user.name
|
||||
)));
|
||||
}
|
||||
|
||||
user_token_map.insert(user.name.clone(), user.token.clone());
|
||||
}
|
||||
|
||||
Ok(users)
|
||||
}
|
||||
|
||||
impl UserConfig {
|
||||
pub fn get_user(&self, token: &str) -> Option<&User> {
|
||||
self.user_configs.iter().find(|u| u.token == token)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct User {
|
||||
pub name: String,
|
||||
pub token: String,
|
||||
pub vault_access: VaultAccess,
|
||||
}
|
||||
|
||||
impl Default for UserConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
user_configs: default_users(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
|
||||
#[serde(rename_all = "snake_case", tag = "type")]
|
||||
pub enum VaultAccess {
|
||||
#[default]
|
||||
AllowAccessToAll,
|
||||
|
||||
AllowList(AllowListedVaults),
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
|
||||
pub struct AllowListedVaults {
|
||||
pub allowed: Vec<VaultId>,
|
||||
}
|
||||
|
||||
fn default_users() -> Vec<User> {
|
||||
vec![User {
|
||||
name: "admin".to_owned(),
|
||||
token: get_random_token(),
|
||||
vault_access: VaultAccess::default(),
|
||||
}]
|
||||
}
|
||||
|
||||
pub fn get_random_token() -> String {
|
||||
rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(64)
|
||||
.map(char::from)
|
||||
.collect()
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_validate_users_unique_names_and_tokens() {
|
||||
let config_json = json!({
|
||||
"user_configs": [
|
||||
{
|
||||
"name": "alice",
|
||||
"token": "token1",
|
||||
"vault_access": { "type": "allow_access_to_all" }
|
||||
},
|
||||
{
|
||||
"name": "bob",
|
||||
"token": "token2",
|
||||
"vault_access": { "type": "allow_access_to_all" }
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
let config: Result<UserConfig, _> = serde_json::from_value(config_json);
|
||||
assert!(config.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_users_duplicate_names() {
|
||||
let config_json = json!({
|
||||
"user_configs": [
|
||||
{
|
||||
"name": "alice",
|
||||
"token": "token1",
|
||||
"vault_access": { "type": "allow_access_to_all" }
|
||||
},
|
||||
{
|
||||
"name": "alice",
|
||||
"token": "token2",
|
||||
"vault_access": { "type": "allow_access_to_all" }
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
let config: Result<UserConfig, _> = serde_json::from_value(config_json);
|
||||
assert!(config.is_err());
|
||||
let err = config.unwrap_err().to_string();
|
||||
assert!(err.contains("Duplicate user name found"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_users_duplicate_tokens() {
|
||||
let config_json = json!({
|
||||
"user_configs": [
|
||||
{
|
||||
"name": "alice",
|
||||
"token": "token1",
|
||||
"vault_access": { "type": "allow_access_to_all" }
|
||||
},
|
||||
{
|
||||
"name": "bob",
|
||||
"token": "token1",
|
||||
"vault_access": { "type": "allow_access_to_all" }
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
let config: Result<UserConfig, _> = serde_json::from_value(config_json);
|
||||
assert!(config.is_err());
|
||||
let err = config.unwrap_err().to_string();
|
||||
assert!(err.contains("Duplicate user token found"));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue