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
This commit is contained in:
Ana Hobden 2023-04-11 07:44:49 -07:00 committed by GitHub
parent 9549d793cc
commit 4fc7a0db18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 9 deletions

View file

@ -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));
}
},

View file

@ -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: _,

View file

@ -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: _,