2015-03-15 21:42:05 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2015-11-19 01:26:03 +00:00
|
|
|
"crypto/sha256"
|
2017-04-12 17:39:18 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-03-15 21:42:05 +00:00
|
|
|
"reflect"
|
2016-04-19 20:26:26 +00:00
|
|
|
"strings"
|
2015-03-15 21:42:05 +00:00
|
|
|
"testing"
|
2015-08-31 18:27:49 +00:00
|
|
|
"time"
|
2015-03-15 21:42:05 +00:00
|
|
|
|
2015-09-01 22:29:30 +00:00
|
|
|
"github.com/fatih/structs"
|
2015-03-31 23:45:00 +00:00
|
|
|
"github.com/hashicorp/vault/audit"
|
2017-04-12 17:39:18 +00:00
|
|
|
"github.com/hashicorp/vault/helper/builtinplugins"
|
|
|
|
"github.com/hashicorp/vault/helper/pluginutil"
|
2015-11-19 01:26:03 +00:00
|
|
|
"github.com/hashicorp/vault/helper/salt"
|
2015-03-15 21:42:05 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2017-05-04 02:03:42 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-03-15 21:42:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSystemBackend_RootPaths(t *testing.T) {
|
|
|
|
expected := []string{
|
2015-03-20 19:48:19 +00:00
|
|
|
"auth/*",
|
2015-03-15 21:42:05 +00:00
|
|
|
"remount",
|
2015-03-31 23:45:00 +00:00
|
|
|
"audit",
|
|
|
|
"audit/*",
|
2015-04-02 00:44:43 +00:00
|
|
|
"raw/*",
|
2017-02-17 04:36:06 +00:00
|
|
|
"replication/primary/secondary-token",
|
|
|
|
"replication/reindex",
|
2015-05-28 00:53:42 +00:00
|
|
|
"rotate",
|
2017-06-17 05:51:42 +00:00
|
|
|
"config/cors",
|
2017-02-02 19:49:20 +00:00
|
|
|
"config/auditing/*",
|
2017-04-24 19:47:40 +00:00
|
|
|
"plugins/catalog/*",
|
2017-05-04 02:03:42 +00:00
|
|
|
"revoke-prefix/*",
|
2017-07-25 15:59:43 +00:00
|
|
|
"revoke-force/*",
|
2017-05-04 02:03:42 +00:00
|
|
|
"leases/revoke-prefix/*",
|
|
|
|
"leases/revoke-force/*",
|
|
|
|
"leases/lookup/*",
|
2015-03-15 21:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b := testSystemBackend(t)
|
2015-03-31 00:46:18 +00:00
|
|
|
actual := b.SpecialPaths().Root
|
2015-03-15 21:42:05 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: %#v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:04:55 +00:00
|
|
|
func TestSystemConfigCORS(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
2017-06-17 05:26:25 +00:00
|
|
|
_, barrier, _ := mockBarrier(t)
|
|
|
|
view := NewBarrierView(barrier, "")
|
|
|
|
b.(*SystemBackend).Core.systemBarrierView = view
|
2017-06-17 04:04:55 +00:00
|
|
|
|
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "config/cors")
|
|
|
|
req.Data["allowed_origins"] = "http://www.example.com"
|
|
|
|
_, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"enabled": true,
|
2017-06-17 05:26:25 +00:00
|
|
|
"allowed_origins": []string{"http://www.example.com"},
|
2017-06-17 04:04:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "config/cors")
|
|
|
|
actual, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
2017-06-17 05:26:25 +00:00
|
|
|
t.Fatalf("bad: %#v", actual)
|
2017-06-17 04:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.DeleteOperation, "config/cors")
|
|
|
|
_, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "config/cors")
|
|
|
|
actual, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
2017-06-17 05:26:25 +00:00
|
|
|
"enabled": false,
|
2017-06-17 04:04:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("DELETE FAILED -- bad: %#v", actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:42:05 +00:00
|
|
|
func TestSystemBackend_mounts(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
req := logical.TestRequest(t, logical.ReadOperation, "mounts")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-01 22:29:30 +00:00
|
|
|
// We can't know the pointer address ahead of time so simply
|
|
|
|
// copy what's given
|
2015-03-15 21:42:05 +00:00
|
|
|
exp := map[string]interface{}{
|
2015-08-31 18:27:49 +00:00
|
|
|
"secret/": map[string]interface{}{
|
2015-03-15 21:42:05 +00:00
|
|
|
"type": "generic",
|
|
|
|
"description": "generic secret storage",
|
2017-06-26 17:14:36 +00:00
|
|
|
"accessor": resp.Data["secret/"].(map[string]interface{})["accessor"],
|
2015-08-31 18:27:49 +00:00
|
|
|
"config": map[string]interface{}{
|
2016-06-21 00:08:12 +00:00
|
|
|
"default_lease_ttl": resp.Data["secret/"].(map[string]interface{})["config"].(map[string]interface{})["default_lease_ttl"].(int64),
|
|
|
|
"max_lease_ttl": resp.Data["secret/"].(map[string]interface{})["config"].(map[string]interface{})["max_lease_ttl"].(int64),
|
2017-03-08 14:20:09 +00:00
|
|
|
"force_no_cache": false,
|
2015-08-31 18:27:49 +00:00
|
|
|
},
|
2017-02-16 21:29:30 +00:00
|
|
|
"local": false,
|
2015-03-15 21:42:05 +00:00
|
|
|
},
|
2015-08-31 18:27:49 +00:00
|
|
|
"sys/": map[string]interface{}{
|
2015-03-15 21:42:05 +00:00
|
|
|
"type": "system",
|
|
|
|
"description": "system endpoints used for control, policy and debugging",
|
2017-06-26 17:14:36 +00:00
|
|
|
"accessor": resp.Data["sys/"].(map[string]interface{})["accessor"],
|
2015-08-31 18:27:49 +00:00
|
|
|
"config": map[string]interface{}{
|
2016-06-21 00:08:12 +00:00
|
|
|
"default_lease_ttl": resp.Data["sys/"].(map[string]interface{})["config"].(map[string]interface{})["default_lease_ttl"].(int64),
|
|
|
|
"max_lease_ttl": resp.Data["sys/"].(map[string]interface{})["config"].(map[string]interface{})["max_lease_ttl"].(int64),
|
2017-03-08 14:20:09 +00:00
|
|
|
"force_no_cache": false,
|
2015-08-31 18:27:49 +00:00
|
|
|
},
|
2017-02-16 21:29:30 +00:00
|
|
|
"local": false,
|
2015-03-15 21:42:05 +00:00
|
|
|
},
|
2015-09-10 01:58:09 +00:00
|
|
|
"cubbyhole/": map[string]interface{}{
|
|
|
|
"description": "per-token private secret storage",
|
|
|
|
"type": "cubbyhole",
|
2017-06-26 17:14:36 +00:00
|
|
|
"accessor": resp.Data["cubbyhole/"].(map[string]interface{})["accessor"],
|
2015-09-10 01:58:09 +00:00
|
|
|
"config": map[string]interface{}{
|
2016-06-21 00:08:12 +00:00
|
|
|
"default_lease_ttl": resp.Data["cubbyhole/"].(map[string]interface{})["config"].(map[string]interface{})["default_lease_ttl"].(int64),
|
|
|
|
"max_lease_ttl": resp.Data["cubbyhole/"].(map[string]interface{})["config"].(map[string]interface{})["max_lease_ttl"].(int64),
|
2017-03-08 14:20:09 +00:00
|
|
|
"force_no_cache": false,
|
2015-09-10 01:58:09 +00:00
|
|
|
},
|
2017-02-18 18:51:05 +00:00
|
|
|
"local": true,
|
2015-09-10 01:58:09 +00:00
|
|
|
},
|
2015-03-15 21:42:05 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp.Data, exp) {
|
2015-08-31 18:27:49 +00:00
|
|
|
t.Fatalf("Got:\n%#v\nExpected:\n%#v", resp.Data, exp)
|
2015-03-15 21:42:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_mount(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "mounts/prod/secret/")
|
2015-03-15 21:42:05 +00:00
|
|
|
req.Data["type"] = "generic"
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:20:09 +00:00
|
|
|
func TestSystemBackend_mount_force_no_cache(t *testing.T) {
|
|
|
|
core, b, _ := testCoreSystemBackend(t)
|
|
|
|
|
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "mounts/prod/secret/")
|
|
|
|
req.Data["type"] = "generic"
|
|
|
|
req.Data["config"] = map[string]interface{}{
|
|
|
|
"force_no_cache": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
mountEntry := core.router.MatchingMountEntry("prod/secret/")
|
|
|
|
if mountEntry == nil {
|
|
|
|
t.Fatalf("missing mount entry")
|
|
|
|
}
|
|
|
|
if !mountEntry.Config.ForceNoCache {
|
|
|
|
t.Fatalf("bad config %#v", mountEntry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:42:05 +00:00
|
|
|
func TestSystemBackend_mount_invalid(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "mounts/prod/secret/")
|
2015-03-15 21:42:05 +00:00
|
|
|
req.Data["type"] = "nope"
|
|
|
|
resp, err := b.HandleRequest(req)
|
2015-03-15 21:53:41 +00:00
|
|
|
if err != logical.ErrInvalidRequest {
|
2015-03-15 21:42:05 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-03-15 23:25:38 +00:00
|
|
|
if resp.Data["error"] != "unknown backend type: nope" {
|
2015-03-15 21:42:05 +00:00
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_unmount(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2015-03-16 17:52:35 +00:00
|
|
|
req := logical.TestRequest(t, logical.DeleteOperation, "mounts/secret/")
|
2015-03-15 21:42:05 +00:00
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 15:58:06 +00:00
|
|
|
var capabilitiesPolicy = `
|
|
|
|
name = "test"
|
|
|
|
path "foo/bar*" {
|
|
|
|
capabilities = ["create", "sudo", "update"]
|
|
|
|
}
|
|
|
|
path "sys/capabilities*" {
|
|
|
|
capabilities = ["update"]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestSystemBackend_Capabilities(t *testing.T) {
|
|
|
|
testCapabilities(t, "capabilities")
|
|
|
|
testCapabilities(t, "capabilities-self")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCapabilities(t *testing.T, endpoint string) {
|
|
|
|
core, b, rootToken := testCoreSystemBackend(t)
|
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, endpoint)
|
|
|
|
req.Data["token"] = rootToken
|
|
|
|
req.Data["path"] = "any_path"
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resp.Data["capabilities"]
|
|
|
|
expected := []string{"root"}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: got\n%#v\nexpected\n%#v\n", actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, _ := Parse(capabilitiesPolicy)
|
|
|
|
err = core.policyStore.SetPolicy(policy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testMakeToken(t, core.tokenStore, rootToken, "tokenid", "", []string{"test"})
|
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, endpoint)
|
|
|
|
req.Data["token"] = "tokenid"
|
|
|
|
req.Data["path"] = "foo/bar"
|
|
|
|
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = resp.Data["capabilities"]
|
2016-03-18 16:40:17 +00:00
|
|
|
expected = []string{"create", "sudo", "update"}
|
2016-03-18 15:58:06 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: got\n%#v\nexpected\n%#v\n", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 14:37:49 +00:00
|
|
|
func TestSystemBackend_CapabilitiesAccessor(t *testing.T) {
|
2016-03-18 15:58:06 +00:00
|
|
|
core, b, rootToken := testCoreSystemBackend(t)
|
|
|
|
te, err := core.tokenStore.Lookup(rootToken)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-03-18 14:37:49 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "capabilities-accessor")
|
2016-03-18 15:58:06 +00:00
|
|
|
// Accessor of root token
|
|
|
|
req.Data["accessor"] = te.Accessor
|
|
|
|
req.Data["path"] = "any_path"
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(req)
|
2016-03-18 14:37:49 +00:00
|
|
|
if err != nil {
|
2016-03-18 15:58:06 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := resp.Data["capabilities"]
|
|
|
|
expected := []string{"root"}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: got\n%#v\nexpected\n%#v\n", actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, _ := Parse(capabilitiesPolicy)
|
|
|
|
err = core.policyStore.SetPolicy(policy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testMakeToken(t, core.tokenStore, rootToken, "tokenid", "", []string{"test"})
|
|
|
|
|
|
|
|
te, err = core.tokenStore.Lookup("tokenid")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, "capabilities-accessor")
|
|
|
|
req.Data["accessor"] = te.Accessor
|
|
|
|
req.Data["path"] = "foo/bar"
|
|
|
|
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = resp.Data["capabilities"]
|
2016-03-18 16:40:17 +00:00
|
|
|
expected = []string{"create", "sudo", "update"}
|
2016-03-18 15:58:06 +00:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("bad: got\n%#v\nexpected\n%#v\n", actual, expected)
|
2016-03-18 14:37:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:42:05 +00:00
|
|
|
func TestSystemBackend_remount(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "remount")
|
2015-03-15 21:42:05 +00:00
|
|
|
req.Data["from"] = "secret"
|
|
|
|
req.Data["to"] = "foo"
|
2015-09-01 22:29:30 +00:00
|
|
|
req.Data["config"] = structs.Map(MountConfig{})
|
2015-03-15 21:42:05 +00:00
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_remount_invalid(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "remount")
|
2015-03-15 21:42:05 +00:00
|
|
|
req.Data["from"] = "unknown"
|
|
|
|
req.Data["to"] = "foo"
|
2015-09-01 22:29:30 +00:00
|
|
|
req.Data["config"] = structs.Map(MountConfig{})
|
2015-03-15 21:42:05 +00:00
|
|
|
resp, err := b.HandleRequest(req)
|
2015-03-15 21:53:41 +00:00
|
|
|
if err != logical.ErrInvalidRequest {
|
2015-03-15 21:42:05 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Data["error"] != "no matching mount at 'unknown/'" {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_remount_system(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "remount")
|
2015-03-15 21:42:05 +00:00
|
|
|
req.Data["from"] = "sys"
|
|
|
|
req.Data["to"] = "foo"
|
|
|
|
resp, err := b.HandleRequest(req)
|
2015-03-15 21:53:41 +00:00
|
|
|
if err != logical.ErrInvalidRequest {
|
2015-03-15 21:42:05 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Data["error"] != "cannot remount 'sys/'" {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:03:42 +00:00
|
|
|
func TestSystemBackend_leases(t *testing.T) {
|
|
|
|
core, b, root := testCoreSystemBackend(t)
|
|
|
|
|
|
|
|
// Create a key with a lease
|
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "secret/foo")
|
|
|
|
req.Data["foo"] = "bar"
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err := core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a key with a LeaseID
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/foo")
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read lease
|
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, "leases/lookup")
|
|
|
|
req.Data["lease_id"] = resp.Secret.LeaseID
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Data["renewable"] == nil || resp.Data["renewable"].(bool) {
|
|
|
|
t.Fatal("generic leases are not renewable")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid lease
|
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, "leases/lookup")
|
|
|
|
req.Data["lease_id"] = "invalid"
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("expected invalid request, got err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_leases_list(t *testing.T) {
|
|
|
|
core, b, root := testCoreSystemBackend(t)
|
|
|
|
|
|
|
|
// Create a key with a lease
|
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "secret/foo")
|
|
|
|
req.Data["foo"] = "bar"
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err := core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a key with a LeaseID
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/foo")
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List top level
|
|
|
|
req = logical.TestRequest(t, logical.ListOperation, "leases/lookup/")
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Data == nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
var keys []string
|
|
|
|
if err := mapstructure.WeakDecode(resp.Data["keys"], &keys); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("Expected 1 subkey lease, got %d: %#v", len(keys), keys)
|
|
|
|
}
|
|
|
|
if keys[0] != "secret/" {
|
|
|
|
t.Fatal("Expected only secret subkey")
|
|
|
|
}
|
|
|
|
|
|
|
|
// List lease
|
|
|
|
req = logical.TestRequest(t, logical.ListOperation, "leases/lookup/secret/foo")
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Data == nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
keys = []string{}
|
|
|
|
if err := mapstructure.WeakDecode(resp.Data["keys"], &keys); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("Expected 1 secret lease, got %d: %#v", len(keys), keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate multiple leases
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/foo")
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/foo")
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.ListOperation, "leases/lookup/secret/foo")
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Data == nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
keys = []string{}
|
|
|
|
if err := mapstructure.WeakDecode(resp.Data["keys"], &keys); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 3 {
|
|
|
|
t.Fatalf("Expected 3 secret lease, got %d: %#v", len(keys), keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listing subkeys
|
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, "secret/bar")
|
|
|
|
req.Data["foo"] = "bar"
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a key with a LeaseID
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/bar")
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.ListOperation, "leases/lookup/secret")
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Data == nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
keys = []string{}
|
|
|
|
if err := mapstructure.WeakDecode(resp.Data["keys"], &keys); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(keys) != 2 {
|
|
|
|
t.Fatalf("Expected 2 secret lease, got %d: %#v", len(keys), keys)
|
|
|
|
}
|
|
|
|
expected := []string{"bar/", "foo/"}
|
|
|
|
if !reflect.DeepEqual(expected, keys) {
|
|
|
|
t.Fatalf("exp: %#v, act: %#v", expected, keys)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:11:55 +00:00
|
|
|
func TestSystemBackend_renew(t *testing.T) {
|
2015-03-24 18:37:07 +00:00
|
|
|
core, b, root := testCoreSystemBackend(t)
|
2015-03-16 23:11:55 +00:00
|
|
|
|
|
|
|
// Create a key with a lease
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "secret/foo")
|
2015-03-16 23:11:55 +00:00
|
|
|
req.Data["foo"] = "bar"
|
2015-03-24 18:37:07 +00:00
|
|
|
req.ClientToken = root
|
2015-03-16 23:11:55 +00:00
|
|
|
resp, err := core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
2015-04-08 20:35:32 +00:00
|
|
|
// Read a key with a LeaseID
|
2015-03-16 23:11:55 +00:00
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/foo")
|
2015-03-24 18:37:07 +00:00
|
|
|
req.ClientToken = root
|
2015-03-16 23:11:55 +00:00
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-04-08 20:35:32 +00:00
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
2015-03-16 23:11:55 +00:00
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt renew
|
2017-05-04 02:03:42 +00:00
|
|
|
req2 := logical.TestRequest(t, logical.UpdateOperation, "leases/renew/"+resp.Secret.LeaseID)
|
2015-03-16 23:11:55 +00:00
|
|
|
resp2, err := b.HandleRequest(req2)
|
2015-04-09 00:03:46 +00:00
|
|
|
if err != logical.ErrInvalidRequest {
|
2015-03-16 23:11:55 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:03:46 +00:00
|
|
|
// Should get error about non-renewability
|
|
|
|
if resp2.Data["error"] != "lease is not renewable" {
|
2015-03-16 23:11:55 +00:00
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
2016-08-08 22:32:18 +00:00
|
|
|
|
|
|
|
// Add a TTL to the lease
|
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, "secret/foo")
|
|
|
|
req.Data["foo"] = "bar"
|
|
|
|
req.Data["ttl"] = "180s"
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a key with a LeaseID
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/foo")
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt renew
|
2017-05-04 02:03:42 +00:00
|
|
|
req2 = logical.TestRequest(t, logical.UpdateOperation, "leases/renew/"+resp.Secret.LeaseID)
|
2016-08-08 22:32:18 +00:00
|
|
|
resp2, err = b.HandleRequest(req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.IsError() {
|
|
|
|
t.Fatalf("got an error")
|
|
|
|
}
|
|
|
|
if resp2.Data == nil {
|
|
|
|
t.Fatal("nil data")
|
|
|
|
}
|
|
|
|
if resp.Secret.TTL != 180*time.Second {
|
2016-08-09 11:13:29 +00:00
|
|
|
t.Fatalf("bad lease duration: %v", resp.Secret.TTL)
|
2016-08-08 22:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test the other route path
|
2017-05-04 02:03:42 +00:00
|
|
|
req2 = logical.TestRequest(t, logical.UpdateOperation, "leases/renew")
|
|
|
|
req2.Data["lease_id"] = resp.Secret.LeaseID
|
|
|
|
resp2, err = b.HandleRequest(req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.IsError() {
|
|
|
|
t.Fatalf("got an error")
|
|
|
|
}
|
|
|
|
if resp2.Data == nil {
|
|
|
|
t.Fatal("nil data")
|
|
|
|
}
|
|
|
|
if resp.Secret.TTL != 180*time.Second {
|
|
|
|
t.Fatalf("bad lease duration: %v", resp.Secret.TTL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test orig path
|
2016-08-08 22:32:18 +00:00
|
|
|
req2 = logical.TestRequest(t, logical.UpdateOperation, "renew")
|
|
|
|
req2.Data["lease_id"] = resp.Secret.LeaseID
|
|
|
|
resp2, err = b.HandleRequest(req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp2.IsError() {
|
|
|
|
t.Fatalf("got an error")
|
|
|
|
}
|
|
|
|
if resp2.Data == nil {
|
|
|
|
t.Fatal("nil data")
|
|
|
|
}
|
|
|
|
if resp.Secret.TTL != 180*time.Second {
|
2016-08-09 11:13:29 +00:00
|
|
|
t.Fatalf("bad lease duration: %v", resp.Secret.TTL)
|
2016-08-08 22:32:18 +00:00
|
|
|
}
|
2015-03-16 23:11:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:14:53 +00:00
|
|
|
func TestSystemBackend_renew_invalidID(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2017-05-04 02:03:42 +00:00
|
|
|
// Attempt renew
|
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "leases/renew/foobarbaz")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Data["error"] != "lease not found or lease is not renewable" {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt renew with other method
|
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, "leases/renew")
|
|
|
|
req.Data["lease_id"] = "foobarbaz"
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Data["error"] != "lease not found or lease is not renewable" {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_renew_invalidID_origUrl(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2015-03-16 23:14:53 +00:00
|
|
|
// Attempt renew
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "renew/foobarbaz")
|
2015-03-16 23:14:53 +00:00
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-06-18 22:37:08 +00:00
|
|
|
if resp.Data["error"] != "lease not found or lease is not renewable" {
|
2015-03-16 23:14:53 +00:00
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
2017-04-27 14:47:43 +00:00
|
|
|
|
|
|
|
// Attempt renew with other method
|
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, "renew")
|
|
|
|
req.Data["lease_id"] = "foobarbaz"
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Data["error"] != "lease not found or lease is not renewable" {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
2015-03-16 23:14:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:26:34 +00:00
|
|
|
func TestSystemBackend_revoke(t *testing.T) {
|
2015-03-24 18:37:07 +00:00
|
|
|
core, b, root := testCoreSystemBackend(t)
|
2015-03-16 23:26:34 +00:00
|
|
|
|
|
|
|
// Create a key with a lease
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "secret/foo")
|
2015-03-16 23:26:34 +00:00
|
|
|
req.Data["foo"] = "bar"
|
|
|
|
req.Data["lease"] = "1h"
|
2015-03-24 18:37:07 +00:00
|
|
|
req.ClientToken = root
|
2015-03-16 23:26:34 +00:00
|
|
|
resp, err := core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
2015-04-08 20:35:32 +00:00
|
|
|
// Read a key with a LeaseID
|
2015-03-16 23:26:34 +00:00
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/foo")
|
2015-03-24 18:37:07 +00:00
|
|
|
req.ClientToken = root
|
2015-03-16 23:26:34 +00:00
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-04-08 20:35:32 +00:00
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
2015-03-16 23:26:34 +00:00
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:33:48 +00:00
|
|
|
// Attempt revoke
|
2016-01-07 15:30:47 +00:00
|
|
|
req2 := logical.TestRequest(t, logical.UpdateOperation, "revoke/"+resp.Secret.LeaseID)
|
2015-03-16 23:26:34 +00:00
|
|
|
resp2, err := b.HandleRequest(req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v %#v", err, resp2)
|
|
|
|
}
|
|
|
|
if resp2 != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
2015-03-16 23:33:48 +00:00
|
|
|
|
|
|
|
// Attempt renew
|
2016-01-07 15:30:47 +00:00
|
|
|
req3 := logical.TestRequest(t, logical.UpdateOperation, "renew/"+resp.Secret.LeaseID)
|
2015-03-16 23:33:48 +00:00
|
|
|
resp3, err := b.HandleRequest(req3)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-06-18 22:37:08 +00:00
|
|
|
if resp3.Data["error"] != "lease not found or lease is not renewable" {
|
2015-03-16 23:33:48 +00:00
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
2017-04-27 14:47:43 +00:00
|
|
|
|
|
|
|
// Read a key with a LeaseID
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/foo")
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the other route path
|
|
|
|
req2 = logical.TestRequest(t, logical.UpdateOperation, "revoke")
|
|
|
|
req2.Data["lease_id"] = resp.Secret.LeaseID
|
|
|
|
resp2, err = b.HandleRequest(req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v %#v", err, resp2)
|
|
|
|
}
|
|
|
|
if resp2 != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
2017-05-04 02:03:42 +00:00
|
|
|
|
|
|
|
// Read a key with a LeaseID
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/foo")
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the other route path
|
|
|
|
req2 = logical.TestRequest(t, logical.UpdateOperation, "leases/revoke")
|
|
|
|
req2.Data["lease_id"] = resp.Secret.LeaseID
|
|
|
|
resp2, err = b.HandleRequest(req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v %#v", err, resp2)
|
|
|
|
}
|
|
|
|
if resp2 != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
2015-03-16 23:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_revoke_invalidID(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2017-05-04 02:03:42 +00:00
|
|
|
// Attempt revoke
|
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "leases/revoke/foobarbaz")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt revoke with other method
|
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, "leases/revoke")
|
|
|
|
req.Data["lease_id"] = "foobarbaz"
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_revoke_invalidID_origUrl(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2017-04-27 14:47:43 +00:00
|
|
|
// Attempt revoke
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "revoke/foobarbaz")
|
2015-03-16 23:26:34 +00:00
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
2017-04-27 14:47:43 +00:00
|
|
|
|
|
|
|
// Attempt revoke with other method
|
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, "revoke")
|
|
|
|
req.Data["lease_id"] = "foobarbaz"
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
2015-03-16 23:26:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:33:48 +00:00
|
|
|
func TestSystemBackend_revokePrefix(t *testing.T) {
|
2015-03-24 18:37:07 +00:00
|
|
|
core, b, root := testCoreSystemBackend(t)
|
2015-03-16 23:33:48 +00:00
|
|
|
|
|
|
|
// Create a key with a lease
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "secret/foo")
|
2015-03-16 23:33:48 +00:00
|
|
|
req.Data["foo"] = "bar"
|
|
|
|
req.Data["lease"] = "1h"
|
2015-03-24 18:37:07 +00:00
|
|
|
req.ClientToken = root
|
2015-03-16 23:33:48 +00:00
|
|
|
resp, err := core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
2015-04-08 20:35:32 +00:00
|
|
|
// Read a key with a LeaseID
|
2015-03-16 23:33:48 +00:00
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/foo")
|
2015-03-24 18:37:07 +00:00
|
|
|
req.ClientToken = root
|
2015-03-16 23:33:48 +00:00
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-04-08 20:35:32 +00:00
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
2015-03-16 23:33:48 +00:00
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:03:42 +00:00
|
|
|
// Attempt revoke
|
|
|
|
req2 := logical.TestRequest(t, logical.UpdateOperation, "leases/revoke-prefix/secret/")
|
|
|
|
resp2, err := b.HandleRequest(req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v %#v", err, resp2)
|
|
|
|
}
|
|
|
|
if resp2 != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt renew
|
|
|
|
req3 := logical.TestRequest(t, logical.UpdateOperation, "leases/renew/"+resp.Secret.LeaseID)
|
|
|
|
resp3, err := b.HandleRequest(req3)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp3.Data["error"] != "lease not found or lease is not renewable" {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_revokePrefix_origUrl(t *testing.T) {
|
|
|
|
core, b, root := testCoreSystemBackend(t)
|
|
|
|
|
|
|
|
// Create a key with a lease
|
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "secret/foo")
|
|
|
|
req.Data["foo"] = "bar"
|
|
|
|
req.Data["lease"] = "1h"
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err := core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a key with a LeaseID
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "secret/foo")
|
|
|
|
req.ClientToken = root
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Secret == nil || resp.Secret.LeaseID == "" {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:33:48 +00:00
|
|
|
// Attempt revoke
|
2016-01-07 15:30:47 +00:00
|
|
|
req2 := logical.TestRequest(t, logical.UpdateOperation, "revoke-prefix/secret/")
|
2015-03-16 23:33:48 +00:00
|
|
|
resp2, err := b.HandleRequest(req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v %#v", err, resp2)
|
|
|
|
}
|
|
|
|
if resp2 != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt renew
|
2016-01-07 15:30:47 +00:00
|
|
|
req3 := logical.TestRequest(t, logical.UpdateOperation, "renew/"+resp.Secret.LeaseID)
|
2015-03-16 23:33:48 +00:00
|
|
|
resp3, err := b.HandleRequest(req3)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-06-18 22:37:08 +00:00
|
|
|
if resp3.Data["error"] != "lease not found or lease is not renewable" {
|
2015-03-16 23:33:48 +00:00
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 22:04:05 +00:00
|
|
|
func TestSystemBackend_revokePrefixAuth(t *testing.T) {
|
|
|
|
core, ts, _, _ := TestCoreWithTokenStore(t)
|
|
|
|
bc := &logical.BackendConfig{
|
|
|
|
Logger: core.logger,
|
|
|
|
System: logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: time.Hour * 24,
|
2016-09-28 22:32:49 +00:00
|
|
|
MaxLeaseTTLVal: time.Hour * 24 * 32,
|
2016-03-31 22:04:05 +00:00
|
|
|
},
|
|
|
|
}
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
b := NewSystemBackend(core)
|
|
|
|
err := b.Backend.Setup(bc)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-03-31 22:04:05 +00:00
|
|
|
exp := ts.expiration
|
|
|
|
|
|
|
|
te := &TokenEntry{
|
2017-05-04 02:03:42 +00:00
|
|
|
ID: "foo",
|
|
|
|
Path: "auth/github/login/bar",
|
|
|
|
}
|
|
|
|
err = ts.create(te)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
te, err = ts.Lookup("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if te == nil {
|
|
|
|
t.Fatal("token entry was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new token
|
|
|
|
auth := &logical.Auth{
|
|
|
|
ClientToken: te.ID,
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: time.Hour,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = exp.RegisterAuth(te.Path, auth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "leases/revoke-prefix/auth/github/")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v %v", err, resp)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
te, err = ts.Lookup(te.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if te != nil {
|
|
|
|
t.Fatalf("bad: %v", te)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_revokePrefixAuth_origUrl(t *testing.T) {
|
2016-03-31 22:04:05 +00:00
|
|
|
core, ts, _, _ := TestCoreWithTokenStore(t)
|
|
|
|
bc := &logical.BackendConfig{
|
|
|
|
Logger: core.logger,
|
|
|
|
System: logical.StaticSystemView{
|
|
|
|
DefaultLeaseTTLVal: time.Hour * 24,
|
2016-09-28 22:32:49 +00:00
|
|
|
MaxLeaseTTLVal: time.Hour * 24 * 32,
|
2016-03-31 22:04:05 +00:00
|
|
|
},
|
|
|
|
}
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
b := NewSystemBackend(core)
|
|
|
|
err := b.Backend.Setup(bc)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-03-31 22:04:05 +00:00
|
|
|
exp := ts.expiration
|
|
|
|
|
|
|
|
te := &TokenEntry{
|
|
|
|
ID: "foo",
|
|
|
|
Path: "auth/github/login/bar",
|
|
|
|
}
|
2016-09-29 04:01:28 +00:00
|
|
|
err = ts.create(te)
|
2016-03-31 22:04:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
te, err = ts.Lookup("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if te == nil {
|
|
|
|
t.Fatal("token entry was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new token
|
|
|
|
auth := &logical.Auth{
|
|
|
|
ClientToken: te.ID,
|
|
|
|
LeaseOptions: logical.LeaseOptions{
|
|
|
|
TTL: time.Hour,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = exp.RegisterAuth(te.Path, auth)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "revoke-prefix/auth/github/")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v %v", err, resp)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
te, err = ts.Lookup(te.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if te != nil {
|
|
|
|
t.Fatalf("bad: %v", te)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 19:48:19 +00:00
|
|
|
func TestSystemBackend_authTable(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
req := logical.TestRequest(t, logical.ReadOperation, "auth")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := map[string]interface{}{
|
2016-06-20 19:55:21 +00:00
|
|
|
"token/": map[string]interface{}{
|
2015-03-20 19:48:19 +00:00
|
|
|
"type": "token",
|
|
|
|
"description": "token based credentials",
|
2017-06-26 17:14:36 +00:00
|
|
|
"accessor": resp.Data["token/"].(map[string]interface{})["accessor"],
|
2016-06-20 19:55:21 +00:00
|
|
|
"config": map[string]interface{}{
|
2016-06-21 00:08:12 +00:00
|
|
|
"default_lease_ttl": int64(0),
|
|
|
|
"max_lease_ttl": int64(0),
|
2016-06-20 19:55:21 +00:00
|
|
|
},
|
2017-02-16 21:29:30 +00:00
|
|
|
"local": false,
|
2015-03-20 19:48:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp.Data, exp) {
|
|
|
|
t.Fatalf("got: %#v expect: %#v", resp.Data, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_enableAuth(t *testing.T) {
|
2015-03-24 18:37:07 +00:00
|
|
|
c, b, _ := testCoreSystemBackend(t)
|
2015-07-01 00:30:43 +00:00
|
|
|
c.credentialBackends["noop"] = func(*logical.BackendConfig) (logical.Backend, error) {
|
2015-03-31 01:07:05 +00:00
|
|
|
return &NoopBackend{}, nil
|
2015-03-20 19:48:19 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "auth/foo")
|
2015-03-20 19:48:19 +00:00
|
|
|
req.Data["type"] = "noop"
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_enableAuth_invalid(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "auth/foo")
|
2015-03-20 19:48:19 +00:00
|
|
|
req.Data["type"] = "nope"
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Data["error"] != "unknown backend type: nope" {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_disableAuth(t *testing.T) {
|
2015-03-24 18:37:07 +00:00
|
|
|
c, b, _ := testCoreSystemBackend(t)
|
2015-07-01 00:30:43 +00:00
|
|
|
c.credentialBackends["noop"] = func(*logical.BackendConfig) (logical.Backend, error) {
|
2015-03-31 01:07:05 +00:00
|
|
|
return &NoopBackend{}, nil
|
2015-03-20 19:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register the backend
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "auth/foo")
|
2015-03-20 19:48:19 +00:00
|
|
|
req.Data["type"] = "noop"
|
|
|
|
b.HandleRequest(req)
|
|
|
|
|
|
|
|
// Deregister it
|
|
|
|
req = logical.TestRequest(t, logical.DeleteOperation, "auth/foo")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-23 21:43:31 +00:00
|
|
|
func TestSystemBackend_policyList(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
req := logical.TestRequest(t, logical.ReadOperation, "policy")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := map[string]interface{}{
|
2016-07-25 19:59:02 +00:00
|
|
|
"keys": []string{"default", "root"},
|
|
|
|
"policies": []string{"default", "root"},
|
2015-03-23 21:43:31 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp.Data, exp) {
|
|
|
|
t.Fatalf("got: %#v expect: %#v", resp.Data, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_policyCRUD(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
|
|
|
// Create the policy
|
|
|
|
rules := `path "foo/" { policy = "read" }`
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "policy/Foo")
|
2015-03-23 21:43:31 +00:00
|
|
|
req.Data["rules"] = rules
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v %#v", err, resp)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the policy
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "policy/foo")
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := map[string]interface{}{
|
|
|
|
"name": "foo",
|
|
|
|
"rules": rules,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp.Data, exp) {
|
|
|
|
t.Fatalf("got: %#v expect: %#v", resp.Data, exp)
|
|
|
|
}
|
|
|
|
|
2015-10-07 17:52:21 +00:00
|
|
|
// Read, and make sure that case has been normalized
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "policy/Foo")
|
|
|
|
resp, err = b.HandleRequest(req)
|
2017-07-04 17:58:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-07 17:52:21 +00:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("err: expected nil response, got %#v", *resp)
|
|
|
|
}
|
|
|
|
|
2015-03-23 21:43:31 +00:00
|
|
|
// List the policies
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "policy")
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp = map[string]interface{}{
|
2016-07-25 19:59:02 +00:00
|
|
|
"keys": []string{"default", "foo", "root"},
|
|
|
|
"policies": []string{"default", "foo", "root"},
|
2015-03-23 21:43:31 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp.Data, exp) {
|
|
|
|
t.Fatalf("got: %#v expect: %#v", resp.Data, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the policy
|
|
|
|
req = logical.TestRequest(t, logical.DeleteOperation, "policy/foo")
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the policy (deleted)
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "policy/foo")
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the policies (deleted)
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "policy")
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp = map[string]interface{}{
|
2016-07-25 19:59:02 +00:00
|
|
|
"keys": []string{"default", "root"},
|
|
|
|
"policies": []string{"default", "root"},
|
2015-03-23 21:43:31 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp.Data, exp) {
|
|
|
|
t.Fatalf("got: %#v expect: %#v", resp.Data, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:45:00 +00:00
|
|
|
func TestSystemBackend_enableAudit(t *testing.T) {
|
|
|
|
c, b, _ := testCoreSystemBackend(t)
|
2015-09-18 21:36:42 +00:00
|
|
|
c.auditBackends["noop"] = func(config *audit.BackendConfig) (audit.Backend, error) {
|
|
|
|
return &NoopAudit{
|
|
|
|
Config: config,
|
|
|
|
}, nil
|
2015-03-31 23:45:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "audit/foo")
|
2015-03-31 23:45:00 +00:00
|
|
|
req.Data["type"] = "noop"
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 01:26:03 +00:00
|
|
|
func TestSystemBackend_auditHash(t *testing.T) {
|
|
|
|
c, b, _ := testCoreSystemBackend(t)
|
|
|
|
c.auditBackends["noop"] = func(config *audit.BackendConfig) (audit.Backend, error) {
|
|
|
|
view := &logical.InmemStorage{}
|
|
|
|
view.Put(&logical.StorageEntry{
|
|
|
|
Key: "salt",
|
|
|
|
Value: []byte("foo"),
|
|
|
|
})
|
2017-05-24 00:36:20 +00:00
|
|
|
config.SaltView = view
|
|
|
|
config.SaltConfig = &salt.Config{
|
2015-11-19 01:26:03 +00:00
|
|
|
HMAC: sha256.New,
|
|
|
|
HMACType: "hmac-sha256",
|
2017-05-24 00:36:20 +00:00
|
|
|
Location: salt.DefaultLocation,
|
2015-11-19 01:26:03 +00:00
|
|
|
}
|
|
|
|
return &NoopAudit{
|
|
|
|
Config: config,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "audit/foo")
|
2015-11-19 01:26:03 +00:00
|
|
|
req.Data["type"] = "noop"
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, "audit-hash/foo")
|
2015-11-19 01:26:03 +00:00
|
|
|
req.Data["input"] = "bar"
|
|
|
|
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp == nil || resp.Data == nil {
|
|
|
|
t.Fatalf("response or its data was nil")
|
|
|
|
}
|
|
|
|
hash, ok := resp.Data["hash"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("did not get hash back in response, response was %#v", resp.Data)
|
|
|
|
}
|
|
|
|
if hash.(string) != "hmac-sha256:f9320baf0249169e73850cd6156ded0106e2bb6ad8cab01b7bbbebe6d1065317" {
|
|
|
|
t.Fatalf("bad hash back: %s", hash.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:45:00 +00:00
|
|
|
func TestSystemBackend_enableAudit_invalid(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "audit/foo")
|
2015-03-31 23:45:00 +00:00
|
|
|
req.Data["type"] = "nope"
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Data["error"] != "unknown backend type: nope" {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_auditTable(t *testing.T) {
|
|
|
|
c, b, _ := testCoreSystemBackend(t)
|
2015-09-18 21:36:42 +00:00
|
|
|
c.auditBackends["noop"] = func(config *audit.BackendConfig) (audit.Backend, error) {
|
|
|
|
return &NoopAudit{
|
|
|
|
Config: config,
|
|
|
|
}, nil
|
2015-03-31 23:45:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "audit/foo")
|
2015-03-31 23:45:00 +00:00
|
|
|
req.Data["type"] = "noop"
|
|
|
|
req.Data["description"] = "testing"
|
|
|
|
req.Data["options"] = map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
2017-02-16 21:29:30 +00:00
|
|
|
req.Data["local"] = true
|
2015-03-31 23:45:00 +00:00
|
|
|
b.HandleRequest(req)
|
|
|
|
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "audit")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := map[string]interface{}{
|
2015-04-03 21:27:33 +00:00
|
|
|
"foo/": map[string]interface{}{
|
2016-03-14 22:40:12 +00:00
|
|
|
"path": "foo/",
|
2015-03-31 23:45:00 +00:00
|
|
|
"type": "noop",
|
|
|
|
"description": "testing",
|
|
|
|
"options": map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
2017-02-16 21:29:30 +00:00
|
|
|
"local": true,
|
2015-03-31 23:45:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp.Data, exp) {
|
|
|
|
t.Fatalf("got: %#v expect: %#v", resp.Data, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_disableAudit(t *testing.T) {
|
|
|
|
c, b, _ := testCoreSystemBackend(t)
|
2015-09-18 21:36:42 +00:00
|
|
|
c.auditBackends["noop"] = func(config *audit.BackendConfig) (audit.Backend, error) {
|
|
|
|
return &NoopAudit{
|
|
|
|
Config: config,
|
|
|
|
}, nil
|
2015-03-31 23:45:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "audit/foo")
|
2015-03-31 23:45:00 +00:00
|
|
|
req.Data["type"] = "noop"
|
|
|
|
req.Data["description"] = "testing"
|
|
|
|
req.Data["options"] = map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
b.HandleRequest(req)
|
|
|
|
|
|
|
|
// Deregister it
|
|
|
|
req = logical.TestRequest(t, logical.DeleteOperation, "audit/foo")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 17:24:41 +00:00
|
|
|
func TestSystemBackend_rawRead_Protected(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
|
|
|
req := logical.TestRequest(t, logical.ReadOperation, "raw/"+keyringPath)
|
|
|
|
_, err := b.HandleRequest(req)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_rawWrite_Protected(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "raw/"+keyringPath)
|
2015-05-28 17:24:41 +00:00
|
|
|
_, err := b.HandleRequest(req)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 20:26:26 +00:00
|
|
|
func TestSystemBackend_rawReadWrite(t *testing.T) {
|
2015-04-02 00:44:43 +00:00
|
|
|
c, b, _ := testCoreSystemBackend(t)
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "raw/sys/policy/test")
|
2015-04-02 00:44:43 +00:00
|
|
|
req.Data["value"] = `path "secret/" { policy = "read" }`
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
2016-04-19 20:26:26 +00:00
|
|
|
// Read via raw API
|
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "raw/sys/policy/test")
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(resp.Data["value"].(string), "path") {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
2015-04-02 00:44:43 +00:00
|
|
|
// Read the policy!
|
2015-11-06 16:52:26 +00:00
|
|
|
p, err := c.policyStore.GetPolicy("test")
|
2015-04-02 00:44:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if p == nil || len(p.Paths) == 0 {
|
|
|
|
t.Fatalf("missing policy %#v", p)
|
|
|
|
}
|
2016-01-07 20:10:05 +00:00
|
|
|
if p.Paths[0].Prefix != "secret/" || p.Paths[0].Policy != ReadCapability {
|
2015-04-02 00:44:43 +00:00
|
|
|
t.Fatalf("Bad: %#v", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 17:24:41 +00:00
|
|
|
func TestSystemBackend_rawDelete_Protected(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
|
|
|
req := logical.TestRequest(t, logical.DeleteOperation, "raw/"+keyringPath)
|
|
|
|
_, err := b.HandleRequest(req)
|
|
|
|
if err != logical.ErrInvalidRequest {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 00:44:43 +00:00
|
|
|
func TestSystemBackend_rawDelete(t *testing.T) {
|
|
|
|
c, b, _ := testCoreSystemBackend(t)
|
|
|
|
|
|
|
|
// set the policy!
|
|
|
|
p := &Policy{Name: "test"}
|
2015-11-06 16:52:26 +00:00
|
|
|
err := c.policyStore.SetPolicy(p)
|
2015-04-02 00:44:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the policy
|
|
|
|
req := logical.TestRequest(t, logical.DeleteOperation, "raw/sys/policy/test")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policy should be gone
|
2015-11-06 16:52:26 +00:00
|
|
|
c.policyStore.lru.Purge()
|
|
|
|
out, err := c.policyStore.GetPolicy("test")
|
2015-04-02 00:44:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("policy should be gone")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 00:53:42 +00:00
|
|
|
func TestSystemBackend_keyStatus(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
req := logical.TestRequest(t, logical.ReadOperation, "key-status")
|
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := map[string]interface{}{
|
|
|
|
"term": 1,
|
|
|
|
}
|
|
|
|
delete(resp.Data, "install_time")
|
|
|
|
if !reflect.DeepEqual(resp.Data, exp) {
|
|
|
|
t.Fatalf("got: %#v expect: %#v", resp.Data, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemBackend_rotate(t *testing.T) {
|
|
|
|
b := testSystemBackend(t)
|
|
|
|
|
2016-01-07 15:30:47 +00:00
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "rotate")
|
2015-05-28 00:53:42 +00:00
|
|
|
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, "key-status")
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := map[string]interface{}{
|
|
|
|
"term": 2,
|
|
|
|
}
|
|
|
|
delete(resp.Data, "install_time")
|
|
|
|
if !reflect.DeepEqual(resp.Data, exp) {
|
|
|
|
t.Fatalf("got: %#v expect: %#v", resp.Data, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 00:35:59 +00:00
|
|
|
func testSystemBackend(t *testing.T) logical.Backend {
|
2015-03-29 23:18:08 +00:00
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
2015-09-04 20:58:12 +00:00
|
|
|
bc := &logical.BackendConfig{
|
|
|
|
Logger: c.logger,
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
2016-09-29 04:01:28 +00:00
|
|
|
|
2017-06-17 05:26:25 +00:00
|
|
|
b := NewSystemBackend(c)
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
err := b.Backend.Setup(bc)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
2015-03-15 21:42:05 +00:00
|
|
|
}
|
2015-03-16 23:11:55 +00:00
|
|
|
|
2015-03-24 18:37:07 +00:00
|
|
|
func testCoreSystemBackend(t *testing.T) (*Core, logical.Backend, string) {
|
2015-03-29 23:18:08 +00:00
|
|
|
c, _, root := TestCoreUnsealed(t)
|
2015-09-04 20:58:12 +00:00
|
|
|
bc := &logical.BackendConfig{
|
|
|
|
Logger: c.logger,
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
2016-09-29 04:01:28 +00:00
|
|
|
|
2017-06-17 05:26:25 +00:00
|
|
|
b := NewSystemBackend(c)
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
err := b.Backend.Setup(bc)
|
2016-09-29 04:01:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return c, b, root
|
2015-03-16 23:11:55 +00:00
|
|
|
}
|
2017-04-12 17:39:18 +00:00
|
|
|
|
|
|
|
func TestSystemBackend_PluginCatalog_CRUD(t *testing.T) {
|
|
|
|
c, b, _ := testCoreSystemBackend(t)
|
|
|
|
// Bootstrap the pluginCatalog
|
|
|
|
sym, err := filepath.EvalSymlinks(os.TempDir())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %v", err)
|
|
|
|
}
|
|
|
|
c.pluginCatalog.directory = sym
|
|
|
|
|
2017-04-24 20:48:46 +00:00
|
|
|
req := logical.TestRequest(t, logical.ListOperation, "plugins/catalog/")
|
2017-04-12 17:39:18 +00:00
|
|
|
resp, err := b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-04-24 17:30:33 +00:00
|
|
|
if len(resp.Data["keys"].([]string)) != len(builtinplugins.Keys()) {
|
|
|
|
t.Fatalf("Wrong number of plugins, got %d, expected %d", len(resp.Data["keys"].([]string)), len(builtinplugins.Keys()))
|
2017-04-12 17:39:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 20:48:46 +00:00
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "plugins/catalog/mysql-database-plugin")
|
2017-04-12 17:39:18 +00:00
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
actualRespData := resp.Data
|
2017-04-12 17:39:18 +00:00
|
|
|
|
|
|
|
expectedBuiltin := &pluginutil.PluginRunner{
|
|
|
|
Name: "mysql-database-plugin",
|
|
|
|
Builtin: true,
|
|
|
|
}
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
expectedRespData := structs.New(expectedBuiltin).Map()
|
2017-04-12 17:39:18 +00:00
|
|
|
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
if !reflect.DeepEqual(actualRespData, expectedRespData) {
|
|
|
|
t.Fatalf("expected did not match actual, got %#v\n expected %#v\n", actualRespData, expectedRespData)
|
2017-04-12 17:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set a plugin
|
|
|
|
file, err := ioutil.TempFile(os.TempDir(), "temp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
command := fmt.Sprintf("%s --test", filepath.Base(file.Name()))
|
2017-04-24 20:48:46 +00:00
|
|
|
req = logical.TestRequest(t, logical.UpdateOperation, "plugins/catalog/test-plugin")
|
2017-04-12 17:39:18 +00:00
|
|
|
req.Data["sha_256"] = hex.EncodeToString([]byte{'1'})
|
|
|
|
req.Data["command"] = command
|
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-04-24 20:48:46 +00:00
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "plugins/catalog/test-plugin")
|
2017-04-12 17:39:18 +00:00
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
actual := resp.Data
|
2017-04-12 17:39:18 +00:00
|
|
|
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
expectedRunner := &pluginutil.PluginRunner{
|
2017-04-12 17:39:18 +00:00
|
|
|
Name: "test-plugin",
|
|
|
|
Command: filepath.Join(sym, filepath.Base(file.Name())),
|
|
|
|
Args: []string{"--test"},
|
|
|
|
Sha256: []byte{'1'},
|
|
|
|
Builtin: false,
|
|
|
|
}
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
expected := structs.New(expectedRunner).Map()
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("expected did not match actual, got %#v\n expected %#v\n", actual, expected)
|
2017-04-12 17:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete plugin
|
2017-04-24 20:48:46 +00:00
|
|
|
req = logical.TestRequest(t, logical.DeleteOperation, "plugins/catalog/test-plugin")
|
2017-04-12 17:39:18 +00:00
|
|
|
resp, err = b.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-04-24 20:48:46 +00:00
|
|
|
req = logical.TestRequest(t, logical.ReadOperation, "plugins/catalog/test-plugin")
|
2017-04-12 17:39:18 +00:00
|
|
|
resp, err = b.HandleRequest(req)
|
2017-04-25 04:24:19 +00:00
|
|
|
if resp != nil || err != nil {
|
|
|
|
t.Fatalf("expected nil response, plugin not deleted correctly got resp: %v, err: %v", resp, err)
|
2017-04-12 17:39:18 +00:00
|
|
|
}
|
|
|
|
}
|