From 28b24f39b0ed2f2a299967453c879ae1cd593649 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 11 Jul 2021 13:49:59 +0200 Subject: [PATCH] Return format string with admin logs --- src/api_data/admin/admin_log_api.rs | 10 +++-- .../admin/admin_keys_controller.rs | 16 ++++++-- src/data/admin_action_log.rs | 39 +++++++++++++++++-- src/helpers/admin_account_key_helper.rs | 4 +- src/helpers/admin_log_helper.rs | 2 +- 5 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/api_data/admin/admin_log_api.rs b/src/api_data/admin/admin_log_api.rs index 776fb99..3e46106 100644 --- a/src/api_data/admin/admin_log_api.rs +++ b/src/api_data/admin/admin_log_api.rs @@ -2,7 +2,7 @@ //! //! @author Pierre Hubert -use crate::data::admin_action_log::{AdminActionLog, AdminAction}; +use crate::data::admin_action_log::{AdminAction, AdminActionLog}; #[derive(serde::Serialize)] pub struct AdminLogAPI { @@ -10,17 +10,19 @@ pub struct AdminLogAPI { admin_id: u64, ip: String, time: u64, - action: AdminAction + action: AdminAction, + format: &'static str, } impl AdminLogAPI { - pub fn new(log: &AdminActionLog) -> Self{ + pub fn new(log: &AdminActionLog) -> Self { Self { id: log.id, admin_id: log.admin_id.id(), ip: log.ip.to_string(), time: log.time, - action: log.action.clone() + action: log.action.clone(), + format: log.action.format_string(), } } } \ No newline at end of file diff --git a/src/controllers/admin/admin_keys_controller.rs b/src/controllers/admin/admin_keys_controller.rs index 838a5a6..7dbce0a 100644 --- a/src/controllers/admin/admin_keys_controller.rs +++ b/src/controllers/admin/admin_keys_controller.rs @@ -62,7 +62,7 @@ pub fn challenge_register_key(r: &mut HttpRequestHandler) -> RequestResult { /// Register key pub fn register_key(r: &mut HttpRequestHandler) -> RequestResult { - let name = r.post_string("name")?; + let key_name = r.post_string("name")?; let creds = r.post_register_public_key_credential("key")?; let state = r.some_or_internal_error( @@ -73,10 +73,14 @@ pub fn register_key(r: &mut HttpRequestHandler) -> RequestResult { let wan = get_wan(); let key = wan.register_credential(creds, state, |_| Ok(false))?; - admin_account_key_helper::add_key(r.admin_id()?, &name, key)?; + let key_id = admin_account_key_helper::add_key(r.admin_id()?, &key_name, key)?; log_admin_action(r.admin_id()?, &r.remote_ip(), - AdminAction::RegisteredAdminKey { name, target: r.admin_id()? })?; + AdminAction::RegisteredAdminKey { + key_id, + key_name, + target: r.admin_id()?, + })?; r.ok() } @@ -93,7 +97,11 @@ pub fn delete_auth_key(r: &mut HttpRequestHandler) -> RequestResult { for key in admin_account_key_helper::get_admin_keys(admin_id)? { if key.id == key_id { log_admin_action(r.admin_id()?, &r.remote_ip(), - AdminAction::DeletedAdminKey { name: key.name.to_string(), target: admin_id })?; + AdminAction::DeletedAdminKey { + key_id, + key_name: key.name.to_string(), + target: admin_id, + })?; admin_account_key_helper::delete_key(key)?; diff --git a/src/data/admin_action_log.rs b/src/data/admin_action_log.rs index aec804c..9bde05b 100644 --- a/src/data/admin_action_log.rs +++ b/src/data/admin_action_log.rs @@ -6,16 +6,49 @@ use crate::data::admin::AdminID; pub enum AdminAction { AuthWithResetToken, AuthWithAccessKey { key: String, key_id: u64 }, - RegisteredAdminKey { name: String, target: AdminID }, - DeletedAdminKey { name: String, target: AdminID }, + RegisteredAdminKey { key_id: u64, key_name: String, target: AdminID }, + DeletedAdminKey { key_id: u64, key_name: String, target: AdminID }, GeneratedAdminResetToken { target: AdminID }, CreatedAdmin { id: AdminID, name: String, email: String }, UpdatedAdminGeneralSettings { target: AdminID, new_email: String, new_name: String }, AddAdminRole { target: AdminID, role: String }, RemoveAdminRole { target: AdminID, role: String }, - UnsupportedAction, + UnsupportedAction { raw_data: String }, } +impl AdminAction { + pub fn format_string(&self) -> &'static str { + match self { + AdminAction::AuthWithResetToken => { "Authenticated with a reset token" } + + AdminAction::AuthWithAccessKey { .. } => + { "Authenticated with key #{key_id} : \"{key}\"" } + + AdminAction::RegisteredAdminKey { .. } => + { "Registered a new key #{key_id} with name \'{key_name}\' for [admin]{target}[/admin]" } + + AdminAction::DeletedAdminKey { .. } => + { "Deleted key #{key_id} \'{key_name\' of [admin]{target}[/admin]" } + + AdminAction::GeneratedAdminResetToken { .. } => + { "Generated new admin access reset token for [admin]{target}[/admin]" } + + AdminAction::CreatedAdmin { .. } => + { "Created a new administrator [admin]#{id}[/admin] with name \'{name}\' and email \'email\'" } + + AdminAction::UpdatedAdminGeneralSettings { .. } => + { "Updated general admin settings of [admin]{target}[/admin]. New name: \'{new_name}\' / New email: \'{new_email}\'" } + + AdminAction::AddAdminRole { .. } => + { "Add a new role to [admin]{target}[/admin] => {role}" } + + AdminAction::RemoveAdminRole { .. } => + { "Removed a role from [admin]{target}[/admin] => {role}" } + + AdminAction::UnsupportedAction { .. } => { "Unsupported action. Raw data: {raw_data}" } + } + } +} pub struct AdminActionLog { pub id: u64, diff --git a/src/helpers/admin_account_key_helper.rs b/src/helpers/admin_account_key_helper.rs index 3f90e5f..a2d0224 100644 --- a/src/helpers/admin_account_key_helper.rs +++ b/src/helpers/admin_account_key_helper.rs @@ -11,13 +11,13 @@ use crate::helpers::database; use crate::utils::date_utils::time; /// Save a new key in the database -pub fn add_key(id: AdminID, name: &str, key: Credential) -> Res { +pub fn add_key(id: AdminID, name: &str, key: Credential) -> Res { database::InsertQuery::new(ADMIN_KEYS_TABLE) .add_admin_id("admin_id", id) .add_str("name", name) .add_u64("time_add", time()) .add_str("credential", &serde_json::to_string(&key)?) - .insert_drop_result() + .insert_expect_result() } /// Get the list of keys of a given admin diff --git a/src/helpers/admin_log_helper.rs b/src/helpers/admin_log_helper.rs index a1a600a..fa56bff 100644 --- a/src/helpers/admin_log_helper.rs +++ b/src/helpers/admin_log_helper.rs @@ -48,6 +48,6 @@ fn db_to_log(row: &database::RowResult) -> Res { ip: row.get_str("ip")?, time: row.get_u64("time")?, action: serde_json::from_str(&row.get_str("action")?) - .unwrap_or(AdminAction::UnsupportedAction), + .unwrap_or(AdminAction::UnsupportedAction { raw_data: row.get_str("action").unwrap() }), }) } \ No newline at end of file