mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Made bakashrot-cert write a certificate and its key both or neither, Refused a clock before 1970 at start, Noted the replication limits in the example config
- bakashrot-cert ca and issue write nothing when either file of the pair is there already, and remove the certificate again when its key can't be written. - The server refuses to start with a clock before 1970 (pushed_at 0); set back that far while running isn't checked, noted in known-issues.md. - The example config says to keep sync_interval under 5 min (idle links are closed) and what a replicated attachment can be resumed past (read back and hashed in 30 s). Both left as they are, why in known-issues.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -25,6 +25,11 @@ database = "bakashrot.sqlite"
|
||||
# second or so. ("off" isn't allowed.)
|
||||
synchronous = "full"
|
||||
|
||||
# Replication cut partway through an attachment resumes once the peer has
|
||||
# read back and hashed what it already has, in 30 s at most: up to about
|
||||
# 4 GB received on a hard disk, 15 GB on a SATA SSD, 30-60 GB on NVMe
|
||||
# (estimates). Past that, resuming fails at every retry: keep attachments
|
||||
# well under it (more than a few GB is a lot for this anyway).
|
||||
max_attachment_size = "1G" # bytes, or K M G T (1024-based); 0: none
|
||||
retention_interval = "1m" # how often old messages are deleted
|
||||
|
||||
@@ -42,7 +47,7 @@ logs = "30d"
|
||||
push_to = [] # dialed and sent to: ["10.0.0.2:7000", "server-2.lan:7000"]
|
||||
accept_from = [] # taken from: ["10.0.0.2", "server-2.lan"], or ["*"]
|
||||
retry_delay = "10s"
|
||||
sync_interval = "5s"
|
||||
sync_interval = "5s" # keep it under 5 min: a link idle that long is closed
|
||||
batch_size = 1000 # messages per packet, 1-10000
|
||||
|
||||
# What certificates with a role may do. A certificate gets everything its
|
||||
|
||||
@@ -72,6 +72,7 @@ impl Server {
|
||||
/// Reads the server's certificates and CRL, opens its database, starts
|
||||
/// its writer. Errors say which file and why.
|
||||
pub fn start(config: Config) -> Result<Server, String> {
|
||||
check_clock(SystemTime::now())?;
|
||||
let server = &config.server;
|
||||
let ca = read(&server.ca)?;
|
||||
let certificate = read(&server.certificate)?;
|
||||
@@ -307,6 +308,15 @@ fn load_revocations(path: Option<&Path>, ca: &str) -> Result<Revocations, String
|
||||
Revocations::load(&read(path)?, ca).map_err(|error| format!("{}: {error}", path.display()))
|
||||
}
|
||||
|
||||
/// Refuses a clock before 1970: messages would be stamped `pushed_at` 0,
|
||||
/// which readers refuse.
|
||||
fn check_clock(now: SystemTime) -> Result<(), String> {
|
||||
match now.duration_since(UNIX_EPOCH) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err("the system's clock is before 1970: set it right first".into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Now, in ms since 1970 UTC: when a message is pushed.
|
||||
fn now() -> u64 {
|
||||
SystemTime::now()
|
||||
@@ -319,3 +329,17 @@ fn stamp(path: &Path) -> Option<(SystemTime, u64)> {
|
||||
let metadata = std::fs::metadata(path).ok()?;
|
||||
Some((metadata.modified().ok()?, metadata.len()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_clock_before_1970_is_refused() {
|
||||
assert!(check_clock(UNIX_EPOCH).is_ok());
|
||||
assert!(check_clock(SystemTime::now()).is_ok());
|
||||
let before = UNIX_EPOCH - Duration::from_secs(1);
|
||||
let error = check_clock(before).unwrap_err();
|
||||
assert!(error.contains("before 1970"), "{error}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,8 +99,12 @@ fn run(command: Command) -> Result<(), String> {
|
||||
match command {
|
||||
Command::Ca { name, days, out } => {
|
||||
let ca = Ca::create(&name, days).map_err(|e| e.to_string())?;
|
||||
write_new(&out.join("ca.crt"), &ca.certificate, false)?;
|
||||
write_new(&out.join("ca.key"), &ca.key, true)?;
|
||||
write_pair(
|
||||
&out.join("ca.crt"),
|
||||
&ca.certificate,
|
||||
&out.join("ca.key"),
|
||||
&ca.key,
|
||||
)?;
|
||||
}
|
||||
Command::Issue {
|
||||
name,
|
||||
@@ -118,8 +122,12 @@ fn run(command: Command) -> Result<(), String> {
|
||||
.issue(&identity, usage, days)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let file = file.unwrap_or(name);
|
||||
write_new(&out.join(format!("{file}.crt")), &issued.certificate, false)?;
|
||||
write_new(&out.join(format!("{file}.key")), &issued.key, true)?;
|
||||
write_pair(
|
||||
&out.join(format!("{file}.crt")),
|
||||
&issued.certificate,
|
||||
&out.join(format!("{file}.key")),
|
||||
&issued.key,
|
||||
)?;
|
||||
}
|
||||
Command::Revoke {
|
||||
certificates,
|
||||
@@ -159,8 +167,28 @@ fn write_new(path: &Path, text: &str, private: bool) -> Result<(), String> {
|
||||
.open(path)
|
||||
.map_err(|error| format!("{}: {error}", path.display()))?;
|
||||
file.write_all(text.as_bytes())
|
||||
.map_err(|error| format!("{}: {error}", path.display()))?;
|
||||
println!("Wrote {}", path.display());
|
||||
.map_err(|error| format!("{}: {error}", path.display()))
|
||||
}
|
||||
|
||||
/// Writes a certificate and its key, both or neither: nothing is written
|
||||
/// when either is there already, the certificate is removed again when
|
||||
/// its key can't be written.
|
||||
fn write_pair(certificate: &Path, pem: &str, key: &Path, key_pem: &str) -> Result<(), String> {
|
||||
for path in [certificate, key] {
|
||||
if path.exists() {
|
||||
return Err(format!(
|
||||
"{}: already there, nothing written",
|
||||
path.display()
|
||||
));
|
||||
}
|
||||
}
|
||||
write_new(certificate, pem, false)?;
|
||||
if let Err(error) = write_new(key, key_pem, true) {
|
||||
let _ = fs::remove_file(certificate);
|
||||
return Err(format!("{error}, nothing written"));
|
||||
}
|
||||
println!("Wrote {}", certificate.display());
|
||||
println!("Wrote {}", key.display());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -121,6 +121,20 @@ fn nothing_is_overwritten_or_mixed_up() {
|
||||
assert!(!ok, "{text}");
|
||||
assert_eq!(read(&folder, "ca.key"), key, "the CA's key is still there");
|
||||
|
||||
// Half of a pair there already: refused, the other half not written.
|
||||
let half = folder.join("half");
|
||||
fs::create_dir_all(&half).unwrap();
|
||||
fs::write(half.join("ca.key"), "kept").unwrap();
|
||||
let (ok, text) = cert(&half, &["ca", "--name", "Half"]);
|
||||
assert!(!ok && text.contains("nothing written"), "{text}");
|
||||
assert!(!half.join("ca.crt").exists());
|
||||
assert_eq!(read(&half, "ca.key"), "kept");
|
||||
fs::write(folder.join("porch.key"), "kept").unwrap();
|
||||
let (ok, text) = cert(&folder, &["issue", "--name", "porch"]);
|
||||
assert!(!ok && text.contains("nothing written"), "{text}");
|
||||
assert!(!folder.join("porch.crt").exists());
|
||||
assert_eq!(read(&folder, "porch.key"), "kept");
|
||||
|
||||
assert!(cert(&folder, &["issue", "--name", "kitchen"]).0);
|
||||
assert!(
|
||||
!cert(&folder, &["issue", "--name", "kitchen"]).0,
|
||||
|
||||
Reference in New Issue
Block a user