//! The daemon against a real SpiceDB and a real Postgres (`docker compose up -d --wait` at the repo root). //! Runs only with REBAC_IT=1, and says SKIPPED loudly otherwise: a fence test that silently passes is worse than none. use rebacd::{ backfill, closure, config::{Config, Mode}, loops, project, relay, spicedb::{SpiceDb, TupleOp}, verify, Ctx, }; use std::time::Duration; const SCHEMA: &str = r#" definition user {} definition workspace { relation member: user relation admin: user } definition group { relation member: user } definition container { relation workspace: workspace relation parent: container relation member: user | group#member relation editor: user permission view = member + editor + workspace->admin + parent->view } "#; fn enabled() -> bool { if std::env::var("REBAC_IT").as_deref() == Ok("1") { return true; } eprintln!("!!!!!!!! SKIPPED: the rebacd integration test needs REBAC_IT=1 and `docker compose up -d --wait`"); false } const PG: &str = "postgres://postgres:rebac@127.0.0.1:55433"; const KEY: &str = "rebac-test-key"; async fn fresh_db(name: &str) -> String { let (admin, conn) = tokio_postgres::connect(&format!("{PG}/postgres"), tokio_postgres::NoTls) .await .unwrap(); tokio::spawn(conn); admin .batch_execute(&format!("drop database if exists {name} with (force)")) .await .unwrap(); admin .batch_execute(&format!("create database {name}")) .await .unwrap(); let url = format!("{PG}/{name}"); let (db, conn) = tokio_postgres::connect(&url, tokio_postgres::NoTls) .await .unwrap(); tokio::spawn(conn); let sql = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/../../sql/rebac.sql")) .unwrap(); db.batch_execute(&format!("set client_min_messages = warning; {sql}")) .await .unwrap(); url } fn config(url_env: &str, endpoint: &str, mode: Mode) -> Config { Config { database_url_env: url_env.into(), spicedb_endpoint: endpoint.into(), spicedb_token_env: "REBAC_IT_TOKEN".into(), mode, deadline_ms: 2_000, read_deadline_ms: 2_000, ..Config::default() } } fn op(touch: bool, resource: &str, relation: &str, subject: &str) -> TupleOp { TupleOp { touch, resource: resource.into(), relation: relation.into(), subject: subject.into(), } } async fn set_of(ctx: &Ctx, tenant: &str, p: &str) -> Vec { let c = ctx.pool.get().await.unwrap(); c.query( "select container from rebac.readable where tenant = $1 and principal = $2 order by 1", &[&tenant, &p], ) .await .unwrap() .iter() .map(|r| r.get(0)) .collect() } async fn sql(ctx: &Ctx, q: &str) { ctx.pool .get() .await .unwrap() .batch_execute(q) .await .unwrap(); } #[tokio::test(flavor = "multi_thread")] async fn the_daemon_end_to_end() { if !enabled() { return; } std::env::set_var("REBAC_IT_TOKEN", KEY); let url = fresh_db("rebacd_it").await; std::env::set_var("REBAC_IT_DB", &url); let endpoint = std::env::var("REBAC_IT_SPICEDB").unwrap_or_else(|_| "http://127.0.0.1:50061".into()); // A fresh SpiceDB: the schema, and relationships written by "someone else" before pg_rebac existed. let s = SpiceDb::connect(&endpoint, KEY, rebacd::config::Consistency::FullyConsistent) .await .unwrap(); // A store left by an earlier run may hold tuples the schema below drops: clear them first (best effort). for ty in ["container", "workspace", "group"] { let _ = s.delete_all(ty, Duration::from_secs(5)).await; } s.write_schema(SCHEMA, Duration::from_secs(5)) .await .unwrap(); for ty in ["container", "workspace", "group"] { s.delete_all(ty, Duration::from_secs(5)).await.unwrap(); } s.write( &[ op(true, "container:acme/space", "member", "user:acme/ana"), op( true, "container:acme/file", "parent", "container:acme/space", ), op( true, "container:acme/w1doc", "workspace", "workspace:acme/w1", ), op(true, "workspace:acme/w1", "admin", "user:acme/adm"), op(true, "container:acme/other", "editor", "user:acme/bo"), // The same principal id in another tenant reads that tenant's containers only. op(true, "container:globex/g1", "member", "user:globex/ana"), // A grant to a group, and one member of it. op( true, "container:acme/team", "member", "group:acme/eng#member", ), op(true, "group:acme/eng", "member", "user:acme/dee"), ], Duration::from_secs(5), ) .await .unwrap(); let ctx = Ctx::connect(config("REBAC_IT_DB", &endpoint, Mode::Live)) .await .unwrap(); // CLOSURE: derived from the live schema at start, and a stored list that disagrees refuses the next start. closure::check_at_start(&ctx, false).await.unwrap(); let n: i64 = ctx .pool .get() .await .unwrap() .query_one("select count(*) from rebac.view_relation", &[]) .await .unwrap() .get(0); assert_eq!( n, 6, "member, editor, parent, workspace, workspace#admin, group#member" ); sql( &ctx, "delete from rebac.view_relation where relation = 'editor'", ) .await; assert!( closure::check_at_start(&ctx, false).await.is_err(), "a closure missing a view relation refuses the start" ); closure::check_at_start(&ctx, true).await.unwrap(); // BACKFILL: edges mirrored, every named principal projected through arrows and the via relation. let b = backfill::backfill(&ctx, "acme").await.unwrap(); assert_eq!(b.failed, 0); assert!(b.edges >= 2, "parent and workspace edges mirrored: {b:?}"); assert_eq!(set_of(&ctx, "acme", "ana").await, vec!["file", "space"]); assert_eq!(set_of(&ctx, "acme", "adm").await, vec!["w1doc"]); assert_eq!(set_of(&ctx, "acme", "bo").await, vec!["other"]); assert_eq!( set_of(&ctx, "acme", "dee").await, vec!["team"], "read through a group" ); let set_grants: i64 = ctx .pool .get() .await .unwrap() .query_one( "select count(*) from rebac.set_grant where tenant = 'acme'", &[], ) .await .unwrap() .get(0); assert_eq!( set_grants, 1, "the grant to the group is mirrored by backfill" ); backfill::backfill(&ctx, "globex").await.unwrap(); assert_eq!( set_of(&ctx, "globex", "ana").await, vec!["g1"], "tenants stay apart" ); let d = verify::diff(&ctx, "acme", None, verify::Against::SpiceDb, "readable") .await .unwrap(); assert_eq!( (d.extra_rows, d.missing_rows, d.unavailable), (0, 0, 0), "{d:?}" ); let iso = verify::isolation(&ctx).await.unwrap(); assert_eq!(iso.extra_rows, 0); // A GRANT through rebac.write: relayed, then refreshed at least as fresh as its own write. sql(&ctx, "set rebac.tenant = 'acme'; select rebac.write('touch', 'container:acme/new', 'member', 'user:acme/ana')").await; assert_eq!(relay::relay_once(&ctx).await.unwrap(), 1); let tok: Option = ctx .pool .get() .await .unwrap() .query_one( "select zedtoken from rebac.refresh where tenant = 'acme' and principal = 'ana'", &[], ) .await .unwrap() .get(0); assert!(tok.is_some(), "the refresh carries the delivery's zedtoken"); loops::refresh_pass(&ctx, 2).await.unwrap(); assert_eq!( set_of(&ctx, "acme", "ana").await, vec!["file", "new", "space"] ); // A REVOCATION is in force at COMMIT, before the relay has run, and SpiceDB follows when it does. sql(&ctx, "begin; set local rebac.tenant = 'acme'; select rebac.write('delete', 'container:acme/space', 'member', 'user:acme/ana'); commit").await; assert_eq!( set_of(&ctx, "acme", "ana").await, vec!["new"], "the space and its file are gone at commit" ); // A refresh before delivery cannot restore it: the pending delete is subtracted. project::refresh_one(&ctx, "acme", "ana", None, Duration::from_secs(2)) .await .unwrap(); assert_eq!( set_of(&ctx, "acme", "ana").await, vec!["new"], "no regrowth from a pending revocation" ); relay::relay_once(&ctx).await.unwrap(); loops::refresh_pass(&ctx, 2).await.unwrap(); assert_eq!(set_of(&ctx, "acme", "ana").await, vec!["new"]); let d = verify::diff(&ctx, "acme", None, verify::Against::SpiceDb, "readable") .await .unwrap(); assert_eq!(d.extra_rows, 0, "{d:?}"); // An arrow touch names no reader: LookupSubjects finds who can now see the file. sql(&ctx, "set rebac.tenant = 'acme'; select rebac.write('touch', 'container:acme/file2', 'parent', 'container:acme/other')").await; relay::relay_once(&ctx).await.unwrap(); loops::refresh_pass(&ctx, 2).await.unwrap(); // the resource queue: LookupSubjects, enqueue bo loops::refresh_pass(&ctx, 2).await.unwrap(); // bo refreshed assert_eq!(set_of(&ctx, "acme", "bo").await, vec!["file2", "other"]); // SUBJECT SETS: a new member reads what the group was granted after delivery; removed, it is gone at commit. sql(&ctx, "set rebac.tenant = 'acme'; select rebac.write('touch', 'group:acme/eng', 'member', 'user:acme/eve')").await; relay::relay_once(&ctx).await.unwrap(); loops::refresh_pass(&ctx, 2).await.unwrap(); assert_eq!(set_of(&ctx, "acme", "eve").await, vec!["team"]); sql(&ctx, "set rebac.tenant = 'acme'; select rebac.write('delete', 'group:acme/eng', 'member', 'user:acme/dee')").await; assert!( set_of(&ctx, "acme", "dee").await.is_empty(), "a membership removed narrows at commit" ); project::refresh_one(&ctx, "acme", "dee", None, Duration::from_secs(2)) .await .unwrap(); assert!( set_of(&ctx, "acme", "dee").await.is_empty(), "and no refresh regrows it while pending" ); relay::relay_once(&ctx).await.unwrap(); loops::refresh_pass(&ctx, 2).await.unwrap(); assert!(set_of(&ctx, "acme", "dee").await.is_empty()); assert_eq!(set_of(&ctx, "acme", "eve").await, vec!["team"]); // The workspace admin revoked: narrowed through the edge at commit. sql(&ctx, "set rebac.tenant = 'acme'; select rebac.write('delete', 'workspace:acme/w1', 'admin', 'user:acme/adm')").await; assert!(set_of(&ctx, "acme", "adm").await.is_empty()); relay::relay_once(&ctx).await.unwrap(); // SPICEDB DOWN: a revocation is still in force at commit; the relay fails and marks nothing delivered. let down = Ctx::connect(config("REBAC_IT_DB", "http://127.0.0.1:1", Mode::Live)) .await .unwrap(); sql(&down, "set rebac.tenant = 'acme'; select rebac.write('delete', 'container:acme/other', 'editor', 'user:acme/bo')").await; assert!( set_of(&down, "acme", "bo").await.is_empty(), "narrowed with SpiceDB unreachable" ); assert!(relay::relay_once(&down).await.is_err()); let unsynced: i64 = down .pool .get() .await .unwrap() .query_one( "select count(*) from rebac.outbox where synced_at is null", &[], ) .await .unwrap() .get(0); assert_eq!(unsynced, 1, "an outage marks nothing delivered"); // A first read with SpiceDB down is unavailable, never empty. assert!( project::refresh_one(&down, "acme", "newcomer", None, Duration::from_millis(300)) .await .is_err() ); drop(down); relay::relay_once(&ctx).await.unwrap(); // A QUARANTINED row: SpiceDB refuses it on its own; the rest of the batch is delivered. sql( &ctx, "set rebac.tenant = 'acme'; select rebac.write('touch', 'container:acme/fine', 'member', 'user:acme/cy'); select rebac.write('touch', 'container:acme/bad', 'nosuchrelation', 'user:acme/cy')", ) .await; // An unknown relation is SpiceDB's FAILED_PRECONDITION: possibly a schema that lags a deploy, so while young the // row is DEFERRED (undelivered, no error, the rest delivered, the pass not counting it), and quarantined once settled. let counts = |c: deadpool_postgres::Object| async move { let r = c .query_one("select count(*) filter (where error is not null), count(*) filter (where synced_at is null) from rebac.outbox", &[]) .await.unwrap(); (r.get::<_, i64>(0), r.get::<_, i64>(1)) }; assert_eq!( relay::relay_once(&ctx).await.unwrap(), 1, "only the delivered row counts" ); assert_eq!( counts(ctx.pool.get().await.unwrap()).await, (0, 1), "deferred: undelivered, not quarantined" ); assert_eq!( relay::relay_once(&ctx).await.unwrap(), 0, "a pass holding only a deferred row idles" ); sql(&ctx, "update rebac.outbox set created_at = now() - interval '11 minutes' where synced_at is null").await; relay::relay_once(&ctx).await.unwrap(); assert_eq!( counts(ctx.pool.get().await.unwrap()).await, (1, 0), "quarantined once past the settle window" ); // SHADOW: a shadow daemon writes only readable_shadow, and the compare is clean. let shadow = Ctx::connect(config("REBAC_IT_DB", &endpoint, Mode::Shadow)) .await .unwrap(); loops::refresh_pass(&ctx, 2).await.unwrap(); let before = set_of(&ctx, "acme", "cy").await; for p in ["ana", "adm", "bo", "cy", "dee", "eve"] { project::refresh_one(&shadow, "acme", p, None, Duration::from_secs(2)) .await .unwrap(); } assert_eq!( set_of(&ctx, "acme", "cy").await, before, "shadow leaves the live projection alone" ); project::refresh_one(&shadow, "globex", "ana", None, Duration::from_secs(2)) .await .unwrap(); let (tenants, all) = verify::all_tenants(&shadow, None, verify::Against::Shadow) .await .unwrap(); assert_eq!( (tenants, all.extra_rows, all.missing_rows), (2, 0, 0), "{all:?}" ); let d = verify::diff(&shadow, "acme", None, verify::Against::Shadow, "readable") .await .unwrap(); assert_eq!((d.extra_rows, d.missing_rows), (0, 0), "{d:?}"); // HEARTBEAT: beaten while SpiceDB answers; reads are fresh, and back-dated past 5 s they fail closed. let (stop_tx, stop) = tokio::sync::watch::channel(false); let hb = tokio::spawn(loops::heartbeat_loop( std::sync::Arc::new( Ctx::connect(config("REBAC_IT_DB", &endpoint, Mode::Live)) .await .unwrap(), ), stop.clone(), )); let mut fresh = false; for _ in 0..50 { let n: i64 = ctx.pool.get().await.unwrap() .query_one("select count(*) from rebac.watch_state where scope = '*' and heartbeat_at > now() - interval '3 seconds'", &[]) .await.unwrap().get(0); if n == 1 { fresh = true; break; } tokio::time::sleep(Duration::from_millis(100)).await; } assert!(fresh, "the daemon beats the heartbeat"); stop_tx.send(true).unwrap(); hb.await.unwrap(); { let c = ctx.pool.get().await.unwrap(); c.batch_execute("set rebac.tenant = 'acme'").await.unwrap(); assert!(c .query("select * from rebac.readable_set('ana')", &[]) .await .is_ok()); c.batch_execute("update rebac.watch_state set heartbeat_at = now() - interval '6 seconds'") .await .unwrap(); let err = c .query("select * from rebac.readable_set('ana')", &[]) .await .unwrap_err(); assert_eq!(err.code().map(|c| c.code()), Some("RB503")); } // WATCH, observe-only: a write made straight to SpiceDB (not through rebac.write) is counted as external. let (stop_tx, stop) = tokio::sync::watch::channel(false); let w = tokio::spawn(loops::watch_loop( std::sync::Arc::new( Ctx::connect(config("REBAC_IT_DB", &endpoint, Mode::Live)) .await .unwrap(), ), stop, )); let mut counted = false; for i in 0..100 { if i % 10 == 0 { s.write( &[op( true, "container:acme/elsewhere", "member", &format!("user:acme/ext{i}"), )], Duration::from_secs(2), ) .await .unwrap(); } let n: Option = ctx .pool .get() .await .unwrap() .query_opt( "select external_writes from rebac.watch_state where scope = 'watch'", &[], ) .await .unwrap() .map(|r| r.get(0)); if n.unwrap_or(0) > 0 { counted = true; break; } tokio::time::sleep(Duration::from_millis(100)).await; } stop_tx.send(true).unwrap(); w.await.unwrap(); assert!(counted, "an external write is counted on Watch"); // FRESHNESS: every set the daemon projected carries when its lookup began, and Watch reports its own position. let c = ctx.pool.get().await.unwrap(); let unknown: i64 = c .query_one( "select count(*) from rebac.principal_state where state = 'projected' and looked_up_at is null", &[], ) .await .unwrap() .get(0); assert_eq!(unknown, 0, "the daemon stamps every lookup"); let watched: bool = c .query_one( "select watched_at is not null from rebac.watch_state where scope = 'watch'", &[], ) .await .unwrap() .get(0); assert!(watched, "Watch records when it last heard from SpiceDB"); drop(c); // LIVE SCHEMA CHANGES. A new relation that grants view: the closure is re-derived, every tenant rebuilt, and the // grant it carries is projected. let with_viewer = SCHEMA.replace( "relation editor: user\n permission view = member + editor", "relation editor: user\n relation viewer: user\n permission view = member + editor + viewer", ); assert_ne!(with_viewer, SCHEMA); s.write_schema(&with_viewer, Duration::from_secs(5)) .await .unwrap(); s.write( &[op(true, "container:acme/vdoc", "viewer", "user:acme/ana")], Duration::from_secs(5), ) .await .unwrap(); assert_eq!( closure::recheck(&ctx).await.unwrap(), closure::Recheck::Rebuilt ); let c = ctx.pool.get().await.unwrap(); let row = c .query_one( "select state, (select count(*) from rebac.view_relation where relation = 'viewer') from rebac.closure_state", &[], ) .await .unwrap(); assert_eq!(row.get::<_, String>(0), "valid", "rebuilt, valid again"); assert_eq!( row.get::<_, i64>(1), 1, "the new relation is in the closure" ); assert!( set_of(&ctx, "acme", "ana") .await .contains(&"vdoc".to_string()), "the rebuild projected the new relation's grant" ); assert_eq!( closure::recheck(&ctx).await.unwrap(), closure::Recheck::Unchanged ); // An unsupported schema (exclusion: removing a tuple can grant) fails every read closed until it is fixed. let excluding = with_viewer .replace( "relation viewer: user\n", "relation viewer: user\n relation banned: user\n", ) .replace("+ parent->view", "+ parent->view - banned"); s.write_schema(&excluding, Duration::from_secs(5)) .await .unwrap(); assert_eq!( closure::recheck(&ctx).await.unwrap(), closure::Recheck::Unsupported ); c.batch_execute("select rebac.heartbeat('*')") .await .unwrap(); let err = c .batch_execute("set rebac.tenant = 'acme'; select * from rebac.readable_set('ana')") .await .unwrap_err(); assert_eq!( err.code().map(|c| c.code()), Some("RB503"), "an unsupported closure is RB503" ); // The closure loop notices on its own (no Watch here), and rebuilds once the schema is supported again. let loop_ctx = std::sync::Arc::new( Ctx::connect(Config { closure_check_ms: 200, ..config("REBAC_IT_DB", &endpoint, Mode::Live) }) .await .unwrap(), ); let (stop_tx, stop) = tokio::sync::watch::channel(false); let cl = tokio::spawn(loops::closure_loop(loop_ctx.clone(), stop)); // SpiceDB refuses to drop a relation that still has tuples. s.write( &[op(false, "container:acme/vdoc", "viewer", "user:acme/ana")], Duration::from_secs(5), ) .await .unwrap(); s.write_schema(SCHEMA, Duration::from_secs(5)) .await .unwrap(); let mut valid = false; for _ in 0..200 { let st: String = c .query_one("select state from rebac.closure_state", &[]) .await .unwrap() .get(0); let viewer: i64 = c .query_one( "select count(*) from rebac.view_relation where relation = 'viewer'", &[], ) .await .unwrap() .get(0); if st == "valid" && viewer == 0 { valid = true; break; } tokio::time::sleep(Duration::from_millis(100)).await; } stop_tx.send(true).unwrap(); cl.await.unwrap(); assert!( valid, "the closure loop re-derived and rebuilt after the schema was fixed" ); assert!( !set_of(&ctx, "acme", "ana") .await .contains(&"vdoc".to_string()), "the relation that left the closure no longer grants" ); // SHADOW MODE (how a shadow deployment runs): a closure change rebuilds the shadow projection, and never touches // the live closure state, the live projection or the mirrors. let shadow = Ctx::connect(config("REBAC_IT_DB", &endpoint, Mode::Shadow)) .await .unwrap(); let snapshot = "select (select state || '@' || changed_at::text from rebac.closure_state), (select count(*) from rebac.readable), (select count(*) from rebac.edge), (select count(*) from rebac.set_grant)"; let before = c.query_one(snapshot, &[]).await.unwrap(); let before: (String, i64, i64, i64) = (before.get(0), before.get(1), before.get(2), before.get(3)); s.write_schema(&with_viewer, Duration::from_secs(5)) .await .unwrap(); s.write( &[op(true, "container:acme/vdoc", "viewer", "user:acme/ana")], Duration::from_secs(5), ) .await .unwrap(); assert_eq!( closure::recheck(&shadow).await.unwrap(), closure::Recheck::Rebuilt ); let after = c.query_one(snapshot, &[]).await.unwrap(); let after: (String, i64, i64, i64) = (after.get(0), after.get(1), after.get(2), after.get(3)); assert_eq!( before, after, "shadow mode leaves the live closure state, projection and mirrors alone" ); let shadow_ana: Vec = c .query( "select container from rebac.readable_shadow where tenant = 'acme' and principal = 'ana'", &[], ) .await .unwrap() .iter() .map(|r| r.get(0)) .collect(); assert!( shadow_ana.contains(&"vdoc".to_string()), "shadow mode rebuilt the shadow under the new closure" ); // Put the store and the live closure back. s.write( &[op(false, "container:acme/vdoc", "viewer", "user:acme/ana")], Duration::from_secs(5), ) .await .unwrap(); s.write_schema(SCHEMA, Duration::from_secs(5)) .await .unwrap(); assert_eq!( closure::recheck(&ctx).await.unwrap(), closure::Recheck::Rebuilt ); }