From c8d2b4b5f769af2f09ae92cd285eaf57014e1095 Mon Sep 17 00:00:00 2001 From: Billy Batista Date: Wed, 9 Oct 2024 23:35:12 -0400 Subject: [PATCH] add UpdateFileMetadata api also made sure to check for uploading or modifying specific files in the UploadFileMetadata API. This is probably worthless, but on the 1% chance that we bind a token to a file we're also uploading(?), then this is necessary. --- Cargo.lock | 2 +- Dockerfile | 2 +- src/main.rs | 39 +++++++++++++++++++++++++++++++++++++-- src/meta_db.rs | 24 ++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11501e0..6f2dc43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -306,7 +306,7 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" [[package]] name = "bfsp" version = "0.1.0" -source = "git+https://github.com/Billy-s-E2EE-File-Server/bfsp.git#1533c836ed11ef7783b78795290a19308862e7f1" +source = "git+https://github.com/Billy-s-E2EE-File-Server/bfsp.git#5f41bac12af9457609775fe950610c60fd96da8b" dependencies = [ "anyhow", "argon2", diff --git a/Dockerfile b/Dockerfile index e16c42f..e587b82 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,4 +31,4 @@ COPY --from=builder-sqlx /build/bin/sqlx /usr/bin/sqlx COPY migrations /app/migrations COPY fly-airship-client ./fly-airship-client -CMD ["./fly-airship-client", "--", "/app/bin/file_server"] +CMD ["/app/bin/file_server"] diff --git a/src/main.rs b/src/main.rs index ee88fa6..5952328 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,7 @@ use bfsp::{ file_server_message::Message::{ ChunksUploadedQuery, DeleteChunksQuery, DeleteFileMetadataQuery, DownloadChunkQuery, DownloadFileMetadataQuery, GetUsageQuery, ListChunkMetadataQuery, ListFileMetadataQuery, - UploadChunk, UploadFileMetadata, + UpdateFileMetadata, UploadChunk, UploadFileMetadata, }, ChunkID, ChunkMetadata, ChunksUploadedQueryResp, DownloadChunkResp, FileServerMessage, Message, }; @@ -497,6 +497,13 @@ pub async fn handle_message( .encode_to_vec(), Err(_) => todo!(), }, + UpdateFileMetadata(query) => { + let enc_meta = query.encrypted_file_metadata.unwrap(); + match handle_update_file_metadata(meta_db.as_ref(), &token, enc_meta).await { + Ok(_) => bfsp::UpdateFileMetadataResp { err: None }.encode_to_vec(), + Err(_) => todo!(), + } + } _ => todo!(), } .prepend_len()) @@ -660,7 +667,7 @@ pub async fn handle_upload_file_metadata( token: &Biscuit, enc_file_meta: EncryptedFileMetadata, ) -> Result<(), UploadMetadataError> { - let user_id = authorize(Right::Write, token, Vec::new(), meta_db) + let user_id = authorize(Right::Write, token, vec![enc_file_meta.id.clone()], meta_db) .await .unwrap(); @@ -682,6 +689,34 @@ pub async fn handle_upload_file_metadata( Ok(()) } +#[tracing::instrument(err, skip(token, meta_db, enc_file_meta))] +pub async fn handle_update_file_metadata( + meta_db: &D, + token: &Biscuit, + enc_file_meta: EncryptedFileMetadata, +) -> Result<(), UploadMetadataError> { + let user_id = authorize(Right::Write, token, vec![enc_file_meta.id.clone()], meta_db) + .await + .unwrap(); + + let storage_usages = meta_db.total_usages(&[user_id]).await.unwrap(); + let storage_usage = *storage_usages.get(&user_id).unwrap(); + + let storage_caps = meta_db.storage_caps(&[user_id]).await.unwrap(); + let storage_cap = *storage_caps.get(&user_id).unwrap(); + + if storage_usage + enc_file_meta.metadata.len() as u64 > storage_cap { + todo!("Deny uploads that exceed storage cap"); + } + + meta_db + .update_file_meta(enc_file_meta, user_id) + .await + .unwrap(); + + Ok(()) +} + #[tracing::instrument(err, skip(token, meta_db))] pub async fn handle_download_file_metadata( meta_db: &D, diff --git a/src/meta_db.rs b/src/meta_db.rs index 5d8e9b0..76b714f 100644 --- a/src/meta_db.rs +++ b/src/meta_db.rs @@ -84,6 +84,12 @@ pub trait MetaDB: Sized + Send + Sync + std::fmt::Debug { enc_metadata: EncryptedFileMetadata, user_id: i64, ) -> impl Future> + Send; + fn update_file_meta( + &self, + enc_metadata: EncryptedFileMetadata, + user_id: i64, + ) -> impl Future> + Send; + fn get_file_meta( &self, meta_id: String, @@ -297,6 +303,24 @@ impl MetaDB for PostgresMetaDB { Ok(()) } + #[tracing::instrument(err, skip(enc_file_meta))] + async fn update_file_meta( + &self, + enc_file_meta: EncryptedFileMetadata, + user_id: i64, + ) -> Result<()> { + sqlx::query( + "update file_metadata set id = $1, encrypted_metadata = $2, user_id = $3 where id = $1", + ) + .bind(enc_file_meta.id) + .bind(enc_file_meta.metadata) + .bind(user_id) + .execute(&self.pool) + .await?; + + Ok(()) + } + #[tracing::instrument(err)] async fn get_file_meta( &self,