Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(storage): add storage grant module with tests

Steven Enamakele486505debf6
diff --git a/crates/openhuman-core/src/modules/storage_grants.rs b/crates/openhuman-core/src/modules/storage_grants.rs
index 75b7239f53..d600f1dc43 100644
--- a/crates/openhuman-core/src/modules/storage_grants.rs
+++ b/crates/openhuman-core/src/modules/storage_grants.rs
@@ -49,8 +49,6 @@ pub(crate) enum Denied {
Schema,
/// The selector is not part of the contract, or carries no usable target.
Malformed,
- /// The selector needs a bus stream, which this host does not serve yet.
- Unsupported,
}
impl Denied {
@@ -62,7 +60,6 @@ impl Denied {
Self::ReadOnly => "read-only-grant",
Self::Schema => "schema-is-host-only",
Self::Malformed => "malformed-request",
- Self::Unsupported => "unsupported-selector",
}
}
}
@@ -290,7 +287,7 @@ fn is_write(object: Object, selector: &str) -> bool {
) | (
Object::Streams,
"Append" | "AppendBatch" | "AppendBatchIfOffset" | "TruncateBefore" | "DeleteStream"
- ) | (Object::Blobs, "Put" | "Delete" | "PresignPut")
+ ) | (Object::Blobs, "Put" | "PutStream" | "Delete" | "PresignPut")
| (Object::Secrets, "Set" | "Delete")
)
}
@@ -342,10 +339,16 @@ fn documents(grant: &StorageGrant, selector: &str, request: &Value) -> Result<Pl
write: false,
})
}
- // Watches are projected to bus signals, which this host does not
- // route per module yet; opening one would leave a native watch task
- // with nobody to tell, so it is refused instead of half-served.
- "WatchOpen" | "WatchClose" => Err(Denied::Unsupported),
+ // A watch is bound to the module's own server, and its invalidations
+ // are unicast to that module's peer (`emit_to`), so the grant only has
+ // to cover the collection. Closing names a handle the module's own
+ // binding issued; another module's id is `NotFound` there.
+ "WatchOpen" => {
+ let collection = text(request, "collection")?;
+ grant.collection(collection)?;
+ Ok(forward(collection))
+ }
+ "WatchClose" => Ok(forward("")),
"Capabilities" => Ok(forward("")),
_ => Err(Denied::Malformed),
}
@@ -392,16 +395,15 @@ fn streams(grant: &StorageGrant, selector: &str, request: &Value) -> Result<Plan
fn blobs(grant: &StorageGrant, selector: &str, request: &Value) -> Result<Plan, Denied> {
match selector {
- // Streamed bodies need a bus stream argument, which the dynamic
- // interface here does not carry; the inline selectors cover small
- // blobs and these refuse rather than pretend.
- "PutStream" | "GetStream" => Err(Denied::Unsupported),
- "Get" | "GetRange" | "Head" | "PresignGet" => {
+ // The streamed transfers carry the request head as `request`; the body
+ // travels on a peer-bound bus stream the endpoint opens or accepts
+ // (`Connection::accept_stream_from` / `open_stream`).
+ "Get" | "GetStream" | "GetRange" | "Head" | "PresignGet" => {
let key = text(request, "key")?;
let granted = StorageGrant::prefixed(&grant.blob_prefixes, key)?;
Ok(forward(granted))
}
- "Put" | "Delete" | "PresignPut" => {
+ "Put" | "PutStream" | "Delete" | "PresignPut" => {
let key = text(request, "key")?;
let granted = StorageGrant::prefixed(&grant.blob_prefixes, key)?;
Ok(forward(granted))
diff --git a/crates/openhuman-core/src/modules/storage_grants_tests.rs b/crates/openhuman-core/src/modules/storage_grants_tests.rs
index e22c185a9d..c914246f47 100644
--- a/crates/openhuman-core/src/modules/storage_grants_tests.rs
+++ b/crates/openhuman-core/src/modules/storage_grants_tests.rs
@@ -285,24 +285,52 @@ fn schema_is_the_hosts_to_change() {
}
#[test]
-fn streamed_blobs_and_watches_are_refused_as_unsupported() {
+fn streamed_blobs_and_watches_follow_the_grant() {
let g = grant();
- assert_eq!(
+ // A streamed upload is a write to a granted blob prefix; a download a read.
+ assert!(matches!(
authorize(&g, Object::Blobs, "PutStream", &json!({"key": "files/a"})),
- Err(Denied::Unsupported)
- );
- assert_eq!(
+ Ok(Plan::Forward { write: true, .. })
+ ));
+ assert!(matches!(
authorize(&g, Object::Blobs, "GetStream", &json!({"key": "files/a"})),
- Err(Denied::Unsupported)
- );
+ Ok(Plan::Forward { write: false, .. })
+ ));
+ for selector in ["PutStream", "GetStream"] {
+ assert_eq!(
+ authorize(&g, Object::Blobs, selector, &json!({"key": "elsewhere/a"})),
+ Err(Denied::Target),
+ "{selector}"
+ );
+ }
+ let mut read_only = grant();
+ read_only.writable = false;
assert_eq!(
+ authorize(
+ &read_only,
+ Object::Blobs,
+ "PutStream",
+ &json!({"key": "files/a"})
+ ),
+ Err(Denied::ReadOnly)
+ );
+ assert!(matches!(
authorize(
&g,
Object::Documents,
"WatchOpen",
&json!({"collection": "notes"})
),
- Err(Denied::Unsupported)
+ Ok(Plan::Forward { write: false, .. })
+ ));
+ assert_eq!(
+ authorize(
+ &g,
+ Object::Documents,
+ "WatchOpen",
+ &json!({"collection": "not_mine"})
+ ),
+ Err(Denied::Target)
);
}