Skip to content

Commit e8e935c

Browse files
committed
feat: [#978] add a config option to disable cheking keys' expiration
When the tracker is running in private mode you can disable checking keys' expiration in the configuration with: ```toml [core] private = false [core.private_mode] check_keys_expiration = true ``` All keys will be valid as long as they exist in the database.
1 parent 680f642 commit e8e935c

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

packages/configuration/src/v2/core.rs

+39
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use derive_more::{Constructor, Display};
12
use serde::{Deserialize, Serialize};
23

34
use super::network::Network;
@@ -32,6 +33,10 @@ pub struct Core {
3233
#[serde(default = "Core::default_private")]
3334
pub private: bool,
3435

36+
// Configuration specific when the tracker is running in private mode.
37+
#[serde(default = "Core::default_private_mode")]
38+
pub private_mode: Option<PrivateMode>,
39+
3540
// Tracker policy configuration.
3641
#[serde(default = "Core::default_tracker_policy")]
3742
pub tracker_policy: TrackerPolicy,
@@ -54,6 +59,7 @@ impl Default for Core {
5459
listed: Self::default_listed(),
5560
net: Self::default_network(),
5661
private: Self::default_private(),
62+
private_mode: Self::default_private_mode(),
5763
tracker_policy: Self::default_tracker_policy(),
5864
tracker_usage_statistics: Self::default_tracker_usage_statistics(),
5965
}
@@ -85,10 +91,43 @@ impl Core {
8591
false
8692
}
8793

94+
fn default_private_mode() -> Option<PrivateMode> {
95+
if Self::default_private() {
96+
Some(PrivateMode::default())
97+
} else {
98+
None
99+
}
100+
}
101+
88102
fn default_tracker_policy() -> TrackerPolicy {
89103
TrackerPolicy::default()
90104
}
91105
fn default_tracker_usage_statistics() -> bool {
92106
true
93107
}
94108
}
109+
110+
/// Configuration specific when the tracker is running in private mode.
111+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Copy, Constructor, Display)]
112+
pub struct PrivateMode {
113+
/// A flag to disable expiration date for peer keys.
114+
///
115+
/// When true, if the keys is not permanent the expiration date will be
116+
/// ignored. The key will be accepted even if it has expired.
117+
#[serde(default = "PrivateMode::default_check_keys_expiration")]
118+
pub check_keys_expiration: bool,
119+
}
120+
121+
impl Default for PrivateMode {
122+
fn default() -> Self {
123+
Self {
124+
check_keys_expiration: Self::default_check_keys_expiration(),
125+
}
126+
}
127+
}
128+
129+
impl PrivateMode {
130+
fn default_check_keys_expiration() -> bool {
131+
true
132+
}
133+
}

src/core/auth.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//!
3434
//! // And you can later verify it with:
3535
//!
36-
//! assert!(auth::verify_key(&expiring_key).is_ok());
36+
//! assert!(auth::verify_key_expiration(&expiring_key).is_ok());
3737
//! ```
3838
3939
use std::panic::Location;
@@ -106,7 +106,7 @@ pub fn generate_key(lifetime: Option<Duration>) -> PeerKey {
106106
///
107107
/// - `Error::KeyExpired` if `auth_key.valid_until` is past the `current_time`.
108108
/// - `Error::KeyInvalid` if `auth_key.valid_until` is past the `None`.
109-
pub fn verify_key(auth_key: &PeerKey) -> Result<(), Error> {
109+
pub fn verify_key_expiration(auth_key: &PeerKey) -> Result<(), Error> {
110110
let current_time: DurationSinceUnixEpoch = CurrentClock::now();
111111

112112
match auth_key.valid_until {
@@ -322,7 +322,7 @@ mod tests {
322322
fn should_be_generated_with_a_expiration_time() {
323323
let expiring_key = auth::generate_key(Some(Duration::new(9999, 0)));
324324

325-
assert!(auth::verify_key(&expiring_key).is_ok());
325+
assert!(auth::verify_key_expiration(&expiring_key).is_ok());
326326
}
327327

328328
#[test]
@@ -336,12 +336,12 @@ mod tests {
336336
// Mock the time has passed 10 sec.
337337
clock::Stopped::local_add(&Duration::from_secs(10)).unwrap();
338338

339-
assert!(auth::verify_key(&expiring_key).is_ok());
339+
assert!(auth::verify_key_expiration(&expiring_key).is_ok());
340340

341341
// Mock the time has passed another 10 sec.
342342
clock::Stopped::local_add(&Duration::from_secs(10)).unwrap();
343343

344-
assert!(auth::verify_key(&expiring_key).is_err());
344+
assert!(auth::verify_key_expiration(&expiring_key).is_err());
345345
}
346346
}
347347
}

src/core/mod.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,16 @@ impl Tracker {
996996
location: Location::caller(),
997997
key: Box::new(key.clone()),
998998
}),
999-
Some(key) => auth::verify_key(key),
999+
Some(key) => match self.config.private_mode {
1000+
Some(private_mode) => {
1001+
if private_mode.check_keys_expiration {
1002+
return auth::verify_key_expiration(key);
1003+
}
1004+
1005+
Ok(())
1006+
}
1007+
None => auth::verify_key_expiration(key),
1008+
},
10001009
}
10011010
}
10021011

@@ -1779,8 +1788,9 @@ mod tests {
17791788
use std::time::Duration;
17801789

17811790
use torrust_tracker_clock::clock::Time;
1791+
use torrust_tracker_configuration::v2::core::PrivateMode;
17821792

1783-
use crate::core::auth;
1793+
use crate::core::auth::{self, Key};
17841794
use crate::core::tests::the_tracker::private_tracker;
17851795
use crate::CurrentClock;
17861796

@@ -1829,6 +1839,24 @@ mod tests {
18291839
assert!(tracker.verify_auth_key(&expiring_key.key()).await.is_ok());
18301840
}
18311841

1842+
#[tokio::test]
1843+
async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration() {
1844+
let mut tracker = private_tracker();
1845+
1846+
tracker.config.private_mode = Some(PrivateMode {
1847+
check_keys_expiration: false,
1848+
});
1849+
1850+
let past_time = Some(Duration::ZERO);
1851+
1852+
let expiring_key = tracker
1853+
.add_auth_key(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(), past_time)
1854+
.await
1855+
.unwrap();
1856+
1857+
assert!(tracker.authenticate(&expiring_key.key()).await.is_ok());
1858+
}
1859+
18321860
#[tokio::test]
18331861
async fn it_should_fail_verifying_an_unregistered_authentication_key() {
18341862
let tracker = private_tracker();

0 commit comments

Comments
 (0)