From 969fffff1ada107e0ccba9ea55cd06944213be6a Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 15 May 2024 18:28:32 +0100 Subject: [PATCH] feat: [#426] add TSL info to the [net] section in the config toml file ```toml [net] port = 3001 [net.tsl] ssl_cert_path = "./storage/index/lib/tls/localhost.crt" ssl_key_path = "./storage/index/lib/tls/localhost.key" ``` ```json { "net": { "port": 3001, "tsl": { "ssl_cert_path": "./storage/index/lib/tls/localhost.crt", "ssl_key_path": "./storage/index/lib/tls/localhost.key" } } } ``` The TSL configuration is optional, but if you have that table (dict), it must contain the fields. This is an invalid configuration: ``` [net.tsl] ssl_cert_path = "" ssl_key_path = "" ``` See https://github.com/torrust/torrust-tracker/discussions/853. --- .../config/index.development.sqlite3.toml | 22 ++++++++------ src/config.rs | 30 +++++++++++++++++++ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/share/default/config/index.development.sqlite3.toml b/share/default/config/index.development.sqlite3.toml index 669979af..0fffbced 100644 --- a/share/default/config/index.development.sqlite3.toml +++ b/share/default/config/index.development.sqlite3.toml @@ -4,19 +4,23 @@ log_level = "info" name = "Torrust" [tracker] -url = "udp://localhost:6969" -mode = "Public" api_url = "http://localhost:1212" +mode = "Public" token = "MyAccessToken" token_valid_seconds = 7257600 +url = "udp://localhost:6969" [net] port = 3001 +#[net.tsl] +#ssl_cert_path = "./storage/index/lib/tls/localhost.crt" +#ssl_key_path = "./storage/index/lib/tls/localhost.key" + [auth] email_on_signup = "Optional" -min_password_length = 6 max_password_length = 64 +min_password_length = 6 secret_key = "MaxVerstappenWC2021" [database] @@ -25,23 +29,23 @@ connect_url = "sqlite://data.db?mode=rwc" [mail] email_verification_enabled = false from = "example@email.com" -reply_to = "noreply@email.com" -username = "" password = "" -server = "" port = 25 +reply_to = "noreply@email.com" +server = "" +username = "" [image_cache] -max_request_timeout_ms = 1000 capacity = 128000000 entry_size_limit = 4000000 -user_quota_period_seconds = 3600 +max_request_timeout_ms = 1000 user_quota_bytes = 64000000 +user_quota_period_seconds = 3600 [api] default_torrent_page_size = 10 max_torrent_page_size = 30 [tracker_statistics_importer] +port = 3002 torrent_info_update_interval = 3600 -port = 3002 \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 6df6e7fe..eb7b3915 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,9 +3,11 @@ use std::path::Path; use std::sync::Arc; use std::{env, fs}; +use camino::Utf8PathBuf; use config::{Config, ConfigError, File, FileFormat}; use log::warn; use serde::{Deserialize, Serialize}; +use serde_with::{serde_as, NoneAsEmptyString}; use thiserror::Error; use tokio::sync::RwLock; use torrust_index_located_error::{Located, LocatedError}; @@ -216,6 +218,8 @@ pub struct Network { /// The base URL for the API. For example: `http://localhost`. /// If not set, the base URL will be inferred from the request. pub base_url: Option, + /// TSL configuration. + pub tsl: Option, } impl Default for Network { @@ -223,6 +227,7 @@ impl Default for Network { Self { port: 3001, base_url: None, + tsl: None, } } } @@ -394,6 +399,31 @@ impl Default for ImageCache { } } +#[serde_as] +#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Default)] +pub struct Tsl { + /// Path to the SSL certificate file. + #[serde_as(as = "NoneAsEmptyString")] + #[serde(default = "Tsl::default_ssl_cert_path")] + pub ssl_cert_path: Option, + /// Path to the SSL key file. + #[serde_as(as = "NoneAsEmptyString")] + #[serde(default = "Tsl::default_ssl_key_path")] + pub ssl_key_path: Option, +} + +impl Tsl { + #[allow(clippy::unnecessary_wraps)] + fn default_ssl_cert_path() -> Option { + Some(Utf8PathBuf::new()) + } + + #[allow(clippy::unnecessary_wraps)] + fn default_ssl_key_path() -> Option { + Some(Utf8PathBuf::new()) + } +} + /// The whole configuration for the index. #[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct TorrustIndex {