From 4fc7a0db18965beeeac71790845d6733968bad5c Mon Sep 17 00:00:00 2001 From: Ana Hobden Date: Tue, 11 Apr 2023 07:44:49 -0700 Subject: [PATCH] Check user group commands exist during plan (#411) * Check user/group related commands exist prior to execution/revert * Fixup * Don't check for executables on mac * fmt --- src/action/base/add_user_to_group.rs | 23 +++++++++++++++++++++-- src/action/base/create_group.rs | 22 ++++++++++++++++++---- src/action/base/create_user.rs | 20 +++++++++++++++++--- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/action/base/add_user_to_group.rs b/src/action/base/add_user_to_group.rs index 9ec2b6b..bf88b37 100644 --- a/src/action/base/add_user_to_group.rs +++ b/src/action/base/add_user_to_group.rs @@ -35,6 +35,21 @@ impl AddUserToGroup { groupname, gid, }; + + match OperatingSystem::host() { + OperatingSystem::MacOSX { .. } | OperatingSystem::Darwin => (), + _ => { + if !(which::which("addgroup").is_ok() || which::which("gpasswd").is_ok()) { + return Err(Self::error(ActionErrorKind::MissingAddUserToGroupCommand)); + } + if !(which::which("delgroup").is_ok() || which::which("gpasswd").is_ok()) { + return Err(Self::error( + ActionErrorKind::MissingRemoveUserFromGroupCommand, + )); + } + }, + } + // Ensure user does not exists if let Some(user) = User::from_name(name.as_str()) .map_err(|e| ActionErrorKind::GettingUserId(name.clone(), e)) @@ -57,7 +72,7 @@ impl AddUserToGroup { } // See if group membership needs to be done - match target_lexicon::OperatingSystem::host() { + match OperatingSystem::host() { OperatingSystem::MacOSX { major: _, minor: _, @@ -119,7 +134,11 @@ impl AddUserToGroup { let user_in_group = output_str.split(" ").any(|v| v == &this.groupname); if user_in_group { - tracing::debug!("Creating user `{}` already complete", this.name); + tracing::debug!( + "Adding user `{}` to group `{}` already complete", + this.name, + this.groupname + ); return Ok(StatefulAction::completed(this)); } }, diff --git a/src/action/base/create_group.rs b/src/action/base/create_group.rs index 25971e2..de21e1d 100644 --- a/src/action/base/create_group.rs +++ b/src/action/base/create_group.rs @@ -1,4 +1,5 @@ use nix::unistd::Group; +use target_lexicon::OperatingSystem; use tokio::process::Command; use tracing::{span, Span}; @@ -23,6 +24,19 @@ impl CreateGroup { name: name.clone(), gid, }; + + match OperatingSystem::host() { + OperatingSystem::MacOSX { .. } | OperatingSystem::Darwin => (), + _ => { + if !(which::which("groupadd").is_ok() || which::which("addgroup").is_ok()) { + return Err(Self::error(ActionErrorKind::MissingGroupCreationCommand)); + } + if !(which::which("groupdel").is_ok() || which::which("delgroup").is_ok()) { + return Err(Self::error(ActionErrorKind::MissingGroupDeletionCommand)); + } + }, + } + // Ensure group does not exists if let Some(group) = Group::from_name(name.as_str()) .map_err(|e| ActionErrorKind::GettingGroupId(name.clone(), e)) @@ -75,8 +89,8 @@ impl Action for CreateGroup { async fn execute(&mut self) -> Result<(), ActionError> { let Self { name, gid } = self; - use target_lexicon::OperatingSystem; - match target_lexicon::OperatingSystem::host() { + use OperatingSystem; + match OperatingSystem::host() { OperatingSystem::MacOSX { major: _, minor: _, @@ -142,8 +156,8 @@ impl Action for CreateGroup { async fn revert(&mut self) -> Result<(), ActionError> { let Self { name, gid: _ } = self; - use target_lexicon::OperatingSystem; - match target_lexicon::OperatingSystem::host() { + use OperatingSystem; + match OperatingSystem::host() { OperatingSystem::MacOSX { major: _, minor: _, diff --git a/src/action/base/create_user.rs b/src/action/base/create_user.rs index 548b4e6..a1637ad 100644 --- a/src/action/base/create_user.rs +++ b/src/action/base/create_user.rs @@ -1,4 +1,5 @@ use nix::unistd::User; +use target_lexicon::OperatingSystem; use tokio::process::Command; use tracing::{span, Span}; @@ -35,6 +36,19 @@ impl CreateUser { gid, comment, }; + + match OperatingSystem::host() { + OperatingSystem::MacOSX { .. } | OperatingSystem::Darwin => (), + _ => { + if !(which::which("useradd").is_ok() || which::which("adduser").is_ok()) { + return Err(Self::error(ActionErrorKind::MissingUserCreationCommand)); + } + if !(which::which("userdel").is_ok() || which::which("deluser").is_ok()) { + return Err(Self::error(ActionErrorKind::MissingUserDeletionCommand)); + } + }, + } + // Ensure user does not exists if let Some(user) = User::from_name(name.as_str()) .map_err(|e| ActionErrorKind::GettingUserId(name.clone(), e)) @@ -107,7 +121,7 @@ impl Action for CreateUser { comment, } = self; - use target_lexicon::OperatingSystem; + use OperatingSystem; match OperatingSystem::host() { OperatingSystem::MacOSX { major: _, @@ -262,8 +276,8 @@ impl Action for CreateUser { #[tracing::instrument(level = "debug", skip_all)] async fn revert(&mut self) -> Result<(), ActionError> { - use target_lexicon::OperatingSystem; - match target_lexicon::OperatingSystem::host() { + use OperatingSystem; + match OperatingSystem::host() { OperatingSystem::MacOSX { major: _, minor: _,