use std::io::Write;
use std::path::Path;
use age::x25519::{Identity, Recipient};
use anyhow::{Context, bail};
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use serde::{Deserialize, Serialize};
use serde_json::Value;
pub const DIR_NAME: &str = "queue";
const PLAIN_SUFFIX: &str = ".json";
const SEALED_SUFFIX: &str = ".sealed";
pub fn is_entry_name(name: &str) -> bool {
let Some(stem) = name
.strip_suffix(PLAIN_SUFFIX)
.or_else(|| name.strip_suffix(SEALED_SUFFIX))
else {
return false;
};
let Some((nanos, id)) = stem.split_at_checked(19) else {
return false;
};
nanos.bytes().all(|b| b.is_ascii_digit())
&& id
.strip_prefix('-')
.is_some_and(|id| id.len() == 36 && uuid::Uuid::parse_str(id).is_ok())
}
pub fn is_sealed_name(name: &str) -> bool {
name.ends_with(SEALED_SUFFIX) && is_entry_name(name)
}
pub fn entry_name(nanos: u128, sealed: bool) -> String {
let suffix = if sealed { SEALED_SUFFIX } else { PLAIN_SUFFIX };
format!("{nanos:019}-{}{suffix}", uuid::Uuid::new_v4())
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Entry {
pub tool: String,
pub method: String,
pub path: String,
#[serde(default)]
pub query: Option<String>,
#[serde(default)]
pub body: Option<Value>,
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct Label {
pub tool: String,
pub method: String,
pub path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub session_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub kind: Option<String>,
}
#[derive(Serialize, Deserialize)]
struct SealedFile {
#[serde(flatten)]
label: Label,
sealed: String,
}
#[derive(Deserialize)]
struct StoredFile {
#[serde(flatten)]
label: Label,
#[serde(default)]
sealed: Option<String>,
#[serde(default)]
query: Option<String>,
#[serde(default)]
body: Option<Value>,
}
#[derive(Debug)]
pub enum OpenError {
NoKey,
Unreadable(anyhow::Error),
}
impl std::fmt::Display for OpenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NoKey => f.write_str("the entry is sealed and the device key is unavailable"),
Self::Unreadable(e) => write!(f, "{e:#}"),
}
}
}
impl std::error::Error for OpenError {}
impl Entry {
pub fn label(&self) -> Label {
let field = |name: &str| {
self.body
.as_ref()
.and_then(|b| b.get(name))
.and_then(Value::as_str)
.map(str::to_string)
};
let (source, session_id, kind) = match self.tool.as_str() {
"ingest" => (field("source"), None, None),
"note" => (None, field("session_id"), field("kind")),
_ => (None, None, None),
};
Label {
tool: self.tool.clone(),
method: self.method.clone(),
path: self.path.clone(),
source,
session_id,
kind,
}
}
pub fn to_plain_file(&self) -> anyhow::Result<Vec<u8>> {
Ok(serde_json::to_vec(self)?)
}
pub fn to_sealed_file(&self, recipients: &[Recipient]) -> anyhow::Result<Vec<u8>> {
if recipients.is_empty() {
bail!("sealing a queued write needs a recipient");
}
let encryptor =
age::Encryptor::with_recipients(recipients.iter().map(|r| r as &dyn age::Recipient))
.context("sealing a queued write")?;
let mut ciphertext = Vec::new();
let mut writer = encryptor.wrap_output(&mut ciphertext)?;
writer.write_all(&serde_json::to_vec(self)?)?;
writer.finish()?;
Ok(serde_json::to_vec(&SealedFile {
label: self.label(),
sealed: STANDARD.encode(ciphertext),
})?)
}
}
pub fn open(bytes: &[u8], identity: Option<&Identity>) -> Result<Entry, OpenError> {
let stored: StoredFile =
serde_json::from_slice(bytes).map_err(|e| OpenError::Unreadable(e.into()))?;
let Some(sealed) = stored.sealed else {
return Ok(Entry {
tool: stored.label.tool,
method: stored.label.method,
path: stored.label.path,
query: stored.query,
body: stored.body,
});
};
let identity = identity.ok_or(OpenError::NoKey)?;
let opened = STANDARD
.decode(sealed)
.context("decoding a sealed queued write")
.and_then(|ciphertext| {
age::decrypt(identity, &ciphertext).context("opening a sealed queued write")
})
.and_then(|plain| Ok(serde_json::from_slice(&plain)?))
.map_err(OpenError::Unreadable)?;
Ok(opened)
}
pub fn reseal_dir(dir: &Path, old: &Identity, recipients: &[Recipient]) -> anyhow::Result<usize> {
let items = match std::fs::read_dir(dir) {
Ok(items) => items,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(0),
Err(e) => return Err(e).with_context(|| format!("listing {}", dir.display())),
};
let mut resealed = 0;
for item in items {
let name = item?.file_name().to_string_lossy().into_owned();
if !is_sealed_name(&name) {
continue;
}
let path = dir.join(&name);
let bytes = match std::fs::read(&path) {
Ok(bytes) => bytes,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
Err(e) => {
tracing::warn!("cannot reseal {}: {e}", path.display());
continue;
}
};
let Ok(entry) = open(&bytes, Some(old)) else {
continue;
};
replace_file(dir, &name, &entry.to_sealed_file(recipients)?)?;
resealed += 1;
}
if resealed > 0 {
std::fs::File::open(dir)
.and_then(|d| d.sync_all())
.with_context(|| format!("syncing {}", dir.display()))?;
}
Ok(resealed)
}
fn replace_file(dir: &Path, name: &str, bytes: &[u8]) -> anyhow::Result<()> {
let tmp = dir.join(format!("{name}.{}.tmp", uuid::Uuid::new_v4().simple()));
let mut options = std::fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
let written = options
.open(&tmp)
.and_then(|mut file| {
file.write_all(bytes)?;
file.sync_all()
})
.and_then(|()| std::fs::rename(&tmp, dir.join(name)));
if let Err(e) = written {
if let Err(cleanup) = std::fs::remove_file(&tmp)
&& cleanup.kind() != std::io::ErrorKind::NotFound
{
tracing::warn!("cannot remove {}: {cleanup}", tmp.display());
}
return Err(e).with_context(|| format!("resealing {}", dir.join(name).display()));
}
Ok(())
}
pub fn read_label(bytes: &[u8]) -> anyhow::Result<Label> {
let stored: StoredFile = serde_json::from_slice(bytes)?;
if stored.sealed.is_some() {
return Ok(stored.label);
}
Ok(Entry {
tool: stored.label.tool,
method: stored.label.method,
path: stored.label.path,
query: stored.query,
body: stored.body,
}
.label())
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
fn note() -> Entry {
Entry {
tool: "note".to_string(),
method: "POST".to_string(),
path: "/v1/notes".to_string(),
query: None,
body: Some(json!({
"session_id": "s-1",
"kind": "session_end",
"content": "the plan for the migration",
})),
}
}
fn forget() -> Entry {
Entry {
tool: "forget".to_string(),
method: "DELETE".to_string(),
path: "/v1/memory/m-1".to_string(),
query: Some("hard=true&reason=leaked+the+password".to_string()),
body: None,
}
}
#[test]
fn only_timestamped_uuid_entry_names_count() {
let id = "0b5c2f9e-5d0e-4c1a-9a57-0c7f4f1f2a10";
assert!(is_entry_name(&format!("1793505622000000000-{id}.json")));
assert!(is_entry_name(&format!("1793505622000000000-{id}.sealed")));
for name in [
"projects.json".to_string(),
".lock".to_string(),
format!("1793505622000000000-{id}.json.tmp"),
format!("1793505622000000000-{id}.sealed.tmp"),
format!("179350562200000000-{id}.json"),
format!("1793505622000000000-{id}.unreadable"),
format!("1793505622000000000-{id}.sealed.json"),
"1793505622000000000-not-a-uuid.json".to_string(),
] {
assert!(!is_entry_name(&name), "{name}");
}
assert!(is_entry_name(&entry_name(7, true)));
assert!(is_sealed_name(&entry_name(7, true)));
assert!(!is_sealed_name(&entry_name(7, false)));
assert!(entry_name(7, true).ends_with(".sealed"));
assert!(entry_name(7, false).ends_with(".json"));
assert!(entry_name(7, false).starts_with("0000000000000000007-"));
}
#[test]
fn a_sealed_entry_shows_only_its_label_and_opens_with_the_device_key() {
let identity = Identity::generate();
for entry in [note(), forget()] {
let file = entry.to_sealed_file(&[identity.to_public()]).unwrap();
let text = String::from_utf8_lossy(&file);
for secret in ["migration", "password", "reason", "content"] {
assert!(!text.contains(secret), "{secret} in {text}");
}
assert_eq!(read_label(&file).unwrap(), entry.label());
assert_eq!(open(&file, Some(&identity)).unwrap(), entry);
assert!(matches!(open(&file, None), Err(OpenError::NoKey)));
assert!(matches!(
open(&file, Some(&Identity::generate())),
Err(OpenError::Unreadable(_))
));
}
}
#[test]
fn a_plaintext_entry_from_an_older_binary_opens_without_a_key() {
let older = br#"{"tool":"note","method":"POST","path":"/v1/notes","query":null,"body":{"session_id":"s-1","kind":"session_end","content":"x"}}"#;
assert_eq!(
open(older, None).unwrap().body,
note().body.map(|mut b| {
b["content"] = json!("x");
b
})
);
let label = read_label(older).unwrap();
assert_eq!(label.session_id.as_deref(), Some("s-1"));
assert_eq!(label.kind.as_deref(), Some("session_end"));
let entry = forget();
assert_eq!(open(&entry.to_plain_file().unwrap(), None).unwrap(), entry);
}
#[test]
fn labels_carry_an_ingests_source_and_a_notes_session_only() {
let ingest = Entry {
tool: "ingest".to_string(),
method: "POST".to_string(),
path: "/v1/ingest".to_string(),
query: None,
body: Some(json!({ "source": "cursor", "session_id": "s-1" })),
};
let label = ingest.label();
assert_eq!(label.source.as_deref(), Some("cursor"));
assert_eq!(label.session_id, None);
let note = note().label();
assert_eq!(
(
note.source,
note.session_id.as_deref(),
note.kind.as_deref()
),
(None, Some("s-1"), Some("session_end"))
);
let remember = Entry {
tool: "remember".to_string(),
body: Some(json!({ "session_id": "s-1", "source": "x" })),
..forget()
};
assert_eq!(
remember.label(),
Label {
tool: "remember".to_string(),
method: "DELETE".to_string(),
path: "/v1/memory/m-1".to_string(),
..Label::default()
}
);
}
#[test]
fn a_sealed_entry_opens_with_any_of_its_recipients() {
let (a, b) = (Identity::generate(), Identity::generate());
let file = forget()
.to_sealed_file(&[a.to_public(), b.to_public()])
.unwrap();
assert_eq!(open(&file, Some(&a)).unwrap(), forget());
assert_eq!(open(&file, Some(&b)).unwrap(), forget());
assert!(forget().to_sealed_file(&[]).is_err());
}
#[test]
fn resealing_moves_what_the_old_key_opens_to_the_new_recipients() {
let dir = tempfile::tempdir().unwrap();
let (old, fresh, stranger) = (
Identity::generate(),
Identity::generate(),
Identity::generate(),
);
let ours = entry_name(1, true);
let theirs = entry_name(2, true);
let plain = entry_name(3, false);
let write =
|name: &str, bytes: Vec<u8>| std::fs::write(dir.path().join(name), bytes).unwrap();
write(&ours, note().to_sealed_file(&[old.to_public()]).unwrap());
write(
&theirs,
forget().to_sealed_file(&[stranger.to_public()]).unwrap(),
);
write(&plain, forget().to_plain_file().unwrap());
let resealed = reseal_dir(dir.path(), &old, &[fresh.to_public()]).unwrap();
assert_eq!(resealed, 1);
let read = |name: &str| std::fs::read(dir.path().join(name)).unwrap();
assert_eq!(open(&read(&ours), Some(&fresh)).unwrap(), note());
assert!(open(&read(&ours), Some(&old)).is_err());
assert_eq!(open(&read(&theirs), Some(&stranger)).unwrap(), forget());
assert_eq!(open(&read(&plain), None).unwrap(), forget());
let names: Vec<_> = std::fs::read_dir(dir.path())
.unwrap()
.flatten()
.map(|e| e.file_name().to_string_lossy().into_owned())
.collect();
assert_eq!(names.len(), 3, "{names:?}");
assert_eq!(
reseal_dir(&dir.path().join("missing"), &old, &[fresh.to_public()]).unwrap(),
0
);
}
#[test]
fn a_reseal_that_cannot_write_fails_and_leaves_the_entry() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().unwrap();
let (old, fresh) = (Identity::generate(), Identity::generate());
let name = entry_name(1, true);
let sealed = note().to_sealed_file(&[old.to_public()]).unwrap();
std::fs::write(dir.path().join(&name), &sealed).unwrap();
let read_only = std::fs::Permissions::from_mode(0o500);
std::fs::set_permissions(dir.path(), read_only).unwrap();
let result = reseal_dir(dir.path(), &old, &[fresh.to_public()]);
std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
assert!(result.is_err());
assert_eq!(std::fs::read(dir.path().join(&name)).unwrap(), sealed);
}
#[test]
fn a_corrupt_entry_is_unreadable() {
assert!(matches!(open(b"{", None), Err(OpenError::Unreadable(_))));
let bad = br#"{"tool":"note","method":"POST","path":"/v1/notes","sealed":"!!"}"#;
assert!(matches!(
open(bad, Some(&Identity::generate())),
Err(OpenError::Unreadable(_))
));
}
}