Skip to content

Commit a6c35b6

Browse files
afresh1RBird111
andauthored
Directly exec ExternalTokenHelper rather than using a SHELL (#29653)
* [OT] use `new` builtin for visual clarity `new(ExternalTokenHelper)` is a lot easier to parse than `(*ExternalTokenHelper)(nil)` * add `Args` field to `ExternalTokenHelper` This will be used to store any extra command arguments and allows `BinaryPath` to hold *just* the binary path. * remove shell invocation Since `BinPath` no longer has to hold any additional arguments we can execute the command directly without inoking the shell first. * update `testExternalTokenHelper` to make use of the new `Args` field * updated `ExternalTokenHelper` documentation * Add changelog entry for token_helper without shell Currently using 0.txt until we have a PR id. * Rename 0.txt to 29653.txt We got a PR ID, so fix the changelog file --------- Co-authored-by: Roosevelt Burden <[email protected]> Co-authored-by: Roosevelt Burden <[email protected]>
1 parent 70b3ff7 commit a6c35b6

File tree

3 files changed

+16
-37
lines changed

3 files changed

+16
-37
lines changed

api/tokenhelper/helper_external.go

+11-26
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12-
"runtime"
1312
"strings"
1413
)
1514

@@ -40,14 +39,14 @@ func ExternalTokenHelperPath(path string) (string, error) {
4039
return path, nil
4140
}
4241

43-
var _ TokenHelper = (*ExternalTokenHelper)(nil)
42+
var _ TokenHelper = new(ExternalTokenHelper)
4443

4544
// ExternalTokenHelper should only be used in a dev mode. For all other cases,
4645
// InternalTokenHelper should be used.
4746
// ExternalTokenHelper is the struct that has all the logic for storing and retrieving
4847
// tokens from the token helper. The API for the helpers is simple: the
49-
// BinaryPath is executed within a shell with environment Env. The last argument
50-
// appended will be the operation, which is:
48+
// BinaryPath is executed directly with arguments Args and environment Env.
49+
// The last argument appended to Args will be the operation, which is:
5150
//
5251
// - "get" - Read the value of the token and write it to stdout.
5352
// - "store" - Store the value of the token which is on stdin. Output
@@ -58,6 +57,7 @@ var _ TokenHelper = (*ExternalTokenHelper)(nil)
5857
// exit code then the stderr will be made part of the error value.
5958
type ExternalTokenHelper struct {
6059
BinaryPath string
60+
Args []string
6161
Env []string
6262
}
6363

@@ -109,28 +109,13 @@ func (h *ExternalTokenHelper) Path() string {
109109
}
110110

111111
func (h *ExternalTokenHelper) cmd(op string) (*exec.Cmd, error) {
112-
script := strings.ReplaceAll(h.BinaryPath, "\\", "\\\\") + " " + op
113-
cmd, err := execScript(script)
114-
if err != nil {
115-
return nil, err
116-
}
117-
cmd.Env = h.Env
118-
return cmd, nil
119-
}
112+
binPath := strings.ReplaceAll(h.BinaryPath, "\\", "\\\\")
120113

121-
// execScript returns a command to execute a script
122-
func execScript(script string) (*exec.Cmd, error) {
123-
var shell, flag string
124-
if runtime.GOOS == "windows" {
125-
shell = "cmd"
126-
flag = "/C"
127-
} else {
128-
shell = "/bin/sh"
129-
flag = "-c"
130-
}
131-
if other := os.Getenv("SHELL"); other != "" {
132-
shell = other
133-
}
134-
cmd := exec.Command(shell, flag, script)
114+
args := make([]string, len(h.Args))
115+
copy(args, h.Args)
116+
args = append(args, op)
117+
118+
cmd := exec.Command(binPath, args...)
119+
cmd.Env = h.Env
135120
return cmd, nil
136121
}

api/tokenhelper/helper_external_test.go

+2-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io"
99
"os"
1010
"runtime"
11-
"strings"
1211
"testing"
1312
)
1413

@@ -57,16 +56,8 @@ func TestExternalTokenHelper(t *testing.T) {
5756
}
5857

5958
func testExternalTokenHelper() *ExternalTokenHelper {
60-
return &ExternalTokenHelper{BinaryPath: helperPath("helper"), Env: helperEnv()}
61-
}
62-
63-
func helperPath(s ...string) string {
64-
cs := []string{"-test.run=TestExternalTokenHelperProcess", "--"}
65-
cs = append(cs, s...)
66-
return fmt.Sprintf(
67-
"%s %s",
68-
os.Args[0],
69-
strings.Join(cs, " "))
59+
args := []string{"-test.run=TestExternalTokenHelperProcess", "--", "helper"}
60+
return &ExternalTokenHelper{BinaryPath: os.Args[0], Args: args, Env: helperEnv()}
7061
}
7162

7263
func helperEnv() []string {

changelog/29653.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
api/tokenhelper: Exec token_helper without a shell
3+
```

0 commit comments

Comments
 (0)