2015-04-01 21:51:13 +00:00
|
|
|
package framework
|
|
|
|
|
|
|
|
import (
|
2018-01-08 20:26:13 +00:00
|
|
|
"context"
|
2015-04-01 21:51:13 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-01-18 00:48:32 +00:00
|
|
|
saltpkg "github.com/hashicorp/vault/helper/salt"
|
2015-04-01 21:51:13 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPathMap(t *testing.T) {
|
|
|
|
p := &PathMap{Name: "foo"}
|
|
|
|
storage := new(logical.InmemStorage)
|
|
|
|
var b logical.Backend = &Backend{Paths: p.Paths()}
|
|
|
|
|
|
|
|
// Write via HTTP
|
2018-01-08 20:26:13 +00:00
|
|
|
_, err := b.HandleRequest(context.Background(), &logical.Request{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-04-01 21:51:13 +00:00
|
|
|
Path: "map/foo/a",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"value": "bar",
|
|
|
|
},
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read via HTTP
|
2018-01-08 20:26:13 +00:00
|
|
|
resp, err := b.HandleRequest(context.Background(), &logical.Request{
|
2015-04-01 21:51:13 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "map/foo/a",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if resp.Data["value"] != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read via API
|
|
|
|
v, err := p.Get(storage, "a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
2015-04-17 16:35:49 +00:00
|
|
|
if v["value"] != "bar" {
|
2015-04-01 21:51:13 +00:00
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
2015-04-24 01:04:26 +00:00
|
|
|
|
2015-05-11 17:27:04 +00:00
|
|
|
// Read via API with other casing
|
|
|
|
v, err = p.Get(storage, "A")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if v["value"] != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
|
2015-04-24 01:04:26 +00:00
|
|
|
// Verify List
|
|
|
|
keys, err := p.List(storage, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 1 || keys[0] != "a" {
|
|
|
|
t.Fatalf("bad: %#v", keys)
|
|
|
|
}
|
2015-05-14 12:28:33 +00:00
|
|
|
|
2017-01-11 21:09:01 +00:00
|
|
|
// LIST via HTTP
|
2018-01-08 20:26:13 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2017-01-11 21:09:01 +00:00
|
|
|
Operation: logical.ListOperation,
|
|
|
|
Path: "map/foo/",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if len(resp.Data) != 1 || len(resp.Data["keys"].([]string)) != 1 ||
|
|
|
|
resp.Data["keys"].([]string)[0] != "a" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
2015-05-14 12:28:33 +00:00
|
|
|
// Delete via HTTP
|
2018-01-08 20:26:13 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2015-05-14 12:28:33 +00:00
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "map/foo/a",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-read via HTTP
|
2018-01-08 20:26:13 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2015-05-14 12:28:33 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "map/foo/a",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if _, ok := resp.Data["value"]; ok {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-read via API
|
|
|
|
v, err = p.Get(storage, "a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
2015-04-01 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathMap_getInvalid(t *testing.T) {
|
|
|
|
p := &PathMap{Name: "foo"}
|
|
|
|
storage := new(logical.InmemStorage)
|
|
|
|
|
|
|
|
v, err := p.Get(storage, "nope")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
2015-04-17 16:35:49 +00:00
|
|
|
if v != nil {
|
2015-04-01 21:51:13 +00:00
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
2015-05-02 20:17:42 +00:00
|
|
|
|
|
|
|
func TestPathMap_routes(t *testing.T) {
|
|
|
|
p := &PathMap{Name: "foo"}
|
|
|
|
TestBackendRoutes(t, &Backend{Paths: p.Paths()}, []string{
|
|
|
|
"map/foo", // Normal
|
|
|
|
"map/foo/bar", // Normal
|
|
|
|
"map/foo/bar-baz", // Hyphen key
|
|
|
|
})
|
|
|
|
}
|
2015-06-30 21:28:45 +00:00
|
|
|
|
|
|
|
func TestPathMap_Salted(t *testing.T) {
|
|
|
|
storage := new(logical.InmemStorage)
|
2018-01-18 00:48:32 +00:00
|
|
|
|
|
|
|
salt, err := saltpkg.NewSalt(storage, &saltpkg.Config{
|
|
|
|
HashFunc: saltpkg.SHA1Hash,
|
2015-09-18 16:18:37 +00:00
|
|
|
})
|
2015-06-30 21:28:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-18 00:48:32 +00:00
|
|
|
|
|
|
|
testSalting(t, storage, salt, &PathMap{Name: "foo", Salt: salt})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSalting(t *testing.T, storage logical.Storage, salt *saltpkg.Salt, p *PathMap) {
|
2015-06-30 21:28:45 +00:00
|
|
|
var b logical.Backend = &Backend{Paths: p.Paths()}
|
2018-01-18 00:48:32 +00:00
|
|
|
var err error
|
2015-06-30 21:28:45 +00:00
|
|
|
|
|
|
|
// Write via HTTP
|
2018-01-08 20:26:13 +00:00
|
|
|
_, err = b.HandleRequest(context.Background(), &logical.Request{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-06-30 21:28:45 +00:00
|
|
|
Path: "map/foo/a",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"value": "bar",
|
|
|
|
},
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-salted version should not be there
|
|
|
|
out, err := storage.Get("struct/map/foo/a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("non-salted key found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the path is salted
|
2018-01-18 00:48:32 +00:00
|
|
|
expect := "s" + salt.SaltIDHashFunc("a", saltpkg.SHA256Hash)
|
2017-05-09 21:51:09 +00:00
|
|
|
out, err = storage.Get("struct/map/foo/" + expect)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("missing salted key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read via HTTP
|
2018-01-08 20:26:13 +00:00
|
|
|
resp, err := b.HandleRequest(context.Background(), &logical.Request{
|
2017-05-09 21:51:09 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "map/foo/a",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if resp.Data["value"] != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read via API
|
|
|
|
v, err := p.Get(storage, "a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if v["value"] != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read via API with other casing
|
|
|
|
v, err = p.Get(storage, "A")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if v["value"] != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify List
|
|
|
|
keys, err := p.List(storage, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 1 || keys[0] != expect {
|
|
|
|
t.Fatalf("bad: %#v", keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete via HTTP
|
2018-01-08 20:26:13 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2017-05-09 21:51:09 +00:00
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "map/foo/a",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-read via HTTP
|
2018-01-08 20:26:13 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2017-05-09 21:51:09 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "map/foo/a",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if _, ok := resp.Data["value"]; ok {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-read via API
|
|
|
|
v, err = p.Get(storage, "a")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
2017-06-07 19:22:08 +00:00
|
|
|
|
|
|
|
// Put in a non-salted version and make sure that after reading it's been
|
|
|
|
// upgraded
|
|
|
|
err = storage.Put(&logical.StorageEntry{
|
|
|
|
Key: "struct/map/foo/b",
|
|
|
|
Value: []byte(`{"foo": "bar"}`),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("err: %v", err)
|
|
|
|
}
|
|
|
|
// A read should transparently upgrade
|
2018-01-08 20:26:13 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2017-06-07 19:22:08 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "map/foo/b",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
list, _ := storage.List("struct/map/foo/")
|
|
|
|
if len(list) != 1 {
|
|
|
|
t.Fatalf("unexpected number of entries left after upgrade; expected 1, got %d", len(list))
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, v := range list {
|
2018-01-18 00:48:32 +00:00
|
|
|
if v == "s"+salt.SaltIDHashFunc("b", saltpkg.SHA256Hash) {
|
2017-06-07 19:22:08 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatal("did not find upgraded value")
|
|
|
|
}
|
2015-06-30 21:28:45 +00:00
|
|
|
|
2018-01-18 00:48:32 +00:00
|
|
|
// Put in a SHA1 salted version and make sure that after reading its been
|
2017-06-07 19:22:08 +00:00
|
|
|
// upgraded
|
|
|
|
err = storage.Put(&logical.StorageEntry{
|
2018-01-18 00:48:32 +00:00
|
|
|
Key: "struct/map/foo/" + salt.SaltID("b"),
|
2017-06-07 19:22:08 +00:00
|
|
|
Value: []byte(`{"foo": "bar"}`),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-18 00:48:32 +00:00
|
|
|
t.Fatal(err)
|
2017-06-07 19:22:08 +00:00
|
|
|
}
|
2018-01-18 00:48:32 +00:00
|
|
|
|
2017-06-07 19:22:08 +00:00
|
|
|
// A read should transparently upgrade
|
2018-01-08 20:26:13 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), &logical.Request{
|
2017-06-07 19:22:08 +00:00
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "map/foo/b",
|
|
|
|
Storage: storage,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-18 00:48:32 +00:00
|
|
|
list, _ = storage.List("struct/map/foo/")
|
2017-06-07 19:22:08 +00:00
|
|
|
if len(list) != 1 {
|
|
|
|
t.Fatalf("unexpected number of entries left after upgrade; expected 1, got %d", len(list))
|
|
|
|
}
|
2018-01-18 00:48:32 +00:00
|
|
|
found = false
|
2017-06-07 19:22:08 +00:00
|
|
|
for _, v := range list {
|
2018-01-18 00:48:32 +00:00
|
|
|
if v == "s"+salt.SaltIDHashFunc("b", saltpkg.SHA256Hash) {
|
2017-06-07 19:22:08 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatal("did not find upgraded value")
|
|
|
|
}
|
2015-06-30 21:28:45 +00:00
|
|
|
}
|
2018-01-18 00:48:32 +00:00
|
|
|
|
|
|
|
func TestPathMap_SaltFunc(t *testing.T) {
|
|
|
|
storage := new(logical.InmemStorage)
|
|
|
|
|
|
|
|
salt, err := saltpkg.NewSalt(storage, &saltpkg.Config{
|
|
|
|
HashFunc: saltpkg.SHA1Hash,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
saltFunc := func() (*saltpkg.Salt, error) {
|
|
|
|
return salt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
testSalting(t, storage, salt, &PathMap{Name: "foo", SaltFunc: saltFunc})
|
|
|
|
}
|