Skip to content

Commit

Permalink
Issue #439 double quotes in ARG value
Browse files Browse the repository at this point in the history
* Strip out double quotes enclosing ARG value after parsing
dockerfile
  • Loading branch information
cvgw committed Oct 24, 2019
1 parent cbf4de8 commit 79649a1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
12 changes: 12 additions & 0 deletions integration/dockerfiles/Dockerfile_test_arg_multi_with_quotes
Original file line number Diff line number Diff line change
@@ -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 /
24 changes: 24 additions & 0 deletions pkg/dockerfile/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
51 changes: 51 additions & 0 deletions pkg/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 79649a1

Please sign in to comment.