diff --git a/infra/lib/database-stack.ts b/infra/lib/database-stack.ts index 9c4c11d4e..cfe1de395 100644 --- a/infra/lib/database-stack.ts +++ b/infra/lib/database-stack.ts @@ -433,8 +433,9 @@ export class DatabaseStack extends cdk.Stack { partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING }, sortKey: { name: "sk", type: dynamodb.AttributeType.STRING }, billing: dynamodb.Billing.onDemand(), - // Remote MCP sign-ins in progress (ten minutes) and the tool-call audit - // trail (90 days) carry `ttl`; server and token records never do. + // Remote MCP sign-ins in progress (ten minutes), the tool-call audit + // trail (90 days) and the hosted hybrid broker's holding partitions + // (a day) carry `ttl`; a team's own server and token records never do. timeToLiveAttribute: "ttl", encryption: dynamodb.TableEncryptionV2.customerManagedKey( this.encryptionKey diff --git a/services/common/src/mcp_remote/store.rs b/services/common/src/mcp_remote/store.rs index d261b3878..c60943a56 100644 --- a/services/common/src/mcp_remote/store.rs +++ b/services/common/src/mcp_remote/store.rs @@ -359,6 +359,7 @@ impl ServerRecord { pub fn to_item(&self, team: &str) -> Item { let mut i = Item::new(); + add_holding_ttl(&mut i, team); i.insert("pk".into(), av(team)); i.insert("sk".into(), av(self.sk())); i.insert("name".into(), av(&self.name)); @@ -525,9 +526,31 @@ pub fn token_sk(server_id: &str, owner: &Owner) -> String { format!("{TOKEN_PREFIX}{server_id}#{}", owner.key()) } +/// Marks a holding partition: a team key with this in it holds one sign-in +/// that isn't anyone's yet (the hosted hybrid broker finishes a provider +/// sign-in there and moves it to the install only when the install redeems +/// the code). Its server and token records expire on their own. +pub const HOLDING_MARKER: &str = "#SIGNIN#"; + +/// How long a holding partition's records live when nobody takes them. +pub const HOLDING_TTL_SECS: i64 = 24 * 3600; + +/// When a record in `team` expires: only in a holding partition. +pub fn holding_expiry(team: &str, now: i64) -> Option { + team.contains(HOLDING_MARKER) + .then_some(now + HOLDING_TTL_SECS) +} + +fn add_holding_ttl(i: &mut Item, team: &str) { + if let Some(t) = holding_expiry(team, chrono::Utc::now().timestamp()) { + i.insert("ttl".into(), num(t)); + } +} + impl TokenRecord { pub fn to_item(&self, team: &str, server_id: &str) -> Item { let mut i = Item::new(); + add_holding_ttl(&mut i, team); i.insert("pk".into(), av(team)); i.insert("sk".into(), av(token_sk(server_id, &self.owner))); i.insert("token_sealed".into(), av(&self.sealed)); @@ -1262,6 +1285,37 @@ pub mod fake { mod tests { use super::*; + /// A holding partition's sign-in that nobody takes is gone within a + /// day; every other team's records never expire. + #[test] + fn holding_partitions_expire_and_nothing_else_does() { + let now = chrono::Utc::now().timestamp(); + let hold = "HYBRID#ins_abcdefghijklmnopqrstuvwx#SIGNIN#fl_x"; + let s = ServerRecord::new("docs", "Docs", "https://mcp.example.com/mcp"); + let t = TokenRecord { + owner: Owner::Team, + sealed: "x".into(), + expires_at: None, + scope: String::new(), + version: 1, + status: Status::Connected, + status_reason: String::new(), + step_up: vec![], + connected_by: String::new(), + connected_at: String::new(), + }; + for item in [s.to_item(hold), t.to_item(hold, "docs")] { + let ttl: i64 = item["ttl"].as_n().unwrap().parse().unwrap(); + assert!(ttl >= now + HOLDING_TTL_SECS && ttl <= now + HOLDING_TTL_SECS + 60); + } + for team in ["TEAM#a", "HYBRID#ins_abcdefghijklmnopqrstuvwx"] { + assert!(!s.to_item(team).contains_key("ttl"), "{team}"); + assert!(!t.to_item(team, "docs").contains_key("ttl"), "{team}"); + } + assert_eq!(holding_expiry("TEAM#a", 10), None); + assert_eq!(holding_expiry(hold, 10), Some(10 + HOLDING_TTL_SECS)); + } + fn sample() -> ServerRecord { let mut s = ServerRecord::new("docs", "Docs", "https://mcp.example.com/mcp"); s.auth = AuthKind::OAuth; diff --git a/services/gateway/src/routes/hybrid.rs b/services/gateway/src/routes/hybrid.rs index de1bbbb6e..cb348f0cb 100644 --- a/services/gateway/src/routes/hybrid.rs +++ b/services/gateway/src/routes/hybrid.rs @@ -2150,7 +2150,11 @@ fn hosted_team(install: &str) -> String { /// authorize link is public, so whoever follows it could otherwise put their /// own provider account (or nothing) in place of the install's. pub fn signin_team(install: &str, flow: &str) -> String { - format!("{}#SIGNIN#{flow}", relay::install_pk(install)) + format!( + "{}{}{flow}", + relay::install_pk(install), + common::mcp_remote::store::HOLDING_MARKER + ) } /// A flow id: what [`signin_team`] is keyed by. @@ -2993,6 +2997,9 @@ mod tests { let hold = signin_team(id, &flow); assert_ne!(hold, hosted_team(id)); assert!(hold.starts_with(&format!("{}#SIGNIN#fl_", hosted_team(id)))); + // Its records expire by themselves if the install never takes them. + assert!(common::mcp_remote::store::holding_expiry(&hold, 0).is_some()); + assert!(common::mcp_remote::store::holding_expiry(&hosted_team(id), 0).is_none()); assert!(!is_flow("")); assert!(!is_flow("fl_short")); assert!(!is_flow(&format!("fl_{}#x", "a".repeat(38))));