Skip to content

Commit c9893e1

Browse files
authored
Add validation (#264)
Add checks for validating permissions on staging and local storage directory
1 parent 63ed836 commit c9893e1

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

server/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ async fn main() -> anyhow::Result<()> {
6464
env_logger::init();
6565
CONFIG.validate();
6666
let storage = CONFIG.storage().get_object_store();
67+
CONFIG.validate_staging()?;
6768
CONFIG.validate_storage(&*storage).await;
6869
let metadata = storage::resolve_parseable_metadata().await?;
6970
banner::print(&CONFIG, metadata);

server/src/option.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::storage::{
2626
FSConfig, ObjectStorage, ObjectStorageError, ObjectStorageProvider, S3Config,
2727
LOCAL_SYNC_INTERVAL,
2828
};
29+
use crate::utils::validate_path_is_writeable;
2930

3031
lazy_static::lazy_static! {
3132
#[derive(Debug)]
@@ -112,6 +113,11 @@ impl Config {
112113
}
113114
}
114115

116+
pub fn validate_staging(&self) -> anyhow::Result<()> {
117+
let staging_path = self.staging_dir();
118+
validate_path_is_writeable(staging_path)
119+
}
120+
115121
pub fn storage(&self) -> Arc<dyn ObjectStorageProvider + Send + Sync> {
116122
self.storage.clone()
117123
}

server/src/storage/localfs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use relative_path::RelativePath;
3636
use tokio::fs;
3737
use tokio_stream::wrappers::ReadDirStream;
3838

39-
use crate::{option::validation, query::Query};
39+
use crate::{option::validation, query::Query, utils::validate_path_is_writeable};
4040

4141
use super::{LogStream, ObjectStorage, ObjectStorageError, ObjectStorageProvider};
4242

@@ -117,7 +117,9 @@ impl ObjectStorage for LocalFS {
117117
}
118118

119119
async fn check(&self) -> Result<(), ObjectStorageError> {
120-
Ok(fs::create_dir_all(&self.root).await?)
120+
fs::create_dir_all(&self.root).await?;
121+
validate_path_is_writeable(&self.root)
122+
.map_err(|e| ObjectStorageError::UnhandledError(e.into()))
121123
}
122124

123125
async fn delete_stream(&self, stream_name: &str) -> Result<(), ObjectStorageError> {

server/src/utils.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*
1717
*/
1818

19+
use std::path::Path;
20+
1921
use chrono::{DateTime, NaiveDate, Timelike, Utc};
2022
use serde_json::{json, Value};
2123

@@ -122,6 +124,15 @@ pub fn capitalize_ascii(s: &str) -> String {
122124
s[0..1].to_uppercase() + &s[1..]
123125
}
124126

127+
pub fn validate_path_is_writeable(path: &Path) -> anyhow::Result<()> {
128+
let Ok(md) = std::fs::metadata(path) else { anyhow::bail!("Could not read metadata for staging dir") };
129+
let permissions = md.permissions();
130+
if permissions.readonly() {
131+
anyhow::bail!("Staging directory {} is unwritable", path.display())
132+
}
133+
Ok(())
134+
}
135+
125136
pub mod uid {
126137
use ulid::Ulid;
127138

0 commit comments

Comments
 (0)