mirror of
https://github.com/dani-garcia/vaultwarden.git
synced 2025-06-12 22:00:07 +00:00
Additional PR improvements
This commit is contained in:
parent
05f5993ab5
commit
d47e245fcd
3 changed files with 28 additions and 66 deletions
|
@ -1,4 +1,3 @@
|
|||
use std::error::Error as _;
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
|
||||
|
@ -424,27 +423,7 @@ async fn post_send_file_v2_data(
|
|||
|
||||
let file_path = format!("{send_id}/{file_id}");
|
||||
|
||||
save_temp_file(PathType::Sends, &file_path, data.data, false).await.map_err(|e| {
|
||||
let was_file_exists_error = e
|
||||
.source()
|
||||
.and_then(|e| e.downcast_ref::<std::io::Error>())
|
||||
.and_then(|e| e.get_ref())
|
||||
.and_then(|e| e.downcast_ref::<opendal::Error>())
|
||||
.map(|e| e.kind() == opendal::ErrorKind::ConditionNotMatch)
|
||||
.unwrap_or(false);
|
||||
|
||||
if was_file_exists_error {
|
||||
return crate::Error::new(
|
||||
"Send file has already been uploaded.",
|
||||
format!("File {file_path:?} already exists"),
|
||||
);
|
||||
}
|
||||
|
||||
crate::Error::new(
|
||||
"Unexpected error while creating send file",
|
||||
format!("Error while saving send file at path {file_path}: {e:?}"),
|
||||
)
|
||||
})?;
|
||||
save_temp_file(PathType::Sends, &file_path, data.data, false).await?;
|
||||
|
||||
nt.send_send_update(
|
||||
UpdateType::SyncSendCreate,
|
||||
|
|
53
src/auth.rs
53
src/auth.rs
|
@ -39,43 +39,32 @@ static PRIVATE_RSA_KEY: OnceCell<EncodingKey> = OnceCell::new();
|
|||
static PUBLIC_RSA_KEY: OnceCell<DecodingKey> = OnceCell::new();
|
||||
|
||||
pub async fn initialize_keys() -> Result<(), Error> {
|
||||
async fn read_key(create_if_missing: bool) -> Result<(Rsa<openssl::pkey::Private>, Vec<u8>), std::io::Error> {
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::io::Error;
|
||||
|
||||
let rsa_key_filename = std::path::PathBuf::from(CONFIG.private_rsa_key())
|
||||
.file_name()
|
||||
.ok_or_else(|| Error::other("Private RSA key path missing filename"))?
|
||||
.to_str()
|
||||
.ok_or_else(|| Error::other("Private RSA key path filename is not valid UTF-8"))?
|
||||
.to_string();
|
||||
let rsa_key_filename = std::path::PathBuf::from(CONFIG.private_rsa_key())
|
||||
.file_name()
|
||||
.ok_or_else(|| Error::other("Private RSA key path missing filename"))?
|
||||
.to_str()
|
||||
.ok_or_else(|| Error::other("Private RSA key path filename is not valid UTF-8"))?
|
||||
.to_string();
|
||||
|
||||
let operator = CONFIG.opendal_operator_for_path_type(PathType::RsaKey).map_err(Error::other)?;
|
||||
let operator = CONFIG.opendal_operator_for_path_type(PathType::RsaKey).map_err(Error::other)?;
|
||||
|
||||
let priv_key_buffer = match operator.read(&rsa_key_filename).await {
|
||||
Ok(buffer) => Some(buffer),
|
||||
Err(e) if e.kind() == opendal::ErrorKind::NotFound && create_if_missing => None,
|
||||
Err(e) if e.kind() == opendal::ErrorKind::NotFound => {
|
||||
return Err(Error::new(ErrorKind::NotFound, "Private key not found"))
|
||||
}
|
||||
Err(e) => return Err(Error::new(ErrorKind::InvalidData, format!("Error reading private key: {e}"))),
|
||||
};
|
||||
|
||||
if let Some(priv_key_buffer) = priv_key_buffer {
|
||||
Ok((Rsa::private_key_from_pem(priv_key_buffer.to_vec().as_slice())?, priv_key_buffer.to_vec()))
|
||||
} else {
|
||||
let rsa_key = Rsa::generate(2048)?;
|
||||
let priv_key_buffer = rsa_key.private_key_to_pem()?;
|
||||
operator.write(&rsa_key_filename, priv_key_buffer).await?;
|
||||
info!("Private key '{}' created correctly", CONFIG.private_rsa_key());
|
||||
Err(Error::new(ErrorKind::NotFound, "Private key created, forcing attempt to read it again"))
|
||||
}
|
||||
}
|
||||
|
||||
let (priv_key, priv_key_buffer) = match read_key(true).await {
|
||||
Ok(key) => key,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => read_key(false).await?,
|
||||
let priv_key_buffer = match operator.read(&rsa_key_filename).await {
|
||||
Ok(buffer) => Some(buffer),
|
||||
Err(e) if e.kind() == opendal::ErrorKind::NotFound => None,
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
|
||||
let (priv_key, priv_key_buffer) = if let Some(priv_key_buffer) = priv_key_buffer {
|
||||
(Rsa::private_key_from_pem(priv_key_buffer.to_vec().as_slice())?, priv_key_buffer.to_vec())
|
||||
} else {
|
||||
let rsa_key = Rsa::generate(2048)?;
|
||||
let priv_key_buffer = rsa_key.private_key_to_pem()?;
|
||||
operator.write(&rsa_key_filename, priv_key_buffer.clone()).await?;
|
||||
info!("Private key '{}' created correctly", CONFIG.private_rsa_key());
|
||||
(rsa_key, priv_key_buffer)
|
||||
};
|
||||
let pub_key_buffer = priv_key.public_key_to_pem()?;
|
||||
|
||||
let enc = EncodingKey::from_rsa_pem(&priv_key_buffer)?;
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
env::consts::EXE_SUFFIX,
|
||||
process::exit,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
LazyLock, Mutex, RwLock,
|
||||
LazyLock, RwLock,
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -136,10 +135,8 @@ macro_rules! make_config {
|
|||
async fn from_file() -> Result<Self, Error> {
|
||||
let operator = opendal_operator_for_path(&CONFIG_FILE_PARENT_DIR)?;
|
||||
let config_bytes = operator.read(&CONFIG_FILENAME).await?;
|
||||
let config_str = String::from_utf8(config_bytes.to_vec())
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
|
||||
println!("[INFO] Using saved config from `{}` for configuration.\n", *CONFIG_FILE);
|
||||
serde_json::from_str(&config_str).map_err(Into::into)
|
||||
serde_json::from_slice(&config_bytes.to_vec()).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn clear_non_editable(&mut self) {
|
||||
|
@ -1166,13 +1163,10 @@ fn smtp_convert_deprecated_ssl_options(smtp_ssl: Option<bool>, smtp_explicit_tls
|
|||
|
||||
fn opendal_operator_for_path(path: &str) -> Result<opendal::Operator, Error> {
|
||||
// Cache of previously built operators by path
|
||||
static OPERATORS_BY_PATH: LazyLock<Mutex<HashMap<String, opendal::Operator>>> =
|
||||
LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||
static OPERATORS_BY_PATH: LazyLock<dashmap::DashMap<String, opendal::Operator>> =
|
||||
LazyLock::new(dashmap::DashMap::new);
|
||||
|
||||
let mut operators_by_path =
|
||||
OPERATORS_BY_PATH.lock().map_err(|e| format!("Failed to lock OpenDAL operators cache: {e}"))?;
|
||||
|
||||
if let Some(operator) = operators_by_path.get(path) {
|
||||
if let Some(operator) = OPERATORS_BY_PATH.get(path) {
|
||||
return Ok(operator.clone());
|
||||
}
|
||||
|
||||
|
@ -1187,7 +1181,7 @@ fn opendal_operator_for_path(path: &str) -> Result<opendal::Operator, Error> {
|
|||
opendal::Operator::new(builder)?.finish()
|
||||
};
|
||||
|
||||
operators_by_path.insert(path.to_string(), operator.clone());
|
||||
OPERATORS_BY_PATH.insert(path.to_string(), operator.clone());
|
||||
|
||||
Ok(operator)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue