From a4c751b366e30969dc13e04bd4f97dda501f66b3 Mon Sep 17 00:00:00 2001 From: Sebastian YEPES Date: Thu, 12 Jan 2023 12:32:52 +0100 Subject: [PATCH] Fix auth token --- CHANGELOG.md | 4 ++ Cargo.lock | 106 ++++++++++++++-------------------------------- Cargo.toml | 2 +- src/sio/client.rs | 88 +++++++++++++++++++------------------- 4 files changed, 81 insertions(+), 119 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aad01dc..bdacdcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachang ## [Unreleased] +## 1.0.3 - 2023-01-12 + +- Fix the reutilization of the auth token + ## 1.0.2 - 2023-01-07 - Fix lint warning diff --git a/Cargo.lock b/Cargo.lock index 60e2330..43ce5c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -282,9 +282,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.85" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5add3fc1717409d029b20c5b6903fc0c0b02fa6741d820054f4a2efa5e5816fd" +checksum = "51d1075c37807dcf850c379432f0df05ba52cc30f279c5cfc43cc221ce7f8579" dependencies = [ "cc", "cxxbridge-flags", @@ -294,9 +294,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.85" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4c87959ba14bc6fbc61df77c3fcfe180fc32b93538c4f1031dd802ccb5f2ff0" +checksum = "5044281f61b27bc598f2f6647d480aed48d2bf52d6eb0b627d84c0361b17aa70" dependencies = [ "cc", "codespan-reporting", @@ -309,15 +309,15 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.85" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69a3e162fde4e594ed2b07d0f83c6c67b745e7f28ce58c6df5e6b6bef99dfb59" +checksum = "61b50bc93ba22c27b0d31128d2d130a0a6b3d267ae27ef7e4fae2167dfe8781c" [[package]] name = "cxxbridge-macro" -version = "1.0.85" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e7e2adeb6a0d4a282e581096b06e1791532b7d576dcde5ccd9382acf55db8e6" +checksum = "39e61fda7e62115119469c7b3591fd913ecca96fb766cfd3f2e2502ab7bc87a5" dependencies = [ "proc-macro2", "quote", @@ -757,14 +757,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c" dependencies = [ "libc", - "windows-sys 0.42.0", + "windows-sys", ] [[package]] name = "ipnet" -version = "2.7.0" +version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11b0d96e660696543b251e58030cf9787df56da39dab19ad60eae7353040917e" +checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146" [[package]] name = "is-terminal" @@ -775,7 +775,7 @@ dependencies = [ "hermit-abi", "io-lifetimes", "rustix", - "windows-sys 0.42.0", + "windows-sys", ] [[package]] @@ -879,7 +879,7 @@ dependencies = [ "libc", "log", "wasi 0.11.0+wasi-snapshot-preview1", - "windows-sys 0.42.0", + "windows-sys", ] [[package]] @@ -949,9 +949,9 @@ dependencies = [ [[package]] name = "object" -version = "0.30.1" +version = "0.30.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d864c91689fdc196779b98dba0aceac6118594c2df6ee5d943eb6a8df4d107a" +checksum = "2b8c786513eb403643f2a88c244c2aaa270ef2153f55094587d0c48a3cf22a83" dependencies = [ "memchr", ] @@ -1031,15 +1031,15 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.5" +version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ff9f3fef3968a3ec5945535ed654cb38ff72d7495a25619e2247fb15a2ed9ba" +checksum = "ba1ef8814b5c993410bb3adfad7a5ed269563e4a2f90c41f5d85be7fb47133bf" dependencies = [ "cfg-if", "libc", "redox_syscall 0.2.16", "smallvec", - "windows-sys 0.42.0", + "windows-sys", ] [[package]] @@ -1210,9 +1210,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.7.0" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" dependencies = [ "aho-corasick", "memchr", @@ -1300,7 +1300,7 @@ dependencies = [ "io-lifetimes", "libc", "linux-raw-sys", - "windows-sys 0.42.0", + "windows-sys", ] [[package]] @@ -1326,12 +1326,11 @@ checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" [[package]] name = "schannel" -version = "0.1.20" +version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2" +checksum = "713cfb06c7059f3588fb8044c0fad1d09e3c01d225e25b9220dbfdcf16dbb1b3" dependencies = [ - "lazy_static", - "windows-sys 0.36.1", + "windows-sys", ] [[package]] @@ -1451,7 +1450,7 @@ dependencies = [ [[package]] name = "sio2prom" -version = "1.0.2" +version = "1.0.3" dependencies = [ "anyhow", "chrono", @@ -1618,7 +1617,7 @@ dependencies = [ "socket2", "tokio-macros", "tracing", - "windows-sys 0.42.0", + "windows-sys", ] [[package]] @@ -2015,19 +2014,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows-sys" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" -dependencies = [ - "windows_aarch64_msvc 0.36.1", - "windows_i686_gnu 0.36.1", - "windows_i686_msvc 0.36.1", - "windows_x86_64_gnu 0.36.1", - "windows_x86_64_msvc 0.36.1", -] - [[package]] name = "windows-sys" version = "0.42.0" @@ -2035,12 +2021,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" dependencies = [ "windows_aarch64_gnullvm", - "windows_aarch64_msvc 0.42.0", - "windows_i686_gnu 0.42.0", - "windows_i686_msvc 0.42.0", - "windows_x86_64_gnu 0.42.0", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", "windows_x86_64_gnullvm", - "windows_x86_64_msvc 0.42.0", + "windows_x86_64_msvc", ] [[package]] @@ -2049,48 +2035,24 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" -[[package]] -name = "windows_aarch64_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" - [[package]] name = "windows_aarch64_msvc" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" -[[package]] -name = "windows_i686_gnu" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" - [[package]] name = "windows_i686_gnu" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" -[[package]] -name = "windows_i686_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" - [[package]] name = "windows_i686_msvc" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" -[[package]] -name = "windows_x86_64_gnu" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" - [[package]] name = "windows_x86_64_gnu" version = "0.42.0" @@ -2103,12 +2065,6 @@ version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" -[[package]] -name = "windows_x86_64_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" - [[package]] name = "windows_x86_64_msvc" version = "0.42.0" diff --git a/Cargo.toml b/Cargo.toml index 846f5d2..cf8ccca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sio2prom" -version = "1.0.2" +version = "1.0.3" edition = "2021" authors = ["Sebastian YEPES F "] keywords = ["prometheus", "metrics", "scaleio", "VxFlex", "PowerFlex"] diff --git a/src/sio/client.rs b/src/sio/client.rs index a1a7836..955be33 100644 --- a/src/sio/client.rs +++ b/src/sio/client.rs @@ -23,50 +23,52 @@ impl<'a> ClientInfo<'a> { async fn auth(&mut self) { trace!("auth"); - if let Ok(c) = reqwest::Client::builder().user_agent(env!("CARGO_PKG_NAME")).danger_accept_invalid_certs(true).timeout(Duration::from_secs(10)).connection_verbose(true).build() { - if !self.auth_usr.unwrap().is_empty() && !self.auth_pwd.unwrap().is_empty() { - let req_url = format!("https://{ip}/api/login", ip = self.ip.unwrap()); - trace!("Auth on {:?} with {:?}/{:?}", req_url.clone(), self.auth_usr.unwrap().to_string(), self.auth_pwd.unwrap().to_string()); - - let req = c.get(req_url).basic_auth(self.auth_usr.unwrap(), Some(self.auth_pwd.unwrap())); - match req.send().await { - Ok(r) => { - trace!("resp:{:#?}", r); - match r.status() { - StatusCode::OK => { - match r.json::().await { - Ok(t) => { - *self.token.borrow_mut() = Some(t.to_string().replace('"', "")); - }, - _ => *self.token.borrow_mut() = None, - } - }, - StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => { - *self.token.borrow_mut() = None; - let msg: String = match r.json::().await { - Ok(Value::Object(m)) => m.get("message").map(|m| m.to_string().replace('"', "")).unwrap_or_else(|| "unknown".to_string()), - _ => "unknown".to_string(), - }; - error!("Auth failed: {:?}", msg); - }, - _ => { - *self.token.borrow_mut() = None; - let msg: String = match r.json::().await { - Ok(Value::Object(m)) => m.get("message").map(|m| m.to_string().replace('"', "")).unwrap_or_else(|| "unknown".to_string()), - _ => "unknown".to_string(), - }; - error!("Unknown auth request error: {:?}", msg); - }, - }; - }, - Err(e) => { - *self.token.borrow_mut() = None; - error!("Auth request error: {:?}", e.to_string()); - }, + if self.token.borrow().is_none() { + if let Ok(c) = reqwest::Client::builder().user_agent(env!("CARGO_PKG_NAME")).danger_accept_invalid_certs(true).timeout(Duration::from_secs(10)).connection_verbose(true).build() { + if !self.auth_usr.unwrap().is_empty() && !self.auth_pwd.unwrap().is_empty() && self.token.borrow().is_none() { + let req_url = format!("https://{ip}/api/login", ip = self.ip.unwrap()); + trace!("Auth on {:?} with {:?}/{:?}", req_url.clone(), self.auth_usr.unwrap().to_string(), self.auth_pwd.unwrap().to_string()); + + let req = c.get(req_url).basic_auth(self.auth_usr.unwrap(), Some(self.auth_pwd.unwrap())); + match req.send().await { + Ok(r) => { + trace!("resp:{:#?}", r); + match r.status() { + StatusCode::OK => { + match r.json::().await { + Ok(t) => { + *self.token.borrow_mut() = Some(t.to_string().replace('"', "")); + }, + _ => *self.token.borrow_mut() = None, + } + }, + StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => { + *self.token.borrow_mut() = None; + let msg: String = match r.json::().await { + Ok(Value::Object(m)) => m.get("message").map(|m| m.to_string().replace('"', "")).unwrap_or_else(|| "unknown".to_string()), + _ => "unknown".to_string(), + }; + error!("Auth failed: {:?}", msg); + }, + _ => { + *self.token.borrow_mut() = None; + let msg: String = match r.json::().await { + Ok(Value::Object(m)) => m.get("message").map(|m| m.to_string().replace('"', "")).unwrap_or_else(|| "unknown".to_string()), + _ => "unknown".to_string(), + }; + error!("Unknown auth request error: {:?}", msg); + }, + }; + }, + Err(e) => { + *self.token.borrow_mut() = None; + error!("Auth request error: {:?}", e.to_string()); + }, + } + } else { + *self.token.borrow_mut() = None; + error!("Auth missing credentials"); } - } else { - *self.token.borrow_mut() = None; - error!("Auth missing credentials"); } }