Skip to content

Commit

Permalink
RUSTSEC-2020-0071: drop chrono dependency in icx-asset
Browse files Browse the repository at this point in the history
this required a small rework, because it's not possible to obtain
completely sound local time offset value in multithreaded environment
- time-rs/time#427
- time-rs/time#500
- time-rs/time#493
  • Loading branch information
Marcin Nowak-Liebiediew committed Aug 9, 2022
1 parent fcbb661 commit e842aa5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 56 deletions.
42 changes: 6 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/assets_canister/icx-asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ rust-version = "1.60.0"
[dependencies]
anyhow = "1.0.34"
candid = "0.7.15"
chrono = "0.4.19"
clap = { version = "3.0.14", features = ["derive", "cargo"] }
delay = "0.3.1"
garcon = "0.2.2"
Expand All @@ -33,6 +32,7 @@ pem = "1.0.1"
serde = "1.0.115"
serde_bytes = "0.11.5"
serde_json = "1.0.57"
time = { version = "0.3.9", features = ["formatting", "local-offset"] }
tokio = { version = "1.2.0", features = ["full", "rt"] }
thiserror = "1.0.24"
walkdir = "2.3.1"
18 changes: 10 additions & 8 deletions src/assets_canister/icx-asset/src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use ic_utils::Canister;

use num_traits::ToPrimitive;
use serde::Deserialize;
use std::time::SystemTime;
use time::{format_description, Duration, OffsetDateTime, UtcOffset};

pub async fn list(canister: &Canister<'_>) -> Result {
pub async fn list(canister: &Canister<'_>, local_offset: UtcOffset) -> Result {
#[derive(CandidType, Deserialize)]
struct Encoding {
modified: Int,
Expand All @@ -33,18 +33,20 @@ pub async fn list(canister: &Canister<'_>) -> Result {
.call()
.await?;

use chrono::offset::Local;
use chrono::DateTime;

for entry in entries {
for encoding in entry.encodings {
let modified = encoding.modified;
let modified = SystemTime::UNIX_EPOCH
+ std::time::Duration::from_nanos(modified.0.to_u64().unwrap());
let modified =
OffsetDateTime::from_unix_timestamp_nanos(modified.0.to_i128().unwrap())?;

let offset = Duration::new(local_offset.whole_seconds().into(), 0);
let timestamp = modified.checked_add(offset).unwrap_or(modified);
let timestamp_format =
format_description::parse("[year]-[month]-[day] [hour]:[minute]:[second]")?;

eprintln!(
"{:>20} {:>15} {:50} ({}, {})",
DateTime::<Local>::from(modified).format("%F %X"),
timestamp.format(&timestamp_format)?,
encoding.length.0,
entry.key,
entry.content_type,
Expand Down
41 changes: 30 additions & 11 deletions src/assets_canister/icx-asset/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use candid::Principal;
use clap::{crate_authors, crate_version, Parser};
use ic_agent::identity::{AnonymousIdentity, BasicIdentity, Secp256k1Identity};
use ic_agent::{agent, Agent, Identity};
use time::UtcOffset;

use crate::commands::upload::upload;
use std::path::PathBuf;
Expand Down Expand Up @@ -93,8 +94,7 @@ fn create_identity(maybe_pem: Option<PathBuf>) -> Box<dyn Identity + Sync + Send
}
}

#[tokio::main(flavor = "multi_thread", worker_threads = 10)]
async fn main() -> support::Result {
fn main() -> support::Result {
let opts: Opts = Opts::parse();

let ttl: std::time::Duration = opts
Expand All @@ -110,33 +110,52 @@ async fn main() -> support::Result {
.build()?;

let normalized_replica = opts.replica.strip_suffix('/').unwrap_or(&opts.replica);
if normalized_replica != DEFAULT_IC_GATEWAY {
agent.fetch_root_key().await?;
}

match &opts.subcommand {
let local_offset = time::OffsetDateTime::now_local().map_or(UtcOffset::UTC, |v| v.offset());

let result = tokio::runtime::Builder::new_multi_thread()
.worker_threads(10)
.enable_all()
.build()
.unwrap()
.block_on(async {
if normalized_replica != DEFAULT_IC_GATEWAY {
agent.fetch_root_key().await?;
}

dispatch(&opts.subcommand, agent, ttl, local_offset).await
});

result
}

async fn dispatch(
subcommand: &SubCommand,
agent: Agent,
ttl: std::time::Duration,
local_offset: UtcOffset,
) -> support::Result {
match subcommand {
SubCommand::List(o) => {
let canister = ic_utils::Canister::builder()
.with_agent(&agent)
.with_canister_id(Principal::from_text(&o.canister_id)?)
.build()?;
list(&canister).await?;
list(&canister, local_offset).await
}
SubCommand::Sync(o) => {
let canister = ic_utils::Canister::builder()
.with_agent(&agent)
.with_canister_id(Principal::from_text(&o.canister_id)?)
.build()?;
sync(&canister, ttl, o).await?;
sync(&canister, ttl, o).await
}
SubCommand::Upload(o) => {
let canister = ic_utils::Canister::builder()
.with_agent(&agent)
.with_canister_id(Principal::from_text(&o.canister_id)?)
.build()?;
upload(&canister, o).await?;
upload(&canister, o).await
}
}

Ok(())
}

0 comments on commit e842aa5

Please sign in to comment.