1
Fork 0
mirror of https://github.com/thegeneralist01/archivr synced 2026-07-21 18:55:36 +02:00
archivr/crates/archivr-server/src/registry.rs
TheGeneralist 2d7a4f1766
feat(server): configurable bind address with loopback default and non-loopback warning
- Add optional `bind` field to ServerRegistry (TOML + ARCHIVR_BIND env var)
- Default bind address remains 127.0.0.1:8080; non-loopback prints a warning
- Add route security classification comment block (READ/ADMIN/WRITE/STATIC)
- Add Security and Deployment section to docs/README.md
- Replace vague auth note in ARCHIVR-MENTAL-MODEL.md with concrete model description
- Add three registry tests covering bind field round-trip and defaults
2026-06-23 17:24:24 +02:00

143 lines
4.5 KiB
Rust

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use std::{
fs,
path::{Path, PathBuf},
};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MountedArchive {
pub id: String,
pub label: String,
pub archive_path: PathBuf,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct ServerRegistry {
#[serde(default)]
pub archives: Vec<MountedArchive>,
/// Optional bind address for the server. Defaults to `127.0.0.1:8080`.
/// Set this to `0.0.0.0:8080` only on trusted networks — the server has no authentication.
#[serde(default)]
pub bind: Option<String>,
}
pub fn load_registry(path: &Path) -> Result<ServerRegistry> {
let contents = fs::read_to_string(path)
.with_context(|| format!("failed to read server registry {}", path.display()))?;
let registry = toml::from_str::<ServerRegistry>(&contents)
.with_context(|| format!("failed to parse server registry {}", path.display()))?;
validate_registry(&registry)?;
Ok(registry)
}
#[cfg(test)]
pub fn save_registry(path: &Path, registry: &ServerRegistry) -> Result<()> {
validate_registry(registry)?;
let contents = toml::to_string_pretty(registry)?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(path, contents)?;
Ok(())
}
pub fn validate_registry(registry: &ServerRegistry) -> Result<()> {
let mut ids = std::collections::HashSet::new();
for archive in &registry.archives {
if archive.id.trim().is_empty() {
bail!("archive id must not be empty");
}
if !ids.insert(archive.id.as_str()) {
bail!("duplicate archive id: {}", archive.id);
}
if !archive.archive_path.ends_with(".archivr") {
bail!(
"mounted archive path must point at a .archivr directory: {}",
archive.archive_path.display()
);
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn registry_round_trips_archives_from_toml() {
let temp = tempfile::tempdir().unwrap();
let archive_path = temp.path().join("personal").join(".archivr");
fs::create_dir_all(&archive_path).unwrap();
fs::write(archive_path.join("name"), "Personal").unwrap();
fs::write(
archive_path.join("store_path"),
temp.path().join("store").display().to_string(),
)
.unwrap();
let registry = ServerRegistry {
archives: vec![MountedArchive {
id: "personal".to_string(),
label: "Personal".to_string(),
archive_path: archive_path.clone(),
}],
bind: None,
};
let path = temp.path().join("server.toml");
save_registry(&path, &registry).unwrap();
let loaded = load_registry(&path).unwrap();
assert_eq!(loaded, registry);
}
#[test]
fn registry_rejects_duplicate_archive_ids() {
let registry = ServerRegistry {
archives: vec![
MountedArchive {
id: "personal".to_string(),
label: "Personal".to_string(),
archive_path: PathBuf::from("/tmp/a/.archivr"),
},
MountedArchive {
id: "personal".to_string(),
label: "Duplicate".to_string(),
archive_path: PathBuf::from("/tmp/b/.archivr"),
},
],
bind: None,
};
let err = validate_registry(&registry).unwrap_err().to_string();
assert!(err.contains("duplicate archive id"));
}
#[test]
fn registry_bind_field_round_trips() {
let toml = r#"bind = "127.0.0.1:9090""#;
let registry: ServerRegistry = toml::from_str(toml).unwrap();
assert_eq!(registry.bind.as_deref(), Some("127.0.0.1:9090"));
assert!(registry.archives.is_empty());
}
#[test]
fn registry_bind_field_defaults_to_none_when_absent() {
let toml = r#""#;
let registry: ServerRegistry = toml::from_str(toml).unwrap();
assert!(registry.bind.is_none());
}
#[test]
fn registry_bind_field_does_not_affect_archive_validation() {
let registry = ServerRegistry {
archives: vec![],
bind: Some("0.0.0.0:8080".to_string()),
};
// validate_registry does not reject non-loopback bind — that's main's concern.
assert!(validate_registry(&registry).is_ok());
}
}