Skip to content

Commit

Permalink
fix: lookup user error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Hoehl committed May 27, 2022
1 parent e247184 commit ab0374e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
3 changes: 2 additions & 1 deletion pkg/util/command_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ func LookupUser(userStr string) (*user.User, error) {
if err != nil {
uid, err := getUID(userStr)
if err != nil {
return nil, err
// at this point, the user does not exist and the userStr is not a valid number.
return nil, fmt.Errorf("user %v is not a uid and does not exist on the system", userStr)
}
userObj = &user.User{
Uid: fmt.Sprint(uid),
Expand Down
67 changes: 59 additions & 8 deletions pkg/util/command_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os/user"
"reflect"
"sort"
"strconv"
"testing"

"github.com/GoogleContainerTools/kaniko/testutil"
Expand Down Expand Up @@ -678,9 +679,11 @@ func Test_GetUIDAndGIDFromString(t *testing.T) {
groupID uint32
}

currentUserUid, _ := strconv.ParseUint(currentUser.Uid, 10, 32)
currentUserGid, _ := strconv.ParseUint(currentUser.Gid, 10, 32)
expectedCurrentUser := expected{
userID: currentUser.UID,
groupID: currentUser.GID,
userID: uint32(currentUserUid),
groupID: uint32(currentUserGid),
}

testCases := []struct {
Expand All @@ -692,14 +695,14 @@ func Test_GetUIDAndGIDFromString(t *testing.T) {
{
testname: "current user uid and gid",
args: args{
userGroupStr: fmt.Sprintf("%d:%d", currentUser.UID, currentUser.GID),
userGroupStr: fmt.Sprintf("%d:%d", currentUserUid, currentUserGid),
},
expected: expectedCurrentUser,
},
{
testname: "current user username and gid",
args: args{
userGroupStr: fmt.Sprintf("%s:%d", currentUser.Username, currentUser.GID),
userGroupStr: fmt.Sprintf("%s:%d", currentUser.Username, currentUserGid),
},
expected: expectedCurrentUser,
},
Expand All @@ -713,7 +716,7 @@ func Test_GetUIDAndGIDFromString(t *testing.T) {
{
testname: "current user uid and primary group",
args: args{
userGroupStr: fmt.Sprintf("%d:%s", currentUser.UID, currentUser.PrimaryGroup),
userGroupStr: fmt.Sprintf("%d:%s", currentUserUid, currentUser.PrimaryGroup),
},
expected: expectedCurrentUser,
},
Expand All @@ -734,7 +737,7 @@ func Test_GetUIDAndGIDFromString(t *testing.T) {
},
expected: expected{
userID: 1001,
groupID: uint32(currentUser.GID),
groupID: uint32(currentUserGid),
},
},
{
Expand Down Expand Up @@ -768,15 +771,15 @@ func Test_GetUIDAndGIDFromString(t *testing.T) {
{
testname: "only uid and fallback is false",
args: args{
userGroupStr: fmt.Sprintf("%d", currentUser.UID),
userGroupStr: fmt.Sprintf("%d", currentUserUid),
fallbackToUID: false,
},
wantErr: true,
},
{
testname: "only uid and fallback is true",
args: args{
userGroupStr: fmt.Sprintf("%d", currentUser.UID),
userGroupStr: fmt.Sprintf("%d", currentUserUid),
fallbackToUID: true,
},
expected: expected{
Expand Down Expand Up @@ -804,3 +807,51 @@ func Test_GetUIDAndGIDFromString(t *testing.T) {
}
}
}

func TestLookupUser(t *testing.T) {
currentUser := testutil.GetCurrentUser(t)

type args struct {
userStr string
}
tests := []struct {
testname string
args args
expected *user.User
wantErr bool
}{
{
testname: "non-existing user",
args: args{
userStr: "foobazbar",
},
wantErr: true,
},
{
testname: "uid",
args: args{
userStr: "30000",
},
expected: &user.User{
Uid: "30000",
HomeDir: "/",
},
wantErr: false,
},
{
testname: "current user",
args: args{
userStr: currentUser.Username,
},
expected: currentUser.User,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.testname, func(t *testing.T) {
got, err := LookupUser(tt.args.userStr)
testutil.CheckErrorAndDeepEqual(t, tt.wantErr, err, tt.expected, got)
})
}

}
14 changes: 10 additions & 4 deletions pkg/util/syscall_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package util

import (
"fmt"
"strconv"
"syscall"
"testing"

Expand All @@ -26,6 +27,11 @@ import (

func TestSyscallCredentials(t *testing.T) {
currentUser := testutil.GetCurrentUser(t)
uid, _ := strconv.ParseUint(currentUser.Uid, 10, 32)
currentUserUid32 := uint32(uid)
gid, _ := strconv.ParseUint(currentUser.Gid, 10, 32)
currentUserGid32 := uint32(gid)

type args struct {
userStr string
}
Expand Down Expand Up @@ -57,11 +63,11 @@ func TestSyscallCredentials(t *testing.T) {
{
name: "non-existing uid with existing gid",
args: args{
userStr: fmt.Sprintf("1001:%d", currentUser.GID),
userStr: fmt.Sprintf("1001:%d", currentUserGid32),
},
want: &syscall.Credential{
Uid: 1001,
Gid: currentUser.GID,
Gid: currentUserGid32,
Groups: []uint32{},
},
},
Expand All @@ -71,9 +77,9 @@ func TestSyscallCredentials(t *testing.T) {
userStr: fmt.Sprintf("%s:50000", currentUser.Username),
},
want: &syscall.Credential{
Uid: currentUser.UID,
Uid: currentUserUid32,
Gid: 50000,
Groups: []uint32{currentUser.GID},
Groups: []uint32{currentUserGid32},
},
},
}
Expand Down
12 changes: 3 additions & 9 deletions testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"os/user"
"path/filepath"
"reflect"
"strconv"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -44,9 +43,8 @@ func SetupFiles(path string, files map[string]string) error {
}

type CurrentUser struct {
Username string
UID uint32
GID uint32
*user.User

PrimaryGroup string
}

Expand All @@ -64,14 +62,10 @@ func GetCurrentUser(t *testing.T) CurrentUser {
t.Fatalf("Could not lookup name of group %s: %s", groups[0], err)
}
primaryGroup := primaryGroupObj.Name
uid, _ := strconv.ParseUint(currentUser.Uid, 10, 32)
gid, _ := strconv.ParseUint(currentUser.Gid, 10, 32)

return CurrentUser{
UID: uint32(uid),
GID: uint32(gid),
User: currentUser,
PrimaryGroup: primaryGroup,
Username: currentUser.Username,
}
}

Expand Down

0 comments on commit ab0374e

Please sign in to comment.