From e3bb8bc71a54a6782806918bd15c5d2e3b362c1c Mon Sep 17 00:00:00 2001 From: MMeent Date: Wed, 2 Jan 2019 18:42:36 +0100 Subject: [PATCH] Adds COPY --from=previous stage name/number validation (#491) * Adds COPY --from=previous stage name/number validation This fixes an issue in which COPY --from=previous-stage-name would try to download docker image previous-stage-name instead of checking that previous-stage-name could be a named stage. * Fix linting issues goimports is implemented as 'gofmt + extras', so this should fix import warnings as well. * Fix linting issues Fixes linting issues introduced in the merge * Fix linting issues. --- pkg/executor/build.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/pkg/executor/build.go b/pkg/executor/build.go index d655bf951..59632cf5d 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -414,18 +414,29 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) { func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) error { t := timing.Start("Fetching Extra Stages") defer timing.DefaultRun.Stop(t) - for _, s := range stages { + + var names = []string{} + + for stageIndex, s := range stages { for _, cmd := range s.Commands { c, ok := cmd.(*instructions.CopyCommand) if !ok || c.From == "" { continue } - // FROMs at this point are guaranteed to be either an integer referring to a previous stage or - // a name of a remote image. - if _, err := strconv.Atoi(c.From); err == nil { + // FROMs at this point are guaranteed to be either an integer referring to a previous stage, + // the name of a previous stage, or a name of a remote image. + + // If it is an integer stage index, validate that it is actually a previous index + if fromIndex, err := strconv.Atoi(c.From); err == nil && stageIndex > fromIndex && fromIndex >= 0 { continue } + // Check if the name is the alias of a previous stage + for _, name := range names { + if name == c.From { + continue + } + } // This must be an image name, fetch it. logrus.Debugf("Found extra base image stage %s", c.From) sourceImage, err := util.RetrieveRemoteImage(c.From, opts, false) @@ -439,10 +450,13 @@ func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) e return err } } + // Store the name of the current stage in the list with names, if applicable. + if s.Name != "" { + names = append(names, s.Name) + } } return nil } - func extractImageToDependecyDir(name string, image v1.Image) error { t := timing.Start("Extracting Image to Dependency Dir") defer timing.DefaultRun.Stop(t)