|
| 1 | +// Copyright (c) 2025 Cloudflare, Inc. |
| 2 | +// Licensed under the BSD-3-Clause license found in the LICENSE file or at https://opensource.org/licenses/BSD-3-Clause |
| 3 | + |
| 4 | +//! Cron job to fetch CT log list from Google. Same pattern as ccadb_roots_cron. |
| 5 | +
|
| 6 | +use sct_validator::CtLogList; |
| 7 | +use worker::{kv::KvStore, Env, Fetch, Headers, Method, Request, RequestInit, Result}; |
| 8 | + |
| 9 | +pub(crate) const CT_LOGS_NAMESPACE: &str = "ct_logs"; |
| 10 | +pub(crate) const CT_LOGS_FILENAME: &str = "ct_log_list.json"; |
| 11 | +const CT_LOG_LIST_URL: &str = "https://www.gstatic.com/ct/log_list/v3/log_list.json"; |
| 12 | + |
| 13 | +pub(crate) async fn update_ct_logs(kv: &KvStore) -> Result<()> { |
| 14 | + log::info!("Fetching CT log list from Google"); |
| 15 | + |
| 16 | + let headers = Headers::new(); |
| 17 | + headers.set("User-Agent", "Cloudflare MTCA (ct-logs@cloudflare.com)")?; |
| 18 | + |
| 19 | + let req = Request::new_with_init( |
| 20 | + CT_LOG_LIST_URL, |
| 21 | + &RequestInit { |
| 22 | + method: Method::Get, |
| 23 | + headers, |
| 24 | + ..Default::default() |
| 25 | + }, |
| 26 | + )?; |
| 27 | + |
| 28 | + let resp_bytes = Fetch::Request(req).send().await?.bytes().await?; |
| 29 | + |
| 30 | + // Validate before storing |
| 31 | + let log_list = CtLogList::from_chrome_log_list(&resp_bytes) |
| 32 | + .map_err(|e| format!("Failed to parse CT log list: {e}"))?; |
| 33 | + |
| 34 | + log::info!( |
| 35 | + "Parsed {} CT logs (timestamp: {})", |
| 36 | + log_list.logs.len(), |
| 37 | + log_list.log_list_timestamp |
| 38 | + ); |
| 39 | + |
| 40 | + kv.put_bytes(CT_LOGS_FILENAME, &resp_bytes)?.execute().await?; |
| 41 | + Ok(()) |
| 42 | +} |
| 43 | + |
| 44 | +pub(crate) async fn load_ct_logs(env: &Env) -> Result<CtLogList> { |
| 45 | + let kv = env.kv(CT_LOGS_NAMESPACE)?; |
| 46 | + |
| 47 | + let json_bytes = if let Some(bytes) = kv.get(CT_LOGS_FILENAME).bytes().await? { |
| 48 | + bytes |
| 49 | + } else { |
| 50 | + log::info!("CT log list not found in KV, fetching..."); |
| 51 | + update_ct_logs(&kv).await?; |
| 52 | + kv.get(CT_LOGS_FILENAME) |
| 53 | + .bytes() |
| 54 | + .await? |
| 55 | + .ok_or("CT log list not found after update")? |
| 56 | + }; |
| 57 | + |
| 58 | + CtLogList::from_chrome_log_list(&json_bytes) |
| 59 | + .map_err(|e| format!("Failed to parse CT log list from KV: {e}").into()) |
| 60 | +} |
0 commit comments