From 79649a1614d0d0cd01dd0a5d03fe7d837b750e47 Mon Sep 17 00:00:00 2001 From: Cole Wippern Date: Thu, 24 Oct 2019 16:30:25 -0700 Subject: [PATCH] Issue #439 double quotes in ARG value * Strip out double quotes enclosing ARG value after parsing dockerfile --- .../Dockerfile_test_arg_multi_with_quotes | 12 +++++ pkg/dockerfile/dockerfile.go | 24 +++++++++ pkg/dockerfile/dockerfile_test.go | 51 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 integration/dockerfiles/Dockerfile_test_arg_multi_with_quotes diff --git a/integration/dockerfiles/Dockerfile_test_arg_multi_with_quotes b/integration/dockerfiles/Dockerfile_test_arg_multi_with_quotes new file mode 100644 index 0000000000..8bf2524eb8 --- /dev/null +++ b/integration/dockerfiles/Dockerfile_test_arg_multi_with_quotes @@ -0,0 +1,12 @@ +ARG FILE_NAME="myFile" + +FROM busybox:latest AS builder +ARG FILE_NAME + +RUN echo $FILE_NAME && touch /$FILE_NAME.txt && stat /$FILE_NAME.txt; + +FROM busybox:latest +ARG FILE_NAME + +RUN echo $FILE_NAME && touch /$FILE_NAME.txt && stat /$FILE_NAME.txt; +COPY --from=builder /$FILE_NAME.txt / diff --git a/pkg/dockerfile/dockerfile.go b/pkg/dockerfile/dockerfile.go index 8865b17d00..1537f8c946 100644 --- a/pkg/dockerfile/dockerfile.go +++ b/pkg/dockerfile/dockerfile.go @@ -111,9 +111,33 @@ func Parse(b []byte) ([]instructions.Stage, []instructions.ArgCommand, error) { if err != nil { return nil, nil, err } + + metaArgs = stripEnclosingDoubleQuotes(metaArgs) + return stages, metaArgs, nil } +// stripEnclosingDoubleQuotes removes double quotes enclosing the value of each instructions.ArgCommand in a slice +func stripEnclosingDoubleQuotes(metaArgs []instructions.ArgCommand) []instructions.ArgCommand { + for i := range metaArgs { + arg := metaArgs[i] + v := arg.Value + if v != nil { + val := *v + if val[0] == '"' { + val = val[1:] + } + + if val[len(val)-1] == '"' { + val = val[:len(val)-1] + } + arg.Value = &val + metaArgs[i] = arg + } + } + return metaArgs +} + // targetStage returns the index of the target stage kaniko is trying to build func targetStage(stages []instructions.Stage, target string) (int, error) { if target == "" { diff --git a/pkg/dockerfile/dockerfile_test.go b/pkg/dockerfile/dockerfile_test.go index 1fa68890c6..45c26bdbb1 100644 --- a/pkg/dockerfile/dockerfile_test.go +++ b/pkg/dockerfile/dockerfile_test.go @@ -17,13 +17,64 @@ limitations under the License. package dockerfile import ( + "io/ioutil" + "os" "strconv" "testing" + "github.com/GoogleContainerTools/kaniko/pkg/config" "github.com/GoogleContainerTools/kaniko/testutil" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) +func TestStagesArgValueWithQuotes(t *testing.T) { + dockerfile := ` + ARG IMAGE="ubuntu:16.04" + FROM ${IMAGE} + RUN echo hi > /hi + + FROM scratch AS second + COPY --from=0 /hi /hi2 + + FROM scratch + COPY --from=second /hi2 /hi3 + ` + tmpfile, err := ioutil.TempFile("", "Dockerfile.test") + if err != nil { + t.Fatal(err) + } + + defer os.Remove(tmpfile.Name()) + + if _, err := tmpfile.Write([]byte(dockerfile)); err != nil { + t.Fatal(err) + } + if err := tmpfile.Close(); err != nil { + t.Fatal(err) + } + + stages, err := Stages(&config.KanikoOptions{DockerfilePath: tmpfile.Name()}) + if err != nil { + t.Fatal(err) + } + + if len(stages) == 0 { + t.Fatal("length of stages expected to be greater than zero, but was zero") + + } + + if len(stages[0].MetaArgs) == 0 { + t.Fatal("length of stage[0] meta args expected to be greater than zero, but was zero") + } + + expectedVal := "ubuntu:16.04" + + arg := stages[0].MetaArgs[0] + if arg.ValueString() != expectedVal { + t.Fatalf("expected stages[0].MetaArgs[0] val to be %s but was %s", expectedVal, arg.ValueString()) + } +} + func Test_resolveStages(t *testing.T) { dockerfile := ` FROM scratch