diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa8dce..9cd5ac9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ file is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Changed + +- Replaced `lazy_static` with the new `std::sync::OnceLock`. + ## [0.12.3] - 2023-07-10 ### Changed @@ -225,6 +231,7 @@ Versioning](https://semver.org/spec/v2.0.0.html). - Initial release. +[Unreleased]: https://github.com/aicers/giganto/compare/0.12.3...main [0.12.3]: https://github.com/aicers/giganto/compare/0.12.2...0.12.3 [0.12.2]: https://github.com/aicers/giganto/compare/0.12.1...0.12.2 [0.12.1]: https://github.com/aicers/giganto/compare/0.12.0...0.12.1 diff --git a/Cargo.lock b/Cargo.lock index 319fec1..ea5ce0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -496,12 +496,12 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ - "darling_core 0.20.1", - "darling_macro 0.20.1", + "darling_core 0.20.3", + "darling_macro 0.20.3", ] [[package]] @@ -520,9 +520,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -545,11 +545,11 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ - "darling_core 0.20.1", + "darling_core 0.20.3", "quote 1.0.29", "syn 2.0.25", ] @@ -644,9 +644,9 @@ dependencies = [ [[package]] name = "equivalent" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88bffebc5d80432c9b140ee17875ff173a8ab62faad5b257da912bd2f6c1c0a1" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" @@ -824,14 +824,13 @@ dependencies = [ "giganto-client", "humantime", "humantime-serde", - "lazy_static", "libc", "num_enum", "pcap", "quinn", "rocksdb", "roxy", - "rustls 0.21.3", + "rustls 0.21.5", "rustls-pemfile", "semver", "serde", @@ -1776,7 +1775,7 @@ dependencies = [ "quinn-proto", "quinn-udp", "rustc-hash", - "rustls 0.21.3", + "rustls 0.21.5", "thiserror", "tokio", "tracing", @@ -1792,7 +1791,7 @@ dependencies = [ "rand", "ring", "rustc-hash", - "rustls 0.21.3", + "rustls 0.21.5", "rustls-native-certs", "slab", "thiserror", @@ -1920,8 +1919,8 @@ checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.3.2", - "regex-syntax 0.7.3", + "regex-automata 0.3.3", + "regex-syntax 0.7.4", ] [[package]] @@ -1935,13 +1934,13 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83d3daa6976cffb758ec878f108ba0e062a45b2d6ca3a2cca965338855476caf" +checksum = "39354c10dd07468c2e73926b23bb9c2caca74c5501e38a35da70406f1d923310" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.7.3", + "regex-syntax 0.7.4", ] [[package]] @@ -1952,9 +1951,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "regex-syntax" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ab07dc67230e4a4718e70fd5c20055a4334b121f1f9db8fe63ef39ce9b8c846" +checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" [[package]] name = "ring" @@ -2058,9 +2057,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.21.3" +version = "0.21.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b19faa85ecb5197342b54f987b142fb3e30d0c90da40f80ef4fa9a726e6676ed" +checksum = "79ea77c539259495ce8ca47f53e66ae0330a8819f67e23ac96ca02f50e7b7d36" dependencies = [ "log", "ring", @@ -2236,7 +2235,7 @@ version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "881b6f881b17d13214e5d494c939ebab463d01264ce1811e9d4ac3a882e7695f" dependencies = [ - "darling 0.20.1", + "darling 0.20.3", "proc-macro2 1.0.64", "quote 1.0.29", "syn 2.0.25", diff --git a/Cargo.toml b/Cargo.toml index 823afef..452ea98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,5 @@ warp = { version = "0.3", features = ["tls"] } x509-parser = "0.15" [dev-dependencies] -lazy_static = "1" tempfile = "3" url = "2" diff --git a/src/ingest/tests.rs b/src/ingest/tests.rs index 3f93817..9d4d6ff 100644 --- a/src/ingest/tests.rs +++ b/src/ingest/tests.rs @@ -18,14 +18,13 @@ use giganto_client::{ Packet, RecordType, }, }; -use lazy_static::lazy_static; use quinn::{Connection, Endpoint}; use std::{ collections::HashMap, fs, net::{IpAddr, Ipv6Addr, SocketAddr}, path::Path, - sync::Arc, + sync::{Arc, OnceLock}, }; use tempfile::TempDir; use tokio::{ @@ -33,8 +32,10 @@ use tokio::{ task::JoinHandle, }; -lazy_static! { - pub(crate) static ref TOKEN: Mutex = Mutex::new(0); +fn get_token() -> &'static Mutex { + static TOKEN: OnceLock> = OnceLock::new(); + + TOKEN.get_or_init(|| Mutex::new(0)) } const CERT_PATH: &str = "tests/cert.pem"; @@ -148,7 +149,7 @@ fn init_client() -> Endpoint { let client_crypto = rustls::ClientConfig::builder() .with_safe_defaults() .with_root_certificates(server_root) - .with_single_cert(cert_chain, pv_key) + .with_client_auth_cert(cert_chain, pv_key) .expect("the server root, cert chain or private key are not valid"); let mut endpoint = @@ -162,7 +163,7 @@ fn init_client() -> Endpoint { async fn conn() { const RECORD_TYPE_CONN: RecordType = RecordType::Conn; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -201,7 +202,7 @@ async fn conn() { async fn dns() { const RECORD_TYPE_DNS: RecordType = RecordType::Dns; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -246,7 +247,7 @@ async fn dns() { async fn log() { const RECORD_TYPE_LOG: RecordType = RecordType::Log; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -274,7 +275,7 @@ async fn log() { #[tokio::test] async fn http() { const RECORD_TYPE_HTTP: RecordType = RecordType::Http; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -326,7 +327,7 @@ async fn http() { #[tokio::test] async fn rdp() { const RECORD_TYPE_RDP: RecordType = RecordType::Rdp; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -359,7 +360,7 @@ async fn rdp() { #[tokio::test] async fn periodic_time_series() { const RECORD_TYPE_PERIOD_TIME_SERIES: RecordType = RecordType::PeriodicTimeSeries; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -398,7 +399,7 @@ async fn periodic_time_series() { #[tokio::test] async fn smtp() { const RECORD_TYPE_SMTP: RecordType = RecordType::Smtp; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -436,7 +437,7 @@ async fn smtp() { #[tokio::test] async fn ntlm() { const RECORD_TYPE_NTLM: RecordType = RecordType::Ntlm; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -475,7 +476,7 @@ async fn ntlm() { #[tokio::test] async fn kerberos() { const RECORD_TYPE_KERBEROS: RecordType = RecordType::Kerberos; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -526,7 +527,7 @@ async fn kerberos() { #[tokio::test] async fn ssh() { const RECORD_TYPE_SSH: RecordType = RecordType::Ssh; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -570,7 +571,7 @@ async fn ssh() { #[tokio::test] async fn dce_rpc() { const RECORD_TYPE_DCE_RPC: RecordType = RecordType::DceRpc; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -614,7 +615,7 @@ async fn dce_rpc() { async fn oplog() { const RECORD_TYPE_OPLOG: RecordType = RecordType::Oplog; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -647,7 +648,7 @@ async fn oplog() { async fn packet() { const RECORD_TYPE_PACKET: RecordType = RecordType::Packet; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -679,7 +680,7 @@ async fn packet() { #[tokio::test] async fn ftp() { const RECORD_TYPE_FTP: RecordType = RecordType::Ftp; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -723,7 +724,7 @@ async fn ftp() { #[tokio::test] async fn mqtt() { const RECORD_TYPE_MQTT: RecordType = RecordType::Mqtt; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -761,7 +762,7 @@ async fn mqtt() { #[tokio::test] async fn ldap() { const RECORD_TYPE_LDAP: RecordType = RecordType::Ldap; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -800,7 +801,7 @@ async fn ldap() { #[tokio::test] async fn tls() { const RECORD_TYPE_TLS: RecordType = RecordType::Tls; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -850,7 +851,7 @@ async fn tls() { #[tokio::test] async fn smb() { const RECORD_TYPE_SMB: RecordType = RecordType::Smb; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -893,7 +894,7 @@ async fn smb() { #[tokio::test] async fn nfs() { const RECORD_TYPE_NFS: RecordType = RecordType::Nfs; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -928,7 +929,7 @@ async fn nfs() { async fn ack_info() { const RECORD_TYPE_LOG: RecordType = RecordType::Log; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); @@ -974,7 +975,7 @@ async fn one_short_reproduce_channel_close() { const CHANNEL_CLOSE_TIMESTAMP: i64 = -1; const CHANNEL_CLOSE_MESSAGE: &[u8; 12] = b"channel done"; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); run_server(db_dir); diff --git a/src/peer.rs b/src/peer.rs index b3115b4..0dd2003 100644 --- a/src/peer.rs +++ b/src/peer.rs @@ -686,20 +686,21 @@ mod tests { }; use chrono::Utc; use giganto_client::connection::client_handshake; - use lazy_static::lazy_static; use quinn::{Connection, Endpoint, RecvStream, SendStream}; use std::{ collections::{HashMap, HashSet}, fs::{self, File}, net::{IpAddr, Ipv6Addr, SocketAddr}, path::Path, - sync::Arc, + sync::{Arc, OnceLock}, }; use tempfile::TempDir; use tokio::sync::{Mutex, Notify, RwLock}; - lazy_static! { - pub(crate) static ref TOKEN: Mutex = Mutex::new(0); + fn get_token() -> &'static Mutex { + static TOKEN: OnceLock> = OnceLock::new(); + + TOKEN.get_or_init(|| Mutex::new(0)) } const CERT_PATH: &str = "tests/cert.pem"; @@ -799,7 +800,7 @@ mod tests { let client_crypto = rustls::ClientConfig::builder() .with_safe_defaults() .with_root_certificates(server_root) - .with_single_cert(cert_chain, pv_key) + .with_client_auth_cert(cert_chain, pv_key) .expect("the server root, cert chain or private key are not valid"); let mut endpoint = @@ -827,7 +828,7 @@ mod tests { #[tokio::test] async fn recv_peer_data() { - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; // peer server's peer list let peer_addr = SocketAddr::new("123.123.123.123".parse::().unwrap(), TEST_PORT); diff --git a/src/publish/tests.rs b/src/publish/tests.rs index 8266c27..9a4ef0c 100644 --- a/src/publish/tests.rs +++ b/src/publish/tests.rs @@ -22,7 +22,6 @@ use giganto_client::{ stream::{NodeType, RequestCrusherStream, RequestHogStream, RequestStreamRecord}, }, }; -use lazy_static::lazy_static; use quinn::{Connection, Endpoint, SendStream}; use serde::Serialize; use std::{ @@ -31,12 +30,14 @@ use std::{ fs, net::{IpAddr, Ipv6Addr, SocketAddr}, path::Path, - sync::Arc, + sync::{Arc, OnceLock}, }; use tokio::sync::{Mutex, Notify, RwLock}; -lazy_static! { - pub(crate) static ref TOKEN: Mutex = Mutex::new(0); +fn get_token() -> &'static Mutex { + static TOKEN: OnceLock> = OnceLock::new(); + + TOKEN.get_or_init(|| Mutex::new(0)) } const CERT_PATH: &str = "tests/cert.pem"; @@ -155,7 +156,7 @@ fn init_client() -> Endpoint { let client_crypto = rustls::ClientConfig::builder() .with_safe_defaults() .with_root_certificates(server_root) - .with_single_cert(cert_chain, pv_key) + .with_client_auth_cert(cert_chain, pv_key) .expect("the server root, cert chain or private key are not valid"); let mut endpoint = @@ -682,7 +683,7 @@ async fn request_range_data_with_protocol() { const SMB_KIND: &str = "smb"; const NFS_KIND: &str = "nfs"; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); let db = Database::open(db_dir.path(), &DbOptions::default()).unwrap(); let packet_sources = Arc::new(RwLock::new(HashMap::new())); @@ -1656,7 +1657,7 @@ async fn request_range_data_with_log() { count: usize, } - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); let db = Database::open(db_dir.path(), &DbOptions::default()).unwrap(); let packet_sources = Arc::new(RwLock::new(HashMap::new())); @@ -1738,7 +1739,7 @@ async fn request_range_data_with_period_time_series() { const SAMPLING_POLICY_ID: &str = "policy_one"; const KIND: &str = "timeseries"; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); let db = Database::open(db_dir.path(), &DbOptions::default()).unwrap(); let packet_sources = Arc::new(RwLock::new(HashMap::new())); @@ -1844,7 +1845,7 @@ async fn request_network_event_stream() { const SOURCE_CRUSHER_THREE: &str = "src3"; const POLICY_ID: u32 = 1; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); let db = Database::open(db_dir.path(), &DbOptions::default()).unwrap(); @@ -3506,7 +3507,7 @@ async fn request_raw_events() { const KIND: &str = "conn"; const TIMESTAMP: i64 = 100; - let _lock = TOKEN.lock().await; + let _lock = get_token().lock().await; let db_dir = tempfile::tempdir().unwrap(); let db = Database::open(db_dir.path(), &DbOptions::default()).unwrap(); let packet_sources = Arc::new(RwLock::new(HashMap::new())); diff --git a/src/server.rs b/src/server.rs index 26a4936..696003a 100644 --- a/src/server.rs +++ b/src/server.rs @@ -101,7 +101,7 @@ pub fn config_client( let tls_config = rustls::ClientConfig::builder() .with_safe_defaults() .with_root_certificates(root_store) - .with_single_cert(cert, key)?; + .with_client_auth_cert(cert, key)?; let mut transport = TransportConfig::default(); transport.keep_alive_interval(Some(KEEP_ALIVE_INTERVAL));