Skip to content

Commit

Permalink
cmd/entrypoint: do not interpret anything after --
Browse files Browse the repository at this point in the history
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 <vdemeest@redhat.com>
  • Loading branch information
vdemeester committed Jul 6, 2022
1 parent 6876520 commit faa9f89
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
25 changes: 22 additions & 3 deletions cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,28 @@ func main() {
gitcreds.AddFlags(flag.CommandLine)
dockercreds.AddFlags(flag.CommandLine)

flag.Parse()
// Detect if `--` is present, if it is, parse only the one before.
terminatorIndex := -1
commandArgs := []string{}
args := os.Args[1:]
for i, a := range os.Args {
if a == "--" {
terminatorIndex = i
break
}
}
if terminatorIndex > 0 {
commandArgs = os.Args[terminatorIndex+1:]
args = os.Args[1:terminatorIndex]
} else {
args = os.Args[1:]
}

if err := subcommands.Process(flag.Args()); err != nil {
// We are using the global variable flag.CommandLine here to be able
// to define what args it should parse.
// flag.Parse() does flag.CommandLine.Parse(os.Args[1:])
flag.CommandLine.Parse(args)
if err := subcommands.Process(flag.CommandLine.Args()); err != nil {
log.Println(err.Error())
switch err.(type) {
case subcommands.SubcommandSuccessful:
Expand Down Expand Up @@ -123,7 +142,7 @@ func main() {
}

e := entrypoint.Entrypointer{
Command: append(cmd, flag.Args()...),
Command: append(cmd, commandArgs...),
WaitFiles: strings.Split(*waitFiles, ","),
WaitFileContent: *waitFileContent,
PostFile: *postFile,
Expand Down
27 changes: 27 additions & 0 deletions examples/v1beta1/taskruns/5080-entrypoint-init-regression.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
generateName: tkn-arg-test-
spec:
params:
- name: ARGS
value:
- init
- foo
- bar
taskSpec:
description: >-
Test consuming args, acts as a regression test for bug 5080
params:
- name: ARGS
description: The terraform cli commands to tun
type: array
default:
- "--help"
steps:
- name: echo-cli
image: registry.access.redhat.com/ubi9/ubi-minimal:9.0.0-1580
workingDir: /tekton/home
args:
- "$(params.ARGS)"
command: ["echo"]

0 comments on commit faa9f89

Please sign in to comment.