Browse code

Merge pull request #34263 from estesp/chown-flag-add-copy

Add --chown flag to ADD/COPY commands

Tõnis Tiigi authored on 2017/08/29 01:50:44
Showing 7 changed files
... ...
@@ -56,6 +56,7 @@ type copyInstruction struct {
56 56
 	cmdName                 string
57 57
 	infos                   []copyInfo
58 58
 	dest                    string
59
+	chownStr                string
59 60
 	allowLocalDecompression bool
60 61
 }
61 62
 
... ...
@@ -369,6 +370,7 @@ func downloadSource(output io.Writer, stdout io.Writer, srcURL string) (remote b
369 369
 type copyFileOptions struct {
370 370
 	decompress bool
371 371
 	archiver   *archive.Archiver
372
+	chownPair  idtools.IDPair
372 373
 }
373 374
 
374 375
 func performCopyForInfo(dest copyInfo, source copyInfo, options copyFileOptions) error {
... ...
@@ -388,7 +390,7 @@ func performCopyForInfo(dest copyInfo, source copyInfo, options copyFileOptions)
388 388
 		return errors.Wrapf(err, "source path not found")
389 389
 	}
390 390
 	if src.IsDir() {
391
-		return copyDirectory(archiver, srcPath, destPath)
391
+		return copyDirectory(archiver, srcPath, destPath, options.chownPair)
392 392
 	}
393 393
 	if options.decompress && archive.IsArchivePath(srcPath) && !source.noDecompress {
394 394
 		return archiver.UntarPath(srcPath, destPath)
... ...
@@ -405,26 +407,28 @@ func performCopyForInfo(dest copyInfo, source copyInfo, options copyFileOptions)
405 405
 		// is a symlink
406 406
 		destPath = filepath.Join(destPath, filepath.Base(source.path))
407 407
 	}
408
-	return copyFile(archiver, srcPath, destPath)
408
+	return copyFile(archiver, srcPath, destPath, options.chownPair)
409 409
 }
410 410
 
411
-func copyDirectory(archiver *archive.Archiver, source, dest string) error {
411
+func copyDirectory(archiver *archive.Archiver, source, dest string, chownPair idtools.IDPair) error {
412
+	destExists, err := isExistingDirectory(dest)
413
+	if err != nil {
414
+		return errors.Wrapf(err, "failed to query destination path")
415
+	}
412 416
 	if err := archiver.CopyWithTar(source, dest); err != nil {
413 417
 		return errors.Wrapf(err, "failed to copy directory")
414 418
 	}
415
-	return fixPermissions(source, dest, archiver.IDMappings.RootPair())
419
+	return fixPermissions(source, dest, chownPair, !destExists)
416 420
 }
417 421
 
418
-func copyFile(archiver *archive.Archiver, source, dest string) error {
419
-	rootIDs := archiver.IDMappings.RootPair()
420
-
421
-	if err := idtools.MkdirAllAndChownNew(filepath.Dir(dest), 0755, rootIDs); err != nil {
422
+func copyFile(archiver *archive.Archiver, source, dest string, chownPair idtools.IDPair) error {
423
+	if err := idtools.MkdirAllAndChownNew(filepath.Dir(dest), 0755, chownPair); err != nil {
422 424
 		return errors.Wrapf(err, "failed to create new directory")
423 425
 	}
424 426
 	if err := archiver.CopyFileWithTar(source, dest); err != nil {
425 427
 		return errors.Wrapf(err, "failed to copy file")
426 428
 	}
427
-	return fixPermissions(source, dest, rootIDs)
429
+	return fixPermissions(source, dest, chownPair, false)
428 430
 }
429 431
 
430 432
 func endsInSlash(path string) bool {
... ...
@@ -9,10 +9,16 @@ import (
9 9
 	"github.com/docker/docker/pkg/idtools"
10 10
 )
11 11
 
12
-func fixPermissions(source, destination string, rootIDs idtools.IDPair) error {
13
-	skipChownRoot, err := isExistingDirectory(destination)
14
-	if err != nil {
15
-		return err
12
+func fixPermissions(source, destination string, rootIDs idtools.IDPair, overrideSkip bool) error {
13
+	var (
14
+		skipChownRoot bool
15
+		err           error
16
+	)
17
+	if !overrideSkip {
18
+		skipChownRoot, err = isExistingDirectory(destination)
19
+		if err != nil {
20
+			return err
21
+		}
16 22
 	}
17 23
 
18 24
 	// We Walk on the source rather than on the destination because we don't
... ...
@@ -2,7 +2,7 @@ package dockerfile
2 2
 
3 3
 import "github.com/docker/docker/pkg/idtools"
4 4
 
5
-func fixPermissions(source, destination string, rootIDs idtools.IDPair) error {
5
+func fixPermissions(source, destination string, rootIDs idtools.IDPair, overrideSkip bool) error {
6 6
 	// chown is not supported on Windows
7 7
 	return nil
8 8
 }
... ...
@@ -146,6 +146,7 @@ func add(req dispatchRequest) error {
146 146
 		return errAtLeastTwoArguments("ADD")
147 147
 	}
148 148
 
149
+	flChown := req.flags.AddString("chown", "")
149 150
 	if err := req.flags.Parse(); err != nil {
150 151
 		return err
151 152
 	}
... ...
@@ -157,6 +158,7 @@ func add(req dispatchRequest) error {
157 157
 	if err != nil {
158 158
 		return err
159 159
 	}
160
+	copyInstruction.chownStr = flChown.Value
160 161
 	copyInstruction.allowLocalDecompression = true
161 162
 
162 163
 	return req.builder.performCopy(req.state, copyInstruction)
... ...
@@ -172,6 +174,7 @@ func dispatchCopy(req dispatchRequest) error {
172 172
 	}
173 173
 
174 174
 	flFrom := req.flags.AddString("from", "")
175
+	flChown := req.flags.AddString("chown", "")
175 176
 	if err := req.flags.Parse(); err != nil {
176 177
 		return err
177 178
 	}
... ...
@@ -187,6 +190,7 @@ func dispatchCopy(req dispatchRequest) error {
187 187
 	if err != nil {
188 188
 		return err
189 189
 	}
190
+	copyInstruction.chownStr = flChown.Value
190 191
 
191 192
 	return req.builder.performCopy(req.state, copyInstruction)
192 193
 }
... ...
@@ -7,13 +7,18 @@ import (
7 7
 	"crypto/sha256"
8 8
 	"encoding/hex"
9 9
 	"fmt"
10
+	"path/filepath"
11
+	"strconv"
10 12
 	"strings"
11 13
 
12 14
 	"github.com/docker/docker/api/types"
13 15
 	"github.com/docker/docker/api/types/backend"
14 16
 	"github.com/docker/docker/api/types/container"
15 17
 	"github.com/docker/docker/image"
18
+	"github.com/docker/docker/pkg/idtools"
16 19
 	"github.com/docker/docker/pkg/stringid"
20
+	"github.com/docker/docker/pkg/symlink"
21
+	lcUser "github.com/opencontainers/runc/libcontainer/user"
17 22
 	"github.com/pkg/errors"
18 23
 )
19 24
 
... ...
@@ -107,10 +112,16 @@ func (b *Builder) exportImage(state *dispatchState, imageMount *imageMount, runC
107 107
 func (b *Builder) performCopy(state *dispatchState, inst copyInstruction) error {
108 108
 	srcHash := getSourceHashFromInfos(inst.infos)
109 109
 
110
+	var chownComment string
111
+	if inst.chownStr != "" {
112
+		chownComment = fmt.Sprintf("--chown=%s", inst.chownStr)
113
+	}
114
+	commentStr := fmt.Sprintf("%s %s%s in %s ", inst.cmdName, chownComment, srcHash, inst.dest)
115
+
110 116
 	// TODO: should this have been using origPaths instead of srcHash in the comment?
111 117
 	runConfigWithCommentCmd := copyRunConfig(
112 118
 		state.runConfig,
113
-		withCmdCommentString(fmt.Sprintf("%s %s in %s ", inst.cmdName, srcHash, inst.dest), b.platform))
119
+		withCmdCommentString(commentStr, b.platform))
114 120
 	hit, err := b.probeCache(state, runConfigWithCommentCmd)
115 121
 	if err != nil || hit {
116 122
 		return err
... ...
@@ -125,9 +136,21 @@ func (b *Builder) performCopy(state *dispatchState, inst copyInstruction) error
125 125
 		return err
126 126
 	}
127 127
 
128
+	chownPair := b.archiver.IDMappings.RootPair()
129
+	// if a chown was requested, perform the steps to get the uid, gid
130
+	// translated (if necessary because of user namespaces), and replace
131
+	// the root pair with the chown pair for copy operations
132
+	if inst.chownStr != "" {
133
+		chownPair, err = parseChownFlag(inst.chownStr, destInfo.root, b.archiver.IDMappings)
134
+		if err != nil {
135
+			return errors.Wrapf(err, "unable to convert uid/gid chown string to host mapping")
136
+		}
137
+	}
138
+
128 139
 	opts := copyFileOptions{
129 140
 		decompress: inst.allowLocalDecompression,
130 141
 		archiver:   b.archiver,
142
+		chownPair:  chownPair,
131 143
 	}
132 144
 	for _, info := range inst.infos {
133 145
 		if err := performCopyForInfo(destInfo, info, opts); err != nil {
... ...
@@ -137,6 +160,88 @@ func (b *Builder) performCopy(state *dispatchState, inst copyInstruction) error
137 137
 	return b.exportImage(state, imageMount, runConfigWithCommentCmd)
138 138
 }
139 139
 
140
+func parseChownFlag(chown, ctrRootPath string, idMappings *idtools.IDMappings) (idtools.IDPair, error) {
141
+	var userStr, grpStr string
142
+	parts := strings.Split(chown, ":")
143
+	if len(parts) > 2 {
144
+		return idtools.IDPair{}, errors.New("invalid chown string format: " + chown)
145
+	}
146
+	if len(parts) == 1 {
147
+		// if no group specified, use the user spec as group as well
148
+		userStr, grpStr = parts[0], parts[0]
149
+	} else {
150
+		userStr, grpStr = parts[0], parts[1]
151
+	}
152
+
153
+	passwdPath, err := symlink.FollowSymlinkInScope(filepath.Join(ctrRootPath, "etc", "passwd"), ctrRootPath)
154
+	if err != nil {
155
+		return idtools.IDPair{}, errors.Wrapf(err, "can't resolve /etc/passwd path in container rootfs")
156
+	}
157
+	groupPath, err := symlink.FollowSymlinkInScope(filepath.Join(ctrRootPath, "etc", "group"), ctrRootPath)
158
+	if err != nil {
159
+		return idtools.IDPair{}, errors.Wrapf(err, "can't resolve /etc/group path in container rootfs")
160
+	}
161
+	uid, err := lookupUser(userStr, passwdPath)
162
+	if err != nil {
163
+		return idtools.IDPair{}, errors.Wrapf(err, "can't find uid for user "+userStr)
164
+	}
165
+	gid, err := lookupGroup(grpStr, groupPath)
166
+	if err != nil {
167
+		return idtools.IDPair{}, errors.Wrapf(err, "can't find gid for group "+grpStr)
168
+	}
169
+
170
+	// convert as necessary because of user namespaces
171
+	chownPair, err := idMappings.ToHost(idtools.IDPair{UID: uid, GID: gid})
172
+	if err != nil {
173
+		return idtools.IDPair{}, errors.Wrapf(err, "unable to convert uid/gid to host mapping")
174
+	}
175
+	return chownPair, nil
176
+}
177
+
178
+func lookupUser(userStr, filepath string) (int, error) {
179
+	// if the string is actually a uid integer, parse to int and return
180
+	// as we don't need to translate with the help of files
181
+	uid, err := strconv.Atoi(userStr)
182
+	if err == nil {
183
+		return uid, nil
184
+	}
185
+	users, err := lcUser.ParsePasswdFileFilter(filepath, func(u lcUser.User) bool {
186
+		if u.Name == userStr {
187
+			return true
188
+		}
189
+		return false
190
+	})
191
+	if err != nil {
192
+		return 0, err
193
+	}
194
+	if len(users) == 0 {
195
+		return 0, errors.New("no such user: " + userStr)
196
+	}
197
+	return users[0].Uid, nil
198
+}
199
+
200
+func lookupGroup(groupStr, filepath string) (int, error) {
201
+	// if the string is actually a gid integer, parse to int and return
202
+	// as we don't need to translate with the help of files
203
+	gid, err := strconv.Atoi(groupStr)
204
+	if err == nil {
205
+		return gid, nil
206
+	}
207
+	groups, err := lcUser.ParseGroupFileFilter(filepath, func(g lcUser.Group) bool {
208
+		if g.Name == groupStr {
209
+			return true
210
+		}
211
+		return false
212
+	})
213
+	if err != nil {
214
+		return 0, err
215
+	}
216
+	if len(groups) == 0 {
217
+		return 0, errors.New("no such group: " + groupStr)
218
+	}
219
+	return groups[0].Gid, nil
220
+}
221
+
140 222
 func createDestInfo(workingDir string, inst copyInstruction, imageMount *imageMount) (copyInfo, error) {
141 223
 	// Twiddle the destination when it's a relative path - meaning, make it
142 224
 	// relative to the WORKINGDIR
... ...
@@ -2,6 +2,8 @@ package dockerfile
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"os"
6
+	"path/filepath"
5 7
 	"runtime"
6 8
 	"testing"
7 9
 
... ...
@@ -11,6 +13,7 @@ import (
11 11
 	"github.com/docker/docker/builder"
12 12
 	"github.com/docker/docker/builder/remotecontext"
13 13
 	"github.com/docker/docker/pkg/archive"
14
+	"github.com/docker/docker/pkg/idtools"
14 15
 	"github.com/stretchr/testify/assert"
15 16
 	"github.com/stretchr/testify/require"
16 17
 )
... ...
@@ -129,3 +132,130 @@ func TestCopyRunConfig(t *testing.T) {
129 129
 	}
130 130
 
131 131
 }
132
+
133
+func TestChownFlagParsing(t *testing.T) {
134
+	testFiles := map[string]string{
135
+		"passwd": `root:x:0:0::/bin:/bin/false
136
+bin:x:1:1::/bin:/bin/false
137
+wwwwww:x:21:33::/bin:/bin/false
138
+unicorn:x:1001:1002::/bin:/bin/false
139
+		`,
140
+		"group": `root:x:0:
141
+bin:x:1:
142
+wwwwww:x:33:
143
+unicorn:x:1002:
144
+somegrp:x:5555:
145
+othergrp:x:6666:
146
+		`,
147
+	}
148
+	// test mappings for validating use of maps
149
+	idMaps := []idtools.IDMap{
150
+		{
151
+			ContainerID: 0,
152
+			HostID:      100000,
153
+			Size:        65536,
154
+		},
155
+	}
156
+	remapped := idtools.NewIDMappingsFromMaps(idMaps, idMaps)
157
+	unmapped := &idtools.IDMappings{}
158
+
159
+	contextDir, cleanup := createTestTempDir(t, "", "builder-chown-parse-test")
160
+	defer cleanup()
161
+
162
+	if err := os.Mkdir(filepath.Join(contextDir, "etc"), 0755); err != nil {
163
+		t.Fatalf("error creating test directory: %v", err)
164
+	}
165
+
166
+	for filename, content := range testFiles {
167
+		createTestTempFile(t, filepath.Join(contextDir, "etc"), filename, content, 0644)
168
+	}
169
+
170
+	// positive tests
171
+	for _, testcase := range []struct {
172
+		name      string
173
+		chownStr  string
174
+		idMapping *idtools.IDMappings
175
+		expected  idtools.IDPair
176
+	}{
177
+		{
178
+			name:      "UIDNoMap",
179
+			chownStr:  "1",
180
+			idMapping: unmapped,
181
+			expected:  idtools.IDPair{UID: 1, GID: 1},
182
+		},
183
+		{
184
+			name:      "UIDGIDNoMap",
185
+			chownStr:  "0:1",
186
+			idMapping: unmapped,
187
+			expected:  idtools.IDPair{UID: 0, GID: 1},
188
+		},
189
+		{
190
+			name:      "UIDWithMap",
191
+			chownStr:  "0",
192
+			idMapping: remapped,
193
+			expected:  idtools.IDPair{UID: 100000, GID: 100000},
194
+		},
195
+		{
196
+			name:      "UIDGIDWithMap",
197
+			chownStr:  "1:33",
198
+			idMapping: remapped,
199
+			expected:  idtools.IDPair{UID: 100001, GID: 100033},
200
+		},
201
+		{
202
+			name:      "UserNoMap",
203
+			chownStr:  "bin:5555",
204
+			idMapping: unmapped,
205
+			expected:  idtools.IDPair{UID: 1, GID: 5555},
206
+		},
207
+		{
208
+			name:      "GroupWithMap",
209
+			chownStr:  "0:unicorn",
210
+			idMapping: remapped,
211
+			expected:  idtools.IDPair{UID: 100000, GID: 101002},
212
+		},
213
+		{
214
+			name:      "UserOnlyWithMap",
215
+			chownStr:  "unicorn",
216
+			idMapping: remapped,
217
+			expected:  idtools.IDPair{UID: 101001, GID: 101002},
218
+		},
219
+	} {
220
+		t.Run(testcase.name, func(t *testing.T) {
221
+			idPair, err := parseChownFlag(testcase.chownStr, contextDir, testcase.idMapping)
222
+			require.NoError(t, err, "Failed to parse chown flag: %q", testcase.chownStr)
223
+			assert.Equal(t, testcase.expected, idPair, "chown flag mapping failure")
224
+		})
225
+	}
226
+
227
+	// error tests
228
+	for _, testcase := range []struct {
229
+		name      string
230
+		chownStr  string
231
+		idMapping *idtools.IDMappings
232
+		descr     string
233
+	}{
234
+		{
235
+			name:      "BadChownFlagFormat",
236
+			chownStr:  "bob:1:555",
237
+			idMapping: unmapped,
238
+			descr:     "invalid chown string format: bob:1:555",
239
+		},
240
+		{
241
+			name:      "UserNoExist",
242
+			chownStr:  "bob",
243
+			idMapping: unmapped,
244
+			descr:     "can't find uid for user bob: no such user: bob",
245
+		},
246
+		{
247
+			name:      "GroupNoExist",
248
+			chownStr:  "root:bob",
249
+			idMapping: unmapped,
250
+			descr:     "can't find gid for group bob: no such group: bob",
251
+		},
252
+	} {
253
+		t.Run(testcase.name, func(t *testing.T) {
254
+			_, err := parseChownFlag(testcase.chownStr, contextDir, testcase.idMapping)
255
+			assert.EqualError(t, err, testcase.descr, "Expected error string doesn't match")
256
+		})
257
+	}
258
+}
... ...
@@ -409,6 +409,35 @@ func (s *DockerSuite) TestBuildAddRemoteNoDecompress(c *check.C) {
409 409
 	assert.Contains(c, string(out), "Successfully built")
410 410
 }
411 411
 
412
+func (s *DockerSuite) TestBuildChownOnCopy(c *check.C) {
413
+	testRequires(c, DaemonIsLinux)
414
+	dockerfile := `FROM busybox
415
+		RUN echo 'test1:x:1001:1001::/bin:/bin/false' >> /etc/passwd
416
+		RUN echo 'test1:x:1001:' >> /etc/group
417
+		RUN echo 'test2:x:1002:' >> /etc/group
418
+		COPY --chown=test1:1002 . /new_dir
419
+		RUN ls -l /
420
+		RUN [ $(ls -l / | grep new_dir | awk '{print $3":"$4}') = 'test1:test2' ]
421
+		RUN [ $(ls -nl / | grep new_dir | awk '{print $3":"$4}') = '1001:1002' ]
422
+	`
423
+	ctx := fakecontext.New(c, "",
424
+		fakecontext.WithDockerfile(dockerfile),
425
+		fakecontext.WithFile("test_file1", "some test content"),
426
+	)
427
+	defer ctx.Close()
428
+
429
+	res, body, err := request.Post(
430
+		"/build",
431
+		request.RawContent(ctx.AsTarReader(c)),
432
+		request.ContentType("application/x-tar"))
433
+	c.Assert(err, checker.IsNil)
434
+	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
435
+
436
+	out, err := testutil.ReadBody(body)
437
+	require.NoError(c, err)
438
+	assert.Contains(c, string(out), "Successfully built")
439
+}
440
+
412 441
 func (s *DockerSuite) TestBuildWithSession(c *check.C) {
413 442
 	testRequires(c, ExperimentalDaemon)
414 443