refactor: replace unmaintained chrono with time (RUSTSEC-2026-0543)
Adds a timeutil module (unix_now, is_valid_unix, RFC 3339 format/parse) and moves ClickHouse event timestamps to time::serde::rfc3339. cargo deny is green again.
This commit is contained in:
parent
f25dc3558a
commit
e2adf8601c
19 changed files with 74 additions and 128 deletions
|
|
@ -235,7 +235,7 @@ async fn push_attack_event(writer: &Option<Arc<tokio::sync::Mutex<ClickHouseWrit
|
|||
return;
|
||||
};
|
||||
let event = ClickHouseEvent {
|
||||
timestamp: chrono::Utc::now(),
|
||||
timestamp: time::OffsetDateTime::now_utc(),
|
||||
event_type: "attack".to_string(),
|
||||
ip: String::new(),
|
||||
data_float: pps,
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ impl Gateway {
|
|||
return;
|
||||
};
|
||||
let event = ClickHouseEvent {
|
||||
timestamp: chrono::Utc::now(),
|
||||
timestamp: time::OffsetDateTime::now_utc(),
|
||||
event_type: event_type.to_string(),
|
||||
ip: ip.to_string(),
|
||||
data_float: 0.0,
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ pub mod manager;
|
|||
pub mod metrics;
|
||||
pub mod protocol;
|
||||
pub mod store;
|
||||
pub mod timeutil;
|
||||
pub mod traffic;
|
||||
pub mod tui;
|
||||
pub mod xdp;
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ async fn backfill(state: &AppState) -> ApiResult<usize> {
|
|||
.get_multiplexed_async_connection()
|
||||
.await
|
||||
.map_err(redis_unavailable)?;
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
let now = crate::timeutil::unix_now();
|
||||
let mut cursor = 0_u64;
|
||||
let mut filled = 0_usize;
|
||||
loop {
|
||||
|
|
|
|||
|
|
@ -46,9 +46,7 @@ pub(super) fn legacy_metadata(now: i64) -> BlacklistMetadata {
|
|||
}
|
||||
|
||||
pub(super) fn format_timestamp(timestamp: i64) -> String {
|
||||
chrono::DateTime::from_timestamp(timestamp, 0)
|
||||
.map(|value| value.to_rfc3339())
|
||||
.unwrap_or_else(|| chrono::Utc::now().to_rfc3339())
|
||||
crate::timeutil::format_rfc3339(timestamp).unwrap_or_else(crate::timeutil::now_rfc3339)
|
||||
}
|
||||
|
||||
pub(super) fn event_payload(
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ pub async fn list_blacklist(
|
|||
.take(usize::try_from(limit).unwrap_or(usize::MAX))
|
||||
.collect();
|
||||
let metadata = load_metadata(&mut conn, &page).await?;
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
let now = crate::timeutil::unix_now();
|
||||
let items = page
|
||||
.iter()
|
||||
.filter_map(|target| entry(target, metadata.get(target), now))
|
||||
|
|
@ -116,8 +116,8 @@ fn entry(target: &str, metadata: Option<&Option<BlacklistMetadata>>, now: i64) -
|
|||
None => legacy_metadata(now),
|
||||
};
|
||||
if record.expires_at_unix <= now
|
||||
|| chrono::DateTime::from_timestamp(record.created_at_unix, 0).is_none()
|
||||
|| chrono::DateTime::from_timestamp(record.expires_at_unix, 0).is_none()
|
||||
|| !crate::timeutil::is_valid_unix(record.created_at_unix)
|
||||
|| !crate::timeutil::is_valid_unix(record.expires_at_unix)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ pub async fn add_blacklist(
|
|||
}
|
||||
|
||||
let target = ip.to_string();
|
||||
let created_at_unix = chrono::Utc::now().timestamp();
|
||||
let created_at_unix = crate::timeutil::unix_now();
|
||||
let metadata = super::common::BlacklistMetadata {
|
||||
created_at_unix,
|
||||
expires_at_unix: created_at_unix + duration_secs as i64,
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ pub struct Claims {
|
|||
}
|
||||
|
||||
pub fn create_token(secret: &str, expiration: u64, audience: &str) -> Result<String, jsonwebtoken::errors::Error> {
|
||||
let now = chrono::Utc::now().timestamp() as usize;
|
||||
let now = crate::timeutil::unix_now() as usize;
|
||||
let claims = Claims {
|
||||
sub: "rampart-admin".to_string(),
|
||||
aud: audience.to_string(),
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ async fn check_nodes(state: &AppState) -> anyhow::Result<()> {
|
|||
let mut conn = state.redis_client.get_multiplexed_async_connection().await?;
|
||||
let keys = collect_node_keys(&mut conn).await?;
|
||||
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
let now = crate::timeutil::unix_now();
|
||||
|
||||
for key in &keys {
|
||||
let raw: Option<String> = conn.get(key).await?;
|
||||
|
|
@ -73,8 +73,7 @@ pub fn offline_update(raw: &str, now: i64) -> Option<String> {
|
|||
let mut node = serde_json::from_str::<serde_json::Value>(raw).ok()?;
|
||||
let hb = node["last_heartbeat"]
|
||||
.as_str()
|
||||
.and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
|
||||
.map(|t| t.timestamp())
|
||||
.and_then(crate::timeutil::parse_rfc3339_unix)
|
||||
.unwrap_or(0);
|
||||
if now - hb <= NODE_TTL_SECS {
|
||||
return None;
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@
|
|||
//! request timeout and every flush by a global timeout, so an unreachable or
|
||||
//! hung ClickHouse degrades into dropped events instead of a stalled daemon.
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use prometheus::IntCounter;
|
||||
use serde::Serialize;
|
||||
use std::sync::{Arc, LazyLock};
|
||||
use std::time::Duration;
|
||||
use time::OffsetDateTime;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::watch;
|
||||
|
||||
|
|
@ -39,7 +39,8 @@ pub static CLICKHOUSE_DROPPED: LazyLock<IntCounter> = LazyLock::new(|| {
|
|||
/// One row of the `rampart_events` table.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ClickHouseEvent {
|
||||
pub timestamp: DateTime<Utc>,
|
||||
#[serde(with = "time::serde::rfc3339")]
|
||||
pub timestamp: OffsetDateTime,
|
||||
pub event_type: String,
|
||||
pub ip: String,
|
||||
pub data_float: f64,
|
||||
|
|
|
|||
|
|
@ -58,10 +58,10 @@ pub(super) fn handle(msg: &Msg, blacklist: &Blacklist) -> anyhow::Result<()> {
|
|||
///
|
||||
/// Returns `Ok(None)` when the expiry is already in the past.
|
||||
fn absolute_duration(expires_at: i64) -> anyhow::Result<Option<Duration>> {
|
||||
if chrono::DateTime::from_timestamp(expires_at, 0).is_none() {
|
||||
if !crate::timeutil::is_valid_unix(expires_at) {
|
||||
anyhow::bail!("invalid blacklist event expiry timestamp");
|
||||
}
|
||||
let remaining = expires_at.saturating_sub(chrono::Utc::now().timestamp());
|
||||
let remaining = expires_at.saturating_sub(crate::timeutil::unix_now());
|
||||
if remaining <= 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@ async fn publish_ban(
|
|||
manager: &mut Option<ConnectionManager>,
|
||||
event: &LocalBanEvent,
|
||||
) -> anyhow::Result<()> {
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
let now = crate::timeutil::unix_now();
|
||||
let remaining = event.expires_at_unix.saturating_sub(now);
|
||||
if remaining <= 0 {
|
||||
return Ok(());
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ pub(super) async fn reconcile(manager: &ConnectionManager, blacklist: &Blacklist
|
|||
let mut conn = manager.clone();
|
||||
let members: Vec<String> = command(redis::cmd("SMEMBERS").arg(BLACKLIST_KEY).query_async(&mut conn)).await?;
|
||||
let metadata: HashMap<String, String> = command(conn.hgetall(BLACKLIST_METADATA_KEY)).await?;
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
let now = crate::timeutil::unix_now();
|
||||
let mut active = Vec::with_capacity(members.len());
|
||||
|
||||
for target in members {
|
||||
|
|
@ -50,8 +50,8 @@ pub(super) async fn reconcile(manager: &ConnectionManager, blacklist: &Blacklist
|
|||
None => continue,
|
||||
},
|
||||
};
|
||||
if chrono::DateTime::from_timestamp(record.created_at_unix, 0).is_none()
|
||||
|| chrono::DateTime::from_timestamp(record.expires_at_unix, 0).is_none()
|
||||
if !crate::timeutil::is_valid_unix(record.created_at_unix)
|
||||
|| !crate::timeutil::is_valid_unix(record.expires_at_unix)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
43
src/timeutil.rs
Normal file
43
src/timeutil.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
//! Wall-clock helpers on top of the `time` crate (unix seconds and RFC 3339).
|
||||
//!
|
||||
//! Every timestamp that crosses a process boundary (Redis records, manager API,
|
||||
//! ClickHouse rows, JWT claims) is unix seconds or an RFC 3339 string; this module is
|
||||
//! the single place that converts between them and rejects out-of-range values.
|
||||
|
||||
use time::OffsetDateTime;
|
||||
use time::format_description::well_known::Rfc3339;
|
||||
|
||||
/// Current unix time in seconds.
|
||||
#[must_use]
|
||||
pub fn unix_now() -> i64 {
|
||||
OffsetDateTime::now_utc().unix_timestamp()
|
||||
}
|
||||
|
||||
/// Whether `ts` (unix seconds) is a representable calendar date. Absurd values from
|
||||
/// untrusted records (Redis, API) must be rejected instead of flowing into arithmetic.
|
||||
#[must_use]
|
||||
pub fn is_valid_unix(ts: i64) -> bool {
|
||||
OffsetDateTime::from_unix_timestamp(ts).is_ok()
|
||||
}
|
||||
|
||||
/// Formats unix seconds as RFC 3339 (UTC, `Z` suffix); `None` when out of range.
|
||||
#[must_use]
|
||||
pub fn format_rfc3339(ts: i64) -> Option<String> {
|
||||
OffsetDateTime::from_unix_timestamp(ts).ok()?.format(&Rfc3339).ok()
|
||||
}
|
||||
|
||||
/// Current time as RFC 3339.
|
||||
#[must_use]
|
||||
pub fn now_rfc3339() -> String {
|
||||
OffsetDateTime::now_utc()
|
||||
.format(&Rfc3339)
|
||||
.unwrap_or_else(|_| String::from("1970-01-01T00:00:00Z"))
|
||||
}
|
||||
|
||||
/// Parses an RFC 3339 string (any offset) into unix seconds.
|
||||
#[must_use]
|
||||
pub fn parse_rfc3339_unix(text: &str) -> Option<i64> {
|
||||
OffsetDateTime::parse(text, &Rfc3339)
|
||||
.ok()
|
||||
.map(OffsetDateTime::unix_timestamp)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue