fix user metadata string

This commit is contained in:
Tejal Desai 2020-03-04 13:08:38 -08:00
parent 04db3834c6
commit c1e3f42625
4 changed files with 30 additions and 20 deletions

View File

@ -0,0 +1,20 @@
# 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
RUN touch ~/hello

View File

@ -71,7 +71,7 @@ func (r *RunCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
var userStr string var userStr string
// If specified, run the command as a specific user // If specified, run the command as a specific user
if config.User != "" { if config.User != "" {
uid, gid, err := util.GetUIDAndGIDFromString(config.User, false) uid, gid, err := util.GetUIDAndGIDFromString(config.User, true)
if err != nil { if err != nil {
return err return err
} }

View File

@ -47,16 +47,15 @@ func (r *UserCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
if err != nil { if err != nil {
return errors.Wrap(err, fmt.Sprintf("resolving user %s", userAndGroup[0])) return errors.Wrap(err, fmt.Sprintf("resolving user %s", userAndGroup[0]))
} }
var groupStr = setGroupDefault(userStr)
if len(userAndGroup) > 1 { if len(userAndGroup) > 1 {
groupStr, err = util.ResolveEnvironmentReplacement(userAndGroup[1], replacementEnvs, false) groupStr, err := util.ResolveEnvironmentReplacement(userAndGroup[1], replacementEnvs, false)
if err != nil { if err != nil {
return errors.Wrap(err, fmt.Sprintf("resolving group %s", userAndGroup[1])) return errors.Wrap(err, fmt.Sprintf("resolving group %s", userAndGroup[1]))
} }
userStr = userStr + ":" + groupStr
} }
userStr = userStr + ":" + groupStr
config.User = userStr config.User = userStr
return nil return nil
} }
@ -64,12 +63,3 @@ func (r *UserCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
func (r *UserCommand) String() string { func (r *UserCommand) String() string {
return r.cmd.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
}

View File

@ -37,22 +37,22 @@ var userTests = []struct {
{ {
user: "root", user: "root",
userObj: &user.User{Uid: "root", Gid: "root"}, userObj: &user.User{Uid: "root", Gid: "root"},
expectedUID: "root:root", expectedUID: "root",
}, },
{ {
user: "root-add", user: "root-add",
userObj: &user.User{Uid: "root-add", Gid: "root"}, userObj: &user.User{Uid: "root-add", Gid: "root"},
expectedUID: "root-add:root", expectedUID: "root-add",
}, },
{ {
user: "0", user: "0",
userObj: &user.User{Uid: "0", Gid: "0"}, userObj: &user.User{Uid: "0", Gid: "0"},
expectedUID: "0:0", expectedUID: "0",
}, },
{ {
user: "fakeUser", user: "fakeUser",
userObj: &user.User{Uid: "fakeUser", Gid: "fakeUser"}, userObj: &user.User{Uid: "fakeUser", Gid: "fakeUser"},
expectedUID: "fakeUser:fakeUser", expectedUID: "fakeUser",
}, },
{ {
user: "root:root", user: "root:root",
@ -78,7 +78,7 @@ var userTests = []struct {
{ {
user: "$envuser", user: "$envuser",
userObj: &user.User{Uid: "root", Gid: "root"}, userObj: &user.User{Uid: "root", Gid: "root"},
expectedUID: "root:root", expectedUID: "root",
}, },
{ {
user: "root:$envgroup", user: "root:$envgroup",
@ -92,7 +92,7 @@ var userTests = []struct {
}, },
{ {
user: "some", user: "some",
expectedUID: "some:", expectedUID: "some",
}, },
} }