feat: require authentication and own uploads per Outpost (auth M0)

- auth is mandatory by default, including loopback and local proxies; startup refuses a hub with no credentials; TESSERA_LOCAL_DEVELOPMENT is explicit and loopback-only
- legacy uploads are bound to an owning Outpost (PUT /api/v1/orbits/:id/outpost); unowned orbits and sessions fail closed
- typed Principal and payload-free audit_events table
- endpoint authorization contract with tests comparing it to the router
- A00 compatibility spike (tests/auth-compat) and library research
- archive the superseded A00-A25 plan

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 12:59:08 +07:00
parent f6a41ee702
commit a9ebded5ba
22 changed files with 5355 additions and 35 deletions

View File

@@ -0,0 +1,112 @@
use super::*;
use crate::auth::{AuditAction, AuditOutcome, AuditResource, Principal};
pub(super) fn migrate(conn: &Connection) -> Result<()> {
conn.execute_batch("CREATE TABLE IF NOT EXISTS audit_events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
at INTEGER NOT NULL,
request_id TEXT NOT NULL,
actor_id TEXT NOT NULL,
actor_kind TEXT NOT NULL CHECK(actor_kind IN ('user','service','outpost','anonymous')),
auth_method TEXT NOT NULL,
action TEXT NOT NULL,
resource_type TEXT NOT NULL,
resource_id TEXT,
outcome TEXT NOT NULL CHECK(outcome IN ('allowed','denied','succeeded','failed'))
);
CREATE INDEX IF NOT EXISTS audit_events_actor_at ON audit_events(actor_id,at);
CREATE INDEX IF NOT EXISTS audit_events_resource_at ON audit_events(resource_type,resource_id,at);")?;
Ok(())
}
impl Db {
pub fn audit_event(
&self,
actor: &Principal,
action: AuditAction,
resource: AuditResource,
outcome: AuditOutcome,
request_id: Uuid,
) -> Result<()> {
let (id, kind, method) = actor.fields();
let (resource_type, resource_id) = resource.fields();
self.conn.lock().unwrap().execute(
"INSERT INTO audit_events(at,request_id,actor_id,actor_kind,auth_method,action,resource_type,resource_id,outcome)
VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9)",
params![chrono::Utc::now().timestamp(), request_id.to_string(), id, kind, method,
action.as_str(), resource_type, resource_id, outcome.as_str()],
)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn structured_audit_survives_reopen_and_preserves_legacy_events() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("fixture.db");
let db = Db::connect(&path).await.unwrap();
db.migrate().await.unwrap();
db.audit("legacy.fixture", "legacy audit remains")
.await
.unwrap();
let request = Uuid::new_v4();
let orbit = Uuid::new_v4();
db.audit_event(
&Principal::Emergency("devops-alice".into()),
AuditAction::OutpostBind,
AuditResource::Orbit(orbit),
AuditOutcome::Succeeded,
request,
)
.unwrap();
db.audit_event(
&Principal::Outpost(Uuid::new_v4()),
AuditAction::Authorization,
AuditResource::Outpost(Uuid::new_v4()),
AuditOutcome::Denied,
Uuid::new_v4(),
)
.unwrap();
drop(db);
let reopened = Db::connect(&path).await.unwrap();
reopened.migrate().await.unwrap();
let conn = reopened.conn.lock().unwrap();
let row: (String,String,String,String,String) = conn.query_row(
"SELECT actor_id,actor_kind,action,resource_id,request_id FROM audit_events WHERE outcome='succeeded'",
[], |r| Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?))).unwrap();
assert_eq!(
row,
(
"emergency:devops-alice".into(),
"user".into(),
"orbit.outpost_bind".into(),
orbit.to_string(),
request.to_string()
)
);
assert_eq!(
conn.query_row(
"SELECT count(*) FROM audit_log WHERE event='legacy.fixture'",
[],
|r| r.get::<_, i64>(0)
)
.unwrap(),
1
);
// The typed event accepts neither a password nor arbitrary client detail/payload.
let columns: Vec<String> = conn
.prepare("PRAGMA table_info(audit_events)")
.unwrap()
.query_map([], |r| r.get(1))
.unwrap()
.collect::<rusqlite::Result<_>>()
.unwrap();
assert!(!columns
.iter()
.any(|name| ["detail", "payload", "token", "password"].contains(&name.as_str())));
}
}