test(secret-engines/vault): table-test key validation
This commit is contained in:
2 files changed
+30
-10
No files matched your search
@@ -21,11 +21,7 @@ func resolve(ctx context.Context, key string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// The key reaches the vault CLI as arguments; a leading "-" would be read
|
||||
// as a flag such as -address.
|
||||
if strings.HasPrefix(path, "-") || field == "" {
|
||||
return "", fmt.Errorf("key %q is not a valid path/field", key)
|
||||
}
|
||||
|
||||
value, err := secretengine.Run(ctx, "vault", "kv", "get", "-field="+field, path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read %s/%s: %w", path, field, err)
|
||||
@@ -36,15 +32,18 @@ func resolve(ctx context.Context, key string) (string, error) {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// target splits key into the CLI's path and field. Both reach the vault CLI as
|
||||
// arguments, so a path with a leading "-" is refused: it would be read as a
|
||||
// flag such as -address.
|
||||
func target(key string) (path, field string, err error) {
|
||||
path, field = os.Getenv("VAULT_SECRET_PATH"), key
|
||||
if i := strings.LastIndex(key, "/"); i >= 0 {
|
||||
return key[:i], key[i+1:], nil
|
||||
path, field = key[:i], key[i+1:]
|
||||
}
|
||||
prefix := os.Getenv("VAULT_SECRET_PATH")
|
||||
if prefix == "" {
|
||||
return "", "", errors.New("key has no path and VAULT_SECRET_PATH is not set")
|
||||
if path == "" || field == "" || strings.HasPrefix(path, "-") {
|
||||
return "", "", fmt.Errorf("key %q is not a valid path/field (a key with no path needs VAULT_SECRET_PATH)", key)
|
||||
}
|
||||
return prefix, key, nil
|
||||
return path, field, nil
|
||||
}
|
||||
|
||||
func main() { secretengine.ServeFunc(resolve) }
|
||||
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestTarget(t *testing.T) {
|
||||
t.Setenv("VAULT_SECRET_PATH", "")
|
||||
for _, tc := range []struct {
|
||||
key, path, field string
|
||||
ok bool
|
||||
}{
|
||||
{"secret/app/token", "secret/app", "token", true},
|
||||
{"-address=http://evil/x", "", "", false},
|
||||
{"secret/app/", "", "", false},
|
||||
{"token", "", "", false},
|
||||
} {
|
||||
path, field, err := target(tc.key)
|
||||
if (err == nil) != tc.ok || path != tc.path || field != tc.field {
|
||||
t.Errorf("target(%q) = %q, %q, %v", tc.key, path, field, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user