diff options
author | Lukasz Woznicki <lukasz.woznicki@softwareag.com> | 2022-02-09 23:35:36 +0000 |
---|---|---|
committer | Lukasz Woznicki <lukasz.woznicki@softwareag.com> | 2022-02-14 08:26:22 +0000 |
commit | 03124da93bf8788c0b79ce61219e3c8a2c603768 (patch) | |
tree | 8ecf531576a39cc209b597bb4cad3c0267c7dcbc /crates/core/tedge_mapper | |
parent | 379ea3fcdaccf2aec01b24d16c485d4bcabd9a58 (diff) |
Use 'time' instead of 'chrono' due to CVE for thin_edge_json and all dependent crates
Signed-off-by: Lukasz Woznicki <lukasz.woznicki@softwareag.com>
Diffstat (limited to 'crates/core/tedge_mapper')
5 files changed, 21 insertions, 35 deletions
diff --git a/crates/core/tedge_mapper/Cargo.toml b/crates/core/tedge_mapper/Cargo.toml index c1a13215..baf8403f 100644 --- a/crates/core/tedge_mapper/Cargo.toml +++ b/crates/core/tedge_mapper/Cargo.toml @@ -32,7 +32,6 @@ async-trait = "0.1" batcher = { path = "../../common/batcher" } c8y_smartrest = { path = "../c8y_smartrest" } c8y_translator = { path = "../c8y_translator" } -chrono = "0.4" clock = { path = "../../common/clock" } csv = "1.1" download = { path = "../../common/download" } diff --git a/crates/core/tedge_mapper/src/az_converter.rs b/crates/core/tedge_mapper/src/az_converter.rs index 27e6c1e6..d6ca77dc 100644 --- a/crates/core/tedge_mapper/src/az_converter.rs +++ b/crates/core/tedge_mapper/src/az_converter.rs @@ -53,15 +53,15 @@ mod tests { use crate::size_threshold::SizeThresholdExceeded; use assert_json_diff::*; use assert_matches::*; - use chrono::{FixedOffset, TimeZone}; use mqtt_channel::Topic; use serde_json::json; + use time::macros::datetime; struct TestClock; impl Clock for TestClock { fn now(&self) -> clock::Timestamp { - FixedOffset::east(5 * 3600).ymd(2021, 4, 8).and_hms(0, 0, 0) + datetime!(2021-04-08 00:00:00 +05:00) } } diff --git a/crates/core/tedge_mapper/src/collectd_mapper/batcher.rs b/crates/core/tedge_mapper/src/collectd_mapper/batcher.rs index e7ecd830..9844485e 100644 --- a/crates/core/tedge_mapper/src/collectd_mapper/batcher.rs +++ b/crates/core/tedge_mapper/src/collectd_mapper/batcher.rs @@ -7,7 +7,6 @@ use thin_edge_json::{ }; use crate::collectd_mapper::{collectd::CollectdMessage, error::DeviceMonitorError}; -use chrono::Local; use thin_edge_json::group::MeasurementGrouperError; #[derive(Debug)] @@ -22,7 +21,7 @@ impl MessageBatch { let mut messages = messages.into_iter(); if let Some(first_message) = messages.next() { - let timestamp = first_message.timestamp.with_timezone(Local::now().offset()); + let timestamp = first_message.timestamp; let mut batch = MessageBatch::start_batch(first_message, timestamp)?; for message in messages { batch.add_to_batch(message)?; @@ -72,12 +71,12 @@ impl MessageBatch { mod tests { use super::*; use assert_matches::assert_matches; - use chrono::{TimeZone, Utc}; use clock::{Clock, WallClock}; + use time::macros::datetime; #[test] fn test_message_batch_processor() -> anyhow::Result<()> { - let timestamp = Utc.ymd(2015, 5, 15).and_hms_milli(0, 0, 1, 444); + let timestamp = datetime!(2015-05-15 0:00:01.444 UTC); let collectd_message = CollectdMessage::new("temperature", "value", 32.5, timestamp); let mut message_batch = MessageBatch::start_batch(collectd_message, WallClock.now())?; diff --git a/crates/core/tedge_mapper/src/collectd_mapper/collectd.rs b/crates/core/tedge_mapper/src/collectd_mapper/collectd.rs index ab98f67d..0bb0fe22 100644 --- a/crates/core/tedge_mapper/src/collectd_mapper/collectd.rs +++ b/crates/core/tedge_mapper/src/collectd_mapper/collectd.rs @@ -1,13 +1,13 @@ use batcher::Batchable; -use chrono::{DateTime, NaiveDateTime, Utc}; use mqtt_channel::Message; use thin_edge_json::measurement::MeasurementVisitor; +use time::{Duration, OffsetDateTime}; #[derive(Debug)] pub struct CollectdMessage { pub metric_group_key: String, pub metric_key: String, - pub timestamp: DateTime<Utc>, + pub timestamp: OffsetDateTime, pub metric_value: f64, } @@ -43,7 +43,7 @@ impl CollectdMessage { metric_group_key: &str, metric_key: &str, metric_value: f64, - timestamp: DateTime<Utc>, + timestamp: OffsetDateTime, ) -> Self { Self { metric_group_key: metric_group_key.to_string(), @@ -167,10 +167,11 @@ impl CollectdPayload { }) } - pub fn timestamp(&self) -> DateTime<Utc> { + pub fn timestamp(&self) -> OffsetDateTime { let timestamp = self.timestamp.trunc() as i64; let nanoseconds = (self.timestamp.fract() * 1.0e9) as u32; - DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(timestamp, nanoseconds), Utc) + OffsetDateTime::from_unix_timestamp(timestamp).unwrap() + + Duration::nanoseconds(nanoseconds as i64) } } @@ -181,18 +182,17 @@ impl Batchable for CollectdMessage { format!("{}/{}", &self.metric_group_key, &self.metric_key) } - fn event_time(&self) -> DateTime<Utc> { + fn event_time(&self) -> OffsetDateTime { self.timestamp } } #[cfg(test)] mod tests { - use std::ops::Index; - use assert_matches::assert_matches; - use chrono::TimeZone; use mqtt_channel::Topic; + use std::ops::Index; + use time::macros::datetime; use super::*; @@ -212,10 +212,7 @@ mod tests { assert_eq!(metric_group_key, "temperature"); assert_eq!(metric_key, "value"); - assert_eq!( - *timestamp, - Utc.ymd(1973, 11, 29).and_hms_milli(21, 33, 09, 0) - ); + assert_eq!(*timestamp, datetime!(1973-11-29 21:33:09.0 UTC)); assert_eq!(*metric_value, 32.5); } @@ -230,15 +227,12 @@ mod tests { metric_group_key, metric_key, timestamp, - metric_value, + metric_value: _, } = collectd_message.index(0); assert_eq!(metric_group_key, "temperature"); assert_eq!(metric_key, "value_val1"); - assert_eq!( - *timestamp, - Utc.ymd(1973, 11, 29).and_hms_milli(21, 33, 09, 0) - ); + assert_eq!(*timestamp, datetime!(1973-11-29 21:33:09.0 UTC)); let CollectdMessage { metric_group_key, @@ -249,10 +243,7 @@ mod tests { assert_eq!(metric_group_key, "temperature"); assert_eq!(metric_key, "value_val2"); - assert_eq!( - *timestamp, - Utc.ymd(1973, 11, 29).and_hms_milli(21, 33, 09, 0) - ); + assert_eq!(*timestamp, datetime!(1973-11-29 21:33:09.0 UTC)); assert_eq!(*metric_value, 45.2); } @@ -272,10 +263,7 @@ mod tests { assert_eq!(metric_group_key, "temperature"); assert_eq!(metric_key, "value"); - assert_eq!( - *timestamp, - Utc.ymd(1973, 11, 29).and_hms_milli(21, 33, 09, 125) - ); + assert_eq!(*timestamp, datetime!(1973-11-29 21:33:09.125 UTC)); assert_eq!(*metric_value, 32.5); } diff --git a/crates/core/tedge_mapper/src/sm_c8y_mapper/mapper.rs b/crates/core/tedge_mapper/src/sm_c8y_mapper/mapper.rs index 251e0f90..a778f86b 100644 --- a/crates/core/tedge_mapper/src/sm_c8y_mapper/mapper.rs +++ b/crates/core/tedge_mapper/src/sm_c8y_mapper/mapper.rs @@ -618,7 +618,7 @@ mod tests { #[test_case("/path/to/another-variant-2021-10-25T07:45:41Z.log")] #[test_case("/yet-another-variant-2021-10-25T07:45:41Z.log")] fn test_datetime_parsing_from_path(file_path: &str) { - // checking that `get_date_from_file_path` unwraps a `chrono::NaiveDateTime` object. + // checking that `get_date_from_file_path` unwraps a `OffsetDateTime` object. // this should return an Ok Result. let path_buf = PathBuf::from_str(file_path).unwrap(); let path_buf_datetime = get_datetime_from_file_path(&path_buf); @@ -630,7 +630,7 @@ mod tests { #[test_case("/path/to/another-variant-07:45:41Z-2021-10-25T.log")] #[test_case("/yet-another-variant-2021-10-25T07:45Z.log")] fn test_datetime_parsing_from_path_fail(file_path: &str) { - // checking that `get_date_from_file_path` unwraps a `chrono::NaiveDateTime` object. + // checking that `get_date_from_file_path` unwraps a `OffsetDateTime` object. // this should return an err. let path_buf = PathBuf::from_str(file_path).unwrap(); let path_buf_datetime = get_datetime_from_file_path(&path_buf); |