use crate::error::{Error, Result};
use rand::Rng;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::path::Path;
use std::sync::Arc;
use std::sync::OnceLock;
use std::sync::atomic::{AtomicI32, AtomicU64, Ordering};
use std::sync::mpsc::{Receiver, SyncSender, TrySendError, sync_channel};
use std::time::{Duration, Instant};
use time::OffsetDateTime;
use time::UtcOffset;
use time::format_description::well_known::Rfc3339;
#[cfg(unix)]
use std::os::unix::fs::{MetadataExt, PermissionsExt};
pub fn now_secs() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
pub fn now_millis() -> u128 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis())
.unwrap_or(0)
}
fn local_offset(at: OffsetDateTime) -> UtcOffset {
UtcOffset::local_offset_at(at).unwrap_or(UtcOffset::UTC)
}
const OFFSET_CACHE_SECS: u64 = 1;
fn cached_local_offset(at: OffsetDateTime) -> UtcOffset {
static OFFSET_SECS: AtomicI32 = AtomicI32::new(0);
static VALID_UNTIL: AtomicU64 = AtomicU64::new(0);
let now = at.unix_timestamp().max(0) as u64;
if now < VALID_UNTIL.load(Ordering::Relaxed) {
return UtcOffset::from_whole_seconds(OFFSET_SECS.load(Ordering::Relaxed))
.unwrap_or(UtcOffset::UTC);
}
let offset = local_offset(at);
OFFSET_SECS.store(offset.whole_seconds(), Ordering::Relaxed);
VALID_UNTIL.store(now.saturating_add(OFFSET_CACHE_SECS), Ordering::Relaxed);
offset
}
fn ts_at(at: OffsetDateTime, offset: UtcOffset) -> String {
at.to_offset(offset)
.format(&Rfc3339)
.unwrap_or_else(|_| at.unix_timestamp().to_string())
}
pub fn ts(secs: u64) -> String {
let at = OffsetDateTime::from_unix_timestamp(secs as i64).unwrap_or(OffsetDateTime::UNIX_EPOCH);
ts_at(at, local_offset(at))
}
pub fn now_ts() -> String {
let now = OffsetDateTime::now_utc();
now.to_offset(cached_local_offset(now))
.format(&Rfc3339)
.unwrap_or_else(|_| now_secs().to_string())
}
pub struct LocalTimer;
impl tracing_subscriber::fmt::time::FormatTime for LocalTimer {
fn format_time(&self, w: &mut tracing_subscriber::fmt::format::Writer<'_>) -> std::fmt::Result {
w.write_str(&now_ts())
}
}
pub fn allow_insecure() -> bool {
std::env::var("KEYSINK_ALLOW_INSECURE").as_deref() == Ok("1")
}
pub(crate) fn private_open_options(mode: u32) -> OpenOptions {
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt as _;
let mut opts = OpenOptions::new();
opts.mode(mode).custom_flags(libc::O_NOFOLLOW);
opts
}
#[cfg(not(unix))]
{
let _ = mode;
OpenOptions::new()
}
}
pub fn ensure_private_file(path: &Path, what: &str) -> Result<()> {
let md = fs::symlink_metadata(path)?;
if md.file_type().is_symlink() {
return Err(Error::msg(format!(
"{what} is a symlink, refusing to use it: {}",
path.display()
)));
}
if !md.is_file() {
return Err(Error::msg(format!(
"{what} is not a regular file: {}",
path.display()
)));
}
#[cfg(unix)]
{
let mode = md.permissions().mode() & 0o777;
if mode & 0o077 != 0 && !allow_insecure() {
return Err(Error::msg(format!(
"{what} permissions too wide ({:o}); chmod 600 {} — or set KEYSINK_ALLOW_INSECURE=1 once you accept the risk",
mode,
path.display()
)));
}
}
Ok(())
}
pub fn ensure_private_dir(path: &Path) -> Result<()> {
let md = fs::symlink_metadata(path)?;
if md.file_type().is_symlink() {
return Err(Error::msg(format!(
"directory is a symlink, refusing to use it: {}",
path.display()
)));
}
if !md.is_dir() {
return Err(Error::msg(format!("not a directory: {}", path.display())));
}
#[cfg(unix)]
{
let mode = md.permissions().mode() & 0o777;
if mode & 0o077 != 0 && !allow_insecure() {
return Err(Error::msg(format!(
"directory permissions too wide ({:o}); chmod 700 {} — or set KEYSINK_ALLOW_INSECURE=1 once you accept the risk",
mode,
path.display()
)));
}
}
Ok(())
}
pub fn create_private_dir_all(path: &Path) -> Result<()> {
if let Ok(md) = fs::symlink_metadata(path)
&& md.file_type().is_symlink()
{
return Err(Error::msg(format!(
"directory is a symlink, refusing to use it: {}",
path.display()
)));
}
fs::create_dir_all(path)?;
#[cfg(unix)]
fs::set_permissions(path, fs::Permissions::from_mode(0o700))?;
Ok(())
}
pub fn read_no_follow(path: &Path) -> Result<Vec<u8>> {
let mut f = private_open_options(0).read(true).open(path)?;
let mut buf = Vec::new();
f.read_to_end(&mut buf)?;
Ok(buf)
}
pub fn read_no_follow_limited(path: &Path, limit: usize) -> Result<Vec<u8>> {
let file = private_open_options(0).read(true).open(path)?;
let mut buf = Vec::new();
let mut reader = file.take(limit as u64 + 1);
reader.read_to_end(&mut buf)?;
if buf.len() > limit {
return Err(Error::msg(format!(
"file exceeds the {} MiB limit; refusing to read it: {}",
limit / (1024 * 1024),
path.display()
)));
}
Ok(buf)
}
pub fn write_new_private(path: &Path, data: &[u8]) -> Result<()> {
let mut f = private_open_options(0o600)
.write(true)
.create_new(true)
.open(path)?;
inherit_dir_owner(path, path)?;
f.write_all(data)?;
f.sync_all()?;
Ok(())
}
#[cfg(unix)]
pub(crate) fn inherit_dir_owner(file: &Path, target: &Path) -> Result<()> {
if unsafe { libc::geteuid() } != 0 {
return Ok(());
}
let dir = target
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
let md = fs::symlink_metadata(dir)?;
if md.uid() == 0 && md.gid() == 0 {
return Ok(());
}
std::os::unix::fs::chown(file, Some(md.uid()), Some(md.gid())).map_err(|e| {
Error::msg(format!(
"failed to set owner of {} to {}:{}: {e}",
file.display(),
md.uid(),
md.gid()
))
})?;
Ok(())
}
#[cfg(not(unix))]
pub(crate) fn inherit_dir_owner(_file: &Path, _target: &Path) -> Result<()> {
Ok(())
}
#[cfg(unix)]
pub fn owner_matches_dir(path: &Path) -> Result<bool> {
let dir = path
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
let file = fs::symlink_metadata(path)?;
let dir = fs::symlink_metadata(dir)?;
Ok(file.uid() == dir.uid() && file.gid() == dir.gid())
}
#[cfg(not(unix))]
pub fn owner_matches_dir(_path: &Path) -> Result<bool> {
Ok(true)
}
pub fn atomic_write(path: &Path, data: &[u8], mode: u32) -> Result<()> {
let dir = path
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
let file_name = path
.file_name()
.and_then(|s| s.to_str())
.ok_or_else(|| Error::msg(format!("invalid path: {}", path.display())))?;
let mut suffix = [0u8; 8];
rand::rng().fill_bytes(&mut suffix);
let tmp = dir.join(format!(".{file_name}.tmp.{}", hex(&suffix)));
let result = (|| -> Result<()> {
let mut f = private_open_options(mode)
.write(true)
.create_new(true)
.open(&tmp)?;
f.write_all(data)?;
f.sync_all()?;
drop(f);
inherit_dir_owner(&tmp, path)?;
fs::rename(&tmp, path)?;
#[cfg(unix)]
{
File::open(dir)?.sync_all()?;
}
Ok(())
})();
if result.is_err() {
let _ = fs::remove_file(&tmp);
}
result
}
pub fn random_bytes<const N: usize>() -> [u8; N] {
let mut out = [0u8; N];
rand::rng().fill_bytes(&mut out);
out
}
const LOG_QUEUE_DEPTH: usize = 4096;
enum LogMsg {
Line(Vec<u8>),
Flush(std::sync::mpsc::Sender<()>),
}
static LOG_FLUSH: OnceLock<SyncSender<LogMsg>> = OnceLock::new();
pub fn non_blocking_stdout() -> NonBlockingMakeWriter {
let (tx, rx) = sync_channel::<LogMsg>(LOG_QUEUE_DEPTH);
let dropped = Arc::new(AtomicU64::new(0));
let spawned = std::thread::Builder::new()
.name("keysink-log".into())
.spawn({
let mut out = std::io::stdout();
move || log_writer_loop(rx, &mut out)
});
if let Err(e) = spawned {
let _ = writeln!(
std::io::stderr(),
"keysink: failed to start the log writer thread ({e}); log output will be dropped"
);
}
let _ = LOG_FLUSH.set(tx.clone());
NonBlockingMakeWriter { tx, dropped }
}
fn log_writer_loop(rx: Receiver<LogMsg>, out: &mut impl Write) {
while let Ok(msg) = rx.recv() {
let line = match msg {
LogMsg::Flush(ack) => {
let _ = out.flush();
let _ = ack.send(());
continue;
}
LogMsg::Line(line) => line,
};
if out.write_all(&line).and_then(|()| out.flush()).is_err() {
for msg in rx.iter() {
if let LogMsg::Flush(ack) = msg {
let _ = ack.send(());
}
}
break;
}
}
let _ = out.flush();
}
pub fn flush_logs(timeout: Duration) {
let Some(tx) = LOG_FLUSH.get() else {
return;
};
send_flush_marker(tx, timeout);
}
fn send_flush_marker(tx: &SyncSender<LogMsg>, timeout: Duration) {
let (ack_tx, ack_rx) = std::sync::mpsc::channel();
let deadline = Instant::now() + timeout;
loop {
match tx.try_send(LogMsg::Flush(ack_tx.clone())) {
Ok(()) => {
let left = deadline.saturating_duration_since(Instant::now());
let _ = ack_rx.recv_timeout(left);
return;
}
Err(TrySendError::Full(_)) if Instant::now() < deadline => {
std::thread::sleep(Duration::from_millis(5));
}
Err(_) => return,
}
}
}
pub struct NonBlockingMakeWriter {
tx: SyncSender<LogMsg>,
dropped: Arc<AtomicU64>,
}
impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for NonBlockingMakeWriter {
type Writer = QueuedWriter;
fn make_writer(&'a self) -> Self::Writer {
QueuedWriter {
tx: self.tx.clone(),
buf: Vec::new(),
dropped: self.dropped.clone(),
}
}
}
pub struct QueuedWriter {
tx: SyncSender<LogMsg>,
buf: Vec<u8>,
dropped: Arc<AtomicU64>,
}
impl Write for QueuedWriter {
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
self.buf.extend_from_slice(data);
Ok(data.len())
}
fn flush(&mut self) -> std::io::Result<()> {
if self.buf.is_empty() {
return Ok(());
}
let line = std::mem::take(&mut self.buf);
let len = line.len();
if self.tx.try_send(LogMsg::Line(line)).is_err() {
let n = self.dropped.fetch_add(1, Ordering::Relaxed) + 1;
if n % 1000 == 1 {
let _ = writeln!(
std::io::stderr(),
"keysink: log queue full or writer gone; dropping log lines ({n} dropped so far, {len} bytes)"
);
}
}
Ok(())
}
}
impl Drop for QueuedWriter {
fn drop(&mut self) {
let _ = self.flush();
}
}
pub fn hex(bytes: &[u8]) -> String {
const DIGITS: &[u8; 16] = b"0123456789abcdef";
let mut out = String::with_capacity(bytes.len() * 2);
for b in bytes {
out.push(DIGITS[(b >> 4) as usize] as char);
out.push(DIGITS[(b & 0x0f) as usize] as char);
}
out
}
pub fn percent_encode(value: &str) -> String {
let mut out = String::with_capacity(value.len());
for b in value.as_bytes() {
let c = *b as char;
if c.is_ascii_alphanumeric() || matches!(c, '-' | '.' | '_' | '~') {
out.push(c);
} else {
out.push('%');
out.push_str(&format!("{b:02X}"));
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
#[test]
fn percent_encode_basic() {
assert_eq!(percent_encode("abc-._~"), "abc-._~");
assert_eq!(percent_encode("a b&c"), "a%20b%26c");
}
#[test]
fn hex_roundtrip_shape() {
assert_eq!(hex(&[0x00, 0x0f, 0xff]), "000fff");
}
#[test]
fn ts_at_honours_the_offset() {
let epoch = OffsetDateTime::UNIX_EPOCH;
assert_eq!(ts_at(epoch, UtcOffset::UTC), "1970-01-01T00:00:00Z");
assert_eq!(
ts_at(epoch, UtcOffset::from_hms(8, 0, 0).unwrap()),
"1970-01-01T08:00:00+08:00"
);
assert_eq!(
ts_at(epoch, UtcOffset::from_hms(-5, 0, 0).unwrap()),
"1969-12-31T19:00:00-05:00"
);
}
#[test]
fn ts_round_trips_to_the_same_instant() {
let secs = 1_772_955_000_u64;
let parsed = OffsetDateTime::parse(&ts(secs), &Rfc3339).unwrap();
assert_eq!(parsed.unix_timestamp(), secs as i64);
}
#[test]
fn now_ts_is_rfc3339_and_close_to_now() {
let text = now_ts();
let parsed = OffsetDateTime::parse(&text, &Rfc3339).unwrap();
let skew = (parsed.unix_timestamp() - now_secs() as i64).abs();
assert!(skew <= 5, "now_ts drifted: {text}");
}
#[test]
fn read_no_follow_limited_refuses_before_buffering_the_whole_file() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("blob");
fs::write(&path, b"hello").unwrap();
assert_eq!(read_no_follow_limited(&path, 5).unwrap(), b"hello");
let err = read_no_follow_limited(&path, 4).unwrap_err().to_string();
assert!(err.contains("exceeds"), "{err}");
}
#[test]
fn atomic_write_replaces_content_without_residue() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("store.age");
atomic_write(&path, b"one", 0o600).unwrap();
atomic_write(&path, b"two", 0o600).unwrap();
assert_eq!(fs::read(&path).unwrap(), b"two");
assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 1);
}
#[cfg(unix)]
#[test]
fn atomic_write_keeps_mode_and_dir_owner() {
use std::os::unix::fs::MetadataExt as _;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("store.age");
atomic_write(&path, b"one", 0o600).unwrap();
let md = fs::metadata(&path).unwrap();
assert_eq!(md.mode() & 0o777, 0o600);
assert_eq!(md.uid(), fs::metadata(dir.path()).unwrap().uid());
}
#[test]
fn owner_matches_dir_detects_same_owner() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("vault.age");
fs::write(&path, b"x").unwrap();
assert!(owner_matches_dir(&path).unwrap());
}
#[cfg(unix)]
#[test]
fn create_private_dir_all_refuses_a_preexisting_symlink() {
let dir = tempfile::tempdir().unwrap();
let target = dir.path().join("target");
fs::create_dir(&target).unwrap();
let before = fs::metadata(&target).unwrap().permissions().mode() & 0o777;
let link = dir.path().join("link");
std::os::unix::fs::symlink(&target, &link).unwrap();
let err = create_private_dir_all(&link).unwrap_err().to_string();
assert!(err.contains("symlink"), "{err}");
assert_eq!(
fs::metadata(&target).unwrap().permissions().mode() & 0o777,
before,
"the chmod must not have landed on the target"
);
}
#[test]
fn queued_log_writer_drops_instead_of_blocking() {
let (tx, rx) = sync_channel::<LogMsg>(1);
let dropped = Arc::new(AtomicU64::new(0));
let mut w = QueuedWriter {
tx,
buf: Vec::new(),
dropped: dropped.clone(),
};
w.write_all(b"first\n").unwrap();
w.flush().unwrap();
w.write_all(b"second\n").unwrap();
w.flush().unwrap();
assert_eq!(dropped.load(Ordering::Relaxed), 1);
assert!(
matches!(rx.try_recv(), Ok(LogMsg::Line(line)) if line == b"first\n"),
"the first line must be queued"
);
assert!(
rx.try_recv().is_err(),
"the second line must have been dropped, not queued"
);
}
struct SharedSink(Arc<Mutex<Vec<u8>>>);
impl Write for SharedSink {
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
self.0.lock().unwrap().extend_from_slice(data);
Ok(data.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
#[test]
fn flush_marker_drains_queued_lines_before_acking() {
let (tx, rx) = sync_channel::<LogMsg>(4);
let sink = Arc::new(Mutex::new(Vec::new()));
let writer_sink = sink.clone();
let handle = std::thread::spawn(move || {
let mut writer_sink = SharedSink(writer_sink);
log_writer_loop(rx, &mut writer_sink)
});
tx.try_send(LogMsg::Line(b"a\n".to_vec())).unwrap();
tx.try_send(LogMsg::Line(b"b\n".to_vec())).unwrap();
send_flush_marker(&tx, Duration::from_secs(2));
assert_eq!(
sink.lock().unwrap().as_slice(),
b"a\nb\n".as_slice(),
"the ack must certify the queued lines are written"
);
drop(tx);
handle.join().unwrap();
}
struct BrokenSink;
impl Write for BrokenSink {
fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
Err(std::io::Error::other("stdout gone"))
}
fn flush(&mut self) -> std::io::Result<()> {
Err(std::io::Error::other("stdout gone"))
}
}
#[test]
fn broken_log_sink_still_acks_flush_markers() {
let (tx, rx) = sync_channel::<LogMsg>(4);
let handle = std::thread::spawn(move || log_writer_loop(rx, &mut BrokenSink));
tx.try_send(LogMsg::Line(b"a\n".to_vec())).unwrap();
let (ack_tx, ack_rx) = std::sync::mpsc::channel();
tx.try_send(LogMsg::Flush(ack_tx)).unwrap();
ack_rx
.recv_timeout(Duration::from_secs(2))
.expect("drain mode must ack flush markers");
drop(tx);
handle.join().unwrap();
}
}