2017-08-28 20:44:35 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/strutil"
|
2017-08-28 20:44:35 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPredictVaultPaths(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
data := map[string]interface{}{"a": "b"}
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Logical().Write("secret/bar", data); err != nil {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Logical().Write("secret/foo", data); err != nil {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Logical().Write("secret/zip/zap", data); err != nil {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Logical().Write("secret/zip/zonk", data); err != nil {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Logical().Write("secret/zip/twoot", data); err != nil {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-07 19:12:58 +00:00
|
|
|
if err := client.Sys().Mount("level1a/level2a/level3a", &api.MountInput{Type: "kv"}); err != nil {
|
2020-02-07 14:58:30 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-07 19:12:58 +00:00
|
|
|
if err := client.Sys().Mount("level1a/level2a/level3b", &api.MountInput{Type: "kv"}); err != nil {
|
2020-02-07 14:58:30 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-08-28 20:44:35 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
2017-08-28 21:05:09 +00:00
|
|
|
name string
|
|
|
|
args complete.Args
|
|
|
|
includeFiles bool
|
|
|
|
exp []string
|
2017-08-28 20:44:35 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"has_args",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret/foo", "a=b"},
|
|
|
|
Last: "a=b",
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
true,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"has_args_no_files",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret/foo", "a=b"},
|
|
|
|
Last: "a=b",
|
|
|
|
},
|
|
|
|
false,
|
2017-08-28 20:44:35 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"part_mount",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "s"},
|
|
|
|
Last: "s",
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
true,
|
|
|
|
[]string{"secret/", "sys/"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"part_mount_no_files",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "s"},
|
|
|
|
Last: "s",
|
|
|
|
},
|
|
|
|
false,
|
2017-08-28 20:44:35 +00:00
|
|
|
[]string{"secret/", "sys/"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"only_mount",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "sec"},
|
|
|
|
Last: "sec",
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
true,
|
2017-08-28 20:44:35 +00:00
|
|
|
[]string{"secret/bar", "secret/foo", "secret/zip/"},
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
{
|
|
|
|
"only_mount_no_files",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "sec"},
|
|
|
|
Last: "sec",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
[]string{"secret/zip/"},
|
|
|
|
},
|
2017-08-28 20:44:35 +00:00
|
|
|
{
|
|
|
|
"full_mount",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret"},
|
|
|
|
Last: "secret",
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
true,
|
2017-08-28 20:44:35 +00:00
|
|
|
[]string{"secret/bar", "secret/foo", "secret/zip/"},
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
{
|
|
|
|
"full_mount_no_files",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret"},
|
|
|
|
Last: "secret",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
[]string{"secret/zip/"},
|
|
|
|
},
|
2017-08-28 20:44:35 +00:00
|
|
|
{
|
|
|
|
"full_mount_slash",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret/"},
|
|
|
|
Last: "secret/",
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
true,
|
2017-08-28 20:44:35 +00:00
|
|
|
[]string{"secret/bar", "secret/foo", "secret/zip/"},
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
{
|
|
|
|
"full_mount_slash_no_files",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret/"},
|
|
|
|
Last: "secret/",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
[]string{"secret/zip/"},
|
|
|
|
},
|
2017-08-28 20:44:35 +00:00
|
|
|
{
|
|
|
|
"path_partial",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret/z"},
|
|
|
|
Last: "secret/z",
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
true,
|
2017-08-28 20:44:35 +00:00
|
|
|
[]string{"secret/zip/twoot", "secret/zip/zap", "secret/zip/zonk"},
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
{
|
|
|
|
"path_partial_no_files",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret/z"},
|
|
|
|
Last: "secret/z",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
[]string{"secret/zip/"},
|
|
|
|
},
|
2017-08-28 20:44:35 +00:00
|
|
|
{
|
|
|
|
"subpath_partial_z",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret/zip/z"},
|
|
|
|
Last: "secret/zip/z",
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
true,
|
2017-08-28 20:44:35 +00:00
|
|
|
[]string{"secret/zip/zap", "secret/zip/zonk"},
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
{
|
|
|
|
"subpath_partial_z_no_files",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret/zip/z"},
|
|
|
|
Last: "secret/zip/z",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
[]string{"secret/zip/z"},
|
|
|
|
},
|
2017-08-28 20:44:35 +00:00
|
|
|
{
|
|
|
|
"subpath_partial_t",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret/zip/t"},
|
|
|
|
Last: "secret/zip/t",
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
true,
|
2017-08-28 20:44:35 +00:00
|
|
|
[]string{"secret/zip/twoot"},
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
{
|
|
|
|
"subpath_partial_t_no_files",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "secret/zip/t"},
|
|
|
|
Last: "secret/zip/t",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
[]string{"secret/zip/t"},
|
|
|
|
},
|
2020-02-07 14:58:30 +00:00
|
|
|
{
|
|
|
|
"multi_nested",
|
|
|
|
complete.Args{
|
|
|
|
All: []string{"read", "level1a/level2a"},
|
|
|
|
Last: "level1a/level2a",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
[]string{
|
|
|
|
"level1a/level2a/level3a/",
|
|
|
|
"level1a/level2a/level3b/",
|
|
|
|
},
|
|
|
|
},
|
2017-08-28 20:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("group", func(t *testing.T) {
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
p := NewPredict()
|
|
|
|
p.client = client
|
|
|
|
|
|
|
|
f := p.vaultPaths(tc.includeFiles)
|
2017-08-28 20:44:35 +00:00
|
|
|
act := f(tc.args)
|
|
|
|
if !reflect.DeepEqual(act, tc.exp) {
|
|
|
|
t.Errorf("expected %q to be %q", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
func TestPredict_Audits(t *testing.T) {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
badClient, badCloser := testVaultServerBad(t)
|
|
|
|
defer badCloser()
|
|
|
|
|
2022-04-07 19:12:58 +00:00
|
|
|
if err := client.Sys().EnableAuditWithOptions("file", &api.EnableAuditOptions{
|
2017-09-05 03:55:19 +00:00
|
|
|
Type: "file",
|
|
|
|
Options: map[string]string{
|
|
|
|
"file_path": "discard",
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-08-28 20:44:35 +00:00
|
|
|
cases := []struct {
|
2017-09-05 03:55:19 +00:00
|
|
|
name string
|
|
|
|
client *api.Client
|
|
|
|
exp []string
|
2017-08-28 20:44:35 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-09-05 03:55:19 +00:00
|
|
|
"not_connected_client",
|
|
|
|
badClient,
|
2017-08-28 20:44:35 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2017-09-05 03:55:19 +00:00
|
|
|
"good_path",
|
|
|
|
client,
|
|
|
|
[]string{"file/"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("group", func(t *testing.T) {
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
p := NewPredict()
|
|
|
|
p.client = tc.client
|
|
|
|
|
|
|
|
act := p.audits()
|
|
|
|
if !reflect.DeepEqual(act, tc.exp) {
|
|
|
|
t.Errorf("expected %q to be %q", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPredict_Mounts(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
badClient, badCloser := testVaultServerBad(t)
|
|
|
|
defer badCloser()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
client *api.Client
|
|
|
|
exp []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"not_connected_client",
|
|
|
|
badClient,
|
|
|
|
defaultPredictVaultMounts,
|
2017-08-28 20:44:35 +00:00
|
|
|
},
|
|
|
|
{
|
2017-09-05 03:55:19 +00:00
|
|
|
"good_path",
|
|
|
|
client,
|
2018-01-10 16:33:53 +00:00
|
|
|
[]string{"cubbyhole/", "identity/", "secret/", "sys/"},
|
2017-08-28 20:44:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("group", func(t *testing.T) {
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
p := NewPredict()
|
2017-09-05 03:55:19 +00:00
|
|
|
p.client = tc.client
|
|
|
|
|
|
|
|
act := p.mounts()
|
|
|
|
if !reflect.DeepEqual(act, tc.exp) {
|
|
|
|
t.Errorf("expected %q to be %q", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:40:33 +00:00
|
|
|
func TestPredict_Plugins(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
badClient, badCloser := testVaultServerBad(t)
|
|
|
|
defer badCloser()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
client *api.Client
|
|
|
|
exp []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"not_connected_client",
|
|
|
|
badClient,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"good_path",
|
|
|
|
client,
|
|
|
|
[]string{
|
2018-11-07 01:21:24 +00:00
|
|
|
"ad",
|
|
|
|
"alicloud",
|
|
|
|
"app-id",
|
|
|
|
"approle",
|
|
|
|
"aws",
|
|
|
|
"azure",
|
|
|
|
"cassandra",
|
2018-03-26 17:40:33 +00:00
|
|
|
"cassandra-database-plugin",
|
2018-11-07 01:21:24 +00:00
|
|
|
"centrify",
|
|
|
|
"cert",
|
2019-08-26 16:55:08 +00:00
|
|
|
"cf",
|
2018-11-07 01:21:24 +00:00
|
|
|
"consul",
|
2020-10-22 23:20:17 +00:00
|
|
|
"couchbase-database-plugin",
|
|
|
|
"elasticsearch-database-plugin",
|
2018-11-07 01:21:24 +00:00
|
|
|
"gcp",
|
|
|
|
"gcpkms",
|
|
|
|
"github",
|
2018-03-26 17:40:33 +00:00
|
|
|
"hana-database-plugin",
|
2019-01-10 01:25:40 +00:00
|
|
|
"influxdb-database-plugin",
|
2018-11-07 01:21:24 +00:00
|
|
|
"jwt",
|
2019-12-11 19:18:37 +00:00
|
|
|
"kerberos",
|
2020-10-29 23:47:34 +00:00
|
|
|
"keymgmt",
|
2019-06-21 00:55:10 +00:00
|
|
|
"kmip",
|
2018-11-07 01:21:24 +00:00
|
|
|
"kubernetes",
|
|
|
|
"kv",
|
|
|
|
"ldap",
|
|
|
|
"mongodb",
|
2018-03-26 17:40:33 +00:00
|
|
|
"mongodb-database-plugin",
|
2020-02-07 22:09:39 +00:00
|
|
|
"mongodbatlas",
|
2020-10-22 23:20:17 +00:00
|
|
|
"mongodbatlas-database-plugin",
|
2018-11-07 01:21:24 +00:00
|
|
|
"mssql",
|
2018-03-26 17:40:33 +00:00
|
|
|
"mssql-database-plugin",
|
2018-11-07 01:21:24 +00:00
|
|
|
"mysql",
|
2018-03-26 17:40:33 +00:00
|
|
|
"mysql-aurora-database-plugin",
|
|
|
|
"mysql-database-plugin",
|
|
|
|
"mysql-legacy-database-plugin",
|
|
|
|
"mysql-rds-database-plugin",
|
2018-11-07 01:21:24 +00:00
|
|
|
"nomad",
|
2019-09-05 20:50:12 +00:00
|
|
|
"oci",
|
2019-02-14 20:13:34 +00:00
|
|
|
"oidc",
|
2018-11-07 01:21:24 +00:00
|
|
|
"okta",
|
2020-02-15 18:21:07 +00:00
|
|
|
"openldap",
|
2019-08-26 16:55:08 +00:00
|
|
|
"pcf", // Deprecated.
|
2018-11-07 01:21:24 +00:00
|
|
|
"pki",
|
|
|
|
"postgresql",
|
2018-03-26 17:40:33 +00:00
|
|
|
"postgresql-database-plugin",
|
2018-11-07 01:21:24 +00:00
|
|
|
"rabbitmq",
|
|
|
|
"radius",
|
2022-09-09 18:42:25 +00:00
|
|
|
"redis-database-plugin",
|
2022-09-09 20:16:30 +00:00
|
|
|
"redis-elasticache-database-plugin",
|
2020-02-13 17:42:30 +00:00
|
|
|
"redshift-database-plugin",
|
2021-01-07 17:30:24 +00:00
|
|
|
"snowflake-database-plugin",
|
2018-11-07 01:21:24 +00:00
|
|
|
"ssh",
|
2021-02-19 22:38:56 +00:00
|
|
|
"terraform",
|
2018-11-07 01:21:24 +00:00
|
|
|
"totp",
|
2020-04-07 18:26:51 +00:00
|
|
|
"transform",
|
2018-11-07 01:21:24 +00:00
|
|
|
"transit",
|
|
|
|
"userpass",
|
2018-03-26 17:40:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("group", func(t *testing.T) {
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
p := NewPredict()
|
|
|
|
p.client = tc.client
|
|
|
|
|
|
|
|
act := p.plugins()
|
2019-06-21 00:57:46 +00:00
|
|
|
|
2020-10-29 23:47:34 +00:00
|
|
|
if !strutil.StrListContains(act, "keymgmt") {
|
|
|
|
for i, v := range tc.exp {
|
|
|
|
if v == "keymgmt" {
|
|
|
|
tc.exp = append(tc.exp[:i], tc.exp[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 00:57:46 +00:00
|
|
|
if !strutil.StrListContains(act, "kmip") {
|
2019-06-21 01:00:01 +00:00
|
|
|
for i, v := range tc.exp {
|
2019-06-21 00:57:46 +00:00
|
|
|
if v == "kmip" {
|
2019-06-21 01:00:01 +00:00
|
|
|
tc.exp = append(tc.exp[:i], tc.exp[i+1:]...)
|
2019-06-21 00:57:46 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:26:51 +00:00
|
|
|
if !strutil.StrListContains(act, "transform") {
|
|
|
|
for i, v := range tc.exp {
|
|
|
|
if v == "transform" {
|
|
|
|
tc.exp = append(tc.exp[:i], tc.exp[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 17:40:33 +00:00
|
|
|
if !reflect.DeepEqual(act, tc.exp) {
|
2020-02-07 22:09:39 +00:00
|
|
|
t.Errorf("expected:%q, got: %q", tc.exp, act)
|
2018-03-26 17:40:33 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
func TestPredict_Policies(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
badClient, badCloser := testVaultServerBad(t)
|
|
|
|
defer badCloser()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
client *api.Client
|
|
|
|
exp []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"not_connected_client",
|
|
|
|
badClient,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"good_path",
|
|
|
|
client,
|
|
|
|
[]string{"default", "root"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("group", func(t *testing.T) {
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
p := NewPredict()
|
|
|
|
p.client = tc.client
|
2017-08-29 04:24:22 +00:00
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
act := p.policies()
|
2017-08-28 20:44:35 +00:00
|
|
|
if !reflect.DeepEqual(act, tc.exp) {
|
|
|
|
t.Errorf("expected %q to be %q", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
func TestPredict_Paths(t *testing.T) {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
data := map[string]interface{}{"a": "b"}
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Logical().Write("secret/bar", data); err != nil {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Logical().Write("secret/foo", data); err != nil {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Logical().Write("secret/zip/zap", data); err != nil {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
2017-08-28 21:05:09 +00:00
|
|
|
name string
|
|
|
|
path string
|
|
|
|
includeFiles bool
|
|
|
|
exp []string
|
2017-08-28 20:44:35 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"bad_path",
|
|
|
|
"nope/not/a/real/path/ever",
|
2017-08-28 21:05:09 +00:00
|
|
|
true,
|
2017-08-28 20:44:35 +00:00
|
|
|
[]string{"nope/not/a/real/path/ever"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"good_path",
|
|
|
|
"secret/",
|
2017-08-28 21:05:09 +00:00
|
|
|
true,
|
2017-08-28 20:44:35 +00:00
|
|
|
[]string{"secret/bar", "secret/foo", "secret/zip/"},
|
|
|
|
},
|
2017-08-28 21:05:09 +00:00
|
|
|
{
|
|
|
|
"good_path_no_files",
|
|
|
|
"secret/",
|
|
|
|
false,
|
|
|
|
[]string{"secret/zip/"},
|
|
|
|
},
|
2017-08-28 20:44:35 +00:00
|
|
|
{
|
|
|
|
"partial_match",
|
|
|
|
"secret/z",
|
2017-08-28 21:05:09 +00:00
|
|
|
true,
|
|
|
|
[]string{"secret/zip/"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"partial_match_no_files",
|
|
|
|
"secret/z",
|
|
|
|
false,
|
2017-08-28 20:44:35 +00:00
|
|
|
[]string{"secret/zip/"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("group", func(t *testing.T) {
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
p := NewPredict()
|
|
|
|
p.client = client
|
|
|
|
|
2022-09-13 16:11:00 +00:00
|
|
|
act := p.paths("kv", "1", tc.path, tc.includeFiles)
|
|
|
|
if !reflect.DeepEqual(act, tc.exp) {
|
|
|
|
t.Errorf("expected %q to be %q", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPredict_PathsKVv2(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServerWithKVVersion(t, "2")
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
data := map[string]interface{}{"data": map[string]interface{}{"a": "b"}}
|
|
|
|
if _, err := client.Logical().Write("secret/data/bar", data); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := client.Logical().Write("secret/data/foo", data); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := client.Logical().Write("secret/data/zip/zap", data); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
includeFiles bool
|
|
|
|
exp []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"bad_path",
|
|
|
|
"nope/not/a/real/path/ever",
|
|
|
|
true,
|
|
|
|
[]string{"nope/not/a/real/path/ever"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"good_path",
|
|
|
|
"secret/",
|
|
|
|
true,
|
|
|
|
[]string{"secret/bar", "secret/foo", "secret/zip/"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"good_path_no_files",
|
|
|
|
"secret/",
|
|
|
|
false,
|
|
|
|
[]string{"secret/zip/"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"partial_match",
|
|
|
|
"secret/z",
|
|
|
|
true,
|
|
|
|
[]string{"secret/zip/"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"partial_match_no_files",
|
|
|
|
"secret/z",
|
|
|
|
false,
|
|
|
|
[]string{"secret/zip/"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("group", func(t *testing.T) {
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
p := NewPredict()
|
|
|
|
p.client = client
|
|
|
|
|
|
|
|
act := p.paths("kv", "2", tc.path, tc.includeFiles)
|
2017-08-28 20:44:35 +00:00
|
|
|
if !reflect.DeepEqual(act, tc.exp) {
|
|
|
|
t.Errorf("expected %q to be %q", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
func TestPredict_ListPaths(t *testing.T) {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
2017-09-05 03:55:19 +00:00
|
|
|
badClient, badCloser := testVaultServerBad(t)
|
|
|
|
defer badCloser()
|
|
|
|
|
2017-08-28 20:44:35 +00:00
|
|
|
data := map[string]interface{}{"a": "b"}
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Logical().Write("secret/bar", data); err != nil {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-07 19:12:58 +00:00
|
|
|
if _, err := client.Logical().Write("secret/foo", data); err != nil {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
client *api.Client
|
|
|
|
path string
|
|
|
|
exp []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"bad_path",
|
|
|
|
client,
|
|
|
|
"nope/not/a/real/path/ever",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"good_path",
|
|
|
|
client,
|
|
|
|
"secret/",
|
|
|
|
[]string{"bar", "foo"},
|
|
|
|
},
|
2017-09-05 03:55:19 +00:00
|
|
|
{
|
|
|
|
"not_connected_client",
|
|
|
|
badClient,
|
|
|
|
"secret/",
|
|
|
|
nil,
|
|
|
|
},
|
2017-08-28 20:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("group", func(t *testing.T) {
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
p := NewPredict()
|
2017-09-05 03:55:19 +00:00
|
|
|
p.client = tc.client
|
2017-08-29 04:24:22 +00:00
|
|
|
|
|
|
|
act := p.listPaths(tc.path)
|
2017-08-28 20:44:35 +00:00
|
|
|
if !reflect.DeepEqual(act, tc.exp) {
|
|
|
|
t.Errorf("expected %q to be %q", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
func TestPredict_HasPathArg(t *testing.T) {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
exp bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil",
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty",
|
|
|
|
[]string{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty_string",
|
|
|
|
[]string{""},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"single",
|
|
|
|
[]string{"foo"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"multiple",
|
|
|
|
[]string{"foo", "bar", "baz"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-08-29 04:24:22 +00:00
|
|
|
p := NewPredict()
|
|
|
|
if act := p.hasPathArg(tc.args); act != tc.exp {
|
2017-08-28 20:44:35 +00:00
|
|
|
t.Errorf("expected %t to be %t", act, tc.exp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|