From 5c54dfee3adabe4e070dd02020ac3d96972bdec0 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Wed, 3 Jun 2020 17:07:32 +0200 Subject: [PATCH 1/3] Fixed an issue when DNS resolving fails. In the event of a failed DNS Resolving checking for new versions will cause a huge delay, and in the end a timeout when loading the page. - Check if DNS resolving failed, if that is the case, do not check for new versions - Changed `fn get_github_api` to make use of structs - Added a timeout of 10 seconds for the version check requests - Moved the "Unknown" lables to the "Latest" lable --- src/api/admin.rs | 89 ++++++++++++---------- src/static/templates/admin/diagnostics.hbs | 10 ++- 2 files changed, 56 insertions(+), 43 deletions(-) diff --git a/src/api/admin.rs b/src/api/admin.rs index bd9bf3e..46338a9 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -1,5 +1,6 @@ use once_cell::sync::Lazy; use serde_json::Value; +use serde::de::DeserializeOwned; use std::process::Command; use rocket::http::{Cookie, Cookies, SameSite}; @@ -315,28 +316,34 @@ fn organizations_overview(_token: AdminToken, conn: DbConn) -> ApiResult Result { +#[derive(Deserialize)] +struct GitRelease { + tag_name: String, +} + +#[derive(Deserialize)] +struct GitCommit { + sha: String, +} + +fn get_github_api(url: &str) -> Result { use reqwest::{header::USER_AGENT, blocking::Client}; + use std::time::Duration; let github_api = Client::builder().build()?; - let res = github_api - .get(url) + Ok( + github_api.get(url) + .timeout(Duration::from_secs(10)) .header(USER_AGENT, "Bitwarden_RS") - .send()?; - - let res_status = res.status(); - if res_status != 200 { - error!("Could not retrieve '{}', response code: {}", url, res_status); - } - - let value: Value = res.error_for_status()?.json()?; - Ok(value) + .send()? + .error_for_status()? + .json::()? + ) } #[get("/diagnostics")] @@ -350,32 +357,36 @@ fn diagnostics(_token: AdminToken, _conn: DbConn) -> ApiResult> { let web_vault_version: WebVaultVersion = serde_json::from_str(&vault_version_str)?; let github_ips = ("github.com", 0).to_socket_addrs().map(|mut i| i.next()); - let dns_resolved = match github_ips { - Ok(Some(a)) => a.ip().to_string(), - _ => "Could not resolve domain name.".to_string(), + let (dns_resolved, dns_ok) = match github_ips { + Ok(Some(a)) => (a.ip().to_string(), true), + _ => ("Could not resolve domain name.".to_string(), false), }; - let bitwarden_rs_releases = get_github_api("https://api.github.com/repos/dani-garcia/bitwarden_rs/releases/latest"); - let latest_release = match &bitwarden_rs_releases { - Ok(j) => j["tag_name"].as_str().unwrap(), - _ => "-", + // If the DNS Check failed, do not even attempt to check for new versions since we were not able to resolve github.com + let (latest_release, latest_commit, latest_web_build) = if dns_ok { + ( + match get_github_api::("https://api.github.com/repos/dani-garcia/bitwarden_rs/releases/latest") { + Ok(r) => r.tag_name, + _ => "-".to_string() + }, + match get_github_api::("https://api.github.com/repos/dani-garcia/bitwarden_rs/commits/master") { + Ok(mut c) => { + c.sha.truncate(8); + c.sha + }, + _ => "-".to_string() + }, + match get_github_api::("https://api.github.com/repos/dani-garcia/bw_web_builds/releases/latest") { + Ok(r) => r.tag_name.trim_start_matches('v').to_string(), + _ => "-".to_string() + }, + ) + } else { + ("-".to_string(), "-".to_string(), "-".to_string()) }; - - let bitwarden_rs_commits = get_github_api("https://api.github.com/repos/dani-garcia/bitwarden_rs/commits/master"); - let mut latest_commit = match &bitwarden_rs_commits { - Ok(j) => j["sha"].as_str().unwrap(), - _ => "-", - }; - if latest_commit.len() >= 8 { - latest_commit = &latest_commit[..8]; - } - - let bw_web_builds_releases = get_github_api("https://api.github.com/repos/dani-garcia/bw_web_builds/releases/latest"); - let latest_web_build = match &bw_web_builds_releases { - Ok(j) => j["tag_name"].as_str().unwrap(), - _ => "-", - }; - + + // Run the date check as the last item right before filling the json. + // This should ensure that the time difference between the browser and the server is as minimal as possible. let dt = Utc::now(); let server_time = dt.format("%Y-%m-%d %H:%M:%S").to_string(); @@ -385,7 +396,7 @@ fn diagnostics(_token: AdminToken, _conn: DbConn) -> ApiResult> { "web_vault_version": web_vault_version.version, "latest_release": latest_release, "latest_commit": latest_commit, - "latest_web_build": latest_web_build.replace("v", ""), + "latest_web_build": latest_web_build, }); let text = AdminTemplateData::diagnostics(diagnostics_json).render()?; diff --git a/src/static/templates/admin/diagnostics.hbs b/src/static/templates/admin/diagnostics.hbs index b5eca6c..21aa1f6 100644 --- a/src/static/templates/admin/diagnostics.hbs +++ b/src/static/templates/admin/diagnostics.hbs @@ -9,24 +9,26 @@
Server Installed Ok Update - Unknown
{{version}}
-
Server Latest
+
Server Latest + Unknown +
{{diagnostics.latest_release}}-{{diagnostics.latest_commit}}
Web Installed Ok Update - Unknown
{{diagnostics.web_vault_version}}
-
Web Latest
+
Web Latest + Unknown +
{{diagnostics.latest_web_build}}
From 2fffaec226e2dcfb9b013aa985049e368b88f368 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Wed, 3 Jun 2020 17:57:03 +0200 Subject: [PATCH 2/3] Added attachment info per user and some layout fix - Added the amount and size of the attachments per user - Changed the items count function a bit - Some small layout changes --- src/api/admin.rs | 8 +++++--- src/db/models/attachment.rs | 10 ++++++++++ src/db/models/cipher.rs | 3 ++- src/static/templates/admin/users.hbs | 9 ++++++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/api/admin.rs b/src/api/admin.rs index 46338a9..4921c30 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -253,13 +253,15 @@ fn get_users_json(_token: AdminToken, conn: DbConn) -> JsonResult { #[get("/users/overview")] fn users_overview(_token: AdminToken, conn: DbConn) -> ApiResult> { + use crate::util::get_display_size; + let users = User::get_all(&conn); let users_json: Vec = users.iter() .map(|u| { let mut usr = u.to_json(&conn); - if let Some(ciphers) = Cipher::count_owned_by_user(&u.uuid, &conn) { - usr["cipher_count"] = json!(ciphers); - }; + usr["cipher_count"] = json!(Cipher::count_owned_by_user(&u.uuid, &conn)); + usr["attachment_count"] = json!(Attachment::count_by_user(&u.uuid, &conn)); + usr["attachment_size"] = json!(get_display_size(Attachment::size_by_user(&u.uuid, &conn) as i32)); usr }).collect(); diff --git a/src/db/models/attachment.rs b/src/db/models/attachment.rs index f0efa93..58f893b 100644 --- a/src/db/models/attachment.rs +++ b/src/db/models/attachment.rs @@ -130,6 +130,16 @@ impl Attachment { result.unwrap_or(0) } + pub fn count_by_user(user_uuid: &str, conn: &DbConn) -> i64 { + attachments::table + .left_join(ciphers::table.on(ciphers::uuid.eq(attachments::cipher_uuid))) + .filter(ciphers::user_uuid.eq(user_uuid)) + .count() + .first::(&**conn) + .ok() + .unwrap_or(0) + } + pub fn size_by_org(org_uuid: &str, conn: &DbConn) -> i64 { let result: Option = attachments::table .left_join(ciphers::table.on(ciphers::uuid.eq(attachments::cipher_uuid))) diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index 94d7d1e..2fb09c0 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -355,12 +355,13 @@ impl Cipher { .load::(&**conn).expect("Error loading ciphers") } - pub fn count_owned_by_user(user_uuid: &str, conn: &DbConn) -> Option { + pub fn count_owned_by_user(user_uuid: &str, conn: &DbConn) -> i64 { ciphers::table .filter(ciphers::user_uuid.eq(user_uuid)) .count() .first::(&**conn) .ok() + .unwrap_or(0) } pub fn find_by_org(org_uuid: &str, conn: &DbConn) -> Vec { diff --git a/src/static/templates/admin/users.hbs b/src/static/templates/admin/users.hbs index 758031f..297a04f 100644 --- a/src/static/templates/admin/users.hbs +++ b/src/static/templates/admin/users.hbs @@ -10,7 +10,8 @@ User - Items + Items + Attachments Organizations Actions @@ -37,6 +38,12 @@ {{cipher_count}} + + Amount: {{attachment_count}} + {{#if attachment_count}} + Size: {{attachment_size}} + {{/if}} + {{#each Organizations}} {{Name}} From ac2723f898c45120ec023bbb2e0e66b26ad71a01 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Wed, 3 Jun 2020 20:37:31 +0200 Subject: [PATCH 3/3] Updated Organizations overview - Changed HTML to match users overview - Added User count - Added Org cipher amount - Added Attachment count and size --- src/api/admin.rs | 12 +++-- src/db/models/attachment.rs | 10 ++++ src/db/models/cipher.rs | 9 ++++ src/db/models/organization.rs | 9 ++++ src/static/templates/admin/organizations.hbs | 51 ++++++++++++++------ src/static/templates/admin/users.hbs | 2 - 6 files changed, 73 insertions(+), 20 deletions(-) diff --git a/src/api/admin.rs b/src/api/admin.rs index 4921c30..a897be1 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -15,6 +15,7 @@ use crate::config::ConfigBuilder; use crate::db::{backup_database, models::*, DbConn}; use crate::error::Error; use crate::mail; +use crate::util::get_display_size; use crate::CONFIG; pub fn routes() -> Vec { @@ -253,8 +254,6 @@ fn get_users_json(_token: AdminToken, conn: DbConn) -> JsonResult { #[get("/users/overview")] fn users_overview(_token: AdminToken, conn: DbConn) -> ApiResult> { - use crate::util::get_display_size; - let users = User::get_all(&conn); let users_json: Vec = users.iter() .map(|u| { @@ -312,7 +311,14 @@ fn update_revision_users(_token: AdminToken, conn: DbConn) -> EmptyResult { #[get("/organizations/overview")] fn organizations_overview(_token: AdminToken, conn: DbConn) -> ApiResult> { let organizations = Organization::get_all(&conn); - let organizations_json: Vec = organizations.iter().map(|o| o.to_json()).collect(); + let organizations_json: Vec = organizations.iter().map(|o| { + let mut org = o.to_json(); + org["user_count"] = json!(UserOrganization::count_by_org(&o.uuid, &conn)); + org["cipher_count"] = json!(Cipher::count_by_org(&o.uuid, &conn)); + org["attachment_count"] = json!(Attachment::count_by_org(&o.uuid, &conn)); + org["attachment_size"] = json!(get_display_size(Attachment::size_by_org(&o.uuid, &conn) as i32)); + org + }).collect(); let text = AdminTemplateData::organizations(organizations_json).render()?; Ok(Html(text)) diff --git a/src/db/models/attachment.rs b/src/db/models/attachment.rs index 58f893b..b0e5003 100644 --- a/src/db/models/attachment.rs +++ b/src/db/models/attachment.rs @@ -150,4 +150,14 @@ impl Attachment { result.unwrap_or(0) } + + pub fn count_by_org(org_uuid: &str, conn: &DbConn) -> i64 { + attachments::table + .left_join(ciphers::table.on(ciphers::uuid.eq(attachments::cipher_uuid))) + .filter(ciphers::organization_uuid.eq(org_uuid)) + .count() + .first(&**conn) + .ok() + .unwrap_or(0) + } } diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index 2fb09c0..d0eb4e8 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -370,6 +370,15 @@ impl Cipher { .load::(&**conn).expect("Error loading ciphers") } + pub fn count_by_org(org_uuid: &str, conn: &DbConn) -> i64 { + ciphers::table + .filter(ciphers::organization_uuid.eq(org_uuid)) + .count() + .first::(&**conn) + .ok() + .unwrap_or(0) + } + pub fn find_by_folder(folder_uuid: &str, conn: &DbConn) -> Vec { folders_ciphers::table.inner_join(ciphers::table) .filter(folders_ciphers::folder_uuid.eq(folder_uuid)) diff --git a/src/db/models/organization.rs b/src/db/models/organization.rs index 8ce476c..fac5cad 100644 --- a/src/db/models/organization.rs +++ b/src/db/models/organization.rs @@ -437,6 +437,15 @@ impl UserOrganization { .expect("Error loading user organizations") } + pub fn count_by_org(org_uuid: &str, conn: &DbConn) -> i64 { + users_organizations::table + .filter(users_organizations::org_uuid.eq(org_uuid)) + .count() + .first::(&**conn) + .ok() + .unwrap_or(0) + } + pub fn find_by_org_and_type(org_uuid: &str, atype: i32, conn: &DbConn) -> Vec { users_organizations::table .filter(users_organizations::org_uuid.eq(org_uuid)) diff --git a/src/static/templates/admin/organizations.hbs b/src/static/templates/admin/organizations.hbs index 6dbcbbb..0646b4e 100644 --- a/src/static/templates/admin/organizations.hbs +++ b/src/static/templates/admin/organizations.hbs @@ -2,24 +2,45 @@
Organizations
-
- {{#each organizations}} -
- -
-
-
+
+ + + + + + + + + + + {{#each organizations}} + + + + + + + + {{/each}} + +
OrganizationUsersItemsAttachments
{{Name}} - {{#if Id}} - {{Id}} + ({{BillingEmail}}) + + {{Id}} + + + {{user_count}} + + {{cipher_count}} + + Amount: {{attachment_count}} + {{#if attachment_count}} + Size: {{attachment_size}} {{/if}} - {{BillingEmail}} - - - - - {{/each}} +
+
diff --git a/src/static/templates/admin/users.hbs b/src/static/templates/admin/users.hbs index 297a04f..5b99c85 100644 --- a/src/static/templates/admin/users.hbs +++ b/src/static/templates/admin/users.hbs @@ -2,8 +2,6 @@
Registered Users
- -