Skip to content

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

chore: files changed crates/openhuman-core/src/profiles/gateway.rs,crates/openhuman-core/src/profile

Steven Enamakela4b6dda15f79
diff --git a/crates/openhuman-core/src/profiles/gateway.rs b/crates/openhuman-core/src/profiles/gateway.rs
index 2c5bcf61b5..8ee05e4173 100644
--- a/crates/openhuman-core/src/profiles/gateway.rs
+++ b/crates/openhuman-core/src/profiles/gateway.rs
@@ -16,6 +16,10 @@
//! headers by mistake cannot be talked into acting as another user.
//!
//! The core never verifies the user's own credential; the gateway did that.
+//!
+//! A profile another node hosts (its lease is live there) is refused with
+//! `409`: the refusal carries the holder ([`HeldBy`]) so the gateway can
+//! route the user there, or retry after the lease runs out.
use std::sync::Arc;
@@ -23,6 +27,7 @@ use hmac::{Hmac, KeyInit, Mac};
use sha2_011::Sha256;
use super::host::{self, Profile};
+use super::lease::OpenError;
use super::types::ProfileId;
/// The gateway's user header.
@@ -69,19 +74,63 @@ pub fn verify(secret: &str, user_id: &str, header: &str, now: u64) -> Result<(),
.map_err(|_| "user signature does not match".to_string())
}
+/// The `error` code of a refusal for a profile another node hosts.
+pub const PROFILE_HELD: &str = "profile_held";
+
+/// The response header naming the node that hosts a refused profile.
+pub const PROFILE_OWNER_HEADER: &str = "x-openhuman-profile-owner";
+
+/// The node that hosts a profile this one was asked for.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct HeldBy {
+ /// The holder's node id.
+ pub owner: String,
+ /// Where the holder can be reached, when it advertises an endpoint.
+ pub endpoint: Option<String>,
+ /// Milliseconds until its lease lapses unless renewed.
+ pub retry_after_ms: u64,
+}
+
/// Why a gateway request was refused.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GatewayRefusal {
/// The HTTP status to answer with.
pub status: u16,
pub message: String,
+ /// Set on a `409`: who hosts the profile instead.
+ pub held_by: Option<HeldBy>,
}
impl GatewayRefusal {
- fn new(status: u16, message: impl Into<String>) -> Self {
+ pub fn new(status: u16, message: impl Into<String>) -> Self {
Self {
status,
message: message.into(),
+ held_by: None,
+ }
+ }
+
+ /// The refusal for profile `id`'s [`OpenError`] at `now_ms`.
+ pub fn from_open_error(id: &ProfileId, error: OpenError, now_ms: u64) -> Self {
+ match error {
+ OpenError::NotProvisioned(_) => Self::new(403, error.to_string()),
+ OpenError::Full { .. } | OpenError::Storage(_) => Self::new(503, error.to_string()),
+ OpenError::HeldElsewhere(record) => {
+ log::debug!(
+ "[profiles][gateway] profile={id} is held by node={} epoch={}",
+ record.owner,
+ record.epoch
+ );
+ Self {
+ status: 409,
+ message: PROFILE_HELD.to_string(),
+ held_by: Some(HeldBy {
+ retry_after_ms: record.retry_after_ms(now_ms),
+ owner: record.owner,
+ endpoint: record.endpoint,
+ }),
+ }
+ }
}
}
}
@@ -98,7 +147,7 @@ pub enum GatewayScope {
/// Pick the scope for a request that already presented the service bearer.
///
/// `secret` is the service bearer; `now` is unix seconds.
-pub fn resolve_scope(
+pub async fn resolve_scope(
user_id: Option<&str>,
signature: Option<&str>,
secret: &str,
@@ -118,13 +167,16 @@ pub fn resolve_scope(
GatewayRefusal::new(401, e)
})?;
}
- match host.open(&profile) {
+ match host.open(&profile).await {
Ok(state) => {
log::debug!("[profiles][gateway] scoped request to profile={profile}");
Ok(GatewayScope::User(state))
}
- Err(e) if e.contains("not provisioned") => Err(GatewayRefusal::new(403, e)),
- Err(e) => Err(GatewayRefusal::new(503, e)),
+ Err(error) => Err(GatewayRefusal::from_open_error(
+ &profile,
+ error,
+ super::lease::now_ms(),
+ )),
}
}
diff --git a/crates/openhuman-core/src/profiles/gateway_tests.rs b/crates/openhuman-core/src/profiles/gateway_tests.rs
index aeb3730288..acfc6ff093 100644
--- a/crates/openhuman-core/src/profiles/gateway_tests.rs
+++ b/crates/openhuman-core/src/profiles/gateway_tests.rs
@@ -47,10 +47,51 @@ fn a_tampered_tag_is_refused() {
.contains("does not match"));
}
-#[test]
-fn no_user_header_is_the_operator_plane() {
+#[tokio::test]
+async fn no_user_header_is_the_operator_plane() {
assert!(matches!(
- resolve_scope(None, None, SECRET, 0),
+ resolve_scope(None, None, SECRET, 0).await,
Ok(GatewayScope::Operator)
));
}
+
+fn id(name: &str) -> ProfileId {
+ ProfileId::parse(name).unwrap()
+}
+
+#[test]
+fn a_profile_held_elsewhere_is_a_409_naming_the_holder() {
+ let record = crate::storage::lease::LeaseRecord {
+ owner: "node-a".into(),
+ endpoint: Some("http://10.0.0.1:7788".into()),
+ epoch: 3,
+ expires_at_ms: 10_000,
+ released: false,
+ };
+ let refusal = GatewayRefusal::from_open_error(
+ &id("alice"),
+ OpenError::HeldElsewhere(record),
+ 4_000,
+ );
+ assert_eq!(refusal.status, 409);
+ assert_eq!(refusal.message, PROFILE_HELD);
+ assert_eq!(
+ refusal.held_by,
+ Some(HeldBy {
+ owner: "node-a".into(),
+ endpoint: Some("http://10.0.0.1:7788".into()),
+ retry_after_ms: 6_000,
+ })
+ );
+}
+
+#[test]
+fn other_open_errors_keep_their_statuses() {
+ let alice = id("alice");
+ let status = |error| GatewayRefusal::from_open_error(&alice, error, 0).status;
+ assert_eq!(status(OpenError::NotProvisioned(alice.clone())), 403);
+ assert_eq!(status(OpenError::Full { max: 2 }), 503);
+ assert_eq!(status(OpenError::Storage("down".into())), 503);
+ let refusal = GatewayRefusal::from_open_error(&alice, OpenError::Full { max: 2 }, 0);
+ assert!(refusal.held_by.is_none());
+}