Skip to content

Commit

Permalink
returns trace.BadParameter error when adding group with invalid name
Browse files Browse the repository at this point in the history
  • Loading branch information
eriktate committed Aug 30, 2024
1 parent ce82959 commit 711bae1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/srv/usermgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,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 @@ -32,6 +32,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 @@ -53,10 +54,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

0 comments on commit 711bae1

Please sign in to comment.