dragoon-server 0.1.0

Public-relay server for the dragoon remote-executor: axum + rusqlite + ed25519 task signing + per-user message inbox.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! SQLite schema, bootstrap, and forward migrations.
//!
//! Mirrors `python/src/remote_executor/server/db.py`. The schema text is
//! identical so a Python-managed database is readable by the Rust server
//! and vice versa — supporting the rolling Python ↔ Rust migration the
//! plan calls for.

use std::path::Path;

use anyhow::{Context, Result};
use rusqlite::Connection;

pub const SCHEMA_VERSION: i64 = 3;

const SCHEMA_SQL: &str = r"
CREATE TABLE IF NOT EXISTS schema_version (
    version INTEGER PRIMARY KEY
);

CREATE TABLE IF NOT EXISTS users (
    id                INTEGER PRIMARY KEY AUTOINCREMENT,
    username          TEXT NOT NULL UNIQUE,
    password_hash     TEXT NOT NULL,
    totp_secret_enc   TEXT NOT NULL,
    recovery_codes_hash TEXT,
    created_at        TEXT NOT NULL,
    revoked_at        TEXT
);

CREATE TABLE IF NOT EXISTS user_pubkeys (
    id           INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id      INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    fingerprint  TEXT NOT NULL,
    alg          TEXT NOT NULL,
    pubkey_blob  BLOB NOT NULL,
    label        TEXT,
    added_at     TEXT NOT NULL,
    revoked_at   TEXT
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_pubkeys_fp
    ON user_pubkeys(user_id, fingerprint);

CREATE TABLE IF NOT EXISTS sessions (
    token_hash   TEXT PRIMARY KEY,
    user_id      INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    fingerprint  TEXT NOT NULL,
    created_at   TEXT NOT NULL,
    expires_at   TEXT NOT NULL,
    revoked_at   TEXT
);
CREATE INDEX IF NOT EXISTS idx_sessions_user ON sessions(user_id);

CREATE TABLE IF NOT EXISTS nonces (
    user_id     INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    nonce       TEXT NOT NULL,
    expires_at  TEXT NOT NULL,
    PRIMARY KEY (user_id, nonce)
);
CREATE INDEX IF NOT EXISTS idx_nonces_exp ON nonces(expires_at);

CREATE TABLE IF NOT EXISTS challenges (
    challenge   TEXT PRIMARY KEY,
    expires_at  TEXT NOT NULL,
    used_at     TEXT
);
CREATE INDEX IF NOT EXISTS idx_challenges_exp ON challenges(expires_at);

CREATE TABLE IF NOT EXISTS workers (
    id                       INTEGER PRIMARY KEY AUTOINCREMENT,
    name                     TEXT NOT NULL UNIQUE,
    token_hash               TEXT,
    register_code_hash       TEXT,
    register_code_expires    TEXT,
    last_poll_at             TEXT,
    current_pwd              TEXT,
    current_task_id          TEXT,
    status                   TEXT,
    created_at               TEXT NOT NULL,
    revoked_at               TEXT,
    client_pubkey            BLOB
);

CREATE TABLE IF NOT EXISTS server_signing_key (
    id           INTEGER PRIMARY KEY CHECK (id = 1),
    priv_pem     BLOB NOT NULL,
    pub_blob     BLOB NOT NULL,
    fingerprint  TEXT NOT NULL,
    created_at   TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS tasks (
    task_id          TEXT PRIMARY KEY,
    worker_name      TEXT NOT NULL,
    submitter        TEXT NOT NULL,
    kind             TEXT NOT NULL,
    payload          TEXT NOT NULL,
    collect_json     TEXT NOT NULL,
    limits_json      TEXT NOT NULL,
    state            TEXT NOT NULL,
    submitted_at     TEXT NOT NULL,
    started_at       TEXT,
    finished_at      TEXT,
    exit_code        INTEGER,
    final_pwd        TEXT,
    error            TEXT,
    fetch_path       TEXT,
    purged           INTEGER NOT NULL DEFAULT 0,
    last_access_at   TEXT,
    cancel_requested INTEGER NOT NULL DEFAULT 0,
    worker_seq       INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_tasks_worker_state ON tasks(worker_name, state);
CREATE INDEX IF NOT EXISTS idx_tasks_submitted_at ON tasks(submitted_at);
CREATE INDEX IF NOT EXISTS idx_tasks_worker_seq ON tasks(worker_name, worker_seq);

CREATE TABLE IF NOT EXISTS controller_messages (
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id       INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    kind          TEXT NOT NULL,
    task_id       TEXT,
    worker_name   TEXT,
    error         TEXT,
    payload_json  TEXT NOT NULL,
    created_at    TEXT NOT NULL,
    read_at       TEXT
);
CREATE INDEX IF NOT EXISTS idx_msgs_user ON controller_messages(user_id, id);
CREATE INDEX IF NOT EXISTS idx_msgs_unread
    ON controller_messages(user_id, read_at) WHERE read_at IS NULL;

CREATE TABLE IF NOT EXISTS artifacts (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    task_id     TEXT NOT NULL REFERENCES tasks(task_id) ON DELETE CASCADE,
    path        TEXT NOT NULL,
    size        INTEGER NOT NULL,
    sha256      TEXT NOT NULL,
    blob_path   TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_artifacts_task ON artifacts(task_id);

CREATE TABLE IF NOT EXISTS audit_log (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    ts              TEXT NOT NULL,
    actor           TEXT,
    action          TEXT NOT NULL,
    target          TEXT,
    key_fingerprint TEXT,
    metadata_json   TEXT
);
CREATE INDEX IF NOT EXISTS idx_audit_ts ON audit_log(ts);
";

/// Open a SQLite connection at `path`, set WAL + foreign-keys + relaxed
/// fsync (matching the Python `connect()`), and return it. Schema is NOT
/// applied — call [`bootstrap`] afterwards.
pub fn connect(path: impl AsRef<Path>) -> Result<Connection> {
    let conn = Connection::open(path.as_ref())
        .with_context(|| format!("opening sqlite at {}", path.as_ref().display()))?;
    conn.pragma_update(None, "journal_mode", "WAL")?;
    conn.pragma_update(None, "foreign_keys", "ON")?;
    conn.pragma_update(None, "synchronous", "NORMAL")?;
    Ok(conn)
}

/// Open an in-memory SQLite for tests.
pub fn connect_in_memory() -> Result<Connection> {
    let conn = Connection::open_in_memory()?;
    conn.pragma_update(None, "foreign_keys", "ON")?;
    Ok(conn)
}

/// Apply the schema (CREATE TABLE/INDEX IF NOT EXISTS) and run forward
/// migrations from older versions. Idempotent on a v3 database.
///
/// Order matters: when an older schema is detected we run the column
/// additions FIRST so that the v3 SCHEMA_SQL — which references the new
/// columns in `CREATE INDEX` statements — can be applied as a no-op
/// idempotent layer afterwards.
pub fn bootstrap(conn: &Connection) -> Result<()> {
    // Always make sure the version table exists so we can read the cursor
    // before deciding what else to apply.
    conn.execute_batch(
        "CREATE TABLE IF NOT EXISTS schema_version (version INTEGER PRIMARY KEY)",
    )?;
    let mut current: i64 = conn
        .query_row("SELECT version FROM schema_version", [], |r| r.get(0))
        .unwrap_or(0);

    // Forward migrations: bring an existing pre-v3 database up to v3 BEFORE
    // we apply SCHEMA_SQL. The migrations themselves are pure ALTER TABLE.
    if current == 1 {
        let has_client_pubkey: bool = conn
            .prepare("PRAGMA table_info(workers)")?
            .query_map([], |r| r.get::<_, String>(1))?
            .filter_map(Result::ok)
            .any(|c| c == "client_pubkey");
        if !has_client_pubkey {
            conn.execute("ALTER TABLE workers ADD COLUMN client_pubkey BLOB", [])?;
        }
        current = 2;
    }
    if current == 2 {
        let has_worker_seq: bool = conn
            .prepare("PRAGMA table_info(tasks)")?
            .query_map([], |r| r.get::<_, String>(1))?
            .filter_map(Result::ok)
            .any(|c| c == "worker_seq");
        if !has_worker_seq {
            conn.execute(
                "ALTER TABLE tasks ADD COLUMN worker_seq INTEGER NOT NULL DEFAULT 0",
                [],
            )?;
        }
        current = 3;
    }

    // Now apply the v3 schema. CREATE TABLE IF NOT EXISTS is a no-op on
    // existing tables; CREATE INDEX IF NOT EXISTS references columns that
    // now exist either because the migrations above added them or because
    // the table was just freshly created.
    conn.execute_batch(SCHEMA_SQL)?;

    let stamped: Option<i64> = conn
        .query_row("SELECT version FROM schema_version", [], |r| r.get(0))
        .ok();
    if stamped.is_none() {
        conn.execute(
            "INSERT INTO schema_version (version) VALUES (?)",
            [SCHEMA_VERSION],
        )?;
    } else {
        conn.execute("UPDATE schema_version SET version=?", [SCHEMA_VERSION])?;
    }

    if current != SCHEMA_VERSION && current != 0 {
        anyhow::bail!("unsupported schema version {current}");
    }
    Ok(())
}

/// Read the schema_version row (returns 0 if unset).
pub fn schema_version(conn: &Connection) -> Result<i64> {
    Ok(conn
        .query_row("SELECT version FROM schema_version", [], |r| r.get(0))
        .unwrap_or(0))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn fresh() -> Connection {
        let c = connect_in_memory().unwrap();
        bootstrap(&c).unwrap();
        c
    }

    fn table_names(conn: &Connection) -> Vec<String> {
        conn.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name")
            .unwrap()
            .query_map([], |r| r.get::<_, String>(0))
            .unwrap()
            .filter_map(Result::ok)
            .collect()
    }

    #[test]
    fn fresh_bootstrap_creates_all_tables() {
        let c = fresh();
        let tables: std::collections::HashSet<String> = table_names(&c).into_iter().collect();
        for required in &[
            "schema_version",
            "users",
            "user_pubkeys",
            "sessions",
            "nonces",
            "challenges",
            "workers",
            "tasks",
            "artifacts",
            "audit_log",
            "server_signing_key",
            "controller_messages",
        ] {
            assert!(
                tables.contains(*required),
                "expected table {required} after bootstrap"
            );
        }
    }

    #[test]
    fn bootstrap_is_idempotent() {
        let c = fresh();
        bootstrap(&c).unwrap();
        bootstrap(&c).unwrap();
        assert_eq!(schema_version(&c).unwrap(), SCHEMA_VERSION);
    }

    #[test]
    fn schema_version_after_fresh_bootstrap_is_three() {
        let c = fresh();
        assert_eq!(schema_version(&c).unwrap(), 3);
    }

    #[test]
    fn migration_v1_to_v3_adds_columns_and_tables() {
        // Build a minimal v1 schema: no client_pubkey on workers, no
        // worker_seq on tasks, no controller_messages, no server_signing_key.
        let c = connect_in_memory().unwrap();
        c.execute_batch(
            "
            CREATE TABLE schema_version (version INTEGER PRIMARY KEY);
            CREATE TABLE users (id INTEGER PRIMARY KEY, username TEXT NOT NULL UNIQUE,
                password_hash TEXT NOT NULL, totp_secret_enc TEXT NOT NULL, created_at TEXT NOT NULL);
            CREATE TABLE workers (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE,
                token_hash TEXT, created_at TEXT NOT NULL);
            CREATE TABLE tasks (task_id TEXT PRIMARY KEY, worker_name TEXT NOT NULL,
                submitter TEXT NOT NULL, kind TEXT NOT NULL, payload TEXT NOT NULL,
                collect_json TEXT NOT NULL, limits_json TEXT NOT NULL, state TEXT NOT NULL,
                submitted_at TEXT NOT NULL,
                purged INTEGER NOT NULL DEFAULT 0,
                cancel_requested INTEGER NOT NULL DEFAULT 0);
            INSERT INTO schema_version (version) VALUES (1);
            ",
        )
        .unwrap();

        bootstrap(&c).unwrap();

        // workers.client_pubkey must now exist
        let cols: Vec<String> = c
            .prepare("PRAGMA table_info(workers)")
            .unwrap()
            .query_map([], |r| r.get::<_, String>(1))
            .unwrap()
            .filter_map(Result::ok)
            .collect();
        assert!(cols.contains(&"client_pubkey".to_string()));

        // tasks.worker_seq must now exist
        let cols: Vec<String> = c
            .prepare("PRAGMA table_info(tasks)")
            .unwrap()
            .query_map([], |r| r.get::<_, String>(1))
            .unwrap()
            .filter_map(Result::ok)
            .collect();
        assert!(cols.contains(&"worker_seq".to_string()));

        // controller_messages + server_signing_key created
        let tables: std::collections::HashSet<_> = table_names(&c).into_iter().collect();
        assert!(tables.contains("controller_messages"));
        assert!(tables.contains("server_signing_key"));

        assert_eq!(schema_version(&c).unwrap(), 3);
    }

    #[test]
    fn foreign_key_cascade_users_to_pubkeys() {
        let c = fresh();
        c.execute(
            "INSERT INTO users (username, password_hash, totp_secret_enc, created_at)
             VALUES (?, ?, ?, ?)",
            rusqlite::params!["alice", "h", "s", "2026-01-01T00:00:00Z"],
        )
        .unwrap();
        let uid: i64 = c.last_insert_rowid();
        c.execute(
            "INSERT INTO user_pubkeys (user_id, fingerprint, alg, pubkey_blob, added_at)
             VALUES (?, ?, ?, ?, ?)",
            rusqlite::params![uid, "SHA256:fp", "ssh-ed25519", &b"k"[..], "2026-01-01T00:00:00Z"],
        )
        .unwrap();
        c.execute("DELETE FROM users WHERE id=?", [uid]).unwrap();
        let n: i64 = c
            .query_row("SELECT COUNT(*) FROM user_pubkeys", [], |r| r.get(0))
            .unwrap();
        assert_eq!(n, 0, "deleting a user must cascade-delete their pubkeys");
    }

    #[test]
    fn workers_unique_name_constraint() {
        let c = fresh();
        c.execute(
            "INSERT INTO workers (name, created_at) VALUES (?, ?)",
            rusqlite::params!["w1", "2026-01-01T00:00:00Z"],
        )
        .unwrap();
        let dup = c.execute(
            "INSERT INTO workers (name, created_at) VALUES (?, ?)",
            rusqlite::params!["w1", "2026-01-01T00:00:00Z"],
        );
        assert!(dup.is_err(), "duplicate worker name must violate UNIQUE");
    }

    #[test]
    fn nonces_pk_user_nonce() {
        let c = fresh();
        c.execute(
            "INSERT INTO users (username, password_hash, totp_secret_enc, created_at)
             VALUES (?, ?, ?, ?)",
            rusqlite::params!["alice", "h", "s", "2026-01-01T00:00:00Z"],
        )
        .unwrap();
        let uid: i64 = c.last_insert_rowid();
        c.execute(
            "INSERT INTO nonces (user_id, nonce, expires_at) VALUES (?,?,?)",
            rusqlite::params![uid, "abc", "2026-01-01T00:00:00Z"],
        )
        .unwrap();
        let dup = c.execute(
            "INSERT INTO nonces (user_id, nonce, expires_at) VALUES (?,?,?)",
            rusqlite::params![uid, "abc", "2026-01-01T00:00:00Z"],
        );
        assert!(dup.is_err(), "(user_id,nonce) replay must be rejected");
    }
}