|
| 1 | +//! UFW firewall configuration step |
| 2 | +//! |
| 3 | +//! This module provides the `ConfigureFirewallStep` which handles configuration |
| 4 | +//! of UFW (Uncomplicated Firewall) on remote hosts via Ansible playbooks. |
| 5 | +//! This step ensures that the firewall is configured with restrictive default |
| 6 | +//! policies while maintaining SSH access to prevent lockout. |
| 7 | +//! |
| 8 | +//! ## Key Features |
| 9 | +//! |
| 10 | +//! - Configures UFW with restrictive default policies (deny incoming, allow outgoing) |
| 11 | +//! - Preserves SSH access on the configured port |
| 12 | +//! - Uses Tera template for dynamic SSH port resolution |
| 13 | +//! - Comprehensive SSH lockout prevention measures |
| 14 | +//! - Verification steps to ensure firewall is active and SSH is accessible |
| 15 | +//! |
| 16 | +//! ## Configuration Process |
| 17 | +//! |
| 18 | +//! The step executes the "configure-firewall" Ansible playbook which handles: |
| 19 | +//! - UFW installation and setup |
| 20 | +//! - Reset UFW to clean state |
| 21 | +//! - Set restrictive default policies |
| 22 | +//! - Allow SSH access BEFORE enabling firewall (critical for preventing lockout) |
| 23 | +//! - Enable UFW firewall |
| 24 | +//! - Verify firewall status and SSH access |
| 25 | +//! |
| 26 | +//! ## SSH Lockout Prevention |
| 27 | +//! |
| 28 | +//! This is a **high-risk operation** that could result in SSH lockout if not |
| 29 | +//! handled correctly. Safety measures include: |
| 30 | +//! |
| 31 | +//! 1. **Correct Sequencing**: SSH rules are added BEFORE enabling firewall |
| 32 | +//! 2. **Dual SSH Protection**: Both port-specific and service-name rules |
| 33 | +//! 3. **Port Configuration**: Uses actual SSH port from user configuration |
| 34 | +//! 4. **Verification Steps**: Ansible tasks verify SSH access is preserved |
| 35 | +//! 5. **Comprehensive Logging**: Detailed logging of each firewall step |
| 36 | +
|
| 37 | +use std::sync::Arc; |
| 38 | +use tracing::{info, instrument, warn}; |
| 39 | + |
| 40 | +use crate::adapters::ansible::AnsibleClient; |
| 41 | +use crate::shared::command::CommandError; |
| 42 | + |
| 43 | +/// Step that configures UFW firewall on a remote host via Ansible |
| 44 | +/// |
| 45 | +/// This step configures a restrictive UFW firewall policy while ensuring |
| 46 | +/// SSH access is maintained. The SSH port is resolved during template rendering |
| 47 | +/// and embedded in the final Ansible playbook. The configuration follows the |
| 48 | +/// principle of "allow SSH BEFORE enabling firewall" to prevent lockout. |
| 49 | +pub struct ConfigureFirewallStep { |
| 50 | + ansible_client: Arc<AnsibleClient>, |
| 51 | +} |
| 52 | + |
| 53 | +impl ConfigureFirewallStep { |
| 54 | + /// Create a new firewall configuration step |
| 55 | + /// |
| 56 | + /// # Arguments |
| 57 | + /// |
| 58 | + /// * `ansible_client` - Ansible client for running playbooks |
| 59 | + /// |
| 60 | + /// # Note |
| 61 | + /// |
| 62 | + /// SSH port configuration is resolved during template rendering phase, |
| 63 | + /// not at step execution time. The rendered playbook contains the |
| 64 | + /// resolved SSH port value. |
| 65 | + #[must_use] |
| 66 | + pub fn new(ansible_client: Arc<AnsibleClient>) -> Self { |
| 67 | + Self { ansible_client } |
| 68 | + } |
| 69 | + |
| 70 | + /// Execute the firewall configuration |
| 71 | + /// |
| 72 | + /// # Safety |
| 73 | + /// |
| 74 | + /// This method is designed to prevent SSH lockout by: |
| 75 | + /// 1. Resetting UFW to clean state |
| 76 | + /// 2. Allowing SSH access BEFORE enabling firewall |
| 77 | + /// 3. Using the correct SSH port from user configuration |
| 78 | + /// |
| 79 | + /// The SSH port is resolved during template rendering and embedded in the |
| 80 | + /// playbook, so this method executes a playbook with pre-configured values. |
| 81 | + /// |
| 82 | + /// # Errors |
| 83 | + /// |
| 84 | + /// Returns `CommandError` if: |
| 85 | + /// - Ansible playbook execution fails |
| 86 | + /// - UFW commands fail |
| 87 | + /// - SSH rules cannot be applied |
| 88 | + /// - Firewall verification fails |
| 89 | + #[instrument( |
| 90 | + name = "configure_firewall", |
| 91 | + skip_all, |
| 92 | + fields(step_type = "system", component = "firewall", method = "ansible") |
| 93 | + )] |
| 94 | + pub fn execute(&self) -> Result<(), CommandError> { |
| 95 | + warn!( |
| 96 | + step = "configure_firewall", |
| 97 | + action = "configure_ufw", |
| 98 | + "Configuring UFW firewall - CRITICAL: SSH access will be restricted to configured port" |
| 99 | + ); |
| 100 | + |
| 101 | + // Run Ansible playbook (SSH port already resolved during template rendering) |
| 102 | + match self.ansible_client.run_playbook("configure-firewall") { |
| 103 | + Ok(_) => { |
| 104 | + info!( |
| 105 | + step = "configure_firewall", |
| 106 | + status = "success", |
| 107 | + "UFW firewall configured successfully with SSH access preserved" |
| 108 | + ); |
| 109 | + Ok(()) |
| 110 | + } |
| 111 | + Err(e) => { |
| 112 | + // Propagate errors to the caller. Tests that run in container environments |
| 113 | + // should explicitly opt-out of running this step (for example via an |
| 114 | + // environment variable) instead of relying on runtime error detection. |
| 115 | + Err(e) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +#[cfg(test)] |
| 122 | +mod tests { |
| 123 | + use std::path::PathBuf; |
| 124 | + use std::sync::Arc; |
| 125 | + |
| 126 | + use super::*; |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn it_should_create_configure_firewall_step() { |
| 130 | + let ansible_client = Arc::new(AnsibleClient::new(PathBuf::from("test_inventory.yml"))); |
| 131 | + let step = ConfigureFirewallStep::new(ansible_client); |
| 132 | + |
| 133 | + // Test that the step can be created successfully |
| 134 | + assert_eq!( |
| 135 | + std::mem::size_of_val(&step), |
| 136 | + std::mem::size_of::<Arc<AnsibleClient>>() |
| 137 | + ); |
| 138 | + } |
| 139 | +} |
0 commit comments