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

This commit is contained in:
2026-10-15 01:05:01 +00:00
parent 60d2e5d80f
commit 06925e9af8
10 changed files with 495 additions and 44 deletions
+80 -14
View File
@@ -11,14 +11,14 @@ const ENCRYPTION_VERSION: i16 = 1;
static CONTENT_COUNTER: AtomicU64 = AtomicU64::new(0);
pub struct Crypto {
recipient: Option<String>,
local_recipient: Option<String>,
identity_file: Option<PathBuf>,
}
impl Crypto {
pub fn from_env() -> Self {
Self {
recipient: env::var("ROUTINE_AGE_RECIPIENT").ok(),
local_recipient: env::var("ROUTINE_AGE_RECIPIENT").ok(),
identity_file: env::var_os("ROUTINE_AGE_IDENTITY_FILE").map(PathBuf::from),
}
}
@@ -26,7 +26,7 @@ impl Crypto {
#[cfg(test)]
pub fn disabled() -> Self {
Self {
recipient: None,
local_recipient: None,
identity_file: None,
}
}
@@ -35,14 +35,16 @@ impl Crypto {
&self,
private_title: Option<String>,
notes: Option<String>,
recipients: &[String],
) -> Result<EncryptedFields, String> {
if private_title.is_none() && notes.is_none() {
return Ok(EncryptedFields::default());
}
let recipient = self
.recipient
.as_deref()
.ok_or_else(|| "ROUTINE_AGE_RECIPIENT is required for private fields".to_owned())?;
if recipients.is_empty() {
return Err(
"register at least one active encryption device for private fields".to_owned(),
);
}
let content_id = new_content_id()?;
let plaintext = serde_json::to_vec(&PrivatePayload {
version: ENCRYPTION_VERSION as u8,
@@ -51,10 +53,12 @@ impl Crypto {
notes,
})
.map_err(|error| format!("cannot encode private fields: {error}"))?;
let encrypted_payload = run_age(
&["--encrypt", "--armor", "--recipient", recipient],
&plaintext,
)?;
let mut arguments = vec!["--encrypt".to_owned(), "--armor".to_owned()];
for recipient in recipients {
arguments.push("--recipient".to_owned());
arguments.push(recipient.clone());
}
let encrypted_payload = run_age(&arguments, &plaintext)?;
let encrypted_payload = String::from_utf8(encrypted_payload)
.map_err(|_| "age returned non-UTF-8 armored output".to_owned())?;
Ok(EncryptedFields {
@@ -64,6 +68,10 @@ 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(),
@@ -113,7 +121,11 @@ impl Crypto {
let identity_file = self.identity_file.as_ref().ok_or(())?;
let identity = identity_file.to_str().ok_or(())?;
let plaintext = run_age(
&["--decrypt", "--identity", identity],
&[
"--decrypt".to_owned(),
"--identity".to_owned(),
identity.to_owned(),
],
encrypted_payload.as_bytes(),
)
.map_err(|_| ())?;
@@ -127,7 +139,7 @@ impl Crypto {
}
}
fn run_age(arguments: &[&str], input: &[u8]) -> Result<Vec<u8>, String> {
fn run_age(arguments: &[String], input: &[u8]) -> Result<Vec<u8>, String> {
let mut child = Command::new("age")
.args(arguments)
.stdin(Stdio::piped())
@@ -184,7 +196,7 @@ mod tests {
assert!(recipient.status.success());
let crypto = Crypto {
recipient: Some(
local_recipient: Some(
String::from_utf8(recipient.stdout)
.unwrap()
.trim()
@@ -196,6 +208,7 @@ mod tests {
.encrypt_optional(
Some("Private appointment".to_owned()),
Some("Private notes".to_owned()),
&[crypto.local_recipient().to_owned()],
)
.unwrap();
let decrypted = crypto
@@ -215,4 +228,57 @@ mod tests {
assert_eq!(decrypted.notes.as_deref(), Some("Private notes"));
std::fs::remove_dir_all(test_dir).unwrap();
}
#[test]
fn one_payload_decrypts_with_each_registered_identity() {
let test_dir = env::temp_dir().join(format!(
"routine-age-multi-test-{}",
new_content_id().unwrap()
));
std::fs::create_dir(&test_dir).unwrap();
let mut identities = Vec::new();
let mut recipients = Vec::new();
for index in 0..2 {
let identity = test_dir.join(format!("identity-{index}.txt"));
assert!(Command::new("age-keygen")
.args(["-o", identity.to_str().unwrap()])
.status()
.unwrap()
.success());
let recipient = Command::new("age-keygen")
.args(["-y", identity.to_str().unwrap()])
.output()
.unwrap();
recipients.push(
String::from_utf8(recipient.stdout)
.unwrap()
.trim()
.to_owned(),
);
identities.push(identity);
}
let encryptor = Crypto {
local_recipient: None,
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),
};
let decrypted = decryptor
.decrypt_fields(
encrypted.encrypted_payload.as_deref(),
encrypted.encryption_version,
encrypted.content_id.as_deref(),
)
.unwrap()
.unwrap();
assert_eq!(decrypted.private_title.as_deref(), Some("shared"));
}
std::fs::remove_dir_all(test_dir).unwrap();
}
}