-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from sebadob/audit-part-1
audit part 1
- Loading branch information
Showing
17 changed files
with
292 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[build] | ||
rustflags = ["--cfg=uuid_unstable"] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "rauthy-events" | ||
version.workspace = true | ||
edition.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
|
||
[dependencies] | ||
chrono = { workspace = true } | ||
flume = { workspace = true } | ||
rauthy-common = { path = "../rauthy-common" } | ||
serde = { workspace = true } | ||
serde_json = { workspace = true } | ||
sqlx = { workspace = true } | ||
tracing = { workspace = true } | ||
tracing-subscriber = { workspace = true } | ||
tokio = { workspace = true } | ||
uuid = { version = "1", features = ["serde", "v7"] } | ||
validator = { workspace = true } | ||
|
||
[dev-dependencies] | ||
pretty_assertions = "1" | ||
tokio-test = "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
use chrono::{DateTime, Timelike, Utc}; | ||
use rauthy_common::error_response::{ErrorResponse, ErrorResponseType}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::{Display, Formatter}; | ||
use tracing::error; | ||
use uuid::Uuid; | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
pub enum EventLevel { | ||
Info, | ||
Notice, | ||
Warning, | ||
Critical, | ||
} | ||
|
||
impl Display for EventLevel { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
let s = match self { | ||
EventLevel::Info => "INFO ", | ||
EventLevel::Notice => "NOTICE ", | ||
EventLevel::Warning => "WARNING ", | ||
EventLevel::Critical => "CRITICAL", | ||
}; | ||
write!(f, "{}", s) | ||
} | ||
} | ||
|
||
impl EventLevel { | ||
pub fn as_str(&self) -> &str { | ||
match self { | ||
EventLevel::Info => "INFO", | ||
EventLevel::Notice => "NOTICE", | ||
EventLevel::Warning => "WARNING", | ||
EventLevel::Critical => "CRITICAL", | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
pub enum EventType { | ||
//github.com/ number of invalid logins | ||
InvalidLogins(u32), | ||
IpBlacklisted, | ||
//github.com/ E-Mail of the new admin role member | ||
NewRauthyAdmin(String), | ||
PossibleBruteForce, | ||
PossibleDoS, | ||
} | ||
|
||
impl ToString for EventType { | ||
fn to_string(&self) -> String { | ||
match self { | ||
EventType::InvalidLogins(count) => format!("invalid logins: {}", count), | ||
EventType::IpBlacklisted => "IP blacklisted".to_string(), | ||
EventType::NewRauthyAdmin(email) => format!("new rauthy_admin member: {}", email), | ||
EventType::PossibleBruteForce => "possible brute force".to_string(), | ||
EventType::PossibleDoS => "possible DoS".to_string(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
pub struct Event { | ||
pub id: Uuid, | ||
pub timestamp: DateTime<Utc>, | ||
pub level: EventLevel, | ||
pub typ: EventType, | ||
pub ip: Option<String>, | ||
} | ||
|
||
impl Display for Event { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!( | ||
f, | ||
"EVENT {} {}:{}:{} {} {} {}", | ||
self.timestamp.date_naive(), | ||
self.timestamp.hour(), | ||
self.timestamp.minute(), | ||
self.timestamp.second(), | ||
self.level, | ||
self.typ.to_string(), | ||
self.ip.as_deref().unwrap_or_default(), | ||
) | ||
} | ||
} | ||
|
||
impl Event { | ||
pub fn new(level: EventLevel, typ: EventType, ip: Option<String>) -> Self { | ||
Self { | ||
id: Uuid::now_v7(), | ||
timestamp: Utc::now(), | ||
level, | ||
typ, | ||
ip, | ||
} | ||
} | ||
|
||
//github.com/ The EventLevel will change depending on the amount of invalid logins | ||
pub fn invalid_login(count: u32, ip: String) -> Self { | ||
let level = if count > 6 { | ||
EventLevel::Warning | ||
} else if count > 3 { | ||
EventLevel::Notice | ||
} else { | ||
EventLevel::Info | ||
}; | ||
Self::new(level, EventType::InvalidLogins(count), Some(ip)) | ||
} | ||
|
||
pub fn ip_blacklisted(ip: String) -> Self { | ||
Self::new(EventLevel::Warning, EventType::IpBlacklisted, Some(ip)) | ||
} | ||
|
||
pub fn rauthy_admin(email: String) -> Self { | ||
Self::new(EventLevel::Notice, EventType::NewRauthyAdmin(email), None) | ||
} | ||
|
||
pub fn brute_force(ip: String) -> Self { | ||
Self::new( | ||
EventLevel::Critical, | ||
EventType::PossibleBruteForce, | ||
Some(ip), | ||
) | ||
} | ||
|
||
pub fn dos(ip: String) -> Self { | ||
Self::new(EventLevel::Critical, EventType::PossibleDoS, Some(ip)) | ||
} | ||
|
||
#[inline(always)] | ||
pub async fn send(self, tx: &flume::Sender<Self>) -> Result<(), ErrorResponse> { | ||
match tx.send_async(self).await { | ||
Ok(_) => Ok(()), | ||
Err(err) => { | ||
error!("Event::send: {:?}", err); | ||
Err(ErrorResponse::new( | ||
ErrorResponseType::Internal, | ||
format!("Error sending event internally: {:?}", err), | ||
)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Rauthy - OpenID Connect and Single Sign-On Identity & Access Management | ||
// Copyright (C) 2023 Sebastian Dobe <sebastiandobe@mailbox.org> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
pub mod event; | ||
pub mod listener; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::event::{Event, EventLevel}; | ||
use rauthy_common::error_response::ErrorResponse; | ||
use tracing::{info, warn}; | ||
|
||
pub struct EventListener; | ||
|
||
impl EventListener { | ||
pub async fn listen(rx: flume::Receiver<Event>) -> Result<(), ErrorResponse> { | ||
while let Ok(event) = rx.recv_async().await { | ||
match event.level { | ||
EventLevel::Info | EventLevel::Notice => info!("{}", event), | ||
EventLevel::Warning => warn!("{}", event), | ||
EventLevel::Critical => warn!("{}", event), | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.