|
| 1 | +//! Validated configuration for the Tracker API service. |
| 2 | +//! |
| 3 | +//! [``crate::HttpApi``] is a DTO containing the parsed data from the toml |
| 4 | +//! file. |
| 5 | +//! |
| 6 | +//! This configuration is a first level of validation that can be perform |
| 7 | +//! statically without running the service. |
| 8 | +//! |
| 9 | +//! For example, if SSL is enabled you must provide the certificate path. That |
| 10 | +//! can be validated. However, this validation does not check if the |
| 11 | +//! certificate is valid. |
| 12 | +use serde::{Deserialize, Serialize}; |
| 13 | +use thiserror::Error; |
1 | 14 |
|
| 15 | +use crate::{AccessTokens, HttpApi}; |
| 16 | + |
| 17 | +/// Errors that can occur when validating the plain configuration. |
| 18 | +#[derive(Error, Debug, PartialEq)] |
| 19 | +pub enum ValidationError { |
| 20 | + /// Missing SSL cert path. |
| 21 | + #[error("missing SSL cert path")] |
| 22 | + MissingSslCertPath, |
| 23 | + /// Missing SSL key path. |
| 24 | + #[error("missing SSL key path")] |
| 25 | + MissingSslKeyPath, |
| 26 | +} |
| 27 | + |
| 28 | +/// Configuration for each HTTP tracker. |
| 29 | +#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] |
| 30 | +pub struct Config { |
| 31 | + enabled: bool, |
| 32 | + bind_address: String, // todo: use SocketAddr |
| 33 | + ssl_enabled: bool, |
| 34 | + ssl_cert_path: Option<String>, // todo: use Path |
| 35 | + ssl_key_path: Option<String>, // todo: use Path |
| 36 | + access_tokens: AccessTokens, |
| 37 | +} |
| 38 | + |
| 39 | +impl Config { |
| 40 | + #[must_use] |
| 41 | + pub fn is_enabled(&self) -> bool { |
| 42 | + self.enabled |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl TryFrom<HttpApi> for Config { |
| 47 | + type Error = ValidationError; |
| 48 | + |
| 49 | + fn try_from(config: HttpApi) -> Result<Self, Self::Error> { |
| 50 | + if config.ssl_enabled { |
| 51 | + match config.ssl_cert_path.clone() { |
| 52 | + Some(ssl_cert_path) => { |
| 53 | + if ssl_cert_path.is_empty() { |
| 54 | + Err(ValidationError::MissingSslCertPath) |
| 55 | + } else { |
| 56 | + Ok(()) |
| 57 | + } |
| 58 | + } |
| 59 | + None => Err(ValidationError::MissingSslCertPath), |
| 60 | + }?; |
| 61 | + |
| 62 | + match config.ssl_key_path.clone() { |
| 63 | + Some(ssl_key_path) => { |
| 64 | + if ssl_key_path.is_empty() { |
| 65 | + Err(ValidationError::MissingSslKeyPath) |
| 66 | + } else { |
| 67 | + Ok(()) |
| 68 | + } |
| 69 | + } |
| 70 | + None => Err(ValidationError::MissingSslKeyPath), |
| 71 | + }?; |
| 72 | + } |
| 73 | + |
| 74 | + Ok(Self { |
| 75 | + enabled: config.enabled, |
| 76 | + bind_address: config.bind_address, |
| 77 | + ssl_enabled: config.ssl_enabled, |
| 78 | + ssl_cert_path: config.ssl_cert_path, |
| 79 | + ssl_key_path: config.ssl_key_path, |
| 80 | + access_tokens: config.access_tokens, |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl From<Config> for HttpApi { |
| 86 | + fn from(config: Config) -> Self { |
| 87 | + Self { |
| 88 | + enabled: config.enabled, |
| 89 | + bind_address: config.bind_address, |
| 90 | + ssl_enabled: config.ssl_enabled, |
| 91 | + ssl_cert_path: config.ssl_cert_path, |
| 92 | + ssl_key_path: config.ssl_key_path, |
| 93 | + access_tokens: config.access_tokens, |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + |
| 101 | + mod when_ssl_is_enabled { |
| 102 | + use std::collections::HashMap; |
| 103 | + |
| 104 | + use crate::tracker_api::{Config, ValidationError}; |
| 105 | + use crate::HttpApi; |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn it_should_return_an_error_when_ssl_is_enabled_but_the_cert_path_is_not_provided() { |
| 109 | + let plain_config = HttpApi { |
| 110 | + enabled: true, |
| 111 | + bind_address: "127.0.0.1:1212".to_string(), |
| 112 | + ssl_enabled: true, |
| 113 | + ssl_cert_path: None, |
| 114 | + ssl_key_path: Some("./localhost.key".to_string()), |
| 115 | + access_tokens: HashMap::new(), |
| 116 | + }; |
| 117 | + |
| 118 | + assert_eq!(Config::try_from(plain_config), Err(ValidationError::MissingSslCertPath)); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn it_should_return_an_error_when_ssl_is_enabled_but_the_cert_path_is_empty() { |
| 123 | + let plain_config = HttpApi { |
| 124 | + enabled: true, |
| 125 | + bind_address: "127.0.0.1:1212".to_string(), |
| 126 | + ssl_enabled: true, |
| 127 | + ssl_cert_path: Some(String::new()), |
| 128 | + ssl_key_path: Some("./localhost.key".to_string()), |
| 129 | + access_tokens: HashMap::new(), |
| 130 | + }; |
| 131 | + |
| 132 | + assert_eq!(Config::try_from(plain_config), Err(ValidationError::MissingSslCertPath)); |
| 133 | + } |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn it_should_return_an_error_when_ssl_is_enabled_but_the_key_path_is_not_provided() { |
| 137 | + let plain_config = HttpApi { |
| 138 | + enabled: true, |
| 139 | + bind_address: "127.0.0.1:1212".to_string(), |
| 140 | + ssl_enabled: true, |
| 141 | + ssl_cert_path: Some("./localhost.crt".to_string()), |
| 142 | + ssl_key_path: None, |
| 143 | + access_tokens: HashMap::new(), |
| 144 | + }; |
| 145 | + |
| 146 | + assert_eq!(Config::try_from(plain_config), Err(ValidationError::MissingSslKeyPath)); |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn it_should_return_an_error_when_ssl_is_enabled_but_the_key_path_is_empty() { |
| 151 | + let plain_config = HttpApi { |
| 152 | + enabled: true, |
| 153 | + bind_address: "127.0.0.1:1212".to_string(), |
| 154 | + ssl_enabled: true, |
| 155 | + ssl_cert_path: Some("./localhost.crt".to_string()), |
| 156 | + ssl_key_path: Some(String::new()), |
| 157 | + access_tokens: HashMap::new(), |
| 158 | + }; |
| 159 | + |
| 160 | + assert_eq!(Config::try_from(plain_config), Err(ValidationError::MissingSslKeyPath)); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments