Skip to content

Commit dc26a92

Browse files
authored
Merge pull request #16 from coder/cj/embed-binary
Supporting changes for envbuilder#234
2 parents 9d2f7ea + 7208a49 commit dc26a92

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

pkg/util/fs_util.go

+43-1
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,30 @@ const (
5656
type IgnoreListEntry struct {
5757
Path string
5858
PrefixMatchOnly bool
59+
// AllowedPaths specifies **exact matches** to allow, even if they are under
60+
// Path.
61+
AllowedPaths []string
5962
}
6063

6164
var defaultIgnoreList = []IgnoreListEntry{
6265
{
6366
Path: filepath.Clean(config.KanikoDir),
6467
PrefixMatchOnly: false,
68+
AllowedPaths: nil,
6569
},
6670
{
6771
// similarly, we ignore /etc/mtab, since there is no way to know if the file was mounted or came
6872
// from the base image
6973
Path: "/etc/mtab",
7074
PrefixMatchOnly: false,
75+
AllowedPaths: nil,
7176
},
7277
{
7378
// we ingore /tmp/apt-key-gpghome, since the apt keys are added temporarily in this directory.
7479
// from the base image
7580
Path: "/tmp/apt-key-gpghome",
7681
PrefixMatchOnly: true,
82+
AllowedPaths: nil,
7783
},
7884
}
7985

@@ -111,16 +117,29 @@ func AddToIgnoreList(entry IgnoreListEntry) {
111117
ignorelist = append(ignorelist, IgnoreListEntry{
112118
Path: filepath.Clean(entry.Path),
113119
PrefixMatchOnly: entry.PrefixMatchOnly,
120+
AllowedPaths: nil,
114121
})
115122
}
116123

117124
func AddToDefaultIgnoreList(entry IgnoreListEntry) {
118125
defaultIgnoreList = append(defaultIgnoreList, IgnoreListEntry{
119126
Path: filepath.Clean(entry.Path),
120127
PrefixMatchOnly: entry.PrefixMatchOnly,
128+
AllowedPaths: nil,
121129
})
122130
}
123131

132+
func AddAllowedPathToDefaultIgnoreList(allowPath string) error {
133+
for _, ile := range defaultIgnoreList {
134+
if !strings.HasPrefix(allowPath, ile.Path) {
135+
continue
136+
}
137+
ile.AllowedPaths = append(ile.AllowedPaths, allowPath)
138+
return nil
139+
}
140+
return fmt.Errorf("path %q is not covered by any current entry in ignore list", allowPath)
141+
}
142+
124143
func IncludeWhiteout() FSOpt {
125144
return func(opts *FSConfig) {
126145
opts.includeWhiteout = true
@@ -500,6 +519,11 @@ func IsInIgnoreList(path string) bool {
500519

501520
func CheckCleanedPathAgainstProvidedIgnoreList(path string, wl []IgnoreListEntry) bool {
502521
for _, wl := range ignorelist {
522+
for _, ap := range wl.AllowedPaths {
523+
if ap == path {
524+
return false
525+
}
526+
}
503527
if hasCleanedFilepathPrefix(path, wl.Path, wl.PrefixMatchOnly) {
504528
return true
505529
}
@@ -556,6 +580,7 @@ func DetectFilesystemIgnoreList(path string) error {
556580
AddToIgnoreList(IgnoreListEntry{
557581
Path: lineArr[4],
558582
PrefixMatchOnly: false,
583+
AllowedPaths: nil,
559584
})
560585
}
561586
if err == io.EOF {
@@ -668,14 +693,30 @@ func CreateFile(path string, reader io.Reader, perm os.FileMode, uid uint32, gid
668693
}
669694
}
670695

696+
var renamed string
671697
dest, err := os.Create(path)
672698
if err != nil {
673-
return errors.Wrap(err, "creating file")
699+
if !errors.Is(err, syscall.ETXTBSY) {
700+
return errors.Wrap(err, "creating file")
701+
}
702+
// If the file is busy, just write to a temp file and
703+
// move to dest.
704+
dest, err = os.CreateTemp(os.TempDir(), "")
705+
if err != nil {
706+
return errors.Wrap(err, "create temp file")
707+
}
708+
defer os.Remove(dest.Name())
709+
renamed = dest.Name()
674710
}
675711
defer dest.Close()
676712
if _, err := io.Copy(dest, reader); err != nil {
677713
return errors.Wrap(err, "copying file")
678714
}
715+
if renamed != "" {
716+
if err := os.Rename(renamed, path); err != nil {
717+
return errors.Wrap(err, "rename temp file")
718+
}
719+
}
679720
return setFilePermissions(path, perm, int(uid), int(gid))
680721
}
681722

@@ -685,6 +726,7 @@ func AddVolumePathToIgnoreList(path string) {
685726
AddToIgnoreList(IgnoreListEntry{
686727
Path: path,
687728
PrefixMatchOnly: true,
729+
AllowedPaths: nil,
688730
})
689731
volumes = append(volumes, path)
690732
}

pkg/util/fs_util_test.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ func Test_DetectFilesystemSkiplist(t *testing.T) {
5858

5959
err := DetectFilesystemIgnoreList(path)
6060
expectedSkiplist := []IgnoreListEntry{
61-
{"/kaniko", false},
62-
{"/proc", false},
63-
{"/dev", false},
64-
{"/dev/pts", false},
65-
{"/sys", false},
66-
{"/etc/mtab", false},
67-
{"/tmp/apt-key-gpghome", true},
61+
{"/kaniko", false, nil},
62+
{"/proc", false, nil},
63+
{"/dev", false, nil},
64+
{"/dev/pts", false, nil},
65+
{"/sys", false, nil},
66+
{"/etc/mtab", false, nil},
67+
{"/tmp/apt-key-gpghome", true, nil},
6868
}
6969
actualSkiplist := ignorelist
7070
sort.Slice(actualSkiplist, func(i, j int) bool {
@@ -274,39 +274,39 @@ func Test_CheckIgnoreList(t *testing.T) {
274274
name: "file ignored",
275275
args: args{
276276
path: "/foo",
277-
ignorelist: []IgnoreListEntry{{"/foo", false}},
277+
ignorelist: []IgnoreListEntry{{"/foo", false, nil}},
278278
},
279279
want: true,
280280
},
281281
{
282282
name: "directory ignored",
283283
args: args{
284284
path: "/foo/bar",
285-
ignorelist: []IgnoreListEntry{{"/foo", false}},
285+
ignorelist: []IgnoreListEntry{{"/foo", false, nil}},
286286
},
287287
want: true,
288288
},
289289
{
290290
name: "grandparent ignored",
291291
args: args{
292292
path: "/foo/bar/baz",
293-
ignorelist: []IgnoreListEntry{{"/foo", false}},
293+
ignorelist: []IgnoreListEntry{{"/foo", false, nil}},
294294
},
295295
want: true,
296296
},
297297
{
298298
name: "sibling ignored",
299299
args: args{
300300
path: "/foo/bar/baz",
301-
ignorelist: []IgnoreListEntry{{"/foo/bat", false}},
301+
ignorelist: []IgnoreListEntry{{"/foo/bat", false, nil}},
302302
},
303303
want: false,
304304
},
305305
{
306306
name: "prefix match only ",
307307
args: args{
308308
path: "/tmp/apt-key-gpghome.xft/gpg.key",
309-
ignorelist: []IgnoreListEntry{{"/tmp/apt-key-gpghome.*", true}},
309+
ignorelist: []IgnoreListEntry{{"/tmp/apt-key-gpghome.*", true, nil}},
310310
},
311311
want: true,
312312
},
@@ -1487,22 +1487,27 @@ func TestInitIgnoreList(t *testing.T) {
14871487
{
14881488
Path: "/kaniko",
14891489
PrefixMatchOnly: false,
1490+
AllowedPaths: nil,
14901491
},
14911492
{
14921493
Path: "/test/kaniko",
14931494
PrefixMatchOnly: false,
1495+
AllowedPaths: nil,
14941496
},
14951497
{
14961498
Path: "/test/proc",
14971499
PrefixMatchOnly: false,
1500+
AllowedPaths: nil,
14981501
},
14991502
{
15001503
Path: "/etc/mtab",
15011504
PrefixMatchOnly: false,
1505+
AllowedPaths: nil,
15021506
},
15031507
{
15041508
Path: "/tmp/apt-key-gpghome",
15051509
PrefixMatchOnly: true,
1510+
AllowedPaths: nil,
15061511
},
15071512
}
15081513

0 commit comments

Comments
 (0)