|
| 1 | +//! Automatic security updates configuration step |
| 2 | +//! |
| 3 | +//! This module provides the `ConfigureSecurityUpdatesStep` which handles |
| 4 | +//! configuration of automatic security updates on remote hosts via Ansible playbooks. |
| 5 | +//! This step ensures that the system automatically receives and installs security |
| 6 | +//! patches with scheduled reboots. |
| 7 | +//! |
| 8 | +//! ## Key Features |
| 9 | +//! |
| 10 | +//! - Installs and configures unattended-upgrades package |
| 11 | +//! - Enables automatic security update installation |
| 12 | +//! - Configures automatic reboots at 2:00 AM when updates require restart |
| 13 | +//! - Verifies configuration is valid and service is running |
| 14 | +//! - Integration with the step-based deployment architecture |
| 15 | +//! |
| 16 | +//! ## Configuration Process |
| 17 | +//! |
| 18 | +//! The step executes the "configure-security-updates" Ansible playbook which handles: |
| 19 | +//! - Package installation (unattended-upgrades) |
| 20 | +//! - Automatic update configuration |
| 21 | +//! - Reboot scheduling for security updates |
| 22 | +//! - Service enablement and startup |
| 23 | +//! - Configuration verification |
| 24 | +
|
| 25 | +use std::sync::Arc; |
| 26 | +use tracing::{info, instrument}; |
| 27 | + |
| 28 | +use crate::adapters::ansible::AnsibleClient; |
| 29 | +use crate::shared::command::CommandError; |
| 30 | + |
| 31 | +/// Step that configures automatic security updates on a remote host via Ansible |
| 32 | +pub struct ConfigureSecurityUpdatesStep { |
| 33 | + ansible_client: Arc<AnsibleClient>, |
| 34 | +} |
| 35 | + |
| 36 | +impl ConfigureSecurityUpdatesStep { |
| 37 | + #[must_use] |
| 38 | + pub fn new(ansible_client: Arc<AnsibleClient>) -> Self { |
| 39 | + Self { ansible_client } |
| 40 | + } |
| 41 | + |
| 42 | + /// Execute the security updates configuration step |
| 43 | + /// |
| 44 | + /// This will run the "configure-security-updates" Ansible playbook to configure |
| 45 | + /// unattended-upgrades on the remote host. The playbook handles package installation, |
| 46 | + /// automatic update configuration, and scheduled reboot setup. |
| 47 | + /// |
| 48 | + /// # Errors |
| 49 | + /// |
| 50 | + /// Returns an error if: |
| 51 | + /// * The Ansible client fails to execute the playbook |
| 52 | + /// * Package installation fails |
| 53 | + /// * Configuration file modification fails |
| 54 | + /// * Service startup fails |
| 55 | + /// * Configuration verification fails |
| 56 | + /// * The playbook execution fails for any other reason |
| 57 | + #[instrument( |
| 58 | + name = "configure_security_updates", |
| 59 | + skip_all, |
| 60 | + fields( |
| 61 | + step_type = "system", |
| 62 | + component = "security_updates", |
| 63 | + method = "ansible" |
| 64 | + ) |
| 65 | + )] |
| 66 | + pub fn execute(&self) -> Result<(), CommandError> { |
| 67 | + info!( |
| 68 | + step = "configure_security_updates", |
| 69 | + action = "configure_automatic_updates", |
| 70 | + "Configuring automatic security updates via Ansible" |
| 71 | + ); |
| 72 | + |
| 73 | + self.ansible_client |
| 74 | + .run_playbook("configure-security-updates")?; |
| 75 | + |
| 76 | + info!( |
| 77 | + step = "configure_security_updates", |
| 78 | + status = "success", |
| 79 | + "Automatic security updates configuration completed" |
| 80 | + ); |
| 81 | + |
| 82 | + Ok(()) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use std::path::PathBuf; |
| 89 | + use std::sync::Arc; |
| 90 | + |
| 91 | + use super::*; |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn it_should_create_configure_security_updates_step() { |
| 95 | + let ansible_client = Arc::new(AnsibleClient::new(PathBuf::from("test_inventory.yml"))); |
| 96 | + let step = ConfigureSecurityUpdatesStep::new(ansible_client); |
| 97 | + |
| 98 | + // Test that the step can be created successfully |
| 99 | + assert_eq!( |
| 100 | + std::mem::size_of_val(&step), |
| 101 | + std::mem::size_of::<Arc<AnsibleClient>>() |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments