Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix user metadata set to USER:GROUP if group string is not set #1105

Merged
merged 2 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions integration/dockerfiles/Dockerfile_test_user_without_grp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2018 Google, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM debian:9.11
RUN groupadd testgroup && \
useradd --create-home --gid testgroup alice

USER alice
2 changes: 1 addition & 1 deletion pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (r *RunCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
var userStr string
// If specified, run the command as a specific user
if config.User != "" {
uid, gid, err := util.GetUIDAndGIDFromString(config.User, false)
uid, gid, err := util.GetUIDAndGIDFromString(config.User, true)
if err != nil {
return err
}
Expand Down
16 changes: 3 additions & 13 deletions pkg/commands/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,19 @@ func (r *UserCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
if err != nil {
return errors.Wrap(err, fmt.Sprintf("resolving user %s", userAndGroup[0]))
}
var groupStr = setGroupDefault(userStr)

if len(userAndGroup) > 1 {
groupStr, err = util.ResolveEnvironmentReplacement(userAndGroup[1], replacementEnvs, false)
groupStr, err := util.ResolveEnvironmentReplacement(userAndGroup[1], replacementEnvs, false)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("resolving group %s", userAndGroup[1]))
}
userStr = userStr + ":" + groupStr
}

userStr = userStr + ":" + groupStr

config.User = userStr
return nil
}

func (r *UserCommand) String() string {
return r.cmd.String()
}

func setGroupDefault(userStr string) string {
userObj, err := Lookup(userStr)
if err != nil {
logrus.Debugf("could not lookup user %s. Setting group empty", userStr)
return ""
}
return userObj.Gid
}
12 changes: 6 additions & 6 deletions pkg/commands/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ var userTests = []struct {
{
user: "root",
userObj: &user.User{Uid: "root", Gid: "root"},
expectedUID: "root:root",
expectedUID: "root",
},
{
user: "root-add",
userObj: &user.User{Uid: "root-add", Gid: "root"},
expectedUID: "root-add:root",
expectedUID: "root-add",
},
{
user: "0",
userObj: &user.User{Uid: "0", Gid: "0"},
expectedUID: "0:0",
expectedUID: "0",
},
{
user: "fakeUser",
userObj: &user.User{Uid: "fakeUser", Gid: "fakeUser"},
expectedUID: "fakeUser:fakeUser",
expectedUID: "fakeUser",
},
{
user: "root:root",
Expand All @@ -78,7 +78,7 @@ var userTests = []struct {
{
user: "$envuser",
userObj: &user.User{Uid: "root", Gid: "root"},
expectedUID: "root:root",
expectedUID: "root",
},
{
user: "root:$envgroup",
Expand All @@ -92,7 +92,7 @@ var userTests = []struct {
},
{
user: "some",
expectedUID: "some:",
expectedUID: "some",
},
}

Expand Down