Skip to content

Commit 3a53718

Browse files
pashabitzConvex, Inc.
authored and
Convex, Inc.
committed
[ENG-7377] Block storage access if backend not running (#30127)
GitOrigin-RevId: 589909c731163f8e4ccb1195997a6a5c7d0cf506
1 parent 096031c commit 3a53718

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

crates/application/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ use minitrace::{
166166
};
167167
use model::{
168168
auth::AuthInfoModel,
169+
backend_state::BackendStateModel,
169170
components::{
170171
config::ComponentConfigModel,
171172
handles::FunctionHandlesModel,
@@ -2312,6 +2313,19 @@ impl<RT: Runtime> Application<RT> {
23122313
Ok(())
23132314
}
23142315

2316+
async fn bail_if_not_running(&self) -> anyhow::Result<()> {
2317+
let backend_state = BackendStateModel::new(&mut self.begin(Identity::Unknown).await?)
2318+
.get_backend_state()
2319+
.await?;
2320+
if backend_state.is_stopped() {
2321+
anyhow::bail!(ErrorMetadata::bad_request(
2322+
"BackendIsNotRunning",
2323+
"Cannot perform this operation when the backend is not running"
2324+
));
2325+
}
2326+
Ok(())
2327+
}
2328+
23152329
pub async fn store_file(
23162330
&self,
23172331
component: ComponentId,
@@ -2320,6 +2334,7 @@ impl<RT: Runtime> Application<RT> {
23202334
expected_sha256: Option<Sha256Digest>,
23212335
body: BoxStream<'_, anyhow::Result<Bytes>>,
23222336
) -> anyhow::Result<DeveloperDocumentId> {
2337+
self.bail_if_not_running().await?;
23232338
let storage_id = self
23242339
.file_storage
23252340
.store_file(
@@ -2374,6 +2389,7 @@ impl<RT: Runtime> Application<RT> {
23742389
component: ComponentId,
23752390
storage_id: FileStorageId,
23762391
) -> anyhow::Result<FileStream> {
2392+
self.bail_if_not_running().await?;
23772393
let file_entry = self.get_file_entry(component, storage_id).await?;
23782394
self
23792395
.file_storage
@@ -2389,6 +2405,7 @@ impl<RT: Runtime> Application<RT> {
23892405
storage_id: FileStorageId,
23902406
bytes_range: (Bound<u64>, Bound<u64>),
23912407
) -> anyhow::Result<FileRangeStream> {
2408+
self.bail_if_not_running().await?;
23922409
let mut file_storage_tx = self.begin(Identity::system()).await?;
23932410

23942411
let Some(file_entry) = self

crates/application/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod returns_validation;
99
mod scheduled_jobs;
1010
mod schema;
1111
mod source_package;
12+
mod storage;
1213

1314
const NODE_SOURCE: &str = r#"
1415
var nodeFunction = () => {};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use common::{
2+
components::ComponentId,
3+
types::BackendState,
4+
};
5+
use errors::ErrorMetadataAnyhowExt;
6+
use futures::stream;
7+
use keybroker::Identity;
8+
use model::backend_state::BackendStateModel;
9+
use runtime::testing::TestRuntime;
10+
11+
use crate::{
12+
test_helpers::ApplicationTestExt,
13+
Application,
14+
};
15+
16+
#[convex_macro::test_runtime]
17+
pub(crate) async fn test_backend_not_running_cannot_store_file(
18+
rt: TestRuntime,
19+
) -> anyhow::Result<()> {
20+
let app = Application::new_for_tests(&rt).await?;
21+
22+
let file_body = Box::pin(stream::once(async {
23+
Ok(bytes::Bytes::from(vec![55; 1024 + 1]))
24+
}));
25+
let ok_result = app
26+
.store_file(ComponentId::Root, None, None, None, file_body)
27+
.await;
28+
assert!(ok_result.is_ok());
29+
30+
let mut tx = app.begin(Identity::system()).await?;
31+
BackendStateModel::new(&mut tx)
32+
.toggle_backend_state(BackendState::Disabled)
33+
.await?;
34+
app.commit_test(tx).await?;
35+
let file_body = Box::pin(stream::once(async {
36+
Ok(bytes::Bytes::from(vec![55; 1024 + 1]))
37+
}));
38+
let result = app
39+
.store_file(ComponentId::Root, None, None, None, file_body)
40+
.await;
41+
assert!(result.is_err());
42+
let error = result.unwrap_err();
43+
assert!(error.is_bad_request());
44+
assert_eq!(error.short_msg(), "BackendIsNotRunning");
45+
Ok(())
46+
}

0 commit comments

Comments
 (0)