Merge pull request #1433 from Random-Liu/fix-with-user-id

WithUserID should not return error when /etc/passwd doesn't exist.
This commit is contained in:
Michael Crosby 2017-08-28 10:10:41 -04:00 committed by GitHub
commit b9879d46f4

View File

@ -297,8 +297,9 @@ func WithUidGid(uid, gid uint32) SpecOpts {
} }
// WithUserID sets the correct UID and GID for the container based // WithUserID sets the correct UID and GID for the container based
// on the image's /etc/passwd contents. If uid is not found in // on the image's /etc/passwd contents. If /etc/passwd does not exist,
// /etc/passwd, it sets uid but leaves gid 0, and not returns error. // or uid is not found in /etc/passwd, it sets gid to be the same with
// uid, and not returns error.
func WithUserID(uid uint32) SpecOpts { func WithUserID(uid uint32) SpecOpts {
return func(ctx context.Context, client *Client, c *containers.Container, s *specs.Spec) error { return func(ctx context.Context, client *Client, c *containers.Container, s *specs.Spec) error {
if c.Snapshotter == "" { if c.Snapshotter == "" {
@ -329,6 +330,10 @@ func WithUserID(uid uint32) SpecOpts {
} }
f, err := os.Open(ppath) f, err := os.Open(ppath)
if err != nil { if err != nil {
if os.IsNotExist(err) {
s.Process.User.UID, s.Process.User.GID = uid, uid
return nil
}
return err return err
} }
defer f.Close() defer f.Close()
@ -339,7 +344,7 @@ func WithUserID(uid uint32) SpecOpts {
return err return err
} }
if len(users) == 0 { if len(users) == 0 {
s.Process.User.UID = uid s.Process.User.UID, s.Process.User.GID = uid, uid
return nil return nil
} }
u := users[0] u := users[0]
@ -349,8 +354,9 @@ func WithUserID(uid uint32) SpecOpts {
} }
// WithUsername sets the correct UID and GID for the container // WithUsername sets the correct UID and GID for the container
// based on the the image's /etc/passwd contents. If the username // based on the the image's /etc/passwd contents. If /etc/passwd
// is not found in /etc/passwd, it returns error. // does not exist, or the username is not found in /etc/passwd,
// it returns error.
func WithUsername(username string) SpecOpts { func WithUsername(username string) SpecOpts {
return func(ctx context.Context, client *Client, c *containers.Container, s *specs.Spec) error { return func(ctx context.Context, client *Client, c *containers.Container, s *specs.Spec) error {
if c.Snapshotter == "" { if c.Snapshotter == "" {