mirror of
https://github.com/zhaofengli/attic.git
synced 2024-12-14 11:57:30 +00:00
remove duplicate rs256 generation, simplify output
This commit is contained in:
parent
6378a5a215
commit
86b375381f
3 changed files with 14 additions and 42 deletions
|
@ -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 {
|
||||
|
|
|
@ -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(){
|
||||
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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue