diff --git a/Cargo.toml b/Cargo.toml index 4708e88..6112f0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,8 @@ edition = "2021" sys_metrics = { git = "https://github.com/Martichou/sys_metrics" } askama = "0.12" actix-web = { version = "4.3", features = ["rustls"] } -actix-session = { version = "0.7", features = ["cookie-session"] } -axum = { version = "0.6" } +actix-session = { version = "0.10", features = ["cookie-session"] } +axum = { version = "0.7" } chrono = { version = "0.4", features = ["serde"] } diesel = { version = "2.0", features = ["postgres", "r2d2", "chrono", "uuid"] } futures-util = "0.3" @@ -18,14 +18,14 @@ log = "0.4" once_cell = "1.14" r2d2 = "0.8" regex = "1.6" -rustls = "0.20" -rustls-pemfile = "1.0" +rustls = "0.23" +rustls-pemfile = "2.0" serde_json = "1.0" -simd-json = "0.9" +simd-json = "0.14" serde = { version = "1.0", features = ["derive"] } snmalloc-rs = "0.3" thiserror = "1.0" -ts-rs = { version = "7.1", features = ["serde-compat", "uuid-impl", "chrono-impl"] } +ts-rs = { version = "10.0", features = ["serde-compat", "uuid-impl", "chrono-impl"] } uuid = { version = "1.1", features = ["serde", "v4"] } walkdir = "2.3" diff --git a/bindings/index.ts b/bindings/index.ts index e7b00d3..1f80f98 100644 --- a/bindings/index.ts +++ b/bindings/index.ts @@ -1,18 +1,18 @@ -export * from "./AlertsDTOUpdate" -export * from "./IoBlock" -export * from "./IncidentsJoined" +export * from "./Incidents" export * from "./ApiKey" +export * from "./HttpIncidentsCount" +export * from "./Customers" export * from "./HttpAlertsCount" -export * from "./Alerts" -export * from "./Swap" -export * from "./AlertsDTO" export * from "./CpuTimes" -export * from "./Incidents" -export * from "./Host" +export * from "./IncidentsJoined" export * from "./IoNet" -export * from "./Memory" -export * from "./HttpIncidentsCount" -export * from "./LoadAvg" +export * from "./IoBlock" export * from "./CpuStats" +export * from "./LoadAvg" +export * from "./AlertsDTOUpdate" +export * from "./Host" +export * from "./Swap" +export * from "./Memory" +export * from "./AlertsDTO" export * from "./Disk" -export * from "./Customers" \ No newline at end of file +export * from "./Alerts" \ No newline at end of file diff --git a/.cargo/config b/config.toml similarity index 100% rename from .cargo/config rename to config.toml diff --git a/src/lib.rs b/src/lib.rs index 5ac5496..508d025 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,10 @@ use actix_session::config::{CookieContentSecurity, PersistentSession}; use actix_session::storage::CookieSessionStore; use actix_session::SessionMiddleware; use diesel::{prelude::PgConnection, r2d2::ConnectionManager}; -use rustls::{Certificate, PrivateKey, ServerConfig}; +use rustls::{ + pki_types::{CertificateDer, PrivateKeyDer}, + ServerConfig, +}; use crate::apierrors::ApiError; @@ -81,29 +84,37 @@ pub fn prog() -> Option { /// /// Use key and cert for the path to find the files. pub fn get_ssl_builder(key: &str, cert: &str) -> Result { - let key_file = &mut BufReader::new(File::open(key)?); - // Extract all PKCS8-encoded private key from key_file and generate a Vec from them - let mut keys = rustls_pemfile::pkcs8_private_keys(key_file)?; - // If no keys are found, we try using the rsa type - if keys.is_empty() { - // Reopen a new BufReader as pkcs8_private_keys took over the previous one - let key_file = &mut BufReader::new(File::open(key)?); - keys = rustls_pemfile::rsa_private_keys(key_file)?; - } - // Convert the first key to be a PrivateKey - let key: PrivateKey = PrivateKey(keys.remove(0)); + let mut reader = BufReader::new(File::open(key)?); + + // Try to read PKCS8 private keys first + let keys = rustls_pemfile::pkcs8_private_keys(&mut reader) + .map(|v| v.unwrap().clone_key()) + .collect::>(); + + let key = if !keys.is_empty() { + PrivateKeyDer::Pkcs8(keys[0].clone_key()) + } else { + // If no PKCS8 keys are found, try to read RSA private keys + let mut reader = BufReader::new(File::open(key)?); + let keys = rustls_pemfile::rsa_private_keys(&mut reader) + .map(|v| v.unwrap().clone_key()) + .collect::>(); + + if !keys.is_empty() { + PrivateKeyDer::Pkcs1(keys[0].clone_key()) + } else { + return Err(ApiError::ExplicitError("No private keys found".to_owned())); + } + }; let cert_file = &mut BufReader::new(File::open(cert)?); // Create a Vec of certificate by extracting all cert from cert_file let cert_chain = rustls_pemfile::certs(cert_file) - .unwrap() - .iter() - .map(|v| Certificate(v.clone())) + .map(|v| CertificateDer::from_slice(&v.unwrap().clone()).into_owned()) .collect(); // Return the ServerConfig to be used Ok(ServerConfig::builder() - .with_safe_defaults() .with_no_client_auth() .with_single_cert(cert_chain, key)?) } diff --git a/src/models/balerts/mod.rs b/src/models/balerts/mod.rs index 34ea1db..5d468f4 100644 --- a/src/models/balerts/mod.rs +++ b/src/models/balerts/mod.rs @@ -1,3 +1,6 @@ +use once_cell::sync::Lazy; +use regex::Regex; + mod alerts; mod alerts_impl; mod alerts_querying; @@ -8,9 +11,6 @@ pub use alerts_querying::*; mod incidents; mod incidents_impl; pub use incidents::*; -pub use incidents_impl::*; -use once_cell::sync::Lazy; -use regex::Regex; pub mod qtype; diff --git a/src/models/bauth/mod.rs b/src/models/bauth/mod.rs index da68143..d3524e2 100644 --- a/src/models/bauth/mod.rs +++ b/src/models/bauth/mod.rs @@ -1,9 +1,7 @@ mod apikeys; mod apikeys_impl; pub use apikeys::*; -pub use apikeys_impl::*; mod customers; mod customers_impl; pub use customers::*; -pub use customers_impl::*; diff --git a/src/models/bserver/mod.rs b/src/models/bserver/mod.rs index 5c48c7e..851233a 100644 --- a/src/models/bserver/mod.rs +++ b/src/models/bserver/mod.rs @@ -1,42 +1,34 @@ mod cpustats; mod cpustats_impl; pub use cpustats::*; -pub use cpustats_impl::*; mod cputimes; mod cputimes_impl; pub use cputimes::*; -pub use cputimes_impl::*; mod disk; mod disk_impl; pub use disk::*; -pub use disk_impl::*; mod hosts; mod hosts_impl; pub use hosts::*; -pub use hosts_impl::*; mod ionet; mod ionet_impl; pub use ionet::*; -pub use ionet_impl::*; mod ioblock; mod ioblock_impl; pub use ioblock::*; -pub use ioblock_impl::*; mod loadavg; mod loadavg_impl; pub use loadavg::*; -pub use loadavg_impl::*; mod memory; mod memory_impl; pub use memory::*; -pub use memory_impl::*; mod swap; mod swap_impl; diff --git a/src/models/mod.rs b/src/models/mod.rs index ae678a9..21bbcde 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -45,10 +45,7 @@ pub struct InnerUser { /// which means for size = 21600 that we'll get the avg of each 60s intervals #[inline] pub fn get_granularity(size: i64) -> u32 { - std::cmp::min( - 86400, - std::cmp::max(1, ((0.003 * size as f32) * (0.93) + 0.298206) as u32), - ) + (((0.003 * size as f32) * (0.93) + 0.298206) as u32).clamp(1, 86400) } #[inline]