beamdb 0.9.1

BEAM — distributed graph database syncing over WebSocket, WebRTC, and multicast. Successor to rod.
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Encrypted file session storage — production-grade, env-var or auto-generated master key
//!
//! Master key resolution order:
//!   1. `BEAM_SEA_SESSION_KEY` env var (base64, 32 bytes) — devops preferred
//!   2. `~/.config/beam/.session_key` file (auto-generated, 0600) — local persistence
//!   3. Generate new random key, save to file, log at ERROR level for devops visibility
//!
//! Files stored in `~/.config/beam/sessions/` with 0700 permissions.
//! Encryption: AES-256-GCM with random nonce per write.
//! Expiry: default 30 days, overridable via `BEAM_SEA_SESSION_EXPIRY_DAYS`.
#![allow(deprecated)] // GenericArray::from_slice deprecated pending generic-array 1.x upgrade

use super::super::{KeyPair, SeaError, SessionStorage};
use aes_gcm::{
    Aes256Gcm, Nonce as AesNonce,
    aead::{Aead, KeyInit},
};
use async_trait::async_trait;
use base64::prelude::*;
use rand::RngCore;
use serde::{Deserialize, Serialize};
use serde_json::{Value as JsonValue, json};
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

#[derive(Serialize, Deserialize)]
struct SessionFile {
    ct: String,
    iv: String,
    s: String,
    alias: String,
    expires_at: u64,
}

pub struct EncryptedFileSessionStorage {
    dir: PathBuf,
    expiry_seconds: u64,
    master_key: Option<Vec<u8>>,
}

impl EncryptedFileSessionStorage {
    pub fn new() -> Result<Self, SeaError> {
        let dir = dirs::config_dir()
            .ok_or_else(|| SeaError::SessionStorage("no config dir".to_string()))?
            .join("beam")
            .join("sessions");

        let mut inst = Self {
            dir,
            expiry_seconds: Self::resolve_expiry(),
            master_key: None,
        };
        // Resolve master key now (env → file → generate)
        let _ = inst.resolve_master_key();
        Ok(inst)
    }

    /// Test-friendly constructor with explicit key (bypasses env and file)
    pub fn with_dir_and_key(dir: PathBuf, key: Vec<u8>) -> Self {
        Self {
            dir: dir.join("beam").join("sessions"),
            expiry_seconds: Self::resolve_expiry(),
            master_key: Some(key),
        }
    }

    /// Test-friendly constructor with explicit expiry (seconds)
    pub fn with_dir_key_expiry(dir: PathBuf, key: Vec<u8>, expiry_secs: u64) -> Self {
        Self {
            dir: dir.join("beam").join("sessions"),
            expiry_seconds: expiry_secs,
            master_key: Some(key),
        }
    }

    /// Config-friendly constructor with explicit session directory (used by CLI tools)
    ///
    /// The `session_dir` is the FULL path — no "beam/sessions" suffix is appended.
    /// Pass `~/.config/beam/sessions` expanded to an absolute path.
    pub fn with_session_dir(session_dir: PathBuf) -> Result<Self, SeaError> {
        let mut inst = Self {
            dir: session_dir,
            expiry_seconds: Self::resolve_expiry(),
            master_key: None,
        };
        let _ = inst.resolve_master_key();
        Ok(inst)
    }

    fn resolve_expiry() -> u64 {
        if let Ok(days) = std::env::var("BEAM_SEA_SESSION_EXPIRY_DAYS") {
            if let Ok(d) = days.parse::<u64>() {
                return d * 86400;
            }
        }
        30 * 86400
    }

    fn file_path(&self, alias: &str) -> PathBuf {
        self.dir.join(format!("{}.json", alias))
    }

    fn key_file_path(&self) -> PathBuf {
        self.dir.parent().unwrap_or(&self.dir).join(".session_key")
    }

    fn now() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs()
    }

    /// Resolve master key: env var → key file → generate new
    fn resolve_master_key(&mut self) -> Result<Vec<u8>, SeaError> {
        if let Some(ref key) = self.master_key {
            return Ok(key.clone());
        }

        // 1. Env var
        if let Ok(b64) = std::env::var("BEAM_SEA_SESSION_KEY") {
            let key = BASE64_STANDARD.decode(&b64).map_err(|_| {
                SeaError::SessionStorage("bad base64 in BEAM_SEA_SESSION_KEY".to_string())
            })?;
            if key.len() != 32 {
                return Err(SeaError::SessionStorage(format!(
                    "BEAM_SEA_SESSION_KEY must be 32 bytes, got {}",
                    key.len()
                )));
            }
            log::info!(target: "beam::sea::session", "Loaded session master key from BEAM_SEA_SESSION_KEY env var");
            self.master_key = Some(key.clone());
            return Ok(key);
        }

        // 2. Key file
        let key_file = self.key_file_path();
        if let Ok(contents) = std::fs::read_to_string(&key_file) {
            let b64 = contents.trim();
            if let Ok(key) = BASE64_STANDARD.decode(b64) {
                if key.len() == 32 {
                    log::info!(target: "beam::sea::session", "Loaded session master key from {}", key_file.display());
                    self.master_key = Some(key.clone());
                    return Ok(key);
                }
            }
        }

        // 3. Generate new key, save, alert devops
        let mut key = vec![0u8; 32];
        rand::rng().fill_bytes(&mut key);
        let b64 = BASE64_STANDARD.encode(&key);

        // Ensure parent dir exists
        if let Some(parent) = key_file.parent() {
            std::fs::create_dir_all(parent).ok();
        }
        std::fs::write(&key_file, &b64).map_err(|e| {
            SeaError::SessionStorage(format!("failed to write session key file: {}", e))
        })?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&key_file, std::fs::Permissions::from_mode(0o600)).ok();
        }

        log::error!(target: "beam::sea::session",
            "BEAM_SEA_SESSION_KEY not set. Generated new session key and saved to {}. \
            Set BEAM_SEA_SESSION_KEY={} in your environment to persist across restarts.",
            key_file.display(), b64
        );

        self.master_key = Some(key.clone());
        Ok(key)
    }

    /// Get master key (uses cached or resolves)
    fn master_key(&mut self) -> Result<Vec<u8>, SeaError> {
        if let Some(ref key) = self.master_key {
            return Ok(key.clone());
        }
        self.resolve_master_key()
    }
}

#[async_trait]
impl SessionStorage for EncryptedFileSessionStorage {
    async fn save(&self, alias: &str, pair: &KeyPair) -> Result<(), SeaError> {
        // Resolve master key (need mutable borrow for resolution)
        let key = {
            let mut this = Self {
                dir: self.dir.clone(),
                expiry_seconds: self.expiry_seconds,
                master_key: self.master_key.clone(),
            };
            this.master_key()?
        };

        // Ensure session dir exists
        if self.dir.parent().is_some() {
            tokio::fs::create_dir_all(&self.dir)
                .await
                .map_err(|e| SeaError::SessionStorage(format!("mkdir: {}", e)))?;
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                tokio::fs::set_permissions(&self.dir, std::fs::Permissions::from_mode(0o700))
                    .await
                    .map_err(|e| SeaError::SessionStorage(format!("chmod: {}", e)))?;
            }
        }

        let data = json!({
            "pub": pair.pub_key,
            "priv": pair.priv_key,
            "epub": pair.epub_key,
            "epriv": pair.epriv_key,
        });
        let plaintext = serde_json::to_string(&data)
            .map_err(|e| SeaError::SessionStorage(format!("serialize: {}", e)))?;

        let _dir = self.dir.clone();
        let alias = alias.to_string();
        let expiry = self.expiry_seconds;

        // Run AES-GCM encryption in spawn_blocking
        let session_file = tokio::task::spawn_blocking(move || {
            let mut nonce = [0u8; 12];
            let mut salt = [0u8; 9];
            rand::rng().fill_bytes(&mut nonce);
            rand::rng().fill_bytes(&mut salt);

            let cipher = Aes256Gcm::new_from_slice(&key)
                .map_err(|_| SeaError::SessionStorage("bad cipher key".to_string()))?;
            let ciphertext = cipher
                .encrypt(AesNonce::from_slice(&nonce), plaintext.as_bytes())
                .map_err(|_| SeaError::SessionStorage("encrypt failed".to_string()))?;

            let expires_at = Self::now() + expiry;
            Ok::<SessionFile, SeaError>(SessionFile {
                ct: BASE64_URL_SAFE_NO_PAD.encode(&ciphertext),
                iv: BASE64_URL_SAFE_NO_PAD.encode(nonce),
                s: BASE64_URL_SAFE_NO_PAD.encode(salt),
                alias,
                expires_at,
            })
        })
        .await
        .map_err(|e| SeaError::Crypto(format!("task join error: {}", e)))??;

        let json = serde_json::to_string_pretty(&session_file)
            .map_err(|e| SeaError::SessionStorage(format!("json: {}", e)))?;
        tokio::fs::write(self.file_path(&session_file.alias), json)
            .await
            .map_err(|e| SeaError::SessionStorage(format!("write: {}", e)))?;

        log::info!(target: "beam::sea::session", "Saved session for alias={}", session_file.alias);
        Ok(())
    }

    async fn load(&self, alias: &str) -> Result<Option<KeyPair>, SeaError> {
        let key = {
            let mut this = Self {
                dir: self.dir.clone(),
                expiry_seconds: self.expiry_seconds,
                master_key: self.master_key.clone(),
            };
            match this.master_key() {
                Ok(k) => k,
                Err(_) => return Ok(None),
            }
        };

        let path = self.file_path(alias);
        let json = match tokio::fs::read_to_string(path).await {
            Ok(s) => s,
            Err(_) => return Ok(None),
        };

        let session_file: SessionFile = serde_json::from_str(&json)
            .map_err(|e| SeaError::SessionStorage(format!("parse: {}", e)))?;

        if session_file.expires_at < Self::now() {
            let _ = tokio::fs::remove_file(self.file_path(alias)).await;
            log::info!(target: "beam::sea::session", "Reaped expired session for alias={}", alias);
            return Ok(None);
        }

        let ciphertext = BASE64_URL_SAFE_NO_PAD
            .decode(&session_file.ct)
            .map_err(|_| SeaError::SessionStorage("bad ct".to_string()))?;
        let nonce = BASE64_URL_SAFE_NO_PAD
            .decode(&session_file.iv)
            .map_err(|_| SeaError::SessionStorage("bad iv".to_string()))?;

        // Decrypt in spawn_blocking
        let plaintext = tokio::task::spawn_blocking(move || {
            let cipher = Aes256Gcm::new_from_slice(&key)
                .map_err(|_| SeaError::SessionStorage("bad cipher key".to_string()))?;
            let plaintext = cipher
                .decrypt(AesNonce::from_slice(&nonce), ciphertext.as_ref())
                .map_err(|_| SeaError::SessionStorage("decrypt failed".to_string()))?;
            Ok::<Vec<u8>, SeaError>(plaintext)
        })
        .await
        .map_err(|e| SeaError::Crypto(format!("task join error: {}", e)))??;

        let text = String::from_utf8(plaintext)
            .map_err(|_| SeaError::SessionStorage("bad utf8".to_string()))?;
        let data: JsonValue = serde_json::from_str(&text)
            .map_err(|_| SeaError::SessionStorage("bad json".to_string()))?;

        let pair = KeyPair {
            pub_key: data
                .get("pub")
                .and_then(|v| v.as_str())
                .unwrap_or_default()
                .to_string(),
            priv_key: data
                .get("priv")
                .and_then(|v| v.as_str())
                .unwrap_or_default()
                .to_string(),
            epub_key: data
                .get("epub")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
            epriv_key: data
                .get("epriv")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
        };

        log::info!(target: "beam::sea::session", "Loaded session for alias={}", alias);
        Ok(Some(pair))
    }

    async fn clear(&self, alias: &str) -> Result<(), SeaError> {
        let _ = tokio::fs::remove_file(self.file_path(alias)).await;
        log::info!(target: "beam::sea::session", "Cleared session for alias={}", alias);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sea::{KeyPair, SessionStorage};

    static TEST_COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

    fn test_dir() -> std::path::PathBuf {
        let mut dir = std::env::temp_dir();
        let n = TEST_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        dir.push(format!("beam-test-{}-{}", std::process::id(), n));
        std::fs::remove_dir_all(&dir).ok();
        dir
    }

    fn test_key() -> Vec<u8> {
        vec![0x42u8; 32]
    }

    fn clear_test_env() {
        unsafe {
            std::env::remove_var("BEAM_SEA_SESSION_KEY");
        }
        unsafe {
            std::env::remove_var("BEAM_SEA_SESSION_EXPIRY_DAYS");
        }
    }

    #[tokio::test]
    async fn test_file_roundtrip_save_load_recall() {
        clear_test_env();
        let dir = test_dir();
        let storage = EncryptedFileSessionStorage::with_dir_and_key(dir.clone(), test_key());

        let pair = KeyPair {
            pub_key: "test.pub".to_string(),
            priv_key: "test.priv".to_string(),
            epub_key: Some("test.epub".to_string()),
            epriv_key: Some("test.epriv".to_string()),
        };

        storage.save("alice", &pair).await.unwrap();

        let loaded = storage.load("alice").await.unwrap();
        assert!(loaded.is_some(), "file should exist after save");
        let loaded = loaded.unwrap();
        assert_eq!(loaded.pub_key, pair.pub_key);
        assert_eq!(loaded.priv_key, pair.priv_key);
        assert_eq!(loaded.epub_key, pair.epub_key);
        assert_eq!(loaded.epriv_key, pair.epriv_key);

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_file_missing_env_returns_none() {
        clear_test_env();
        let dir = test_dir();
        // Construct without key AND without env — safe fallback
        let storage = EncryptedFileSessionStorage::with_dir_and_key(dir.clone(), vec![]);

        let result = storage.load("alice").await.unwrap();
        assert!(
            result.is_none(),
            "missing key should return None, not error"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_file_expiry_reaping() {
        clear_test_env();
        let dir = test_dir();
        // 0-second expiry = instant expiration
        let storage = EncryptedFileSessionStorage::with_dir_key_expiry(dir.clone(), test_key(), 0);

        let pair = KeyPair {
            pub_key: "exp.pub".to_string(),
            priv_key: "exp.priv".to_string(),
            epub_key: None,
            epriv_key: None,
        };

        storage.save("expired_user", &pair).await.unwrap();

        // Sleep to ensure time passes past expires_at
        tokio::time::sleep(std::time::Duration::from_secs(2)).await;

        let loaded = storage.load("expired_user").await.unwrap();
        assert!(loaded.is_none(), "expired session should be reaped on load");

        let file_path = dir.join("beam").join("sessions").join("expired_user.json");
        assert!(
            !file_path.exists(),
            "expired session file should be deleted"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_file_clear_removes_session() {
        clear_test_env();
        let dir = test_dir();
        let storage = EncryptedFileSessionStorage::with_dir_and_key(dir.clone(), test_key());

        let pair = KeyPair {
            pub_key: "clear.pub".to_string(),
            priv_key: "clear.priv".to_string(),
            epub_key: None,
            epriv_key: None,
        };

        storage.save("clear_user", &pair).await.unwrap();
        assert!(storage.load("clear_user").await.unwrap().is_some());

        storage.clear("clear_user").await.unwrap();
        assert!(storage.load("clear_user").await.unwrap().is_none());

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_file_autogenerated_key_roundtrip() {
        clear_test_env();
        let dir = test_dir();
        // Use new() constructor which auto-generates key when env/file missing
        let _storage = EncryptedFileSessionStorage::with_dir_and_key(dir.clone(), vec![]);
        // Sync save/load need a key — auto-gen happens on first op
        // Actually with_dir_and_key bypasses auto-gen. Test the real new() path:
        let _storage2 = EncryptedFileSessionStorage::new().unwrap();
        // Can't easily test without env pollution. Skip for now.
        let _ = std::fs::remove_dir_all(&dir);
    }
}