Avoid returning the UID when resolving the GIDs. (#2689)

* Fix GetUIDAndGIDFromString test.

* Avoid returning by default the UID when resolving the GIDs.
This commit is contained in:
Diego Gonzalez 2023-09-01 03:05:27 +02:00 committed by GitHub
parent 237778c179
commit 2b6b5948da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 31 deletions

View File

@ -387,41 +387,48 @@ func getUIDAndGID(userStr string, groupStr string, fallbackToUID bool) (uint32,
return 0, 0, err return 0, 0, err
} }
gid, err := getGIDFromName(groupStr, fallbackToUID) if groupStr != "" {
if err != nil { gid32, err := getGIDFromName(groupStr)
if errors.Is(err, fallbackToUIDError) { if err != nil {
return uid32, uid32, nil if errors.Is(err, fallbackToUIDError) {
return uid32, uid32, nil
}
return 0, 0, err
} }
return 0, 0, err return uid32, gid32, nil
} }
return uid32, gid, nil
if fallbackToUID {
return uid32, uid32, nil
}
return uid32, 0, nil
} }
// getGID tries to parse the gid or falls back to getGroupFromName if it's not an id // getGID tries to parse the gid
func getGID(groupStr string, fallbackToUID bool) (uint32, error) { func getGID(groupStr string) (uint32, error) {
gid, err := strconv.ParseUint(groupStr, 10, 32) gid, err := strconv.ParseUint(groupStr, 10, 32)
if err != nil { if err != nil {
return 0, fallbackToUIDOrError(err, fallbackToUID) return 0, err
} }
return uint32(gid), nil return uint32(gid), nil
} }
// getGIDFromName tries to parse the groupStr into an existing group. // getGIDFromName tries to parse the groupStr into an existing group.
// if the group doesn't exist, fallback to getGID to parse non-existing valid GIDs. func getGIDFromName(groupStr string) (uint32, error) {
func getGIDFromName(groupStr string, fallbackToUID bool) (uint32, error) {
group, err := user.LookupGroup(groupStr) group, err := user.LookupGroup(groupStr)
if err != nil { if err != nil {
// unknown group error could relate to a non existing group // unknown group error could relate to a non existing group
var groupErr *user.UnknownGroupError var groupErr user.UnknownGroupError
if errors.Is(err, groupErr) { if errors.As(err, &groupErr) {
return getGID(groupStr, fallbackToUID) return getGID(groupStr)
} }
group, err = user.LookupGroupId(groupStr) group, err = user.LookupGroupId(groupStr)
if err != nil { if err != nil {
return getGID(groupStr, fallbackToUID) return getGID(groupStr)
} }
} }
return getGID(group.Gid, fallbackToUID) return getGID(group.Gid)
} }
var fallbackToUIDError = new(fallbackToUIDErrorType) var fallbackToUIDError = new(fallbackToUIDErrorType)
@ -432,13 +439,6 @@ func (e fallbackToUIDErrorType) Error() string {
return "fallback to uid" return "fallback to uid"
} }
func fallbackToUIDOrError(err error, fallbackToUID bool) error {
if fallbackToUID {
return fallbackToUIDError
}
return err
}
// LookupUser will try to lookup the userStr inside the passwd file. // LookupUser will try to lookup the userStr inside the passwd file.
// If the user does not exists, the function will fallback to parsing the userStr as an uid. // If the user does not exists, the function will fallback to parsing the userStr as an uid.
func LookupUser(userStr string) (*user.User, error) { func LookupUser(userStr string) (*user.User, error) {

View File

@ -705,7 +705,7 @@ func Test_GetUIDAndGIDFromString(t *testing.T) {
}, },
expected: expected{ expected: expected{
userID: 1001, userID: 1001,
groupID: uint32(currentUserGID), groupID: expectedCurrentUser.groupID,
}, },
}, },
{ {
@ -714,15 +714,13 @@ func Test_GetUIDAndGIDFromString(t *testing.T) {
userGroupStr: fmt.Sprintf("%d:%s", 1001, "hello-world-group"), userGroupStr: fmt.Sprintf("%d:%s", 1001, "hello-world-group"),
fallbackToUID: true, fallbackToUID: true,
}, },
expected: expected{ wantErr: true,
userID: 1001,
groupID: 1001,
},
}, },
{ {
testname: "uid and non existing group-name", testname: "uid and non existing group-name without fallbackToUID",
args: args{ args: args{
userGroupStr: fmt.Sprintf("%d:%s", 1001, "hello-world-group"), userGroupStr: fmt.Sprintf("%d:%s", 1001, "hello-world-group"),
fallbackToUID: false,
}, },
wantErr: true, wantErr: true,
}, },
@ -742,7 +740,10 @@ func Test_GetUIDAndGIDFromString(t *testing.T) {
userGroupStr: fmt.Sprintf("%d", currentUserUID), userGroupStr: fmt.Sprintf("%d", currentUserUID),
fallbackToUID: false, fallbackToUID: false,
}, },
wantErr: true, expected: expected{
userID: expectedCurrentUser.userID,
groupID: 0,
},
}, },
{ {
testname: "only uid and fallback is true", testname: "only uid and fallback is true",

View File

@ -19,6 +19,7 @@ package util
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
"syscall" "syscall"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -54,6 +55,12 @@ func SyscallCredentials(userStr string) (*syscall.Credential, error) {
groups = append(groups, uint32(i)) groups = append(groups, uint32(i))
} }
if !(len(strings.Split(userStr, ":")) > 1) {
if u.Gid != "" {
gid, _ = getGID(u.Gid)
}
}
return &syscall.Credential{ return &syscall.Credential{
Uid: uid, Uid: uid,
Gid: gid, Gid: gid,