diff --git a/command/token/helper.go b/command/token/helper.go index 04ba75ed7d..85157a3f6b 100644 --- a/command/token/helper.go +++ b/command/token/helper.go @@ -4,8 +4,29 @@ import ( "bytes" "fmt" "os/exec" + "path/filepath" + "strings" ) +// HelperPath takes the configured path to a helper and expands it to +// a full absolute path that can be executed. If the path is relative then +// a prefix of "vault token-" will be prepended to the path. +func HelperPath(path string) string { + space := strings.Index(path, " ") + if space == -1 { + space = len(path) + } + + // Get the binary name. If it isn't absolute, prepend "vault token-" + binary := path[0:space] + if !filepath.IsAbs(binary) { + binary = "vault token-" + binary + } + + // Return the resulting string + return fmt.Sprintf("%s%s", binary, path[space:]) +} + // Helper is the struct that has all the logic for storing and retrieving // tokens from the token helper. The API for the helpers is simple: the // Path is executed within a shell. The last argument appended will be the diff --git a/command/token/helper_test.go b/command/token/helper_test.go index 84baea15a8..8038a1a7f4 100644 --- a/command/token/helper_test.go +++ b/command/token/helper_test.go @@ -9,6 +9,22 @@ import ( "testing" ) +func TestHelperPath(t *testing.T) { + cases := map[string]string{ + "/foo": "/foo", + "foo": "vault token-foo", + } + + for k, v := range cases { + actual := HelperPath(k) + if actual != v { + t.Fatalf( + "input: %s, expected: %s, got: %s", + k, v, actual) + } + } +} + func TestHelper(t *testing.T) { h := testHelper(t) Test(t, h.Path)