mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-22 03:02:46 +02:00
Fix bug in copy command by refactoring whitelist checks (#231)
* Fixed bug * WIP * fix unit tests
This commit is contained in:
+6
-21
@@ -91,12 +91,12 @@ func GetFSFromImage(img v1.Image) error {
|
||||
continue
|
||||
}
|
||||
|
||||
if checkWhitelist(path, whitelist) {
|
||||
if CheckWhitelist(path) {
|
||||
logrus.Infof("Not adding %s because it is whitelisted", path)
|
||||
continue
|
||||
}
|
||||
if hdr.Typeflag == tar.TypeSymlink {
|
||||
if checkWhitelist(hdr.Linkname, whitelist) {
|
||||
if CheckWhitelist(hdr.Linkname) {
|
||||
logrus.Debugf("skipping symlink from %s to %s because %s is whitelisted", hdr.Linkname, path, hdr.Linkname)
|
||||
continue
|
||||
}
|
||||
@@ -115,7 +115,7 @@ func GetFSFromImage(img v1.Image) error {
|
||||
func DeleteFilesystem() error {
|
||||
logrus.Info("Deleting filesystem...")
|
||||
err := filepath.Walk(constants.RootDir, func(path string, info os.FileInfo, err error) error {
|
||||
if PathInWhitelist(path, constants.RootDir) || ChildDirInWhitelist(path, constants.RootDir) {
|
||||
if CheckWhitelist(path) || ChildDirInWhitelist(path, constants.RootDir) {
|
||||
logrus.Debugf("Not deleting %s, as it's whitelisted", path)
|
||||
return nil
|
||||
}
|
||||
@@ -233,21 +233,6 @@ func extractFile(dest string, hdr *tar.Header, tr io.Reader) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func PathInWhitelist(path, directory string) bool {
|
||||
for _, c := range constants.KanikoBuildFiles {
|
||||
if path == c {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for _, d := range whitelist {
|
||||
dirPath := filepath.Join(directory, d)
|
||||
if HasFilepathPrefix(path, dirPath) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func checkWhiteouts(path string, whiteouts map[string]struct{}) bool {
|
||||
// Don't add the file if it or it's directory are whited out.
|
||||
if _, ok := whiteouts[path]; ok {
|
||||
@@ -262,7 +247,7 @@ func checkWhiteouts(path string, whiteouts map[string]struct{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func checkWhitelist(path string, whitelist []string) bool {
|
||||
func CheckWhitelist(path string) bool {
|
||||
for _, wl := range whitelist {
|
||||
if HasFilepathPrefix(path, wl) {
|
||||
return true
|
||||
@@ -316,7 +301,7 @@ func RelativeFiles(fp string, root string) ([]string, error) {
|
||||
fullPath := filepath.Join(root, fp)
|
||||
logrus.Debugf("Getting files and contents at root %s", fullPath)
|
||||
err := filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error {
|
||||
if PathInWhitelist(path, root) {
|
||||
if CheckWhitelist(path) && !HasFilepathPrefix(path, root) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
@@ -337,7 +322,7 @@ func Files(root string) ([]string, error) {
|
||||
var files []string
|
||||
logrus.Debugf("Getting files and contents at root %s", root)
|
||||
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||
if PathInWhitelist(path, root) {
|
||||
if CheckWhitelist(path) {
|
||||
return nil
|
||||
}
|
||||
files = append(files, path)
|
||||
|
||||
@@ -216,7 +216,7 @@ func Test_checkWhiteouts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_checkWhitelist(t *testing.T) {
|
||||
func Test_CheckWhitelist(t *testing.T) {
|
||||
type args struct {
|
||||
path string
|
||||
whitelist []string
|
||||
@@ -261,8 +261,13 @@ func Test_checkWhitelist(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := checkWhitelist(tt.args.path, tt.args.whitelist); got != tt.want {
|
||||
t.Errorf("checkWhitelist() = %v, want %v", got, tt.want)
|
||||
original := whitelist
|
||||
defer func() {
|
||||
whitelist = original
|
||||
}()
|
||||
whitelist = tt.args.whitelist
|
||||
if got := CheckWhitelist(tt.args.path); got != tt.want {
|
||||
t.Errorf("CheckWhitelist() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user