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-03-31 00:46:18 +00:00
|
|
|
root := b.SpecialPaths()
|
|
|
|
if root != nil {
|
2015-03-15 21:26:48 +00:00
|
|
|
t.Fatalf("unexpected: %v", root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassthroughBackend_Write(t *testing.T) {
|
2015-03-16 00:07:54 +00:00
|
|
|
b := testPassthroughBackend()
|
2015-03-15 21:26:48 +00:00
|
|
|
req := logical.TestRequest(t, logical.WriteOperation, "foo")
|
|
|
|
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-08-20 23:41:25 +00:00
|
|
|
func TestPassthroughBackend_Read_Lease(t *testing.T) {
|
2015-03-16 00:07:54 +00:00
|
|
|
b := testPassthroughBackend()
|
2015-03-15 21:26:48 +00:00
|
|
|
req := logical.TestRequest(t, logical.WriteOperation, "foo")
|
|
|
|
req.Data["raw"] = "test"
|
|
|
|
req.Data["lease"] = "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{
|
2015-03-19 22:11:42 +00:00
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-04-14 00:44:04 +00:00
|
|
|
Renewable: true,
|
2015-08-20 23:41:25 +00:00
|
|
|
TTL: time.Hour,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-03-15 21:26:48 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"raw": "test",
|
|
|
|
"lease": "1h",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-19 22:11:42 +00:00
|
|
|
resp.Secret.InternalData = nil
|
2015-04-08 20:35:32 +00:00
|
|
|
resp.Secret.LeaseID = ""
|
2015-03-15 21:26:48 +00:00
|
|
|
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
|
|
|
func TestPassthroughBackend_Read_TTL(t *testing.T) {
|
|
|
|
b := testPassthroughBackend()
|
|
|
|
req := logical.TestRequest(t, logical.WriteOperation, "foo")
|
|
|
|
req.Data["raw"] = "test"
|
|
|
|
req.Data["ttl"] = "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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"raw": "test",
|
|
|
|
"ttl": "1h",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-15 21:26:48 +00:00
|
|
|
func TestPassthroughBackend_Delete(t *testing.T) {
|
2015-03-16 00:07:54 +00:00
|
|
|
b := testPassthroughBackend()
|
2015-03-15 21:26:48 +00:00
|
|
|
req := logical.TestRequest(t, logical.WriteOperation, "foo")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassthroughBackend_List(t *testing.T) {
|
2015-03-16 00:07:54 +00:00
|
|
|
b := testPassthroughBackend()
|
2015-03-15 21:26:48 +00:00
|
|
|
req := logical.TestRequest(t, logical.WriteOperation, "foo")
|
|
|
|
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"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(resp, expected) {
|
|
|
|
t.Fatalf("bad response.\n\nexpected: %#v\n\nGot: %#v", expected, resp)
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
MaxLeaseTTLVal: time.Hour * 24 * 30,
|
|
|
|
},
|
|
|
|
})
|
2015-03-16 00:07:54 +00:00
|
|
|
return b
|
|
|
|
}
|