Skip to main content

max / makenotwork

1.5 KB · 53 lines History Blame Raw
1 //! TLS certificate check storage.
2
3 use super::{Result, SqlitePool, TlsStatus};
4 use tracing::instrument;
5
6 #[derive(Debug, sqlx::FromRow, serde::Serialize)]
7 pub struct TlsCheckRow {
8 pub id: i64,
9 pub target: String,
10 pub host: String,
11 pub valid: bool,
12 pub days_remaining: i64,
13 pub not_before: String,
14 pub not_after: String,
15 pub subject: String,
16 pub issuer: String,
17 pub checked_at: String,
18 pub error: Option<String>,
19 }
20
21 #[instrument(skip_all)]
22 pub async fn insert_tls_check(pool: &SqlitePool, status: &TlsStatus) -> Result<i64> {
23 let result = sqlx::query(
24 "INSERT INTO tls_checks (target, host, valid, days_remaining, not_before, not_after, subject, issuer, checked_at, error)
25 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
26 )
27 .bind(&status.target)
28 .bind(&status.host)
29 .bind(status.valid)
30 .bind(status.days_remaining)
31 .bind(&status.not_before)
32 .bind(&status.not_after)
33 .bind(&status.subject)
34 .bind(&status.issuer)
35 .bind(&status.checked_at)
36 .bind(&status.error)
37 .execute(pool)
38 .await?;
39
40 Ok(result.last_insert_rowid())
41 }
42
43 #[instrument(skip_all)]
44 pub async fn get_latest_tls_check(pool: &SqlitePool, target: &str) -> Result<Option<TlsCheckRow>> {
45 Ok(sqlx::query_as::<_, TlsCheckRow>(
46 "SELECT id, target, host, valid, days_remaining, not_before, not_after, subject, issuer, checked_at, error
47 FROM tls_checks WHERE target = ? ORDER BY id DESC LIMIT 1",
48 )
49 .bind(target)
50 .fetch_optional(pool)
51 .await?)
52 }
53