Merge remote-tracking branch 'origin/master' into fix/1172

This commit is contained in:
Yoan Blanc
2020-06-06 08:42:30 +02:00
34 changed files with 697 additions and 275 deletions
+21
View File
@@ -17,8 +17,10 @@ limitations under the License.
package buildcontext
import (
"io"
"os"
"path/filepath"
"strings"
"cloud.google.com/go/storage"
"github.com/GoogleContainerTools/kaniko/pkg/constants"
@@ -37,6 +39,25 @@ func (g *GCS) UnpackTarFromBuildContext() (string, error) {
return constants.BuildContextDir, unpackTarFromGCSBucket(bucket, item, constants.BuildContextDir)
}
func UploadToBucket(r io.Reader, dest string) error {
ctx := context.Background()
context := strings.SplitAfter(dest, "://")[1]
bucketName, item := util.GetBucketAndItem(context)
client, err := storage.NewClient(ctx)
if err != nil {
return err
}
bucket := client.Bucket(bucketName)
w := bucket.Object(item).NewWriter(ctx)
if _, err := io.Copy(w, r); err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
return nil
}
// unpackTarFromGCSBucket unpacks the context.tar.gz file in the given bucket to the given directory
func unpackTarFromGCSBucket(bucketName, item, directory string) error {
// Get the tar from the bucket
+1 -1
View File
@@ -48,7 +48,7 @@ func (v *VolumeCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.
for _, volume := range resolvedVolumes {
var x struct{}
existingVolumes[volume] = x
util.AddVolumePathToWhitelist(volume)
util.AddVolumePathToIgnoreList(volume)
// Only create and snapshot the dir if it didn't exist already
if _, err := os.Stat(volume); os.IsNotExist(err) {
+2 -2
View File
@@ -22,10 +22,10 @@ import (
var RootDir string
var KanikoDir string
var WhitelistPath string
var IgnoreListPath string
func init() {
RootDir = constants.RootDir
KanikoDir = constants.KanikoDir
WhitelistPath = constants.WhitelistPath
IgnoreListPath = constants.IgnoreListPath
}
+1 -1
View File
@@ -55,7 +55,7 @@ type KanikoOptions struct {
NoPush bool
Cache bool
Cleanup bool
WhitelistVarRun bool
IgnoreVarRun bool
SkipUnusedStages bool
}
+1 -1
View File
@@ -26,7 +26,7 @@ const (
//KanikoDir is the path to the Kaniko directory
KanikoDir = "/kaniko"
WhitelistPath = "/proc/self/mountinfo"
IgnoreListPath = "/proc/self/mountinfo"
Author = "kaniko"
+24
View File
@@ -58,6 +58,11 @@ func ParseStages(opts *config.KanikoOptions) ([]instructions.Stage, []instructio
return nil, nil, errors.Wrap(err, "parsing dockerfile")
}
metaArgs, err = expandNested(metaArgs, opts.BuildArgs)
if err != nil {
return nil, nil, errors.Wrap(err, "expanding meta ARGs")
}
return stages, metaArgs, nil
}
@@ -97,6 +102,25 @@ func Parse(b []byte) ([]instructions.Stage, []instructions.ArgCommand, error) {
return stages, metaArgs, nil
}
// expandNestedArgs tries to resolve nested ARG value against the previously defined ARGs
func expandNested(metaArgs []instructions.ArgCommand, buildArgs []string) ([]instructions.ArgCommand, error) {
prevArgs := make([]string, 0)
for i := range metaArgs {
arg := metaArgs[i]
v := arg.Value
if v != nil {
val, err := util.ResolveEnvironmentReplacement(*v, append(prevArgs, buildArgs...), false)
if err != nil {
return nil, err
}
prevArgs = append(prevArgs, arg.Key+"="+val)
arg.Value = &val
metaArgs[i] = arg
}
}
return metaArgs, nil
}
// stripEnclosingQuotes removes quotes enclosing the value of each instructions.ArgCommand in a slice
// if the quotes are escaped it leaves them
func stripEnclosingQuotes(metaArgs []instructions.ArgCommand) ([]instructions.ArgCommand, error) {
+7 -4
View File
@@ -33,6 +33,9 @@ func Test_ParseStages_ArgValueWithQuotes(t *testing.T) {
dockerfile := `
ARG IMAGE="ubuntu:16.04"
ARG FOO=bar
ARG HELLO="Hello"
ARG WORLD="World"
ARG NESTED="$HELLO $WORLD"
FROM ${IMAGE}
RUN echo hi > /hi
@@ -65,11 +68,11 @@ func Test_ParseStages_ArgValueWithQuotes(t *testing.T) {
t.Fatal("length of stages expected to be greater than zero, but was zero")
}
if len(metaArgs) != 2 {
t.Fatalf("length of stage meta args expected to be 2, but was %d", len(metaArgs))
if len(metaArgs) != 5 {
t.Fatalf("length of stage meta args expected to be 5, but was %d", len(metaArgs))
}
for i, expectedVal := range []string{"ubuntu:16.04", "bar"} {
for i, expectedVal := range []string{"ubuntu:16.04", "bar", "Hello", "World", "Hello World"} {
if metaArgs[i].ValueString() != expectedVal {
t.Fatalf("expected metaArg %d val to be %s but was %s", i, expectedVal, metaArgs[i].ValueString())
}
@@ -565,7 +568,7 @@ func Test_SkipingUnusedStages(t *testing.T) {
# Make sure that we snapshot intermediate images correctly
RUN date > /date
ENV foo bar
# This base image contains symlinks with relative paths to whitelisted directories
# This base image contains symlinks with relative paths to ignored directories
# We need to test they're extracted correctly
FROM fedora@sha256:c4cc32b09c6ae3f1353e7e33a8dda93dc41676b923d6d89afa996b421cc5aa48
FROM fourth
+2 -2
View File
@@ -314,8 +314,8 @@ func (s *stageBuilder) build() error {
logrus.Info("Skipping unpacking as no commands require it.")
}
if err := util.DetectFilesystemWhitelist(config.WhitelistPath); err != nil {
return errors.Wrap(err, "failed to check filesystem whitelist")
if err := util.DetectFilesystemIgnoreList(config.IgnoreListPath); err != nil {
return errors.Wrap(err, "failed to check filesystem mount paths")
}
initSnapshotTaken := false
+1 -1
View File
@@ -69,7 +69,7 @@ func (s *CompositeCache) AddPath(p, context string) error {
}
// Only add the hash of this directory to the key
// if there is any whitelisted content.
// if there is any ignored content.
if !empty || !util.ExcludeFile(p, context) {
s.keys = append(s.keys, k)
}
+3 -3
View File
@@ -166,7 +166,7 @@ func setupMultistageTests(t *testing.T) (string, func()) {
// set up config
config.RootDir = testDir
config.KanikoDir = fmt.Sprintf("%s/%s", testDir, "kaniko")
// Write a whitelist path
// Write path to ignore list
if err := os.MkdirAll(filepath.Join(testDir, "proc"), 0755); err != nil {
t.Fatal(err)
}
@@ -178,10 +178,10 @@ func setupMultistageTests(t *testing.T) (string, func()) {
if err := ioutil.WriteFile(mFile, []byte(mountInfo), 0644); err != nil {
t.Fatal(err)
}
config.WhitelistPath = mFile
config.IgnoreListPath = mFile
return testDir, func() {
config.KanikoDir = constants.KanikoDir
config.RootDir = constants.RootDir
config.WhitelistPath = constants.WhitelistPath
config.IgnoreListPath = constants.IgnoreListPath
}
}
+10 -11
View File
@@ -26,24 +26,23 @@ import (
"github.com/sirupsen/logrus"
)
// ResolvePaths takes a slice of file paths and a slice of whitelist entries. It resolve each
// ResolvePaths takes a slice of file paths and a list of skipped file paths. It resolve each
// file path according to a set of rules and then returns a slice of resolved paths or error.
// File paths are resolved according to the following rules:
// * If path is whitelisted, skip it.
// * If path is in ignorelist, skip it.
// * If path is a symlink, resolve it's ancestor link and add it to the output set.
// * If path is a symlink, resolve it's target. If the target is not whitelisted add it to the
// * If path is a symlink, resolve it's target. If the target is not ignored add it to the
// output set.
// * Add all ancestors of each path to the output set.
func ResolvePaths(paths []string, wl []util.WhitelistEntry) (pathsToAdd []string, err error) {
logrus.Infof("Resolving %d paths", len(paths))
func ResolvePaths(paths []string, wl []util.IgnoreListEntry) (pathsToAdd []string, err error) {
logrus.Tracef("Resolving paths %s", paths)
fileSet := make(map[string]bool)
for _, f := range paths {
// If the given path is part of the whitelist ignore it
if util.IsInProvidedWhitelist(f, wl) {
logrus.Debugf("path %s is whitelisted, ignoring it", f)
// If the given path is part of the ignorelist ignore it
if util.IsInProvidedIgnoreList(f, wl) {
logrus.Debugf("path %s is in list to ignore, ignoring it", f)
continue
}
@@ -76,10 +75,10 @@ func ResolvePaths(paths []string, wl []util.WhitelistEntry) (pathsToAdd []string
continue
}
// If the given path is a symlink and the target is part of the whitelist
// If the given path is a symlink and the target is part of the ignorelist
// ignore the target
if util.IsInProvidedWhitelist(evaled, wl) {
logrus.Debugf("path %s is whitelisted, ignoring it", evaled)
if util.IsInProvidedIgnoreList(evaled, wl) {
logrus.Debugf("path %s is ignored, ignoring it", evaled)
continue
}
+7 -7
View File
@@ -85,8 +85,8 @@ func Test_ResolvePaths(t *testing.T) {
}
}
t.Run("none are whitelisted", func(t *testing.T) {
wl := []util.WhitelistEntry{}
t.Run("none are ignored", func(t *testing.T) {
wl := []util.IgnoreListEntry{}
inputFiles := []string{}
expectedFiles := []string{}
@@ -107,8 +107,8 @@ func Test_ResolvePaths(t *testing.T) {
validateResults(t, files, expectedFiles, err)
})
t.Run("some are whitelisted", func(t *testing.T) {
wl := []util.WhitelistEntry{
t.Run("some are ignored", func(t *testing.T) {
wl := []util.IgnoreListEntry{
{
Path: filepath.Join(dir, "link", "baz"),
},
@@ -124,7 +124,7 @@ func Test_ResolvePaths(t *testing.T) {
link := filepath.Join(dir, "link", f)
inputFiles = append(inputFiles, link)
if util.IsInProvidedWhitelist(link, wl) {
if util.IsInProvidedIgnoreList(link, wl) {
t.Logf("skipping %s", link)
continue
}
@@ -133,7 +133,7 @@ func Test_ResolvePaths(t *testing.T) {
target := filepath.Join(dir, "target", f)
if util.IsInProvidedWhitelist(target, wl) {
if util.IsInProvidedIgnoreList(target, wl) {
t.Logf("skipping %s", target)
continue
}
@@ -177,7 +177,7 @@ func Test_ResolvePaths(t *testing.T) {
inputFiles := []string{}
expectedFiles := []string{}
wl := []util.WhitelistEntry{}
wl := []util.IgnoreListEntry{}
files, err := ResolvePaths(inputFiles, wl)
+39 -43
View File
@@ -39,14 +39,14 @@ var snapshotPathPrefix = config.KanikoDir
// Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken
type Snapshotter struct {
l *LayeredMap
directory string
whitelist []util.WhitelistEntry
l *LayeredMap
directory string
ignorelist []util.IgnoreListEntry
}
// NewSnapshotter creates a new snapshotter rooted at d
func NewSnapshotter(l *LayeredMap, d string) *Snapshotter {
return &Snapshotter{l: l, directory: d, whitelist: util.Whitelist()}
return &Snapshotter{l: l, directory: d, ignorelist: util.IgnoreList()}
}
// Init initializes a new snapshotter
@@ -60,7 +60,7 @@ func (s *Snapshotter) Key() (string, error) {
return s.l.Key()
}
// TakeSnapshot takes a snapshot of the specified files, avoiding directories in the whitelist, and creates
// TakeSnapshot takes a snapshot of the specified files, avoiding directories in the ignorelist, and creates
// a tarball of the changed files. Return contents of the tarball, and whether or not any files were changed
func (s *Snapshotter) TakeSnapshot(files []string) (string, error) {
f, err := ioutil.TempFile(config.KanikoDir, "")
@@ -75,7 +75,7 @@ func (s *Snapshotter) TakeSnapshot(files []string) (string, error) {
return "", nil
}
filesToAdd, err := filesystem.ResolvePaths(files, s.whitelist)
filesToAdd, err := filesystem.ResolvePaths(files, s.ignorelist)
if err != nil {
return "", nil
}
@@ -100,7 +100,7 @@ func (s *Snapshotter) TakeSnapshot(files []string) (string, error) {
return f.Name(), nil
}
// TakeSnapshotFS takes a snapshot of the filesystem, avoiding directories in the whitelist, and creates
// TakeSnapshotFS takes a snapshot of the filesystem, avoiding directories in the ignorelist, and creates
// a tarball of the changed files.
func (s *Snapshotter) TakeSnapshotFS() (string, error) {
f, err := ioutil.TempFile(snapshotPathPrefix, "")
@@ -139,9 +139,9 @@ func (s *Snapshotter) scanFullFilesystem() ([]string, []string, error) {
godirwalk.Walk(s.directory, &godirwalk.Options{
Callback: func(path string, ent *godirwalk.Dirent) error {
if util.IsInWhitelist(path) {
if util.IsInIgnoreList(path) {
if util.IsDestDir(path) {
logrus.Tracef("Skipping paths under %s, as it is a whitelisted directory", path)
logrus.Tracef("Skipping paths under %s, as it is a ignored directory", path)
return filepath.SkipDir
}
@@ -157,57 +157,53 @@ func (s *Snapshotter) scanFullFilesystem() ([]string, []string, error) {
},
)
timing.DefaultRun.Stop(timer)
timer = timing.Start("Resolving Paths")
resolvedFiles, err := filesystem.ResolvePaths(foundPaths, s.whitelist)
if err != nil {
return nil, nil, err
}
resolvedMemFs := make(map[string]bool)
for _, f := range resolvedFiles {
resolvedMemFs[f] = true
}
// First handle whiteouts
// Get a list of all the files that existed before this layer
existingPaths := s.l.getFlattenedPathsForWhiteOut()
// Find the delta by removing everything left in this layer.
for p := range resolvedMemFs {
delete(existingPaths, p)
filesToAdd := []string{}
resolvedMemFs := make(map[string]bool)
for _, path := range foundPaths {
delete(existingPaths, path)
resolvedFiles, err := filesystem.ResolvePaths([]string{path}, s.ignorelist)
if err != nil {
return nil, nil, err
}
for _, path := range resolvedFiles {
// Continue if this path is already processed
if _, ok := resolvedMemFs[path]; ok {
continue
}
if util.CheckIgnoreList(path) {
logrus.Tracef("Not adding %s to layer, as it's whitelisted", path)
continue
}
// Only add changed files.
fileChanged, err := s.l.CheckFileChange(path)
if err != nil {
return nil, nil, fmt.Errorf("could not check if file has changed %s %s", path, err)
}
if fileChanged {
logrus.Tracef("Adding file %s to layer, because it was changed.", path)
filesToAdd = append(filesToAdd, path)
}
}
}
// The paths left here are the ones that have been deleted in this layer.
// The paths left here are the ones that have been deleted in this layer.
filesToWhiteOut := []string{}
for path := range existingPaths {
// Only add the whiteout if the directory for the file still exists.
dir := filepath.Dir(path)
if _, ok := resolvedMemFs[dir]; ok {
if _, ok := existingPaths[dir]; !ok {
if s.l.MaybeAddWhiteout(path) {
logrus.Debugf("Adding whiteout for %s", path)
filesToWhiteOut = append(filesToWhiteOut, path)
}
}
}
filesToAdd := []string{}
for path := range resolvedMemFs {
if util.CheckWhitelist(path) {
logrus.Tracef("Not adding %s to layer, as it's whitelisted", path)
continue
}
// Only add changed files.
fileChanged, err := s.l.CheckFileChange(path)
if err != nil {
return nil, nil, fmt.Errorf("could not check if file has changed %s %s", path, err)
}
if fileChanged {
logrus.Tracef("Adding file %s to layer, because it was changed.", path)
filesToAdd = append(filesToAdd, path)
}
}
timing.DefaultRun.Stop(timer)
sort.Strings(filesToAdd)
// Add files to the layered map
+42 -42
View File
@@ -44,31 +44,31 @@ import (
const DoNotChangeUID = -1
const DoNotChangeGID = -1
type WhitelistEntry struct {
type IgnoreListEntry struct {
Path string
PrefixMatchOnly bool
}
var initialWhitelist = []WhitelistEntry{
var initialIgnoreList = []IgnoreListEntry{
{
Path: config.KanikoDir,
PrefixMatchOnly: false,
},
{
// similarly, we whitelist /etc/mtab, since there is no way to know if the file was mounted or came
// similarly, we ignore /etc/mtab, since there is no way to know if the file was mounted or came
// from the base image
Path: "/etc/mtab",
PrefixMatchOnly: false,
},
{
// we whitelist /tmp/apt-key-gpghome, since the apt keys are added temporarily in this directory.
// we ingore /tmp/apt-key-gpghome, since the apt keys are added temporarily in this directory.
// from the base image
Path: "/tmp/apt-key-gpghome",
PrefixMatchOnly: true,
},
}
var whitelist = initialWhitelist
var ignorelist = initialIgnoreList
var volumes = []string{}
@@ -84,8 +84,8 @@ type FSConfig struct {
type FSOpt func(*FSConfig)
func Whitelist() []WhitelistEntry {
return whitelist
func IgnoreList() []IgnoreListEntry {
return ignorelist
}
func IncludeWhiteout() FSOpt {
@@ -126,11 +126,11 @@ func GetFSFromLayers(root string, layers []v1.Layer, opts ...FSOpt) ([]string, e
return nil, errors.New("must supply an extract function")
}
if err := DetectFilesystemWhitelist(config.WhitelistPath); err != nil {
if err := DetectFilesystemIgnoreList(config.IgnoreListPath); err != nil {
return nil, err
}
logrus.Debugf("Mounted directories: %v", whitelist)
logrus.Debugf("Mounted directories: %v", ignorelist)
extractedFiles := []string{}
for i, l := range layers {
@@ -195,19 +195,19 @@ func DeleteFilesystem() error {
return nil
}
if CheckWhitelist(path) {
if CheckIgnoreList(path) {
if !isExist(path) {
logrus.Debugf("Path %s whitelisted, but not exists", path)
logrus.Debugf("Path %s ignored, but not exists", path)
return nil
}
if info.IsDir() {
return filepath.SkipDir
}
logrus.Debugf("Not deleting %s, as it's whitelisted", path)
logrus.Debugf("Not deleting %s, as it's ignored", path)
return nil
}
if childDirInWhitelist(path) {
logrus.Debugf("Not deleting %s, as it contains a whitelisted path", path)
if childDirInIgnoreList(path) {
logrus.Debugf("Not deleting %s, as it contains a ignored path", path)
return nil
}
if path == config.RootDir {
@@ -225,9 +225,9 @@ func isExist(path string) bool {
return false
}
// ChildDirInWhitelist returns true if there is a child file or directory of the path in the whitelist
func childDirInWhitelist(path string) bool {
for _, d := range whitelist {
// childDirInIgnoreList returns true if there is a child file or directory of the path in the ignorelist
func childDirInIgnoreList(path string) bool {
for _, d := range ignorelist {
if HasFilepathPrefix(d.Path, path, d.PrefixMatchOnly) {
return true
}
@@ -268,8 +268,8 @@ func ExtractFile(dest string, hdr *tar.Header, tr io.Reader) error {
return err
}
if CheckWhitelist(abs) && !checkWhitelistRoot(dest) {
logrus.Debugf("Not adding %s because it is whitelisted", path)
if CheckIgnoreList(abs) && !checkIgnoreListRoot(dest) {
logrus.Debugf("Not adding %s because it is ignored", path)
return nil
}
switch hdr.Typeflag {
@@ -325,8 +325,8 @@ func ExtractFile(dest string, hdr *tar.Header, tr io.Reader) error {
if err != nil {
return err
}
if CheckWhitelist(abs) {
logrus.Tracef("skipping symlink from %s to %s because %s is whitelisted", hdr.Linkname, path, hdr.Linkname)
if CheckIgnoreList(abs) {
logrus.Tracef("skipping symlink from %s to %s because %s is ignored", hdr.Linkname, path, hdr.Linkname)
return nil
}
// The base directory for a link may not exist before it is created.
@@ -365,11 +365,11 @@ func ExtractFile(dest string, hdr *tar.Header, tr io.Reader) error {
return nil
}
func IsInWhitelist(path string) bool {
return IsInProvidedWhitelist(path, whitelist)
func IsInIgnoreList(path string) bool {
return IsInProvidedIgnoreList(path, ignorelist)
}
func IsInProvidedWhitelist(path string, wl []WhitelistEntry) bool {
func IsInProvidedIgnoreList(path string, wl []IgnoreListEntry) bool {
for _, entry := range wl {
if !entry.PrefixMatchOnly && path == entry.Path {
return true
@@ -378,8 +378,8 @@ func IsInProvidedWhitelist(path string, wl []WhitelistEntry) bool {
return false
}
func CheckWhitelist(path string) bool {
for _, wl := range whitelist {
func CheckIgnoreList(path string) bool {
for _, wl := range ignorelist {
if HasFilepathPrefix(path, wl.Path, wl.PrefixMatchOnly) {
return true
}
@@ -388,21 +388,21 @@ func CheckWhitelist(path string) bool {
return false
}
func checkWhitelistRoot(root string) bool {
func checkIgnoreListRoot(root string) bool {
if root == config.RootDir {
return false
}
return CheckWhitelist(root)
return CheckIgnoreList(root)
}
// Get whitelist from roots of mounted files
// Get ignorelist from roots of mounted files
// Each line of /proc/self/mountinfo is in the form:
// 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
// (1)(2)(3) (4) (5) (6) (7) (8) (9) (10) (11)
// Where (5) is the mount point relative to the process's root
// From: https://www.kernel.org/doc/Documentation/filesystems/proc.txt
func DetectFilesystemWhitelist(path string) error {
whitelist = initialWhitelist
func DetectFilesystemIgnoreList(path string) error {
ignorelist = initialIgnoreList
volumes = []string{}
f, err := os.Open(path)
if err != nil {
@@ -426,7 +426,7 @@ func DetectFilesystemWhitelist(path string) error {
}
if lineArr[4] != config.RootDir {
logrus.Tracef("Appending %s from line: %s", lineArr[4], line)
whitelist = append(whitelist, WhitelistEntry{
ignorelist = append(ignorelist, IgnoreListEntry{
Path: lineArr[4],
PrefixMatchOnly: false,
})
@@ -448,7 +448,7 @@ func RelativeFiles(fp string, root string) ([]string, error) {
if err != nil {
return err
}
if CheckWhitelist(path) && !HasFilepathPrefix(path, root, false) {
if CheckIgnoreList(path) && !HasFilepathPrefix(path, root, false) {
return nil
}
relPath, err := filepath.Rel(root, path)
@@ -522,10 +522,10 @@ func CreateFile(path string, reader io.Reader, perm os.FileMode, uid uint32, gid
return setFilePermissions(path, perm, int(uid), int(gid))
}
// AddVolumePath adds the given path to the volume whitelist.
func AddVolumePathToWhitelist(path string) {
logrus.Infof("adding volume %s to whitelist", path)
whitelist = append(whitelist, WhitelistEntry{
// AddVolumePath adds the given path to the volume ignorelist.
func AddVolumePathToIgnoreList(path string) {
logrus.Infof("adding volume %s to ignorelist", path)
ignorelist = append(ignorelist, IgnoreListEntry{
Path: path,
PrefixMatchOnly: true,
})
@@ -861,13 +861,13 @@ func createParentDirectory(path string) error {
return nil
}
// UpdateInitialWhitelist will add /var/run to whitelisted paths if
func UpdateWhitelist(whitelistVarRun bool) {
if !whitelistVarRun {
// UpdateInitialIgnoreList will add /var/run to ignored paths if
func UpdateInitialIgnoreList(ignoreVarRun bool) {
if !ignoreVarRun {
return
}
logrus.Trace("Adding /var/run to initialWhitelist ")
initialWhitelist = append(initialWhitelist, WhitelistEntry{
logrus.Trace("Adding /var/run to initialIgnoreList ")
initialIgnoreList = append(initialIgnoreList, IgnoreListEntry{
// /var/run is a special case. It's common to mount in /var/run/docker.sock or something similar
// which leads to a special mount on the /var/run/docker.sock file itself, but the directory to exist
// in the image with no way to tell if it came from the base image or not.
+57 -57
View File
@@ -38,7 +38,7 @@ import (
"github.com/google/go-containerregistry/pkg/v1/types"
)
func Test_DetectFilesystemWhitelist(t *testing.T) {
func Test_DetectFilesystemSkiplist(t *testing.T) {
testDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Error creating tempdir: %s", err)
@@ -58,8 +58,8 @@ func Test_DetectFilesystemWhitelist(t *testing.T) {
t.Fatalf("Error writing file contents to %s: %s", path, err)
}
err = DetectFilesystemWhitelist(path)
expectedWhitelist := []WhitelistEntry{
err = DetectFilesystemIgnoreList(path)
expectedSkiplist := []IgnoreListEntry{
{"/kaniko", false},
{"/proc", false},
{"/dev", false},
@@ -68,14 +68,14 @@ func Test_DetectFilesystemWhitelist(t *testing.T) {
{"/etc/mtab", false},
{"/tmp/apt-key-gpghome", true},
}
actualWhitelist := whitelist
sort.Slice(actualWhitelist, func(i, j int) bool {
return actualWhitelist[i].Path < actualWhitelist[j].Path
actualSkiplist := ignorelist
sort.Slice(actualSkiplist, func(i, j int) bool {
return actualSkiplist[i].Path < actualSkiplist[j].Path
})
sort.Slice(expectedWhitelist, func(i, j int) bool {
return expectedWhitelist[i].Path < expectedWhitelist[j].Path
sort.Slice(expectedSkiplist, func(i, j int) bool {
return expectedSkiplist[i].Path < expectedSkiplist[j].Path
})
testutil.CheckErrorAndDeepEqual(t, false, err, expectedWhitelist, actualWhitelist)
testutil.CheckErrorAndDeepEqual(t, false, err, expectedSkiplist, actualSkiplist)
}
var tests = []struct {
@@ -251,10 +251,10 @@ func Test_ParentDirectoriesWithoutLeadingSlash(t *testing.T) {
}
}
func Test_CheckWhitelist(t *testing.T) {
func Test_CheckIgnoreList(t *testing.T) {
type args struct {
path string
whitelist []WhitelistEntry
path string
ignorelist []IgnoreListEntry
}
tests := []struct {
name string
@@ -262,56 +262,56 @@ func Test_CheckWhitelist(t *testing.T) {
want bool
}{
{
name: "file whitelisted",
name: "file ignored",
args: args{
path: "/foo",
whitelist: []WhitelistEntry{{"/foo", false}},
path: "/foo",
ignorelist: []IgnoreListEntry{{"/foo", false}},
},
want: true,
},
{
name: "directory whitelisted",
name: "directory ignored",
args: args{
path: "/foo/bar",
whitelist: []WhitelistEntry{{"/foo", false}},
path: "/foo/bar",
ignorelist: []IgnoreListEntry{{"/foo", false}},
},
want: true,
},
{
name: "grandparent whitelisted",
name: "grandparent ignored",
args: args{
path: "/foo/bar/baz",
whitelist: []WhitelistEntry{{"/foo", false}},
path: "/foo/bar/baz",
ignorelist: []IgnoreListEntry{{"/foo", false}},
},
want: true,
},
{
name: "sibling whitelisted",
name: "sibling ignored",
args: args{
path: "/foo/bar/baz",
whitelist: []WhitelistEntry{{"/foo/bat", false}},
path: "/foo/bar/baz",
ignorelist: []IgnoreListEntry{{"/foo/bat", false}},
},
want: false,
},
{
name: "prefix match only ",
args: args{
path: "/tmp/apt-key-gpghome.xft/gpg.key",
whitelist: []WhitelistEntry{{"/tmp/apt-key-gpghome.*", true}},
path: "/tmp/apt-key-gpghome.xft/gpg.key",
ignorelist: []IgnoreListEntry{{"/tmp/apt-key-gpghome.*", true}},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
original := whitelist
original := ignorelist
defer func() {
whitelist = original
ignorelist = original
}()
whitelist = tt.args.whitelist
got := CheckWhitelist(tt.args.path)
ignorelist = tt.args.ignorelist
got := CheckIgnoreList(tt.args.path)
if got != tt.want {
t.Errorf("CheckWhitelist() = %v, want %v", got, tt.want)
t.Errorf("CheckIgnoreList() = %v, want %v", got, tt.want)
}
})
}
@@ -879,10 +879,10 @@ func TestCopySymlink(t *testing.T) {
}
}
func Test_childDirInWhitelist(t *testing.T) {
func Test_childDirInSkiplist(t *testing.T) {
type args struct {
path string
whitelist []WhitelistEntry
path string
ignorelist []IgnoreListEntry
}
tests := []struct {
name string
@@ -890,17 +890,17 @@ func Test_childDirInWhitelist(t *testing.T) {
want bool
}{
{
name: "not in whitelist",
name: "not in ignorelist",
args: args{
path: "/foo",
},
want: false,
},
{
name: "child in whitelist",
name: "child in ignorelist",
args: args{
path: "/foo",
whitelist: []WhitelistEntry{
ignorelist: []IgnoreListEntry{
{
Path: "/foo/bar",
},
@@ -909,16 +909,16 @@ func Test_childDirInWhitelist(t *testing.T) {
want: true,
},
}
oldWhitelist := whitelist
oldIgnoreList := ignorelist
defer func() {
whitelist = oldWhitelist
ignorelist = oldIgnoreList
}()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
whitelist = tt.args.whitelist
if got := childDirInWhitelist(tt.args.path); got != tt.want {
t.Errorf("childDirInWhitelist() = %v, want %v", got, tt.want)
ignorelist = tt.args.ignorelist
if got := childDirInIgnoreList(tt.args.path); got != tt.want {
t.Errorf("childDirInIgnoreList() = %v, want %v", got, tt.want)
}
})
}
@@ -1315,16 +1315,16 @@ func assertGetFSFromLayers(
}
}
func TestUpdateWhitelist(t *testing.T) {
func TestUpdateSkiplist(t *testing.T) {
tests := []struct {
name string
whitelistVarRun bool
expected []WhitelistEntry
name string
skipVarRun bool
expected []IgnoreListEntry
}{
{
name: "var/run whitelisted",
whitelistVarRun: true,
expected: []WhitelistEntry{
name: "var/run ignored",
skipVarRun: true,
expected: []IgnoreListEntry{
{
Path: "/kaniko",
PrefixMatchOnly: false,
@@ -1344,8 +1344,8 @@ func TestUpdateWhitelist(t *testing.T) {
},
},
{
name: "var/run not whitelisted",
expected: []WhitelistEntry{
name: "var/run not ignored",
expected: []IgnoreListEntry{
{
Path: "/kaniko",
PrefixMatchOnly: false,
@@ -1363,16 +1363,16 @@ func TestUpdateWhitelist(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
original := initialWhitelist
defer func() { initialWhitelist = original }()
UpdateWhitelist(tt.whitelistVarRun)
original := initialIgnoreList
defer func() { initialIgnoreList = original }()
UpdateInitialIgnoreList(tt.skipVarRun)
sort.Slice(tt.expected, func(i, j int) bool {
return tt.expected[i].Path < tt.expected[j].Path
})
sort.Slice(initialWhitelist, func(i, j int) bool {
return initialWhitelist[i].Path < initialWhitelist[j].Path
sort.Slice(initialIgnoreList, func(i, j int) bool {
return initialIgnoreList[i].Path < initialIgnoreList[j].Path
})
testutil.CheckDeepEqual(t, tt.expected, initialWhitelist)
testutil.CheckDeepEqual(t, tt.expected, initialIgnoreList)
})
}
}