1
0
Fork 0
mirror of https://github.com/zhaofengli/attic.git synced 2024-12-15 17:50:57 +00:00

remove duplicate rs256 generation, simplify output

This commit is contained in:
vonjackets 2024-11-30 18:02:14 +00:00
parent 6378a5a215
commit 86b375381f
3 changed files with 14 additions and 42 deletions

View file

@ -194,7 +194,7 @@ pub(crate) async fn create_cache(
) -> ServerResult<()> { ) -> ServerResult<()> {
let permission = req_state.auth.get_permission_for_cache(&cache_name, false); let permission = req_state.auth.get_permission_for_cache(&cache_name, false);
permission.require_create_cache()?; permission.require_create_cache()?;
let database = state.database().await?; let database = state.database().await?;
let keypair = match payload.keypair { let keypair = match payload.keypair {

View file

@ -612,13 +612,12 @@ pub async fn load_config(config_path: Option<&Path>) -> Option<Config> {
else if let Ok(config_path) = get_xdg_config_path(){ else if let Ok(config_path) = get_xdg_config_path(){
match load_config_from_path(&config_path) { match load_config_from_path(&config_path) {
Ok(config) => Some(config), Ok(config) => Some(config),
Err(e) => { Err(_) => {
eprintln!("Unable to read configuration from XDG path: {e}");
None None
} }
} }
} else { } else {
//couldn't find anything eprintln!("No configuration found!");
Option::None Option::None
} }
@ -653,7 +652,7 @@ fn generate_root_token(rs256_secret_base64: String) -> String {
perm.configure_cache = true; perm.configure_cache = true;
perm.configure_cache_retention = true; perm.configure_cache_retention = true;
perm.destroy_cache = true; perm.destroy_cache = true;
let key = decode_token_rs256_secret_base64(&rs256_secret_base64).unwrap(); let key = decode_token_rs256_secret_base64(&rs256_secret_base64).unwrap();
return token.encode(&SignatureType::RS256(key), &None, &None).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"); let storage_path = data_path.join("storage");
fs::create_dir_all(&storage_path).await?; 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 //no config provided, start fresh and create a config, a token, and a sqllite db
//generate rsa256 key //generate rsa256 key
let rs256_secret_base64 = { let rs256_secret_base64 = {
@ -748,6 +730,16 @@ pub async fn generate_monolithic_config() -> Result<()> {
BASE64_STANDARD.encode(pkcs1_pem.as_bytes()) 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 // Generate a JWT token
let root_token = generate_root_token(rs256_secret_base64); let root_token = generate_root_token(rs256_secret_base64);

View file

@ -106,9 +106,6 @@ async fn main() -> Result<()> {
attic_server::run_migrations(config.clone()).await?; attic_server::run_migrations(config.clone()).await?;
} }
attic_server::run_api_server(opts.listen, config).await?; attic_server::run_api_server(opts.listen, config).await?;
} else {
//Exit gracefully, no config present
display_no_config_msg();
} }
} }
ServerMode::GarbageCollector => { ServerMode::GarbageCollector => {
@ -117,9 +114,6 @@ async fn main() -> Result<()> {
config::reinit_from_config(config.clone()).await?; config::reinit_from_config(config.clone()).await?;
} }
attic_server::gc::run_garbage_collection(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?; config::reinit_from_config(config.clone()).await?;
} }
attic_server::run_migrations(config).await?; attic_server::run_migrations(config).await?;
} else {
//Exit gracefully, no config present
display_no_config_msg();
} }
} }
ServerMode::GarbageCollectorOnce => { ServerMode::GarbageCollectorOnce => {
@ -140,9 +131,6 @@ async fn main() -> Result<()> {
config::reinit_from_config(config.clone()).await?; config::reinit_from_config(config.clone()).await?;
} }
attic_server::gc::run_garbage_collection_once(config).await?; attic_server::gc::run_garbage_collection_once(config).await?;
} else {
//Exit gracefully, no config present
display_no_config_msg();
} }
} }
ServerMode::CheckConfig => { ServerMode::CheckConfig => {
@ -159,9 +147,6 @@ async fn main() -> Result<()> {
eprintln!("Enjoy!"); eprintln!("Enjoy!");
eprintln!("-----------------"); eprintln!("-----------------");
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) { fn init_logging(tokio_console: bool) {
let env_filter = EnvFilter::from_default_env(); let env_filter = EnvFilter::from_default_env();
let fmt_layer = tracing_subscriber::fmt::layer().with_filter(env_filter); let fmt_layer = tracing_subscriber::fmt::layer().with_filter(env_filter);