Skip to content

Commit 6a0575d

Browse files
vdemeestertekton-robot
authored andcommitted
cmd/entrypoint: do not interpret anything after --
The way the `flag` package works, it can eat the flag "terminator" (aka the double dash `--`). This means that, in some very specific cases — where the first item after the `--` is also a subcommand, it would execute the entrypoint subcommand instead of the actual command. For example : ``` $ /ko-app/entrypoint -- init a b $ /ko-app/entrypoint init a b ``` This is fixed by making sure we remove anything after `--` for the subcommand processing. And then we pass the rest (after `--`) to the entrypointer to be executed. Signed-off-by: Vincent Demeester <[email protected]> (cherry picked from commit da91bf3) Signed-off-by: Vincent Demeester <[email protected]>
1 parent a59ccb5 commit 6a0575d

File tree

4 files changed

+143
-4
lines changed

4 files changed

+143
-4
lines changed

cmd/entrypoint/args.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2022 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
func extractArgs(initialArgs []string) ([]string, []string) {
20+
commandArgs := []string{}
21+
args := initialArgs
22+
if len(initialArgs) == 0 {
23+
return args, commandArgs
24+
}
25+
// Detect if `--` is present, if it is, parse only the one before.
26+
terminatorIndex := -1
27+
for i, a := range initialArgs {
28+
if a == "--" {
29+
terminatorIndex = i
30+
break
31+
}
32+
}
33+
if terminatorIndex > 0 {
34+
commandArgs = initialArgs[terminatorIndex+1:]
35+
args = initialArgs[:terminatorIndex]
36+
}
37+
return args, commandArgs
38+
}

cmd/entrypoint/args_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2022 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
)
24+
25+
func TestExtractArgs(t *testing.T) {
26+
for _, c := range []struct {
27+
desc string
28+
args []string
29+
expectedArgs []string
30+
expectedCommandArgs []string
31+
}{{
32+
desc: "empty arguments",
33+
args: []string{},
34+
expectedArgs: []string{},
35+
expectedCommandArgs: []string{},
36+
}, {
37+
desc: "with args",
38+
args: []string{"--foo", "bar", "a", "b", "c"},
39+
expectedArgs: []string{"--foo", "bar", "a", "b", "c"},
40+
expectedCommandArgs: []string{},
41+
}, {
42+
desc: "with terminator on its own",
43+
args: []string{"--foo", "bar", "--"},
44+
expectedArgs: []string{"--foo", "bar"},
45+
expectedCommandArgs: []string{},
46+
}, {
47+
desc: "with terminator",
48+
args: []string{"--foo", "bar", "--", "a", "b", "c"},
49+
expectedArgs: []string{"--foo", "bar"},
50+
expectedCommandArgs: []string{"a", "b", "c"},
51+
}, {
52+
desc: "with args and terminator",
53+
args: []string{"--foo", "bar", "baz", "--", "a", "b", "c"},
54+
expectedArgs: []string{"--foo", "bar", "baz"},
55+
expectedCommandArgs: []string{"a", "b", "c"},
56+
}} {
57+
t.Run(c.desc, func(t *testing.T) {
58+
args, commandArgs := extractArgs(c.args)
59+
if d := cmp.Diff(c.expectedArgs, args); d != "" {
60+
t.Errorf("args: diff(-want,+got):\n%s", d)
61+
}
62+
if d := cmp.Diff(c.expectedCommandArgs, commandArgs); d != "" {
63+
t.Errorf("commandArgs: diff(-want,+got):\n%s", d)
64+
}
65+
})
66+
}
67+
}

cmd/entrypoint/main.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,16 @@ func main() {
7878
gitcreds.AddFlags(flag.CommandLine)
7979
dockercreds.AddFlags(flag.CommandLine)
8080

81-
flag.Parse()
82-
83-
if err := subcommands.Process(flag.Args()); err != nil {
81+
// Split args with `--` for the entrypoint and what it should execute
82+
args, commandArgs := extractArgs(os.Args[1:])
83+
84+
// We are using the global variable flag.CommandLine here to be able
85+
// to define what args it should parse.
86+
// flag.Parse() does flag.CommandLine.Parse(os.Args[1:])
87+
if err := flag.CommandLine.Parse(args); err != nil {
88+
os.Exit(1)
89+
}
90+
if err := subcommands.Process(flag.CommandLine.Args()); err != nil {
8491
log.Println(err.Error())
8592
switch err.(type) {
8693
case subcommands.SubcommandSuccessful:
@@ -123,7 +130,7 @@ func main() {
123130
}
124131

125132
e := entrypoint.Entrypointer{
126-
Command: append(cmd, flag.Args()...),
133+
Command: append(cmd, commandArgs...),
127134
WaitFiles: strings.Split(*waitFiles, ","),
128135
WaitFileContent: *waitFileContent,
129136
PostFile: *postFile,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: TaskRun
3+
metadata:
4+
generateName: tkn-arg-test-
5+
spec:
6+
params:
7+
- name: ARGS
8+
value:
9+
- init
10+
- foo
11+
- bar
12+
taskSpec:
13+
description: >-
14+
Test consuming args, acts as a regression test for bug 5080
15+
params:
16+
- name: ARGS
17+
description: The terraform cli commands to tun
18+
type: array
19+
default:
20+
- "--help"
21+
steps:
22+
- name: echo-cli
23+
image: registry.access.redhat.com/ubi9/ubi-minimal:9.0.0-1580
24+
workingDir: /tekton/home
25+
args:
26+
- "$(params.ARGS)"
27+
command: ["echo"]

0 commit comments

Comments
 (0)