-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.rs
More file actions
269 lines (237 loc) · 8.13 KB
/
Copy pathhandler.rs
File metadata and controls
269 lines (237 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! Validate Command Handler
//!
//! This module contains the application layer handler for the validate command.
//! It validates environment configuration files without creating deployments.
use std::convert::TryInto;
use std::fs;
use std::path::Path;
use crate::application::command_handlers::create::config::EnvironmentCreationConfig;
use crate::domain::environment::EnvironmentParams;
use super::errors::ValidateCommandHandlerError;
/// Application layer handler for validate command
///
/// This handler validates environment configuration files by:
/// 1. Parsing the JSON structure
/// 2. Validating field types and values
/// 3. Verifying referenced files exist (SSH keys)
/// 4. Checking domain constraints
pub struct ValidateCommandHandler;
impl ValidateCommandHandler {
/// Create a new validate command handler
#[must_use]
pub fn new() -> Self {
Self
}
/// Validate an environment configuration file
///
/// This method performs comprehensive validation:
/// - JSON syntax and structure
/// - Field types and constraints
/// - SSH key file existence
/// - Domain business rules
///
/// # Arguments
///
/// * `config_path` - Path to the configuration file to validate
///
/// # Returns
///
/// * `Ok(ValidationResult)` - Configuration is valid with details
/// * `Err(ValidateCommandHandlerError)` - Validation failed with specific reason
///
/// # Errors
///
/// Returns an error if:
/// - File cannot be read
/// - JSON syntax is invalid
/// - Required fields are missing
/// - Field values violate constraints
/// - Referenced SSH keys don't exist
/// - Domain rules are violated
///
/// # Examples
///
/// ```rust,no_run
/// use std::path::Path;
/// use torrust_tracker_deployer_lib::application::command_handlers::validate::ValidateCommandHandler;
///
/// let handler = ValidateCommandHandler::new();
/// let result = handler.validate(Path::new("envs/my-env.json"))?;
///
/// println!("Configuration is valid!");
/// println!("Environment name: {}", result.environment_name);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn validate(
&self,
config_path: &Path,
) -> Result<ValidationResult, ValidateCommandHandlerError> {
// Step 1: Read file contents
let content = fs::read_to_string(config_path).map_err(|source| {
ValidateCommandHandlerError::FileReadFailed {
path: config_path.to_path_buf(),
source,
}
})?;
// Step 2: Parse JSON to EnvironmentCreationConfig
// This validates JSON syntax and maps to our structure
let config: EnvironmentCreationConfig =
serde_json::from_str(&content).map_err(|source| {
ValidateCommandHandlerError::JsonParsingFailed {
path: config_path.to_path_buf(),
source,
}
})?;
// Step 3: Convert to domain types (validates all constraints)
// This includes:
// - SSH key paths must be absolute (file existence checked at runtime)
// - Port numbers must be valid
// - Domain names must be well-formed
// - All business rules must pass
let _validated_params: EnvironmentParams = config
.clone()
.try_into()
.map_err(ValidateCommandHandlerError::DomainValidationFailed)?;
// All validation passed!
Ok(ValidationResult {
environment_name: config.environment.name.clone(),
provider: config.provider.provider().to_string(),
has_prometheus: config.prometheus.is_some(),
has_grafana: config.grafana.is_some(),
has_https: config.https.is_some(),
has_backup: config.backup.is_some(),
})
}
}
impl Default for ValidateCommandHandler {
fn default() -> Self {
Self::new()
}
}
/// Result of successful validation
///
/// Contains key information about the validated configuration
/// for user feedback.
#[allow(clippy::struct_excessive_bools)] // Intentional: presentation data with feature flags
#[derive(Debug, Clone)]
pub struct ValidationResult {
/// Name of the environment
pub environment_name: String,
/// Provider type (lxd or hetzner)
pub provider: String,
/// Whether Prometheus is configured
pub has_prometheus: bool,
/// Whether Grafana is configured
pub has_grafana: bool,
/// Whether HTTPS is configured
pub has_https: bool,
/// Whether backups are configured
pub has_backup: bool,
}
#[cfg(test)]
mod tests {
use super::*;
use std::env;
use std::fs;
use tempfile::TempDir;
#[test]
fn it_should_validate_valid_configuration_when_all_fields_are_correct() {
let handler = ValidateCommandHandler::new();
// Create temp directory for test config
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let config_path = temp_dir.path().join("test-config.json");
// Get absolute paths to test fixtures
let project_root = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
let private_key_path = format!("{project_root}/fixtures/testing_rsa");
let public_key_path = format!("{project_root}/fixtures/testing_rsa.pub");
// Create test config with absolute paths
let config_json = format!(
r#"{{
"environment": {{
"name": "test-validation"
}},
"ssh_credentials": {{
"private_key_path": "{private_key_path}",
"public_key_path": "{public_key_path}",
"username": "torrust",
"port": 22
}},
"provider": {{
"provider": "lxd",
"profile_name": "test-profile"
}},
"tracker": {{
"core": {{
"database": {{
"driver": "sqlite3",
"database_name": "tracker.db"
}},
"private": false
}},
"udp_trackers": [
{{
"bind_address": "0.0.0.0:6969",
"domain": "udp.tracker.local"
}}
],
"http_trackers": [
{{
"bind_address": "0.0.0.0:7070",
"domain": "http.tracker.local"
}}
],
"http_api": {{
"bind_address": "0.0.0.0:1212",
"admin_token": "MyAccessToken",
"domain": "api.tracker.local"
}},
"health_check_api": {{
"bind_address": "0.0.0.0:1313",
"domain": "health.tracker.local"
}}
}},
"grafana": {{
"admin_user": "admin",
"admin_password": "admin-password",
"domain": "grafana.tracker.local"
}},
"prometheus": {{
"scrape_interval_in_secs": 15
}}
}}"#
);
fs::write(&config_path, config_json).expect("Failed to write test config");
// Run validation
let result = handler.validate(&config_path);
assert!(result.is_ok(), "Valid configuration should pass validation");
}
#[test]
fn it_should_return_error_when_file_does_not_exist() {
let handler = ValidateCommandHandler::new();
let result = handler.validate(Path::new("/tmp/nonexistent.json"));
assert!(
matches!(
result,
Err(ValidateCommandHandlerError::FileReadFailed { .. })
),
"Non-existent file should return FileReadFailed error"
);
}
#[test]
fn it_should_return_error_when_json_is_malformed() {
let handler = ValidateCommandHandler::new();
// Create a temporary file with invalid JSON
let temp_file = std::env::temp_dir().join("invalid.json");
fs::write(&temp_file, "{ invalid json }").unwrap();
let result = handler.validate(&temp_file);
// Cleanup
drop(fs::remove_file(temp_file));
assert!(
matches!(
result,
Err(ValidateCommandHandlerError::JsonParsingFailed { .. })
),
"Malformed JSON should return JsonParsingFailed error"
);
}
}