overude
runner nix smoke / nix label and flake smoke (push) Canceled after 0s

This commit is contained in:
2026-10-15 23:53:23 +00:00
parent 5aeb3c5d50
commit 21cc43a1c3
9 changed files with 332 additions and 91 deletions
+117 -22
View File
@@ -1,5 +1,9 @@
use std::env;
use std::fs::OpenOptions;
use std::io::Write;
use std::os::unix::fs::OpenOptionsExt;
#[cfg(test)]
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicU64, Ordering};
@@ -11,23 +15,50 @@ const ENCRYPTION_VERSION: i16 = 1;
static CONTENT_COUNTER: AtomicU64 = AtomicU64::new(0);
pub struct Crypto {
local_recipient: Option<String>,
identity_file: Option<PathBuf>,
temporary_identity_file: Option<PathBuf>,
}
impl Crypto {
pub fn from_env() -> Self {
Self {
local_recipient: env::var("ROUTINE_AGE_RECIPIENT").ok(),
identity_file: env::var_os("ROUTINE_AGE_IDENTITY_FILE").map(PathBuf::from),
pub fn from_env() -> Result<Self, String> {
Self::from_configuration(
env::var_os("ROUTINE_AGE_IDENTITY_FILE").map(PathBuf::from),
env::var("ROUTINE_AGE_IDENTITY_COMMAND").ok(),
)
}
fn from_configuration(
identity_file: Option<PathBuf>,
identity_command: Option<String>,
) -> Result<Self, String> {
match (identity_file, identity_command) {
(Some(_), Some(_)) => Err(
"set only one of ROUTINE_AGE_IDENTITY_FILE or ROUTINE_AGE_IDENTITY_COMMAND"
.to_owned(),
),
(Some(identity_file), None) => Ok(Self {
identity_file: Some(identity_file),
temporary_identity_file: None,
}),
(None, Some(command)) => {
let identity_file = run_identity_command(&command)?;
Ok(Self {
identity_file: Some(identity_file.clone()),
temporary_identity_file: Some(identity_file),
})
}
(None, None) => Ok(Self {
identity_file: None,
temporary_identity_file: None,
}),
}
}
#[cfg(test)]
pub fn disabled() -> Self {
Self {
local_recipient: None,
identity_file: None,
temporary_identity_file: None,
}
}
@@ -68,10 +99,6 @@ impl Crypto {
})
}
pub fn local_recipient(&self) -> &str {
self.local_recipient.as_deref().unwrap_or("")
}
pub fn decrypt_task(&self, task: &mut Task) {
match self.decrypt_fields(
task.encrypted_payload.as_deref(),
@@ -139,6 +166,49 @@ impl Crypto {
}
}
impl Drop for Crypto {
fn drop(&mut self) {
if let Some(path) = &self.temporary_identity_file {
let _ = std::fs::remove_file(path);
}
}
}
fn run_identity_command(command: &str) -> Result<PathBuf, String> {
if command.trim().is_empty() {
return Err("ROUTINE_AGE_IDENTITY_COMMAND must not be empty".to_owned());
}
let output = Command::new("sh")
.args(["-c", command])
.stdin(Stdio::inherit())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.output()
.map_err(|error| format!("cannot start ROUTINE_AGE_IDENTITY_COMMAND: {error}"))?;
if !output.status.success() {
return Err(format!(
"ROUTINE_AGE_IDENTITY_COMMAND exited with {}",
output.status
));
}
if output.stdout.iter().all(u8::is_ascii_whitespace) {
return Err("ROUTINE_AGE_IDENTITY_COMMAND returned an empty identity".to_owned());
}
let path = env::temp_dir().join(format!("routine-age-identity-{}", new_content_id()?));
let mut file = OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o600)
.open(&path)
.map_err(|error| format!("cannot create temporary age identity: {error}"))?;
if let Err(error) = file.write_all(&output.stdout) {
let _ = std::fs::remove_file(&path);
return Err(format!("cannot write temporary age identity: {error}"));
}
Ok(path)
}
fn run_age(arguments: &[String], input: &[u8]) -> Result<Vec<u8>, String> {
let mut child = Command::new("age")
.args(arguments)
@@ -195,20 +265,18 @@ mod tests {
.unwrap();
assert!(recipient.status.success());
let crypto = Crypto {
local_recipient: Some(
String::from_utf8(recipient.stdout)
.unwrap()
.trim()
.to_owned(),
),
identity_file: Some(identity_file),
};
let crypto =
Crypto::from_configuration(None, Some(format!("cat {}", identity_file.display())))
.unwrap();
let recipient = String::from_utf8(recipient.stdout)
.unwrap()
.trim()
.to_owned();
let encrypted = crypto
.encrypt_optional(
Some("Private appointment".to_owned()),
Some("Private notes".to_owned()),
&[crypto.local_recipient().to_owned()],
&[recipient],
)
.unwrap();
let decrypted = crypto
@@ -258,16 +326,16 @@ mod tests {
identities.push(identity);
}
let encryptor = Crypto {
local_recipient: None,
identity_file: None,
temporary_identity_file: None,
};
let encrypted = encryptor
.encrypt_optional(Some("shared".to_owned()), None, &recipients)
.unwrap();
for identity_file in identities {
let decryptor = Crypto {
local_recipient: None,
identity_file: Some(identity_file),
temporary_identity_file: None,
};
let decrypted = decryptor
.decrypt_fields(
@@ -281,4 +349,31 @@ mod tests {
}
std::fs::remove_dir_all(test_dir).unwrap();
}
#[test]
fn identity_command_is_loaded_before_use_and_removed_on_drop() {
let crypto =
Crypto::from_configuration(None, Some("printf 'AGE-SECRET-KEY-TEST\\n'".to_owned()))
.unwrap();
let path = crypto.identity_file.clone().unwrap();
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
"AGE-SECRET-KEY-TEST\n"
);
let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;
assert_eq!(mode, 0o600);
drop(crypto);
assert!(!path.exists());
}
#[test]
fn identity_sources_are_mutually_exclusive() {
let error = Crypto::from_configuration(
Some(PathBuf::from("identity.txt")),
Some("password-manager read identity".to_owned()),
)
.err()
.unwrap();
assert!(error.contains("set only one"));
}
}