Skip to content

Commit 393a6ac

Browse files
committed
Merge #1196: Overhaul core Tracker: refactor authentication module
dff6bca refactor: [#1195] remove authentication::Facade service (Jose Celano) 77eccdc fix: [#1195] format (Jose Celano) 661fe6a refactor: [#1195] remove AuthenticationService from authentication Facade (Jose Celano) 747b608 refactor: [#1195] use AuthenticationService directy (Jose Celano) 457d01b refactor: [#1195] rename service to AuthenticationService (Jose Celano) 965e911 refactor: [#1195] inject dependencies into authenticattion::Facade (Jose Celano) 504357c refactor: [#1195] inject dependencies in authenticatio::Facade (Jose Celano) 663250b refactor: [#1195] rename methods (Jose Celano) 9c61b26 refactor: [#1195] move tests to KeysHandler (Jose Celano) c06da07 refactor: [#1195] more authentication tests to authentication service (Jose Celano) bb2f9e0 refactor: [#1195] extract core::authentication::handler::KeysHandler (Jose Celano) 23590e7 refactor: [#1195] make method private (Jose Celano) 81b4b3c refactor: [#1195] extract and move method (Jose Celano) cd542dc refactor: [#1195] extract authentication::Service (Jose Celano) a93a79c refactor: [#1195] remove deprecated context section in docs (Jose Celano) 12a62ce refactor: [#1195] extract InMemoryKeyRepository (Jose Celano) f4c7b97 refactor: [#1195] extract DatabaseKeyRepository (Jose Celano) 44255e2 refactor: [#1195] create dir for mod (Jose Celano) Pull request description: Overhaul core Tracker: refactor authentication module. ACKs for top commit: josecelano: ACK dff6bca Tree-SHA512: 6f0fb0c9c97a9bb2056e293d1774bcbcf507e2a922eccb2c97675ac0963efcfe4848b53c45b88b512ae5a73e394f368e9bb9c4c6ee0af3b900a008eeddba02b9
2 parents 757126d + dff6bca commit 393a6ac

34 files changed

+1063
-641
lines changed

src/app.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
5252
// Load peer keys
5353
if app_container.tracker.is_private() {
5454
app_container
55-
.tracker
56-
.authentication
55+
.keys_handler
5756
.load_keys_from_database()
5857
.await
5958
.expect("Could not retrieve keys from database.");
@@ -100,6 +99,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
10099
if let Some(job) = http_tracker::start_job(
101100
http_tracker_config,
102101
app_container.tracker.clone(),
102+
app_container.authentication_service.clone(),
103103
app_container.whitelist_authorization.clone(),
104104
app_container.stats_event_sender.clone(),
105105
registar.give_form(),
@@ -119,6 +119,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
119119
if let Some(job) = tracker_apis::start_job(
120120
http_api_config,
121121
app_container.tracker.clone(),
122+
app_container.keys_handler.clone(),
122123
app_container.whitelist_manager.clone(),
123124
app_container.ban_service.clone(),
124125
app_container.stats_event_sender.clone(),

src/app_test.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ use std::sync::Arc;
33

44
use torrust_tracker_configuration::Configuration;
55

6+
use crate::core::authentication::handler::KeysHandler;
7+
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
8+
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
9+
use crate::core::authentication::service::{self, AuthenticationService};
610
use crate::core::databases::Database;
711
use crate::core::services::initialize_database;
12+
use crate::core::whitelist;
813
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
9-
use crate::core::{authentication, whitelist};
1014

1115
/// Initialize the tracker dependencies.
1216
#[allow(clippy::type_complexity)]
@@ -17,15 +21,21 @@ pub fn initialize_tracker_dependencies(
1721
Arc<Box<dyn Database>>,
1822
Arc<InMemoryWhitelist>,
1923
Arc<whitelist::authorization::Authorization>,
20-
Arc<authentication::Facade>,
24+
Arc<AuthenticationService>,
2125
) {
2226
let database = initialize_database(config);
2327
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
2428
let whitelist_authorization = Arc::new(whitelist::authorization::Authorization::new(
2529
&config.core,
2630
&in_memory_whitelist.clone(),
2731
));
28-
let authentication = Arc::new(authentication::Facade::new(&config.core, &database.clone()));
32+
let db_key_repository = Arc::new(DatabaseKeyRepository::new(&database));
33+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
34+
let authentication_service = Arc::new(service::AuthenticationService::new(&config.core, &in_memory_key_repository));
35+
let _keys_handler = Arc::new(KeysHandler::new(
36+
&db_key_repository.clone(),
37+
&in_memory_key_repository.clone(),
38+
));
2939

30-
(database, in_memory_whitelist, whitelist_authorization, authentication)
40+
(database, in_memory_whitelist, whitelist_authorization, authentication_service)
3141
}

src/bootstrap/app.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ use tracing::instrument;
2222
use super::config::initialize_configuration;
2323
use crate::bootstrap;
2424
use crate::container::AppContainer;
25+
use crate::core::authentication::handler::KeysHandler;
26+
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
27+
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
28+
use crate::core::authentication::service;
2529
use crate::core::services::{initialize_database, initialize_tracker, initialize_whitelist_manager, statistics};
30+
use crate::core::whitelist;
2631
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
27-
use crate::core::{authentication, whitelist};
2832
use crate::servers::udp::server::banning::BanService;
2933
use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP;
3034
use crate::shared::crypto::ephemeral_instance_keys;
@@ -89,23 +93,28 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
8993
&in_memory_whitelist.clone(),
9094
));
9195
let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone());
92-
let authentication = Arc::new(authentication::Facade::new(&configuration.core, &database.clone()));
93-
94-
let tracker = Arc::new(initialize_tracker(
95-
configuration,
96-
&database,
97-
&whitelist_authorization,
98-
&authentication,
96+
let db_key_repository = Arc::new(DatabaseKeyRepository::new(&database));
97+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
98+
let authentication_service = Arc::new(service::AuthenticationService::new(
99+
&configuration.core,
100+
&in_memory_key_repository,
99101
));
102+
let keys_handler = Arc::new(KeysHandler::new(
103+
&db_key_repository.clone(),
104+
&in_memory_key_repository.clone(),
105+
));
106+
107+
let tracker = Arc::new(initialize_tracker(configuration, &database, &whitelist_authorization));
100108

101109
AppContainer {
102110
tracker,
111+
keys_handler,
112+
authentication_service,
103113
whitelist_authorization,
104114
ban_service,
105115
stats_event_sender,
106116
stats_repository,
107117
whitelist_manager,
108-
authentication,
109118
}
110119
}
111120

src/bootstrap/jobs/http_tracker.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use torrust_tracker_configuration::HttpTracker;
1919
use tracing::instrument;
2020

2121
use super::make_rust_tls;
22+
use crate::core::authentication::service::AuthenticationService;
2223
use crate::core::statistics::event::sender::Sender;
2324
use crate::core::{self, statistics, whitelist};
2425
use crate::servers::http::server::{HttpServer, Launcher};
@@ -34,10 +35,11 @@ use crate::servers::registar::ServiceRegistrationForm;
3435
///
3536
/// It would panic if the `config::HttpTracker` struct would contain inappropriate values.
3637
///
37-
#[instrument(skip(config, tracker, whitelist_authorization, stats_event_sender, form))]
38+
#[instrument(skip(config, tracker, authentication_service, whitelist_authorization, stats_event_sender, form))]
3839
pub async fn start_job(
3940
config: &HttpTracker,
4041
tracker: Arc<core::Tracker>,
42+
authentication_service: Arc<AuthenticationService>,
4143
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
4244
stats_event_sender: Arc<Option<Box<dyn Sender>>>,
4345
form: ServiceRegistrationForm,
@@ -55,6 +57,7 @@ pub async fn start_job(
5557
socket,
5658
tls,
5759
tracker.clone(),
60+
authentication_service.clone(),
5861
whitelist_authorization.clone(),
5962
stats_event_sender.clone(),
6063
form,
@@ -70,12 +73,19 @@ async fn start_v1(
7073
socket: SocketAddr,
7174
tls: Option<RustlsConfig>,
7275
tracker: Arc<core::Tracker>,
76+
authentication_service: Arc<AuthenticationService>,
7377
whitelist_authorization: Arc<whitelist::authorization::Authorization>,
7478
stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
7579
form: ServiceRegistrationForm,
7680
) -> JoinHandle<()> {
7781
let server = HttpServer::new(Launcher::new(socket, tls))
78-
.start(tracker, whitelist_authorization, stats_event_sender, form)
82+
.start(
83+
tracker,
84+
authentication_service,
85+
whitelist_authorization,
86+
stats_event_sender,
87+
form,
88+
)
7989
.await
8090
.expect("it should be able to start to the http tracker");
8191

@@ -100,9 +110,13 @@ mod tests {
100110

101111
use crate::bootstrap::app::initialize_global_services;
102112
use crate::bootstrap::jobs::http_tracker::start_job;
113+
use crate::core::authentication::handler::KeysHandler;
114+
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
115+
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
116+
use crate::core::authentication::service;
103117
use crate::core::services::{initialize_database, initialize_tracker, statistics};
118+
use crate::core::whitelist;
104119
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
105-
use crate::core::{authentication, whitelist};
106120
use crate::servers::http::Version;
107121
use crate::servers::registar::Registar;
108122

@@ -123,15 +137,22 @@ mod tests {
123137
&cfg.core,
124138
&in_memory_whitelist.clone(),
125139
));
126-
let authentication = Arc::new(authentication::Facade::new(&cfg.core, &database.clone()));
140+
let db_key_repository = Arc::new(DatabaseKeyRepository::new(&database));
141+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
142+
let authentication_service = Arc::new(service::AuthenticationService::new(&cfg.core, &in_memory_key_repository));
143+
let _keys_handler = Arc::new(KeysHandler::new(
144+
&db_key_repository.clone(),
145+
&in_memory_key_repository.clone(),
146+
));
127147

128-
let tracker = Arc::new(initialize_tracker(&cfg, &database, &whitelist_authorization, &authentication));
148+
let tracker = Arc::new(initialize_tracker(&cfg, &database, &whitelist_authorization));
129149

130150
let version = Version::V1;
131151

132152
start_job(
133153
config,
134154
tracker,
155+
authentication_service,
135156
whitelist_authorization,
136157
stats_event_sender,
137158
Registar::default().give_form(),

src/bootstrap/jobs/tracker_apis.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use torrust_tracker_configuration::{AccessTokens, HttpApi};
3030
use tracing::instrument;
3131

3232
use super::make_rust_tls;
33+
use crate::core::authentication::handler::KeysHandler;
3334
use crate::core::statistics::event::sender::Sender;
3435
use crate::core::statistics::repository::Repository;
3536
use crate::core::whitelist::manager::WhiteListManager;
@@ -60,10 +61,20 @@ pub struct ApiServerJobStarted();
6061
///
6162
///
6263
#[allow(clippy::too_many_arguments)]
63-
#[instrument(skip(config, tracker, whitelist_manager, ban_service, stats_event_sender, stats_repository, form))]
64+
#[instrument(skip(
65+
config,
66+
tracker,
67+
keys_handler,
68+
whitelist_manager,
69+
ban_service,
70+
stats_event_sender,
71+
stats_repository,
72+
form
73+
))]
6474
pub async fn start_job(
6575
config: &HttpApi,
6676
tracker: Arc<core::Tracker>,
77+
keys_handler: Arc<KeysHandler>,
6778
whitelist_manager: Arc<WhiteListManager>,
6879
ban_service: Arc<RwLock<BanService>>,
6980
stats_event_sender: Arc<Option<Box<dyn Sender>>>,
@@ -85,6 +96,7 @@ pub async fn start_job(
8596
bind_to,
8697
tls,
8798
tracker.clone(),
99+
keys_handler.clone(),
88100
whitelist_manager.clone(),
89101
ban_service.clone(),
90102
stats_event_sender.clone(),
@@ -103,6 +115,7 @@ pub async fn start_job(
103115
socket,
104116
tls,
105117
tracker,
118+
keys_handler,
106119
whitelist_manager,
107120
ban_service,
108121
stats_event_sender,
@@ -114,6 +127,7 @@ async fn start_v1(
114127
socket: SocketAddr,
115128
tls: Option<RustlsConfig>,
116129
tracker: Arc<core::Tracker>,
130+
keys_handler: Arc<KeysHandler>,
117131
whitelist_manager: Arc<WhiteListManager>,
118132
ban_service: Arc<RwLock<BanService>>,
119133
stats_event_sender: Arc<Option<Box<dyn Sender>>>,
@@ -124,6 +138,7 @@ async fn start_v1(
124138
let server = ApiServer::new(Launcher::new(socket, tls))
125139
.start(
126140
tracker,
141+
keys_handler,
127142
whitelist_manager,
128143
stats_event_sender,
129144
stats_repository,
@@ -149,9 +164,13 @@ mod tests {
149164

150165
use crate::bootstrap::app::initialize_global_services;
151166
use crate::bootstrap::jobs::tracker_apis::start_job;
167+
use crate::core::authentication::handler::KeysHandler;
168+
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
169+
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
170+
use crate::core::authentication::service;
152171
use crate::core::services::{initialize_database, initialize_tracker, initialize_whitelist_manager, statistics};
172+
use crate::core::whitelist;
153173
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
154-
use crate::core::{authentication, whitelist};
155174
use crate::servers::apis::Version;
156175
use crate::servers::registar::Registar;
157176
use crate::servers::udp::server::banning::BanService;
@@ -176,15 +195,22 @@ mod tests {
176195
&in_memory_whitelist.clone(),
177196
));
178197
let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone());
179-
let authentication = Arc::new(authentication::Facade::new(&cfg.core, &database.clone()));
198+
let db_key_repository = Arc::new(DatabaseKeyRepository::new(&database));
199+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
200+
let _authentication_service = Arc::new(service::AuthenticationService::new(&cfg.core, &in_memory_key_repository));
201+
let keys_handler = Arc::new(KeysHandler::new(
202+
&db_key_repository.clone(),
203+
&in_memory_key_repository.clone(),
204+
));
180205

181-
let tracker = Arc::new(initialize_tracker(&cfg, &database, &whitelist_authorization, &authentication));
206+
let tracker = Arc::new(initialize_tracker(&cfg, &database, &whitelist_authorization));
182207

183208
let version = Version::V1;
184209

185210
start_job(
186211
config,
187212
tracker,
213+
keys_handler,
188214
whitelist_manager,
189215
ban_service,
190216
stats_event_sender,

src/container.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ use std::sync::Arc;
22

33
use tokio::sync::RwLock;
44

5+
use crate::core::authentication::handler::KeysHandler;
6+
use crate::core::authentication::service::AuthenticationService;
57
use crate::core::statistics::event::sender::Sender;
68
use crate::core::statistics::repository::Repository;
79
use crate::core::whitelist::manager::WhiteListManager;
8-
use crate::core::{authentication, whitelist, Tracker};
10+
use crate::core::{whitelist, Tracker};
911
use crate::servers::udp::server::banning::BanService;
1012

1113
pub struct AppContainer {
1214
pub tracker: Arc<Tracker>,
15+
pub keys_handler: Arc<KeysHandler>,
16+
pub authentication_service: Arc<AuthenticationService>,
1317
pub whitelist_authorization: Arc<whitelist::authorization::Authorization>,
1418
pub ban_service: Arc<RwLock<BanService>>,
1519
pub stats_event_sender: Arc<Option<Box<dyn Sender>>>,
1620
pub stats_repository: Arc<Repository>,
1721
pub whitelist_manager: Arc<WhiteListManager>,
18-
pub authentication: Arc<authentication::Facade>,
1922
}

0 commit comments

Comments
 (0)