Commit d7dfc3b 1 parent e8e935c commit d7dfc3b Copy full SHA for d7dfc3b
File tree 6 files changed +48
-1
lines changed
packages/configuration/src
6 files changed +48
-1
lines changed Original file line number Diff line number Diff line change 5
5
//!
6
6
//! The current version for configuration is [`v2`].
7
7
pub mod v2;
8
+ pub mod validator;
8
9
9
10
use std:: collections:: HashMap ;
10
11
use std:: env;
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
3
3
4
4
use super :: network:: Network ;
5
5
use crate :: v2:: database:: Database ;
6
+ use crate :: validator:: { SemanticValidationError , Validator } ;
6
7
use crate :: { AnnouncePolicy , TrackerPolicy } ;
7
8
8
9
#[ allow( clippy:: struct_excessive_bools) ]
@@ -131,3 +132,13 @@ impl PrivateMode {
131
132
true
132
133
}
133
134
}
135
+
136
+ impl Validator for Core {
137
+ fn validate ( & self ) -> Result < ( ) , SemanticValidationError > {
138
+ if self . private_mode . is_some ( ) && !self . private {
139
+ return Err ( SemanticValidationError :: UselessPrivateModeSection ) ;
140
+ }
141
+
142
+ Ok ( ( ) )
143
+ }
144
+ }
Original file line number Diff line number Diff line change @@ -251,6 +251,7 @@ use self::health_check_api::HealthCheckApi;
251
251
use self :: http_tracker:: HttpTracker ;
252
252
use self :: tracker_api:: HttpApi ;
253
253
use self :: udp_tracker:: UdpTracker ;
254
+ use crate :: validator:: { SemanticValidationError , Validator } ;
254
255
use crate :: { Error , Info , Metadata , Version } ;
255
256
256
257
/// This configuration version
@@ -394,6 +395,12 @@ impl Configuration {
394
395
}
395
396
}
396
397
398
+ impl Validator for Configuration {
399
+ fn validate ( & self ) -> Result < ( ) , SemanticValidationError > {
400
+ self . core . validate ( )
401
+ }
402
+ }
403
+
397
404
#[ cfg( test) ]
398
405
mod tests {
399
406
Original file line number Diff line number Diff line change
1
+ //! Trait to validate semantic errors.
2
+ //!
3
+ //! Errors could involve more than one configuration option. Some configuration
4
+ //! combinations can be incompatible.
5
+ use thiserror:: Error ;
6
+
7
+ /// Errors that can occur validating the configuration.
8
+ #[ derive( Error , Debug ) ]
9
+ pub enum SemanticValidationError {
10
+ #[ error( "Private mode section in configuration can only be included when the tracker is running in private mode." ) ]
11
+ UselessPrivateModeSection ,
12
+ }
13
+
14
+ pub trait Validator {
15
+ /// # Errors
16
+ ///
17
+ /// Will return an error if the configuration is invalid.
18
+ fn validate ( & self ) -> Result < ( ) , SemanticValidationError > ;
19
+ }
Original file line number Diff line number Diff line change 14
14
use std:: sync:: Arc ;
15
15
16
16
use torrust_tracker_clock:: static_time;
17
+ use torrust_tracker_configuration:: validator:: Validator ;
17
18
use torrust_tracker_configuration:: Configuration ;
18
19
use tracing:: info;
19
20
@@ -24,10 +25,18 @@ use crate::core::Tracker;
24
25
use crate :: shared:: crypto:: ephemeral_instance_keys;
25
26
26
27
/// It loads the configuration from the environment and builds the main domain [`Tracker`] struct.
28
+ ///
29
+ /// # Panics
30
+ ///
31
+ /// Setup can file if the configuration is invalid.
27
32
#[ must_use]
28
33
pub fn setup ( ) -> ( Configuration , Arc < Tracker > ) {
29
34
let configuration = initialize_configuration ( ) ;
30
35
36
+ if let Err ( e) = configuration. validate ( ) {
37
+ panic ! ( "Configuration error: {e}" ) ;
38
+ }
39
+
31
40
let tracker = initialize_with_configuration ( & configuration) ;
32
41
33
42
info ! ( "Configuration:\n {}" , configuration. clone( ) . mask_secrets( ) . to_json( ) ) ;
Original file line number Diff line number Diff line change 4
4
//! Tracker keys are tokens used to authenticate the tracker clients when the tracker runs
5
5
//! in `private` or `private_listed` modes.
6
6
//!
7
- //! There are services to [`generate_key`] and [`verify_key `] authentication keys.
7
+ //! There are services to [`generate_key`] and [`verify_key_expiration `] authentication keys.
8
8
//!
9
9
//! Authentication keys are used only by [`HTTP`](crate::servers::http) trackers. All keys have an expiration time, that means
10
10
//! they are only valid during a period of time. After that time the expiring key will no longer be valid.
You can’t perform that action at this time.
0 commit comments