command/token: HelperPath

This commit is contained in:
Mitchell Hashimoto 2015-03-30 10:11:17 -07:00
parent 27bc188758
commit cb09c95824
2 changed files with 37 additions and 0 deletions

View File

@ -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

View File

@ -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)