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 29, 2024
1 parent ce82959 commit 996c9a2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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, "creating group %q", group)
}

// isUnknownGroupError returns whether the error from LookupGroup is an unknown group error.
Expand Down
10 changes: 10 additions & 0 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 @@ -56,6 +57,15 @@ func GroupAdd(groupname string, gid string) (exitCode int, err error) {
if cmd.ProcessState.ExitCode() == GroupExistExit {
return cmd.ProcessState.ExitCode(), trace.AlreadyExists("group already exists")
}

if cmd.ProcessState.ExitCode() == GroupInvalidArg {
errMsg := "bad parameter"
if strings.Contains(string(output), "not a valid group name") {
errMsg = "invalid group name"
}
return cmd.ProcessState.ExitCode(), trace.BadParameter(errMsg)
}

return cmd.ProcessState.ExitCode(), trace.Wrap(err)
}

Expand Down

0 comments on commit 996c9a2

Please sign in to comment.