From 2a1b29a9f1e9fde062326e878ce4f6431338f8ba Mon Sep 17 00:00:00 2001 From: Vishal Khot <55139240+vishal-khot@users.noreply.github.com> Date: Tue, 3 Oct 2023 06:06:15 +0530 Subject: [PATCH] Remove fallbackToUID bool option from Kaniko code (#2767) --- pkg/util/command_util.go | 16 ++++--------- pkg/util/command_util_test.go | 42 ++++++++------------------------- pkg/util/syscall_credentials.go | 2 +- 3 files changed, 16 insertions(+), 44 deletions(-) diff --git a/pkg/util/command_util.go b/pkg/util/command_util.go index f71459171..1e84c9bdc 100644 --- a/pkg/util/command_util.go +++ b/pkg/util/command_util.go @@ -355,7 +355,7 @@ func GetUserGroup(chownStr string, env []string) (int64, int64, error) { return -1, -1, err } - uid32, gid32, err := getUIDAndGIDFromString(chown, true) + uid32, gid32, err := getUIDAndGIDFromString(chown) if err != nil { return -1, -1, err } @@ -364,20 +364,18 @@ func GetUserGroup(chownStr string, env []string) (int64, int64, error) { } // Extract user and group id from a string formatted 'user:group'. -// If fallbackToUID is set, the gid is equal to uid if the group is not specified -// otherwise gid is set to zero. // UserID and GroupID don't need to be present on the system. -func getUIDAndGIDFromString(userGroupString string, fallbackToUID bool) (uint32, uint32, error) { +func getUIDAndGIDFromString(userGroupString string) (uint32, uint32, error) { userAndGroup := strings.Split(userGroupString, ":") userStr := userAndGroup[0] var groupStr string if len(userAndGroup) > 1 { groupStr = userAndGroup[1] } - return getUIDAndGIDFunc(userStr, groupStr, fallbackToUID) + return getUIDAndGIDFunc(userStr, groupStr) } -func getUIDAndGID(userStr string, groupStr string, fallbackToUID bool) (uint32, uint32, error) { +func getUIDAndGID(userStr string, groupStr string) (uint32, uint32, error) { user, err := LookupUser(userStr) if err != nil { return 0, 0, err @@ -398,11 +396,7 @@ func getUIDAndGID(userStr string, groupStr string, fallbackToUID bool) (uint32, return uid32, gid32, nil } - if fallbackToUID { - return uid32, uid32, nil - } - - return uid32, 0, nil + return uid32, uid32, nil } // getGID tries to parse the gid diff --git a/pkg/util/command_util_test.go b/pkg/util/command_util_test.go index f811f5138..19915cfd7 100644 --- a/pkg/util/command_util_test.go +++ b/pkg/util/command_util_test.go @@ -526,7 +526,7 @@ func TestGetUserGroup(t *testing.T) { description string chown string env []string - mockIDGetter func(userStr string, groupStr string, fallbackToUID bool) (uint32, uint32, error) + mockIDGetter func(userStr string, groupStr string) (uint32, uint32, error) // needed, in case uid is a valid number, but group is a name mockGroupIDGetter func(groupStr string) (*user.Group, error) expectedU int64 @@ -537,7 +537,7 @@ func TestGetUserGroup(t *testing.T) { description: "non empty chown", chown: "some:some", env: []string{}, - mockIDGetter: func(string, string, bool) (uint32, uint32, error) { + mockIDGetter: func(string, string) (uint32, uint32, error) { return 100, 1000, nil }, expectedU: 100, @@ -547,7 +547,7 @@ func TestGetUserGroup(t *testing.T) { description: "non empty chown with env replacement", chown: "some:$foo", env: []string{"foo=key"}, - mockIDGetter: func(userStr string, groupStr string, fallbackToUID bool) (uint32, uint32, error) { + mockIDGetter: func(userStr string, groupStr string) (uint32, uint32, error) { if userStr == "some" && groupStr == "key" { return 10, 100, nil } @@ -558,7 +558,7 @@ func TestGetUserGroup(t *testing.T) { }, { description: "empty chown string", - mockIDGetter: func(string, string, bool) (uint32, uint32, error) { + mockIDGetter: func(string, string) (uint32, uint32, error) { return 0, 0, fmt.Errorf("should not be called") }, expectedU: -1, @@ -638,8 +638,7 @@ func Test_GetUIDAndGIDFromString(t *testing.T) { currentUser := testutil.GetCurrentUser(t) type args struct { - userGroupStr string - fallbackToUID bool + userGroupStr string } type expected struct { @@ -709,18 +708,9 @@ func Test_GetUIDAndGIDFromString(t *testing.T) { }, }, { - testname: "uid and non existing group-name with fallbackToUID", + testname: "uid and non existing group-name", args: args{ - userGroupStr: fmt.Sprintf("%d:%s", 1001, "hello-world-group"), - fallbackToUID: true, - }, - wantErr: true, - }, - { - testname: "uid and non existing group-name without fallbackToUID", - args: args{ - userGroupStr: fmt.Sprintf("%d:%s", 1001, "hello-world-group"), - fallbackToUID: false, + userGroupStr: fmt.Sprintf("%d:%s", 1001, "hello-world-group"), }, wantErr: true, }, @@ -735,21 +725,9 @@ func Test_GetUIDAndGIDFromString(t *testing.T) { }, }, { - testname: "only uid and fallback is false", + testname: "only uid", args: args{ - userGroupStr: fmt.Sprintf("%d", currentUserUID), - fallbackToUID: false, - }, - expected: expected{ - userID: expectedCurrentUser.userID, - groupID: 0, - }, - }, - { - testname: "only uid and fallback is true", - args: args{ - userGroupStr: fmt.Sprintf("%d", currentUserUID), - fallbackToUID: true, + userGroupStr: fmt.Sprintf("%d", currentUserUID), }, expected: expected{ userID: expectedCurrentUser.userID, @@ -765,7 +743,7 @@ func Test_GetUIDAndGIDFromString(t *testing.T) { }, } for _, tt := range testCases { - uid, gid, err := getUIDAndGIDFromString(tt.args.userGroupStr, tt.args.fallbackToUID) + uid, gid, err := getUIDAndGIDFromString(tt.args.userGroupStr) testutil.CheckError(t, tt.wantErr, err) if uid != tt.expected.userID || gid != tt.expected.groupID { t.Errorf("%v failed. Could not correctly decode %s to uid/gid %d:%d. Result: %d:%d", diff --git a/pkg/util/syscall_credentials.go b/pkg/util/syscall_credentials.go index 2177abd52..d6cb9e75a 100644 --- a/pkg/util/syscall_credentials.go +++ b/pkg/util/syscall_credentials.go @@ -27,7 +27,7 @@ import ( ) func SyscallCredentials(userStr string) (*syscall.Credential, error) { - uid, gid, err := getUIDAndGIDFromString(userStr, true) + uid, gid, err := getUIDAndGIDFromString(userStr) if err != nil { return nil, errors.Wrap(err, "get uid/gid") }