Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v14] Return better error during group creation with invalid group name #46082

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/srv/usermgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,13 @@ func (u *HostUserManagement) createGroupIfNotExist(group string) error {
if err != nil && !isUnknownGroupError(err, group) {
return trace.Wrap(err)
}

err = u.backend.CreateGroup(group, "")
if trace.IsAlreadyExists(err) {
return nil
}
return trace.Wrap(err)

return trace.Wrap(err, "%q", group)
}

// isUnknownGroupError returns whether the error from LookupGroup is an unknown group error.
Expand Down
16 changes: 13 additions & 3 deletions lib/utils/host/hostusers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

// man GROUPADD(8), exit codes section
const GroupExistExit = 9
const GroupInvalidArg = 3

// man USERADD(8), exit codes section
const UserExistExit = 9
Expand All @@ -49,10 +50,19 @@ func GroupAdd(groupname string, gid string) (exitCode int, err error) {
cmd := exec.Command(groupaddBin, args...)
output, err := cmd.CombinedOutput()
log.Debugf("%s output: %s", cmd.Path, string(output))
if cmd.ProcessState.ExitCode() == GroupExistExit {
return cmd.ProcessState.ExitCode(), trace.AlreadyExists("group already exists")

switch code := cmd.ProcessState.ExitCode(); code {
case GroupExistExit:
return code, trace.AlreadyExists("group already exists")
case GroupInvalidArg:
errMsg := "bad parameter"
if strings.Contains(string(output), "not a valid group name") {
errMsg = "invalid group name"
}
return code, trace.BadParameter(errMsg)
default:
return code, trace.Wrap(err)
}
return cmd.ProcessState.ExitCode(), trace.Wrap(err)
}

// UserAdd creates a user on a host using `useradd`
Expand Down
Loading