The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(security): add device store import support
Steven Enamakel2ddfcf8c2a50
diff --git a/crates/openhuman-core/src/security/devices/mod.rs b/crates/openhuman-core/src/security/devices/mod.rsindex f0b670bd91..c9c76e6a76 100644--- a/crates/openhuman-core/src/security/devices/mod.rs+++ b/crates/openhuman-core/src/security/devices/mod.rs@@ -10,6 +10,7 @@ pub mod rpc;pub mod schemas;pub mod store;mod store_documents;+mod store_import;pub(crate) use store_documents::import_source;pub mod tunnel_client;pub mod types;diff --git a/crates/openhuman-core/src/security/devices/store.rs b/crates/openhuman-core/src/security/devices/store.rsindex 1448f79ae1..0ccf63b429 100644--- a/crates/openhuman-core/src/security/devices/store.rs+++ b/crates/openhuman-core/src/security/devices/store.rs@@ -1,17 +1,11 @@-//! SQLite persistence for paired devices.+//! Paired devices.//!-//! With a storage backend configured ([`crate::storage`]) every function-//! here is served from the document port instead (`store_documents.rs`).-//!-//! Follows the same `with_connection` pattern as `cron/store.rs`:-//! open a per-call connection to a domain-scoped `.db` file inside the-//! workspace directory, execute DDL on each open (idempotent), then run-//! the requested query and return.+//! Every function here is served from the document port+//! (`store_documents.rs`), in the storage backend and the acting scope. The+//! `devices/devices.db` older installs wrote is read once, read-only, by the+//! storage importer (`store_import.rs`).-use crate::storage::rusqlite;-use anyhow::{Context, Result};-use chrono::Utc;-use rusqlite::{params, Connection};+use anyhow::Result;use crate::config::Config;use crate::security::devices::types::PairedDevice;@@ -28,154 +22,34 @@ pub fn insert_device(device_pubkey: &str,core_session_token_hash: &str,) -> Result<PairedDevice> {- if let Some(docs) = super::store_documents::current(config)? {- return docs.insert_device(channel_id, label, device_pubkey, core_session_token_hash);- }- let now = Utc::now().to_rfc3339();- with_connection(config, |conn| {- conn.execute(- "INSERT OR REPLACE INTO paired_devices \- (channel_id, label, device_pubkey, core_session_token_hash, \- shared_secret_encrypted, created_at, last_seen_at, revoked) \- VALUES (?1, ?2, ?3, ?4, NULL, ?5, NULL, 0)",- params![- channel_id,- label,- device_pubkey,- core_session_token_hash,- now- ],- )- .context("insert_device: INSERT failed")?;- Ok(())- })?;- get_device(config, channel_id)?.ok_or_else(|| anyhow::anyhow!("device not found after insert"))+ super::store_documents::current(config)?.insert_device(+ channel_id,+ label,+ device_pubkey,+ core_session_token_hash,+ )}/// Update `last_seen_at` for a device (called on `tunnel:peer-status` online events).pub fn touch_device(config: &Config, channel_id: &str) -> Result<()> {- if let Some(docs) = super::store_documents::current(config)? {- return docs.touch_device(channel_id);- }- let now = Utc::now().to_rfc3339();- with_connection(config, |conn| {- conn.execute(- "UPDATE paired_devices SET last_seen_at = ?1 WHERE channel_id = ?2 AND revoked = 0",- params![now, channel_id],- )- .context("touch_device: UPDATE failed")?;- Ok(())- })+ super::store_documents::current(config)?.touch_device(channel_id)}/// Mark a device as revoked (soft delete).pub fn revoke_device(config: &Config, channel_id: &str) -> Result<bool> {- if let Some(docs) = super::store_documents::current(config)? {- return docs.revoke_device(channel_id);- }- let rows = with_connection(config, |conn| {- conn.execute(- "UPDATE paired_devices SET revoked = 1 WHERE channel_id = ?1",- params![channel_id],- )- .context("revoke_device: UPDATE failed")- })?;- Ok(rows > 0)+ super::store_documents::current(config)?.revoke_device(channel_id)}/// Load a single paired device by channel_id (returns None if not found).pub fn get_device(config: &Config, channel_id: &str) -> Result<Option<PairedDevice>> {- if let Some(docs) = super::store_documents::current(config)? {- return docs.get_device(channel_id);- }- with_connection(config, |conn| {- let mut stmt = conn.prepare(- "SELECT channel_id, label, device_pubkey, created_at, last_seen_at, revoked \- FROM paired_devices WHERE channel_id = ?1",- )?;- let mut rows = stmt.query_map(params![channel_id], map_device_row)?;- rows.next().transpose().map_err(Into::into)- })+ super::store_documents::current(config)?.get_device(channel_id)}/// List all non-revoked paired devices ordered by creation time.pub fn list_devices(config: &Config) -> Result<Vec<PairedDevice>> {- if let Some(docs) = super::store_documents::current(config)? {- return docs.list_devices();- }- with_connection(config, |conn| {- let mut stmt = conn.prepare(- "SELECT channel_id, label, device_pubkey, created_at, last_seen_at, revoked \- FROM paired_devices WHERE revoked = 0 ORDER BY created_at ASC",- )?;- let rows = stmt.query_map([], map_device_row)?;- rows.collect::<rusqlite::Result<Vec<_>>>()- .map_err(Into::into)- })-}--// ----------------------------------------------------------------------------// Private helpers-// -----------------------------------------------------------------------------fn map_device_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<PairedDevice> {- Ok(PairedDevice {- channel_id: row.get(0)?,- label: row.get(1)?,- device_pubkey: row.get(2)?,- created_at: row.get(3)?,- last_seen_at: row.get(4)?,- peer_online: None, // populated from in-memory peer-status map, not SQLite- revoked: row.get::<_, i64>(5)? != 0,- })-}--pub(super) fn db_path(config: &Config) -> std::path::PathBuf {- config.workspace_dir.join("devices").join("devices.db")-}--pub(super) fn with_connection<T>(- config: &Config,- f: impl FnOnce(&Connection) -> Result<T>,-) -> Result<T> {- let db_path = db_path(config);- if let Some(parent) = db_path.parent() {- std::fs::create_dir_all(parent)- .with_context(|| format!("create devices dir: {}", parent.display()))?;- }-- let conn = Connection::open(&db_path)- .with_context(|| format!("open devices DB: {}", db_path.display()))?;-- conn.execute_batch(- "PRAGMA foreign_keys = ON;- CREATE TABLE IF NOT EXISTS paired_devices (- channel_id TEXT PRIMARY KEY,- label TEXT NOT NULL,- device_pubkey TEXT NOT NULL,- core_session_token_hash TEXT NOT NULL,- shared_secret_encrypted BLOB,- created_at TEXT NOT NULL,- last_seen_at TEXT,- revoked INTEGER NOT NULL DEFAULT 0- );",- )- .context("devices DDL failed")?;-- log::debug!(- "[devices/store] connection opened path={}",- db_path.display()- );- f(&conn)+ super::store_documents::current(config)?.list_devices()}-#[path = "store_import.rs"]-pub(super) mod import;--// ----------------------------------------------------------------------------// Tests-// ----------------------------------------------------------------------------#[cfg(test)]#[path = "store_tests.rs"]mod tests;diff --git a/crates/openhuman-core/src/security/devices/store_documents.rs b/crates/openhuman-core/src/security/devices/store_documents.rsindex f4b94b6b92..9e8f7bd143 100644--- a/crates/openhuman-core/src/security/devices/store_documents.rs+++ b/crates/openhuman-core/src/security/devices/store_documents.rs@@ -30,7 +30,7 @@ pub(crate) fn import_source() -> crate::storage::import::Source {domain: "devices",db: "devices/devices.db",collections,- read_legacy: |scratch, _real| super::store::import::read(scratch),+ read_legacy: |scratch, _real| super::store_import::read(scratch),rekey: |record, _config| record,}}diff --git a/crates/openhuman-core/src/security/devices/store_import.rs b/crates/openhuman-core/src/security/devices/store_import.rsindex 6f52aa7cb5..a108de93e7 100644--- a/crates/openhuman-core/src/security/devices/store_import.rs+++ b/crates/openhuman-core/src/security/devices/store_import.rs@@ -2,8 +2,8 @@//! document layout (`store_documents.rs`): one `paired_devices` document per//! `channel_id`, with the columns as fields and `revoked` as a boolean.//! `shared_secret_encrypted` was never written by the store and is not-//! carried over. The driver ([`crate::storage::local`]) writes the documents-//! and then renames the table to `_legacy_paired_devices`.+//! carried over. The legacy file is only ever opened by the storage importer+//! (on a scratch copy of it), never by the running store.use crate::storage::rusqlite;use anyhow::{Context, Result};@@ -14,12 +14,44 @@ use super::super::store_documents::DEVICES;use crate::config::Config;use crate::storage::import::{table_exists, ImportDoc};-/// The legacy tables this import retires.-pub(crate) const TABLES: &[&str] = &["paired_devices"];+/// The legacy database, `<workspace>/devices/devices.db`.+pub(crate) fn db_path(config: &Config) -> std::path::PathBuf {+ config.workspace_dir.join("devices").join("devices.db")+}++/// Opens the legacy database (creating the table when absent, which only+/// happens on the importer's scratch copy) and runs `f` on it.+pub(crate) fn with_connection<T>(+ config: &Config,+ f: impl FnOnce(&Connection) -> Result<T>,+) -> Result<T> {+ let db_path = db_path(config);+ if let Some(parent) = db_path.parent() {+ std::fs::create_dir_all(parent)+ .with_context(|| format!("create devices dir: {}", parent.display()))?;+ }+ let conn = Connection::open(&db_path)+ .with_context(|| format!("open devices DB: {}", db_path.display()))?;+ conn.execute_batch(+ "PRAGMA foreign_keys = ON;+ CREATE TABLE IF NOT EXISTS paired_devices (+ channel_id TEXT PRIMARY KEY,+ label TEXT NOT NULL,+ device_pubkey TEXT NOT NULL,+ core_session_token_hash TEXT NOT NULL,+ shared_secret_encrypted BLOB,+ created_at TEXT NOT NULL,+ last_seen_at TEXT,+ revoked INTEGER NOT NULL DEFAULT 0+ );",+ )+ .context("devices DDL failed")?;+ f(&conn)+}/// Reads every legacy row.pub(crate) fn read(config: &Config) -> Result<Vec<ImportDoc>> {- super::with_connection(config, read_rows)+ with_connection(config, read_rows)}fn read_rows(conn: &Connection) -> Result<Vec<ImportDoc>> {