2015-03-15 21:26:48 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPassthroughBackend_RootPaths(t *testing.T) {
|
2015-03-16 00:07:54 +00:00
|
|
|
b := testPassthroughBackend()
|
2015-09-21 20:57:41 +00:00
|
|
|
test := func(b logical.Backend) {
|
|
|
|
root := b.SpecialPaths()
|
|
|
|
if root != nil {
|
|
|
|
t.Fatalf("unexpected: %v", root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
test(b)
|
|
|
|
b = testPassthroughLeasedBackend()
|
|
|
|
test(b)
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassthroughBackend_Write(t *testing.T) {
|
2015-09-21 20:57:41 +00:00
|
|
|
test := func(b logical.Backend) {
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "foo")
|
2015-09-21 20:57:41 +00:00
|
|
|
req.Data["raw"] = "test"
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := req.Storage.Get("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("failed to write to view")
|
|
|
|
}
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
2015-09-21 20:57:41 +00:00
|
|
|
b := testPassthroughBackend()
|
|
|
|
test(b)
|
|
|
|
b = testPassthroughLeasedBackend()
|
|
|
|
test(b)
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 20:57:41 +00:00
|
|
|
func TestPassthroughBackend_Read(t *testing.T) {
|
|
|
|
test := func(b logical.Backend, ttlType string, leased bool) {
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "foo")
|
2015-09-21 20:57:41 +00:00
|
|
|
req.Data["raw"] = "test"
|
|
|
|
req.Data[ttlType] = "1h"
|
|
|
|
storage := req.Storage
|
|
|
|
|
|
|
|
if _, err := b.HandleRequest(req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "foo")
|
|
|
|
req.Storage = storage
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &logical.Response{
|
|
|
|
Secret: &logical.Secret{
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
Renewable: true,
|
|
|
|
TTL: time.Hour,
|
|
|
|
},
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-09-21 20:57:41 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"raw": "test",
|
|
|
|
ttlType: "1h",
|
2015-08-20 23:41:25 +00:00
|
|
|
},
|
2015-09-21 20:57:41 +00:00
|
|
|
}
|
2015-08-20 23:41:25 +00:00
|
|
|
|
2015-09-21 20:57:41 +00:00
|
|
|
if !leased {
|
|
|
|
expected.Secret.Renewable = false
|
|
|
|
}
|
|
|
|
resp.Secret.InternalData = nil
|
|
|
|
resp.Secret.LeaseID = ""
|
|
|
|
if !reflect.DeepEqual(resp, expected) {
|
|
|
|
t.Fatalf("bad response.\n\nexpected: %#v\n\nGot: %#v", expected, resp)
|
|
|
|
}
|
2015-08-20 23:41:25 +00:00
|
|
|
}
|
2015-09-21 20:57:41 +00:00
|
|
|
b := testPassthroughLeasedBackend()
|
|
|
|
test(b, "lease", true)
|
|
|
|
test(b, "ttl", true)
|
|
|
|
b = testPassthroughBackend()
|
|
|
|
test(b, "lease", false)
|
|
|
|
test(b, "ttl", false)
|
2015-08-20 23:41:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-15 21:26:48 +00:00
|
|
|
func TestPassthroughBackend_Delete(t *testing.T) {
|
2015-09-21 20:57:41 +00:00
|
|
|
test := func(b logical.Backend) {
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "foo")
|
2015-09-21 20:57:41 +00:00
|
|
|
req.Data["raw"] = "test"
|
|
|
|
storage := req.Storage
|
|
|
|
|
|
|
|
if _, err := b.HandleRequest(req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.DeleteOperation, "foo")
|
|
|
|
req.Storage = storage
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "foo")
|
|
|
|
req.Storage = storage
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
2015-09-21 20:57:41 +00:00
|
|
|
b := testPassthroughBackend()
|
|
|
|
test(b)
|
|
|
|
b = testPassthroughLeasedBackend()
|
|
|
|
test(b)
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassthroughBackend_List(t *testing.T) {
|
2015-09-21 20:57:41 +00:00
|
|
|
test := func(b logical.Backend) {
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "foo")
|
2015-09-21 20:57:41 +00:00
|
|
|
req.Data["raw"] = "test"
|
|
|
|
storage := req.Storage
|
|
|
|
|
|
|
|
if _, err := b.HandleRequest(req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.ListOperation, "")
|
|
|
|
req.Storage = storage
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"keys": []string{"foo"},
|
|
|
|
},
|
|
|
|
}
|
2015-03-15 21:26:48 +00:00
|
|
|
|
2015-09-21 20:57:41 +00:00
|
|
|
if !reflect.DeepEqual(resp, expected) {
|
|
|
|
t.Fatalf("bad response.\n\nexpected: %#v\n\nGot: %#v", expected, resp)
|
|
|
|
}
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
2015-09-21 20:57:41 +00:00
|
|
|
b := testPassthroughBackend()
|
|
|
|
test(b)
|
|
|
|
b = testPassthroughLeasedBackend()
|
|
|
|
test(b)
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
2015-03-16 00:07:54 +00:00
|
|
|
|
2015-10-07 15:42:23 +00:00
|
|
|
func TestPassthroughBackend_Revoke(t *testing.T) {
|
|
|
|
test := func(b logical.Backend) {
|
|
|
|
req := logical.TestRequest(t, logical.RevokeOperation, "generic")
|
|
|
|
req.Secret = &logical.Secret{
|
|
|
|
InternalData: map[string]interface{}{
|
|
|
|
"secret_type": "generic",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := b.HandleRequest(req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b := testPassthroughBackend()
|
|
|
|
test(b)
|
|
|
|
b = testPassthroughLeasedBackend()
|
|
|
|
test(b)
|
|
|
|
}
|
|
|
|
|
2015-03-16 00:07:54 +00:00
|
|
|
func testPassthroughBackend() logical.Backend {
|
2015-09-04 20:58:12 +00:00
|
|
|
b, _ := PassthroughBackendFactory(&logical.BackendConfig{
|
|
|
|
Logger: nil,
|
|
|
|
System: logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: time.Hour * 24,
|
2016-09-28 22:32:49 +00:00
|
|
|
MaxLeaseTTLVal: time.Hour * 24 * 32,
|
2015-09-04 20:58:12 +00:00
|
|
|
},
|
|
|
|
})
|
2015-03-16 00:07:54 +00:00
|
|
|
return b
|
|
|
|
}
|
2015-09-19 22:24:53 +00:00
|
|
|
|
|
|
|
func testPassthroughLeasedBackend() logical.Backend {
|
|
|
|
b, _ := LeasedPassthroughBackendFactory(&logical.BackendConfig{
|
|
|
|
Logger: nil,
|
|
|
|
System: logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: time.Hour * 24,
|
2016-09-28 22:32:49 +00:00
|
|
|
MaxLeaseTTLVal: time.Hour * 24 * 32,
|
2015-09-19 22:24:53 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return b
|
|
|
|
}
|