From 86b375381f8c74f79acf3f4e24baca0e18761774 Mon Sep 17 00:00:00 2001 From: vonjackets Date: Sat, 30 Nov 2024 18:02:14 +0000 Subject: [PATCH] remove duplicate rs256 generation, simplify output --- server/src/api/v1/cache_config.rs | 2 +- server/src/config.rs | 34 ++++++++++++------------------- server/src/main.rs | 20 ------------------ 3 files changed, 14 insertions(+), 42 deletions(-) diff --git a/server/src/api/v1/cache_config.rs b/server/src/api/v1/cache_config.rs index 4626ecf..2c7159e 100644 --- a/server/src/api/v1/cache_config.rs +++ b/server/src/api/v1/cache_config.rs @@ -194,7 +194,7 @@ pub(crate) async fn create_cache( ) -> ServerResult<()> { let permission = req_state.auth.get_permission_for_cache(&cache_name, false); permission.require_create_cache()?; - + let database = state.database().await?; let keypair = match payload.keypair { diff --git a/server/src/config.rs b/server/src/config.rs index 30be6a5..0193d9f 100644 --- a/server/src/config.rs +++ b/server/src/config.rs @@ -612,13 +612,12 @@ pub async fn load_config(config_path: Option<&Path>) -> Option { else if let Ok(config_path) = get_xdg_config_path(){ match load_config_from_path(&config_path) { Ok(config) => Some(config), - Err(e) => { - eprintln!("Unable to read configuration from XDG path: {e}"); + Err(_) => { None } } } else { - //couldn't find anything + eprintln!("No configuration found!"); Option::None } @@ -653,7 +652,7 @@ fn generate_root_token(rs256_secret_base64: String) -> String { perm.configure_cache = true; perm.configure_cache_retention = true; perm.destroy_cache = true; - + let key = decode_token_rs256_secret_base64(&rs256_secret_base64).unwrap(); return token.encode(&SignatureType::RS256(key), &None, &None).unwrap(); @@ -721,23 +720,6 @@ pub async fn generate_monolithic_config() -> Result<()> { let storage_path = data_path.join("storage"); fs::create_dir_all(&storage_path).await?; - let rs256_secret_base64 = { - let mut rng = rand::thread_rng(); - let private_key = rsa::RsaPrivateKey::new(&mut rng, 4096)?; - let pkcs1_pem = private_key.to_pkcs1_pem(rsa::pkcs1::LineEnding::LF)?; - - BASE64_STANDARD.encode(pkcs1_pem.as_bytes()) - }; - - let config_content = CONFIG_TEMPLATE - .replace("%database_url%", &database_url) - .replace("%storage_path%", storage_path.to_str().unwrap()) - .replace("%token_rs256_secret_base64%", &rs256_secret_base64); - - let config_path = get_xdg_config_path()?; - - fs::write(&config_path, config_content.as_bytes()).await?; - //no config provided, start fresh and create a config, a token, and a sqllite db //generate rsa256 key let rs256_secret_base64 = { @@ -748,6 +730,16 @@ pub async fn generate_monolithic_config() -> Result<()> { BASE64_STANDARD.encode(pkcs1_pem.as_bytes()) }; + let config_content = CONFIG_TEMPLATE + .replace("%database_url%", &database_url) + .replace("%storage_path%", storage_path.to_str().unwrap()) + .replace("%token_rs256_secret_base64%", &rs256_secret_base64); + + let config_path = get_xdg_config_path()?; + + eprintln!("writing server.toml to {}",config_path.display()); + fs::write(&config_path, config_content.as_bytes()).await?; + // Generate a JWT token let root_token = generate_root_token(rs256_secret_base64); diff --git a/server/src/main.rs b/server/src/main.rs index 8723ac4..389b410 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -106,9 +106,6 @@ async fn main() -> Result<()> { attic_server::run_migrations(config.clone()).await?; } attic_server::run_api_server(opts.listen, config).await?; - } else { - //Exit gracefully, no config present - display_no_config_msg(); } } ServerMode::GarbageCollector => { @@ -117,9 +114,6 @@ async fn main() -> Result<()> { config::reinit_from_config(config.clone()).await?; } attic_server::gc::run_garbage_collection(config.clone()).await; - } else { - //Exit gracefully, no config present - display_no_config_msg(); } } @@ -129,9 +123,6 @@ async fn main() -> Result<()> { config::reinit_from_config(config.clone()).await?; } attic_server::run_migrations(config).await?; - } else { - //Exit gracefully, no config present - display_no_config_msg(); } } ServerMode::GarbageCollectorOnce => { @@ -140,9 +131,6 @@ async fn main() -> Result<()> { config::reinit_from_config(config.clone()).await?; } attic_server::gc::run_garbage_collection_once(config).await?; - } else { - //Exit gracefully, no config present - display_no_config_msg(); } } ServerMode::CheckConfig => { @@ -159,9 +147,6 @@ async fn main() -> Result<()> { eprintln!("Enjoy!"); eprintln!("-----------------"); eprintln!(); - } else { - //Exit gracefully, no config present - display_no_config_msg(); } } @@ -183,11 +168,6 @@ async fn run_monolithic(opts: Opts, config: Config) -> Result<()> { } } -fn display_no_config_msg() { - eprintln!(); - eprintln!("No config found, please provide a config.toml file"); -} - fn init_logging(tokio_console: bool) { let env_filter = EnvFilter::from_default_env(); let fmt_layer = tracing_subscriber::fmt::layer().with_filter(env_filter);