Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ddboline committed Nov 24, 2024
1 parent 0f71736 commit ac210f5
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 99 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "security_log_analysis_rust"
version = "0.11.10"
version = "0.12.0"
authors = ["Daniel Boline <[email protected]>"]
edition = "2018"

Expand All @@ -14,12 +14,12 @@ Analyze Auth Logs."""

[dependencies]
anyhow = "1.0"
authorized_users = { git = "https://github.com/ddboline/auth_server_rust.git", tag="0.11.15"}
authorized_users = { git = "https://github.com/ddboline/auth_server_rust.git", tag="0.12.0"}
aws-config = {version="1.0", features=["behavior-version-latest"]}
aws-sdk-s3 = "1.1"
aws-sdk-ses = "1.1"
bytes = "1.0"
cached = {version="0.53", features=["async", "async_tokio_rt_multi_thread"]}
cached = {version="0.54", features=["async", "async_tokio_rt_multi_thread"]}
chrono = "0.4"
clap = {version="4.0", features=["derive"]}
deadpool = {version = "0.12", features=["serde", "rt_tokio_1"]}
Expand All @@ -40,7 +40,7 @@ itertools = "0.13"
log = "0.4"
maplit = "1.0"
parking_lot = "0.12"
polars = {version="0.43", features=["temporal", "parquet", "lazy"]}
polars = {version="0.44", features=["temporal", "parquet", "lazy"]}
postgres_query = {git = "https://github.com/ddboline/rust-postgres-query", tag = "0.3.8", features=["deadpool"]}
postgres-types = {version="0.2", features=["with-time-0_3", "with-uuid-1", "with-serde_json-1"]}
rand = "0.8"
Expand All @@ -51,13 +51,13 @@ serde = { version="1.0", features=["derive"]}
serde_json = "1.0"
serde_yml = "0.0.12"
smallvec = "1.6"
stack-string = { git = "https://github.com/ddboline/stack-string-rs.git", features=["postgres_types", "rweb-openapi"], tag="0.9.3" }
stack-string = { git = "https://github.com/ddboline/stack-string-rs.git", features=["postgres_types", "rweb-openapi"], tag="1.0.2" }
stdout-channel = "0.6"
thiserror = "1.0"
thiserror = "2.0"
time = {version="0.3", features=["serde-human-readable", "macros", "formatting"]}
time-tz = {version="2.0", features=["system"]}
tokio-postgres = {version="0.7", features=["with-time-0_3", "with-uuid-1", "with-serde_json-1"]}
tokio = {version="1.38", features=["rt", "macros", "rt-multi-thread"]}
tokio = {version="1.41", features=["rt", "macros", "rt-multi-thread"]}
rweb = {git = "https://github.com/ddboline/rweb.git", features=["openapi"], default-features=false, tag="0.15.2"}
rweb-helper = { git = "https://github.com/ddboline/rweb_helper.git", tag="0.5.3" }
uuid = { version = "1.0", features = ["serde", "v4"] }
Expand Down
2 changes: 2 additions & 0 deletions migrations/V11__authorized_users_created_deleted.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE authorized_users ADD COLUMN created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now();
ALTER TABLE authorized_users ADD COLUMN deleted_at TIMESTAMP WITH TIME ZONE;
63 changes: 45 additions & 18 deletions src/logged_user.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub use authorized_users::{
get_random_key, get_secrets, token::Token, AuthorizedUser, AUTHORIZED_USERS, JWT_SECRET,
KEY_LENGTH, LOGIN_HTML, SECRET_KEY, TRIGGER_DB_UPDATE,
get_random_key, get_secrets, token::Token, AuthorizedUser as ExternalUser, AUTHORIZED_USERS,
JWT_SECRET, KEY_LENGTH, LOGIN_HTML, SECRET_KEY, TRIGGER_DB_UPDATE,
};
use futures::TryStreamExt;
use log::debug;
use maplit::hashset;
use maplit::hashmap;
use rweb::{
filters::{cookie::cookie, BoxedFilter},
Filter, FromRequest, Rejection, Schema,
Expand All @@ -13,10 +13,12 @@ use rweb_helper::UuidWrapper;
use serde::{Deserialize, Serialize};
use stack_string::StackString;
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
env::var,
str::FromStr,
};
use time::OffsetDateTime;
use uuid::Uuid;

use crate::{errors::ServiceError as Error, models::AuthorizedUsers, pgpool::PgPool};
Expand Down Expand Up @@ -63,8 +65,8 @@ impl FromRequest for LoggedUser {
}
}

impl From<AuthorizedUser> for LoggedUser {
fn from(user: AuthorizedUser) -> Self {
impl From<ExternalUser> for LoggedUser {
fn from(user: ExternalUser) -> Self {
Self {
email: user.email,
session: user.session.into(),
Expand Down Expand Up @@ -99,21 +101,46 @@ impl FromStr for LoggedUser {
/// # Errors
/// Return error if db query fails
pub async fn fill_from_db(pool: &PgPool) -> Result<(), Error> {
debug!("{:?}", *TRIGGER_DB_UPDATE);
let users = if TRIGGER_DB_UPDATE.check() {
AuthorizedUsers::get_authorized_users(pool)
.await?
.map_ok(|user| user.email)
.try_collect()
.await?
} else {
AUTHORIZED_USERS.get_users()
};
if let Ok("true") = var("TESTENV").as_ref().map(String::as_str) {
AUTHORIZED_USERS.update_users(hashset! {"user@test".into()});
AUTHORIZED_USERS.update_users(hashmap! {
"user@test".into() => ExternalUser {
email: "user@test".into(),
session: Uuid::new_v4(),
secret_key: StackString::default(),
created_at: Some(OffsetDateTime::now_utc())
}
});
return Ok(());
}
let (created_at, deleted_at) = AuthorizedUsers::get_most_recent(pool).await?;
let most_recent_user_db = created_at.max(deleted_at);
let existing_users = AUTHORIZED_USERS.get_users();
let most_recent_user = existing_users.values().map(|i| i.created_at).max();
debug!("most_recent_user_db {most_recent_user_db:?} most_recent_user {most_recent_user:?}");
if most_recent_user_db.is_some()
&& most_recent_user.is_some()
&& most_recent_user_db <= most_recent_user
{
return Ok(());
}
AUTHORIZED_USERS.update_users(users);

debug!("{:?}", *AUTHORIZED_USERS);
let result: Result<HashMap<StackString, _>, _> = AuthorizedUsers::get_authorized_users(pool)
.await?
.map_ok(|u| {
(
u.email.clone(),
ExternalUser {
email: u.email,
session: Uuid::new_v4(),
secret_key: StackString::default(),
created_at: Some(u.created_at),
},
)
})
.try_collect()
.await;
let users = result?;
AUTHORIZED_USERS.update_users(users);
debug!("AUTHORIZED_USERS {:?}", *AUTHORIZED_USERS);
Ok(())
}
Loading

0 comments on commit ac210f5

Please sign in to comment.