Skip to main content

second_brain_core/keys/
store_lifecycle.rs

1use std::collections::BTreeSet;
2use std::fs;
3use std::path::Path;
4
5use anyhow::{Context, Result, anyhow, ensure};
6
7use super::device::sync_dir;
8use super::store_key::{dir_of, remove_if_present, same_dir, store_file_in};
9use super::{
10    DeviceKeys, KeyStorage, NEXT_STORE_KEY_FILE, STORE_KEY_FILE, StoreKey, WRAPPED_STORE_KEY_FILE,
11    WrapRefresh, create_device_keys, load_device_keys_in, read_store_key,
12    refresh_wrapped_store_key, store_key_for, write_store_key,
13};
14use crate::sqlite_store::SqliteStore;
15use crate::sqlite_store::encrypt_migration::{
16    FileState, NotReopened, Rewrite, RewriteTo, db_file_state, remove_interrupted_rewrite,
17};
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20#[non_exhaustive]
21pub enum StoreEncryption {
22    Encrypted,
23    /// Plaintext, waiting for the one-time migration.
24    Pending,
25    /// Plaintext, and never migrated: the store is outside the data dir
26    /// (a tool's copy, whose key files would sit in someone else's data dir),
27    /// or the build has no SQLCipher.
28    Plaintext,
29}
30
31#[non_exhaustive]
32pub struct DeviceStore {
33    pub store: SqliteStore,
34    pub encryption: StoreEncryption,
35}
36
37/// Opens the daemon's store. A store in `data_dir` that does not exist yet
38/// is created encrypted, generating the device keys and the store key
39/// first; an encrypted store opens under its key from `data_dir`. A copy
40/// left by an interrupted rewrite is dropped first.
41pub fn open_device_store(
42    db: &Path,
43    data_dir: &Path,
44    machine_id: String,
45    storage: &KeyStorage,
46) -> Result<DeviceStore> {
47    remove_interrupted_rewrite(db)?;
48    let in_data_dir = same_dir(dir_of(db), data_dir);
49    let can_encrypt = in_data_dir && cfg!(feature = "encryption");
50    match db_file_state(db)? {
51        FileState::Encrypted => {
52            let device = load_device_keys_in(data_dir, storage)?.ok_or_else(|| {
53                anyhow!(
54                    "{} is encrypted, but {} holds no device key to open its store key; \
55                     restore the data dir from a backup with `sb restore`",
56                    db.display(),
57                    data_dir.display()
58                )
59            })?;
60            let key = store_key_for(db, &device)?;
61            Ok(DeviceStore {
62                store: SqliteStore::open_with_key(db, machine_id, &key)?,
63                encryption: StoreEncryption::Encrypted,
64            })
65        }
66        FileState::Absent if can_encrypt => {
67            let device = match load_device_keys_in(data_dir, storage)? {
68                Some(device) => device,
69                None => create_device_keys(data_dir, storage)?,
70            };
71            let key = pending_store_key(data_dir, &device)?;
72            let store = SqliteStore::open_with_key(db, machine_id, &key)?;
73            store.mark_revocations_covered(&store.rotation_causes()?)?;
74            Ok(DeviceStore {
75                store,
76                encryption: StoreEncryption::Encrypted,
77            })
78        }
79        _ => Ok(DeviceStore {
80            store: SqliteStore::open(db, machine_id)?,
81            encryption: if can_encrypt {
82                StoreEncryption::Pending
83            } else {
84                StoreEncryption::Plaintext
85            },
86        }),
87    }
88}
89
90/// The one-time migration of a plaintext store in `data_dir` to SQLCipher.
91/// The store key is written before the rewrite, so a migration cut short
92/// at any point reuses it on the next attempt.
93pub fn encrypt_store(store: &SqliteStore, data_dir: &Path, device: &DeviceKeys) -> Result<Rewrite> {
94    store_file_in(store, data_dir)?;
95    ensure!(!store.is_encrypted(), "the store is already encrypted");
96    let key = pending_store_key(data_dir, device)?;
97    let covering = store.rotation_causes()?;
98    let rewrite = store.rewrite_under_key(RewriteTo::Key(&key))?;
99    if let Err(e) = store.record_encrypted(chrono::Utc::now().timestamp_micros(), rewrite.pause) {
100        tracing::warn!("cannot record the encryption of the store: {e:#}");
101    }
102    after_new_key(store, data_dir, device, &key, &covering);
103    Ok(rewrite)
104}
105
106/// `refresh_wrapped_store_key` with the key the store is open under.
107pub fn refresh_store_key_wrap(
108    store: &SqliteStore,
109    data_dir: &Path,
110    device: &DeviceKeys,
111) -> Result<WrapRefresh> {
112    store_file_in(store, data_dir)?;
113    let key = store
114        .store_key()
115        .ok_or_else(|| anyhow!("a plaintext store has no store key to wrap"))?;
116    refresh_wrapped_store_key(store, data_dir, device, &key)
117}
118
119/// Rewrites the store under a fresh store key, so a copy of the old key,
120/// in a backup or a revoked device's hands, opens no later copy of it.
121pub fn rotate_store_key(
122    store: &SqliteStore,
123    data_dir: &Path,
124    device: &DeviceKeys,
125) -> Result<Rewrite> {
126    let db = store_file_in(store, data_dir)?;
127    ensure!(
128        store.is_encrypted(),
129        "a plaintext store has no key to rotate"
130    );
131    settle_next_key(&db, data_dir, device)?;
132    let key = StoreKey::generate()?;
133    let covering = store.rotation_causes()?;
134    write_store_key(data_dir, NEXT_STORE_KEY_FILE, &key, device)?;
135    let rewrite = match store.rewrite_under_key(RewriteTo::Key(&key)) {
136        Ok(rewrite) => rewrite,
137        Err(e) => return Err(settle_failed_rotation(data_dir, e)),
138    };
139    promote_next_key(data_dir)?;
140    after_new_key(store, data_dir, device, &key, &covering);
141    Ok(rewrite)
142}
143
144/// A `store.key.next` left by an earlier rotation may be the only copy of
145/// the key the file is under. It is settled as an open settles it, by the
146/// file rather than the store's in-memory key (which keeps the old key after
147/// a failed reopen), before a new rotation overwrites it.
148fn settle_next_key(db: &Path, data_dir: &Path, device: &DeviceKeys) -> Result<()> {
149    let next = data_dir.join(NEXT_STORE_KEY_FILE);
150    if !fs::exists(&next).with_context(|| format!("checking {}", next.display()))? {
151        return Ok(());
152    }
153    store_key_for(db, device).map(drop)
154}
155
156fn promote_next_key(data_dir: &Path) -> Result<()> {
157    fs::rename(
158        data_dir.join(NEXT_STORE_KEY_FILE),
159        data_dir.join(STORE_KEY_FILE),
160    )
161    .context("promoting the new store key")?;
162    sync_dir(data_dir)
163}
164
165/// A rewrite that renamed its copy into place before failing leaves the
166/// store under the new key, which then lives only in `store.key.next`;
167/// that file is promoted at the next open. Any other failure left the
168/// store under its old key, so the new key goes.
169fn settle_failed_rotation(data_dir: &Path, error: anyhow::Error) -> anyhow::Error {
170    if error.downcast_ref::<NotReopened>().is_some() {
171        return error;
172    }
173    match remove_if_present(&data_dir.join(NEXT_STORE_KEY_FILE)) {
174        Ok(()) => error,
175        Err(cleanup) => error.context(format!("{cleanup:#}")),
176    }
177}
178
179/// `covering` is the revocation and retirement set read before the
180/// rewrite, so one that lands during it still asks for another rotation.
181fn after_new_key(
182    store: &SqliteStore,
183    data_dir: &Path,
184    device: &DeviceKeys,
185    key: &StoreKey,
186    covering: &BTreeSet<String>,
187) {
188    if let Err(e) = store.mark_revocations_covered(covering) {
189        tracing::warn!("cannot record which revocations the store key follows: {e:#}");
190    }
191    if let Err(e) = refresh_wrapped_store_key(store, data_dir, device, key) {
192        tracing::warn!("cannot write {WRAPPED_STORE_KEY_FILE}: {e:#}");
193    }
194}
195
196fn pending_store_key(data_dir: &Path, device: &DeviceKeys) -> Result<StoreKey> {
197    if let Some(key) = read_store_key(&data_dir.join(STORE_KEY_FILE), device)? {
198        return Ok(key);
199    }
200    let key = StoreKey::generate()?;
201    write_store_key(data_dir, STORE_KEY_FILE, &key, device)?;
202    Ok(key)
203}
204
205#[cfg(test)]
206mod tests {
207    use super::*;
208
209    #[test]
210    fn a_rotation_that_failed_after_its_rename_keeps_the_new_key() {
211        let dir = tempfile::tempdir().unwrap();
212        let next = dir.path().join(NEXT_STORE_KEY_FILE);
213        std::fs::write(&next, b"sealed").unwrap();
214        let reopen = anyhow::Error::from(NotReopened(anyhow!("EMFILE")));
215        let kept = settle_failed_rotation(dir.path(), reopen);
216        assert!(kept.downcast_ref::<NotReopened>().is_some());
217        assert!(next.exists(), "the only copy of the key the store is under");
218
219        let before_rename = anyhow!("the copy failed its check");
220        settle_failed_rotation(dir.path(), before_rename);
221        assert!(!next.exists(), "the store is still under its old key");
222    }
223
224    #[cfg(feature = "encryption")]
225    #[test]
226    fn a_rotation_retried_after_a_failed_reopen_keeps_the_key_the_file_is_under() {
227        use crate::schema::{Memory, MemoryType};
228        use crate::sqlite_store::encrypt_migration::FAIL_REOPEN;
229        use crate::store::Store;
230
231        let dir = tempfile::tempdir().unwrap();
232        let db = dir.path().join("brain.db");
233        let open = || open_device_store(&db, dir.path(), "m".into(), &KeyStorage::File).unwrap();
234        let opened = open();
235        opened
236            .store
237            .store_memory(&Memory::new(
238                "kept".to_string(),
239                MemoryType::Semantic,
240                "test".to_string(),
241                String::new(),
242            ))
243            .unwrap();
244        let device = load_device_keys_in(dir.path(), &KeyStorage::File)
245            .unwrap()
246            .unwrap();
247
248        FAIL_REOPEN.set(true);
249        let failed = rotate_store_key(&opened.store, dir.path(), &device).unwrap_err();
250        FAIL_REOPEN.set(false);
251        assert!(failed.downcast_ref::<NotReopened>().is_some(), "{failed:#}");
252        let _retried = rotate_store_key(&opened.store, dir.path(), &device);
253        drop(opened);
254
255        assert_eq!(open().store.memory_count().unwrap(), 1);
256    }
257}