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
+117 -2
View File
@@ -1,7 +1,10 @@
use chrono::{Datelike, Days, NaiveDate, NaiveTime};
use postgres::{Client, NoTls};
use crate::model::{CreateRoutine, CreateTask, DueNotification, Routine, Schedule, Task};
use crate::model::{
CreateEncryptionDevice, CreateRoutine, CreateTask, DueNotification, EncryptionDevice,
EncryptionRecipient, Routine, Schedule, Task,
};
pub type Result<T> = std::result::Result<T, String>;
@@ -68,11 +71,97 @@ impl Store {
ALTER TABLE tasks
ADD COLUMN IF NOT EXISTS status_before_archive TEXT;
ALTER TABLE tasks
ADD COLUMN IF NOT EXISTS archived_at TIMESTAMPTZ;",
ADD COLUMN IF NOT EXISTS archived_at TIMESTAMPTZ;
CREATE TABLE IF NOT EXISTS encryption_devices (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
age_recipient TEXT NOT NULL UNIQUE,
status TEXT NOT NULL DEFAULT 'active',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
revoked_at TIMESTAMPTZ,
CHECK (status IN ('active', 'revoked')),
CHECK ((status = 'active' AND revoked_at IS NULL)
OR (status = 'revoked' AND revoked_at IS NOT NULL))
);",
)
.map_err(db_error)
}
pub fn list_encryption_devices(&self) -> Result<Vec<EncryptionDevice>> {
let mut client = self.connect()?;
let rows = client
.query(
"SELECT id, name, age_recipient, status,
EXTRACT(EPOCH FROM created_at)::BIGINT,
EXTRACT(EPOCH FROM revoked_at)::BIGINT
FROM encryption_devices
ORDER BY status, id",
&[],
)
.map_err(db_error)?;
Ok(rows
.into_iter()
.map(|row| EncryptionDevice {
id: row.get(0),
name: row.get(1),
age_recipient: row.get(2),
status: row.get(3),
created_at: row.get(4),
revoked_at: row.get(5),
})
.collect())
}
pub fn list_encryption_recipients(&self) -> Result<Vec<EncryptionRecipient>> {
let mut client = self.connect()?;
let rows = client
.query(
"SELECT id, name, age_recipient
FROM encryption_devices
WHERE status = 'active'
ORDER BY id",
&[],
)
.map_err(db_error)?;
Ok(rows
.into_iter()
.map(|row| EncryptionRecipient {
id: row.get(0),
name: row.get(1),
age_recipient: row.get(2),
})
.collect())
}
pub fn create_encryption_device(&self, device: CreateEncryptionDevice) -> Result<i64> {
let name = device.name.trim();
let recipient = device.age_recipient.trim();
validate_device(name, recipient)?;
let mut client = self.connect()?;
let row = client
.query_one(
"INSERT INTO encryption_devices (name, age_recipient)
VALUES ($1, $2)
RETURNING id",
&[&name, &recipient],
)
.map_err(db_error)?;
Ok(row.get(0))
}
pub fn revoke_encryption_device(&self, id: i64) -> Result<bool> {
let mut client = self.connect()?;
client
.execute(
"UPDATE encryption_devices
SET status = 'revoked', revoked_at = now()
WHERE id = $1 AND status = 'active'",
&[&id],
)
.map(|changed| changed == 1)
.map_err(db_error)
}
pub fn create_task(&self, task: CreateTask) -> Result<i64> {
validate_public_title(&task.public_title)?;
validate_encrypted_fields(
@@ -394,6 +483,22 @@ fn validate_public_title(title: &str) -> Result<()> {
}
}
fn validate_device(name: &str, recipient: &str) -> Result<()> {
if name.is_empty() {
return Err("device name must not be empty".to_owned());
}
if name.len() > 100 {
return Err("device name must be at most 100 bytes".to_owned());
}
if recipient.is_empty() {
return Err("age recipient must not be empty".to_owned());
}
if recipient.len() > 4096 || recipient.contains('\n') || recipient.contains('\r') {
return Err("age recipient must be a single line of at most 4096 bytes".to_owned());
}
Ok(())
}
fn validate_encrypted_fields(
payload: Option<&str>,
version: Option<i16>,
@@ -527,4 +632,14 @@ mod tests {
)
.unwrap());
}
#[test]
fn validates_encryption_device_fields() {
assert!(validate_device("Laptop", "age1example").is_ok());
assert_eq!(
validate_device("", "age1example").unwrap_err(),
"device name must not be empty"
);
assert!(validate_device("Laptop", "age1example\nother").is_err());
}
}