Skip to content

Commit 19df0a0

Browse files
committed
Move options from AuthManagerParams to AuthManagerOptions
1 parent a8ea664 commit 19df0a0

File tree

5 files changed

+58
-28
lines changed

5 files changed

+58
-28
lines changed

src/auth_monitor.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use std::error::Error;
22
use std::time::{Duration, SystemTime};
33

44
use crate::auth_file_watcher::AuthFileWatcher;
5+
use crate::auth_monitor_options::AuthMonitorOptions;
56
use crate::auth_monitor_params::AuthMonitorParams;
67
use crate::message_parser::is_auth_failed_message;
78

89
pub struct AuthMonitor {
910
failed_attempts: i32,
10-
max_failed_attempts: i32,
11-
reset_after_seconds: u64,
11+
options: AuthMonitorOptions,
1212
file_watcher: AuthFileWatcher,
1313
last_failed_auth: SystemTime,
1414
}
@@ -18,8 +18,7 @@ impl AuthMonitor {
1818
params.validate()?;
1919
return Ok(AuthMonitor {
2020
failed_attempts: 0,
21-
max_failed_attempts: params.max_failed_attempts,
22-
reset_after_seconds: params.reset_after_seconds as u64,
21+
options: params.options,
2322
file_watcher: AuthFileWatcher::new(&params.filepath)?,
2423
last_failed_auth: SystemTime::UNIX_EPOCH,
2524
});
@@ -41,14 +40,14 @@ impl AuthMonitor {
4140
}
4241

4342
fn should_reset_failed_attempts(&self) -> bool {
44-
if self.failed_attempts <= 0 || self.failed_attempts >= self.max_failed_attempts {
43+
if self.failed_attempts <= 0 || self.failed_attempts >= self.options.max_failed_attempts {
4544
return false;
4645
}
4746
let seconds_from_last_error = SystemTime::now()
4847
.duration_since(self.last_failed_auth)
4948
.unwrap_or(Duration::ZERO)
5049
.as_secs();
51-
return seconds_from_last_error > self.reset_after_seconds;
50+
return seconds_from_last_error > self.options.reset_after_seconds as u64;
5251
}
5352

5453
fn reset_failed_attempts(&mut self) {
@@ -65,7 +64,7 @@ impl AuthMonitor {
6564
self.last_failed_auth = SystemTime::now();
6665
self.failed_attempts += failed_attempts;
6766
println!("Authentication failed {} time(s)", self.failed_attempts);
68-
if self.failed_attempts >= self.max_failed_attempts {
67+
if self.failed_attempts >= self.options.max_failed_attempts {
6968
println!("Authentication fail limit reached, shutting down");
7069
on_max_failed_attempts();
7170
}

src/auth_monitor_options.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::fmt::{Display, Formatter};
2+
3+
const MAX_FAILED_ATTEMPTS: i32 = 3;
4+
const RESET_AFTER_SECONDS: i32 = 1800;
5+
6+
pub struct AuthMonitorOptions {
7+
pub max_failed_attempts: i32,
8+
pub reset_after_seconds: i32,
9+
}
10+
11+
impl Default for AuthMonitorOptions {
12+
fn default() -> Self {
13+
return AuthMonitorOptions {
14+
max_failed_attempts: MAX_FAILED_ATTEMPTS,
15+
reset_after_seconds: RESET_AFTER_SECONDS,
16+
};
17+
}
18+
}
19+
20+
impl Display for AuthMonitorOptions {
21+
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
22+
return write!(
23+
formatter,
24+
"max_failed_attempts={}, reset_after_seconds={}",
25+
self.max_failed_attempts, self.reset_after_seconds
26+
);
27+
}
28+
}

src/auth_monitor_params.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::error::Error;
22
use std::fmt::{Display, Formatter};
33
use std::str::FromStr;
44

5+
use crate::auth_monitor_options::AuthMonitorOptions;
6+
57
const OPTION_PREFIX: &str = "--";
68
const OPTION_PREFIX_LENGTH: usize = OPTION_PREFIX.len();
79

@@ -11,13 +13,9 @@ const OPTION_VALUE_SEPARATOR_LENGTH: usize = 1;
1113
const MAX_FAILED_ATTEMPTS_OPTION: &str = "max-failed-attempts";
1214
const RESET_AFTER_SECONDS_OPTION: &str = "reset-after-seconds";
1315

14-
const MAX_FAILED_ATTEMPTS: i32 = 3;
15-
const RESET_AFTER_SECONDS: i32 = 1800;
16-
1716
pub struct AuthMonitorParams {
1817
pub filepath: String,
19-
pub max_failed_attempts: i32,
20-
pub reset_after_seconds: i32,
18+
pub options: AuthMonitorOptions,
2119
}
2220

2321
impl AuthMonitorParams {
@@ -42,11 +40,11 @@ impl AuthMonitorParams {
4240
};
4341
match &option_name[OPTION_PREFIX_LENGTH..] {
4442
MAX_FAILED_ATTEMPTS_OPTION => {
45-
params.max_failed_attempts =
43+
params.options.max_failed_attempts =
4644
Self::parse_option_value(option_name, option_value)?;
4745
}
4846
RESET_AFTER_SECONDS_OPTION => {
49-
params.reset_after_seconds =
47+
params.options.reset_after_seconds =
5048
Self::parse_option_value(option_name, option_value)?;
5149
}
5250
_ => Err(format!("Unknown option {}", argument))?,
@@ -82,13 +80,13 @@ impl AuthMonitorParams {
8280
if self.filepath.is_empty() {
8381
Err("File path not specified")?;
8482
}
85-
if self.max_failed_attempts <= 0 {
83+
if self.options.max_failed_attempts <= 0 {
8684
return Err(format!(
8785
"{} must be greater than 0",
8886
MAX_FAILED_ATTEMPTS_OPTION
8987
))?;
9088
}
91-
if self.reset_after_seconds <= 0 {
89+
if self.options.reset_after_seconds <= 0 {
9290
return Err(format!(
9391
"{} must be greater than 0",
9492
RESET_AFTER_SECONDS_OPTION
@@ -102,8 +100,7 @@ impl Default for AuthMonitorParams {
102100
fn default() -> Self {
103101
return AuthMonitorParams {
104102
filepath: String::new(),
105-
max_failed_attempts: MAX_FAILED_ATTEMPTS,
106-
reset_after_seconds: RESET_AFTER_SECONDS,
103+
options: AuthMonitorOptions::default(),
107104
};
108105
}
109106
}
@@ -112,8 +109,8 @@ impl Display for AuthMonitorParams {
112109
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
113110
return write!(
114111
formatter,
115-
"filepath={}, max_failed_attempts={}, reset_after_seconds={}",
116-
self.filepath, self.max_failed_attempts, self.reset_after_seconds
112+
"filepath={}, options: {}",
113+
self.filepath, self.options
117114
);
118115
}
119116
}

src/auth_monitor_params_test.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::error::Error;
22

3+
use crate::auth_monitor_options::AuthMonitorOptions;
34
use crate::auth_monitor_params::{
45
AuthMonitorParams, MAX_FAILED_ATTEMPTS_OPTION, RESET_AFTER_SECONDS_OPTION,
56
};
@@ -80,11 +81,11 @@ fn expect_equals(result: Result<AuthMonitorParams, Box<dyn Error>>, expected: &A
8081
"File path does not match"
8182
);
8283
assert_eq!(
83-
params.max_failed_attempts, expected.max_failed_attempts,
84+
params.options.max_failed_attempts, expected.options.max_failed_attempts,
8485
"Max failed attempts does not match"
8586
);
8687
assert_eq!(
87-
params.reset_after_seconds, expected.reset_after_seconds,
88+
params.options.reset_after_seconds, expected.options.reset_after_seconds,
8889
"Reset after seconds does not match"
8990
);
9091
}
@@ -98,16 +99,18 @@ fn when_parsing_filepath_with_one_option_with_correct_value_then_return_params_w
9899
let arguments = [String::from(FILEPATH), option_argument];
99100
let max_failed_attempts = match option == MAX_FAILED_ATTEMPTS_OPTION {
100101
true => value,
101-
false => default_params.max_failed_attempts,
102+
false => default_params.options.max_failed_attempts,
102103
};
103104
let reset_after_seconds = match option == RESET_AFTER_SECONDS_OPTION {
104105
true => value,
105-
false => default_params.reset_after_seconds,
106+
false => default_params.options.reset_after_seconds,
106107
};
107108
let expected = AuthMonitorParams {
108109
filepath: String::from(FILEPATH),
109-
max_failed_attempts,
110-
reset_after_seconds,
110+
options: AuthMonitorOptions {
111+
max_failed_attempts,
112+
reset_after_seconds,
113+
},
111114
};
112115
expect_equals(AuthMonitorParams::from_arguments(&arguments), &expected);
113116
}
@@ -172,8 +175,10 @@ fn when_parsing_filename_and_multiple_options_then_return_params_with_parsed_val
172175
];
173176
let expected = AuthMonitorParams {
174177
filepath: String::from(FILEPATH),
175-
max_failed_attempts,
176-
reset_after_seconds,
178+
options: AuthMonitorOptions {
179+
max_failed_attempts,
180+
reset_after_seconds,
181+
},
177182
};
178183
expect_equals(AuthMonitorParams::from_arguments(&arguments), &expected);
179184
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::auth_monitor_params::AuthMonitorParams;
1414
mod auth_file_reader;
1515
mod auth_file_watcher;
1616
mod auth_monitor;
17+
mod auth_monitor_options;
1718
mod auth_monitor_params;
1819
mod file_event_filter;
1920
mod file_path;

0 commit comments

Comments
 (0)