|
| 1 | +use crate::utils::Worker; |
| 2 | +use anyhow::Result; |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | +use tracing::info; |
| 5 | +use which::which; |
| 6 | + |
| 7 | +const PACKAGE: &str = "sudo-rs"; |
| 8 | +const FIRST_SUPPORTED_RELEASE: &str = "24.04"; |
| 9 | + |
| 10 | +/// An experiment to install and configure sudo-rs as a replacement for sudo. |
| 11 | +pub struct SudoRsExperiment<'a> { |
| 12 | + system: &'a dyn Worker, |
| 13 | +} |
| 14 | + |
| 15 | +impl<'a> SudoRsExperiment<'a> { |
| 16 | + /// Create a new SudoRsExperiment. |
| 17 | + pub fn new(system: &'a dyn Worker) -> Self { |
| 18 | + Self { system } |
| 19 | + } |
| 20 | + |
| 21 | + /// Check if the system is compatible with the experiment. |
| 22 | + pub fn check_compatible(&self) -> bool { |
| 23 | + self.system.distribution().release.as_str() >= FIRST_SUPPORTED_RELEASE |
| 24 | + } |
| 25 | + |
| 26 | + /// Reports the first supported release for the experiment. |
| 27 | + pub fn first_supported_release(&self) -> &str { |
| 28 | + FIRST_SUPPORTED_RELEASE |
| 29 | + } |
| 30 | + |
| 31 | + /// Check if the package is installed. |
| 32 | + pub fn check_installed(&self) -> bool { |
| 33 | + self.system.check_installed(PACKAGE).unwrap_or(false) |
| 34 | + } |
| 35 | + |
| 36 | + /// Report the name of the experiment. |
| 37 | + pub fn name(&self) -> String { |
| 38 | + String::from("sudo-rs") |
| 39 | + } |
| 40 | + |
| 41 | + /// Enable the experiment by installing and configuring the package. |
| 42 | + pub fn enable(&self) -> Result<()> { |
| 43 | + info!("Installing and configuring {}", PACKAGE); |
| 44 | + self.system.install_package(PACKAGE)?; |
| 45 | + |
| 46 | + for f in Self::sudors_files() { |
| 47 | + let filename = f.file_name().unwrap(); |
| 48 | + let existing = match which(filename) { |
| 49 | + Ok(path) => path, |
| 50 | + Err(_) => Path::new("/usr/bin").join(filename), |
| 51 | + }; |
| 52 | + self.system.replace_file_with_symlink(f, existing)?; |
| 53 | + } |
| 54 | + |
| 55 | + Ok(()) |
| 56 | + } |
| 57 | + |
| 58 | + /// Disable the experiment by removing the package and restoring the original files. |
| 59 | + pub fn disable(&self) -> Result<()> { |
| 60 | + info!("Removing {}", PACKAGE); |
| 61 | + self.system.remove_package(PACKAGE)?; |
| 62 | + |
| 63 | + for f in Self::sudors_files() { |
| 64 | + let filename = f.file_name().unwrap(); |
| 65 | + let existing = match which(filename) { |
| 66 | + Ok(path) => path, |
| 67 | + Err(_) => Path::new("/usr/bin").join(filename), |
| 68 | + }; |
| 69 | + self.system.restore_file(existing.clone())?; |
| 70 | + } |
| 71 | + |
| 72 | + Ok(()) |
| 73 | + } |
| 74 | + |
| 75 | + /// List of files from the package to replace system equivalents with. |
| 76 | + fn sudors_files() -> Vec<PathBuf> { |
| 77 | + vec![ |
| 78 | + PathBuf::from("/usr/lib/cargo/bin/su"), |
| 79 | + PathBuf::from("/usr/lib/cargo/bin/sudo"), |
| 80 | + PathBuf::from("/usr/lib/cargo/bin/visudo"), |
| 81 | + ] |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + use super::*; |
| 88 | + use crate::utils::{Distribution, MockSystem}; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_sudors_incompatible_distribution() { |
| 92 | + let runner = incompatible_runner(); |
| 93 | + let coreutils = sudors_fixture(&runner); |
| 94 | + assert!(!coreutils.check_compatible()); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_sudors_install_success() { |
| 99 | + let runner = sudors_compatible_runner(); |
| 100 | + let sudors = sudors_fixture(&runner); |
| 101 | + |
| 102 | + assert!(sudors.enable().is_ok()); |
| 103 | + |
| 104 | + let commands = runner.commands.clone().into_inner(); |
| 105 | + assert_eq!(commands, &["apt-get install -y sudo-rs"]); |
| 106 | + |
| 107 | + let backed_up_files = runner.backed_up_files.clone().into_inner(); |
| 108 | + let expected = ["/usr/bin/sudo", "/usr/bin/su", "/usr/sbin/visudo"]; |
| 109 | + |
| 110 | + assert_eq!(backed_up_files.len(), 3); |
| 111 | + for f in backed_up_files.iter() { |
| 112 | + assert!(expected.contains(&f.as_str())); |
| 113 | + } |
| 114 | + |
| 115 | + let created_symlinks = runner.created_symlinks.clone().into_inner(); |
| 116 | + let expected = [ |
| 117 | + ("/usr/lib/cargo/bin/su", "/usr/bin/su"), |
| 118 | + ("/usr/lib/cargo/bin/sudo", "/usr/bin/sudo"), |
| 119 | + ("/usr/lib/cargo/bin/visudo", "/usr/sbin/visudo"), |
| 120 | + ]; |
| 121 | + |
| 122 | + assert_eq!(created_symlinks.len(), 3); |
| 123 | + for (from, to) in created_symlinks.iter() { |
| 124 | + assert!(expected.contains(&(from.as_str(), to.as_str()))); |
| 125 | + } |
| 126 | + |
| 127 | + assert_eq!(runner.restored_files.clone().into_inner().len(), 0); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_sudors_restore() { |
| 132 | + let runner = sudors_compatible_runner(); |
| 133 | + runner.mock_install_package("sudo-rs"); |
| 134 | + |
| 135 | + let sudors = sudors_fixture(&runner); |
| 136 | + assert!(sudors.disable().is_ok()); |
| 137 | + |
| 138 | + assert_eq!(runner.created_symlinks.clone().into_inner().len(), 0); |
| 139 | + assert_eq!(runner.backed_up_files.clone().into_inner().len(), 0); |
| 140 | + |
| 141 | + let commands = runner.commands.clone().into_inner(); |
| 142 | + assert_eq!(commands.len(), 1); |
| 143 | + assert!(commands.contains(&"apt-get remove -y sudo-rs".to_string())); |
| 144 | + |
| 145 | + let restored_files = runner.restored_files.clone().into_inner(); |
| 146 | + let expected = ["/usr/bin/sudo", "/usr/bin/su", "/usr/sbin/visudo"]; |
| 147 | + |
| 148 | + assert_eq!(restored_files.len(), 3); |
| 149 | + for f in restored_files.iter() { |
| 150 | + assert!(expected.contains(&f.as_str())); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + fn sudors_fixture(system: &MockSystem) -> SudoRsExperiment { |
| 155 | + SudoRsExperiment::new(system) |
| 156 | + } |
| 157 | + |
| 158 | + fn sudors_compatible_runner() -> MockSystem { |
| 159 | + let runner = MockSystem::default(); |
| 160 | + runner.mock_files(vec![ |
| 161 | + ("/usr/lib/cargo/bin/sudo", ""), |
| 162 | + ("/usr/lib/cargo/bin/su", ""), |
| 163 | + ("/usr/lib/cargo/bin/visudo", ""), |
| 164 | + ("/usr/bin/sudo", ""), |
| 165 | + ("/usr/bin/su", ""), |
| 166 | + ("/usr/sbin/visudo", ""), |
| 167 | + ]); |
| 168 | + runner |
| 169 | + } |
| 170 | + |
| 171 | + fn incompatible_runner() -> MockSystem { |
| 172 | + MockSystem::new(Distribution { |
| 173 | + id: "Ubuntu".to_string(), |
| 174 | + release: "20.04".to_string(), |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
0 commit comments