File size: 15,114 Bytes
443bd1e
 
 
 
 
 
 
 
 
 
 
 
15b4f2d
 
 
 
 
 
 
 
 
443bd1e
 
 
 
 
 
ad1ac50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443bd1e
 
 
ad1ac50
 
 
 
 
 
 
443bd1e
ad1ac50
 
 
 
 
 
 
 
443bd1e
 
15b4f2d
 
443bd1e
15b4f2d
ad1ac50
15b4f2d
443bd1e
 
15b4f2d
 
 
443bd1e
 
 
15b4f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443bd1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad1ac50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443bd1e
ad1ac50
 
443bd1e
 
 
 
 
 
 
ad1ac50
 
443bd1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad1ac50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use archon_kernel::basepoint::{generate_basepoint_seal, generate_entropy};
use chrono::Utc;
use dialoguer::{theme::ColorfulTheme, Input};
use serde_json::json;
use std::fs;
use std::path::PathBuf;

use crate::preflight::{execute_preflight_probe, PreflightResult};
use serde_json::Value as JsonValue;

/// Returns the SELIN data directory (~/.selin)
pub fn selin_dir() -> PathBuf {
    // No /tmp fallback (work order item 3): an audit store in a world-writable,
    // auto-cleaned directory is not an audit store.
    match std::env::var("HOME") {
        Ok(h) if !h.trim().is_empty() => PathBuf::from(h).join(".selin"),
        _ => {
            eprintln!("[error] HOME is not set; SELIN needs a private home directory for its audit store.");
            std::process::exit(2);
        }
    }
}

pub fn myelin_db_path() -> PathBuf {
    selin_dir().join("myelin.db")
}

/// Derive the 32-byte database key from the operator passphrase with Argon2id
/// (19 MiB, 2 passes, 1 lane: the RustCrypto default parameters). Returns hex.
pub fn derive_db_key(passphrase: &str, salt: &[u8]) -> Result<String, String> {
    if passphrase.chars().count() < 8 {
        return Err("database passphrase must be at least 8 characters".into());
    }
    let mut out = [0u8; 32];
    argon2::Argon2::default()
        .hash_password_into(passphrase.as_bytes(), salt, &mut out)
        .map_err(|e| format!("argon2id failed: {e}"))?;
    Ok(hex::encode(out))
}

/// The database key (work order item 3, part 2). The key is NOT stored: basepoint.json
/// holds only a random salt, and the key is derived from SELIN_DB_PASSPHRASE with Argon2id.
/// A legacy basepoint with a stored `db_key` is still read, with a warning to re-initialize.
pub fn db_key() -> Option<String> {
    let raw = std::fs::read_to_string(basepoint_path()).ok()?;
    let v: JsonValue = serde_json::from_str(&raw).ok()?;
    if let Some(salt_hex) = v.get("db_key_salt").and_then(|s| s.as_str()) {
        let salt = hex::decode(salt_hex).ok()?;
        let pass = std::env::var("SELIN_DB_PASSPHRASE").ok()?;
        return derive_db_key(&pass, &salt).ok();
    }
    let legacy = v
        .get("db_key")
        .and_then(|k| k.as_str())
        .map(|s| s.to_string());
    if legacy.is_some() {
        eprintln!(
            "[warn] this SELIN store keeps its database key in basepoint.json (legacy). \
             Re-initialize with SELIN_FORCE_REINIT=1 to switch to a passphrase-derived key."
        );
    }
    legacy
}

/// Open the Myelin SQLite store. A database key is REQUIRED: there is no unencrypted
/// fallback (work order item 3). The chain columns are added to older stores on open.
pub fn open_myelin() -> Result<rusqlite::Connection, String> {
    let key = db_key().ok_or_else(|| {
        "no database key: set SELIN_DB_PASSPHRASE (or run `selin init` first). SELIN never opens its audit store unencrypted".to_string()
    })?;
    let conn = rusqlite::Connection::open(myelin_db_path())
        .map_err(|e| format!("could not open {}: {e}", myelin_db_path().display()))?;
    conn.pragma_update(None, "key", &key)
        .map_err(|e| format!("could not apply database encryption: {e}"))?;
    ensure_chain_columns(&conn)?;
    Ok(conn)
}

/// Add prev_hash / row_hash to an older adccl_runs table, if the table exists without them.
pub fn ensure_chain_columns(conn: &rusqlite::Connection) -> Result<(), String> {
    let mut stmt = match conn.prepare("PRAGMA table_info(adccl_runs)") {
        Ok(s) => s,
        Err(_) => return Ok(()),
    };
    let cols: Vec<String> = stmt
        .query_map([], |r| r.get::<_, String>(1))
        .map_err(|e| format!("could not read schema: {e}"))?
        .filter_map(Result::ok)
        .collect();
    if cols.is_empty() {
        return Ok(());
    }
    for c in ["prev_hash", "row_hash"] {
        if !cols.iter().any(|x| x == c) {
            conn.execute(&format!("ALTER TABLE adccl_runs ADD COLUMN {c} TEXT"), [])
                .map_err(|e| format!("could not add {c}: {e}"))?;
        }
    }
    Ok(())
}

pub fn basepoint_path() -> PathBuf {
    selin_dir().join("basepoint.json")
}

/// Public entry point. Prints a friendly error instead of panicking on failure.
pub async fn execute_init() {
    if let Err(e) = run_init().await {
        eprintln!("\n  βœ— Initialization failed: {e}");
        eprintln!("    Nothing was overwritten. Fix the issue above and re-run `selin init`.");
        std::process::exit(1);
    }
}

async fn run_init() -> Result<(), String> {
    println!("╔══════════════════════════════════════════════════════════════╗");
    println!("β•‘     SELIN Series (ARCHON v1.0) β€” Init Wizard          β•‘");
    println!("β•‘     Reflect-It-Yourself Unit (RIYU) Sovereign Onboarding     β•‘");
    println!("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•");
    println!();

    // Check for existing identity
    if basepoint_path().exists() {
        if std::env::var("SELIN_FORCE_REINIT").unwrap_or_default() != "1" {
            return Err(format!(
                "an identity already exists at {}.\n    Re-initializing replaces your basepoint seal. \
                 To proceed intentionally, re-run with SELIN_FORCE_REINIT=1.",
                basepoint_path().display()
            ));
        }
        // Create timestamped backup of old identity before overwriting
        let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S").to_string();
        let bp_backup = selin_dir().join(format!("basepoint.json.bak.{timestamp}"));
        if let Err(e) = std::fs::copy(basepoint_path(), &bp_backup) {
            eprintln!("[warn] Could not backup existing basepoint.json: {e}");
        } else {
            eprintln!(
                "[info] Backed up existing basepoint to {}",
                bp_backup.display()
            );
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                let _ =
                    std::fs::set_permissions(&bp_backup, std::fs::Permissions::from_mode(0o600));
            }
        }
        let myelin_file = myelin_db_path();
        if myelin_file.exists() {
            let myelin_backup = selin_dir().join(format!("myelin.db.bak.{timestamp}"));
            if let Err(e) = std::fs::copy(&myelin_file, &myelin_backup) {
                eprintln!("[warn] Could not backup existing myelin.db: {e}");
            } else {
                eprintln!(
                    "[info] Backed up existing myelin.db to {}",
                    myelin_backup.display()
                );
                #[cfg(unix)]
                {
                    use std::os::unix::fs::PermissionsExt;
                    let _ = std::fs::set_permissions(
                        &myelin_backup,
                        std::fs::Permissions::from_mode(0o600),
                    );
                }
            }
        }
        eprintln!("[warn] Overwriting existing identity (SELIN_FORCE_REINIT=1)");
    }

    println!("[1/4] Identity Directives");
    println!("      Enter your governing principles (comma-separated).");
    println!("      These are used to derive your seal; they are NOT stored in plaintext.\n");

    let theme = ColorfulTheme::default();
    let directives: String = Input::with_theme(&theme)
        .with_prompt("Directives")
        .default("sovereignty,verifiable-accuracy,anti-drift".to_string())
        .interact_text()
        .map_err(|e| format!("could not read directives: {e}"))?;

    let endpoint: String = Input::with_theme(&theme)
        .with_prompt("Model endpoint")
        .default("http://localhost:11434".to_string())
        .interact_text()
        .map_err(|e| format!("could not read endpoint: {e}"))?;

    // Step 2: Generate the seal from CSPRNG entropy (was hostname:timestamp).
    println!("\n[2/4] Generating Sovereign Identity Basepoint Seal (CSPRNG + HKDF)…");
    let salt = generate_entropy()?;
    let salt_hex = hex::encode(salt);
    let seal = generate_basepoint_seal(&directives, &salt);

    fs::create_dir_all(selin_dir())
        .map_err(|e| format!("could not create {}: {e}", selin_dir().display()))?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let _ = fs::set_permissions(selin_dir(), fs::Permissions::from_mode(0o700));
    }

    // Database key (SQLCipher): derived from the operator's passphrase with Argon2id.
    // Only the random salt is stored; the key itself never touches disk.
    let pass = match std::env::var("SELIN_DB_PASSPHRASE") {
        Ok(p) => p,
        Err(_) => dialoguer::Password::with_theme(&ColorfulTheme::default())
            .with_prompt(
                "Database passphrase (min 8 chars; needed on every run as SELIN_DB_PASSPHRASE)",
            )
            .with_confirmation("Confirm passphrase", "Passphrases did not match")
            .interact()
            .map_err(|e| format!("could not read passphrase: {e}"))?,
    };
    let db_salt = generate_entropy()?;
    derive_db_key(&pass, &db_salt)?;
    let db_salt_hex = hex::encode(db_salt);
    // Make the key available to this process for the schema step below.
    std::env::set_var("SELIN_DB_PASSPHRASE", &pass);

    // basepoint.json stores the seal, the seal's salt and the database key's salt. It
    // stores neither the database key nor the directives or any hint of them.
    let bp_json = json!({
        "version": 2,
        "seal": seal,
        "salt": salt_hex,
        "seal_alg": "HKDF-SHA256/HMAC-SHA256",
        "created_at": Utc::now().to_rfc3339(),
        "endpoint": endpoint,
        "db_key_salt": db_salt_hex,
        "db_key_kdf": "argon2id-v19-m19456-t2-p1",
    });
    fs::write(
        basepoint_path(),
        serde_json::to_string_pretty(&bp_json)
            .map_err(|e| format!("could not serialize basepoint: {e}"))?,
    )
    .map_err(|e| format!("could not write {}: {e}", basepoint_path().display()))?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let _ = fs::set_permissions(basepoint_path(), fs::Permissions::from_mode(0o600));
    }

    println!("      Seal (HMAC-SHA256): {seal}");
    println!("      Written to: {}", basepoint_path().display());

    // Step 3: Preflight probe (non-fatal).
    println!("\n[3/4] Running Model Capability Preflight Diagnostic…");
    let preflight = execute_preflight_probe(&endpoint).await;
    match &preflight {
        PreflightResult::Pass { model, latency_ms } => {
            println!("      [βœ“] Model '{model}' responded in {latency_ms}ms β€” gate cleared.");
        }
        PreflightResult::Fail { reason } => {
            println!("      [βœ—] Preflight FAILED: {reason}");
            println!("      Continuing init β€” you can fix the endpoint later.");
        }
    }

    // Step 4: Myelin store.
    println!("\n[4/4] Initializing Myelin SQLite Store…");
    let schema_sql = include_str!("../../templates/myelin_schema.sql");
    let conn = open_myelin()?;
    conn.execute_batch(schema_sql)
        .map_err(|e| format!("could not apply myelin schema: {e}"))?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let _ = fs::set_permissions(myelin_db_path(), fs::Permissions::from_mode(0o600));
    }

    let (schema_ok, chi_ok, latency) = match &preflight {
        PreflightResult::Pass { latency_ms, .. } => (1, 1, *latency_ms as i64),
        PreflightResult::Fail { .. } => (0, 0, -1),
    };
    let model_name = match &preflight {
        PreflightResult::Pass { model, .. } => model.clone(),
        PreflightResult::Fail { reason } => format!("unknown ({reason})"),
    };
    let preflight_result = match &preflight {
        PreflightResult::Pass { .. } => "PASS",
        PreflightResult::Fail { .. } => "FAIL",
    };

    // Re-init (force): replace the single identity row rather than IGNORE it.
    conn.execute("DELETE FROM selin_identity", [])
        .map_err(|e| format!("could not reset identity: {e}"))?;
    conn.execute(
        "INSERT INTO selin_identity (basepoint_seal, seal_version) VALUES (?1, 2)",
        rusqlite::params![seal],
    )
    .map_err(|e| format!("could not insert identity: {e}"))?;

    conn.execute(
        "INSERT INTO preflight_log (endpoint, schema_adherence, chi_compliance, model_name, latency_ms, result) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
        rusqlite::params![endpoint, schema_ok, chi_ok, model_name, latency, preflight_result],
    )
    .map_err(|e| format!("could not insert preflight log: {e}"))?;

    let db_size = fs::metadata(myelin_db_path()).map(|m| m.len()).unwrap_or(0);
    println!(
        "      Myelin DB: {} ({db_size} bytes) [SQLCipher encrypted]",
        myelin_db_path().display()
    );

    println!();
    println!("╔══════════════════════════════════════════════════════════════╗");
    println!("β•‘  Initialization Complete.                                     β•‘");
    println!("β•‘  Your SELIN instance is locked to your identity basepoint.   β•‘");
    println!("β•‘  Run `selin preflight` anytime to re-verify your endpoint.   β•‘");
    println!("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•");
    Ok(())
}

/// Load configured endpoint from basepoint.json, env, or default.
pub fn load_endpoint_from_bp() -> String {
    if let Ok(ep) = std::env::var("MODEL_ENDPOINT") {
        return ep;
    }
    if let Ok(raw) = std::fs::read_to_string(basepoint_path()) {
        if let Ok(v) = serde_json::from_str::<JsonValue>(&raw) {
            if let Some(ep) = v.get("endpoint").and_then(|e| e.as_str()) {
                return ep.to_string();
            }
        }
    }
    "http://localhost:11434".to_string()
}

#[cfg(test)]
mod key_tests {
    use super::derive_db_key;

    #[test]
    fn same_passphrase_and_salt_give_the_same_key() {
        let a = derive_db_key("correct horse battery", b"0123456789abcdef").unwrap();
        let b = derive_db_key("correct horse battery", b"0123456789abcdef").unwrap();
        assert_eq!(a, b);
        assert_eq!(a.len(), 64);
    }

    #[test]
    fn wrong_passphrase_or_salt_gives_a_different_key() {
        let a = derive_db_key("correct horse battery", b"0123456789abcdef").unwrap();
        assert_ne!(
            a,
            derive_db_key("correct horse batterz", b"0123456789abcdef").unwrap()
        );
        assert_ne!(
            a,
            derive_db_key("correct horse battery", b"fedcba9876543210").unwrap()
        );
    }

    #[test]
    fn short_passphrase_is_refused() {
        assert!(derive_db_key("short", b"0123456789abcdef").is_err());
    }
}