Skip to main content

max / makenotwork

2.3 KB · 66 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 /// Whether the chain validated against the bundled web-PKI roots. `None` on
20 /// rows written before migration 14, which carry no trust readings.
21 pub webpki_trusted: Option<bool>,
22 /// Whether the chain validated against this host's own trust store. `None`
23 /// as above. `Some(false)` here alongside `Some(true)` for the web PKI is
24 /// the host CA bundle failing while the public one is fine.
25 pub platform_trusted: Option<bool>,
26 pub webpki_error: Option<String>,
27 pub platform_error: Option<String>,
28 }
29
30 #[instrument(skip_all)]
31 pub async fn insert_tls_check(pool: &SqlitePool, status: &TlsStatus) -> Result<i64> {
32 let result = sqlx::query(
33 "INSERT INTO tls_checks (target, host, valid, days_remaining, not_before, not_after, subject, issuer, checked_at, error, webpki_trusted, platform_trusted, webpki_error, platform_error)
34 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
35 )
36 .bind(&status.target)
37 .bind(&status.host)
38 .bind(status.valid)
39 .bind(status.days_remaining)
40 .bind(&status.not_before)
41 .bind(&status.not_after)
42 .bind(&status.subject)
43 .bind(&status.issuer)
44 .bind(&status.checked_at)
45 .bind(&status.error)
46 .bind(status.webpki_trusted)
47 .bind(status.platform_trusted)
48 .bind(&status.webpki_error)
49 .bind(&status.platform_error)
50 .execute(pool)
51 .await?;
52
53 Ok(result.last_insert_rowid())
54 }
55
56 #[instrument(skip_all)]
57 pub async fn get_latest_tls_check(pool: &SqlitePool, target: &str) -> Result<Option<TlsCheckRow>> {
58 Ok(sqlx::query_as::<_, TlsCheckRow>(
59 "SELECT id, target, host, valid, days_remaining, not_before, not_after, subject, issuer, checked_at, error, webpki_trusted, platform_trusted, webpki_error, platform_error
60 FROM tls_checks WHERE target = ? ORDER BY id DESC LIMIT 1",
61 )
62 .bind(target)
63 .fetch_optional(pool)
64 .await?)
65 }
66