2015-03-11 22:33:25 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2016-05-26 16:55:00 +00:00
|
|
|
"encoding/json"
|
2015-03-11 22:33:25 +00:00
|
|
|
"reflect"
|
2016-08-09 07:43:03 +00:00
|
|
|
"strings"
|
2015-03-11 22:33:25 +00:00
|
|
|
"testing"
|
2015-04-02 18:47:44 +00:00
|
|
|
"time"
|
|
|
|
|
2016-05-26 17:38:51 +00:00
|
|
|
"github.com/hashicorp/vault/audit"
|
2016-08-09 07:43:03 +00:00
|
|
|
"github.com/hashicorp/vault/helper/compressutil"
|
2015-04-02 18:47:44 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2015-03-11 22:33:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCore_DefaultMountTable(t *testing.T) {
|
2015-03-29 23:18:08 +00:00
|
|
|
c, key, _ := TestCoreUnsealed(t)
|
2015-03-11 22:33:25 +00:00
|
|
|
verifyDefaultTable(t, c.mounts)
|
|
|
|
|
|
|
|
// Start a second core with same physical
|
2015-04-29 01:12:57 +00:00
|
|
|
conf := &CoreConfig{
|
|
|
|
Physical: c.physical,
|
|
|
|
DisableMlock: true,
|
|
|
|
}
|
2015-03-11 22:33:25 +00:00
|
|
|
c2, err := NewCore(conf)
|
|
|
|
if err != nil {
|
2015-03-12 01:29:49 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
unseal, err := TestCoreUnseal(c2, key)
|
2015-03-12 01:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !unseal {
|
|
|
|
t.Fatalf("should be unsealed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify matching mount tables
|
|
|
|
if !reflect.DeepEqual(c.mounts, c2.mounts) {
|
|
|
|
t.Fatalf("mismatch: %v %v", c.mounts, c2.mounts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
vault: make Mount related core functions public
/cc @armon - So I know the conversation we had related to this about
auth, but I think we still need to export these and do auth only at the
external API layer. If you're writing to the internal API, then all bets
are off.
The reason is simply that if you have access to the code, you can
already work around it anyways (you can disable auth or w/e), so a
compromised Vault source/binary is already a failure, and that is the
only thing that our previous unexported methods were protecting against.
If you write an external tool to access a Vault, it still needs to be
unsealed so _that_ is the primary security mechanism from an API
perspective. Once it is unsealed then the core API has full access to
the Vault, and identity/auth is only done at the external API layer, not
at the internal API layer.
The benefits of this approach is that it lets us still treat the "sys"
mount specially but at least have sys adopt helper/backend and use that
machinery and it can still be the only backend which actually has a
reference to *vault.Core to do core things (a key difference). So, an
AWS backend still will never be able to muck with things it can't, but
we're explicitly giving Sys (via struct initialization in Go itself)
a reference to *vault.Core.
2015-03-15 00:26:59 +00:00
|
|
|
func TestCore_Mount(t *testing.T) {
|
2015-03-29 23:18:08 +00:00
|
|
|
c, key, _ := TestCoreUnsealed(t)
|
2015-03-12 01:29:49 +00:00
|
|
|
me := &MountEntry{
|
2016-05-26 16:55:00 +00:00
|
|
|
Table: mountTableType,
|
|
|
|
Path: "foo",
|
|
|
|
Type: "generic",
|
2015-03-12 01:29:49 +00:00
|
|
|
}
|
2015-03-15 01:31:31 +00:00
|
|
|
err := c.mount(me)
|
2015-03-12 01:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
match := c.router.MatchingMount("foo/bar")
|
|
|
|
if match != "foo/" {
|
|
|
|
t.Fatalf("missing mount")
|
|
|
|
}
|
|
|
|
|
2015-04-29 01:12:57 +00:00
|
|
|
conf := &CoreConfig{
|
|
|
|
Physical: c.physical,
|
|
|
|
DisableMlock: true,
|
|
|
|
}
|
2015-03-12 01:29:49 +00:00
|
|
|
c2, err := NewCore(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
unseal, err := TestCoreUnseal(c2, key)
|
2015-03-12 01:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !unseal {
|
|
|
|
t.Fatalf("should be unsealed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify matching mount tables
|
|
|
|
if !reflect.DeepEqual(c.mounts, c2.mounts) {
|
|
|
|
t.Fatalf("mismatch: %v %v", c.mounts, c2.mounts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
vault: make Mount related core functions public
/cc @armon - So I know the conversation we had related to this about
auth, but I think we still need to export these and do auth only at the
external API layer. If you're writing to the internal API, then all bets
are off.
The reason is simply that if you have access to the code, you can
already work around it anyways (you can disable auth or w/e), so a
compromised Vault source/binary is already a failure, and that is the
only thing that our previous unexported methods were protecting against.
If you write an external tool to access a Vault, it still needs to be
unsealed so _that_ is the primary security mechanism from an API
perspective. Once it is unsealed then the core API has full access to
the Vault, and identity/auth is only done at the external API layer, not
at the internal API layer.
The benefits of this approach is that it lets us still treat the "sys"
mount specially but at least have sys adopt helper/backend and use that
machinery and it can still be the only backend which actually has a
reference to *vault.Core to do core things (a key difference). So, an
AWS backend still will never be able to muck with things it can't, but
we're explicitly giving Sys (via struct initialization in Go itself)
a reference to *vault.Core.
2015-03-15 00:26:59 +00:00
|
|
|
func TestCore_Unmount(t *testing.T) {
|
2015-03-29 23:18:08 +00:00
|
|
|
c, key, _ := TestCoreUnsealed(t)
|
2016-09-19 17:02:25 +00:00
|
|
|
existed, err := c.unmount("secret")
|
|
|
|
if !existed || err != nil {
|
|
|
|
t.Fatalf("existed: %v; err: %v", existed, err)
|
2015-03-12 01:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match := c.router.MatchingMount("secret/foo")
|
|
|
|
if match != "" {
|
|
|
|
t.Fatalf("backend present")
|
|
|
|
}
|
|
|
|
|
2015-04-29 01:12:57 +00:00
|
|
|
conf := &CoreConfig{
|
|
|
|
Physical: c.physical,
|
|
|
|
DisableMlock: true,
|
|
|
|
}
|
2015-03-12 01:29:49 +00:00
|
|
|
c2, err := NewCore(conf)
|
|
|
|
if err != nil {
|
2015-03-11 22:33:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
unseal, err := TestCoreUnseal(c2, key)
|
2015-03-11 22:33:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !unseal {
|
|
|
|
t.Fatalf("should be unsealed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify matching mount tables
|
|
|
|
if !reflect.DeepEqual(c.mounts, c2.mounts) {
|
|
|
|
t.Fatalf("mismatch: %v %v", c.mounts, c2.mounts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 18:47:44 +00:00
|
|
|
func TestCore_Unmount_Cleanup(t *testing.T) {
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
c, _, root := TestCoreUnsealed(t)
|
2015-07-01 00:30:43 +00:00
|
|
|
c.logicalBackends["noop"] = func(*logical.BackendConfig) (logical.Backend, error) {
|
2015-04-02 18:47:44 +00:00
|
|
|
return noop, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mount the noop backend
|
|
|
|
me := &MountEntry{
|
2016-05-26 16:55:00 +00:00
|
|
|
Table: mountTableType,
|
|
|
|
Path: "test/",
|
|
|
|
Type: "noop",
|
2015-04-02 18:47:44 +00:00
|
|
|
}
|
|
|
|
if err := c.mount(me); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the view
|
2015-09-15 16:27:22 +00:00
|
|
|
view := c.router.MatchingStorageView("test/")
|
2015-04-02 18:47:44 +00:00
|
|
|
|
|
|
|
// Inject data
|
|
|
|
se := &logical.StorageEntry{
|
|
|
|
Key: "plstodelete",
|
|
|
|
Value: []byte("test"),
|
|
|
|
}
|
|
|
|
if err := view.Put(se); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup response
|
|
|
|
resp := &logical.Response{
|
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Hour,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-04-02 18:47:44 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
noop.Response = resp
|
|
|
|
|
|
|
|
// Generate leased secret
|
|
|
|
r := &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "test/foo",
|
|
|
|
ClientToken: root,
|
|
|
|
}
|
|
|
|
resp, err := c.HandleRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-04-08 20:35:32 +00:00
|
|
|
if resp.Secret.LeaseID == "" {
|
2015-04-02 18:47:44 +00:00
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmount, this should cleanup
|
2016-09-19 17:02:25 +00:00
|
|
|
if existed, err := c.unmount("test/"); !existed || err != nil {
|
|
|
|
t.Fatalf("existed: %v; err: %v", existed, err)
|
2015-04-02 18:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback should be invoked
|
|
|
|
if noop.Requests[1].Operation != logical.RollbackOperation {
|
|
|
|
t.Fatalf("bad: %#v", noop.Requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke should be invoked
|
|
|
|
if noop.Requests[2].Operation != logical.RevokeOperation {
|
|
|
|
t.Fatalf("bad: %#v", noop.Requests)
|
|
|
|
}
|
|
|
|
if noop.Requests[2].Path != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", noop.Requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
// View should be empty
|
2017-01-06 20:42:18 +00:00
|
|
|
out, err := logical.CollectKeys(view)
|
2015-04-02 18:47:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
vault: make Mount related core functions public
/cc @armon - So I know the conversation we had related to this about
auth, but I think we still need to export these and do auth only at the
external API layer. If you're writing to the internal API, then all bets
are off.
The reason is simply that if you have access to the code, you can
already work around it anyways (you can disable auth or w/e), so a
compromised Vault source/binary is already a failure, and that is the
only thing that our previous unexported methods were protecting against.
If you write an external tool to access a Vault, it still needs to be
unsealed so _that_ is the primary security mechanism from an API
perspective. Once it is unsealed then the core API has full access to
the Vault, and identity/auth is only done at the external API layer, not
at the internal API layer.
The benefits of this approach is that it lets us still treat the "sys"
mount specially but at least have sys adopt helper/backend and use that
machinery and it can still be the only backend which actually has a
reference to *vault.Core to do core things (a key difference). So, an
AWS backend still will never be able to muck with things it can't, but
we're explicitly giving Sys (via struct initialization in Go itself)
a reference to *vault.Core.
2015-03-15 00:26:59 +00:00
|
|
|
func TestCore_Remount(t *testing.T) {
|
2015-03-29 23:18:08 +00:00
|
|
|
c, key, _ := TestCoreUnsealed(t)
|
2015-03-15 01:31:31 +00:00
|
|
|
err := c.remount("secret", "foo")
|
2015-03-12 19:09:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
match := c.router.MatchingMount("foo/bar")
|
|
|
|
if match != "foo/" {
|
|
|
|
t.Fatalf("failed remount")
|
|
|
|
}
|
|
|
|
|
2015-04-29 01:12:57 +00:00
|
|
|
conf := &CoreConfig{
|
|
|
|
Physical: c.physical,
|
|
|
|
DisableMlock: true,
|
|
|
|
}
|
2015-03-12 19:09:30 +00:00
|
|
|
c2, err := NewCore(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-08-15 13:42:42 +00:00
|
|
|
unseal, err := TestCoreUnseal(c2, key)
|
2015-03-12 19:09:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !unseal {
|
|
|
|
t.Fatalf("should be unsealed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify matching mount tables
|
|
|
|
if !reflect.DeepEqual(c.mounts, c2.mounts) {
|
|
|
|
t.Fatalf("mismatch: %v %v", c.mounts, c2.mounts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 19:04:37 +00:00
|
|
|
func TestCore_Remount_Cleanup(t *testing.T) {
|
|
|
|
noop := &NoopBackend{}
|
|
|
|
c, _, root := TestCoreUnsealed(t)
|
2015-07-01 00:30:43 +00:00
|
|
|
c.logicalBackends["noop"] = func(*logical.BackendConfig) (logical.Backend, error) {
|
2015-04-02 19:04:37 +00:00
|
|
|
return noop, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mount the noop backend
|
|
|
|
me := &MountEntry{
|
2016-05-26 16:55:00 +00:00
|
|
|
Table: mountTableType,
|
|
|
|
Path: "test/",
|
|
|
|
Type: "noop",
|
2015-04-02 19:04:37 +00:00
|
|
|
}
|
|
|
|
if err := c.mount(me); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the view
|
2015-09-15 16:27:22 +00:00
|
|
|
view := c.router.MatchingStorageView("test/")
|
2015-04-02 19:04:37 +00:00
|
|
|
|
|
|
|
// Inject data
|
|
|
|
se := &logical.StorageEntry{
|
|
|
|
Key: "plstokeep",
|
|
|
|
Value: []byte("test"),
|
|
|
|
}
|
|
|
|
if err := view.Put(se); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup response
|
|
|
|
resp := &logical.Response{
|
|
|
|
Secret: &logical.Secret{
|
2015-04-09 19:14:04 +00:00
|
|
|
LeaseOptions: logical.LeaseOptions{
|
2015-08-21 00:47:17 +00:00
|
|
|
TTL: time.Hour,
|
2015-04-09 19:14:04 +00:00
|
|
|
},
|
2015-04-02 19:04:37 +00:00
|
|
|
},
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
noop.Response = resp
|
|
|
|
|
|
|
|
// Generate leased secret
|
|
|
|
r := &logical.Request{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "test/foo",
|
|
|
|
ClientToken: root,
|
|
|
|
}
|
|
|
|
resp, err := c.HandleRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-04-08 20:35:32 +00:00
|
|
|
if resp.Secret.LeaseID == "" {
|
2015-04-02 19:04:37 +00:00
|
|
|
t.Fatalf("bad: %#v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remount, this should cleanup
|
|
|
|
if err := c.remount("test/", "new/"); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback should be invoked
|
|
|
|
if noop.Requests[1].Operation != logical.RollbackOperation {
|
|
|
|
t.Fatalf("bad: %#v", noop.Requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke should be invoked
|
|
|
|
if noop.Requests[2].Operation != logical.RevokeOperation {
|
|
|
|
t.Fatalf("bad: %#v", noop.Requests)
|
|
|
|
}
|
|
|
|
if noop.Requests[2].Path != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", noop.Requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
// View should not be empty
|
2017-01-06 20:42:18 +00:00
|
|
|
out, err := logical.CollectKeys(view)
|
2015-04-02 19:04:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out) != 1 && out[0] != "plstokeep" {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
vault: make Mount related core functions public
/cc @armon - So I know the conversation we had related to this about
auth, but I think we still need to export these and do auth only at the
external API layer. If you're writing to the internal API, then all bets
are off.
The reason is simply that if you have access to the code, you can
already work around it anyways (you can disable auth or w/e), so a
compromised Vault source/binary is already a failure, and that is the
only thing that our previous unexported methods were protecting against.
If you write an external tool to access a Vault, it still needs to be
unsealed so _that_ is the primary security mechanism from an API
perspective. Once it is unsealed then the core API has full access to
the Vault, and identity/auth is only done at the external API layer, not
at the internal API layer.
The benefits of this approach is that it lets us still treat the "sys"
mount specially but at least have sys adopt helper/backend and use that
machinery and it can still be the only backend which actually has a
reference to *vault.Core to do core things (a key difference). So, an
AWS backend still will never be able to muck with things it can't, but
we're explicitly giving Sys (via struct initialization in Go itself)
a reference to *vault.Core.
2015-03-15 00:26:59 +00:00
|
|
|
func TestCore_Remount_Protected(t *testing.T) {
|
2015-03-29 23:18:08 +00:00
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
2015-03-15 01:31:31 +00:00
|
|
|
err := c.remount("sys", "foo")
|
2015-03-12 19:09:30 +00:00
|
|
|
if err.Error() != "cannot remount 'sys/'" {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 22:33:25 +00:00
|
|
|
func TestDefaultMountTable(t *testing.T) {
|
|
|
|
table := defaultMountTable()
|
|
|
|
verifyDefaultTable(t, table)
|
|
|
|
}
|
|
|
|
|
2016-05-26 16:55:00 +00:00
|
|
|
func TestCore_MountTable_UpgradeToTyped(t *testing.T) {
|
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
|
|
|
|
2016-05-26 17:38:51 +00:00
|
|
|
c.auditBackends["noop"] = func(config *audit.BackendConfig) (audit.Backend, error) {
|
|
|
|
return &NoopAudit{
|
|
|
|
Config: config,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
me := &MountEntry{
|
|
|
|
Table: auditTableType,
|
|
|
|
Path: "foo",
|
|
|
|
Type: "noop",
|
|
|
|
}
|
|
|
|
err := c.enableAudit(me)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.credentialBackends["noop"] = func(*logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
return &NoopBackend{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
me = &MountEntry{
|
|
|
|
Table: credentialTableType,
|
|
|
|
Path: "foo",
|
|
|
|
Type: "noop",
|
|
|
|
}
|
|
|
|
err = c.enableCredential(me)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testCore_MountTable_UpgradeToTyped_Common(t, c, "mounts")
|
|
|
|
testCore_MountTable_UpgradeToTyped_Common(t, c, "audits")
|
|
|
|
testCore_MountTable_UpgradeToTyped_Common(t, c, "credentials")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCore_MountTable_UpgradeToTyped_Common(
|
|
|
|
t *testing.T,
|
|
|
|
c *Core,
|
|
|
|
testType string) {
|
|
|
|
|
|
|
|
var path string
|
|
|
|
var mt *MountTable
|
|
|
|
switch testType {
|
|
|
|
case "mounts":
|
|
|
|
path = coreMountConfigPath
|
|
|
|
mt = c.mounts
|
|
|
|
case "audits":
|
|
|
|
path = coreAuditConfigPath
|
|
|
|
mt = c.audit
|
|
|
|
case "credentials":
|
|
|
|
path = coreAuthConfigPath
|
|
|
|
mt = c.auth
|
|
|
|
}
|
|
|
|
|
2016-05-26 16:55:00 +00:00
|
|
|
// Save the expected table
|
2016-05-26 17:38:51 +00:00
|
|
|
goodJson, err := json.Marshal(mt)
|
2016-05-26 16:55:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a pre-typed version
|
2016-05-26 17:38:51 +00:00
|
|
|
mt.Type = ""
|
|
|
|
for _, entry := range mt.Entries {
|
2016-05-26 16:55:00 +00:00
|
|
|
entry.Table = ""
|
|
|
|
}
|
|
|
|
|
2016-05-26 17:38:51 +00:00
|
|
|
raw, err := json.Marshal(mt)
|
2016-05-26 16:55:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.DeepEqual(raw, goodJson) {
|
|
|
|
t.Fatalf("bad: values here should be different")
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &Entry{
|
2016-05-26 17:38:51 +00:00
|
|
|
Key: path,
|
2016-05-26 16:55:00 +00:00
|
|
|
Value: raw,
|
|
|
|
}
|
|
|
|
if err := c.barrier.Put(entry); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-05-26 17:38:51 +00:00
|
|
|
var persistFunc func(*MountTable) error
|
|
|
|
|
2016-05-26 16:55:00 +00:00
|
|
|
// It should load successfully and be upgraded and persisted
|
2016-05-26 17:38:51 +00:00
|
|
|
switch testType {
|
|
|
|
case "mounts":
|
|
|
|
err = c.loadMounts()
|
|
|
|
persistFunc = c.persistMounts
|
|
|
|
mt = c.mounts
|
|
|
|
case "credentials":
|
|
|
|
err = c.loadCredentials()
|
|
|
|
persistFunc = c.persistAuth
|
|
|
|
mt = c.auth
|
|
|
|
case "audits":
|
|
|
|
err = c.loadAudits()
|
|
|
|
persistFunc = c.persistAudit
|
|
|
|
mt = c.audit
|
|
|
|
}
|
2016-05-26 16:55:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-05-26 17:38:51 +00:00
|
|
|
entry, err = c.barrier.Get(path)
|
2016-05-26 16:55:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-09 16:06:59 +00:00
|
|
|
decompressedBytes, uncompressed, err := compressutil.Decompress(entry.Value)
|
2016-08-09 07:43:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := decompressedBytes
|
|
|
|
if uncompressed {
|
|
|
|
actual = entry.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(string(actual)) != strings.TrimSpace(string(goodJson)) {
|
|
|
|
t.Fatalf("bad: expected\n%s\nactual\n%s\n", string(goodJson), string(actual))
|
2016-05-26 16:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now try saving invalid versions
|
2016-05-26 17:38:51 +00:00
|
|
|
origTableType := mt.Type
|
|
|
|
mt.Type = "foo"
|
|
|
|
if err := persistFunc(mt); err == nil {
|
2016-05-26 16:55:00 +00:00
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
2016-05-26 17:38:51 +00:00
|
|
|
if len(mt.Entries) > 0 {
|
|
|
|
mt.Type = origTableType
|
|
|
|
mt.Entries[0].Table = "bar"
|
|
|
|
if err := persistFunc(mt); err == nil {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
2016-05-26 16:55:00 +00:00
|
|
|
|
2016-05-26 17:38:51 +00:00
|
|
|
mt.Entries[0].Table = mt.Type
|
|
|
|
if err := persistFunc(mt); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-26 16:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 22:33:25 +00:00
|
|
|
func verifyDefaultTable(t *testing.T, table *MountTable) {
|
2015-09-10 01:58:09 +00:00
|
|
|
if len(table.Entries) != 3 {
|
2015-03-11 22:33:25 +00:00
|
|
|
t.Fatalf("bad: %v", table.Entries)
|
|
|
|
}
|
|
|
|
for idx, entry := range table.Entries {
|
|
|
|
switch idx {
|
|
|
|
case 0:
|
|
|
|
if entry.Path != "secret/" {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
if entry.Type != "generic" {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
case 1:
|
2015-09-10 01:58:09 +00:00
|
|
|
if entry.Path != "cubbyhole/" {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
if entry.Type != "cubbyhole" {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
case 2:
|
2015-03-11 22:33:25 +00:00
|
|
|
if entry.Path != "sys/" {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
if entry.Type != "system" {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 16:55:00 +00:00
|
|
|
if entry.Table != mountTableType {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
2015-03-11 22:33:25 +00:00
|
|
|
if entry.Description == "" {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
if entry.UUID == "" {
|
|
|
|
t.Fatalf("bad: %v", entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|